]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_vfsops.c
Merge branch 'zpl'
[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 sizes.
1001 * We report the fragsize as the smallest block size we support,
1002 * and we report our blocksize as the filesystem's maximum blocksize.
1003 */
1004 statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
1005 statp->f_bsize = zsb->z_max_blksz;
1006 bshift = fls(statp->f_bsize) - 1;
1007
1008 /*
1009 * The following report "total" blocks of various kinds in
1010 * the file system, but reported in terms of f_bsize - the
1011 * "preferred" size.
1012 */
1013
1014 statp->f_blocks = (refdbytes + availbytes) >> bshift;
1015 statp->f_bfree = availbytes >> bshift;
1016 statp->f_bavail = statp->f_bfree; /* no root reservation */
1017
1018 /*
1019 * statvfs() should really be called statufs(), because it assumes
1020 * static metadata. ZFS doesn't preallocate files, so the best
1021 * we can do is report the max that could possibly fit in f_files,
1022 * and that minus the number actually used in f_ffree.
1023 * For f_ffree, report the smaller of the number of object available
1024 * and the number of blocks (each object will take at least a block).
1025 */
1026 statp->f_ffree = MIN(availobjs, statp->f_bfree);
1027 statp->f_files = statp->f_ffree + usedobjs;
1028 statp->f_fsid.val[0] = 0; /* XXX: Map up some unique ID */
1029 statp->f_fsid.val[1] = 0;
1030 statp->f_type = ZFS_SUPER_MAGIC;
1031 statp->f_namelen = ZFS_MAXNAMELEN;
1032
1033 /*
1034 * We have all of 40 characters to stuff a string here.
1035 * Is there anything useful we could/should provide?
1036 */
1037 bzero(statp->f_spare, sizeof (statp->f_spare));
1038
1039 ZFS_EXIT(zsb);
1040 return (0);
1041 }
1042 EXPORT_SYMBOL(zfs_statvfs);
1043
1044 int
1045 zfs_root(zfs_sb_t *zsb, struct inode **ipp)
1046 {
1047 znode_t *rootzp;
1048 int error;
1049
1050 ZFS_ENTER(zsb);
1051
1052 error = zfs_zget(zsb, zsb->z_root, &rootzp);
1053 if (error == 0)
1054 *ipp = ZTOI(rootzp);
1055
1056 ZFS_EXIT(zsb);
1057 return (error);
1058 }
1059 EXPORT_SYMBOL(zfs_root);
1060
1061 /*
1062 * Teardown the zfs_sb_t::z_os.
1063 *
1064 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1065 * and 'z_teardown_inactive_lock' held.
1066 */
1067 int
1068 zfsvfs_teardown(zfs_sb_t *zsb, boolean_t unmounting)
1069 {
1070 znode_t *zp;
1071
1072 rrw_enter(&zsb->z_teardown_lock, RW_WRITER, FTAG);
1073
1074 if (!unmounting) {
1075 /*
1076 * We purge the parent filesystem's super block as the
1077 * parent filesystem and all of its snapshots have their
1078 * inode's super block set to the parent's filesystem's
1079 * super block. Note, 'z_parent' is self referential
1080 * for non-snapshots.
1081 */
1082 shrink_dcache_sb(zsb->z_parent->z_sb);
1083 invalidate_inodes(zsb->z_parent->z_sb);
1084 }
1085
1086 /*
1087 * Close the zil. NB: Can't close the zil while zfs_inactive
1088 * threads are blocked as zil_close can call zfs_inactive.
1089 */
1090 if (zsb->z_log) {
1091 zil_close(zsb->z_log);
1092 zsb->z_log = NULL;
1093 }
1094
1095 rw_enter(&zsb->z_teardown_inactive_lock, RW_WRITER);
1096
1097 /*
1098 * If we are not unmounting (ie: online recv) and someone already
1099 * unmounted this file system while we were doing the switcheroo,
1100 * or a reopen of z_os failed then just bail out now.
1101 */
1102 if (!unmounting && (zsb->z_unmounted || zsb->z_os == NULL)) {
1103 rw_exit(&zsb->z_teardown_inactive_lock);
1104 rrw_exit(&zsb->z_teardown_lock, FTAG);
1105 return (EIO);
1106 }
1107
1108 /*
1109 * At this point there are no vops active, and any new vops will
1110 * fail with EIO since we have z_teardown_lock for writer (only
1111 * relavent for forced unmount).
1112 *
1113 * Release all holds on dbufs.
1114 */
1115 mutex_enter(&zsb->z_znodes_lock);
1116 for (zp = list_head(&zsb->z_all_znodes); zp != NULL;
1117 zp = list_next(&zsb->z_all_znodes, zp))
1118 if (zp->z_sa_hdl) {
1119 ASSERT(atomic_read(&ZTOI(zp)->i_count) > 0);
1120 zfs_znode_dmu_fini(zp);
1121 }
1122 mutex_exit(&zsb->z_znodes_lock);
1123
1124 /*
1125 * If we are unmounting, set the unmounted flag and let new vops
1126 * unblock. zfs_inactive will have the unmounted behavior, and all
1127 * other vops will fail with EIO.
1128 */
1129 if (unmounting) {
1130 zsb->z_unmounted = B_TRUE;
1131 rrw_exit(&zsb->z_teardown_lock, FTAG);
1132 rw_exit(&zsb->z_teardown_inactive_lock);
1133 }
1134
1135 /*
1136 * z_os will be NULL if there was an error in attempting to reopen
1137 * zsb, so just return as the properties had already been
1138 *
1139 * unregistered and cached data had been evicted before.
1140 */
1141 if (zsb->z_os == NULL)
1142 return (0);
1143
1144 /*
1145 * Unregister properties.
1146 */
1147 zfs_unregister_callbacks(zsb);
1148
1149 /*
1150 * Evict cached data
1151 */
1152 if (dmu_objset_is_dirty_anywhere(zsb->z_os))
1153 if (!(zsb->z_vfs->mnt_flags & MNT_READONLY))
1154 txg_wait_synced(dmu_objset_pool(zsb->z_os), 0);
1155 (void) dmu_objset_evict_dbufs(zsb->z_os);
1156
1157 return (0);
1158 }
1159
1160 int
1161 zfs_domount(struct super_block *sb, void *data, int silent)
1162 {
1163 zpl_mount_data_t *zmd = data;
1164 const char *osname = zmd->z_osname;
1165 zfs_sb_t *zsb;
1166 struct inode *root_inode;
1167 uint64_t recordsize;
1168 int error;
1169
1170 /*
1171 * Linux allows multiple vfs mounts per super block. However, the
1172 * zfs_sb_t only contains a pointer for a single vfs mount. This
1173 * back reference in the long term could be extended to a list of
1174 * vfs mounts if a hook were added to the kernel to notify us when
1175 * a vfsmount is destroyed. Until then we must limit the number
1176 * of mounts per super block to one.
1177 */
1178 if (atomic_read(&sb->s_active) > 1)
1179 return (EBUSY);
1180
1181 error = zfs_sb_create(osname, &zsb);
1182 if (error)
1183 return (error);
1184
1185 if ((error = dsl_prop_get_integer(osname, "recordsize",
1186 &recordsize, NULL)))
1187 goto out;
1188
1189 zsb->z_sb = sb;
1190 zsb->z_vfs = zmd->z_vfs;
1191 sb->s_fs_info = zsb;
1192 sb->s_magic = ZFS_SUPER_MAGIC;
1193 sb->s_maxbytes = MAX_LFS_FILESIZE;
1194 sb->s_time_gran = 1;
1195 sb->s_blocksize = recordsize;
1196 sb->s_blocksize_bits = ilog2(recordsize);
1197
1198 /* Set callback operations for the file system. */
1199 sb->s_op = &zpl_super_operations;
1200 sb->s_xattr = zpl_xattr_handlers;
1201 #ifdef HAVE_EXPORTS
1202 sb->s_export_op = &zpl_export_operations;
1203 #endif /* HAVE_EXPORTS */
1204
1205 /* Set features for file system. */
1206 zfs_set_fuid_feature(zsb);
1207
1208 if (dmu_objset_is_snapshot(zsb->z_os)) {
1209 uint64_t pval;
1210
1211 atime_changed_cb(zsb, B_FALSE);
1212 readonly_changed_cb(zsb, B_TRUE);
1213 if ((error = dsl_prop_get_integer(osname,"xattr",&pval,NULL)))
1214 goto out;
1215 xattr_changed_cb(zsb, pval);
1216 zsb->z_issnap = B_TRUE;
1217 zsb->z_os->os_sync = ZFS_SYNC_DISABLED;
1218
1219 mutex_enter(&zsb->z_os->os_user_ptr_lock);
1220 dmu_objset_set_user(zsb->z_os, zsb);
1221 mutex_exit(&zsb->z_os->os_user_ptr_lock);
1222 } else {
1223 error = zfs_sb_setup(zsb, B_TRUE);
1224 #ifdef HAVE_SNAPSHOT
1225 (void) zfs_snap_create(zsb);
1226 #endif /* HAVE_SNAPSHOT */
1227 }
1228
1229 /* Allocate a root inode for the filesystem. */
1230 error = zfs_root(zsb, &root_inode);
1231 if (error) {
1232 (void) zfs_umount(sb);
1233 goto out;
1234 }
1235
1236 /* Allocate a root dentry for the filesystem */
1237 sb->s_root = d_alloc_root(root_inode);
1238 if (sb->s_root == NULL) {
1239 (void) zfs_umount(sb);
1240 error = ENOMEM;
1241 goto out;
1242 }
1243 out:
1244 if (error) {
1245 dmu_objset_disown(zsb->z_os, zsb);
1246 zfs_sb_free(zsb);
1247 }
1248
1249 return (error);
1250 }
1251 EXPORT_SYMBOL(zfs_domount);
1252
1253 /*ARGSUSED*/
1254 int
1255 zfs_umount(struct super_block *sb)
1256 {
1257 zfs_sb_t *zsb = sb->s_fs_info;
1258 objset_t *os;
1259
1260 VERIFY(zfsvfs_teardown(zsb, B_TRUE) == 0);
1261 os = zsb->z_os;
1262
1263 /*
1264 * z_os will be NULL if there was an error in
1265 * attempting to reopen zsb.
1266 */
1267 if (os != NULL) {
1268 /*
1269 * Unset the objset user_ptr.
1270 */
1271 mutex_enter(&os->os_user_ptr_lock);
1272 dmu_objset_set_user(os, NULL);
1273 mutex_exit(&os->os_user_ptr_lock);
1274
1275 /*
1276 * Finally release the objset
1277 */
1278 dmu_objset_disown(os, zsb);
1279 }
1280
1281 zfs_sb_free(zsb);
1282 return (0);
1283 }
1284 EXPORT_SYMBOL(zfs_umount);
1285
1286 int
1287 zfs_vget(struct vfsmount *vfsp, struct inode **ipp, fid_t *fidp)
1288 {
1289 zfs_sb_t *zsb = VTOZSB(vfsp);
1290 znode_t *zp;
1291 uint64_t object = 0;
1292 uint64_t fid_gen = 0;
1293 uint64_t gen_mask;
1294 uint64_t zp_gen;
1295 int i, err;
1296
1297 *ipp = NULL;
1298
1299 ZFS_ENTER(zsb);
1300
1301 if (fidp->fid_len == LONG_FID_LEN) {
1302 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1303 uint64_t objsetid = 0;
1304 uint64_t setgen = 0;
1305
1306 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1307 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1308
1309 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1310 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1311
1312 ZFS_EXIT(zsb);
1313
1314 #ifdef HAVE_SNAPSHOT
1315 err = zfsctl_lookup_objset(vfsp, objsetid, &zsb);
1316 if (err)
1317 return (EINVAL);
1318 #endif /* HAVE_SNAPSHOT */
1319 ZFS_ENTER(zsb);
1320 }
1321
1322 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1323 zfid_short_t *zfid = (zfid_short_t *)fidp;
1324
1325 for (i = 0; i < sizeof (zfid->zf_object); i++)
1326 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1327
1328 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1329 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1330 } else {
1331 ZFS_EXIT(zsb);
1332 return (EINVAL);
1333 }
1334
1335 #ifdef HAVE_SNAPSHOT
1336 /* A zero fid_gen means we are in the .zfs control directories */
1337 if (fid_gen == 0 &&
1338 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1339 *ipp = zsb->z_ctldir;
1340 ASSERT(*ipp != NULL);
1341 if (object == ZFSCTL_INO_SNAPDIR) {
1342 VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp, NULL,
1343 0, NULL, NULL, NULL, NULL, NULL) == 0);
1344 } else {
1345 igrab(*ipp);
1346 }
1347 ZFS_EXIT(zsb);
1348 return (0);
1349 }
1350 #endif /* HAVE_SNAPSHOT */
1351
1352 gen_mask = -1ULL >> (64 - 8 * i);
1353
1354 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
1355 if ((err = zfs_zget(zsb, object, &zp))) {
1356 ZFS_EXIT(zsb);
1357 return (err);
1358 }
1359 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb), &zp_gen,
1360 sizeof (uint64_t));
1361 zp_gen = zp_gen & gen_mask;
1362 if (zp_gen == 0)
1363 zp_gen = 1;
1364 if (zp->z_unlinked || zp_gen != fid_gen) {
1365 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
1366 iput(ZTOI(zp));
1367 ZFS_EXIT(zsb);
1368 return (EINVAL);
1369 }
1370
1371 *ipp = ZTOI(zp);
1372 if (*ipp)
1373 zfs_inode_update(ITOZ(*ipp));
1374
1375 ZFS_EXIT(zsb);
1376 return (0);
1377 }
1378 EXPORT_SYMBOL(zfs_vget);
1379
1380 /*
1381 * Block out VOPs and close zfs_sb_t::z_os
1382 *
1383 * Note, if successful, then we return with the 'z_teardown_lock' and
1384 * 'z_teardown_inactive_lock' write held.
1385 */
1386 int
1387 zfs_suspend_fs(zfs_sb_t *zsb)
1388 {
1389 int error;
1390
1391 if ((error = zfsvfs_teardown(zsb, B_FALSE)) != 0)
1392 return (error);
1393 dmu_objset_disown(zsb->z_os, zsb);
1394
1395 return (0);
1396 }
1397 EXPORT_SYMBOL(zfs_suspend_fs);
1398
1399 /*
1400 * Reopen zfs_sb_t::z_os and release VOPs.
1401 */
1402 int
1403 zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
1404 {
1405 int err, err2;
1406
1407 ASSERT(RRW_WRITE_HELD(&zsb->z_teardown_lock));
1408 ASSERT(RW_WRITE_HELD(&zsb->z_teardown_inactive_lock));
1409
1410 err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zsb, &zsb->z_os);
1411 if (err) {
1412 zsb->z_os = NULL;
1413 } else {
1414 znode_t *zp;
1415 uint64_t sa_obj = 0;
1416
1417 err2 = zap_lookup(zsb->z_os, MASTER_NODE_OBJ,
1418 ZFS_SA_ATTRS, 8, 1, &sa_obj);
1419
1420 if ((err || err2) && zsb->z_version >= ZPL_VERSION_SA)
1421 goto bail;
1422
1423
1424 if ((err = sa_setup(zsb->z_os, sa_obj,
1425 zfs_attr_table, ZPL_END, &zsb->z_attr_table)) != 0)
1426 goto bail;
1427
1428 VERIFY(zfs_sb_setup(zsb, B_FALSE) == 0);
1429
1430 /*
1431 * Attempt to re-establish all the active znodes with
1432 * their dbufs. If a zfs_rezget() fails, then we'll let
1433 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1434 * when they try to use their znode.
1435 */
1436 mutex_enter(&zsb->z_znodes_lock);
1437 for (zp = list_head(&zsb->z_all_znodes); zp;
1438 zp = list_next(&zsb->z_all_znodes, zp)) {
1439 (void) zfs_rezget(zp);
1440 }
1441 mutex_exit(&zsb->z_znodes_lock);
1442
1443 }
1444
1445 bail:
1446 /* release the VOPs */
1447 rw_exit(&zsb->z_teardown_inactive_lock);
1448 rrw_exit(&zsb->z_teardown_lock, FTAG);
1449
1450 if (err) {
1451 /*
1452 * Since we couldn't reopen zfs_sb_t::z_os, force
1453 * unmount this file system.
1454 */
1455 (void) zfs_umount(zsb->z_sb);
1456 }
1457 return (err);
1458 }
1459 EXPORT_SYMBOL(zfs_resume_fs);
1460
1461 int
1462 zfs_set_version(zfs_sb_t *zsb, uint64_t newvers)
1463 {
1464 int error;
1465 objset_t *os = zsb->z_os;
1466 dmu_tx_t *tx;
1467
1468 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1469 return (EINVAL);
1470
1471 if (newvers < zsb->z_version)
1472 return (EINVAL);
1473
1474 if (zfs_spa_version_map(newvers) >
1475 spa_version(dmu_objset_spa(zsb->z_os)))
1476 return (ENOTSUP);
1477
1478 tx = dmu_tx_create(os);
1479 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
1480 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
1481 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1482 ZFS_SA_ATTRS);
1483 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1484 }
1485 error = dmu_tx_assign(tx, TXG_WAIT);
1486 if (error) {
1487 dmu_tx_abort(tx);
1488 return (error);
1489 }
1490
1491 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1492 8, 1, &newvers, tx);
1493
1494 if (error) {
1495 dmu_tx_commit(tx);
1496 return (error);
1497 }
1498
1499 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
1500 uint64_t sa_obj;
1501
1502 ASSERT3U(spa_version(dmu_objset_spa(zsb->z_os)), >=,
1503 SPA_VERSION_SA);
1504 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1505 DMU_OT_NONE, 0, tx);
1506
1507 error = zap_add(os, MASTER_NODE_OBJ,
1508 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1509 ASSERT3U(error, ==, 0);
1510
1511 VERIFY(0 == sa_set_sa_object(os, sa_obj));
1512 sa_register_update_callback(os, zfs_sa_upgrade);
1513 }
1514
1515 spa_history_log_internal(LOG_DS_UPGRADE,
1516 dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu",
1517 zsb->z_version, newvers, dmu_objset_id(os));
1518
1519 dmu_tx_commit(tx);
1520
1521 zsb->z_version = newvers;
1522
1523 if (zsb->z_version >= ZPL_VERSION_FUID)
1524 zfs_set_fuid_feature(zsb);
1525
1526 return (0);
1527 }
1528 EXPORT_SYMBOL(zfs_set_version);
1529
1530 /*
1531 * Read a property stored within the master node.
1532 */
1533 int
1534 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1535 {
1536 const char *pname;
1537 int error = ENOENT;
1538
1539 /*
1540 * Look up the file system's value for the property. For the
1541 * version property, we look up a slightly different string.
1542 */
1543 if (prop == ZFS_PROP_VERSION)
1544 pname = ZPL_VERSION_STR;
1545 else
1546 pname = zfs_prop_to_name(prop);
1547
1548 if (os != NULL)
1549 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1550
1551 if (error == ENOENT) {
1552 /* No value set, use the default value */
1553 switch (prop) {
1554 case ZFS_PROP_VERSION:
1555 *value = ZPL_VERSION;
1556 break;
1557 case ZFS_PROP_NORMALIZE:
1558 case ZFS_PROP_UTF8ONLY:
1559 *value = 0;
1560 break;
1561 case ZFS_PROP_CASE:
1562 *value = ZFS_CASE_SENSITIVE;
1563 break;
1564 default:
1565 return (error);
1566 }
1567 error = 0;
1568 }
1569 return (error);
1570 }
1571
1572 void
1573 zfs_init(void)
1574 {
1575 zfs_znode_init();
1576 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
1577 register_filesystem(&zpl_fs_type);
1578 }
1579
1580 void
1581 zfs_fini(void)
1582 {
1583 unregister_filesystem(&zpl_fs_type);
1584 zfs_znode_fini();
1585 }