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