1/*	$NetBSD: coda_vfsops.c,v 1.70 2011/09/27 00:54:47 christos Exp $	*/
2
3/*
4 *
5 *             Coda: an Experimental Distributed File System
6 *                              Release 3.1
7 *
8 *           Copyright (c) 1987-1998 Carnegie Mellon University
9 *                          All Rights Reserved
10 *
11 * Permission  to  use, copy, modify and distribute this software and its
12 * documentation is hereby granted,  provided  that  both  the  copyright
13 * notice  and  this  permission  notice  appear  in  all  copies  of the
14 * software, derivative works or  modified  versions,  and  any  portions
15 * thereof, and that both notices appear in supporting documentation, and
16 * that credit is given to Carnegie Mellon University  in  all  documents
17 * and publicity pertaining to direct or indirect use of this code or its
18 * derivatives.
19 *
20 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
21 * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
22 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
23 * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
24 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
25 * ANY DERIVATIVE WORK.
26 *
27 * Carnegie  Mellon  encourages  users  of  this  software  to return any
28 * improvements or extensions that  they  make,  and  to  grant  Carnegie
29 * Mellon the rights to redistribute these changes without encumbrance.
30 *
31 * 	@(#) cfs/coda_vfsops.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
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 <sys/cdefs.h>
48__KERNEL_RCSID(0, "$NetBSD: coda_vfsops.c,v 1.70 2011/09/27 00:54:47 christos Exp $");
49
50#ifndef _KERNEL_OPT
51#define	NVCODA 4
52#else
53#include <vcoda.h>
54#endif
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/sysctl.h>
59#include <sys/malloc.h>
60#include <sys/conf.h>
61#include <sys/namei.h>
62#include <sys/dirent.h>
63#include <sys/mount.h>
64#include <sys/proc.h>
65#include <sys/select.h>
66#include <sys/kauth.h>
67#include <sys/module.h>
68
69#include <coda/coda.h>
70#include <coda/cnode.h>
71#include <coda/coda_vfsops.h>
72#include <coda/coda_venus.h>
73#include <coda/coda_subr.h>
74#include <coda/coda_opstats.h>
75/* for VN_RDEV */
76#include <miscfs/specfs/specdev.h>
77#include <miscfs/genfs/genfs.h>
78
79MODULE(MODULE_CLASS_VFS, coda, NULL);
80
81MALLOC_DEFINE(M_CODA, "coda", "Coda file system structures and tables");
82
83int codadebug = 0;
84
85int coda_vfsop_print_entry = 0;
86#define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__func__))
87
88struct vnode *coda_ctlvp;
89struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
90
91/* structure to keep statistics of internally generated/satisfied calls */
92
93struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
94
95#define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
96#define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
97#define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
98#define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
99
100extern const struct cdevsw vcoda_cdevsw;
101extern const struct vnodeopv_desc coda_vnodeop_opv_desc;
102
103const struct vnodeopv_desc * const coda_vnodeopv_descs[] = {
104	&coda_vnodeop_opv_desc,
105	NULL,
106};
107
108struct vfsops coda_vfsops = {
109    MOUNT_CODA,
110    256,		/* This is the pathname, unlike every other fs */
111    coda_mount,
112    coda_start,
113    coda_unmount,
114    coda_root,
115    (void *)eopnotsupp,	/* vfs_quotactl */
116    coda_nb_statvfs,
117    coda_sync,
118    coda_vget,
119    (void *)eopnotsupp,	/* vfs_fhtovp */
120    (void *)eopnotsupp,	/* vfs_vptofh */
121    coda_init,
122    NULL,		/* vfs_reinit */
123    coda_done,
124    (int (*)(void)) eopnotsupp,
125    (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
126    vfs_stdextattrctl,
127    (void *)eopnotsupp,	/* vfs_suspendctl */
128    genfs_renamelock_enter,
129    genfs_renamelock_exit,
130	(void *)eopnotsupp,
131    coda_vnodeopv_descs,
132    0,			/* vfs_refcount */
133    { NULL, NULL },	/* vfs_list */
134};
135
136static int
137coda_modcmd(modcmd_t cmd, void *arg)
138{
139
140	switch (cmd) {
141	case MODULE_CMD_INIT:
142		return vfs_attach(&coda_vfsops);
143	case MODULE_CMD_FINI:
144		return vfs_detach(&coda_vfsops);
145	default:
146		return ENOTTY;
147	}
148}
149
150int
151coda_vfsopstats_init(void)
152{
153	int i;
154
155	for (i=0;i<CODA_VFSOPS_SIZE;i++) {
156		coda_vfsopstats[i].opcode = i;
157		coda_vfsopstats[i].entries = 0;
158		coda_vfsopstats[i].sat_intrn = 0;
159		coda_vfsopstats[i].unsat_intrn = 0;
160		coda_vfsopstats[i].gen_intrn = 0;
161	}
162
163	return 0;
164}
165
166/*
167 * cfs mount vfsop
168 * Set up mount info record and attach it to vfs struct.
169 */
170/*ARGSUSED*/
171int
172coda_mount(struct mount *vfsp,	/* Allocated and initialized by mount(2) */
173    const char *path,	/* path covered: ignored by the fs-layer */
174    void *data,		/* Need to define a data type for this in netbsd? */
175    size_t *data_len)
176{
177    struct lwp *l = curlwp;
178    struct vnode *dvp;
179    struct cnode *cp;
180    dev_t dev;
181    struct coda_mntinfo *mi;
182    struct vnode *rtvp;
183    const struct cdevsw *cdev;
184    CodaFid rootfid = INVAL_FID;
185    CodaFid ctlfid = CTL_FID;
186    int error;
187
188    if (data == NULL)
189	return EINVAL;
190    if (vfsp->mnt_flag & MNT_GETARGS)
191	return EINVAL;
192    ENTRY;
193
194    coda_vfsopstats_init();
195    coda_vnodeopstats_init();
196
197    MARK_ENTRY(CODA_MOUNT_STATS);
198    if (CODA_MOUNTED(vfsp)) {
199	MARK_INT_FAIL(CODA_MOUNT_STATS);
200	return(EBUSY);
201    }
202
203    /* Validate mount device.  Similar to getmdev(). */
204
205    /*
206     * XXX: coda passes the mount device as the entire mount args,
207     * All other fs pass a structure contining a pointer.
208     * In order to get sys_mount() to do the copyin() we've set a
209     * fixed default size for the filename buffer.
210     */
211    /* Ensure that namei() doesn't run off the filename buffer */
212    ((char *)data)[*data_len - 1] = 0;
213    error = namei_simple_kernel((char *)data, NSM_FOLLOW_NOEMULROOT,
214		&dvp);
215
216    if (error) {
217	MARK_INT_FAIL(CODA_MOUNT_STATS);
218	return (error);
219    }
220    if (dvp->v_type != VCHR) {
221	MARK_INT_FAIL(CODA_MOUNT_STATS);
222	vrele(dvp);
223	return(ENXIO);
224    }
225    dev = dvp->v_rdev;
226    vrele(dvp);
227    cdev = cdevsw_lookup(dev);
228    if (cdev == NULL) {
229	MARK_INT_FAIL(CODA_MOUNT_STATS);
230	return(ENXIO);
231    }
232
233    /*
234     * See if the device table matches our expectations.
235     */
236    if (cdev != &vcoda_cdevsw)
237    {
238	MARK_INT_FAIL(CODA_MOUNT_STATS);
239	return(ENXIO);
240    }
241
242    if (minor(dev) >= NVCODA) {
243	MARK_INT_FAIL(CODA_MOUNT_STATS);
244	return(ENXIO);
245    }
246
247    /*
248     * Initialize the mount record and link it to the vfs struct
249     */
250    mi = &coda_mnttbl[minor(dev)];
251
252    if (!VC_OPEN(&mi->mi_vcomm)) {
253	MARK_INT_FAIL(CODA_MOUNT_STATS);
254	return(ENODEV);
255    }
256
257    /* No initialization (here) of mi_vcomm! */
258    vfsp->mnt_data = mi;
259    vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0;
260    vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA);
261    vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0];
262    vfsp->mnt_stat.f_namemax = CODA_MAXNAMLEN;
263    mi->mi_vfsp = vfsp;
264
265    /*
266     * Make a root vnode to placate the Vnode interface, but don't
267     * actually make the CODA_ROOT call to venus until the first call
268     * to coda_root in case a server is down while venus is starting.
269     */
270    cp = make_coda_node(&rootfid, vfsp, VDIR);
271    rtvp = CTOV(cp);
272    rtvp->v_vflag |= VV_ROOT;
273
274/*  cp = make_coda_node(&ctlfid, vfsp, VCHR);
275    The above code seems to cause a loop in the cnode links.
276    I don't totally understand when it happens, it is caught
277    when closing down the system.
278 */
279    cp = make_coda_node(&ctlfid, 0, VCHR);
280
281    coda_ctlvp = CTOV(cp);
282
283    /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
284    mi->mi_vfsp = vfsp;
285    mi->mi_rootvp = rtvp;
286
287    /* set filesystem block size */
288    vfsp->mnt_stat.f_bsize = 8192;	    /* XXX -JJK */
289    vfsp->mnt_stat.f_frsize = 8192;	    /* XXX -JJK */
290
291    /* error is currently guaranteed to be zero, but in case some
292       code changes... */
293    CODADEBUG(1,
294	     myprintf(("coda_mount returned %d\n",error)););
295    if (error)
296	MARK_INT_FAIL(CODA_MOUNT_STATS);
297    else
298	MARK_INT_SAT(CODA_MOUNT_STATS);
299
300    return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE,
301	vfsp->mnt_op->vfs_name, vfsp, l);
302}
303
304int
305coda_start(struct mount *vfsp, int flags)
306{
307    ENTRY;
308    vftomi(vfsp)->mi_started = 1;
309    return (0);
310}
311
312int
313coda_unmount(struct mount *vfsp, int mntflags)
314{
315    struct coda_mntinfo *mi = vftomi(vfsp);
316    int active, error = 0;
317
318    ENTRY;
319    MARK_ENTRY(CODA_UMOUNT_STATS);
320    if (!CODA_MOUNTED(vfsp)) {
321	MARK_INT_FAIL(CODA_UMOUNT_STATS);
322	return(EINVAL);
323    }
324
325    if (mi->mi_vfsp == vfsp) {	/* We found the victim */
326	if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
327	    return (EBUSY); 	/* Venus is still running */
328
329#ifdef	DEBUG
330	printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
331#endif
332	mi->mi_started = 0;
333
334	vrele(mi->mi_rootvp);
335
336	active = coda_kill(vfsp, NOT_DOWNCALL);
337	mi->mi_rootvp->v_vflag &= ~VV_ROOT;
338	error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE);
339	printf("coda_unmount: active = %d, vflush active %d\n", active, error);
340	error = 0;
341
342	/* I'm going to take this out to allow lookups to go through. I'm
343	 * not sure it's important anyway. -- DCS 2/2/94
344	 */
345	/* vfsp->VFS_DATA = NULL; */
346
347	/* No more vfsp's to hold onto */
348	mi->mi_vfsp = NULL;
349	mi->mi_rootvp = NULL;
350
351	if (error)
352	    MARK_INT_FAIL(CODA_UMOUNT_STATS);
353	else
354	    MARK_INT_SAT(CODA_UMOUNT_STATS);
355
356	return(error);
357    }
358    return (EINVAL);
359}
360
361/*
362 * find root of cfs
363 */
364int
365coda_root(struct mount *vfsp, struct vnode **vpp)
366{
367    struct coda_mntinfo *mi = vftomi(vfsp);
368    int error;
369    struct lwp *l = curlwp;    /* XXX - bnoble */
370    CodaFid VFid;
371    static const CodaFid invalfid = INVAL_FID;
372
373    ENTRY;
374    MARK_ENTRY(CODA_ROOT_STATS);
375
376    if (vfsp == mi->mi_vfsp) {
377    	if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid, sizeof(CodaFid)))
378	    { /* Found valid root. */
379		*vpp = mi->mi_rootvp;
380		/* On Mach, this is vref.  On NetBSD, VOP_LOCK */
381		vref(*vpp);
382		vn_lock(*vpp, LK_EXCLUSIVE);
383		MARK_INT_SAT(CODA_ROOT_STATS);
384		return(0);
385	    }
386    }
387
388    error = venus_root(vftomi(vfsp), l->l_cred, l->l_proc, &VFid);
389
390    if (!error) {
391	/*
392	 * Save the new rootfid in the cnode, and rehash the cnode into the
393	 * cnode hash with the new fid key.
394	 */
395	coda_unsave(VTOC(mi->mi_rootvp));
396	VTOC(mi->mi_rootvp)->c_fid = VFid;
397	coda_save(VTOC(mi->mi_rootvp));
398
399	*vpp = mi->mi_rootvp;
400	vref(*vpp);
401	vn_lock(*vpp, LK_EXCLUSIVE);
402	MARK_INT_SAT(CODA_ROOT_STATS);
403	goto exit;
404    } else if (error == ENODEV || error == EINTR) {
405	/* Gross hack here! */
406	/*
407	 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
408	 * ENODEV. Return the uninitialized root vnode to allow vfs
409	 * operations such as unmount to continue. Without this hack,
410	 * there is no way to do an unmount if Venus dies before a
411	 * successful CODA_ROOT call is done. All vnode operations
412	 * will fail.
413	 */
414	*vpp = mi->mi_rootvp;
415	vref(*vpp);
416	vn_lock(*vpp, LK_EXCLUSIVE);
417	MARK_INT_FAIL(CODA_ROOT_STATS);
418	error = 0;
419	goto exit;
420    } else {
421	CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
422	MARK_INT_FAIL(CODA_ROOT_STATS);
423
424	goto exit;
425    }
426 exit:
427    return(error);
428}
429
430/*
431 * Get file system statistics.
432 */
433int
434coda_nb_statvfs(struct mount *vfsp, struct statvfs *sbp)
435{
436    struct lwp *l = curlwp;
437    struct coda_statfs fsstat;
438    int error;
439
440    ENTRY;
441    MARK_ENTRY(CODA_STATFS_STATS);
442    if (!CODA_MOUNTED(vfsp)) {
443/*	MARK_INT_FAIL(CODA_STATFS_STATS); */
444	return(EINVAL);
445    }
446
447    /* XXX - what to do about f_flags, others? --bnoble */
448    /* Below This is what AFS does
449    	#define NB_SFS_SIZ 0x895440
450     */
451    /* Note: Normal fs's have a bsize of 0x400 == 1024 */
452
453    error = venus_statfs(vftomi(vfsp), l->l_cred, l, &fsstat);
454
455    if (!error) {
456	sbp->f_bsize = 8192; /* XXX */
457	sbp->f_frsize = 8192; /* XXX */
458	sbp->f_iosize = 8192; /* XXX */
459	sbp->f_blocks = fsstat.f_blocks;
460	sbp->f_bfree  = fsstat.f_bfree;
461	sbp->f_bavail = fsstat.f_bavail;
462	sbp->f_bresvd = 0;
463	sbp->f_files  = fsstat.f_files;
464	sbp->f_ffree  = fsstat.f_ffree;
465	sbp->f_favail = fsstat.f_ffree;
466	sbp->f_fresvd = 0;
467	copy_statvfs_info(sbp, vfsp);
468    }
469
470    MARK_INT_SAT(CODA_STATFS_STATS);
471    return(error);
472}
473
474/*
475 * Flush any pending I/O.
476 */
477int
478coda_sync(struct mount *vfsp, int waitfor,
479    kauth_cred_t cred)
480{
481    ENTRY;
482    MARK_ENTRY(CODA_SYNC_STATS);
483    MARK_INT_SAT(CODA_SYNC_STATS);
484    return(0);
485}
486
487int
488coda_vget(struct mount *vfsp, ino_t ino,
489    struct vnode **vpp)
490{
491    ENTRY;
492    return (EOPNOTSUPP);
493}
494
495/*
496 * fhtovp is now what vget used to be in 4.3-derived systems.  For
497 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
498 * a type-specific fid.
499 */
500int
501coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam,
502    struct vnode **vpp, int *exflagsp,
503    kauth_cred_t *creadanonp)
504{
505    struct cfid *cfid = (struct cfid *)fhp;
506    struct cnode *cp = 0;
507    int error;
508    struct lwp *l = curlwp; /* XXX -mach */
509    CodaFid VFid;
510    int vtype;
511
512    ENTRY;
513
514    MARK_ENTRY(CODA_VGET_STATS);
515    /* Check for vget of control object. */
516    if (IS_CTL_FID(&cfid->cfid_fid)) {
517	*vpp = coda_ctlvp;
518	vref(coda_ctlvp);
519	MARK_INT_SAT(CODA_VGET_STATS);
520	return(0);
521    }
522
523    error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, l->l_cred, l->l_proc, &VFid, &vtype);
524
525    if (error) {
526	CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
527	    *vpp = (struct vnode *)0;
528    } else {
529	CODADEBUG(CODA_VGET,
530		 myprintf(("vget: %s type %d result %d\n",
531			coda_f2s(&VFid), vtype, error)); )
532
533	cp = make_coda_node(&VFid, vfsp, vtype);
534	*vpp = CTOV(cp);
535    }
536    return(error);
537}
538
539int
540coda_vptofh(struct vnode *vnp, struct fid *fidp)
541{
542    ENTRY;
543    return (EOPNOTSUPP);
544}
545
546void
547coda_init(void)
548{
549    ENTRY;
550}
551
552void
553coda_done(void)
554{
555    ENTRY;
556}
557
558SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup")
559{
560	sysctl_createv(clog, 0, NULL, NULL,
561		       CTLFLAG_PERMANENT,
562		       CTLTYPE_NODE, "vfs", NULL,
563		       NULL, 0, NULL, 0,
564		       CTL_VFS, CTL_EOL);
565	sysctl_createv(clog, 0, NULL, NULL,
566		       CTLFLAG_PERMANENT,
567		       CTLTYPE_NODE, "coda",
568		       SYSCTL_DESCR("code vfs options"),
569		       NULL, 0, NULL, 0,
570		       CTL_VFS, 18, CTL_EOL);
571	/*
572	 * XXX the "18" above could be dynamic, thereby eliminating
573	 * one more instance of the "number to vfs" mapping problem,
574	 * but "18" is the order as taken from sys/mount.h
575	 */
576
577/*
578	sysctl_createv(clog, 0, NULL, NULL,
579		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
580		       CTLTYPE_INT, "clusterread",
581		       SYSCTL_DESCR( anyone? ),
582		       NULL, 0, &doclusterread, 0,
583		       CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL);
584*/
585}
586
587/*
588 * To allow for greater ease of use, some vnodes may be orphaned when
589 * Venus dies.  Certain operations should still be allowed to go
590 * through, but without propagating orphan-ness.  So this function will
591 * get a new vnode for the file from the current run of Venus.
592 */
593
594int
595getNewVnode(struct vnode **vpp)
596{
597    struct cfid cfid;
598    struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
599
600    ENTRY;
601
602    cfid.cfid_len = (short)sizeof(CodaFid);
603    cfid.cfid_fid = VTOC(*vpp)->c_fid;	/* Structure assignment. */
604    /* XXX ? */
605
606    /* We're guessing that if set, the 1st element on the list is a
607     * valid vnode to use. If not, return ENODEV as venus is dead.
608     */
609    if (mi->mi_vfsp == NULL)
610	return ENODEV;
611
612    return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
613		      NULL, NULL);
614}
615
616#include <ufs/ufs/quota.h>
617#include <ufs/ufs/ufsmount.h>
618/* get the mount structure corresponding to a given device.  Assume
619 * device corresponds to a UFS. Return NULL if no device is found.
620 */
621struct mount *devtomp(dev_t dev)
622{
623    struct mount *mp, *nmp;
624
625    for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) {
626	nmp = mp->mnt_list.cqe_next;
627	if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) &&
628	    ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) {
629	    /* mount corresponds to UFS and the device matches one we want */
630	    return(mp);
631	}
632    }
633    /* mount structure wasn't found */
634    return(NULL);
635}
636