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