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