pull-prototypes-to-head-20020821
[openafs.git] / src / afs / NBSD / 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 <afsconfig.h>
14 #include "../afs/param.h"
15
16 RCSID("$Header$");
17
18 #include "../afs/sysincludes.h" /* Standard vendor system headers */
19 #include "../afs/afsincludes.h" /* Afs-based standard headers */
20 #include "../afs/afs_stats.h"   /* statistics stuff */
21 #include <sys/types.h>
22 #include <kern/mach_param.h>
23 #include <sys/sysconfig.h>
24 #include <sys/systm.h>
25 #include <sys/resource.h>
26 #include <sys/errno.h>
27 #include <sys/conf.h>
28 #include <machine/machlimits.h>
29
30
31 struct vcache *afs_globalVp = 0;
32 struct mount *afs_globalVFS = 0;
33
34 static u_char afs_mntid;
35 int afs_vfsdev = 0;
36 udecl_simple_lock_data(, afsmntid_lock)
37 #define AFSMNTID_LOCK()         usimple_lock(&afsmntid_lock)
38 #define AFSMNTID_UNLOCK()       usimple_unlock(&afsmntid_lock)
39 #define AFSMNTID_LOCK_INIT()    usimple_lock_init(&afsmntid_lock)
40
41
42 int mp_afs_mount(struct mount *afsp,char * path, caddr_t data,
43                  struct nameidata *ndp)
44 {
45     u_int size;
46
47     fsid_t tfsid;
48     struct mount *xmp, *getvfs();
49     int code;
50
51     AFS_GLOCK();
52     AFS_STATCNT(afs_mount);
53
54     if (afs_globalVFS) { /* Don't allow remounts. */
55         AFS_GUNLOCK();
56         return (EBUSY);
57     }
58
59     afs_globalVFS = afsp;
60     afsp->vfs_bsize = 8192;
61 /*
62  * Generate a unique afs mount i.d. ( see nfs_mount() ).
63  */
64     afsp->m_stat.f_fsid.val[0] = makedev(130, 0);
65     afsp->m_stat.f_fsid.val[1] = MOUNT_AFS;
66     AFSMNTID_LOCK();
67     if (++afs_mntid == 0)
68         ++afs_mntid;
69     AFSMNTID_UNLOCK();
70     BM(AFSMNTID_LOCK());
71     tfsid.val[0] = makedev(130, afs_mntid);
72     tfsid.val[1] = MOUNT_AFS;
73     BM(AFSMNTID_UNLOCK());
74
75     while (xmp = getvfs(&tfsid)) {
76         UNMOUNT_READ_UNLOCK(xmp);
77         tfsid.val[0]++;
78         AFSMNTID_LOCK();
79         afs_mntid++;
80         AFSMNTID_UNLOCK();
81     }
82     if (major(tfsid.val[0]) != 130) {
83         AFS_GUNLOCK();
84         return (ENOENT);
85     }
86     afsp->m_stat.f_fsid.val[0] = tfsid.val[0];
87
88     afsp->m_stat.f_mntonname = AFS_KALLOC(MNAMELEN);
89     afsp->m_stat.f_mntfromname = AFS_KALLOC(MNAMELEN); 
90     if ( !afsp->m_stat.f_mntonname || !afsp->m_stat.f_mntfromname)
91         panic("malloc failure in afs_mount\n");
92
93     memset(afsp->m_stat.f_mntonname, 0, MNAMELEN);
94     memset(afsp->m_stat.f_mntfromname, 0, MNAMELEN);
95     AFS_COPYINSTR(path, (caddr_t)afsp->m_stat.f_mntonname, MNAMELEN, &size, code);
96     memcpy(afsp->m_stat.f_mntfromname, "AFS", 4);
97     AFS_GUNLOCK();
98     (void) mp_afs_statfs(afsp);
99     AFS_GLOCK();
100     afs_vfsdev = afsp->m_stat.f_fsid.val[0];
101
102 #ifndef AFS_NONFSTRANS
103     /* Set up the xlator in case it wasn't done elsewhere */
104     afs_xlatorinit_v2();
105     afs_xlatorinit_v3();
106 #endif
107     AFS_GUNLOCK();
108     return 0;
109 }
110
111
112 int mp_afs_unmount (struct mount *afsp, int flag)
113 {
114     AFS_GLOCK();
115     AFS_STATCNT(afs_unmount);
116     afs_globalVFS = 0;
117     afs_shutdown();
118     AFS_GUNLOCK();
119     return 0;
120 }
121
122
123 int mp_afs_start(struct mount *mp, int flags)
124 {
125     return(0);
126 }
127
128 int mp_afs_root (struct mount *afsp, struct vnode **avpp)
129 {
130     register afs_int32 code = 0;
131     struct vrequest treq;
132     register struct vcache *tvp=0;
133
134     AFS_GLOCK();
135     AFS_STATCNT(afs_root);
136     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
137         tvp = afs_globalVp;
138     } else {
139         if (afs_globalVp) {
140             afs_PutVCache(afs_globalVp);
141             afs_globalVp = NULL;
142         }
143
144         if (!(code = afs_InitReq(&treq, cred)) &&
145             !(code = afs_CheckInit())) {
146             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
147             /* we really want this to stay around */
148             if (tvp) {
149                 afs_globalVp = tvp;
150             } else
151                 code = ENOENT;
152         }
153     }
154     if (tvp) {
155         AFS_GUNLOCK();
156         VN_HOLD((struct vnode *)tvp);
157         VN_LOCK((struct vnode *)tvp);
158         tvp->v.v_flag |= VROOT;     /* No-op on Ultrix 2.2 */
159         VN_UNLOCK((struct vnode *)tvp);
160         AFS_GLOCK();
161
162         afs_globalVFS = afsp;
163         *avpp = (struct vnode *) tvp;
164     }
165
166     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *avpp,
167                ICL_TYPE_INT32, code);
168     AFS_GUNLOCK();
169     return code;
170 }
171
172
173 mp_afs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg)
174 {
175     return EOPNOTSUPP;
176 }
177
178 int mp_afs_statfs(struct mount *afsp)
179 {
180     struct nstatfs *abp = &afsp->m_stat;
181
182     AFS_GLOCK();
183     AFS_STATCNT(afs_statfs);
184
185     abp->f_type = MOUNT_AFS;
186     abp->f_bsize = afsp->vfs_bsize;
187
188     /* Fake a high number below to satisfy programs that use the statfs call
189      * to make sure that there's enough space in the device partition before
190      * storing something there.
191      */
192     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
193         abp->f_ffree  = 2000000;
194     abp->f_fsize = 1024;
195
196     abp->f_fsid.val[0] = afsp->m_stat.f_fsid.val[0];
197     abp->f_fsid.val[1] = afsp->m_stat.f_fsid.val[1];
198
199     AFS_GUNLOCK();
200     return 0;
201 }
202
203
204 int mp_afs_sync(struct mount *mp, int flags)
205 {
206     AFS_STATCNT(afs_sync);
207     return 0;
208 }
209
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, 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 = VTOAFS(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             memcpy(fidp->fid_data, (caddr_t)&Sfid, AFS_FIDDATASIZE);   
307         } else {
308             memcpy(fidp->fid_data, (caddr_t)addr, AFS_FIDDATASIZE);   
309         }
310     } else {
311         memcpy(fidp->fid_data, (caddr_t)&Sfid, 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 };
363
364
365 /*
366  * System Call Entry Points
367  */
368 #define NULL_FUNC          (int (*)(int))0
369
370 int     (*afs_syscall_func)() = NULL_FUNC;
371 int     (*afs_xsetgroups_func)() = NULL_FUNC;
372 int     (*afs_xioctl_func)() = NULL_FUNC;
373
374 afssyscall(p, args, retval)
375     struct proc *p;
376     void *args;
377     long *retval;
378 {
379     int     (*func)();
380     int code;
381
382     AFS_GLOCK();
383     func = afs_syscall_func;
384     if (func == NULL_FUNC) {
385         code = nosys(p, args, retval);
386     } else {
387         code = (*func)(p, args, retval);
388     }
389     AFS_GUNLOCK();
390     return code;
391 }
392
393 afsxsetgroups(p, args, retval)
394     struct proc *p;
395     void *args;
396     long *retval;
397 {
398     int     (*func)();
399     int code;
400
401     AFS_GLOCK();
402     func = afs_xsetgroups_func;
403     if (func == NULL_FUNC) {
404         code = nosys(p, args, retval);
405     } else {
406         code = (*func)(p, args, retval);
407     }
408     AFS_GUNLOCK();
409     return code;
410 }
411
412 afsxioctl(p, args, retval)
413     struct proc *p;
414     void *args;
415     long *retval;
416 {
417     int     (*func)();
418     int code;
419
420     AFS_GLOCK();
421     func = afs_xioctl_func;
422     if (func == NULL_FUNC) {
423         code = nosys(p, args, retval);
424     } else {
425         code = (*func)(p, args, retval);
426     }
427     AFS_GUNLOCK();
428     return code;
429 }
430
431
432 /*
433  * VFS initialization and unload
434  */
435
436 afs_unconfig()
437 {
438     return EBUSY;
439 }
440
441
442 cfg_subsys_attr_t afs_attributes[]  = {
443     {"", 0, 0, 0, 0, 0, 0}      /* must be the last element */
444 };
445
446 afs_configure(cfg_op_t op, caddr_t indata, size_t indata_size, caddr_t outdata, size_t outdata_size)
447 {
448     cfg_attr_t  *attributes;
449     int ret = ESUCCESS;
450     int i, j, size;
451     caddr_t p;
452
453     switch (op) {
454     case CFG_OP_CONFIGURE:
455         /*
456          * The indata parameter is a list of attributes to be configured, and 
457          * indata_size is the count of attributes.
458          */
459         if ((ret = vfssw_add_fsname(MOUNT_AFS, &afs_vfsops, "afs")) != 0)
460             return(ret);
461         break;
462     case CFG_OP_UNCONFIGURE:
463         if ((ret = afs_unconfig()) != 0)
464             return(ret);
465         break;
466     default:
467         ret = EINVAL;
468         break;
469     }
470     return ret;
471 }
472
473
474 int mp_Afs_init(void)
475 {
476     extern int Afs_xsetgroups(), afs_xioctl(), afs3_syscall();
477     
478     AFS_GLOCK();
479     sysent[AFS_SYSCALL].sy_call = afs3_syscall;
480     sysent[AFS_SYSCALL].sy_parallel = 0;
481     sysent[AFS_SYSCALL].sy_narg = 6;
482     sysent[SYS_setgroups].sy_call = Afs_xsetgroups;
483     afs_xioctl_func = afsxioctl;    
484     afs_xsetgroups_func = afsxsetgroups;
485     afs_syscall_func = afssyscall;
486     AFS_GUNLOCK();
487
488     return 0;
489 }