2575638870528545e42a78d6fab2dffb154d0672
[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          * Drain the iput_taskq to ensure all active references to the
1096          * zfs_sb_t have been handled only then can it be safely destroyed.
1097          */
1098         taskq_wait(dsl_pool_iput_taskq(dmu_objset_pool(zsb->z_os)));
1099
1100         /*
1101          * Close the zil. NB: Can't close the zil while zfs_inactive
1102          * threads are blocked as zil_close can call zfs_inactive.
1103          */
1104         if (zsb->z_log) {
1105                 zil_close(zsb->z_log);
1106                 zsb->z_log = NULL;
1107         }
1108
1109         rw_enter(&zsb->z_teardown_inactive_lock, RW_WRITER);
1110
1111         /*
1112          * If we are not unmounting (ie: online recv) and someone already
1113          * unmounted this file system while we were doing the switcheroo,
1114          * or a reopen of z_os failed then just bail out now.
1115          */
1116         if (!unmounting && (zsb->z_unmounted || zsb->z_os == NULL)) {
1117                 rw_exit(&zsb->z_teardown_inactive_lock);
1118                 rrw_exit(&zsb->z_teardown_lock, FTAG);
1119                 return (EIO);
1120         }
1121
1122         /*
1123          * At this point there are no vops active, and any new vops will
1124          * fail with EIO since we have z_teardown_lock for writer (only
1125          * relavent for forced unmount).
1126          *
1127          * Release all holds on dbufs.
1128          */
1129         mutex_enter(&zsb->z_znodes_lock);
1130         for (zp = list_head(&zsb->z_all_znodes); zp != NULL;
1131             zp = list_next(&zsb->z_all_znodes, zp))
1132                 if (zp->z_sa_hdl) {
1133                         ASSERT(atomic_read(&ZTOI(zp)->i_count) > 0);
1134                         zfs_znode_dmu_fini(zp);
1135                 }
1136         mutex_exit(&zsb->z_znodes_lock);
1137
1138         /*
1139          * If we are unmounting, set the unmounted flag and let new vops
1140          * unblock.  zfs_inactive will have the unmounted behavior, and all
1141          * other vops will fail with EIO.
1142          */
1143         if (unmounting) {
1144                 zsb->z_unmounted = B_TRUE;
1145                 rrw_exit(&zsb->z_teardown_lock, FTAG);
1146                 rw_exit(&zsb->z_teardown_inactive_lock);
1147         }
1148
1149         /*
1150          * z_os will be NULL if there was an error in attempting to reopen
1151          * zsb, so just return as the properties had already been
1152          *
1153          * unregistered and cached data had been evicted before.
1154          */
1155         if (zsb->z_os == NULL)
1156                 return (0);
1157
1158         /*
1159          * Unregister properties.
1160          */
1161         zfs_unregister_callbacks(zsb);
1162
1163         /*
1164          * Evict cached data
1165          */
1166         if (dmu_objset_is_dirty_anywhere(zsb->z_os))
1167                 if (!(zsb->z_vfs->mnt_flags & MNT_READONLY))
1168                         txg_wait_synced(dmu_objset_pool(zsb->z_os), 0);
1169         (void) dmu_objset_evict_dbufs(zsb->z_os);
1170
1171         return (0);
1172 }
1173
1174 int
1175 zfs_domount(struct super_block *sb, void *data, int silent)
1176 {
1177         zpl_mount_data_t *zmd = data;
1178         const char *osname = zmd->z_osname;
1179         zfs_sb_t *zsb;
1180         struct inode *root_inode;
1181         uint64_t recordsize;
1182         int error;
1183
1184         /*
1185          * Linux allows multiple vfs mounts per super block.  However, the
1186          * zfs_sb_t only contains a pointer for a single vfs mount.  This
1187          * back reference in the long term could be extended to a list of
1188          * vfs mounts if a hook were added to the kernel to notify us when
1189          * a vfsmount is destroyed.  Until then we must limit the number
1190          * of mounts per super block to one.
1191          */
1192         if (atomic_read(&sb->s_active) > 1)
1193                 return (EBUSY);
1194
1195         error = zfs_sb_create(osname, &zsb);
1196         if (error)
1197                 return (error);
1198
1199         if ((error = dsl_prop_get_integer(osname, "recordsize",
1200             &recordsize, NULL)))
1201                 goto out;
1202
1203         zsb->z_sb = sb;
1204         zsb->z_vfs = zmd->z_vfs;
1205         sb->s_fs_info = zsb;
1206         sb->s_magic = ZFS_SUPER_MAGIC;
1207         sb->s_maxbytes = MAX_LFS_FILESIZE;
1208         sb->s_time_gran = 1;
1209         sb->s_blocksize = recordsize;
1210         sb->s_blocksize_bits = ilog2(recordsize);
1211
1212         /* Set callback operations for the file system. */
1213         sb->s_op = &zpl_super_operations;
1214         sb->s_xattr = zpl_xattr_handlers;
1215         sb->s_export_op = &zpl_export_operations;
1216
1217         /* Set features for file system. */
1218         zfs_set_fuid_feature(zsb);
1219
1220         if (dmu_objset_is_snapshot(zsb->z_os)) {
1221                 uint64_t pval;
1222
1223                 atime_changed_cb(zsb, B_FALSE);
1224                 readonly_changed_cb(zsb, B_TRUE);
1225                 if ((error = dsl_prop_get_integer(osname,"xattr",&pval,NULL)))
1226                         goto out;
1227                 xattr_changed_cb(zsb, pval);
1228                 zsb->z_issnap = B_TRUE;
1229                 zsb->z_os->os_sync = ZFS_SYNC_DISABLED;
1230
1231                 mutex_enter(&zsb->z_os->os_user_ptr_lock);
1232                 dmu_objset_set_user(zsb->z_os, zsb);
1233                 mutex_exit(&zsb->z_os->os_user_ptr_lock);
1234         } else {
1235                 error = zfs_sb_setup(zsb, B_TRUE);
1236 #ifdef HAVE_SNAPSHOT
1237                 (void) zfs_snap_create(zsb);
1238 #endif /* HAVE_SNAPSHOT */
1239         }
1240
1241         /* Allocate a root inode for the filesystem. */
1242         error = zfs_root(zsb, &root_inode);
1243         if (error) {
1244                 (void) zfs_umount(sb);
1245                 goto out;
1246         }
1247
1248         /* Allocate a root dentry for the filesystem */
1249         sb->s_root = d_alloc_root(root_inode);
1250         if (sb->s_root == NULL) {
1251                 (void) zfs_umount(sb);
1252                 error = ENOMEM;
1253                 goto out;
1254         }
1255 out:
1256         if (error) {
1257                 dmu_objset_disown(zsb->z_os, zsb);
1258                 zfs_sb_free(zsb);
1259         }
1260
1261         return (error);
1262 }
1263 EXPORT_SYMBOL(zfs_domount);
1264
1265 /*ARGSUSED*/
1266 int
1267 zfs_umount(struct super_block *sb)
1268 {
1269         zfs_sb_t *zsb = sb->s_fs_info;
1270         objset_t *os;
1271
1272         VERIFY(zfsvfs_teardown(zsb, B_TRUE) == 0);
1273         os = zsb->z_os;
1274
1275         /*
1276          * z_os will be NULL if there was an error in
1277          * attempting to reopen zsb.
1278          */
1279         if (os != NULL) {
1280                 /*
1281                  * Unset the objset user_ptr.
1282                  */
1283                 mutex_enter(&os->os_user_ptr_lock);
1284                 dmu_objset_set_user(os, NULL);
1285                 mutex_exit(&os->os_user_ptr_lock);
1286
1287                 /*
1288                  * Finally release the objset
1289                  */
1290                 dmu_objset_disown(os, zsb);
1291         }
1292
1293         zfs_sb_free(zsb);
1294         return (0);
1295 }
1296 EXPORT_SYMBOL(zfs_umount);
1297
1298 int
1299 zfs_remount(struct super_block *sb, int *flags, char *data)
1300 {
1301         zfs_sb_t *zsb = sb->s_fs_info;
1302         boolean_t readonly = B_FALSE;
1303         boolean_t setuid = B_TRUE;
1304         boolean_t exec = B_TRUE;
1305         boolean_t devices = B_TRUE;
1306         boolean_t atime = B_TRUE;
1307
1308         if (*flags & MS_RDONLY)
1309                 readonly = B_TRUE;
1310
1311         if (*flags & MS_NOSUID) {
1312                 devices = B_FALSE;
1313                 setuid = B_FALSE;
1314         } else {
1315                 if (*flags & MS_NODEV)
1316                         devices = B_FALSE;
1317         }
1318
1319         if (*flags & MS_NOEXEC)
1320                 exec = B_FALSE;
1321
1322         if (*flags & MS_NOATIME)
1323                 atime = B_FALSE;
1324
1325         /*
1326          * Invoke our callbacks to set required flags.
1327          */
1328         readonly_changed_cb(zsb, readonly);
1329         setuid_changed_cb(zsb, setuid);
1330         exec_changed_cb(zsb, exec);
1331         devices_changed_cb(zsb, devices);
1332         atime_changed_cb(zsb, atime);
1333
1334         return (0);
1335 }
1336 EXPORT_SYMBOL(zfs_remount);
1337
1338 int
1339 zfs_vget(struct vfsmount *vfsp, struct inode **ipp, fid_t *fidp)
1340 {
1341         zfs_sb_t        *zsb = VTOZSB(vfsp);
1342         znode_t         *zp;
1343         uint64_t        object = 0;
1344         uint64_t        fid_gen = 0;
1345         uint64_t        gen_mask;
1346         uint64_t        zp_gen;
1347         int             i, err;
1348
1349         *ipp = NULL;
1350
1351         ZFS_ENTER(zsb);
1352
1353         if (fidp->fid_len == LONG_FID_LEN) {
1354                 zfid_long_t     *zlfid = (zfid_long_t *)fidp;
1355                 uint64_t        objsetid = 0;
1356                 uint64_t        setgen = 0;
1357
1358                 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1359                         objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1360
1361                 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1362                         setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1363
1364                 ZFS_EXIT(zsb);
1365
1366 #ifdef HAVE_SNAPSHOT
1367                 err = zfsctl_lookup_objset(vfsp, objsetid, &zsb);
1368                 if (err)
1369                         return (EINVAL);
1370 #endif /* HAVE_SNAPSHOT */
1371                 ZFS_ENTER(zsb);
1372         }
1373
1374         if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1375                 zfid_short_t    *zfid = (zfid_short_t *)fidp;
1376
1377                 for (i = 0; i < sizeof (zfid->zf_object); i++)
1378                         object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1379
1380                 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1381                         fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1382         } else {
1383                 ZFS_EXIT(zsb);
1384                 return (EINVAL);
1385         }
1386
1387 #ifdef HAVE_SNAPSHOT
1388         /* A zero fid_gen means we are in the .zfs control directories */
1389         if (fid_gen == 0 &&
1390             (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1391                 *ipp = zsb->z_ctldir;
1392                 ASSERT(*ipp != NULL);
1393                 if (object == ZFSCTL_INO_SNAPDIR) {
1394                         VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp, NULL,
1395                             0, NULL, NULL, NULL, NULL, NULL) == 0);
1396                 } else {
1397                         igrab(*ipp);
1398                 }
1399                 ZFS_EXIT(zsb);
1400                 return (0);
1401         }
1402 #endif /* HAVE_SNAPSHOT */
1403
1404         gen_mask = -1ULL >> (64 - 8 * i);
1405
1406         dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1407         if ((err = zfs_zget(zsb, object, &zp))) {
1408                 ZFS_EXIT(zsb);
1409                 return (err);
1410         }
1411         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb), &zp_gen,
1412             sizeof (uint64_t));
1413         zp_gen = zp_gen & gen_mask;
1414         if (zp_gen == 0)
1415                 zp_gen = 1;
1416         if (zp->z_unlinked || zp_gen != fid_gen) {
1417                 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1418                 iput(ZTOI(zp));
1419                 ZFS_EXIT(zsb);
1420                 return (EINVAL);
1421         }
1422
1423         *ipp = ZTOI(zp);
1424         if (*ipp)
1425                 zfs_inode_update(ITOZ(*ipp));
1426
1427         ZFS_EXIT(zsb);
1428         return (0);
1429 }
1430 EXPORT_SYMBOL(zfs_vget);
1431
1432 /*
1433  * Block out VOPs and close zfs_sb_t::z_os
1434  *
1435  * Note, if successful, then we return with the 'z_teardown_lock' and
1436  * 'z_teardown_inactive_lock' write held.
1437  */
1438 int
1439 zfs_suspend_fs(zfs_sb_t *zsb)
1440 {
1441         int error;
1442
1443         if ((error = zfsvfs_teardown(zsb, B_FALSE)) != 0)
1444                 return (error);
1445         dmu_objset_disown(zsb->z_os, zsb);
1446
1447         return (0);
1448 }
1449 EXPORT_SYMBOL(zfs_suspend_fs);
1450
1451 /*
1452  * Reopen zfs_sb_t::z_os and release VOPs.
1453  */
1454 int
1455 zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
1456 {
1457         int err, err2;
1458
1459         ASSERT(RRW_WRITE_HELD(&zsb->z_teardown_lock));
1460         ASSERT(RW_WRITE_HELD(&zsb->z_teardown_inactive_lock));
1461
1462         err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zsb, &zsb->z_os);
1463         if (err) {
1464                 zsb->z_os = NULL;
1465         } else {
1466                 znode_t *zp;
1467                 uint64_t sa_obj = 0;
1468
1469                 err2 = zap_lookup(zsb->z_os, MASTER_NODE_OBJ,
1470                     ZFS_SA_ATTRS, 8, 1, &sa_obj);
1471
1472                 if ((err || err2) && zsb->z_version >= ZPL_VERSION_SA)
1473                         goto bail;
1474
1475
1476                 if ((err = sa_setup(zsb->z_os, sa_obj,
1477                     zfs_attr_table,  ZPL_END, &zsb->z_attr_table)) != 0)
1478                         goto bail;
1479
1480                 VERIFY(zfs_sb_setup(zsb, B_FALSE) == 0);
1481
1482                 /*
1483                  * Attempt to re-establish all the active znodes with
1484                  * their dbufs.  If a zfs_rezget() fails, then we'll let
1485                  * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1486                  * when they try to use their znode.
1487                  */
1488                 mutex_enter(&zsb->z_znodes_lock);
1489                 for (zp = list_head(&zsb->z_all_znodes); zp;
1490                     zp = list_next(&zsb->z_all_znodes, zp)) {
1491                         (void) zfs_rezget(zp);
1492                 }
1493                 mutex_exit(&zsb->z_znodes_lock);
1494
1495         }
1496
1497 bail:
1498         /* release the VOPs */
1499         rw_exit(&zsb->z_teardown_inactive_lock);
1500         rrw_exit(&zsb->z_teardown_lock, FTAG);
1501
1502         if (err) {
1503                 /*
1504                  * Since we couldn't reopen zfs_sb_t::z_os, force
1505                  * unmount this file system.
1506                  */
1507                 (void) zfs_umount(zsb->z_sb);
1508         }
1509         return (err);
1510 }
1511 EXPORT_SYMBOL(zfs_resume_fs);
1512
1513 int
1514 zfs_set_version(zfs_sb_t *zsb, uint64_t newvers)
1515 {
1516         int error;
1517         objset_t *os = zsb->z_os;
1518         dmu_tx_t *tx;
1519
1520         if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1521                 return (EINVAL);
1522
1523         if (newvers < zsb->z_version)
1524                 return (EINVAL);
1525
1526         if (zfs_spa_version_map(newvers) >
1527             spa_version(dmu_objset_spa(zsb->z_os)))
1528                 return (ENOTSUP);
1529
1530         tx = dmu_tx_create(os);
1531         dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
1532         if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
1533                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1534                     ZFS_SA_ATTRS);
1535                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1536         }
1537         error = dmu_tx_assign(tx, TXG_WAIT);
1538         if (error) {
1539                 dmu_tx_abort(tx);
1540                 return (error);
1541         }
1542
1543         error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1544             8, 1, &newvers, tx);
1545
1546         if (error) {
1547                 dmu_tx_commit(tx);
1548                 return (error);
1549         }
1550
1551         if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
1552                 uint64_t sa_obj;
1553
1554                 ASSERT3U(spa_version(dmu_objset_spa(zsb->z_os)), >=,
1555                     SPA_VERSION_SA);
1556                 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1557                     DMU_OT_NONE, 0, tx);
1558
1559                 error = zap_add(os, MASTER_NODE_OBJ,
1560                     ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1561                 ASSERT3U(error, ==, 0);
1562
1563                 VERIFY(0 == sa_set_sa_object(os, sa_obj));
1564                 sa_register_update_callback(os, zfs_sa_upgrade);
1565         }
1566
1567         spa_history_log_internal(LOG_DS_UPGRADE,
1568             dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
1569             zsb->z_version, newvers, dmu_objset_id(os));
1570
1571         dmu_tx_commit(tx);
1572
1573         zsb->z_version = newvers;
1574
1575         if (zsb->z_version >= ZPL_VERSION_FUID)
1576                 zfs_set_fuid_feature(zsb);
1577
1578         return (0);
1579 }
1580 EXPORT_SYMBOL(zfs_set_version);
1581
1582 /*
1583  * Read a property stored within the master node.
1584  */
1585 int
1586 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1587 {
1588         const char *pname;
1589         int error = ENOENT;
1590
1591         /*
1592          * Look up the file system's value for the property.  For the
1593          * version property, we look up a slightly different string.
1594          */
1595         if (prop == ZFS_PROP_VERSION)
1596                 pname = ZPL_VERSION_STR;
1597         else
1598                 pname = zfs_prop_to_name(prop);
1599
1600         if (os != NULL)
1601                 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1602
1603         if (error == ENOENT) {
1604                 /* No value set, use the default value */
1605                 switch (prop) {
1606                 case ZFS_PROP_VERSION:
1607                         *value = ZPL_VERSION;
1608                         break;
1609                 case ZFS_PROP_NORMALIZE:
1610                 case ZFS_PROP_UTF8ONLY:
1611                         *value = 0;
1612                         break;
1613                 case ZFS_PROP_CASE:
1614                         *value = ZFS_CASE_SENSITIVE;
1615                         break;
1616                 default:
1617                         return (error);
1618                 }
1619                 error = 0;
1620         }
1621         return (error);
1622 }
1623
1624 void
1625 zfs_init(void)
1626 {
1627         zfs_znode_init();
1628         dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
1629         register_filesystem(&zpl_fs_type);
1630 }
1631
1632 void
1633 zfs_fini(void)
1634 {
1635         unregister_filesystem(&zpl_fs_type);
1636         zfs_znode_fini();
1637 }