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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 */
26
27 /* Portions Copyright 2010 Robert Milkowski */
28
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/sysmacros.h>
32 #include <sys/kmem.h>
33 #include <sys/pathname.h>
34 #include <sys/vnode.h>
35 #include <sys/vfs.h>
36 #include <sys/mntent.h>
37 #include <sys/cmn_err.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_vnops.h>
40 #include <sys/zfs_dir.h>
41 #include <sys/zil.h>
42 #include <sys/fs/zfs.h>
43 #include <sys/dmu.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dsl_dataset.h>
46 #include <sys/dsl_deleg.h>
47 #include <sys/spa.h>
48 #include <sys/zap.h>
49 #include <sys/sa.h>
50 #include <sys/sa_impl.h>
51 #include <sys/policy.h>
52 #include <sys/atomic.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/zfs_ctldir.h>
55 #include <sys/zfs_fuid.h>
56 #include <sys/zfs_quota.h>
57 #include <sys/sunddi.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/dsl_dir.h>
60 #include <sys/objlist.h>
61 #include <sys/zfeature.h>
62 #include <sys/zpl.h>
63 #include <linux/vfs_compat.h>
64 #include <linux/fs.h>
65 #include "zfs_comutil.h"
66
67 enum {
68 TOKEN_RO,
69 TOKEN_RW,
70 TOKEN_SETUID,
71 TOKEN_NOSETUID,
72 TOKEN_EXEC,
73 TOKEN_NOEXEC,
74 TOKEN_DEVICES,
75 TOKEN_NODEVICES,
76 TOKEN_DIRXATTR,
77 TOKEN_SAXATTR,
78 TOKEN_XATTR,
79 TOKEN_NOXATTR,
80 TOKEN_ATIME,
81 TOKEN_NOATIME,
82 TOKEN_RELATIME,
83 TOKEN_NORELATIME,
84 TOKEN_NBMAND,
85 TOKEN_NONBMAND,
86 TOKEN_MNTPOINT,
87 TOKEN_LAST,
88 };
89
90 static const match_table_t zpl_tokens = {
91 { TOKEN_RO, MNTOPT_RO },
92 { TOKEN_RW, MNTOPT_RW },
93 { TOKEN_SETUID, MNTOPT_SETUID },
94 { TOKEN_NOSETUID, MNTOPT_NOSETUID },
95 { TOKEN_EXEC, MNTOPT_EXEC },
96 { TOKEN_NOEXEC, MNTOPT_NOEXEC },
97 { TOKEN_DEVICES, MNTOPT_DEVICES },
98 { TOKEN_NODEVICES, MNTOPT_NODEVICES },
99 { TOKEN_DIRXATTR, MNTOPT_DIRXATTR },
100 { TOKEN_SAXATTR, MNTOPT_SAXATTR },
101 { TOKEN_XATTR, MNTOPT_XATTR },
102 { TOKEN_NOXATTR, MNTOPT_NOXATTR },
103 { TOKEN_ATIME, MNTOPT_ATIME },
104 { TOKEN_NOATIME, MNTOPT_NOATIME },
105 { TOKEN_RELATIME, MNTOPT_RELATIME },
106 { TOKEN_NORELATIME, MNTOPT_NORELATIME },
107 { TOKEN_NBMAND, MNTOPT_NBMAND },
108 { TOKEN_NONBMAND, MNTOPT_NONBMAND },
109 { TOKEN_MNTPOINT, MNTOPT_MNTPOINT "=%s" },
110 { TOKEN_LAST, NULL },
111 };
112
113 static void
zfsvfs_vfs_free(vfs_t * vfsp)114 zfsvfs_vfs_free(vfs_t *vfsp)
115 {
116 if (vfsp != NULL) {
117 if (vfsp->vfs_mntpoint != NULL)
118 kmem_strfree(vfsp->vfs_mntpoint);
119 mutex_destroy(&vfsp->vfs_mntpt_lock);
120 kmem_free(vfsp, sizeof (vfs_t));
121 }
122 }
123
124 static int
zfsvfs_parse_option(char * option,int token,substring_t * args,vfs_t * vfsp)125 zfsvfs_parse_option(char *option, int token, substring_t *args, vfs_t *vfsp)
126 {
127 switch (token) {
128 case TOKEN_RO:
129 vfsp->vfs_readonly = B_TRUE;
130 vfsp->vfs_do_readonly = B_TRUE;
131 break;
132 case TOKEN_RW:
133 vfsp->vfs_readonly = B_FALSE;
134 vfsp->vfs_do_readonly = B_TRUE;
135 break;
136 case TOKEN_SETUID:
137 vfsp->vfs_setuid = B_TRUE;
138 vfsp->vfs_do_setuid = B_TRUE;
139 break;
140 case TOKEN_NOSETUID:
141 vfsp->vfs_setuid = B_FALSE;
142 vfsp->vfs_do_setuid = B_TRUE;
143 break;
144 case TOKEN_EXEC:
145 vfsp->vfs_exec = B_TRUE;
146 vfsp->vfs_do_exec = B_TRUE;
147 break;
148 case TOKEN_NOEXEC:
149 vfsp->vfs_exec = B_FALSE;
150 vfsp->vfs_do_exec = B_TRUE;
151 break;
152 case TOKEN_DEVICES:
153 vfsp->vfs_devices = B_TRUE;
154 vfsp->vfs_do_devices = B_TRUE;
155 break;
156 case TOKEN_NODEVICES:
157 vfsp->vfs_devices = B_FALSE;
158 vfsp->vfs_do_devices = B_TRUE;
159 break;
160 case TOKEN_DIRXATTR:
161 vfsp->vfs_xattr = ZFS_XATTR_DIR;
162 vfsp->vfs_do_xattr = B_TRUE;
163 break;
164 case TOKEN_SAXATTR:
165 vfsp->vfs_xattr = ZFS_XATTR_SA;
166 vfsp->vfs_do_xattr = B_TRUE;
167 break;
168 case TOKEN_XATTR:
169 vfsp->vfs_xattr = ZFS_XATTR_SA;
170 vfsp->vfs_do_xattr = B_TRUE;
171 break;
172 case TOKEN_NOXATTR:
173 vfsp->vfs_xattr = ZFS_XATTR_OFF;
174 vfsp->vfs_do_xattr = B_TRUE;
175 break;
176 case TOKEN_ATIME:
177 vfsp->vfs_atime = B_TRUE;
178 vfsp->vfs_do_atime = B_TRUE;
179 break;
180 case TOKEN_NOATIME:
181 vfsp->vfs_atime = B_FALSE;
182 vfsp->vfs_do_atime = B_TRUE;
183 break;
184 case TOKEN_RELATIME:
185 vfsp->vfs_relatime = B_TRUE;
186 vfsp->vfs_do_relatime = B_TRUE;
187 break;
188 case TOKEN_NORELATIME:
189 vfsp->vfs_relatime = B_FALSE;
190 vfsp->vfs_do_relatime = B_TRUE;
191 break;
192 case TOKEN_NBMAND:
193 vfsp->vfs_nbmand = B_TRUE;
194 vfsp->vfs_do_nbmand = B_TRUE;
195 break;
196 case TOKEN_NONBMAND:
197 vfsp->vfs_nbmand = B_FALSE;
198 vfsp->vfs_do_nbmand = B_TRUE;
199 break;
200 case TOKEN_MNTPOINT:
201 if (vfsp->vfs_mntpoint != NULL)
202 kmem_strfree(vfsp->vfs_mntpoint);
203 vfsp->vfs_mntpoint = match_strdup(&args[0]);
204 if (vfsp->vfs_mntpoint == NULL)
205 return (SET_ERROR(ENOMEM));
206 break;
207 default:
208 break;
209 }
210
211 return (0);
212 }
213
214 /*
215 * Parse the raw mntopts and return a vfs_t describing the options.
216 */
217 static int
zfsvfs_parse_options(char * mntopts,vfs_t ** vfsp)218 zfsvfs_parse_options(char *mntopts, vfs_t **vfsp)
219 {
220 vfs_t *tmp_vfsp;
221 int error;
222
223 tmp_vfsp = kmem_zalloc(sizeof (vfs_t), KM_SLEEP);
224 mutex_init(&tmp_vfsp->vfs_mntpt_lock, NULL, MUTEX_DEFAULT, NULL);
225
226 if (mntopts != NULL) {
227 substring_t args[MAX_OPT_ARGS];
228 char *tmp_mntopts, *p, *t;
229 int token;
230
231 tmp_mntopts = t = kmem_strdup(mntopts);
232 if (tmp_mntopts == NULL)
233 return (SET_ERROR(ENOMEM));
234
235 while ((p = strsep(&t, ",")) != NULL) {
236 if (!*p)
237 continue;
238
239 args[0].to = args[0].from = NULL;
240 token = match_token(p, zpl_tokens, args);
241 error = zfsvfs_parse_option(p, token, args, tmp_vfsp);
242 if (error) {
243 kmem_strfree(tmp_mntopts);
244 zfsvfs_vfs_free(tmp_vfsp);
245 return (error);
246 }
247 }
248
249 kmem_strfree(tmp_mntopts);
250 }
251
252 *vfsp = tmp_vfsp;
253
254 return (0);
255 }
256
257 boolean_t
zfs_is_readonly(zfsvfs_t * zfsvfs)258 zfs_is_readonly(zfsvfs_t *zfsvfs)
259 {
260 return (!!(zfsvfs->z_sb->s_flags & SB_RDONLY));
261 }
262
263 int
zfs_sync(struct super_block * sb,int wait,cred_t * cr)264 zfs_sync(struct super_block *sb, int wait, cred_t *cr)
265 {
266 (void) cr;
267 zfsvfs_t *zfsvfs = sb->s_fs_info;
268
269 /*
270 * Semantically, the only requirement is that the sync be initiated.
271 * The DMU syncs out txgs frequently, so there's nothing to do.
272 */
273 if (!wait)
274 return (0);
275
276 if (zfsvfs != NULL) {
277 /*
278 * Sync a specific filesystem.
279 */
280 dsl_pool_t *dp;
281 int error;
282
283 if ((error = zfs_enter(zfsvfs, FTAG)) != 0)
284 return (error);
285 dp = dmu_objset_pool(zfsvfs->z_os);
286
287 /*
288 * If the system is shutting down, then skip any
289 * filesystems which may exist on a suspended pool.
290 */
291 if (spa_suspended(dp->dp_spa)) {
292 zfs_exit(zfsvfs, FTAG);
293 return (0);
294 }
295
296 if (zfsvfs->z_log != NULL)
297 zil_commit(zfsvfs->z_log, 0);
298
299 zfs_exit(zfsvfs, FTAG);
300 } else {
301 /*
302 * Sync all ZFS filesystems. This is what happens when you
303 * run sync(1). Unlike other filesystems, ZFS honors the
304 * request by waiting for all pools to commit all dirty data.
305 */
306 spa_sync_allpools();
307 }
308
309 return (0);
310 }
311
312 static void
atime_changed_cb(void * arg,uint64_t newval)313 atime_changed_cb(void *arg, uint64_t newval)
314 {
315 zfsvfs_t *zfsvfs = arg;
316 struct super_block *sb = zfsvfs->z_sb;
317
318 if (sb == NULL)
319 return;
320 /*
321 * Update SB_NOATIME bit in VFS super block. Since atime update is
322 * determined by atime_needs_update(), atime_needs_update() needs to
323 * return false if atime is turned off, and not unconditionally return
324 * false if atime is turned on.
325 */
326 if (newval)
327 sb->s_flags &= ~SB_NOATIME;
328 else
329 sb->s_flags |= SB_NOATIME;
330 }
331
332 static void
relatime_changed_cb(void * arg,uint64_t newval)333 relatime_changed_cb(void *arg, uint64_t newval)
334 {
335 ((zfsvfs_t *)arg)->z_relatime = newval;
336 }
337
338 static void
xattr_changed_cb(void * arg,uint64_t newval)339 xattr_changed_cb(void *arg, uint64_t newval)
340 {
341 zfsvfs_t *zfsvfs = arg;
342
343 if (newval == ZFS_XATTR_OFF) {
344 zfsvfs->z_flags &= ~ZSB_XATTR;
345 } else {
346 zfsvfs->z_flags |= ZSB_XATTR;
347
348 if (newval == ZFS_XATTR_SA)
349 zfsvfs->z_xattr_sa = B_TRUE;
350 else
351 zfsvfs->z_xattr_sa = B_FALSE;
352 }
353 }
354
355 static void
acltype_changed_cb(void * arg,uint64_t newval)356 acltype_changed_cb(void *arg, uint64_t newval)
357 {
358 zfsvfs_t *zfsvfs = arg;
359
360 switch (newval) {
361 case ZFS_ACLTYPE_NFSV4:
362 case ZFS_ACLTYPE_OFF:
363 zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF;
364 zfsvfs->z_sb->s_flags &= ~SB_POSIXACL;
365 break;
366 case ZFS_ACLTYPE_POSIX:
367 #ifdef CONFIG_FS_POSIX_ACL
368 zfsvfs->z_acl_type = ZFS_ACLTYPE_POSIX;
369 zfsvfs->z_sb->s_flags |= SB_POSIXACL;
370 #else
371 zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF;
372 zfsvfs->z_sb->s_flags &= ~SB_POSIXACL;
373 #endif /* CONFIG_FS_POSIX_ACL */
374 break;
375 default:
376 break;
377 }
378 }
379
380 static void
blksz_changed_cb(void * arg,uint64_t newval)381 blksz_changed_cb(void *arg, uint64_t newval)
382 {
383 zfsvfs_t *zfsvfs = arg;
384 ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
385 ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
386 ASSERT(ISP2(newval));
387
388 zfsvfs->z_max_blksz = newval;
389 }
390
391 static void
readonly_changed_cb(void * arg,uint64_t newval)392 readonly_changed_cb(void *arg, uint64_t newval)
393 {
394 zfsvfs_t *zfsvfs = arg;
395 struct super_block *sb = zfsvfs->z_sb;
396
397 if (sb == NULL)
398 return;
399
400 if (newval)
401 sb->s_flags |= SB_RDONLY;
402 else
403 sb->s_flags &= ~SB_RDONLY;
404 }
405
406 static void
devices_changed_cb(void * arg,uint64_t newval)407 devices_changed_cb(void *arg, uint64_t newval)
408 {
409 }
410
411 static void
setuid_changed_cb(void * arg,uint64_t newval)412 setuid_changed_cb(void *arg, uint64_t newval)
413 {
414 }
415
416 static void
exec_changed_cb(void * arg,uint64_t newval)417 exec_changed_cb(void *arg, uint64_t newval)
418 {
419 }
420
421 static void
nbmand_changed_cb(void * arg,uint64_t newval)422 nbmand_changed_cb(void *arg, uint64_t newval)
423 {
424 zfsvfs_t *zfsvfs = arg;
425 struct super_block *sb = zfsvfs->z_sb;
426
427 if (sb == NULL)
428 return;
429
430 if (newval == TRUE)
431 sb->s_flags |= SB_MANDLOCK;
432 else
433 sb->s_flags &= ~SB_MANDLOCK;
434 }
435
436 static void
snapdir_changed_cb(void * arg,uint64_t newval)437 snapdir_changed_cb(void *arg, uint64_t newval)
438 {
439 ((zfsvfs_t *)arg)->z_show_ctldir = newval;
440 }
441
442 static void
acl_mode_changed_cb(void * arg,uint64_t newval)443 acl_mode_changed_cb(void *arg, uint64_t newval)
444 {
445 zfsvfs_t *zfsvfs = arg;
446
447 zfsvfs->z_acl_mode = newval;
448 }
449
450 static void
acl_inherit_changed_cb(void * arg,uint64_t newval)451 acl_inherit_changed_cb(void *arg, uint64_t newval)
452 {
453 ((zfsvfs_t *)arg)->z_acl_inherit = newval;
454 }
455
456 static void
longname_changed_cb(void * arg,uint64_t newval)457 longname_changed_cb(void *arg, uint64_t newval)
458 {
459 ((zfsvfs_t *)arg)->z_longname = newval;
460 }
461
462 static int
zfs_register_callbacks(vfs_t * vfsp)463 zfs_register_callbacks(vfs_t *vfsp)
464 {
465 struct dsl_dataset *ds = NULL;
466 objset_t *os = NULL;
467 zfsvfs_t *zfsvfs = NULL;
468 int error = 0;
469
470 ASSERT(vfsp);
471 zfsvfs = vfsp->vfs_data;
472 ASSERT(zfsvfs);
473 os = zfsvfs->z_os;
474
475 /*
476 * The act of registering our callbacks will destroy any mount
477 * options we may have. In order to enable temporary overrides
478 * of mount options, we stash away the current values and
479 * restore them after we register the callbacks.
480 */
481 if (zfs_is_readonly(zfsvfs) || !spa_writeable(dmu_objset_spa(os))) {
482 vfsp->vfs_do_readonly = B_TRUE;
483 vfsp->vfs_readonly = B_TRUE;
484 }
485
486 /*
487 * Register property callbacks.
488 *
489 * It would probably be fine to just check for i/o error from
490 * the first prop_register(), but I guess I like to go
491 * overboard...
492 */
493 ds = dmu_objset_ds(os);
494 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
495 error = dsl_prop_register(ds,
496 zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
497 error = error ? error : dsl_prop_register(ds,
498 zfs_prop_to_name(ZFS_PROP_RELATIME), relatime_changed_cb, zfsvfs);
499 error = error ? error : dsl_prop_register(ds,
500 zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
501 error = error ? error : dsl_prop_register(ds,
502 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
503 error = error ? error : dsl_prop_register(ds,
504 zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
505 error = error ? error : dsl_prop_register(ds,
506 zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
507 error = error ? error : dsl_prop_register(ds,
508 zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
509 error = error ? error : dsl_prop_register(ds,
510 zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
511 error = error ? error : dsl_prop_register(ds,
512 zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
513 error = error ? error : dsl_prop_register(ds,
514 zfs_prop_to_name(ZFS_PROP_ACLTYPE), acltype_changed_cb, zfsvfs);
515 error = error ? error : dsl_prop_register(ds,
516 zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
517 error = error ? error : dsl_prop_register(ds,
518 zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
519 zfsvfs);
520 error = error ? error : dsl_prop_register(ds,
521 zfs_prop_to_name(ZFS_PROP_NBMAND), nbmand_changed_cb, zfsvfs);
522 error = error ? error : dsl_prop_register(ds,
523 zfs_prop_to_name(ZFS_PROP_LONGNAME), longname_changed_cb, zfsvfs);
524 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
525 if (error)
526 goto unregister;
527
528 /*
529 * Invoke our callbacks to restore temporary mount options.
530 */
531 if (vfsp->vfs_do_readonly)
532 readonly_changed_cb(zfsvfs, vfsp->vfs_readonly);
533 if (vfsp->vfs_do_setuid)
534 setuid_changed_cb(zfsvfs, vfsp->vfs_setuid);
535 if (vfsp->vfs_do_exec)
536 exec_changed_cb(zfsvfs, vfsp->vfs_exec);
537 if (vfsp->vfs_do_devices)
538 devices_changed_cb(zfsvfs, vfsp->vfs_devices);
539 if (vfsp->vfs_do_xattr)
540 xattr_changed_cb(zfsvfs, vfsp->vfs_xattr);
541 if (vfsp->vfs_do_atime)
542 atime_changed_cb(zfsvfs, vfsp->vfs_atime);
543 if (vfsp->vfs_do_relatime)
544 relatime_changed_cb(zfsvfs, vfsp->vfs_relatime);
545 if (vfsp->vfs_do_nbmand)
546 nbmand_changed_cb(zfsvfs, vfsp->vfs_nbmand);
547
548 return (0);
549
550 unregister:
551 dsl_prop_unregister_all(ds, zfsvfs);
552 return (error);
553 }
554
555 /*
556 * Takes a dataset, a property, a value and that value's setpoint as
557 * found in the ZAP. Checks if the property has been changed in the vfs.
558 * If so, val and setpoint will be overwritten with updated content.
559 * Otherwise, they are left unchanged.
560 */
561 int
zfs_get_temporary_prop(dsl_dataset_t * ds,zfs_prop_t zfs_prop,uint64_t * val,char * setpoint)562 zfs_get_temporary_prop(dsl_dataset_t *ds, zfs_prop_t zfs_prop, uint64_t *val,
563 char *setpoint)
564 {
565 int error;
566 zfsvfs_t *zfvp;
567 vfs_t *vfsp;
568 objset_t *os;
569 uint64_t tmp = *val;
570
571 error = dmu_objset_from_ds(ds, &os);
572 if (error != 0)
573 return (error);
574
575 if (dmu_objset_type(os) != DMU_OST_ZFS)
576 return (EINVAL);
577
578 mutex_enter(&os->os_user_ptr_lock);
579 zfvp = dmu_objset_get_user(os);
580 mutex_exit(&os->os_user_ptr_lock);
581 if (zfvp == NULL)
582 return (ESRCH);
583
584 vfsp = zfvp->z_vfs;
585
586 switch (zfs_prop) {
587 case ZFS_PROP_ATIME:
588 if (vfsp->vfs_do_atime)
589 tmp = vfsp->vfs_atime;
590 break;
591 case ZFS_PROP_RELATIME:
592 if (vfsp->vfs_do_relatime)
593 tmp = vfsp->vfs_relatime;
594 break;
595 case ZFS_PROP_DEVICES:
596 if (vfsp->vfs_do_devices)
597 tmp = vfsp->vfs_devices;
598 break;
599 case ZFS_PROP_EXEC:
600 if (vfsp->vfs_do_exec)
601 tmp = vfsp->vfs_exec;
602 break;
603 case ZFS_PROP_SETUID:
604 if (vfsp->vfs_do_setuid)
605 tmp = vfsp->vfs_setuid;
606 break;
607 case ZFS_PROP_READONLY:
608 if (vfsp->vfs_do_readonly)
609 tmp = vfsp->vfs_readonly;
610 break;
611 case ZFS_PROP_XATTR:
612 if (vfsp->vfs_do_xattr)
613 tmp = vfsp->vfs_xattr;
614 break;
615 case ZFS_PROP_NBMAND:
616 if (vfsp->vfs_do_nbmand)
617 tmp = vfsp->vfs_nbmand;
618 break;
619 default:
620 return (ENOENT);
621 }
622
623 if (tmp != *val) {
624 if (setpoint)
625 (void) strcpy(setpoint, "temporary");
626 *val = tmp;
627 }
628 return (0);
629 }
630
631 /*
632 * Associate this zfsvfs with the given objset, which must be owned.
633 * This will cache a bunch of on-disk state from the objset in the
634 * zfsvfs.
635 */
636 static int
zfsvfs_init(zfsvfs_t * zfsvfs,objset_t * os)637 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
638 {
639 int error;
640 uint64_t val;
641
642 zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
643 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
644 zfsvfs->z_os = os;
645
646 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
647 if (error != 0)
648 return (error);
649 if (zfsvfs->z_version >
650 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
651 (void) printk("Can't mount a version %lld file system "
652 "on a version %lld pool\n. Pool must be upgraded to mount "
653 "this file system.\n", (u_longlong_t)zfsvfs->z_version,
654 (u_longlong_t)spa_version(dmu_objset_spa(os)));
655 return (SET_ERROR(ENOTSUP));
656 }
657 error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
658 if (error != 0)
659 return (error);
660 zfsvfs->z_norm = (int)val;
661
662 error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
663 if (error != 0)
664 return (error);
665 zfsvfs->z_utf8 = (val != 0);
666
667 error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
668 if (error != 0)
669 return (error);
670 zfsvfs->z_case = (uint_t)val;
671
672 if ((error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &val)) != 0)
673 return (error);
674 zfsvfs->z_acl_type = (uint_t)val;
675
676 /*
677 * Fold case on file systems that are always or sometimes case
678 * insensitive.
679 */
680 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
681 zfsvfs->z_case == ZFS_CASE_MIXED)
682 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
683
684 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
685 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
686
687 uint64_t sa_obj = 0;
688 if (zfsvfs->z_use_sa) {
689 /* should either have both of these objects or none */
690 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
691 &sa_obj);
692 if (error != 0)
693 return (error);
694
695 error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &val);
696 if ((error == 0) && (val == ZFS_XATTR_SA))
697 zfsvfs->z_xattr_sa = B_TRUE;
698 }
699
700 error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTUSERQUOTA,
701 &zfsvfs->z_defaultuserquota);
702 if (error != 0)
703 return (error);
704
705 error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTGROUPQUOTA,
706 &zfsvfs->z_defaultgroupquota);
707 if (error != 0)
708 return (error);
709
710 error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTPROJECTQUOTA,
711 &zfsvfs->z_defaultprojectquota);
712 if (error != 0)
713 return (error);
714
715 error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTUSEROBJQUOTA,
716 &zfsvfs->z_defaultuserobjquota);
717 if (error != 0)
718 return (error);
719
720 error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTGROUPOBJQUOTA,
721 &zfsvfs->z_defaultgroupobjquota);
722 if (error != 0)
723 return (error);
724
725 error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTPROJECTOBJQUOTA,
726 &zfsvfs->z_defaultprojectobjquota);
727 if (error != 0)
728 return (error);
729
730 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
731 &zfsvfs->z_root);
732 if (error != 0)
733 return (error);
734 ASSERT(zfsvfs->z_root != 0);
735
736 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
737 &zfsvfs->z_unlinkedobj);
738 if (error != 0)
739 return (error);
740
741 error = zap_lookup(os, MASTER_NODE_OBJ,
742 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
743 8, 1, &zfsvfs->z_userquota_obj);
744 if (error == ENOENT)
745 zfsvfs->z_userquota_obj = 0;
746 else if (error != 0)
747 return (error);
748
749 error = zap_lookup(os, MASTER_NODE_OBJ,
750 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
751 8, 1, &zfsvfs->z_groupquota_obj);
752 if (error == ENOENT)
753 zfsvfs->z_groupquota_obj = 0;
754 else if (error != 0)
755 return (error);
756
757 error = zap_lookup(os, MASTER_NODE_OBJ,
758 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
759 8, 1, &zfsvfs->z_projectquota_obj);
760 if (error == ENOENT)
761 zfsvfs->z_projectquota_obj = 0;
762 else if (error != 0)
763 return (error);
764
765 error = zap_lookup(os, MASTER_NODE_OBJ,
766 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
767 8, 1, &zfsvfs->z_userobjquota_obj);
768 if (error == ENOENT)
769 zfsvfs->z_userobjquota_obj = 0;
770 else if (error != 0)
771 return (error);
772
773 error = zap_lookup(os, MASTER_NODE_OBJ,
774 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
775 8, 1, &zfsvfs->z_groupobjquota_obj);
776 if (error == ENOENT)
777 zfsvfs->z_groupobjquota_obj = 0;
778 else if (error != 0)
779 return (error);
780
781 error = zap_lookup(os, MASTER_NODE_OBJ,
782 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
783 8, 1, &zfsvfs->z_projectobjquota_obj);
784 if (error == ENOENT)
785 zfsvfs->z_projectobjquota_obj = 0;
786 else if (error != 0)
787 return (error);
788
789 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
790 &zfsvfs->z_fuid_obj);
791 if (error == ENOENT)
792 zfsvfs->z_fuid_obj = 0;
793 else if (error != 0)
794 return (error);
795
796 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
797 &zfsvfs->z_shares_dir);
798 if (error == ENOENT)
799 zfsvfs->z_shares_dir = 0;
800 else if (error != 0)
801 return (error);
802
803 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
804 &zfsvfs->z_attr_table);
805 if (error != 0)
806 return (error);
807
808 if (zfsvfs->z_version >= ZPL_VERSION_SA)
809 sa_register_update_callback(os, zfs_sa_upgrade);
810
811 return (0);
812 }
813
814 int
zfsvfs_create(const char * osname,boolean_t readonly,zfsvfs_t ** zfvp)815 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
816 {
817 objset_t *os;
818 zfsvfs_t *zfsvfs;
819 int error;
820 boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
821
822 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
823
824 error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs, &os);
825 if (error != 0) {
826 kmem_free(zfsvfs, sizeof (zfsvfs_t));
827 return (error);
828 }
829
830 error = zfsvfs_create_impl(zfvp, zfsvfs, os);
831
832 return (error);
833 }
834
835
836 /*
837 * Note: zfsvfs is assumed to be malloc'd, and will be freed by this function
838 * on a failure. Do not pass in a statically allocated zfsvfs.
839 */
840 int
zfsvfs_create_impl(zfsvfs_t ** zfvp,zfsvfs_t * zfsvfs,objset_t * os)841 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
842 {
843 int error;
844
845 zfsvfs->z_vfs = NULL;
846 zfsvfs->z_sb = NULL;
847 zfsvfs->z_parent = zfsvfs;
848
849 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
850 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
851 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
852 offsetof(znode_t, z_link_node));
853 ZFS_TEARDOWN_INIT(zfsvfs);
854 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
855 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
856
857 int size = MIN(1 << (highbit64(zfs_object_mutex_size) - 1),
858 ZFS_OBJ_MTX_MAX);
859 zfsvfs->z_hold_size = size;
860 zfsvfs->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size,
861 KM_SLEEP);
862 zfsvfs->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
863 for (int i = 0; i != size; i++) {
864 avl_create(&zfsvfs->z_hold_trees[i], zfs_znode_hold_compare,
865 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
866 mutex_init(&zfsvfs->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
867 }
868
869 error = zfsvfs_init(zfsvfs, os);
870 if (error != 0) {
871 dmu_objset_disown(os, B_TRUE, zfsvfs);
872 *zfvp = NULL;
873 zfsvfs_free(zfsvfs);
874 return (error);
875 }
876
877 zfsvfs->z_drain_task = TASKQID_INVALID;
878 zfsvfs->z_draining = B_FALSE;
879 zfsvfs->z_drain_cancel = B_TRUE;
880
881 *zfvp = zfsvfs;
882 return (0);
883 }
884
885 static int
zfsvfs_setup(zfsvfs_t * zfsvfs,boolean_t mounting)886 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
887 {
888 int error;
889 boolean_t readonly = zfs_is_readonly(zfsvfs);
890
891 error = zfs_register_callbacks(zfsvfs->z_vfs);
892 if (error)
893 return (error);
894
895 /*
896 * If we are not mounting (ie: online recv), then we don't
897 * have to worry about replaying the log as we blocked all
898 * operations out since we closed the ZIL.
899 */
900 if (mounting) {
901 ASSERT3P(zfsvfs->z_kstat.dk_kstats, ==, NULL);
902 error = dataset_kstats_create(&zfsvfs->z_kstat, zfsvfs->z_os);
903 if (error)
904 return (error);
905 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data,
906 &zfsvfs->z_kstat.dk_zil_sums);
907
908 /*
909 * During replay we remove the read only flag to
910 * allow replays to succeed.
911 */
912 if (readonly != 0) {
913 readonly_changed_cb(zfsvfs, B_FALSE);
914 } else {
915 zap_stats_t zs;
916 if (zap_get_stats(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
917 &zs) == 0) {
918 dataset_kstats_update_nunlinks_kstat(
919 &zfsvfs->z_kstat, zs.zs_num_entries);
920 dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
921 "num_entries in unlinked set: %llu",
922 zs.zs_num_entries);
923 }
924 zfs_unlinked_drain(zfsvfs);
925 dsl_dir_t *dd = zfsvfs->z_os->os_dsl_dataset->ds_dir;
926 dd->dd_activity_cancelled = B_FALSE;
927 }
928
929 /*
930 * Parse and replay the intent log.
931 *
932 * Because of ziltest, this must be done after
933 * zfs_unlinked_drain(). (Further note: ziltest
934 * doesn't use readonly mounts, where
935 * zfs_unlinked_drain() isn't called.) This is because
936 * ziltest causes spa_sync() to think it's committed,
937 * but actually it is not, so the intent log contains
938 * many txg's worth of changes.
939 *
940 * In particular, if object N is in the unlinked set in
941 * the last txg to actually sync, then it could be
942 * actually freed in a later txg and then reallocated
943 * in a yet later txg. This would write a "create
944 * object N" record to the intent log. Normally, this
945 * would be fine because the spa_sync() would have
946 * written out the fact that object N is free, before
947 * we could write the "create object N" intent log
948 * record.
949 *
950 * But when we are in ziltest mode, we advance the "open
951 * txg" without actually spa_sync()-ing the changes to
952 * disk. So we would see that object N is still
953 * allocated and in the unlinked set, and there is an
954 * intent log record saying to allocate it.
955 */
956 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
957 if (zil_replay_disable) {
958 zil_destroy(zfsvfs->z_log, B_FALSE);
959 } else {
960 zfsvfs->z_replay = B_TRUE;
961 zil_replay(zfsvfs->z_os, zfsvfs,
962 zfs_replay_vector);
963 zfsvfs->z_replay = B_FALSE;
964 }
965 }
966
967 /* restore readonly bit */
968 if (readonly != 0)
969 readonly_changed_cb(zfsvfs, B_TRUE);
970 } else {
971 ASSERT3P(zfsvfs->z_kstat.dk_kstats, !=, NULL);
972 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data,
973 &zfsvfs->z_kstat.dk_zil_sums);
974 }
975
976 /*
977 * Set the objset user_ptr to track its zfsvfs.
978 */
979 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
980 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
981 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
982
983 return (0);
984 }
985
986 void
zfsvfs_free(zfsvfs_t * zfsvfs)987 zfsvfs_free(zfsvfs_t *zfsvfs)
988 {
989 int i, size = zfsvfs->z_hold_size;
990
991 zfs_fuid_destroy(zfsvfs);
992
993 mutex_destroy(&zfsvfs->z_znodes_lock);
994 mutex_destroy(&zfsvfs->z_lock);
995 list_destroy(&zfsvfs->z_all_znodes);
996 ZFS_TEARDOWN_DESTROY(zfsvfs);
997 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
998 rw_destroy(&zfsvfs->z_fuid_lock);
999 for (i = 0; i != size; i++) {
1000 avl_destroy(&zfsvfs->z_hold_trees[i]);
1001 mutex_destroy(&zfsvfs->z_hold_locks[i]);
1002 }
1003 vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size);
1004 vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size);
1005 zfsvfs_vfs_free(zfsvfs->z_vfs);
1006 dataset_kstats_destroy(&zfsvfs->z_kstat);
1007 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1008 }
1009
1010 static void
zfs_set_fuid_feature(zfsvfs_t * zfsvfs)1011 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1012 {
1013 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1014 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1015 }
1016
1017 static void
zfs_unregister_callbacks(zfsvfs_t * zfsvfs)1018 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1019 {
1020 objset_t *os = zfsvfs->z_os;
1021
1022 if (!dmu_objset_is_snapshot(os))
1023 dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1024 }
1025
1026 #ifdef HAVE_MLSLABEL
1027 /*
1028 * Check that the hex label string is appropriate for the dataset being
1029 * mounted into the global_zone proper.
1030 *
1031 * Return an error if the hex label string is not default or
1032 * admin_low/admin_high. For admin_low labels, the corresponding
1033 * dataset must be readonly.
1034 */
1035 int
zfs_check_global_label(const char * dsname,const char * hexsl)1036 zfs_check_global_label(const char *dsname, const char *hexsl)
1037 {
1038 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1039 return (0);
1040 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1041 return (0);
1042 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1043 /* must be readonly */
1044 uint64_t rdonly;
1045
1046 if (dsl_prop_get_integer(dsname,
1047 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1048 return (SET_ERROR(EACCES));
1049 return (rdonly ? 0 : SET_ERROR(EACCES));
1050 }
1051 return (SET_ERROR(EACCES));
1052 }
1053 #endif /* HAVE_MLSLABEL */
1054
1055 static int
zfs_statfs_project(zfsvfs_t * zfsvfs,znode_t * zp,struct kstatfs * statp,uint32_t bshift)1056 zfs_statfs_project(zfsvfs_t *zfsvfs, znode_t *zp, struct kstatfs *statp,
1057 uint32_t bshift)
1058 {
1059 char buf[20 + DMU_OBJACCT_PREFIX_LEN];
1060 uint64_t offset = DMU_OBJACCT_PREFIX_LEN;
1061 uint64_t quota;
1062 uint64_t used;
1063 int err;
1064
1065 strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
1066 err = zfs_id_to_fuidstr(zfsvfs, NULL, zp->z_projid, buf + offset,
1067 sizeof (buf) - offset, B_FALSE);
1068 if (err)
1069 return (err);
1070
1071 if (zfsvfs->z_projectquota_obj == 0) {
1072 if (zfsvfs->z_defaultprojectquota == 0)
1073 goto objs;
1074 quota = zfsvfs->z_defaultprojectquota;
1075 } else {
1076 err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectquota_obj,
1077 buf + offset, 8, 1, "a);
1078 if (err && (quota = zfsvfs->z_defaultprojectquota) == 0) {
1079 if (err == ENOENT)
1080 goto objs;
1081 return (err);
1082 }
1083 }
1084
1085 err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1086 buf + offset, 8, 1, &used);
1087 if (unlikely(err == ENOENT)) {
1088 uint32_t blksize;
1089 u_longlong_t nblocks;
1090
1091 /*
1092 * Quota accounting is async, so it is possible race case.
1093 * There is at least one object with the given project ID.
1094 */
1095 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
1096 if (unlikely(zp->z_blksz == 0))
1097 blksize = zfsvfs->z_max_blksz;
1098
1099 used = blksize * nblocks;
1100 } else if (err) {
1101 return (err);
1102 }
1103
1104 statp->f_blocks = quota >> bshift;
1105 statp->f_bfree = (quota > used) ? ((quota - used) >> bshift) : 0;
1106 statp->f_bavail = statp->f_bfree;
1107
1108 objs:
1109
1110 if (zfsvfs->z_projectobjquota_obj == 0) {
1111 if (zfsvfs->z_defaultprojectobjquota == 0)
1112 return (0);
1113 quota = zfsvfs->z_defaultprojectobjquota;
1114 } else {
1115 err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectobjquota_obj,
1116 buf + offset, 8, 1, "a);
1117 if (err && (quota = zfsvfs->z_defaultprojectobjquota) == 0) {
1118 if (err == ENOENT)
1119 return (0);
1120 return (err);
1121 }
1122 }
1123
1124
1125 err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1126 buf, 8, 1, &used);
1127 if (unlikely(err == ENOENT)) {
1128 /*
1129 * Quota accounting is async, so it is possible race case.
1130 * There is at least one object with the given project ID.
1131 */
1132 used = 1;
1133 } else if (err) {
1134 return (err);
1135 }
1136
1137 statp->f_files = quota;
1138 statp->f_ffree = (quota > used) ? (quota - used) : 0;
1139
1140 return (0);
1141 }
1142
1143 int
zfs_statvfs(struct inode * ip,struct kstatfs * statp)1144 zfs_statvfs(struct inode *ip, struct kstatfs *statp)
1145 {
1146 zfsvfs_t *zfsvfs = ITOZSB(ip);
1147 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1148 int err = 0;
1149
1150 if ((err = zfs_enter(zfsvfs, FTAG)) != 0)
1151 return (err);
1152
1153 dmu_objset_space(zfsvfs->z_os,
1154 &refdbytes, &availbytes, &usedobjs, &availobjs);
1155
1156 uint64_t fsid = dmu_objset_fsid_guid(zfsvfs->z_os);
1157 /*
1158 * The underlying storage pool actually uses multiple block
1159 * size. Under Solaris frsize (fragment size) is reported as
1160 * the smallest block size we support, and bsize (block size)
1161 * as the filesystem's maximum block size. Unfortunately,
1162 * under Linux the fragment size and block size are often used
1163 * interchangeably. Thus we are forced to report both of them
1164 * as the filesystem's maximum block size.
1165 */
1166 statp->f_frsize = zfsvfs->z_max_blksz;
1167 statp->f_bsize = zfsvfs->z_max_blksz;
1168 uint32_t bshift = fls(statp->f_bsize) - 1;
1169
1170 /*
1171 * The following report "total" blocks of various kinds in
1172 * the file system, but reported in terms of f_bsize - the
1173 * "preferred" size.
1174 */
1175
1176 /* Round up so we never have a filesystem using 0 blocks. */
1177 refdbytes = P2ROUNDUP(refdbytes, statp->f_bsize);
1178 statp->f_blocks = (refdbytes + availbytes) >> bshift;
1179 statp->f_bfree = availbytes >> bshift;
1180 statp->f_bavail = statp->f_bfree; /* no root reservation */
1181
1182 /*
1183 * statvfs() should really be called statufs(), because it assumes
1184 * static metadata. ZFS doesn't preallocate files, so the best
1185 * we can do is report the max that could possibly fit in f_files,
1186 * and that minus the number actually used in f_ffree.
1187 * For f_ffree, report the smaller of the number of objects available
1188 * and the number of blocks (each object will take at least a block).
1189 */
1190 statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
1191 statp->f_files = statp->f_ffree + usedobjs;
1192 statp->f_fsid.val[0] = (uint32_t)fsid;
1193 statp->f_fsid.val[1] = (uint32_t)(fsid >> 32);
1194 statp->f_type = ZFS_SUPER_MAGIC;
1195 statp->f_namelen =
1196 zfsvfs->z_longname ? (ZAP_MAXNAMELEN_NEW - 1) : (MAXNAMELEN - 1);
1197
1198 /*
1199 * We have all of 40 characters to stuff a string here.
1200 * Is there anything useful we could/should provide?
1201 */
1202 memset(statp->f_spare, 0, sizeof (statp->f_spare));
1203
1204 if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
1205 dmu_objset_projectquota_present(zfsvfs->z_os)) {
1206 znode_t *zp = ITOZ(ip);
1207
1208 if (zp->z_pflags & ZFS_PROJINHERIT && zp->z_projid &&
1209 zpl_is_valid_projid(zp->z_projid))
1210 err = zfs_statfs_project(zfsvfs, zp, statp, bshift);
1211 }
1212
1213 zfs_exit(zfsvfs, FTAG);
1214 return (err);
1215 }
1216
1217 static int
zfs_root(zfsvfs_t * zfsvfs,struct inode ** ipp)1218 zfs_root(zfsvfs_t *zfsvfs, struct inode **ipp)
1219 {
1220 znode_t *rootzp;
1221 int error;
1222
1223 if ((error = zfs_enter(zfsvfs, FTAG)) != 0)
1224 return (error);
1225
1226 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1227 if (error == 0)
1228 *ipp = ZTOI(rootzp);
1229
1230 zfs_exit(zfsvfs, FTAG);
1231 return (error);
1232 }
1233
1234 /*
1235 * The ARC has requested that the filesystem drop entries from the dentry
1236 * and inode caches. This can occur when the ARC needs to free meta data
1237 * blocks but can't because they are all pinned by entries in these caches.
1238 */
1239 #if defined(HAVE_SUPER_BLOCK_S_SHRINK)
1240 #define S_SHRINK(sb) (&(sb)->s_shrink)
1241 #elif defined(HAVE_SUPER_BLOCK_S_SHRINK_PTR)
1242 #define S_SHRINK(sb) ((sb)->s_shrink)
1243 #endif
1244
1245 int
zfs_prune(struct super_block * sb,unsigned long nr_to_scan,int * objects)1246 zfs_prune(struct super_block *sb, unsigned long nr_to_scan, int *objects)
1247 {
1248 zfsvfs_t *zfsvfs = sb->s_fs_info;
1249 int error = 0;
1250 struct shrinker *shrinker = S_SHRINK(sb);
1251 struct shrink_control sc = {
1252 .nr_to_scan = nr_to_scan,
1253 .gfp_mask = GFP_KERNEL,
1254 };
1255
1256 if ((error = zfs_enter(zfsvfs, FTAG)) != 0)
1257 return (error);
1258
1259 #ifdef SHRINKER_NUMA_AWARE
1260 if (shrinker->flags & SHRINKER_NUMA_AWARE) {
1261 long tc = 1;
1262 for_each_online_node(sc.nid) {
1263 long c = shrinker->count_objects(shrinker, &sc);
1264 if (c == 0 || c == SHRINK_EMPTY)
1265 continue;
1266 tc += c;
1267 }
1268 *objects = 0;
1269 for_each_online_node(sc.nid) {
1270 long c = shrinker->count_objects(shrinker, &sc);
1271 if (c == 0 || c == SHRINK_EMPTY)
1272 continue;
1273 if (c > tc)
1274 tc = c;
1275 sc.nr_to_scan = mult_frac(nr_to_scan, c, tc) + 1;
1276 *objects += (*shrinker->scan_objects)(shrinker, &sc);
1277 }
1278 } else {
1279 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1280 }
1281 #else
1282 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1283 #endif
1284
1285 zfs_exit(zfsvfs, FTAG);
1286
1287 dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
1288 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1289 nr_to_scan, *objects, error);
1290
1291 return (error);
1292 }
1293
1294 /*
1295 * Teardown the zfsvfs_t.
1296 *
1297 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1298 * and 'z_teardown_inactive_lock' held.
1299 */
1300 static int
zfsvfs_teardown(zfsvfs_t * zfsvfs,boolean_t unmounting)1301 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1302 {
1303 znode_t *zp;
1304
1305 zfs_unlinked_drain_stop_wait(zfsvfs);
1306
1307 /*
1308 * If someone has not already unmounted this file system,
1309 * drain the zrele_taskq to ensure all active references to the
1310 * zfsvfs_t have been handled only then can it be safely destroyed.
1311 */
1312 if (zfsvfs->z_os) {
1313 /*
1314 * If we're unmounting we have to wait for the list to
1315 * drain completely.
1316 *
1317 * If we're not unmounting there's no guarantee the list
1318 * will drain completely, but iputs run from the taskq
1319 * may add the parents of dir-based xattrs to the taskq
1320 * so we want to wait for these.
1321 *
1322 * We can safely check z_all_znodes for being empty because the
1323 * VFS has already blocked operations which add to it.
1324 */
1325 int round = 0;
1326 while (!list_is_empty(&zfsvfs->z_all_znodes)) {
1327 taskq_wait_outstanding(dsl_pool_zrele_taskq(
1328 dmu_objset_pool(zfsvfs->z_os)), 0);
1329 if (++round > 1 && !unmounting)
1330 break;
1331 }
1332 }
1333
1334 ZFS_TEARDOWN_ENTER_WRITE(zfsvfs, FTAG);
1335
1336 if (!unmounting) {
1337 /*
1338 * We purge the parent filesystem's super block as the
1339 * parent filesystem and all of its snapshots have their
1340 * inode's super block set to the parent's filesystem's
1341 * super block. Note, 'z_parent' is self referential
1342 * for non-snapshots.
1343 */
1344 shrink_dcache_sb(zfsvfs->z_parent->z_sb);
1345 }
1346
1347 /*
1348 * Close the zil. NB: Can't close the zil while zfs_inactive
1349 * threads are blocked as zil_close can call zfs_inactive.
1350 */
1351 if (zfsvfs->z_log) {
1352 zil_close(zfsvfs->z_log);
1353 zfsvfs->z_log = NULL;
1354 }
1355
1356 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1357
1358 /*
1359 * If we are not unmounting (ie: online recv) and someone already
1360 * unmounted this file system while we were doing the switcheroo,
1361 * or a reopen of z_os failed then just bail out now.
1362 */
1363 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1364 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1365 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1366 return (SET_ERROR(EIO));
1367 }
1368
1369 /*
1370 * At this point there are no VFS ops active, and any new VFS ops
1371 * will fail with EIO since we have z_teardown_lock for writer (only
1372 * relevant for forced unmount).
1373 *
1374 * Release all holds on dbufs. We also grab an extra reference to all
1375 * the remaining inodes so that the kernel does not attempt to free
1376 * any inodes of a suspended fs. This can cause deadlocks since the
1377 * zfs_resume_fs() process may involve starting threads, which might
1378 * attempt to free unreferenced inodes to free up memory for the new
1379 * thread.
1380 */
1381 if (!unmounting) {
1382 mutex_enter(&zfsvfs->z_znodes_lock);
1383 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1384 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1385 if (zp->z_sa_hdl)
1386 zfs_znode_dmu_fini(zp);
1387 if (igrab(ZTOI(zp)) != NULL)
1388 zp->z_suspended = B_TRUE;
1389
1390 }
1391 mutex_exit(&zfsvfs->z_znodes_lock);
1392 }
1393
1394 /*
1395 * If we are unmounting, set the unmounted flag and let new VFS ops
1396 * unblock. zfs_inactive will have the unmounted behavior, and all
1397 * other VFS ops will fail with EIO.
1398 */
1399 if (unmounting) {
1400 zfsvfs->z_unmounted = B_TRUE;
1401 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1402 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1403 }
1404
1405 /*
1406 * z_os will be NULL if there was an error in attempting to reopen
1407 * zfsvfs, so just return as the properties had already been
1408 *
1409 * unregistered and cached data had been evicted before.
1410 */
1411 if (zfsvfs->z_os == NULL)
1412 return (0);
1413
1414 /*
1415 * Unregister properties.
1416 */
1417 zfs_unregister_callbacks(zfsvfs);
1418
1419 /*
1420 * Evict cached data. We must write out any dirty data before
1421 * disowning the dataset.
1422 */
1423 objset_t *os = zfsvfs->z_os;
1424 boolean_t os_dirty = B_FALSE;
1425 for (int t = 0; t < TXG_SIZE; t++) {
1426 if (dmu_objset_is_dirty(os, t)) {
1427 os_dirty = B_TRUE;
1428 break;
1429 }
1430 }
1431 if (!zfs_is_readonly(zfsvfs) && os_dirty) {
1432 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1433 }
1434 dmu_objset_evict_dbufs(zfsvfs->z_os);
1435 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1436 dsl_dir_cancel_waiters(dd);
1437
1438 return (0);
1439 }
1440
1441 static atomic_long_t zfs_bdi_seq = ATOMIC_LONG_INIT(0);
1442
1443 int
zfs_domount(struct super_block * sb,zfs_mnt_t * zm,int silent)1444 zfs_domount(struct super_block *sb, zfs_mnt_t *zm, int silent)
1445 {
1446 const char *osname = zm->mnt_osname;
1447 struct inode *root_inode = NULL;
1448 uint64_t recordsize;
1449 int error = 0;
1450 zfsvfs_t *zfsvfs = NULL;
1451 vfs_t *vfs = NULL;
1452 int canwrite;
1453 int dataset_visible_zone;
1454
1455 ASSERT(zm);
1456 ASSERT(osname);
1457
1458 dataset_visible_zone = zone_dataset_visible(osname, &canwrite);
1459
1460 /*
1461 * Refuse to mount a filesystem if we are in a namespace and the
1462 * dataset is not visible or writable in that namespace.
1463 */
1464 if (!INGLOBALZONE(curproc) &&
1465 (!dataset_visible_zone || !canwrite)) {
1466 return (SET_ERROR(EPERM));
1467 }
1468
1469 error = zfsvfs_parse_options(zm->mnt_data, &vfs);
1470 if (error)
1471 return (error);
1472
1473 /*
1474 * If a non-writable filesystem is being mounted without the
1475 * read-only flag, pretend it was set, as done for snapshots.
1476 */
1477 if (!canwrite)
1478 vfs->vfs_readonly = B_TRUE;
1479
1480 error = zfsvfs_create(osname, vfs->vfs_readonly, &zfsvfs);
1481 if (error) {
1482 zfsvfs_vfs_free(vfs);
1483 goto out;
1484 }
1485
1486 if ((error = dsl_prop_get_integer(osname, "recordsize",
1487 &recordsize, NULL))) {
1488 zfsvfs_vfs_free(vfs);
1489 goto out;
1490 }
1491
1492 vfs->vfs_data = zfsvfs;
1493 zfsvfs->z_vfs = vfs;
1494 zfsvfs->z_sb = sb;
1495 sb->s_fs_info = zfsvfs;
1496 sb->s_magic = ZFS_SUPER_MAGIC;
1497 sb->s_maxbytes = MAX_LFS_FILESIZE;
1498 sb->s_time_gran = 1;
1499 sb->s_blocksize = recordsize;
1500 sb->s_blocksize_bits = ilog2(recordsize);
1501
1502 error = -super_setup_bdi_name(sb, "%.28s-%ld", "zfs",
1503 atomic_long_inc_return(&zfs_bdi_seq));
1504 if (error)
1505 goto out;
1506
1507 sb->s_bdi->ra_pages = 0;
1508
1509 /* Set callback operations for the file system. */
1510 sb->s_op = &zpl_super_operations;
1511 sb->s_xattr = zpl_xattr_handlers;
1512 sb->s_export_op = &zpl_export_operations;
1513
1514 /* Set features for file system. */
1515 zfs_set_fuid_feature(zfsvfs);
1516
1517 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1518 uint64_t pval;
1519
1520 atime_changed_cb(zfsvfs, B_FALSE);
1521 readonly_changed_cb(zfsvfs, B_TRUE);
1522 if ((error = dsl_prop_get_integer(osname,
1523 "xattr", &pval, NULL)))
1524 goto out;
1525 xattr_changed_cb(zfsvfs, pval);
1526 if ((error = dsl_prop_get_integer(osname,
1527 "acltype", &pval, NULL)))
1528 goto out;
1529 acltype_changed_cb(zfsvfs, pval);
1530 zfsvfs->z_issnap = B_TRUE;
1531 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1532 zfsvfs->z_snap_defer_time = jiffies;
1533
1534 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1535 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1536 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1537 } else {
1538 if ((error = zfsvfs_setup(zfsvfs, B_TRUE)))
1539 goto out;
1540 }
1541
1542 /* Allocate a root inode for the filesystem. */
1543 error = zfs_root(zfsvfs, &root_inode);
1544 if (error) {
1545 (void) zfs_umount(sb);
1546 zfsvfs = NULL; /* avoid double-free; first in zfs_umount */
1547 goto out;
1548 }
1549
1550 /* Allocate a root dentry for the filesystem */
1551 sb->s_root = d_make_root(root_inode);
1552 if (sb->s_root == NULL) {
1553 (void) zfs_umount(sb);
1554 zfsvfs = NULL; /* avoid double-free; first in zfs_umount */
1555 error = SET_ERROR(ENOMEM);
1556 goto out;
1557 }
1558
1559 if (!zfsvfs->z_issnap)
1560 zfsctl_create(zfsvfs);
1561
1562 zfsvfs->z_arc_prune = arc_add_prune_callback(zpl_prune_sb, sb);
1563 out:
1564 if (error) {
1565 if (zfsvfs != NULL) {
1566 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1567 zfsvfs_free(zfsvfs);
1568 }
1569 /*
1570 * make sure we don't have dangling sb->s_fs_info which
1571 * zfs_preumount will use.
1572 */
1573 sb->s_fs_info = NULL;
1574 }
1575
1576 return (error);
1577 }
1578
1579 /*
1580 * Called when an unmount is requested and certain sanity checks have
1581 * already passed. At this point no dentries or inodes have been reclaimed
1582 * from their respective caches. We drop the extra reference on the .zfs
1583 * control directory to allow everything to be reclaimed. All snapshots
1584 * must already have been unmounted to reach this point.
1585 */
1586 void
zfs_preumount(struct super_block * sb)1587 zfs_preumount(struct super_block *sb)
1588 {
1589 zfsvfs_t *zfsvfs = sb->s_fs_info;
1590
1591 /* zfsvfs is NULL when zfs_domount fails during mount */
1592 if (zfsvfs) {
1593 zfs_unlinked_drain_stop_wait(zfsvfs);
1594 zfsctl_destroy(sb->s_fs_info);
1595 /*
1596 * Wait for zrele_async before entering evict_inodes in
1597 * generic_shutdown_super. The reason we must finish before
1598 * evict_inodes is when lazytime is on, or when zfs_purgedir
1599 * calls zfs_zget, zrele would bump i_count from 0 to 1. This
1600 * would race with the i_count check in evict_inodes. This means
1601 * it could destroy the inode while we are still using it.
1602 *
1603 * We wait for two passes. xattr directories in the first pass
1604 * may add xattr entries in zfs_purgedir, so in the second pass
1605 * we wait for them. We don't use taskq_wait here because it is
1606 * a pool wide taskq. Other mounted filesystems can constantly
1607 * do zrele_async and there's no guarantee when taskq will be
1608 * empty.
1609 */
1610 taskq_wait_outstanding(dsl_pool_zrele_taskq(
1611 dmu_objset_pool(zfsvfs->z_os)), 0);
1612 taskq_wait_outstanding(dsl_pool_zrele_taskq(
1613 dmu_objset_pool(zfsvfs->z_os)), 0);
1614 }
1615 }
1616
1617 /*
1618 * Called once all other unmount released tear down has occurred.
1619 * It is our responsibility to release any remaining infrastructure.
1620 */
1621 int
zfs_umount(struct super_block * sb)1622 zfs_umount(struct super_block *sb)
1623 {
1624 zfsvfs_t *zfsvfs = sb->s_fs_info;
1625 objset_t *os;
1626
1627 if (zfsvfs->z_arc_prune != NULL)
1628 arc_remove_prune_callback(zfsvfs->z_arc_prune);
1629 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1630 os = zfsvfs->z_os;
1631
1632 /*
1633 * z_os will be NULL if there was an error in
1634 * attempting to reopen zfsvfs.
1635 */
1636 if (os != NULL) {
1637 /*
1638 * Unset the objset user_ptr.
1639 */
1640 mutex_enter(&os->os_user_ptr_lock);
1641 dmu_objset_set_user(os, NULL);
1642 mutex_exit(&os->os_user_ptr_lock);
1643
1644 /*
1645 * Finally release the objset
1646 */
1647 dmu_objset_disown(os, B_TRUE, zfsvfs);
1648 }
1649
1650 zfsvfs_free(zfsvfs);
1651 sb->s_fs_info = NULL;
1652 return (0);
1653 }
1654
1655 int
zfs_remount(struct super_block * sb,int * flags,zfs_mnt_t * zm)1656 zfs_remount(struct super_block *sb, int *flags, zfs_mnt_t *zm)
1657 {
1658 zfsvfs_t *zfsvfs = sb->s_fs_info;
1659 vfs_t *vfsp;
1660 boolean_t issnap = dmu_objset_is_snapshot(zfsvfs->z_os);
1661 int error;
1662
1663 if ((issnap || !spa_writeable(dmu_objset_spa(zfsvfs->z_os))) &&
1664 !(*flags & SB_RDONLY)) {
1665 *flags |= SB_RDONLY;
1666 return (EROFS);
1667 }
1668
1669 error = zfsvfs_parse_options(zm->mnt_data, &vfsp);
1670 if (error)
1671 return (error);
1672
1673 if (!zfs_is_readonly(zfsvfs) && (*flags & SB_RDONLY))
1674 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1675
1676 zfs_unregister_callbacks(zfsvfs);
1677 zfsvfs_vfs_free(zfsvfs->z_vfs);
1678
1679 vfsp->vfs_data = zfsvfs;
1680 zfsvfs->z_vfs = vfsp;
1681 if (!issnap)
1682 (void) zfs_register_callbacks(vfsp);
1683
1684 return (error);
1685 }
1686
1687 int
zfs_vget(struct super_block * sb,struct inode ** ipp,fid_t * fidp)1688 zfs_vget(struct super_block *sb, struct inode **ipp, fid_t *fidp)
1689 {
1690 zfsvfs_t *zfsvfs = sb->s_fs_info;
1691 znode_t *zp;
1692 uint64_t object = 0;
1693 uint64_t fid_gen = 0;
1694 uint64_t gen_mask;
1695 uint64_t zp_gen;
1696 int i, err;
1697
1698 *ipp = NULL;
1699
1700 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1701 zfid_short_t *zfid = (zfid_short_t *)fidp;
1702
1703 for (i = 0; i < sizeof (zfid->zf_object); i++)
1704 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1705
1706 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1707 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1708 } else {
1709 return (SET_ERROR(EINVAL));
1710 }
1711
1712 /* LONG_FID_LEN means snapdirs */
1713 if (fidp->fid_len == LONG_FID_LEN) {
1714 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1715 uint64_t objsetid = 0;
1716 uint64_t setgen = 0;
1717
1718 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1719 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1720
1721 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1722 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1723
1724 if (objsetid != ZFSCTL_INO_SNAPDIRS - object) {
1725 dprintf("snapdir fid: objsetid (%llu) != "
1726 "ZFSCTL_INO_SNAPDIRS (%llu) - object (%llu)\n",
1727 objsetid, ZFSCTL_INO_SNAPDIRS, object);
1728
1729 return (SET_ERROR(EINVAL));
1730 }
1731
1732 if (fid_gen > 1 || setgen != 0) {
1733 dprintf("snapdir fid: fid_gen (%llu) and setgen "
1734 "(%llu)\n", fid_gen, setgen);
1735 return (SET_ERROR(EINVAL));
1736 }
1737
1738 return (zfsctl_snapdir_vget(sb, objsetid, fid_gen, ipp));
1739 }
1740
1741 if ((err = zfs_enter(zfsvfs, FTAG)) != 0)
1742 return (err);
1743 /* A zero fid_gen means we are in the .zfs control directories */
1744 if (fid_gen == 0 &&
1745 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1746 if (zfsvfs->z_show_ctldir == ZFS_SNAPDIR_DISABLED) {
1747 zfs_exit(zfsvfs, FTAG);
1748 return (SET_ERROR(ENOENT));
1749 }
1750
1751 *ipp = zfsvfs->z_ctldir;
1752 ASSERT(*ipp != NULL);
1753
1754 if (object == ZFSCTL_INO_SNAPDIR) {
1755 VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp,
1756 0, kcred, NULL, NULL) == 0);
1757 } else {
1758 /*
1759 * Must have an existing ref, so igrab()
1760 * cannot return NULL
1761 */
1762 VERIFY3P(igrab(*ipp), !=, NULL);
1763 }
1764 zfs_exit(zfsvfs, FTAG);
1765 return (0);
1766 }
1767
1768 gen_mask = -1ULL >> (64 - 8 * i);
1769
1770 dprintf("getting %llu [%llu mask %llx]\n", object, fid_gen, gen_mask);
1771 if ((err = zfs_zget(zfsvfs, object, &zp))) {
1772 zfs_exit(zfsvfs, FTAG);
1773 return (err);
1774 }
1775
1776 /* Don't export xattr stuff */
1777 if (zp->z_pflags & ZFS_XATTR) {
1778 zrele(zp);
1779 zfs_exit(zfsvfs, FTAG);
1780 return (SET_ERROR(ENOENT));
1781 }
1782
1783 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1784 sizeof (uint64_t));
1785 zp_gen = zp_gen & gen_mask;
1786 if (zp_gen == 0)
1787 zp_gen = 1;
1788 if ((fid_gen == 0) && (zfsvfs->z_root == object))
1789 fid_gen = zp_gen;
1790 if (zp->z_unlinked || zp_gen != fid_gen) {
1791 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen,
1792 fid_gen);
1793 zrele(zp);
1794 zfs_exit(zfsvfs, FTAG);
1795 return (SET_ERROR(ENOENT));
1796 }
1797
1798 *ipp = ZTOI(zp);
1799 if (*ipp)
1800 zfs_znode_update_vfs(ITOZ(*ipp));
1801
1802 zfs_exit(zfsvfs, FTAG);
1803 return (0);
1804 }
1805
1806 /*
1807 * Block out VFS ops and close zfsvfs_t
1808 *
1809 * Note, if successful, then we return with the 'z_teardown_lock' and
1810 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1811 * dataset and objset intact so that they can be atomically handed off during
1812 * a subsequent rollback or recv operation and the resume thereafter.
1813 */
1814 int
zfs_suspend_fs(zfsvfs_t * zfsvfs)1815 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1816 {
1817 int error;
1818
1819 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1820 return (error);
1821
1822 return (0);
1823 }
1824
1825 /*
1826 * Rebuild SA and release VOPs. Note that ownership of the underlying dataset
1827 * is an invariant across any of the operations that can be performed while the
1828 * filesystem was suspended. Whether it succeeded or failed, the preconditions
1829 * are the same: the relevant objset and associated dataset are owned by
1830 * zfsvfs, held, and long held on entry.
1831 */
1832 int
zfs_resume_fs(zfsvfs_t * zfsvfs,dsl_dataset_t * ds)1833 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
1834 {
1835 int err, err2;
1836 znode_t *zp;
1837
1838 ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs));
1839 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1840
1841 /*
1842 * We already own this, so just update the objset_t, as the one we
1843 * had before may have been evicted.
1844 */
1845 objset_t *os;
1846 VERIFY3P(ds->ds_owner, ==, zfsvfs);
1847 VERIFY(dsl_dataset_long_held(ds));
1848 dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
1849 dsl_pool_config_enter(dp, FTAG);
1850 VERIFY0(dmu_objset_from_ds(ds, &os));
1851 dsl_pool_config_exit(dp, FTAG);
1852
1853 err = zfsvfs_init(zfsvfs, os);
1854 if (err != 0)
1855 goto bail;
1856
1857 ds->ds_dir->dd_activity_cancelled = B_FALSE;
1858 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1859
1860 zfs_set_fuid_feature(zfsvfs);
1861 zfsvfs->z_rollback_time = jiffies;
1862
1863 /*
1864 * Attempt to re-establish all the active inodes with their
1865 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1866 * and mark it stale. This prevents a collision if a new
1867 * inode/object is created which must use the same inode
1868 * number. The stale inode will be be released when the
1869 * VFS prunes the dentry holding the remaining references
1870 * on the stale inode.
1871 */
1872 mutex_enter(&zfsvfs->z_znodes_lock);
1873 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1874 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1875 err2 = zfs_rezget(zp);
1876 if (err2) {
1877 zpl_d_drop_aliases(ZTOI(zp));
1878 remove_inode_hash(ZTOI(zp));
1879 }
1880
1881 /* see comment in zfs_suspend_fs() */
1882 if (zp->z_suspended) {
1883 zfs_zrele_async(zp);
1884 zp->z_suspended = B_FALSE;
1885 }
1886 }
1887 mutex_exit(&zfsvfs->z_znodes_lock);
1888
1889 if (!zfs_is_readonly(zfsvfs) && !zfsvfs->z_unmounted) {
1890 /*
1891 * zfs_suspend_fs() could have interrupted freeing
1892 * of dnodes. We need to restart this freeing so
1893 * that we don't "leak" the space.
1894 */
1895 zfs_unlinked_drain(zfsvfs);
1896 }
1897
1898 /*
1899 * Most of the time zfs_suspend_fs is used for changing the contents
1900 * of the underlying dataset. ZFS rollback and receive operations
1901 * might create files for which negative dentries are present in
1902 * the cache. Since walking the dcache would require a lot of GPL-only
1903 * code duplication, it's much easier on these rather rare occasions
1904 * just to flush the whole dcache for the given dataset/filesystem.
1905 */
1906 shrink_dcache_sb(zfsvfs->z_sb);
1907
1908 bail:
1909 if (err != 0)
1910 zfsvfs->z_unmounted = B_TRUE;
1911
1912 /* release the VFS ops */
1913 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1914 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1915
1916 if (err != 0) {
1917 /*
1918 * Since we couldn't setup the sa framework, try to force
1919 * unmount this file system.
1920 */
1921 if (zfsvfs->z_os)
1922 (void) zfs_umount(zfsvfs->z_sb);
1923 }
1924 return (err);
1925 }
1926
1927 /*
1928 * Release VOPs and unmount a suspended filesystem.
1929 */
1930 int
zfs_end_fs(zfsvfs_t * zfsvfs,dsl_dataset_t * ds)1931 zfs_end_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
1932 {
1933 ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs));
1934 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1935
1936 /*
1937 * We already own this, so just hold and rele it to update the
1938 * objset_t, as the one we had before may have been evicted.
1939 */
1940 objset_t *os;
1941 VERIFY3P(ds->ds_owner, ==, zfsvfs);
1942 VERIFY(dsl_dataset_long_held(ds));
1943 dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
1944 dsl_pool_config_enter(dp, FTAG);
1945 VERIFY0(dmu_objset_from_ds(ds, &os));
1946 dsl_pool_config_exit(dp, FTAG);
1947 zfsvfs->z_os = os;
1948
1949 /* release the VOPs */
1950 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1951 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG);
1952
1953 /*
1954 * Try to force unmount this file system.
1955 */
1956 (void) zfs_umount(zfsvfs->z_sb);
1957 zfsvfs->z_unmounted = B_TRUE;
1958 return (0);
1959 }
1960
1961 /*
1962 * Automounted snapshots rely on periodic revalidation
1963 * to defer snapshots from being automatically unmounted.
1964 */
1965
1966 inline void
zfs_exit_fs(zfsvfs_t * zfsvfs)1967 zfs_exit_fs(zfsvfs_t *zfsvfs)
1968 {
1969 if (!zfsvfs->z_issnap)
1970 return;
1971
1972 if (time_after(jiffies, zfsvfs->z_snap_defer_time +
1973 MAX(zfs_expire_snapshot * HZ / 2, HZ))) {
1974 zfsvfs->z_snap_defer_time = jiffies;
1975 zfsctl_snapshot_unmount_delay(zfsvfs->z_os->os_spa,
1976 dmu_objset_id(zfsvfs->z_os),
1977 zfs_expire_snapshot);
1978 }
1979 }
1980
1981 int
zfs_set_version(zfsvfs_t * zfsvfs,uint64_t newvers)1982 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
1983 {
1984 int error;
1985 objset_t *os = zfsvfs->z_os;
1986 dmu_tx_t *tx;
1987
1988 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1989 return (SET_ERROR(EINVAL));
1990
1991 if (newvers < zfsvfs->z_version)
1992 return (SET_ERROR(EINVAL));
1993
1994 if (zfs_spa_version_map(newvers) >
1995 spa_version(dmu_objset_spa(zfsvfs->z_os)))
1996 return (SET_ERROR(ENOTSUP));
1997
1998 tx = dmu_tx_create(os);
1999 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2000 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2001 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2002 ZFS_SA_ATTRS);
2003 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2004 }
2005 error = dmu_tx_assign(tx, DMU_TX_WAIT);
2006 if (error) {
2007 dmu_tx_abort(tx);
2008 return (error);
2009 }
2010
2011 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2012 8, 1, &newvers, tx);
2013
2014 if (error) {
2015 dmu_tx_commit(tx);
2016 return (error);
2017 }
2018
2019 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2020 uint64_t sa_obj;
2021
2022 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2023 SPA_VERSION_SA);
2024 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2025 DMU_OT_NONE, 0, tx);
2026
2027 error = zap_add(os, MASTER_NODE_OBJ,
2028 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2029 ASSERT0(error);
2030
2031 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2032 sa_register_update_callback(os, zfs_sa_upgrade);
2033 }
2034
2035 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2036 "from %llu to %llu", zfsvfs->z_version, newvers);
2037
2038 dmu_tx_commit(tx);
2039
2040 zfsvfs->z_version = newvers;
2041 os->os_version = newvers;
2042
2043 zfs_set_fuid_feature(zfsvfs);
2044
2045 return (0);
2046 }
2047
2048 int
zfs_set_default_quota(zfsvfs_t * zfsvfs,zfs_prop_t prop,uint64_t quota)2049 zfs_set_default_quota(zfsvfs_t *zfsvfs, zfs_prop_t prop, uint64_t quota)
2050 {
2051 int error;
2052 objset_t *os = zfsvfs->z_os;
2053 const char *propstr = zfs_prop_to_name(prop);
2054 dmu_tx_t *tx;
2055
2056 tx = dmu_tx_create(os);
2057 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, propstr);
2058 error = dmu_tx_assign(tx, DMU_TX_WAIT);
2059 if (error) {
2060 dmu_tx_abort(tx);
2061 return (error);
2062 }
2063
2064 if (quota == 0) {
2065 error = zap_remove(os, MASTER_NODE_OBJ, propstr, tx);
2066 if (error == ENOENT)
2067 error = 0;
2068 } else {
2069 error = zap_update(os, MASTER_NODE_OBJ, propstr, 8, 1,
2070 "a, tx);
2071 }
2072
2073 if (error)
2074 goto out;
2075
2076 switch (prop) {
2077 case ZFS_PROP_DEFAULTUSERQUOTA:
2078 zfsvfs->z_defaultuserquota = quota;
2079 break;
2080 case ZFS_PROP_DEFAULTGROUPQUOTA:
2081 zfsvfs->z_defaultgroupquota = quota;
2082 break;
2083 case ZFS_PROP_DEFAULTPROJECTQUOTA:
2084 zfsvfs->z_defaultprojectquota = quota;
2085 break;
2086 case ZFS_PROP_DEFAULTUSEROBJQUOTA:
2087 zfsvfs->z_defaultuserobjquota = quota;
2088 break;
2089 case ZFS_PROP_DEFAULTGROUPOBJQUOTA:
2090 zfsvfs->z_defaultgroupobjquota = quota;
2091 break;
2092 case ZFS_PROP_DEFAULTPROJECTOBJQUOTA:
2093 zfsvfs->z_defaultprojectobjquota = quota;
2094 break;
2095 default:
2096 break;
2097 }
2098
2099 out:
2100 dmu_tx_commit(tx);
2101 return (error);
2102 }
2103
2104 /*
2105 * Return true if the corresponding vfs's unmounted flag is set.
2106 * Otherwise return false.
2107 * If this function returns true we know VFS unmount has been initiated.
2108 */
2109 boolean_t
zfs_get_vfs_flag_unmounted(objset_t * os)2110 zfs_get_vfs_flag_unmounted(objset_t *os)
2111 {
2112 zfsvfs_t *zfvp;
2113 boolean_t unmounted = B_FALSE;
2114
2115 ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2116
2117 mutex_enter(&os->os_user_ptr_lock);
2118 zfvp = dmu_objset_get_user(os);
2119 if (zfvp != NULL && zfvp->z_unmounted)
2120 unmounted = B_TRUE;
2121 mutex_exit(&os->os_user_ptr_lock);
2122
2123 return (unmounted);
2124 }
2125
2126 void
zfsvfs_update_fromname(const char * oldname,const char * newname)2127 zfsvfs_update_fromname(const char *oldname, const char *newname)
2128 {
2129 /*
2130 * We don't need to do anything here, the devname is always current by
2131 * virtue of zfsvfs->z_sb->s_op->show_devname.
2132 */
2133 (void) oldname, (void) newname;
2134 }
2135
2136 void
zfs_init(void)2137 zfs_init(void)
2138 {
2139 zfsctl_init();
2140 zfs_znode_init();
2141 dmu_objset_register_type(DMU_OST_ZFS, zpl_get_file_info);
2142 register_filesystem(&zpl_fs_type);
2143 }
2144
2145 void
zfs_fini(void)2146 zfs_fini(void)
2147 {
2148 /*
2149 * we don't use outstanding because zpl_posix_acl_free might add more.
2150 */
2151 taskq_wait(system_delay_taskq);
2152 taskq_wait(system_taskq);
2153 unregister_filesystem(&zpl_fs_type);
2154 zfs_znode_fini();
2155 zfsctl_fini();
2156 }
2157
2158 #if defined(_KERNEL)
2159 EXPORT_SYMBOL(zfs_suspend_fs);
2160 EXPORT_SYMBOL(zfs_resume_fs);
2161 EXPORT_SYMBOL(zfs_set_version);
2162 EXPORT_SYMBOL(zfsvfs_create);
2163 EXPORT_SYMBOL(zfsvfs_free);
2164 EXPORT_SYMBOL(zfs_is_readonly);
2165 EXPORT_SYMBOL(zfs_domount);
2166 EXPORT_SYMBOL(zfs_preumount);
2167 EXPORT_SYMBOL(zfs_umount);
2168 EXPORT_SYMBOL(zfs_remount);
2169 EXPORT_SYMBOL(zfs_statvfs);
2170 EXPORT_SYMBOL(zfs_vget);
2171 EXPORT_SYMBOL(zfs_prune);
2172 EXPORT_SYMBOL(zfs_set_default_quota);
2173 #endif
2174