tru64-50-initial-support-20010210
[openafs.git] / src / afs / DUX / osi_vfsops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * osi_vfsops.c for DUX
12  */
13 #include "../afs/param.h"       /* Should be always first */
14 #include "../afs/sysincludes.h" /* Standard vendor system headers */
15 #include "../afs/afsincludes.h" /* Afs-based standard headers */
16 #include "../afs/afs_stats.h"   /* statistics stuff */
17 #include <sys/types.h>
18 #include <kern/mach_param.h>
19 #include <sys/sysconfig.h>
20 #include <sys/systm.h>
21 #include <sys/resource.h>
22 #include <sys/errno.h>
23 #include <sys/conf.h>
24 #include <machine/machlimits.h>
25
26
27 struct vcache *afs_globalVp = 0;
28 struct mount *afs_globalVFS = 0;
29
30 static u_char afs_mntid;
31 int afs_vfsdev = 0;
32 udecl_simple_lock_data(, afsmntid_lock)
33 #define AFSMNTID_LOCK()         usimple_lock(&afsmntid_lock)
34 #define AFSMNTID_UNLOCK()       usimple_unlock(&afsmntid_lock)
35 #define AFSMNTID_LOCK_INIT()    usimple_lock_init(&afsmntid_lock)
36
37
38 int mp_afs_mount(struct mount *afsp,char * path, caddr_t data,
39                  struct nameidata *ndp)
40 {
41     u_int size;
42
43     fsid_t tfsid;
44     struct mount *xmp, *getvfs();
45     int code;
46
47     AFS_GLOCK();
48     AFS_STATCNT(afs_mount);
49
50     if (afs_globalVFS) { /* Don't allow remounts. */
51         AFS_GUNLOCK();
52         return (EBUSY);
53     }
54
55     afs_globalVFS = afsp;
56     afsp->vfs_bsize = 8192;
57 /*
58  * Generate a unique afs mount i.d. ( see nfs_mount() ).
59  */
60     afsp->m_stat.f_fsid.val[0] = makedev(130, 0);
61     afsp->m_stat.f_fsid.val[1] = MOUNT_AFS;
62     AFSMNTID_LOCK();
63     if (++afs_mntid == 0)
64         ++afs_mntid;
65     AFSMNTID_UNLOCK();
66     BM(AFSMNTID_LOCK());
67     tfsid.val[0] = makedev(130, afs_mntid);
68     tfsid.val[1] = MOUNT_AFS;
69     BM(AFSMNTID_UNLOCK());
70
71     while (xmp = getvfs(&tfsid)) {
72         UNMOUNT_READ_UNLOCK(xmp);
73         tfsid.val[0]++;
74         AFSMNTID_LOCK();
75         afs_mntid++;
76         AFSMNTID_UNLOCK();
77     }
78     if (major(tfsid.val[0]) != 130) {
79         AFS_GUNLOCK();
80         return (ENOENT);
81     }
82     afsp->m_stat.f_fsid.val[0] = tfsid.val[0];
83
84     afsp->m_stat.f_mntonname = AFS_KALLOC(MNAMELEN);
85     afsp->m_stat.f_mntfromname = AFS_KALLOC(MNAMELEN); 
86     if ( !afsp->m_stat.f_mntonname || !afsp->m_stat.f_mntfromname)
87         panic("malloc failure in afs_mount\n");
88
89     bzero(afsp->m_stat.f_mntonname, MNAMELEN);
90     bzero(afsp->m_stat.f_mntfromname, MNAMELEN);
91     AFS_COPYINSTR(path, (caddr_t)afsp->m_stat.f_mntonname, MNAMELEN, &size, code);
92     bcopy("AFS", afsp->m_stat.f_mntfromname, 4);
93     AFS_GUNLOCK();
94     (void) mp_afs_statfs(afsp);
95     AFS_GLOCK();
96     afs_vfsdev = afsp->m_stat.f_fsid.val[0];
97
98 #ifndef AFS_NONFSTRANS
99     /* Set up the xlator in case it wasn't done elsewhere */
100     afs_xlatorinit_v2();
101     afs_xlatorinit_v3();
102 #endif
103     AFS_GUNLOCK();
104     return 0;
105 }
106
107
108 int mp_afs_unmount (struct mount *afsp, int flag)
109 {
110     AFS_GLOCK();
111     AFS_STATCNT(afs_unmount);
112     afs_globalVFS = 0;
113     afs_shutdown();
114     AFS_GUNLOCK();
115     return 0;
116 }
117
118
119 int mp_afs_start(struct mount *mp, int flags)
120 {
121     return(0);
122 }
123
124 int mp_afs_root (struct mount *afsp, struct vnode **avpp)
125 {
126     register afs_int32 code = 0;
127     struct vrequest treq;
128     register struct vcache *tvp=0;
129
130     AFS_GLOCK();
131     AFS_STATCNT(afs_root);
132     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
133         tvp = afs_globalVp;
134     } else {
135         if (!(code = afs_InitReq(&treq, u.u_cred)) &&
136             !(code = afs_CheckInit())) {
137             tvp = afs_GetVCache(&afs_rootFid, &treq, (afs_int32 *)0,
138                                 (struct vcache*)0, WRITE_LOCK);
139             /* we really want this to stay around */
140             if (tvp) {
141                 afs_globalVp = tvp;
142             } else
143                 code = ENOENT;
144         }
145     }
146     if (tvp) {
147         AFS_GUNLOCK();
148         VN_HOLD((struct vnode *)tvp);
149         VN_LOCK((struct vnode *)tvp);
150         tvp->v.v_flag |= VROOT;     /* No-op on Ultrix 2.2 */
151         VN_UNLOCK((struct vnode *)tvp);
152         AFS_GLOCK();
153
154         afs_globalVFS = afsp;
155         *avpp = (struct vnode *) tvp;
156     }
157
158     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *avpp,
159                ICL_TYPE_INT32, code);
160     AFS_GUNLOCK();
161     return code;
162 }
163
164
165 mp_afs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg)
166 {
167     return EOPNOTSUPP;
168 }
169
170 int mp_afs_statfs(struct mount *afsp)
171 {
172     struct nstatfs *abp = &afsp->m_stat;
173
174     AFS_GLOCK();
175     AFS_STATCNT(afs_statfs);
176
177     abp->f_type = MOUNT_AFS;
178     abp->f_bsize = afsp->vfs_bsize;
179
180     /* Fake a high number below to satisfy programs that use the statfs call
181      * to make sure that there's enough space in the device partition before
182      * storing something there.
183      */
184     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
185         abp->f_ffree  = 2000000;
186     abp->f_fsize = 1024;
187
188     abp->f_fsid.val[0] = afsp->m_stat.f_fsid.val[0];
189     abp->f_fsid.val[1] = afsp->m_stat.f_fsid.val[1];
190
191     AFS_GUNLOCK();
192     return 0;
193 }
194
195
196 int mp_afs_sync(struct mount *mp, int flags)
197 {
198     AFS_STATCNT(afs_sync);
199     return 0;
200 }
201
202
203 #ifdef AFS_DUX50_ENV
204 int mp_afs_smoothsync(struct mount *mp, u_int age, u_int smsync_flag)
205 {
206     AFS_STATCNT(afs_sync);
207     return 0;
208 }
209 #endif
210
211 int mp_afs_fhtovp(struct mount *afsp, struct fid *fidp, struct vnode **avcp)
212 {
213     struct vrequest treq;
214     register code = 0;
215
216     AFS_GLOCK();
217     AFS_STATCNT(afs_vget);
218
219     *avcp = NULL;
220
221     if ((code = afs_InitReq(&treq, u.u_cred)) == 0) {
222         code = afs_osi_vget((struct vcache**)avcp, fidp, &treq);
223     }
224
225     afs_Trace3(afs_iclSetp, CM_TRACE_VGET, ICL_TYPE_POINTER, *avcp,
226                ICL_TYPE_INT32, treq.uid, ICL_TYPE_FID, fidp);
227
228     code = afs_CheckCode(code, &treq, 42);
229     AFS_GUNLOCK();
230     return code;
231 }
232
233
234 /*
235  *  afs_vptofh
236  * 
237  * afs_vptofh can return two flavors of NFS fid, depending on if submounts are
238  * allowed. The reason for this is that we can't guarantee that we found all 
239  * the entry points any OS might use to get the fid for the NFS mountd.
240  * Hence we return a "magic" fid for all but /afs. If it goes through the
241  * translator code, it will get transformed into a SmallFid that we recognize.
242  * So, if submounts are disallowed, and an NFS client tries a submount, it will
243  * get a fid which we don't recognize and the mount will either fail or we
244  * will ignore subsequent requests for that mount.
245  *
246  * The Alpha fid is organized differently than for other platforms. Their
247  * intention was to have the data portion of the fid aligned on a 4 byte
248  * boundary. To do so, the fid is organized as:
249  * u_short reserved
250  * u_short len
251  * char data[8]
252  * The len field is the length of the entire fid, from reserved through data.
253  * This length is used by fid_copy to include copying the reserved field. 
254  * Alpha's zero the reserved field before handing us the fid, but they use
255  * it in fid_cmp. We use the reserved field to store the 16 bits of the Vnode.
256  *
257  * Note that the SmallFid only allows for 8 bits of the cell index and
258  * 16 bits of the vnode. 
259  */
260
261 #define AFS_FIDDATASIZE 8
262 #define AFS_SIZEOFSMALLFID 12 /* full size of fid, including len field */
263 extern int afs_NFSRootOnly; /* 1 => only allow NFS mounts of /afs. */
264 int afs_fid_vnodeoverflow=0, afs_fid_uniqueoverflow=0;
265
266 int mp_afs_vptofh(struct vnode *avn, struct fid *fidp)
267 {
268     struct SmallFid Sfid;
269     long addr[2];
270     register struct cell *tcell;
271     int rootvp = 0;
272     struct vcache *avc = (struct vcache *)avn;
273
274     AFS_GLOCK();
275     AFS_STATCNT(afs_fid);
276
277     if (afs_shuttingdown) {
278         AFS_GUNLOCK();
279         return EIO;
280     }
281
282     if (afs_NFSRootOnly && (avc == afs_globalVp)) rootvp = 1;
283     if (!afs_NFSRootOnly || rootvp) {
284         tcell = afs_GetCell(avc->fid.Cell, READ_LOCK);
285         Sfid.Volume = avc->fid.Fid.Volume;
286         fidp->fid_reserved = avc->fid.Fid.Vnode;
287         Sfid.CellAndUnique = ((tcell->cellIndex << 24) +
288                               (avc->fid.Fid.Unique & 0xffffff));
289         afs_PutCell(tcell, READ_LOCK);
290         if (avc->fid.Fid.Vnode > 0xffff)
291             afs_fid_vnodeoverflow++;
292         if (avc->fid.Fid.Unique > 0xffffff)
293             afs_fid_uniqueoverflow++;
294     } else {
295         fidp->fid_reserved = AFS_XLATOR_MAGIC;
296         addr[0] = (long)avc;
297         AFS_GUNLOCK();
298         VN_HOLD((struct vnode *)avc);
299         AFS_GLOCK();
300     }
301
302     /* Use the fid pointer passed to us. */
303     fidp->fid_len = AFS_SIZEOFSMALLFID;
304     if (afs_NFSRootOnly) {
305         if (rootvp) {
306             bcopy((caddr_t)&Sfid, fidp->fid_data, AFS_FIDDATASIZE);   
307         } else {
308             bcopy((caddr_t)addr, fidp->fid_data, AFS_FIDDATASIZE);   
309         }
310     } else {
311         bcopy((caddr_t)&Sfid, fidp->fid_data, AFS_FIDDATASIZE);   
312     }
313     AFS_GUNLOCK();
314     return 0;
315 }
316
317
318 int mp_Afs_init(void); /* vfs_init - defined below */
319
320
321 /* This is only called by vfs_mount when afs is going to be mounted as root.
322  * Since we don't support diskless clients we shouldn't come here.
323  */
324 int afsmountroot=0;
325 int mp_afs_mountroot(struct mount *afsp, struct vnode **vp)
326 {
327     AFS_GLOCK();
328     AFS_STATCNT(afs_mountroot);
329     afsmountroot++;
330     AFS_GUNLOCK();
331     return EINVAL;
332 }
333
334
335 /* It's called to setup swapping over the net for diskless clients; again
336  * not for us.
337  */
338 int afsswapvp=0;
339 int mp_afs_swapvp(void)
340 {
341     AFS_GLOCK();
342     AFS_STATCNT(afs_swapvp);
343     afsswapvp++;
344     AFS_GUNLOCK();
345     return EINVAL;
346 }
347
348
349 struct vfsops afs_vfsops = {
350     mp_afs_mount,
351     mp_afs_start,
352     mp_afs_unmount,
353     mp_afs_root,
354     mp_afs_quotactl,
355     mp_afs_statfs,
356     mp_afs_sync,
357     mp_afs_fhtovp,      /* afs_vget */
358     mp_afs_vptofh,
359     mp_Afs_init,
360     mp_afs_mountroot,
361     mp_afs_swapvp,
362 #ifdef AFS_DUX50_ENV
363     mp_afs_smoothsync
364 #endif
365 };
366
367
368 /*
369  * System Call Entry Points
370  */
371 #define NULL_FUNC          (int (*)(int))0
372
373 int     (*afs_syscall_func)() = NULL_FUNC;
374 int     (*afs_xsetgroups_func)() = NULL_FUNC;
375 int     (*afs_xioctl_func)() = NULL_FUNC;
376
377 afssyscall(p, args, retval)
378     struct proc *p;
379     void *args;
380     long *retval;
381 {
382     int     (*func)();
383     int code;
384
385     AFS_GLOCK();
386     func = afs_syscall_func;
387     if (func == NULL_FUNC) {
388         code = nosys(p, args, retval);
389     } else {
390         code = (*func)(p, args, retval);
391     }
392     AFS_GUNLOCK();
393     return code;
394 }
395
396 afsxsetgroups(p, args, retval)
397     struct proc *p;
398     void *args;
399     long *retval;
400 {
401     int     (*func)();
402     int code;
403
404     AFS_GLOCK();
405     func = afs_xsetgroups_func;
406     if (func == NULL_FUNC) {
407         code = nosys(p, args, retval);
408     } else {
409         code = (*func)(p, args, retval);
410     }
411     AFS_GUNLOCK();
412     return code;
413 }
414
415 afsxioctl(p, args, retval)
416     struct proc *p;
417     void *args;
418     long *retval;
419 {
420     int     (*func)();
421     int code;
422
423     AFS_GLOCK();
424     func = afs_xioctl_func;
425     if (func == NULL_FUNC) {
426         code = nosys(p, args, retval);
427     } else {
428         code = (*func)(p, args, retval);
429     }
430     AFS_GUNLOCK();
431     return code;
432 }
433
434
435 /*
436  * VFS initialization and unload
437  */
438
439 afs_unconfig()
440 {
441     return EBUSY;
442 }
443
444
445 cfg_subsys_attr_t afs_attributes[]  = {
446     {"", 0, 0, 0, 0, 0, 0}      /* must be the last element */
447 };
448
449 afs_configure(cfg_op_t op, caddr_t indata, size_t indata_size, caddr_t outdata, size_t outdata_size)
450 {
451     cfg_attr_t  *attributes;
452     int ret = ESUCCESS;
453     int i, j, size;
454     caddr_t p;
455
456     switch (op) {
457     case CFG_OP_CONFIGURE:
458         /*
459          * The indata parameter is a list of attributes to be configured, and 
460          * indata_size is the count of attributes.
461          */
462         if ((ret = vfssw_add_fsname(MOUNT_AFS, &afs_vfsops, "afs")) != 0)
463             return(ret);
464         break;
465     case CFG_OP_UNCONFIGURE:
466         if ((ret = afs_unconfig()) != 0)
467             return(ret);
468         break;
469     default:
470         ret = EINVAL;
471         break;
472     }
473     return ret;
474 }
475
476
477 int mp_Afs_init(void)
478 {
479     extern int Afs_xsetgroups(), afs_xioctl(), afs3_syscall();
480     
481     AFS_GLOCK();
482     sysent[AFS_SYSCALL].sy_call = afs3_syscall;
483     sysent[AFS_SYSCALL].sy_parallel = 0;
484     sysent[AFS_SYSCALL].sy_narg = 6;
485     sysent[SYS_setgroups].sy_call = Afs_xsetgroups;
486     afs_xioctl_func = afsxioctl;    
487     afs_xsetgroups_func = afsxsetgroups;
488     afs_syscall_func = afssyscall;
489     AFS_GUNLOCK();
490
491     return 0;
492 }