Optimize lwkt_rwlock.c a bit
[dragonfly.git] / sys / vfs / coda / coda_vfsops.c
CommitLineData
984263bc
MD
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 $
1de703da 31 * $DragonFly: src/sys/vfs/coda/Attic/coda_vfsops.c,v 1.2 2003/06/17 04:28:19 dillon Exp $
984263bc
MD
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
69MALLOC_DEFINE(M_CODA, "CODA storage", "Various Coda Structures");
70
71int codadebug = 0;
72int coda_vfsop_print_entry = 0;
73#define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
74
75struct vnode *coda_ctlvp;
76struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
77
78/* structure to keep statistics of internally generated/satisfied calls */
79
80struct 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
87extern int coda_nc_initialized; /* Set if cache has been initialized */
88extern int vc_nb_open __P((dev_t, int, int, struct proc *));
89
90int
91coda_vfsopstats_init(void)
92{
93 register 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*/
111int
112coda_mount(vfsp, path, data, ndp, p)
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 proc *p; /* 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, p);
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 /*
159 * See if the device table matches our expectations.
160 */
161 if (devsw(dev)->d_open != vc_nb_open)
162 {
163 MARK_INT_FAIL(CODA_MOUNT_STATS);
164 return(ENXIO);
165 }
166
167 if (minor(dev) >= NVCODA || minor(dev) < 0) {
168 MARK_INT_FAIL(CODA_MOUNT_STATS);
169 return(ENXIO);
170 }
171
172 /*
173 * Initialize the mount record and link it to the vfs struct
174 */
175 mi = &coda_mnttbl[minor(dev)];
176
177 if (!VC_OPEN(&mi->mi_vcomm)) {
178 MARK_INT_FAIL(CODA_MOUNT_STATS);
179 return(ENODEV);
180 }
181
182 /* No initialization (here) of mi_vcomm! */
183 vfsp->mnt_data = (qaddr_t)mi;
184 vfs_getnewfsid (vfsp);
185
186 mi->mi_vfsp = vfsp;
187
188 /*
189 * Make a root vnode to placate the Vnode interface, but don't
190 * actually make the CODA_ROOT call to venus until the first call
191 * to coda_root in case a server is down while venus is starting.
192 */
193 rootfid.Volume = 0;
194 rootfid.Vnode = 0;
195 rootfid.Unique = 0;
196 cp = make_coda_node(&rootfid, vfsp, VDIR);
197 rootvp = CTOV(cp);
198 rootvp->v_flag |= VROOT;
199
200 ctlfid.Volume = CTL_VOL;
201 ctlfid.Vnode = CTL_VNO;
202 ctlfid.Unique = CTL_UNI;
203/* cp = make_coda_node(&ctlfid, vfsp, VCHR);
204 The above code seems to cause a loop in the cnode links.
205 I don't totally understand when it happens, it is caught
206 when closing down the system.
207 */
208 cp = make_coda_node(&ctlfid, 0, VCHR);
209
210 coda_ctlvp = CTOV(cp);
211
212 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
213 mi->mi_vfsp = vfsp;
214 mi->mi_rootvp = rootvp;
215
216 /* set filesystem block size */
217 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */
218
219 /* Set f_iosize. XXX -- inamura@isl.ntt.co.jp.
220 For vnode_pager_haspage() references. The value should be obtained
221 from underlying UFS. */
222 /* Checked UFS. iosize is set as 8192 */
223 vfsp->mnt_stat.f_iosize = 8192;
224
225 /* error is currently guaranteed to be zero, but in case some
226 code changes... */
227 CODADEBUG(1,
228 myprintf(("coda_mount returned %d\n",error)););
229 if (error)
230 MARK_INT_FAIL(CODA_MOUNT_STATS);
231 else
232 MARK_INT_SAT(CODA_MOUNT_STATS);
233
234 return(error);
235}
236
237int
238coda_unmount(vfsp, mntflags, p)
239 struct mount *vfsp;
240 int mntflags;
241 struct proc *p;
242{
243 struct coda_mntinfo *mi = vftomi(vfsp);
244 int active, error = 0;
245
246 ENTRY;
247 MARK_ENTRY(CODA_UMOUNT_STATS);
248 if (!CODA_MOUNTED(vfsp)) {
249 MARK_INT_FAIL(CODA_UMOUNT_STATS);
250 return(EINVAL);
251 }
252
253 if (mi->mi_vfsp == vfsp) { /* We found the victim */
254 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
255 return (EBUSY); /* Venus is still running */
256
257#ifdef DEBUG
258 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
259#endif
260 vrele(mi->mi_rootvp);
261
262 active = coda_kill(vfsp, NOT_DOWNCALL);
263 mi->mi_rootvp->v_flag &= ~VROOT;
264 error = vflush(mi->mi_vfsp, 0, FORCECLOSE);
265 printf("coda_unmount: active = %d, vflush active %d\n", active, error);
266 error = 0;
267 /* I'm going to take this out to allow lookups to go through. I'm
268 * not sure it's important anyway. -- DCS 2/2/94
269 */
270 /* vfsp->VFS_DATA = NULL; */
271
272 /* No more vfsp's to hold onto */
273 mi->mi_vfsp = NULL;
274 mi->mi_rootvp = NULL;
275
276 if (error)
277 MARK_INT_FAIL(CODA_UMOUNT_STATS);
278 else
279 MARK_INT_SAT(CODA_UMOUNT_STATS);
280
281 return(error);
282 }
283 return (EINVAL);
284}
285
286/*
287 * find root of cfs
288 */
289int
290coda_root(vfsp, vpp)
291 struct mount *vfsp;
292 struct vnode **vpp;
293{
294 struct coda_mntinfo *mi = vftomi(vfsp);
295 struct vnode **result;
296 int error;
297 struct proc *p = curproc; /* XXX - bnoble */
298 ViceFid VFid;
299
300 ENTRY;
301 MARK_ENTRY(CODA_ROOT_STATS);
302 result = NULL;
303
304 if (vfsp == mi->mi_vfsp) {
305 if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
306 (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
307 (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
308 { /* Found valid root. */
309 *vpp = mi->mi_rootvp;
310 /* On Mach, this is vref. On NetBSD, VOP_LOCK */
311#if 1
312 vref(*vpp);
313 vn_lock(*vpp, LK_EXCLUSIVE, p);
314#else
315 vget(*vpp, LK_EXCLUSIVE, p);
316#endif
317 MARK_INT_SAT(CODA_ROOT_STATS);
318 return(0);
319 }
320 }
321
322 error = venus_root(vftomi(vfsp), p->p_ucred, p, &VFid);
323
324 if (!error) {
325 /*
326 * Save the new rootfid in the cnode, and rehash the cnode into the
327 * cnode hash with the new fid key.
328 */
329 coda_unsave(VTOC(mi->mi_rootvp));
330 VTOC(mi->mi_rootvp)->c_fid = VFid;
331 coda_save(VTOC(mi->mi_rootvp));
332
333 *vpp = mi->mi_rootvp;
334#if 1
335 vref(*vpp);
336 vn_lock(*vpp, LK_EXCLUSIVE, p);
337#else
338 vget(*vpp, LK_EXCLUSIVE, p);
339#endif
340
341 MARK_INT_SAT(CODA_ROOT_STATS);
342 goto exit;
343 } else if (error == ENODEV || error == EINTR) {
344 /* Gross hack here! */
345 /*
346 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
347 * ENODEV. Return the uninitialized root vnode to allow vfs
348 * operations such as unmount to continue. Without this hack,
349 * there is no way to do an unmount if Venus dies before a
350 * successful CODA_ROOT call is done. All vnode operations
351 * will fail.
352 */
353 *vpp = mi->mi_rootvp;
354#if 1
355 vref(*vpp);
356 vn_lock(*vpp, LK_EXCLUSIVE, p);
357#else
358 vget(*vpp, LK_EXCLUSIVE, p);
359#endif
360
361 MARK_INT_FAIL(CODA_ROOT_STATS);
362 error = 0;
363 goto exit;
364 } else {
365 CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
366 MARK_INT_FAIL(CODA_ROOT_STATS);
367
368 goto exit;
369 }
370
371 exit:
372 return(error);
373}
374
375/*
376 * Get file system statistics.
377 */
378int
379coda_nb_statfs(vfsp, sbp, p)
380 register struct mount *vfsp;
381 struct statfs *sbp;
382 struct proc *p;
383{
384 ENTRY;
385/* MARK_ENTRY(CODA_STATFS_STATS); */
386 if (!CODA_MOUNTED(vfsp)) {
387/* MARK_INT_FAIL(CODA_STATFS_STATS);*/
388 return(EINVAL);
389 }
390
391 bzero(sbp, sizeof(struct statfs));
392 /* XXX - what to do about f_flags, others? --bnoble */
393 /* Below This is what AFS does
394 #define NB_SFS_SIZ 0x895440
395 */
396 /* Note: Normal fs's have a bsize of 0x400 == 1024 */
397 sbp->f_type = vfsp->mnt_vfc->vfc_typenum;
398 sbp->f_bsize = 8192; /* XXX */
399 sbp->f_iosize = 8192; /* XXX */
400#define NB_SFS_SIZ 0x8AB75D
401 sbp->f_blocks = NB_SFS_SIZ;
402 sbp->f_bfree = NB_SFS_SIZ;
403 sbp->f_bavail = NB_SFS_SIZ;
404 sbp->f_files = NB_SFS_SIZ;
405 sbp->f_ffree = NB_SFS_SIZ;
406 bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
407 snprintf(sbp->f_mntonname, sizeof(sbp->f_mntonname), "/coda");
408 snprintf(sbp->f_mntfromname, sizeof(sbp->f_mntfromname), "CODA");
409/* MARK_INT_SAT(CODA_STATFS_STATS); */
410 return(0);
411}
412
413/*
414 * Flush any pending I/O.
415 */
416int
417coda_sync(vfsp, waitfor, cred, p)
418 struct mount *vfsp;
419 int waitfor;
420 struct ucred *cred;
421 struct proc *p;
422{
423 ENTRY;
424 MARK_ENTRY(CODA_SYNC_STATS);
425 MARK_INT_SAT(CODA_SYNC_STATS);
426 return(0);
427}
428
429/*
430 * fhtovp is now what vget used to be in 4.3-derived systems. For
431 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
432 * a type-specific fid.
433 */
434int
435coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
436 register struct mount *vfsp;
437 struct fid *fhp;
438 struct mbuf *nam;
439 struct vnode **vpp;
440 int *exflagsp;
441 struct ucred **creadanonp;
442{
443 struct cfid *cfid = (struct cfid *)fhp;
444 struct cnode *cp = 0;
445 int error;
446 struct proc *p = curproc; /* XXX -mach */
447 ViceFid VFid;
448 int vtype;
449
450 ENTRY;
451
452 MARK_ENTRY(CODA_VGET_STATS);
453 /* Check for vget of control object. */
454 if (IS_CTL_FID(&cfid->cfid_fid)) {
455 *vpp = coda_ctlvp;
456 vref(coda_ctlvp);
457 MARK_INT_SAT(CODA_VGET_STATS);
458 return(0);
459 }
460
461 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, p->p_ucred, p, &VFid, &vtype);
462
463 if (error) {
464 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
465 *vpp = (struct vnode *)0;
466 } else {
467 CODADEBUG(CODA_VGET,
468 myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
469 VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
470
471 cp = make_coda_node(&VFid, vfsp, vtype);
472 *vpp = CTOV(cp);
473 }
474 return(error);
475}
476
477/*
478 * To allow for greater ease of use, some vnodes may be orphaned when
479 * Venus dies. Certain operations should still be allowed to go
480 * through, but without propagating ophan-ness. So this function will
481 * get a new vnode for the file from the current run of Venus. */
482
483int
484getNewVnode(vpp)
485 struct vnode **vpp;
486{
487 struct cfid cfid;
488 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
489
490 ENTRY;
491
492 cfid.cfid_len = (short)sizeof(ViceFid);
493 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */
494 /* XXX ? */
495
496 /* We're guessing that if set, the 1st element on the list is a
497 * valid vnode to use. If not, return ENODEV as venus is dead.
498 */
499 if (mi->mi_vfsp == NULL)
500 return ENODEV;
501
502 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
503 NULL, NULL);
504}
505
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 */
511struct 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
526struct 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
543VFS_SET(coda_vfsops, coda, VFCF_NETWORK);