null_vnops.c revision 182943
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: head/sys/fs/nullfs/null_vnops.c 182943 2008-09-11 20:15:34Z ed $ 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 33222521Sdyson/* 33322521Sdyson * We have to carry on the locking protocol on the null layer vnodes 33422521Sdyson * as we progress through the tree. We also have to enforce read-only 33522521Sdyson * if this layer is mounted read-only. 33622521Sdyson */ 33722521Sdysonstatic int 338140728Sphknull_lookup(struct vop_lookup_args *ap) 33922521Sdyson{ 34022521Sdyson struct componentname *cnp = ap->a_cnp; 34166356Sbp struct vnode *dvp = ap->a_dvp; 34222521Sdyson int flags = cnp->cn_flags; 34366356Sbp struct vnode *vp, *ldvp, *lvp; 34422521Sdyson int error; 3451541Srgrimes 34666356Sbp if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 34722521Sdyson (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 34822521Sdyson return (EROFS); 34966356Sbp /* 35066356Sbp * Although it is possible to call null_bypass(), we'll do 35166356Sbp * a direct call to reduce overhead 35266356Sbp */ 35366356Sbp ldvp = NULLVPTOLOWERVP(dvp); 35466356Sbp vp = lvp = NULL; 35566356Sbp error = VOP_LOOKUP(ldvp, &lvp, cnp); 35622521Sdyson if (error == EJUSTRETURN && (flags & ISLASTCN) && 35766356Sbp (dvp->v_mount->mnt_flag & MNT_RDONLY) && 35822521Sdyson (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 35922521Sdyson error = EROFS; 36066356Sbp 36166356Sbp if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) { 36266356Sbp if (ldvp == lvp) { 36366356Sbp *ap->a_vpp = dvp; 36466356Sbp VREF(dvp); 36566356Sbp vrele(lvp); 36666356Sbp } else { 36798183Ssemenu error = null_nodeget(dvp->v_mount, lvp, &vp); 36898183Ssemenu if (error) { 36998183Ssemenu /* XXX Cleanup needed... */ 37098183Ssemenu panic("null_nodeget failed"); 37198183Ssemenu } 37298183Ssemenu *ap->a_vpp = vp; 37366356Sbp } 37422521Sdyson } 37522521Sdyson return (error); 37622521Sdyson} 37722521Sdyson 378140776Sphkstatic int 379140776Sphknull_open(struct vop_open_args *ap) 380140776Sphk{ 381140776Sphk int retval; 382140776Sphk struct vnode *vp, *ldvp; 383140776Sphk 384140776Sphk vp = ap->a_vp; 385140776Sphk ldvp = NULLVPTOLOWERVP(vp); 386140776Sphk retval = null_bypass(&ap->a_gen); 387140776Sphk if (retval == 0) 388140776Sphk vp->v_object = ldvp->v_object; 389140776Sphk return (retval); 390140776Sphk} 391140776Sphk 3921541Srgrimes/* 39322521Sdyson * Setattr call. Disallow write attempts if the layer is mounted read-only. 39422521Sdyson */ 395105211Sphkstatic int 396140728Sphknull_setattr(struct vop_setattr_args *ap) 39722521Sdyson{ 39822521Sdyson struct vnode *vp = ap->a_vp; 39922521Sdyson struct vattr *vap = ap->a_vap; 40022521Sdyson 40122521Sdyson if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 40222597Smpp vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 40322597Smpp vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 40422521Sdyson (vp->v_mount->mnt_flag & MNT_RDONLY)) 40522521Sdyson return (EROFS); 40622521Sdyson if (vap->va_size != VNOVAL) { 40722521Sdyson switch (vp->v_type) { 40822521Sdyson case VDIR: 40922521Sdyson return (EISDIR); 41022521Sdyson case VCHR: 41122521Sdyson case VBLK: 41222521Sdyson case VSOCK: 41322521Sdyson case VFIFO: 41436840Speter if (vap->va_flags != VNOVAL) 41536840Speter return (EOPNOTSUPP); 41622521Sdyson return (0); 41722521Sdyson case VREG: 41822521Sdyson case VLNK: 41922521Sdyson default: 42022521Sdyson /* 42122521Sdyson * Disallow write attempts if the filesystem is 42222521Sdyson * mounted read-only. 42322521Sdyson */ 42422521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 42522521Sdyson return (EROFS); 42622521Sdyson } 42722521Sdyson } 42866356Sbp 42922607Smpp return (null_bypass((struct vop_generic_args *)ap)); 43022521Sdyson} 43122521Sdyson 43222521Sdyson/* 4331541Srgrimes * We handle getattr only to change the fsid. 4341541Srgrimes */ 43512769Sphkstatic int 436140728Sphknull_getattr(struct vop_getattr_args *ap) 4371541Srgrimes{ 4381541Srgrimes int error; 43922521Sdyson 44043311Sdillon if ((error = null_bypass((struct vop_generic_args *)ap)) != 0) 4411541Srgrimes return (error); 44265467Sbp 44365467Sbp ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 4441541Srgrimes return (0); 4451541Srgrimes} 4461541Srgrimes 44766356Sbp/* 44866356Sbp * Handle to disallow write access if mounted read-only. 44966356Sbp */ 45022521Sdysonstatic int 451140728Sphknull_access(struct vop_access_args *ap) 45222521Sdyson{ 45322521Sdyson struct vnode *vp = ap->a_vp; 45422521Sdyson mode_t mode = ap->a_mode; 4551541Srgrimes 45622521Sdyson /* 45722521Sdyson * Disallow write attempts on read-only layers; 45822521Sdyson * unless the file is a socket, fifo, or a block or 45996755Strhodes * character device resident on the filesystem. 46022521Sdyson */ 46122521Sdyson if (mode & VWRITE) { 46222521Sdyson switch (vp->v_type) { 46322521Sdyson case VDIR: 46422521Sdyson case VLNK: 46522521Sdyson case VREG: 46622521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 46722521Sdyson return (EROFS); 46822521Sdyson break; 46943305Sdillon default: 47043305Sdillon break; 47122521Sdyson } 47222521Sdyson } 47322607Smpp return (null_bypass((struct vop_generic_args *)ap)); 47422521Sdyson} 47522521Sdyson 47622521Sdyson/* 47765467Sbp * We handle this to eliminate null FS to lower FS 47865467Sbp * file moving. Don't know why we don't allow this, 47965467Sbp * possibly we should. 48065467Sbp */ 48165467Sbpstatic int 482140728Sphknull_rename(struct vop_rename_args *ap) 48365467Sbp{ 48465467Sbp struct vnode *tdvp = ap->a_tdvp; 48565467Sbp struct vnode *fvp = ap->a_fvp; 48665467Sbp struct vnode *fdvp = ap->a_fdvp; 48765467Sbp struct vnode *tvp = ap->a_tvp; 48865467Sbp 48965467Sbp /* Check for cross-device rename. */ 49065467Sbp if ((fvp->v_mount != tdvp->v_mount) || 49165467Sbp (tvp && (fvp->v_mount != tvp->v_mount))) { 49265467Sbp if (tdvp == tvp) 49365467Sbp vrele(tdvp); 49465467Sbp else 49565467Sbp vput(tdvp); 49665467Sbp if (tvp) 49765467Sbp vput(tvp); 49865467Sbp vrele(fdvp); 49965467Sbp vrele(fvp); 50065467Sbp return (EXDEV); 50165467Sbp } 50265467Sbp 50365467Sbp return (null_bypass((struct vop_generic_args *)ap)); 50465467Sbp} 50565467Sbp 50665467Sbp/* 50722521Sdyson * We need to process our own vnode lock and then clear the 50822521Sdyson * interlock flag as it applies only to our vnode, not the 50922521Sdyson * vnodes below us on the stack. 51022521Sdyson */ 51122597Smppstatic int 512169671Skibnull_lock(struct vop_lock1_args *ap) 51322521Sdyson{ 51466356Sbp struct vnode *vp = ap->a_vp; 51566356Sbp int flags = ap->a_flags; 516143642Sjeff struct null_node *nn; 51766356Sbp struct vnode *lvp; 51866356Sbp int error; 51922521Sdyson 52066356Sbp 521143513Sjeff if ((flags & LK_INTERLOCK) == 0) { 522143513Sjeff VI_LOCK(vp); 523143642Sjeff ap->a_flags = flags |= LK_INTERLOCK; 524143513Sjeff } 525143642Sjeff nn = VTONULL(vp); 526143642Sjeff /* 527143642Sjeff * If we're still active we must ask the lower layer to 528143642Sjeff * lock as ffs has special lock considerations in it's 529143642Sjeff * vop lock. 530143642Sjeff */ 531143642Sjeff if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) { 532145424Sjeff VI_LOCK_FLAGS(lvp, MTX_DUPOK); 533116469Stjr VI_UNLOCK(vp); 53466356Sbp /* 535143642Sjeff * We have to hold the vnode here to solve a potential 536143642Sjeff * reclaim race. If we're forcibly vgone'd while we 537143642Sjeff * still have refs, a thread could be sleeping inside 538143642Sjeff * the lowervp's vop_lock routine. When we vgone we will 539143642Sjeff * drop our last ref to the lowervp, which would allow it 540143642Sjeff * to be reclaimed. The lowervp could then be recycled, 541143642Sjeff * in which case it is not legal to be sleeping in it's VOP. 542143642Sjeff * We prevent it from being recycled by holding the vnode 543143642Sjeff * here. 54466356Sbp */ 545143642Sjeff vholdl(lvp); 546175294Sattilio error = VOP_LOCK(lvp, flags); 547150181Skan 548150181Skan /* 549150181Skan * We might have slept to get the lock and someone might have 550150181Skan * clean our vnode already, switching vnode lock from one in 551150181Skan * lowervp to v_lock in our own vnode structure. Handle this 552150181Skan * case by reacquiring correct lock in requested mode. 553150181Skan */ 554150181Skan if (VTONULL(vp) == NULL && error == 0) { 555150181Skan ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK); 556150181Skan switch (flags & LK_TYPE_MASK) { 557150181Skan case LK_SHARED: 558150181Skan ap->a_flags |= LK_SHARED; 559150181Skan break; 560150181Skan case LK_UPGRADE: 561150181Skan case LK_EXCLUSIVE: 562150181Skan ap->a_flags |= LK_EXCLUSIVE; 563150181Skan break; 564150181Skan default: 565150181Skan panic("Unsupported lock request %d\n", 566150181Skan ap->a_flags); 567150181Skan } 568175294Sattilio VOP_UNLOCK(lvp, 0); 569150181Skan error = vop_stdlock(ap); 570150181Skan } 571143642Sjeff vdrop(lvp); 572143642Sjeff } else 573143642Sjeff error = vop_stdlock(ap); 574143642Sjeff 575143642Sjeff return (error); 57622521Sdyson} 57722521Sdyson 57822521Sdyson/* 57922521Sdyson * We need to process our own vnode unlock and then clear the 58022521Sdyson * interlock flag as it applies only to our vnode, not the 58122521Sdyson * vnodes below us on the stack. 58222521Sdyson */ 58322597Smppstatic int 584140728Sphknull_unlock(struct vop_unlock_args *ap) 58522521Sdyson{ 58666356Sbp struct vnode *vp = ap->a_vp; 58766356Sbp int flags = ap->a_flags; 588172644Sdaichi int mtxlkflag = 0; 589143642Sjeff struct null_node *nn; 59066570Sbp struct vnode *lvp; 591143642Sjeff int error; 59266356Sbp 593172644Sdaichi if ((flags & LK_INTERLOCK) != 0) 594172644Sdaichi mtxlkflag = 1; 595172644Sdaichi else if (mtx_owned(VI_MTX(vp)) == 0) { 596172644Sdaichi VI_LOCK(vp); 597172644Sdaichi mtxlkflag = 2; 59866356Sbp } 599143642Sjeff nn = VTONULL(vp); 600172644Sdaichi if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) { 601172644Sdaichi VI_LOCK_FLAGS(lvp, MTX_DUPOK); 602172644Sdaichi flags |= LK_INTERLOCK; 603172644Sdaichi vholdl(lvp); 604172644Sdaichi VI_UNLOCK(vp); 605175294Sattilio error = VOP_UNLOCK(lvp, flags); 606172644Sdaichi vdrop(lvp); 607172644Sdaichi if (mtxlkflag == 0) 608172644Sdaichi VI_LOCK(vp); 609172644Sdaichi } else { 610172644Sdaichi if (mtxlkflag == 2) 611172644Sdaichi VI_UNLOCK(vp); 612143642Sjeff error = vop_stdunlock(ap); 613172644Sdaichi } 614143642Sjeff 615143642Sjeff return (error); 61622521Sdyson} 61722521Sdyson 61822597Smppstatic int 619140728Sphknull_islocked(struct vop_islocked_args *ap) 62066356Sbp{ 62166356Sbp struct vnode *vp = ap->a_vp; 62266356Sbp 623176559Sattilio return (lockstatus(vp->v_vnlock)); 62466356Sbp} 62566356Sbp 62666356Sbp/* 62766356Sbp * There is no way to tell that someone issued remove/rmdir operation 628182943Sed * on the underlying filesystem. For now we just have to release lowervp 62966356Sbp * as soon as possible. 63098183Ssemenu * 63198183Ssemenu * Note, we can't release any resources nor remove vnode from hash before 63298183Ssemenu * appropriate VXLOCK stuff is is done because other process can find this 63398183Ssemenu * vnode in hash during inactivation and may be sitting in vget() and waiting 63498183Ssemenu * for null_inactive to unlock vnode. Thus we will do all those in VOP_RECLAIM. 63566356Sbp */ 63666356Sbpstatic int 637140728Sphknull_inactive(struct vop_inactive_args *ap) 6381541Srgrimes{ 63930636Sroberto struct vnode *vp = ap->a_vp; 64098175Ssemenu struct thread *td = ap->a_td; 64192540Smckusick 642141447Sphk vp->v_object = NULL; 64398175Ssemenu 64492540Smckusick /* 64592540Smckusick * If this is the last reference, then free up the vnode 64692540Smckusick * so as not to tie up the lower vnodes. 64792540Smckusick */ 648140936Sphk vrecycle(vp, td); 64998175Ssemenu 65092540Smckusick return (0); 65192540Smckusick} 65292540Smckusick 65392540Smckusick/* 65498183Ssemenu * Now, the VXLOCK is in force and we're free to destroy the null vnode. 65592540Smckusick */ 65692540Smckusickstatic int 657140728Sphknull_reclaim(struct vop_reclaim_args *ap) 65892540Smckusick{ 65992540Smckusick struct vnode *vp = ap->a_vp; 66030636Sroberto struct null_node *xp = VTONULL(vp); 66130636Sroberto struct vnode *lowervp = xp->null_lowervp; 662143630Sjeff struct lock *vnlock; 66366356Sbp 664155899Sjeff if (lowervp) 665155899Sjeff null_hashrem(xp); 666143744Sjeff /* 667143744Sjeff * Use the interlock to protect the clearing of v_data to 668143744Sjeff * prevent faults in null_lock(). 669143744Sjeff */ 670143744Sjeff VI_LOCK(vp); 671143744Sjeff vp->v_data = NULL; 672155899Sjeff vp->v_object = NULL; 673150181Skan vnlock = vp->v_vnlock; 674150181Skan vp->v_vnlock = &vp->v_lock; 675149722Sssouhlal if (lowervp) { 676175635Sattilio lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_INTERLOCK, VI_MTX(vp)); 677149722Sssouhlal vput(lowervp); 678149722Sssouhlal } else 679182943Sed panic("null_reclaim: reclaiming a node with no lowervp"); 68098176Ssemenu FREE(xp, M_NULLFSNODE); 68166356Sbp 6821541Srgrimes return (0); 6831541Srgrimes} 6841541Srgrimes 68512769Sphkstatic int 686140728Sphknull_print(struct vop_print_args *ap) 6871541Srgrimes{ 688140732Sphk struct vnode *vp = ap->a_vp; 689155899Sjeff 690111841Snjl printf("\tvp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp)); 6911541Srgrimes return (0); 6921541Srgrimes} 6931541Srgrimes 694156585Sjeff/* ARGSUSED */ 695156585Sjeffstatic int 696156585Sjeffnull_getwritemount(struct vop_getwritemount_args *ap) 697156585Sjeff{ 698156585Sjeff struct null_node *xp; 699156585Sjeff struct vnode *lowervp; 700156585Sjeff struct vnode *vp; 701156585Sjeff 702156585Sjeff vp = ap->a_vp; 703156585Sjeff VI_LOCK(vp); 704156585Sjeff xp = VTONULL(vp); 705156585Sjeff if (xp && (lowervp = xp->null_lowervp)) { 706156585Sjeff VI_LOCK_FLAGS(lowervp, MTX_DUPOK); 707156585Sjeff VI_UNLOCK(vp); 708156585Sjeff vholdl(lowervp); 709156585Sjeff VI_UNLOCK(lowervp); 710156585Sjeff VOP_GETWRITEMOUNT(lowervp, ap->a_mpp); 711156585Sjeff vdrop(lowervp); 712156585Sjeff } else { 713156585Sjeff VI_UNLOCK(vp); 714156585Sjeff *(ap->a_mpp) = NULL; 715156585Sjeff } 716156585Sjeff return (0); 717156585Sjeff} 718156585Sjeff 719166774Spjdstatic int 720166774Spjdnull_vptofh(struct vop_vptofh_args *ap) 721166774Spjd{ 722166774Spjd struct vnode *lvp; 723166774Spjd 724166774Spjd lvp = NULLVPTOLOWERVP(ap->a_vp); 725166774Spjd return VOP_VPTOFH(lvp, ap->a_fhp); 726166774Spjd} 727166774Spjd 7281541Srgrimes/* 7291541Srgrimes * Global vfs data structures 7301541Srgrimes */ 731138290Sphkstruct vop_vector null_vnodeops = { 732138290Sphk .vop_bypass = null_bypass, 733138290Sphk .vop_access = null_access, 734138290Sphk .vop_bmap = VOP_EOPNOTSUPP, 735138290Sphk .vop_getattr = null_getattr, 736156585Sjeff .vop_getwritemount = null_getwritemount, 737138290Sphk .vop_inactive = null_inactive, 738138290Sphk .vop_islocked = null_islocked, 739169671Skib .vop_lock1 = null_lock, 740138290Sphk .vop_lookup = null_lookup, 741140776Sphk .vop_open = null_open, 742138290Sphk .vop_print = null_print, 743138290Sphk .vop_reclaim = null_reclaim, 744138290Sphk .vop_rename = null_rename, 745138290Sphk .vop_setattr = null_setattr, 746138290Sphk .vop_strategy = VOP_EOPNOTSUPP, 747138290Sphk .vop_unlock = null_unlock, 748166774Spjd .vop_vptofh = null_vptofh, 7491541Srgrimes}; 750