MASSIVE reorganization of the device operations vector. Change cdevsw
[dragonfly.git] / sys / vfs / coda / coda_vfsops.c
1 /*
2  * 
3  *             Coda: an Experimental Distributed File System
4  *                              Release 3.1
5  * 
6  *           Copyright (c) 1987-1998 Carnegie Mellon University
7  *                          All Rights Reserved
8  * 
9  * Permission  to  use, copy, modify and distribute this software and its
10  * documentation is hereby granted,  provided  that  both  the  copyright
11  * notice  and  this  permission  notice  appear  in  all  copies  of the
12  * software, derivative works or  modified  versions,  and  any  portions
13  * thereof, and that both notices appear in supporting documentation, and
14  * that credit is given to Carnegie Mellon University  in  all  documents
15  * and publicity pertaining to direct or indirect use of this code or its
16  * derivatives.
17  * 
18  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
19  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
20  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
21  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
22  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
23  * ANY DERIVATIVE WORK.
24  * 
25  * Carnegie  Mellon  encourages  users  of  this  software  to return any
26  * improvements or extensions that  they  make,  and  to  grant  Carnegie
27  * Mellon the rights to redistribute these changes without encumbrance.
28  * 
29  *      @(#) src/sys/cfs/coda_vfsops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
30  * $FreeBSD: src/sys/coda/coda_vfsops.c,v 1.24.2.1 2001/07/26 20:36:45 iedowse Exp $
31  * $DragonFly: src/sys/vfs/coda/Attic/coda_vfsops.c,v 1.25 2006/07/28 02:17:41 dillon Exp $
32  * 
33  */
34
35 /* 
36  * Mach Operating System
37  * Copyright (c) 1989 Carnegie-Mellon University
38  * All rights reserved.  The CMU software License Agreement specifies
39  * the terms and conditions for use and redistribution.
40  */
41
42 /*
43  * This code was written for the Coda file system at Carnegie Mellon
44  * University.  Contributers include David Steere, James Kistler, and
45  * M. Satyanarayanan.  
46  */
47
48 #include "use_vcoda.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/proc.h>
54 #include <sys/malloc.h>
55 #include <sys/conf.h>
56 #include <sys/nlookup.h>
57 #include <sys/mount.h>
58 #include <sys/select.h>
59
60 #include <vm/vm_zone.h>
61
62 #include "coda.h"
63 #include "cnode.h"
64 #include "coda_vfsops.h"
65 #include "coda_venus.h"
66 #include "coda_subr.h"
67 #include "coda_opstats.h"
68
69 MALLOC_DEFINE(M_CODA, "CODA storage", "Various Coda Structures");
70
71 int codadebug = 0;
72 int coda_vfsop_print_entry = 0;
73 #define ENTRY    if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__func__))
74
75 struct vnode *coda_ctlvp;
76 struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
77
78 /* structure to keep statistics of internally generated/satisfied calls */
79
80 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
81
82 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
83 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
84 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
85 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
86
87 extern int coda_nc_initialized;     /* Set if cache has been initialized */
88
89 int
90 coda_vfsopstats_init(void)
91 {
92         int i;
93         
94         for (i=0;i<CODA_VFSOPS_SIZE;i++) {
95                 coda_vfsopstats[i].opcode = i;
96                 coda_vfsopstats[i].entries = 0;
97                 coda_vfsopstats[i].sat_intrn = 0;
98                 coda_vfsopstats[i].unsat_intrn = 0;
99                 coda_vfsopstats[i].gen_intrn = 0;
100         }
101         
102         return 0;
103 }
104
105 /*
106  * cfs mount vfsop
107  * Set up mount info record and attach it to vfs struct.
108  */
109 /*ARGSUSED*/
110 int
111 coda_mount(struct mount *vfsp,  /* Allocated and initialized by mount(2) */
112            char *path,          /* path covered: ignored by the fs-layer */
113            caddr_t data,        /* Need to define a data type for this in
114                                  * netbsd? */
115            struct ucred *cred)  /* Mount credential */
116 {
117     struct vnode *dvp;
118     struct cnode *cp;
119     udev_t udev;
120     struct coda_mntinfo *mi;
121     struct vnode *rootvp;
122     ViceFid rootfid;
123     ViceFid ctlfid;
124     int error;
125     struct nlookupdata nd;
126
127     ENTRY;
128
129     coda_vfsopstats_init();
130     coda_vnodeopstats_init();
131     
132     MARK_ENTRY(CODA_MOUNT_STATS);
133     if (CODA_MOUNTED(vfsp)) {
134         MARK_INT_FAIL(CODA_MOUNT_STATS);
135         return(EBUSY);
136     }
137     
138     /* Validate mount device.  Similar to getmdev(). */
139     dvp = NULL;
140     error = nlookup_init(&nd, data, UIO_USERSPACE, NLC_FOLLOW);
141     if (error == 0)
142         error = nlookup(&nd);
143     if (error == 0)
144         error = cache_vref(nd.nl_ncp, nd.nl_cred, &dvp);
145     nlookup_done(&nd);
146     if (error) {
147         MARK_INT_FAIL(CODA_MOUNT_STATS);
148         return (error);
149     }
150     if (dvp->v_type != VCHR) {
151         MARK_INT_FAIL(CODA_MOUNT_STATS);
152         vrele(dvp);
153         return(ENXIO);
154     }
155     udev = dvp->v_udev;
156     vrele(dvp);
157
158     if (uminor(udev) >= NVCODA || uminor(udev) < 0) {
159         MARK_INT_FAIL(CODA_MOUNT_STATS);
160         return(ENXIO);
161     }
162     
163     /*
164      * Initialize the mount record and link it to the vfs struct
165      */
166     mi = &coda_mnttbl[uminor(udev)];
167     
168     if (!VC_OPEN(&mi->mi_vcomm)) {
169         MARK_INT_FAIL(CODA_MOUNT_STATS);
170         return(ENODEV);
171     }
172     
173     /* No initialization (here) of mi_vcomm! */
174     vfsp->mnt_data = (qaddr_t)mi;
175     vfs_getnewfsid (vfsp);
176
177     mi->mi_vfsp = vfsp;
178     
179     /*
180      * Make a root vnode to placate the Vnode interface, but don't
181      * actually make the CODA_ROOT call to venus until the first call
182      * to coda_root in case a server is down while venus is starting.
183      */
184     rootfid.Volume = 0;
185     rootfid.Vnode = 0;
186     rootfid.Unique = 0;
187     cp = make_coda_node(&rootfid, vfsp, VDIR);
188     rootvp = CTOV(cp);
189     rootvp->v_flag |= VROOT;
190         
191     ctlfid.Volume = CTL_VOL;
192     ctlfid.Vnode = CTL_VNO;
193     ctlfid.Unique = CTL_UNI;
194 /*  cp = make_coda_node(&ctlfid, vfsp, VCHR);
195     The above code seems to cause a loop in the cnode links.
196     I don't totally understand when it happens, it is caught
197     when closing down the system.
198  */
199     cp = make_coda_node(&ctlfid, 0, VCHR);
200
201     coda_ctlvp = CTOV(cp);
202
203     /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
204     mi->mi_vfsp = vfsp;
205     mi->mi_rootvp = rootvp;
206     
207     /* set filesystem block size */
208     vfsp->mnt_stat.f_bsize = 8192;          /* XXX -JJK */
209
210     /* Set f_iosize.  XXX -- inamura@isl.ntt.co.jp. 
211        For vnode_pager_haspage() references. The value should be obtained 
212        from underlying UFS. */
213     /* Checked UFS. iosize is set as 8192 */
214     vfsp->mnt_stat.f_iosize = 8192;
215
216     /* error is currently guaranteed to be zero, but in case some
217        code changes... */
218     CODADEBUG(1,
219              myprintf(("coda_mount returned %d\n",error)););
220     if (error)
221         MARK_INT_FAIL(CODA_MOUNT_STATS);
222     else
223         MARK_INT_SAT(CODA_MOUNT_STATS);
224     
225     return(error);
226 }
227
228 int
229 coda_unmount(struct mount *vfsp, int mntflags)
230 {
231     struct coda_mntinfo *mi = vftomi(vfsp);
232     int active, error = 0;
233     
234     ENTRY;
235     MARK_ENTRY(CODA_UMOUNT_STATS);
236     if (!CODA_MOUNTED(vfsp)) {
237         MARK_INT_FAIL(CODA_UMOUNT_STATS);
238         return(EINVAL);
239     }
240     
241     if (mi->mi_vfsp == vfsp) {  /* We found the victim */
242         if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
243             return (EBUSY);     /* Venus is still running */
244
245 #ifdef  DEBUG
246         printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
247 #endif
248         vrele(mi->mi_rootvp);
249
250         active = coda_kill(vfsp, NOT_DOWNCALL);
251         mi->mi_rootvp->v_flag &= ~VROOT;
252         error = vflush(mi->mi_vfsp, 0, FORCECLOSE);
253         printf("coda_unmount: active = %d, vflush active %d\n", active, error);
254         error = 0;
255         /* I'm going to take this out to allow lookups to go through. I'm
256          * not sure it's important anyway. -- DCS 2/2/94
257          */
258         /* vfsp->VFS_DATA = NULL; */
259
260         /* No more vfsp's to hold onto */
261         mi->mi_vfsp = NULL;
262         mi->mi_rootvp = NULL;
263
264         if (error)
265             MARK_INT_FAIL(CODA_UMOUNT_STATS);
266         else
267             MARK_INT_SAT(CODA_UMOUNT_STATS);
268
269         return(error);
270     }
271     return (EINVAL);
272 }
273
274 /*
275  * find root of cfs
276  */
277 int
278 coda_root(struct mount *vfsp, struct vnode **vpp)
279 {
280     struct coda_mntinfo *mi = vftomi(vfsp);
281     struct vnode **result;
282     int error;
283     struct thread *td = curthread;    /* XXX - bnoble */
284     struct ucred *cred;
285     ViceFid VFid;
286
287     KKASSERT(td->td_proc);
288     cred = td->td_proc->p_ucred;
289
290     ENTRY;
291     MARK_ENTRY(CODA_ROOT_STATS);
292     result = NULL;
293     
294     if (vfsp == mi->mi_vfsp) {
295         if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
296             (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
297             (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
298             { /* Found valid root. */
299                 *vpp = mi->mi_rootvp;
300                 /* On Mach, this is vref.  On NetBSD, VOP_LOCK */
301                 vget(*vpp, LK_EXCLUSIVE);
302                 MARK_INT_SAT(CODA_ROOT_STATS);
303                 return(0);
304             }
305     }
306
307     error = venus_root(vftomi(vfsp), cred, td, &VFid);
308
309     if (!error) {
310         /*
311          * Save the new rootfid in the cnode, and rehash the cnode into the
312          * cnode hash with the new fid key.
313          */
314         coda_unsave(VTOC(mi->mi_rootvp));
315         VTOC(mi->mi_rootvp)->c_fid = VFid;
316         coda_save(VTOC(mi->mi_rootvp));
317
318         *vpp = mi->mi_rootvp;
319         vget(*vpp, LK_EXCLUSIVE);
320
321         MARK_INT_SAT(CODA_ROOT_STATS);
322         goto exit;
323     } else if (error == ENODEV || error == EINTR) {
324         /* Gross hack here! */
325         /*
326          * If Venus fails to respond to the CODA_ROOT call, coda_call returns
327          * ENODEV. Return the uninitialized root vnode to allow vfs
328          * operations such as unmount to continue. Without this hack,
329          * there is no way to do an unmount if Venus dies before a 
330          * successful CODA_ROOT call is done. All vnode operations 
331          * will fail.
332          */
333         *vpp = mi->mi_rootvp;
334         vget(*vpp, LK_EXCLUSIVE);
335
336         MARK_INT_FAIL(CODA_ROOT_STATS);
337         error = 0;
338         goto exit;
339     } else {
340         CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
341         MARK_INT_FAIL(CODA_ROOT_STATS);
342                 
343         goto exit;
344     }
345
346  exit:
347     return(error);
348 }
349
350 /*
351  * Get file system statistics.
352  */
353 int
354 coda_nb_statfs(struct mount *vfsp, struct statfs *sbp, struct ucred *cred)
355 {
356     ENTRY;
357 /*  MARK_ENTRY(CODA_STATFS_STATS); */
358     if (!CODA_MOUNTED(vfsp)) {
359 /*      MARK_INT_FAIL(CODA_STATFS_STATS);*/
360         return(EINVAL);
361     }
362     
363     bzero(sbp, sizeof(struct statfs));
364     /* XXX - what to do about f_flags, others? --bnoble */
365     /* Below This is what AFS does
366         #define NB_SFS_SIZ 0x895440
367      */
368     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
369     sbp->f_type = vfsp->mnt_vfc->vfc_typenum;
370     sbp->f_bsize = 8192; /* XXX */
371     sbp->f_iosize = 8192; /* XXX */
372 #define NB_SFS_SIZ 0x8AB75D
373     sbp->f_blocks = NB_SFS_SIZ;
374     sbp->f_bfree = NB_SFS_SIZ;
375     sbp->f_bavail = NB_SFS_SIZ;
376     sbp->f_files = NB_SFS_SIZ;
377     sbp->f_ffree = NB_SFS_SIZ;
378     bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
379     snprintf(sbp->f_mntfromname, sizeof(sbp->f_mntfromname), "CODA");
380 /*  MARK_INT_SAT(CODA_STATFS_STATS); */
381     return(0);
382 }
383
384 /*
385  * Flush any pending I/O.
386  */
387 int
388 coda_sync(struct mount *vfsp, int waitfor)
389 {
390     ENTRY;
391     MARK_ENTRY(CODA_SYNC_STATS);
392     MARK_INT_SAT(CODA_SYNC_STATS);
393     return(0);
394 }
395
396 /* 
397  * fhtovp is now what vget used to be in 4.3-derived systems.  For
398  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
399  * a type-specific fid.  
400  */
401 int
402 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam,
403             struct vnode **vpp, int *exflagsp, struct ucred **creadanonp)
404 {
405     struct cfid *cfid = (struct cfid *)fhp;
406     struct cnode *cp = 0;
407     int error;
408     struct thread *td = curthread; /* XXX -mach */
409     struct ucred *cred;
410     ViceFid VFid;
411     int vtype;
412
413     KKASSERT(td->td_proc);
414     cred = td->td_proc->p_ucred;
415
416     ENTRY;
417     
418     MARK_ENTRY(CODA_VGET_STATS);
419     /* Check for vget of control object. */
420     if (IS_CTL_FID(&cfid->cfid_fid)) {
421         *vpp = coda_ctlvp;
422         vref(coda_ctlvp);
423         MARK_INT_SAT(CODA_VGET_STATS);
424         return(0);
425     }
426     
427     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, cred, td, &VFid, &vtype);
428     
429     if (error) {
430         CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
431             *vpp = (struct vnode *)0;
432     } else {
433         CODADEBUG(CODA_VGET, 
434                  myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
435                         VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
436             
437         cp = make_coda_node(&VFid, vfsp, vtype);
438         *vpp = CTOV(cp);
439     }
440     return(error);
441 }
442
443 /*
444  * To allow for greater ease of use, some vnodes may be orphaned when
445  * Venus dies.  Certain operations should still be allowed to go
446  * through, but without propagating orphan-ness.  So this function will
447  * get a new vnode for the file from the current run of Venus.
448  */
449  
450 int
451 getNewVnode(struct vnode **vpp)
452 {
453     struct cfid cfid;
454     struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
455     
456     ENTRY;
457
458     cfid.cfid_len = (short)sizeof(ViceFid);
459     cfid.cfid_fid = VTOC(*vpp)->c_fid;  /* Structure assignment. */
460     /* XXX ? */
461
462     /* We're guessing that if set, the 1st element on the list is a
463      * valid vnode to use. If not, return ENODEV as venus is dead.
464      */
465     if (mi->mi_vfsp == NULL)
466         return ENODEV;
467     
468     return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
469                       NULL, NULL);
470 }
471
472 #include <vfs/ufs/quota.h>
473 #include <vfs/ufs/ufsmount.h>
474
475 /*
476  * get the mount structure corresponding to a given device.  Assume 
477  * device corresponds to a UFS. Return NULL if no device is found.
478  */ 
479 struct devtomp_info {
480         struct mount *ret_mp;
481         dev_t dev;
482 };
483
484 static int devtomp_callback(struct mount *mp, void *data);
485
486 struct mount *
487 devtomp(dev_t dev)
488 {
489     struct devtomp_info info;
490
491     info.ret_mp = NULL;
492     info.dev = dev;
493
494     mountlist_scan(devtomp_callback, &info, MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
495     return(info.ret_mp);
496 }
497
498 static int
499 devtomp_callback(struct mount *mp, void *data)
500 {
501         struct devtomp_info *info = data;
502
503         if ((VFSTOUFS(mp))->um_dev == info->dev) {
504                 info->ret_mp = mp;
505                 return(-1);
506         }
507         return(0);
508 }
509
510 struct vfsops coda_vfsops = {
511     .vfs_mount =        coda_mount,
512     .vfs_unmount =      coda_unmount,
513     .vfs_root =         coda_root,
514     .vfs_statfs =       coda_nb_statfs,
515     .vfs_sync =         coda_sync
516 };
517
518 VFS_SET(coda_vfsops, coda, VFCF_NETWORK);