Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/fs/nullfs/null_vnops.c
103335 views
1
/*-
2
* SPDX-License-Identifier: BSD-3-Clause
3
*
4
* Copyright (c) 1992, 1993
5
* The Regents of the University of California. All rights reserved.
6
*
7
* This code is derived from software contributed to Berkeley by
8
* John Heidemann of the UCLA Ficus project.
9
*
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
12
* are met:
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
21
*
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
* SUCH DAMAGE.
33
*
34
* Ancestors:
35
* ...and...
36
*/
37
38
/*
39
* Null Layer
40
*
41
* (See mount_nullfs(8) for more information.)
42
*
43
* The null layer duplicates a portion of the filesystem
44
* name space under a new name. In this respect, it is
45
* similar to the loopback filesystem. It differs from
46
* the loopback fs in two respects: it is implemented using
47
* a stackable layers techniques, and its "null-node"s stack above
48
* all lower-layer vnodes, not just over directory vnodes.
49
*
50
* The null layer has two purposes. First, it serves as a demonstration
51
* of layering by proving a layer which does nothing. (It actually
52
* does everything the loopback filesystem does, which is slightly
53
* more than nothing.) Second, the null layer can serve as a prototype
54
* layer. Since it provides all necessary layer framework,
55
* new filesystem layers can be created very easily be starting
56
* with a null layer.
57
*
58
* The remainder of this man page examines the null layer as a basis
59
* for constructing new layers.
60
*
61
*
62
* INSTANTIATING NEW NULL LAYERS
63
*
64
* New null layers are created with mount_nullfs(8).
65
* Mount_nullfs(8) takes two arguments, the pathname
66
* of the lower vfs (target-pn) and the pathname where the null
67
* layer will appear in the namespace (alias-pn). After
68
* the null layer is put into place, the contents
69
* of target-pn subtree will be aliased under alias-pn.
70
*
71
*
72
* OPERATION OF A NULL LAYER
73
*
74
* The null layer is the minimum filesystem layer,
75
* simply bypassing all possible operations to the lower layer
76
* for processing there. The majority of its activity centers
77
* on the bypass routine, through which nearly all vnode operations
78
* pass.
79
*
80
* The bypass routine accepts arbitrary vnode operations for
81
* handling by the lower layer. It begins by examining vnode
82
* operation arguments and replacing any null-nodes by their
83
* lower-layer equivlants. It then invokes the operation
84
* on the lower layer. Finally, it replaces the null-nodes
85
* in the arguments and, if a vnode is return by the operation,
86
* stacks a null-node on top of the returned vnode.
87
*
88
* Although bypass handles most operations, vop_getattr, vop_lock,
89
* vop_unlock, vop_inactive, vop_reclaim, and vop_print are not
90
* bypassed. Vop_getattr must change the fsid being returned.
91
* Vop_lock and vop_unlock must handle any locking for the
92
* current vnode as well as pass the lock request down.
93
* Vop_inactive and vop_reclaim are not bypassed so that
94
* they can handle freeing null-layer specific data. Vop_print
95
* is not bypassed to avoid excessive debugging information.
96
* Also, certain vnode operations change the locking state within
97
* the operation (create, mknod, remove, link, rename, mkdir, rmdir,
98
* and symlink). Ideally these operations should not change the
99
* lock state, but should be changed to let the caller of the
100
* function unlock them. Otherwise all intermediate vnode layers
101
* (such as union, umapfs, etc) must catch these functions to do
102
* the necessary locking at their layer.
103
*
104
*
105
* INSTANTIATING VNODE STACKS
106
*
107
* Mounting associates the null layer with a lower layer,
108
* effect stacking two VFSes. Vnode stacks are instead
109
* created on demand as files are accessed.
110
*
111
* The initial mount creates a single vnode stack for the
112
* root of the new null layer. All other vnode stacks
113
* are created as a result of vnode operations on
114
* this or other null vnode stacks.
115
*
116
* New vnode stacks come into existence as a result of
117
* an operation which returns a vnode.
118
* The bypass routine stacks a null-node above the new
119
* vnode before returning it to the caller.
120
*
121
* For example, imagine mounting a null layer with
122
* "mount_nullfs /usr/include /dev/layer/null".
123
* Changing directory to /dev/layer/null will assign
124
* the root null-node (which was created when the null layer was mounted).
125
* Now consider opening "sys". A vop_lookup would be
126
* done on the root null-node. This operation would bypass through
127
* to the lower layer which would return a vnode representing
128
* the UFS "sys". Null_bypass then builds a null-node
129
* aliasing the UFS "sys" and returns this to the caller.
130
* Later operations on the null-node "sys" will repeat this
131
* process when constructing other vnode stacks.
132
*
133
*
134
* CREATING OTHER FILE SYSTEM LAYERS
135
*
136
* One of the easiest ways to construct new filesystem layers is to make
137
* a copy of the null layer, rename all files and variables, and
138
* then begin modifing the copy. Sed can be used to easily rename
139
* all variables.
140
*
141
* The umap layer is an example of a layer descended from the
142
* null layer.
143
*
144
*
145
* INVOKING OPERATIONS ON LOWER LAYERS
146
*
147
* There are two techniques to invoke operations on a lower layer
148
* when the operation cannot be completely bypassed. Each method
149
* is appropriate in different situations. In both cases,
150
* it is the responsibility of the aliasing layer to make
151
* the operation arguments "correct" for the lower layer
152
* by mapping a vnode arguments to the lower layer.
153
*
154
* The first approach is to call the aliasing layer's bypass routine.
155
* This method is most suitable when you wish to invoke the operation
156
* currently being handled on the lower layer. It has the advantage
157
* that the bypass routine already must do argument mapping.
158
* An example of this is null_getattrs in the null layer.
159
*
160
* A second approach is to directly invoke vnode operations on
161
* the lower layer with the VOP_OPERATIONNAME interface.
162
* The advantage of this method is that it is easy to invoke
163
* arbitrary operations on the lower layer. The disadvantage
164
* is that vnode arguments must be manualy mapped.
165
*
166
*/
167
168
#include <sys/param.h>
169
#include <sys/systm.h>
170
#include <sys/conf.h>
171
#include <sys/kernel.h>
172
#include <sys/lock.h>
173
#include <sys/malloc.h>
174
#include <sys/mount.h>
175
#include <sys/mutex.h>
176
#include <sys/namei.h>
177
#include <sys/proc.h>
178
#include <sys/smr.h>
179
#include <sys/sysctl.h>
180
#include <sys/vnode.h>
181
#include <sys/stat.h>
182
183
#include <fs/nullfs/null.h>
184
185
#include <vm/vm.h>
186
#include <vm/vm_extern.h>
187
#include <vm/vm_object.h>
188
#include <vm/vnode_pager.h>
189
190
VFS_SMR_DECLARE;
191
192
static int null_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
193
SYSCTL_INT(_debug, OID_AUTO, nullfs_bug_bypass, CTLFLAG_RW,
194
&null_bug_bypass, 0, "");
195
196
/*
197
* Synchronize inotify flags with the lower vnode:
198
* - If the upper vnode has the flag set and the lower does not, then the lower
199
* vnode is unwatched and the upper vnode does not need to go through
200
* VOP_INOTIFY.
201
* - If the lower vnode is watched, then the upper vnode should go through
202
* VOP_INOTIFY, so copy the flag up.
203
*/
204
static void
205
null_copy_inotify(struct vnode *vp, struct vnode *lvp, short flag)
206
{
207
if ((vn_irflag_read(vp) & flag) != 0) {
208
if (__predict_false((vn_irflag_read(lvp) & flag) == 0))
209
vn_irflag_unset(vp, flag);
210
} else if ((vn_irflag_read(lvp) & flag) != 0) {
211
if (__predict_false((vn_irflag_read(vp) & flag) == 0))
212
vn_irflag_set(vp, flag);
213
}
214
}
215
216
/*
217
* This is the 10-Apr-92 bypass routine.
218
* This version has been optimized for speed, throwing away some
219
* safety checks. It should still always work, but it's not as
220
* robust to programmer errors.
221
*
222
* In general, we map all vnodes going down and unmap them on the way back.
223
* As an exception to this, vnodes can be marked "unmapped" by setting
224
* the Nth bit in operation's vdesc_flags.
225
*
226
* Also, some BSD vnode operations have the side effect of vrele'ing
227
* their arguments. With stacking, the reference counts are held
228
* by the upper node, not the lower one, so we must handle these
229
* side-effects here. This is not of concern in Sun-derived systems
230
* since there are no such side-effects.
231
*
232
* This makes the following assumptions:
233
* - only one returned vpp
234
* - no INOUT vpp's (Sun's vop_open has one of these)
235
* - the vnode operation vector of the first vnode should be used
236
* to determine what implementation of the op should be invoked
237
* - all mapped vnodes are of our vnode-type (NEEDSWORK:
238
* problems on rmdir'ing mount points and renaming?)
239
*/
240
int
241
null_bypass(struct vop_generic_args *ap)
242
{
243
struct vnode **this_vp_p;
244
struct vnode *old_vps[VDESC_MAX_VPS];
245
struct vnode **vps_p[VDESC_MAX_VPS];
246
struct vnode ***vppp;
247
struct vnode *lvp;
248
struct vnodeop_desc *descp = ap->a_desc;
249
int error, i, reles;
250
251
if (null_bug_bypass)
252
printf ("null_bypass: %s\n", descp->vdesc_name);
253
254
#ifdef DIAGNOSTIC
255
/*
256
* We require at least one vp.
257
*/
258
if (descp->vdesc_vp_offsets == NULL ||
259
descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
260
panic ("null_bypass: no vp's in map");
261
#endif
262
263
/*
264
* Map the vnodes going in.
265
* Later, we'll invoke the operation based on
266
* the first mapped vnode's operation vector.
267
*/
268
reles = descp->vdesc_flags;
269
for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
270
if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
271
break; /* bail out at end of list */
272
vps_p[i] = this_vp_p = VOPARG_OFFSETTO(struct vnode **,
273
descp->vdesc_vp_offsets[i], ap);
274
275
/*
276
* We're not guaranteed that any but the first vnode
277
* are of our type. Check for and don't map any
278
* that aren't. (We must always map first vp or vclean fails.)
279
*/
280
if (i != 0 && (*this_vp_p == NULL ||
281
!null_is_nullfs_vnode(*this_vp_p))) {
282
old_vps[i] = NULL;
283
} else {
284
old_vps[i] = *this_vp_p;
285
*(vps_p[i]) = NULLVPTOLOWERVP(*this_vp_p);
286
287
/*
288
* The upper vnode reference to the lower
289
* vnode is the only reference that keeps our
290
* pointer to the lower vnode alive. If lower
291
* vnode is relocked during the VOP call,
292
* upper vnode might become unlocked and
293
* reclaimed, which invalidates our reference.
294
* Add a transient hold around VOP call.
295
*/
296
vhold(*this_vp_p);
297
298
/*
299
* XXX - Several operations have the side effect
300
* of vrele'ing their vp's. We must account for
301
* that. (This should go away in the future.)
302
*/
303
if (reles & VDESC_VP0_WILLRELE)
304
vref(*this_vp_p);
305
}
306
}
307
308
/*
309
* Call the operation on the lower layer
310
* with the modified argument structure.
311
*/
312
if (vps_p[0] != NULL && *vps_p[0] != NULL) {
313
error = ap->a_desc->vdesc_call(ap);
314
} else {
315
printf("null_bypass: no map for %s\n", descp->vdesc_name);
316
error = EINVAL;
317
}
318
319
/*
320
* Maintain the illusion of call-by-value
321
* by restoring vnodes in the argument structure
322
* to their original value.
323
*/
324
reles = descp->vdesc_flags;
325
for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
326
if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
327
break; /* bail out at end of list */
328
if (old_vps[i] != NULL) {
329
lvp = *(vps_p[i]);
330
331
/*
332
* Get rid of the transient hold on lvp. Copy inotify
333
* flags up in case something is watching the lower
334
* layer.
335
*
336
* If lowervp was unlocked during VOP
337
* operation, nullfs upper vnode could have
338
* been reclaimed, which changes its v_vnlock
339
* back to private v_lock. In this case we
340
* must move lock ownership from lower to
341
* upper (reclaimed) vnode.
342
*/
343
if (lvp != NULL) {
344
null_copy_inotify(old_vps[i], lvp,
345
VIRF_INOTIFY);
346
null_copy_inotify(old_vps[i], lvp,
347
VIRF_INOTIFY_PARENT);
348
if (VOP_ISLOCKED(lvp) == LK_EXCLUSIVE &&
349
old_vps[i]->v_vnlock != lvp->v_vnlock) {
350
VOP_UNLOCK(lvp);
351
VOP_LOCK(old_vps[i], LK_EXCLUSIVE |
352
LK_RETRY);
353
}
354
vdrop(lvp);
355
}
356
357
*(vps_p[i]) = old_vps[i];
358
#if 0
359
if (reles & VDESC_VP0_WILLUNLOCK)
360
VOP_UNLOCK(*(vps_p[i]), 0);
361
#endif
362
if (reles & VDESC_VP0_WILLRELE)
363
vrele(*(vps_p[i]));
364
}
365
}
366
367
/*
368
* Map the possible out-going vpp
369
* (Assumes that the lower layer always returns
370
* a VREF'ed vpp unless it gets an error.)
371
*/
372
if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && error == 0) {
373
/*
374
* XXX - even though some ops have vpp returned vp's,
375
* several ops actually vrele this before returning.
376
* We must avoid these ops.
377
* (This should go away when these ops are regularized.)
378
*/
379
vppp = VOPARG_OFFSETTO(struct vnode ***,
380
descp->vdesc_vpp_offset, ap);
381
if (*vppp != NULL)
382
error = null_nodeget(old_vps[0]->v_mount, **vppp,
383
*vppp);
384
}
385
386
return (error);
387
}
388
389
static int
390
null_add_writecount(struct vop_add_writecount_args *ap)
391
{
392
struct vnode *lvp, *vp;
393
int error;
394
395
vp = ap->a_vp;
396
lvp = NULLVPTOLOWERVP(vp);
397
VI_LOCK(vp);
398
/* text refs are bypassed to lowervp */
399
VNASSERT(vp->v_writecount >= 0, vp, ("wrong null writecount"));
400
VNASSERT(vp->v_writecount + ap->a_inc >= 0, vp,
401
("wrong writecount inc %d", ap->a_inc));
402
error = VOP_ADD_WRITECOUNT(lvp, ap->a_inc);
403
if (error == 0)
404
vp->v_writecount += ap->a_inc;
405
VI_UNLOCK(vp);
406
return (error);
407
}
408
409
/*
410
* We have to carry on the locking protocol on the null layer vnodes
411
* as we progress through the tree. We also have to enforce read-only
412
* if this layer is mounted read-only.
413
*/
414
static int
415
null_lookup(struct vop_lookup_args *ap)
416
{
417
struct componentname *cnp = ap->a_cnp;
418
struct vnode *dvp = ap->a_dvp;
419
uint64_t flags = cnp->cn_flags;
420
struct vnode *vp, *ldvp, *lvp;
421
struct mount *mp;
422
int error;
423
424
mp = dvp->v_mount;
425
if ((flags & ISLASTCN) != 0 && (mp->mnt_flag & MNT_RDONLY) != 0 &&
426
(cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
427
return (EROFS);
428
/*
429
* Although it is possible to call null_bypass(), we'll do
430
* a direct call to reduce overhead
431
*/
432
ldvp = NULLVPTOLOWERVP(dvp);
433
vp = lvp = NULL;
434
435
/*
436
* Renames in the lower mounts might create an inconsistent
437
* configuration where lower vnode is moved out of the directory tree
438
* remounted by our null mount.
439
*
440
* Do not try to handle it fancy, just avoid VOP_LOOKUP() with DOTDOT
441
* name which cannot be handled by the VOP.
442
*/
443
if ((flags & ISDOTDOT) != 0) {
444
struct nameidata *ndp;
445
446
if ((ldvp->v_vflag & VV_ROOT) != 0) {
447
KASSERT((dvp->v_vflag & VV_ROOT) == 0,
448
("ldvp %p fl %#x dvp %p fl %#x flags %#jx",
449
ldvp, ldvp->v_vflag, dvp, dvp->v_vflag,
450
(uintmax_t)flags));
451
return (ENOENT);
452
}
453
ndp = vfs_lookup_nameidata(cnp);
454
if (ndp != NULL && vfs_lookup_isroot(ndp, ldvp))
455
return (ENOENT);
456
}
457
458
/*
459
* Hold ldvp. The reference on it, owned by dvp, is lost in
460
* case of dvp reclamation, and we need ldvp to move our lock
461
* from ldvp to dvp.
462
*/
463
vhold(ldvp);
464
465
error = VOP_LOOKUP(ldvp, &lvp, cnp);
466
467
/*
468
* VOP_LOOKUP() on lower vnode may unlock ldvp, which allows
469
* dvp to be reclaimed due to shared v_vnlock. Check for the
470
* doomed state and return error.
471
*/
472
if (VN_IS_DOOMED(dvp)) {
473
if (error == 0 || error == EJUSTRETURN) {
474
if (lvp != NULL)
475
vput(lvp);
476
error = ENOENT;
477
}
478
479
/*
480
* If vgone() did reclaimed dvp before curthread
481
* relocked ldvp, the locks of dvp and ldpv are no
482
* longer shared. In this case, relock of ldvp in
483
* lower fs VOP_LOOKUP() does not restore the locking
484
* state of dvp. Compensate for this by unlocking
485
* ldvp and locking dvp, which is also correct if the
486
* locks are still shared.
487
*/
488
VOP_UNLOCK(ldvp);
489
vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
490
}
491
vdrop(ldvp);
492
493
if (error == EJUSTRETURN && (flags & ISLASTCN) != 0 &&
494
(mp->mnt_flag & MNT_RDONLY) != 0 &&
495
(cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
496
error = EROFS;
497
498
if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) {
499
if (ldvp == lvp) {
500
*ap->a_vpp = dvp;
501
vref(dvp);
502
vrele(lvp);
503
} else {
504
error = null_nodeget(mp, lvp, &vp);
505
if (error == 0)
506
*ap->a_vpp = vp;
507
}
508
}
509
return (error);
510
}
511
512
static int
513
null_open(struct vop_open_args *ap)
514
{
515
int retval;
516
struct vnode *vp, *ldvp;
517
518
vp = ap->a_vp;
519
ldvp = NULLVPTOLOWERVP(vp);
520
retval = null_bypass(&ap->a_gen);
521
if (retval == 0) {
522
vp->v_object = ldvp->v_object;
523
if ((vn_irflag_read(ldvp) & VIRF_PGREAD) != 0) {
524
MPASS(vp->v_object != NULL);
525
if ((vn_irflag_read(vp) & VIRF_PGREAD) == 0) {
526
vn_irflag_set_cond(vp, VIRF_PGREAD);
527
}
528
}
529
}
530
return (retval);
531
}
532
533
/*
534
* Setattr call. Disallow write attempts if the layer is mounted read-only.
535
*/
536
static int
537
null_setattr(struct vop_setattr_args *ap)
538
{
539
struct vnode *vp = ap->a_vp;
540
struct vattr *vap = ap->a_vap;
541
542
if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
543
vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
544
vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
545
(vp->v_mount->mnt_flag & MNT_RDONLY))
546
return (EROFS);
547
if (vap->va_size != VNOVAL) {
548
switch (vp->v_type) {
549
case VDIR:
550
return (EISDIR);
551
case VCHR:
552
case VBLK:
553
case VSOCK:
554
case VFIFO:
555
if (vap->va_flags != VNOVAL)
556
return (EOPNOTSUPP);
557
return (0);
558
case VREG:
559
case VLNK:
560
default:
561
/*
562
* Disallow write attempts if the filesystem is
563
* mounted read-only.
564
*/
565
if (vp->v_mount->mnt_flag & MNT_RDONLY)
566
return (EROFS);
567
}
568
}
569
570
return (null_bypass(&ap->a_gen));
571
}
572
573
/*
574
* We handle stat and getattr only to change the fsid.
575
*/
576
static int
577
null_stat(struct vop_stat_args *ap)
578
{
579
int error;
580
581
if ((error = null_bypass(&ap->a_gen)) != 0)
582
return (error);
583
584
ap->a_sb->st_dev = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
585
return (0);
586
}
587
588
static int
589
null_getattr(struct vop_getattr_args *ap)
590
{
591
int error;
592
593
if ((error = null_bypass(&ap->a_gen)) != 0)
594
return (error);
595
596
ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
597
return (0);
598
}
599
600
/*
601
* Handle to disallow write access if mounted read-only.
602
*/
603
static int
604
null_access(struct vop_access_args *ap)
605
{
606
struct vnode *vp = ap->a_vp;
607
accmode_t accmode = ap->a_accmode;
608
609
/*
610
* Disallow write attempts on read-only layers;
611
* unless the file is a socket, fifo, or a block or
612
* character device resident on the filesystem.
613
*/
614
if (accmode & VWRITE) {
615
switch (vp->v_type) {
616
case VDIR:
617
case VLNK:
618
case VREG:
619
if (vp->v_mount->mnt_flag & MNT_RDONLY)
620
return (EROFS);
621
break;
622
default:
623
break;
624
}
625
}
626
return (null_bypass(&ap->a_gen));
627
}
628
629
static int
630
null_accessx(struct vop_accessx_args *ap)
631
{
632
struct vnode *vp = ap->a_vp;
633
accmode_t accmode = ap->a_accmode;
634
635
/*
636
* Disallow write attempts on read-only layers;
637
* unless the file is a socket, fifo, or a block or
638
* character device resident on the filesystem.
639
*/
640
if (accmode & VWRITE) {
641
switch (vp->v_type) {
642
case VDIR:
643
case VLNK:
644
case VREG:
645
if (vp->v_mount->mnt_flag & MNT_RDONLY)
646
return (EROFS);
647
break;
648
default:
649
break;
650
}
651
}
652
return (null_bypass(&ap->a_gen));
653
}
654
655
/*
656
* Increasing refcount of lower vnode is needed at least for the case
657
* when lower FS is NFS to do sillyrename if the file is in use.
658
* Unfortunately v_usecount is incremented in many places in
659
* the kernel and, as such, there may be races that result in
660
* the NFS client doing an extraneous silly rename, but that seems
661
* preferable to not doing a silly rename when it is needed.
662
*/
663
static int
664
null_remove(struct vop_remove_args *ap)
665
{
666
int retval, vreleit;
667
struct vnode *lvp, *vp;
668
669
vp = ap->a_vp;
670
if (vrefcnt(vp) > 1) {
671
lvp = NULLVPTOLOWERVP(vp);
672
vref(lvp);
673
vreleit = 1;
674
} else
675
vreleit = 0;
676
VTONULL(vp)->null_flags |= NULLV_DROP;
677
retval = null_bypass(&ap->a_gen);
678
if (vreleit != 0)
679
vrele(lvp);
680
return (retval);
681
}
682
683
/*
684
* We handle this to eliminate null FS to lower FS
685
* file moving. Don't know why we don't allow this,
686
* possibly we should.
687
*/
688
static int
689
null_rename(struct vop_rename_args *ap)
690
{
691
struct vnode *fdvp, *fvp, *tdvp, *tvp;
692
struct vnode *lfdvp, *lfvp, *ltdvp, *ltvp;
693
struct null_node *fdnn, *fnn, *tdnn, *tnn;
694
int error;
695
696
tdvp = ap->a_tdvp;
697
fvp = ap->a_fvp;
698
fdvp = ap->a_fdvp;
699
tvp = ap->a_tvp;
700
lfdvp = NULL;
701
702
/* Check for cross-device rename. */
703
if ((fvp->v_mount != tdvp->v_mount) ||
704
(tvp != NULL && fvp->v_mount != tvp->v_mount)) {
705
error = EXDEV;
706
goto upper_err;
707
}
708
709
VI_LOCK(fdvp);
710
fdnn = VTONULL(fdvp);
711
if (fdnn == NULL) { /* fdvp is not locked, can be doomed */
712
VI_UNLOCK(fdvp);
713
error = ENOENT;
714
goto upper_err;
715
}
716
lfdvp = fdnn->null_lowervp;
717
vref(lfdvp);
718
VI_UNLOCK(fdvp);
719
720
VI_LOCK(fvp);
721
fnn = VTONULL(fvp);
722
if (fnn == NULL) {
723
VI_UNLOCK(fvp);
724
error = ENOENT;
725
goto upper_err;
726
}
727
lfvp = fnn->null_lowervp;
728
vref(lfvp);
729
VI_UNLOCK(fvp);
730
731
tdnn = VTONULL(tdvp);
732
ltdvp = tdnn->null_lowervp;
733
vref(ltdvp);
734
735
if (tvp != NULL) {
736
tnn = VTONULL(tvp);
737
ltvp = tnn->null_lowervp;
738
vref(ltvp);
739
tnn->null_flags |= NULLV_DROP;
740
} else {
741
ltvp = NULL;
742
}
743
744
error = VOP_RENAME(lfdvp, lfvp, ap->a_fcnp, ltdvp, ltvp, ap->a_tcnp);
745
vrele(fdvp);
746
vrele(fvp);
747
vrele(tdvp);
748
if (tvp != NULL)
749
vrele(tvp);
750
return (error);
751
752
upper_err:
753
if (tdvp == tvp)
754
vrele(tdvp);
755
else
756
vput(tdvp);
757
if (tvp)
758
vput(tvp);
759
if (lfdvp != NULL)
760
vrele(lfdvp);
761
vrele(fdvp);
762
vrele(fvp);
763
return (error);
764
}
765
766
static int
767
null_rmdir(struct vop_rmdir_args *ap)
768
{
769
770
VTONULL(ap->a_vp)->null_flags |= NULLV_DROP;
771
return (null_bypass(&ap->a_gen));
772
}
773
774
/*
775
* We need to process our own vnode lock and then clear the interlock flag as
776
* it applies only to our vnode, not the vnodes below us on the stack.
777
*
778
* We have to hold the vnode here to solve a potential reclaim race. If we're
779
* forcibly vgone'd while we still have refs, a thread could be sleeping inside
780
* the lowervp's vop_lock routine. When we vgone we will drop our last ref to
781
* the lowervp, which would allow it to be reclaimed. The lowervp could then
782
* be recycled, in which case it is not legal to be sleeping in its VOP. We
783
* prevent it from being recycled by holding the vnode here.
784
*/
785
static struct vnode *
786
null_lock_prep_with_smr(struct vop_lock1_args *ap)
787
{
788
struct null_node *nn;
789
struct vnode *lvp;
790
791
lvp = NULL;
792
793
vfs_smr_enter();
794
795
nn = VTONULL_SMR(ap->a_vp);
796
if (__predict_true(nn != NULL)) {
797
lvp = nn->null_lowervp;
798
if (lvp != NULL && !vhold_smr(lvp))
799
lvp = NULL;
800
}
801
802
vfs_smr_exit();
803
return (lvp);
804
}
805
806
static struct vnode *
807
null_lock_prep_with_interlock(struct vop_lock1_args *ap)
808
{
809
struct null_node *nn;
810
struct vnode *lvp;
811
812
ASSERT_VI_LOCKED(ap->a_vp, __func__);
813
814
ap->a_flags &= ~LK_INTERLOCK;
815
816
lvp = NULL;
817
818
nn = VTONULL(ap->a_vp);
819
if (__predict_true(nn != NULL)) {
820
lvp = nn->null_lowervp;
821
if (lvp != NULL)
822
vholdnz(lvp);
823
}
824
VI_UNLOCK(ap->a_vp);
825
return (lvp);
826
}
827
828
static int
829
null_lock(struct vop_lock1_args *ap)
830
{
831
struct vnode *lvp;
832
int error, flags;
833
834
if (__predict_true((ap->a_flags & LK_INTERLOCK) == 0)) {
835
lvp = null_lock_prep_with_smr(ap);
836
if (__predict_false(lvp == NULL)) {
837
VI_LOCK(ap->a_vp);
838
lvp = null_lock_prep_with_interlock(ap);
839
}
840
} else {
841
lvp = null_lock_prep_with_interlock(ap);
842
}
843
844
ASSERT_VI_UNLOCKED(ap->a_vp, __func__);
845
846
if (__predict_false(lvp == NULL))
847
return (vop_stdlock(ap));
848
849
VNPASS(lvp->v_holdcnt > 0, lvp);
850
error = VOP_LOCK(lvp, ap->a_flags);
851
/*
852
* We might have slept to get the lock and someone might have
853
* clean our vnode already, switching vnode lock from one in
854
* lowervp to v_lock in our own vnode structure. Handle this
855
* case by reacquiring correct lock in requested mode.
856
*/
857
if (VTONULL(ap->a_vp) == NULL && error == 0) {
858
VOP_UNLOCK(lvp);
859
860
flags = ap->a_flags;
861
ap->a_flags &= ~LK_TYPE_MASK;
862
switch (flags & LK_TYPE_MASK) {
863
case LK_SHARED:
864
ap->a_flags |= LK_SHARED;
865
break;
866
case LK_UPGRADE:
867
case LK_EXCLUSIVE:
868
ap->a_flags |= LK_EXCLUSIVE;
869
break;
870
default:
871
panic("Unsupported lock request %d\n",
872
flags);
873
}
874
error = vop_stdlock(ap);
875
}
876
vdrop(lvp);
877
return (error);
878
}
879
880
static int
881
null_unlock(struct vop_unlock_args *ap)
882
{
883
struct vnode *vp = ap->a_vp;
884
struct null_node *nn;
885
struct vnode *lvp;
886
int error;
887
888
/*
889
* Contrary to null_lock, we don't need to hold the vnode around
890
* unlock.
891
*
892
* We hold the lock, which means we can't be racing against vgone.
893
*
894
* At the same time VOP_UNLOCK promises to not touch anything after
895
* it finishes unlock, just like we don't.
896
*
897
* vop_stdunlock for a doomed vnode matches doomed locking in null_lock.
898
*/
899
nn = VTONULL(vp);
900
if (nn != NULL && (lvp = NULLVPTOLOWERVP(vp)) != NULL) {
901
error = VOP_UNLOCK(lvp);
902
} else {
903
error = vop_stdunlock(ap);
904
}
905
906
return (error);
907
}
908
909
/*
910
* Do not allow the VOP_INACTIVE to be passed to the lower layer,
911
* since the reference count on the lower vnode is not related to
912
* ours.
913
*/
914
static int
915
null_want_recycle(struct vnode *vp)
916
{
917
struct vnode *lvp;
918
struct null_node *xp;
919
struct mount *mp;
920
struct null_mount *xmp;
921
922
xp = VTONULL(vp);
923
lvp = NULLVPTOLOWERVP(vp);
924
mp = vp->v_mount;
925
xmp = MOUNTTONULLMOUNT(mp);
926
if ((xmp->nullm_flags & NULLM_CACHE) == 0 ||
927
(xp->null_flags & NULLV_DROP) != 0 ||
928
(lvp->v_vflag & VV_NOSYNC) != 0) {
929
/*
930
* If this is the last reference and caching of the
931
* nullfs vnodes is not enabled, or the lower vnode is
932
* deleted, then free up the vnode so as not to tie up
933
* the lower vnodes.
934
*/
935
return (1);
936
}
937
return (0);
938
}
939
940
static int
941
null_inactive(struct vop_inactive_args *ap)
942
{
943
struct vnode *vp;
944
945
vp = ap->a_vp;
946
if (null_want_recycle(vp)) {
947
vp->v_object = NULL;
948
vrecycle(vp);
949
}
950
return (0);
951
}
952
953
static int
954
null_need_inactive(struct vop_need_inactive_args *ap)
955
{
956
957
return (null_want_recycle(ap->a_vp) || vn_need_pageq_flush(ap->a_vp));
958
}
959
960
/*
961
* Now, the nullfs vnode and, due to the sharing lock, the lower
962
* vnode, are exclusively locked, and we shall destroy the null vnode.
963
*/
964
static int
965
null_reclaim(struct vop_reclaim_args *ap)
966
{
967
struct vnode *vp;
968
struct null_node *xp;
969
struct vnode *lowervp;
970
971
vp = ap->a_vp;
972
xp = VTONULL(vp);
973
lowervp = xp->null_lowervp;
974
975
KASSERT(lowervp != NULL && vp->v_vnlock != &vp->v_lock,
976
("Reclaiming incomplete null vnode %p", vp));
977
978
null_hashrem(xp);
979
/*
980
* Use the interlock to protect the clearing of v_data to
981
* prevent faults in null_lock().
982
*/
983
lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
984
VI_LOCK(vp);
985
vp->v_data = NULL;
986
vp->v_object = NULL;
987
vp->v_vnlock = &vp->v_lock;
988
989
/*
990
* If we were opened for write, we leased the write reference
991
* to the lower vnode. If this is a reclamation due to the
992
* forced unmount, undo the reference now.
993
*/
994
if (vp->v_writecount > 0)
995
VOP_ADD_WRITECOUNT(lowervp, -vp->v_writecount);
996
else if (vp->v_writecount < 0)
997
vp->v_writecount = 0;
998
999
VI_UNLOCK(vp);
1000
1001
if ((xp->null_flags & NULLV_NOUNLOCK) != 0)
1002
vunref(lowervp);
1003
else
1004
vput(lowervp);
1005
uma_zfree_smr(null_node_zone, xp);
1006
1007
return (0);
1008
}
1009
1010
static int
1011
null_print(struct vop_print_args *ap)
1012
{
1013
struct vnode *vp = ap->a_vp;
1014
1015
printf("\tvp=%p, lowervp=%p\n", vp, VTONULL(vp)->null_lowervp);
1016
return (0);
1017
}
1018
1019
/* ARGSUSED */
1020
static int
1021
null_getwritemount(struct vop_getwritemount_args *ap)
1022
{
1023
struct null_node *xp;
1024
struct vnode *lowervp;
1025
struct vnode *vp;
1026
1027
vp = ap->a_vp;
1028
VI_LOCK(vp);
1029
xp = VTONULL(vp);
1030
if (xp && (lowervp = xp->null_lowervp)) {
1031
vholdnz(lowervp);
1032
VI_UNLOCK(vp);
1033
VOP_GETWRITEMOUNT(lowervp, ap->a_mpp);
1034
vdrop(lowervp);
1035
} else {
1036
VI_UNLOCK(vp);
1037
*(ap->a_mpp) = NULL;
1038
}
1039
return (0);
1040
}
1041
1042
static int
1043
null_vptofh(struct vop_vptofh_args *ap)
1044
{
1045
struct vnode *lvp;
1046
1047
lvp = NULLVPTOLOWERVP(ap->a_vp);
1048
return VOP_VPTOFH(lvp, ap->a_fhp);
1049
}
1050
1051
static int
1052
null_vptocnp(struct vop_vptocnp_args *ap)
1053
{
1054
struct vnode *vp = ap->a_vp;
1055
struct vnode **dvp = ap->a_vpp;
1056
struct vnode *lvp, *ldvp;
1057
struct mount *mp;
1058
int error, locked;
1059
1060
locked = VOP_ISLOCKED(vp);
1061
lvp = NULLVPTOLOWERVP(vp);
1062
mp = vp->v_mount;
1063
error = vfs_busy(mp, MBF_NOWAIT);
1064
if (error != 0)
1065
return (error);
1066
vhold(lvp);
1067
VOP_UNLOCK(vp); /* vp is held by vn_vptocnp_locked that called us */
1068
ldvp = lvp;
1069
vref(lvp);
1070
error = vn_vptocnp(&ldvp, ap->a_buf, ap->a_buflen);
1071
vdrop(lvp);
1072
if (error != 0) {
1073
vn_lock(vp, locked | LK_RETRY);
1074
vfs_unbusy(mp);
1075
return (ENOENT);
1076
}
1077
1078
error = vn_lock(ldvp, LK_SHARED);
1079
if (error != 0) {
1080
vrele(ldvp);
1081
vn_lock(vp, locked | LK_RETRY);
1082
vfs_unbusy(mp);
1083
return (ENOENT);
1084
}
1085
error = null_nodeget(mp, ldvp, dvp);
1086
if (error == 0) {
1087
#ifdef DIAGNOSTIC
1088
NULLVPTOLOWERVP(*dvp);
1089
#endif
1090
VOP_UNLOCK(*dvp); /* keep reference on *dvp */
1091
}
1092
vn_lock(vp, locked | LK_RETRY);
1093
vfs_unbusy(mp);
1094
return (error);
1095
}
1096
1097
static int
1098
null_read_pgcache(struct vop_read_pgcache_args *ap)
1099
{
1100
struct vnode *lvp, *vp;
1101
struct null_node *xp;
1102
int error;
1103
1104
vp = ap->a_vp;
1105
VI_LOCK(vp);
1106
xp = VTONULL(vp);
1107
if (xp == NULL) {
1108
VI_UNLOCK(vp);
1109
return (EJUSTRETURN);
1110
}
1111
lvp = xp->null_lowervp;
1112
vref(lvp);
1113
VI_UNLOCK(vp);
1114
error = VOP_READ_PGCACHE(lvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1115
vrele(lvp);
1116
return (error);
1117
}
1118
1119
static int
1120
null_advlock(struct vop_advlock_args *ap)
1121
{
1122
struct vnode *lvp, *vp;
1123
struct null_node *xp;
1124
int error;
1125
1126
vp = ap->a_vp;
1127
VI_LOCK(vp);
1128
xp = VTONULL(vp);
1129
if (xp == NULL) {
1130
VI_UNLOCK(vp);
1131
return (EBADF);
1132
}
1133
lvp = xp->null_lowervp;
1134
vref(lvp);
1135
VI_UNLOCK(vp);
1136
error = VOP_ADVLOCK(lvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);
1137
vrele(lvp);
1138
return (error);
1139
}
1140
1141
/*
1142
* Avoid standard bypass, since lower dvp and vp could be no longer
1143
* valid after vput().
1144
*/
1145
static int
1146
null_vput_pair(struct vop_vput_pair_args *ap)
1147
{
1148
struct mount *mp;
1149
struct vnode *dvp, *ldvp, *lvp, *vp, *vp1, **vpp;
1150
int error, res;
1151
1152
dvp = ap->a_dvp;
1153
ldvp = NULLVPTOLOWERVP(dvp);
1154
vref(ldvp);
1155
1156
vpp = ap->a_vpp;
1157
vp = NULL;
1158
lvp = NULL;
1159
mp = NULL;
1160
if (vpp != NULL)
1161
vp = *vpp;
1162
if (vp != NULL) {
1163
lvp = NULLVPTOLOWERVP(vp);
1164
vref(lvp);
1165
if (!ap->a_unlock_vp) {
1166
vhold(vp);
1167
vhold(lvp);
1168
mp = vp->v_mount;
1169
vfs_ref(mp);
1170
}
1171
}
1172
1173
res = VOP_VPUT_PAIR(ldvp, lvp != NULL ? &lvp : NULL, true);
1174
if (vp != NULL && ap->a_unlock_vp)
1175
vrele(vp);
1176
vrele(dvp);
1177
1178
if (vp == NULL || ap->a_unlock_vp)
1179
return (res);
1180
1181
/* lvp has been unlocked and vp might be reclaimed */
1182
VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1183
if (vp->v_data == NULL && vfs_busy(mp, MBF_NOWAIT) == 0) {
1184
vput(vp);
1185
vget(lvp, LK_EXCLUSIVE | LK_RETRY);
1186
if (VN_IS_DOOMED(lvp)) {
1187
vput(lvp);
1188
vget(vp, LK_EXCLUSIVE | LK_RETRY);
1189
} else {
1190
error = null_nodeget(mp, lvp, &vp1);
1191
if (error == 0) {
1192
*vpp = vp1;
1193
} else {
1194
vget(vp, LK_EXCLUSIVE | LK_RETRY);
1195
}
1196
}
1197
vfs_unbusy(mp);
1198
}
1199
vdrop(lvp);
1200
vdrop(vp);
1201
vfs_rel(mp);
1202
1203
return (res);
1204
}
1205
1206
static int
1207
null_getlowvnode(struct vop_getlowvnode_args *ap)
1208
{
1209
struct vnode *vp, *vpl;
1210
1211
vp = ap->a_vp;
1212
if (vn_lock(vp, LK_SHARED) != 0)
1213
return (EBADF);
1214
1215
vpl = NULLVPTOLOWERVP(vp);
1216
vhold(vpl);
1217
VOP_UNLOCK(vp);
1218
VOP_GETLOWVNODE(vpl, ap->a_vplp, ap->a_flags);
1219
vdrop(vpl);
1220
return (0);
1221
}
1222
1223
/*
1224
* Global vfs data structures
1225
*/
1226
struct vop_vector null_vnodeops = {
1227
.vop_bypass = null_bypass,
1228
.vop_access = null_access,
1229
.vop_accessx = null_accessx,
1230
.vop_advlock = null_advlock,
1231
.vop_advlockpurge = vop_stdadvlockpurge,
1232
.vop_bmap = VOP_EOPNOTSUPP,
1233
.vop_stat = null_stat,
1234
.vop_getattr = null_getattr,
1235
.vop_getlowvnode = null_getlowvnode,
1236
.vop_getwritemount = null_getwritemount,
1237
.vop_inactive = null_inactive,
1238
.vop_need_inactive = null_need_inactive,
1239
.vop_islocked = vop_stdislocked,
1240
.vop_lock1 = null_lock,
1241
.vop_lookup = null_lookup,
1242
.vop_open = null_open,
1243
.vop_print = null_print,
1244
.vop_read_pgcache = null_read_pgcache,
1245
.vop_reclaim = null_reclaim,
1246
.vop_remove = null_remove,
1247
.vop_rename = null_rename,
1248
.vop_rmdir = null_rmdir,
1249
.vop_setattr = null_setattr,
1250
.vop_strategy = VOP_EOPNOTSUPP,
1251
.vop_unlock = null_unlock,
1252
.vop_vptocnp = null_vptocnp,
1253
.vop_vptofh = null_vptofh,
1254
.vop_add_writecount = null_add_writecount,
1255
.vop_vput_pair = null_vput_pair,
1256
.vop_copy_file_range = VOP_PANIC,
1257
};
1258
VFS_VOP_VECTOR_REGISTER(null_vnodeops);
1259
1260
struct vop_vector null_vnodeops_no_unp_bypass = {
1261
.vop_default = &null_vnodeops,
1262
.vop_unp_bind = vop_stdunp_bind,
1263
.vop_unp_connect = vop_stdunp_connect,
1264
.vop_unp_detach = vop_stdunp_detach,
1265
};
1266
VFS_VOP_VECTOR_REGISTER(null_vnodeops_no_unp_bypass);
1267
1268