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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2014 Integros [integros.com]
25 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
35 #include <sys/pathname.h>
36 #include <sys/vnode.h>
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
40 #include <sys/cmn_err.h>
41 #include "sys/fs_subr.h"
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_dir.h>
45 #include <sys/fs/zfs.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_deleg.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>
66 #include <sys/dmu_objset.h>
67 #include <sys/spa_boot.h>
68 #include "zfs_comutil.h"
71 static major_t zfs_major
;
72 static minor_t zfs_minor
;
73 static kmutex_t zfs_dev_mtx
;
75 extern int sys_shutdown
;
77 static int zfs_mount(vfs_t
*vfsp
, vnode_t
*mvp
, struct mounta
*uap
, cred_t
*cr
);
78 static int zfs_umount(vfs_t
*vfsp
, int fflag
, cred_t
*cr
);
79 static int zfs_mountroot(vfs_t
*vfsp
, enum whymountroot
);
80 static int zfs_root(vfs_t
*vfsp
, vnode_t
**vpp
);
81 static int zfs_statvfs(vfs_t
*vfsp
, struct statvfs64
*statp
);
82 static int zfs_vget(vfs_t
*vfsp
, vnode_t
**vpp
, fid_t
*fidp
);
83 static void zfs_freevfs(vfs_t
*vfsp
);
85 static const struct vfsops zfs_vfsops
= {
86 .vfs_mount
= zfs_mount
,
87 .vfs_mountroot
= zfs_mountroot
,
88 .vfs_unmount
= zfs_umount
,
90 .vfs_statvfs
= zfs_statvfs
,
93 .vfs_freevfs
= zfs_freevfs
,
97 * We need to keep a count of active fs's.
98 * This is necessary to prevent our module
99 * from being unloaded after a umount -f
101 static uint32_t zfs_active_fs_count
= 0;
103 static char *noatime_cancel
[] = { MNTOPT_ATIME
, NULL
};
104 static char *atime_cancel
[] = { MNTOPT_NOATIME
, NULL
};
105 static char *noxattr_cancel
[] = { MNTOPT_XATTR
, NULL
};
106 static char *xattr_cancel
[] = { MNTOPT_NOXATTR
, NULL
};
109 * MO_DEFAULT is not used since the default value is determined
110 * by the equivalent property.
112 static mntopt_t mntopts
[] = {
113 { MNTOPT_NOXATTR
, noxattr_cancel
, NULL
, 0, NULL
},
114 { MNTOPT_XATTR
, xattr_cancel
, NULL
, 0, NULL
},
115 { MNTOPT_NOATIME
, noatime_cancel
, NULL
, 0, NULL
},
116 { MNTOPT_ATIME
, atime_cancel
, NULL
, 0, NULL
}
119 static mntopts_t zfs_mntopts
= {
120 sizeof (mntopts
) / sizeof (mntopt_t
),
126 zfs_sync(vfs_t
*vfsp
, short flag
, cred_t
*cr
)
129 * Data integrity is job one. We don't want a compromised kernel
130 * writing to the storage pool, so we never sync during panic.
136 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
137 * to sync metadata, which they would otherwise cache indefinitely.
138 * Semantically, the only requirement is that the sync be initiated.
139 * The DMU syncs out txgs frequently, so there's nothing to do.
141 if (flag
& SYNC_ATTR
)
146 * Sync a specific filesystem.
148 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
152 dp
= dmu_objset_pool(zfsvfs
->z_os
);
155 * If the system is shutting down, then skip any
156 * filesystems which may exist on a suspended pool.
158 if (sys_shutdown
&& spa_suspended(dp
->dp_spa
)) {
163 if (zfsvfs
->z_log
!= NULL
)
164 zil_commit(zfsvfs
->z_log
, 0);
169 * Sync all ZFS filesystems. This is what happens when you
170 * run sync(1M). Unlike other filesystems, ZFS honors the
171 * request by waiting for all pools to commit all dirty data.
180 zfs_create_unique_device(dev_t
*dev
)
185 ASSERT3U(zfs_minor
, <=, MAXMIN32
);
186 minor_t start
= zfs_minor
;
188 mutex_enter(&zfs_dev_mtx
);
189 if (zfs_minor
>= MAXMIN32
) {
191 * If we're still using the real major
192 * keep out of /dev/zfs and /dev/zvol minor
193 * number space. If we're using a getudev()'ed
194 * major number, we can use all of its minors.
196 if (zfs_major
== ddi_name_to_major(ZFS_DRIVER
))
197 zfs_minor
= ZFS_MIN_MINOR
;
203 *dev
= makedevice(zfs_major
, zfs_minor
);
204 mutex_exit(&zfs_dev_mtx
);
205 } while (vfs_devismounted(*dev
) && zfs_minor
!= start
);
206 if (zfs_minor
== start
) {
208 * We are using all ~262,000 minor numbers for the
209 * current major number. Create a new major number.
211 if ((new_major
= getudev()) == (major_t
)-1) {
213 "zfs_mount: Can't get unique major "
217 mutex_enter(&zfs_dev_mtx
);
218 zfs_major
= new_major
;
221 mutex_exit(&zfs_dev_mtx
);
225 /* CONSTANTCONDITION */
232 atime_changed_cb(void *arg
, uint64_t newval
)
234 zfsvfs_t
*zfsvfs
= arg
;
236 if (newval
== TRUE
) {
237 zfsvfs
->z_atime
= TRUE
;
238 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_NOATIME
);
239 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_ATIME
, NULL
, 0);
241 zfsvfs
->z_atime
= FALSE
;
242 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_ATIME
);
243 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_NOATIME
, NULL
, 0);
248 xattr_changed_cb(void *arg
, uint64_t newval
)
250 zfsvfs_t
*zfsvfs
= arg
;
252 if (newval
== TRUE
) {
253 /* XXX locking on vfs_flag? */
254 zfsvfs
->z_vfs
->vfs_flag
|= VFS_XATTR
;
255 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_NOXATTR
);
256 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_XATTR
, NULL
, 0);
258 /* XXX locking on vfs_flag? */
259 zfsvfs
->z_vfs
->vfs_flag
&= ~VFS_XATTR
;
260 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_XATTR
);
261 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_NOXATTR
, NULL
, 0);
266 blksz_changed_cb(void *arg
, uint64_t newval
)
268 zfsvfs_t
*zfsvfs
= arg
;
269 ASSERT3U(newval
, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs
->z_os
)));
270 ASSERT3U(newval
, >=, SPA_MINBLOCKSIZE
);
271 ASSERT(ISP2(newval
));
273 zfsvfs
->z_max_blksz
= newval
;
274 zfsvfs
->z_vfs
->vfs_bsize
= newval
;
278 readonly_changed_cb(void *arg
, uint64_t newval
)
280 zfsvfs_t
*zfsvfs
= arg
;
283 /* XXX locking on vfs_flag? */
284 zfsvfs
->z_vfs
->vfs_flag
|= VFS_RDONLY
;
285 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_RW
);
286 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_RO
, NULL
, 0);
288 /* XXX locking on vfs_flag? */
289 zfsvfs
->z_vfs
->vfs_flag
&= ~VFS_RDONLY
;
290 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_RO
);
291 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_RW
, NULL
, 0);
296 devices_changed_cb(void *arg
, uint64_t newval
)
298 zfsvfs_t
*zfsvfs
= arg
;
300 if (newval
== FALSE
) {
301 zfsvfs
->z_vfs
->vfs_flag
|= VFS_NODEVICES
;
302 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_DEVICES
);
303 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_NODEVICES
, NULL
, 0);
305 zfsvfs
->z_vfs
->vfs_flag
&= ~VFS_NODEVICES
;
306 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_NODEVICES
);
307 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_DEVICES
, NULL
, 0);
312 setuid_changed_cb(void *arg
, uint64_t newval
)
314 zfsvfs_t
*zfsvfs
= arg
;
316 if (newval
== FALSE
) {
317 zfsvfs
->z_vfs
->vfs_flag
|= VFS_NOSETUID
;
318 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_SETUID
);
319 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_NOSETUID
, NULL
, 0);
321 zfsvfs
->z_vfs
->vfs_flag
&= ~VFS_NOSETUID
;
322 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_NOSETUID
);
323 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_SETUID
, NULL
, 0);
328 exec_changed_cb(void *arg
, uint64_t newval
)
330 zfsvfs_t
*zfsvfs
= arg
;
332 if (newval
== FALSE
) {
333 zfsvfs
->z_vfs
->vfs_flag
|= VFS_NOEXEC
;
334 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_EXEC
);
335 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_NOEXEC
, NULL
, 0);
337 zfsvfs
->z_vfs
->vfs_flag
&= ~VFS_NOEXEC
;
338 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_NOEXEC
);
339 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_EXEC
, NULL
, 0);
344 * The nbmand mount option can be changed at mount time.
345 * We can't allow it to be toggled on live file systems or incorrect
346 * behavior may be seen from cifs clients
348 * This property isn't registered via dsl_prop_register(), but this callback
349 * will be called when a file system is first mounted
352 nbmand_changed_cb(void *arg
, uint64_t newval
)
354 zfsvfs_t
*zfsvfs
= arg
;
355 if (newval
== FALSE
) {
356 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_NBMAND
);
357 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_NONBMAND
, NULL
, 0);
359 vfs_clearmntopt(zfsvfs
->z_vfs
, MNTOPT_NONBMAND
);
360 vfs_setmntopt(zfsvfs
->z_vfs
, MNTOPT_NBMAND
, NULL
, 0);
365 snapdir_changed_cb(void *arg
, uint64_t newval
)
367 zfsvfs_t
*zfsvfs
= arg
;
369 zfsvfs
->z_show_ctldir
= newval
;
373 vscan_changed_cb(void *arg
, uint64_t newval
)
375 zfsvfs_t
*zfsvfs
= arg
;
377 zfsvfs
->z_vscan
= newval
;
381 acl_mode_changed_cb(void *arg
, uint64_t newval
)
383 zfsvfs_t
*zfsvfs
= arg
;
385 zfsvfs
->z_acl_mode
= newval
;
389 acl_inherit_changed_cb(void *arg
, uint64_t newval
)
391 zfsvfs_t
*zfsvfs
= arg
;
393 zfsvfs
->z_acl_inherit
= newval
;
397 zfs_register_callbacks(vfs_t
*vfsp
)
399 struct dsl_dataset
*ds
= NULL
;
401 zfsvfs_t
*zfsvfs
= NULL
;
403 boolean_t readonly
= B_FALSE
;
404 boolean_t do_readonly
= B_FALSE
;
405 boolean_t setuid
= B_FALSE
;
406 boolean_t do_setuid
= B_FALSE
;
407 boolean_t exec
= B_FALSE
;
408 boolean_t do_exec
= B_FALSE
;
409 boolean_t devices
= B_FALSE
;
410 boolean_t do_devices
= B_FALSE
;
411 boolean_t xattr
= B_FALSE
;
412 boolean_t do_xattr
= B_FALSE
;
413 boolean_t atime
= B_FALSE
;
414 boolean_t do_atime
= B_FALSE
;
418 zfsvfs
= vfsp
->vfs_data
;
423 * The act of registering our callbacks will destroy any mount
424 * options we may have. In order to enable temporary overrides
425 * of mount options, we stash away the current values and
426 * restore them after we register the callbacks.
428 if (vfs_optionisset(vfsp
, MNTOPT_RO
, NULL
) ||
429 !spa_writeable(dmu_objset_spa(os
))) {
431 do_readonly
= B_TRUE
;
432 } else if (vfs_optionisset(vfsp
, MNTOPT_RW
, NULL
)) {
434 do_readonly
= B_TRUE
;
436 if (vfs_optionisset(vfsp
, MNTOPT_NOSUID
, NULL
)) {
442 if (vfs_optionisset(vfsp
, MNTOPT_NODEVICES
, NULL
)) {
445 } else if (vfs_optionisset(vfsp
, MNTOPT_DEVICES
, NULL
)) {
450 if (vfs_optionisset(vfsp
, MNTOPT_NOSETUID
, NULL
)) {
453 } else if (vfs_optionisset(vfsp
, MNTOPT_SETUID
, NULL
)) {
458 if (vfs_optionisset(vfsp
, MNTOPT_NOEXEC
, NULL
)) {
461 } else if (vfs_optionisset(vfsp
, MNTOPT_EXEC
, NULL
)) {
465 if (vfs_optionisset(vfsp
, MNTOPT_NOXATTR
, NULL
)) {
468 } else if (vfs_optionisset(vfsp
, MNTOPT_XATTR
, NULL
)) {
472 if (vfs_optionisset(vfsp
, MNTOPT_NOATIME
, NULL
)) {
475 } else if (vfs_optionisset(vfsp
, MNTOPT_ATIME
, NULL
)) {
481 * nbmand is a special property. It can only be changed at
484 * This is weird, but it is documented to only be changeable
487 if (vfs_optionisset(vfsp
, MNTOPT_NONBMAND
, NULL
)) {
489 } else if (vfs_optionisset(vfsp
, MNTOPT_NBMAND
, NULL
)) {
492 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
494 dmu_objset_name(os
, osname
);
495 if (error
= dsl_prop_get_integer(osname
, "nbmand", &nbmand
,
502 * Register property callbacks.
504 * It would probably be fine to just check for i/o error from
505 * the first prop_register(), but I guess I like to go
508 ds
= dmu_objset_ds(os
);
509 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
510 error
= dsl_prop_register(ds
,
511 zfs_prop_to_name(ZFS_PROP_ATIME
), atime_changed_cb
, zfsvfs
);
512 error
= error
? error
: dsl_prop_register(ds
,
513 zfs_prop_to_name(ZFS_PROP_XATTR
), xattr_changed_cb
, zfsvfs
);
514 error
= error
? error
: dsl_prop_register(ds
,
515 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
), blksz_changed_cb
, zfsvfs
);
516 error
= error
? error
: dsl_prop_register(ds
,
517 zfs_prop_to_name(ZFS_PROP_READONLY
), readonly_changed_cb
, zfsvfs
);
518 error
= error
? error
: dsl_prop_register(ds
,
519 zfs_prop_to_name(ZFS_PROP_DEVICES
), devices_changed_cb
, zfsvfs
);
520 error
= error
? error
: dsl_prop_register(ds
,
521 zfs_prop_to_name(ZFS_PROP_SETUID
), setuid_changed_cb
, zfsvfs
);
522 error
= error
? error
: dsl_prop_register(ds
,
523 zfs_prop_to_name(ZFS_PROP_EXEC
), exec_changed_cb
, zfsvfs
);
524 error
= error
? error
: dsl_prop_register(ds
,
525 zfs_prop_to_name(ZFS_PROP_SNAPDIR
), snapdir_changed_cb
, zfsvfs
);
526 error
= error
? error
: dsl_prop_register(ds
,
527 zfs_prop_to_name(ZFS_PROP_ACLMODE
), acl_mode_changed_cb
, zfsvfs
);
528 error
= error
? error
: dsl_prop_register(ds
,
529 zfs_prop_to_name(ZFS_PROP_ACLINHERIT
), acl_inherit_changed_cb
,
531 error
= error
? error
: dsl_prop_register(ds
,
532 zfs_prop_to_name(ZFS_PROP_VSCAN
), vscan_changed_cb
, zfsvfs
);
533 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
538 * Invoke our callbacks to restore temporary mount options.
541 readonly_changed_cb(zfsvfs
, readonly
);
543 setuid_changed_cb(zfsvfs
, setuid
);
545 exec_changed_cb(zfsvfs
, exec
);
547 devices_changed_cb(zfsvfs
, devices
);
549 xattr_changed_cb(zfsvfs
, xattr
);
551 atime_changed_cb(zfsvfs
, atime
);
553 nbmand_changed_cb(zfsvfs
, nbmand
);
558 dsl_prop_unregister_all(ds
, zfsvfs
);
563 zfs_space_delta_cb(dmu_object_type_t bonustype
, void *data
,
564 uint64_t *userp
, uint64_t *groupp
)
567 * Is it a valid type of object to track?
569 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
570 return (SET_ERROR(ENOENT
));
573 * If we have a NULL data pointer
574 * then assume the id's aren't changing and
575 * return EEXIST to the dmu to let it know to
579 return (SET_ERROR(EEXIST
));
581 if (bonustype
== DMU_OT_ZNODE
) {
582 znode_phys_t
*znp
= data
;
583 *userp
= znp
->zp_uid
;
584 *groupp
= znp
->zp_gid
;
587 sa_hdr_phys_t
*sap
= data
;
588 sa_hdr_phys_t sa
= *sap
;
589 boolean_t swap
= B_FALSE
;
591 ASSERT(bonustype
== DMU_OT_SA
);
593 if (sa
.sa_magic
== 0) {
595 * This should only happen for newly created
596 * files that haven't had the znode data filled
603 if (sa
.sa_magic
== BSWAP_32(SA_MAGIC
)) {
604 sa
.sa_magic
= SA_MAGIC
;
605 sa
.sa_layout_info
= BSWAP_16(sa
.sa_layout_info
);
608 VERIFY3U(sa
.sa_magic
, ==, SA_MAGIC
);
611 hdrsize
= sa_hdrsize(&sa
);
612 VERIFY3U(hdrsize
, >=, sizeof (sa_hdr_phys_t
));
613 *userp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
615 *groupp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
618 *userp
= BSWAP_64(*userp
);
619 *groupp
= BSWAP_64(*groupp
);
626 fuidstr_to_sid(zfsvfs_t
*zfsvfs
, const char *fuidstr
,
627 char *domainbuf
, int buflen
, uid_t
*ridp
)
632 fuid
= zfs_strtonum(fuidstr
, NULL
);
634 domain
= zfs_fuid_find_by_idx(zfsvfs
, FUID_INDEX(fuid
));
636 (void) strlcpy(domainbuf
, domain
, buflen
);
639 *ridp
= FUID_RID(fuid
);
643 zfs_userquota_prop_to_obj(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
)
646 case ZFS_PROP_USERUSED
:
647 return (DMU_USERUSED_OBJECT
);
648 case ZFS_PROP_GROUPUSED
:
649 return (DMU_GROUPUSED_OBJECT
);
650 case ZFS_PROP_USERQUOTA
:
651 return (zfsvfs
->z_userquota_obj
);
652 case ZFS_PROP_GROUPQUOTA
:
653 return (zfsvfs
->z_groupquota_obj
);
659 zfs_userspace_many(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
,
660 uint64_t *cookiep
, void *vbuf
, uint64_t *bufsizep
)
665 zfs_useracct_t
*buf
= vbuf
;
668 if (!dmu_objset_userspace_present(zfsvfs
->z_os
))
669 return (SET_ERROR(ENOTSUP
));
671 obj
= zfs_userquota_prop_to_obj(zfsvfs
, type
);
677 for (zap_cursor_init_serialized(&zc
, zfsvfs
->z_os
, obj
, *cookiep
);
678 (error
= zap_cursor_retrieve(&zc
, &za
)) == 0;
679 zap_cursor_advance(&zc
)) {
680 if ((uintptr_t)buf
- (uintptr_t)vbuf
+ sizeof (zfs_useracct_t
) >
684 fuidstr_to_sid(zfsvfs
, za
.za_name
,
685 buf
->zu_domain
, sizeof (buf
->zu_domain
), &buf
->zu_rid
);
687 buf
->zu_space
= za
.za_first_integer
;
693 ASSERT3U((uintptr_t)buf
- (uintptr_t)vbuf
, <=, *bufsizep
);
694 *bufsizep
= (uintptr_t)buf
- (uintptr_t)vbuf
;
695 *cookiep
= zap_cursor_serialize(&zc
);
696 zap_cursor_fini(&zc
);
701 * buf must be big enough (eg, 32 bytes)
704 id_to_fuidstr(zfsvfs_t
*zfsvfs
, const char *domain
, uid_t rid
,
705 char *buf
, boolean_t addok
)
710 if (domain
&& domain
[0]) {
711 domainid
= zfs_fuid_find_by_domain(zfsvfs
, domain
, NULL
, addok
);
713 return (SET_ERROR(ENOENT
));
715 fuid
= FUID_ENCODE(domainid
, rid
);
716 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
721 zfs_userspace_one(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
,
722 const char *domain
, uint64_t rid
, uint64_t *valp
)
730 if (!dmu_objset_userspace_present(zfsvfs
->z_os
))
731 return (SET_ERROR(ENOTSUP
));
733 obj
= zfs_userquota_prop_to_obj(zfsvfs
, type
);
737 err
= id_to_fuidstr(zfsvfs
, domain
, rid
, buf
, B_FALSE
);
741 err
= zap_lookup(zfsvfs
->z_os
, obj
, buf
, 8, 1, valp
);
748 zfs_set_userquota(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
,
749 const char *domain
, uint64_t rid
, uint64_t quota
)
755 boolean_t fuid_dirtied
;
757 if (type
!= ZFS_PROP_USERQUOTA
&& type
!= ZFS_PROP_GROUPQUOTA
)
758 return (SET_ERROR(EINVAL
));
760 if (zfsvfs
->z_version
< ZPL_VERSION_USERSPACE
)
761 return (SET_ERROR(ENOTSUP
));
763 objp
= (type
== ZFS_PROP_USERQUOTA
) ? &zfsvfs
->z_userquota_obj
:
764 &zfsvfs
->z_groupquota_obj
;
766 err
= id_to_fuidstr(zfsvfs
, domain
, rid
, buf
, B_TRUE
);
769 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
771 tx
= dmu_tx_create(zfsvfs
->z_os
);
772 dmu_tx_hold_zap(tx
, *objp
? *objp
: DMU_NEW_OBJECT
, B_TRUE
, NULL
);
774 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
775 zfs_userquota_prop_prefixes
[type
]);
778 zfs_fuid_txhold(zfsvfs
, tx
);
779 err
= dmu_tx_assign(tx
, TXG_WAIT
);
785 mutex_enter(&zfsvfs
->z_lock
);
787 *objp
= zap_create(zfsvfs
->z_os
, DMU_OT_USERGROUP_QUOTA
,
789 VERIFY(0 == zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
790 zfs_userquota_prop_prefixes
[type
], 8, 1, objp
, tx
));
792 mutex_exit(&zfsvfs
->z_lock
);
795 err
= zap_remove(zfsvfs
->z_os
, *objp
, buf
, tx
);
799 err
= zap_update(zfsvfs
->z_os
, *objp
, buf
, 8, 1, "a
, tx
);
803 zfs_fuid_sync(zfsvfs
, tx
);
809 zfs_fuid_overquota(zfsvfs_t
*zfsvfs
, boolean_t isgroup
, uint64_t fuid
)
812 uint64_t used
, quota
, usedobj
, quotaobj
;
815 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
816 quotaobj
= isgroup
? zfsvfs
->z_groupquota_obj
: zfsvfs
->z_userquota_obj
;
818 if (quotaobj
== 0 || zfsvfs
->z_replay
)
821 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
822 err
= zap_lookup(zfsvfs
->z_os
, quotaobj
, buf
, 8, 1, "a
);
826 err
= zap_lookup(zfsvfs
->z_os
, usedobj
, buf
, 8, 1, &used
);
829 return (used
>= quota
);
833 zfs_owner_overquota(zfsvfs_t
*zfsvfs
, znode_t
*zp
, boolean_t isgroup
)
838 quotaobj
= isgroup
? zfsvfs
->z_groupquota_obj
: zfsvfs
->z_userquota_obj
;
840 fuid
= isgroup
? zp
->z_gid
: zp
->z_uid
;
842 if (quotaobj
== 0 || zfsvfs
->z_replay
)
845 return (zfs_fuid_overquota(zfsvfs
, isgroup
, fuid
));
849 * Associate this zfsvfs with the given objset, which must be owned.
850 * This will cache a bunch of on-disk state from the objset in the
854 zfsvfs_init(zfsvfs_t
*zfsvfs
, objset_t
*os
)
859 zfsvfs
->z_max_blksz
= SPA_OLD_MAXBLOCKSIZE
;
860 zfsvfs
->z_show_ctldir
= ZFS_SNAPDIR_VISIBLE
;
863 error
= zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zfsvfs
->z_version
);
866 if (zfsvfs
->z_version
>
867 zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)))) {
868 (void) printf("Can't mount a version %lld file system "
869 "on a version %lld pool\n. Pool must be upgraded to mount "
870 "this file system.", (u_longlong_t
)zfsvfs
->z_version
,
871 (u_longlong_t
)spa_version(dmu_objset_spa(os
)));
872 return (SET_ERROR(ENOTSUP
));
874 error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &val
);
877 zfsvfs
->z_norm
= (int)val
;
879 error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &val
);
882 zfsvfs
->z_utf8
= (val
!= 0);
884 error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &val
);
887 zfsvfs
->z_case
= (uint_t
)val
;
890 * Fold case on file systems that are always or sometimes case
893 if (zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
894 zfsvfs
->z_case
== ZFS_CASE_MIXED
)
895 zfsvfs
->z_norm
|= U8_TEXTPREP_TOUPPER
;
897 zfsvfs
->z_use_fuids
= USE_FUIDS(zfsvfs
->z_version
, zfsvfs
->z_os
);
898 zfsvfs
->z_use_sa
= USE_SA(zfsvfs
->z_version
, zfsvfs
->z_os
);
901 if (zfsvfs
->z_use_sa
) {
902 /* should either have both of these objects or none */
903 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1,
909 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
910 &zfsvfs
->z_attr_table
);
914 if (zfsvfs
->z_version
>= ZPL_VERSION_SA
)
915 sa_register_update_callback(os
, zfs_sa_upgrade
);
917 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_ROOT_OBJ
, 8, 1,
921 ASSERT(zfsvfs
->z_root
!= 0);
923 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_UNLINKED_SET
, 8, 1,
924 &zfsvfs
->z_unlinkedobj
);
928 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
929 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
],
930 8, 1, &zfsvfs
->z_userquota_obj
);
932 zfsvfs
->z_userquota_obj
= 0;
936 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
937 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
],
938 8, 1, &zfsvfs
->z_groupquota_obj
);
940 zfsvfs
->z_groupquota_obj
= 0;
944 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
, 8, 1,
945 &zfsvfs
->z_fuid_obj
);
947 zfsvfs
->z_fuid_obj
= 0;
951 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SHARES_DIR
, 8, 1,
952 &zfsvfs
->z_shares_dir
);
954 zfsvfs
->z_shares_dir
= 0;
962 zfsvfs_create(const char *osname
, zfsvfs_t
**zfvp
)
968 zfsvfs
= kmem_zalloc(sizeof (zfsvfs_t
), KM_SLEEP
);
971 * We claim to always be readonly so we can open snapshots;
972 * other ZPL code will prevent us from writing to snapshots.
975 error
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_TRUE
, zfsvfs
, &os
);
977 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
981 error
= zfsvfs_create_impl(zfvp
, zfsvfs
, os
);
983 dmu_objset_disown(os
, zfsvfs
);
990 zfsvfs_create_impl(zfsvfs_t
**zfvp
, zfsvfs_t
*zfsvfs
, objset_t
*os
)
994 zfsvfs
->z_vfs
= NULL
;
995 zfsvfs
->z_parent
= zfsvfs
;
997 mutex_init(&zfsvfs
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
998 mutex_init(&zfsvfs
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
999 list_create(&zfsvfs
->z_all_znodes
, sizeof (znode_t
),
1000 offsetof(znode_t
, z_link_node
));
1001 rrm_init(&zfsvfs
->z_teardown_lock
, B_FALSE
);
1002 rw_init(&zfsvfs
->z_teardown_inactive_lock
, NULL
, RW_DEFAULT
, NULL
);
1003 rw_init(&zfsvfs
->z_fuid_lock
, NULL
, RW_DEFAULT
, NULL
);
1004 for (int i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1005 mutex_init(&zfsvfs
->z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
1007 error
= zfsvfs_init(zfsvfs
, os
);
1010 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
1019 zfsvfs_setup(zfsvfs_t
*zfsvfs
, boolean_t mounting
)
1023 error
= zfs_register_callbacks(zfsvfs
->z_vfs
);
1027 zfsvfs
->z_log
= zil_open(zfsvfs
->z_os
, zfs_get_data
);
1030 * If we are not mounting (ie: online recv), then we don't
1031 * have to worry about replaying the log as we blocked all
1032 * operations out since we closed the ZIL.
1038 * During replay we remove the read only flag to
1039 * allow replays to succeed.
1041 readonly
= zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
;
1043 zfsvfs
->z_vfs
->vfs_flag
&= ~VFS_RDONLY
;
1045 zfs_unlinked_drain(zfsvfs
);
1048 * Parse and replay the intent log.
1050 * Because of ziltest, this must be done after
1051 * zfs_unlinked_drain(). (Further note: ziltest
1052 * doesn't use readonly mounts, where
1053 * zfs_unlinked_drain() isn't called.) This is because
1054 * ziltest causes spa_sync() to think it's committed,
1055 * but actually it is not, so the intent log contains
1056 * many txg's worth of changes.
1058 * In particular, if object N is in the unlinked set in
1059 * the last txg to actually sync, then it could be
1060 * actually freed in a later txg and then reallocated
1061 * in a yet later txg. This would write a "create
1062 * object N" record to the intent log. Normally, this
1063 * would be fine because the spa_sync() would have
1064 * written out the fact that object N is free, before
1065 * we could write the "create object N" intent log
1068 * But when we are in ziltest mode, we advance the "open
1069 * txg" without actually spa_sync()-ing the changes to
1070 * disk. So we would see that object N is still
1071 * allocated and in the unlinked set, and there is an
1072 * intent log record saying to allocate it.
1074 if (spa_writeable(dmu_objset_spa(zfsvfs
->z_os
))) {
1075 if (zil_replay_disable
) {
1076 zil_destroy(zfsvfs
->z_log
, B_FALSE
);
1078 zfsvfs
->z_replay
= B_TRUE
;
1079 zil_replay(zfsvfs
->z_os
, zfsvfs
,
1081 zfsvfs
->z_replay
= B_FALSE
;
1084 zfsvfs
->z_vfs
->vfs_flag
|= readonly
; /* restore readonly bit */
1088 * Set the objset user_ptr to track its zfsvfs.
1090 mutex_enter(&zfsvfs
->z_os
->os_user_ptr_lock
);
1091 dmu_objset_set_user(zfsvfs
->z_os
, zfsvfs
);
1092 mutex_exit(&zfsvfs
->z_os
->os_user_ptr_lock
);
1098 zfsvfs_free(zfsvfs_t
*zfsvfs
)
1101 extern krwlock_t zfsvfs_lock
; /* in zfs_znode.c */
1104 * This is a barrier to prevent the filesystem from going away in
1105 * zfs_znode_move() until we can safely ensure that the filesystem is
1106 * not unmounted. We consider the filesystem valid before the barrier
1107 * and invalid after the barrier.
1109 rw_enter(&zfsvfs_lock
, RW_READER
);
1110 rw_exit(&zfsvfs_lock
);
1112 zfs_fuid_destroy(zfsvfs
);
1114 mutex_destroy(&zfsvfs
->z_znodes_lock
);
1115 mutex_destroy(&zfsvfs
->z_lock
);
1116 list_destroy(&zfsvfs
->z_all_znodes
);
1117 rrm_destroy(&zfsvfs
->z_teardown_lock
);
1118 rw_destroy(&zfsvfs
->z_teardown_inactive_lock
);
1119 rw_destroy(&zfsvfs
->z_fuid_lock
);
1120 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
1121 mutex_destroy(&zfsvfs
->z_hold_mtx
[i
]);
1122 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
1126 zfs_set_fuid_feature(zfsvfs_t
*zfsvfs
)
1128 zfsvfs
->z_use_fuids
= USE_FUIDS(zfsvfs
->z_version
, zfsvfs
->z_os
);
1129 if (zfsvfs
->z_vfs
) {
1130 if (zfsvfs
->z_use_fuids
) {
1131 vfs_set_feature(zfsvfs
->z_vfs
, VFSFT_XVATTR
);
1132 vfs_set_feature(zfsvfs
->z_vfs
, VFSFT_SYSATTR_VIEWS
);
1133 vfs_set_feature(zfsvfs
->z_vfs
, VFSFT_ACEMASKONACCESS
);
1134 vfs_set_feature(zfsvfs
->z_vfs
, VFSFT_ACLONCREATE
);
1135 vfs_set_feature(zfsvfs
->z_vfs
, VFSFT_ACCESS_FILTER
);
1136 vfs_set_feature(zfsvfs
->z_vfs
, VFSFT_REPARSE
);
1138 vfs_clear_feature(zfsvfs
->z_vfs
, VFSFT_XVATTR
);
1139 vfs_clear_feature(zfsvfs
->z_vfs
, VFSFT_SYSATTR_VIEWS
);
1140 vfs_clear_feature(zfsvfs
->z_vfs
, VFSFT_ACEMASKONACCESS
);
1141 vfs_clear_feature(zfsvfs
->z_vfs
, VFSFT_ACLONCREATE
);
1142 vfs_clear_feature(zfsvfs
->z_vfs
, VFSFT_ACCESS_FILTER
);
1143 vfs_clear_feature(zfsvfs
->z_vfs
, VFSFT_REPARSE
);
1146 zfsvfs
->z_use_sa
= USE_SA(zfsvfs
->z_version
, zfsvfs
->z_os
);
1150 zfs_domount(vfs_t
*vfsp
, char *osname
)
1153 uint64_t recordsize
, fsid_guid
;
1160 error
= zfsvfs_create(osname
, &zfsvfs
);
1163 zfsvfs
->z_vfs
= vfsp
;
1165 /* Initialize the generic filesystem structure. */
1166 vfsp
->vfs_bcount
= 0;
1167 vfsp
->vfs_data
= NULL
;
1169 if (zfs_create_unique_device(&mount_dev
) == -1) {
1170 error
= SET_ERROR(ENODEV
);
1173 ASSERT(vfs_devismounted(mount_dev
) == 0);
1175 if (error
= dsl_prop_get_integer(osname
, "recordsize", &recordsize
,
1179 vfsp
->vfs_dev
= mount_dev
;
1180 vfsp
->vfs_fstype
= zfsfstype
;
1181 vfsp
->vfs_bsize
= recordsize
;
1182 vfsp
->vfs_flag
|= VFS_NOTRUNC
;
1183 vfsp
->vfs_data
= zfsvfs
;
1186 * The fsid is 64 bits, composed of an 8-bit fs type, which
1187 * separates our fsid from any other filesystem types, and a
1188 * 56-bit objset unique ID. The objset unique ID is unique to
1189 * all objsets open on this system, provided by unique_create().
1190 * The 8-bit fs type must be put in the low bits of fsid[1]
1191 * because that's where other Solaris filesystems put it.
1193 fsid_guid
= dmu_objset_fsid_guid(zfsvfs
->z_os
);
1194 ASSERT((fsid_guid
& ~((1ULL<<56)-1)) == 0);
1195 vfsp
->vfs_fsid
.val
[0] = fsid_guid
;
1196 vfsp
->vfs_fsid
.val
[1] = ((fsid_guid
>>32) << 8) |
1200 * Set features for file system.
1202 zfs_set_fuid_feature(zfsvfs
);
1203 if (zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) {
1204 vfs_set_feature(vfsp
, VFSFT_DIRENTFLAGS
);
1205 vfs_set_feature(vfsp
, VFSFT_CASEINSENSITIVE
);
1206 vfs_set_feature(vfsp
, VFSFT_NOCASESENSITIVE
);
1207 } else if (zfsvfs
->z_case
== ZFS_CASE_MIXED
) {
1208 vfs_set_feature(vfsp
, VFSFT_DIRENTFLAGS
);
1209 vfs_set_feature(vfsp
, VFSFT_CASEINSENSITIVE
);
1211 vfs_set_feature(vfsp
, VFSFT_ZEROCOPY_SUPPORTED
);
1213 if (dmu_objset_is_snapshot(zfsvfs
->z_os
)) {
1216 atime_changed_cb(zfsvfs
, B_FALSE
);
1217 readonly_changed_cb(zfsvfs
, B_TRUE
);
1218 if (error
= dsl_prop_get_integer(osname
, "xattr", &pval
, NULL
))
1220 xattr_changed_cb(zfsvfs
, pval
);
1221 zfsvfs
->z_issnap
= B_TRUE
;
1222 zfsvfs
->z_os
->os_sync
= ZFS_SYNC_DISABLED
;
1224 mutex_enter(&zfsvfs
->z_os
->os_user_ptr_lock
);
1225 dmu_objset_set_user(zfsvfs
->z_os
, zfsvfs
);
1226 mutex_exit(&zfsvfs
->z_os
->os_user_ptr_lock
);
1228 error
= zfsvfs_setup(zfsvfs
, B_TRUE
);
1231 if (!zfsvfs
->z_issnap
)
1232 zfsctl_create(zfsvfs
);
1235 dmu_objset_disown(zfsvfs
->z_os
, zfsvfs
);
1236 zfsvfs_free(zfsvfs
);
1238 atomic_inc_32(&zfs_active_fs_count
);
1245 zfs_unregister_callbacks(zfsvfs_t
*zfsvfs
)
1247 objset_t
*os
= zfsvfs
->z_os
;
1249 if (!dmu_objset_is_snapshot(os
))
1250 dsl_prop_unregister_all(dmu_objset_ds(os
), zfsvfs
);
1254 * Convert a decimal digit string to a uint64_t integer.
1257 str_to_uint64(char *str
, uint64_t *objnum
)
1262 if (*str
< '0' || *str
> '9')
1263 return (SET_ERROR(EINVAL
));
1265 num
= num
*10 + *str
++ - '0';
1273 * The boot path passed from the boot loader is in the form of
1274 * "rootpool-name/root-filesystem-object-number'. Convert this
1275 * string to a dataset name: "rootpool-name/root-filesystem-name".
1278 zfs_parse_bootfs(char *bpath
, char *outpath
)
1284 if (*bpath
== 0 || *bpath
== '/')
1285 return (SET_ERROR(EINVAL
));
1287 (void) strcpy(outpath
, bpath
);
1289 slashp
= strchr(bpath
, '/');
1291 /* if no '/', just return the pool name */
1292 if (slashp
== NULL
) {
1296 /* if not a number, just return the root dataset name */
1297 if (str_to_uint64(slashp
+1, &objnum
)) {
1302 error
= dsl_dsobj_to_dsname(bpath
, objnum
, outpath
);
1309 zfs_mountroot(vfs_t
*vfsp
, enum whymountroot why
)
1312 static int zfsrootdone
= 0;
1313 zfsvfs_t
*zfsvfs
= NULL
;
1322 * The filesystem that we mount as root is defined in the
1323 * boot property "zfs-bootfs" with a format of
1324 * "poolname/root-dataset-objnum".
1326 if (why
== ROOT_INIT
) {
1328 return (SET_ERROR(EBUSY
));
1330 * the process of doing a spa_load will require the
1331 * clock to be set before we could (for example) do
1332 * something better by looking at the timestamp on
1333 * an uberblock, so just set it to -1.
1337 if ((zfs_bootfs
= spa_get_bootprop("zfs-bootfs")) == NULL
) {
1338 cmn_err(CE_NOTE
, "spa_get_bootfs: can not get "
1340 return (SET_ERROR(EINVAL
));
1342 zfs_devid
= spa_get_bootprop("diskdevid");
1343 error
= spa_import_rootpool(rootfs
.bo_name
, zfs_devid
);
1345 spa_free_bootprop(zfs_devid
);
1347 spa_free_bootprop(zfs_bootfs
);
1348 cmn_err(CE_NOTE
, "spa_import_rootpool: error %d",
1352 if (error
= zfs_parse_bootfs(zfs_bootfs
, rootfs
.bo_name
)) {
1353 spa_free_bootprop(zfs_bootfs
);
1354 cmn_err(CE_NOTE
, "zfs_parse_bootfs: error %d",
1359 spa_free_bootprop(zfs_bootfs
);
1361 if (error
= vfs_lock(vfsp
))
1364 if (error
= zfs_domount(vfsp
, rootfs
.bo_name
)) {
1365 cmn_err(CE_NOTE
, "zfs_domount: error %d", error
);
1369 zfsvfs
= (zfsvfs_t
*)vfsp
->vfs_data
;
1371 if (error
= zfs_zget(zfsvfs
, zfsvfs
->z_root
, &zp
)) {
1372 cmn_err(CE_NOTE
, "zfs_zget: error %d", error
);
1377 mutex_enter(&vp
->v_lock
);
1378 vp
->v_flag
|= VROOT
;
1379 mutex_exit(&vp
->v_lock
);
1383 * Leave rootvp held. The root file system is never unmounted.
1387 (vfsp
->vfs_flag
& VFS_RDONLY
) ? MS_RDONLY
: 0);
1391 } else if (why
== ROOT_REMOUNT
) {
1392 readonly_changed_cb(vfsp
->vfs_data
, B_FALSE
);
1393 vfsp
->vfs_flag
|= VFS_REMOUNT
;
1395 /* refresh mount options */
1396 zfs_unregister_callbacks(vfsp
->vfs_data
);
1397 return (zfs_register_callbacks(vfsp
));
1399 } else if (why
== ROOT_UNMOUNT
) {
1400 zfs_unregister_callbacks((zfsvfs_t
*)vfsp
->vfs_data
);
1401 (void) zfs_sync(vfsp
, 0, 0);
1406 * if "why" is equal to anything else other than ROOT_INIT,
1407 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1409 return (SET_ERROR(ENOTSUP
));
1414 zfs_mount(vfs_t
*vfsp
, vnode_t
*mvp
, struct mounta
*uap
, cred_t
*cr
)
1419 uio_seg_t fromspace
= (uap
->flags
& MS_SYSSPACE
) ?
1420 UIO_SYSSPACE
: UIO_USERSPACE
;
1423 if (mvp
->v_type
!= VDIR
)
1424 return (SET_ERROR(ENOTDIR
));
1426 mutex_enter(&mvp
->v_lock
);
1427 if ((uap
->flags
& MS_REMOUNT
) == 0 &&
1428 (uap
->flags
& MS_OVERLAY
) == 0 &&
1429 (mvp
->v_count
!= 1 || (mvp
->v_flag
& VROOT
))) {
1430 mutex_exit(&mvp
->v_lock
);
1431 return (SET_ERROR(EBUSY
));
1433 mutex_exit(&mvp
->v_lock
);
1436 * ZFS does not support passing unparsed data in via MS_DATA.
1437 * Users should use the MS_OPTIONSTR interface; this means
1438 * that all option parsing is already done and the options struct
1439 * can be interrogated.
1441 if ((uap
->flags
& MS_DATA
) && uap
->datalen
> 0)
1442 return (SET_ERROR(EINVAL
));
1445 * Get the objset name (the "special" mount argument).
1447 if (error
= pn_get(uap
->spec
, fromspace
, &spn
))
1450 osname
= spn
.pn_path
;
1453 * Check for mount privilege?
1455 * If we don't have privilege then see if
1456 * we have local permission to allow it
1458 error
= secpolicy_fs_mount(cr
, mvp
, vfsp
);
1460 if (dsl_deleg_access(osname
, ZFS_DELEG_PERM_MOUNT
, cr
) == 0) {
1464 * Make sure user is the owner of the mount point
1465 * or has sufficient privileges.
1468 vattr
.va_mask
= AT_UID
;
1470 if (fop_getattr(mvp
, &vattr
, 0, cr
, NULL
)) {
1474 if (secpolicy_vnode_owner(cr
, vattr
.va_uid
) != 0 &&
1475 fop_access(mvp
, VWRITE
, 0, cr
, NULL
) != 0) {
1478 secpolicy_fs_mount_clearopts(cr
, vfsp
);
1485 * Refuse to mount a filesystem if we are in a local zone and the
1486 * dataset is not visible.
1488 if (!INGLOBALZONE(curproc
) &&
1489 (!zone_dataset_visible(osname
, &canwrite
) || !canwrite
)) {
1490 error
= SET_ERROR(EPERM
);
1495 * When doing a remount, we simply refresh our temporary properties
1496 * according to those options set in the current VFS options.
1498 if (uap
->flags
& MS_REMOUNT
) {
1499 /* refresh mount options */
1500 zfs_unregister_callbacks(vfsp
->vfs_data
);
1501 error
= zfs_register_callbacks(vfsp
);
1505 error
= zfs_domount(vfsp
, osname
);
1508 * Add an extra VFS_HOLD on our parent vfs so that it can't
1509 * disappear due to a forced unmount.
1511 if (error
== 0 && ((zfsvfs_t
*)vfsp
->vfs_data
)->z_issnap
)
1512 VFS_HOLD(mvp
->v_vfsp
);
1520 zfs_statvfs(vfs_t
*vfsp
, struct statvfs64
*statp
)
1522 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
1524 uint64_t refdbytes
, availbytes
, usedobjs
, availobjs
;
1528 dmu_objset_space(zfsvfs
->z_os
,
1529 &refdbytes
, &availbytes
, &usedobjs
, &availobjs
);
1532 * The underlying storage pool actually uses multiple block sizes.
1533 * We report the fragsize as the smallest block size we support,
1534 * and we report our blocksize as the filesystem's maximum blocksize.
1536 statp
->f_frsize
= 1UL << SPA_MINBLOCKSHIFT
;
1537 statp
->f_bsize
= zfsvfs
->z_max_blksz
;
1540 * The following report "total" blocks of various kinds in the
1541 * file system, but reported in terms of f_frsize - the
1545 statp
->f_blocks
= (refdbytes
+ availbytes
) >> SPA_MINBLOCKSHIFT
;
1546 statp
->f_bfree
= availbytes
>> SPA_MINBLOCKSHIFT
;
1547 statp
->f_bavail
= statp
->f_bfree
; /* no root reservation */
1550 * statvfs() should really be called statufs(), because it assumes
1551 * static metadata. ZFS doesn't preallocate files, so the best
1552 * we can do is report the max that could possibly fit in f_files,
1553 * and that minus the number actually used in f_ffree.
1554 * For f_ffree, report the smaller of the number of object available
1555 * and the number of blocks (each object will take at least a block).
1557 statp
->f_ffree
= MIN(availobjs
, statp
->f_bfree
);
1558 statp
->f_favail
= statp
->f_ffree
; /* no "root reservation" */
1559 statp
->f_files
= statp
->f_ffree
+ usedobjs
;
1561 (void) cmpldev(&d32
, vfsp
->vfs_dev
);
1562 statp
->f_fsid
= d32
;
1565 * We're a zfs filesystem.
1567 (void) strcpy(statp
->f_basetype
, vfssw
[vfsp
->vfs_fstype
].vsw_name
);
1569 statp
->f_flag
= vf_to_stf(vfsp
->vfs_flag
);
1571 statp
->f_namemax
= MAXNAMELEN
- 1;
1574 * We have all of 32 characters to stuff a string here.
1575 * Is there anything useful we could/should provide?
1577 bzero(statp
->f_fstr
, sizeof (statp
->f_fstr
));
1584 zfs_root(vfs_t
*vfsp
, vnode_t
**vpp
)
1586 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
1592 error
= zfs_zget(zfsvfs
, zfsvfs
->z_root
, &rootzp
);
1594 *vpp
= ZTOV(rootzp
);
1601 * Teardown the zfsvfs::z_os.
1603 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1604 * and 'z_teardown_inactive_lock' held.
1607 zfsvfs_teardown(zfsvfs_t
*zfsvfs
, boolean_t unmounting
)
1611 rrm_enter(&zfsvfs
->z_teardown_lock
, RW_WRITER
, FTAG
);
1615 * We purge the parent filesystem's vfsp as the parent
1616 * filesystem and all of its snapshots have their vnode's
1617 * v_vfsp set to the parent's filesystem's vfsp. Note,
1618 * 'z_parent' is self referential for non-snapshots.
1620 (void) dnlc_purge_vfsp(zfsvfs
->z_parent
->z_vfs
, 0);
1624 * Close the zil. NB: Can't close the zil while zfs_inactive
1625 * threads are blocked as zil_close can call zfs_inactive.
1627 if (zfsvfs
->z_log
) {
1628 zil_close(zfsvfs
->z_log
);
1629 zfsvfs
->z_log
= NULL
;
1632 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_WRITER
);
1635 * If we are not unmounting (ie: online recv) and someone already
1636 * unmounted this file system while we were doing the switcheroo,
1637 * or a reopen of z_os failed then just bail out now.
1639 if (!unmounting
&& (zfsvfs
->z_unmounted
|| zfsvfs
->z_os
== NULL
)) {
1640 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1641 rrm_exit(&zfsvfs
->z_teardown_lock
, FTAG
);
1642 return (SET_ERROR(EIO
));
1646 * At this point there are no vops active, and any new vops will
1647 * fail with EIO since we have z_teardown_lock for writer (only
1648 * relavent for forced unmount).
1650 * Release all holds on dbufs.
1652 mutex_enter(&zfsvfs
->z_znodes_lock
);
1653 for (zp
= list_head(&zfsvfs
->z_all_znodes
); zp
!= NULL
;
1654 zp
= list_next(&zfsvfs
->z_all_znodes
, zp
))
1656 ASSERT(ZTOV(zp
)->v_count
> 0);
1657 zfs_znode_dmu_fini(zp
);
1659 mutex_exit(&zfsvfs
->z_znodes_lock
);
1662 * If we are unmounting, set the unmounted flag and let new vops
1663 * unblock. zfs_inactive will have the unmounted behavior, and all
1664 * other vops will fail with EIO.
1667 zfsvfs
->z_unmounted
= B_TRUE
;
1668 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1669 rrm_exit(&zfsvfs
->z_teardown_lock
, FTAG
);
1673 * z_os will be NULL if there was an error in attempting to reopen
1674 * zfsvfs, so just return as the properties had already been
1675 * unregistered and cached data had been evicted before.
1677 if (zfsvfs
->z_os
== NULL
)
1681 * Unregister properties.
1683 zfs_unregister_callbacks(zfsvfs
);
1688 if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs
->z_os
)) &&
1689 !(zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
))
1690 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), 0);
1691 dmu_objset_evict_dbufs(zfsvfs
->z_os
);
1698 zfs_umount(vfs_t
*vfsp
, int fflag
, cred_t
*cr
)
1700 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
1704 ret
= secpolicy_fs_unmount(cr
, vfsp
);
1706 if (dsl_deleg_access((char *)refstr_value(vfsp
->vfs_resource
),
1707 ZFS_DELEG_PERM_MOUNT
, cr
))
1712 * We purge the parent filesystem's vfsp as the parent filesystem
1713 * and all of its snapshots have their vnode's v_vfsp set to the
1714 * parent's filesystem's vfsp. Note, 'z_parent' is self
1715 * referential for non-snapshots.
1717 (void) dnlc_purge_vfsp(zfsvfs
->z_parent
->z_vfs
, 0);
1720 * Unmount any snapshots mounted under .zfs before unmounting the
1723 if (zfsvfs
->z_ctldir
!= NULL
&&
1724 (ret
= zfsctl_umount_snapshots(vfsp
, fflag
, cr
)) != 0) {
1728 if (!(fflag
& MS_FORCE
)) {
1730 * Check the number of active vnodes in the file system.
1731 * Our count is maintained in the vfs structure, but the
1732 * number is off by 1 to indicate a hold on the vfs
1735 * The '.zfs' directory maintains a reference of its
1736 * own, and any active references underneath are
1737 * reflected in the vnode count.
1739 if (zfsvfs
->z_ctldir
== NULL
) {
1740 if (vfsp
->vfs_count
> 1)
1741 return (SET_ERROR(EBUSY
));
1743 if (vfsp
->vfs_count
> 2 ||
1744 zfsvfs
->z_ctldir
->v_count
> 1)
1745 return (SET_ERROR(EBUSY
));
1749 vfsp
->vfs_flag
|= VFS_UNMOUNTED
;
1751 VERIFY(zfsvfs_teardown(zfsvfs
, B_TRUE
) == 0);
1755 * z_os will be NULL if there was an error in
1756 * attempting to reopen zfsvfs.
1760 * Unset the objset user_ptr.
1762 mutex_enter(&os
->os_user_ptr_lock
);
1763 dmu_objset_set_user(os
, NULL
);
1764 mutex_exit(&os
->os_user_ptr_lock
);
1767 * Finally release the objset
1769 dmu_objset_disown(os
, zfsvfs
);
1773 * We can now safely destroy the '.zfs' directory node.
1775 if (zfsvfs
->z_ctldir
!= NULL
)
1776 zfsctl_destroy(zfsvfs
);
1782 zfs_vget(vfs_t
*vfsp
, vnode_t
**vpp
, fid_t
*fidp
)
1784 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
1786 uint64_t object
= 0;
1787 uint64_t fid_gen
= 0;
1796 if (fidp
->fid_len
== LONG_FID_LEN
) {
1797 zfid_long_t
*zlfid
= (zfid_long_t
*)fidp
;
1798 uint64_t objsetid
= 0;
1799 uint64_t setgen
= 0;
1801 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
1802 objsetid
|= ((uint64_t)zlfid
->zf_setid
[i
]) << (8 * i
);
1804 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
1805 setgen
|= ((uint64_t)zlfid
->zf_setgen
[i
]) << (8 * i
);
1809 err
= zfsctl_lookup_objset(vfsp
, objsetid
, &zfsvfs
);
1811 return (SET_ERROR(EINVAL
));
1815 if (fidp
->fid_len
== SHORT_FID_LEN
|| fidp
->fid_len
== LONG_FID_LEN
) {
1816 zfid_short_t
*zfid
= (zfid_short_t
*)fidp
;
1818 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
1819 object
|= ((uint64_t)zfid
->zf_object
[i
]) << (8 * i
);
1821 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
1822 fid_gen
|= ((uint64_t)zfid
->zf_gen
[i
]) << (8 * i
);
1825 return (SET_ERROR(EINVAL
));
1828 /* A zero fid_gen means we are in the .zfs control directories */
1830 (object
== ZFSCTL_INO_ROOT
|| object
== ZFSCTL_INO_SNAPDIR
)) {
1831 *vpp
= zfsvfs
->z_ctldir
;
1832 ASSERT(*vpp
!= NULL
);
1833 if (object
== ZFSCTL_INO_SNAPDIR
) {
1834 VERIFY(zfsctl_root_lookup(*vpp
, "snapshot", vpp
, NULL
,
1835 0, NULL
, NULL
, NULL
, NULL
, NULL
) == 0);
1843 gen_mask
= -1ULL >> (64 - 8 * i
);
1845 dprintf("getting %llu [%u mask %llx]\n", object
, fid_gen
, gen_mask
);
1846 if (err
= zfs_zget(zfsvfs
, object
, &zp
)) {
1850 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
), &zp_gen
,
1852 zp_gen
= zp_gen
& gen_mask
;
1855 if (zp
->z_unlinked
|| zp_gen
!= fid_gen
) {
1856 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen
, fid_gen
);
1859 return (SET_ERROR(EINVAL
));
1868 * Block out VOPs and close zfsvfs_t::z_os
1870 * Note, if successful, then we return with the 'z_teardown_lock' and
1871 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1872 * dataset and objset intact so that they can be atomically handed off during
1873 * a subsequent rollback or recv operation and the resume thereafter.
1876 zfs_suspend_fs(zfsvfs_t
*zfsvfs
)
1880 if ((error
= zfsvfs_teardown(zfsvfs
, B_FALSE
)) != 0)
1887 * Rebuild SA and release VOPs. Note that ownership of the underlying dataset
1888 * is an invariant across any of the operations that can be performed while the
1889 * filesystem was suspended. Whether it succeeded or failed, the preconditions
1890 * are the same: the relevant objset and associated dataset are owned by
1891 * zfsvfs, held, and long held on entry.
1894 zfs_resume_fs(zfsvfs_t
*zfsvfs
, dsl_dataset_t
*ds
)
1899 ASSERT(RRM_WRITE_HELD(&zfsvfs
->z_teardown_lock
));
1900 ASSERT(RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
));
1903 * We already own this, so just update the objset_t, as the one we
1904 * had before may have been evicted.
1907 VERIFY3P(ds
->ds_owner
, ==, zfsvfs
);
1908 VERIFY(dsl_dataset_long_held(ds
));
1909 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1911 err
= zfsvfs_init(zfsvfs
, os
);
1915 VERIFY(zfsvfs_setup(zfsvfs
, B_FALSE
) == 0);
1917 zfs_set_fuid_feature(zfsvfs
);
1920 * Attempt to re-establish all the active znodes with
1921 * their dbufs. If a zfs_rezget() fails, then we'll let
1922 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1923 * when they try to use their znode.
1925 mutex_enter(&zfsvfs
->z_znodes_lock
);
1926 for (zp
= list_head(&zfsvfs
->z_all_znodes
); zp
;
1927 zp
= list_next(&zfsvfs
->z_all_znodes
, zp
)) {
1928 (void) zfs_rezget(zp
);
1930 mutex_exit(&zfsvfs
->z_znodes_lock
);
1933 /* release the VOPs */
1934 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1935 rrm_exit(&zfsvfs
->z_teardown_lock
, FTAG
);
1939 * Since we couldn't setup the sa framework, try to force
1940 * unmount this file system.
1942 if (vn_vfswlock(zfsvfs
->z_vfs
->vfs_vnodecovered
) == 0)
1943 (void) dounmount(zfsvfs
->z_vfs
, MS_FORCE
, CRED());
1949 zfs_freevfs(vfs_t
*vfsp
)
1951 zfsvfs_t
*zfsvfs
= vfsp
->vfs_data
;
1954 * If this is a snapshot, we have an extra VFS_HOLD on our parent
1955 * from zfs_mount(). Release it here. If we came through
1956 * zfs_mountroot() instead, we didn't grab an extra hold, so
1957 * skip the VFS_RELE for rootvfs.
1959 if (zfsvfs
->z_issnap
&& (vfsp
!= rootvfs
))
1960 VFS_RELE(zfsvfs
->z_parent
->z_vfs
);
1962 zfsvfs_free(zfsvfs
);
1964 atomic_dec_32(&zfs_active_fs_count
);
1968 * VFS_INIT() initialization. Note that there is no VFS_FINI(),
1969 * so we can't safely do any non-idempotent initialization here.
1970 * Leave that to zfs_init() and zfs_fini(), which are called
1971 * from the module's _init() and _fini() entry points.
1975 zfs_vfsinit(int fstype
, char *name
)
1982 * Setup vfsops and vnodeops tables.
1984 error
= vfs_setfsops(fstype
, &zfs_vfsops
);
1986 cmn_err(CE_WARN
, "zfs: bad fstype");
1989 error
= zfs_create_op_tables();
1991 zfs_remove_op_tables();
1992 cmn_err(CE_WARN
, "zfs: bad vnode ops template");
1993 (void) vfs_freevfsops_by_type(zfsfstype
);
1997 mutex_init(&zfs_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2000 * Unique major number for all zfs mounts.
2001 * If we run out of 32-bit minors, we'll getudev() another major.
2003 zfs_major
= ddi_name_to_major(ZFS_DRIVER
);
2004 zfs_minor
= ZFS_MIN_MINOR
;
2013 * Initialize .zfs directory structures
2018 * Initialize znode cache, vnode ops, etc...
2022 dmu_objset_register_type(DMU_OST_ZFS
, zfs_space_delta_cb
);
2035 return (zfs_active_fs_count
!= 0);
2039 zfs_set_version(zfsvfs_t
*zfsvfs
, uint64_t newvers
)
2042 objset_t
*os
= zfsvfs
->z_os
;
2045 if (newvers
< ZPL_VERSION_INITIAL
|| newvers
> ZPL_VERSION
)
2046 return (SET_ERROR(EINVAL
));
2048 if (newvers
< zfsvfs
->z_version
)
2049 return (SET_ERROR(EINVAL
));
2051 if (zfs_spa_version_map(newvers
) >
2052 spa_version(dmu_objset_spa(zfsvfs
->z_os
)))
2053 return (SET_ERROR(ENOTSUP
));
2055 tx
= dmu_tx_create(os
);
2056 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_FALSE
, ZPL_VERSION_STR
);
2057 if (newvers
>= ZPL_VERSION_SA
&& !zfsvfs
->z_use_sa
) {
2058 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
2060 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
2062 error
= dmu_tx_assign(tx
, TXG_WAIT
);
2068 error
= zap_update(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
2069 8, 1, &newvers
, tx
);
2076 if (newvers
>= ZPL_VERSION_SA
&& !zfsvfs
->z_use_sa
) {
2079 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs
->z_os
)), >=,
2081 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
2082 DMU_OT_NONE
, 0, tx
);
2084 error
= zap_add(os
, MASTER_NODE_OBJ
,
2085 ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
2088 VERIFY(0 == sa_set_sa_object(os
, sa_obj
));
2089 sa_register_update_callback(os
, zfs_sa_upgrade
);
2092 spa_history_log_internal_ds(dmu_objset_ds(os
), "upgrade", tx
,
2093 "from %llu to %llu", zfsvfs
->z_version
, newvers
);
2097 zfsvfs
->z_version
= newvers
;
2099 zfs_set_fuid_feature(zfsvfs
);
2105 * Read a property stored within the master node.
2108 zfs_get_zplprop(objset_t
*os
, zfs_prop_t prop
, uint64_t *value
)
2114 * Look up the file system's value for the property. For the
2115 * version property, we look up a slightly different string.
2117 if (prop
== ZFS_PROP_VERSION
)
2118 pname
= ZPL_VERSION_STR
;
2120 pname
= zfs_prop_to_name(prop
);
2123 ASSERT3U(os
->os_phys
->os_type
, ==, DMU_OST_ZFS
);
2124 error
= zap_lookup(os
, MASTER_NODE_OBJ
, pname
, 8, 1, value
);
2127 if (error
== ENOENT
) {
2128 /* No value set, use the default value */
2130 case ZFS_PROP_VERSION
:
2131 *value
= ZPL_VERSION
;
2133 case ZFS_PROP_NORMALIZE
:
2134 case ZFS_PROP_UTF8ONLY
:
2138 *value
= ZFS_CASE_SENSITIVE
;
2149 * Return true if the coresponding vfs's unmounted flag is set.
2150 * Otherwise return false.
2151 * If this function returns true we know VFS unmount has been initiated.
2154 zfs_get_vfs_flag_unmounted(objset_t
*os
)
2157 boolean_t unmounted
= B_FALSE
;
2159 ASSERT(dmu_objset_type(os
) == DMU_OST_ZFS
);
2161 mutex_enter(&os
->os_user_ptr_lock
);
2162 zfvp
= dmu_objset_get_user(os
);
2163 if (zfvp
!= NULL
&& zfvp
->z_vfs
!= NULL
&&
2164 (zfvp
->z_vfs
->vfs_flag
& VFS_UNMOUNTED
))
2166 mutex_exit(&os
->os_user_ptr_lock
);
2171 static vfsdef_t vfw
= {
2175 VSW_HASPROTO
|VSW_CANRWRO
|VSW_CANREMOUNT
|VSW_VOLATILEDEV
|VSW_STATS
|
2180 struct modlfs zfs_modlfs
= {
2181 &mod_fsops
, "ZFS filesystem version " SPA_VERSION_STRING
, &vfw