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