Remove unused mount functions
[zfs.git] / module / 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/sysmacros.h>
31 #include <sys/kmem.h>
32 #include <sys/pathname.h>
33 #include <sys/vnode.h>
34 #include <sys/vfs.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/mntent.h>
37 #include <sys/mount.h>
38 #include <sys/cmn_err.h>
39 #include "fs/fs_subr.h"
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_dir.h>
42 #include <sys/zil.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/dmu.h>
45 #include <sys/dsl_prop.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_deleg.h>
48 #include <sys/spa.h>
49 #include <sys/zap.h>
50 #include <sys/sa.h>
51 #include <sys/varargs.h>
52 #include <sys/policy.h>
53 #include <sys/atomic.h>
54 #include <sys/mkdev.h>
55 #include <sys/modctl.h>
56 #include <sys/refstr.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/zfs_ctldir.h>
59 #include <sys/zfs_fuid.h>
60 #include <sys/bootconf.h>
61 #include <sys/sunddi.h>
62 #include <sys/dnlc.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/spa_boot.h>
65 #include <sys/sa.h>
66 #include "zfs_comutil.h"
67
68 #ifdef HAVE_ZPL
69 extern int sys_shutdown;
70
71 /*
72  * We need to keep a count of active fs's.
73  * This is necessary to prevent our module
74  * from being unloaded after a umount -f
75  */
76 static uint32_t zfs_active_fs_count = 0;
77
78 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
79 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
80 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
81 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
82
83 /*
84  * MO_DEFAULT is not used since the default value is determined
85  * by the equivalent property.
86  */
87 static mntopt_t mntopts[] = {
88         { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
89         { MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
90         { MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
91         { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
92 };
93
94 static mntopts_t zfs_mntopts = {
95         sizeof (mntopts) / sizeof (mntopt_t),
96         mntopts
97 };
98
99 /*ARGSUSED*/
100 int
101 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
102 {
103         /*
104          * Data integrity is job one.  We don't want a compromised kernel
105          * writing to the storage pool, so we never sync during panic.
106          */
107         if (panicstr)
108                 return (0);
109
110         /*
111          * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
112          * to sync metadata, which they would otherwise cache indefinitely.
113          * Semantically, the only requirement is that the sync be initiated.
114          * The DMU syncs out txgs frequently, so there's nothing to do.
115          */
116         if (flag & SYNC_ATTR)
117                 return (0);
118
119         if (vfsp != NULL) {
120                 /*
121                  * Sync a specific filesystem.
122                  */
123                 zfsvfs_t *zfsvfs = vfsp->vfs_data;
124                 dsl_pool_t *dp;
125
126                 ZFS_ENTER(zfsvfs);
127                 dp = dmu_objset_pool(zfsvfs->z_os);
128
129                 /*
130                  * If the system is shutting down, then skip any
131                  * filesystems which may exist on a suspended pool.
132                  */
133                 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
134                         ZFS_EXIT(zfsvfs);
135                         return (0);
136                 }
137
138                 if (zfsvfs->z_log != NULL)
139                         zil_commit(zfsvfs->z_log, 0);
140
141                 ZFS_EXIT(zfsvfs);
142         } else {
143                 /*
144                  * Sync all ZFS filesystems.  This is what happens when you
145                  * run sync(1M).  Unlike other filesystems, ZFS honors the
146                  * request by waiting for all pools to commit all dirty data.
147                  */
148                 spa_sync_allpools();
149         }
150
151         return (0);
152 }
153 EXPORT_SYMBOL(zfs_sync);
154
155 static void
156 atime_changed_cb(void *arg, uint64_t newval)
157 {
158         zfsvfs_t *zfsvfs = arg;
159
160         if (newval == TRUE) {
161                 zfsvfs->z_atime = TRUE;
162                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
163                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
164         } else {
165                 zfsvfs->z_atime = FALSE;
166                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
167                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
168         }
169 }
170
171 static void
172 xattr_changed_cb(void *arg, uint64_t newval)
173 {
174         zfsvfs_t *zfsvfs = arg;
175
176         if (newval == TRUE) {
177                 /* XXX locking on vfs_flag? */
178                 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
179                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
180                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
181         } else {
182                 /* XXX locking on vfs_flag? */
183                 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
184                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
185                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
186         }
187 }
188
189 static void
190 blksz_changed_cb(void *arg, uint64_t newval)
191 {
192         zfsvfs_t *zfsvfs = arg;
193
194         if (newval < SPA_MINBLOCKSIZE ||
195             newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
196                 newval = SPA_MAXBLOCKSIZE;
197
198         zfsvfs->z_max_blksz = newval;
199         zfsvfs->z_vfs->vfs_bsize = newval;
200 }
201
202 static void
203 readonly_changed_cb(void *arg, uint64_t newval)
204 {
205         zfsvfs_t *zfsvfs = arg;
206
207         if (newval) {
208                 /* XXX locking on vfs_flag? */
209                 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
210                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
211                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
212         } else {
213                 /* XXX locking on vfs_flag? */
214                 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
215                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
216                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
217         }
218 }
219
220 static void
221 devices_changed_cb(void *arg, uint64_t newval)
222 {
223         zfsvfs_t *zfsvfs = arg;
224
225         if (newval == FALSE) {
226                 zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
227                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
228                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
229         } else {
230                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
231                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
232                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
233         }
234 }
235
236 static void
237 setuid_changed_cb(void *arg, uint64_t newval)
238 {
239         zfsvfs_t *zfsvfs = arg;
240
241         if (newval == FALSE) {
242                 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
243                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
244                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
245         } else {
246                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
247                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
248                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
249         }
250 }
251
252 static void
253 exec_changed_cb(void *arg, uint64_t newval)
254 {
255         zfsvfs_t *zfsvfs = arg;
256
257         if (newval == FALSE) {
258                 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
259                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
260                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
261         } else {
262                 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
263                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
264                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
265         }
266 }
267
268 /*
269  * The nbmand mount option can be changed at mount time.
270  * We can't allow it to be toggled on live file systems or incorrect
271  * behavior may be seen from cifs clients
272  *
273  * This property isn't registered via dsl_prop_register(), but this callback
274  * will be called when a file system is first mounted
275  */
276 static void
277 nbmand_changed_cb(void *arg, uint64_t newval)
278 {
279         zfsvfs_t *zfsvfs = arg;
280         if (newval == FALSE) {
281                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
282                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
283         } else {
284                 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
285                 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
286         }
287 }
288
289 static void
290 snapdir_changed_cb(void *arg, uint64_t newval)
291 {
292         zfsvfs_t *zfsvfs = arg;
293
294         zfsvfs->z_show_ctldir = newval;
295 }
296
297 static void
298 vscan_changed_cb(void *arg, uint64_t newval)
299 {
300         zfsvfs_t *zfsvfs = arg;
301
302         zfsvfs->z_vscan = newval;
303 }
304
305 static void
306 acl_inherit_changed_cb(void *arg, uint64_t newval)
307 {
308         zfsvfs_t *zfsvfs = arg;
309
310         zfsvfs->z_acl_inherit = newval;
311 }
312
313 int
314 zfs_register_callbacks(vfs_t *vfsp)
315 {
316         struct dsl_dataset *ds = NULL;
317         objset_t *os = NULL;
318         zfsvfs_t *zfsvfs = NULL;
319         uint64_t nbmand;
320         int readonly, do_readonly = B_FALSE;
321         int setuid, do_setuid = B_FALSE;
322         int exec, do_exec = B_FALSE;
323         int devices, do_devices = B_FALSE;
324         int xattr, do_xattr = B_FALSE;
325         int atime, do_atime = B_FALSE;
326         int error = 0;
327
328         ASSERT(vfsp);
329         zfsvfs = vfsp->vfs_data;
330         ASSERT(zfsvfs);
331         os = zfsvfs->z_os;
332
333         /*
334          * The act of registering our callbacks will destroy any mount
335          * options we may have.  In order to enable temporary overrides
336          * of mount options, we stash away the current values and
337          * restore them after we register the callbacks.
338          */
339         if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
340             !spa_writeable(dmu_objset_spa(os))) {
341                 readonly = B_TRUE;
342                 do_readonly = B_TRUE;
343         } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
344                 readonly = B_FALSE;
345                 do_readonly = B_TRUE;
346         }
347         if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
348                 devices = B_FALSE;
349                 setuid = B_FALSE;
350                 do_devices = B_TRUE;
351                 do_setuid = B_TRUE;
352         } else {
353                 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
354                         devices = B_FALSE;
355                         do_devices = B_TRUE;
356                 } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
357                         devices = B_TRUE;
358                         do_devices = B_TRUE;
359                 }
360
361                 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
362                         setuid = B_FALSE;
363                         do_setuid = B_TRUE;
364                 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
365                         setuid = B_TRUE;
366                         do_setuid = B_TRUE;
367                 }
368         }
369         if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
370                 exec = B_FALSE;
371                 do_exec = B_TRUE;
372         } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
373                 exec = B_TRUE;
374                 do_exec = B_TRUE;
375         }
376         if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
377                 xattr = B_FALSE;
378                 do_xattr = B_TRUE;
379         } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
380                 xattr = B_TRUE;
381                 do_xattr = B_TRUE;
382         }
383         if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
384                 atime = B_FALSE;
385                 do_atime = B_TRUE;
386         } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
387                 atime = B_TRUE;
388                 do_atime = B_TRUE;
389         }
390
391         /*
392          * nbmand is a special property.  It can only be changed at
393          * mount time.
394          *
395          * This is weird, but it is documented to only be changeable
396          * at mount time.
397          */
398         if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
399                 nbmand = B_FALSE;
400         } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
401                 nbmand = B_TRUE;
402         } else {
403                 char osname[MAXNAMELEN];
404
405                 dmu_objset_name(os, osname);
406                 if ((error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
407                     NULL))) {
408                         return (error);
409                 }
410         }
411
412         /*
413          * Register property callbacks.
414          *
415          * It would probably be fine to just check for i/o error from
416          * the first prop_register(), but I guess I like to go
417          * overboard...
418          */
419         ds = dmu_objset_ds(os);
420         error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
421         error = error ? error : dsl_prop_register(ds,
422             "xattr", xattr_changed_cb, zfsvfs);
423         error = error ? error : dsl_prop_register(ds,
424             "recordsize", blksz_changed_cb, zfsvfs);
425         error = error ? error : dsl_prop_register(ds,
426             "readonly", readonly_changed_cb, zfsvfs);
427         error = error ? error : dsl_prop_register(ds,
428             "devices", devices_changed_cb, zfsvfs);
429         error = error ? error : dsl_prop_register(ds,
430             "setuid", setuid_changed_cb, zfsvfs);
431         error = error ? error : dsl_prop_register(ds,
432             "exec", exec_changed_cb, zfsvfs);
433         error = error ? error : dsl_prop_register(ds,
434             "snapdir", snapdir_changed_cb, zfsvfs);
435         error = error ? error : dsl_prop_register(ds,
436             "aclinherit", acl_inherit_changed_cb, zfsvfs);
437         error = error ? error : dsl_prop_register(ds,
438             "vscan", vscan_changed_cb, zfsvfs);
439         if (error)
440                 goto unregister;
441
442         /*
443          * Invoke our callbacks to restore temporary mount options.
444          */
445         if (do_readonly)
446                 readonly_changed_cb(zfsvfs, readonly);
447         if (do_setuid)
448                 setuid_changed_cb(zfsvfs, setuid);
449         if (do_exec)
450                 exec_changed_cb(zfsvfs, exec);
451         if (do_devices)
452                 devices_changed_cb(zfsvfs, devices);
453         if (do_xattr)
454                 xattr_changed_cb(zfsvfs, xattr);
455         if (do_atime)
456                 atime_changed_cb(zfsvfs, atime);
457
458         nbmand_changed_cb(zfsvfs, nbmand);
459
460         return (0);
461
462 unregister:
463         /*
464          * We may attempt to unregister some callbacks that are not
465          * registered, but this is OK; it will simply return ENOMSG,
466          * which we will ignore.
467          */
468         (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
469         (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
470         (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
471         (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
472         (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
473         (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
474         (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
475         (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
476         (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
477             zfsvfs);
478         (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
479         return (error);
480
481 }
482 EXPORT_SYMBOL(zfs_register_callbacks);
483 #endif /* HAVE_ZPL */
484
485 static int
486 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
487     uint64_t *userp, uint64_t *groupp)
488 {
489         znode_phys_t *znp = data;
490         int error = 0;
491
492         /*
493          * Is it a valid type of object to track?
494          */
495         if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
496                 return (ENOENT);
497
498         /*
499          * If we have a NULL data pointer
500          * then assume the id's aren't changing and
501          * return EEXIST to the dmu to let it know to
502          * use the same ids
503          */
504         if (data == NULL)
505                 return (EEXIST);
506
507         if (bonustype == DMU_OT_ZNODE) {
508                 *userp = znp->zp_uid;
509                 *groupp = znp->zp_gid;
510         } else {
511                 int hdrsize;
512
513                 ASSERT(bonustype == DMU_OT_SA);
514                 hdrsize = sa_hdrsize(data);
515
516                 if (hdrsize != 0) {
517                         *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
518                             SA_UID_OFFSET));
519                         *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
520                             SA_GID_OFFSET));
521                 } else {
522                         /*
523                          * This should only happen for newly created
524                          * files that haven't had the znode data filled
525                          * in yet.
526                          */
527                         *userp = 0;
528                         *groupp = 0;
529                 }
530         }
531         return (error);
532 }
533
534 #ifdef HAVE_ZPL
535 static void
536 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
537     char *domainbuf, int buflen, uid_t *ridp)
538 {
539         uint64_t fuid;
540         const char *domain;
541
542         fuid = strtonum(fuidstr, NULL);
543
544         domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
545         if (domain)
546                 (void) strlcpy(domainbuf, domain, buflen);
547         else
548                 domainbuf[0] = '\0';
549         *ridp = FUID_RID(fuid);
550 }
551
552 static uint64_t
553 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
554 {
555         switch (type) {
556         case ZFS_PROP_USERUSED:
557                 return (DMU_USERUSED_OBJECT);
558         case ZFS_PROP_GROUPUSED:
559                 return (DMU_GROUPUSED_OBJECT);
560         case ZFS_PROP_USERQUOTA:
561                 return (zfsvfs->z_userquota_obj);
562         case ZFS_PROP_GROUPQUOTA:
563                 return (zfsvfs->z_groupquota_obj);
564         default:
565                 return (ENOTSUP);
566         }
567         return (0);
568 }
569
570 int
571 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
572     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
573 {
574         int error;
575         zap_cursor_t zc;
576         zap_attribute_t za;
577         zfs_useracct_t *buf = vbuf;
578         uint64_t obj;
579
580         if (!dmu_objset_userspace_present(zfsvfs->z_os))
581                 return (ENOTSUP);
582
583         obj = zfs_userquota_prop_to_obj(zfsvfs, type);
584         if (obj == 0) {
585                 *bufsizep = 0;
586                 return (0);
587         }
588
589         for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
590             (error = zap_cursor_retrieve(&zc, &za)) == 0;
591             zap_cursor_advance(&zc)) {
592                 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
593                     *bufsizep)
594                         break;
595
596                 fuidstr_to_sid(zfsvfs, za.za_name,
597                     buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
598
599                 buf->zu_space = za.za_first_integer;
600                 buf++;
601         }
602         if (error == ENOENT)
603                 error = 0;
604
605         ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
606         *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
607         *cookiep = zap_cursor_serialize(&zc);
608         zap_cursor_fini(&zc);
609         return (error);
610 }
611 EXPORT_SYMBOL(zfs_userspace_many);
612
613 /*
614  * buf must be big enough (eg, 32 bytes)
615  */
616 static int
617 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
618     char *buf, boolean_t addok)
619 {
620         uint64_t fuid;
621         int domainid = 0;
622
623         if (domain && domain[0]) {
624                 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
625                 if (domainid == -1)
626                         return (ENOENT);
627         }
628         fuid = FUID_ENCODE(domainid, rid);
629         (void) sprintf(buf, "%llx", (longlong_t)fuid);
630         return (0);
631 }
632
633 int
634 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
635     const char *domain, uint64_t rid, uint64_t *valp)
636 {
637         char buf[32];
638         int err;
639         uint64_t obj;
640
641         *valp = 0;
642
643         if (!dmu_objset_userspace_present(zfsvfs->z_os))
644                 return (ENOTSUP);
645
646         obj = zfs_userquota_prop_to_obj(zfsvfs, type);
647         if (obj == 0)
648                 return (0);
649
650         err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
651         if (err)
652                 return (err);
653
654         err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
655         if (err == ENOENT)
656                 err = 0;
657         return (err);
658 }
659 EXPORT_SYMBOL(zfs_userspace_one);
660
661 int
662 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
663     const char *domain, uint64_t rid, uint64_t quota)
664 {
665         char buf[32];
666         int err;
667         dmu_tx_t *tx;
668         uint64_t *objp;
669         boolean_t fuid_dirtied;
670
671         if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
672                 return (EINVAL);
673
674         if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
675                 return (ENOTSUP);
676
677         objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
678             &zfsvfs->z_groupquota_obj;
679
680         err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
681         if (err)
682                 return (err);
683         fuid_dirtied = zfsvfs->z_fuid_dirty;
684
685         tx = dmu_tx_create(zfsvfs->z_os);
686         dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
687         if (*objp == 0) {
688                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
689                     zfs_userquota_prop_prefixes[type]);
690         }
691         if (fuid_dirtied)
692                 zfs_fuid_txhold(zfsvfs, tx);
693         err = dmu_tx_assign(tx, TXG_WAIT);
694         if (err) {
695                 dmu_tx_abort(tx);
696                 return (err);
697         }
698
699         mutex_enter(&zfsvfs->z_lock);
700         if (*objp == 0) {
701                 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
702                     DMU_OT_NONE, 0, tx);
703                 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
704                     zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
705         }
706         mutex_exit(&zfsvfs->z_lock);
707
708         if (quota == 0) {
709                 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
710                 if (err == ENOENT)
711                         err = 0;
712         } else {
713                 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
714         }
715         ASSERT(err == 0);
716         if (fuid_dirtied)
717                 zfs_fuid_sync(zfsvfs, tx);
718         dmu_tx_commit(tx);
719         return (err);
720 }
721 EXPORT_SYMBOL(zfs_set_userquota);
722
723 boolean_t
724 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
725 {
726         char buf[32];
727         uint64_t used, quota, usedobj, quotaobj;
728         int err;
729
730         usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
731         quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
732
733         if (quotaobj == 0 || zfsvfs->z_replay)
734                 return (B_FALSE);
735
736         (void) sprintf(buf, "%llx", (longlong_t)fuid);
737         err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
738         if (err != 0)
739                 return (B_FALSE);
740
741         err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
742         if (err != 0)
743                 return (B_FALSE);
744         return (used >= quota);
745 }
746 EXPORT_SYMBOL(zfs_fuid_overquota);
747
748 boolean_t
749 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
750 {
751         uint64_t fuid;
752         uint64_t quotaobj;
753
754         quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
755
756         fuid = isgroup ? zp->z_gid : zp->z_uid;
757
758         if (quotaobj == 0 || zfsvfs->z_replay)
759                 return (B_FALSE);
760
761         return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
762 }
763 EXPORT_SYMBOL(zfs_owner_overquota);
764
765 int
766 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
767 {
768         objset_t *os;
769         zfsvfs_t *zfsvfs;
770         uint64_t zval;
771         int i, error;
772         uint64_t sa_obj;
773
774         zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
775
776         /*
777          * We claim to always be readonly so we can open snapshots;
778          * other ZPL code will prevent us from writing to snapshots.
779          */
780         error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
781         if (error) {
782                 kmem_free(zfsvfs, sizeof (zfsvfs_t));
783                 return (error);
784         }
785
786         /*
787          * Initialize the zfs-specific filesystem structure.
788          * Should probably make this a kmem cache, shuffle fields,
789          * and just bzero up to z_hold_mtx[].
790          */
791         zfsvfs->z_vfs = NULL;
792         zfsvfs->z_parent = zfsvfs;
793         zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
794         zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
795         zfsvfs->z_os = os;
796
797         error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
798         if (error) {
799                 goto out;
800         } else if (zfsvfs->z_version >
801             zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
802                 (void) printk("Can't mount a version %lld file system "
803                     "on a version %lld pool\n. Pool must be upgraded to mount "
804                     "this file system.", (u_longlong_t)zfsvfs->z_version,
805                     (u_longlong_t)spa_version(dmu_objset_spa(os)));
806                 error = ENOTSUP;
807                 goto out;
808         }
809         if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
810                 goto out;
811         zfsvfs->z_norm = (int)zval;
812
813         if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
814                 goto out;
815         zfsvfs->z_utf8 = (zval != 0);
816
817         if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
818                 goto out;
819         zfsvfs->z_case = (uint_t)zval;
820
821         /*
822          * Fold case on file systems that are always or sometimes case
823          * insensitive.
824          */
825         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
826             zfsvfs->z_case == ZFS_CASE_MIXED)
827                 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
828
829         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
830         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
831
832         if (zfsvfs->z_use_sa) {
833                 /* should either have both of these objects or none */
834                 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
835                     &sa_obj);
836                 if (error)
837                         return (error);
838         } else {
839                 /*
840                  * Pre SA versions file systems should never touch
841                  * either the attribute registration or layout objects.
842                  */
843                 sa_obj = 0;
844         }
845
846         error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
847             &zfsvfs->z_attr_table);
848         if (error)
849                 goto out;
850
851         if (zfsvfs->z_version >= ZPL_VERSION_SA)
852                 sa_register_update_callback(os, zfs_sa_upgrade);
853
854         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
855             &zfsvfs->z_root);
856         if (error)
857                 goto out;
858         ASSERT(zfsvfs->z_root != 0);
859
860         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
861             &zfsvfs->z_unlinkedobj);
862         if (error)
863                 goto out;
864
865         error = zap_lookup(os, MASTER_NODE_OBJ,
866             zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
867             8, 1, &zfsvfs->z_userquota_obj);
868         if (error && error != ENOENT)
869                 goto out;
870
871         error = zap_lookup(os, MASTER_NODE_OBJ,
872             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
873             8, 1, &zfsvfs->z_groupquota_obj);
874         if (error && error != ENOENT)
875                 goto out;
876
877         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
878             &zfsvfs->z_fuid_obj);
879         if (error && error != ENOENT)
880                 goto out;
881
882         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
883             &zfsvfs->z_shares_dir);
884         if (error && error != ENOENT)
885                 goto out;
886
887         mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
888         mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
889         list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
890             offsetof(znode_t, z_link_node));
891         rrw_init(&zfsvfs->z_teardown_lock);
892         rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
893         rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
894         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
895                 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
896
897         *zfvp = zfsvfs;
898         return (0);
899
900 out:
901         dmu_objset_disown(os, zfsvfs);
902         *zfvp = NULL;
903         kmem_free(zfsvfs, sizeof (zfsvfs_t));
904         return (error);
905 }
906
907 static int
908 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
909 {
910         int error;
911
912         error = zfs_register_callbacks(zfsvfs->z_vfs);
913         if (error)
914                 return (error);
915
916         /*
917          * Set the objset user_ptr to track its zfsvfs.
918          */
919         mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
920         dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
921         mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
922
923         zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
924
925         /*
926          * If we are not mounting (ie: online recv), then we don't
927          * have to worry about replaying the log as we blocked all
928          * operations out since we closed the ZIL.
929          */
930         if (mounting) {
931                 boolean_t readonly;
932
933                 /*
934                  * During replay we remove the read only flag to
935                  * allow replays to succeed.
936                  */
937                 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
938                 if (readonly != 0)
939                         zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
940                 else
941                         zfs_unlinked_drain(zfsvfs);
942
943                 /*
944                  * Parse and replay the intent log.
945                  *
946                  * Because of ziltest, this must be done after
947                  * zfs_unlinked_drain().  (Further note: ziltest
948                  * doesn't use readonly mounts, where
949                  * zfs_unlinked_drain() isn't called.)  This is because
950                  * ziltest causes spa_sync() to think it's committed,
951                  * but actually it is not, so the intent log contains
952                  * many txg's worth of changes.
953                  *
954                  * In particular, if object N is in the unlinked set in
955                  * the last txg to actually sync, then it could be
956                  * actually freed in a later txg and then reallocated
957                  * in a yet later txg.  This would write a "create
958                  * object N" record to the intent log.  Normally, this
959                  * would be fine because the spa_sync() would have
960                  * written out the fact that object N is free, before
961                  * we could write the "create object N" intent log
962                  * record.
963                  *
964                  * But when we are in ziltest mode, we advance the "open
965                  * txg" without actually spa_sync()-ing the changes to
966                  * disk.  So we would see that object N is still
967                  * allocated and in the unlinked set, and there is an
968                  * intent log record saying to allocate it.
969                  */
970                 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
971                         if (zil_replay_disable) {
972                                 zil_destroy(zfsvfs->z_log, B_FALSE);
973                         } else {
974                                 zfsvfs->z_replay = B_TRUE;
975                                 zil_replay(zfsvfs->z_os, zfsvfs,
976                                     zfs_replay_vector);
977                                 zfsvfs->z_replay = B_FALSE;
978                         }
979                 }
980                 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
981         }
982
983         return (0);
984 }
985
986 void
987 zfsvfs_free(zfsvfs_t *zfsvfs)
988 {
989         int i;
990         extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
991
992         /*
993          * This is a barrier to prevent the filesystem from going away in
994          * zfs_znode_move() until we can safely ensure that the filesystem is
995          * not unmounted. We consider the filesystem valid before the barrier
996          * and invalid after the barrier.
997          */
998         rw_enter(&zfsvfs_lock, RW_READER);
999         rw_exit(&zfsvfs_lock);
1000
1001         zfs_fuid_destroy(zfsvfs);
1002
1003         mutex_destroy(&zfsvfs->z_znodes_lock);
1004         mutex_destroy(&zfsvfs->z_lock);
1005         list_destroy(&zfsvfs->z_all_znodes);
1006         rrw_destroy(&zfsvfs->z_teardown_lock);
1007         rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1008         rw_destroy(&zfsvfs->z_fuid_lock);
1009         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1010                 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1011         kmem_free(zfsvfs, sizeof (zfsvfs_t));
1012 }
1013
1014 static void
1015 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1016 {
1017         zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1018         if (zfsvfs->z_use_fuids && zfsvfs->z_vfs) {
1019                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1020                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1021                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1022                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1023                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1024                 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1025         }
1026         zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1027 }
1028
1029 int
1030 zfs_domount(vfs_t *vfsp, char *osname)
1031 {
1032         uint64_t recordsize, fsid_guid;
1033         int error = 0;
1034         zfsvfs_t *zfsvfs;
1035
1036         ASSERT(vfsp);
1037         ASSERT(osname);
1038
1039         error = zfsvfs_create(osname, &zfsvfs);
1040         if (error)
1041                 return (error);
1042         zfsvfs->z_vfs = vfsp;
1043
1044         /* Initialize the generic filesystem structure. */
1045         vfsp->vfs_bcount = 0;
1046         vfsp->vfs_data = NULL;
1047
1048         if ((error = dsl_prop_get_integer(osname, "recordsize",
1049             &recordsize, NULL)))
1050                 goto out;
1051
1052         vfsp->vfs_bsize = recordsize;
1053         vfsp->vfs_flag |= VFS_NOTRUNC;
1054         vfsp->vfs_data = zfsvfs;
1055
1056         /*
1057          * The fsid is 64 bits, composed of an 8-bit fs type, which
1058          * separates our fsid from any other filesystem types, and a
1059          * 56-bit objset unique ID.  The objset unique ID is unique to
1060          * all objsets open on this system, provided by unique_create().
1061          * The 8-bit fs type must be put in the low bits of fsid[1]
1062          * because that's where other Solaris filesystems put it.
1063          */
1064         fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1065         ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1066         vfsp->vfs_fsid.val[0] = fsid_guid;
1067         vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8);
1068
1069         /*
1070          * Set features for file system.
1071          */
1072         zfs_set_fuid_feature(zfsvfs);
1073         if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1074                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1075                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1076                 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1077         } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1078                 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1079                 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1080         }
1081         vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1082
1083         if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1084                 uint64_t pval;
1085
1086                 atime_changed_cb(zfsvfs, B_FALSE);
1087                 readonly_changed_cb(zfsvfs, B_TRUE);
1088                 if ((error = dsl_prop_get_integer(osname,"xattr",&pval,NULL)))
1089                         goto out;
1090                 xattr_changed_cb(zfsvfs, pval);
1091                 zfsvfs->z_issnap = B_TRUE;
1092                 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1093
1094                 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1095                 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1096                 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1097         } else {
1098                 error = zfsvfs_setup(zfsvfs, B_TRUE);
1099         }
1100
1101         if (!zfsvfs->z_issnap)
1102                 zfsctl_create(zfsvfs);
1103 out:
1104         if (error) {
1105                 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1106                 zfsvfs_free(zfsvfs);
1107         } else {
1108                 atomic_add_32(&zfs_active_fs_count, 1);
1109         }
1110
1111         return (error);
1112 }
1113 EXPORT_SYMBOL(zfs_domount);
1114
1115 void
1116 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1117 {
1118         objset_t *os = zfsvfs->z_os;
1119         struct dsl_dataset *ds;
1120
1121         /*
1122          * Unregister properties.
1123          */
1124         if (!dmu_objset_is_snapshot(os)) {
1125                 ds = dmu_objset_ds(os);
1126                 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
1127                     zfsvfs) == 0);
1128
1129                 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
1130                     zfsvfs) == 0);
1131
1132                 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
1133                     zfsvfs) == 0);
1134
1135                 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
1136                     zfsvfs) == 0);
1137
1138                 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
1139                     zfsvfs) == 0);
1140
1141                 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
1142                     zfsvfs) == 0);
1143
1144                 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
1145                     zfsvfs) == 0);
1146
1147                 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
1148                     zfsvfs) == 0);
1149
1150                 VERIFY(dsl_prop_unregister(ds, "aclinherit",
1151                     acl_inherit_changed_cb, zfsvfs) == 0);
1152
1153                 VERIFY(dsl_prop_unregister(ds, "vscan",
1154                     vscan_changed_cb, zfsvfs) == 0);
1155         }
1156 }
1157 EXPORT_SYMBOL(zfs_unregister_callbacks);
1158
1159 #ifdef HAVE_MLSLABEL
1160 /*
1161  * zfs_check_global_label:
1162  *      Check that the hex label string is appropriate for the dataset
1163  *      being mounted into the global_zone proper.
1164  *
1165  *      Return an error if the hex label string is not default or
1166  *      admin_low/admin_high.  For admin_low labels, the corresponding
1167  *      dataset must be readonly.
1168  */
1169 int
1170 zfs_check_global_label(const char *dsname, const char *hexsl)
1171 {
1172         if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1173                 return (0);
1174         if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1175                 return (0);
1176         if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1177                 /* must be readonly */
1178                 uint64_t rdonly;
1179
1180                 if (dsl_prop_get_integer(dsname,
1181                     zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1182                         return (EACCES);
1183                 return (rdonly ? 0 : EACCES);
1184         }
1185         return (EACCES);
1186 }
1187 #endif /* HAVE_MLSLABEL */
1188
1189 int
1190 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1191 {
1192         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1193         dev32_t d32;
1194         uint64_t refdbytes, availbytes, usedobjs, availobjs;
1195
1196         ZFS_ENTER(zfsvfs);
1197
1198         dmu_objset_space(zfsvfs->z_os,
1199             &refdbytes, &availbytes, &usedobjs, &availobjs);
1200
1201         /*
1202          * The underlying storage pool actually uses multiple block sizes.
1203          * We report the fragsize as the smallest block size we support,
1204          * and we report our blocksize as the filesystem's maximum blocksize.
1205          */
1206         statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1207         statp->f_bsize = zfsvfs->z_max_blksz;
1208
1209         /*
1210          * The following report "total" blocks of various kinds in the
1211          * file system, but reported in terms of f_frsize - the
1212          * "fragment" size.
1213          */
1214
1215         statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1216         statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
1217         statp->f_bavail = statp->f_bfree; /* no root reservation */
1218
1219         /*
1220          * statvfs() should really be called statufs(), because it assumes
1221          * static metadata.  ZFS doesn't preallocate files, so the best
1222          * we can do is report the max that could possibly fit in f_files,
1223          * and that minus the number actually used in f_ffree.
1224          * For f_ffree, report the smaller of the number of object available
1225          * and the number of blocks (each object will take at least a block).
1226          */
1227         statp->f_ffree = MIN(availobjs, statp->f_bfree);
1228         statp->f_favail = statp->f_ffree;       /* no "root reservation" */
1229         statp->f_files = statp->f_ffree + usedobjs;
1230
1231         (void) cmpldev(&d32, vfsp->vfs_dev);
1232         statp->f_fsid = d32;
1233
1234         /*
1235          * We're a zfs filesystem.
1236          */
1237         (void) strcpy(statp->f_basetype, MNTTYPE_ZFS);
1238
1239         statp->f_flag = vf_to_stf(vfsp->vfs_flag);
1240
1241         statp->f_namemax = ZFS_MAXNAMELEN;
1242
1243         /*
1244          * We have all of 32 characters to stuff a string here.
1245          * Is there anything useful we could/should provide?
1246          */
1247         bzero(statp->f_fstr, sizeof (statp->f_fstr));
1248
1249         ZFS_EXIT(zfsvfs);
1250         return (0);
1251 }
1252 EXPORT_SYMBOL(zfs_statvfs);
1253
1254 int
1255 zfs_root(vfs_t *vfsp, vnode_t **vpp)
1256 {
1257         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1258         znode_t *rootzp;
1259         int error;
1260
1261         ZFS_ENTER(zfsvfs);
1262
1263         error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1264         if (error == 0)
1265                 *vpp = ZTOV(rootzp);
1266
1267         ZFS_EXIT(zfsvfs);
1268         return (error);
1269 }
1270 EXPORT_SYMBOL(zfs_root);
1271
1272 /*
1273  * Teardown the zfsvfs::z_os.
1274  *
1275  * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1276  * and 'z_teardown_inactive_lock' held.
1277  */
1278 static int
1279 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1280 {
1281         znode_t *zp;
1282
1283         rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1284
1285         if (!unmounting) {
1286                 /*
1287                  * We purge the parent filesystem's vfsp as the parent
1288                  * filesystem and all of its snapshots have their vnode's
1289                  * v_vfsp set to the parent's filesystem's vfsp.  Note,
1290                  * 'z_parent' is self referential for non-snapshots.
1291                  */
1292                 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1293         }
1294
1295         /*
1296          * Close the zil. NB: Can't close the zil while zfs_inactive
1297          * threads are blocked as zil_close can call zfs_inactive.
1298          */
1299         if (zfsvfs->z_log) {
1300                 zil_close(zfsvfs->z_log);
1301                 zfsvfs->z_log = NULL;
1302         }
1303
1304         rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1305
1306         /*
1307          * If we are not unmounting (ie: online recv) and someone already
1308          * unmounted this file system while we were doing the switcheroo,
1309          * or a reopen of z_os failed then just bail out now.
1310          */
1311         if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1312                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1313                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1314                 return (EIO);
1315         }
1316
1317         /*
1318          * At this point there are no vops active, and any new vops will
1319          * fail with EIO since we have z_teardown_lock for writer (only
1320          * relavent for forced unmount).
1321          *
1322          * Release all holds on dbufs.
1323          */
1324         mutex_enter(&zfsvfs->z_znodes_lock);
1325         for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1326             zp = list_next(&zfsvfs->z_all_znodes, zp))
1327                 if (zp->z_sa_hdl) {
1328                         ASSERT(ZTOV(zp)->v_count > 0);
1329                         zfs_znode_dmu_fini(zp);
1330                 }
1331         mutex_exit(&zfsvfs->z_znodes_lock);
1332
1333         /*
1334          * If we are unmounting, set the unmounted flag and let new vops
1335          * unblock.  zfs_inactive will have the unmounted behavior, and all
1336          * other vops will fail with EIO.
1337          */
1338         if (unmounting) {
1339                 zfsvfs->z_unmounted = B_TRUE;
1340                 rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1341                 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1342         }
1343
1344         /*
1345          * z_os will be NULL if there was an error in attempting to reopen
1346          * zfsvfs, so just return as the properties had already been
1347          * unregistered and cached data had been evicted before.
1348          */
1349         if (zfsvfs->z_os == NULL)
1350                 return (0);
1351
1352         /*
1353          * Unregister properties.
1354          */
1355         zfs_unregister_callbacks(zfsvfs);
1356
1357         /*
1358          * Evict cached data
1359          */
1360         if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
1361                 if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
1362                         txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1363         (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
1364
1365         return (0);
1366 }
1367
1368 /*ARGSUSED*/
1369 int
1370 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
1371 {
1372         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1373         objset_t *os;
1374         int ret;
1375
1376         ret = secpolicy_fs_unmount(cr, vfsp);
1377         if (ret) {
1378                 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
1379                     ZFS_DELEG_PERM_MOUNT, cr))
1380                         return (ret);
1381         }
1382
1383         /*
1384          * We purge the parent filesystem's vfsp as the parent filesystem
1385          * and all of its snapshots have their vnode's v_vfsp set to the
1386          * parent's filesystem's vfsp.  Note, 'z_parent' is self
1387          * referential for non-snapshots.
1388          */
1389         (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
1390
1391         /*
1392          * Unmount any snapshots mounted under .zfs before unmounting the
1393          * dataset itself.
1394          */
1395         if (zfsvfs->z_ctldir != NULL &&
1396             (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
1397                 return (ret);
1398         }
1399
1400         if (!(fflag & MS_FORCE)) {
1401                 /*
1402                  * Check the number of active vnodes in the file system.
1403                  * Our count is maintained in the vfs structure, but the
1404                  * number is off by 1 to indicate a hold on the vfs
1405                  * structure itself.
1406                  *
1407                  * The '.zfs' directory maintains a reference of its
1408                  * own, and any active references underneath are
1409                  * reflected in the vnode count.
1410                  */
1411                 if (zfsvfs->z_ctldir == NULL) {
1412                         if (vfsp->vfs_count > 1)
1413                                 return (EBUSY);
1414                 } else {
1415                         if (vfsp->vfs_count > 2 ||
1416                             zfsvfs->z_ctldir->v_count > 1)
1417                                 return (EBUSY);
1418                 }
1419         }
1420
1421         vfsp->vfs_flag |= VFS_UNMOUNTED;
1422
1423         VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1424         os = zfsvfs->z_os;
1425
1426         /*
1427          * z_os will be NULL if there was an error in
1428          * attempting to reopen zfsvfs.
1429          */
1430         if (os != NULL) {
1431                 /*
1432                  * Unset the objset user_ptr.
1433                  */
1434                 mutex_enter(&os->os_user_ptr_lock);
1435                 dmu_objset_set_user(os, NULL);
1436                 mutex_exit(&os->os_user_ptr_lock);
1437
1438                 /*
1439                  * Finally release the objset
1440                  */
1441                 dmu_objset_disown(os, zfsvfs);
1442         }
1443
1444         /*
1445          * We can now safely destroy the '.zfs' directory node.
1446          */
1447         if (zfsvfs->z_ctldir != NULL)
1448                 zfsctl_destroy(zfsvfs);
1449
1450         return (0);
1451 }
1452 EXPORT_SYMBOL(zfs_umount);
1453
1454 int
1455 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1456 {
1457         zfsvfs_t        *zfsvfs = vfsp->vfs_data;
1458         znode_t         *zp;
1459         uint64_t        object = 0;
1460         uint64_t        fid_gen = 0;
1461         uint64_t        gen_mask;
1462         uint64_t        zp_gen;
1463         int             i, err;
1464
1465         *vpp = NULL;
1466
1467         ZFS_ENTER(zfsvfs);
1468
1469         if (fidp->fid_len == LONG_FID_LEN) {
1470                 zfid_long_t     *zlfid = (zfid_long_t *)fidp;
1471                 uint64_t        objsetid = 0;
1472                 uint64_t        setgen = 0;
1473
1474                 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1475                         objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1476
1477                 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1478                         setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1479
1480                 ZFS_EXIT(zfsvfs);
1481
1482                 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
1483                 if (err)
1484                         return (EINVAL);
1485                 ZFS_ENTER(zfsvfs);
1486         }
1487
1488         if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1489                 zfid_short_t    *zfid = (zfid_short_t *)fidp;
1490
1491                 for (i = 0; i < sizeof (zfid->zf_object); i++)
1492                         object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1493
1494                 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1495                         fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1496         } else {
1497                 ZFS_EXIT(zfsvfs);
1498                 return (EINVAL);
1499         }
1500
1501         /* A zero fid_gen means we are in the .zfs control directories */
1502         if (fid_gen == 0 &&
1503             (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1504                 *vpp = zfsvfs->z_ctldir;
1505                 ASSERT(*vpp != NULL);
1506                 if (object == ZFSCTL_INO_SNAPDIR) {
1507                         VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
1508                             0, NULL, NULL, NULL, NULL, NULL) == 0);
1509                 } else {
1510                         VN_HOLD(*vpp);
1511                 }
1512                 ZFS_EXIT(zfsvfs);
1513                 return (0);
1514         }
1515
1516         gen_mask = -1ULL >> (64 - 8 * i);
1517
1518         dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1519         if ((err = zfs_zget(zfsvfs, object, &zp))) {
1520                 ZFS_EXIT(zfsvfs);
1521                 return (err);
1522         }
1523         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
1524             sizeof (uint64_t));
1525         zp_gen = zp_gen & gen_mask;
1526         if (zp_gen == 0)
1527                 zp_gen = 1;
1528         if (zp->z_unlinked || zp_gen != fid_gen) {
1529                 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1530                 VN_RELE(ZTOV(zp));
1531                 ZFS_EXIT(zfsvfs);
1532                 return (EINVAL);
1533         }
1534
1535         *vpp = ZTOV(zp);
1536         if (*vpp)
1537                 zfs_inode_update(VTOZ(*vpp));
1538
1539         ZFS_EXIT(zfsvfs);
1540         return (0);
1541 }
1542 EXPORT_SYMBOL(zfs_vget);
1543
1544 /*
1545  * Block out VOPs and close zfsvfs_t::z_os
1546  *
1547  * Note, if successful, then we return with the 'z_teardown_lock' and
1548  * 'z_teardown_inactive_lock' write held.
1549  */
1550 int
1551 zfs_suspend_fs(zfsvfs_t *zfsvfs)
1552 {
1553         int error;
1554
1555         if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
1556                 return (error);
1557         dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1558
1559         return (0);
1560 }
1561 EXPORT_SYMBOL(zfs_suspend_fs);
1562
1563 /*
1564  * Reopen zfsvfs_t::z_os and release VOPs.
1565  */
1566 int
1567 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
1568 {
1569         int err, err2;
1570
1571         ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
1572         ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
1573
1574         err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
1575             &zfsvfs->z_os);
1576         if (err) {
1577                 zfsvfs->z_os = NULL;
1578         } else {
1579                 znode_t *zp;
1580                 uint64_t sa_obj = 0;
1581
1582                 err2 = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
1583                     ZFS_SA_ATTRS, 8, 1, &sa_obj);
1584
1585                 if ((err || err2) && zfsvfs->z_version >= ZPL_VERSION_SA)
1586                         goto bail;
1587
1588
1589                 if ((err = sa_setup(zfsvfs->z_os, sa_obj,
1590                     zfs_attr_table,  ZPL_END, &zfsvfs->z_attr_table)) != 0)
1591                         goto bail;
1592
1593                 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
1594
1595                 /*
1596                  * Attempt to re-establish all the active znodes with
1597                  * their dbufs.  If a zfs_rezget() fails, then we'll let
1598                  * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1599                  * when they try to use their znode.
1600                  */
1601                 mutex_enter(&zfsvfs->z_znodes_lock);
1602                 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
1603                     zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1604                         (void) zfs_rezget(zp);
1605                 }
1606                 mutex_exit(&zfsvfs->z_znodes_lock);
1607
1608         }
1609
1610 bail:
1611         /* release the VOPs */
1612         rw_exit(&zfsvfs->z_teardown_inactive_lock);
1613         rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
1614
1615         if (err) {
1616                 /*
1617                  * Since we couldn't reopen zfsvfs::z_os, force
1618                  * unmount this file system.
1619                  */
1620                 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
1621                         (void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
1622         }
1623         return (err);
1624 }
1625 EXPORT_SYMBOL(zfs_resume_fs);
1626
1627 static void
1628 zfs_freevfs(vfs_t *vfsp)
1629 {
1630         zfsvfs_t *zfsvfs = vfsp->vfs_data;
1631
1632         zfsvfs_free(zfsvfs);
1633
1634         atomic_add_32(&zfs_active_fs_count, -1);
1635 }
1636 #endif /* HAVE_ZPL */
1637
1638 void
1639 zfs_init(void)
1640 {
1641         zfsctl_init();
1642         zfs_znode_init();
1643
1644         dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
1645 }
1646
1647 void
1648 zfs_fini(void)
1649 {
1650         zfsctl_fini();
1651         zfs_znode_fini();
1652 }
1653
1654 #ifdef HAVE_ZPL
1655 int
1656 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
1657 {
1658         int error;
1659         objset_t *os = zfsvfs->z_os;
1660         dmu_tx_t *tx;
1661
1662         if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1663                 return (EINVAL);
1664
1665         if (newvers < zfsvfs->z_version)
1666                 return (EINVAL);
1667
1668         if (zfs_spa_version_map(newvers) >
1669             spa_version(dmu_objset_spa(zfsvfs->z_os)))
1670                 return (ENOTSUP);
1671
1672         tx = dmu_tx_create(os);
1673         dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
1674         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
1675                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1676                     ZFS_SA_ATTRS);
1677                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1678         }
1679         error = dmu_tx_assign(tx, TXG_WAIT);
1680         if (error) {
1681                 dmu_tx_abort(tx);
1682                 return (error);
1683         }
1684
1685         error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1686             8, 1, &newvers, tx);
1687
1688         if (error) {
1689                 dmu_tx_commit(tx);
1690                 return (error);
1691         }
1692
1693         if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
1694                 uint64_t sa_obj;
1695
1696                 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
1697                     SPA_VERSION_SA);
1698                 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1699                     DMU_OT_NONE, 0, tx);
1700
1701                 error = zap_add(os, MASTER_NODE_OBJ,
1702                     ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1703                 ASSERT3U(error, ==, 0);
1704
1705                 VERIFY(0 == sa_set_sa_object(os, sa_obj));
1706                 sa_register_update_callback(os, zfs_sa_upgrade);
1707         }
1708
1709         spa_history_log_internal(LOG_DS_UPGRADE,
1710             dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
1711             zfsvfs->z_version, newvers, dmu_objset_id(os));
1712
1713         dmu_tx_commit(tx);
1714
1715         zfsvfs->z_version = newvers;
1716
1717         if (zfsvfs->z_version >= ZPL_VERSION_FUID)
1718                 zfs_set_fuid_feature(zfsvfs);
1719
1720         return (0);
1721 }
1722 EXPORT_SYMBOL(zfs_set_version);
1723 #endif /* HAVE_ZPL */
1724
1725 /*
1726  * Read a property stored within the master node.
1727  */
1728 int
1729 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1730 {
1731         const char *pname;
1732         int error = ENOENT;
1733
1734         /*
1735          * Look up the file system's value for the property.  For the
1736          * version property, we look up a slightly different string.
1737          */
1738         if (prop == ZFS_PROP_VERSION)
1739                 pname = ZPL_VERSION_STR;
1740         else
1741                 pname = zfs_prop_to_name(prop);
1742
1743         if (os != NULL)
1744                 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1745
1746         if (error == ENOENT) {
1747                 /* No value set, use the default value */
1748                 switch (prop) {
1749                 case ZFS_PROP_VERSION:
1750                         *value = ZPL_VERSION;
1751                         break;
1752                 case ZFS_PROP_NORMALIZE:
1753                 case ZFS_PROP_UTF8ONLY:
1754                         *value = 0;
1755                         break;
1756                 case ZFS_PROP_CASE:
1757                         *value = ZFS_CASE_SENSITIVE;
1758                         break;
1759                 default:
1760                         return (error);
1761                 }
1762                 error = 0;
1763         }
1764         return (error);
1765 }