]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nullfs/null_vfsops.c
MFV r328245: 8856 arc_cksum_is_equal() doesn't take into account ABD-logic
[FreeBSD/FreeBSD.git] / sys / fs / nullfs / null_vfsops.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993, 1995
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
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  *      @(#)null_vfsops.c       8.2 (Berkeley) 1/21/94
35  *
36  * @(#)lofs_vfsops.c    1.2 (Berkeley) 6/18/92
37  * $FreeBSD$
38  */
39
40 /*
41  * Null Layer
42  * (See null_vnops.c for a description of what this does.)
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/fcntl.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/proc.h>
54 #include <sys/vnode.h>
55 #include <sys/jail.h>
56
57 #include <fs/nullfs/null.h>
58
59 static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
60
61 static vfs_fhtovp_t     nullfs_fhtovp;
62 static vfs_mount_t      nullfs_mount;
63 static vfs_quotactl_t   nullfs_quotactl;
64 static vfs_root_t       nullfs_root;
65 static vfs_sync_t       nullfs_sync;
66 static vfs_statfs_t     nullfs_statfs;
67 static vfs_unmount_t    nullfs_unmount;
68 static vfs_vget_t       nullfs_vget;
69 static vfs_extattrctl_t nullfs_extattrctl;
70
71 /*
72  * Mount null layer
73  */
74 static int
75 nullfs_mount(struct mount *mp)
76 {
77         int error = 0;
78         struct vnode *lowerrootvp, *vp;
79         struct vnode *nullm_rootvp;
80         struct null_mount *xmp;
81         struct thread *td = curthread;
82         char *target;
83         int isvnunlocked = 0, len;
84         struct nameidata nd, *ndp = &nd;
85
86         NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
87
88         if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_NULLFS))
89                 return (EPERM);
90         if (mp->mnt_flag & MNT_ROOTFS)
91                 return (EOPNOTSUPP);
92
93         /*
94          * Update is a no-op
95          */
96         if (mp->mnt_flag & MNT_UPDATE) {
97                 /*
98                  * Only support update mounts for NFS export.
99                  */
100                 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
101                         return (0);
102                 else
103                         return (EOPNOTSUPP);
104         }
105
106         /*
107          * Get argument
108          */
109         error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
110         if (error || target[len - 1] != '\0')
111                 return (EINVAL);
112
113         /*
114          * Unlock lower node to avoid possible deadlock.
115          */
116         if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
117             VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
118                 VOP_UNLOCK(mp->mnt_vnodecovered, 0);
119                 isvnunlocked = 1;
120         }
121         /*
122          * Find lower node
123          */
124         NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
125         error = namei(ndp);
126
127         /*
128          * Re-lock vnode.
129          * XXXKIB This is deadlock-prone as well.
130          */
131         if (isvnunlocked)
132                 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
133
134         if (error)
135                 return (error);
136         NDFREE(ndp, NDF_ONLY_PNBUF);
137
138         /*
139          * Sanity check on lower vnode
140          */
141         lowerrootvp = ndp->ni_vp;
142
143         /*
144          * Check multi null mount to avoid `lock against myself' panic.
145          */
146         if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
147                 NULLFSDEBUG("nullfs_mount: multi null mount?\n");
148                 vput(lowerrootvp);
149                 return (EDEADLK);
150         }
151
152         xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
153             M_NULLFSMNT, M_WAITOK | M_ZERO);
154
155         /*
156          * Save reference to underlying FS
157          */
158         xmp->nullm_vfs = lowerrootvp->v_mount;
159
160         /*
161          * Save reference.  Each mount also holds
162          * a reference on the root vnode.
163          */
164         error = null_nodeget(mp, lowerrootvp, &vp);
165         /*
166          * Make sure the node alias worked
167          */
168         if (error) {
169                 free(xmp, M_NULLFSMNT);
170                 return (error);
171         }
172
173         /*
174          * Keep a held reference to the root vnode.
175          * It is vrele'd in nullfs_unmount.
176          */
177         nullm_rootvp = vp;
178         nullm_rootvp->v_vflag |= VV_ROOT;
179         xmp->nullm_rootvp = nullm_rootvp;
180
181         /*
182          * Unlock the node (either the lower or the alias)
183          */
184         VOP_UNLOCK(vp, 0);
185
186         if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
187                 MNT_ILOCK(mp);
188                 mp->mnt_flag |= MNT_LOCAL;
189                 MNT_IUNLOCK(mp);
190         }
191
192         xmp->nullm_flags |= NULLM_CACHE;
193         if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0 ||
194             (xmp->nullm_vfs->mnt_kern_flag & MNTK_NULL_NOCACHE) != 0)
195                 xmp->nullm_flags &= ~NULLM_CACHE;
196
197         MNT_ILOCK(mp);
198         if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
199                 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
200                     (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED |
201                     MNTK_EXTENDED_SHARED);
202         }
203         mp->mnt_kern_flag |= MNTK_LOOKUP_EXCL_DOTDOT;
204         mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
205             (MNTK_USES_BCACHE | MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS);
206         MNT_IUNLOCK(mp);
207         mp->mnt_data = xmp;
208         vfs_getnewfsid(mp);
209         if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
210                 MNT_ILOCK(xmp->nullm_vfs);
211                 TAILQ_INSERT_TAIL(&xmp->nullm_vfs->mnt_uppers, mp,
212                     mnt_upper_link);
213                 MNT_IUNLOCK(xmp->nullm_vfs);
214         }
215
216         vfs_mountedfrom(mp, target);
217
218         NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
219                 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
220         return (0);
221 }
222
223 /*
224  * Free reference to null layer
225  */
226 static int
227 nullfs_unmount(mp, mntflags)
228         struct mount *mp;
229         int mntflags;
230 {
231         struct null_mount *mntdata;
232         struct mount *ump;
233         int error, flags;
234
235         NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
236
237         if (mntflags & MNT_FORCE)
238                 flags = FORCECLOSE;
239         else
240                 flags = 0;
241
242         /* There is 1 extra root vnode reference (nullm_rootvp). */
243         error = vflush(mp, 1, flags, curthread);
244         if (error)
245                 return (error);
246
247         /*
248          * Finally, throw away the null_mount structure
249          */
250         mntdata = mp->mnt_data;
251         ump = mntdata->nullm_vfs;
252         if ((mntdata->nullm_flags & NULLM_CACHE) != 0) {
253                 MNT_ILOCK(ump);
254                 while ((ump->mnt_kern_flag & MNTK_VGONE_UPPER) != 0) {
255                         ump->mnt_kern_flag |= MNTK_VGONE_WAITER;
256                         msleep(&ump->mnt_uppers, &ump->mnt_mtx, 0, "vgnupw", 0);
257                 }
258                 TAILQ_REMOVE(&ump->mnt_uppers, mp, mnt_upper_link);
259                 MNT_IUNLOCK(ump);
260         }
261         mp->mnt_data = NULL;
262         free(mntdata, M_NULLFSMNT);
263         return (0);
264 }
265
266 static int
267 nullfs_root(mp, flags, vpp)
268         struct mount *mp;
269         int flags;
270         struct vnode **vpp;
271 {
272         struct vnode *vp;
273
274         NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
275             (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
276             (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
277
278         /*
279          * Return locked reference to root.
280          */
281         vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
282         VREF(vp);
283
284         ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
285         vn_lock(vp, flags | LK_RETRY);
286         *vpp = vp;
287         return 0;
288 }
289
290 static int
291 nullfs_quotactl(mp, cmd, uid, arg)
292         struct mount *mp;
293         int cmd;
294         uid_t uid;
295         void *arg;
296 {
297         return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
298 }
299
300 static int
301 nullfs_statfs(mp, sbp)
302         struct mount *mp;
303         struct statfs *sbp;
304 {
305         int error;
306         struct statfs *mstat;
307
308         NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
309             (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
310             (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
311
312         mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO);
313
314         error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, mstat);
315         if (error) {
316                 free(mstat, M_STATFS);
317                 return (error);
318         }
319
320         /* now copy across the "interesting" information and fake the rest */
321         sbp->f_type = mstat->f_type;
322         sbp->f_flags = (sbp->f_flags & (MNT_RDONLY | MNT_NOEXEC | MNT_NOSUID |
323             MNT_UNION | MNT_NOSYMFOLLOW | MNT_AUTOMOUNTED)) |
324             (mstat->f_flags & ~(MNT_ROOTFS | MNT_AUTOMOUNTED));
325         sbp->f_bsize = mstat->f_bsize;
326         sbp->f_iosize = mstat->f_iosize;
327         sbp->f_blocks = mstat->f_blocks;
328         sbp->f_bfree = mstat->f_bfree;
329         sbp->f_bavail = mstat->f_bavail;
330         sbp->f_files = mstat->f_files;
331         sbp->f_ffree = mstat->f_ffree;
332
333         free(mstat, M_STATFS);
334         return (0);
335 }
336
337 static int
338 nullfs_sync(mp, waitfor)
339         struct mount *mp;
340         int waitfor;
341 {
342         /*
343          * XXX - Assumes no data cached at null layer.
344          */
345         return (0);
346 }
347
348 static int
349 nullfs_vget(mp, ino, flags, vpp)
350         struct mount *mp;
351         ino_t ino;
352         int flags;
353         struct vnode **vpp;
354 {
355         int error;
356
357         KASSERT((flags & LK_TYPE_MASK) != 0,
358             ("nullfs_vget: no lock requested"));
359
360         error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
361         if (error != 0)
362                 return (error);
363         return (null_nodeget(mp, *vpp, vpp));
364 }
365
366 static int
367 nullfs_fhtovp(mp, fidp, flags, vpp)
368         struct mount *mp;
369         struct fid *fidp;
370         int flags;
371         struct vnode **vpp;
372 {
373         int error;
374
375         error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags,
376             vpp);
377         if (error != 0)
378                 return (error);
379         return (null_nodeget(mp, *vpp, vpp));
380 }
381
382 static int                        
383 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
384         struct mount *mp;
385         int cmd;
386         struct vnode *filename_vp;
387         int namespace;
388         const char *attrname;
389 {
390
391         return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd,
392             filename_vp, namespace, attrname));
393 }
394
395 static void
396 nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp)
397 {
398         struct vnode *vp;
399
400         vp = null_hashget(mp, lowervp);
401         if (vp == NULL)
402                 return;
403         VTONULL(vp)->null_flags |= NULLV_NOUNLOCK;
404         vgone(vp);
405         vput(vp);
406 }
407
408 static void
409 nullfs_unlink_lowervp(struct mount *mp, struct vnode *lowervp)
410 {
411         struct vnode *vp;
412         struct null_node *xp;
413
414         vp = null_hashget(mp, lowervp);
415         if (vp == NULL)
416                 return;
417         xp = VTONULL(vp);
418         xp->null_flags |= NULLV_DROP | NULLV_NOUNLOCK;
419         vhold(vp);
420         vunref(vp);
421
422         if (vp->v_usecount == 0) {
423                 /*
424                  * If vunref() dropped the last use reference on the
425                  * nullfs vnode, it must be reclaimed, and its lock
426                  * was split from the lower vnode lock.  Need to do
427                  * extra unlock before allowing the final vdrop() to
428                  * free the vnode.
429                  */
430                 KASSERT((vp->v_iflag & VI_DOOMED) != 0,
431                     ("not reclaimed nullfs vnode %p", vp));
432                 VOP_UNLOCK(vp, 0);
433         } else {
434                 /*
435                  * Otherwise, the nullfs vnode still shares the lock
436                  * with the lower vnode, and must not be unlocked.
437                  * Also clear the NULLV_NOUNLOCK, the flag is not
438                  * relevant for future reclamations.
439                  */
440                 ASSERT_VOP_ELOCKED(vp, "unlink_lowervp");
441                 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
442                     ("reclaimed nullfs vnode %p", vp));
443                 xp->null_flags &= ~NULLV_NOUNLOCK;
444         }
445         vdrop(vp);
446 }
447
448 static struct vfsops null_vfsops = {
449         .vfs_extattrctl =       nullfs_extattrctl,
450         .vfs_fhtovp =           nullfs_fhtovp,
451         .vfs_init =             nullfs_init,
452         .vfs_mount =            nullfs_mount,
453         .vfs_quotactl =         nullfs_quotactl,
454         .vfs_root =             nullfs_root,
455         .vfs_statfs =           nullfs_statfs,
456         .vfs_sync =             nullfs_sync,
457         .vfs_uninit =           nullfs_uninit,
458         .vfs_unmount =          nullfs_unmount,
459         .vfs_vget =             nullfs_vget,
460         .vfs_reclaim_lowervp =  nullfs_reclaim_lowervp,
461         .vfs_unlink_lowervp =   nullfs_unlink_lowervp,
462 };
463
464 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL);