null_vnops.c revision 54444
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1992, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * John Heidemann of the UCLA Ficus project. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 3. All advertising materials mentioning features or use of this software 171541Srgrimes * must display the following acknowledgement: 181541Srgrimes * This product includes software developed by the University of 191541Srgrimes * California, Berkeley and its contributors. 201541Srgrimes * 4. Neither the name of the University nor the names of its contributors 211541Srgrimes * may be used to endorse or promote products derived from this software 221541Srgrimes * without specific prior written permission. 231541Srgrimes * 241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341541Srgrimes * SUCH DAMAGE. 351541Srgrimes * 3622521Sdyson * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95 371541Srgrimes * 3822521Sdyson * Ancestors: 3922521Sdyson * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92 4050477Speter * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 54444 1999-12-11 16:13:02Z eivind $ 4122521Sdyson * ...and... 4222521Sdyson * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project 4322521Sdyson * 4450477Speter * $FreeBSD: head/sys/fs/nullfs/null_vnops.c 54444 1999-12-11 16:13:02Z eivind $ 451541Srgrimes */ 461541Srgrimes 471541Srgrimes/* 481541Srgrimes * Null Layer 491541Srgrimes * 501541Srgrimes * (See mount_null(8) for more information.) 511541Srgrimes * 521541Srgrimes * The null layer duplicates a portion of the file system 531541Srgrimes * name space under a new name. In this respect, it is 541541Srgrimes * similar to the loopback file system. It differs from 551541Srgrimes * the loopback fs in two respects: it is implemented using 5635256Sdes * a stackable layers techniques, and its "null-node"s stack above 571541Srgrimes * all lower-layer vnodes, not just over directory vnodes. 581541Srgrimes * 591541Srgrimes * The null layer has two purposes. First, it serves as a demonstration 601541Srgrimes * of layering by proving a layer which does nothing. (It actually 611541Srgrimes * does everything the loopback file system does, which is slightly 621541Srgrimes * more than nothing.) Second, the null layer can serve as a prototype 631541Srgrimes * layer. Since it provides all necessary layer framework, 641541Srgrimes * new file system layers can be created very easily be starting 651541Srgrimes * with a null layer. 661541Srgrimes * 671541Srgrimes * The remainder of this man page examines the null layer as a basis 681541Srgrimes * for constructing new layers. 691541Srgrimes * 701541Srgrimes * 711541Srgrimes * INSTANTIATING NEW NULL LAYERS 721541Srgrimes * 731541Srgrimes * New null layers are created with mount_null(8). 741541Srgrimes * Mount_null(8) takes two arguments, the pathname 751541Srgrimes * of the lower vfs (target-pn) and the pathname where the null 761541Srgrimes * layer will appear in the namespace (alias-pn). After 771541Srgrimes * the null layer is put into place, the contents 781541Srgrimes * of target-pn subtree will be aliased under alias-pn. 791541Srgrimes * 801541Srgrimes * 811541Srgrimes * OPERATION OF A NULL LAYER 821541Srgrimes * 831541Srgrimes * The null layer is the minimum file system layer, 841541Srgrimes * simply bypassing all possible operations to the lower layer 851541Srgrimes * for processing there. The majority of its activity centers 8626963Salex * on the bypass routine, through which nearly all vnode operations 871541Srgrimes * pass. 881541Srgrimes * 891541Srgrimes * The bypass routine accepts arbitrary vnode operations for 901541Srgrimes * handling by the lower layer. It begins by examing vnode 911541Srgrimes * operation arguments and replacing any null-nodes by their 921541Srgrimes * lower-layer equivlants. It then invokes the operation 931541Srgrimes * on the lower layer. Finally, it replaces the null-nodes 941541Srgrimes * in the arguments and, if a vnode is return by the operation, 951541Srgrimes * stacks a null-node on top of the returned vnode. 961541Srgrimes * 9722521Sdyson * Although bypass handles most operations, vop_getattr, vop_lock, 9822521Sdyson * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not 9922521Sdyson * bypassed. Vop_getattr must change the fsid being returned. 10022521Sdyson * Vop_lock and vop_unlock must handle any locking for the 10122521Sdyson * current vnode as well as pass the lock request down. 1021541Srgrimes * Vop_inactive and vop_reclaim are not bypassed so that 10322521Sdyson * they can handle freeing null-layer specific data. Vop_print 10422521Sdyson * is not bypassed to avoid excessive debugging information. 10522521Sdyson * Also, certain vnode operations change the locking state within 10622521Sdyson * the operation (create, mknod, remove, link, rename, mkdir, rmdir, 10722521Sdyson * and symlink). Ideally these operations should not change the 10822521Sdyson * lock state, but should be changed to let the caller of the 10922521Sdyson * function unlock them. Otherwise all intermediate vnode layers 11022521Sdyson * (such as union, umapfs, etc) must catch these functions to do 11122521Sdyson * the necessary locking at their layer. 1121541Srgrimes * 1131541Srgrimes * 1141541Srgrimes * INSTANTIATING VNODE STACKS 1151541Srgrimes * 1161541Srgrimes * Mounting associates the null layer with a lower layer, 1171541Srgrimes * effect stacking two VFSes. Vnode stacks are instead 1181541Srgrimes * created on demand as files are accessed. 1191541Srgrimes * 1201541Srgrimes * The initial mount creates a single vnode stack for the 1211541Srgrimes * root of the new null layer. All other vnode stacks 1221541Srgrimes * are created as a result of vnode operations on 1231541Srgrimes * this or other null vnode stacks. 1241541Srgrimes * 1251541Srgrimes * New vnode stacks come into existance as a result of 1268876Srgrimes * an operation which returns a vnode. 1271541Srgrimes * The bypass routine stacks a null-node above the new 1281541Srgrimes * vnode before returning it to the caller. 1291541Srgrimes * 1301541Srgrimes * For example, imagine mounting a null layer with 1311541Srgrimes * "mount_null /usr/include /dev/layer/null". 1321541Srgrimes * Changing directory to /dev/layer/null will assign 1331541Srgrimes * the root null-node (which was created when the null layer was mounted). 1341541Srgrimes * Now consider opening "sys". A vop_lookup would be 1351541Srgrimes * done on the root null-node. This operation would bypass through 1368876Srgrimes * to the lower layer which would return a vnode representing 1371541Srgrimes * the UFS "sys". Null_bypass then builds a null-node 1381541Srgrimes * aliasing the UFS "sys" and returns this to the caller. 1391541Srgrimes * Later operations on the null-node "sys" will repeat this 1401541Srgrimes * process when constructing other vnode stacks. 1411541Srgrimes * 1421541Srgrimes * 1431541Srgrimes * CREATING OTHER FILE SYSTEM LAYERS 1441541Srgrimes * 1451541Srgrimes * One of the easiest ways to construct new file system layers is to make 1461541Srgrimes * a copy of the null layer, rename all files and variables, and 1471541Srgrimes * then begin modifing the copy. Sed can be used to easily rename 1481541Srgrimes * all variables. 1491541Srgrimes * 1508876Srgrimes * The umap layer is an example of a layer descended from the 1511541Srgrimes * null layer. 1521541Srgrimes * 1531541Srgrimes * 1541541Srgrimes * INVOKING OPERATIONS ON LOWER LAYERS 1551541Srgrimes * 1568876Srgrimes * There are two techniques to invoke operations on a lower layer 1571541Srgrimes * when the operation cannot be completely bypassed. Each method 1581541Srgrimes * is appropriate in different situations. In both cases, 1591541Srgrimes * it is the responsibility of the aliasing layer to make 1601541Srgrimes * the operation arguments "correct" for the lower layer 1611541Srgrimes * by mapping an vnode arguments to the lower layer. 1621541Srgrimes * 1631541Srgrimes * The first approach is to call the aliasing layer's bypass routine. 1641541Srgrimes * This method is most suitable when you wish to invoke the operation 16526964Salex * currently being handled on the lower layer. It has the advantage 1661541Srgrimes * that the bypass routine already must do argument mapping. 1671541Srgrimes * An example of this is null_getattrs in the null layer. 1681541Srgrimes * 16926964Salex * A second approach is to directly invoke vnode operations on 1701541Srgrimes * the lower layer with the VOP_OPERATIONNAME interface. 1711541Srgrimes * The advantage of this method is that it is easy to invoke 1721541Srgrimes * arbitrary operations on the lower layer. The disadvantage 17326964Salex * is that vnode arguments must be manualy mapped. 1741541Srgrimes * 1751541Srgrimes */ 1761541Srgrimes 1771541Srgrimes#include <sys/param.h> 1781541Srgrimes#include <sys/systm.h> 1792960Swollman#include <sys/kernel.h> 18012769Sphk#include <sys/sysctl.h> 1811541Srgrimes#include <sys/vnode.h> 1821541Srgrimes#include <sys/mount.h> 1831541Srgrimes#include <sys/namei.h> 1841541Srgrimes#include <sys/malloc.h> 1851541Srgrimes#include <sys/buf.h> 1861541Srgrimes#include <miscfs/nullfs/null.h> 1871541Srgrimes 18812769Sphkstatic int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */ 18912769SphkSYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW, 19012769Sphk &null_bug_bypass, 0, ""); 1911541Srgrimes 19222597Smppstatic int null_access __P((struct vop_access_args *ap)); 19312769Sphkstatic int null_getattr __P((struct vop_getattr_args *ap)); 19412769Sphkstatic int null_inactive __P((struct vop_inactive_args *ap)); 19522597Smppstatic int null_lock __P((struct vop_lock_args *ap)); 19622597Smppstatic int null_lookup __P((struct vop_lookup_args *ap)); 19712769Sphkstatic int null_print __P((struct vop_print_args *ap)); 19812769Sphkstatic int null_reclaim __P((struct vop_reclaim_args *ap)); 19922597Smppstatic int null_setattr __P((struct vop_setattr_args *ap)); 20022597Smppstatic int null_unlock __P((struct vop_unlock_args *ap)); 20112595Sbde 2021541Srgrimes/* 2031541Srgrimes * This is the 10-Apr-92 bypass routine. 2041541Srgrimes * This version has been optimized for speed, throwing away some 2051541Srgrimes * safety checks. It should still always work, but it's not as 2061541Srgrimes * robust to programmer errors. 2071541Srgrimes * 2081541Srgrimes * In general, we map all vnodes going down and unmap them on the way back. 2091541Srgrimes * As an exception to this, vnodes can be marked "unmapped" by setting 2101541Srgrimes * the Nth bit in operation's vdesc_flags. 2111541Srgrimes * 2121541Srgrimes * Also, some BSD vnode operations have the side effect of vrele'ing 2131541Srgrimes * their arguments. With stacking, the reference counts are held 2141541Srgrimes * by the upper node, not the lower one, so we must handle these 2151541Srgrimes * side-effects here. This is not of concern in Sun-derived systems 2161541Srgrimes * since there are no such side-effects. 2171541Srgrimes * 2181541Srgrimes * This makes the following assumptions: 2191541Srgrimes * - only one returned vpp 2201541Srgrimes * - no INOUT vpp's (Sun's vop_open has one of these) 2211541Srgrimes * - the vnode operation vector of the first vnode should be used 2221541Srgrimes * to determine what implementation of the op should be invoked 2231541Srgrimes * - all mapped vnodes are of our vnode-type (NEEDSWORK: 2241541Srgrimes * problems on rmdir'ing mount points and renaming?) 2258876Srgrimes */ 22622521Sdysonint 2271541Srgrimesnull_bypass(ap) 2281541Srgrimes struct vop_generic_args /* { 2291541Srgrimes struct vnodeop_desc *a_desc; 2301541Srgrimes <other random data follows, presumably> 2311541Srgrimes } */ *ap; 2321541Srgrimes{ 2331541Srgrimes register struct vnode **this_vp_p; 2341541Srgrimes int error; 2351541Srgrimes struct vnode *old_vps[VDESC_MAX_VPS]; 2361541Srgrimes struct vnode **vps_p[VDESC_MAX_VPS]; 2371541Srgrimes struct vnode ***vppp; 2381541Srgrimes struct vnodeop_desc *descp = ap->a_desc; 2391541Srgrimes int reles, i; 2401541Srgrimes 2411541Srgrimes if (null_bug_bypass) 2421541Srgrimes printf ("null_bypass: %s\n", descp->vdesc_name); 2431541Srgrimes 24450616Sbde#ifdef DIAGNOSTIC 2451541Srgrimes /* 2461541Srgrimes * We require at least one vp. 2471541Srgrimes */ 2481541Srgrimes if (descp->vdesc_vp_offsets == NULL || 2491541Srgrimes descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET) 25050616Sbde panic ("null_bypass: no vp's in map"); 2511541Srgrimes#endif 2521541Srgrimes 2531541Srgrimes /* 2541541Srgrimes * Map the vnodes going in. 2551541Srgrimes * Later, we'll invoke the operation based on 2561541Srgrimes * the first mapped vnode's operation vector. 2571541Srgrimes */ 2581541Srgrimes reles = descp->vdesc_flags; 2591541Srgrimes for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 2601541Srgrimes if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 2611541Srgrimes break; /* bail out at end of list */ 2628876Srgrimes vps_p[i] = this_vp_p = 2631541Srgrimes VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap); 2641541Srgrimes /* 2651541Srgrimes * We're not guaranteed that any but the first vnode 2661541Srgrimes * are of our type. Check for and don't map any 2671541Srgrimes * that aren't. (We must always map first vp or vclean fails.) 2681541Srgrimes */ 26924987Skato if (i && (*this_vp_p == NULLVP || 27022521Sdyson (*this_vp_p)->v_op != null_vnodeop_p)) { 27124987Skato old_vps[i] = NULLVP; 2721541Srgrimes } else { 2731541Srgrimes old_vps[i] = *this_vp_p; 2741541Srgrimes *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p); 2751541Srgrimes /* 2761541Srgrimes * XXX - Several operations have the side effect 2771541Srgrimes * of vrele'ing their vp's. We must account for 2781541Srgrimes * that. (This should go away in the future.) 2791541Srgrimes */ 2801541Srgrimes if (reles & 1) 2811541Srgrimes VREF(*this_vp_p); 2821541Srgrimes } 2838876Srgrimes 2841541Srgrimes } 2851541Srgrimes 2861541Srgrimes /* 2871541Srgrimes * Call the operation on the lower layer 2881541Srgrimes * with the modified argument structure. 2891541Srgrimes */ 2901541Srgrimes error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap); 2911541Srgrimes 2921541Srgrimes /* 2931541Srgrimes * Maintain the illusion of call-by-value 2941541Srgrimes * by restoring vnodes in the argument structure 2951541Srgrimes * to their original value. 2961541Srgrimes */ 2971541Srgrimes reles = descp->vdesc_flags; 2981541Srgrimes for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 2991541Srgrimes if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 3001541Srgrimes break; /* bail out at end of list */ 3011541Srgrimes if (old_vps[i]) { 3021541Srgrimes *(vps_p[i]) = old_vps[i]; 3031541Srgrimes if (reles & 1) 3041541Srgrimes vrele(*(vps_p[i])); 3051541Srgrimes } 3061541Srgrimes } 3071541Srgrimes 3081541Srgrimes /* 3091541Srgrimes * Map the possible out-going vpp 3101541Srgrimes * (Assumes that the lower layer always returns 3111541Srgrimes * a VREF'ed vpp unless it gets an error.) 3121541Srgrimes */ 3131541Srgrimes if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && 3141541Srgrimes !(descp->vdesc_flags & VDESC_NOMAP_VPP) && 3151541Srgrimes !error) { 3161541Srgrimes /* 3171541Srgrimes * XXX - even though some ops have vpp returned vp's, 3181541Srgrimes * several ops actually vrele this before returning. 3191541Srgrimes * We must avoid these ops. 3201541Srgrimes * (This should go away when these ops are regularized.) 3211541Srgrimes */ 3221541Srgrimes if (descp->vdesc_flags & VDESC_VPP_WILLRELE) 3231541Srgrimes goto out; 3241541Srgrimes vppp = VOPARG_OFFSETTO(struct vnode***, 3251541Srgrimes descp->vdesc_vpp_offset,ap); 32629584Sphk if (*vppp) 32729584Sphk error = null_node_create(old_vps[0]->v_mount, **vppp, *vppp); 3281541Srgrimes } 3291541Srgrimes 3301541Srgrimes out: 3311541Srgrimes return (error); 3321541Srgrimes} 3331541Srgrimes 33422521Sdyson/* 33522521Sdyson * We have to carry on the locking protocol on the null layer vnodes 33622521Sdyson * as we progress through the tree. We also have to enforce read-only 33722521Sdyson * if this layer is mounted read-only. 33822521Sdyson */ 33922521Sdysonstatic int 34022521Sdysonnull_lookup(ap) 34122521Sdyson struct vop_lookup_args /* { 34222521Sdyson struct vnode * a_dvp; 34322521Sdyson struct vnode ** a_vpp; 34422521Sdyson struct componentname * a_cnp; 34522521Sdyson } */ *ap; 34622521Sdyson{ 34722521Sdyson struct componentname *cnp = ap->a_cnp; 34822521Sdyson struct proc *p = cnp->cn_proc; 34922521Sdyson int flags = cnp->cn_flags; 35022521Sdyson struct vop_lock_args lockargs; 35122521Sdyson struct vop_unlock_args unlockargs; 35222521Sdyson struct vnode *dvp, *vp; 35322521Sdyson int error; 3541541Srgrimes 35522521Sdyson if ((flags & ISLASTCN) && (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) && 35622521Sdyson (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 35722521Sdyson return (EROFS); 35822607Smpp error = null_bypass((struct vop_generic_args *)ap); 35922521Sdyson if (error == EJUSTRETURN && (flags & ISLASTCN) && 36022521Sdyson (ap->a_dvp->v_mount->mnt_flag & MNT_RDONLY) && 36122521Sdyson (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 36222521Sdyson error = EROFS; 36322521Sdyson /* 36422521Sdyson * We must do the same locking and unlocking at this layer as 36522521Sdyson * is done in the layers below us. We could figure this out 36622521Sdyson * based on the error return and the LASTCN, LOCKPARENT, and 36722521Sdyson * LOCKLEAF flags. However, it is more expidient to just find 36822521Sdyson * out the state of the lower level vnodes and set ours to the 36922521Sdyson * same state. 37022521Sdyson */ 37122521Sdyson dvp = ap->a_dvp; 37222521Sdyson vp = *ap->a_vpp; 37322521Sdyson if (dvp == vp) 37422521Sdyson return (error); 37554444Seivind if (!VOP_ISLOCKED(dvp, NULL)) { 37622521Sdyson unlockargs.a_vp = dvp; 37722521Sdyson unlockargs.a_flags = 0; 37822521Sdyson unlockargs.a_p = p; 37922521Sdyson vop_nounlock(&unlockargs); 38022521Sdyson } 38154444Seivind if (vp != NULLVP && VOP_ISLOCKED(vp, NULL)) { 38222521Sdyson lockargs.a_vp = vp; 38322521Sdyson lockargs.a_flags = LK_SHARED; 38422521Sdyson lockargs.a_p = p; 38522521Sdyson vop_nolock(&lockargs); 38622521Sdyson } 38722521Sdyson return (error); 38822521Sdyson} 38922521Sdyson 3901541Srgrimes/* 39122521Sdyson * Setattr call. Disallow write attempts if the layer is mounted read-only. 39222521Sdyson */ 39322521Sdysonint 39422521Sdysonnull_setattr(ap) 39522521Sdyson struct vop_setattr_args /* { 39622521Sdyson struct vnodeop_desc *a_desc; 39722521Sdyson struct vnode *a_vp; 39822521Sdyson struct vattr *a_vap; 39922521Sdyson struct ucred *a_cred; 40022521Sdyson struct proc *a_p; 40122521Sdyson } */ *ap; 40222521Sdyson{ 40322521Sdyson struct vnode *vp = ap->a_vp; 40422521Sdyson struct vattr *vap = ap->a_vap; 40522521Sdyson 40622521Sdyson if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 40722597Smpp vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 40822597Smpp vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 40922521Sdyson (vp->v_mount->mnt_flag & MNT_RDONLY)) 41022521Sdyson return (EROFS); 41122521Sdyson if (vap->va_size != VNOVAL) { 41222521Sdyson switch (vp->v_type) { 41322521Sdyson case VDIR: 41422521Sdyson return (EISDIR); 41522521Sdyson case VCHR: 41622521Sdyson case VBLK: 41722521Sdyson case VSOCK: 41822521Sdyson case VFIFO: 41936840Speter if (vap->va_flags != VNOVAL) 42036840Speter return (EOPNOTSUPP); 42122521Sdyson return (0); 42222521Sdyson case VREG: 42322521Sdyson case VLNK: 42422521Sdyson default: 42522521Sdyson /* 42622521Sdyson * Disallow write attempts if the filesystem is 42722521Sdyson * mounted read-only. 42822521Sdyson */ 42922521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 43022521Sdyson return (EROFS); 43122521Sdyson } 43222521Sdyson } 43322607Smpp return (null_bypass((struct vop_generic_args *)ap)); 43422521Sdyson} 43522521Sdyson 43622521Sdyson/* 4371541Srgrimes * We handle getattr only to change the fsid. 4381541Srgrimes */ 43912769Sphkstatic int 4401541Srgrimesnull_getattr(ap) 4411541Srgrimes struct vop_getattr_args /* { 4421541Srgrimes struct vnode *a_vp; 4431541Srgrimes struct vattr *a_vap; 4441541Srgrimes struct ucred *a_cred; 4451541Srgrimes struct proc *a_p; 4461541Srgrimes } */ *ap; 4471541Srgrimes{ 4481541Srgrimes int error; 44922521Sdyson 45043311Sdillon if ((error = null_bypass((struct vop_generic_args *)ap)) != 0) 4511541Srgrimes return (error); 4521541Srgrimes return (0); 4531541Srgrimes} 4541541Srgrimes 45522521Sdysonstatic int 45622521Sdysonnull_access(ap) 45722521Sdyson struct vop_access_args /* { 45822521Sdyson struct vnode *a_vp; 45922521Sdyson int a_mode; 46022521Sdyson struct ucred *a_cred; 46122521Sdyson struct proc *a_p; 46222521Sdyson } */ *ap; 46322521Sdyson{ 46422521Sdyson struct vnode *vp = ap->a_vp; 46522521Sdyson mode_t mode = ap->a_mode; 4661541Srgrimes 46722521Sdyson /* 46822521Sdyson * Disallow write attempts on read-only layers; 46922521Sdyson * unless the file is a socket, fifo, or a block or 47022521Sdyson * character device resident on the file system. 47122521Sdyson */ 47222521Sdyson if (mode & VWRITE) { 47322521Sdyson switch (vp->v_type) { 47422521Sdyson case VDIR: 47522521Sdyson case VLNK: 47622521Sdyson case VREG: 47722521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 47822521Sdyson return (EROFS); 47922521Sdyson break; 48043305Sdillon default: 48143305Sdillon break; 48222521Sdyson } 48322521Sdyson } 48422607Smpp return (null_bypass((struct vop_generic_args *)ap)); 48522521Sdyson} 48622521Sdyson 48722521Sdyson/* 48822521Sdyson * We need to process our own vnode lock and then clear the 48922521Sdyson * interlock flag as it applies only to our vnode, not the 49022521Sdyson * vnodes below us on the stack. 49122521Sdyson */ 49222597Smppstatic int 49322521Sdysonnull_lock(ap) 49422521Sdyson struct vop_lock_args /* { 49522521Sdyson struct vnode *a_vp; 49622521Sdyson int a_flags; 49722521Sdyson struct proc *a_p; 49822521Sdyson } */ *ap; 49922521Sdyson{ 50022521Sdyson 50122521Sdyson vop_nolock(ap); 50222521Sdyson if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN) 50322521Sdyson return (0); 50422521Sdyson ap->a_flags &= ~LK_INTERLOCK; 50522607Smpp return (null_bypass((struct vop_generic_args *)ap)); 50622521Sdyson} 50722521Sdyson 50822521Sdyson/* 50922521Sdyson * We need to process our own vnode unlock and then clear the 51022521Sdyson * interlock flag as it applies only to our vnode, not the 51122521Sdyson * vnodes below us on the stack. 51222521Sdyson */ 51322597Smppstatic int 51422521Sdysonnull_unlock(ap) 51522521Sdyson struct vop_unlock_args /* { 51622521Sdyson struct vnode *a_vp; 51722521Sdyson int a_flags; 51822521Sdyson struct proc *a_p; 51922521Sdyson } */ *ap; 52022521Sdyson{ 52122521Sdyson vop_nounlock(ap); 52222521Sdyson ap->a_flags &= ~LK_INTERLOCK; 52322607Smpp return (null_bypass((struct vop_generic_args *)ap)); 52422521Sdyson} 52522521Sdyson 52622597Smppstatic int 5271541Srgrimesnull_inactive(ap) 5281541Srgrimes struct vop_inactive_args /* { 5291541Srgrimes struct vnode *a_vp; 53022521Sdyson struct proc *a_p; 5311541Srgrimes } */ *ap; 5321541Srgrimes{ 53330636Sroberto struct vnode *vp = ap->a_vp; 53430636Sroberto struct null_node *xp = VTONULL(vp); 53530636Sroberto struct vnode *lowervp = xp->null_lowervp; 5361541Srgrimes /* 5371541Srgrimes * Do nothing (and _don't_ bypass). 5381541Srgrimes * Wait to vrele lowervp until reclaim, 5391541Srgrimes * so that until then our null_node is in the 5401541Srgrimes * cache and reusable. 54130636Sroberto * We still have to tell the lower layer the vnode 54230636Sroberto * is now inactive though. 5431541Srgrimes * 5441541Srgrimes * NEEDSWORK: Someday, consider inactive'ing 5451541Srgrimes * the lowervp and then trying to reactivate it 5461541Srgrimes * with capabilities (v_id) 5471541Srgrimes * like they do in the name lookup cache code. 5481541Srgrimes * That's too much work for now. 5491541Srgrimes */ 55030636Sroberto VOP_INACTIVE(lowervp, ap->a_p); 55122521Sdyson VOP_UNLOCK(ap->a_vp, 0, ap->a_p); 5521541Srgrimes return (0); 5531541Srgrimes} 5541541Srgrimes 55512769Sphkstatic int 5561541Srgrimesnull_reclaim(ap) 5571541Srgrimes struct vop_reclaim_args /* { 5581541Srgrimes struct vnode *a_vp; 55922521Sdyson struct proc *a_p; 5601541Srgrimes } */ *ap; 5611541Srgrimes{ 5621541Srgrimes struct vnode *vp = ap->a_vp; 5631541Srgrimes struct null_node *xp = VTONULL(vp); 5641541Srgrimes struct vnode *lowervp = xp->null_lowervp; 5651541Srgrimes 5661541Srgrimes /* 5671541Srgrimes * Note: in vop_reclaim, vp->v_op == dead_vnodeop_p, 5681541Srgrimes * so we can't call VOPs on ourself. 5691541Srgrimes */ 5701541Srgrimes /* After this assignment, this node will not be re-used. */ 57124987Skato xp->null_lowervp = NULLVP; 57222521Sdyson LIST_REMOVE(xp, null_hash); 5731541Srgrimes FREE(vp->v_data, M_TEMP); 5741541Srgrimes vp->v_data = NULL; 5751541Srgrimes vrele (lowervp); 5761541Srgrimes return (0); 5771541Srgrimes} 5781541Srgrimes 57912769Sphkstatic int 5801541Srgrimesnull_print(ap) 5811541Srgrimes struct vop_print_args /* { 5821541Srgrimes struct vnode *a_vp; 5831541Srgrimes } */ *ap; 5841541Srgrimes{ 5851541Srgrimes register struct vnode *vp = ap->a_vp; 5863496Sphk printf ("\ttag VT_NULLFS, vp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp)); 5871541Srgrimes return (0); 5881541Srgrimes} 5891541Srgrimes 5901541Srgrimes/* 5911541Srgrimes * Global vfs data structures 5921541Srgrimes */ 59312158Sbdevop_t **null_vnodeop_p; 59412769Sphkstatic struct vnodeopv_entry_desc null_vnodeop_entries[] = { 59530431Sphk { &vop_default_desc, (vop_t *) null_bypass }, 59630431Sphk { &vop_access_desc, (vop_t *) null_access }, 59730431Sphk { &vop_getattr_desc, (vop_t *) null_getattr }, 59830434Sphk { &vop_inactive_desc, (vop_t *) null_inactive }, 59930431Sphk { &vop_lock_desc, (vop_t *) null_lock }, 60030431Sphk { &vop_lookup_desc, (vop_t *) null_lookup }, 60130431Sphk { &vop_print_desc, (vop_t *) null_print }, 60230431Sphk { &vop_reclaim_desc, (vop_t *) null_reclaim }, 60330431Sphk { &vop_setattr_desc, (vop_t *) null_setattr }, 60430431Sphk { &vop_unlock_desc, (vop_t *) null_unlock }, 60512158Sbde { NULL, NULL } 6061541Srgrimes}; 60712769Sphkstatic struct vnodeopv_desc null_vnodeop_opv_desc = 6081541Srgrimes { &null_vnodeop_p, null_vnodeop_entries }; 6092946Swollman 6102946SwollmanVNODEOP_SET(null_vnodeop_opv_desc); 611