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