]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_vfsops.c
MFC r241286,r242135: zfs_mount: taste geom providers for root pool config
[FreeBSD/stable/8.git] / sys / cddl / contrib / opensolaris / uts / common / fs / 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) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
24  * All rights reserved.
25  */
26
27 /* Portions Copyright 2010 Robert Milkowski */
28
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/sysmacros.h>
34 #include <sys/kmem.h>
35 #include <sys/acl.h>
36 #include <sys/vnode.h>
37 #include <sys/vfs.h>
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
40 #include <sys/cmn_err.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/zfs_dir.h>
43 #include <sys/zil.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/dmu.h>
46 #include <sys/dsl_prop.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_deleg.h>
49 #include <sys/spa.h>
50 #include <sys/zap.h>
51 #include <sys/sa.h>
52 #include <sys/varargs.h>
53 #include <sys/policy.h>
54 #include <sys/atomic.h>
55 #include <sys/zfs_ioctl.h>
56 #include <sys/zfs_ctldir.h>
57 #include <sys/zfs_fuid.h>
58 #include <sys/sunddi.h>
59 #include <sys/dnlc.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/spa_boot.h>
62 #include <sys/sa.h>
63 #include "zfs_comutil.h"
64
65 struct mtx zfs_debug_mtx;
66 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
67
68 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
69
70 int zfs_super_owner;
71 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
72     "File system owner can perform privileged operation on his file systems");
73
74 int zfs_debug_level;
75 TUNABLE_INT("vfs.zfs.debug", &zfs_debug_level);
76 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RW, &zfs_debug_level, 0,
77     "Debug level");
78
79 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
80 static int zfs_version_acl = ZFS_ACL_VERSION;
81 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
82     "ZFS_ACL_VERSION");
83 static int zfs_version_spa = SPA_VERSION;
84 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
85     "SPA_VERSION");
86 static int zfs_version_zpl = ZPL_VERSION;
87 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
88     "ZPL_VERSION");
89
90 static int zfs_mount(vfs_t *vfsp);
91 static int zfs_umount(vfs_t *vfsp, int fflag);
92 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
93 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
94 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
95 static int zfs_sync(vfs_t *vfsp, int waitfor);
96 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
97     struct ucred **credanonp, int *numsecflavors, int **secflavors);
98 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp);
99 static void zfs_objset_close(zfsvfs_t *zfsvfs);
100 static void zfs_freevfs(vfs_t *vfsp);
101
102 static struct vfsops zfs_vfsops = {
103         .vfs_mount =            zfs_mount,
104         .vfs_unmount =          zfs_umount,
105         .vfs_root =             zfs_root,
106         .vfs_statfs =           zfs_statfs,
107         .vfs_vget =             zfs_vget,
108         .vfs_sync =             zfs_sync,
109         .vfs_checkexp =         zfs_checkexp,
110         .vfs_fhtovp =           zfs_fhtovp,
111 };
112
113 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
114
115 /*
116  * We need to keep a count of active fs's.
117  * This is necessary to prevent our module
118  * from being unloaded after a umount -f
119  */
120 static uint32_t zfs_active_fs_count = 0;
121
122 /*ARGSUSED*/
123 static int
124 zfs_sync(vfs_t *vfsp, int waitfor)
125 {
126
127         /*
128          * Data integrity is job one.  We don't want a compromised kernel
129          * writing to the storage pool, so we never sync during panic.
130          */
131         if (panicstr)
132                 return (0);
133
134         if (vfsp != NULL) {
135                 /*
136                  * Sync a specific filesystem.
137                  */
138                 zfsvfs_t *zfsvfs = vfsp->vfs_data;
139                 dsl_pool_t *dp;
140                 int error;
141
142                 error = vfs_stdsync(vfsp, waitfor);
143                 if (error != 0)
144                         return (error);
145
146                 ZFS_ENTER(zfsvfs);
147                 dp = dmu_objset_pool(zfsvfs->z_os);
148
149                 /*
150                  * If the system is shutting down, then skip any
151                  * filesystems which may exist on a suspended pool.
152                  */
153                 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
154                         ZFS_EXIT(zfsvfs);
155                         return (0);
156                 }
157
158                 if (zfsvfs->z_log != NULL)
159                         zil_commit(zfsvfs->z_log, 0);
160
161                 ZFS_EXIT(zfsvfs);
162         } else {
163                 /*
164                  * Sync all ZFS filesystems.  This is what happens when you
165                  * run sync(1M).  Unlike other filesystems, ZFS honors the
166                  * request by waiting for all pools to commit all dirty data.
167                  */
168                 spa_sync_allpools();
169         }
170
171         return (0);
172 }
173
174 #ifndef __FreeBSD__
175 static int
176 zfs_create_unique_device(dev_t *dev)
177 {
178         major_t new_major;
179
180         do {
181                 ASSERT3U(zfs_minor, <=, MAXMIN32);
182                 minor_t start = zfs_minor;
183                 do {
184                         mutex_enter(&zfs_dev_mtx);
185                         if (zfs_minor >= MAXMIN32) {
186                                 /*
187                                  * If we're still using the real major
188                                  * keep out of /dev/zfs and /dev/zvol minor
189                                  * number space.  If we're using a getudev()'ed
190                                  * major number, we can use all of its minors.
191                                  */
192                                 if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
193                                         zfs_minor = ZFS_MIN_MINOR;
194                                 else
195                                         zfs_minor = 0;
196                         } else {
197                                 zfs_minor++;
198                         }
199                         *dev = makedevice(zfs_major, zfs_minor);
200                         mutex_exit(&zfs_dev_mtx);
201                 } while (vfs_devismounted(*dev) && zfs_minor != start);
202                 if (zfs_minor == start) {
203                         /*
204                          * We are using all ~262,000 minor numbers for the
205                          * current major number.  Create a new major number.
206                          */
207                         if ((new_major = getudev()) == (major_t)-1) {
208                                 cmn_err(CE_WARN,
209                                     "zfs_mount: Can't get unique major "
210                                     "device number.");
211                                 return (-1);
212                         }
213                         mutex_enter(&zfs_dev_mtx);
214                         zfs_major = new_major;
215                         zfs_minor = 0;
216
217                         mutex_exit(&zfs_dev_mtx);
218                 } else {
219                         break;
220                 }
221                 /* CONSTANTCONDITION */
222         } while (1);
223
224         return (0);
225 }
226 #endif  /* !__FreeBSD__ */
227
228 static void
229 atime_changed_cb(void *arg, uint64_t newval)
230 {
231         zfsvfs_t *zfsvfs = arg;
232
233         if (newval == TRUE) {
234                 zfsvfs->z_atime = TRUE;
235                 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
236                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
237                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
238         } else {
239                 zfsvfs->z_atime = FALSE;
240                 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
241                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
242                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
243         }
244 }
245
246 static void
247 xattr_changed_cb(void *arg, uint64_t newval)
248 {
249         zfsvfs_t *zfsvfs = arg;
250
251         if (newval == TRUE) {
252                 /* XXX locking on vfs_flag? */
253 #ifdef TODO
254                 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
255 #endif
256                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
257                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
258         } else {
259                 /* XXX locking on vfs_flag? */
260 #ifdef TODO
261                 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
262 #endif
263                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
264                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
265         }
266 }
267
268 static void
269 blksz_changed_cb(void *arg, uint64_t newval)
270 {
271         zfsvfs_t *zfsvfs = arg;
272
273         if (newval < SPA_MINBLOCKSIZE ||
274             newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
275                 newval = SPA_MAXBLOCKSIZE;
276
277         zfsvfs->z_max_blksz = newval;
278         zfsvfs->z_vfs->mnt_stat.f_iosize = newval;
279 }
280
281 static void
282 readonly_changed_cb(void *arg, uint64_t newval)
283 {
284         zfsvfs_t *zfsvfs = arg;
285
286         if (newval) {
287                 /* XXX locking on vfs_flag? */
288                 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
289                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
290                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
291         } else {
292                 /* XXX locking on vfs_flag? */
293                 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
294                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
295                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
296         }
297 }
298
299 static void
300 setuid_changed_cb(void *arg, uint64_t newval)
301 {
302         zfsvfs_t *zfsvfs = arg;
303
304         if (newval == FALSE) {
305                 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
306                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
307                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
308         } else {
309                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
310                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
311                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
312         }
313 }
314
315 static void
316 exec_changed_cb(void *arg, uint64_t newval)
317 {
318         zfsvfs_t *zfsvfs = arg;
319
320         if (newval == FALSE) {
321                 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
322                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
323                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
324         } else {
325                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
326                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
327                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
328         }
329 }
330
331 /*
332  * The nbmand mount option can be changed at mount time.
333  * We can't allow it to be toggled on live file systems or incorrect
334  * behavior may be seen from cifs clients
335  *
336  * This property isn't registered via dsl_prop_register(), but this callback
337  * will be called when a file system is first mounted
338  */
339 static void
340 nbmand_changed_cb(void *arg, uint64_t newval)
341 {
342         zfsvfs_t *zfsvfs = arg;
343         if (newval == FALSE) {
344                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
345                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
346         } else {
347                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
348                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
349         }
350 }
351
352 static void
353 snapdir_changed_cb(void *arg, uint64_t newval)
354 {
355         zfsvfs_t *zfsvfs = arg;
356
357         zfsvfs->z_show_ctldir = newval;
358 }
359
360 static void
361 vscan_changed_cb(void *arg, uint64_t newval)
362 {
363         zfsvfs_t *zfsvfs = arg;
364
365         zfsvfs->z_vscan = newval;
366 }
367
368 static void
369 acl_mode_changed_cb(void *arg, uint64_t newval)
370 {
371         zfsvfs_t *zfsvfs = arg;
372
373         zfsvfs->z_acl_mode = newval;
374 }
375
376 static void
377 acl_inherit_changed_cb(void *arg, uint64_t newval)
378 {
379         zfsvfs_t *zfsvfs = arg;
380
381         zfsvfs->z_acl_inherit = newval;
382 }
383
384 static int
385 zfs_register_callbacks(vfs_t *vfsp)
386 {
387         struct dsl_dataset *ds = NULL;
388         objset_t *os = NULL;
389         zfsvfs_t *zfsvfs = NULL;
390         uint64_t nbmand;
391         int readonly, do_readonly = B_FALSE;
392         int setuid, do_setuid = B_FALSE;
393         int exec, do_exec = B_FALSE;
394         int xattr, do_xattr = B_FALSE;
395         int atime, do_atime = B_FALSE;
396         int error = 0;
397
398         ASSERT(vfsp);
399         zfsvfs = vfsp->vfs_data;
400         ASSERT(zfsvfs);
401         os = zfsvfs->z_os;
402
403         /*
404          * This function can be called for a snapshot when we update snapshot's
405          * mount point, which isn't really supported.
406          */
407         if (dmu_objset_is_snapshot(os))
408                 return (EOPNOTSUPP);
409
410         /*
411          * The act of registering our callbacks will destroy any mount
412          * options we may have.  In order to enable temporary overrides
413          * of mount options, we stash away the current values and
414          * restore them after we register the callbacks.
415          */
416         if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
417             !spa_writeable(dmu_objset_spa(os))) {
418                 readonly = B_TRUE;
419                 do_readonly = B_TRUE;
420         } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
421                 readonly = B_FALSE;
422                 do_readonly = B_TRUE;
423         }
424         if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
425                 setuid = B_FALSE;
426                 do_setuid = B_TRUE;
427         } else {
428                 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
429                         setuid = B_FALSE;
430                         do_setuid = B_TRUE;
431                 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
432                         setuid = B_TRUE;
433                         do_setuid = B_TRUE;
434                 }
435         }
436         if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
437                 exec = B_FALSE;
438                 do_exec = B_TRUE;
439         } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
440                 exec = B_TRUE;
441                 do_exec = B_TRUE;
442         }
443         if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
444                 xattr = B_FALSE;
445                 do_xattr = B_TRUE;
446         } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
447                 xattr = B_TRUE;
448                 do_xattr = B_TRUE;
449         }
450         if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
451                 atime = B_FALSE;
452                 do_atime = B_TRUE;
453         } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
454                 atime = B_TRUE;
455                 do_atime = B_TRUE;
456         }
457
458         /*
459          * nbmand is a special property.  It can only be changed at
460          * mount time.
461          *
462          * This is weird, but it is documented to only be changeable
463          * at mount time.
464          */
465         if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
466                 nbmand = B_FALSE;
467         } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
468                 nbmand = B_TRUE;
469         } else {
470                 char osname[MAXNAMELEN];
471
472                 dmu_objset_name(os, osname);
473                 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
474                     NULL)) {
475                         return (error);
476                 }
477         }
478
479         /*
480          * Register property callbacks.
481          *
482          * It would probably be fine to just check for i/o error from
483          * the first prop_register(), but I guess I like to go
484          * overboard...
485          */
486         ds = dmu_objset_ds(os);
487         error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
488         error = error ? error : dsl_prop_register(ds,
489             "xattr", xattr_changed_cb, zfsvfs);
490         error = error ? error : dsl_prop_register(ds,
491             "recordsize", blksz_changed_cb, zfsvfs);
492         error = error ? error : dsl_prop_register(ds,
493             "readonly", readonly_changed_cb, zfsvfs);
494         error = error ? error : dsl_prop_register(ds,
495             "setuid", setuid_changed_cb, zfsvfs);
496         error = error ? error : dsl_prop_register(ds,
497             "exec", exec_changed_cb, zfsvfs);
498         error = error ? error : dsl_prop_register(ds,
499             "snapdir", snapdir_changed_cb, zfsvfs);
500         error = error ? error : dsl_prop_register(ds,
501             "aclmode", acl_mode_changed_cb, zfsvfs);
502         error = error ? error : dsl_prop_register(ds,
503             "aclinherit", acl_inherit_changed_cb, zfsvfs);
504         error = error ? error : dsl_prop_register(ds,
505             "vscan", vscan_changed_cb, zfsvfs);
506         if (error)
507                 goto unregister;
508
509         /*
510          * Invoke our callbacks to restore temporary mount options.
511          */
512         if (do_readonly)
513                 readonly_changed_cb(zfsvfs, readonly);
514         if (do_setuid)
515                 setuid_changed_cb(zfsvfs, setuid);
516         if (do_exec)
517                 exec_changed_cb(zfsvfs, exec);
518         if (do_xattr)
519                 xattr_changed_cb(zfsvfs, xattr);
520         if (do_atime)
521                 atime_changed_cb(zfsvfs, atime);
522
523         nbmand_changed_cb(zfsvfs, nbmand);
524
525         return (0);
526
527 unregister:
528         /*
529          * We may attempt to unregister some callbacks that are not
530          * registered, but this is OK; it will simply return ENOMSG,
531          * which we will ignore.
532          */
533         (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
534         (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
535         (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
536         (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
537         (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
538         (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
539         (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
540         (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
541         (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
542             zfsvfs);
543         (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
544         return (error);
545
546 }
547
548 static int
549 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
550     uint64_t *userp, uint64_t *groupp)
551 {
552         znode_phys_t *znp = data;
553         int error = 0;
554
555         /*
556          * Is it a valid type of object to track?
557          */
558         if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
559                 return (ENOENT);
560
561         /*
562          * If we have a NULL data pointer
563          * then assume the id's aren't changing and
564          * return EEXIST to the dmu to let it know to
565          * use the same ids
566          */
567         if (data == NULL)
568                 return (EEXIST);
569
570         if (bonustype == DMU_OT_ZNODE) {
571                 *userp = znp->zp_uid;
572                 *groupp = znp->zp_gid;
573         } else {
574                 int hdrsize;
575
576                 ASSERT(bonustype == DMU_OT_SA);
577                 hdrsize = sa_hdrsize(data);
578
579                 if (hdrsize != 0) {
580                         *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
581                             SA_UID_OFFSET));
582                         *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
583                             SA_GID_OFFSET));
584                 } else {
585                         /*
586                          * This should only happen for newly created
587                          * files that haven't had the znode data filled
588                          * in yet.
589                          */
590                         *userp = 0;
591                         *groupp = 0;
592                 }
593         }
594         return (error);
595 }
596
597 static void
598 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
599     char *domainbuf, int buflen, uid_t *ridp)
600 {
601         uint64_t fuid;
602         const char *domain;
603
604         fuid = strtonum(fuidstr, NULL);
605
606         domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
607         if (domain)
608                 (void) strlcpy(domainbuf, domain, buflen);
609         else
610                 domainbuf[0] = '\0';
611         *ridp = FUID_RID(fuid);
612 }
613
614 static uint64_t
615 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
616 {
617         switch (type) {
618         case ZFS_PROP_USERUSED:
619                 return (DMU_USERUSED_OBJECT);
620         case ZFS_PROP_GROUPUSED:
621                 return (DMU_GROUPUSED_OBJECT);
622         case ZFS_PROP_USERQUOTA:
623                 return (zfsvfs->z_userquota_obj);
624         case ZFS_PROP_GROUPQUOTA:
625                 return (zfsvfs->z_groupquota_obj);
626         }
627         return (0);
628 }
629
630 int
631 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
632     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
633 {
634         int error;
635         zap_cursor_t zc;
636         zap_attribute_t za;
637         zfs_useracct_t *buf = vbuf;
638         uint64_t obj;
639
640         if (!dmu_objset_userspace_present(zfsvfs->z_os))
641                 return (ENOTSUP);
642
643         obj = zfs_userquota_prop_to_obj(zfsvfs, type);
644         if (obj == 0) {
645                 *bufsizep = 0;
646                 return (0);
647         }
648
649         for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
650             (error = zap_cursor_retrieve(&zc, &za)) == 0;
651             zap_cursor_advance(&zc)) {
652                 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
653                     *bufsizep)
654                         break;
655
656                 fuidstr_to_sid(zfsvfs, za.za_name,
657                     buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
658
659                 buf->zu_space = za.za_first_integer;
660                 buf++;
661         }
662         if (error == ENOENT)
663                 error = 0;
664
665         ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
666         *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
667         *cookiep = zap_cursor_serialize(&zc);
668         zap_cursor_fini(&zc);
669         return (error);
670 }
671
672 /*
673  * buf must be big enough (eg, 32 bytes)
674  */
675 static int
676 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
677     char *buf, boolean_t addok)
678 {
679         uint64_t fuid;
680         int domainid = 0;
681
682         if (domain && domain[0]) {
683                 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
684                 if (domainid == -1)
685                         return (ENOENT);
686         }
687         fuid = FUID_ENCODE(domainid, rid);
688         (void) sprintf(buf, "%llx", (longlong_t)fuid);
689         return (0);
690 }
691
692 int
693 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
694     const char *domain, uint64_t rid, uint64_t *valp)
695 {
696         char buf[32];
697         int err;
698         uint64_t obj;
699
700         *valp = 0;
701
702         if (!dmu_objset_userspace_present(zfsvfs->z_os))
703                 return (ENOTSUP);
704
705         obj = zfs_userquota_prop_to_obj(zfsvfs, type);
706         if (obj == 0)
707                 return (0);
708
709         err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
710         if (err)
711                 return (err);
712
713         err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
714         if (err == ENOENT)
715                 err = 0;
716         return (err);
717 }
718
719 int
720 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
721     const char *domain, uint64_t rid, uint64_t quota)
722 {
723         char buf[32];
724         int err;
725         dmu_tx_t *tx;
726         uint64_t *objp;
727         boolean_t fuid_dirtied;
728
729         if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
730                 return (EINVAL);
731
732         if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
733                 return (ENOTSUP);
734
735         objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
736             &zfsvfs->z_groupquota_obj;
737
738         err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
739         if (err)
740                 return (err);
741         fuid_dirtied = zfsvfs->z_fuid_dirty;
742
743         tx = dmu_tx_create(zfsvfs->z_os);
744         dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
745         if (*objp == 0) {
746                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
747                     zfs_userquota_prop_prefixes[type]);
748         }
749         if (fuid_dirtied)
750                 zfs_fuid_txhold(zfsvfs, tx);
751         err = dmu_tx_assign(tx, TXG_WAIT);
752         if (err) {
753                 dmu_tx_abort(tx);
754                 return (err);
755         }
756
757         mutex_enter(&zfsvfs->z_lock);
758         if (*objp == 0) {
759                 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
760                     DMU_OT_NONE, 0, tx);
761                 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
762                     zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
763         }
764         mutex_exit(&zfsvfs->z_lock);
765
766         if (quota == 0) {
767                 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
768                 if (err == ENOENT)
769                         err = 0;
770         } else {
771                 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
772         }
773         ASSERT(err == 0);
774         if (fuid_dirtied)
775                 zfs_fuid_sync(zfsvfs, tx);
776         dmu_tx_commit(tx);
777         return (err);
778 }
779
780 boolean_t
781 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
782 {
783         char buf[32];
784         uint64_t used, quota, usedobj, quotaobj;
785         int err;
786
787         usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
788         quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
789
790         if (quotaobj == 0 || zfsvfs->z_replay)
791                 return (B_FALSE);
792
793         (void) sprintf(buf, "%llx", (longlong_t)fuid);
794         err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
795         if (err != 0)
796                 return (B_FALSE);
797
798         err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
799         if (err != 0)
800                 return (B_FALSE);
801         return (used >= quota);
802 }
803
804 boolean_t
805 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
806 {
807         uint64_t fuid;
808         uint64_t quotaobj;
809
810         quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
811
812         fuid = isgroup ? zp->z_gid : zp->z_uid;
813
814         if (quotaobj == 0 || zfsvfs->z_replay)
815                 return (B_FALSE);
816
817         return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
818 }
819
820 int
821 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
822 {
823         objset_t *os;
824         zfsvfs_t *zfsvfs;
825         uint64_t zval;
826         int i, error;
827         uint64_t sa_obj;
828
829         zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
830
831         /*
832          * We claim to always be readonly so we can open snapshots;
833          * other ZPL code will prevent us from writing to snapshots.
834          */
835         error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
836         if (error) {
837                 kmem_free(zfsvfs, sizeof (zfsvfs_t));
838                 return (error);
839         }
840
841         /*
842          * Initialize the zfs-specific filesystem structure.
843          * Should probably make this a kmem cache, shuffle fields,
844          * and just bzero up to z_hold_mtx[].
845          */
846         zfsvfs->z_vfs = NULL;
847         zfsvfs->z_parent = zfsvfs;
848         zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
849         zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
850         zfsvfs->z_os = os;
851
852         error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
853         if (error) {
854                 goto out;
855         } else if (zfsvfs->z_version >
856             zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
857                 (void) printf("Can't mount a version %lld file system "
858                     "on a version %lld pool\n. Pool must be upgraded to mount "
859                     "this file system.", (u_longlong_t)zfsvfs->z_version,
860                     (u_longlong_t)spa_version(dmu_objset_spa(os)));
861                 error = ENOTSUP;
862                 goto out;
863         }
864         if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
865                 goto out;
866         zfsvfs->z_norm = (int)zval;
867
868         if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
869                 goto out;
870         zfsvfs->z_utf8 = (zval != 0);
871
872         if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
873                 goto out;
874         zfsvfs->z_case = (uint_t)zval;
875
876         /*
877          * Fold case on file systems that are always or sometimes case
878          * insensitive.
879          */
880         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
881             zfsvfs->z_case == ZFS_CASE_MIXED)
882                 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
883
884         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
885         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
886
887         if (zfsvfs->z_use_sa) {
888                 /* should either have both of these objects or none */
889                 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
890                     &sa_obj);
891                 if (error)
892                         return (error);
893         } else {
894                 /*
895                  * Pre SA versions file systems should never touch
896                  * either the attribute registration or layout objects.
897                  */
898                 sa_obj = 0;
899         }
900
901         error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
902             &zfsvfs->z_attr_table);
903         if (error)
904                 goto out;
905
906         if (zfsvfs->z_version >= ZPL_VERSION_SA)
907                 sa_register_update_callback(os, zfs_sa_upgrade);
908
909         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
910             &zfsvfs->z_root);
911         if (error)
912                 goto out;
913         ASSERT(zfsvfs->z_root != 0);
914
915         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
916             &zfsvfs->z_unlinkedobj);
917         if (error)
918                 goto out;
919
920         error = zap_lookup(os, MASTER_NODE_OBJ,
921             zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
922             8, 1, &zfsvfs->z_userquota_obj);
923         if (error && error != ENOENT)
924                 goto out;
925
926         error = zap_lookup(os, MASTER_NODE_OBJ,
927             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
928             8, 1, &zfsvfs->z_groupquota_obj);
929         if (error && error != ENOENT)
930                 goto out;
931
932         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
933             &zfsvfs->z_fuid_obj);
934         if (error && error != ENOENT)
935                 goto out;
936
937         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
938             &zfsvfs->z_shares_dir);
939         if (error && error != ENOENT)
940                 goto out;
941
942         mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
943         mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
944         list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
945             offsetof(znode_t, z_link_node));
946         rrw_init(&zfsvfs->z_teardown_lock);
947         rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
948         rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
949         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
950                 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
951
952         *zfvp = zfsvfs;
953         return (0);
954
955 out:
956         dmu_objset_disown(os, zfsvfs);
957         *zfvp = NULL;
958         kmem_free(zfsvfs, sizeof (zfsvfs_t));
959         return (error);
960 }
961
962 static int
963 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
964 {
965         int error;
966
967         error = zfs_register_callbacks(zfsvfs->z_vfs);
968         if (error)
969                 return (error);
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         zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
979
980         /*
981          * If we are not mounting (ie: online recv), then we don't
982          * have to worry about replaying the log as we blocked all
983          * operations out since we closed the ZIL.
984          */
985         if (mounting) {
986                 boolean_t readonly;
987
988                 /*
989                  * During replay we remove the read only flag to
990                  * allow replays to succeed.
991                  */
992                 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
993                 if (readonly != 0)
994                         zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
995                 else
996                         zfs_unlinked_drain(zfsvfs);
997
998                 /*
999                  * Parse and replay the intent log.
1000                  *
1001                  * Because of ziltest, this must be done after
1002                  * zfs_unlinked_drain().  (Further note: ziltest
1003                  * doesn't use readonly mounts, where
1004                  * zfs_unlinked_drain() isn't called.)  This is because
1005                  * ziltest causes spa_sync() to think it's committed,
1006                  * but actually it is not, so the intent log contains
1007                  * many txg's worth of changes.
1008                  *
1009                  * In particular, if object N is in the unlinked set in
1010                  * the last txg to actually sync, then it could be
1011                  * actually freed in a later txg and then reallocated
1012                  * in a yet later txg.  This would write a "create
1013                  * object N" record to the intent log.  Normally, this
1014                  * would be fine because the spa_sync() would have
1015                  * written out the fact that object N is free, before
1016                  * we could write the "create object N" intent log
1017                  * record.
1018                  *
1019                  * But when we are in ziltest mode, we advance the "open
1020                  * txg" without actually spa_sync()-ing the changes to
1021                  * disk.  So we would see that object N is still
1022                  * allocated and in the unlinked set, and there is an
1023                  * intent log record saying to allocate it.
1024                  */
1025                 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1026                         if (zil_replay_disable) {
1027                                 zil_destroy(zfsvfs->z_log, B_FALSE);
1028                         } else {
1029                                 zfsvfs->z_replay = B_TRUE;
1030                                 zil_replay(zfsvfs->z_os, zfsvfs,
1031                                     zfs_replay_vector);
1032                                 zfsvfs->z_replay = B_FALSE;
1033                         }
1034                 }
1035                 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1036         }
1037
1038         return (0);
1039 }
1040
1041 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1042
1043 void
1044 zfsvfs_free(zfsvfs_t *zfsvfs)
1045 {
1046         int i;
1047
1048         /*
1049          * This is a barrier to prevent the filesystem from going away in
1050          * zfs_znode_move() until we can safely ensure that the filesystem is
1051          * not unmounted. We consider the filesystem valid before the barrier
1052          * and invalid after the barrier.
1053          */
1054         rw_enter(&zfsvfs_lock, RW_READER);
1055         rw_exit(&zfsvfs_lock);
1056
1057         zfs_fuid_destroy(zfsvfs);
1058
1059         mutex_destroy(&zfsvfs->z_znodes_lock);
1060         mutex_destroy(&zfsvfs->z_lock);
1061         list_destroy(&zfsvfs->z_all_znodes);
1062         rrw_destroy(&zfsvfs->z_teardown_lock);
1063         rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1064         rw_destroy(&zfsvfs->z_fuid_lock);
1065         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1066                 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1067         kmem_free(zfsvfs, sizeof (zfsvfs_t));
1068 }
1069
1070 static void
1071 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1072 {
1073         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1074         if (zfsvfs->z_vfs) {
1075                 if (zfsvfs->z_use_fuids) {
1076                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1077                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1078                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1079                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1080                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1081                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1082                 } else {
1083                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1084                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1085                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1086                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1087                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1088                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1089                 }
1090         }
1091         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1092 }
1093
1094 static int
1095 zfs_domount(vfs_t *vfsp, char *osname)
1096 {
1097         uint64_t recordsize, fsid_guid;
1098         int error = 0;
1099         zfsvfs_t *zfsvfs;
1100         vnode_t *vp;
1101
1102         ASSERT(vfsp);
1103         ASSERT(osname);
1104
1105         error = zfsvfs_create(osname, &zfsvfs);
1106         if (error)
1107                 return (error);
1108         zfsvfs->z_vfs = vfsp;
1109
1110         if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1111             NULL))
1112                 goto out;
1113         zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
1114         zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
1115
1116         vfsp->vfs_data = zfsvfs;
1117         vfsp->mnt_flag |= MNT_LOCAL;
1118         vfsp->mnt_kern_flag |= MNTK_MPSAFE;
1119         vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
1120         vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
1121
1122         /*
1123          * The fsid is 64 bits, composed of an 8-bit fs type, which
1124          * separates our fsid from any other filesystem types, and a
1125          * 56-bit objset unique ID.  The objset unique ID is unique to
1126          * all objsets open on this system, provided by unique_create().
1127          * The 8-bit fs type must be put in the low bits of fsid[1]
1128          * because that's where other Solaris filesystems put it.
1129          */
1130         fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1131         ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1132         vfsp->vfs_fsid.val[0] = fsid_guid;
1133         vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1134             vfsp->mnt_vfc->vfc_typenum & 0xFF;
1135
1136         /*
1137          * Set features for file system.
1138          */
1139         zfs_set_fuid_feature(zfsvfs);
1140         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1141                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1142                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1143                 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1144         } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1145                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1146                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1147         }
1148         vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1149
1150         if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1151                 uint64_t pval;
1152
1153                 atime_changed_cb(zfsvfs, B_FALSE);
1154                 readonly_changed_cb(zfsvfs, B_TRUE);
1155                 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1156                         goto out;
1157                 xattr_changed_cb(zfsvfs, pval);
1158                 zfsvfs->z_issnap = B_TRUE;
1159                 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1160
1161                 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1162                 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1163                 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1164         } else {
1165                 error = zfsvfs_setup(zfsvfs, B_TRUE);
1166         }
1167
1168         vfs_mountedfrom(vfsp, osname);
1169         /* Grab extra reference. */
1170         VERIFY(VFS_ROOT(vfsp, LK_EXCLUSIVE, &vp) == 0);
1171         VOP_UNLOCK(vp, 0);
1172
1173         if (!zfsvfs->z_issnap)
1174                 zfsctl_create(zfsvfs);
1175 out:
1176         if (error) {
1177                 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1178                 zfsvfs_free(zfsvfs);
1179         } else {
1180                 atomic_add_32(&zfs_active_fs_count, 1);
1181         }
1182
1183         return (error);
1184 }
1185
1186 void
1187 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1188 {
1189         objset_t *os = zfsvfs->z_os;
1190         struct dsl_dataset *ds;
1191
1192         /*
1193          * Unregister properties.
1194          */
1195         if (!dmu_objset_is_snapshot(os)) {
1196                 ds = dmu_objset_ds(os);
1197                 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1198                     zfsvfs) == 0);
1199
1200                 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
1201                     zfsvfs) == 0);
1202
1203                 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1204                     zfsvfs) == 0);
1205
1206                 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1207                     zfsvfs) == 0);
1208
1209                 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1210                     zfsvfs) == 0);
1211
1212                 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1213                     zfsvfs) == 0);
1214
1215                 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1216                     zfsvfs) == 0);
1217
1218                 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
1219                     zfsvfs) == 0);
1220
1221                 VERIFY(dsl_prop_unregister(ds, "aclinherit",
1222                     acl_inherit_changed_cb, zfsvfs) == 0);
1223
1224                 VERIFY(dsl_prop_unregister(ds, "vscan",
1225                     vscan_changed_cb, zfsvfs) == 0);
1226         }
1227 }
1228
1229 #ifdef SECLABEL
1230 /*
1231  * Convert a decimal digit string to a uint64_t integer.
1232  */
1233 static int
1234 str_to_uint64(char *str, uint64_t *objnum)
1235 {
1236         uint64_t num = 0;
1237
1238         while (*str) {
1239                 if (*str < '0' || *str > '9')
1240                         return (EINVAL);
1241
1242                 num = num*10 + *str++ - '0';
1243         }
1244
1245         *objnum = num;
1246         return (0);
1247 }
1248
1249 /*
1250  * The boot path passed from the boot loader is in the form of
1251  * "rootpool-name/root-filesystem-object-number'. Convert this
1252  * string to a dataset name: "rootpool-name/root-filesystem-name".
1253  */
1254 static int
1255 zfs_parse_bootfs(char *bpath, char *outpath)
1256 {
1257         char *slashp;
1258         uint64_t objnum;
1259         int error;
1260
1261         if (*bpath == 0 || *bpath == '/')
1262                 return (EINVAL);
1263
1264         (void) strcpy(outpath, bpath);
1265
1266         slashp = strchr(bpath, '/');
1267
1268         /* if no '/', just return the pool name */
1269         if (slashp == NULL) {
1270                 return (0);
1271         }
1272
1273         /* if not a number, just return the root dataset name */
1274         if (str_to_uint64(slashp+1, &objnum)) {
1275                 return (0);
1276         }
1277
1278         *slashp = '\0';
1279         error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1280         *slashp = '/';
1281
1282         return (error);
1283 }
1284
1285 /*
1286  * zfs_check_global_label:
1287  *      Check that the hex label string is appropriate for the dataset
1288  *      being mounted into the global_zone proper.
1289  *
1290  *      Return an error if the hex label string is not default or
1291  *      admin_low/admin_high.  For admin_low labels, the corresponding
1292  *      dataset must be readonly.
1293  */
1294 int
1295 zfs_check_global_label(const char *dsname, const char *hexsl)
1296 {
1297         if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1298                 return (0);
1299         if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1300                 return (0);
1301         if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1302                 /* must be readonly */
1303                 uint64_t rdonly;
1304
1305                 if (dsl_prop_get_integer(dsname,
1306                     zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1307                         return (EACCES);
1308                 return (rdonly ? 0 : EACCES);
1309         }
1310         return (EACCES);
1311 }
1312
1313 /*
1314  * zfs_mount_label_policy:
1315  *      Determine whether the mount is allowed according to MAC check.
1316  *      by comparing (where appropriate) label of the dataset against
1317  *      the label of the zone being mounted into.  If the dataset has
1318  *      no label, create one.
1319  *
1320  *      Returns:
1321  *               0 :    access allowed
1322  *              >0 :    error code, such as EACCES
1323  */
1324 static int
1325 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1326 {
1327         int             error, retv;
1328         zone_t          *mntzone = NULL;
1329         ts_label_t      *mnt_tsl;
1330         bslabel_t       *mnt_sl;
1331         bslabel_t       ds_sl;
1332         char            ds_hexsl[MAXNAMELEN];
1333
1334         retv = EACCES;                          /* assume the worst */
1335
1336         /*
1337          * Start by getting the dataset label if it exists.
1338          */
1339         error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1340             1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1341         if (error)
1342                 return (EACCES);
1343
1344         /*
1345          * If labeling is NOT enabled, then disallow the mount of datasets
1346          * which have a non-default label already.  No other label checks
1347          * are needed.
1348          */
1349         if (!is_system_labeled()) {
1350                 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1351                         return (0);
1352                 return (EACCES);
1353         }
1354
1355         /*
1356          * Get the label of the mountpoint.  If mounting into the global
1357          * zone (i.e. mountpoint is not within an active zone and the
1358          * zoned property is off), the label must be default or
1359          * admin_low/admin_high only; no other checks are needed.
1360          */
1361         mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1362         if (mntzone->zone_id == GLOBAL_ZONEID) {
1363                 uint64_t zoned;
1364
1365                 zone_rele(mntzone);
1366
1367                 if (dsl_prop_get_integer(osname,
1368                     zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1369                         return (EACCES);
1370                 if (!zoned)
1371                         return (zfs_check_global_label(osname, ds_hexsl));
1372                 else
1373                         /*
1374                          * This is the case of a zone dataset being mounted
1375                          * initially, before the zone has been fully created;
1376                          * allow this mount into global zone.
1377                          */
1378                         return (0);
1379         }
1380
1381         mnt_tsl = mntzone->zone_slabel;
1382         ASSERT(mnt_tsl != NULL);
1383         label_hold(mnt_tsl);
1384         mnt_sl = label2bslabel(mnt_tsl);
1385
1386         if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1387                 /*
1388                  * The dataset doesn't have a real label, so fabricate one.
1389                  */
1390                 char *str = NULL;
1391
1392                 if (l_to_str_internal(mnt_sl, &str) == 0 &&
1393                     dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1394                     ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
1395                         retv = 0;
1396                 if (str != NULL)
1397                         kmem_free(str, strlen(str) + 1);
1398         } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1399                 /*
1400                  * Now compare labels to complete the MAC check.  If the
1401                  * labels are equal then allow access.  If the mountpoint
1402                  * label dominates the dataset label, allow readonly access.
1403                  * Otherwise, access is denied.
1404                  */
1405                 if (blequal(mnt_sl, &ds_sl))
1406                         retv = 0;
1407                 else if (bldominates(mnt_sl, &ds_sl)) {
1408                         vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1409                         retv = 0;
1410                 }
1411         }
1412
1413         label_rele(mnt_tsl);
1414         zone_rele(mntzone);
1415         return (retv);
1416 }
1417 #endif  /* SECLABEL */
1418
1419 #ifdef OPENSOLARIS_MOUNTROOT
1420 static int
1421 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1422 {
1423         int error = 0;
1424         static int zfsrootdone = 0;
1425         zfsvfs_t *zfsvfs = NULL;
1426         znode_t *zp = NULL;
1427         vnode_t *vp = NULL;
1428         char *zfs_bootfs;
1429         char *zfs_devid;
1430
1431         ASSERT(vfsp);
1432
1433         /*
1434          * The filesystem that we mount as root is defined in the
1435          * boot property "zfs-bootfs" with a format of
1436          * "poolname/root-dataset-objnum".
1437          */
1438         if (why == ROOT_INIT) {
1439                 if (zfsrootdone++)
1440                         return (EBUSY);
1441                 /*
1442                  * the process of doing a spa_load will require the
1443                  * clock to be set before we could (for example) do
1444                  * something better by looking at the timestamp on
1445                  * an uberblock, so just set it to -1.
1446                  */
1447                 clkset(-1);
1448
1449                 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1450                         cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1451                             "bootfs name");
1452                         return (EINVAL);
1453                 }
1454                 zfs_devid = spa_get_bootprop("diskdevid");
1455                 error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1456                 if (zfs_devid)
1457                         spa_free_bootprop(zfs_devid);
1458                 if (error) {
1459                         spa_free_bootprop(zfs_bootfs);
1460                         cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1461                             error);
1462                         return (error);
1463                 }
1464                 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1465                         spa_free_bootprop(zfs_bootfs);
1466                         cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1467                             error);
1468                         return (error);
1469                 }
1470
1471                 spa_free_bootprop(zfs_bootfs);
1472
1473                 if (error = vfs_lock(vfsp))
1474                         return (error);
1475
1476                 if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1477                         cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1478                         goto out;
1479                 }
1480
1481                 zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1482                 ASSERT(zfsvfs);
1483                 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1484                         cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1485                         goto out;
1486                 }
1487
1488                 vp = ZTOV(zp);
1489                 mutex_enter(&vp->v_lock);
1490                 vp->v_flag |= VROOT;
1491                 mutex_exit(&vp->v_lock);
1492                 rootvp = vp;
1493
1494                 /*
1495                  * Leave rootvp held.  The root file system is never unmounted.
1496                  */
1497
1498                 vfs_add((struct vnode *)0, vfsp,
1499                     (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1500 out:
1501                 vfs_unlock(vfsp);
1502                 return (error);
1503         } else if (why == ROOT_REMOUNT) {
1504                 readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1505                 vfsp->vfs_flag |= VFS_REMOUNT;
1506
1507                 /* refresh mount options */
1508                 zfs_unregister_callbacks(vfsp->vfs_data);
1509                 return (zfs_register_callbacks(vfsp));
1510
1511         } else if (why == ROOT_UNMOUNT) {
1512                 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1513                 (void) zfs_sync(vfsp, 0, 0);
1514                 return (0);
1515         }
1516
1517         /*
1518          * if "why" is equal to anything else other than ROOT_INIT,
1519          * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1520          */
1521         return (ENOTSUP);
1522 }
1523 #endif  /* OPENSOLARIS_MOUNTROOT */
1524
1525 static int
1526 getpoolname(const char *osname, char *poolname)
1527 {
1528         char *p;
1529
1530         p = strchr(osname, '/');
1531         if (p == NULL) {
1532                 if (strlen(osname) >= MAXNAMELEN)
1533                         return (ENAMETOOLONG);
1534                 (void) strcpy(poolname, osname);
1535         } else {
1536                 if (p - osname >= MAXNAMELEN)
1537                         return (ENAMETOOLONG);
1538                 (void) strncpy(poolname, osname, p - osname);
1539                 poolname[p - osname] = '\0';
1540         }
1541         return (0);
1542 }
1543
1544 /*ARGSUSED*/
1545 static int
1546 zfs_mount(vfs_t *vfsp)
1547 {
1548         kthread_t       *td = curthread;
1549         vnode_t         *mvp = vfsp->mnt_vnodecovered;
1550         cred_t          *cr = td->td_ucred;
1551         char            *osname;
1552         int             error = 0;
1553         int             canwrite;
1554
1555         if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
1556                 return (EINVAL);
1557
1558         /*
1559          * If full-owner-access is enabled and delegated administration is
1560          * turned on, we must set nosuid.
1561          */
1562         if (zfs_super_owner &&
1563             dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
1564                 secpolicy_fs_mount_clearopts(cr, vfsp);
1565         }
1566
1567         /*
1568          * Check for mount privilege?
1569          *
1570          * If we don't have privilege then see if
1571          * we have local permission to allow it
1572          */
1573         error = secpolicy_fs_mount(cr, mvp, vfsp);
1574         if (error) {
1575                 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0)
1576                         goto out;
1577
1578                 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
1579                         vattr_t         vattr;
1580
1581                         /*
1582                          * Make sure user is the owner of the mount point
1583                          * or has sufficient privileges.
1584                          */
1585
1586                         vattr.va_mask = AT_UID;
1587
1588                         vn_lock(mvp, LK_SHARED | LK_RETRY);
1589                         if (VOP_GETATTR(mvp, &vattr, cr)) {
1590                                 VOP_UNLOCK(mvp, 0);
1591                                 goto out;
1592                         }
1593
1594                         if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
1595                             VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
1596                                 VOP_UNLOCK(mvp, 0);
1597                                 goto out;
1598                         }
1599                         VOP_UNLOCK(mvp, 0);
1600                 }
1601
1602                 secpolicy_fs_mount_clearopts(cr, vfsp);
1603         }
1604
1605         /*
1606          * Refuse to mount a filesystem if we are in a local zone and the
1607          * dataset is not visible.
1608          */
1609         if (!INGLOBALZONE(curthread) &&
1610             (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1611                 error = EPERM;
1612                 goto out;
1613         }
1614
1615 #ifdef SECLABEL
1616         error = zfs_mount_label_policy(vfsp, osname);
1617         if (error)
1618                 goto out;
1619 #endif
1620
1621         vfsp->vfs_flag |= MNT_NFS4ACLS;
1622
1623         /*
1624          * When doing a remount, we simply refresh our temporary properties
1625          * according to those options set in the current VFS options.
1626          */
1627         if (vfsp->vfs_flag & MS_REMOUNT) {
1628                 /* refresh mount options */
1629                 zfs_unregister_callbacks(vfsp->vfs_data);
1630                 error = zfs_register_callbacks(vfsp);
1631                 goto out;
1632         }
1633
1634         /* Initial root mount: try hard to import the requested root pool. */
1635         if ((vfsp->vfs_flag & MNT_ROOTFS) != 0 &&
1636             (vfsp->vfs_flag & MNT_UPDATE) == 0) {
1637                 char pname[MAXNAMELEN];
1638                 spa_t *spa;
1639                 int prefer_cache;
1640
1641                 error = getpoolname(osname, pname);
1642                 if (error)
1643                         goto out;
1644
1645                 prefer_cache = 1;
1646                 TUNABLE_INT_FETCH("vfs.zfs.rootpool.prefer_cached_config",
1647                     &prefer_cache);
1648                 mutex_enter(&spa_namespace_lock);
1649                 spa = spa_lookup(pname);
1650                 mutex_exit(&spa_namespace_lock);
1651                 if (!prefer_cache || spa == NULL) {
1652                         error = spa_import_rootpool(pname);
1653                         if (error)
1654                                 goto out;
1655                 }
1656         }
1657         DROP_GIANT();
1658         error = zfs_domount(vfsp, osname);
1659         PICKUP_GIANT();
1660
1661 #ifdef sun
1662         /*
1663          * Add an extra VFS_HOLD on our parent vfs so that it can't
1664          * disappear due to a forced unmount.
1665          */
1666         if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1667                 VFS_HOLD(mvp->v_vfsp);
1668 #endif  /* sun */
1669
1670 out:
1671         return (error);
1672 }
1673
1674 static int
1675 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
1676 {
1677         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1678         uint64_t refdbytes, availbytes, usedobjs, availobjs;
1679
1680         statp->f_version = STATFS_VERSION;
1681
1682         ZFS_ENTER(zfsvfs);
1683
1684         dmu_objset_space(zfsvfs->z_os,
1685             &refdbytes, &availbytes, &usedobjs, &availobjs);
1686
1687         /*
1688          * The underlying storage pool actually uses multiple block sizes.
1689          * We report the fragsize as the smallest block size we support,
1690          * and we report our blocksize as the filesystem's maximum blocksize.
1691          */
1692         statp->f_bsize = SPA_MINBLOCKSIZE;
1693         statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
1694
1695         /*
1696          * The following report "total" blocks of various kinds in the
1697          * file system, but reported in terms of f_frsize - the
1698          * "fragment" size.
1699          */
1700
1701         statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1702         statp->f_bfree = availbytes / statp->f_bsize;
1703         statp->f_bavail = statp->f_bfree; /* no root reservation */
1704
1705         /*
1706          * statvfs() should really be called statufs(), because it assumes
1707          * static metadata.  ZFS doesn't preallocate files, so the best
1708          * we can do is report the max that could possibly fit in f_files,
1709          * and that minus the number actually used in f_ffree.
1710          * For f_ffree, report the smaller of the number of object available
1711          * and the number of blocks (each object will take at least a block).
1712          */
1713         statp->f_ffree = MIN(availobjs, statp->f_bfree);
1714         statp->f_files = statp->f_ffree + usedobjs;
1715
1716         /*
1717          * We're a zfs filesystem.
1718          */
1719         (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
1720
1721         strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
1722             sizeof(statp->f_mntfromname));
1723         strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
1724             sizeof(statp->f_mntonname));
1725
1726         statp->f_namemax = ZFS_MAXNAMELEN;
1727
1728         ZFS_EXIT(zfsvfs);
1729         return (0);
1730 }
1731
1732 int
1733 zfs_vnode_lock(vnode_t *vp, int flags)
1734 {
1735         int error;
1736
1737         ASSERT(vp != NULL);
1738
1739         /*
1740          * Check if the file system wasn't forcibly unmounted in the meantime.
1741          */
1742         error = vn_lock(vp, flags);
1743         if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0) {
1744                 VOP_UNLOCK(vp, 0);
1745                 error = ENOENT;
1746         }
1747
1748         return (error);
1749 }
1750
1751 static int
1752 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
1753 {
1754         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1755         znode_t *rootzp;
1756         int error;
1757
1758         ZFS_ENTER_NOERROR(zfsvfs);
1759
1760         error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1761         if (error == 0)
1762                 *vpp = ZTOV(rootzp);
1763
1764         ZFS_EXIT(zfsvfs);
1765
1766         if (error == 0) {
1767                 error = zfs_vnode_lock(*vpp, flags);
1768                 if (error == 0)
1769                         (*vpp)->v_vflag |= VV_ROOT;
1770         }
1771         if (error != 0)
1772                 *vpp = NULL;
1773
1774         return (error);
1775 }
1776
1777 /*
1778  * Teardown the zfsvfs::z_os.
1779  *
1780  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1781  * and 'z_teardown_inactive_lock' held.
1782  */
1783 static int
1784 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1785 {
1786         znode_t *zp;
1787
1788         rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1789
1790         if (!unmounting) {
1791                 /*
1792                  * We purge the parent filesystem's vfsp as the parent
1793                  * filesystem and all of its snapshots have their vnode's
1794                  * v_vfsp set to the parent's filesystem's vfsp.  Note,
1795                  * 'z_parent' is self referential for non-snapshots.
1796                  */
1797                 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1798 #ifdef FREEBSD_NAMECACHE
1799                 cache_purgevfs(zfsvfs->z_parent->z_vfs);
1800 #endif
1801         }
1802
1803         /*
1804          * Close the zil. NB: Can't close the zil while zfs_inactive
1805          * threads are blocked as zil_close can call zfs_inactive.
1806          */
1807         if (zfsvfs->z_log) {
1808                 zil_close(zfsvfs->z_log);
1809                 zfsvfs->z_log = NULL;
1810         }
1811
1812         rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1813
1814         /*
1815          * If we are not unmounting (ie: online recv) and someone already
1816          * unmounted this file system while we were doing the switcheroo,
1817          * or a reopen of z_os failed then just bail out now.
1818          */
1819         if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1820                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1821                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1822                 return (EIO);
1823         }
1824
1825         /*
1826          * At this point there are no vops active, and any new vops will
1827          * fail with EIO since we have z_teardown_lock for writer (only
1828          * relavent for forced unmount).
1829          *
1830          * Release all holds on dbufs.
1831          */
1832         mutex_enter(&zfsvfs->z_znodes_lock);
1833         for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1834             zp = list_next(&zfsvfs->z_all_znodes, zp))
1835                 if (zp->z_sa_hdl) {
1836                         ASSERT(ZTOV(zp)->v_count >= 0);
1837                         zfs_znode_dmu_fini(zp);
1838                 }
1839         mutex_exit(&zfsvfs->z_znodes_lock);
1840
1841         /*
1842          * If we are unmounting, set the unmounted flag and let new vops
1843          * unblock.  zfs_inactive will have the unmounted behavior, and all
1844          * other vops will fail with EIO.
1845          */
1846         if (unmounting) {
1847                 zfsvfs->z_unmounted = B_TRUE;
1848                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1849                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1850
1851 #ifdef __FreeBSD__
1852                 /*
1853                  * Some znodes might not be fully reclaimed, wait for them.
1854                  */
1855                 mutex_enter(&zfsvfs->z_znodes_lock);
1856                 while (list_head(&zfsvfs->z_all_znodes) != NULL) {
1857                         msleep(zfsvfs, &zfsvfs->z_znodes_lock, 0,
1858                             "zteardown", 0);
1859                 }
1860                 mutex_exit(&zfsvfs->z_znodes_lock);
1861 #endif
1862         }
1863
1864         /*
1865          * z_os will be NULL if there was an error in attempting to reopen
1866          * zfsvfs, so just return as the properties had already been
1867          * unregistered and cached data had been evicted before.
1868          */
1869         if (zfsvfs->z_os == NULL)
1870                 return (0);
1871
1872         /*
1873          * Unregister properties.
1874          */
1875         zfs_unregister_callbacks(zfsvfs);
1876
1877         /*
1878          * Evict cached data
1879          */
1880         if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
1881                 if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1882                         txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1883         (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1884
1885         return (0);
1886 }
1887
1888 /*ARGSUSED*/
1889 static int
1890 zfs_umount(vfs_t *vfsp, int fflag)
1891 {
1892         kthread_t *td = curthread;
1893         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1894         objset_t *os;
1895         cred_t *cr = td->td_ucred;
1896         int ret;
1897
1898         ret = secpolicy_fs_unmount(cr, vfsp);
1899         if (ret) {
1900                 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1901                     ZFS_DELEG_PERM_MOUNT, cr))
1902                         return (ret);
1903         }
1904
1905         /*
1906          * We purge the parent filesystem's vfsp as the parent filesystem
1907          * and all of its snapshots have their vnode's v_vfsp set to the
1908          * parent's filesystem's vfsp.  Note, 'z_parent' is self
1909          * referential for non-snapshots.
1910          */
1911         (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1912
1913         /*
1914          * Unmount any snapshots mounted under .zfs before unmounting the
1915          * dataset itself.
1916          */
1917         if (zfsvfs->z_ctldir != NULL) {
1918                 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1919                         return (ret);
1920                 ret = vflush(vfsp, 0, 0, td);
1921                 ASSERT(ret == EBUSY);
1922                 if (!(fflag & MS_FORCE)) {
1923                         if (zfsvfs->z_ctldir->v_count > 1)
1924                                 return (EBUSY);
1925                         ASSERT(zfsvfs->z_ctldir->v_count == 1);
1926                 }
1927                 zfsctl_destroy(zfsvfs);
1928                 ASSERT(zfsvfs->z_ctldir == NULL);
1929         }
1930
1931         if (fflag & MS_FORCE) {
1932                 /*
1933                  * Mark file system as unmounted before calling
1934                  * vflush(FORCECLOSE). This way we ensure no future vnops
1935                  * will be called and risk operating on DOOMED vnodes.
1936                  */
1937                 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1938                 zfsvfs->z_unmounted = B_TRUE;
1939                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1940         }
1941
1942         /*
1943          * Flush all the files.
1944          */
1945         ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
1946         if (ret != 0) {
1947                 if (!zfsvfs->z_issnap) {
1948                         zfsctl_create(zfsvfs);
1949                         ASSERT(zfsvfs->z_ctldir != NULL);
1950                 }
1951                 return (ret);
1952         }
1953
1954         if (!(fflag & MS_FORCE)) {
1955                 /*
1956                  * Check the number of active vnodes in the file system.
1957                  * Our count is maintained in the vfs structure, but the
1958                  * number is off by 1 to indicate a hold on the vfs
1959                  * structure itself.
1960                  *
1961                  * The '.zfs' directory maintains a reference of its
1962                  * own, and any active references underneath are
1963                  * reflected in the vnode count.
1964                  */
1965                 if (zfsvfs->z_ctldir == NULL) {
1966                         if (vfsp->vfs_count > 1)
1967                                 return (EBUSY);
1968                 } else {
1969                         if (vfsp->vfs_count > 2 ||
1970                             zfsvfs->z_ctldir->v_count > 1)
1971                                 return (EBUSY);
1972                 }
1973         } else {
1974                 MNT_ILOCK(vfsp);
1975                 vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
1976                 MNT_IUNLOCK(vfsp);
1977         }
1978
1979         VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1980         os = zfsvfs->z_os;
1981
1982         /*
1983          * z_os will be NULL if there was an error in
1984          * attempting to reopen zfsvfs.
1985          */
1986         if (os != NULL) {
1987                 /*
1988                  * Unset the objset user_ptr.
1989                  */
1990                 mutex_enter(&os->os_user_ptr_lock);
1991                 dmu_objset_set_user(os, NULL);
1992                 mutex_exit(&os->os_user_ptr_lock);
1993
1994                 /*
1995                  * Finally release the objset
1996                  */
1997                 dmu_objset_disown(os, zfsvfs);
1998         }
1999
2000         /*
2001          * We can now safely destroy the '.zfs' directory node.
2002          */
2003         if (zfsvfs->z_ctldir != NULL)
2004                 zfsctl_destroy(zfsvfs);
2005         if (zfsvfs->z_issnap) {
2006                 vnode_t *svp = vfsp->mnt_vnodecovered;
2007
2008                 if (svp->v_count >= 2)
2009                         VN_RELE(svp);
2010         }
2011         zfs_freevfs(vfsp);
2012
2013         return (0);
2014 }
2015
2016 static int
2017 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
2018 {
2019         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
2020         znode_t         *zp;
2021         int             err;
2022
2023         /*
2024          * zfs_zget() can't operate on virtual entries like .zfs/ or
2025          * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP.
2026          * This will make NFS to switch to LOOKUP instead of using VGET.
2027          */
2028         if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR)
2029                 return (EOPNOTSUPP);
2030
2031         ZFS_ENTER(zfsvfs);
2032         err = zfs_zget(zfsvfs, ino, &zp);
2033         if (err == 0 && zp->z_unlinked) {
2034                 VN_RELE(ZTOV(zp));
2035                 err = EINVAL;
2036         }
2037         if (err == 0)
2038                 *vpp = ZTOV(zp);
2039         ZFS_EXIT(zfsvfs);
2040         if (err == 0)
2041                 err = zfs_vnode_lock(*vpp, flags);
2042         if (err != 0)
2043                 *vpp = NULL;
2044         return (err);
2045 }
2046
2047 static int
2048 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
2049     struct ucred **credanonp, int *numsecflavors, int **secflavors)
2050 {
2051         zfsvfs_t *zfsvfs = vfsp->vfs_data;
2052
2053         /*
2054          * If this is regular file system vfsp is the same as
2055          * zfsvfs->z_parent->z_vfs, but if it is snapshot,
2056          * zfsvfs->z_parent->z_vfs represents parent file system
2057          * which we have to use here, because only this file system
2058          * has mnt_export configured.
2059          */
2060         return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
2061             credanonp, numsecflavors, secflavors));
2062 }
2063
2064 CTASSERT(SHORT_FID_LEN <= sizeof(struct fid));
2065 CTASSERT(LONG_FID_LEN <= sizeof(struct fid));
2066
2067 static int
2068 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp)
2069 {
2070         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
2071         znode_t         *zp;
2072         uint64_t        object = 0;
2073         uint64_t        fid_gen = 0;
2074         uint64_t        gen_mask;
2075         uint64_t        zp_gen;
2076         int             i, err;
2077
2078         *vpp = NULL;
2079
2080         ZFS_ENTER(zfsvfs);
2081
2082         /*
2083          * On FreeBSD we can get snapshot's mount point or its parent file
2084          * system mount point depending if snapshot is already mounted or not.
2085          */
2086         if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
2087                 zfid_long_t     *zlfid = (zfid_long_t *)fidp;
2088                 uint64_t        objsetid = 0;
2089                 uint64_t        setgen = 0;
2090
2091                 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2092                         objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2093
2094                 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2095                         setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2096
2097                 ZFS_EXIT(zfsvfs);
2098
2099                 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2100                 if (err)
2101                         return (EINVAL);
2102                 ZFS_ENTER(zfsvfs);
2103         }
2104
2105         if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2106                 zfid_short_t    *zfid = (zfid_short_t *)fidp;
2107
2108                 for (i = 0; i < sizeof (zfid->zf_object); i++)
2109                         object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2110
2111                 for (i = 0; i < sizeof (zfid->zf_gen); i++)
2112                         fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2113         } else {
2114                 ZFS_EXIT(zfsvfs);
2115                 return (EINVAL);
2116         }
2117
2118         /* A zero fid_gen means we are in the .zfs control directories */
2119         if (fid_gen == 0 &&
2120             (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2121                 *vpp = zfsvfs->z_ctldir;
2122                 ASSERT(*vpp != NULL);
2123                 if (object == ZFSCTL_INO_SNAPDIR) {
2124                         VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
2125                             0, NULL, NULL, NULL, NULL, NULL) == 0);
2126                 } else {
2127                         VN_HOLD(*vpp);
2128                 }
2129                 ZFS_EXIT(zfsvfs);
2130                 err = zfs_vnode_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
2131                 if (err != 0)
2132                         *vpp = NULL;
2133                 return (err);
2134         }
2135
2136         gen_mask = -1ULL >> (64 - 8 * i);
2137
2138         dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2139         if (err = zfs_zget(zfsvfs, object, &zp)) {
2140                 ZFS_EXIT(zfsvfs);
2141                 return (err);
2142         }
2143         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2144             sizeof (uint64_t));
2145         zp_gen = zp_gen & gen_mask;
2146         if (zp_gen == 0)
2147                 zp_gen = 1;
2148         if (zp->z_unlinked || zp_gen != fid_gen) {
2149                 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2150                 VN_RELE(ZTOV(zp));
2151                 ZFS_EXIT(zfsvfs);
2152                 return (EINVAL);
2153         }
2154
2155         *vpp = ZTOV(zp);
2156         ZFS_EXIT(zfsvfs);
2157         err = zfs_vnode_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
2158         if (err == 0)
2159                 vnode_create_vobject(*vpp, zp->z_size, curthread);
2160         else
2161                 *vpp = NULL;
2162         return (err);
2163 }
2164
2165 /*
2166  * Block out VOPs and close zfsvfs_t::z_os
2167  *
2168  * Note, if successful, then we return with the 'z_teardown_lock' and
2169  * 'z_teardown_inactive_lock' write held.
2170  */
2171 int
2172 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2173 {
2174         int error;
2175
2176         if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2177                 return (error);
2178         dmu_objset_disown(zfsvfs->z_os, zfsvfs);
2179
2180         return (0);
2181 }
2182
2183 /*
2184  * Reopen zfsvfs_t::z_os and release VOPs.
2185  */
2186 int
2187 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
2188 {
2189         int err;
2190
2191         ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
2192         ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2193
2194         err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
2195             &zfsvfs->z_os);
2196         if (err) {
2197                 zfsvfs->z_os = NULL;
2198         } else {
2199                 znode_t *zp;
2200                 uint64_t sa_obj = 0;
2201
2202                 /*
2203                  * Make sure version hasn't changed
2204                  */
2205
2206                 err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
2207                     &zfsvfs->z_version);
2208
2209                 if (err)
2210                         goto bail;
2211
2212                 err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
2213                     ZFS_SA_ATTRS, 8, 1, &sa_obj);
2214
2215                 if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
2216                         goto bail;
2217
2218                 if ((err = sa_setup(zfsvfs->z_os, sa_obj,
2219                     zfs_attr_table,  ZPL_END, &zfsvfs->z_attr_table)) != 0)
2220                         goto bail;
2221
2222                 if (zfsvfs->z_version >= ZPL_VERSION_SA)
2223                         sa_register_update_callback(zfsvfs->z_os,
2224                             zfs_sa_upgrade);
2225
2226                 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2227
2228                 zfs_set_fuid_feature(zfsvfs);
2229
2230                 /*
2231                  * Attempt to re-establish all the active znodes with
2232                  * their dbufs.  If a zfs_rezget() fails, then we'll let
2233                  * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2234                  * when they try to use their znode.
2235                  */
2236                 mutex_enter(&zfsvfs->z_znodes_lock);
2237                 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2238                     zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2239                         (void) zfs_rezget(zp);
2240                 }
2241                 mutex_exit(&zfsvfs->z_znodes_lock);
2242         }
2243
2244 bail:
2245         /* release the VOPs */
2246         rw_exit(&zfsvfs->z_teardown_inactive_lock);
2247         rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
2248
2249         if (err) {
2250                 /*
2251                  * Since we couldn't reopen zfsvfs::z_os, or
2252                  * setup the sa framework force unmount this file system.
2253                  */
2254                 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2255                         (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
2256         }
2257         return (err);
2258 }
2259
2260 static void
2261 zfs_freevfs(vfs_t *vfsp)
2262 {
2263         zfsvfs_t *zfsvfs = vfsp->vfs_data;
2264
2265 #ifdef sun
2266         /*
2267          * If this is a snapshot, we have an extra VFS_HOLD on our parent
2268          * from zfs_mount().  Release it here.  If we came through
2269          * zfs_mountroot() instead, we didn't grab an extra hold, so
2270          * skip the VFS_RELE for rootvfs.
2271          */
2272         if (zfsvfs->z_issnap && (vfsp != rootvfs))
2273                 VFS_RELE(zfsvfs->z_parent->z_vfs);
2274 #endif  /* sun */
2275
2276         zfsvfs_free(zfsvfs);
2277
2278         atomic_add_32(&zfs_active_fs_count, -1);
2279 }
2280
2281 #ifdef __i386__
2282 static int desiredvnodes_backup;
2283 #endif
2284
2285 static void
2286 zfs_vnodes_adjust(void)
2287 {
2288 #ifdef __i386__
2289         int newdesiredvnodes;
2290
2291         desiredvnodes_backup = desiredvnodes;
2292
2293         /*
2294          * We calculate newdesiredvnodes the same way it is done in
2295          * vntblinit(). If it is equal to desiredvnodes, it means that
2296          * it wasn't tuned by the administrator and we can tune it down.
2297          */
2298         newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 *
2299             vm_kmem_size / (5 * (sizeof(struct vm_object) +
2300             sizeof(struct vnode))));
2301         if (newdesiredvnodes == desiredvnodes)
2302                 desiredvnodes = (3 * newdesiredvnodes) / 4;
2303 #endif
2304 }
2305
2306 static void
2307 zfs_vnodes_adjust_back(void)
2308 {
2309
2310 #ifdef __i386__
2311         desiredvnodes = desiredvnodes_backup;
2312 #endif
2313 }
2314
2315 void
2316 zfs_init(void)
2317 {
2318
2319         printf("ZFS filesystem version " ZPL_VERSION_STRING "\n");
2320
2321         /*
2322          * Initialize .zfs directory structures
2323          */
2324         zfsctl_init();
2325
2326         /*
2327          * Initialize znode cache, vnode ops, etc...
2328          */
2329         zfs_znode_init();
2330
2331         /*
2332          * Reduce number of vnodes. Originally number of vnodes is calculated
2333          * with UFS inode in mind. We reduce it here, because it's too big for
2334          * ZFS/i386.
2335          */
2336         zfs_vnodes_adjust();
2337
2338         dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2339 }
2340
2341 void
2342 zfs_fini(void)
2343 {
2344         zfsctl_fini();
2345         zfs_znode_fini();
2346         zfs_vnodes_adjust_back();
2347 }
2348
2349 int
2350 zfs_busy(void)
2351 {
2352         return (zfs_active_fs_count != 0);
2353 }
2354
2355 int
2356 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2357 {
2358         int error;
2359         objset_t *os = zfsvfs->z_os;
2360         dmu_tx_t *tx;
2361
2362         if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2363                 return (EINVAL);
2364
2365         if (newvers < zfsvfs->z_version)
2366                 return (EINVAL);
2367
2368         if (zfs_spa_version_map(newvers) >
2369             spa_version(dmu_objset_spa(zfsvfs->z_os)))
2370                 return (ENOTSUP);
2371
2372         tx = dmu_tx_create(os);
2373         dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2374         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2375                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2376                     ZFS_SA_ATTRS);
2377                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2378         }
2379         error = dmu_tx_assign(tx, TXG_WAIT);
2380         if (error) {
2381                 dmu_tx_abort(tx);
2382                 return (error);
2383         }
2384
2385         error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2386             8, 1, &newvers, tx);
2387
2388         if (error) {
2389                 dmu_tx_commit(tx);
2390                 return (error);
2391         }
2392
2393         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2394                 uint64_t sa_obj;
2395
2396                 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2397                     SPA_VERSION_SA);
2398                 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2399                     DMU_OT_NONE, 0, tx);
2400
2401                 error = zap_add(os, MASTER_NODE_OBJ,
2402                     ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2403                 ASSERT3U(error, ==, 0);
2404
2405                 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2406                 sa_register_update_callback(os, zfs_sa_upgrade);
2407         }
2408
2409         spa_history_log_internal(LOG_DS_UPGRADE,
2410             dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
2411             zfsvfs->z_version, newvers, dmu_objset_id(os));
2412
2413         dmu_tx_commit(tx);
2414
2415         zfsvfs->z_version = newvers;
2416
2417         zfs_set_fuid_feature(zfsvfs);
2418
2419         return (0);
2420 }
2421
2422 /*
2423  * Read a property stored within the master node.
2424  */
2425 int
2426 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2427 {
2428         const char *pname;
2429         int error = ENOENT;
2430
2431         /*
2432          * Look up the file system's value for the property.  For the
2433          * version property, we look up a slightly different string.
2434          */
2435         if (prop == ZFS_PROP_VERSION)
2436                 pname = ZPL_VERSION_STR;
2437         else
2438                 pname = zfs_prop_to_name(prop);
2439
2440         if (os != NULL)
2441                 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2442
2443         if (error == ENOENT) {
2444                 /* No value set, use the default value */
2445                 switch (prop) {
2446                 case ZFS_PROP_VERSION:
2447                         *value = ZPL_VERSION;
2448                         break;
2449                 case ZFS_PROP_NORMALIZE:
2450                 case ZFS_PROP_UTF8ONLY:
2451                         *value = 0;
2452                         break;
2453                 case ZFS_PROP_CASE:
2454                         *value = ZFS_CASE_SENSITIVE;
2455                         break;
2456                 default:
2457                         return (error);
2458                 }
2459                 error = 0;
2460         }
2461         return (error);
2462 }
2463
2464 #ifdef _KERNEL
2465 void
2466 zfsvfs_update_fromname(const char *oldname, const char *newname)
2467 {
2468         char tmpbuf[MAXPATHLEN];
2469         struct mount *mp;
2470         char *fromname;
2471         size_t oldlen;
2472
2473         oldlen = strlen(oldname);
2474
2475         mtx_lock(&mountlist_mtx);
2476         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2477                 fromname = mp->mnt_stat.f_mntfromname;
2478                 if (strcmp(fromname, oldname) == 0) {
2479                         (void)strlcpy(fromname, newname,
2480                             sizeof(mp->mnt_stat.f_mntfromname));
2481                         continue;
2482                 }
2483                 if (strncmp(fromname, oldname, oldlen) == 0 &&
2484                     (fromname[oldlen] == '/' || fromname[oldlen] == '@')) {
2485                         (void)snprintf(tmpbuf, sizeof(tmpbuf), "%s%s",
2486                             newname, fromname + oldlen);
2487                         (void)strlcpy(fromname, tmpbuf,
2488                             sizeof(mp->mnt_stat.f_mntfromname));
2489                         continue;
2490                 }
2491         }
2492         mtx_unlock(&mountlist_mtx);
2493 }
2494 #endif