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