]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/coda/coda_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r51728,
[FreeBSD/FreeBSD.git] / sys / 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$
31  * 
32  */
33
34 /* 
35  * Mach Operating System
36  * Copyright (c) 1989 Carnegie-Mellon University
37  * All rights reserved.  The CMU software License Agreement specifies
38  * the terms and conditions for use and redistribution.
39  */
40
41 /*
42  * This code was written for the Coda file system at Carnegie Mellon
43  * University.  Contributers include David Steere, James Kistler, and
44  * M. Satyanarayanan.  
45  */
46
47 #include <vcoda.h>
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/conf.h>
55 #include <sys/namei.h>
56 #include <sys/mount.h>
57 #include <sys/select.h>
58
59 #include <coda/coda.h>
60 #include <coda/cnode.h>
61 #include <coda/coda_vfsops.h>
62 #include <coda/coda_venus.h>
63 #include <coda/coda_subr.h>
64 #include <coda/coda_opstats.h>
65
66 MALLOC_DEFINE(M_CODA, "CODA storage", "Various Coda Structures");
67
68 int codadebug = 0;
69 int coda_vfsop_print_entry = 0;
70 #define ENTRY    if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
71
72 struct vnode *coda_ctlvp;
73 struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
74
75 /* structure to keep statistics of internally generated/satisfied calls */
76
77 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
78
79 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
80 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
81 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
82 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
83
84 extern int coda_nc_initialized;     /* Set if cache has been initialized */
85 extern int vc_nb_open __P((dev_t, int, int, struct proc *));
86
87 int
88 coda_vfsopstats_init(void)
89 {
90         register int i;
91         
92         for (i=0;i<CODA_VFSOPS_SIZE;i++) {
93                 coda_vfsopstats[i].opcode = i;
94                 coda_vfsopstats[i].entries = 0;
95                 coda_vfsopstats[i].sat_intrn = 0;
96                 coda_vfsopstats[i].unsat_intrn = 0;
97                 coda_vfsopstats[i].gen_intrn = 0;
98         }
99         
100         return 0;
101 }
102
103 /*
104  * cfs mount vfsop
105  * Set up mount info record and attach it to vfs struct.
106  */
107 /*ARGSUSED*/
108 int
109 coda_mount(vfsp, path, data, ndp, p)
110     struct mount *vfsp;         /* Allocated and initialized by mount(2) */
111     char *path;                 /* path covered: ignored by the fs-layer */
112     caddr_t data;               /* Need to define a data type for this in netbsd? */
113     struct nameidata *ndp;      /* Clobber this to lookup the device name */
114     struct proc *p;             /* The ever-famous proc pointer */
115 {
116     struct vnode *dvp;
117     struct cnode *cp;
118     dev_t dev;
119     struct coda_mntinfo *mi;
120     struct vnode *rootvp;
121     ViceFid rootfid;
122     ViceFid ctlfid;
123     int error;
124
125     ENTRY;
126
127     coda_vfsopstats_init();
128     coda_vnodeopstats_init();
129     
130     MARK_ENTRY(CODA_MOUNT_STATS);
131     if (CODA_MOUNTED(vfsp)) {
132         MARK_INT_FAIL(CODA_MOUNT_STATS);
133         return(EBUSY);
134     }
135     
136     /* Validate mount device.  Similar to getmdev(). */
137
138     NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, p);
139     error = namei(ndp);
140     dvp = ndp->ni_vp;
141
142     if (error) {
143         MARK_INT_FAIL(CODA_MOUNT_STATS);
144         return (error);
145     }
146     if (dvp->v_type != VCHR) {
147         MARK_INT_FAIL(CODA_MOUNT_STATS);
148         vrele(dvp);
149         return(ENXIO);
150     }
151     dev = dvp->v_rdev;
152     vrele(dvp);
153
154     /*
155      * See if the device table matches our expectations.
156      */
157     if (devsw(dev)->d_open != vc_nb_open)
158     {
159         MARK_INT_FAIL(CODA_MOUNT_STATS);
160         return(ENXIO);
161     }
162     
163     if (minor(dev) >= NVCODA || minor(dev) < 0) {
164         MARK_INT_FAIL(CODA_MOUNT_STATS);
165         return(ENXIO);
166     }
167     
168     /*
169      * Initialize the mount record and link it to the vfs struct
170      */
171     mi = &coda_mnttbl[minor(dev)];
172     
173     if (!VC_OPEN(&mi->mi_vcomm)) {
174         MARK_INT_FAIL(CODA_MOUNT_STATS);
175         return(ENODEV);
176     }
177     
178     /* No initialization (here) of mi_vcomm! */
179     vfsp->mnt_data = (qaddr_t)mi;
180     vfs_getnewfsid (vfsp);
181
182     mi->mi_vfsp = vfsp;
183     
184     /*
185      * Make a root vnode to placate the Vnode interface, but don't
186      * actually make the CODA_ROOT call to venus until the first call
187      * to coda_root in case a server is down while venus is starting.
188      */
189     rootfid.Volume = 0;
190     rootfid.Vnode = 0;
191     rootfid.Unique = 0;
192     cp = make_coda_node(&rootfid, vfsp, VDIR);
193     rootvp = CTOV(cp);
194     rootvp->v_flag |= VROOT;
195         
196     ctlfid.Volume = CTL_VOL;
197     ctlfid.Vnode = CTL_VNO;
198     ctlfid.Unique = CTL_UNI;
199 /*  cp = make_coda_node(&ctlfid, vfsp, VCHR);
200     The above code seems to cause a loop in the cnode links.
201     I don't totally understand when it happens, it is caught
202     when closing down the system.
203  */
204     cp = make_coda_node(&ctlfid, 0, VCHR);
205
206     coda_ctlvp = CTOV(cp);
207
208     /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
209     mi->mi_vfsp = vfsp;
210     mi->mi_rootvp = rootvp;
211     
212     /* set filesystem block size */
213     vfsp->mnt_stat.f_bsize = 8192;          /* XXX -JJK */
214
215     /* Set f_iosize.  XXX -- inamura@isl.ntt.co.jp. 
216        For vnode_pager_haspage() references. The value should be obtained 
217        from underlying UFS. */
218     /* Checked UFS. iosize is set as 8192 */
219     vfsp->mnt_stat.f_iosize = 8192;
220
221     /* error is currently guaranteed to be zero, but in case some
222        code changes... */
223     CODADEBUG(1,
224              myprintf(("coda_mount returned %d\n",error)););
225     if (error)
226         MARK_INT_FAIL(CODA_MOUNT_STATS);
227     else
228         MARK_INT_SAT(CODA_MOUNT_STATS);
229     
230     return(error);
231 }
232
233 int
234 coda_unmount(vfsp, mntflags, p)
235     struct mount *vfsp;
236     int mntflags;
237     struct proc *p;
238 {
239     struct coda_mntinfo *mi = vftomi(vfsp);
240     int active, error = 0;
241     
242     ENTRY;
243     MARK_ENTRY(CODA_UMOUNT_STATS);
244     if (!CODA_MOUNTED(vfsp)) {
245         MARK_INT_FAIL(CODA_UMOUNT_STATS);
246         return(EINVAL);
247     }
248     
249     if (mi->mi_vfsp == vfsp) {  /* We found the victim */
250         if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
251             return (EBUSY);     /* Venus is still running */
252
253 #ifdef  DEBUG
254         printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
255 #endif
256         vrele(mi->mi_rootvp);
257
258         active = coda_kill(vfsp, NOT_DOWNCALL);
259         mi->mi_rootvp->v_flag &= ~VROOT;
260         error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE);
261         printf("coda_unmount: active = %d, vflush active %d\n", active, error);
262         error = 0;
263         /* I'm going to take this out to allow lookups to go through. I'm
264          * not sure it's important anyway. -- DCS 2/2/94
265          */
266         /* vfsp->VFS_DATA = NULL; */
267
268         /* No more vfsp's to hold onto */
269         mi->mi_vfsp = NULL;
270         mi->mi_rootvp = NULL;
271
272         if (error)
273             MARK_INT_FAIL(CODA_UMOUNT_STATS);
274         else
275             MARK_INT_SAT(CODA_UMOUNT_STATS);
276
277         return(error);
278     }
279     return (EINVAL);
280 }
281
282 /*
283  * find root of cfs
284  */
285 int
286 coda_root(vfsp, vpp)
287         struct mount *vfsp;
288         struct vnode **vpp;
289 {
290     struct coda_mntinfo *mi = vftomi(vfsp);
291     struct vnode **result;
292     int error;
293     struct proc *p = curproc;    /* XXX - bnoble */
294     ViceFid VFid;
295
296     ENTRY;
297     MARK_ENTRY(CODA_ROOT_STATS);
298     result = NULL;
299     
300     if (vfsp == mi->mi_vfsp) {
301         if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
302             (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
303             (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
304             { /* Found valid root. */
305                 *vpp = mi->mi_rootvp;
306                 /* On Mach, this is vref.  On NetBSD, VOP_LOCK */
307 #if     1
308                 vref(*vpp);
309                 vn_lock(*vpp, LK_EXCLUSIVE, p);
310 #else
311                 vget(*vpp, LK_EXCLUSIVE, p);
312 #endif
313                 MARK_INT_SAT(CODA_ROOT_STATS);
314                 return(0);
315             }
316     }
317
318     error = venus_root(vftomi(vfsp), p->p_cred->pc_ucred, p, &VFid);
319
320     if (!error) {
321         /*
322          * Save the new rootfid in the cnode, and rehash the cnode into the
323          * cnode hash with the new fid key.
324          */
325         coda_unsave(VTOC(mi->mi_rootvp));
326         VTOC(mi->mi_rootvp)->c_fid = VFid;
327         coda_save(VTOC(mi->mi_rootvp));
328
329         *vpp = mi->mi_rootvp;
330 #if     1
331         vref(*vpp);
332         vn_lock(*vpp, LK_EXCLUSIVE, p);
333 #else
334         vget(*vpp, LK_EXCLUSIVE, p);
335 #endif
336
337         MARK_INT_SAT(CODA_ROOT_STATS);
338         goto exit;
339     } else if (error == ENODEV || error == EINTR) {
340         /* Gross hack here! */
341         /*
342          * If Venus fails to respond to the CODA_ROOT call, coda_call returns
343          * ENODEV. Return the uninitialized root vnode to allow vfs
344          * operations such as unmount to continue. Without this hack,
345          * there is no way to do an unmount if Venus dies before a 
346          * successful CODA_ROOT call is done. All vnode operations 
347          * will fail.
348          */
349         *vpp = mi->mi_rootvp;
350 #if     1
351         vref(*vpp);
352         vn_lock(*vpp, LK_EXCLUSIVE, p);
353 #else
354         vget(*vpp, LK_EXCLUSIVE, p);
355 #endif
356
357         MARK_INT_FAIL(CODA_ROOT_STATS);
358         error = 0;
359         goto exit;
360     } else {
361         CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
362         MARK_INT_FAIL(CODA_ROOT_STATS);
363                 
364         goto exit;
365     }
366
367  exit:
368     return(error);
369 }
370
371 /*
372  * Get file system statistics.
373  */
374 int
375 coda_nb_statfs(vfsp, sbp, p)
376     register struct mount *vfsp;
377     struct statfs *sbp;
378     struct proc *p;
379 {
380     ENTRY;
381 /*  MARK_ENTRY(CODA_STATFS_STATS); */
382     if (!CODA_MOUNTED(vfsp)) {
383 /*      MARK_INT_FAIL(CODA_STATFS_STATS);*/
384         return(EINVAL);
385     }
386     
387     bzero(sbp, sizeof(struct statfs));
388     /* XXX - what to do about f_flags, others? --bnoble */
389     /* Below This is what AFS does
390         #define NB_SFS_SIZ 0x895440
391      */
392     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
393     sbp->f_type = vfsp->mnt_vfc->vfc_typenum;
394     sbp->f_bsize = 8192; /* XXX */
395     sbp->f_iosize = 8192; /* XXX */
396 #define NB_SFS_SIZ 0x8AB75D
397     sbp->f_blocks = NB_SFS_SIZ;
398     sbp->f_bfree = NB_SFS_SIZ;
399     sbp->f_bavail = NB_SFS_SIZ;
400     sbp->f_files = NB_SFS_SIZ;
401     sbp->f_ffree = NB_SFS_SIZ;
402     bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
403     snprintf(sbp->f_mntonname, sizeof(sbp->f_mntonname), "/coda");
404     snprintf(sbp->f_mntfromname, sizeof(sbp->f_mntfromname), "CODA");
405 /*  MARK_INT_SAT(CODA_STATFS_STATS); */
406     return(0);
407 }
408
409 /*
410  * Flush any pending I/O.
411  */
412 int
413 coda_sync(vfsp, waitfor, cred, p)
414     struct mount *vfsp;
415     int    waitfor;
416     struct ucred *cred;
417     struct proc *p;
418 {
419     ENTRY;
420     MARK_ENTRY(CODA_SYNC_STATS);
421     MARK_INT_SAT(CODA_SYNC_STATS);
422     return(0);
423 }
424
425 /* 
426  * fhtovp is now what vget used to be in 4.3-derived systems.  For
427  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
428  * a type-specific fid.  
429  */
430 int
431 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
432     register struct mount *vfsp;    
433     struct fid *fhp;
434     struct mbuf *nam;
435     struct vnode **vpp;
436     int *exflagsp;
437     struct ucred **creadanonp;
438 {
439     struct cfid *cfid = (struct cfid *)fhp;
440     struct cnode *cp = 0;
441     int error;
442     struct proc *p = curproc; /* XXX -mach */
443     ViceFid VFid;
444     int vtype;
445
446     ENTRY;
447     
448     MARK_ENTRY(CODA_VGET_STATS);
449     /* Check for vget of control object. */
450     if (IS_CTL_FID(&cfid->cfid_fid)) {
451         *vpp = coda_ctlvp;
452         vref(coda_ctlvp);
453         MARK_INT_SAT(CODA_VGET_STATS);
454         return(0);
455     }
456     
457     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, p->p_cred->pc_ucred, p, &VFid, &vtype);
458     
459     if (error) {
460         CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
461             *vpp = (struct vnode *)0;
462     } else {
463         CODADEBUG(CODA_VGET, 
464                  myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
465                         VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
466             
467         cp = make_coda_node(&VFid, vfsp, vtype);
468         *vpp = CTOV(cp);
469     }
470     return(error);
471 }
472
473 /*
474  * To allow for greater ease of use, some vnodes may be orphaned when
475  * Venus dies.  Certain operations should still be allowed to go
476  * through, but without propagating ophan-ness.  So this function will
477  * get a new vnode for the file from the current run of Venus.  */
478  
479 int
480 getNewVnode(vpp)
481      struct vnode **vpp;
482 {
483     struct cfid cfid;
484     struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
485     
486     ENTRY;
487
488     cfid.cfid_len = (short)sizeof(ViceFid);
489     cfid.cfid_fid = VTOC(*vpp)->c_fid;  /* Structure assignment. */
490     /* XXX ? */
491
492     /* We're guessing that if set, the 1st element on the list is a
493      * valid vnode to use. If not, return ENODEV as venus is dead.
494      */
495     if (mi->mi_vfsp == NULL)
496         return ENODEV;
497     
498     return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
499                       NULL, NULL);
500 }
501
502 #include <ufs/ufs/quota.h>
503 #include <ufs/ufs/ufsmount.h>
504 /* get the mount structure corresponding to a given device.  Assume 
505  * device corresponds to a UFS. Return NULL if no device is found.
506  */ 
507 struct mount *devtomp(dev)
508     dev_t dev;
509 {
510     struct mount *mp, *nmp;
511     
512     for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) {
513         nmp = mp->mnt_list.cqe_next;
514         if (((VFSTOUFS(mp))->um_dev == dev)) {
515             /* mount corresponds to UFS and the device matches one we want */
516             return(mp); 
517         }
518     }
519     /* mount structure wasn't found */ 
520     return(NULL); 
521 }
522
523 struct vfsops coda_vfsops = {
524     coda_mount,
525     vfs_stdstart,
526     coda_unmount,
527     coda_root,
528     vfs_stdquotactl,
529     coda_nb_statfs,
530     coda_sync,
531     vfs_stdvget,
532     vfs_stdfhtovp,
533     vfs_stdcheckexp,
534     vfs_stdvptofh,
535     vfs_stdinit
536 };
537
538 VFS_SET(coda_vfsops, coda, VFCF_NETWORK);