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