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