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