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