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