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