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