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