/*-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!null_is_nullfs_vnode(*this_vp_p))) {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;789790lvp = NULL;791792vfs_smr_enter();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) {857VOP_UNLOCK(lvp);858859flags = ap->a_flags;860ap->a_flags &= ~LK_TYPE_MASK;861switch (flags & LK_TYPE_MASK) {862case LK_SHARED:863ap->a_flags |= LK_SHARED;864break;865case LK_UPGRADE:866case LK_EXCLUSIVE:867ap->a_flags |= LK_EXCLUSIVE;868break;869default:870panic("Unsupported lock request %d\n",871flags);872}873error = vop_stdlock(ap);874}875vdrop(lvp);876return (error);877}878879static int880null_unlock(struct vop_unlock_args *ap)881{882struct vnode *vp = ap->a_vp;883struct null_node *nn;884struct vnode *lvp;885int error;886887/*888* Contrary to null_lock, we don't need to hold the vnode around889* unlock.890*891* We hold the lock, which means we can't be racing against vgone.892*893* At the same time VOP_UNLOCK promises to not touch anything after894* it finishes unlock, just like we don't.895*896* vop_stdunlock for a doomed vnode matches doomed locking in null_lock.897*/898nn = VTONULL(vp);899if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {900error = VOP_UNLOCK(lvp);901} else {902error = vop_stdunlock(ap);903}904905return (error);906}907908/*909* Do not allow the VOP_INACTIVE to be passed to the lower layer,910* since the reference count on the lower vnode is not related to911* ours.912*/913static int914null_want_recycle(struct vnode *vp)915{916struct vnode *lvp;917struct null_node *xp;918struct mount *mp;919struct null_mount *xmp;920921xp = VTONULL(vp);922lvp = NULLVPTOLOWERVP(vp);923mp = vp->v_mount;924xmp = MOUNTTONULLMOUNT(mp);925if ((xmp->nullm_flags & NULLM_CACHE) == 0 ||926(xp->null_flags & NULLV_DROP) != 0 ||927(lvp->v_vflag & VV_NOSYNC) != 0) {928/*929* If this is the last reference and caching of the930* nullfs vnodes is not enabled, or the lower vnode is931* deleted, then free up the vnode so as not to tie up932* the lower vnodes.933*/934return (1);935}936return (0);937}938939static int940null_inactive(struct vop_inactive_args *ap)941{942struct vnode *vp;943944vp = ap->a_vp;945if (null_want_recycle(vp)) {946vp->v_object = NULL;947vrecycle(vp);948}949return (0);950}951952static int953null_need_inactive(struct vop_need_inactive_args *ap)954{955956return (null_want_recycle(ap->a_vp) || vn_need_pageq_flush(ap->a_vp));957}958959/*960* Now, the nullfs vnode and, due to the sharing lock, the lower961* vnode, are exclusively locked, and we shall destroy the null vnode.962*/963static int964null_reclaim(struct vop_reclaim_args *ap)965{966struct vnode *vp;967struct null_node *xp;968struct vnode *lowervp;969970vp = ap->a_vp;971xp = VTONULL(vp);972lowervp = xp->null_lowervp;973974KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock,975("Reclaiming incomplete null vnode %p", vp));976977null_hashrem(xp);978/*979* Use the interlock to protect the clearing of v_data to980* prevent faults in null_lock().981*/982lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);983VI_LOCK(vp);984vp->v_data = NULL;985vp->v_object = NULL;986vp->v_vnlock = &vp->v_lock;987988/*989* If we were opened for write, we leased the write reference990* to the lower vnode. If this is a reclamation due to the991* forced unmount, undo the reference now.992*/993if (vp->v_writecount > 0)994VOP_ADD_WRITECOUNT(lowervp, -vp->v_writecount);995else if (vp->v_writecount < 0)996vp->v_writecount = 0;997998VI_UNLOCK(vp);9991000if ((xp->null_flags & NULLV_NOUNLOCK) != 0)1001vunref(lowervp);1002else1003vput(lowervp);1004uma_zfree_smr(null_node_zone, xp);10051006return (0);1007}10081009static int1010null_print(struct vop_print_args *ap)1011{1012struct vnode *vp = ap->a_vp;10131014printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp);1015return (0);1016}10171018/* ARGSUSED */1019static int1020null_getwritemount(struct vop_getwritemount_args *ap)1021{1022struct null_node *xp;1023struct vnode *lowervp;1024struct vnode *vp;10251026vp = ap->a_vp;1027VI_LOCK(vp);1028xp = VTONULL(vp);1029if (xp && (lowervp = xp->null_lowervp)) {1030vholdnz(lowervp);1031VI_UNLOCK(vp);1032VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);1033vdrop(lowervp);1034} else {1035VI_UNLOCK(vp);1036*(ap->a_mpp) = NULL;1037}1038return (0);1039}10401041static int1042null_vptofh(struct vop_vptofh_args *ap)1043{1044struct vnode *lvp;10451046lvp = NULLVPTOLOWERVP(ap->a_vp);1047return VOP_VPTOFH(lvp, ap->a_fhp);1048}10491050static int1051null_vptocnp(struct vop_vptocnp_args *ap)1052{1053struct vnode *vp = ap->a_vp;1054struct vnode **dvp = ap->a_vpp;1055struct vnode *lvp, *ldvp;1056struct mount *mp;1057int error, locked;10581059locked = VOP_ISLOCKED(vp);1060lvp = NULLVPTOLOWERVP(vp);1061mp = vp->v_mount;1062error = vfs_busy(mp, MBF_NOWAIT);1063if (error != 0)1064return (error);1065vhold(lvp);1066VOP_UNLOCK(vp); /* vp is held by vn_vptocnp_locked that called us */1067ldvp = lvp;1068vref(lvp);1069error = vn_vptocnp(&ldvp, ap->a_buf, ap->a_buflen);1070vdrop(lvp);1071if (error != 0) {1072vn_lock(vp, locked | LK_RETRY);1073vfs_unbusy(mp);1074return (ENOENT);1075}10761077error = vn_lock(ldvp, LK_SHARED);1078if (error != 0) {1079vrele(ldvp);1080vn_lock(vp, locked | LK_RETRY);1081vfs_unbusy(mp);1082return (ENOENT);1083}1084error = null_nodeget(mp, ldvp, dvp);1085if (error == 0) {1086#ifdef DIAGNOSTIC1087NULLVPTOLOWERVP(*dvp);1088#endif1089VOP_UNLOCK(*dvp); /* keep reference on *dvp */1090}1091vn_lock(vp, locked | LK_RETRY);1092vfs_unbusy(mp);1093return (error);1094}10951096static int1097null_read_pgcache(struct vop_read_pgcache_args *ap)1098{1099struct vnode *lvp, *vp;1100struct null_node *xp;1101int error;11021103vp = ap->a_vp;1104VI_LOCK(vp);1105xp = VTONULL(vp);1106if (xp == NULL) {1107VI_UNLOCK(vp);1108return (EJUSTRETURN);1109}1110lvp = xp->null_lowervp;1111vref(lvp);1112VI_UNLOCK(vp);1113error = VOP_READ_PGCACHE(lvp, ap->a_uio, ap->a_ioflag, ap->a_cred);1114vrele(lvp);1115return (error);1116}11171118static int1119null_advlock(struct vop_advlock_args *ap)1120{1121struct vnode *lvp, *vp;1122struct null_node *xp;1123int error;11241125vp = ap->a_vp;1126VI_LOCK(vp);1127xp = VTONULL(vp);1128if (xp == NULL) {1129VI_UNLOCK(vp);1130return (EBADF);1131}1132lvp = xp->null_lowervp;1133vref(lvp);1134VI_UNLOCK(vp);1135error = VOP_ADVLOCK(lvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);1136vrele(lvp);1137return (error);1138}11391140/*1141* Avoid standard bypass, since lower dvp and vp could be no longer1142* valid after vput().1143*/1144static int1145null_vput_pair(struct vop_vput_pair_args *ap)1146{1147struct mount *mp;1148struct vnode *dvp, *ldvp, *lvp, *vp, *vp1, **vpp;1149int error, res;11501151dvp = ap->a_dvp;1152ldvp = NULLVPTOLOWERVP(dvp);1153vref(ldvp);11541155vpp = ap->a_vpp;1156vp = NULL;1157lvp = NULL;1158mp = NULL;1159if (vpp != NULL)1160vp = *vpp;1161if (vp != NULL) {1162lvp = NULLVPTOLOWERVP(vp);1163vref(lvp);1164if (!ap->a_unlock_vp) {1165vhold(vp);1166vhold(lvp);1167mp = vp->v_mount;1168vfs_ref(mp);1169}1170}11711172res = VOP_VPUT_PAIR(ldvp, lvp != NULL ? &lvp : NULL, true);1173if (vp != NULL && ap->a_unlock_vp)1174vrele(vp);1175vrele(dvp);11761177if (vp == NULL || ap->a_unlock_vp)1178return (res);11791180/* lvp has been unlocked and vp might be reclaimed */1181VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);1182if (vp->v_data == NULL && vfs_busy(mp, MBF_NOWAIT) == 0) {1183vput(vp);1184vget(lvp, LK_EXCLUSIVE | LK_RETRY);1185if (VN_IS_DOOMED(lvp)) {1186vput(lvp);1187vget(vp, LK_EXCLUSIVE | LK_RETRY);1188} else {1189error = null_nodeget(mp, lvp, &vp1);1190if (error == 0) {1191*vpp = vp1;1192} else {1193vget(vp, LK_EXCLUSIVE | LK_RETRY);1194}1195}1196vfs_unbusy(mp);1197}1198vdrop(lvp);1199vdrop(vp);1200vfs_rel(mp);12011202return (res);1203}12041205static int1206null_getlowvnode(struct vop_getlowvnode_args *ap)1207{1208struct vnode *vp, *vpl;12091210vp = ap->a_vp;1211if (vn_lock(vp, LK_SHARED) != 0)1212return (EBADF);12131214vpl = NULLVPTOLOWERVP(vp);1215vhold(vpl);1216VOP_UNLOCK(vp);1217VOP_GETLOWVNODE(vpl, ap->a_vplp, ap->a_flags);1218vdrop(vpl);1219return (0);1220}12211222/*1223* Global vfs data structures1224*/1225struct vop_vector null_vnodeops = {1226.vop_bypass = null_bypass,1227.vop_access = null_access,1228.vop_accessx = null_accessx,1229.vop_advlock = null_advlock,1230.vop_advlockpurge = vop_stdadvlockpurge,1231.vop_bmap = VOP_EOPNOTSUPP,1232.vop_stat = null_stat,1233.vop_getattr = null_getattr,1234.vop_getlowvnode = null_getlowvnode,1235.vop_getwritemount = null_getwritemount,1236.vop_inactive = null_inactive,1237.vop_need_inactive = null_need_inactive,1238.vop_islocked = vop_stdislocked,1239.vop_lock1 = null_lock,1240.vop_lookup = null_lookup,1241.vop_open = null_open,1242.vop_print = null_print,1243.vop_read_pgcache = null_read_pgcache,1244.vop_reclaim = null_reclaim,1245.vop_remove = null_remove,1246.vop_rename = null_rename,1247.vop_rmdir = null_rmdir,1248.vop_setattr = null_setattr,1249.vop_strategy = VOP_EOPNOTSUPP,1250.vop_unlock = null_unlock,1251.vop_vptocnp = null_vptocnp,1252.vop_vptofh = null_vptofh,1253.vop_add_writecount = null_add_writecount,1254.vop_vput_pair = null_vput_pair,1255.vop_copy_file_range = VOP_PANIC,1256};1257VFS_VOP_VECTOR_REGISTER(null_vnodeops);12581259struct vop_vector null_vnodeops_no_unp_bypass = {1260.vop_default = &null_vnodeops,1261.vop_unp_bind = vop_stdunp_bind,1262.vop_unp_connect = vop_stdunp_connect,1263.vop_unp_detach = vop_stdunp_detach,1264};1265VFS_VOP_VECTOR_REGISTER(null_vnodeops_no_unp_bypass);126612671268