VFS messaging/interfacing work stage 1/99. This stage replaces the old
[dragonfly.git] / sys / vfs / nullfs / null_vnops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * John Heidemann of the UCLA Ficus project.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95
37 *
38 * Ancestors:
39 * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92
40 * $FreeBSD: src/sys/miscfs/nullfs/null_vnops.c,v 1.38.2.6 2002/07/31 00:32:28 semenu Exp $
2d3e977e 41 * $DragonFly: src/sys/vfs/nullfs/null_vnops.c,v 1.12 2004/08/13 17:51:12 dillon Exp $
984263bc
MD
42 * ...and...
43 * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project
44 *
45 * $FreeBSD: src/sys/miscfs/nullfs/null_vnops.c,v 1.38.2.6 2002/07/31 00:32:28 semenu Exp $
46 */
47
48/*
49 * Null Layer
50 *
51 * (See mount_null(8) for more information.)
52 *
53 * The null layer duplicates a portion of the file system
54 * name space under a new name. In this respect, it is
55 * similar to the loopback file system. It differs from
56 * the loopback fs in two respects: it is implemented using
57 * a stackable layers techniques, and its "null-node"s stack above
58 * all lower-layer vnodes, not just over directory vnodes.
59 *
60 * The null layer has two purposes. First, it serves as a demonstration
61 * of layering by proving a layer which does nothing. (It actually
62 * does everything the loopback file system does, which is slightly
63 * more than nothing.) Second, the null layer can serve as a prototype
64 * layer. Since it provides all necessary layer framework,
65 * new file system layers can be created very easily be starting
66 * with a null layer.
67 *
68 * The remainder of this man page examines the null layer as a basis
69 * for constructing new layers.
70 *
71 *
72 * INSTANTIATING NEW NULL LAYERS
73 *
74 * New null layers are created with mount_null(8).
75 * Mount_null(8) takes two arguments, the pathname
76 * of the lower vfs (target-pn) and the pathname where the null
77 * layer will appear in the namespace (alias-pn). After
78 * the null layer is put into place, the contents
79 * of target-pn subtree will be aliased under alias-pn.
80 *
81 *
82 * OPERATION OF A NULL LAYER
83 *
84 * The null layer is the minimum file system layer,
85 * simply bypassing all possible operations to the lower layer
86 * for processing there. The majority of its activity centers
87 * on the bypass routine, through which nearly all vnode operations
88 * pass.
89 *
90 * The bypass routine accepts arbitrary vnode operations for
91 * handling by the lower layer. It begins by examing vnode
92 * operation arguments and replacing any null-nodes by their
93 * lower-layer equivlants. It then invokes the operation
94 * on the lower layer. Finally, it replaces the null-nodes
95 * in the arguments and, if a vnode is return by the operation,
96 * stacks a null-node on top of the returned vnode.
97 *
98 * Although bypass handles most operations, vop_getattr, vop_lock,
99 * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
100 * bypassed. Vop_getattr must change the fsid being returned.
101 * Vop_lock and vop_unlock must handle any locking for the
102 * current vnode as well as pass the lock request down.
103 * Vop_inactive and vop_reclaim are not bypassed so that
104 * they can handle freeing null-layer specific data. Vop_print
105 * is not bypassed to avoid excessive debugging information.
106 * Also, certain vnode operations change the locking state within
107 * the operation (create, mknod, remove, link, rename, mkdir, rmdir,
108 * and symlink). Ideally these operations should not change the
109 * lock state, but should be changed to let the caller of the
110 * function unlock them. Otherwise all intermediate vnode layers
111 * (such as union, umapfs, etc) must catch these functions to do
112 * the necessary locking at their layer.
113 *
114 *
115 * INSTANTIATING VNODE STACKS
116 *
117 * Mounting associates the null layer with a lower layer,
118 * effect stacking two VFSes. Vnode stacks are instead
119 * created on demand as files are accessed.
120 *
121 * The initial mount creates a single vnode stack for the
122 * root of the new null layer. All other vnode stacks
123 * are created as a result of vnode operations on
124 * this or other null vnode stacks.
125 *
126 * New vnode stacks come into existance as a result of
127 * an operation which returns a vnode.
128 * The bypass routine stacks a null-node above the new
129 * vnode before returning it to the caller.
130 *
131 * For example, imagine mounting a null layer with
132 * "mount_null /usr/include /dev/layer/null".
133 * Changing directory to /dev/layer/null will assign
134 * the root null-node (which was created when the null layer was mounted).
135 * Now consider opening "sys". A vop_lookup would be
136 * done on the root null-node. This operation would bypass through
137 * to the lower layer which would return a vnode representing
138 * the UFS "sys". Null_bypass then builds a null-node
139 * aliasing the UFS "sys" and returns this to the caller.
140 * Later operations on the null-node "sys" will repeat this
141 * process when constructing other vnode stacks.
142 *
143 *
144 * CREATING OTHER FILE SYSTEM LAYERS
145 *
146 * One of the easiest ways to construct new file system layers is to make
147 * a copy of the null layer, rename all files and variables, and
148 * then begin modifing the copy. Sed can be used to easily rename
149 * all variables.
150 *
151 * The umap layer is an example of a layer descended from the
152 * null layer.
153 *
154 *
155 * INVOKING OPERATIONS ON LOWER LAYERS
156 *
157 * There are two techniques to invoke operations on a lower layer
158 * when the operation cannot be completely bypassed. Each method
159 * is appropriate in different situations. In both cases,
160 * it is the responsibility of the aliasing layer to make
161 * the operation arguments "correct" for the lower layer
162 * by mapping an vnode arguments to the lower layer.
163 *
164 * The first approach is to call the aliasing layer's bypass routine.
165 * This method is most suitable when you wish to invoke the operation
166 * currently being handled on the lower layer. It has the advantage
167 * that the bypass routine already must do argument mapping.
168 * An example of this is null_getattrs in the null layer.
169 *
170 * A second approach is to directly invoke vnode operations on
171 * the lower layer with the VOP_OPERATIONNAME interface.
172 * The advantage of this method is that it is easy to invoke
173 * arbitrary operations on the lower layer. The disadvantage
174 * is that vnode arguments must be manualy mapped.
175 *
176 */
177
178#include <sys/param.h>
179#include <sys/systm.h>
180#include <sys/kernel.h>
181#include <sys/sysctl.h>
182#include <sys/vnode.h>
183#include <sys/mount.h>
dadab5e9 184#include <sys/proc.h>
984263bc
MD
185#include <sys/namei.h>
186#include <sys/malloc.h>
187#include <sys/buf.h>
1f2de5d4 188#include "null.h"
984263bc
MD
189
190static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
191SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
192 &null_bug_bypass, 0, "");
193
194static int null_access(struct vop_access_args *ap);
195static int null_createvobject(struct vop_createvobject_args *ap);
196static int null_destroyvobject(struct vop_destroyvobject_args *ap);
197static int null_getattr(struct vop_getattr_args *ap);
198static int null_getvobject(struct vop_getvobject_args *ap);
199static int null_inactive(struct vop_inactive_args *ap);
200static int null_islocked(struct vop_islocked_args *ap);
201static int null_lock(struct vop_lock_args *ap);
202static int null_lookup(struct vop_lookup_args *ap);
203static int null_open(struct vop_open_args *ap);
204static int null_print(struct vop_print_args *ap);
205static int null_reclaim(struct vop_reclaim_args *ap);
206static int null_rename(struct vop_rename_args *ap);
207static int null_setattr(struct vop_setattr_args *ap);
208static int null_unlock(struct vop_unlock_args *ap);
209
210/*
211 * This is the 10-Apr-92 bypass routine.
212 * This version has been optimized for speed, throwing away some
213 * safety checks. It should still always work, but it's not as
214 * robust to programmer errors.
215 *
216 * In general, we map all vnodes going down and unmap them on the way back.
217 * As an exception to this, vnodes can be marked "unmapped" by setting
218 * the Nth bit in operation's vdesc_flags.
219 *
220 * Also, some BSD vnode operations have the side effect of vrele'ing
221 * their arguments. With stacking, the reference counts are held
222 * by the upper node, not the lower one, so we must handle these
223 * side-effects here. This is not of concern in Sun-derived systems
224 * since there are no such side-effects.
225 *
226 * This makes the following assumptions:
227 * - only one returned vpp
228 * - no INOUT vpp's (Sun's vop_open has one of these)
229 * - the vnode operation vector of the first vnode should be used
230 * to determine what implementation of the op should be invoked
231 * - all mapped vnodes are of our vnode-type (NEEDSWORK:
232 * problems on rmdir'ing mount points and renaming?)
f4a3189a
CP
233 *
234 * null_bypass(struct vnodeop_desc *a_desc, ...)
984263bc
MD
235 */
236int
f4a3189a 237null_bypass(struct vop_generic_args *ap)
984263bc
MD
238{
239 register struct vnode **this_vp_p;
240 int error;
241 struct vnode *old_vps[VDESC_MAX_VPS];
242 struct vnode **vps_p[VDESC_MAX_VPS];
243 struct vnode ***vppp;
244 struct vnodeop_desc *descp = ap->a_desc;
245 int reles, i;
246
247 if (null_bug_bypass)
248 printf ("null_bypass: %s\n", descp->vdesc_name);
249
250#ifdef DIAGNOSTIC
251 /*
252 * We require at least one vp.
253 */
254 if (descp->vdesc_vp_offsets == NULL ||
255 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
256 panic ("null_bypass: no vp's in map");
257#endif
258
259 /*
260 * Map the vnodes going in.
261 * Later, we'll invoke the operation based on
262 * the first mapped vnode's operation vector.
263 */
264 reles = descp->vdesc_flags;
265 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
266 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
267 break; /* bail out at end of list */
268 vps_p[i] = this_vp_p =
269 VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
270 /*
271 * We're not guaranteed that any but the first vnode
272 * are of our type. Check for and don't map any
273 * that aren't. (We must always map first vp or vclean fails.)
274 */
275 if (i && (*this_vp_p == NULLVP ||
2d3e977e 276 (*this_vp_p)->v_vops != null_vnode_vops)) {
984263bc
MD
277 old_vps[i] = NULLVP;
278 } else {
279 old_vps[i] = *this_vp_p;
280 *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
281 /*
282 * XXX - Several operations have the side effect
283 * of vrele'ing their vp's. We must account for
284 * that. (This should go away in the future.)
285 */
286 if (reles & VDESC_VP0_WILLRELE)
597aea93 287 vref(*this_vp_p);
984263bc
MD
288 }
289
290 }
291
292 /*
293 * Call the operation on the lower layer
294 * with the modified argument structure.
295 */
296 if (vps_p[0] && *vps_p[0])
297 error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
298 else {
299 printf("null_bypass: no map for %s\n", descp->vdesc_name);
300 error = EINVAL;
301 }
302
303 /*
304 * Maintain the illusion of call-by-value
305 * by restoring vnodes in the argument structure
306 * to their original value.
307 */
308 reles = descp->vdesc_flags;
309 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
310 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
311 break; /* bail out at end of list */
312 if (old_vps[i]) {
313 *(vps_p[i]) = old_vps[i];
314#if 0
315 if (reles & VDESC_VP0_WILLUNLOCK)
41a01a4d 316 VOP_UNLOCK(*(vps_p[i]), NULL, LK_THISLAYER, curproc);
984263bc
MD
317#endif
318 if (reles & VDESC_VP0_WILLRELE)
319 vrele(*(vps_p[i]));
320 }
321 }
322
323 /*
324 * Map the possible out-going vpp
325 * (Assumes that the lower layer always returns
597aea93 326 * a vref'ed vpp unless it gets an error.)
984263bc
MD
327 */
328 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
329 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
330 !error) {
331 /*
332 * XXX - even though some ops have vpp returned vp's,
333 * several ops actually vrele this before returning.
334 * We must avoid these ops.
335 * (This should go away when these ops are regularized.)
336 */
337 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
338 goto out;
339 vppp = VOPARG_OFFSETTO(struct vnode***,
340 descp->vdesc_vpp_offset,ap);
341 if (*vppp)
342 error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
343 }
344
345 out:
346 return (error);
347}
348
349/*
350 * We have to carry on the locking protocol on the null layer vnodes
351 * as we progress through the tree. We also have to enforce read-only
352 * if this layer is mounted read-only.
f4a3189a
CP
353 *
354 * null_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
355 * struct componentname *a_cnp)
984263bc
MD
356 */
357static int
f4a3189a 358null_lookup(struct vop_lookup_args *ap)
984263bc
MD
359{
360 struct componentname *cnp = ap->a_cnp;
361 struct vnode *dvp = ap->a_dvp;
dadab5e9 362 struct thread *td = cnp->cn_td;
984263bc
MD
363 int flags = cnp->cn_flags;
364 struct vnode *vp, *ldvp, *lvp;
365 int error;
366
2b69e610
MD
367 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
368 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
984263bc
MD
369 return (EROFS);
370 /*
371 * Although it is possible to call null_bypass(), we'll do
372 * a direct call to reduce overhead
373 */
374 ldvp = NULLVPTOLOWERVP(dvp);
375 vp = lvp = NULL;
bc0c094e 376 error = VOP_LOOKUP(ldvp, NCPNULL, &lvp, NCPPNULL, cnp);
2b69e610 377 if (error == EJUSTRETURN && (flags & CNP_ISLASTCN) &&
984263bc 378 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2b69e610 379 (cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME))
984263bc
MD
380 error = EROFS;
381
382 /*
383 * Rely only on the PDIRUNLOCK flag which should be carefully
384 * tracked by underlying filesystem.
385 */
2b69e610 386 if (cnp->cn_flags & CNP_PDIRUNLOCK)
41a01a4d 387 VOP_UNLOCK(dvp, NULL, LK_THISLAYER, td);
984263bc
MD
388 if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
389 if (ldvp == lvp) {
390 *ap->a_vpp = dvp;
597aea93 391 vref(dvp);
984263bc
MD
392 vrele(lvp);
393 } else {
394 error = null_node_create(dvp->v_mount, lvp, &vp);
395 if (error == 0)
396 *ap->a_vpp = vp;
397 }
398 }
399 return (error);
400}
401
402/*
403 * Setattr call. Disallow write attempts if the layer is mounted read-only.
f4a3189a
CP
404 *
405 * null_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
406 * struct vattr *a_vap, struct ucred *a_cred,
407 * struct thread *a_td)
984263bc
MD
408 */
409int
f4a3189a 410null_setattr(struct vop_setattr_args *ap)
984263bc
MD
411{
412 struct vnode *vp = ap->a_vp;
413 struct vattr *vap = ap->a_vap;
414
415 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
416 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
417 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
418 (vp->v_mount->mnt_flag & MNT_RDONLY))
419 return (EROFS);
420 if (vap->va_size != VNOVAL) {
421 switch (vp->v_type) {
422 case VDIR:
423 return (EISDIR);
424 case VCHR:
425 case VBLK:
426 case VSOCK:
427 case VFIFO:
428 if (vap->va_flags != VNOVAL)
429 return (EOPNOTSUPP);
430 return (0);
431 case VREG:
432 case VLNK:
433 default:
434 /*
435 * Disallow write attempts if the filesystem is
436 * mounted read-only.
437 */
438 if (vp->v_mount->mnt_flag & MNT_RDONLY)
439 return (EROFS);
440 }
441 }
442
2d3e977e 443 return (null_bypass(&ap->a_head));
984263bc
MD
444}
445
446/*
447 * We handle getattr only to change the fsid.
f4a3189a
CP
448 *
449 * null_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
450 * struct thread *a_td)
984263bc
MD
451 */
452static int
f4a3189a 453null_getattr(struct vop_getattr_args *ap)
984263bc
MD
454{
455 int error;
456
2d3e977e 457 if ((error = null_bypass(&ap->a_head)) != 0)
984263bc
MD
458 return (error);
459
460 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
461 return (0);
462}
463
464/*
465 * Handle to disallow write access if mounted read-only.
f4a3189a
CP
466 *
467 * null_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
468 * struct thread *a_td)
984263bc
MD
469 */
470static int
f4a3189a 471null_access(struct vop_access_args *ap)
984263bc
MD
472{
473 struct vnode *vp = ap->a_vp;
474 mode_t mode = ap->a_mode;
475
476 /*
477 * Disallow write attempts on read-only layers;
478 * unless the file is a socket, fifo, or a block or
479 * character device resident on the file system.
480 */
481 if (mode & VWRITE) {
482 switch (vp->v_type) {
483 case VDIR:
484 case VLNK:
485 case VREG:
486 if (vp->v_mount->mnt_flag & MNT_RDONLY)
487 return (EROFS);
488 break;
489 default:
490 break;
491 }
492 }
2d3e977e 493 return (null_bypass(&ap->a_head));
984263bc
MD
494}
495
496/*
497 * We must handle open to be able to catch MNT_NODEV and friends.
f4a3189a
CP
498 *
499 * null_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
500 * struct thread *a_td)
984263bc
MD
501 */
502static int
f4a3189a 503null_open(struct vop_open_args *ap)
984263bc
MD
504{
505 struct vnode *vp = ap->a_vp;
506 struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
507
508 if ((vp->v_mount->mnt_flag & MNT_NODEV) &&
509 (lvp->v_type == VBLK || lvp->v_type == VCHR))
510 return ENXIO;
511
2d3e977e 512 return (null_bypass(&ap->a_head));
984263bc
MD
513}
514
515/*
516 * We handle this to eliminate null FS to lower FS
517 * file moving. Don't know why we don't allow this,
518 * possibly we should.
f4a3189a
CP
519 *
520 * null_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
521 * struct componentname *a_fcnp, struct vnode *a_tdvp,
522 * struct vnode *a_tvp, struct componentname *a_tcnp)
984263bc
MD
523 */
524static int
f4a3189a 525null_rename(struct vop_rename_args *ap)
984263bc
MD
526{
527 struct vnode *tdvp = ap->a_tdvp;
528 struct vnode *fvp = ap->a_fvp;
529 struct vnode *fdvp = ap->a_fdvp;
530 struct vnode *tvp = ap->a_tvp;
531
532 /* Check for cross-device rename. */
533 if ((fvp->v_mount != tdvp->v_mount) ||
534 (tvp && (fvp->v_mount != tvp->v_mount))) {
535 if (tdvp == tvp)
536 vrele(tdvp);
537 else
538 vput(tdvp);
539 if (tvp)
540 vput(tvp);
541 vrele(fdvp);
542 vrele(fvp);
543 return (EXDEV);
544 }
545
2d3e977e 546 return (null_bypass(&ap->a_head));
984263bc
MD
547}
548
549/*
550 * We need to process our own vnode lock and then clear the
551 * interlock flag as it applies only to our vnode, not the
552 * vnodes below us on the stack.
f4a3189a
CP
553 *
554 * null_lock(struct vnode *a_vp, lwkt_tokref_t a_vlock, int a_flags,
555 * struct thread *a_td)
984263bc
MD
556 */
557static int
f4a3189a 558null_lock(struct vop_lock_args *ap)
984263bc
MD
559{
560 struct vnode *vp = ap->a_vp;
561 int flags = ap->a_flags;
984263bc
MD
562 struct null_node *np = VTONULL(vp);
563 struct vnode *lvp;
564 int error;
565
566 if (flags & LK_THISLAYER) {
567 if (vp->v_vnlock != NULL) {
568 /* lock is shared across layers */
569 if (flags & LK_INTERLOCK)
41a01a4d 570 lwkt_reltoken(ap->a_vlock);
984263bc
MD
571 return 0;
572 }
573 error = lockmgr(&np->null_lock, flags & ~LK_THISLAYER,
41a01a4d 574 ap->a_vlock, ap->a_td);
984263bc
MD
575 return (error);
576 }
577
578 if (vp->v_vnlock != NULL) {
579 /*
580 * The lower level has exported a struct lock to us. Use
581 * it so that all vnodes in the stack lock and unlock
582 * simultaneously. Note: we don't DRAIN the lock as DRAIN
583 * decommissions the lock - just because our vnode is
584 * going away doesn't mean the struct lock below us is.
585 * LK_EXCLUSIVE is fine.
586 */
587 if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
588 NULLFSDEBUG("null_lock: avoiding LK_DRAIN\n");
589 return(lockmgr(vp->v_vnlock,
590 (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
41a01a4d 591 ap->a_vlock, ap->a_td));
984263bc 592 }
41a01a4d 593 return(lockmgr(vp->v_vnlock, flags, ap->a_vlock, ap->a_td));
984263bc
MD
594 }
595 /*
596 * To prevent race conditions involving doing a lookup
597 * on "..", we have to lock the lower node, then lock our
598 * node. Most of the time it won't matter that we lock our
599 * node (as any locking would need the lower one locked
600 * first). But we can LK_DRAIN the upper lock as a step
601 * towards decomissioning it.
602 */
603 lvp = NULLVPTOLOWERVP(vp);
604 if (lvp == NULL)
41a01a4d 605 return (lockmgr(&np->null_lock, flags, ap->a_vlock, ap->a_td));
984263bc 606 if (flags & LK_INTERLOCK) {
41a01a4d 607 VI_UNLOCK(ap->a_vlock, vp);
984263bc
MD
608 flags &= ~LK_INTERLOCK;
609 }
610 if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
41a01a4d 611 error = VOP_LOCK(lvp, ap->a_vlock,
dadab5e9 612 (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE, ap->a_td);
984263bc 613 } else
41a01a4d 614 error = VOP_LOCK(lvp, ap->a_vlock, flags, ap->a_td);
984263bc
MD
615 if (error)
616 return (error);
41a01a4d 617 error = lockmgr(&np->null_lock, flags, ap->a_vlock, ap->a_td);
984263bc 618 if (error)
41a01a4d 619 VOP_UNLOCK(lvp, NULL, 0, ap->a_td);
984263bc
MD
620 return (error);
621}
622
623/*
624 * We need to process our own vnode unlock and then clear the
625 * interlock flag as it applies only to our vnode, not the
626 * vnodes below us on the stack.
f4a3189a
CP
627 *
628 * null_unlock(struct vnode *a_vp, lwkt_tokref_t a_vlock, int a_flags,
629 * struct thread *a_td)
984263bc
MD
630 */
631static int
f4a3189a 632null_unlock(struct vop_unlock_args *ap)
984263bc
MD
633{
634 struct vnode *vp = ap->a_vp;
635 int flags = ap->a_flags;
984263bc
MD
636 struct null_node *np = VTONULL(vp);
637 struct vnode *lvp;
638
639 if (vp->v_vnlock != NULL) {
640 if (flags & LK_THISLAYER)
641 return 0; /* the lock is shared across layers */
642 flags &= ~LK_THISLAYER;
643 return (lockmgr(vp->v_vnlock, flags | LK_RELEASE,
41a01a4d 644 ap->a_vlock, ap->a_td));
984263bc
MD
645 }
646 lvp = NULLVPTOLOWERVP(vp);
647 if (lvp == NULL)
41a01a4d 648 return (lockmgr(&np->null_lock, flags | LK_RELEASE, ap->a_vlock, ap->a_td));
984263bc
MD
649 if ((flags & LK_THISLAYER) == 0) {
650 if (flags & LK_INTERLOCK) {
41a01a4d 651 VI_UNLOCK(ap->a_vlock, vp);
984263bc
MD
652 flags &= ~LK_INTERLOCK;
653 }
41a01a4d
MD
654 VOP_UNLOCK(lvp, ap->a_vlock, flags, ap->a_td);
655 } else {
984263bc 656 flags &= ~LK_THISLAYER;
41a01a4d 657 }
984263bc 658 ap->a_flags = flags;
41a01a4d 659 return (lockmgr(&np->null_lock, flags | LK_RELEASE, ap->a_vlock, ap->a_td));
984263bc
MD
660}
661
f4a3189a
CP
662/*
663 * null_islocked(struct vnode *a_vp, struct thread *a_td)
664 */
984263bc 665static int
f4a3189a 666null_islocked(struct vop_islocked_args *ap)
984263bc
MD
667{
668 struct vnode *vp = ap->a_vp;
984263bc
MD
669
670 if (vp->v_vnlock != NULL)
dadab5e9
MD
671 return (lockstatus(vp->v_vnlock, ap->a_td));
672 return (lockstatus(&VTONULL(vp)->null_lock, ap->a_td));
984263bc
MD
673}
674
675
676/*
677 * There is no way to tell that someone issued remove/rmdir operation
678 * on the underlying filesystem. For now we just have to release lowevrp
679 * as soon as possible.
f4a3189a
CP
680 *
681 * null_inactive(struct vnode *a_vp, struct thread *a_td)
984263bc
MD
682 */
683static int
f4a3189a 684null_inactive(struct vop_inactive_args *ap)
984263bc
MD
685{
686 struct vnode *vp = ap->a_vp;
984263bc
MD
687 struct null_node *xp = VTONULL(vp);
688 struct vnode *lowervp = xp->null_lowervp;
689
dadab5e9 690 lockmgr(&null_hashlock, LK_EXCLUSIVE, NULL, ap->a_td);
984263bc 691 LIST_REMOVE(xp, null_hash);
dadab5e9 692 lockmgr(&null_hashlock, LK_RELEASE, NULL, ap->a_td);
984263bc
MD
693
694 xp->null_lowervp = NULLVP;
695 if (vp->v_vnlock != NULL) {
696 vp->v_vnlock = &xp->null_lock; /* we no longer share the lock */
41a01a4d
MD
697 } else {
698 VOP_UNLOCK(vp, NULL, LK_THISLAYER, ap->a_td);
699 }
984263bc
MD
700
701 vput(lowervp);
702 /*
703 * Now it is safe to drop references to the lower vnode.
704 * VOP_INACTIVE() will be called by vrele() if necessary.
705 */
706 vrele (lowervp);
707
708 return (0);
709}
710
711/*
712 * We can free memory in null_inactive, but we do this
713 * here. (Possible to guard vp->v_data to point somewhere)
f4a3189a
CP
714 *
715 * null_reclaim(struct vnode *a_vp, struct thread *a_td)
984263bc
MD
716 */
717static int
f4a3189a 718null_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
719{
720 struct vnode *vp = ap->a_vp;
721 void *vdata = vp->v_data;
722
723 vp->v_data = NULL;
724 FREE(vdata, M_NULLFSNODE);
725
726 return (0);
727}
728
f4a3189a
CP
729/*
730 * null_print(struct vnode *a_vp)
731 */
984263bc 732static int
f4a3189a 733null_print(struct vop_print_args *ap)
984263bc
MD
734{
735 struct vnode *vp = ap->a_vp;
736
737 printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp));
738 if (vp->v_vnlock != NULL) {
739 printf("\tvnlock: ");
740 lockmgr_printinfo(vp->v_vnlock);
741 } else {
742 printf("\tnull_lock: ");
743 lockmgr_printinfo(&VTONULL(vp)->null_lock);
744 }
745 printf("\n");
746 return (0);
747}
748
749/*
750 * Let an underlying filesystem do the work
f4a3189a
CP
751 *
752 * null_createvobject(struct vnode *vp, struct ucred *cred, struct proc *p)
984263bc
MD
753 */
754static int
f4a3189a 755null_createvobject(struct vop_createvobject_args *ap)
984263bc
MD
756{
757 struct vnode *vp = ap->a_vp;
758 struct vnode *lowervp = VTONULL(vp) ? NULLVPTOLOWERVP(vp) : NULL;
759 int error;
760
761 if (vp->v_type == VNON || lowervp == NULL)
762 return 0;
3b568787 763 error = VOP_CREATEVOBJECT(lowervp, ap->a_td);
984263bc
MD
764 if (error)
765 return (error);
766 vp->v_flag |= VOBJBUF;
767 return (0);
768}
769
770/*
771 * We have nothing to destroy and this operation shouldn't be bypassed.
f4a3189a
CP
772 *
773 * null_destroyvobject(struct vnode *vp)
984263bc
MD
774 */
775static int
f4a3189a 776null_destroyvobject(struct vop_destroyvobject_args *ap)
984263bc
MD
777{
778 struct vnode *vp = ap->a_vp;
779
780 vp->v_flag &= ~VOBJBUF;
781 return (0);
782}
783
f4a3189a
CP
784/*
785 * null_getvobject(struct vnode *vp, struct vm_object **objpp)
786 */
984263bc 787static int
f4a3189a 788null_getvobject(struct vop_getvobject_args *ap)
984263bc
MD
789{
790 struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
791
792 if (lvp == NULL)
793 return EINVAL;
794 return (VOP_GETVOBJECT(lvp, ap->a_objpp));
795}
796
797/*
798 * Global vfs data structures
799 */
2d3e977e 800struct vop_ops *null_vnode_vops;
984263bc 801static struct vnodeopv_entry_desc null_vnodeop_entries[] = {
2d3e977e
MD
802 { &vop_default_desc, (void *) null_bypass },
803 { &vop_access_desc, (void *) null_access },
804 { &vop_createvobject_desc, (void *) null_createvobject },
805 { &vop_destroyvobject_desc, (void *) null_destroyvobject },
806 { &vop_getattr_desc, (void *) null_getattr },
807 { &vop_getvobject_desc, (void *) null_getvobject },
808 { &vop_inactive_desc, (void *) null_inactive },
809 { &vop_islocked_desc, (void *) null_islocked },
810 { &vop_lock_desc, (void *) null_lock },
811 { &vop_lookup_desc, (void *) null_lookup },
812 { &vop_open_desc, (void *) null_open },
813 { &vop_print_desc, (void *) null_print },
814 { &vop_reclaim_desc, (void *) null_reclaim },
815 { &vop_rename_desc, (void *) null_rename },
816 { &vop_setattr_desc, (void *) null_setattr },
817 { &vop_unlock_desc, (void *) null_unlock },
984263bc
MD
818 { NULL, NULL }
819};
820static struct vnodeopv_desc null_vnodeop_opv_desc =
2d3e977e 821 { &null_vnode_vops, null_vnodeop_entries };
984263bc
MD
822
823VNODEOP_SET(null_vnodeop_opv_desc);