Use M_INTWAIT, not M_NOWAIT. We don't really support fast interrupt
[dragonfly.git] / sys / vfs / nullfs / null_vnops.c
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 $
41  * $DragonFly: src/sys/vfs/nullfs/null_vnops.c,v 1.21 2004/12/17 00:18:30 dillon Exp $
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>
184 #include <sys/proc.h>
185 #include <sys/namei.h>
186 #include <sys/malloc.h>
187 #include <sys/buf.h>
188 #include "null.h"
189
190 static int null_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
191 SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW, 
192         &null_bug_bypass, 0, "");
193
194 static int      null_nresolve(struct vop_nresolve_args *ap);
195 static int      null_ncreate(struct vop_ncreate_args *ap);
196 static int      null_nmkdir(struct vop_nmkdir_args *ap);
197 static int      null_nremove(struct vop_nremove_args *ap);
198 static int      null_nrmdir(struct vop_nrmdir_args *ap);
199 static int      null_nrename(struct vop_nrename_args *ap);
200
201 static int      null_revoke(struct vop_revoke_args *ap);
202 static int      null_access(struct vop_access_args *ap);
203 static int      null_createvobject(struct vop_createvobject_args *ap);
204 static int      null_destroyvobject(struct vop_destroyvobject_args *ap);
205 static int      null_getattr(struct vop_getattr_args *ap);
206 static int      null_getvobject(struct vop_getvobject_args *ap);
207 static int      null_inactive(struct vop_inactive_args *ap);
208 static int      null_islocked(struct vop_islocked_args *ap);
209 static int      null_lock(struct vop_lock_args *ap);
210 static int      null_lookup(struct vop_lookup_args *ap);
211 static int      null_open(struct vop_open_args *ap);
212 static int      null_print(struct vop_print_args *ap);
213 static int      null_reclaim(struct vop_reclaim_args *ap);
214 static int      null_rename(struct vop_rename_args *ap);
215 static int      null_setattr(struct vop_setattr_args *ap);
216 static int      null_unlock(struct vop_unlock_args *ap);
217
218 /*
219  * This is the 10-Apr-92 bypass routine.
220  *    This version has been optimized for speed, throwing away some
221  * safety checks.  It should still always work, but it's not as
222  * robust to programmer errors.
223  *
224  * In general, we map all vnodes going down and unmap them on the way back.
225  * As an exception to this, vnodes can be marked "unmapped" by setting
226  * the Nth bit in operation's vdesc_flags.
227  *
228  * Also, some BSD vnode operations have the side effect of vrele'ing
229  * their arguments.  With stacking, the reference counts are held
230  * by the upper node, not the lower one, so we must handle these
231  * side-effects here.  This is not of concern in Sun-derived systems
232  * since there are no such side-effects.
233  *
234  * This makes the following assumptions:
235  * - only one returned vpp
236  * - no INOUT vpp's (Sun's vop_open has one of these)
237  * - the vnode operation vector of the first vnode should be used
238  *   to determine what implementation of the op should be invoked
239  * - all mapped vnodes are of our vnode-type (NEEDSWORK:
240  *   problems on rmdir'ing mount points and renaming?)
241  *
242  * null_bypass(struct vnodeop_desc *a_desc, ...)
243  */
244 int
245 null_bypass(struct vop_generic_args *ap)
246 {
247         struct vnode **this_vp_p;
248         int error;
249         struct vnode *old_vps[VDESC_MAX_VPS];
250         struct vnode **vps_p[VDESC_MAX_VPS];
251         struct vnode ***vppp;
252         struct vnodeop_desc *descp = ap->a_desc;
253         int reles, i, j;
254
255         if (null_bug_bypass)
256                 printf ("null_bypass: %s\n", descp->vdesc_name);
257
258 #ifdef DIAGNOSTIC
259         /*
260          * We require at least one vp.
261          */
262         if (descp->vdesc_vp_offsets == NULL ||
263             descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
264                 panic ("null_bypass: no vp's in map");
265 #endif
266
267         /*
268          * Map the vnodes going in.
269          */
270         reles = descp->vdesc_flags;
271         for (i = 0; i < VDESC_MAX_VPS; ++i) {
272                 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
273                         break;   /* bail out at end of list */
274                 vps_p[i] = this_vp_p =
275                         VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap);
276                 /*
277                  * We're not guaranteed that any but the first vnode
278                  * are of our type.  Check for and don't map any
279                  * that aren't.  (We must always map first vp or vclean fails.)
280                  */
281                 if (i && (*this_vp_p == NULLVP ||
282                     (*this_vp_p)->v_tag != VT_NULL)) {
283                         old_vps[i] = NULLVP;
284                 } else {
285                         old_vps[i] = *this_vp_p;
286                         *this_vp_p = NULLVPTOLOWERVP(*this_vp_p);
287                         /*
288                          * Several operations have the side effect of vrele'ing
289                          * their vp's.  We must account for that in the lower
290                          * vp we pass down.
291                          */
292                         if (reles & (VDESC_VP0_WILLRELE << i))
293                                 vref(*this_vp_p);
294                 }
295
296         }
297
298         /*
299          * Call the operation on the lower layer with the modified
300          * argument structure.  We have to adjust a_fm to point to the
301          * lower vp's vop_ops structure.
302          */
303         if (vps_p[0] && *vps_p[0]) {
304                 ap->a_ops = *(*(vps_p[0]))->v_ops;
305                 error = vop_vnoperate_ap(ap);
306         } else {
307                 printf("null_bypass: no map for %s\n", descp->vdesc_name);
308                 error = EINVAL;
309         }
310
311         /*
312          * Maintain the illusion of call-by-value by restoring vnodes in the
313          * argument structure to their original value.
314          */
315         reles = descp->vdesc_flags;
316         for (i = 0; i < VDESC_MAX_VPS; ++i) {
317                 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
318                         break;   /* bail out at end of list */
319                 if (old_vps[i]) {
320                         *(vps_p[i]) = old_vps[i];
321
322                         /*
323                          * Since we operated on the lowervp's instead of the
324                          * null node vp's, we have to adjust the null node
325                          * vp's based on what the VOP did to the lower vp.
326                          * 
327                          * Note: the unlock case only occurs with rename.
328                          * tdvp and tvp are both locked on call and must be
329                          * unlocked on return.
330                          *
331                          * Unlock semantics indicate that if two locked vp's
332                          * are passed and they are the same vp, they are only
333                          * actually locked once.
334                          */
335                         if (reles & (VDESC_VP0_WILLUNLOCK << i)) {
336                                 VOP_UNLOCK(old_vps[i], LK_THISLAYER, curthread);
337                                 for (j = i + 1; j < VDESC_MAX_VPS; ++j) {
338                                         if (descp->vdesc_vp_offsets[j] == VDESC_NO_OFFSET)
339                                                 break;
340                                         if (old_vps[i] == old_vps[j]) {
341                                                 reles &= ~(1 << (VDESC_VP0_WILLUNLOCK << j));
342                                         }
343                                 }
344                         }
345
346                         if (reles & (VDESC_VP0_WILLRELE << i))
347                                 vrele(old_vps[i]);
348                 }
349         }
350
351         /*
352          * Map the possible out-going vpp
353          * (Assumes that the lower layer always returns
354          * a vref'ed vpp unless it gets an error.)
355          */
356         if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
357             !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
358             !error) {
359                 /*
360                  * XXX - even though some ops have vpp returned vp's,
361                  * several ops actually vrele this before returning.
362                  * We must avoid these ops.
363                  * (This should go away when these ops are regularized.)
364                  */
365                 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
366                         goto out;
367                 vppp = VOPARG_OFFSETTO(struct vnode***,
368                                  descp->vdesc_vpp_offset,ap);
369                 if (*vppp)
370                         error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp);
371         }
372
373  out:
374         return (error);
375 }
376
377 /*
378  * We have to carry on the locking protocol on the null layer vnodes
379  * as we progress through the tree. We also have to enforce read-only
380  * if this layer is mounted read-only.
381  *
382  * null_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
383  *              struct componentname *a_cnp)
384  */
385 static int
386 null_lookup(struct vop_lookup_args *ap)
387 {
388         struct componentname *cnp = ap->a_cnp;
389         struct vnode *dvp = ap->a_dvp;
390         struct thread *td = cnp->cn_td;
391         int flags = cnp->cn_flags;
392         struct vnode *vp, *ldvp, *lvp;
393         int error;
394
395         if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
396             (cnp->cn_nameiop == NAMEI_DELETE || 
397              cnp->cn_nameiop == NAMEI_RENAME)) {
398                 return (EROFS);
399         }
400         ldvp = NULLVPTOLOWERVP(dvp);
401
402         /*
403          * If we are doing a ".." lookup we must release the lock on dvp
404          * now, before we run a lookup in the underlying fs, or we may 
405          * deadlock.  If we do this we must protect ldvp by ref'ing it.
406          */
407         if (flags & CNP_ISDOTDOT) {
408                 vref(ldvp);
409                 VOP_UNLOCK(dvp, LK_THISLAYER, td);
410         }
411
412         /*
413          * Due to the non-deterministic nature of the handling of the
414          * parent directory lock by lookup, we cannot call null_bypass()
415          * here.  We must make a direct call.  It's faster to do a direct
416          * call, anyway.
417          */
418         vp = lvp = NULL;
419         error = VOP_LOOKUP(ldvp, &lvp, cnp);
420         if (error == EJUSTRETURN && 
421             (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
422             (cnp->cn_nameiop == NAMEI_CREATE || 
423              cnp->cn_nameiop == NAMEI_RENAME)) {
424                 error = EROFS;
425         }
426
427         if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
428                 if (ldvp == lvp) {
429                         *ap->a_vpp = dvp;
430                         vref(dvp);
431                         vrele(lvp);
432                 } else {
433                         error = null_node_create(dvp->v_mount, lvp, &vp);
434                         if (error == 0)
435                                 *ap->a_vpp = vp;
436                 }
437         }
438
439         /*
440          * The underlying fs will set PDIRUNLOCK if it unlocked the parent
441          * directory, which means we have to follow suit in the nullfs layer.
442          * Note that the parent directory may have already been unlocked due
443          * to the ".." case.  Note that use of cnp->cn_flags instead of flags.
444          */
445         if (flags & CNP_ISDOTDOT) {
446                 if ((cnp->cn_flags & CNP_PDIRUNLOCK) == 0)
447                         VOP_LOCK(dvp, LK_THISLAYER | LK_EXCLUSIVE, td);
448                 vrele(ldvp);
449         } else if (cnp->cn_flags & CNP_PDIRUNLOCK) {
450                 VOP_UNLOCK(dvp, LK_THISLAYER, td);
451         }
452         return (error);
453 }
454
455 /*
456  * Setattr call. Disallow write attempts if the layer is mounted read-only.
457  *
458  * null_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
459  *              struct vattr *a_vap, struct ucred *a_cred,
460  *              struct thread *a_td)
461  */
462 int
463 null_setattr(struct vop_setattr_args *ap)
464 {
465         struct vnode *vp = ap->a_vp;
466         struct vattr *vap = ap->a_vap;
467
468         if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
469             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
470             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
471             (vp->v_mount->mnt_flag & MNT_RDONLY))
472                 return (EROFS);
473         if (vap->va_size != VNOVAL) {
474                 switch (vp->v_type) {
475                 case VDIR:
476                         return (EISDIR);
477                 case VCHR:
478                 case VBLK:
479                 case VSOCK:
480                 case VFIFO:
481                         if (vap->va_flags != VNOVAL)
482                                 return (EOPNOTSUPP);
483                         return (0);
484                 case VREG:
485                 case VLNK:
486                 default:
487                         /*
488                          * Disallow write attempts if the filesystem is
489                          * mounted read-only.
490                          */
491                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
492                                 return (EROFS);
493                 }
494         }
495
496         return (null_bypass(&ap->a_head));
497 }
498
499 /*
500  *  We handle getattr only to change the fsid.
501  *
502  * null_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
503  *              struct thread *a_td)
504  */
505 static int
506 null_getattr(struct vop_getattr_args *ap)
507 {
508         int error;
509
510         if ((error = null_bypass(&ap->a_head)) != 0)
511                 return (error);
512
513         ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
514         return (0);
515 }
516
517 /*
518  * Resolve a locked ncp at the nullfs layer.
519  */
520 static int
521 null_nresolve(struct vop_nresolve_args *ap)
522 {
523         return(vop_compat_nresolve(ap));
524 }
525
526 /*
527  * Create a file
528  */
529 static int
530 null_ncreate(struct vop_ncreate_args *ap)
531 {
532         return(vop_compat_ncreate(ap));
533 }
534
535 static int
536 null_nmkdir(struct vop_nmkdir_args *ap)
537 {
538         return(vop_compat_nmkdir(ap));
539 }
540
541 static int
542 null_nremove(struct vop_nremove_args *ap)
543 {
544         return(vop_compat_nremove(ap));
545 }
546
547 static int
548 null_nrmdir(struct vop_nrmdir_args *ap)
549 {
550         return(vop_compat_nrmdir(ap));
551 }
552
553 static int
554 null_nrename(struct vop_nrename_args *ap)
555 {
556         return(vop_compat_nrename(ap));
557 }
558
559 /*
560  * revoke is VX locked, we can't go through null_bypass
561  */
562 static int
563 null_revoke(struct vop_revoke_args *ap)
564 {
565         struct null_node *np;
566         struct vnode *lvp;
567
568         np = VTONULL(ap->a_vp);
569         vx_unlock(ap->a_vp);
570         if ((lvp = np->null_lowervp) != NULL) {
571                 vx_get(lvp);
572                 VOP_REVOKE(lvp, ap->a_flags);
573                 vx_put(lvp);
574         }
575         vx_lock(ap->a_vp);
576         vgone(ap->a_vp);
577         return(0);
578 }
579
580 /*
581  * Handle to disallow write access if mounted read-only.
582  *
583  * null_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
584  *              struct thread *a_td)
585  */
586 static int
587 null_access(struct vop_access_args *ap)
588 {
589         struct vnode *vp = ap->a_vp;
590         mode_t mode = ap->a_mode;
591
592         /*
593          * Disallow write attempts on read-only layers;
594          * unless the file is a socket, fifo, or a block or
595          * character device resident on the file system.
596          */
597         if (mode & VWRITE) {
598                 switch (vp->v_type) {
599                 case VDIR:
600                 case VLNK:
601                 case VREG:
602                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
603                                 return (EROFS);
604                         break;
605                 default:
606                         break;
607                 }
608         }
609         return (null_bypass(&ap->a_head));
610 }
611
612 /*
613  * We must handle open to be able to catch MNT_NODEV and friends.
614  *
615  * null_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
616  *           struct thread *a_td)
617  */
618 static int
619 null_open(struct vop_open_args *ap)
620 {
621         struct vnode *vp = ap->a_vp;
622         struct vnode *lvp = NULLVPTOLOWERVP(ap->a_vp);
623
624         if ((vp->v_mount->mnt_flag & MNT_NODEV) &&
625             (lvp->v_type == VBLK || lvp->v_type == VCHR))
626                 return ENXIO;
627
628         return (null_bypass(&ap->a_head));
629 }
630
631 /*
632  * We handle this to eliminate null FS to lower FS
633  * file moving. Don't know why we don't allow this,
634  * possibly we should.
635  *
636  * null_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
637  *              struct componentname *a_fcnp, struct vnode *a_tdvp,
638  *              struct vnode *a_tvp, struct componentname *a_tcnp)
639  */
640 static int
641 null_rename(struct vop_rename_args *ap)
642 {
643         struct vnode *tdvp = ap->a_tdvp;
644         struct vnode *fvp = ap->a_fvp;
645         struct vnode *fdvp = ap->a_fdvp;
646         struct vnode *tvp = ap->a_tvp;
647
648         /* Check for cross-device rename. */
649         if ((fvp->v_mount != tdvp->v_mount) ||
650             (tvp && (fvp->v_mount != tvp->v_mount))) {
651                 if (tdvp == tvp)
652                         vrele(tdvp);
653                 else
654                         vput(tdvp);
655                 if (tvp)
656                         vput(tvp);
657                 vrele(fdvp);
658                 vrele(fvp);
659                 return (EXDEV);
660         }
661         
662         return (null_bypass(&ap->a_head));
663 }
664
665 /*
666  * A special flag, LK_THISLAYER, causes the locking function to operate
667  * ONLY on the nullfs layer.  Otherwise we are responsible for locking not
668  * only our layer, but the lower layer as well.
669  *
670  * null_lock(struct vnode *a_vp, int a_flags, struct thread *a_td)
671  */
672 static int
673 null_lock(struct vop_lock_args *ap)
674 {
675         struct vnode *vp = ap->a_vp;
676         int flags = ap->a_flags;
677         struct null_node *np = VTONULL(vp);
678         struct vnode *lvp;
679         int error;
680
681         /*
682          * Lock the nullfs layer first, disposing of the interlock in the
683          * process.
684          */
685         KKASSERT((flags & LK_INTERLOCK) == 0);
686         error = lockmgr(&vp->v_lock, flags & ~LK_THISLAYER,
687                         NULL, ap->a_td);
688
689         /*
690          * If locking only the nullfs layer, or if there is no lower layer,
691          * or if an error occured while attempting to lock the nullfs layer,
692          * we are done.
693          *
694          * np can be NULL is the vnode is being recycled from a previous
695          * hash collision.
696          */
697         if ((flags & LK_THISLAYER) || np == NULL ||
698             np->null_lowervp == NULL || error) {
699                 return (error);
700         }
701
702         /*
703          * Lock the underlying vnode.  If we are draining we should not drain
704          * the underlying vnode, since it is not being destroyed, but we do
705          * lock it exclusively in that case.  Note that any interlocks have
706          * already been disposed of above.
707          */
708         lvp = np->null_lowervp;
709         if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
710                 NULLFSDEBUG("null_lock: avoiding LK_DRAIN\n");
711                 error = vn_lock(lvp, (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE,
712                                 ap->a_td);
713         } else {
714                 error = vn_lock(lvp, flags, ap->a_td);
715         }
716
717         /*
718          * If an error occured we have to undo our nullfs lock, then return
719          * the original error.
720          */
721         if (error)
722                 lockmgr(&vp->v_lock, LK_RELEASE, NULL, ap->a_td);
723         return(error);
724 }
725
726 /*
727  * A special flag, LK_THISLAYER, causes the unlocking function to operate
728  * ONLY on the nullfs layer.  Otherwise we are responsible for unlocking not
729  * only our layer, but the lower layer as well.
730  *
731  * null_unlock(struct vnode *a_vp, int a_flags, struct thread *a_td)
732  */
733 static int
734 null_unlock(struct vop_unlock_args *ap)
735 {
736         struct vnode *vp = ap->a_vp;
737         int flags = ap->a_flags;
738         struct null_node *np = VTONULL(vp);
739         struct vnode *lvp;
740         int error;
741
742         KKASSERT((flags & LK_INTERLOCK) == 0);
743         /*
744          * nullfs layer only
745          */
746         if (flags & LK_THISLAYER) {
747                 error = lockmgr(&vp->v_lock, 
748                                 (flags & ~LK_THISLAYER) | LK_RELEASE,
749                                 NULL, ap->a_td);
750                 return (error);
751         }
752
753         /*
754          * If there is no underlying vnode the lock operation occurs at
755          * the nullfs layer.  np can be NULL is the vnode is being recycled
756          * from a previous hash collision.
757          */
758         if (np == NULL || (lvp = np->null_lowervp) == NULL) {
759                 error = lockmgr(&vp->v_lock, flags | LK_RELEASE,
760                                 NULL, ap->a_td);
761                 return(error);
762         }
763
764         /*
765          * Unlock the lower layer first, then our nullfs layer.
766          */
767         VOP_UNLOCK(lvp, flags, ap->a_td);
768         error = lockmgr(&vp->v_lock, flags | LK_RELEASE, NULL, ap->a_td);
769         return (error);
770 }
771
772 /*
773  * null_islocked(struct vnode *a_vp, struct thread *a_td)
774  *
775  * If a lower layer exists return the lock status of the lower layer,
776  * otherwise return the lock status of our nullfs layer.
777  */
778 static int
779 null_islocked(struct vop_islocked_args *ap)
780 {
781         struct vnode *vp = ap->a_vp;
782         struct vnode *lvp;
783         struct null_node *np = VTONULL(vp);
784         int error;
785
786         lvp = np->null_lowervp;
787         if (lvp == NULL)
788                 error = lockstatus(&vp->v_lock, ap->a_td);
789         else
790                 error = VOP_ISLOCKED(lvp, ap->a_td);
791         return (error);
792 }
793
794
795 /*
796  * The vnode is no longer active.  However, the new VFS API may retain
797  * the node in the vfs cache.  There is no way to tell that someone issued
798  * a remove/rmdir operation on the underlying filesystem (yet), but we can't
799  * remove the lowervp reference here.
800  *
801  * null_inactive(struct vnode *a_vp, struct thread *a_td)
802  */
803 static int
804 null_inactive(struct vop_inactive_args *ap)
805 {
806         /*struct vnode *vp = ap->a_vp;*/
807         /*struct null_node *np = VTONULL(vp);*/
808
809         /*
810          * At the moment don't do anything here.  All the rest of the code
811          * assumes that lowervp will remain inact, and the inactive nullvp
812          * may be reactivated at any time.  XXX I'm not sure why the 4.x code
813          * even worked.
814          */
815
816         /*
817          * Now it is safe to release our nullfs layer vnode.
818          */
819         return (0);
820 }
821
822 /*
823  * We can free memory in null_inactive, but we do this
824  * here. (Possible to guard vp->v_data to point somewhere)
825  *
826  * null_reclaim(struct vnode *a_vp, struct thread *a_td)
827  */
828 static int
829 null_reclaim(struct vop_reclaim_args *ap)
830 {
831         struct vnode *vp = ap->a_vp;
832         struct vnode *lowervp;
833         struct null_node *np;
834
835         np = VTONULL(vp);
836         vp->v_data = NULL;
837         /*
838          * null_lowervp reference to lowervp.  The lower vnode's
839          * inactive routine may or may not be called when we do the
840          * final vrele().
841          */
842         if (np) {
843                 null_node_rem(np);
844                 lowervp = np->null_lowervp;
845                 np->null_lowervp = NULLVP;
846                 if (lowervp)
847                         vrele(lowervp);
848                 free(np, M_NULLFSNODE);
849         }
850         return (0);
851 }
852
853 /*
854  * null_print(struct vnode *a_vp)
855  */
856 static int
857 null_print(struct vop_print_args *ap)
858 {
859         struct vnode *vp = ap->a_vp;
860         struct null_node *np = VTONULL(vp);
861
862         if (np == NULL) {
863                 printf ("\ttag VT_NULLFS, vp=%p, NULL v_data!\n", vp);
864                 return(0);
865         }
866         printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, np->null_lowervp);
867         if (np->null_lowervp != NULL) {
868                 printf("\tlowervp_lock: ");
869                 lockmgr_printinfo(&np->null_lowervp->v_lock);
870         } else {
871                 printf("\tnull_lock: ");
872                 lockmgr_printinfo(&vp->v_lock);
873         }
874         printf("\n");
875         return (0);
876 }
877
878 /*
879  * Let an underlying filesystem do the work
880  *
881  * null_createvobject(struct vnode *vp, struct ucred *cred, struct proc *p)
882  */
883 static int
884 null_createvobject(struct vop_createvobject_args *ap)
885 {
886         struct vnode *vp = ap->a_vp;
887         struct vnode *lowervp = VTONULL(vp) ? NULLVPTOLOWERVP(vp) : NULL;
888         int error;
889
890         if (vp->v_type == VNON || lowervp == NULL)
891                 return 0;
892         error = VOP_CREATEVOBJECT(lowervp, ap->a_td);
893         if (error)
894                 return (error);
895         vp->v_flag |= VOBJBUF;
896         return (0);
897 }
898
899 /*
900  * We have nothing to destroy and this operation shouldn't be bypassed.
901  *
902  * null_destroyvobject(struct vnode *vp)
903  */
904 static int
905 null_destroyvobject(struct vop_destroyvobject_args *ap)
906 {
907         struct vnode *vp = ap->a_vp;
908
909         vp->v_flag &= ~VOBJBUF;
910         return (0);
911 }
912
913 /*
914  * null_getvobject(struct vnode *vp, struct vm_object **objpp)
915  *
916  * Note that this can be called when a vnode is being recycled, and
917  * v_data may be NULL in that case if nullfs had to recycle a vnode
918  * due to a null_node collision.
919  */
920 static int
921 null_getvobject(struct vop_getvobject_args *ap)
922 {
923         struct vnode *lvp;
924
925         if (ap->a_vp->v_data == NULL)
926                 return EINVAL;
927
928         lvp = NULLVPTOLOWERVP(ap->a_vp);
929         if (lvp == NULL)
930                 return EINVAL;
931         return (VOP_GETVOBJECT(lvp, ap->a_objpp));
932 }
933
934 /*
935  * Global vfs data structures
936  */
937 struct vnodeopv_entry_desc null_vnodeop_entries[] = {
938         { &vop_default_desc,            (void *) null_bypass },
939         { &vop_access_desc,             (void *) null_access },
940         { &vop_createvobject_desc,      (void *) null_createvobject },
941         { &vop_destroyvobject_desc,     (void *) null_destroyvobject },
942         { &vop_getattr_desc,            (void *) null_getattr },
943         { &vop_getvobject_desc,         (void *) null_getvobject },
944         { &vop_inactive_desc,           (void *) null_inactive },
945         { &vop_islocked_desc,           (void *) null_islocked },
946         { &vop_lock_desc,               (void *) null_lock },
947         { &vop_lookup_desc,             (void *) null_lookup },
948         { &vop_open_desc,               (void *) null_open },
949         { &vop_print_desc,              (void *) null_print },
950         { &vop_reclaim_desc,            (void *) null_reclaim },
951         { &vop_rename_desc,             (void *) null_rename },
952         { &vop_setattr_desc,            (void *) null_setattr },
953         { &vop_unlock_desc,             (void *) null_unlock },
954         { &vop_revoke_desc,             (void *) null_revoke },
955
956         { &vop_nresolve_desc,           (void *) null_nresolve },
957         { &vop_ncreate_desc,            (void *) null_ncreate },
958         { &vop_nmkdir_desc,             (void *) null_nmkdir },
959         { &vop_nremove_desc,            (void *) null_nremove },
960         { &vop_nrmdir_desc,             (void *) null_nrmdir },
961         { &vop_nrename_desc,            (void *) null_nrename },
962         { NULL, NULL }
963 };
964