null_vnops.c revision 193092
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 193092 2009-05-30 13:59:05Z trasz $ 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); 368185335Skib if (error) 369185335Skib vput(lvp); 370185335Skib else 371185335Skib *ap->a_vpp = vp; 37266356Sbp } 37322521Sdyson } 37422521Sdyson return (error); 37522521Sdyson} 37622521Sdyson 377140776Sphkstatic int 378140776Sphknull_open(struct vop_open_args *ap) 379140776Sphk{ 380140776Sphk int retval; 381140776Sphk struct vnode *vp, *ldvp; 382140776Sphk 383140776Sphk vp = ap->a_vp; 384140776Sphk ldvp = NULLVPTOLOWERVP(vp); 385140776Sphk retval = null_bypass(&ap->a_gen); 386140776Sphk if (retval == 0) 387140776Sphk vp->v_object = ldvp->v_object; 388140776Sphk return (retval); 389140776Sphk} 390140776Sphk 3911541Srgrimes/* 39222521Sdyson * Setattr call. Disallow write attempts if the layer is mounted read-only. 39322521Sdyson */ 394105211Sphkstatic int 395140728Sphknull_setattr(struct vop_setattr_args *ap) 39622521Sdyson{ 39722521Sdyson struct vnode *vp = ap->a_vp; 39822521Sdyson struct vattr *vap = ap->a_vap; 39922521Sdyson 40022521Sdyson if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL || 40122597Smpp vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 40222597Smpp vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 40322521Sdyson (vp->v_mount->mnt_flag & MNT_RDONLY)) 40422521Sdyson return (EROFS); 40522521Sdyson if (vap->va_size != VNOVAL) { 40622521Sdyson switch (vp->v_type) { 40722521Sdyson case VDIR: 40822521Sdyson return (EISDIR); 40922521Sdyson case VCHR: 41022521Sdyson case VBLK: 41122521Sdyson case VSOCK: 41222521Sdyson case VFIFO: 41336840Speter if (vap->va_flags != VNOVAL) 41436840Speter return (EOPNOTSUPP); 41522521Sdyson return (0); 41622521Sdyson case VREG: 41722521Sdyson case VLNK: 41822521Sdyson default: 41922521Sdyson /* 42022521Sdyson * Disallow write attempts if the filesystem is 42122521Sdyson * mounted read-only. 42222521Sdyson */ 42322521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 42422521Sdyson return (EROFS); 42522521Sdyson } 42622521Sdyson } 42766356Sbp 42822607Smpp return (null_bypass((struct vop_generic_args *)ap)); 42922521Sdyson} 43022521Sdyson 43122521Sdyson/* 4321541Srgrimes * We handle getattr only to change the fsid. 4331541Srgrimes */ 43412769Sphkstatic int 435140728Sphknull_getattr(struct vop_getattr_args *ap) 4361541Srgrimes{ 4371541Srgrimes int error; 43822521Sdyson 43943311Sdillon if ((error = null_bypass((struct vop_generic_args *)ap)) != 0) 4401541Srgrimes return (error); 44165467Sbp 44265467Sbp ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 4431541Srgrimes return (0); 4441541Srgrimes} 4451541Srgrimes 44666356Sbp/* 44766356Sbp * Handle to disallow write access if mounted read-only. 44866356Sbp */ 44922521Sdysonstatic int 450140728Sphknull_access(struct vop_access_args *ap) 45122521Sdyson{ 45222521Sdyson struct vnode *vp = ap->a_vp; 453184413Strasz accmode_t accmode = ap->a_accmode; 4541541Srgrimes 45522521Sdyson /* 45622521Sdyson * Disallow write attempts on read-only layers; 45722521Sdyson * unless the file is a socket, fifo, or a block or 45896755Strhodes * character device resident on the filesystem. 45922521Sdyson */ 460184413Strasz if (accmode & VWRITE) { 46122521Sdyson switch (vp->v_type) { 46222521Sdyson case VDIR: 46322521Sdyson case VLNK: 46422521Sdyson case VREG: 46522521Sdyson if (vp->v_mount->mnt_flag & MNT_RDONLY) 46622521Sdyson return (EROFS); 46722521Sdyson break; 46843305Sdillon default: 46943305Sdillon break; 47022521Sdyson } 47122521Sdyson } 47222607Smpp return (null_bypass((struct vop_generic_args *)ap)); 47322521Sdyson} 47422521Sdyson 475193092Straszstatic int 476193092Strasznull_accessx(struct vop_accessx_args *ap) 477193092Strasz{ 478193092Strasz struct vnode *vp = ap->a_vp; 479193092Strasz accmode_t accmode = ap->a_accmode; 480193092Strasz 481193092Strasz /* 482193092Strasz * Disallow write attempts on read-only layers; 483193092Strasz * unless the file is a socket, fifo, or a block or 484193092Strasz * character device resident on the filesystem. 485193092Strasz */ 486193092Strasz if (accmode & VWRITE) { 487193092Strasz switch (vp->v_type) { 488193092Strasz case VDIR: 489193092Strasz case VLNK: 490193092Strasz case VREG: 491193092Strasz if (vp->v_mount->mnt_flag & MNT_RDONLY) 492193092Strasz return (EROFS); 493193092Strasz break; 494193092Strasz default: 495193092Strasz break; 496193092Strasz } 497193092Strasz } 498193092Strasz return (null_bypass((struct vop_generic_args *)ap)); 499193092Strasz} 500193092Strasz 50122521Sdyson/* 50265467Sbp * We handle this to eliminate null FS to lower FS 50365467Sbp * file moving. Don't know why we don't allow this, 50465467Sbp * possibly we should. 50565467Sbp */ 50665467Sbpstatic int 507140728Sphknull_rename(struct vop_rename_args *ap) 50865467Sbp{ 50965467Sbp struct vnode *tdvp = ap->a_tdvp; 51065467Sbp struct vnode *fvp = ap->a_fvp; 51165467Sbp struct vnode *fdvp = ap->a_fdvp; 51265467Sbp struct vnode *tvp = ap->a_tvp; 51365467Sbp 51465467Sbp /* Check for cross-device rename. */ 51565467Sbp if ((fvp->v_mount != tdvp->v_mount) || 51665467Sbp (tvp && (fvp->v_mount != tvp->v_mount))) { 51765467Sbp if (tdvp == tvp) 51865467Sbp vrele(tdvp); 51965467Sbp else 52065467Sbp vput(tdvp); 52165467Sbp if (tvp) 52265467Sbp vput(tvp); 52365467Sbp vrele(fdvp); 52465467Sbp vrele(fvp); 52565467Sbp return (EXDEV); 52665467Sbp } 52765467Sbp 52865467Sbp return (null_bypass((struct vop_generic_args *)ap)); 52965467Sbp} 53065467Sbp 53165467Sbp/* 53222521Sdyson * We need to process our own vnode lock and then clear the 53322521Sdyson * interlock flag as it applies only to our vnode, not the 53422521Sdyson * vnodes below us on the stack. 53522521Sdyson */ 53622597Smppstatic int 537169671Skibnull_lock(struct vop_lock1_args *ap) 53822521Sdyson{ 53966356Sbp struct vnode *vp = ap->a_vp; 54066356Sbp int flags = ap->a_flags; 541143642Sjeff struct null_node *nn; 54266356Sbp struct vnode *lvp; 54366356Sbp int error; 54422521Sdyson 54566356Sbp 546143513Sjeff if ((flags & LK_INTERLOCK) == 0) { 547143513Sjeff VI_LOCK(vp); 548143642Sjeff ap->a_flags = flags |= LK_INTERLOCK; 549143513Sjeff } 550143642Sjeff nn = VTONULL(vp); 551143642Sjeff /* 552143642Sjeff * If we're still active we must ask the lower layer to 553143642Sjeff * lock as ffs has special lock considerations in it's 554143642Sjeff * vop lock. 555143642Sjeff */ 556143642Sjeff if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) { 557145424Sjeff VI_LOCK_FLAGS(lvp, MTX_DUPOK); 558116469Stjr VI_UNLOCK(vp); 55966356Sbp /* 560143642Sjeff * We have to hold the vnode here to solve a potential 561143642Sjeff * reclaim race. If we're forcibly vgone'd while we 562143642Sjeff * still have refs, a thread could be sleeping inside 563143642Sjeff * the lowervp's vop_lock routine. When we vgone we will 564143642Sjeff * drop our last ref to the lowervp, which would allow it 565143642Sjeff * to be reclaimed. The lowervp could then be recycled, 566143642Sjeff * in which case it is not legal to be sleeping in it's VOP. 567143642Sjeff * We prevent it from being recycled by holding the vnode 568143642Sjeff * here. 56966356Sbp */ 570143642Sjeff vholdl(lvp); 571175294Sattilio error = VOP_LOCK(lvp, flags); 572150181Skan 573150181Skan /* 574150181Skan * We might have slept to get the lock and someone might have 575150181Skan * clean our vnode already, switching vnode lock from one in 576150181Skan * lowervp to v_lock in our own vnode structure. Handle this 577150181Skan * case by reacquiring correct lock in requested mode. 578150181Skan */ 579150181Skan if (VTONULL(vp) == NULL && error == 0) { 580150181Skan ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK); 581150181Skan switch (flags & LK_TYPE_MASK) { 582150181Skan case LK_SHARED: 583150181Skan ap->a_flags |= LK_SHARED; 584150181Skan break; 585150181Skan case LK_UPGRADE: 586150181Skan case LK_EXCLUSIVE: 587150181Skan ap->a_flags |= LK_EXCLUSIVE; 588150181Skan break; 589150181Skan default: 590150181Skan panic("Unsupported lock request %d\n", 591150181Skan ap->a_flags); 592150181Skan } 593175294Sattilio VOP_UNLOCK(lvp, 0); 594150181Skan error = vop_stdlock(ap); 595150181Skan } 596143642Sjeff vdrop(lvp); 597143642Sjeff } else 598143642Sjeff error = vop_stdlock(ap); 599143642Sjeff 600143642Sjeff return (error); 60122521Sdyson} 60222521Sdyson 60322521Sdyson/* 60422521Sdyson * We need to process our own vnode unlock and then clear the 60522521Sdyson * interlock flag as it applies only to our vnode, not the 60622521Sdyson * vnodes below us on the stack. 60722521Sdyson */ 60822597Smppstatic int 609140728Sphknull_unlock(struct vop_unlock_args *ap) 61022521Sdyson{ 61166356Sbp struct vnode *vp = ap->a_vp; 61266356Sbp int flags = ap->a_flags; 613172644Sdaichi int mtxlkflag = 0; 614143642Sjeff struct null_node *nn; 61566570Sbp struct vnode *lvp; 616143642Sjeff int error; 61766356Sbp 618172644Sdaichi if ((flags & LK_INTERLOCK) != 0) 619172644Sdaichi mtxlkflag = 1; 620172644Sdaichi else if (mtx_owned(VI_MTX(vp)) == 0) { 621172644Sdaichi VI_LOCK(vp); 622172644Sdaichi mtxlkflag = 2; 62366356Sbp } 624143642Sjeff nn = VTONULL(vp); 625172644Sdaichi if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) { 626172644Sdaichi VI_LOCK_FLAGS(lvp, MTX_DUPOK); 627172644Sdaichi flags |= LK_INTERLOCK; 628172644Sdaichi vholdl(lvp); 629172644Sdaichi VI_UNLOCK(vp); 630175294Sattilio error = VOP_UNLOCK(lvp, flags); 631172644Sdaichi vdrop(lvp); 632172644Sdaichi if (mtxlkflag == 0) 633172644Sdaichi VI_LOCK(vp); 634172644Sdaichi } else { 635172644Sdaichi if (mtxlkflag == 2) 636172644Sdaichi VI_UNLOCK(vp); 637143642Sjeff error = vop_stdunlock(ap); 638172644Sdaichi } 639143642Sjeff 640143642Sjeff return (error); 64122521Sdyson} 64222521Sdyson 64366356Sbp/* 64466356Sbp * There is no way to tell that someone issued remove/rmdir operation 645182943Sed * on the underlying filesystem. For now we just have to release lowervp 64666356Sbp * as soon as possible. 64798183Ssemenu * 64898183Ssemenu * Note, we can't release any resources nor remove vnode from hash before 64998183Ssemenu * appropriate VXLOCK stuff is is done because other process can find this 65098183Ssemenu * vnode in hash during inactivation and may be sitting in vget() and waiting 65198183Ssemenu * for null_inactive to unlock vnode. Thus we will do all those in VOP_RECLAIM. 65266356Sbp */ 65366356Sbpstatic int 654140728Sphknull_inactive(struct vop_inactive_args *ap) 6551541Srgrimes{ 65630636Sroberto struct vnode *vp = ap->a_vp; 65798175Ssemenu struct thread *td = ap->a_td; 65892540Smckusick 659141447Sphk vp->v_object = NULL; 66098175Ssemenu 66192540Smckusick /* 66292540Smckusick * If this is the last reference, then free up the vnode 66392540Smckusick * so as not to tie up the lower vnodes. 66492540Smckusick */ 665140936Sphk vrecycle(vp, td); 66698175Ssemenu 66792540Smckusick return (0); 66892540Smckusick} 66992540Smckusick 67092540Smckusick/* 67198183Ssemenu * Now, the VXLOCK is in force and we're free to destroy the null vnode. 67292540Smckusick */ 67392540Smckusickstatic int 674140728Sphknull_reclaim(struct vop_reclaim_args *ap) 67592540Smckusick{ 67692540Smckusick struct vnode *vp = ap->a_vp; 67730636Sroberto struct null_node *xp = VTONULL(vp); 67830636Sroberto struct vnode *lowervp = xp->null_lowervp; 67966356Sbp 680155899Sjeff if (lowervp) 681155899Sjeff null_hashrem(xp); 682143744Sjeff /* 683143744Sjeff * Use the interlock to protect the clearing of v_data to 684143744Sjeff * prevent faults in null_lock(). 685143744Sjeff */ 686143744Sjeff VI_LOCK(vp); 687143744Sjeff vp->v_data = NULL; 688155899Sjeff vp->v_object = NULL; 689150181Skan vp->v_vnlock = &vp->v_lock; 690149722Sssouhlal if (lowervp) { 691175635Sattilio lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_INTERLOCK, VI_MTX(vp)); 692149722Sssouhlal vput(lowervp); 693149722Sssouhlal } else 694182943Sed panic("null_reclaim: reclaiming a node with no lowervp"); 695184205Sdes free(xp, M_NULLFSNODE); 69666356Sbp 6971541Srgrimes return (0); 6981541Srgrimes} 6991541Srgrimes 70012769Sphkstatic int 701140728Sphknull_print(struct vop_print_args *ap) 7021541Srgrimes{ 703140732Sphk struct vnode *vp = ap->a_vp; 704155899Sjeff 705111841Snjl printf("\tvp=%p, lowervp=%p\n", vp, NULLVPTOLOWERVP(vp)); 7061541Srgrimes return (0); 7071541Srgrimes} 7081541Srgrimes 709156585Sjeff/* ARGSUSED */ 710156585Sjeffstatic int 711156585Sjeffnull_getwritemount(struct vop_getwritemount_args *ap) 712156585Sjeff{ 713156585Sjeff struct null_node *xp; 714156585Sjeff struct vnode *lowervp; 715156585Sjeff struct vnode *vp; 716156585Sjeff 717156585Sjeff vp = ap->a_vp; 718156585Sjeff VI_LOCK(vp); 719156585Sjeff xp = VTONULL(vp); 720156585Sjeff if (xp && (lowervp = xp->null_lowervp)) { 721156585Sjeff VI_LOCK_FLAGS(lowervp, MTX_DUPOK); 722156585Sjeff VI_UNLOCK(vp); 723156585Sjeff vholdl(lowervp); 724156585Sjeff VI_UNLOCK(lowervp); 725156585Sjeff VOP_GETWRITEMOUNT(lowervp, ap->a_mpp); 726156585Sjeff vdrop(lowervp); 727156585Sjeff } else { 728156585Sjeff VI_UNLOCK(vp); 729156585Sjeff *(ap->a_mpp) = NULL; 730156585Sjeff } 731156585Sjeff return (0); 732156585Sjeff} 733156585Sjeff 734166774Spjdstatic int 735166774Spjdnull_vptofh(struct vop_vptofh_args *ap) 736166774Spjd{ 737166774Spjd struct vnode *lvp; 738166774Spjd 739166774Spjd lvp = NULLVPTOLOWERVP(ap->a_vp); 740166774Spjd return VOP_VPTOFH(lvp, ap->a_fhp); 741166774Spjd} 742166774Spjd 7431541Srgrimes/* 7441541Srgrimes * Global vfs data structures 7451541Srgrimes */ 746138290Sphkstruct vop_vector null_vnodeops = { 747138290Sphk .vop_bypass = null_bypass, 748138290Sphk .vop_access = null_access, 749193092Strasz .vop_accessx = null_accessx, 750138290Sphk .vop_bmap = VOP_EOPNOTSUPP, 751138290Sphk .vop_getattr = null_getattr, 752156585Sjeff .vop_getwritemount = null_getwritemount, 753138290Sphk .vop_inactive = null_inactive, 754189961Spho .vop_islocked = vop_stdislocked, 755169671Skib .vop_lock1 = null_lock, 756138290Sphk .vop_lookup = null_lookup, 757140776Sphk .vop_open = null_open, 758138290Sphk .vop_print = null_print, 759138290Sphk .vop_reclaim = null_reclaim, 760138290Sphk .vop_rename = null_rename, 761138290Sphk .vop_setattr = null_setattr, 762138290Sphk .vop_strategy = VOP_EOPNOTSUPP, 763138290Sphk .vop_unlock = null_unlock, 764189622Skib .vop_vptocnp = vop_stdvptocnp, 765166774Spjd .vop_vptofh = null_vptofh, 7661541Srgrimes}; 767