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