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