]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_vfsops.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/sysmacros.h>
31 #include <sys/kmem.h>
32 #include <sys/acl.h>
33 #include <sys/vnode.h>
34 #include <sys/vfs.h>
35 #include <sys/mntent.h>
36 #include <sys/mount.h>
37 #include <sys/cmn_err.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_dir.h>
40 #include <sys/zil.h>
41 #include <sys/fs/zfs.h>
42 #include <sys/dmu.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/dsl_dataset.h>
45 #include <sys/dsl_deleg.h>
46 #include <sys/spa.h>
47 #include <sys/zap.h>
48 #include <sys/varargs.h>
49 #include <sys/policy.h>
50 #include <sys/atomic.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/zfs_ctldir.h>
53 #include <sys/zfs_fuid.h>
54 #include <sys/sunddi.h>
55 #include <sys/dnlc.h>
56 #include <sys/dmu_objset.h>
57 #include <sys/spa_boot.h>
58 #include <sys/vdev_impl.h>      /* VDEV_BOOT_VERSION */
59
60 struct mtx zfs_debug_mtx;
61 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
62
63 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
64
65 int zfs_super_owner = 0;
66 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
67     "File system owner can perform privileged operation on his file systems");
68
69 int zfs_debug_level = 0;
70 TUNABLE_INT("vfs.zfs.debug", &zfs_debug_level);
71 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RW, &zfs_debug_level, 0,
72     "Debug level");
73
74 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
75 static int zfs_version_acl = ZFS_ACL_VERSION;
76 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
77     "ZFS_ACL_VERSION");
78 static int zfs_version_dmu_backup_header = DMU_BACKUP_HEADER_VERSION;
79 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, dmu_backup_header, CTLFLAG_RD,
80     &zfs_version_dmu_backup_header, 0, "DMU_BACKUP_HEADER_VERSION");
81 static int zfs_version_dmu_backup_stream = DMU_BACKUP_STREAM_VERSION;
82 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, dmu_backup_stream, CTLFLAG_RD,
83     &zfs_version_dmu_backup_stream, 0, "DMU_BACKUP_STREAM_VERSION");
84 static int zfs_version_spa = SPA_VERSION;
85 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
86     "SPA_VERSION");
87 static int zfs_version_vdev_boot = VDEV_BOOT_VERSION;
88 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, vdev_boot, CTLFLAG_RD,
89     &zfs_version_vdev_boot, 0, "VDEV_BOOT_VERSION");
90 static int zfs_version_zpl = ZPL_VERSION;
91 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
92     "ZPL_VERSION");
93
94 static int zfs_mount(vfs_t *vfsp);
95 static int zfs_umount(vfs_t *vfsp, int fflag);
96 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
97 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
98 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
99 static int zfs_sync(vfs_t *vfsp, int waitfor);
100 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
101     struct ucred **credanonp, int *numsecflavors, int **secflavors);
102 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp);
103 static void zfs_objset_close(zfsvfs_t *zfsvfs);
104 static void zfs_freevfs(vfs_t *vfsp);
105
106 static struct vfsops zfs_vfsops = {
107         .vfs_mount =            zfs_mount,
108         .vfs_unmount =          zfs_umount,
109         .vfs_root =             zfs_root,
110         .vfs_statfs =           zfs_statfs,
111         .vfs_vget =             zfs_vget,
112         .vfs_sync =             zfs_sync,
113         .vfs_checkexp =         zfs_checkexp,
114         .vfs_fhtovp =           zfs_fhtovp,
115 };
116
117 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
118
119 /*
120  * We need to keep a count of active fs's.
121  * This is necessary to prevent our module
122  * from being unloaded after a umount -f
123  */
124 static uint32_t zfs_active_fs_count = 0;
125
126 /*ARGSUSED*/
127 static int
128 zfs_sync(vfs_t *vfsp, int waitfor)
129 {
130
131         /*
132          * Data integrity is job one.  We don't want a compromised kernel
133          * writing to the storage pool, so we never sync during panic.
134          */
135         if (panicstr)
136                 return (0);
137
138         if (vfsp != NULL) {
139                 /*
140                  * Sync a specific filesystem.
141                  */
142                 zfsvfs_t *zfsvfs = vfsp->vfs_data;
143                 int error;
144
145                 error = vfs_stdsync(vfsp, waitfor);
146                 if (error != 0)
147                         return (error);
148
149                 ZFS_ENTER(zfsvfs);
150                 if (zfsvfs->z_log != NULL)
151                         zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
152                 else
153                         txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
154                 ZFS_EXIT(zfsvfs);
155         } else {
156                 /*
157                  * Sync all ZFS filesystems.  This is what happens when you
158                  * run sync(1M).  Unlike other filesystems, ZFS honors the
159                  * request by waiting for all pools to commit all dirty data.
160                  */
161                 spa_sync_allpools();
162         }
163
164         return (0);
165 }
166
167 static void
168 atime_changed_cb(void *arg, uint64_t newval)
169 {
170         zfsvfs_t *zfsvfs = arg;
171
172         if (newval == TRUE) {
173                 zfsvfs->z_atime = TRUE;
174                 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
175                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
176                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
177         } else {
178                 zfsvfs->z_atime = FALSE;
179                 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
180                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
181                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
182         }
183 }
184
185 static void
186 xattr_changed_cb(void *arg, uint64_t newval)
187 {
188         zfsvfs_t *zfsvfs = arg;
189
190         if (newval == TRUE) {
191                 /* XXX locking on vfs_flag? */
192 #ifdef TODO
193                 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
194 #endif
195                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
196                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
197         } else {
198                 /* XXX locking on vfs_flag? */
199 #ifdef TODO
200                 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
201 #endif
202                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
203                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
204         }
205 }
206
207 static void
208 blksz_changed_cb(void *arg, uint64_t newval)
209 {
210         zfsvfs_t *zfsvfs = arg;
211
212         if (newval < SPA_MINBLOCKSIZE ||
213             newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
214                 newval = SPA_MAXBLOCKSIZE;
215
216         zfsvfs->z_max_blksz = newval;
217         zfsvfs->z_vfs->vfs_bsize = newval;
218 }
219
220 static void
221 readonly_changed_cb(void *arg, uint64_t newval)
222 {
223         zfsvfs_t *zfsvfs = arg;
224
225         if (newval) {
226                 /* XXX locking on vfs_flag? */
227                 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
228                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
229                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
230         } else {
231                 /* XXX locking on vfs_flag? */
232                 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
233                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
234                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
235         }
236 }
237
238 static void
239 setuid_changed_cb(void *arg, uint64_t newval)
240 {
241         zfsvfs_t *zfsvfs = arg;
242
243         if (newval == FALSE) {
244                 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
245                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
246                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
247         } else {
248                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
249                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
250                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
251         }
252 }
253
254 static void
255 exec_changed_cb(void *arg, uint64_t newval)
256 {
257         zfsvfs_t *zfsvfs = arg;
258
259         if (newval == FALSE) {
260                 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
261                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
262                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
263         } else {
264                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
265                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
266                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
267         }
268 }
269
270 /*
271  * The nbmand mount option can be changed at mount time.
272  * We can't allow it to be toggled on live file systems or incorrect
273  * behavior may be seen from cifs clients
274  *
275  * This property isn't registered via dsl_prop_register(), but this callback
276  * will be called when a file system is first mounted
277  */
278 static void
279 nbmand_changed_cb(void *arg, uint64_t newval)
280 {
281         zfsvfs_t *zfsvfs = arg;
282         if (newval == FALSE) {
283                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
284                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
285         } else {
286                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
287                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
288         }
289 }
290
291 static void
292 snapdir_changed_cb(void *arg, uint64_t newval)
293 {
294         zfsvfs_t *zfsvfs = arg;
295
296         zfsvfs->z_show_ctldir = newval;
297 }
298
299 static void
300 vscan_changed_cb(void *arg, uint64_t newval)
301 {
302         zfsvfs_t *zfsvfs = arg;
303
304         zfsvfs->z_vscan = newval;
305 }
306
307 static void
308 acl_mode_changed_cb(void *arg, uint64_t newval)
309 {
310         zfsvfs_t *zfsvfs = arg;
311
312         zfsvfs->z_acl_mode = newval;
313 }
314
315 static void
316 acl_inherit_changed_cb(void *arg, uint64_t newval)
317 {
318         zfsvfs_t *zfsvfs = arg;
319
320         zfsvfs->z_acl_inherit = newval;
321 }
322
323 static int
324 zfs_register_callbacks(vfs_t *vfsp)
325 {
326         struct dsl_dataset *ds = NULL;
327         objset_t *os = NULL;
328         zfsvfs_t *zfsvfs = NULL;
329         uint64_t nbmand;
330         int readonly, do_readonly = FALSE;
331         int setuid, do_setuid = FALSE;
332         int exec, do_exec = FALSE;
333         int xattr, do_xattr = FALSE;
334         int atime, do_atime = FALSE;
335         int error = 0;
336
337         ASSERT(vfsp);
338         zfsvfs = vfsp->vfs_data;
339         ASSERT(zfsvfs);
340         os = zfsvfs->z_os;
341
342         /*
343          * This function can be called for a snapshot when we update snapshot's
344          * mount point, which isn't really supported.
345          */
346         if (dmu_objset_is_snapshot(os))
347                 return (EOPNOTSUPP);
348
349         /*
350          * The act of registering our callbacks will destroy any mount
351          * options we may have.  In order to enable temporary overrides
352          * of mount options, we stash away the current values and
353          * restore them after we register the callbacks.
354          */
355         if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
356                 readonly = B_TRUE;
357                 do_readonly = B_TRUE;
358         } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
359                 readonly = B_FALSE;
360                 do_readonly = B_TRUE;
361         }
362         if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
363                 setuid = B_FALSE;
364                 do_setuid = B_TRUE;
365         } else {
366                 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
367                         setuid = B_FALSE;
368                         do_setuid = B_TRUE;
369                 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
370                         setuid = B_TRUE;
371                         do_setuid = B_TRUE;
372                 }
373         }
374         if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
375                 exec = B_FALSE;
376                 do_exec = B_TRUE;
377         } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
378                 exec = B_TRUE;
379                 do_exec = B_TRUE;
380         }
381         if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
382                 xattr = B_FALSE;
383                 do_xattr = B_TRUE;
384         } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
385                 xattr = B_TRUE;
386                 do_xattr = B_TRUE;
387         }
388         if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
389                 atime = B_FALSE;
390                 do_atime = B_TRUE;
391         } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
392                 atime = B_TRUE;
393                 do_atime = B_TRUE;
394         }
395
396         /*
397          * nbmand is a special property.  It can only be changed at
398          * mount time.
399          *
400          * This is weird, but it is documented to only be changeable
401          * at mount time.
402          */
403         if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
404                 nbmand = B_FALSE;
405         } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
406                 nbmand = B_TRUE;
407         } else {
408                 char osname[MAXNAMELEN];
409
410                 dmu_objset_name(os, osname);
411                 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
412                     NULL)) {
413                         return (error);
414                 }
415         }
416
417         /*
418          * Register property callbacks.
419          *
420          * It would probably be fine to just check for i/o error from
421          * the first prop_register(), but I guess I like to go
422          * overboard...
423          */
424         ds = dmu_objset_ds(os);
425         error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
426         error = error ? error : dsl_prop_register(ds,
427             "xattr", xattr_changed_cb, zfsvfs);
428         error = error ? error : dsl_prop_register(ds,
429             "recordsize", blksz_changed_cb, zfsvfs);
430         error = error ? error : dsl_prop_register(ds,
431             "readonly", readonly_changed_cb, zfsvfs);
432         error = error ? error : dsl_prop_register(ds,
433             "setuid", setuid_changed_cb, zfsvfs);
434         error = error ? error : dsl_prop_register(ds,
435             "exec", exec_changed_cb, zfsvfs);
436         error = error ? error : dsl_prop_register(ds,
437             "snapdir", snapdir_changed_cb, zfsvfs);
438         error = error ? error : dsl_prop_register(ds,
439             "aclmode", acl_mode_changed_cb, zfsvfs);
440         error = error ? error : dsl_prop_register(ds,
441             "aclinherit", acl_inherit_changed_cb, zfsvfs);
442         error = error ? error : dsl_prop_register(ds,
443             "vscan", vscan_changed_cb, zfsvfs);
444         if (error)
445                 goto unregister;
446
447         /*
448          * Invoke our callbacks to restore temporary mount options.
449          */
450         if (do_readonly)
451                 readonly_changed_cb(zfsvfs, readonly);
452         if (do_setuid)
453                 setuid_changed_cb(zfsvfs, setuid);
454         if (do_exec)
455                 exec_changed_cb(zfsvfs, exec);
456         if (do_xattr)
457                 xattr_changed_cb(zfsvfs, xattr);
458         if (do_atime)
459                 atime_changed_cb(zfsvfs, atime);
460
461         nbmand_changed_cb(zfsvfs, nbmand);
462
463         return (0);
464
465 unregister:
466         /*
467          * We may attempt to unregister some callbacks that are not
468          * registered, but this is OK; it will simply return ENOMSG,
469          * which we will ignore.
470          */
471         (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
472         (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
473         (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
474         (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
475         (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
476         (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
477         (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
478         (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
479         (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
480             zfsvfs);
481         (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
482         return (error);
483
484 }
485
486 static int
487 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
488 {
489         int error;
490
491         error = zfs_register_callbacks(zfsvfs->z_vfs);
492         if (error)
493                 return (error);
494
495         /*
496          * Set the objset user_ptr to track its zfsvfs.
497          */
498         mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock);
499         dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
500         mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock);
501
502         /*
503          * If we are not mounting (ie: online recv), then we don't
504          * have to worry about replaying the log as we blocked all
505          * operations out since we closed the ZIL.
506          */
507         if (mounting) {
508                 boolean_t readonly;
509
510                 /*
511                  * During replay we remove the read only flag to
512                  * allow replays to succeed.
513                  */
514                 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
515                 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
516
517                 /*
518                  * Parse and replay the intent log.
519                  */
520                 zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
521                     zfs_replay_vector, zfs_unlinked_drain);
522
523                 zfs_unlinked_drain(zfsvfs);
524                 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
525         }
526
527         if (!zil_disable)
528                 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
529
530         return (0);
531 }
532
533 static void
534 zfs_freezfsvfs(zfsvfs_t *zfsvfs)
535 {
536         mutex_destroy(&zfsvfs->z_znodes_lock);
537         mutex_destroy(&zfsvfs->z_online_recv_lock);
538         list_destroy(&zfsvfs->z_all_znodes);
539         rrw_destroy(&zfsvfs->z_teardown_lock);
540         rw_destroy(&zfsvfs->z_teardown_inactive_lock);
541         rw_destroy(&zfsvfs->z_fuid_lock);
542         kmem_free(zfsvfs, sizeof (zfsvfs_t));
543 }
544
545 static int
546 zfs_domount(vfs_t *vfsp, char *osname)
547 {
548         uint64_t recordsize, readonly;
549         int error = 0;
550         int mode;
551         zfsvfs_t *zfsvfs;
552         znode_t *zp = NULL;
553
554         ASSERT(vfsp);
555         ASSERT(osname);
556
557         /*
558          * Initialize the zfs-specific filesystem structure.
559          * Should probably make this a kmem cache, shuffle fields,
560          * and just bzero up to z_hold_mtx[].
561          */
562         zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
563         zfsvfs->z_vfs = vfsp;
564         zfsvfs->z_parent = zfsvfs;
565         zfsvfs->z_assign = TXG_NOWAIT;
566         zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
567         zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
568
569         mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
570         mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL);
571         list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
572             offsetof(znode_t, z_link_node));
573         rrw_init(&zfsvfs->z_teardown_lock);
574         rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
575         rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
576
577         if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
578             NULL))
579                 goto out;
580         zfsvfs->z_vfs->vfs_bsize = recordsize;
581
582         vfsp->vfs_data = zfsvfs;
583         vfsp->mnt_flag |= MNT_LOCAL;
584         vfsp->mnt_kern_flag |= MNTK_MPSAFE;
585         vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
586         vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
587
588         if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
589                 goto out;
590
591         mode = DS_MODE_OWNER;
592         if (readonly)
593                 mode |= DS_MODE_READONLY;
594
595         error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
596         if (error == EROFS) {
597                 mode = DS_MODE_OWNER | DS_MODE_READONLY;
598                 error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
599                     &zfsvfs->z_os);
600         }
601
602         if (error)
603                 goto out;
604
605         if (error = zfs_init_fs(zfsvfs, &zp))
606                 goto out;
607
608         /*
609          * Set features for file system.
610          */
611         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
612         if (zfsvfs->z_use_fuids) {
613                 vfs_set_feature(vfsp, VFSFT_XVATTR);
614                 vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
615                 vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS);
616                 vfs_set_feature(vfsp, VFSFT_ACLONCREATE);
617         }
618         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
619                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
620                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
621                 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
622         } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
623                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
624                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
625         }
626
627         if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
628                 uint64_t pval;
629
630                 ASSERT(mode & DS_MODE_READONLY);
631                 atime_changed_cb(zfsvfs, B_FALSE);
632                 readonly_changed_cb(zfsvfs, B_TRUE);
633                 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
634                         goto out;
635                 xattr_changed_cb(zfsvfs, pval);
636                 zfsvfs->z_issnap = B_TRUE;
637         } else {
638                 error = zfsvfs_setup(zfsvfs, B_TRUE);
639         }
640
641         vfs_mountedfrom(vfsp, osname);
642
643         if (!zfsvfs->z_issnap)
644                 zfsctl_create(zfsvfs);
645 out:
646         if (error) {
647                 if (zfsvfs->z_os)
648                         dmu_objset_close(zfsvfs->z_os);
649                 zfs_freezfsvfs(zfsvfs);
650         } else {
651                 atomic_add_32(&zfs_active_fs_count, 1);
652         }
653
654         return (error);
655 }
656
657 void
658 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
659 {
660         objset_t *os = zfsvfs->z_os;
661         struct dsl_dataset *ds;
662
663         /*
664          * Unregister properties.
665          */
666         if (!dmu_objset_is_snapshot(os)) {
667                 ds = dmu_objset_ds(os);
668                 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
669                     zfsvfs) == 0);
670
671                 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
672                     zfsvfs) == 0);
673
674                 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
675                     zfsvfs) == 0);
676
677                 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
678                     zfsvfs) == 0);
679
680                 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
681                     zfsvfs) == 0);
682
683                 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
684                     zfsvfs) == 0);
685
686                 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
687                     zfsvfs) == 0);
688
689                 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
690                     zfsvfs) == 0);
691
692                 VERIFY(dsl_prop_unregister(ds, "aclinherit",
693                     acl_inherit_changed_cb, zfsvfs) == 0);
694
695                 VERIFY(dsl_prop_unregister(ds, "vscan",
696                     vscan_changed_cb, zfsvfs) == 0);
697         }
698 }
699
700 /*ARGSUSED*/
701 static int
702 zfs_mount(vfs_t *vfsp)
703 {
704         kthread_t       *td = curthread;
705         vnode_t         *mvp = vfsp->mnt_vnodecovered;
706         cred_t          *cr = td->td_ucred;
707         char            *osname;
708         int             error = 0;
709         int             canwrite;
710
711         if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
712                 return (EINVAL);
713
714         /*
715          * If full-owner-access is enabled and delegated administration is
716          * turned on, we must set nosuid.
717          */
718         if (zfs_super_owner &&
719             dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
720                 secpolicy_fs_mount_clearopts(cr, vfsp);
721         }
722
723         /*
724          * Check for mount privilege?
725          *
726          * If we don't have privilege then see if
727          * we have local permission to allow it
728          */
729         error = secpolicy_fs_mount(cr, mvp, vfsp);
730         if (error) {
731                 error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr);
732                 if (error != 0)
733                         goto out;
734
735                 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
736                         vattr_t         vattr;
737
738                         /*
739                          * Make sure user is the owner of the mount point
740                          * or has sufficient privileges.
741                          */
742
743                         vattr.va_mask = AT_UID;
744
745                         vn_lock(mvp, LK_SHARED | LK_RETRY);
746                         if (error = VOP_GETATTR(mvp, &vattr, cr)) {
747                                 VOP_UNLOCK(mvp, 0);
748                                 goto out;
749                         }
750
751 #if 0 /* CHECK THIS! Is probably needed for zfs_suser. */
752                         if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
753                             VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
754                                 error = EPERM;
755                                 goto out;
756                         }
757 #else
758                         if (error = secpolicy_vnode_owner(mvp, cr, vattr.va_uid)) {
759                                 VOP_UNLOCK(mvp, 0);
760                                 goto out;
761                         }
762
763                         if (error = VOP_ACCESS(mvp, VWRITE, cr, td)) {
764                                 VOP_UNLOCK(mvp, 0);
765                                 goto out;
766                         }
767                         VOP_UNLOCK(mvp, 0);
768 #endif
769                 }
770
771                 secpolicy_fs_mount_clearopts(cr, vfsp);
772         }
773
774         /*
775          * Refuse to mount a filesystem if we are in a local zone and the
776          * dataset is not visible.
777          */
778         if (!INGLOBALZONE(curthread) &&
779             (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
780                 error = EPERM;
781                 goto out;
782         }
783
784         /*
785          * When doing a remount, we simply refresh our temporary properties
786          * according to those options set in the current VFS options.
787          */
788         if (vfsp->vfs_flag & MS_REMOUNT) {
789                 /* refresh mount options */
790                 zfs_unregister_callbacks(vfsp->vfs_data);
791                 error = zfs_register_callbacks(vfsp);
792                 goto out;
793         }
794
795         DROP_GIANT();
796         error = zfs_domount(vfsp, osname);
797         PICKUP_GIANT();
798 out:
799         return (error);
800 }
801
802 static int
803 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
804 {
805         zfsvfs_t *zfsvfs = vfsp->vfs_data;
806         uint64_t refdbytes, availbytes, usedobjs, availobjs;
807
808         statp->f_version = STATFS_VERSION;
809
810         ZFS_ENTER(zfsvfs);
811
812         dmu_objset_space(zfsvfs->z_os,
813             &refdbytes, &availbytes, &usedobjs, &availobjs);
814
815         /*
816          * The underlying storage pool actually uses multiple block sizes.
817          * We report the fragsize as the smallest block size we support,
818          * and we report our blocksize as the filesystem's maximum blocksize.
819          */
820         statp->f_bsize = zfsvfs->z_vfs->vfs_bsize;
821         statp->f_iosize = zfsvfs->z_vfs->vfs_bsize;
822
823         /*
824          * The following report "total" blocks of various kinds in the
825          * file system, but reported in terms of f_frsize - the
826          * "fragment" size.
827          */
828
829         statp->f_blocks = (refdbytes + availbytes) / statp->f_bsize;
830         statp->f_bfree = availbytes / statp->f_bsize;
831         statp->f_bavail = statp->f_bfree; /* no root reservation */
832
833         /*
834          * statvfs() should really be called statufs(), because it assumes
835          * static metadata.  ZFS doesn't preallocate files, so the best
836          * we can do is report the max that could possibly fit in f_files,
837          * and that minus the number actually used in f_ffree.
838          * For f_ffree, report the smaller of the number of object available
839          * and the number of blocks (each object will take at least a block).
840          */
841         statp->f_ffree = MIN(availobjs, statp->f_bfree);
842         statp->f_files = statp->f_ffree + usedobjs;
843
844         /*
845          * We're a zfs filesystem.
846          */
847         (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
848
849         strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
850             sizeof(statp->f_mntfromname));
851         strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
852             sizeof(statp->f_mntonname));
853
854         statp->f_namemax = ZFS_MAXNAMELEN;
855
856         ZFS_EXIT(zfsvfs);
857         return (0);
858 }
859
860 static int
861 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
862 {
863         zfsvfs_t *zfsvfs = vfsp->vfs_data;
864         znode_t *rootzp;
865         int error;
866
867         ZFS_ENTER_NOERROR(zfsvfs);
868
869         error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
870         if (error == 0) {
871                 *vpp = ZTOV(rootzp);
872                 error = vn_lock(*vpp, flags);
873                 (*vpp)->v_vflag |= VV_ROOT;
874         }
875
876         ZFS_EXIT(zfsvfs);
877         return (error);
878 }
879
880 /*
881  * Teardown the zfsvfs::z_os.
882  *
883  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
884  * and 'z_teardown_inactive_lock' held.
885  */
886 static int
887 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
888 {
889         znode_t *zp;
890
891         rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
892
893         if (!unmounting) {
894                 /*
895                  * We purge the parent filesystem's vfsp as the parent
896                  * filesystem and all of its snapshots have their vnode's
897                  * v_vfsp set to the parent's filesystem's vfsp.  Note,
898                  * 'z_parent' is self referential for non-snapshots.
899                  */
900                 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
901 #ifdef FREEBSD_NAMECACHE
902                 cache_purgevfs(zfsvfs->z_parent->z_vfs);
903 #endif
904         }
905
906         /*
907          * Close the zil. NB: Can't close the zil while zfs_inactive
908          * threads are blocked as zil_close can call zfs_inactive.
909          */
910         if (zfsvfs->z_log) {
911                 zil_close(zfsvfs->z_log);
912                 zfsvfs->z_log = NULL;
913         }
914
915         rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
916
917         /*
918          * If we are not unmounting (ie: online recv) and someone already
919          * unmounted this file system while we were doing the switcheroo,
920          * or a reopen of z_os failed then just bail out now.
921          */
922         if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
923                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
924                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
925                 return (EIO);
926         }
927
928         /*
929          * At this point there are no vops active, and any new vops will
930          * fail with EIO since we have z_teardown_lock for writer (only
931          * relavent for forced unmount).
932          *
933          * Release all holds on dbufs.
934          */
935         mutex_enter(&zfsvfs->z_znodes_lock);
936         for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
937             zp = list_next(&zfsvfs->z_all_znodes, zp))
938                 if (zp->z_dbuf) {
939                         ASSERT(ZTOV(zp)->v_count >= 0);
940                         zfs_znode_dmu_fini(zp);
941                 }
942         mutex_exit(&zfsvfs->z_znodes_lock);
943
944         /*
945          * If we are unmounting, set the unmounted flag and let new vops
946          * unblock.  zfs_inactive will have the unmounted behavior, and all
947          * other vops will fail with EIO.
948          */
949         if (unmounting) {
950                 zfsvfs->z_unmounted = B_TRUE;
951                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
952                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
953
954 #ifdef __FreeBSD__
955                 /*
956                  * Some znodes might not be fully reclaimed, wait for them.
957                  */
958                 mutex_enter(&zfsvfs->z_znodes_lock);
959                 while (list_head(&zfsvfs->z_all_znodes) != NULL) {
960                         msleep(zfsvfs, &zfsvfs->z_znodes_lock, 0,
961                             "zteardown", 0);
962                 }
963                 mutex_exit(&zfsvfs->z_znodes_lock);
964 #endif
965         }
966
967         /*
968          * z_os will be NULL if there was an error in attempting to reopen
969          * zfsvfs, so just return as the properties had already been
970          * unregistered and cached data had been evicted before.
971          */
972         if (zfsvfs->z_os == NULL)
973                 return (0);
974
975         /*
976          * Unregister properties.
977          */
978         zfs_unregister_callbacks(zfsvfs);
979
980         /*
981          * Evict cached data
982          */
983         if (dmu_objset_evict_dbufs(zfsvfs->z_os)) {
984                 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
985                 (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
986         }
987
988         return (0);
989 }
990
991 /*ARGSUSED*/
992 static int
993 zfs_umount(vfs_t *vfsp, int fflag)
994 {
995         zfsvfs_t *zfsvfs = vfsp->vfs_data;
996         objset_t *os;
997         cred_t *cr = curthread->td_ucred;
998         int ret;
999
1000         ret = secpolicy_fs_unmount(cr, vfsp);
1001         if (ret) {
1002                 ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1003                     ZFS_DELEG_PERM_MOUNT, cr);
1004                 if (ret)
1005                         return (ret);
1006         }
1007         /*
1008          * We purge the parent filesystem's vfsp as the parent filesystem
1009          * and all of its snapshots have their vnode's v_vfsp set to the
1010          * parent's filesystem's vfsp.  Note, 'z_parent' is self
1011          * referential for non-snapshots.
1012          */
1013         (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1014
1015         /*
1016          * Unmount any snapshots mounted under .zfs before unmounting the
1017          * dataset itself.
1018          */
1019         if (zfsvfs->z_ctldir != NULL) {
1020                 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1021                         return (ret);
1022                 ret = vflush(vfsp, 0, 0, curthread);
1023                 ASSERT(ret == EBUSY);
1024                 if (!(fflag & MS_FORCE)) {
1025                         if (zfsvfs->z_ctldir->v_count > 1)
1026                                 return (EBUSY);
1027                         ASSERT(zfsvfs->z_ctldir->v_count == 1);
1028                 }
1029                 zfsctl_destroy(zfsvfs);
1030                 ASSERT(zfsvfs->z_ctldir == NULL);
1031         }
1032
1033         if (fflag & MS_FORCE) {
1034                 /*
1035                  * Mark file system as unmounted before calling
1036                  * vflush(FORCECLOSE). This way we ensure no future vnops
1037                  * will be called and risk operating on DOOMED vnodes.
1038                  */
1039                 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1040                 zfsvfs->z_unmounted = B_TRUE;
1041                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1042         }
1043
1044         /*
1045          * Flush all the files.
1046          */
1047         ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, curthread);
1048         if (ret != 0) {
1049                 if (!zfsvfs->z_issnap) {
1050                         zfsctl_create(zfsvfs);
1051                         ASSERT(zfsvfs->z_ctldir != NULL);
1052                 }
1053                 return (ret);
1054         }
1055
1056         if (!(fflag & MS_FORCE)) {
1057                 /*
1058                  * Check the number of active vnodes in the file system.
1059                  * Our count is maintained in the vfs structure, but the
1060                  * number is off by 1 to indicate a hold on the vfs
1061                  * structure itself.
1062                  *
1063                  * The '.zfs' directory maintains a reference of its
1064                  * own, and any active references underneath are
1065                  * reflected in the vnode count.
1066                  */
1067                 if (zfsvfs->z_ctldir == NULL) {
1068                         if (vfsp->vfs_count > 1)
1069                                 return (EBUSY);
1070                 } else {
1071                         if (vfsp->vfs_count > 2 ||
1072                             zfsvfs->z_ctldir->v_count > 1)
1073                                 return (EBUSY);
1074                 }
1075         } else {
1076                 MNT_ILOCK(vfsp);
1077                 vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
1078                 MNT_IUNLOCK(vfsp);
1079         }
1080
1081         VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1082         os = zfsvfs->z_os;
1083
1084         /*
1085          * z_os will be NULL if there was an error in
1086          * attempting to reopen zfsvfs.
1087          */
1088         if (os != NULL) {
1089                 /*
1090                  * Unset the objset user_ptr.
1091                  */
1092                 mutex_enter(&os->os->os_user_ptr_lock);
1093                 dmu_objset_set_user(os, NULL);
1094                 mutex_exit(&os->os->os_user_ptr_lock);
1095
1096                 /*
1097                  * Finally release the objset
1098                  */
1099                 dmu_objset_close(os);
1100         }
1101
1102         /*
1103          * We can now safely destroy the '.zfs' directory node.
1104          */
1105         if (zfsvfs->z_ctldir != NULL)
1106                 zfsctl_destroy(zfsvfs);
1107         if (zfsvfs->z_issnap) {
1108                 vnode_t *svp = vfsp->mnt_vnodecovered;
1109
1110                 if (svp->v_count >= 2)
1111                         VN_RELE(svp);
1112         }
1113         zfs_freevfs(vfsp);
1114
1115         return (0);
1116 }
1117
1118 static int
1119 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1120 {
1121         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
1122         znode_t         *zp;
1123         int             err;
1124
1125         /*
1126          * XXXPJD: zfs_zget() can't operate on virtual entires like .zfs/ or
1127          * .zfs/snapshot/ directories, so for now just return EOPNOTSUPP.
1128          * This will make NFS to fall back to using READDIR instead of
1129          * READDIRPLUS.
1130          * Also snapshots are stored in AVL tree, but based on their names,
1131          * not inode numbers, so it will be very inefficient to iterate
1132          * over all snapshots to find the right one.
1133          * Note that OpenSolaris READDIRPLUS implementation does LOOKUP on
1134          * d_name, and not VGET on d_fileno as we do.
1135          */
1136         if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR)
1137                 return (EOPNOTSUPP);
1138
1139         ZFS_ENTER(zfsvfs);
1140         err = zfs_zget(zfsvfs, ino, &zp);
1141         if (err == 0 && zp->z_unlinked) {
1142                 VN_RELE(ZTOV(zp));
1143                 err = EINVAL;
1144         }
1145         if (err != 0)
1146                 *vpp = NULL;
1147         else {
1148                 *vpp = ZTOV(zp);
1149                 vn_lock(*vpp, flags);
1150         }
1151         ZFS_EXIT(zfsvfs);
1152         return (err);
1153 }
1154
1155 static int
1156 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
1157     struct ucred **credanonp, int *numsecflavors, int **secflavors)
1158 {
1159         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1160
1161         /*
1162          * If this is regular file system vfsp is the same as
1163          * zfsvfs->z_parent->z_vfs, but if it is snapshot,
1164          * zfsvfs->z_parent->z_vfs represents parent file system
1165          * which we have to use here, because only this file system
1166          * has mnt_export configured.
1167          */
1168         vfsp = zfsvfs->z_parent->z_vfs;
1169
1170         return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
1171             credanonp, numsecflavors, secflavors));
1172 }
1173
1174 CTASSERT(SHORT_FID_LEN <= sizeof(struct fid));
1175 CTASSERT(LONG_FID_LEN <= sizeof(struct fid));
1176
1177 static int
1178 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp)
1179 {
1180         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
1181         znode_t         *zp;
1182         uint64_t        object = 0;
1183         uint64_t        fid_gen = 0;
1184         uint64_t        gen_mask;
1185         uint64_t        zp_gen;
1186         int             i, err;
1187
1188         *vpp = NULL;
1189
1190         ZFS_ENTER(zfsvfs);
1191
1192         /*
1193          * On FreeBSD we can get snapshot's mount point or its parent file
1194          * system mount point depending if snapshot is already mounted or not.
1195          */
1196         if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
1197                 zfid_long_t     *zlfid = (zfid_long_t *)fidp;
1198                 uint64_t        objsetid = 0;
1199                 uint64_t        setgen = 0;
1200
1201                 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1202                         objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1203
1204                 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1205                         setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1206
1207                 ZFS_EXIT(zfsvfs);
1208
1209                 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1210                 if (err)
1211                         return (EINVAL);
1212                 ZFS_ENTER(zfsvfs);
1213         }
1214
1215         if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1216                 zfid_short_t    *zfid = (zfid_short_t *)fidp;
1217
1218                 for (i = 0; i < sizeof (zfid->zf_object); i++)
1219                         object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1220
1221                 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1222                         fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1223         } else {
1224                 ZFS_EXIT(zfsvfs);
1225                 return (EINVAL);
1226         }
1227
1228         /* A zero fid_gen means we are in the .zfs control directories */
1229         if (fid_gen == 0 &&
1230             (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1231                 *vpp = zfsvfs->z_ctldir;
1232                 ASSERT(*vpp != NULL);
1233                 if (object == ZFSCTL_INO_SNAPDIR) {
1234                         VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1235                             0, NULL, NULL, NULL, NULL, NULL) == 0);
1236                 } else {
1237                         VN_HOLD(*vpp);
1238                 }
1239                 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1240                 ZFS_EXIT(zfsvfs);
1241                 return (0);
1242         }
1243
1244         gen_mask = -1ULL >> (64 - 8 * i);
1245
1246         dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1247         if (err = zfs_zget(zfsvfs, object, &zp)) {
1248                 ZFS_EXIT(zfsvfs);
1249                 return (err);
1250         }
1251         zp_gen = zp->z_phys->zp_gen & gen_mask;
1252         if (zp_gen == 0)
1253                 zp_gen = 1;
1254         if (zp->z_unlinked || zp_gen != fid_gen) {
1255                 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1256                 VN_RELE(ZTOV(zp));
1257                 ZFS_EXIT(zfsvfs);
1258                 return (EINVAL);
1259         }
1260
1261         *vpp = ZTOV(zp);
1262         vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1263         vnode_create_vobject(*vpp, zp->z_phys->zp_size, curthread);
1264         ZFS_EXIT(zfsvfs);
1265         return (0);
1266 }
1267
1268 /*
1269  * Block out VOPs and close zfsvfs_t::z_os
1270  *
1271  * Note, if successful, then we return with the 'z_teardown_lock' and
1272  * 'z_teardown_inactive_lock' write held.
1273  */
1274 int
1275 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode)
1276 {
1277         int error;
1278
1279         if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1280                 return (error);
1281
1282         *mode = zfsvfs->z_os->os_mode;
1283         dmu_objset_name(zfsvfs->z_os, name);
1284         dmu_objset_close(zfsvfs->z_os);
1285
1286         return (0);
1287 }
1288
1289 /*
1290  * Reopen zfsvfs_t::z_os and release VOPs.
1291  */
1292 int
1293 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode)
1294 {
1295         int err;
1296
1297         ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1298         ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1299
1300         err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
1301         if (err) {
1302                 zfsvfs->z_os = NULL;
1303         } else {
1304                 znode_t *zp;
1305
1306                 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1307
1308                 /*
1309                  * Attempt to re-establish all the active znodes with
1310                  * their dbufs.  If a zfs_rezget() fails, then we'll let
1311                  * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1312                  * when they try to use their znode.
1313                  */
1314                 mutex_enter(&zfsvfs->z_znodes_lock);
1315                 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1316                     zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1317                         (void) zfs_rezget(zp);
1318                 }
1319                 mutex_exit(&zfsvfs->z_znodes_lock);
1320
1321         }
1322
1323         /* release the VOPs */
1324         rw_exit(&zfsvfs->z_teardown_inactive_lock);
1325         rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1326
1327         if (err) {
1328                 /*
1329                  * Since we couldn't reopen zfsvfs::z_os, force
1330                  * unmount this file system.
1331                  */
1332                 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1333                         (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
1334         }
1335         return (err);
1336 }
1337
1338 static void
1339 zfs_freevfs(vfs_t *vfsp)
1340 {
1341         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1342         int i;
1343
1344         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1345                 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1346
1347         zfs_fuid_destroy(zfsvfs);
1348         zfs_freezfsvfs(zfsvfs);
1349
1350         atomic_add_32(&zfs_active_fs_count, -1);
1351 }
1352
1353 #ifdef __i386__
1354 static int desiredvnodes_backup;
1355 #endif
1356
1357 static void
1358 zfs_vnodes_adjust(void)
1359 {
1360 #ifdef __i386__
1361         int newdesiredvnodes;
1362
1363         desiredvnodes_backup = desiredvnodes;
1364
1365         /*
1366          * We calculate newdesiredvnodes the same way it is done in
1367          * vntblinit(). If it is equal to desiredvnodes, it means that
1368          * it wasn't tuned by the administrator and we can tune it down.
1369          */
1370         newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 *
1371             vm_kmem_size / (5 * (sizeof(struct vm_object) +
1372             sizeof(struct vnode))));
1373         if (newdesiredvnodes == desiredvnodes)
1374                 desiredvnodes = (3 * newdesiredvnodes) / 4;
1375 #endif
1376 }
1377
1378 static void
1379 zfs_vnodes_adjust_back(void)
1380 {
1381
1382 #ifdef __i386__
1383         desiredvnodes = desiredvnodes_backup;
1384 #endif
1385 }
1386
1387 void
1388 zfs_init(void)
1389 {
1390
1391         printf("ZFS filesystem version " SPA_VERSION_STRING "\n");
1392
1393         /*
1394          * Initialize znode cache, vnode ops, etc...
1395          */
1396         zfs_znode_init();
1397
1398         /*
1399          * Initialize .zfs directory structures
1400          */
1401         zfsctl_init();
1402
1403         /*
1404          * Reduce number of vnode. Originally number of vnodes is calculated
1405          * with UFS inode in mind. We reduce it here, because it's too big for
1406          * ZFS/i386.
1407          */
1408         zfs_vnodes_adjust();
1409 }
1410
1411 void
1412 zfs_fini(void)
1413 {
1414         zfsctl_fini();
1415         zfs_znode_fini();
1416         zfs_vnodes_adjust_back();
1417 }
1418
1419 int
1420 zfs_busy(void)
1421 {
1422         return (zfs_active_fs_count != 0);
1423 }
1424
1425 int
1426 zfs_set_version(const char *name, uint64_t newvers)
1427 {
1428         int error;
1429         objset_t *os;
1430         dmu_tx_t *tx;
1431         uint64_t curvers;
1432
1433         /*
1434          * XXX for now, require that the filesystem be unmounted.  Would
1435          * be nice to find the zfsvfs_t and just update that if
1436          * possible.
1437          */
1438
1439         if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1440                 return (EINVAL);
1441
1442         error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_OWNER, &os);
1443         if (error)
1444                 return (error);
1445
1446         error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1447             8, 1, &curvers);
1448         if (error)
1449                 goto out;
1450         if (newvers < curvers) {
1451                 error = EINVAL;
1452                 goto out;
1453         }
1454
1455         tx = dmu_tx_create(os);
1456         dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR);
1457         error = dmu_tx_assign(tx, TXG_WAIT);
1458         if (error) {
1459                 dmu_tx_abort(tx);
1460                 goto out;
1461         }
1462         error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1,
1463             &newvers, tx);
1464
1465         spa_history_internal_log(LOG_DS_UPGRADE,
1466             dmu_objset_spa(os), tx, CRED(),
1467             "oldver=%llu newver=%llu dataset = %llu", curvers, newvers,
1468             dmu_objset_id(os));
1469         dmu_tx_commit(tx);
1470
1471 out:
1472         dmu_objset_close(os);
1473         return (error);
1474 }
1475 /*
1476  * Read a property stored within the master node.
1477  */
1478 int
1479 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1480 {
1481         const char *pname;
1482         int error = ENOENT;
1483
1484         /*
1485          * Look up the file system's value for the property.  For the
1486          * version property, we look up a slightly different string.
1487          */
1488         if (prop == ZFS_PROP_VERSION)
1489                 pname = ZPL_VERSION_STR;
1490         else
1491                 pname = zfs_prop_to_name(prop);
1492
1493         if (os != NULL)
1494                 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1495
1496         if (error == ENOENT) {
1497                 /* No value set, use the default value */
1498                 switch (prop) {
1499                 case ZFS_PROP_VERSION:
1500                         *value = ZPL_VERSION;
1501                         break;
1502                 case ZFS_PROP_NORMALIZE:
1503                 case ZFS_PROP_UTF8ONLY:
1504                         *value = 0;
1505                         break;
1506                 case ZFS_PROP_CASE:
1507                         *value = ZFS_CASE_SENSITIVE;
1508                         break;
1509                 default:
1510                         return (error);
1511                 }
1512                 error = 0;
1513         }
1514         return (error);
1515 }