]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vfsops.c
Update OpenZFS to 2.0.0-rc3-gfc5966
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / module / os / freebsd / 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  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
26  * Copyright (c) 2014 Integros [integros.com]
27  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28  */
29
30 /* Portions Copyright 2010 Robert Milkowski */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysmacros.h>
37 #include <sys/kmem.h>
38 #include <sys/acl.h>
39 #include <sys/vnode.h>
40 #include <sys/vfs.h>
41 #include <sys/mntent.h>
42 #include <sys/mount.h>
43 #include <sys/cmn_err.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_dir.h>
46 #include <sys/zil.h>
47 #include <sys/fs/zfs.h>
48 #include <sys/dmu.h>
49 #include <sys/dsl_prop.h>
50 #include <sys/dsl_dataset.h>
51 #include <sys/dsl_deleg.h>
52 #include <sys/spa.h>
53 #include <sys/zap.h>
54 #include <sys/sa.h>
55 #include <sys/sa_impl.h>
56 #include <sys/policy.h>
57 #include <sys/atomic.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/zfs_ctldir.h>
60 #include <sys/zfs_fuid.h>
61 #include <sys/sunddi.h>
62 #include <sys/dmu_objset.h>
63 #include <sys/dsl_dir.h>
64 #include <sys/spa_boot.h>
65 #include <sys/jail.h>
66 #include <ufs/ufs/quota.h>
67 #include <sys/zfs_quota.h>
68
69 #include "zfs_comutil.h"
70
71 #ifndef MNTK_VMSETSIZE_BUG
72 #define MNTK_VMSETSIZE_BUG      0
73 #endif
74 #ifndef MNTK_NOMSYNC
75 #define MNTK_NOMSYNC    8
76 #endif
77
78 /* BEGIN CSTYLED */
79 struct mtx zfs_debug_mtx;
80 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
81
82 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
83
84 int zfs_super_owner;
85 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
86     "File system owner can perform privileged operation on his file systems");
87
88 int zfs_debug_level;
89 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RWTUN, &zfs_debug_level, 0,
90         "Debug level");
91
92 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
93 static int zfs_version_acl = ZFS_ACL_VERSION;
94 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
95     "ZFS_ACL_VERSION");
96 static int zfs_version_spa = SPA_VERSION;
97 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
98     "SPA_VERSION");
99 static int zfs_version_zpl = ZPL_VERSION;
100 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
101     "ZPL_VERSION");
102 /* END CSTYLED */
103
104 static int zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg);
105 static int zfs_mount(vfs_t *vfsp);
106 static int zfs_umount(vfs_t *vfsp, int fflag);
107 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
108 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
109 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
110 static int zfs_sync(vfs_t *vfsp, int waitfor);
111 #if __FreeBSD_version >= 1300098
112 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, uint64_t *extflagsp,
113     struct ucred **credanonp, int *numsecflavors, int *secflavors);
114 #else
115 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
116     struct ucred **credanonp, int *numsecflavors, int **secflavors);
117 #endif
118 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp);
119 static void zfs_freevfs(vfs_t *vfsp);
120
121 struct vfsops zfs_vfsops = {
122         .vfs_mount =            zfs_mount,
123         .vfs_unmount =          zfs_umount,
124 #if __FreeBSD_version >= 1300049
125         .vfs_root =             vfs_cache_root,
126         .vfs_cachedroot = zfs_root,
127 #else
128         .vfs_root =             zfs_root,
129 #endif
130         .vfs_statfs =           zfs_statfs,
131         .vfs_vget =             zfs_vget,
132         .vfs_sync =             zfs_sync,
133         .vfs_checkexp =         zfs_checkexp,
134         .vfs_fhtovp =           zfs_fhtovp,
135         .vfs_quotactl =         zfs_quotactl,
136 };
137
138 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
139
140 /*
141  * We need to keep a count of active fs's.
142  * This is necessary to prevent our module
143  * from being unloaded after a umount -f
144  */
145 static uint32_t zfs_active_fs_count = 0;
146
147 int
148 zfs_get_temporary_prop(dsl_dataset_t *ds, zfs_prop_t zfs_prop, uint64_t *val,
149     char *setpoint)
150 {
151         int error;
152         zfsvfs_t *zfvp;
153         vfs_t *vfsp;
154         objset_t *os;
155         uint64_t tmp = *val;
156
157         error = dmu_objset_from_ds(ds, &os);
158         if (error != 0)
159                 return (error);
160
161         error = getzfsvfs_impl(os, &zfvp);
162         if (error != 0)
163                 return (error);
164         if (zfvp == NULL)
165                 return (ENOENT);
166         vfsp = zfvp->z_vfs;
167         switch (zfs_prop) {
168         case ZFS_PROP_ATIME:
169                 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL))
170                         tmp = 0;
171                 if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL))
172                         tmp = 1;
173                 break;
174         case ZFS_PROP_DEVICES:
175                 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
176                         tmp = 0;
177                 if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL))
178                         tmp = 1;
179                 break;
180         case ZFS_PROP_EXEC:
181                 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
182                         tmp = 0;
183                 if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL))
184                         tmp = 1;
185                 break;
186         case ZFS_PROP_SETUID:
187                 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
188                         tmp = 0;
189                 if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL))
190                         tmp = 1;
191                 break;
192         case ZFS_PROP_READONLY:
193                 if (vfs_optionisset(vfsp, MNTOPT_RW, NULL))
194                         tmp = 0;
195                 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL))
196                         tmp = 1;
197                 break;
198         case ZFS_PROP_XATTR:
199                 if (zfvp->z_flags & ZSB_XATTR)
200                         tmp = zfvp->z_xattr;
201                 break;
202         case ZFS_PROP_NBMAND:
203                 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL))
204                         tmp = 0;
205                 if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL))
206                         tmp = 1;
207                 break;
208         default:
209                 vfs_unbusy(vfsp);
210                 return (ENOENT);
211         }
212
213         vfs_unbusy(vfsp);
214         if (tmp != *val) {
215                 (void) strcpy(setpoint, "temporary");
216                 *val = tmp;
217         }
218         return (0);
219 }
220
221 static int
222 zfs_getquota(zfsvfs_t *zfsvfs, uid_t id, int isgroup, struct dqblk64 *dqp)
223 {
224         int error = 0;
225         char buf[32];
226         uint64_t usedobj, quotaobj;
227         uint64_t quota, used = 0;
228         timespec_t now;
229
230         usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
231         quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
232
233         if (quotaobj == 0 || zfsvfs->z_replay) {
234                 error = ENOENT;
235                 goto done;
236         }
237         (void) sprintf(buf, "%llx", (longlong_t)id);
238         if ((error = zap_lookup(zfsvfs->z_os, quotaobj,
239             buf, sizeof (quota), 1, &quota)) != 0) {
240                 dprintf("%s(%d): quotaobj lookup failed\n",
241                     __FUNCTION__, __LINE__);
242                 goto done;
243         }
244         /*
245          * quota(8) uses bsoftlimit as "quoota", and hardlimit as "limit".
246          * So we set them to be the same.
247          */
248         dqp->dqb_bsoftlimit = dqp->dqb_bhardlimit = btodb(quota);
249         error = zap_lookup(zfsvfs->z_os, usedobj, buf, sizeof (used), 1, &used);
250         if (error && error != ENOENT) {
251                 dprintf("%s(%d):  usedobj failed; %d\n",
252                     __FUNCTION__, __LINE__, error);
253                 goto done;
254         }
255         dqp->dqb_curblocks = btodb(used);
256         dqp->dqb_ihardlimit = dqp->dqb_isoftlimit = 0;
257         vfs_timestamp(&now);
258         /*
259          * Setting this to 0 causes FreeBSD quota(8) to print
260          * the number of days since the epoch, which isn't
261          * particularly useful.
262          */
263         dqp->dqb_btime = dqp->dqb_itime = now.tv_sec;
264 done:
265         return (error);
266 }
267
268 static int
269 zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg)
270 {
271         zfsvfs_t *zfsvfs = vfsp->vfs_data;
272         struct thread *td;
273         int cmd, type, error = 0;
274         int bitsize;
275         zfs_userquota_prop_t quota_type;
276         struct dqblk64 dqblk = { 0 };
277
278         td = curthread;
279         cmd = cmds >> SUBCMDSHIFT;
280         type = cmds & SUBCMDMASK;
281
282         ZFS_ENTER(zfsvfs);
283         if (id == -1) {
284                 switch (type) {
285                 case USRQUOTA:
286                         id = td->td_ucred->cr_ruid;
287                         break;
288                 case GRPQUOTA:
289                         id = td->td_ucred->cr_rgid;
290                         break;
291                 default:
292                         error = EINVAL;
293                         if (cmd == Q_QUOTAON || cmd == Q_QUOTAOFF)
294                                 vfs_unbusy(vfsp);
295                         goto done;
296                 }
297         }
298         /*
299          * Map BSD type to:
300          * ZFS_PROP_USERUSED,
301          * ZFS_PROP_USERQUOTA,
302          * ZFS_PROP_GROUPUSED,
303          * ZFS_PROP_GROUPQUOTA
304          */
305         switch (cmd) {
306         case Q_SETQUOTA:
307         case Q_SETQUOTA32:
308                 if (type == USRQUOTA)
309                         quota_type = ZFS_PROP_USERQUOTA;
310                 else if (type == GRPQUOTA)
311                         quota_type = ZFS_PROP_GROUPQUOTA;
312                 else
313                         error = EINVAL;
314                 break;
315         case Q_GETQUOTA:
316         case Q_GETQUOTA32:
317                 if (type == USRQUOTA)
318                         quota_type = ZFS_PROP_USERUSED;
319                 else if (type == GRPQUOTA)
320                         quota_type = ZFS_PROP_GROUPUSED;
321                 else
322                         error = EINVAL;
323                 break;
324         }
325
326         /*
327          * Depending on the cmd, we may need to get
328          * the ruid and domain (see fuidstr_to_sid?),
329          * the fuid (how?), or other information.
330          * Create fuid using zfs_fuid_create(zfsvfs, id,
331          * ZFS_OWNER or ZFS_GROUP, cr, &fuidp)?
332          * I think I can use just the id?
333          *
334          * Look at zfs_id_overquota() to look up a quota.
335          * zap_lookup(something, quotaobj, fuidstring,
336          *     sizeof (long long), 1, &quota)
337          *
338          * See zfs_set_userquota() to set a quota.
339          */
340         if ((uint32_t)type >= MAXQUOTAS) {
341                 error = EINVAL;
342                 goto done;
343         }
344
345         switch (cmd) {
346         case Q_GETQUOTASIZE:
347                 bitsize = 64;
348                 error = copyout(&bitsize, arg, sizeof (int));
349                 break;
350         case Q_QUOTAON:
351                 // As far as I can tell, you can't turn quotas on or off on zfs
352                 error = 0;
353                 vfs_unbusy(vfsp);
354                 break;
355         case Q_QUOTAOFF:
356                 error = ENOTSUP;
357                 vfs_unbusy(vfsp);
358                 break;
359         case Q_SETQUOTA:
360                 error = copyin(arg, &dqblk, sizeof (dqblk));
361                 if (error == 0)
362                         error = zfs_set_userquota(zfsvfs, quota_type,
363                             "", id, dbtob(dqblk.dqb_bhardlimit));
364                 break;
365         case Q_GETQUOTA:
366                 error = zfs_getquota(zfsvfs, id, type == GRPQUOTA, &dqblk);
367                 if (error == 0)
368                         error = copyout(&dqblk, arg, sizeof (dqblk));
369                 break;
370         default:
371                 error = EINVAL;
372                 break;
373         }
374 done:
375         ZFS_EXIT(zfsvfs);
376         return (error);
377 }
378
379
380 boolean_t
381 zfs_is_readonly(zfsvfs_t *zfsvfs)
382 {
383         return (!!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY));
384 }
385
386 /*ARGSUSED*/
387 static int
388 zfs_sync(vfs_t *vfsp, int waitfor)
389 {
390
391         /*
392          * Data integrity is job one.  We don't want a compromised kernel
393          * writing to the storage pool, so we never sync during panic.
394          */
395         if (panicstr)
396                 return (0);
397
398         /*
399          * Ignore the system syncher.  ZFS already commits async data
400          * at zfs_txg_timeout intervals.
401          */
402         if (waitfor == MNT_LAZY)
403                 return (0);
404
405         if (vfsp != NULL) {
406                 /*
407                  * Sync a specific filesystem.
408                  */
409                 zfsvfs_t *zfsvfs = vfsp->vfs_data;
410                 dsl_pool_t *dp;
411                 int error;
412
413                 error = vfs_stdsync(vfsp, waitfor);
414                 if (error != 0)
415                         return (error);
416
417                 ZFS_ENTER(zfsvfs);
418                 dp = dmu_objset_pool(zfsvfs->z_os);
419
420                 /*
421                  * If the system is shutting down, then skip any
422                  * filesystems which may exist on a suspended pool.
423                  */
424                 if (rebooting && spa_suspended(dp->dp_spa)) {
425                         ZFS_EXIT(zfsvfs);
426                         return (0);
427                 }
428
429                 if (zfsvfs->z_log != NULL)
430                         zil_commit(zfsvfs->z_log, 0);
431
432                 ZFS_EXIT(zfsvfs);
433         } else {
434                 /*
435                  * Sync all ZFS filesystems.  This is what happens when you
436                  * run sync(1M).  Unlike other filesystems, ZFS honors the
437                  * request by waiting for all pools to commit all dirty data.
438                  */
439                 spa_sync_allpools();
440         }
441
442         return (0);
443 }
444
445 static void
446 atime_changed_cb(void *arg, uint64_t newval)
447 {
448         zfsvfs_t *zfsvfs = arg;
449
450         if (newval == TRUE) {
451                 zfsvfs->z_atime = TRUE;
452                 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
453                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
454                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
455         } else {
456                 zfsvfs->z_atime = FALSE;
457                 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
458                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
459                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
460         }
461 }
462
463 static void
464 xattr_changed_cb(void *arg, uint64_t newval)
465 {
466         zfsvfs_t *zfsvfs = arg;
467
468         if (newval == ZFS_XATTR_OFF) {
469                 zfsvfs->z_flags &= ~ZSB_XATTR;
470         } else {
471                 zfsvfs->z_flags |= ZSB_XATTR;
472
473                 if (newval == ZFS_XATTR_SA)
474                         zfsvfs->z_xattr_sa = B_TRUE;
475                 else
476                         zfsvfs->z_xattr_sa = B_FALSE;
477         }
478 }
479
480 static void
481 blksz_changed_cb(void *arg, uint64_t newval)
482 {
483         zfsvfs_t *zfsvfs = arg;
484         ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
485         ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
486         ASSERT(ISP2(newval));
487
488         zfsvfs->z_max_blksz = newval;
489         zfsvfs->z_vfs->mnt_stat.f_iosize = newval;
490 }
491
492 static void
493 readonly_changed_cb(void *arg, uint64_t newval)
494 {
495         zfsvfs_t *zfsvfs = arg;
496
497         if (newval) {
498                 /* XXX locking on vfs_flag? */
499                 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
500                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
501                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
502         } else {
503                 /* XXX locking on vfs_flag? */
504                 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
505                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
506                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
507         }
508 }
509
510 static void
511 setuid_changed_cb(void *arg, uint64_t newval)
512 {
513         zfsvfs_t *zfsvfs = arg;
514
515         if (newval == FALSE) {
516                 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
517                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
518                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
519         } else {
520                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
521                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
522                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
523         }
524 }
525
526 static void
527 exec_changed_cb(void *arg, uint64_t newval)
528 {
529         zfsvfs_t *zfsvfs = arg;
530
531         if (newval == FALSE) {
532                 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
533                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
534                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
535         } else {
536                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
537                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
538                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
539         }
540 }
541
542 /*
543  * The nbmand mount option can be changed at mount time.
544  * We can't allow it to be toggled on live file systems or incorrect
545  * behavior may be seen from cifs clients
546  *
547  * This property isn't registered via dsl_prop_register(), but this callback
548  * will be called when a file system is first mounted
549  */
550 static void
551 nbmand_changed_cb(void *arg, uint64_t newval)
552 {
553         zfsvfs_t *zfsvfs = arg;
554         if (newval == FALSE) {
555                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
556                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
557         } else {
558                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
559                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
560         }
561 }
562
563 static void
564 snapdir_changed_cb(void *arg, uint64_t newval)
565 {
566         zfsvfs_t *zfsvfs = arg;
567
568         zfsvfs->z_show_ctldir = newval;
569 }
570
571 static void
572 vscan_changed_cb(void *arg, uint64_t newval)
573 {
574         zfsvfs_t *zfsvfs = arg;
575
576         zfsvfs->z_vscan = newval;
577 }
578
579 static void
580 acl_mode_changed_cb(void *arg, uint64_t newval)
581 {
582         zfsvfs_t *zfsvfs = arg;
583
584         zfsvfs->z_acl_mode = newval;
585 }
586
587 static void
588 acl_inherit_changed_cb(void *arg, uint64_t newval)
589 {
590         zfsvfs_t *zfsvfs = arg;
591
592         zfsvfs->z_acl_inherit = newval;
593 }
594
595 static void
596 acl_type_changed_cb(void *arg, uint64_t newval)
597 {
598         zfsvfs_t *zfsvfs = arg;
599
600         zfsvfs->z_acl_type = newval;
601 }
602
603 static int
604 zfs_register_callbacks(vfs_t *vfsp)
605 {
606         struct dsl_dataset *ds = NULL;
607         objset_t *os = NULL;
608         zfsvfs_t *zfsvfs = NULL;
609         uint64_t nbmand;
610         boolean_t readonly = B_FALSE;
611         boolean_t do_readonly = B_FALSE;
612         boolean_t setuid = B_FALSE;
613         boolean_t do_setuid = B_FALSE;
614         boolean_t exec = B_FALSE;
615         boolean_t do_exec = B_FALSE;
616         boolean_t xattr = B_FALSE;
617         boolean_t atime = B_FALSE;
618         boolean_t do_atime = B_FALSE;
619         boolean_t do_xattr = B_FALSE;
620         int error = 0;
621
622         ASSERT(vfsp);
623         zfsvfs = vfsp->vfs_data;
624         ASSERT(zfsvfs);
625         os = zfsvfs->z_os;
626
627         /*
628          * This function can be called for a snapshot when we update snapshot's
629          * mount point, which isn't really supported.
630          */
631         if (dmu_objset_is_snapshot(os))
632                 return (EOPNOTSUPP);
633
634         /*
635          * The act of registering our callbacks will destroy any mount
636          * options we may have.  In order to enable temporary overrides
637          * of mount options, we stash away the current values and
638          * restore them after we register the callbacks.
639          */
640         if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
641             !spa_writeable(dmu_objset_spa(os))) {
642                 readonly = B_TRUE;
643                 do_readonly = B_TRUE;
644         } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
645                 readonly = B_FALSE;
646                 do_readonly = B_TRUE;
647         }
648         if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
649                 setuid = B_FALSE;
650                 do_setuid = B_TRUE;
651         } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
652                 setuid = B_TRUE;
653                 do_setuid = B_TRUE;
654         }
655         if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
656                 exec = B_FALSE;
657                 do_exec = B_TRUE;
658         } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
659                 exec = B_TRUE;
660                 do_exec = B_TRUE;
661         }
662         if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
663                 zfsvfs->z_xattr = xattr = ZFS_XATTR_OFF;
664                 do_xattr = B_TRUE;
665         } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
666                 zfsvfs->z_xattr = xattr = ZFS_XATTR_DIR;
667                 do_xattr = B_TRUE;
668         } else if (vfs_optionisset(vfsp, MNTOPT_DIRXATTR, NULL)) {
669                 zfsvfs->z_xattr = xattr = ZFS_XATTR_DIR;
670                 do_xattr = B_TRUE;
671         } else if (vfs_optionisset(vfsp, MNTOPT_SAXATTR, NULL)) {
672                 zfsvfs->z_xattr = xattr = ZFS_XATTR_SA;
673                 do_xattr = B_TRUE;
674         }
675         if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
676                 atime = B_FALSE;
677                 do_atime = B_TRUE;
678         } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
679                 atime = B_TRUE;
680                 do_atime = B_TRUE;
681         }
682
683         /*
684          * We need to enter pool configuration here, so that we can use
685          * dsl_prop_get_int_ds() to handle the special nbmand property below.
686          * dsl_prop_get_integer() can not be used, because it has to acquire
687          * spa_namespace_lock and we can not do that because we already hold
688          * z_teardown_lock.  The problem is that spa_write_cachefile() is called
689          * with spa_namespace_lock held and the function calls ZFS vnode
690          * operations to write the cache file and thus z_teardown_lock is
691          * acquired after spa_namespace_lock.
692          */
693         ds = dmu_objset_ds(os);
694         dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
695
696         /*
697          * nbmand is a special property.  It can only be changed at
698          * mount time.
699          *
700          * This is weird, but it is documented to only be changeable
701          * at mount time.
702          */
703         if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
704                 nbmand = B_FALSE;
705         } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
706                 nbmand = B_TRUE;
707         } else if ((error = dsl_prop_get_int_ds(ds, "nbmand", &nbmand) != 0)) {
708                 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
709                 return (error);
710         }
711
712         /*
713          * Register property callbacks.
714          *
715          * It would probably be fine to just check for i/o error from
716          * the first prop_register(), but I guess I like to go
717          * overboard...
718          */
719         error = dsl_prop_register(ds,
720             zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
721         error = error ? error : dsl_prop_register(ds,
722             zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
723         error = error ? error : dsl_prop_register(ds,
724             zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
725         error = error ? error : dsl_prop_register(ds,
726             zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
727         error = error ? error : dsl_prop_register(ds,
728             zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
729         error = error ? error : dsl_prop_register(ds,
730             zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
731         error = error ? error : dsl_prop_register(ds,
732             zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
733         error = error ? error : dsl_prop_register(ds,
734             zfs_prop_to_name(ZFS_PROP_ACLTYPE), acl_type_changed_cb, zfsvfs);
735         error = error ? error : dsl_prop_register(ds,
736             zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
737         error = error ? error : dsl_prop_register(ds,
738             zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
739             zfsvfs);
740         error = error ? error : dsl_prop_register(ds,
741             zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
742         dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
743         if (error)
744                 goto unregister;
745
746         /*
747          * Invoke our callbacks to restore temporary mount options.
748          */
749         if (do_readonly)
750                 readonly_changed_cb(zfsvfs, readonly);
751         if (do_setuid)
752                 setuid_changed_cb(zfsvfs, setuid);
753         if (do_exec)
754                 exec_changed_cb(zfsvfs, exec);
755         if (do_xattr)
756                 xattr_changed_cb(zfsvfs, xattr);
757         if (do_atime)
758                 atime_changed_cb(zfsvfs, atime);
759
760         nbmand_changed_cb(zfsvfs, nbmand);
761
762         return (0);
763
764 unregister:
765         dsl_prop_unregister_all(ds, zfsvfs);
766         return (error);
767 }
768
769 /*
770  * Associate this zfsvfs with the given objset, which must be owned.
771  * This will cache a bunch of on-disk state from the objset in the
772  * zfsvfs.
773  */
774 static int
775 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
776 {
777         int error;
778         uint64_t val;
779
780         zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
781         zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
782         zfsvfs->z_os = os;
783
784         error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
785         if (error != 0)
786                 return (error);
787         if (zfsvfs->z_version >
788             zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
789                 (void) printf("Can't mount a version %lld file system "
790                     "on a version %lld pool\n. Pool must be upgraded to mount "
791                     "this file system.", (u_longlong_t)zfsvfs->z_version,
792                     (u_longlong_t)spa_version(dmu_objset_spa(os)));
793                 return (SET_ERROR(ENOTSUP));
794         }
795         error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
796         if (error != 0)
797                 return (error);
798         zfsvfs->z_norm = (int)val;
799
800         error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
801         if (error != 0)
802                 return (error);
803         zfsvfs->z_utf8 = (val != 0);
804
805         error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
806         if (error != 0)
807                 return (error);
808         zfsvfs->z_case = (uint_t)val;
809
810         error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &val);
811         if (error != 0)
812                 return (error);
813         zfsvfs->z_acl_type = (uint_t)val;
814
815         /*
816          * Fold case on file systems that are always or sometimes case
817          * insensitive.
818          */
819         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
820             zfsvfs->z_case == ZFS_CASE_MIXED)
821                 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
822
823         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
824         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
825
826         uint64_t sa_obj = 0;
827         if (zfsvfs->z_use_sa) {
828                 /* should either have both of these objects or none */
829                 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
830                     &sa_obj);
831                 if (error != 0)
832                         return (error);
833         }
834
835         error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
836             &zfsvfs->z_attr_table);
837         if (error != 0)
838                 return (error);
839
840         if (zfsvfs->z_version >= ZPL_VERSION_SA)
841                 sa_register_update_callback(os, zfs_sa_upgrade);
842
843         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
844             &zfsvfs->z_root);
845         if (error != 0)
846                 return (error);
847         ASSERT(zfsvfs->z_root != 0);
848
849         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
850             &zfsvfs->z_unlinkedobj);
851         if (error != 0)
852                 return (error);
853
854         error = zap_lookup(os, MASTER_NODE_OBJ,
855             zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
856             8, 1, &zfsvfs->z_userquota_obj);
857         if (error == ENOENT)
858                 zfsvfs->z_userquota_obj = 0;
859         else if (error != 0)
860                 return (error);
861
862         error = zap_lookup(os, MASTER_NODE_OBJ,
863             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
864             8, 1, &zfsvfs->z_groupquota_obj);
865         if (error == ENOENT)
866                 zfsvfs->z_groupquota_obj = 0;
867         else if (error != 0)
868                 return (error);
869
870         error = zap_lookup(os, MASTER_NODE_OBJ,
871             zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
872             8, 1, &zfsvfs->z_projectquota_obj);
873         if (error == ENOENT)
874                 zfsvfs->z_projectquota_obj = 0;
875         else if (error != 0)
876                 return (error);
877
878         error = zap_lookup(os, MASTER_NODE_OBJ,
879             zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
880             8, 1, &zfsvfs->z_userobjquota_obj);
881         if (error == ENOENT)
882                 zfsvfs->z_userobjquota_obj = 0;
883         else if (error != 0)
884                 return (error);
885
886         error = zap_lookup(os, MASTER_NODE_OBJ,
887             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
888             8, 1, &zfsvfs->z_groupobjquota_obj);
889         if (error == ENOENT)
890                 zfsvfs->z_groupobjquota_obj = 0;
891         else if (error != 0)
892                 return (error);
893
894         error = zap_lookup(os, MASTER_NODE_OBJ,
895             zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
896             8, 1, &zfsvfs->z_projectobjquota_obj);
897         if (error == ENOENT)
898                 zfsvfs->z_projectobjquota_obj = 0;
899         else if (error != 0)
900                 return (error);
901
902         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
903             &zfsvfs->z_fuid_obj);
904         if (error == ENOENT)
905                 zfsvfs->z_fuid_obj = 0;
906         else if (error != 0)
907                 return (error);
908
909         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
910             &zfsvfs->z_shares_dir);
911         if (error == ENOENT)
912                 zfsvfs->z_shares_dir = 0;
913         else if (error != 0)
914                 return (error);
915
916         /*
917          * Only use the name cache if we are looking for a
918          * name on a file system that does not require normalization
919          * or case folding.  We can also look there if we happen to be
920          * on a non-normalizing, mixed sensitivity file system IF we
921          * are looking for the exact name (which is always the case on
922          * FreeBSD).
923          */
924         zfsvfs->z_use_namecache = !zfsvfs->z_norm ||
925             ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
926             !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER));
927
928         return (0);
929 }
930
931 taskq_t *zfsvfs_taskq;
932
933 static void
934 zfsvfs_task_unlinked_drain(void *context, int pending __unused)
935 {
936
937         zfs_unlinked_drain((zfsvfs_t *)context);
938 }
939
940 int
941 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
942 {
943         objset_t *os;
944         zfsvfs_t *zfsvfs;
945         int error;
946         boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
947
948         /*
949          * XXX: Fix struct statfs so this isn't necessary!
950          *
951          * The 'osname' is used as the filesystem's special node, which means
952          * it must fit in statfs.f_mntfromname, or else it can't be
953          * enumerated, so libzfs_mnttab_find() returns NULL, which causes
954          * 'zfs unmount' to think it's not mounted when it is.
955          */
956         if (strlen(osname) >= MNAMELEN)
957                 return (SET_ERROR(ENAMETOOLONG));
958
959         zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
960
961         error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs,
962             &os);
963         if (error != 0) {
964                 kmem_free(zfsvfs, sizeof (zfsvfs_t));
965                 return (error);
966         }
967
968         error = zfsvfs_create_impl(zfvp, zfsvfs, os);
969
970         return (error);
971 }
972
973
974 int
975 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
976 {
977         int error;
978
979         zfsvfs->z_vfs = NULL;
980         zfsvfs->z_parent = zfsvfs;
981
982         mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
983         mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
984         list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
985             offsetof(znode_t, z_link_node));
986         TASK_INIT(&zfsvfs->z_unlinked_drain_task, 0,
987             zfsvfs_task_unlinked_drain, zfsvfs);
988 #ifdef DIAGNOSTIC
989         rrm_init(&zfsvfs->z_teardown_lock, B_TRUE);
990 #else
991         rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
992 #endif
993         ZFS_INIT_TEARDOWN_INACTIVE(zfsvfs);
994         rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
995         for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
996                 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
997
998         error = zfsvfs_init(zfsvfs, os);
999         if (error != 0) {
1000                 dmu_objset_disown(os, B_TRUE, zfsvfs);
1001                 *zfvp = NULL;
1002                 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1003                 return (error);
1004         }
1005
1006         *zfvp = zfsvfs;
1007         return (0);
1008 }
1009
1010 static int
1011 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1012 {
1013         int error;
1014
1015         /*
1016          * Check for a bad on-disk format version now since we
1017          * lied about owning the dataset readonly before.
1018          */
1019         if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) &&
1020             dmu_objset_incompatible_encryption_version(zfsvfs->z_os))
1021                 return (SET_ERROR(EROFS));
1022
1023         error = zfs_register_callbacks(zfsvfs->z_vfs);
1024         if (error)
1025                 return (error);
1026
1027         zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1028
1029         /*
1030          * If we are not mounting (ie: online recv), then we don't
1031          * have to worry about replaying the log as we blocked all
1032          * operations out since we closed the ZIL.
1033          */
1034         if (mounting) {
1035                 boolean_t readonly;
1036
1037                 ASSERT3P(zfsvfs->z_kstat.dk_kstats, ==, NULL);
1038                 dataset_kstats_create(&zfsvfs->z_kstat, zfsvfs->z_os);
1039
1040                 /*
1041                  * During replay we remove the read only flag to
1042                  * allow replays to succeed.
1043                  */
1044                 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1045                 if (readonly != 0) {
1046                         zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1047                 } else {
1048                         dsl_dir_t *dd;
1049                         zap_stats_t zs;
1050
1051                         if (zap_get_stats(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
1052                             &zs) == 0) {
1053                                 dataset_kstats_update_nunlinks_kstat(
1054                                     &zfsvfs->z_kstat, zs.zs_num_entries);
1055                                 dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
1056                                     "num_entries in unlinked set: %llu",
1057                                     zs.zs_num_entries);
1058                         }
1059
1060                         zfs_unlinked_drain(zfsvfs);
1061                         dd = zfsvfs->z_os->os_dsl_dataset->ds_dir;
1062                         dd->dd_activity_cancelled = B_FALSE;
1063                 }
1064
1065                 /*
1066                  * Parse and replay the intent log.
1067                  *
1068                  * Because of ziltest, this must be done after
1069                  * zfs_unlinked_drain().  (Further note: ziltest
1070                  * doesn't use readonly mounts, where
1071                  * zfs_unlinked_drain() isn't called.)  This is because
1072                  * ziltest causes spa_sync() to think it's committed,
1073                  * but actually it is not, so the intent log contains
1074                  * many txg's worth of changes.
1075                  *
1076                  * In particular, if object N is in the unlinked set in
1077                  * the last txg to actually sync, then it could be
1078                  * actually freed in a later txg and then reallocated
1079                  * in a yet later txg.  This would write a "create
1080                  * object N" record to the intent log.  Normally, this
1081                  * would be fine because the spa_sync() would have
1082                  * written out the fact that object N is free, before
1083                  * we could write the "create object N" intent log
1084                  * record.
1085                  *
1086                  * But when we are in ziltest mode, we advance the "open
1087                  * txg" without actually spa_sync()-ing the changes to
1088                  * disk.  So we would see that object N is still
1089                  * allocated and in the unlinked set, and there is an
1090                  * intent log record saying to allocate it.
1091                  */
1092                 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1093                         if (zil_replay_disable) {
1094                                 zil_destroy(zfsvfs->z_log, B_FALSE);
1095                         } else {
1096                                 boolean_t use_nc = zfsvfs->z_use_namecache;
1097                                 zfsvfs->z_use_namecache = B_FALSE;
1098                                 zfsvfs->z_replay = B_TRUE;
1099                                 zil_replay(zfsvfs->z_os, zfsvfs,
1100                                     zfs_replay_vector);
1101                                 zfsvfs->z_replay = B_FALSE;
1102                                 zfsvfs->z_use_namecache = use_nc;
1103                         }
1104                 }
1105
1106                 /* restore readonly bit */
1107                 if (readonly != 0)
1108                         zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
1109         }
1110
1111         /*
1112          * Set the objset user_ptr to track its zfsvfs.
1113          */
1114         mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1115         dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1116         mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1117
1118         return (0);
1119 }
1120
1121 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1122
1123 void
1124 zfsvfs_free(zfsvfs_t *zfsvfs)
1125 {
1126         int i;
1127
1128         /*
1129          * This is a barrier to prevent the filesystem from going away in
1130          * zfs_znode_move() until we can safely ensure that the filesystem is
1131          * not unmounted. We consider the filesystem valid before the barrier
1132          * and invalid after the barrier.
1133          */
1134         rw_enter(&zfsvfs_lock, RW_READER);
1135         rw_exit(&zfsvfs_lock);
1136
1137         zfs_fuid_destroy(zfsvfs);
1138
1139         mutex_destroy(&zfsvfs->z_znodes_lock);
1140         mutex_destroy(&zfsvfs->z_lock);
1141         ASSERT(zfsvfs->z_nr_znodes == 0);
1142         list_destroy(&zfsvfs->z_all_znodes);
1143         rrm_destroy(&zfsvfs->z_teardown_lock);
1144         ZFS_DESTROY_TEARDOWN_INACTIVE(zfsvfs);
1145         rw_destroy(&zfsvfs->z_fuid_lock);
1146         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1147                 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1148         dataset_kstats_destroy(&zfsvfs->z_kstat);
1149         kmem_free(zfsvfs, sizeof (zfsvfs_t));
1150 }
1151
1152 static void
1153 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1154 {
1155         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1156         if (zfsvfs->z_vfs) {
1157                 if (zfsvfs->z_use_fuids) {
1158                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1159                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1160                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1161                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1162                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1163                         vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1164                 } else {
1165                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1166                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1167                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1168                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1169                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1170                         vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1171                 }
1172         }
1173         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1174 }
1175
1176 static int
1177 zfs_domount(vfs_t *vfsp, char *osname)
1178 {
1179         uint64_t recordsize, fsid_guid;
1180         int error = 0;
1181         zfsvfs_t *zfsvfs;
1182
1183         ASSERT(vfsp);
1184         ASSERT(osname);
1185
1186         error = zfsvfs_create(osname, vfsp->mnt_flag & MNT_RDONLY, &zfsvfs);
1187         if (error)
1188                 return (error);
1189         zfsvfs->z_vfs = vfsp;
1190
1191         if ((error = dsl_prop_get_integer(osname,
1192             "recordsize", &recordsize, NULL)))
1193                 goto out;
1194         zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
1195         zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
1196
1197         vfsp->vfs_data = zfsvfs;
1198         vfsp->mnt_flag |= MNT_LOCAL;
1199         vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
1200         vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
1201         vfsp->mnt_kern_flag |= MNTK_EXTENDED_SHARED;
1202         /*
1203          * This can cause a loss of coherence between ARC and page cache
1204          * on ZoF - unclear if the problem is in FreeBSD or ZoF
1205          */
1206         vfsp->mnt_kern_flag |= MNTK_NO_IOPF;    /* vn_io_fault can be used */
1207         vfsp->mnt_kern_flag |= MNTK_NOMSYNC;
1208         vfsp->mnt_kern_flag |= MNTK_VMSETSIZE_BUG;
1209
1210 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
1211         vfsp->mnt_kern_flag |= MNTK_FPLOOKUP;
1212 #endif
1213         /*
1214          * The fsid is 64 bits, composed of an 8-bit fs type, which
1215          * separates our fsid from any other filesystem types, and a
1216          * 56-bit objset unique ID.  The objset unique ID is unique to
1217          * all objsets open on this system, provided by unique_create().
1218          * The 8-bit fs type must be put in the low bits of fsid[1]
1219          * because that's where other Solaris filesystems put it.
1220          */
1221         fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1222         ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1223         vfsp->vfs_fsid.val[0] = fsid_guid;
1224         vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1225             (vfsp->mnt_vfc->vfc_typenum & 0xFF);
1226
1227         /*
1228          * Set features for file system.
1229          */
1230         zfs_set_fuid_feature(zfsvfs);
1231         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1232                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1233                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1234                 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1235         } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1236                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1237                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1238         }
1239         vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1240
1241         if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1242                 uint64_t pval;
1243
1244                 atime_changed_cb(zfsvfs, B_FALSE);
1245                 readonly_changed_cb(zfsvfs, B_TRUE);
1246                 if ((error = dsl_prop_get_integer(osname,
1247                     "xattr", &pval, NULL)))
1248                         goto out;
1249                 xattr_changed_cb(zfsvfs, pval);
1250                 if ((error = dsl_prop_get_integer(osname,
1251                     "acltype", &pval, NULL)))
1252                         goto out;
1253                 acl_type_changed_cb(zfsvfs, pval);
1254                 zfsvfs->z_issnap = B_TRUE;
1255                 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1256
1257                 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1258                 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1259                 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1260         } else {
1261                 if ((error = zfsvfs_setup(zfsvfs, B_TRUE)))
1262                         goto out;
1263         }
1264
1265         vfs_mountedfrom(vfsp, osname);
1266
1267         if (!zfsvfs->z_issnap)
1268                 zfsctl_create(zfsvfs);
1269 out:
1270         if (error) {
1271                 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1272                 zfsvfs_free(zfsvfs);
1273         } else {
1274                 atomic_inc_32(&zfs_active_fs_count);
1275         }
1276
1277         return (error);
1278 }
1279
1280 static void
1281 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1282 {
1283         objset_t *os = zfsvfs->z_os;
1284
1285         if (!dmu_objset_is_snapshot(os))
1286                 dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1287 }
1288
1289 static int
1290 getpoolname(const char *osname, char *poolname)
1291 {
1292         char *p;
1293
1294         p = strchr(osname, '/');
1295         if (p == NULL) {
1296                 if (strlen(osname) >= MAXNAMELEN)
1297                         return (ENAMETOOLONG);
1298                 (void) strcpy(poolname, osname);
1299         } else {
1300                 if (p - osname >= MAXNAMELEN)
1301                         return (ENAMETOOLONG);
1302                 (void) strncpy(poolname, osname, p - osname);
1303                 poolname[p - osname] = '\0';
1304         }
1305         return (0);
1306 }
1307
1308 /*ARGSUSED*/
1309 static int
1310 zfs_mount(vfs_t *vfsp)
1311 {
1312         kthread_t       *td = curthread;
1313         vnode_t         *mvp = vfsp->mnt_vnodecovered;
1314         cred_t          *cr = td->td_ucred;
1315         char            *osname;
1316         int             error = 0;
1317         int             canwrite;
1318
1319         if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
1320                 return (SET_ERROR(EINVAL));
1321
1322         /*
1323          * If full-owner-access is enabled and delegated administration is
1324          * turned on, we must set nosuid.
1325          */
1326         if (zfs_super_owner &&
1327             dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
1328                 secpolicy_fs_mount_clearopts(cr, vfsp);
1329         }
1330
1331         /*
1332          * Check for mount privilege?
1333          *
1334          * If we don't have privilege then see if
1335          * we have local permission to allow it
1336          */
1337         error = secpolicy_fs_mount(cr, mvp, vfsp);
1338         if (error) {
1339                 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0)
1340                         goto out;
1341
1342                 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
1343                         vattr_t         vattr;
1344
1345                         /*
1346                          * Make sure user is the owner of the mount point
1347                          * or has sufficient privileges.
1348                          */
1349
1350                         vattr.va_mask = AT_UID;
1351
1352                         vn_lock(mvp, LK_SHARED | LK_RETRY);
1353                         if (VOP_GETATTR(mvp, &vattr, cr)) {
1354                                 VOP_UNLOCK1(mvp);
1355                                 goto out;
1356                         }
1357
1358                         if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
1359                             VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
1360                                 VOP_UNLOCK1(mvp);
1361                                 goto out;
1362                         }
1363                         VOP_UNLOCK1(mvp);
1364                 }
1365
1366                 secpolicy_fs_mount_clearopts(cr, vfsp);
1367         }
1368
1369         /*
1370          * Refuse to mount a filesystem if we are in a local zone and the
1371          * dataset is not visible.
1372          */
1373         if (!INGLOBALZONE(curproc) &&
1374             (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1375                 error = SET_ERROR(EPERM);
1376                 goto out;
1377         }
1378
1379         vfsp->vfs_flag |= MNT_NFS4ACLS;
1380
1381         /*
1382          * When doing a remount, we simply refresh our temporary properties
1383          * according to those options set in the current VFS options.
1384          */
1385         if (vfsp->vfs_flag & MS_REMOUNT) {
1386                 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1387
1388                 /*
1389                  * Refresh mount options with z_teardown_lock blocking I/O while
1390                  * the filesystem is in an inconsistent state.
1391                  * The lock also serializes this code with filesystem
1392                  * manipulations between entry to zfs_suspend_fs() and return
1393                  * from zfs_resume_fs().
1394                  */
1395                 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1396                 zfs_unregister_callbacks(zfsvfs);
1397                 error = zfs_register_callbacks(vfsp);
1398                 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1399                 goto out;
1400         }
1401
1402         /* Initial root mount: try hard to import the requested root pool. */
1403         if ((vfsp->vfs_flag & MNT_ROOTFS) != 0 &&
1404             (vfsp->vfs_flag & MNT_UPDATE) == 0) {
1405                 char pname[MAXNAMELEN];
1406
1407                 error = getpoolname(osname, pname);
1408                 if (error == 0)
1409                         error = spa_import_rootpool(pname, false);
1410                 if (error)
1411                         goto out;
1412         }
1413         DROP_GIANT();
1414         error = zfs_domount(vfsp, osname);
1415         PICKUP_GIANT();
1416
1417 out:
1418         return (error);
1419 }
1420
1421 static int
1422 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
1423 {
1424         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1425         uint64_t refdbytes, availbytes, usedobjs, availobjs;
1426
1427         statp->f_version = STATFS_VERSION;
1428
1429         ZFS_ENTER(zfsvfs);
1430
1431         dmu_objset_space(zfsvfs->z_os,
1432             &refdbytes, &availbytes, &usedobjs, &availobjs);
1433
1434         /*
1435          * The underlying storage pool actually uses multiple block sizes.
1436          * We report the fragsize as the smallest block size we support,
1437          * and we report our blocksize as the filesystem's maximum blocksize.
1438          */
1439         statp->f_bsize = SPA_MINBLOCKSIZE;
1440         statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
1441
1442         /*
1443          * The following report "total" blocks of various kinds in the
1444          * file system, but reported in terms of f_frsize - the
1445          * "fragment" size.
1446          */
1447
1448         statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1449         statp->f_bfree = availbytes / statp->f_bsize;
1450         statp->f_bavail = statp->f_bfree; /* no root reservation */
1451
1452         /*
1453          * statvfs() should really be called statufs(), because it assumes
1454          * static metadata.  ZFS doesn't preallocate files, so the best
1455          * we can do is report the max that could possibly fit in f_files,
1456          * and that minus the number actually used in f_ffree.
1457          * For f_ffree, report the smaller of the number of object available
1458          * and the number of blocks (each object will take at least a block).
1459          */
1460         statp->f_ffree = MIN(availobjs, statp->f_bfree);
1461         statp->f_files = statp->f_ffree + usedobjs;
1462
1463         /*
1464          * We're a zfs filesystem.
1465          */
1466         strlcpy(statp->f_fstypename, "zfs",
1467             sizeof (statp->f_fstypename));
1468
1469         strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
1470             sizeof (statp->f_mntfromname));
1471         strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
1472             sizeof (statp->f_mntonname));
1473
1474         statp->f_namemax = MAXNAMELEN - 1;
1475
1476         ZFS_EXIT(zfsvfs);
1477         return (0);
1478 }
1479
1480 static int
1481 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
1482 {
1483         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1484         znode_t *rootzp;
1485         int error;
1486
1487         ZFS_ENTER(zfsvfs);
1488
1489         error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1490         if (error == 0)
1491                 *vpp = ZTOV(rootzp);
1492
1493         ZFS_EXIT(zfsvfs);
1494
1495         if (error == 0) {
1496                 error = vn_lock(*vpp, flags);
1497                 if (error != 0) {
1498                         VN_RELE(*vpp);
1499                         *vpp = NULL;
1500                 }
1501         }
1502         return (error);
1503 }
1504
1505 /*
1506  * Teardown the zfsvfs::z_os.
1507  *
1508  * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1509  * and 'z_teardown_inactive_lock' held.
1510  */
1511 static int
1512 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1513 {
1514         znode_t *zp;
1515         dsl_dir_t *dd;
1516
1517         /*
1518          * If someone has not already unmounted this file system,
1519          * drain the zrele_taskq to ensure all active references to the
1520          * zfsvfs_t have been handled only then can it be safely destroyed.
1521          */
1522         if (zfsvfs->z_os) {
1523                 /*
1524                  * If we're unmounting we have to wait for the list to
1525                  * drain completely.
1526                  *
1527                  * If we're not unmounting there's no guarantee the list
1528                  * will drain completely, but zreles run from the taskq
1529                  * may add the parents of dir-based xattrs to the taskq
1530                  * so we want to wait for these.
1531                  *
1532                  * We can safely read z_nr_znodes without locking because the
1533                  * VFS has already blocked operations which add to the
1534                  * z_all_znodes list and thus increment z_nr_znodes.
1535                  */
1536                 int round = 0;
1537                 while (zfsvfs->z_nr_znodes > 0) {
1538                         taskq_wait_outstanding(dsl_pool_zrele_taskq(
1539                             dmu_objset_pool(zfsvfs->z_os)), 0);
1540                         if (++round > 1 && !unmounting)
1541                                 break;
1542                 }
1543         }
1544         rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1545
1546         if (!unmounting) {
1547                 /*
1548                  * We purge the parent filesystem's vfsp as the parent
1549                  * filesystem and all of its snapshots have their vnode's
1550                  * v_vfsp set to the parent's filesystem's vfsp.  Note,
1551                  * 'z_parent' is self referential for non-snapshots.
1552                  */
1553 #ifdef FREEBSD_NAMECACHE
1554 #if __FreeBSD_version >= 1300117
1555                 cache_purgevfs(zfsvfs->z_parent->z_vfs);
1556 #else
1557                 cache_purgevfs(zfsvfs->z_parent->z_vfs, true);
1558 #endif
1559 #endif
1560         }
1561
1562         /*
1563          * Close the zil. NB: Can't close the zil while zfs_inactive
1564          * threads are blocked as zil_close can call zfs_inactive.
1565          */
1566         if (zfsvfs->z_log) {
1567                 zil_close(zfsvfs->z_log);
1568                 zfsvfs->z_log = NULL;
1569         }
1570
1571         ZFS_WLOCK_TEARDOWN_INACTIVE(zfsvfs);
1572
1573         /*
1574          * If we are not unmounting (ie: online recv) and someone already
1575          * unmounted this file system while we were doing the switcheroo,
1576          * or a reopen of z_os failed then just bail out now.
1577          */
1578         if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1579                 ZFS_WUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
1580                 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1581                 return (SET_ERROR(EIO));
1582         }
1583
1584         /*
1585          * At this point there are no vops active, and any new vops will
1586          * fail with EIO since we have z_teardown_lock for writer (only
1587          * relevant for forced unmount).
1588          *
1589          * Release all holds on dbufs.
1590          */
1591         mutex_enter(&zfsvfs->z_znodes_lock);
1592         for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1593             zp = list_next(&zfsvfs->z_all_znodes, zp))
1594                 if (zp->z_sa_hdl) {
1595                         ASSERT(ZTOV(zp)->v_count >= 0);
1596                         zfs_znode_dmu_fini(zp);
1597                 }
1598         mutex_exit(&zfsvfs->z_znodes_lock);
1599
1600         /*
1601          * If we are unmounting, set the unmounted flag and let new vops
1602          * unblock.  zfs_inactive will have the unmounted behavior, and all
1603          * other vops will fail with EIO.
1604          */
1605         if (unmounting) {
1606                 zfsvfs->z_unmounted = B_TRUE;
1607                 ZFS_WUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
1608                 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1609         }
1610
1611         /*
1612          * z_os will be NULL if there was an error in attempting to reopen
1613          * zfsvfs, so just return as the properties had already been
1614          * unregistered and cached data had been evicted before.
1615          */
1616         if (zfsvfs->z_os == NULL)
1617                 return (0);
1618
1619         /*
1620          * Unregister properties.
1621          */
1622         zfs_unregister_callbacks(zfsvfs);
1623
1624         /*
1625          * Evict cached data
1626          */
1627         if (!zfs_is_readonly(zfsvfs))
1628                 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1629         dmu_objset_evict_dbufs(zfsvfs->z_os);
1630         dd = zfsvfs->z_os->os_dsl_dataset->ds_dir;
1631         dsl_dir_cancel_waiters(dd);
1632
1633         return (0);
1634 }
1635
1636 /*ARGSUSED*/
1637 static int
1638 zfs_umount(vfs_t *vfsp, int fflag)
1639 {
1640         kthread_t *td = curthread;
1641         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1642         objset_t *os;
1643         cred_t *cr = td->td_ucred;
1644         int ret;
1645
1646         ret = secpolicy_fs_unmount(cr, vfsp);
1647         if (ret) {
1648                 if (dsl_deleg_access((char *)vfsp->vfs_resource,
1649                     ZFS_DELEG_PERM_MOUNT, cr))
1650                         return (ret);
1651         }
1652
1653         /*
1654          * Unmount any snapshots mounted under .zfs before unmounting the
1655          * dataset itself.
1656          */
1657         if (zfsvfs->z_ctldir != NULL) {
1658                 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
1659                         return (ret);
1660         }
1661
1662         if (fflag & MS_FORCE) {
1663                 /*
1664                  * Mark file system as unmounted before calling
1665                  * vflush(FORCECLOSE). This way we ensure no future vnops
1666                  * will be called and risk operating on DOOMED vnodes.
1667                  */
1668                 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1669                 zfsvfs->z_unmounted = B_TRUE;
1670                 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1671         }
1672
1673         /*
1674          * Flush all the files.
1675          */
1676         ret = vflush(vfsp, 0, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
1677         if (ret != 0)
1678                 return (ret);
1679         while (taskqueue_cancel(zfsvfs_taskq->tq_queue,
1680             &zfsvfs->z_unlinked_drain_task, NULL) != 0)
1681                 taskqueue_drain(zfsvfs_taskq->tq_queue,
1682                     &zfsvfs->z_unlinked_drain_task);
1683
1684         VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1685         os = zfsvfs->z_os;
1686
1687         /*
1688          * z_os will be NULL if there was an error in
1689          * attempting to reopen zfsvfs.
1690          */
1691         if (os != NULL) {
1692                 /*
1693                  * Unset the objset user_ptr.
1694                  */
1695                 mutex_enter(&os->os_user_ptr_lock);
1696                 dmu_objset_set_user(os, NULL);
1697                 mutex_exit(&os->os_user_ptr_lock);
1698
1699                 /*
1700                  * Finally release the objset
1701                  */
1702                 dmu_objset_disown(os, B_TRUE, zfsvfs);
1703         }
1704
1705         /*
1706          * We can now safely destroy the '.zfs' directory node.
1707          */
1708         if (zfsvfs->z_ctldir != NULL)
1709                 zfsctl_destroy(zfsvfs);
1710         zfs_freevfs(vfsp);
1711
1712         return (0);
1713 }
1714
1715 static int
1716 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
1717 {
1718         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
1719         znode_t         *zp;
1720         int             err;
1721
1722         /*
1723          * zfs_zget() can't operate on virtual entries like .zfs/ or
1724          * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP.
1725          * This will make NFS to switch to LOOKUP instead of using VGET.
1726          */
1727         if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR ||
1728             (zfsvfs->z_shares_dir != 0 && ino == zfsvfs->z_shares_dir))
1729                 return (EOPNOTSUPP);
1730
1731         ZFS_ENTER(zfsvfs);
1732         err = zfs_zget(zfsvfs, ino, &zp);
1733         if (err == 0 && zp->z_unlinked) {
1734                 vrele(ZTOV(zp));
1735                 err = EINVAL;
1736         }
1737         if (err == 0)
1738                 *vpp = ZTOV(zp);
1739         ZFS_EXIT(zfsvfs);
1740         if (err == 0) {
1741                 err = vn_lock(*vpp, flags);
1742                 if (err != 0)
1743                         vrele(*vpp);
1744         }
1745         if (err != 0)
1746                 *vpp = NULL;
1747         return (err);
1748 }
1749
1750 static int
1751 #if __FreeBSD_version >= 1300098
1752 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, uint64_t *extflagsp,
1753     struct ucred **credanonp, int *numsecflavors, int *secflavors)
1754 #else
1755 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
1756     struct ucred **credanonp, int *numsecflavors, int **secflavors)
1757 #endif
1758 {
1759         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1760
1761         /*
1762          * If this is regular file system vfsp is the same as
1763          * zfsvfs->z_parent->z_vfs, but if it is snapshot,
1764          * zfsvfs->z_parent->z_vfs represents parent file system
1765          * which we have to use here, because only this file system
1766          * has mnt_export configured.
1767          */
1768         return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
1769             credanonp, numsecflavors, secflavors));
1770 }
1771
1772 CTASSERT(SHORT_FID_LEN <= sizeof (struct fid));
1773 CTASSERT(LONG_FID_LEN <= sizeof (struct fid));
1774
1775 static int
1776 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp)
1777 {
1778         struct componentname cn;
1779         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
1780         znode_t         *zp;
1781         vnode_t         *dvp;
1782         uint64_t        object = 0;
1783         uint64_t        fid_gen = 0;
1784         uint64_t        gen_mask;
1785         uint64_t        zp_gen;
1786         int             i, err;
1787
1788         *vpp = NULL;
1789
1790         ZFS_ENTER(zfsvfs);
1791
1792         /*
1793          * On FreeBSD we can get snapshot's mount point or its parent file
1794          * system mount point depending if snapshot is already mounted or not.
1795          */
1796         if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
1797                 zfid_long_t     *zlfid = (zfid_long_t *)fidp;
1798                 uint64_t        objsetid = 0;
1799                 uint64_t        setgen = 0;
1800
1801                 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1802                         objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1803
1804                 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1805                         setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1806
1807                 ZFS_EXIT(zfsvfs);
1808
1809                 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1810                 if (err)
1811                         return (SET_ERROR(EINVAL));
1812                 ZFS_ENTER(zfsvfs);
1813         }
1814
1815         if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1816                 zfid_short_t    *zfid = (zfid_short_t *)fidp;
1817
1818                 for (i = 0; i < sizeof (zfid->zf_object); i++)
1819                         object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1820
1821                 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1822                         fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1823         } else {
1824                 ZFS_EXIT(zfsvfs);
1825                 return (SET_ERROR(EINVAL));
1826         }
1827
1828         /*
1829          * A zero fid_gen means we are in .zfs or the .zfs/snapshot
1830          * directory tree. If the object == zfsvfs->z_shares_dir, then
1831          * we are in the .zfs/shares directory tree.
1832          */
1833         if ((fid_gen == 0 &&
1834             (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) ||
1835             (zfsvfs->z_shares_dir != 0 && object == zfsvfs->z_shares_dir)) {
1836                 ZFS_EXIT(zfsvfs);
1837                 VERIFY0(zfsctl_root(zfsvfs, LK_SHARED, &dvp));
1838                 if (object == ZFSCTL_INO_SNAPDIR) {
1839                         cn.cn_nameptr = "snapshot";
1840                         cn.cn_namelen = strlen(cn.cn_nameptr);
1841                         cn.cn_nameiop = LOOKUP;
1842                         cn.cn_flags = ISLASTCN | LOCKLEAF;
1843                         cn.cn_lkflags = flags;
1844                         VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
1845                         vput(dvp);
1846                 } else if (object == zfsvfs->z_shares_dir) {
1847                         /*
1848                          * XXX This branch must not be taken,
1849                          * if it is, then the lookup below will
1850                          * explode.
1851                          */
1852                         cn.cn_nameptr = "shares";
1853                         cn.cn_namelen = strlen(cn.cn_nameptr);
1854                         cn.cn_nameiop = LOOKUP;
1855                         cn.cn_flags = ISLASTCN;
1856                         cn.cn_lkflags = flags;
1857                         VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
1858                         vput(dvp);
1859                 } else {
1860                         *vpp = dvp;
1861                 }
1862                 return (err);
1863         }
1864
1865         gen_mask = -1ULL >> (64 - 8 * i);
1866
1867         dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1868         if ((err = zfs_zget(zfsvfs, object, &zp))) {
1869                 ZFS_EXIT(zfsvfs);
1870                 return (err);
1871         }
1872         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1873             sizeof (uint64_t));
1874         zp_gen = zp_gen & gen_mask;
1875         if (zp_gen == 0)
1876                 zp_gen = 1;
1877         if (zp->z_unlinked || zp_gen != fid_gen) {
1878                 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1879                 vrele(ZTOV(zp));
1880                 ZFS_EXIT(zfsvfs);
1881                 return (SET_ERROR(EINVAL));
1882         }
1883
1884         *vpp = ZTOV(zp);
1885         ZFS_EXIT(zfsvfs);
1886         err = vn_lock(*vpp, flags);
1887         if (err == 0)
1888                 vnode_create_vobject(*vpp, zp->z_size, curthread);
1889         else
1890                 *vpp = NULL;
1891         return (err);
1892 }
1893
1894 /*
1895  * Block out VOPs and close zfsvfs_t::z_os
1896  *
1897  * Note, if successful, then we return with the 'z_teardown_lock' and
1898  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
1899  * dataset and objset intact so that they can be atomically handed off during
1900  * a subsequent rollback or recv operation and the resume thereafter.
1901  */
1902 int
1903 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1904 {
1905         int error;
1906
1907         if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1908                 return (error);
1909
1910         return (0);
1911 }
1912
1913 /*
1914  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
1915  * is an invariant across any of the operations that can be performed while the
1916  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
1917  * are the same: the relevant objset and associated dataset are owned by
1918  * zfsvfs, held, and long held on entry.
1919  */
1920 int
1921 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
1922 {
1923         int err;
1924         znode_t *zp;
1925
1926         ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
1927         ASSERT(ZFS_TEARDOWN_INACTIVE_WLOCKED(zfsvfs));
1928
1929         /*
1930          * We already own this, so just update the objset_t, as the one we
1931          * had before may have been evicted.
1932          */
1933         objset_t *os;
1934         VERIFY3P(ds->ds_owner, ==, zfsvfs);
1935         VERIFY(dsl_dataset_long_held(ds));
1936         dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
1937         dsl_pool_config_enter(dp, FTAG);
1938         VERIFY0(dmu_objset_from_ds(ds, &os));
1939         dsl_pool_config_exit(dp, FTAG);
1940
1941         err = zfsvfs_init(zfsvfs, os);
1942         if (err != 0)
1943                 goto bail;
1944
1945         ds->ds_dir->dd_activity_cancelled = B_FALSE;
1946         VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1947
1948         zfs_set_fuid_feature(zfsvfs);
1949
1950         /*
1951          * Attempt to re-establish all the active znodes with
1952          * their dbufs.  If a zfs_rezget() fails, then we'll let
1953          * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1954          * when they try to use their znode.
1955          */
1956         mutex_enter(&zfsvfs->z_znodes_lock);
1957         for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1958             zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1959                 (void) zfs_rezget(zp);
1960         }
1961         mutex_exit(&zfsvfs->z_znodes_lock);
1962
1963 bail:
1964         /* release the VOPs */
1965         ZFS_WUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
1966         rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1967
1968         if (err) {
1969                 /*
1970                  * Since we couldn't setup the sa framework, try to force
1971                  * unmount this file system.
1972                  */
1973                 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) {
1974                         vfs_ref(zfsvfs->z_vfs);
1975                         (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
1976                 }
1977         }
1978         return (err);
1979 }
1980
1981 static void
1982 zfs_freevfs(vfs_t *vfsp)
1983 {
1984         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1985
1986         zfsvfs_free(zfsvfs);
1987
1988         atomic_dec_32(&zfs_active_fs_count);
1989 }
1990
1991 #ifdef __i386__
1992 static int desiredvnodes_backup;
1993 #include <sys/vmmeter.h>
1994
1995
1996 #include <vm/vm_page.h>
1997 #include <vm/vm_object.h>
1998 #include <vm/vm_kern.h>
1999 #include <vm/vm_map.h>
2000 #endif
2001
2002 static void
2003 zfs_vnodes_adjust(void)
2004 {
2005 #ifdef __i386__
2006         int newdesiredvnodes;
2007
2008         desiredvnodes_backup = desiredvnodes;
2009
2010         /*
2011          * We calculate newdesiredvnodes the same way it is done in
2012          * vntblinit(). If it is equal to desiredvnodes, it means that
2013          * it wasn't tuned by the administrator and we can tune it down.
2014          */
2015         newdesiredvnodes = min(maxproc + vm_cnt.v_page_count / 4, 2 *
2016             vm_kmem_size / (5 * (sizeof (struct vm_object) +
2017             sizeof (struct vnode))));
2018         if (newdesiredvnodes == desiredvnodes)
2019                 desiredvnodes = (3 * newdesiredvnodes) / 4;
2020 #endif
2021 }
2022
2023 static void
2024 zfs_vnodes_adjust_back(void)
2025 {
2026
2027 #ifdef __i386__
2028         desiredvnodes = desiredvnodes_backup;
2029 #endif
2030 }
2031
2032 void
2033 zfs_init(void)
2034 {
2035
2036         printf("ZFS filesystem version: " ZPL_VERSION_STRING "\n");
2037
2038         /*
2039          * Initialize .zfs directory structures
2040          */
2041         zfsctl_init();
2042
2043         /*
2044          * Initialize znode cache, vnode ops, etc...
2045          */
2046         zfs_znode_init();
2047
2048         /*
2049          * Reduce number of vnodes. Originally number of vnodes is calculated
2050          * with UFS inode in mind. We reduce it here, because it's too big for
2051          * ZFS/i386.
2052          */
2053         zfs_vnodes_adjust();
2054
2055         dmu_objset_register_type(DMU_OST_ZFS, zpl_get_file_info);
2056
2057         zfsvfs_taskq = taskq_create("zfsvfs", 1, minclsyspri, 0, 0, 0);
2058 }
2059
2060 void
2061 zfs_fini(void)
2062 {
2063         taskq_destroy(zfsvfs_taskq);
2064         zfsctl_fini();
2065         zfs_znode_fini();
2066         zfs_vnodes_adjust_back();
2067 }
2068
2069 int
2070 zfs_busy(void)
2071 {
2072         return (zfs_active_fs_count != 0);
2073 }
2074
2075 /*
2076  * Release VOPs and unmount a suspended filesystem.
2077  */
2078 int
2079 zfs_end_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2080 {
2081         ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2082         ASSERT(ZFS_TEARDOWN_INACTIVE_WLOCKED(zfsvfs));
2083
2084         /*
2085          * We already own this, so just hold and rele it to update the
2086          * objset_t, as the one we had before may have been evicted.
2087          */
2088         objset_t *os;
2089         VERIFY3P(ds->ds_owner, ==, zfsvfs);
2090         VERIFY(dsl_dataset_long_held(ds));
2091         dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds));
2092         dsl_pool_config_enter(dp, FTAG);
2093         VERIFY0(dmu_objset_from_ds(ds, &os));
2094         dsl_pool_config_exit(dp, FTAG);
2095         zfsvfs->z_os = os;
2096
2097         /* release the VOPs */
2098         ZFS_WUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
2099         rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2100
2101         /*
2102          * Try to force unmount this file system.
2103          */
2104         (void) zfs_umount(zfsvfs->z_vfs, 0);
2105         zfsvfs->z_unmounted = B_TRUE;
2106         return (0);
2107 }
2108
2109 int
2110 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2111 {
2112         int error;
2113         objset_t *os = zfsvfs->z_os;
2114         dmu_tx_t *tx;
2115
2116         if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2117                 return (SET_ERROR(EINVAL));
2118
2119         if (newvers < zfsvfs->z_version)
2120                 return (SET_ERROR(EINVAL));
2121
2122         if (zfs_spa_version_map(newvers) >
2123             spa_version(dmu_objset_spa(zfsvfs->z_os)))
2124                 return (SET_ERROR(ENOTSUP));
2125
2126         tx = dmu_tx_create(os);
2127         dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2128         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2129                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2130                     ZFS_SA_ATTRS);
2131                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2132         }
2133         error = dmu_tx_assign(tx, TXG_WAIT);
2134         if (error) {
2135                 dmu_tx_abort(tx);
2136                 return (error);
2137         }
2138
2139         error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2140             8, 1, &newvers, tx);
2141
2142         if (error) {
2143                 dmu_tx_commit(tx);
2144                 return (error);
2145         }
2146
2147         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2148                 uint64_t sa_obj;
2149
2150                 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2151                     SPA_VERSION_SA);
2152                 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2153                     DMU_OT_NONE, 0, tx);
2154
2155                 error = zap_add(os, MASTER_NODE_OBJ,
2156                     ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2157                 ASSERT0(error);
2158
2159                 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2160                 sa_register_update_callback(os, zfs_sa_upgrade);
2161         }
2162
2163         spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2164             "from %ju to %ju", (uintmax_t)zfsvfs->z_version,
2165             (uintmax_t)newvers);
2166         dmu_tx_commit(tx);
2167
2168         zfsvfs->z_version = newvers;
2169         os->os_version = newvers;
2170
2171         zfs_set_fuid_feature(zfsvfs);
2172
2173         return (0);
2174 }
2175
2176 /*
2177  * Read a property stored within the master node.
2178  */
2179 int
2180 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2181 {
2182         uint64_t *cached_copy = NULL;
2183
2184         /*
2185          * Figure out where in the objset_t the cached copy would live, if it
2186          * is available for the requested property.
2187          */
2188         if (os != NULL) {
2189                 switch (prop) {
2190                 case ZFS_PROP_VERSION:
2191                         cached_copy = &os->os_version;
2192                         break;
2193                 case ZFS_PROP_NORMALIZE:
2194                         cached_copy = &os->os_normalization;
2195                         break;
2196                 case ZFS_PROP_UTF8ONLY:
2197                         cached_copy = &os->os_utf8only;
2198                         break;
2199                 case ZFS_PROP_CASE:
2200                         cached_copy = &os->os_casesensitivity;
2201                         break;
2202                 default:
2203                         break;
2204                 }
2205         }
2206         if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2207                 *value = *cached_copy;
2208                 return (0);
2209         }
2210
2211         /*
2212          * If the property wasn't cached, look up the file system's value for
2213          * the property. For the version property, we look up a slightly
2214          * different string.
2215          */
2216         const char *pname;
2217         int error = ENOENT;
2218         if (prop == ZFS_PROP_VERSION) {
2219                 pname = ZPL_VERSION_STR;
2220         } else {
2221                 pname = zfs_prop_to_name(prop);
2222         }
2223
2224         if (os != NULL) {
2225                 ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2226                 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2227         }
2228
2229         if (error == ENOENT) {
2230                 /* No value set, use the default value */
2231                 switch (prop) {
2232                 case ZFS_PROP_VERSION:
2233                         *value = ZPL_VERSION;
2234                         break;
2235                 case ZFS_PROP_NORMALIZE:
2236                 case ZFS_PROP_UTF8ONLY:
2237                         *value = 0;
2238                         break;
2239                 case ZFS_PROP_CASE:
2240                         *value = ZFS_CASE_SENSITIVE;
2241                         break;
2242                 case ZFS_PROP_ACLTYPE:
2243                         *value = ZFS_ACLTYPE_NFSV4;
2244                         break;
2245                 default:
2246                         return (error);
2247                 }
2248                 error = 0;
2249         }
2250
2251         /*
2252          * If one of the methods for getting the property value above worked,
2253          * copy it into the objset_t's cache.
2254          */
2255         if (error == 0 && cached_copy != NULL) {
2256                 *cached_copy = *value;
2257         }
2258
2259         return (error);
2260 }
2261
2262 /*
2263  * Return true if the corresponding vfs's unmounted flag is set.
2264  * Otherwise return false.
2265  * If this function returns true we know VFS unmount has been initiated.
2266  */
2267 boolean_t
2268 zfs_get_vfs_flag_unmounted(objset_t *os)
2269 {
2270         zfsvfs_t *zfvp;
2271         boolean_t unmounted = B_FALSE;
2272
2273         ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2274
2275         mutex_enter(&os->os_user_ptr_lock);
2276         zfvp = dmu_objset_get_user(os);
2277         if (zfvp != NULL && zfvp->z_vfs != NULL &&
2278             (zfvp->z_vfs->mnt_kern_flag & MNTK_UNMOUNT))
2279                 unmounted = B_TRUE;
2280         mutex_exit(&os->os_user_ptr_lock);
2281
2282         return (unmounted);
2283 }
2284
2285 #ifdef _KERNEL
2286 void
2287 zfsvfs_update_fromname(const char *oldname, const char *newname)
2288 {
2289         char tmpbuf[MAXPATHLEN];
2290         struct mount *mp;
2291         char *fromname;
2292         size_t oldlen;
2293
2294         oldlen = strlen(oldname);
2295
2296         mtx_lock(&mountlist_mtx);
2297         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2298                 fromname = mp->mnt_stat.f_mntfromname;
2299                 if (strcmp(fromname, oldname) == 0) {
2300                         (void) strlcpy(fromname, newname,
2301                             sizeof (mp->mnt_stat.f_mntfromname));
2302                         continue;
2303                 }
2304                 if (strncmp(fromname, oldname, oldlen) == 0 &&
2305                     (fromname[oldlen] == '/' || fromname[oldlen] == '@')) {
2306                         (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s%s",
2307                             newname, fromname + oldlen);
2308                         (void) strlcpy(fromname, tmpbuf,
2309                             sizeof (mp->mnt_stat.f_mntfromname));
2310                         continue;
2311                 }
2312         }
2313         mtx_unlock(&mountlist_mtx);
2314 }
2315 #endif