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