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