]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_vfsops.c
Illumos 5027 - zfs large block support
[mirror_zfs.git] / module / zfs / zfs_vfsops.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2e528b49 23 * Copyright (c) 2013 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
428870ff
BB
26/* Portions Copyright 2010 Robert Milkowski */
27
34dc7c2f
BB
28#include <sys/types.h>
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/sysmacros.h>
32#include <sys/kmem.h>
33#include <sys/pathname.h>
34#include <sys/vnode.h>
35#include <sys/vfs.h>
36#include <sys/vfs_opreg.h>
37#include <sys/mntent.h>
38#include <sys/mount.h>
39#include <sys/cmn_err.h>
40#include "fs/fs_subr.h"
41#include <sys/zfs_znode.h>
3558fd73 42#include <sys/zfs_vnops.h>
34dc7c2f
BB
43#include <sys/zfs_dir.h>
44#include <sys/zil.h>
45#include <sys/fs/zfs.h>
46#include <sys/dmu.h>
47#include <sys/dsl_prop.h>
48#include <sys/dsl_dataset.h>
49#include <sys/dsl_deleg.h>
50#include <sys/spa.h>
51#include <sys/zap.h>
428870ff 52#include <sys/sa.h>
a94addd9 53#include <sys/sa_impl.h>
34dc7c2f
BB
54#include <sys/varargs.h>
55#include <sys/policy.h>
56#include <sys/atomic.h>
57#include <sys/mkdev.h>
58#include <sys/modctl.h>
59#include <sys/refstr.h>
60#include <sys/zfs_ioctl.h>
ebe7e575 61#include <sys/zfs_ctldir.h>
34dc7c2f
BB
62#include <sys/zfs_fuid.h>
63#include <sys/bootconf.h>
64#include <sys/sunddi.h>
65#include <sys/dnlc.h>
66#include <sys/dmu_objset.h>
67#include <sys/spa_boot.h>
3558fd73 68#include <sys/zpl.h>
428870ff 69#include "zfs_comutil.h"
34dc7c2f 70
9babb374 71
34dc7c2f
BB
72/*ARGSUSED*/
73int
03f9ba9d 74zfs_sync(struct super_block *sb, int wait, cred_t *cr)
34dc7c2f 75{
03f9ba9d
BB
76 zfs_sb_t *zsb = sb->s_fs_info;
77
34dc7c2f
BB
78 /*
79 * Data integrity is job one. We don't want a compromised kernel
80 * writing to the storage pool, so we never sync during panic.
81 */
d5e53f9d 82 if (unlikely(oops_in_progress))
34dc7c2f
BB
83 return (0);
84
03f9ba9d
BB
85 /*
86 * Semantically, the only requirement is that the sync be initiated.
87 * The DMU syncs out txgs frequently, so there's nothing to do.
88 */
89 if (!wait)
90 return (0);
91
3558fd73 92 if (zsb != NULL) {
34dc7c2f
BB
93 /*
94 * Sync a specific filesystem.
95 */
9babb374 96 dsl_pool_t *dp;
34dc7c2f 97
3558fd73
BB
98 ZFS_ENTER(zsb);
99 dp = dmu_objset_pool(zsb->z_os);
9babb374
BB
100
101 /*
102 * If the system is shutting down, then skip any
103 * filesystems which may exist on a suspended pool.
104 */
03f9ba9d 105 if (spa_suspended(dp->dp_spa)) {
3558fd73 106 ZFS_EXIT(zsb);
9babb374
BB
107 return (0);
108 }
109
3558fd73
BB
110 if (zsb->z_log != NULL)
111 zil_commit(zsb->z_log, 0);
428870ff 112
3558fd73 113 ZFS_EXIT(zsb);
34dc7c2f
BB
114 } else {
115 /*
116 * Sync all ZFS filesystems. This is what happens when you
117 * run sync(1M). Unlike other filesystems, ZFS honors the
118 * request by waiting for all pools to commit all dirty data.
119 */
120 spa_sync_allpools();
121 }
122
123 return (0);
124}
e5c39b95 125EXPORT_SYMBOL(zfs_sync);
34dc7c2f 126
2cf7f52b
BB
127boolean_t
128zfs_is_readonly(zfs_sb_t *zsb)
129{
130 return (!!(zsb->z_sb->s_flags & MS_RDONLY));
131}
132EXPORT_SYMBOL(zfs_is_readonly);
133
34dc7c2f
BB
134static void
135atime_changed_cb(void *arg, uint64_t newval)
136{
2cf7f52b 137 ((zfs_sb_t *)arg)->z_atime = newval;
34dc7c2f
BB
138}
139
6d111134
TC
140static void
141relatime_changed_cb(void *arg, uint64_t newval)
142{
143 ((zfs_sb_t *)arg)->z_relatime = newval;
144}
145
34dc7c2f
BB
146static void
147xattr_changed_cb(void *arg, uint64_t newval)
148{
3558fd73 149 zfs_sb_t *zsb = arg;
34dc7c2f 150
82a37189 151 if (newval == ZFS_XATTR_OFF) {
2cf7f52b 152 zsb->z_flags &= ~ZSB_XATTR;
82a37189
BB
153 } else {
154 zsb->z_flags |= ZSB_XATTR;
155
156 if (newval == ZFS_XATTR_SA)
157 zsb->z_xattr_sa = B_TRUE;
158 else
159 zsb->z_xattr_sa = B_FALSE;
160 }
34dc7c2f
BB
161}
162
023699cd
MM
163static void
164acltype_changed_cb(void *arg, uint64_t newval)
165{
166 zfs_sb_t *zsb = arg;
167
168 switch (newval) {
169 case ZFS_ACLTYPE_OFF:
170 zsb->z_acl_type = ZFS_ACLTYPE_OFF;
171 zsb->z_sb->s_flags &= ~MS_POSIXACL;
172 break;
173 case ZFS_ACLTYPE_POSIXACL:
b695c34e 174#ifdef CONFIG_FS_POSIX_ACL
023699cd
MM
175 zsb->z_acl_type = ZFS_ACLTYPE_POSIXACL;
176 zsb->z_sb->s_flags |= MS_POSIXACL;
b695c34e
MM
177#else
178 zsb->z_acl_type = ZFS_ACLTYPE_OFF;
179 zsb->z_sb->s_flags &= ~MS_POSIXACL;
180#endif /* CONFIG_FS_POSIX_ACL */
023699cd
MM
181 break;
182 default:
183 break;
184 }
185}
186
34dc7c2f
BB
187static void
188blksz_changed_cb(void *arg, uint64_t newval)
189{
3558fd73 190 zfs_sb_t *zsb = arg;
f1512ee6
MA
191 ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zsb->z_os)));
192 ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
193 ASSERT(ISP2(newval));
34dc7c2f 194
3558fd73 195 zsb->z_max_blksz = newval;
34dc7c2f
BB
196}
197
198static void
199readonly_changed_cb(void *arg, uint64_t newval)
200{
3558fd73
BB
201 zfs_sb_t *zsb = arg;
202 struct super_block *sb = zsb->z_sb;
34dc7c2f 203
2cf7f52b
BB
204 if (sb == NULL)
205 return;
206
207 if (newval)
3558fd73 208 sb->s_flags |= MS_RDONLY;
2cf7f52b 209 else
3558fd73 210 sb->s_flags &= ~MS_RDONLY;
34dc7c2f
BB
211}
212
213static void
214devices_changed_cb(void *arg, uint64_t newval)
215{
34dc7c2f
BB
216}
217
218static void
219setuid_changed_cb(void *arg, uint64_t newval)
220{
34dc7c2f
BB
221}
222
223static void
224exec_changed_cb(void *arg, uint64_t newval)
225{
34dc7c2f
BB
226}
227
34dc7c2f
BB
228static void
229nbmand_changed_cb(void *arg, uint64_t newval)
230{
3558fd73
BB
231 zfs_sb_t *zsb = arg;
232 struct super_block *sb = zsb->z_sb;
233
2cf7f52b
BB
234 if (sb == NULL)
235 return;
236
237 if (newval == TRUE)
3558fd73 238 sb->s_flags |= MS_MANDLOCK;
2cf7f52b 239 else
3558fd73 240 sb->s_flags &= ~MS_MANDLOCK;
34dc7c2f
BB
241}
242
243static void
244snapdir_changed_cb(void *arg, uint64_t newval)
245{
3558fd73 246 ((zfs_sb_t *)arg)->z_show_ctldir = newval;
34dc7c2f
BB
247}
248
249static void
250vscan_changed_cb(void *arg, uint64_t newval)
251{
3558fd73 252 ((zfs_sb_t *)arg)->z_vscan = newval;
34dc7c2f
BB
253}
254
34dc7c2f
BB
255static void
256acl_inherit_changed_cb(void *arg, uint64_t newval)
257{
3558fd73 258 ((zfs_sb_t *)arg)->z_acl_inherit = newval;
34dc7c2f
BB
259}
260
e5c39b95 261int
3558fd73 262zfs_register_callbacks(zfs_sb_t *zsb)
34dc7c2f
BB
263{
264 struct dsl_dataset *ds = NULL;
3558fd73 265 objset_t *os = zsb->z_os;
c9ada6d5 266 boolean_t do_readonly = B_FALSE;
34dc7c2f
BB
267 int error = 0;
268
2cf7f52b 269 if (zfs_is_readonly(zsb) || !spa_writeable(dmu_objset_spa(os)))
c9ada6d5 270 do_readonly = B_TRUE;
34dc7c2f
BB
271
272 /*
273 * Register property callbacks.
274 *
275 * It would probably be fine to just check for i/o error from
276 * the first prop_register(), but I guess I like to go
277 * overboard...
278 */
279 ds = dmu_objset_ds(os);
13fe0198 280 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
3558fd73 281 error = dsl_prop_register(ds,
13fe0198 282 zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zsb);
98fad862 283 error = error ? error : dsl_prop_register(ds,
6d111134 284 zfs_prop_to_name(ZFS_PROP_RELATIME), relatime_changed_cb, zsb);
34dc7c2f 285 error = error ? error : dsl_prop_register(ds,
13fe0198 286 zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zsb);
34dc7c2f 287 error = error ? error : dsl_prop_register(ds,
13fe0198 288 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zsb);
34dc7c2f 289 error = error ? error : dsl_prop_register(ds,
13fe0198 290 zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zsb);
34dc7c2f 291 error = error ? error : dsl_prop_register(ds,
13fe0198 292 zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zsb);
34dc7c2f 293 error = error ? error : dsl_prop_register(ds,
13fe0198 294 zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zsb);
34dc7c2f 295 error = error ? error : dsl_prop_register(ds,
13fe0198 296 zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zsb);
34dc7c2f 297 error = error ? error : dsl_prop_register(ds,
13fe0198 298 zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zsb);
34dc7c2f 299 error = error ? error : dsl_prop_register(ds,
023699cd
MM
300 zfs_prop_to_name(ZFS_PROP_ACLTYPE), acltype_changed_cb, zsb);
301 error = error ? error : dsl_prop_register(ds,
302 zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb, zsb);
34dc7c2f 303 error = error ? error : dsl_prop_register(ds,
13fe0198 304 zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zsb);
2cf7f52b 305 error = error ? error : dsl_prop_register(ds,
13fe0198
MA
306 zfs_prop_to_name(ZFS_PROP_NBMAND), nbmand_changed_cb, zsb);
307 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
34dc7c2f
BB
308 if (error)
309 goto unregister;
310
c9ada6d5
BB
311 if (do_readonly)
312 readonly_changed_cb(zsb, B_TRUE);
313
34dc7c2f
BB
314 return (0);
315
316unregister:
317 /*
318 * We may attempt to unregister some callbacks that are not
319 * registered, but this is OK; it will simply return ENOMSG,
320 * which we will ignore.
321 */
13fe0198
MA
322 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ATIME),
323 atime_changed_cb, zsb);
6d111134
TC
324 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_RELATIME),
325 relatime_changed_cb, zsb);
13fe0198
MA
326 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_XATTR),
327 xattr_changed_cb, zsb);
328 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
329 blksz_changed_cb, zsb);
330 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_READONLY),
331 readonly_changed_cb, zsb);
332 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_DEVICES),
333 devices_changed_cb, zsb);
334 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_SETUID),
335 setuid_changed_cb, zsb);
336 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_EXEC),
337 exec_changed_cb, zsb);
338 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_SNAPDIR),
339 snapdir_changed_cb, zsb);
023699cd
MM
340 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ACLTYPE),
341 acltype_changed_cb, zsb);
13fe0198
MA
342 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ACLINHERIT),
343 acl_inherit_changed_cb, zsb);
344 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_VSCAN),
345 vscan_changed_cb, zsb);
346 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_NBMAND),
347 nbmand_changed_cb, zsb);
34dc7c2f 348
3558fd73 349 return (error);
34dc7c2f 350}
e5c39b95 351EXPORT_SYMBOL(zfs_register_callbacks);
34dc7c2f 352
428870ff
BB
353static int
354zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
355 uint64_t *userp, uint64_t *groupp)
9babb374 356{
428870ff
BB
357 /*
358 * Is it a valid type of object to track?
359 */
360 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
2e528b49 361 return (SET_ERROR(ENOENT));
9babb374 362
428870ff
BB
363 /*
364 * If we have a NULL data pointer
365 * then assume the id's aren't changing and
366 * return EEXIST to the dmu to let it know to
367 * use the same ids
368 */
369 if (data == NULL)
2e528b49 370 return (SET_ERROR(EEXIST));
9babb374 371
428870ff 372 if (bonustype == DMU_OT_ZNODE) {
a94addd9 373 znode_phys_t *znp = data;
428870ff
BB
374 *userp = znp->zp_uid;
375 *groupp = znp->zp_gid;
9babb374 376 } else {
428870ff 377 int hdrsize;
a94addd9
MA
378 sa_hdr_phys_t *sap = data;
379 sa_hdr_phys_t sa = *sap;
380 boolean_t swap = B_FALSE;
9babb374 381
428870ff 382 ASSERT(bonustype == DMU_OT_SA);
428870ff 383
a94addd9 384 if (sa.sa_magic == 0) {
428870ff
BB
385 /*
386 * This should only happen for newly created
387 * files that haven't had the znode data filled
388 * in yet.
389 */
390 *userp = 0;
391 *groupp = 0;
a94addd9
MA
392 return (0);
393 }
394 if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
395 sa.sa_magic = SA_MAGIC;
396 sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
397 swap = B_TRUE;
398 } else {
399 VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
400 }
401
402 hdrsize = sa_hdrsize(&sa);
403 VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
404 *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
405 SA_UID_OFFSET));
406 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
407 SA_GID_OFFSET));
408 if (swap) {
409 *userp = BSWAP_64(*userp);
410 *groupp = BSWAP_64(*groupp);
428870ff 411 }
9babb374 412 }
13fe0198 413 return (0);
9babb374
BB
414}
415
416static void
3558fd73 417fuidstr_to_sid(zfs_sb_t *zsb, const char *fuidstr,
9babb374
BB
418 char *domainbuf, int buflen, uid_t *ridp)
419{
9babb374
BB
420 uint64_t fuid;
421 const char *domain;
422
423 fuid = strtonum(fuidstr, NULL);
424
3558fd73 425 domain = zfs_fuid_find_by_idx(zsb, FUID_INDEX(fuid));
9babb374
BB
426 if (domain)
427 (void) strlcpy(domainbuf, domain, buflen);
428 else
429 domainbuf[0] = '\0';
430 *ridp = FUID_RID(fuid);
431}
432
433static uint64_t
3558fd73 434zfs_userquota_prop_to_obj(zfs_sb_t *zsb, zfs_userquota_prop_t type)
9babb374
BB
435{
436 switch (type) {
437 case ZFS_PROP_USERUSED:
438 return (DMU_USERUSED_OBJECT);
439 case ZFS_PROP_GROUPUSED:
440 return (DMU_GROUPUSED_OBJECT);
441 case ZFS_PROP_USERQUOTA:
3558fd73 442 return (zsb->z_userquota_obj);
9babb374 443 case ZFS_PROP_GROUPQUOTA:
3558fd73 444 return (zsb->z_groupquota_obj);
149e873a 445 default:
2e528b49 446 return (SET_ERROR(ENOTSUP));
9babb374
BB
447 }
448 return (0);
449}
450
451int
3558fd73 452zfs_userspace_many(zfs_sb_t *zsb, zfs_userquota_prop_t type,
9babb374
BB
453 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
454{
455 int error;
456 zap_cursor_t zc;
457 zap_attribute_t za;
458 zfs_useracct_t *buf = vbuf;
459 uint64_t obj;
460
3558fd73 461 if (!dmu_objset_userspace_present(zsb->z_os))
2e528b49 462 return (SET_ERROR(ENOTSUP));
9babb374 463
3558fd73 464 obj = zfs_userquota_prop_to_obj(zsb, type);
9babb374
BB
465 if (obj == 0) {
466 *bufsizep = 0;
467 return (0);
468 }
469
3558fd73 470 for (zap_cursor_init_serialized(&zc, zsb->z_os, obj, *cookiep);
9babb374
BB
471 (error = zap_cursor_retrieve(&zc, &za)) == 0;
472 zap_cursor_advance(&zc)) {
473 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
474 *bufsizep)
475 break;
476
3558fd73 477 fuidstr_to_sid(zsb, za.za_name,
9babb374
BB
478 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
479
480 buf->zu_space = za.za_first_integer;
481 buf++;
482 }
483 if (error == ENOENT)
484 error = 0;
485
486 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
487 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
488 *cookiep = zap_cursor_serialize(&zc);
489 zap_cursor_fini(&zc);
490 return (error);
491}
e5c39b95 492EXPORT_SYMBOL(zfs_userspace_many);
9babb374
BB
493
494/*
495 * buf must be big enough (eg, 32 bytes)
496 */
497static int
3558fd73 498id_to_fuidstr(zfs_sb_t *zsb, const char *domain, uid_t rid,
9babb374
BB
499 char *buf, boolean_t addok)
500{
501 uint64_t fuid;
502 int domainid = 0;
503
504 if (domain && domain[0]) {
3558fd73 505 domainid = zfs_fuid_find_by_domain(zsb, domain, NULL, addok);
9babb374 506 if (domainid == -1)
2e528b49 507 return (SET_ERROR(ENOENT));
9babb374
BB
508 }
509 fuid = FUID_ENCODE(domainid, rid);
510 (void) sprintf(buf, "%llx", (longlong_t)fuid);
511 return (0);
512}
513
514int
3558fd73 515zfs_userspace_one(zfs_sb_t *zsb, zfs_userquota_prop_t type,
9babb374
BB
516 const char *domain, uint64_t rid, uint64_t *valp)
517{
518 char buf[32];
519 int err;
520 uint64_t obj;
521
522 *valp = 0;
523
3558fd73 524 if (!dmu_objset_userspace_present(zsb->z_os))
2e528b49 525 return (SET_ERROR(ENOTSUP));
9babb374 526
3558fd73 527 obj = zfs_userquota_prop_to_obj(zsb, type);
9babb374
BB
528 if (obj == 0)
529 return (0);
530
3558fd73 531 err = id_to_fuidstr(zsb, domain, rid, buf, B_FALSE);
9babb374
BB
532 if (err)
533 return (err);
534
3558fd73 535 err = zap_lookup(zsb->z_os, obj, buf, 8, 1, valp);
9babb374
BB
536 if (err == ENOENT)
537 err = 0;
538 return (err);
539}
e5c39b95 540EXPORT_SYMBOL(zfs_userspace_one);
9babb374
BB
541
542int
3558fd73 543zfs_set_userquota(zfs_sb_t *zsb, zfs_userquota_prop_t type,
9babb374
BB
544 const char *domain, uint64_t rid, uint64_t quota)
545{
546 char buf[32];
547 int err;
548 dmu_tx_t *tx;
549 uint64_t *objp;
550 boolean_t fuid_dirtied;
551
552 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
2e528b49 553 return (SET_ERROR(EINVAL));
9babb374 554
3558fd73 555 if (zsb->z_version < ZPL_VERSION_USERSPACE)
2e528b49 556 return (SET_ERROR(ENOTSUP));
9babb374 557
3558fd73
BB
558 objp = (type == ZFS_PROP_USERQUOTA) ? &zsb->z_userquota_obj :
559 &zsb->z_groupquota_obj;
9babb374 560
3558fd73 561 err = id_to_fuidstr(zsb, domain, rid, buf, B_TRUE);
9babb374
BB
562 if (err)
563 return (err);
3558fd73 564 fuid_dirtied = zsb->z_fuid_dirty;
9babb374 565
3558fd73 566 tx = dmu_tx_create(zsb->z_os);
9babb374
BB
567 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
568 if (*objp == 0) {
569 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
570 zfs_userquota_prop_prefixes[type]);
571 }
572 if (fuid_dirtied)
3558fd73 573 zfs_fuid_txhold(zsb, tx);
9babb374
BB
574 err = dmu_tx_assign(tx, TXG_WAIT);
575 if (err) {
576 dmu_tx_abort(tx);
577 return (err);
578 }
579
3558fd73 580 mutex_enter(&zsb->z_lock);
9babb374 581 if (*objp == 0) {
3558fd73 582 *objp = zap_create(zsb->z_os, DMU_OT_USERGROUP_QUOTA,
9babb374 583 DMU_OT_NONE, 0, tx);
3558fd73 584 VERIFY(0 == zap_add(zsb->z_os, MASTER_NODE_OBJ,
9babb374
BB
585 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
586 }
3558fd73 587 mutex_exit(&zsb->z_lock);
9babb374
BB
588
589 if (quota == 0) {
3558fd73 590 err = zap_remove(zsb->z_os, *objp, buf, tx);
9babb374
BB
591 if (err == ENOENT)
592 err = 0;
593 } else {
3558fd73 594 err = zap_update(zsb->z_os, *objp, buf, 8, 1, &quota, tx);
9babb374
BB
595 }
596 ASSERT(err == 0);
597 if (fuid_dirtied)
3558fd73 598 zfs_fuid_sync(zsb, tx);
9babb374
BB
599 dmu_tx_commit(tx);
600 return (err);
601}
e5c39b95 602EXPORT_SYMBOL(zfs_set_userquota);
9babb374
BB
603
604boolean_t
3558fd73 605zfs_fuid_overquota(zfs_sb_t *zsb, boolean_t isgroup, uint64_t fuid)
9babb374
BB
606{
607 char buf[32];
608 uint64_t used, quota, usedobj, quotaobj;
609 int err;
610
611 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
3558fd73 612 quotaobj = isgroup ? zsb->z_groupquota_obj : zsb->z_userquota_obj;
9babb374 613
3558fd73 614 if (quotaobj == 0 || zsb->z_replay)
9babb374
BB
615 return (B_FALSE);
616
617 (void) sprintf(buf, "%llx", (longlong_t)fuid);
3558fd73 618 err = zap_lookup(zsb->z_os, quotaobj, buf, 8, 1, &quota);
9babb374
BB
619 if (err != 0)
620 return (B_FALSE);
621
3558fd73 622 err = zap_lookup(zsb->z_os, usedobj, buf, 8, 1, &used);
9babb374
BB
623 if (err != 0)
624 return (B_FALSE);
625 return (used >= quota);
626}
e5c39b95 627EXPORT_SYMBOL(zfs_fuid_overquota);
9babb374 628
428870ff 629boolean_t
3558fd73 630zfs_owner_overquota(zfs_sb_t *zsb, znode_t *zp, boolean_t isgroup)
428870ff
BB
631{
632 uint64_t fuid;
633 uint64_t quotaobj;
428870ff 634
3558fd73 635 quotaobj = isgroup ? zsb->z_groupquota_obj : zsb->z_userquota_obj;
428870ff 636
572e2857 637 fuid = isgroup ? zp->z_gid : zp->z_uid;
428870ff 638
3558fd73 639 if (quotaobj == 0 || zsb->z_replay)
428870ff
BB
640 return (B_FALSE);
641
3558fd73 642 return (zfs_fuid_overquota(zsb, isgroup, fuid));
428870ff 643}
e5c39b95 644EXPORT_SYMBOL(zfs_owner_overquota);
428870ff 645
9babb374 646int
3558fd73 647zfs_sb_create(const char *osname, zfs_sb_t **zsbp)
9babb374
BB
648{
649 objset_t *os;
3558fd73 650 zfs_sb_t *zsb;
9babb374
BB
651 uint64_t zval;
652 int i, error;
428870ff 653 uint64_t sa_obj;
9babb374 654
efcd79a8 655 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_SLEEP);
9babb374 656
428870ff
BB
657 /*
658 * We claim to always be readonly so we can open snapshots;
659 * other ZPL code will prevent us from writing to snapshots.
660 */
3558fd73 661 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zsb, &os);
428870ff 662 if (error) {
3558fd73 663 kmem_free(zsb, sizeof (zfs_sb_t));
9babb374 664 return (error);
428870ff 665 }
9babb374
BB
666
667 /*
668 * Initialize the zfs-specific filesystem structure.
669 * Should probably make this a kmem cache, shuffle fields,
670 * and just bzero up to z_hold_mtx[].
671 */
2cf7f52b 672 zsb->z_sb = NULL;
3558fd73 673 zsb->z_parent = zsb;
f1512ee6 674 zsb->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
3558fd73
BB
675 zsb->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
676 zsb->z_os = os;
9babb374 677
3558fd73 678 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zsb->z_version);
9babb374
BB
679 if (error) {
680 goto out;
287be44f 681 } else if (zsb->z_version > ZPL_VERSION) {
2e528b49 682 error = SET_ERROR(ENOTSUP);
9babb374
BB
683 goto out;
684 }
9babb374
BB
685 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
686 goto out;
3558fd73 687 zsb->z_norm = (int)zval;
9babb374
BB
688
689 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
690 goto out;
3558fd73 691 zsb->z_utf8 = (zval != 0);
9babb374
BB
692
693 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
694 goto out;
3558fd73 695 zsb->z_case = (uint_t)zval;
9babb374 696
023699cd
MM
697 if ((error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &zval)) != 0)
698 goto out;
699 zsb->z_acl_type = (uint_t)zval;
700
9babb374
BB
701 /*
702 * Fold case on file systems that are always or sometimes case
703 * insensitive.
704 */
3558fd73
BB
705 if (zsb->z_case == ZFS_CASE_INSENSITIVE ||
706 zsb->z_case == ZFS_CASE_MIXED)
707 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
9babb374 708
3558fd73
BB
709 zsb->z_use_fuids = USE_FUIDS(zsb->z_version, zsb->z_os);
710 zsb->z_use_sa = USE_SA(zsb->z_version, zsb->z_os);
428870ff 711
3558fd73 712 if (zsb->z_use_sa) {
428870ff
BB
713 /* should either have both of these objects or none */
714 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
715 &sa_obj);
716 if (error)
591fb62f 717 goto out;
82a37189
BB
718
719 error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &zval);
720 if ((error == 0) && (zval == ZFS_XATTR_SA))
721 zsb->z_xattr_sa = B_TRUE;
428870ff
BB
722 } else {
723 /*
724 * Pre SA versions file systems should never touch
725 * either the attribute registration or layout objects.
726 */
727 sa_obj = 0;
728 }
729
572e2857 730 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
3558fd73 731 &zsb->z_attr_table);
572e2857
BB
732 if (error)
733 goto out;
428870ff 734
3558fd73 735 if (zsb->z_version >= ZPL_VERSION_SA)
428870ff 736 sa_register_update_callback(os, zfs_sa_upgrade);
9babb374
BB
737
738 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
3558fd73 739 &zsb->z_root);
9babb374
BB
740 if (error)
741 goto out;
3558fd73 742 ASSERT(zsb->z_root != 0);
9babb374
BB
743
744 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
3558fd73 745 &zsb->z_unlinkedobj);
9babb374
BB
746 if (error)
747 goto out;
748
749 error = zap_lookup(os, MASTER_NODE_OBJ,
750 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
3558fd73 751 8, 1, &zsb->z_userquota_obj);
9babb374
BB
752 if (error && error != ENOENT)
753 goto out;
754
755 error = zap_lookup(os, MASTER_NODE_OBJ,
756 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
3558fd73 757 8, 1, &zsb->z_groupquota_obj);
9babb374
BB
758 if (error && error != ENOENT)
759 goto out;
760
761 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
3558fd73 762 &zsb->z_fuid_obj);
9babb374
BB
763 if (error && error != ENOENT)
764 goto out;
765
766 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
3558fd73 767 &zsb->z_shares_dir);
9babb374
BB
768 if (error && error != ENOENT)
769 goto out;
770
3558fd73
BB
771 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
772 mutex_init(&zsb->z_lock, NULL, MUTEX_DEFAULT, NULL);
773 list_create(&zsb->z_all_znodes, sizeof (znode_t),
9babb374 774 offsetof(znode_t, z_link_node));
13fe0198 775 rrw_init(&zsb->z_teardown_lock, B_FALSE);
3558fd73
BB
776 rw_init(&zsb->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
777 rw_init(&zsb->z_fuid_lock, NULL, RW_DEFAULT, NULL);
d07b7c7f
CD
778
779 zsb->z_hold_mtx = vmem_zalloc(sizeof (kmutex_t) * ZFS_OBJ_MTX_SZ,
780 KM_SLEEP);
9babb374 781 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
3558fd73 782 mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
9babb374 783
ebe7e575
BB
784 avl_create(&zsb->z_ctldir_snaps, snapentry_compare,
785 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
786 mutex_init(&zsb->z_ctldir_lock, NULL, MUTEX_DEFAULT, NULL);
787
3558fd73 788 *zsbp = zsb;
9babb374
BB
789 return (0);
790
791out:
3558fd73
BB
792 dmu_objset_disown(os, zsb);
793 *zsbp = NULL;
d07b7c7f
CD
794
795 vmem_free(zsb->z_hold_mtx, sizeof (kmutex_t) * ZFS_OBJ_MTX_SZ);
3558fd73 796 kmem_free(zsb, sizeof (zfs_sb_t));
9babb374
BB
797 return (error);
798}
86f35f34 799EXPORT_SYMBOL(zfs_sb_create);
9babb374 800
86f35f34 801int
3558fd73 802zfs_sb_setup(zfs_sb_t *zsb, boolean_t mounting)
34dc7c2f 803{
34dc7c2f
BB
804 int error;
805
3558fd73 806 error = zfs_register_callbacks(zsb);
34dc7c2f
BB
807 if (error)
808 return (error);
809
810 /*
3558fd73 811 * Set the objset user_ptr to track its zsb.
34dc7c2f 812 */
3558fd73
BB
813 mutex_enter(&zsb->z_os->os_user_ptr_lock);
814 dmu_objset_set_user(zsb->z_os, zsb);
815 mutex_exit(&zsb->z_os->os_user_ptr_lock);
34dc7c2f 816
3558fd73 817 zsb->z_log = zil_open(zsb->z_os, zfs_get_data);
9babb374 818
34dc7c2f
BB
819 /*
820 * If we are not mounting (ie: online recv), then we don't
821 * have to worry about replaying the log as we blocked all
822 * operations out since we closed the ZIL.
823 */
824 if (mounting) {
b128c09f
BB
825 boolean_t readonly;
826
34dc7c2f
BB
827 /*
828 * During replay we remove the read only flag to
829 * allow replays to succeed.
830 */
2cf7f52b 831 readonly = zfs_is_readonly(zsb);
fb5f0bc8 832 if (readonly != 0)
2cf7f52b 833 readonly_changed_cb(zsb, B_FALSE);
fb5f0bc8 834 else
3558fd73 835 zfs_unlinked_drain(zsb);
34dc7c2f 836
428870ff
BB
837 /*
838 * Parse and replay the intent log.
839 *
840 * Because of ziltest, this must be done after
841 * zfs_unlinked_drain(). (Further note: ziltest
842 * doesn't use readonly mounts, where
843 * zfs_unlinked_drain() isn't called.) This is because
844 * ziltest causes spa_sync() to think it's committed,
845 * but actually it is not, so the intent log contains
846 * many txg's worth of changes.
847 *
848 * In particular, if object N is in the unlinked set in
849 * the last txg to actually sync, then it could be
850 * actually freed in a later txg and then reallocated
851 * in a yet later txg. This would write a "create
852 * object N" record to the intent log. Normally, this
853 * would be fine because the spa_sync() would have
854 * written out the fact that object N is free, before
855 * we could write the "create object N" intent log
856 * record.
857 *
858 * But when we are in ziltest mode, we advance the "open
859 * txg" without actually spa_sync()-ing the changes to
860 * disk. So we would see that object N is still
861 * allocated and in the unlinked set, and there is an
862 * intent log record saying to allocate it.
863 */
3558fd73 864 if (spa_writeable(dmu_objset_spa(zsb->z_os))) {
572e2857 865 if (zil_replay_disable) {
3558fd73 866 zil_destroy(zsb->z_log, B_FALSE);
572e2857 867 } else {
3558fd73
BB
868 zsb->z_replay = B_TRUE;
869 zil_replay(zsb->z_os, zsb,
572e2857 870 zfs_replay_vector);
3558fd73 871 zsb->z_replay = B_FALSE;
572e2857 872 }
fb5f0bc8 873 }
2cf7f52b
BB
874
875 /* restore readonly bit */
876 if (readonly != 0)
877 readonly_changed_cb(zsb, B_TRUE);
34dc7c2f
BB
878 }
879
34dc7c2f
BB
880 return (0);
881}
86f35f34 882EXPORT_SYMBOL(zfs_sb_setup);
34dc7c2f 883
9babb374 884void
3558fd73 885zfs_sb_free(zfs_sb_t *zsb)
34dc7c2f 886{
9babb374 887 int i;
9babb374 888
3558fd73 889 zfs_fuid_destroy(zsb);
9babb374 890
3558fd73
BB
891 mutex_destroy(&zsb->z_znodes_lock);
892 mutex_destroy(&zsb->z_lock);
893 list_destroy(&zsb->z_all_znodes);
894 rrw_destroy(&zsb->z_teardown_lock);
895 rw_destroy(&zsb->z_teardown_inactive_lock);
896 rw_destroy(&zsb->z_fuid_lock);
9babb374 897 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
3558fd73 898 mutex_destroy(&zsb->z_hold_mtx[i]);
d07b7c7f 899 vmem_free(zsb->z_hold_mtx, sizeof (kmutex_t) * ZFS_OBJ_MTX_SZ);
ebe7e575
BB
900 mutex_destroy(&zsb->z_ctldir_lock);
901 avl_destroy(&zsb->z_ctldir_snaps);
3558fd73 902 kmem_free(zsb, sizeof (zfs_sb_t));
34dc7c2f 903}
86f35f34 904EXPORT_SYMBOL(zfs_sb_free);
34dc7c2f 905
9babb374 906static void
3558fd73 907zfs_set_fuid_feature(zfs_sb_t *zsb)
9babb374 908{
3558fd73
BB
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);
9babb374 911}
34dc7c2f
BB
912
913void
3558fd73 914zfs_unregister_callbacks(zfs_sb_t *zsb)
34dc7c2f 915{
3558fd73 916 objset_t *os = zsb->z_os;
34dc7c2f
BB
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,
3558fd73 925 zsb) == 0);
34dc7c2f 926
6d111134
TC
927 VERIFY(dsl_prop_unregister(ds, "relatime", relatime_changed_cb,
928 zsb) == 0);
929
34dc7c2f 930 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
3558fd73 931 zsb) == 0);
34dc7c2f
BB
932
933 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
3558fd73 934 zsb) == 0);
34dc7c2f
BB
935
936 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
3558fd73 937 zsb) == 0);
34dc7c2f
BB
938
939 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
3558fd73 940 zsb) == 0);
34dc7c2f
BB
941
942 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
3558fd73 943 zsb) == 0);
34dc7c2f
BB
944
945 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
3558fd73 946 zsb) == 0);
34dc7c2f
BB
947
948 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
3558fd73 949 zsb) == 0);
34dc7c2f 950
023699cd
MM
951 VERIFY(dsl_prop_unregister(ds, "acltype", acltype_changed_cb,
952 zsb) == 0);
953
34dc7c2f 954 VERIFY(dsl_prop_unregister(ds, "aclinherit",
3558fd73 955 acl_inherit_changed_cb, zsb) == 0);
34dc7c2f
BB
956
957 VERIFY(dsl_prop_unregister(ds, "vscan",
3558fd73 958 vscan_changed_cb, zsb) == 0);
2cf7f52b
BB
959
960 VERIFY(dsl_prop_unregister(ds, "nbmand",
961 nbmand_changed_cb, zsb) == 0);
34dc7c2f
BB
962 }
963}
e5c39b95 964EXPORT_SYMBOL(zfs_unregister_callbacks);
34dc7c2f 965
bc3e15e3 966#ifdef HAVE_MLSLABEL
428870ff 967/*
d3cc8b15
WA
968 * Check that the hex label string is appropriate for the dataset being
969 * mounted into the global_zone proper.
428870ff 970 *
d3cc8b15
WA
971 * Return an error if the hex label string is not default or
972 * admin_low/admin_high. For admin_low labels, the corresponding
973 * dataset must be readonly.
428870ff
BB
974 */
975int
976zfs_check_global_label(const char *dsname, const char *hexsl)
977{
978 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
979 return (0);
980 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
981 return (0);
982 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
983 /* must be readonly */
984 uint64_t rdonly;
985
986 if (dsl_prop_get_integer(dsname,
987 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
2e528b49 988 return (SET_ERROR(EACCES));
428870ff
BB
989 return (rdonly ? 0 : EACCES);
990 }
2e528b49 991 return (SET_ERROR(EACCES));
428870ff 992}
86f35f34 993EXPORT_SYMBOL(zfs_check_global_label);
bc3e15e3 994#endif /* HAVE_MLSLABEL */
428870ff 995
e5c39b95 996int
3558fd73 997zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
34dc7c2f 998{
3558fd73 999 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
34dc7c2f 1000 uint64_t refdbytes, availbytes, usedobjs, availobjs;
04f9432d 1001 uint64_t fsid;
3558fd73 1002 uint32_t bshift;
34dc7c2f 1003
3558fd73 1004 ZFS_ENTER(zsb);
34dc7c2f 1005
3558fd73 1006 dmu_objset_space(zsb->z_os,
34dc7c2f
BB
1007 &refdbytes, &availbytes, &usedobjs, &availobjs);
1008
04f9432d 1009 fsid = dmu_objset_fsid_guid(zsb->z_os);
34dc7c2f 1010 /*
05ff35c6
BB
1011 * The underlying storage pool actually uses multiple block
1012 * size. Under Solaris frsize (fragment size) is reported as
1013 * the smallest block size we support, and bsize (block size)
1014 * as the filesystem's maximum block size. Unfortunately,
1015 * under Linux the fragment size and block size are often used
1016 * interchangeably. Thus we are forced to report both of them
1017 * as the filesystem's maximum block size.
34dc7c2f 1018 */
05ff35c6 1019 statp->f_frsize = zsb->z_max_blksz;
3558fd73
BB
1020 statp->f_bsize = zsb->z_max_blksz;
1021 bshift = fls(statp->f_bsize) - 1;
34dc7c2f
BB
1022
1023 /*
3558fd73
BB
1024 * The following report "total" blocks of various kinds in
1025 * the file system, but reported in terms of f_bsize - the
1026 * "preferred" size.
34dc7c2f
BB
1027 */
1028
3558fd73
BB
1029 statp->f_blocks = (refdbytes + availbytes) >> bshift;
1030 statp->f_bfree = availbytes >> bshift;
34dc7c2f
BB
1031 statp->f_bavail = statp->f_bfree; /* no root reservation */
1032
1033 /*
1034 * statvfs() should really be called statufs(), because it assumes
1035 * static metadata. ZFS doesn't preallocate files, so the best
1036 * we can do is report the max that could possibly fit in f_files,
1037 * and that minus the number actually used in f_ffree.
1038 * For f_ffree, report the smaller of the number of object available
1039 * and the number of blocks (each object will take at least a block).
1040 */
baab0630 1041 statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
34dc7c2f 1042 statp->f_files = statp->f_ffree + usedobjs;
04f9432d
CP
1043 statp->f_fsid.val[0] = (uint32_t)fsid;
1044 statp->f_fsid.val[1] = (uint32_t)(fsid >> 32);
3558fd73
BB
1045 statp->f_type = ZFS_SUPER_MAGIC;
1046 statp->f_namelen = ZFS_MAXNAMELEN;
34dc7c2f
BB
1047
1048 /*
3558fd73 1049 * We have all of 40 characters to stuff a string here.
34dc7c2f
BB
1050 * Is there anything useful we could/should provide?
1051 */
3558fd73 1052 bzero(statp->f_spare, sizeof (statp->f_spare));
34dc7c2f 1053
3558fd73 1054 ZFS_EXIT(zsb);
34dc7c2f
BB
1055 return (0);
1056}
e5c39b95 1057EXPORT_SYMBOL(zfs_statvfs);
34dc7c2f 1058
e5c39b95 1059int
3558fd73 1060zfs_root(zfs_sb_t *zsb, struct inode **ipp)
34dc7c2f 1061{
34dc7c2f
BB
1062 znode_t *rootzp;
1063 int error;
1064
3558fd73 1065 ZFS_ENTER(zsb);
34dc7c2f 1066
3558fd73 1067 error = zfs_zget(zsb, zsb->z_root, &rootzp);
34dc7c2f 1068 if (error == 0)
3558fd73 1069 *ipp = ZTOI(rootzp);
34dc7c2f 1070
3558fd73 1071 ZFS_EXIT(zsb);
34dc7c2f
BB
1072 return (error);
1073}
e5c39b95 1074EXPORT_SYMBOL(zfs_root);
34dc7c2f 1075
2cbb06b5
BB
1076/*
1077 * The ARC has requested that the filesystem drop entries from the dentry
1078 * and inode caches. This can occur when the ARC needs to free meta data
1079 * blocks but can't because they are all pinned by entries in these caches.
1080 */
ab26409d
BB
1081int
1082zfs_sb_prune(struct super_block *sb, unsigned long nr_to_scan, int *objects)
1083{
1084 zfs_sb_t *zsb = sb->s_fs_info;
2cbb06b5
BB
1085 int error = 0;
1086#if defined(HAVE_SHRINK) || defined(HAVE_SPLIT_SHRINKER_CALLBACK)
ab26409d
BB
1087 struct shrinker *shrinker = &sb->s_shrink;
1088 struct shrink_control sc = {
1089 .nr_to_scan = nr_to_scan,
1090 .gfp_mask = GFP_KERNEL,
1091 };
2cbb06b5 1092#endif
ab26409d
BB
1093
1094 ZFS_ENTER(zsb);
2cbb06b5
BB
1095
1096#if defined(HAVE_SPLIT_SHRINKER_CALLBACK)
3c832b8c 1097 *objects = (*shrinker->scan_objects)(shrinker, &sc);
2cbb06b5 1098#elif defined(HAVE_SHRINK)
ab26409d 1099 *objects = (*shrinker->shrink)(shrinker, &sc);
2cbb06b5
BB
1100#else
1101 /*
1102 * Linux kernels older than 3.1 do not support a per-filesystem
1103 * shrinker. Therefore, we must fall back to the only available
1104 * interface which is to discard all unused dentries and inodes.
1105 * This behavior clearly isn't ideal but it's required so the ARC
1106 * may free memory. The performance impact is mitigated by the
1107 * fact that the frequently accessed dentry and inode buffers will
1108 * still be in the ARC making them relatively cheap to recreate.
1109 */
1110 *objects = 0;
1111 shrink_dcache_parent(sb->s_root);
3c832b8c 1112#endif
ab26409d
BB
1113 ZFS_EXIT(zsb);
1114
2cbb06b5
BB
1115 dprintf_ds(zsb->z_os->os_dsl_dataset,
1116 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1117 nr_to_scan, *objects, error);
1118
1119 return (error);
ab26409d
BB
1120}
1121EXPORT_SYMBOL(zfs_sb_prune);
ab26409d 1122
34dc7c2f 1123/*
7b3e34ba 1124 * Teardown the zfs_sb_t.
34dc7c2f
BB
1125 *
1126 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1127 * and 'z_teardown_inactive_lock' held.
1128 */
3558fd73 1129int
86f35f34 1130zfs_sb_teardown(zfs_sb_t *zsb, boolean_t unmounting)
34dc7c2f
BB
1131{
1132 znode_t *zp;
1133
fd23720a
BB
1134 /*
1135 * If someone has not already unmounted this file system,
1136 * drain the iput_taskq to ensure all active references to the
1137 * zfs_sb_t have been handled only then can it be safely destroyed.
1138 */
f0da4d15
CD
1139 if (zsb->z_os) {
1140 /*
1141 * If we're unmounting we have to wait for the list to
1142 * drain completely.
1143 *
1144 * If we're not unmounting there's no guarantee the list
1145 * will drain completely, but iputs run from the taskq
1146 * may add the parents of dir-based xattrs to the taskq
1147 * so we want to wait for these.
1148 *
1149 * We can safely read z_nr_znodes without locking because the
1150 * VFS has already blocked operations which add to the
1151 * z_all_znodes list and thus increment z_nr_znodes.
1152 */
1153 int round = 0;
1154 while (zsb->z_nr_znodes > 0) {
1155 taskq_wait(dsl_pool_iput_taskq(dmu_objset_pool(
1156 zsb->z_os)));
1157 if (++round > 1 && !unmounting)
1158 break;
1159 }
1160 }
fd23720a 1161
3558fd73 1162 rrw_enter(&zsb->z_teardown_lock, RW_WRITER, FTAG);
34dc7c2f
BB
1163
1164 if (!unmounting) {
1165 /*
ceb43b93
BB
1166 * We purge the parent filesystem's super block as the
1167 * parent filesystem and all of its snapshots have their
1168 * inode's super block set to the parent's filesystem's
1169 * super block. Note, 'z_parent' is self referential
1170 * for non-snapshots.
34dc7c2f 1171 */
ceb43b93 1172 shrink_dcache_sb(zsb->z_parent->z_sb);
34dc7c2f
BB
1173 }
1174
1175 /*
1176 * Close the zil. NB: Can't close the zil while zfs_inactive
1177 * threads are blocked as zil_close can call zfs_inactive.
1178 */
3558fd73
BB
1179 if (zsb->z_log) {
1180 zil_close(zsb->z_log);
1181 zsb->z_log = NULL;
34dc7c2f
BB
1182 }
1183
3558fd73 1184 rw_enter(&zsb->z_teardown_inactive_lock, RW_WRITER);
34dc7c2f
BB
1185
1186 /*
1187 * If we are not unmounting (ie: online recv) and someone already
1188 * unmounted this file system while we were doing the switcheroo,
1189 * or a reopen of z_os failed then just bail out now.
1190 */
3558fd73
BB
1191 if (!unmounting && (zsb->z_unmounted || zsb->z_os == NULL)) {
1192 rw_exit(&zsb->z_teardown_inactive_lock);
1193 rrw_exit(&zsb->z_teardown_lock, FTAG);
2e528b49 1194 return (SET_ERROR(EIO));
34dc7c2f
BB
1195 }
1196
1197 /*
7b3e34ba
BB
1198 * At this point there are no VFS ops active, and any new VFS ops
1199 * will fail with EIO since we have z_teardown_lock for writer (only
1200 * relevant for forced unmount).
34dc7c2f
BB
1201 *
1202 * Release all holds on dbufs.
1203 */
f0da4d15
CD
1204 if (!unmounting) {
1205 mutex_enter(&zsb->z_znodes_lock);
1206 for (zp = list_head(&zsb->z_all_znodes); zp != NULL;
1207 zp = list_next(&zsb->z_all_znodes, zp)) {
1208 if (zp->z_sa_hdl)
1209 zfs_znode_dmu_fini(zp);
1210 }
1211 mutex_exit(&zsb->z_znodes_lock);
7b3e34ba 1212 }
34dc7c2f
BB
1213
1214 /*
7b3e34ba 1215 * If we are unmounting, set the unmounted flag and let new VFS ops
34dc7c2f 1216 * unblock. zfs_inactive will have the unmounted behavior, and all
7b3e34ba 1217 * other VFS ops will fail with EIO.
34dc7c2f
BB
1218 */
1219 if (unmounting) {
3558fd73
BB
1220 zsb->z_unmounted = B_TRUE;
1221 rrw_exit(&zsb->z_teardown_lock, FTAG);
1222 rw_exit(&zsb->z_teardown_inactive_lock);
34dc7c2f
BB
1223 }
1224
1225 /*
1226 * z_os will be NULL if there was an error in attempting to reopen
3558fd73
BB
1227 * zsb, so just return as the properties had already been
1228 *
34dc7c2f
BB
1229 * unregistered and cached data had been evicted before.
1230 */
3558fd73 1231 if (zsb->z_os == NULL)
34dc7c2f
BB
1232 return (0);
1233
1234 /*
1235 * Unregister properties.
1236 */
3558fd73 1237 zfs_unregister_callbacks(zsb);
34dc7c2f
BB
1238
1239 /*
1240 * Evict cached data
1241 */
04434775
MA
1242 if (dsl_dataset_is_dirty(dmu_objset_ds(zsb->z_os)) &&
1243 !zfs_is_readonly(zsb))
1244 txg_wait_synced(dmu_objset_pool(zsb->z_os), 0);
13fe0198 1245 dmu_objset_evict_dbufs(zsb->z_os);
34dc7c2f
BB
1246
1247 return (0);
1248}
86f35f34 1249EXPORT_SYMBOL(zfs_sb_teardown);
34dc7c2f 1250
8c45def2
BB
1251#if !defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER) && \
1252 !defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
5547c2f1 1253atomic_long_t zfs_bdi_seq = ATOMIC_LONG_INIT(0);
8c45def2 1254#endif
76659dc1 1255
e5c39b95 1256int
3558fd73 1257zfs_domount(struct super_block *sb, void *data, int silent)
34dc7c2f 1258{
3558fd73
BB
1259 zpl_mount_data_t *zmd = data;
1260 const char *osname = zmd->z_osname;
1261 zfs_sb_t *zsb;
1262 struct inode *root_inode;
1263 uint64_t recordsize;
1264 int error;
34dc7c2f 1265
3558fd73
BB
1266 error = zfs_sb_create(osname, &zsb);
1267 if (error)
1268 return (error);
1269
1270 if ((error = dsl_prop_get_integer(osname, "recordsize",
1271 &recordsize, NULL)))
1272 goto out;
1273
1274 zsb->z_sb = sb;
3558fd73
BB
1275 sb->s_fs_info = zsb;
1276 sb->s_magic = ZFS_SUPER_MAGIC;
1277 sb->s_maxbytes = MAX_LFS_FILESIZE;
1278 sb->s_time_gran = 1;
1279 sb->s_blocksize = recordsize;
1280 sb->s_blocksize_bits = ilog2(recordsize);
5547c2f1
BB
1281 zsb->z_bdi.ra_pages = 0;
1282 sb->s_bdi = &zsb->z_bdi;
1283
8c45def2 1284 error = -zpl_bdi_setup_and_register(&zsb->z_bdi, "zfs");
5547c2f1
BB
1285 if (error)
1286 goto out;
3558fd73
BB
1287
1288 /* Set callback operations for the file system. */
1289 sb->s_op = &zpl_super_operations;
1290 sb->s_xattr = zpl_xattr_handlers;
055656d4 1291 sb->s_export_op = &zpl_export_operations;
ee930353
BB
1292#ifdef HAVE_S_D_OP
1293 sb->s_d_op = &zpl_dentry_operations;
1294#endif /* HAVE_S_D_OP */
3558fd73
BB
1295
1296 /* Set features for file system. */
1297 zfs_set_fuid_feature(zsb);
1298
1299 if (dmu_objset_is_snapshot(zsb->z_os)) {
1300 uint64_t pval;
1301
1302 atime_changed_cb(zsb, B_FALSE);
1303 readonly_changed_cb(zsb, B_TRUE);
d1d7e268
MK
1304 if ((error = dsl_prop_get_integer(osname,
1305 "xattr", &pval, NULL)))
3558fd73
BB
1306 goto out;
1307 xattr_changed_cb(zsb, pval);
d1d7e268
MK
1308 if ((error = dsl_prop_get_integer(osname,
1309 "acltype", &pval, NULL)))
023699cd
MM
1310 goto out;
1311 acltype_changed_cb(zsb, pval);
3558fd73
BB
1312 zsb->z_issnap = B_TRUE;
1313 zsb->z_os->os_sync = ZFS_SYNC_DISABLED;
1314
1315 mutex_enter(&zsb->z_os->os_user_ptr_lock);
1316 dmu_objset_set_user(zsb->z_os, zsb);
1317 mutex_exit(&zsb->z_os->os_user_ptr_lock);
1318 } else {
1319 error = zfs_sb_setup(zsb, B_TRUE);
34dc7c2f
BB
1320 }
1321
3558fd73
BB
1322 /* Allocate a root inode for the filesystem. */
1323 error = zfs_root(zsb, &root_inode);
1324 if (error) {
1325 (void) zfs_umount(sb);
1326 goto out;
34dc7c2f
BB
1327 }
1328
3558fd73 1329 /* Allocate a root dentry for the filesystem */
6a0936ba 1330 sb->s_root = d_make_root(root_inode);
3558fd73
BB
1331 if (sb->s_root == NULL) {
1332 (void) zfs_umount(sb);
2e528b49 1333 error = SET_ERROR(ENOMEM);
3558fd73
BB
1334 goto out;
1335 }
ebe7e575
BB
1336
1337 if (!zsb->z_issnap)
1338 zfsctl_create(zsb);
2cbb06b5
BB
1339
1340 zsb->z_arc_prune = arc_add_prune_callback(zpl_prune_sb, sb);
3558fd73
BB
1341out:
1342 if (error) {
1343 dmu_objset_disown(zsb->z_os, zsb);
1344 zfs_sb_free(zsb);
1345 }
34dc7c2f 1346
3558fd73
BB
1347 return (error);
1348}
1349EXPORT_SYMBOL(zfs_domount);
1350
ebe7e575
BB
1351/*
1352 * Called when an unmount is requested and certain sanity checks have
1353 * already passed. At this point no dentries or inodes have been reclaimed
1354 * from their respective caches. We drop the extra reference on the .zfs
1355 * control directory to allow everything to be reclaimed. All snapshots
1356 * must already have been unmounted to reach this point.
1357 */
1358void
1359zfs_preumount(struct super_block *sb)
1360{
1361 zfs_sb_t *zsb = sb->s_fs_info;
1362
1f0d8a56 1363 if (zsb != NULL && zsb->z_ctldir != NULL)
ebe7e575
BB
1364 zfsctl_destroy(zsb);
1365}
1366EXPORT_SYMBOL(zfs_preumount);
1367
1368/*
1369 * Called once all other unmount released tear down has occurred.
1370 * It is our responsibility to release any remaining infrastructure.
1371 */
3558fd73
BB
1372/*ARGSUSED*/
1373int
1374zfs_umount(struct super_block *sb)
1375{
1376 zfs_sb_t *zsb = sb->s_fs_info;
1377 objset_t *os;
1378
2cbb06b5 1379 arc_remove_prune_callback(zsb->z_arc_prune);
86f35f34 1380 VERIFY(zfs_sb_teardown(zsb, B_TRUE) == 0);
3558fd73 1381 os = zsb->z_os;
5547c2f1 1382 bdi_destroy(sb->s_bdi);
76659dc1 1383
34dc7c2f
BB
1384 /*
1385 * z_os will be NULL if there was an error in
3558fd73 1386 * attempting to reopen zsb.
34dc7c2f
BB
1387 */
1388 if (os != NULL) {
1389 /*
1390 * Unset the objset user_ptr.
1391 */
428870ff 1392 mutex_enter(&os->os_user_ptr_lock);
34dc7c2f 1393 dmu_objset_set_user(os, NULL);
428870ff 1394 mutex_exit(&os->os_user_ptr_lock);
34dc7c2f
BB
1395
1396 /*
b128c09f 1397 * Finally release the objset
34dc7c2f 1398 */
3558fd73 1399 dmu_objset_disown(os, zsb);
34dc7c2f
BB
1400 }
1401
3558fd73 1402 zfs_sb_free(zsb);
34dc7c2f
BB
1403 return (0);
1404}
e5c39b95 1405EXPORT_SYMBOL(zfs_umount);
34dc7c2f 1406
0de19dad
BB
1407int
1408zfs_remount(struct super_block *sb, int *flags, char *data)
1409{
0de19dad 1410 /*
2cf7f52b
BB
1411 * All namespace flags (MNT_*) and super block flags (MS_*) will
1412 * be handled by the Linux VFS. Only handle custom options here.
0de19dad 1413 */
0de19dad
BB
1414 return (0);
1415}
1416EXPORT_SYMBOL(zfs_remount);
1417
e5c39b95 1418int
2cf7f52b 1419zfs_vget(struct super_block *sb, struct inode **ipp, fid_t *fidp)
34dc7c2f 1420{
2cf7f52b 1421 zfs_sb_t *zsb = sb->s_fs_info;
34dc7c2f
BB
1422 znode_t *zp;
1423 uint64_t object = 0;
1424 uint64_t fid_gen = 0;
1425 uint64_t gen_mask;
1426 uint64_t zp_gen;
3558fd73 1427 int i, err;
34dc7c2f 1428
3558fd73 1429 *ipp = NULL;
34dc7c2f 1430
3558fd73 1431 ZFS_ENTER(zsb);
34dc7c2f
BB
1432
1433 if (fidp->fid_len == LONG_FID_LEN) {
1434 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1435 uint64_t objsetid = 0;
1436 uint64_t setgen = 0;
1437
1438 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1439 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1440
1441 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1442 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1443
3558fd73 1444 ZFS_EXIT(zsb);
34dc7c2f 1445
ebe7e575 1446 err = zfsctl_lookup_objset(sb, objsetid, &zsb);
34dc7c2f 1447 if (err)
2e528b49 1448 return (SET_ERROR(EINVAL));
ebe7e575 1449
3558fd73 1450 ZFS_ENTER(zsb);
34dc7c2f
BB
1451 }
1452
1453 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1454 zfid_short_t *zfid = (zfid_short_t *)fidp;
1455
1456 for (i = 0; i < sizeof (zfid->zf_object); i++)
1457 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1458
1459 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1460 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1461 } else {
3558fd73 1462 ZFS_EXIT(zsb);
2e528b49 1463 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1464 }
1465
1466 /* A zero fid_gen means we are in the .zfs control directories */
1467 if (fid_gen == 0 &&
1468 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
3558fd73
BB
1469 *ipp = zsb->z_ctldir;
1470 ASSERT(*ipp != NULL);
34dc7c2f 1471 if (object == ZFSCTL_INO_SNAPDIR) {
ebe7e575
BB
1472 VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp,
1473 0, kcred, NULL, NULL) == 0);
34dc7c2f 1474 } else {
3558fd73 1475 igrab(*ipp);
34dc7c2f 1476 }
3558fd73 1477 ZFS_EXIT(zsb);
34dc7c2f
BB
1478 return (0);
1479 }
1480
1481 gen_mask = -1ULL >> (64 - 8 * i);
1482
29e57d15 1483 dprintf("getting %llu [%llu mask %llx]\n", object, fid_gen, gen_mask);
3558fd73
BB
1484 if ((err = zfs_zget(zsb, object, &zp))) {
1485 ZFS_EXIT(zsb);
34dc7c2f
BB
1486 return (err);
1487 }
3558fd73 1488 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb), &zp_gen,
428870ff
BB
1489 sizeof (uint64_t));
1490 zp_gen = zp_gen & gen_mask;
34dc7c2f
BB
1491 if (zp_gen == 0)
1492 zp_gen = 1;
1493 if (zp->z_unlinked || zp_gen != fid_gen) {
29e57d15
NB
1494 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen,
1495 fid_gen);
3558fd73
BB
1496 iput(ZTOI(zp));
1497 ZFS_EXIT(zsb);
2e528b49 1498 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1499 }
1500
3558fd73
BB
1501 *ipp = ZTOI(zp);
1502 if (*ipp)
1503 zfs_inode_update(ITOZ(*ipp));
960e08fe 1504
3558fd73 1505 ZFS_EXIT(zsb);
34dc7c2f
BB
1506 return (0);
1507}
e5c39b95 1508EXPORT_SYMBOL(zfs_vget);
34dc7c2f
BB
1509
1510/*
7b3e34ba 1511 * Block out VFS ops and close zfs_sb_t
34dc7c2f
BB
1512 *
1513 * Note, if successful, then we return with the 'z_teardown_lock' and
831baf06
KW
1514 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1515 * dataset and objset intact so that they can be atomically handed off during
1516 * a subsequent rollback or recv operation and the resume thereafter.
34dc7c2f
BB
1517 */
1518int
3558fd73 1519zfs_suspend_fs(zfs_sb_t *zsb)
34dc7c2f
BB
1520{
1521 int error;
1522
86f35f34 1523 if ((error = zfs_sb_teardown(zsb, B_FALSE)) != 0)
34dc7c2f 1524 return (error);
7b3e34ba 1525
34dc7c2f
BB
1526 return (0);
1527}
e5c39b95 1528EXPORT_SYMBOL(zfs_suspend_fs);
34dc7c2f
BB
1529
1530/*
7b3e34ba 1531 * Reopen zfs_sb_t and release VFS ops.
34dc7c2f
BB
1532 */
1533int
3558fd73 1534zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
34dc7c2f 1535{
2008e920 1536 int err, err2;
831baf06
KW
1537 znode_t *zp;
1538 uint64_t sa_obj = 0;
34dc7c2f 1539
3558fd73
BB
1540 ASSERT(RRW_WRITE_HELD(&zsb->z_teardown_lock));
1541 ASSERT(RW_WRITE_HELD(&zsb->z_teardown_inactive_lock));
34dc7c2f 1542
831baf06
KW
1543 /*
1544 * We already own this, so just hold and rele it to update the
1545 * objset_t, as the one we had before may have been evicted.
1546 */
1547 VERIFY0(dmu_objset_hold(osname, zsb, &zsb->z_os));
1548 VERIFY3P(zsb->z_os->os_dsl_dataset->ds_owner, ==, zsb);
1549 VERIFY(dsl_dataset_long_held(zsb->z_os->os_dsl_dataset));
1550 dmu_objset_rele(zsb->z_os, zsb);
428870ff 1551
831baf06
KW
1552 /*
1553 * Make sure version hasn't changed
1554 */
428870ff 1555
831baf06
KW
1556 err = zfs_get_zplprop(zsb->z_os, ZFS_PROP_VERSION,
1557 &zsb->z_version);
c1fabe79 1558
831baf06
KW
1559 if (err)
1560 goto bail;
428870ff 1561
831baf06
KW
1562 err = zap_lookup(zsb->z_os, MASTER_NODE_OBJ,
1563 ZFS_SA_ATTRS, 8, 1, &sa_obj);
c1fabe79 1564
831baf06
KW
1565 if (err && zsb->z_version >= ZPL_VERSION_SA)
1566 goto bail;
428870ff 1567
831baf06
KW
1568 if ((err = sa_setup(zsb->z_os, sa_obj,
1569 zfs_attr_table, ZPL_END, &zsb->z_attr_table)) != 0)
1570 goto bail;
34dc7c2f 1571
831baf06
KW
1572 if (zsb->z_version >= ZPL_VERSION_SA)
1573 sa_register_update_callback(zsb->z_os,
1574 zfs_sa_upgrade);
c1fabe79 1575
831baf06 1576 VERIFY(zfs_sb_setup(zsb, B_FALSE) == 0);
c1fabe79 1577
831baf06
KW
1578 zfs_set_fuid_feature(zsb);
1579 zsb->z_rollback_time = jiffies;
34dc7c2f 1580
831baf06
KW
1581 /*
1582 * Attempt to re-establish all the active inodes with their
1583 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1584 * and mark it stale. This prevents a collision if a new
1585 * inode/object is created which must use the same inode
1586 * number. The stale inode will be be released when the
1587 * VFS prunes the dentry holding the remaining references
1588 * on the stale inode.
1589 */
1590 mutex_enter(&zsb->z_znodes_lock);
1591 for (zp = list_head(&zsb->z_all_znodes); zp;
1592 zp = list_next(&zsb->z_all_znodes, zp)) {
2008e920
TC
1593 err2 = zfs_rezget(zp);
1594 if (err2) {
831baf06
KW
1595 remove_inode_hash(ZTOI(zp));
1596 zp->z_is_stale = B_TRUE;
34dc7c2f 1597 }
34dc7c2f 1598 }
831baf06 1599 mutex_exit(&zsb->z_znodes_lock);
34dc7c2f 1600
428870ff 1601bail:
7b3e34ba 1602 /* release the VFS ops */
3558fd73
BB
1603 rw_exit(&zsb->z_teardown_inactive_lock);
1604 rrw_exit(&zsb->z_teardown_lock, FTAG);
34dc7c2f
BB
1605
1606 if (err) {
1607 /*
831baf06
KW
1608 * Since we couldn't setup the sa framework, try to force
1609 * unmount this file system.
34dc7c2f 1610 */
ab68b6e5
TC
1611 if (zsb->z_os)
1612 (void) zfs_umount(zsb->z_sb);
34dc7c2f
BB
1613 }
1614 return (err);
1615}
e5c39b95 1616EXPORT_SYMBOL(zfs_resume_fs);
34dc7c2f 1617
34dc7c2f 1618int
3558fd73 1619zfs_set_version(zfs_sb_t *zsb, uint64_t newvers)
34dc7c2f
BB
1620{
1621 int error;
3558fd73 1622 objset_t *os = zsb->z_os;
34dc7c2f 1623 dmu_tx_t *tx;
34dc7c2f
BB
1624
1625 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2e528b49 1626 return (SET_ERROR(EINVAL));
34dc7c2f 1627
3558fd73 1628 if (newvers < zsb->z_version)
2e528b49 1629 return (SET_ERROR(EINVAL));
34dc7c2f 1630
428870ff 1631 if (zfs_spa_version_map(newvers) >
3558fd73 1632 spa_version(dmu_objset_spa(zsb->z_os)))
2e528b49 1633 return (SET_ERROR(ENOTSUP));
428870ff 1634
34dc7c2f 1635 tx = dmu_tx_create(os);
9babb374 1636 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
3558fd73 1637 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
428870ff
BB
1638 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1639 ZFS_SA_ATTRS);
1640 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1641 }
34dc7c2f
BB
1642 error = dmu_tx_assign(tx, TXG_WAIT);
1643 if (error) {
1644 dmu_tx_abort(tx);
9babb374
BB
1645 return (error);
1646 }
428870ff 1647
9babb374
BB
1648 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1649 8, 1, &newvers, tx);
1650
1651 if (error) {
1652 dmu_tx_commit(tx);
1653 return (error);
34dc7c2f 1654 }
34dc7c2f 1655
3558fd73 1656 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
428870ff
BB
1657 uint64_t sa_obj;
1658
3558fd73 1659 ASSERT3U(spa_version(dmu_objset_spa(zsb->z_os)), >=,
428870ff
BB
1660 SPA_VERSION_SA);
1661 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1662 DMU_OT_NONE, 0, tx);
1663
1664 error = zap_add(os, MASTER_NODE_OBJ,
1665 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
c99c9001 1666 ASSERT0(error);
428870ff
BB
1667
1668 VERIFY(0 == sa_set_sa_object(os, sa_obj));
1669 sa_register_update_callback(os, zfs_sa_upgrade);
1670 }
1671
6f1ffb06
MA
1672 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
1673 "from %llu to %llu", zsb->z_version, newvers);
9babb374 1674
34dc7c2f
BB
1675 dmu_tx_commit(tx);
1676
3558fd73 1677 zsb->z_version = newvers;
9babb374 1678
c1fabe79 1679 zfs_set_fuid_feature(zsb);
9babb374
BB
1680
1681 return (0);
34dc7c2f 1682}
e5c39b95 1683EXPORT_SYMBOL(zfs_set_version);
34dc7c2f
BB
1684
1685/*
1686 * Read a property stored within the master node.
1687 */
1688int
1689zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1690{
1691 const char *pname;
2e528b49 1692 int error = SET_ERROR(ENOENT);
34dc7c2f
BB
1693
1694 /*
1695 * Look up the file system's value for the property. For the
1696 * version property, we look up a slightly different string.
1697 */
1698 if (prop == ZFS_PROP_VERSION)
1699 pname = ZPL_VERSION_STR;
1700 else
1701 pname = zfs_prop_to_name(prop);
1702
b128c09f
BB
1703 if (os != NULL)
1704 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
34dc7c2f
BB
1705
1706 if (error == ENOENT) {
1707 /* No value set, use the default value */
1708 switch (prop) {
1709 case ZFS_PROP_VERSION:
1710 *value = ZPL_VERSION;
1711 break;
1712 case ZFS_PROP_NORMALIZE:
1713 case ZFS_PROP_UTF8ONLY:
1714 *value = 0;
1715 break;
1716 case ZFS_PROP_CASE:
1717 *value = ZFS_CASE_SENSITIVE;
1718 break;
023699cd
MM
1719 case ZFS_PROP_ACLTYPE:
1720 *value = ZFS_ACLTYPE_OFF;
1721 break;
34dc7c2f
BB
1722 default:
1723 return (error);
1724 }
1725 error = 0;
1726 }
1727 return (error);
1728}
86f35f34 1729EXPORT_SYMBOL(zfs_get_zplprop);
3558fd73
BB
1730
1731void
1732zfs_init(void)
1733{
ebe7e575 1734 zfsctl_init();
3558fd73
BB
1735 zfs_znode_init();
1736 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
1737 register_filesystem(&zpl_fs_type);
1738}
1739
1740void
1741zfs_fini(void)
1742{
023699cd 1743 taskq_wait(system_taskq);
3558fd73
BB
1744 unregister_filesystem(&zpl_fs_type);
1745 zfs_znode_fini();
ebe7e575 1746 zfsctl_fini();
3558fd73 1747}