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