null_vnops.c revision 143630
1193323Sed/*-
2193323Sed * Copyright (c) 1992, 1993
3193323Sed *	The Regents of the University of California.  All rights reserved.
4193323Sed *
5193323Sed * This code is derived from software contributed to Berkeley by
6193323Sed * John Heidemann of the UCLA Ficus project.
7193323Sed *
8193323Sed * Redistribution and use in source and binary forms, with or without
9193323Sed * modification, are permitted provided that the following conditions
10193323Sed * are met:
11193323Sed * 1. Redistributions of source code must retain the above copyright
12193323Sed *    notice, this list of conditions and the following disclaimer.
13193323Sed * 2. Redistributions in binary form must reproduce the above copyright
14193323Sed *    notice, this list of conditions and the following disclaimer in the
15193323Sed *    documentation and/or other materials provided with the distribution.
16193323Sed * 4. Neither the name of the University nor the names of its contributors
17193323Sed *    may be used to endorse or promote products derived from this software
18193323Sed *    without specific prior written permission.
19193323Sed *
20193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21198090Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30193323Sed * SUCH DAMAGE.
31234353Sdim *
32193323Sed *	@(#)null_vnops.c	8.6 (Berkeley) 5/27/95
33193323Sed *
34193323Sed * Ancestors:
35193323Sed *	@(#)lofs_vnops.c	1.2 (Berkeley) 6/18/92
36193323Sed *	...and...
37193323Sed *	@(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
38193323Sed *
39193323Sed * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 143630 2005-03-15 11:28:45Z jeff $
40193323Sed */
41193323Sed
42193323Sed/*
43193323Sed * Null Layer
44193323Sed *
45193323Sed * (See mount_nullfs(8) for more information.)
46193323Sed *
47193323Sed * The null layer duplicates a portion of the filesystem
48205218Srdivacky * name space under a new name.  In this respect, it is
49205218Srdivacky * similar to the loopback filesystem.  It differs from
50193323Sed * the loopback fs in two respects:  it is implemented using
51218885Sdim * a stackable layers techniques, and its "null-node"s stack above
52218885Sdim * all lower-layer vnodes, not just over directory vnodes.
53218885Sdim *
54193323Sed * The null layer has two purposes.  First, it serves as a demonstration
55193323Sed * of layering by proving a layer which does nothing.  (It actually
56193323Sed * does everything the loopback filesystem does, which is slightly
57193323Sed * more than nothing.)  Second, the null layer can serve as a prototype
58193323Sed * layer.  Since it provides all necessary layer framework,
59193323Sed * new filesystem layers can be created very easily be starting
60193323Sed * with a null layer.
61193323Sed *
62198090Srdivacky * The remainder of this man page examines the null layer as a basis
63198090Srdivacky * for constructing new layers.
64198090Srdivacky *
65198090Srdivacky *
66198090Srdivacky * INSTANTIATING NEW NULL LAYERS
67198090Srdivacky *
68193323Sed * New null layers are created with mount_nullfs(8).
69193323Sed * Mount_nullfs(8) takes two arguments, the pathname
70193323Sed * of the lower vfs (target-pn) and the pathname where the null
71193323Sed * layer will appear in the namespace (alias-pn).  After
72198090Srdivacky * the null layer is put into place, the contents
73198090Srdivacky * of target-pn subtree will be aliased under alias-pn.
74193323Sed *
75193323Sed *
76193323Sed * OPERATION OF A NULL LAYER
77193323Sed *
78193323Sed * The null layer is the minimum filesystem layer,
79193323Sed * simply bypassing all possible operations to the lower layer
80193323Sed * for processing there.  The majority of its activity centers
81193323Sed * on the bypass routine, through which nearly all vnode operations
82193323Sed * pass.
83193323Sed *
84193323Sed * The bypass routine accepts arbitrary vnode operations for
85193323Sed * handling by the lower layer.  It begins by examing vnode
86193323Sed * operation arguments and replacing any null-nodes by their
87193323Sed * lower-layer equivlants.  It then invokes the operation
88193323Sed * on the lower layer.  Finally, it replaces the null-nodes
89193323Sed * in the arguments and, if a vnode is return by the operation,
90218885Sdim * stacks a null-node on top of the returned vnode.
91193323Sed *
92193323Sed * Although bypass handles most operations, vop_getattr, vop_lock,
93193323Sed * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
94218885Sdim * bypassed. Vop_getattr must change the fsid being returned.
95218885Sdim * Vop_lock and vop_unlock must handle any locking for the
96218885Sdim * current vnode as well as pass the lock request down.
97218885Sdim * Vop_inactive and vop_reclaim are not bypassed so that
98218885Sdim * they can handle freeing null-layer specific data. Vop_print
99193323Sed * is not bypassed to avoid excessive debugging information.
100193323Sed * Also, certain vnode operations change the locking state within
101193323Sed * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
102193323Sed * and symlink). Ideally these operations should not change the
103193323Sed * lock state, but should be changed to let the caller of the
104193323Sed * function unlock them. Otherwise all intermediate vnode layers
105193323Sed * (such as union, umapfs, etc) must catch these functions to do
106193323Sed * the necessary locking at their layer.
107193323Sed *
108193323Sed *
109193323Sed * INSTANTIATING VNODE STACKS
110198090Srdivacky *
111198090Srdivacky * Mounting associates the null layer with a lower layer,
112198090Srdivacky * effect stacking two VFSes.  Vnode stacks are instead
113198090Srdivacky * created on demand as files are accessed.
114193323Sed *
115193323Sed * The initial mount creates a single vnode stack for the
116193323Sed * root of the new null layer.  All other vnode stacks
117193323Sed * are created as a result of vnode operations on
118193323Sed * this or other null vnode stacks.
119193323Sed *
120193323Sed * New vnode stacks come into existance as a result of
121193323Sed * an operation which returns a vnode.
122193323Sed * The bypass routine stacks a null-node above the new
123193323Sed * vnode before returning it to the caller.
124193323Sed *
125193323Sed * For example, imagine mounting a null layer with
126208599Srdivacky * "mount_nullfs /usr/include /dev/layer/null".
127218885Sdim * Changing directory to /dev/layer/null will assign
128208599Srdivacky * the root null-node (which was created when the null layer was mounted).
129208599Srdivacky * Now consider opening "sys".  A vop_lookup would be
130193323Sed * done on the root null-node.  This operation would bypass through
131193323Sed * to the lower layer which would return a vnode representing
132193323Sed * the UFS "sys".  Null_bypass then builds a null-node
133193323Sed * aliasing the UFS "sys" and returns this to the caller.
134193323Sed * Later operations on the null-node "sys" will repeat this
135193323Sed * process when constructing other vnode stacks.
136193323Sed *
137193323Sed *
138193323Sed * CREATING OTHER FILE SYSTEM LAYERS
139193323Sed *
140193323Sed * One of the easiest ways to construct new filesystem layers is to make
141193323Sed * a copy of the null layer, rename all files and variables, and
142193323Sed * then begin modifing the copy.  Sed can be used to easily rename
143193323Sed * all variables.
144198090Srdivacky *
145193323Sed * The umap layer is an example of a layer descended from the
146193323Sed * null layer.
147193323Sed *
148193323Sed *
149193323Sed * INVOKING OPERATIONS ON LOWER LAYERS
150193323Sed *
151193323Sed * There are two techniques to invoke operations on a lower layer
152193323Sed * when the operation cannot be completely bypassed.  Each method
153193323Sed * is appropriate in different situations.  In both cases,
154193323Sed * it is the responsibility of the aliasing layer to make
155193323Sed * the operation arguments "correct" for the lower layer
156193323Sed * by mapping a vnode arguments to the lower layer.
157193323Sed *
158193323Sed * The first approach is to call the aliasing layer's bypass routine.
159193323Sed * This method is most suitable when you wish to invoke the operation
160193323Sed * currently being handled on the lower layer.  It has the advantage
161218885Sdim * that the bypass routine already must do argument mapping.
162208599Srdivacky * An example of this is null_getattrs in the null layer.
163193323Sed *
164193323Sed * A second approach is to directly invoke vnode operations on
165193323Sed * the lower layer with the VOP_OPERATIONNAME interface.
166193323Sed * The advantage of this method is that it is easy to invoke
167193323Sed * arbitrary operations on the lower layer.  The disadvantage
168193323Sed * is that vnode arguments must be manualy mapped.
169193323Sed *
170193323Sed */
171193323Sed
172193323Sed#include <sys/param.h>
173193323Sed#include <sys/systm.h>
174193323Sed#include <sys/conf.h>
175193323Sed#include <sys/kernel.h>
176193323Sed#include <sys/lock.h>
177193323Sed#include <sys/malloc.h>
178193323Sed#include <sys/mount.h>
179205218Srdivacky#include <sys/mutex.h>
180193323Sed#include <sys/namei.h>
181193323Sed#include <sys/sysctl.h>
182218885Sdim#include <sys/vnode.h>
183208599Srdivacky
184205218Srdivacky#include <fs/nullfs/null.h>
185193323Sed
186193323Sed#include <vm/vm.h>
187193323Sed#include <vm/vm_extern.h>
188193323Sed#include <vm/vm_object.h>
189193323Sed#include <vm/vnode_pager.h>
190218885Sdim
191218885Sdimstatic int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
192218885SdimSYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
193218885Sdim	&null_bug_bypass, 0, "");
194193323Sed
195218885Sdim/*
196218885Sdim * This is the 10-Apr-92 bypass routine.
197193323Sed *    This version has been optimized for speed, throwing away some
198193323Sed * safety checks.  It should still always work, but it's not as
199193323Sed * robust to programmer errors.
200198090Srdivacky *
201193323Sed * In general, we map all vnodes going down and unmap them on the way back.
202198090Srdivacky * As an exception to this, vnodes can be marked "unmapped" by setting
203193323Sed * the Nth bit in operation's vdesc_flags.
204193323Sed *
205193323Sed * Also, some BSD vnode operations have the side effect of vrele'ing
206193323Sed * their arguments.  With stacking, the reference counts are held
207193323Sed * by the upper node, not the lower one, so we must handle these
208193323Sed * side-effects here.  This is not of concern in Sun-derived systems
209193323Sed * since there are no such side-effects.
210193323Sed *
211193323Sed * This makes the following assumptions:
212193323Sed * - only one returned vpp
213193323Sed * - no INOUT vpp's (Sun's vop_open has one of these)
214193323Sed * - the vnode operation vector of the first vnode should be used
215198090Srdivacky *   to determine what implementation of the op should be invoked
216218885Sdim * - all mapped vnodes are of our vnode-type (NEEDSWORK:
217193323Sed *   problems on rmdir'ing mount points and renaming?)
218193323Sed */
219193323Sedint
220193323Sednull_bypass(struct vop_generic_args *ap)
221193323Sed{
222193323Sed	struct vnode **this_vp_p;
223193323Sed	int error;
224193323Sed	struct vnode *old_vps[VDESC_MAX_VPS];
225193323Sed	struct vnode **vps_p[VDESC_MAX_VPS];
226198090Srdivacky	struct vnode ***vppp;
227218885Sdim	struct vnodeop_desc *descp = ap->a_desc;
228193323Sed	int reles, i;
229193323Sed
230193323Sed	if (null_bug_bypass)
231193323Sed		printf ("null_bypass: %s\n", descp->vdesc_name);
232193323Sed
233249423Sdim#ifdef DIAGNOSTIC
234193323Sed	/*
235193323Sed	 * We require at least one vp.
236193323Sed	 */
237205218Srdivacky	if (descp->vdesc_vp_offsets == NULL ||
238193323Sed	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
239198090Srdivacky		panic ("null_bypass: no vp's in map");
240208599Srdivacky#endif
241205218Srdivacky
242205218Srdivacky	/*
243193323Sed	 * Map the vnodes going in.
244193323Sed	 * Later, we'll invoke the operation based on
245218885Sdim	 * the first mapped vnode's operation vector.
246218885Sdim	 */
247193323Sed	reles = descp->vdesc_flags;
248193323Sed	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
249193323Sed		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
250193323Sed			break;   /* bail out at end of list */
251193323Sed		vps_p[i] = this_vp_p =
252193323Sed			VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
253198090Srdivacky		/*
254218885Sdim		 * We're not guaranteed that any but the first vnode
255208599Srdivacky		 * are of our type.  Check for and don't map any
256198090Srdivacky		 * that aren't.  (We must always map first vp or vclean fails.)
257193323Sed		 */
258193323Sed		if (i && (*this_vp_p == NULLVP ||
259193323Sed		    (*this_vp_p)->v_op != &null_vnodeops)) {
260193323Sed			old_vps[i] = NULLVP;
261193323Sed		} else {
262207618Srdivacky			old_vps[i] = *this_vp_p;
263207618Srdivacky			*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
264207618Srdivacky			/*
265193323Sed			 * XXX - Several operations have the side effect
266212793Sdim			 * of vrele'ing their vp's.  We must account for
267193323Sed			 * that.  (This should go away in the future.)
268198090Srdivacky			 */
269193323Sed			if (reles & VDESC_VP0_WILLRELE)
270207618Srdivacky				VREF(*this_vp_p);
271218885Sdim		}
272198090Srdivacky
273193323Sed	}
274193323Sed
275198090Srdivacky	/*
276198090Srdivacky	 * Call the operation on the lower layer
277198090Srdivacky	 * with the modified argument structure.
278193323Sed	 */
279193323Sed	if (vps_p[0] && *vps_p[0])
280193323Sed		error = VCALL(ap);
281207618Srdivacky	else {
282221337Sdim		printf("null_bypass: no map for %s\n", descp->vdesc_name);
283193323Sed		error = EINVAL;
284193323Sed	}
285208599Srdivacky
286193323Sed	/*
287193323Sed	 * Maintain the illusion of call-by-value
288193323Sed	 * by restoring vnodes in the argument structure
289193323Sed	 * to their original value.
290193323Sed	 */
291193323Sed	reles = descp->vdesc_flags;
292193323Sed	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
293212793Sdim		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
294212793Sdim			break;   /* bail out at end of list */
295193323Sed		if (old_vps[i]) {
296193323Sed			*(vps_p[i]) = old_vps[i];
297193323Sed#if 0
298212793Sdim			if (reles & VDESC_VP0_WILLUNLOCK)
299193323Sed				VOP_UNLOCK(*(vps_p[i]), LK_THISLAYER, curthread);
300193323Sed#endif
301193323Sed			if (reles & VDESC_VP0_WILLRELE)
302193323Sed				vrele(*(vps_p[i]));
303193323Sed		}
304193323Sed	}
305198090Srdivacky
306193323Sed	/*
307198090Srdivacky	 * Map the possible out-going vpp
308198090Srdivacky	 * (Assumes that the lower layer always returns
309193323Sed	 * a VREF'ed vpp unless it gets an error.)
310193323Sed	 */
311198090Srdivacky	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
312193323Sed	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
313193323Sed	    !error) {
314198090Srdivacky		/*
315198090Srdivacky		 * XXX - even though some ops have vpp returned vp's,
316193323Sed		 * several ops actually vrele this before returning.
317193323Sed		 * We must avoid these ops.
318193323Sed		 * (This should go away when these ops are regularized.)
319193323Sed		 */
320193323Sed		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
321193323Sed			goto out;
322207618Srdivacky		vppp = VOPARG_OFFSETTO(struct vnode***,
323221337Sdim				 descp->vdesc_vpp_offset,ap);
324221337Sdim		if (*vppp)
325193323Sed			error = null_nodeget(old_vps[0]->v_mount, **vppp, *vppp);
326198090Srdivacky	}
327193323Sed
328193323Sed out:
329193323Sed	return (error);
330193323Sed}
331198090Srdivacky
332198090Srdivacky/*
333193323Sed * We have to carry on the locking protocol on the null layer vnodes
334193323Sed * as we progress through the tree. We also have to enforce read-only
335198090Srdivacky * if this layer is mounted read-only.
336193323Sed */
337193323Sedstatic int
338193323Sednull_lookup(struct vop_lookup_args *ap)
339193323Sed{
340193323Sed	struct componentname *cnp = ap->a_cnp;
341193323Sed	struct vnode *dvp = ap->a_dvp;
342207618Srdivacky	struct thread *td = cnp->cn_thread;
343207618Srdivacky	int flags = cnp->cn_flags;
344193323Sed	struct vnode *vp, *ldvp, *lvp;
345207618Srdivacky	int error;
346207618Srdivacky
347193323Sed	if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
348193323Sed	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
349198090Srdivacky		return (EROFS);
350193323Sed	/*
351193323Sed	 * Although it is possible to call null_bypass(), we'll do
352198090Srdivacky	 * a direct call to reduce overhead
353193323Sed	 */
354193323Sed	ldvp = NULLVPTOLOWERVP(dvp);
355193323Sed	vp = lvp = NULL;
356193323Sed	error = VOP_LOOKUP(ldvp, &lvp, cnp);
357193323Sed	if (error == EJUSTRETURN && (flags & ISLASTCN) &&
358193323Sed	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
359193323Sed	    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
360193323Sed		error = EROFS;
361193323Sed
362193323Sed	/*
363193323Sed	 * Rely only on the PDIRUNLOCK flag which should be carefully
364193323Sed	 * tracked by underlying filesystem.
365193323Sed	 */
366193323Sed	if ((cnp->cn_flags & PDIRUNLOCK) && dvp->v_vnlock != ldvp->v_vnlock)
367193323Sed		VOP_UNLOCK(dvp, LK_THISLAYER, td);
368193323Sed	if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
369193323Sed		if (ldvp == lvp) {
370193323Sed			*ap->a_vpp = dvp;
371193323Sed			VREF(dvp);
372193323Sed			vrele(lvp);
373193323Sed		} else {
374212793Sdim			error = null_nodeget(dvp->v_mount, lvp, &vp);
375212793Sdim			if (error) {
376212793Sdim				/* XXX Cleanup needed... */
377212793Sdim				panic("null_nodeget failed");
378193323Sed			}
379193323Sed			*ap->a_vpp = vp;
380207618Srdivacky		}
381207618Srdivacky	}
382193323Sed	return (error);
383193323Sed}
384193323Sed
385193323Sedstatic int
386207618Srdivackynull_open(struct vop_open_args *ap)
387207618Srdivacky{
388207618Srdivacky	int retval;
389207618Srdivacky	struct vnode *vp, *ldvp;
390193323Sed
391193323Sed	vp = ap->a_vp;
392207618Srdivacky	ldvp = NULLVPTOLOWERVP(vp);
393207618Srdivacky	retval = null_bypass(&ap->a_gen);
394207618Srdivacky	if (retval == 0)
395207618Srdivacky		vp->v_object = ldvp->v_object;
396207618Srdivacky	return (retval);
397193323Sed}
398193323Sed
399193323Sed/*
400193323Sed * Setattr call. Disallow write attempts if the layer is mounted read-only.
401193323Sed */
402193323Sedstatic int
403193323Sednull_setattr(struct vop_setattr_args *ap)
404193323Sed{
405193323Sed	struct vnode *vp = ap->a_vp;
406218885Sdim	struct vattr *vap = ap->a_vap;
407193323Sed
408193323Sed  	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
409193323Sed	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
410193323Sed	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
411207618Srdivacky	    (vp->v_mount->mnt_flag & MNT_RDONLY))
412207618Srdivacky		return (EROFS);
413207618Srdivacky	if (vap->va_size != VNOVAL) {
414193323Sed 		switch (vp->v_type) {
415207618Srdivacky 		case VDIR:
416212793Sdim 			return (EISDIR);
417207618Srdivacky 		case VCHR:
418207618Srdivacky 		case VBLK:
419193323Sed 		case VSOCK:
420198090Srdivacky 		case VFIFO:
421234353Sdim			if (vap->va_flags != VNOVAL)
422234353Sdim				return (EOPNOTSUPP);
423198090Srdivacky			return (0);
424193323Sed		case VREG:
425193323Sed		case VLNK:
426193323Sed 		default:
427207618Srdivacky			/*
428193323Sed			 * Disallow write attempts if the filesystem is
429193323Sed			 * mounted read-only.
430193323Sed			 */
431193323Sed			if (vp->v_mount->mnt_flag & MNT_RDONLY)
432193323Sed				return (EROFS);
433207618Srdivacky		}
434223013Sdim	}
435193323Sed
436212793Sdim	return (null_bypass((struct vop_generic_args *)ap));
437212793Sdim}
438193323Sed
439207618Srdivacky/*
440212793Sdim *  We handle getattr only to change the fsid.
441193323Sed */
442212793Sdimstatic int
443212793Sdimnull_getattr(struct vop_getattr_args *ap)
444207618Srdivacky{
445207618Srdivacky	int error;
446193323Sed
447193323Sed	if ((error = null_bypass((struct vop_generic_args *)ap)) != 0)
448193323Sed		return (error);
449193323Sed
450198090Srdivacky	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
451193323Sed	return (0);
452193323Sed}
453198090Srdivacky
454193323Sed/*
455193323Sed * Handle to disallow write access if mounted read-only.
456193323Sed */
457193323Sedstatic int
458198090Srdivackynull_access(struct vop_access_args *ap)
459198090Srdivacky{
460198090Srdivacky	struct vnode *vp = ap->a_vp;
461198090Srdivacky	mode_t mode = ap->a_mode;
462193323Sed
463193323Sed	/*
464193323Sed	 * Disallow write attempts on read-only layers;
465193323Sed	 * unless the file is a socket, fifo, or a block or
466193323Sed	 * character device resident on the filesystem.
467193323Sed	 */
468193323Sed	if (mode & VWRITE) {
469193323Sed		switch (vp->v_type) {
470193323Sed		case VDIR:
471193323Sed		case VLNK:
472193323Sed		case VREG:
473			if (vp->v_mount->mnt_flag & MNT_RDONLY)
474				return (EROFS);
475			break;
476		default:
477			break;
478		}
479	}
480	return (null_bypass((struct vop_generic_args *)ap));
481}
482
483/*
484 * We handle this to eliminate null FS to lower FS
485 * file moving. Don't know why we don't allow this,
486 * possibly we should.
487 */
488static int
489null_rename(struct vop_rename_args *ap)
490{
491	struct vnode *tdvp = ap->a_tdvp;
492	struct vnode *fvp = ap->a_fvp;
493	struct vnode *fdvp = ap->a_fdvp;
494	struct vnode *tvp = ap->a_tvp;
495
496	/* Check for cross-device rename. */
497	if ((fvp->v_mount != tdvp->v_mount) ||
498	    (tvp && (fvp->v_mount != tvp->v_mount))) {
499		if (tdvp == tvp)
500			vrele(tdvp);
501		else
502			vput(tdvp);
503		if (tvp)
504			vput(tvp);
505		vrele(fdvp);
506		vrele(fvp);
507		return (EXDEV);
508	}
509
510	return (null_bypass((struct vop_generic_args *)ap));
511}
512
513/*
514 * We need to process our own vnode lock and then clear the
515 * interlock flag as it applies only to our vnode, not the
516 * vnodes below us on the stack.
517 */
518static int
519null_lock(struct vop_lock_args *ap)
520{
521	struct vnode *vp = ap->a_vp;
522	int flags = ap->a_flags;
523	struct thread *td = ap->a_td;
524	struct vnode *lvp;
525	int error;
526	struct null_node *nn;
527
528	if (flags & LK_THISLAYER) {
529		if (vp->v_vnlock != NULL) {
530			/* lock is shared across layers */
531			if (flags & LK_INTERLOCK)
532				mtx_unlock(&vp->v_interlock);
533			return 0;
534		}
535		error = lockmgr(&vp->v_lock, flags & ~LK_THISLAYER,
536		    &vp->v_interlock, td);
537		return (error);
538	}
539
540	if ((flags & LK_INTERLOCK) == 0) {
541		VI_LOCK(vp);
542		flags |= LK_INTERLOCK;
543	}
544	if (vp->v_vnlock != NULL) {
545		/*
546		 * The lower level has exported a struct lock to us. Use
547		 * it so that all vnodes in the stack lock and unlock
548		 * simultaneously. Note: we don't DRAIN the lock as DRAIN
549		 * decommissions the lock - just because our vnode is
550		 * going away doesn't mean the struct lock below us is.
551		 * LK_EXCLUSIVE is fine.
552		 */
553		nn = VTONULL(vp);
554		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
555			NULLFSDEBUG("null_lock: avoiding LK_DRAIN\n");
556			/*
557			 * Emulate lock draining by waiting for all other
558			 * pending locks to complete.  Afterwards the
559			 * lockmgr call might block, but no other threads
560			 * will attempt to use this nullfs vnode due to the
561			 * VI_DOOMED flag.
562			 */
563			while (nn->null_pending_locks > 0) {
564				nn->null_drain_wakeup = 1;
565				msleep(&nn->null_pending_locks,
566				       VI_MTX(vp),
567				       PVFS,
568				       "nuldr", 0);
569			}
570			error = lockmgr(vp->v_vnlock,
571					(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
572					VI_MTX(vp), td);
573			return error;
574		}
575		nn->null_pending_locks++;
576		error = lockmgr(vp->v_vnlock, flags, &vp->v_interlock, td);
577		VI_LOCK(vp);
578		/*
579		 * If we're called from vrele then v_usecount can have been 0
580		 * and another process might have initiated a recycle
581		 * operation.  When that happens, just back out.
582		 */
583		if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0 &&
584		    td != vp->v_vxthread) {
585			lockmgr(vp->v_vnlock,
586				(flags & ~LK_TYPE_MASK) | LK_RELEASE,
587				VI_MTX(vp), td);
588			VI_LOCK(vp);
589			error = ENOENT;
590		}
591		nn->null_pending_locks--;
592		/*
593		 * Wakeup the process draining the vnode after all
594		 * pending lock attempts has been failed.
595		 */
596		if (nn->null_pending_locks == 0 &&
597		    nn->null_drain_wakeup != 0) {
598			nn->null_drain_wakeup = 0;
599			wakeup(&nn->null_pending_locks);
600		}
601		VI_UNLOCK(vp);
602		return error;
603	} else {
604		/*
605		 * To prevent race conditions involving doing a lookup
606		 * on "..", we have to lock the lower node, then lock our
607		 * node. Most of the time it won't matter that we lock our
608		 * node (as any locking would need the lower one locked
609		 * first). But we can LK_DRAIN the upper lock as a step
610		 * towards decomissioning it.
611		 */
612		lvp = NULLVPTOLOWERVP(vp);
613		if (lvp == NULL)
614			return (lockmgr(&vp->v_lock, flags, &vp->v_interlock, td));
615		if (flags & LK_INTERLOCK) {
616			mtx_unlock(&vp->v_interlock);
617			flags &= ~LK_INTERLOCK;
618		}
619		if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
620			error = VOP_LOCK(lvp,
621				(flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE, td);
622		} else
623			error = VOP_LOCK(lvp, flags, td);
624		if (error)
625			return (error);
626		error = lockmgr(&vp->v_lock, flags, &vp->v_interlock, td);
627		if (error)
628			VOP_UNLOCK(lvp, 0, td);
629		return (error);
630	}
631}
632
633/*
634 * We need to process our own vnode unlock and then clear the
635 * interlock flag as it applies only to our vnode, not the
636 * vnodes below us on the stack.
637 */
638static int
639null_unlock(struct vop_unlock_args *ap)
640{
641	struct vnode *vp = ap->a_vp;
642	int flags = ap->a_flags;
643	struct thread *td = ap->a_td;
644	struct vnode *lvp;
645
646	if (vp->v_vnlock != NULL) {
647		if (flags & LK_THISLAYER)
648			return 0;	/* the lock is shared across layers */
649		flags &= ~LK_THISLAYER;
650		return (lockmgr(vp->v_vnlock, flags | LK_RELEASE,
651			&vp->v_interlock, td));
652	}
653	lvp = NULLVPTOLOWERVP(vp);
654	if (lvp == NULL)
655		return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, td));
656	if ((flags & LK_THISLAYER) == 0) {
657		if (flags & LK_INTERLOCK) {
658			mtx_unlock(&vp->v_interlock);
659			flags &= ~LK_INTERLOCK;
660		}
661		VOP_UNLOCK(lvp, flags & ~LK_INTERLOCK, td);
662	} else
663		flags &= ~LK_THISLAYER;
664	return (lockmgr(&vp->v_lock, flags | LK_RELEASE, &vp->v_interlock, td));
665}
666
667static int
668null_islocked(struct vop_islocked_args *ap)
669{
670	struct vnode *vp = ap->a_vp;
671	struct thread *td = ap->a_td;
672
673	if (vp->v_vnlock != NULL)
674		return (lockstatus(vp->v_vnlock, td));
675	return (lockstatus(&vp->v_lock, td));
676}
677
678/*
679 * There is no way to tell that someone issued remove/rmdir operation
680 * on the underlying filesystem. For now we just have to release lowevrp
681 * as soon as possible.
682 *
683 * Note, we can't release any resources nor remove vnode from hash before
684 * appropriate VXLOCK stuff is is done because other process can find this
685 * vnode in hash during inactivation and may be sitting in vget() and waiting
686 * for null_inactive to unlock vnode. Thus we will do all those in VOP_RECLAIM.
687 */
688static int
689null_inactive(struct vop_inactive_args *ap)
690{
691	struct vnode *vp = ap->a_vp;
692	struct thread *td = ap->a_td;
693
694	vp->v_object = NULL;
695
696	/*
697	 * If this is the last reference, then free up the vnode
698	 * so as not to tie up the lower vnodes.
699	 */
700	vrecycle(vp, td);
701
702	return (0);
703}
704
705/*
706 * Now, the VXLOCK is in force and we're free to destroy the null vnode.
707 */
708static int
709null_reclaim(struct vop_reclaim_args *ap)
710{
711	struct vnode *vp = ap->a_vp;
712	struct null_node *xp = VTONULL(vp);
713	struct vnode *lowervp = xp->null_lowervp;
714	struct lock *vnlock;
715
716	if (lowervp) {
717		null_hashrem(xp);
718
719		vrele(lowervp);
720		vrele(lowervp);
721	}
722
723	vp->v_data = NULL;
724	vp->v_object = NULL;
725	vnlock = vp->v_vnlock;
726	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL, curthread);
727	vp->v_vnlock = &vp->v_lock;
728	transferlockers(vnlock, vp->v_vnlock);
729	lockmgr(vnlock, LK_RELEASE, NULL, curthread);
730	FREE(xp, M_NULLFSNODE);
731
732	return (0);
733}
734
735static int
736null_print(struct vop_print_args *ap)
737{
738	struct vnode *vp = ap->a_vp;
739	printf("\tvp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
740	return (0);
741}
742
743/*
744 * Global vfs data structures
745 */
746struct vop_vector null_vnodeops = {
747	.vop_bypass =		null_bypass,
748
749	.vop_access =		null_access,
750	.vop_bmap =		VOP_EOPNOTSUPP,
751	.vop_getattr =		null_getattr,
752	.vop_getwritemount =	vop_stdgetwritemount,
753	.vop_inactive =		null_inactive,
754	.vop_islocked =		null_islocked,
755	.vop_lock =		null_lock,
756	.vop_lookup =		null_lookup,
757	.vop_open =		null_open,
758	.vop_print =		null_print,
759	.vop_reclaim =		null_reclaim,
760	.vop_rename =		null_rename,
761	.vop_setattr =		null_setattr,
762	.vop_strategy =		VOP_EOPNOTSUPP,
763	.vop_unlock =		null_unlock,
764};
765