]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/coda/coda_vfsops.c
Pacify gcc-3.1+, initialize two variables to avoid -Wuninitialized
[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/conf.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/namei.h>
57 #include <sys/proc.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",__func__))
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 thread *));
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, td)
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 thread *td;
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     NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, td);
138     error = namei(ndp);
139     dvp = ndp->ni_vp;
140
141     if (error) {
142         MARK_INT_FAIL(CODA_MOUNT_STATS);
143         return (error);
144     }
145     if (dvp->v_type != VCHR) {
146         MARK_INT_FAIL(CODA_MOUNT_STATS);
147         vrele(dvp);
148         NDFREE(ndp, NDF_ONLY_PNBUF);
149         return(ENXIO);
150     }
151     dev = dvp->v_rdev;
152     vrele(dvp);
153     NDFREE(ndp, NDF_ONLY_PNBUF);
154
155     /*
156      * See if the device table matches our expectations.
157      */
158     if (devsw(dev)->d_open != vc_nb_open)
159     {
160         MARK_INT_FAIL(CODA_MOUNT_STATS);
161         return(ENXIO);
162     }
163     
164     if (minor(dev) >= NVCODA || minor(dev) < 0) {
165         MARK_INT_FAIL(CODA_MOUNT_STATS);
166         return(ENXIO);
167     }
168     
169     /*
170      * Initialize the mount record and link it to the vfs struct
171      */
172     mi = &coda_mnttbl[minor(dev)];
173     
174     if (!VC_OPEN(&mi->mi_vcomm)) {
175         MARK_INT_FAIL(CODA_MOUNT_STATS);
176         return(ENODEV);
177     }
178     
179     /* No initialization (here) of mi_vcomm! */
180     vfsp->mnt_data = (qaddr_t)mi;
181     vfs_getnewfsid (vfsp);
182
183     mi->mi_vfsp = vfsp;
184     
185     /*
186      * Make a root vnode to placate the Vnode interface, but don't
187      * actually make the CODA_ROOT call to venus until the first call
188      * to coda_root in case a server is down while venus is starting.
189      */
190     rootfid.Volume = 0;
191     rootfid.Vnode = 0;
192     rootfid.Unique = 0;
193     cp = make_coda_node(&rootfid, vfsp, VDIR);
194     rootvp = CTOV(cp);
195     rootvp->v_flag |= VROOT;
196         
197     ctlfid.Volume = CTL_VOL;
198     ctlfid.Vnode = CTL_VNO;
199     ctlfid.Unique = CTL_UNI;
200 /*  cp = make_coda_node(&ctlfid, vfsp, VCHR);
201     The above code seems to cause a loop in the cnode links.
202     I don't totally understand when it happens, it is caught
203     when closing down the system.
204  */
205     cp = make_coda_node(&ctlfid, 0, VCHR);
206
207     coda_ctlvp = CTOV(cp);
208
209     /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
210     mi->mi_vfsp = vfsp;
211     mi->mi_rootvp = rootvp;
212     
213     /* set filesystem block size */
214     vfsp->mnt_stat.f_bsize = 8192;          /* XXX -JJK */
215
216     /* Set f_iosize.  XXX -- inamura@isl.ntt.co.jp. 
217        For vnode_pager_haspage() references. The value should be obtained 
218        from underlying UFS. */
219     /* Checked UFS. iosize is set as 8192 */
220     vfsp->mnt_stat.f_iosize = 8192;
221
222     /* error is currently guaranteed to be zero, but in case some
223        code changes... */
224     CODADEBUG(1,
225              myprintf(("coda_mount returned %d\n",error)););
226     if (error)
227         MARK_INT_FAIL(CODA_MOUNT_STATS);
228     else
229         MARK_INT_SAT(CODA_MOUNT_STATS);
230     
231     return(error);
232 }
233
234 int
235 coda_unmount(vfsp, mntflags, td)
236     struct mount *vfsp;
237     int mntflags;
238     struct thread *td;
239 {
240     struct coda_mntinfo *mi = vftomi(vfsp);
241     int active, error = 0;
242     
243     ENTRY;
244     MARK_ENTRY(CODA_UMOUNT_STATS);
245     if (!CODA_MOUNTED(vfsp)) {
246         MARK_INT_FAIL(CODA_UMOUNT_STATS);
247         return(EINVAL);
248     }
249     
250     if (mi->mi_vfsp == vfsp) {  /* We found the victim */
251         if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
252             return (EBUSY);     /* Venus is still running */
253
254 #ifdef  DEBUG
255         printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
256 #endif
257         vrele(mi->mi_rootvp);
258
259         active = coda_kill(vfsp, NOT_DOWNCALL);
260         mi->mi_rootvp->v_flag &= ~VROOT;
261         error = vflush(mi->mi_vfsp, 0, FORCECLOSE);
262         printf("coda_unmount: active = %d, vflush active %d\n", active, error);
263         error = 0;
264         /* I'm going to take this out to allow lookups to go through. I'm
265          * not sure it's important anyway. -- DCS 2/2/94
266          */
267         /* vfsp->VFS_DATA = NULL; */
268
269         /* No more vfsp's to hold onto */
270         mi->mi_vfsp = NULL;
271         mi->mi_rootvp = NULL;
272
273         if (error)
274             MARK_INT_FAIL(CODA_UMOUNT_STATS);
275         else
276             MARK_INT_SAT(CODA_UMOUNT_STATS);
277
278         return(error);
279     }
280     return (EINVAL);
281 }
282
283 /*
284  * find root of cfs
285  */
286 int
287 coda_root(vfsp, vpp)
288         struct mount *vfsp;
289         struct vnode **vpp;
290 {
291     struct coda_mntinfo *mi = vftomi(vfsp);
292     struct vnode **result;
293     int error;
294     struct thread *td = curthread;    /* XXX - bnoble */
295     struct proc *p = td->td_proc;
296     ViceFid VFid;
297  
298     ENTRY;
299     MARK_ENTRY(CODA_ROOT_STATS);
300     result = NULL;
301     
302     if (vfsp == mi->mi_vfsp) {
303         if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
304             (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
305             (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
306             { /* Found valid root. */
307                 *vpp = mi->mi_rootvp;
308                 /* On Mach, this is vref.  On NetBSD, VOP_LOCK */
309 #if     1
310                 vref(*vpp);
311                 vn_lock(*vpp, LK_EXCLUSIVE, td);
312 #else
313                 vget(*vpp, LK_EXCLUSIVE, td);
314 #endif
315                 MARK_INT_SAT(CODA_ROOT_STATS);
316                 return(0);
317             }
318     }
319
320     error = venus_root(vftomi(vfsp), td->td_ucred, p, &VFid);
321
322     if (!error) {
323         /*
324          * Save the new rootfid in the cnode, and rehash the cnode into the
325          * cnode hash with the new fid key.
326          */
327         coda_unsave(VTOC(mi->mi_rootvp));
328         VTOC(mi->mi_rootvp)->c_fid = VFid;
329         coda_save(VTOC(mi->mi_rootvp));
330
331         *vpp = mi->mi_rootvp;
332 #if     1
333         vref(*vpp);
334         vn_lock(*vpp, LK_EXCLUSIVE, td);
335 #else
336         vget(*vpp, LK_EXCLUSIVE, td);
337 #endif
338
339         MARK_INT_SAT(CODA_ROOT_STATS);
340         goto exit;
341     } else if (error == ENODEV || error == EINTR) {
342         /* Gross hack here! */
343         /*
344          * If Venus fails to respond to the CODA_ROOT call, coda_call returns
345          * ENODEV. Return the uninitialized root vnode to allow vfs
346          * operations such as unmount to continue. Without this hack,
347          * there is no way to do an unmount if Venus dies before a 
348          * successful CODA_ROOT call is done. All vnode operations 
349          * will fail.
350          */
351         *vpp = mi->mi_rootvp;
352 #if     1
353         vref(*vpp);
354         vn_lock(*vpp, LK_EXCLUSIVE, td);
355 #else
356         vget(*vpp, LK_EXCLUSIVE, td);
357 #endif
358
359         MARK_INT_FAIL(CODA_ROOT_STATS);
360         error = 0;
361         goto exit;
362     } else {
363         CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
364         MARK_INT_FAIL(CODA_ROOT_STATS);
365                 
366         goto exit;
367     }
368
369  exit:
370     return(error);
371 }
372
373 /*
374  * Get file system statistics.
375  */
376 int
377 coda_nb_statfs(vfsp, sbp, td)
378     register struct mount *vfsp;
379     struct statfs *sbp;
380     struct thread *td;
381 {
382     ENTRY;
383 /*  MARK_ENTRY(CODA_STATFS_STATS); */
384     if (!CODA_MOUNTED(vfsp)) {
385 /*      MARK_INT_FAIL(CODA_STATFS_STATS);*/
386         return(EINVAL);
387     }
388     
389     bzero(sbp, sizeof(struct statfs));
390     /* XXX - what to do about f_flags, others? --bnoble */
391     /* Below This is what AFS does
392         #define NB_SFS_SIZ 0x895440
393      */
394     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
395     sbp->f_type = vfsp->mnt_vfc->vfc_typenum;
396     sbp->f_bsize = 8192; /* XXX */
397     sbp->f_iosize = 8192; /* XXX */
398 #define NB_SFS_SIZ 0x8AB75D
399     sbp->f_blocks = NB_SFS_SIZ;
400     sbp->f_bfree = NB_SFS_SIZ;
401     sbp->f_bavail = NB_SFS_SIZ;
402     sbp->f_files = NB_SFS_SIZ;
403     sbp->f_ffree = NB_SFS_SIZ;
404     bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
405     snprintf(sbp->f_mntonname, sizeof(sbp->f_mntonname), "/coda");
406     snprintf(sbp->f_mntfromname, sizeof(sbp->f_mntfromname), "CODA");
407 /*  MARK_INT_SAT(CODA_STATFS_STATS); */
408     return(0);
409 }
410
411 /*
412  * Flush any pending I/O.
413  */
414 int
415 coda_sync(vfsp, waitfor, cred, td)
416     struct mount *vfsp;
417     int    waitfor;
418     struct ucred *cred;
419     struct thread *td;
420 {
421     ENTRY;
422     MARK_ENTRY(CODA_SYNC_STATS);
423     MARK_INT_SAT(CODA_SYNC_STATS);
424     return(0);
425 }
426
427 /* 
428  * fhtovp is now what vget used to be in 4.3-derived systems.  For
429  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
430  * a type-specific fid.  
431  */
432 int
433 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
434     register struct mount *vfsp;    
435     struct fid *fhp;
436     struct mbuf *nam;
437     struct vnode **vpp;
438     int *exflagsp;
439     struct ucred **creadanonp;
440 {
441     struct cfid *cfid = (struct cfid *)fhp;
442     struct cnode *cp = 0;
443     int error;
444     struct thread *td = curthread; /* XXX -mach */
445     struct proc *p = td->td_proc;
446     ViceFid VFid;
447     int vtype;
448
449     ENTRY;
450     
451     MARK_ENTRY(CODA_VGET_STATS);
452     /* Check for vget of control object. */
453     if (IS_CTL_FID(&cfid->cfid_fid)) {
454         *vpp = coda_ctlvp;
455         vref(coda_ctlvp);
456         MARK_INT_SAT(CODA_VGET_STATS);
457         return(0);
458     }
459     
460     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, td->td_ucred, p, &VFid, &vtype);
461     
462     if (error) {
463         CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
464             *vpp = (struct vnode *)0;
465     } else {
466         CODADEBUG(CODA_VGET, 
467                  myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
468                         VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
469             
470         cp = make_coda_node(&VFid, vfsp, vtype);
471         *vpp = CTOV(cp);
472     }
473     return(error);
474 }
475
476 /*
477  * To allow for greater ease of use, some vnodes may be orphaned when
478  * Venus dies.  Certain operations should still be allowed to go
479  * through, but without propagating ophan-ness.  So this function will
480  * get a new vnode for the file from the current run of Venus.  */
481  
482 int
483 getNewVnode(vpp)
484      struct vnode **vpp;
485 {
486     struct cfid cfid;
487     struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
488     
489     ENTRY;
490
491     cfid.cfid_len = (short)sizeof(ViceFid);
492     cfid.cfid_fid = VTOC(*vpp)->c_fid;  /* Structure assignment. */
493     /* XXX ? */
494
495     /* We're guessing that if set, the 1st element on the list is a
496      * valid vnode to use. If not, return ENODEV as venus is dead.
497      */
498     if (mi->mi_vfsp == NULL)
499         return ENODEV;
500     
501     return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
502                       NULL, NULL);
503 }
504
505 #include <ufs/ufs/extattr.h>
506 #include <ufs/ufs/quota.h>
507 #include <ufs/ufs/ufsmount.h>
508 /* get the mount structure corresponding to a given device.  Assume 
509  * device corresponds to a UFS. Return NULL if no device is found.
510  */ 
511 struct mount *devtomp(dev)
512     dev_t dev;
513 {
514     struct mount *mp;
515    
516     TAILQ_FOREACH(mp, &mountlist, mnt_list) {
517         if (((VFSTOUFS(mp))->um_dev == dev)) {
518             /* mount corresponds to UFS and the device matches one we want */
519             return(mp); 
520         }
521     }
522     /* mount structure wasn't found */ 
523     return(NULL); 
524 }
525
526 struct vfsops coda_vfsops = {
527     coda_mount,
528     vfs_stdstart,
529     coda_unmount,
530     coda_root,
531     vfs_stdquotactl,
532     coda_nb_statfs,
533     coda_sync,
534     vfs_stdvget,
535     vfs_stdfhtovp,
536     vfs_stdcheckexp,
537     vfs_stdvptofh,
538     vfs_stdinit,
539     vfs_stduninit,
540     vfs_stdextattrctl,
541 };
542
543 VFS_SET(coda_vfsops, coda, VFCF_NETWORK);