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