/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1992, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* John Heidemann of the UCLA Ficus project.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33* Ancestors:34* ...and...35*/3637/*38* Null Layer39*40* (See mount_nullfs(8) for more information.)41*42* The null layer duplicates a portion of the filesystem43* name space under a new name. In this respect, it is44* similar to the loopback filesystem. It differs from45* the loopback fs in two respects: it is implemented using46* a stackable layers techniques, and its "null-node"s stack above47* all lower-layer vnodes, not just over directory vnodes.48*49* The null layer has two purposes. First, it serves as a demonstration50* of layering by proving a layer which does nothing. (It actually51* does everything the loopback filesystem does, which is slightly52* more than nothing.) Second, the null layer can serve as a prototype53* layer. Since it provides all necessary layer framework,54* new filesystem layers can be created very easily be starting55* with a null layer.56*57* The remainder of this man page examines the null layer as a basis58* for constructing new layers.59*60*61* INSTANTIATING NEW NULL LAYERS62*63* New null layers are created with mount_nullfs(8).64* Mount_nullfs(8) takes two arguments, the pathname65* of the lower vfs (target-pn) and the pathname where the null66* layer will appear in the namespace (alias-pn). After67* the null layer is put into place, the contents68* of target-pn subtree will be aliased under alias-pn.69*70*71* OPERATION OF A NULL LAYER72*73* The null layer is the minimum filesystem layer,74* simply bypassing all possible operations to the lower layer75* for processing there. The majority of its activity centers76* on the bypass routine, through which nearly all vnode operations77* pass.78*79* The bypass routine accepts arbitrary vnode operations for80* handling by the lower layer. It begins by examining vnode81* operation arguments and replacing any null-nodes by their82* lower-layer equivlants. It then invokes the operation83* on the lower layer. Finally, it replaces the null-nodes84* in the arguments and, if a vnode is return by the operation,85* stacks a null-node on top of the returned vnode.86*87* Although bypass handles most operations, vop_getattr, vop_lock,88* vop_unlock, vop_inactive, vop_reclaim, and vop_print are not89* bypassed. Vop_getattr must change the fsid being returned.90* Vop_lock and vop_unlock must handle any locking for the91* current vnode as well as pass the lock request down.92* Vop_inactive and vop_reclaim are not bypassed so that93* they can handle freeing null-layer specific data. Vop_print94* is not bypassed to avoid excessive debugging information.95* Also, certain vnode operations change the locking state within96* the operation (create, mknod, remove, link, rename, mkdir, rmdir,97* and symlink). Ideally these operations should not change the98* lock state, but should be changed to let the caller of the99* function unlock them. Otherwise all intermediate vnode layers100* (such as union, umapfs, etc) must catch these functions to do101* the necessary locking at their layer.102*103*104* INSTANTIATING VNODE STACKS105*106* Mounting associates the null layer with a lower layer,107* effect stacking two VFSes. Vnode stacks are instead108* created on demand as files are accessed.109*110* The initial mount creates a single vnode stack for the111* root of the new null layer. All other vnode stacks112* are created as a result of vnode operations on113* this or other null vnode stacks.114*115* New vnode stacks come into existence as a result of116* an operation which returns a vnode.117* The bypass routine stacks a null-node above the new118* vnode before returning it to the caller.119*120* For example, imagine mounting a null layer with121* "mount_nullfs /usr/include /dev/layer/null".122* Changing directory to /dev/layer/null will assign123* the root null-node (which was created when the null layer was mounted).124* Now consider opening "sys". A vop_lookup would be125* done on the root null-node. This operation would bypass through126* to the lower layer which would return a vnode representing127* the UFS "sys". Null_bypass then builds a null-node128* aliasing the UFS "sys" and returns this to the caller.129* Later operations on the null-node "sys" will repeat this130* process when constructing other vnode stacks.131*132*133* CREATING OTHER FILE SYSTEM LAYERS134*135* One of the easiest ways to construct new filesystem layers is to make136* a copy of the null layer, rename all files and variables, and137* then begin modifing the copy. Sed can be used to easily rename138* all variables.139*140* The umap layer is an example of a layer descended from the141* null layer.142*143*144* INVOKING OPERATIONS ON LOWER LAYERS145*146* There are two techniques to invoke operations on a lower layer147* when the operation cannot be completely bypassed. Each method148* is appropriate in different situations. In both cases,149* it is the responsibility of the aliasing layer to make150* the operation arguments "correct" for the lower layer151* by mapping a vnode arguments to the lower layer.152*153* The first approach is to call the aliasing layer's bypass routine.154* This method is most suitable when you wish to invoke the operation155* currently being handled on the lower layer. It has the advantage156* that the bypass routine already must do argument mapping.157* An example of this is null_getattrs in the null layer.158*159* A second approach is to directly invoke vnode operations on160* the lower layer with the VOP_OPERATIONNAME interface.161* The advantage of this method is that it is easy to invoke162* arbitrary operations on the lower layer. The disadvantage163* is that vnode arguments must be manualy mapped.164*165*/166167#include <sys/param.h>168#include <sys/systm.h>169#include <sys/conf.h>170#include <sys/kernel.h>171#include <sys/lock.h>172#include <sys/malloc.h>173#include <sys/mount.h>174#include <sys/mutex.h>175#include <sys/namei.h>176#include <sys/proc.h>177#include <sys/smr.h>178#include <sys/sysctl.h>179#include <sys/vnode.h>180#include <sys/stat.h>181182#include <fs/nullfs/null.h>183184#include <vm/vm.h>185#include <vm/vm_extern.h>186#include <vm/vm_object.h>187#include <vm/vnode_pager.h>188189VFS_SMR_DECLARE;190191static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */192SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,193&null_bug_bypass, 0, "");194195/*196* Synchronize inotify flags with the lower vnode:197* - If the upper vnode has the flag set and the lower does not, then the lower198* vnode is unwatched and the upper vnode does not need to go through199* VOP_INOTIFY.200* - If the lower vnode is watched, then the upper vnode should go through201* VOP_INOTIFY, so copy the flag up.202*/203static void204null_copy_inotify(struct vnode *vp, struct vnode *lvp, short flag)205{206if ((vn_irflag_read(vp) & flag) != 0) {207if (__predict_false((vn_irflag_read(lvp) & flag) == 0))208vn_irflag_unset(vp, flag);209} else if ((vn_irflag_read(lvp) & flag) != 0) {210if (__predict_false((vn_irflag_read(vp) & flag) == 0))211vn_irflag_set(vp, flag);212}213}214215/*216* This is the 10-Apr-92 bypass routine.217* This version has been optimized for speed, throwing away some218* safety checks. It should still always work, but it's not as219* robust to programmer errors.220*221* In general, we map all vnodes going down and unmap them on the way back.222* As an exception to this, vnodes can be marked "unmapped" by setting223* the Nth bit in operation's vdesc_flags.224*225* Also, some BSD vnode operations have the side effect of vrele'ing226* their arguments. With stacking, the reference counts are held227* by the upper node, not the lower one, so we must handle these228* side-effects here. This is not of concern in Sun-derived systems229* since there are no such side-effects.230*231* This makes the following assumptions:232* - only one returned vpp233* - no INOUT vpp's (Sun's vop_open has one of these)234* - the vnode operation vector of the first vnode should be used235* to determine what implementation of the op should be invoked236* - all mapped vnodes are of our vnode-type (NEEDSWORK:237* problems on rmdir'ing mount points and renaming?)238*/239int240null_bypass(struct vop_generic_args *ap)241{242struct vnode **this_vp_p;243struct vnode *old_vps[VDESC_MAX_VPS];244struct vnode **vps_p[VDESC_MAX_VPS];245struct vnode ***vppp;246struct vnode *lvp;247struct vnodeop_desc *descp = ap->a_desc;248int error, i, reles;249250if (null_bug_bypass)251printf ("null_bypass: %s\n", descp->vdesc_name);252253#ifdef DIAGNOSTIC254/*255* We require at least one vp.256*/257if (descp->vdesc_vp_offsets == NULL ||258descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)259panic ("null_bypass: no vp's in map");260#endif261262/*263* Map the vnodes going in.264* Later, we'll invoke the operation based on265* the first mapped vnode's operation vector.266*/267reles = descp->vdesc_flags;268for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {269if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)270break; /* bail out at end of list */271vps_p[i] = this_vp_p = VOPARG_OFFSETTO(struct vnode **,272descp->vdesc_vp_offsets[i], ap);273274/*275* We're not guaranteed that any but the first vnode276* are of our type. Check for and don't map any277* that aren't. (We must always map first vp or vclean fails.)278*/279if (i != 0 && (*this_vp_p == NULL ||280(*this_vp_p)->v_op != &null_vnodeops)) {281old_vps[i] = NULL;282} else {283old_vps[i] = *this_vp_p;284*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);285286/*287* The upper vnode reference to the lower288* vnode is the only reference that keeps our289* pointer to the lower vnode alive. If lower290* vnode is relocked during the VOP call,291* upper vnode might become unlocked and292* reclaimed, which invalidates our reference.293* Add a transient hold around VOP call.294*/295vhold(*this_vp_p);296297/*298* XXX - Several operations have the side effect299* of vrele'ing their vp's. We must account for300* that. (This should go away in the future.)301*/302if (reles & VDESC_VP0_WILLRELE)303vref(*this_vp_p);304}305}306307/*308* Call the operation on the lower layer309* with the modified argument structure.310*/311if (vps_p[0] != NULL && *vps_p[0] != NULL) {312error = ap->a_desc->vdesc_call(ap);313} else {314printf("null_bypass: no map for %s\n", descp->vdesc_name);315error = EINVAL;316}317318/*319* Maintain the illusion of call-by-value320* by restoring vnodes in the argument structure321* to their original value.322*/323reles = descp->vdesc_flags;324for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {325if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)326break; /* bail out at end of list */327if (old_vps[i] != NULL) {328lvp = *(vps_p[i]);329330/*331* Get rid of the transient hold on lvp. Copy inotify332* flags up in case something is watching the lower333* layer.334*335* If lowervp was unlocked during VOP336* operation, nullfs upper vnode could have337* been reclaimed, which changes its v_vnlock338* back to private v_lock. In this case we339* must move lock ownership from lower to340* upper (reclaimed) vnode.341*/342if (lvp != NULL) {343null_copy_inotify(old_vps[i], lvp,344VIRF_INOTIFY);345null_copy_inotify(old_vps[i], lvp,346VIRF_INOTIFY_PARENT);347if (VOP_ISLOCKED(lvp) == LK_EXCLUSIVE &&348old_vps[i]->v_vnlock != lvp->v_vnlock) {349VOP_UNLOCK(lvp);350VOP_LOCK(old_vps[i], LK_EXCLUSIVE |351LK_RETRY);352}353vdrop(lvp);354}355356*(vps_p[i]) = old_vps[i];357#if 0358if (reles & VDESC_VP0_WILLUNLOCK)359VOP_UNLOCK(*(vps_p[i]), 0);360#endif361if (reles & VDESC_VP0_WILLRELE)362vrele(*(vps_p[i]));363}364}365366/*367* Map the possible out-going vpp368* (Assumes that the lower layer always returns369* a VREF'ed vpp unless it gets an error.)370*/371if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && error == 0) {372/*373* XXX - even though some ops have vpp returned vp's,374* several ops actually vrele this before returning.375* We must avoid these ops.376* (This should go away when these ops are regularized.)377*/378vppp = VOPARG_OFFSETTO(struct vnode ***,379descp->vdesc_vpp_offset, ap);380if (*vppp != NULL)381error = null_nodeget(old_vps[0]->v_mount, **vppp,382*vppp);383}384385return (error);386}387388static int389null_add_writecount(struct vop_add_writecount_args *ap)390{391struct vnode *lvp, *vp;392int error;393394vp = ap->a_vp;395lvp = NULLVPTOLOWERVP(vp);396VI_LOCK(vp);397/* text refs are bypassed to lowervp */398VNASSERT(vp->v_writecount >= 0, vp, ("wrong null writecount"));399VNASSERT(vp->v_writecount + ap->a_inc >= 0, vp,400("wrong writecount inc %d", ap->a_inc));401error = VOP_ADD_WRITECOUNT(lvp, ap->a_inc);402if (error == 0)403vp->v_writecount += ap->a_inc;404VI_UNLOCK(vp);405return (error);406}407408/*409* We have to carry on the locking protocol on the null layer vnodes410* as we progress through the tree. We also have to enforce read-only411* if this layer is mounted read-only.412*/413static int414null_lookup(struct vop_lookup_args *ap)415{416struct componentname *cnp = ap->a_cnp;417struct vnode *dvp = ap->a_dvp;418uint64_t flags = cnp->cn_flags;419struct vnode *vp, *ldvp, *lvp;420struct mount *mp;421int error;422423mp = dvp->v_mount;424if ((flags & ISLASTCN) != 0 && (mp->mnt_flag & MNT_RDONLY) != 0 &&425(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))426return (EROFS);427/*428* Although it is possible to call null_bypass(), we'll do429* a direct call to reduce overhead430*/431ldvp = NULLVPTOLOWERVP(dvp);432vp = lvp = NULL;433434/*435* Renames in the lower mounts might create an inconsistent436* configuration where lower vnode is moved out of the directory tree437* remounted by our null mount.438*439* Do not try to handle it fancy, just avoid VOP_LOOKUP() with DOTDOT440* name which cannot be handled by the VOP.441*/442if ((flags & ISDOTDOT) != 0) {443struct nameidata *ndp;444445if ((ldvp->v_vflag & VV_ROOT) != 0) {446KASSERT((dvp->v_vflag & VV_ROOT) == 0,447("ldvp %p fl %#x dvp %p fl %#x flags %#jx",448ldvp, ldvp->v_vflag, dvp, dvp->v_vflag,449(uintmax_t)flags));450return (ENOENT);451}452ndp = vfs_lookup_nameidata(cnp);453if (ndp != NULL && vfs_lookup_isroot(ndp, ldvp))454return (ENOENT);455}456457/*458* Hold ldvp. The reference on it, owned by dvp, is lost in459* case of dvp reclamation, and we need ldvp to move our lock460* from ldvp to dvp.461*/462vhold(ldvp);463464error = VOP_LOOKUP(ldvp, &lvp, cnp);465466/*467* VOP_LOOKUP() on lower vnode may unlock ldvp, which allows468* dvp to be reclaimed due to shared v_vnlock. Check for the469* doomed state and return error.470*/471if (VN_IS_DOOMED(dvp)) {472if (error == 0 || error == EJUSTRETURN) {473if (lvp != NULL)474vput(lvp);475error = ENOENT;476}477478/*479* If vgone() did reclaimed dvp before curthread480* relocked ldvp, the locks of dvp and ldpv are no481* longer shared. In this case, relock of ldvp in482* lower fs VOP_LOOKUP() does not restore the locking483* state of dvp. Compensate for this by unlocking484* ldvp and locking dvp, which is also correct if the485* locks are still shared.486*/487VOP_UNLOCK(ldvp);488vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);489}490vdrop(ldvp);491492if (error == EJUSTRETURN && (flags & ISLASTCN) != 0 &&493(mp->mnt_flag & MNT_RDONLY) != 0 &&494(cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))495error = EROFS;496497if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {498if (ldvp == lvp) {499*ap->a_vpp = dvp;500vref(dvp);501vrele(lvp);502} else {503error = null_nodeget(mp, lvp, &vp);504if (error == 0)505*ap->a_vpp = vp;506}507}508return (error);509}510511static int512null_open(struct vop_open_args *ap)513{514int retval;515struct vnode *vp, *ldvp;516517vp = ap->a_vp;518ldvp = NULLVPTOLOWERVP(vp);519retval = null_bypass(&ap->a_gen);520if (retval == 0) {521vp->v_object = ldvp->v_object;522if ((vn_irflag_read(ldvp) & VIRF_PGREAD) != 0) {523MPASS(vp->v_object != NULL);524if ((vn_irflag_read(vp) & VIRF_PGREAD) == 0) {525vn_irflag_set_cond(vp, VIRF_PGREAD);526}527}528}529return (retval);530}531532/*533* Setattr call. Disallow write attempts if the layer is mounted read-only.534*/535static int536null_setattr(struct vop_setattr_args *ap)537{538struct vnode *vp = ap->a_vp;539struct vattr *vap = ap->a_vap;540541if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||542vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||543vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&544(vp->v_mount->mnt_flag & MNT_RDONLY))545return (EROFS);546if (vap->va_size != VNOVAL) {547switch (vp->v_type) {548case VDIR:549return (EISDIR);550case VCHR:551case VBLK:552case VSOCK:553case VFIFO:554if (vap->va_flags != VNOVAL)555return (EOPNOTSUPP);556return (0);557case VREG:558case VLNK:559default:560/*561* Disallow write attempts if the filesystem is562* mounted read-only.563*/564if (vp->v_mount->mnt_flag & MNT_RDONLY)565return (EROFS);566}567}568569return (null_bypass(&ap->a_gen));570}571572/*573* We handle stat and getattr only to change the fsid.574*/575static int576null_stat(struct vop_stat_args *ap)577{578int error;579580if ((error = null_bypass(&ap->a_gen)) != 0)581return (error);582583ap->a_sb->st_dev = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];584return (0);585}586587static int588null_getattr(struct vop_getattr_args *ap)589{590int error;591592if ((error = null_bypass(&ap->a_gen)) != 0)593return (error);594595ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];596return (0);597}598599/*600* Handle to disallow write access if mounted read-only.601*/602static int603null_access(struct vop_access_args *ap)604{605struct vnode *vp = ap->a_vp;606accmode_t accmode = ap->a_accmode;607608/*609* Disallow write attempts on read-only layers;610* unless the file is a socket, fifo, or a block or611* character device resident on the filesystem.612*/613if (accmode & VWRITE) {614switch (vp->v_type) {615case VDIR:616case VLNK:617case VREG:618if (vp->v_mount->mnt_flag & MNT_RDONLY)619return (EROFS);620break;621default:622break;623}624}625return (null_bypass(&ap->a_gen));626}627628static int629null_accessx(struct vop_accessx_args *ap)630{631struct vnode *vp = ap->a_vp;632accmode_t accmode = ap->a_accmode;633634/*635* Disallow write attempts on read-only layers;636* unless the file is a socket, fifo, or a block or637* character device resident on the filesystem.638*/639if (accmode & VWRITE) {640switch (vp->v_type) {641case VDIR:642case VLNK:643case VREG:644if (vp->v_mount->mnt_flag & MNT_RDONLY)645return (EROFS);646break;647default:648break;649}650}651return (null_bypass(&ap->a_gen));652}653654/*655* Increasing refcount of lower vnode is needed at least for the case656* when lower FS is NFS to do sillyrename if the file is in use.657* Unfortunately v_usecount is incremented in many places in658* the kernel and, as such, there may be races that result in659* the NFS client doing an extraneous silly rename, but that seems660* preferable to not doing a silly rename when it is needed.661*/662static int663null_remove(struct vop_remove_args *ap)664{665int retval, vreleit;666struct vnode *lvp, *vp;667668vp = ap->a_vp;669if (vrefcnt(vp) > 1) {670lvp = NULLVPTOLOWERVP(vp);671vref(lvp);672vreleit = 1;673} else674vreleit = 0;675VTONULL(vp)->null_flags |= NULLV_DROP;676retval = null_bypass(&ap->a_gen);677if (vreleit != 0)678vrele(lvp);679return (retval);680}681682/*683* We handle this to eliminate null FS to lower FS684* file moving. Don't know why we don't allow this,685* possibly we should.686*/687static int688null_rename(struct vop_rename_args *ap)689{690struct vnode *fdvp, *fvp, *tdvp, *tvp;691struct vnode *lfdvp, *lfvp, *ltdvp, *ltvp;692struct null_node *fdnn, *fnn, *tdnn, *tnn;693int error;694695tdvp = ap->a_tdvp;696fvp = ap->a_fvp;697fdvp = ap->a_fdvp;698tvp = ap->a_tvp;699lfdvp = NULL;700701/* Check for cross-device rename. */702if ((fvp->v_mount != tdvp->v_mount) ||703(tvp != NULL && fvp->v_mount != tvp->v_mount)) {704error = EXDEV;705goto upper_err;706}707708VI_LOCK(fdvp);709fdnn = VTONULL(fdvp);710if (fdnn == NULL) { /* fdvp is not locked, can be doomed */711VI_UNLOCK(fdvp);712error = ENOENT;713goto upper_err;714}715lfdvp = fdnn->null_lowervp;716vref(lfdvp);717VI_UNLOCK(fdvp);718719VI_LOCK(fvp);720fnn = VTONULL(fvp);721if (fnn == NULL) {722VI_UNLOCK(fvp);723error = ENOENT;724goto upper_err;725}726lfvp = fnn->null_lowervp;727vref(lfvp);728VI_UNLOCK(fvp);729730tdnn = VTONULL(tdvp);731ltdvp = tdnn->null_lowervp;732vref(ltdvp);733734if (tvp != NULL) {735tnn = VTONULL(tvp);736ltvp = tnn->null_lowervp;737vref(ltvp);738tnn->null_flags |= NULLV_DROP;739} else {740ltvp = NULL;741}742743error = VOP_RENAME(lfdvp, lfvp, ap->a_fcnp, ltdvp, ltvp, ap->a_tcnp);744vrele(fdvp);745vrele(fvp);746vrele(tdvp);747if (tvp != NULL)748vrele(tvp);749return (error);750751upper_err:752if (tdvp == tvp)753vrele(tdvp);754else755vput(tdvp);756if (tvp)757vput(tvp);758if (lfdvp != NULL)759vrele(lfdvp);760vrele(fdvp);761vrele(fvp);762return (error);763}764765static int766null_rmdir(struct vop_rmdir_args *ap)767{768769VTONULL(ap->a_vp)->null_flags |= NULLV_DROP;770return (null_bypass(&ap->a_gen));771}772773/*774* We need to process our own vnode lock and then clear the interlock flag as775* it applies only to our vnode, not the vnodes below us on the stack.776*777* We have to hold the vnode here to solve a potential reclaim race. If we're778* forcibly vgone'd while we still have refs, a thread could be sleeping inside779* the lowervp's vop_lock routine. When we vgone we will drop our last ref to780* the lowervp, which would allow it to be reclaimed. The lowervp could then781* be recycled, in which case it is not legal to be sleeping in its VOP. We782* prevent it from being recycled by holding the vnode here.783*/784static struct vnode *785null_lock_prep_with_smr(struct vop_lock1_args *ap)786{787struct null_node *nn;788struct vnode *lvp;789790vfs_smr_enter();791792lvp = NULL;793794nn = VTONULL_SMR(ap->a_vp);795if (__predict_true(nn != NULL)) {796lvp = nn->null_lowervp;797if (lvp != NULL && !vhold_smr(lvp))798lvp = NULL;799}800801vfs_smr_exit();802return (lvp);803}804805static struct vnode *806null_lock_prep_with_interlock(struct vop_lock1_args *ap)807{808struct null_node *nn;809struct vnode *lvp;810811ASSERT_VI_LOCKED(ap->a_vp, __func__);812813ap->a_flags &= ~LK_INTERLOCK;814815lvp = NULL;816817nn = VTONULL(ap->a_vp);818if (__predict_true(nn != NULL)) {819lvp = nn->null_lowervp;820if (lvp != NULL)821vholdnz(lvp);822}823VI_UNLOCK(ap->a_vp);824return (lvp);825}826827static int828null_lock(struct vop_lock1_args *ap)829{830struct vnode *lvp;831int error, flags;832833if (__predict_true((ap->a_flags & LK_INTERLOCK) == 0)) {834lvp = null_lock_prep_with_smr(ap);835if (__predict_false(lvp == NULL)) {836VI_LOCK(ap->a_vp);837lvp = null_lock_prep_with_interlock(ap);838}839} else {840lvp = null_lock_prep_with_interlock(ap);841}842843ASSERT_VI_UNLOCKED(ap->a_vp, __func__);844845if (__predict_false(lvp == NULL))846return (vop_stdlock(ap));847848VNPASS(lvp->v_holdcnt > 0, lvp);849error = VOP_LOCK(lvp, ap->a_flags);850/*851* We might have slept to get the lock and someone might have852* clean our vnode already, switching vnode lock from one in853* lowervp to v_lock in our own vnode structure. Handle this854* case by reacquiring correct lock in requested mode.855*/856if (VTONULL(ap->a_vp) == NULL && error == 0) {857flags = ap->a_flags;858ap->a_flags &= ~LK_TYPE_MASK;859switch (flags & LK_TYPE_MASK) {860case LK_SHARED:861ap->a_flags |= LK_SHARED;862break;863case LK_UPGRADE:864case LK_EXCLUSIVE:865ap->a_flags |= LK_EXCLUSIVE;866break;867default:868panic("Unsupported lock request %d\n",869flags);870}871VOP_UNLOCK(lvp);872error = vop_stdlock(ap);873}874vdrop(lvp);875return (error);876}877878static int879null_unlock(struct vop_unlock_args *ap)880{881struct vnode *vp = ap->a_vp;882struct null_node *nn;883struct vnode *lvp;884int error;885886/*887* Contrary to null_lock, we don't need to hold the vnode around888* unlock.889*890* We hold the lock, which means we can't be racing against vgone.891*892* At the same time VOP_UNLOCK promises to not touch anything after893* it finishes unlock, just like we don't.894*895* vop_stdunlock for a doomed vnode matches doomed locking in null_lock.896*/897nn = VTONULL(vp);898if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {899error = VOP_UNLOCK(lvp);900} else {901error = vop_stdunlock(ap);902}903904return (error);905}906907/*908* Do not allow the VOP_INACTIVE to be passed to the lower layer,909* since the reference count on the lower vnode is not related to910* ours.911*/912static int913null_want_recycle(struct vnode *vp)914{915struct vnode *lvp;916struct null_node *xp;917struct mount *mp;918struct null_mount *xmp;919920xp = VTONULL(vp);921lvp = NULLVPTOLOWERVP(vp);922mp = vp->v_mount;923xmp = MOUNTTONULLMOUNT(mp);924if ((xmp->nullm_flags & NULLM_CACHE) == 0 ||925(xp->null_flags & NULLV_DROP) != 0 ||926(lvp->v_vflag & VV_NOSYNC) != 0) {927/*928* If this is the last reference and caching of the929* nullfs vnodes is not enabled, or the lower vnode is930* deleted, then free up the vnode so as not to tie up931* the lower vnodes.932*/933return (1);934}935return (0);936}937938static int939null_inactive(struct vop_inactive_args *ap)940{941struct vnode *vp;942943vp = ap->a_vp;944if (null_want_recycle(vp)) {945vp->v_object = NULL;946vrecycle(vp);947}948return (0);949}950951static int952null_need_inactive(struct vop_need_inactive_args *ap)953{954955return (null_want_recycle(ap->a_vp) || vn_need_pageq_flush(ap->a_vp));956}957958/*959* Now, the nullfs vnode and, due to the sharing lock, the lower960* vnode, are exclusively locked, and we shall destroy the null vnode.961*/962static int963null_reclaim(struct vop_reclaim_args *ap)964{965struct vnode *vp;966struct null_node *xp;967struct vnode *lowervp;968969vp = ap->a_vp;970xp = VTONULL(vp);971lowervp = xp->null_lowervp;972973KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock,974("Reclaiming incomplete null vnode %p", vp));975976null_hashrem(xp);977/*978* Use the interlock to protect the clearing of v_data to979* prevent faults in null_lock().980*/981lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);982VI_LOCK(vp);983vp->v_data = NULL;984vp->v_object = NULL;985vp->v_vnlock = &vp->v_lock;986987/*988* If we were opened for write, we leased the write reference989* to the lower vnode. If this is a reclamation due to the990* forced unmount, undo the reference now.991*/992if (vp->v_writecount > 0)993VOP_ADD_WRITECOUNT(lowervp, -vp->v_writecount);994else if (vp->v_writecount < 0)995vp->v_writecount = 0;996997VI_UNLOCK(vp);998999if ((xp->null_flags & NULLV_NOUNLOCK) != 0)1000vunref(lowervp);1001else1002vput(lowervp);1003uma_zfree_smr(null_node_zone, xp);10041005return (0);1006}10071008static int1009null_print(struct vop_print_args *ap)1010{1011struct vnode *vp = ap->a_vp;10121013printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp);1014return (0);1015}10161017/* ARGSUSED */1018static int1019null_getwritemount(struct vop_getwritemount_args *ap)1020{1021struct null_node *xp;1022struct vnode *lowervp;1023struct vnode *vp;10241025vp = ap->a_vp;1026VI_LOCK(vp);1027xp = VTONULL(vp);1028if (xp && (lowervp = xp->null_lowervp)) {1029vholdnz(lowervp);1030VI_UNLOCK(vp);1031VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);1032vdrop(lowervp);1033} else {1034VI_UNLOCK(vp);1035*(ap->a_mpp) = NULL;1036}1037return (0);1038}10391040static int1041null_vptofh(struct vop_vptofh_args *ap)1042{1043struct vnode *lvp;10441045lvp = NULLVPTOLOWERVP(ap->a_vp);1046return VOP_VPTOFH(lvp, ap->a_fhp);1047}10481049static int1050null_vptocnp(struct vop_vptocnp_args *ap)1051{1052struct vnode *vp = ap->a_vp;1053struct vnode **dvp = ap->a_vpp;1054struct vnode *lvp, *ldvp;1055struct mount *mp;1056int error, locked;10571058locked = VOP_ISLOCKED(vp);1059lvp = NULLVPTOLOWERVP(vp);1060mp = vp->v_mount;1061error = vfs_busy(mp, MBF_NOWAIT);1062if (error != 0)1063return (error);1064vhold(lvp);1065VOP_UNLOCK(vp); /* vp is held by vn_vptocnp_locked that called us */1066ldvp = lvp;1067vref(lvp);1068error = vn_vptocnp(&ldvp, ap->a_buf, ap->a_buflen);1069vdrop(lvp);1070if (error != 0) {1071vn_lock(vp, locked | LK_RETRY);1072vfs_unbusy(mp);1073return (ENOENT);1074}10751076error = vn_lock(ldvp, LK_SHARED);1077if (error != 0) {1078vrele(ldvp);1079vn_lock(vp, locked | LK_RETRY);1080vfs_unbusy(mp);1081return (ENOENT);1082}1083error = null_nodeget(mp, ldvp, dvp);1084if (error == 0) {1085#ifdef DIAGNOSTIC1086NULLVPTOLOWERVP(*dvp);1087#endif1088VOP_UNLOCK(*dvp); /* keep reference on *dvp */1089}1090vn_lock(vp, locked | LK_RETRY);1091vfs_unbusy(mp);1092return (error);1093}10941095static int1096null_read_pgcache(struct vop_read_pgcache_args *ap)1097{1098struct vnode *lvp, *vp;1099struct null_node *xp;1100int error;11011102vp = ap->a_vp;1103VI_LOCK(vp);1104xp = VTONULL(vp);1105if (xp == NULL) {1106VI_UNLOCK(vp);1107return (EJUSTRETURN);1108}1109lvp = xp->null_lowervp;1110vref(lvp);1111VI_UNLOCK(vp);1112error = VOP_READ_PGCACHE(lvp, ap->a_uio, ap->a_ioflag, ap->a_cred);1113vrele(lvp);1114return (error);1115}11161117static int1118null_advlock(struct vop_advlock_args *ap)1119{1120struct vnode *lvp, *vp;1121struct null_node *xp;1122int error;11231124vp = ap->a_vp;1125VI_LOCK(vp);1126xp = VTONULL(vp);1127if (xp == NULL) {1128VI_UNLOCK(vp);1129return (EBADF);1130}1131lvp = xp->null_lowervp;1132vref(lvp);1133VI_UNLOCK(vp);1134error = VOP_ADVLOCK(lvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);1135vrele(lvp);1136return (error);1137}11381139/*1140* Avoid standard bypass, since lower dvp and vp could be no longer1141* valid after vput().1142*/1143static int1144null_vput_pair(struct vop_vput_pair_args *ap)1145{1146struct mount *mp;1147struct vnode *dvp, *ldvp, *lvp, *vp, *vp1, **vpp;1148int error, res;11491150dvp = ap->a_dvp;1151ldvp = NULLVPTOLOWERVP(dvp);1152vref(ldvp);11531154vpp = ap->a_vpp;1155vp = NULL;1156lvp = NULL;1157mp = NULL;1158if (vpp != NULL)1159vp = *vpp;1160if (vp != NULL) {1161lvp = NULLVPTOLOWERVP(vp);1162vref(lvp);1163if (!ap->a_unlock_vp) {1164vhold(vp);1165vhold(lvp);1166mp = vp->v_mount;1167vfs_ref(mp);1168}1169}11701171res = VOP_VPUT_PAIR(ldvp, lvp != NULL ? &lvp : NULL, true);1172if (vp != NULL && ap->a_unlock_vp)1173vrele(vp);1174vrele(dvp);11751176if (vp == NULL || ap->a_unlock_vp)1177return (res);11781179/* lvp has been unlocked and vp might be reclaimed */1180VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);1181if (vp->v_data == NULL && vfs_busy(mp, MBF_NOWAIT) == 0) {1182vput(vp);1183vget(lvp, LK_EXCLUSIVE | LK_RETRY);1184if (VN_IS_DOOMED(lvp)) {1185vput(lvp);1186vget(vp, LK_EXCLUSIVE | LK_RETRY);1187} else {1188error = null_nodeget(mp, lvp, &vp1);1189if (error == 0) {1190*vpp = vp1;1191} else {1192vget(vp, LK_EXCLUSIVE | LK_RETRY);1193}1194}1195vfs_unbusy(mp);1196}1197vdrop(lvp);1198vdrop(vp);1199vfs_rel(mp);12001201return (res);1202}12031204static int1205null_getlowvnode(struct vop_getlowvnode_args *ap)1206{1207struct vnode *vp, *vpl;12081209vp = ap->a_vp;1210if (vn_lock(vp, LK_SHARED) != 0)1211return (EBADF);12121213vpl = NULLVPTOLOWERVP(vp);1214vhold(vpl);1215VOP_UNLOCK(vp);1216VOP_GETLOWVNODE(vpl, ap->a_vplp, ap->a_flags);1217vdrop(vpl);1218return (0);1219}12201221/*1222* Global vfs data structures1223*/1224struct vop_vector null_vnodeops = {1225.vop_bypass = null_bypass,1226.vop_access = null_access,1227.vop_accessx = null_accessx,1228.vop_advlock = null_advlock,1229.vop_advlockpurge = vop_stdadvlockpurge,1230.vop_bmap = VOP_EOPNOTSUPP,1231.vop_stat = null_stat,1232.vop_getattr = null_getattr,1233.vop_getlowvnode = null_getlowvnode,1234.vop_getwritemount = null_getwritemount,1235.vop_inactive = null_inactive,1236.vop_need_inactive = null_need_inactive,1237.vop_islocked = vop_stdislocked,1238.vop_lock1 = null_lock,1239.vop_lookup = null_lookup,1240.vop_open = null_open,1241.vop_print = null_print,1242.vop_read_pgcache = null_read_pgcache,1243.vop_reclaim = null_reclaim,1244.vop_remove = null_remove,1245.vop_rename = null_rename,1246.vop_rmdir = null_rmdir,1247.vop_setattr = null_setattr,1248.vop_strategy = VOP_EOPNOTSUPP,1249.vop_unlock = null_unlock,1250.vop_vptocnp = null_vptocnp,1251.vop_vptofh = null_vptofh,1252.vop_add_writecount = null_add_writecount,1253.vop_vput_pair = null_vput_pair,1254.vop_copy_file_range = VOP_PANIC,1255};1256VFS_VOP_VECTOR_REGISTER(null_vnodeops);125712581259