1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
24 * Copyright (c) 2023, Datto Inc. All rights reserved.
25 * Copyright (c) 2025, Klara, Inc.
26 * Copyright (c) 2025, Rob Norris <robn@despairlabs.com>
27 * Copyright (c) 2026, TrueNAS.
28 */
29
30
31 #include <sys/zfs_znode.h>
32 #include <sys/zfs_vfsops.h>
33 #include <sys/zfs_vnops.h>
34 #include <sys/zfs_ctldir.h>
35 #include <sys/zpl.h>
36 #include <linux/iversion.h>
37 #include <linux/version.h>
38 #include <linux/vfs_compat.h>
39 #include <linux/fs_context.h>
40 #include <linux/fs_parser.h>
41
42 /*
43 * What to do when the last reference to an inode is released. If 0, the kernel
44 * will cache it on the superblock. If 1, the inode will be freed immediately.
45 * See zpl_drop_inode().
46 */
47 int zfs_delete_inode = 0;
48
49 /*
50 * What to do when the last reference to a dentry is released. If 0, the kernel
51 * will cache it until the entry (file) is destroyed. If 1, the dentry will be
52 * marked for cleanup, at which time its inode reference will be released. See
53 * zpl_dentry_delete().
54 */
55 int zfs_delete_dentry = 0;
56
57 static struct inode *
zpl_inode_alloc(struct super_block * sb)58 zpl_inode_alloc(struct super_block *sb)
59 {
60 struct inode *ip;
61
62 VERIFY3S(zfs_inode_alloc(sb, &ip), ==, 0);
63 inode_set_iversion(ip, 1);
64
65 return (ip);
66 }
67
68 #ifdef HAVE_SOPS_FREE_INODE
69 static void
zpl_inode_free(struct inode * ip)70 zpl_inode_free(struct inode *ip)
71 {
72 ASSERT0(atomic_read(&ip->i_count));
73 zfs_inode_free(ip);
74 }
75 #endif
76
77 static void
zpl_inode_destroy(struct inode * ip)78 zpl_inode_destroy(struct inode *ip)
79 {
80 ASSERT0(atomic_read(&ip->i_count));
81 zfs_inode_destroy(ip);
82 }
83
84 /*
85 * Called from __mark_inode_dirty() to reflect that something in the
86 * inode has changed. We use it to ensure the znode system attributes
87 * are always strictly update to date with respect to the inode.
88 */
89 static void
zpl_dirty_inode(struct inode * ip,int flags)90 zpl_dirty_inode(struct inode *ip, int flags)
91 {
92 fstrans_cookie_t cookie;
93
94 cookie = spl_fstrans_mark();
95 zfs_dirty_inode(ip, flags);
96 spl_fstrans_unmark(cookie);
97 }
98
99 /*
100 * ->drop_inode() is called when the last reference to an inode is released.
101 * Its return value indicates if the inode should be destroyed immediately, or
102 * cached on the superblock structure.
103 *
104 * By default (zfs_delete_inode=0), we call generic_drop_inode(), which returns
105 * "destroy immediately" if the inode is unhashed and has no links (roughly: no
106 * longer exists on disk). On datasets with millions of rarely-accessed files,
107 * this can cause a large amount of memory to be "pinned" by cached inodes,
108 * which in turn pin their associated dnodes and dbufs, until the kernel starts
109 * reporting memory pressure and requests OpenZFS release some memory (see
110 * zfs_prune()).
111 *
112 * When set to 1, we call generic_delete_inode(), which always returns "destroy
113 * immediately", resulting in inodes being destroyed immediately, releasing
114 * their associated dnodes and dbufs to the dbuf cached and the ARC to be
115 * evicted as normal.
116 *
117 * Note that the "last reference" doesn't always mean the last _userspace_
118 * reference; the dentry cache also holds a reference, so "busy" inodes will
119 * still be kept alive that way (subject to dcache tuning).
120 */
121 static int
zpl_drop_inode(struct inode * ip)122 zpl_drop_inode(struct inode *ip)
123 {
124 if (zfs_delete_inode)
125 return (generic_delete_inode(ip));
126 return (generic_drop_inode(ip));
127 }
128
129 /*
130 * The ->evict_inode() callback must minimally truncate the inode pages,
131 * and call clear_inode(). For 2.6.35 and later kernels this will
132 * simply update the inode state, with the sync occurring before the
133 * truncate in evict(). For earlier kernels clear_inode() maps to
134 * end_writeback() which is responsible for completing all outstanding
135 * write back. In either case, once this is done it is safe to cleanup
136 * any remaining inode specific data via zfs_inactive().
137 * remaining filesystem specific data.
138 */
139 static void
zpl_evict_inode(struct inode * ip)140 zpl_evict_inode(struct inode *ip)
141 {
142 fstrans_cookie_t cookie;
143
144 cookie = spl_fstrans_mark();
145 truncate_setsize(ip, 0);
146 clear_inode(ip);
147 zfs_inactive(ip);
148 spl_fstrans_unmark(cookie);
149 }
150
151 static void
zpl_put_super(struct super_block * sb)152 zpl_put_super(struct super_block *sb)
153 {
154 fstrans_cookie_t cookie;
155 int error;
156
157 cookie = spl_fstrans_mark();
158 error = -zfs_umount(sb);
159 spl_fstrans_unmark(cookie);
160 ASSERT3S(error, <=, 0);
161 }
162
163 /*
164 * zfs_sync() is the underlying implementation for the sync(2) and syncfs(2)
165 * syscalls, via sb->s_op->sync_fs().
166 *
167 * Before kernel 5.17 (torvalds/linux@5679897eb104), syncfs() ->
168 * sync_filesystem() would ignore the return from sync_fs(), instead only
169 * considing the error from syncing the underlying block device (sb->s_dev).
170 * Since OpenZFS doesn't _have_ an underlying block device, there's no way for
171 * us to report a sync directly.
172 *
173 * However, in 5.8 (torvalds/linux@735e4ae5ba28) the superblock gained an extra
174 * error store `s_wb_err`, to carry errors seen on page writeback since the
175 * last call to syncfs(). If sync_filesystem() does not return an error, any
176 * existing writeback error on the superblock will be used instead (and cleared
177 * either way). We don't use this (page writeback is a different thing for us),
178 * so for 5.8-5.17 we can use that instead to get syncfs() to return the error.
179 *
180 * Before 5.8, we have no other good options - no matter what happens, the
181 * userspace program will be told the call has succeeded, and so we must make
182 * it so, Therefore, when we are asked to wait for sync to complete (wait ==
183 * 1), if zfs_sync() has returned an error we have no choice but to block,
184 * regardless of the reason.
185 *
186 * The 5.17 change was backported to the 5.10, 5.15 and 5.16 series, and likely
187 * to some vendor kernels. Meanwhile, s_wb_err is still in use in 6.15 (the
188 * mainline Linux series at time of writing), and has likely been backported to
189 * vendor kernels before 5.8. We don't really want to use a workaround when we
190 * don't have to, but we can't really detect whether or not sync_filesystem()
191 * will return our errors (without a difficult runtime test anyway). So, we use
192 * a static version check: any kernel reporting its version as 5.17+ will use a
193 * direct error return, otherwise, we'll either use s_wb_err if it was detected
194 * at configure (5.8-5.16 + vendor backports). If it's unavailable, we will
195 * block to ensure the correct semantics.
196 *
197 * See https://github.com/openzfs/zfs/issues/17416 for further discussion.
198 */
199 static int
zpl_sync_fs(struct super_block * sb,int wait)200 zpl_sync_fs(struct super_block *sb, int wait)
201 {
202 fstrans_cookie_t cookie;
203 cred_t *cr = CRED();
204 int error;
205
206 crhold(cr);
207 cookie = spl_fstrans_mark();
208 error = -zfs_sync(sb, wait, cr);
209
210 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 17, 0)
211 #ifdef HAVE_SUPER_BLOCK_S_WB_ERR
212 if (error && wait)
213 errseq_set(&sb->s_wb_err, error);
214 #else
215 if (error && wait) {
216 zfsvfs_t *zfsvfs = sb->s_fs_info;
217 ASSERT3P(zfsvfs, !=, NULL);
218 if (zfs_enter(zfsvfs, FTAG) == 0) {
219 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
220 zfs_exit(zfsvfs, FTAG);
221 error = 0;
222 }
223 }
224 #endif
225 #endif /* < 5.17.0 */
226
227 spl_fstrans_unmark(cookie);
228 crfree(cr);
229
230 ASSERT3S(error, <=, 0);
231 return (error);
232 }
233
234 static int
zpl_statfs(struct dentry * dentry,struct kstatfs * statp)235 zpl_statfs(struct dentry *dentry, struct kstatfs *statp)
236 {
237 fstrans_cookie_t cookie;
238 int error;
239
240 cookie = spl_fstrans_mark();
241 error = -zfs_statvfs(dentry->d_inode, statp);
242 spl_fstrans_unmark(cookie);
243 ASSERT3S(error, <=, 0);
244
245 /*
246 * If required by a 32-bit system call, dynamically scale the
247 * block size up to 16MiB and decrease the block counts. This
248 * allows for a maximum size of 64EiB to be reported. The file
249 * counts must be artificially capped at 2^32-1.
250 */
251 if (unlikely(zpl_is_32bit_api())) {
252 while (statp->f_blocks > UINT32_MAX &&
253 statp->f_bsize < SPA_MAXBLOCKSIZE) {
254 statp->f_frsize <<= 1;
255 statp->f_bsize <<= 1;
256
257 statp->f_blocks >>= 1;
258 statp->f_bfree >>= 1;
259 statp->f_bavail >>= 1;
260 }
261
262 uint64_t usedobjs = statp->f_files - statp->f_ffree;
263 statp->f_ffree = MIN(statp->f_ffree, UINT32_MAX - usedobjs);
264 statp->f_files = statp->f_ffree + usedobjs;
265 }
266
267 return (error);
268 }
269
270 static int
__zpl_show_devname(struct seq_file * seq,zfsvfs_t * zfsvfs)271 __zpl_show_devname(struct seq_file *seq, zfsvfs_t *zfsvfs)
272 {
273 int error;
274 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
275 return (error);
276
277 char *fsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
278 dmu_objset_name(zfsvfs->z_os, fsname);
279
280 for (int i = 0; fsname[i] != 0; i++) {
281 /*
282 * Spaces in the dataset name must be converted to their
283 * octal escape sequence for getmntent(3) to correctly
284 * parse then fsname portion of /proc/self/mounts.
285 */
286 if (fsname[i] == ' ') {
287 seq_puts(seq, "\\040");
288 } else {
289 seq_putc(seq, fsname[i]);
290 }
291 }
292
293 kmem_free(fsname, ZFS_MAX_DATASET_NAME_LEN);
294
295 zpl_exit(zfsvfs, FTAG);
296
297 return (0);
298 }
299
300 static int
zpl_show_devname(struct seq_file * seq,struct dentry * root)301 zpl_show_devname(struct seq_file *seq, struct dentry *root)
302 {
303 return (__zpl_show_devname(seq, root->d_sb->s_fs_info));
304 }
305
306 static int
__zpl_show_options(struct seq_file * seq,zfsvfs_t * zfsvfs)307 __zpl_show_options(struct seq_file *seq, zfsvfs_t *zfsvfs)
308 {
309 seq_printf(seq, ",%s",
310 zfsvfs->z_flags & ZSB_XATTR ? "xattr" : "noxattr");
311
312 #ifdef CONFIG_FS_POSIX_ACL
313 switch (zfsvfs->z_acl_type) {
314 case ZFS_ACLTYPE_POSIX:
315 seq_puts(seq, ",posixacl");
316 break;
317 default:
318 seq_puts(seq, ",noacl");
319 break;
320 }
321 #endif /* CONFIG_FS_POSIX_ACL */
322
323 switch (zfsvfs->z_case) {
324 case ZFS_CASE_SENSITIVE:
325 seq_puts(seq, ",casesensitive");
326 break;
327 case ZFS_CASE_INSENSITIVE:
328 seq_puts(seq, ",caseinsensitive");
329 break;
330 default:
331 seq_puts(seq, ",casemixed");
332 break;
333 }
334
335 return (0);
336 }
337
338 static int
zpl_show_options(struct seq_file * seq,struct dentry * root)339 zpl_show_options(struct seq_file *seq, struct dentry *root)
340 {
341 return (__zpl_show_options(seq, root->d_sb->s_fs_info));
342 }
343
344 static int
zpl_test_super(struct super_block * s,void * data)345 zpl_test_super(struct super_block *s, void *data)
346 {
347 zfsvfs_t *zfsvfs = s->s_fs_info;
348 objset_t *os = data;
349 /*
350 * If the os doesn't match the z_os in the super_block, assume it is
351 * not a match. Matching would imply a multimount of a dataset. It is
352 * possible that during a multimount, there is a simultaneous operation
353 * that changes the z_os, e.g., rollback, where the match will be
354 * missed, but in that case the user will get an EBUSY.
355 */
356 return (zfsvfs != NULL && os == zfsvfs->z_os);
357 }
358
359 static void
zpl_kill_sb(struct super_block * sb)360 zpl_kill_sb(struct super_block *sb)
361 {
362 zfs_preumount(sb);
363 kill_anon_super(sb);
364 }
365
366 void
zpl_prune_sb(uint64_t nr_to_scan,void * arg)367 zpl_prune_sb(uint64_t nr_to_scan, void *arg)
368 {
369 struct super_block *sb = (struct super_block *)arg;
370 int objects = 0;
371
372 /*
373 * Ensure the superblock is not in the process of being torn down.
374 */
375 #ifdef HAVE_SB_DYING
376 if (down_read_trylock(&sb->s_umount)) {
377 if (!(sb->s_flags & SB_DYING) && sb->s_root &&
378 (sb->s_flags & SB_BORN)) {
379 (void) zfs_prune(sb, nr_to_scan, &objects);
380 }
381 up_read(&sb->s_umount);
382 }
383 #else
384 if (down_read_trylock(&sb->s_umount)) {
385 if (!hlist_unhashed(&sb->s_instances) &&
386 sb->s_root && (sb->s_flags & SB_BORN)) {
387 (void) zfs_prune(sb, nr_to_scan, &objects);
388 }
389 up_read(&sb->s_umount);
390 }
391 #endif
392 }
393
394 /*
395 * Mount option parsing.
396 *
397 * The kernel receives a set of "stringy" mount options, typically a
398 * comma-separated list through mount(2) or fsconfig(2). These are split into a
399 * set of struct fs_parameter, and then vfs_parse_fs_param() is called for
400 * each. That function will handle (and consume) some options directly, and
401 * other subsystems (mainly security modules) are given the opportunity to
402 * consume them too. Any left over are passed to zpl_parse_param(). Our job is
403 * to use them to fill in the vfs_t we've attached previously to
404 * fc->fs_private, ready for the mount or remount call when it comes.
405 *
406 * Historically, mount options have been generated, removed, modified and
407 * otherwise complicated by multiple different actors over a long time: the
408 * kernel itself, the original mount(8) utility and later libmount,
409 * mount.zfs(8), libzfs and the ZFS tools that use it, and any program using
410 * the various mount APIs that have come and gone over the years. This is
411 * further complicated by cross-pollination between OpenSolaris/illumos, Linux
412 * and FreeBSD. Long story short: we could see all sorts of things, and we need
413 * to at least try not to break old userspace programs.
414 *
415 * At time of writing, this is my best understanding of all the options we
416 * might reasonably see, and where and how they're handled.
417 *
418 *
419 * These are common options for all filesystems that are processed by the
420 * kernel directly, without zpl_parse_param() being called. They're a bit of a
421 * mixed bag, but are ultimately all available to us via either sb->s_flags or
422 * fc->sb_flags:
423 *
424 * dirsync: set SB_DIRSYNC
425 * lazytime: set SB_LAZYTIME
426 * mand: set SB_MANDLOCK
427 * ro: set SB_RDONLY
428 * sync: set SB_SYNCHRONOUS
429 *
430 * async: clear SB_SYNCHRONOUS
431 * nolazytime: clear SB_LAZYTIME
432 * nomand: clear SB_MANDLOCK
433 * rw: clear SB_RDONLY
434 *
435 * Fortunately, almost all of these are handled directly by the kernel. 'mand'
436 * and 'nomand' are swallowed by the kernel ('mand' emits a warning in the
437 * kernel log), but it and the corresponding dataset property have been a no-op
438 * in OpenZFS for years, so there's nothing for us to do there.
439 *
440 * The only tricky one is SB_RDONLY ('ro'/'rw'), which can be both a mount and
441 * a superblock option. While we won't receive the "stringy" options, the
442 * kernel will set it for us in fc->sb_flags, and we've always had special
443 * handling for it at mount and remount time (eg handling snapshot mounts), so
444 * it's not a problem to do nothing here because we will sort it out later.
445 *
446 *
447 * These are options that we may receive as "stringy" options but also as mount
448 * flags.
449 *
450 * exec: clear MS_NOEXEC
451 * noexec: set MS_NOEXEC
452 * suid: clear MS_NOSUID
453 * nosuid: set MS_NOSUID
454 * dev: clear MS_NODEV
455 * nodev: set MS_NODEV
456 * atime: clear MS_NOATIME
457 * noatime: set MS_NOATIME
458 * relatime: set MS_RELATIME
459 * norelatime: clear MS_RELATIME
460 *
461 * In testing, it appears that recent libmount will convert them, but our own
462 * mount code (libzfs_mount) may not. We will be called for the stringy
463 * versions, but not for the flags. The flags will later be available on
464 * vfsmount->mnt_flags, not set on the vfs_t. This tends not to matter in
465 * practice, as almost all mounts come through libzfs (via zfs-mount(8) or
466 * mount.zfs(8)) and so as strings, and when they do come through flags, they
467 * will still be reported correctly via mountinfo and by zfs-get(8), which has
468 * special handling for "temporary" properties. Also, we never use these
469 * internally for any decisions; 'exec', 'suid' and 'dev' are handled in the
470 * kernel, and the kernel provides helpers for 'atime' and 'relatime'. The
471 * only place the difference is observable is through zfs_get_temporary_prop(),
472 * which is only used by the zfs.get_prop() Lua call.
473 *
474 * This is fixable by getting at vfsmount->mnt_flags, but this is not readily
475 * available until after the mount operation is completed, and with some
476 * effort. This is all very low impact, so it's left for future improvement.
477 *
478 *
479 * These are true OpenZFS-specific mount options. They give the equivalent
480 * of temporarily setting the pool properties as follows:
481 *
482 * strictatime atime=on, relatime=off
483 *
484 * xattr: xattr=sa
485 * saxattr: xattr=sa
486 * dirxattr: xattr=dir
487 * noxattr: xattr=off
488 *
489 *
490 * mntpoint= provides the canonical mount point for a snapshot mount. This
491 * is an assist for the snapshot automounter call out to userspace, to
492 * understand where the snapshot is mounted even when triggered from an
493 * alternate mount namespace (eg inside a chroot).
494 *
495 * mntpoint= vfs->vfs_mntpoint=...
496 *
497 *
498 * These are used for coordination inside libzfs, and should not make it
499 * to the kernel, but it does not strip them, so we handle them and ignore
500 * them.
501 *
502 * defaults
503 * zfsutil
504 * remount
505 *
506 *
507 * These are specific to SELinux. When that security module is running, it
508 * will consume them, but if not, they will be passed through to us. libzfs
509 * adds them unconditionally, so we will always see them when SELinux is not
510 * running, and ignore them.
511 *
512 * fscontext
513 * defcontext
514 * rootcontext
515 * context
516 *
517 *
518 * When preparing a remount, libmount will read /proc/self/mountinfo and add
519 * any unrecognised flags it finds there to the options. So, we have to accept
520 * anything that __zpl_show_options() can produce.
521 *
522 * posixacl
523 * noacl
524 * casesensitive
525 * caseinsensitive
526 * casemixed
527 *
528 *
529 * mount(8) has a notion of "sloppy" options. According to the documentation,
530 * when the -s switch is provided, unrecognised mount options will be ignored.
531 * Only the Linux NFS and SMB filesystems support it, and traditionally
532 * OpenZFS has too. however, it appears massively underspecified and
533 * inconsistent. Depending on the interplay between mount(8), the mount helper
534 * (eg mount.zfs(8)) and libmount, -s may cause unknown options to be filtered
535 * in userspace, _or_ an additional option 'sloppy' to be passed to the kernel
536 * either before or after the "unknown" option, _or_ nothing at all happens
537 * and the unknown option to be passed through to the kernel as-is. The
538 * kernel NFS and SMB filesystems both expect to see an explicit option
539 * 'sloppy' and use this to either ignore or reject unknown options, but as
540 * described, it's very easy for that option to not appear, or appear too late.
541 *
542 * OpenZFS has a test for this in the test suite, and it's documented in
543 * mount.zfs(8), so to support it we accept 'sloppy' and ignore it, and all
544 * other unknown options produce a notice in the kernel log, and are also
545 * ignored. This allows the "feature" to continue to work, while avoiding
546 * the additional housekeeping for the 'sloppy' option.
547 *
548 * sloppy
549 *
550 *
551 * Finally, all filesystems get automatic handling for the 'source' option,
552 * that is, the "name" of the filesystem (the first column of df(1)'s output).
553 * However, this only happens if the handler does not otherwise handle the
554 * 'source' option. Since we handle _all_ options because of 'sloppy', we have
555 * ot handle it ourselves. Normally we would call vfs_parse_fs_param_source()
556 * to deal with this, but that didn't appear until 5.14, and it's small enough
557 * that we can just handle it ourselves.
558 *
559 * source
560 *
561 *
562 * Thank you for reading this far. I hope you find what you are looking for,
563 * in this life or the next.
564 *
565 * -- robn, 2026-03-26
566 */
567
568 enum {
569 Opt_source,
570 Opt_exec, Opt_suid, Opt_dev,
571 Opt_atime, Opt_relatime, Opt_strictatime,
572 Opt_saxattr, Opt_dirxattr, Opt_noxattr,
573 Opt_mntpoint,
574
575 Opt_ignore, Opt_warn,
576 };
577
578 static const struct fs_parameter_spec zpl_param_spec[] = {
579 fsparam_string("source", Opt_source),
580
581 fsparam_flag_no("exec", Opt_exec),
582 fsparam_flag_no("suid", Opt_suid),
583 fsparam_flag_no("dev", Opt_dev),
584
585 fsparam_flag_no("atime", Opt_atime),
586 fsparam_flag_no("relatime", Opt_relatime),
587 fsparam_flag("strictatime", Opt_strictatime),
588
589 fsparam_flag("xattr", Opt_saxattr),
590 fsparam_flag("saxattr", Opt_saxattr),
591 fsparam_flag("dirxattr", Opt_dirxattr),
592 fsparam_flag("noxattr", Opt_noxattr),
593
594 fsparam_string("mntpoint", Opt_mntpoint),
595
596 fsparam_flag("defaults", Opt_ignore),
597 fsparam_flag("zfsutil", Opt_ignore),
598 fsparam_flag("remount", Opt_ignore),
599
600 fsparam_string("fscontext", Opt_ignore),
601 fsparam_string("defcontext", Opt_ignore),
602 fsparam_string("rootcontext", Opt_ignore),
603 fsparam_string("context", Opt_ignore),
604
605 fsparam_flag("posixacl", Opt_ignore),
606 fsparam_flag("noacl", Opt_ignore),
607 fsparam_flag("casesensitive", Opt_ignore),
608 fsparam_flag("caseinsensitive", Opt_ignore),
609 fsparam_flag("casemixed", Opt_ignore),
610
611 fsparam_flag("sloppy", Opt_ignore),
612
613 {}
614 };
615
616 /*
617 * Before 5.6, fs_parse() took a struct fs_parameter_description
618 * which wraps the parameter specs with name and enum pointers. From 5.6,
619 * the description struct was removed and fs_parse() accepts the
620 * fs_parameter_spec directly.
621 */
622 static int
zpl_fs_parse(struct fs_context * fc,struct fs_parameter * param,struct fs_parse_result * result)623 zpl_fs_parse(struct fs_context *fc, struct fs_parameter *param,
624 struct fs_parse_result *result)
625 {
626 #ifdef HAVE_FS_PARSE_TAKES_SPEC
627 return (fs_parse(fc, zpl_param_spec, param, result));
628 #else
629 static const struct fs_parameter_description zpl_param_desc = {
630 .name = "zfs",
631 .specs = zpl_param_spec,
632 };
633 return (fs_parse(fc, &zpl_param_desc, param, result));
634 #endif
635 }
636
637 static int
zpl_parse_param(struct fs_context * fc,struct fs_parameter * param)638 zpl_parse_param(struct fs_context *fc, struct fs_parameter *param)
639 {
640 vfs_t *vfs = fc->fs_private;
641
642 struct fs_parse_result result;
643 int opt = zpl_fs_parse(fc, param, &result);
644 if (opt == -ENOPARAM) {
645 /*
646 * Convert unknowns to warnings, to work around the whole
647 * "sloppy option" mess.
648 */
649 opt = Opt_warn;
650 }
651 if (opt < 0)
652 return (opt);
653
654 switch (opt) {
655 case Opt_source:
656 if (fc->source != NULL) {
657 cmn_err(CE_NOTE,
658 "ZFS: multiple 'source' options not supported");
659 return (-SET_ERROR(EINVAL));
660 }
661 fc->source = param->string;
662 param->string = NULL;
663 break;
664
665 case Opt_exec:
666 vfs->vfs_exec = !result.negated;
667 vfs->vfs_do_exec = B_TRUE;
668 break;
669 case Opt_suid:
670 vfs->vfs_setuid = !result.negated;
671 vfs->vfs_do_setuid = B_TRUE;
672 break;
673 case Opt_dev:
674 vfs->vfs_devices = !result.negated;
675 vfs->vfs_do_devices = B_TRUE;
676 break;
677
678 case Opt_atime:
679 vfs->vfs_atime = !result.negated;
680 vfs->vfs_do_atime = B_TRUE;
681 break;
682 case Opt_relatime:
683 vfs->vfs_relatime = !result.negated;
684 vfs->vfs_do_relatime = B_TRUE;
685 break;
686 case Opt_strictatime:
687 vfs->vfs_atime = B_TRUE;
688 vfs->vfs_do_atime = B_TRUE;
689 vfs->vfs_relatime = B_FALSE;
690 vfs->vfs_do_relatime = B_TRUE;
691 break;
692
693 case Opt_saxattr:
694 vfs->vfs_xattr = ZFS_XATTR_SA;
695 vfs->vfs_do_xattr = B_TRUE;
696 break;
697 case Opt_dirxattr:
698 vfs->vfs_xattr = ZFS_XATTR_DIR;
699 vfs->vfs_do_xattr = B_TRUE;
700 break;
701 case Opt_noxattr:
702 vfs->vfs_xattr = ZFS_XATTR_OFF;
703 vfs->vfs_do_xattr = B_TRUE;
704 break;
705
706 case Opt_mntpoint:
707 if (vfs->vfs_mntpoint != NULL)
708 kmem_strfree(vfs->vfs_mntpoint);
709 vfs->vfs_mntpoint = kmem_strdup(param->string);
710 break;
711
712 case Opt_ignore:
713 break;
714
715 case Opt_warn:
716 cmn_err(CE_NOTE,
717 "ZFS: ignoring unknown mount option: %s", param->key);
718 break;
719
720 default:
721 return (-SET_ERROR(EINVAL));
722 }
723
724 return (0);
725 }
726
727 /*
728 * Before Linux 5.8, the kernel's individual parameter parsing had a list of
729 * "forbidden" options that would always be rejected early. These were options
730 * that should be specified by MS_* flags, to be set on the superblock
731 * directly. However, it was inconsistently applied (eg it had various "*atime"
732 * options but not "atime", and also caused problems when it was not in sync
733 * with the version of libmount in use. It was deemed needlessly restrictive
734 * and was dropped in torvalds/linux@9193ae87a8af.
735 *
736 * Unfortunately, some of the options on this list are used by OpenZFS, so
737 * we need to see them. These include the aforementioned "*atime", "dev",
738 * "exec" and "suid".
739 *
740 * There is no easy compile-time check available to detect this, so we use
741 * a simple version check that should make it available everywhere needed,
742 * most notably RHEL8's 4.18+extras, which has backported fs_context support
743 * but does not include the 5.8 commit.
744 */
745 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 8, 0)
746 #define HAVE_FORBIDDEN_SB_FLAGS 1
747 #endif
748
749 #ifdef HAVE_FORBIDDEN_SB_FLAGS
750 /*
751 * The typical path for options parsing through mount(2) is:
752 *
753 * ksys_mount
754 * do_mount
755 * generic_parse_monolithic
756 * vfs_parse_fs_string
757 * vfs_parse_fs_param
758 * zpl_parse_param
759 *
760 * vfs_parse_fs_param() calls the internal vfs_parse_sb_flag(), which is
761 * where the "forbidden" flags are applied. If it makes it through there,
762 * it will later call fc->parse_param() ie zpl_parse_param(). We can't
763 * intercept this chain in the middle anywhere; the earliest thing we can
764 * override is generic_parse_monolithic(), substituting our own by setting
765 * fc->parse_monolithic and doing the parsing work ourselves.
766 *
767 * Fortunately, generic_parse_monolithic() is almost entirely splitting the
768 * incoming parameter string on comma and handing off to the rest of the
769 * pipeline. This is easily replaced (almost entirely by reviving a few bits
770 * of our old options parser).
771 *
772 * To keep the change as narrow as possible, we reuse zpl_param_spec and
773 * zpl_parse_param() as much as possible. Once we've parsed the option, we call
774 * fs_parse(zpl_param_spec) to find out if the option is actually one we
775 * explicitly care about. If it is, we call zpl_parse_param() directly,
776 * avoiding vfs_parse_fs_param() and so the risk of being rejected. If it is
777 * not one we explicitly care about, we call zpl_parse_param() as normal,
778 * letting the kernel reject it if it wishes. If it doesn't, it will end up
779 * back in zpl_parse_param() via fc->parse_param, and we can ignore or warn
780 * about it we normally would.
781 */
782 static int
zpl_parse_monolithic(struct fs_context * fc,void * data)783 zpl_parse_monolithic(struct fs_context *fc, void *data)
784 {
785 char *mntopts = data;
786
787 if (mntopts == NULL)
788 return (0);
789
790 /*
791 * Because we supply a .parse_monolithic callback, the kernel does
792 * no consideration of the options blob at all. Because of this, we
793 * have to give LSMs a first look at it. They will remove any options
794 * of interest to them (eg the SELinux *context= options).
795 */
796 int err = security_sb_eat_lsm_opts(mntopts, &fc->security);
797 if (err)
798 return (err);
799
800 char *key;
801 while ((key = strsep(&mntopts, ",")) != NULL) {
802 if (!*key)
803 continue;
804
805 struct fs_parameter param = {
806 .key = key,
807 };
808
809 char *value = strchr(key, '=');
810 if (value != NULL) {
811 /* Key starts with '='. Kernel ignores, we will too. */
812 if (value == key)
813 continue;
814 *value++ = '\0';
815
816 /* key=value is a "string" type, set up for that */
817 param.string = value;
818 param.type = fs_value_is_string;
819 param.size = strlen(value);
820 } else {
821 /* unadorned key is a "flag" type */
822 param.type = fs_value_is_flag;
823 }
824
825 /* Check if this is one of our options. */
826 struct fs_parse_result result;
827 int opt = zpl_fs_parse(fc, ¶m, &result);
828 if (opt >= 0) {
829 /*
830 * We already know this one of our options, so a
831 * failure here would be nonsensical.
832 */
833 VERIFY0(zpl_parse_param(fc, ¶m));
834 } else {
835 /*
836 * Not one of our option, send it through the kernel's
837 * standard parameter handling.
838 */
839 err = vfs_parse_fs_param(fc, ¶m);
840 if (err < 0)
841 return (err);
842 }
843 }
844
845 return (0);
846 }
847 #endif /* HAVE_FORBIDDEN_SB_FLAGS */
848
849 static int
zpl_get_tree(struct fs_context * fc)850 zpl_get_tree(struct fs_context *fc)
851 {
852 struct super_block *sb;
853 objset_t *os;
854 boolean_t issnap = B_FALSE;
855 int err;
856
857 err = dmu_objset_hold(fc->source, FTAG, &os);
858 if (err)
859 return (-err);
860
861 /*
862 * The dsl pool lock must be released prior to calling sget().
863 * It is possible sget() may block on the lock in grab_super()
864 * while deactivate_super() holds that same lock and waits for
865 * a txg sync. If the dsl_pool lock is held over sget()
866 * this can prevent the pool sync and cause a deadlock.
867 */
868 dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
869 dsl_pool_rele(dmu_objset_pool(os), FTAG);
870
871 sb = sget(fc->fs_type, zpl_test_super, set_anon_super,
872 fc->sb_flags, os);
873
874 /*
875 * Recheck with the lock held to prevent mounting the wrong dataset
876 * since z_os can be stale when the teardown lock is held.
877 *
878 * We can't do this in zpl_test_super in since it's under spinlock and
879 * also s_umount lock is not held there so it would race with
880 * zfs_umount and zfsvfs can be freed.
881 */
882 if (!IS_ERR(sb) && sb->s_fs_info != NULL) {
883 zfsvfs_t *zfsvfs = sb->s_fs_info;
884 if (zpl_enter(zfsvfs, FTAG) == 0) {
885 if (os != zfsvfs->z_os)
886 err = SET_ERROR(EBUSY);
887 issnap = zfsvfs->z_issnap;
888 zpl_exit(zfsvfs, FTAG);
889 } else {
890 err = SET_ERROR(EBUSY);
891 }
892 }
893 dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
894 dsl_dataset_rele(dmu_objset_ds(os), FTAG);
895
896 if (IS_ERR(sb))
897 return (PTR_ERR(sb));
898
899 if (err) {
900 deactivate_locked_super(sb);
901 return (-err);
902 }
903
904 if (sb->s_root == NULL) {
905 vfs_t *vfs = fc->fs_private;
906
907 /*
908 * If SB_RDONLY was set/cleared from mount options, update
909 * them in the options struct so we set up the filesystem
910 * in the proper state.
911 */
912 if (fc->sb_flags_mask & SB_RDONLY) {
913 vfs->vfs_readonly =
914 (fc->sb_flags & SB_RDONLY) ? B_TRUE : B_FALSE;
915 vfs->vfs_do_readonly = B_TRUE;
916 }
917
918 fstrans_cookie_t cookie = spl_fstrans_mark();
919 err = zfs_domount(sb, fc->source, vfs,
920 fc->sb_flags & SB_SILENT ? 1 : 0);
921 spl_fstrans_unmark(cookie);
922
923 if (err) {
924 deactivate_locked_super(sb);
925 return (-err);
926 }
927
928 /*
929 * zfsvfs has taken ownership of the mount options, so we
930 * need to ensure we don't free them.
931 */
932 fc->fs_private = NULL;
933
934 sb->s_flags |= SB_ACTIVE;
935 } else if (!issnap && ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY)) {
936 /*
937 * Skip ro check for snap since snap is always ro regardless
938 * ro flag is passed by mount or not.
939 */
940 deactivate_locked_super(sb);
941 return (-SET_ERROR(EBUSY));
942 }
943
944 struct dentry *root = dget(sb->s_root);
945 if (IS_ERR(root))
946 return (PTR_ERR(root));
947
948 fc->root = root;
949 return (0);
950 }
951
952 static int
zpl_reconfigure(struct fs_context * fc)953 zpl_reconfigure(struct fs_context *fc)
954 {
955 fstrans_cookie_t cookie;
956 int error;
957
958 cookie = spl_fstrans_mark();
959 error = -zfs_remount(fc->root->d_sb, fc->fs_private, fc->sb_flags);
960 spl_fstrans_unmark(cookie);
961 ASSERT3S(error, <=, 0);
962
963 if (error == 0) {
964 /*
965 * zfsvfs has taken ownership of the mount options, so we
966 * need to ensure we don't free them.
967 */
968 fc->fs_private = NULL;
969 }
970
971 return (error);
972 }
973
974 static int
zpl_dup_fc(struct fs_context * fc,struct fs_context * src_fc)975 zpl_dup_fc(struct fs_context *fc, struct fs_context *src_fc)
976 {
977 vfs_t *src_vfs = src_fc->fs_private;
978 if (src_vfs == NULL)
979 return (0);
980
981 vfs_t *vfs = zfsvfs_vfs_alloc();
982 if (vfs == NULL)
983 return (-SET_ERROR(ENOMEM));
984
985 /*
986 * This is annoying, but a straight memcpy() would require us to
987 * reinitialise the lock.
988 */
989 vfs->vfs_xattr = src_vfs->vfs_xattr;
990 vfs->vfs_readonly = src_vfs->vfs_readonly;
991 vfs->vfs_do_readonly = src_vfs->vfs_do_readonly;
992 vfs->vfs_setuid = src_vfs->vfs_setuid;
993 vfs->vfs_do_setuid = src_vfs->vfs_do_setuid;
994 vfs->vfs_exec = src_vfs->vfs_exec;
995 vfs->vfs_do_exec = src_vfs->vfs_do_exec;
996 vfs->vfs_devices = src_vfs->vfs_devices;
997 vfs->vfs_do_devices = src_vfs->vfs_do_devices;
998 vfs->vfs_do_xattr = src_vfs->vfs_do_xattr;
999 vfs->vfs_atime = src_vfs->vfs_atime;
1000 vfs->vfs_do_atime = src_vfs->vfs_do_atime;
1001 vfs->vfs_relatime = src_vfs->vfs_relatime;
1002 vfs->vfs_do_relatime = src_vfs->vfs_do_relatime;
1003 vfs->vfs_nbmand = src_vfs->vfs_nbmand;
1004 vfs->vfs_do_nbmand = src_vfs->vfs_do_nbmand;
1005
1006 mutex_enter(&src_vfs->vfs_mntpt_lock);
1007 if (src_vfs->vfs_mntpoint != NULL)
1008 vfs->vfs_mntpoint = kmem_strdup(src_vfs->vfs_mntpoint);
1009 mutex_exit(&src_vfs->vfs_mntpt_lock);
1010
1011 fc->fs_private = vfs;
1012 return (0);
1013 }
1014
1015 static void
zpl_free_fc(struct fs_context * fc)1016 zpl_free_fc(struct fs_context *fc)
1017 {
1018 zfsvfs_vfs_free(fc->fs_private);
1019 }
1020
1021 const struct fs_context_operations zpl_fs_context_operations = {
1022 #ifdef HAVE_FORBIDDEN_SB_FLAGS
1023 .parse_monolithic = zpl_parse_monolithic,
1024 #endif
1025 .parse_param = zpl_parse_param,
1026 .get_tree = zpl_get_tree,
1027 .reconfigure = zpl_reconfigure,
1028 .dup = zpl_dup_fc,
1029 .free = zpl_free_fc,
1030 };
1031
1032 static int
zpl_init_fs_context(struct fs_context * fc)1033 zpl_init_fs_context(struct fs_context *fc)
1034 {
1035 fc->fs_private = zfsvfs_vfs_alloc();
1036 if (fc->fs_private == NULL)
1037 return (-SET_ERROR(ENOMEM));
1038
1039 fc->ops = &zpl_fs_context_operations;
1040
1041 return (0);
1042 }
1043
1044 const struct super_operations zpl_super_operations = {
1045 .alloc_inode = zpl_inode_alloc,
1046 #ifdef HAVE_SOPS_FREE_INODE
1047 .free_inode = zpl_inode_free,
1048 #endif
1049 .destroy_inode = zpl_inode_destroy,
1050 .dirty_inode = zpl_dirty_inode,
1051 .write_inode = NULL,
1052 .drop_inode = zpl_drop_inode,
1053 .evict_inode = zpl_evict_inode,
1054 .put_super = zpl_put_super,
1055 .sync_fs = zpl_sync_fs,
1056 .statfs = zpl_statfs,
1057 .show_devname = zpl_show_devname,
1058 .show_options = zpl_show_options,
1059 .show_stats = NULL,
1060 };
1061
1062 /*
1063 * ->d_delete() is called when the last reference to a dentry is released. Its
1064 * return value indicates if the dentry should be destroyed immediately, or
1065 * retained in the dentry cache.
1066 *
1067 * By default (zfs_delete_dentry=0) the kernel will always cache unused
1068 * entries. Each dentry holds an inode reference, so cached dentries can hold
1069 * the final inode reference indefinitely, leading to the inode and its related
1070 * data being pinned (see zpl_drop_inode()).
1071 *
1072 * When set to 1, we signal that the dentry should be destroyed immediately and
1073 * never cached. This reduces memory usage, at the cost of higher overheads to
1074 * lookup a file, as the inode and its underlying data (dnode/dbuf) need to be
1075 * reloaded and reinflated.
1076 *
1077 * Note that userspace does not have direct control over dentry references and
1078 * reclaim; rather, this is part of the kernel's caching and reclaim subsystems
1079 * (eg vm.vfs_cache_pressure).
1080 */
1081 static int
zpl_dentry_delete(const struct dentry * dentry)1082 zpl_dentry_delete(const struct dentry *dentry)
1083 {
1084 return (zfs_delete_dentry ? 1 : 0);
1085 }
1086
1087 const struct dentry_operations zpl_dentry_operations = {
1088 .d_delete = zpl_dentry_delete,
1089 };
1090
1091 struct file_system_type zpl_fs_type = {
1092 .owner = THIS_MODULE,
1093 .name = ZFS_DRIVER,
1094 #if defined(HAVE_IDMAP_MNT_API)
1095 .fs_flags = FS_USERNS_MOUNT | FS_ALLOW_IDMAP,
1096 #else
1097 .fs_flags = FS_USERNS_MOUNT,
1098 #endif
1099 .init_fs_context = zpl_init_fs_context,
1100 .kill_sb = zpl_kill_sb,
1101 };
1102
1103 ZFS_MODULE_PARAM(zfs, zfs_, delete_inode, INT, ZMOD_RW,
1104 "Delete inodes as soon as the last reference is released.");
1105
1106 ZFS_MODULE_PARAM(zfs, zfs_, delete_dentry, INT, ZMOD_RW,
1107 "Delete dentries from dentry cache as soon as the last reference is "
1108 "released.");
1109