1139776Simp/*- 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 * 4. Neither the name of the University nor the names of its contributors 171541Srgrimes * may be used to endorse or promote products derived from this software 181541Srgrimes * without specific prior written permission. 191541Srgrimes * 201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301541Srgrimes * SUCH DAMAGE. 311541Srgrimes * 3222521Sdyson * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95 331541Srgrimes * 3422521Sdyson * Ancestors: 3522521Sdyson * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92 3622521Sdyson * ...and... 3722521Sdyson * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project 3822521Sdyson * 3950477Speter * $FreeBSD$ 401541Srgrimes */ 411541Srgrimes 421541Srgrimes/* 431541Srgrimes * Null Layer 441541Srgrimes * 4577130Sru * (See mount_nullfs(8) for more information.) 461541Srgrimes * 4796755Strhodes * The null layer duplicates a portion of the filesystem 481541Srgrimes * name space under a new name. In this respect, it is 4996755Strhodes * similar to the loopback filesystem. It differs from 501541Srgrimes * the loopback fs in two respects: it is implemented using 5135256Sdes * a stackable layers techniques, and its "null-node"s stack above 521541Srgrimes * all lower-layer vnodes, not just over directory vnodes. 531541Srgrimes * 541541Srgrimes * The null layer has two purposes. First, it serves as a demonstration 551541Srgrimes * of layering by proving a layer which does nothing. (It actually 5696755Strhodes * does everything the loopback filesystem does, which is slightly 571541Srgrimes * more than nothing.) Second, the null layer can serve as a prototype 581541Srgrimes * layer. Since it provides all necessary layer framework, 5996755Strhodes * new filesystem layers can be created very easily be starting 601541Srgrimes * with a null layer. 611541Srgrimes * 621541Srgrimes * The remainder of this man page examines the null layer as a basis 631541Srgrimes * for constructing new layers. 641541Srgrimes * 651541Srgrimes * 661541Srgrimes * INSTANTIATING NEW NULL LAYERS 671541Srgrimes * 6877130Sru * New null layers are created with mount_nullfs(8). 6977130Sru * Mount_nullfs(8) takes two arguments, the pathname 701541Srgrimes * of the lower vfs (target-pn) and the pathname where the null 711541Srgrimes * layer will appear in the namespace (alias-pn). After 721541Srgrimes * the null layer is put into place, the contents 731541Srgrimes * of target-pn subtree will be aliased under alias-pn. 741541Srgrimes * 751541Srgrimes * 761541Srgrimes * OPERATION OF A NULL LAYER 771541Srgrimes * 7896755Strhodes * The null layer is the minimum filesystem layer, 791541Srgrimes * simply bypassing all possible operations to the lower layer 801541Srgrimes * for processing there. The majority of its activity centers 8126963Salex * on the bypass routine, through which nearly all vnode operations 821541Srgrimes * pass. 831541Srgrimes * 841541Srgrimes * The bypass routine accepts arbitrary vnode operations for 851541Srgrimes * handling by the lower layer. It begins by examing vnode 861541Srgrimes * operation arguments and replacing any null-nodes by their 871541Srgrimes * lower-layer equivlants. It then invokes the operation 881541Srgrimes * on the lower layer. Finally, it replaces the null-nodes 891541Srgrimes * in the arguments and, if a vnode is return by the operation, 901541Srgrimes * stacks a null-node on top of the returned vnode. 911541Srgrimes * 9222521Sdyson * Although bypass handles most operations, vop_getattr, vop_lock, 9322521Sdyson * vop_unlock, vop_inactive, vop_reclaim, and vop_print are not 9422521Sdyson * bypassed. Vop_getattr must change the fsid being returned. 9522521Sdyson * Vop_lock and vop_unlock must handle any locking for the 9622521Sdyson * current vnode as well as pass the lock request down. 971541Srgrimes * Vop_inactive and vop_reclaim are not bypassed so that 9822521Sdyson * they can handle freeing null-layer specific data. Vop_print 9922521Sdyson * is not bypassed to avoid excessive debugging information. 10022521Sdyson * Also, certain vnode operations change the locking state within 10122521Sdyson * the operation (create, mknod, remove, link, rename, mkdir, rmdir, 10222521Sdyson * and symlink). Ideally these operations should not change the 10322521Sdyson * lock state, but should be changed to let the caller of the 10422521Sdyson * function unlock them. Otherwise all intermediate vnode layers 10522521Sdyson * (such as union, umapfs, etc) must catch these functions to do 10622521Sdyson * the necessary locking at their layer. 1071541Srgrimes * 1081541Srgrimes * 1091541Srgrimes * INSTANTIATING VNODE STACKS 1101541Srgrimes * 1111541Srgrimes * Mounting associates the null layer with a lower layer, 1121541Srgrimes * effect stacking two VFSes. Vnode stacks are instead 1131541Srgrimes * created on demand as files are accessed. 1141541Srgrimes * 1151541Srgrimes * The initial mount creates a single vnode stack for the 1161541Srgrimes * root of the new null layer. All other vnode stacks 1171541Srgrimes * are created as a result of vnode operations on 1181541Srgrimes * this or other null vnode stacks. 1191541Srgrimes * 1201541Srgrimes * New vnode stacks come into existance as a result of 1218876Srgrimes * an operation which returns a vnode. 1221541Srgrimes * The bypass routine stacks a null-node above the new 1231541Srgrimes * vnode before returning it to the caller. 1241541Srgrimes * 1251541Srgrimes * For example, imagine mounting a null layer with 12677130Sru * "mount_nullfs /usr/include /dev/layer/null". 1271541Srgrimes * Changing directory to /dev/layer/null will assign 1281541Srgrimes * the root null-node (which was created when the null layer was mounted). 1291541Srgrimes * Now consider opening "sys". A vop_lookup would be 1301541Srgrimes * done on the root null-node. This operation would bypass through 1318876Srgrimes * to the lower layer which would return a vnode representing 1321541Srgrimes * the UFS "sys". Null_bypass then builds a null-node 1331541Srgrimes * aliasing the UFS "sys" and returns this to the caller. 1341541Srgrimes * Later operations on the null-node "sys" will repeat this 1351541Srgrimes * process when constructing other vnode stacks. 1361541Srgrimes * 1371541Srgrimes * 1381541Srgrimes * CREATING OTHER FILE SYSTEM LAYERS 1391541Srgrimes * 14096755Strhodes * One of the easiest ways to construct new filesystem layers is to make 1411541Srgrimes * a copy of the null layer, rename all files and variables, and 1421541Srgrimes * then begin modifing the copy. Sed can be used to easily rename 1431541Srgrimes * all variables. 1441541Srgrimes * 1458876Srgrimes * The umap layer is an example of a layer descended from the 1461541Srgrimes * null layer. 1471541Srgrimes * 1481541Srgrimes * 1491541Srgrimes * INVOKING OPERATIONS ON LOWER LAYERS 1501541Srgrimes * 1518876Srgrimes * There are two techniques to invoke operations on a lower layer 1521541Srgrimes * when the operation cannot be completely bypassed. Each method 1531541Srgrimes * is appropriate in different situations. In both cases, 1541541Srgrimes * it is the responsibility of the aliasing layer to make 1551541Srgrimes * the operation arguments "correct" for the lower layer 156108470Sschweikh * by mapping a vnode arguments to the lower layer. 1571541Srgrimes * 1581541Srgrimes * The first approach is to call the aliasing layer's bypass routine. 1591541Srgrimes * This method is most suitable when you wish to invoke the operation 16026964Salex * currently being handled on the lower layer. It has the advantage 1611541Srgrimes * that the bypass routine already must do argument mapping. 1621541Srgrimes * An example of this is null_getattrs in the null layer. 1631541Srgrimes * 16426964Salex * A second approach is to directly invoke vnode operations on 1651541Srgrimes * the lower layer with the VOP_OPERATIONNAME interface. 1661541Srgrimes * The advantage of this method is that it is easy to invoke 1671541Srgrimes * arbitrary operations on the lower layer. The disadvantage 16826964Salex * is that vnode arguments must be manualy mapped. 1691541Srgrimes * 1701541Srgrimes */ 1711541Srgrimes 1721541Srgrimes#include <sys/param.h> 1731541Srgrimes#include <sys/systm.h> 17476166Smarkm#include <sys/conf.h> 1752960Swollman#include <sys/kernel.h> 17676166Smarkm#include <sys/lock.h> 17776166Smarkm#include <sys/malloc.h> 17876166Smarkm#include <sys/mount.h> 17976166Smarkm#include <sys/mutex.h> 18076166Smarkm#include <sys/namei.h> 18112769Sphk#include <sys/sysctl.h> 1821541Srgrimes#include <sys/vnode.h> 18376166Smarkm 18477031Sru#include <fs/nullfs/null.h> 1851541Srgrimes 18666356Sbp#include <vm/vm.h> 18766356Sbp#include <vm/vm_extern.h> 18866356Sbp#include <vm/vm_object.h> 18966356Sbp#include <vm/vnode_pager.h> 19066356Sbp 19112769Sphkstatic int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */ 19212769SphkSYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW, 19312769Sphk &null_bug_bypass, 0, ""); 1941541Srgrimes 1951541Srgrimes/* 1961541Srgrimes * This is the 10-Apr-92 bypass routine. 1971541Srgrimes * This version has been optimized for speed, throwing away some 1981541Srgrimes * safety checks. It should still always work, but it's not as 1991541Srgrimes * robust to programmer errors. 2001541Srgrimes * 2011541Srgrimes * In general, we map all vnodes going down and unmap them on the way back. 2021541Srgrimes * As an exception to this, vnodes can be marked "unmapped" by setting 2031541Srgrimes * the Nth bit in operation's vdesc_flags. 2041541Srgrimes * 2051541Srgrimes * Also, some BSD vnode operations have the side effect of vrele'ing 2061541Srgrimes * their arguments. With stacking, the reference counts are held 2071541Srgrimes * by the upper node, not the lower one, so we must handle these 2081541Srgrimes * side-effects here. This is not of concern in Sun-derived systems 2091541Srgrimes * since there are no such side-effects. 2101541Srgrimes * 2111541Srgrimes * This makes the following assumptions: 2121541Srgrimes * - only one returned vpp 2131541Srgrimes * - no INOUT vpp's (Sun's vop_open has one of these) 2141541Srgrimes * - the vnode operation vector of the first vnode should be used 2151541Srgrimes * to determine what implementation of the op should be invoked 2161541Srgrimes * - all mapped vnodes are of our vnode-type (NEEDSWORK: 2171541Srgrimes * problems on rmdir'ing mount points and renaming?) 2188876Srgrimes */ 21922521Sdysonint 220140728Sphknull_bypass(struct vop_generic_args *ap) 2211541Srgrimes{ 222140732Sphk struct vnode **this_vp_p; 2231541Srgrimes int error; 2241541Srgrimes struct vnode *old_vps[VDESC_MAX_VPS]; 2251541Srgrimes struct vnode **vps_p[VDESC_MAX_VPS]; 2261541Srgrimes struct vnode ***vppp; 2271541Srgrimes struct vnodeop_desc *descp = ap->a_desc; 2281541Srgrimes int reles, i; 2291541Srgrimes 2301541Srgrimes if (null_bug_bypass) 2311541Srgrimes printf ("null_bypass: %s\n", descp->vdesc_name); 2321541Srgrimes 23350616Sbde#ifdef DIAGNOSTIC 2341541Srgrimes /* 2351541Srgrimes * We require at least one vp. 2361541Srgrimes */ 2371541Srgrimes if (descp->vdesc_vp_offsets == NULL || 2381541Srgrimes descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET) 23950616Sbde panic ("null_bypass: no vp's in map"); 2401541Srgrimes#endif 2411541Srgrimes 2421541Srgrimes /* 2431541Srgrimes * Map the vnodes going in. 2441541Srgrimes * Later, we'll invoke the operation based on 2451541Srgrimes * the first mapped vnode's operation vector. 2461541Srgrimes */ 2471541Srgrimes reles = descp->vdesc_flags; 2481541Srgrimes for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 2491541Srgrimes if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 2501541Srgrimes break; /* bail out at end of list */ 2518876Srgrimes vps_p[i] = this_vp_p = 2521541Srgrimes VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[i],ap); 2531541Srgrimes /* 2541541Srgrimes * We're not guaranteed that any but the first vnode 2551541Srgrimes * are of our type. Check for and don't map any 2561541Srgrimes * that aren't. (We must always map first vp or vclean fails.) 2571541Srgrimes */ 25824987Skato if (i && (*this_vp_p == NULLVP || 259138290Sphk (*this_vp_p)->v_op != &null_vnodeops)) { 26024987Skato old_vps[i] = NULLVP; 2611541Srgrimes } else { 2621541Srgrimes old_vps[i] = *this_vp_p; 2631541Srgrimes *(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p); 2641541Srgrimes /* 2651541Srgrimes * XXX - Several operations have the side effect 2661541Srgrimes * of vrele'ing their vp's. We must account for 2671541Srgrimes * that. (This should go away in the future.) 2681541Srgrimes */ 26966356Sbp if (reles & VDESC_VP0_WILLRELE) 2701541Srgrimes VREF(*this_vp_p); 2711541Srgrimes } 2728876Srgrimes 2731541Srgrimes } 2741541Srgrimes 2751541Srgrimes /* 2761541Srgrimes * Call the operation on the lower layer 2771541Srgrimes * with the modified argument structure. 2781541Srgrimes */ 27966356Sbp if (vps_p[0] && *vps_p[0]) 280140165Sphk error = VCALL(ap); 28166356Sbp else { 28266356Sbp printf("null_bypass: no map for %s\n", descp->vdesc_name); 28366356Sbp error = EINVAL; 28466356Sbp } 2851541Srgrimes 2861541Srgrimes /* 2871541Srgrimes * Maintain the illusion of call-by-value 2881541Srgrimes * by restoring vnodes in the argument structure 2891541Srgrimes * to their original value. 2901541Srgrimes */ 2911541Srgrimes reles = descp->vdesc_flags; 2921541Srgrimes for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 2931541Srgrimes if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 2941541Srgrimes break; /* bail out at end of list */ 2951541Srgrimes if (old_vps[i]) { 2961541Srgrimes *(vps_p[i]) = old_vps[i]; 29766356Sbp#if 0 29866356Sbp if (reles & VDESC_VP0_WILLUNLOCK) 299175294Sattilio VOP_UNLOCK(*(vps_p[i]), 0); 30066356Sbp#endif 30166356Sbp if (reles & VDESC_VP0_WILLRELE) 3021541Srgrimes vrele(*(vps_p[i])); 3031541Srgrimes } 3041541Srgrimes } 3051541Srgrimes 3061541Srgrimes /* 3071541Srgrimes * Map the possible out-going vpp 3081541Srgrimes * (Assumes that the lower layer always returns 3091541Srgrimes * a VREF'ed vpp unless it gets an error.) 3101541Srgrimes */ 3111541Srgrimes if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && 3121541Srgrimes !(descp->vdesc_flags & VDESC_NOMAP_VPP) && 3131541Srgrimes !error) { 3141541Srgrimes /* 3151541Srgrimes * XXX - even though some ops have vpp returned vp's, 3161541Srgrimes * several ops actually vrele this before returning. 3171541Srgrimes * We must avoid these ops. 3181541Srgrimes * (This should go away when these ops are regularized.) 3191541Srgrimes */ 3201541Srgrimes if (descp->vdesc_flags & VDESC_VPP_WILLRELE) 3211541Srgrimes goto out; 3221541Srgrimes vppp = VOPARG_OFFSETTO(struct vnode***, 3231541Srgrimes descp->vdesc_vpp_offset,ap); 32429584Sphk if (*vppp) 32598183Ssemenu error = null_nodeget(old_vps[0]->v_mount, **vppp, *vppp); 3261541Srgrimes } 3271541Srgrimes 3281541Srgrimes out: 3291541Srgrimes return (error); 3301541Srgrimes} 3311541Srgrimes 332244660Skibstatic int 333244660Skibnull_add_writecount(struct vop_add_writecount_args *ap) 334244660Skib{ 335244660Skib struct vnode *lvp, *vp; 336244660Skib int error; 337244660Skib 338244660Skib vp = ap->a_vp; 339244660Skib lvp = NULLVPTOLOWERVP(vp); 340244660Skib KASSERT(vp->v_writecount + ap->a_inc >= 0, ("wrong writecount inc")); 341244660Skib if (vp->v_writecount > 0 && vp->v_writecount + ap->a_inc == 0) 342244660Skib error = VOP_ADD_WRITECOUNT(lvp, -1); 343244660Skib else if (vp->v_writecount == 0 && vp->v_writecount + ap->a_inc > 0) 344244660Skib error = VOP_ADD_WRITECOUNT(lvp, 1); 345244660Skib else 346244660Skib error = 0; 347244660Skib if (error == 0) 348244660Skib vp->v_writecount += ap->a_inc; 349244660Skib return (error); 350244660Skib} 351244660Skib 35222521Sdyson/* 35322521Sdyson * We have to carry on the locking protocol on the null layer vnodes 35422521Sdyson * as we progress through the tree. We also have to enforce read-only 35522521Sdyson * if this layer is mounted read-only. 35622521Sdyson */ 35722521Sdysonstatic int 358140728Sphknull_lookup(struct vop_lookup_args *ap) 35922521Sdyson{ 36022521Sdyson struct componentname *cnp = ap->a_cnp; 36166356Sbp struct vnode *dvp = ap->a_dvp; 36222521Sdyson int flags = cnp->cn_flags; 36366356Sbp struct vnode *vp, *ldvp, *lvp; 36422521Sdyson int error; 3651541Srgrimes 36666356Sbp if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 36722521Sdyson (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 36822521Sdyson return (EROFS); 36966356Sbp /* 37066356Sbp * Although it is possible to call null_bypass(), we'll do 37166356Sbp * a direct call to reduce overhead 37266356Sbp */ 37366356Sbp ldvp = NULLVPTOLOWERVP(dvp); 37466356Sbp vp = lvp = NULL; 37566356Sbp error = VOP_LOOKUP(ldvp, &lvp, cnp); 37622521Sdyson if (error == EJUSTRETURN && (flags & ISLASTCN) && 37766356Sbp (dvp->v_mount->mnt_flag & MNT_RDONLY) && 37822521Sdyson (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 37922521Sdyson error = EROFS; 38066356Sbp 38166356Sbp if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) { 38266356Sbp if (ldvp == lvp) { 38366356Sbp *ap->a_vpp = dvp; 38466356Sbp VREF(dvp); 38566356Sbp vrele(lvp); 38666356Sbp } else { 38798183Ssemenu error = null_nodeget(dvp->v_mount, lvp, &vp); 388229932Skib if (error == 0) 389185335Skib *ap->a_vpp = vp; 39066356Sbp } 39122521Sdyson } 39222521Sdyson return (error); 39322521Sdyson} 39422521Sdyson 395140776Sphkstatic int 396140776Sphknull_open(struct vop_open_args *ap) 397140776Sphk{ 398140776Sphk int retval; 399140776Sphk struct vnode *vp, *ldvp; 400140776Sphk 401140776Sphk vp = ap->a_vp; 402140776Sphk ldvp = NULLVPTOLOWERVP(vp); 403140776Sphk retval = null_bypass(&ap->a_gen); 404140776Sphk if (retval == 0) 405140776Sphk vp->v_object = ldvp->v_object; 406140776Sphk return (retval); 407140776Sphk} 408140776Sphk 4091541Srgrimes/* 41022521Sdyson * Setattr call. Disallow write attempts if the layer is mounted read-only. 41122521Sdyson */ 412105211Sphkstatic int 413140728Sphknull_setattr(struct vop_setattr_args *ap) 41422521Sdyson{ 41522521Sdyson struct vnode *vp = ap->a_vp; 41622521Sdyson struct vattr *vap = ap->a_vap; 41722521Sdyson 41822521Sdyson if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 41922597Smpp vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 42022597Smpp vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 42122521Sdyson (vp->v_mount->mnt_flag & MNT_RDONLY)) 42222521Sdyson return (EROFS); 42322521Sdyson if (vap->va_size != VNOVAL) { 42422521Sdyson switch (vp->v_type) { 42522521Sdyson case VDIR: 42622521Sdyson return (EISDIR); 42722521Sdyson case VCHR: 42822521Sdyson case VBLK: 42922521Sdyson case VSOCK: 43022521Sdyson case VFIFO: 43136840Speter if (vap->va_flags != VNOVAL) 43236840Speter return (EOPNOTSUPP); 43322521Sdyson return (0); 43422521Sdyson case VREG: 43522521Sdyson case VLNK: 43622521Sdyson default: 43722521Sdyson /* 43822521Sdyson * Disallow write attempts if the filesystem is 43922521Sdyson * mounted read-only. 44022521Sdyson */ 44122521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 44222521Sdyson return (EROFS); 44322521Sdyson } 44422521Sdyson } 44566356Sbp 44622607Smpp return (null_bypass((struct vop_generic_args *)ap)); 44722521Sdyson} 44822521Sdyson 44922521Sdyson/* 4501541Srgrimes * We handle getattr only to change the fsid. 4511541Srgrimes */ 45212769Sphkstatic int 453140728Sphknull_getattr(struct vop_getattr_args *ap) 4541541Srgrimes{ 4551541Srgrimes int error; 45622521Sdyson 45743311Sdillon if ((error = null_bypass((struct vop_generic_args *)ap)) != 0) 4581541Srgrimes return (error); 45965467Sbp 46065467Sbp ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 4611541Srgrimes return (0); 4621541Srgrimes} 4631541Srgrimes 46466356Sbp/* 46566356Sbp * Handle to disallow write access if mounted read-only. 46666356Sbp */ 46722521Sdysonstatic int 468140728Sphknull_access(struct vop_access_args *ap) 46922521Sdyson{ 47022521Sdyson struct vnode *vp = ap->a_vp; 471184413Strasz accmode_t accmode = ap->a_accmode; 4721541Srgrimes 47322521Sdyson /* 47422521Sdyson * Disallow write attempts on read-only layers; 47522521Sdyson * unless the file is a socket, fifo, or a block or 47696755Strhodes * character device resident on the filesystem. 47722521Sdyson */ 478184413Strasz if (accmode & VWRITE) { 47922521Sdyson switch (vp->v_type) { 48022521Sdyson case VDIR: 48122521Sdyson case VLNK: 48222521Sdyson case VREG: 48322521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 48422521Sdyson return (EROFS); 48522521Sdyson break; 48643305Sdillon default: 48743305Sdillon break; 48822521Sdyson } 48922521Sdyson } 49022607Smpp return (null_bypass((struct vop_generic_args *)ap)); 49122521Sdyson} 49222521Sdyson 493193092Straszstatic int 494193092Strasznull_accessx(struct vop_accessx_args *ap) 495193092Strasz{ 496193092Strasz struct vnode *vp = ap->a_vp; 497193092Strasz accmode_t accmode = ap->a_accmode; 498193092Strasz 499193092Strasz /* 500193092Strasz * Disallow write attempts on read-only layers; 501193092Strasz * unless the file is a socket, fifo, or a block or 502193092Strasz * character device resident on the filesystem. 503193092Strasz */ 504193092Strasz if (accmode & VWRITE) { 505193092Strasz switch (vp->v_type) { 506193092Strasz case VDIR: 507193092Strasz case VLNK: 508193092Strasz case VREG: 509193092Strasz if (vp->v_mount->mnt_flag & MNT_RDONLY) 510193092Strasz return (EROFS); 511193092Strasz break; 512193092Strasz default: 513193092Strasz break; 514193092Strasz } 515193092Strasz } 516193092Strasz return (null_bypass((struct vop_generic_args *)ap)); 517193092Strasz} 518193092Strasz 51922521Sdyson/* 520212043Srmacklem * Increasing refcount of lower vnode is needed at least for the case 521212043Srmacklem * when lower FS is NFS to do sillyrename if the file is in use. 522212043Srmacklem * Unfortunately v_usecount is incremented in many places in 523212043Srmacklem * the kernel and, as such, there may be races that result in 524212043Srmacklem * the NFS client doing an extraneous silly rename, but that seems 525212043Srmacklem * preferable to not doing a silly rename when it is needed. 526212043Srmacklem */ 527212043Srmacklemstatic int 528212043Srmacklemnull_remove(struct vop_remove_args *ap) 529212043Srmacklem{ 530212043Srmacklem int retval, vreleit; 531212043Srmacklem struct vnode *lvp; 532212043Srmacklem 533212043Srmacklem if (vrefcnt(ap->a_vp) > 1) { 534212043Srmacklem lvp = NULLVPTOLOWERVP(ap->a_vp); 535212043Srmacklem VREF(lvp); 536212043Srmacklem vreleit = 1; 537212043Srmacklem } else 538212043Srmacklem vreleit = 0; 539212043Srmacklem retval = null_bypass(&ap->a_gen); 540212043Srmacklem if (vreleit != 0) 541212043Srmacklem vrele(lvp); 542212043Srmacklem return (retval); 543212043Srmacklem} 544212043Srmacklem 545212043Srmacklem/* 54665467Sbp * We handle this to eliminate null FS to lower FS 54765467Sbp * file moving. Don't know why we don't allow this, 54865467Sbp * possibly we should. 54965467Sbp */ 55065467Sbpstatic int 551140728Sphknull_rename(struct vop_rename_args *ap) 55265467Sbp{ 55365467Sbp struct vnode *tdvp = ap->a_tdvp; 55465467Sbp struct vnode *fvp = ap->a_fvp; 55565467Sbp struct vnode *fdvp = ap->a_fdvp; 55665467Sbp struct vnode *tvp = ap->a_tvp; 557253183Skib struct null_node *tnn; 55865467Sbp 55965467Sbp /* Check for cross-device rename. */ 56065467Sbp if ((fvp->v_mount != tdvp->v_mount) || 56165467Sbp (tvp && (fvp->v_mount != tvp->v_mount))) { 56265467Sbp if (tdvp == tvp) 56365467Sbp vrele(tdvp); 56465467Sbp else 56565467Sbp vput(tdvp); 56665467Sbp if (tvp) 56765467Sbp vput(tvp); 56865467Sbp vrele(fdvp); 56965467Sbp vrele(fvp); 57065467Sbp return (EXDEV); 57165467Sbp } 572253183Skib 573253183Skib if (tvp != NULL) { 574253183Skib tnn = VTONULL(tvp); 575253183Skib tnn->null_flags |= NULLV_DROP; 576253183Skib } 57765467Sbp return (null_bypass((struct vop_generic_args *)ap)); 57865467Sbp} 57965467Sbp 58065467Sbp/* 58122521Sdyson * We need to process our own vnode lock and then clear the 58222521Sdyson * interlock flag as it applies only to our vnode, not the 58322521Sdyson * vnodes below us on the stack. 58422521Sdyson */ 58522597Smppstatic int 586169671Skibnull_lock(struct vop_lock1_args *ap) 58722521Sdyson{ 58866356Sbp struct vnode *vp = ap->a_vp; 58966356Sbp int flags = ap->a_flags; 590143642Sjeff struct null_node *nn; 59166356Sbp struct vnode *lvp; 59266356Sbp int error; 59322521Sdyson 59466356Sbp 595143513Sjeff if ((flags & LK_INTERLOCK) == 0) { 596143513Sjeff VI_LOCK(vp); 597143642Sjeff ap->a_flags = flags |= LK_INTERLOCK; 598143513Sjeff } 599143642Sjeff nn = VTONULL(vp); 600143642Sjeff /* 601143642Sjeff * If we're still active we must ask the lower layer to 602143642Sjeff * lock as ffs has special lock considerations in it's 603143642Sjeff * vop lock. 604143642Sjeff */ 605143642Sjeff if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) { 606145424Sjeff VI_LOCK_FLAGS(lvp, MTX_DUPOK); 607116469Stjr VI_UNLOCK(vp); 60866356Sbp /* 609143642Sjeff * We have to hold the vnode here to solve a potential 610143642Sjeff * reclaim race. If we're forcibly vgone'd while we 611143642Sjeff * still have refs, a thread could be sleeping inside 612143642Sjeff * the lowervp's vop_lock routine. When we vgone we will 613143642Sjeff * drop our last ref to the lowervp, which would allow it 614143642Sjeff * to be reclaimed. The lowervp could then be recycled, 615143642Sjeff * in which case it is not legal to be sleeping in it's VOP. 616143642Sjeff * We prevent it from being recycled by holding the vnode 617143642Sjeff * here. 61866356Sbp */ 619143642Sjeff vholdl(lvp); 620175294Sattilio error = VOP_LOCK(lvp, flags); 621150181Skan 622150181Skan /* 623150181Skan * We might have slept to get the lock and someone might have 624150181Skan * clean our vnode already, switching vnode lock from one in 625150181Skan * lowervp to v_lock in our own vnode structure. Handle this 626150181Skan * case by reacquiring correct lock in requested mode. 627150181Skan */ 628150181Skan if (VTONULL(vp) == NULL && error == 0) { 629150181Skan ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK); 630150181Skan switch (flags & LK_TYPE_MASK) { 631150181Skan case LK_SHARED: 632150181Skan ap->a_flags |= LK_SHARED; 633150181Skan break; 634150181Skan case LK_UPGRADE: 635150181Skan case LK_EXCLUSIVE: 636150181Skan ap->a_flags |= LK_EXCLUSIVE; 637150181Skan break; 638150181Skan default: 639150181Skan panic("Unsupported lock request %d\n", 640150181Skan ap->a_flags); 641150181Skan } 642175294Sattilio VOP_UNLOCK(lvp, 0); 643150181Skan error = vop_stdlock(ap); 644150181Skan } 645143642Sjeff vdrop(lvp); 646143642Sjeff } else 647143642Sjeff error = vop_stdlock(ap); 648143642Sjeff 649143642Sjeff return (error); 65022521Sdyson} 65122521Sdyson 65222521Sdyson/* 65322521Sdyson * We need to process our own vnode unlock and then clear the 65422521Sdyson * interlock flag as it applies only to our vnode, not the 65522521Sdyson * vnodes below us on the stack. 65622521Sdyson */ 65722597Smppstatic int 658140728Sphknull_unlock(struct vop_unlock_args *ap) 65922521Sdyson{ 66066356Sbp struct vnode *vp = ap->a_vp; 66166356Sbp int flags = ap->a_flags; 662172644Sdaichi int mtxlkflag = 0; 663143642Sjeff struct null_node *nn; 66466570Sbp struct vnode *lvp; 665143642Sjeff int error; 66666356Sbp 667172644Sdaichi if ((flags & LK_INTERLOCK) != 0) 668172644Sdaichi mtxlkflag = 1; 669172644Sdaichi else if (mtx_owned(VI_MTX(vp)) == 0) { 670172644Sdaichi VI_LOCK(vp); 671172644Sdaichi mtxlkflag = 2; 67266356Sbp } 673143642Sjeff nn = VTONULL(vp); 674172644Sdaichi if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) { 675172644Sdaichi VI_LOCK_FLAGS(lvp, MTX_DUPOK); 676172644Sdaichi flags |= LK_INTERLOCK; 677172644Sdaichi vholdl(lvp); 678172644Sdaichi VI_UNLOCK(vp); 679175294Sattilio error = VOP_UNLOCK(lvp, flags); 680172644Sdaichi vdrop(lvp); 681172644Sdaichi if (mtxlkflag == 0) 682172644Sdaichi VI_LOCK(vp); 683172644Sdaichi } else { 684172644Sdaichi if (mtxlkflag == 2) 685172644Sdaichi VI_UNLOCK(vp); 686143642Sjeff error = vop_stdunlock(ap); 687172644Sdaichi } 688143642Sjeff 689143642Sjeff return (error); 69022521Sdyson} 69122521Sdyson 69266356Sbp/* 693244654Skib * Do not allow the VOP_INACTIVE to be passed to the lower layer, 694244654Skib * since the reference count on the lower vnode is not related to 695244654Skib * ours. 69666356Sbp */ 69766356Sbpstatic int 698244654Skibnull_inactive(struct vop_inactive_args *ap __unused) 6991541Srgrimes{ 700250978Skib struct vnode *vp, *lvp; 701250978Skib struct null_node *xp; 702245353Skib struct mount *mp; 703245353Skib struct null_mount *xmp; 70492540Smckusick 705245353Skib vp = ap->a_vp; 706250978Skib xp = VTONULL(vp); 707250978Skib lvp = NULLVPTOLOWERVP(vp); 708245353Skib mp = vp->v_mount; 709245353Skib xmp = MOUNTTONULLMOUNT(mp); 710250978Skib if ((xmp->nullm_flags & NULLM_CACHE) == 0 || 711250978Skib (xp->null_flags & NULLV_DROP) != 0 || 712250978Skib (lvp->v_vflag & VV_NOSYNC) != 0) { 713245353Skib /* 714245353Skib * If this is the last reference and caching of the 715250978Skib * nullfs vnodes is not enabled, or the lower vnode is 716250978Skib * deleted, then free up the vnode so as not to tie up 717250978Skib * the lower vnodes. 718245353Skib */ 719245353Skib vp->v_object = NULL; 720245353Skib vrecycle(vp, curthread); 721245353Skib } 72292540Smckusick return (0); 72392540Smckusick} 72492540Smckusick 72592540Smckusick/* 726244654Skib * Now, the nullfs vnode and, due to the sharing lock, the lower 727244654Skib * vnode, are exclusively locked, and we shall destroy the null vnode. 72892540Smckusick */ 72992540Smckusickstatic int 730140728Sphknull_reclaim(struct vop_reclaim_args *ap) 73192540Smckusick{ 732232645Skib struct vnode *vp; 733232645Skib struct null_node *xp; 734232645Skib struct vnode *lowervp; 73566356Sbp 736232645Skib vp = ap->a_vp; 737232645Skib xp = VTONULL(vp); 738232645Skib lowervp = xp->null_lowervp; 739232645Skib 740232645Skib KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock, 741232645Skib ("Reclaiming inclomplete null vnode %p", vp)); 742232645Skib 743232645Skib null_hashrem(xp); 744143744Sjeff /* 745143744Sjeff * Use the interlock to protect the clearing of v_data to 746143744Sjeff * prevent faults in null_lock(). 747143744Sjeff */ 748193172Skib lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL); 749143744Sjeff VI_LOCK(vp); 750143744Sjeff vp->v_data = NULL; 751155899Sjeff vp->v_object = NULL; 752150181Skan vp->v_vnlock = &vp->v_lock; 753193172Skib VI_UNLOCK(vp); 754245352Skib 755245352Skib /* 756245352Skib * If we were opened for write, we leased one write reference 757245352Skib * to the lower vnode. If this is a reclamation due to the 758245352Skib * forced unmount, undo the reference now. 759245352Skib */ 760245352Skib if (vp->v_writecount > 0) 761245352Skib VOP_ADD_WRITECOUNT(lowervp, -1); 762250978Skib if ((xp->null_flags & NULLV_NOUNLOCK) != 0) 763250978Skib vunref(lowervp); 764250978Skib else 765250978Skib vput(lowervp); 766184205Sdes free(xp, M_NULLFSNODE); 76766356Sbp 7681541Srgrimes return (0); 7691541Srgrimes} 7701541Srgrimes 77112769Sphkstatic int 772140728Sphknull_print(struct vop_print_args *ap) 7731541Srgrimes{ 774140732Sphk struct vnode *vp = ap->a_vp; 775155899Sjeff 776229230Skib printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp); 7771541Srgrimes return (0); 7781541Srgrimes} 7791541Srgrimes 780156585Sjeff/* ARGSUSED */ 781156585Sjeffstatic int 782156585Sjeffnull_getwritemount(struct vop_getwritemount_args *ap) 783156585Sjeff{ 784156585Sjeff struct null_node *xp; 785156585Sjeff struct vnode *lowervp; 786156585Sjeff struct vnode *vp; 787156585Sjeff 788156585Sjeff vp = ap->a_vp; 789156585Sjeff VI_LOCK(vp); 790156585Sjeff xp = VTONULL(vp); 791156585Sjeff if (xp && (lowervp = xp->null_lowervp)) { 792156585Sjeff VI_LOCK_FLAGS(lowervp, MTX_DUPOK); 793156585Sjeff VI_UNLOCK(vp); 794156585Sjeff vholdl(lowervp); 795156585Sjeff VI_UNLOCK(lowervp); 796156585Sjeff VOP_GETWRITEMOUNT(lowervp, ap->a_mpp); 797156585Sjeff vdrop(lowervp); 798156585Sjeff } else { 799156585Sjeff VI_UNLOCK(vp); 800156585Sjeff *(ap->a_mpp) = NULL; 801156585Sjeff } 802156585Sjeff return (0); 803156585Sjeff} 804156585Sjeff 805166774Spjdstatic int 806166774Spjdnull_vptofh(struct vop_vptofh_args *ap) 807166774Spjd{ 808166774Spjd struct vnode *lvp; 809166774Spjd 810166774Spjd lvp = NULLVPTOLOWERVP(ap->a_vp); 811166774Spjd return VOP_VPTOFH(lvp, ap->a_fhp); 812166774Spjd} 813166774Spjd 814193175Skibstatic int 815193175Skibnull_vptocnp(struct vop_vptocnp_args *ap) 816193175Skib{ 817193175Skib struct vnode *vp = ap->a_vp; 818193175Skib struct vnode **dvp = ap->a_vpp; 819193175Skib struct vnode *lvp, *ldvp; 820194601Skib struct ucred *cred = ap->a_cred; 821193175Skib int error, locked; 822193175Skib 823193175Skib if (vp->v_type == VDIR) 824193175Skib return (vop_stdvptocnp(ap)); 825193175Skib 826193175Skib locked = VOP_ISLOCKED(vp); 827193175Skib lvp = NULLVPTOLOWERVP(vp); 828193175Skib vhold(lvp); 829193175Skib VOP_UNLOCK(vp, 0); /* vp is held by vn_vptocnp_locked that called us */ 830193175Skib ldvp = lvp; 831229703Skib vref(lvp); 832194601Skib error = vn_vptocnp(&ldvp, cred, ap->a_buf, ap->a_buflen); 833193175Skib vdrop(lvp); 834193175Skib if (error != 0) { 835193175Skib vn_lock(vp, locked | LK_RETRY); 836193175Skib return (ENOENT); 837193175Skib } 838193175Skib 839193175Skib /* 840193175Skib * Exclusive lock is required by insmntque1 call in 841193175Skib * null_nodeget() 842193175Skib */ 843193175Skib error = vn_lock(ldvp, LK_EXCLUSIVE); 844193175Skib if (error != 0) { 845229703Skib vrele(ldvp); 846193175Skib vn_lock(vp, locked | LK_RETRY); 847193175Skib return (ENOENT); 848193175Skib } 849193175Skib vref(ldvp); 850193175Skib error = null_nodeget(vp->v_mount, ldvp, dvp); 851193175Skib if (error == 0) { 852193175Skib#ifdef DIAGNOSTIC 853193175Skib NULLVPTOLOWERVP(*dvp); 854193175Skib#endif 855229703Skib VOP_UNLOCK(*dvp, 0); /* keep reference on *dvp */ 856229932Skib } 857193175Skib vn_lock(vp, locked | LK_RETRY); 858193175Skib return (error); 859193175Skib} 860193175Skib 861255443Sdesstatic int 862255443Sdesnull_link(struct vop_link_args *ap) 863255443Sdes{ 864255443Sdes 865255443Sdes if (ap->a_tdvp->v_mount != ap->a_vp->v_mount) 866255443Sdes return (EXDEV); 867255443Sdes return (null_bypass((struct vop_generic_args *)ap)); 868255443Sdes} 869255443Sdes 8701541Srgrimes/* 8711541Srgrimes * Global vfs data structures 8721541Srgrimes */ 873138290Sphkstruct vop_vector null_vnodeops = { 874138290Sphk .vop_bypass = null_bypass, 875138290Sphk .vop_access = null_access, 876193092Strasz .vop_accessx = null_accessx, 877208128Skib .vop_advlockpurge = vop_stdadvlockpurge, 878138290Sphk .vop_bmap = VOP_EOPNOTSUPP, 879138290Sphk .vop_getattr = null_getattr, 880156585Sjeff .vop_getwritemount = null_getwritemount, 881138290Sphk .vop_inactive = null_inactive, 882189961Spho .vop_islocked = vop_stdislocked, 883255443Sdes .vop_link = null_link, 884169671Skib .vop_lock1 = null_lock, 885138290Sphk .vop_lookup = null_lookup, 886140776Sphk .vop_open = null_open, 887138290Sphk .vop_print = null_print, 888138290Sphk .vop_reclaim = null_reclaim, 889212043Srmacklem .vop_remove = null_remove, 890138290Sphk .vop_rename = null_rename, 891138290Sphk .vop_setattr = null_setattr, 892138290Sphk .vop_strategy = VOP_EOPNOTSUPP, 893138290Sphk .vop_unlock = null_unlock, 894193175Skib .vop_vptocnp = null_vptocnp, 895166774Spjd .vop_vptofh = null_vptofh, 896244660Skib .vop_add_writecount = null_add_writecount, 8971541Srgrimes}; 898