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