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