]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zfs_vfsops.c
New upstream version 0.6.5.10
[mirror_zfs-debian.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.
a08ee875 23 * Copyright (c) 2013 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
428870ff
BB
26/* Portions Copyright 2010 Robert Milkowski */
27
34dc7c2f
BB
28#include <sys/types.h>
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/sysmacros.h>
32#include <sys/kmem.h>
33#include <sys/pathname.h>
34#include <sys/vnode.h>
35#include <sys/vfs.h>
36#include <sys/vfs_opreg.h>
37#include <sys/mntent.h>
38#include <sys/mount.h>
39#include <sys/cmn_err.h>
40#include "fs/fs_subr.h"
41#include <sys/zfs_znode.h>
3558fd73 42#include <sys/zfs_vnops.h>
34dc7c2f
BB
43#include <sys/zfs_dir.h>
44#include <sys/zil.h>
45#include <sys/fs/zfs.h>
46#include <sys/dmu.h>
47#include <sys/dsl_prop.h>
48#include <sys/dsl_dataset.h>
49#include <sys/dsl_deleg.h>
50#include <sys/spa.h>
51#include <sys/zap.h>
428870ff 52#include <sys/sa.h>
a94addd9 53#include <sys/sa_impl.h>
34dc7c2f
BB
54#include <sys/varargs.h>
55#include <sys/policy.h>
56#include <sys/atomic.h>
57#include <sys/mkdev.h>
58#include <sys/modctl.h>
59#include <sys/refstr.h>
60#include <sys/zfs_ioctl.h>
ebe7e575 61#include <sys/zfs_ctldir.h>
34dc7c2f
BB
62#include <sys/zfs_fuid.h>
63#include <sys/bootconf.h>
64#include <sys/sunddi.h>
65#include <sys/dnlc.h>
66#include <sys/dmu_objset.h>
67#include <sys/spa_boot.h>
3558fd73 68#include <sys/zpl.h>
428870ff 69#include "zfs_comutil.h"
34dc7c2f 70
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
a08ee875
LG
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
a08ee875
LG
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:
173#ifdef CONFIG_FS_POSIX_ACL
174 zsb->z_acl_type = ZFS_ACLTYPE_POSIXACL;
175 zsb->z_sb->s_flags |= MS_POSIXACL;
176#else
177 zsb->z_acl_type = ZFS_ACLTYPE_OFF;
178 zsb->z_sb->s_flags &= ~MS_POSIXACL;
179#endif /* CONFIG_FS_POSIX_ACL */
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;
e10b0808
AX
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;
e10b0808 265 zfs_mntopts_t *zmo = zsb->z_mntopts;
34dc7c2f
BB
266 int error = 0;
267
e10b0808
AX
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);
a08ee875
LG
290 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
291 error = dsl_prop_register(ds,
292 zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zsb);
ea04106b 293 error = error ? error : dsl_prop_register(ds,
a08ee875 294 zfs_prop_to_name(ZFS_PROP_RELATIME), relatime_changed_cb, zsb);
34dc7c2f 295 error = error ? error : dsl_prop_register(ds,
a08ee875 296 zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zsb);
34dc7c2f 297 error = error ? error : dsl_prop_register(ds,
a08ee875 298 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zsb);
34dc7c2f 299 error = error ? error : dsl_prop_register(ds,
a08ee875 300 zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zsb);
34dc7c2f 301 error = error ? error : dsl_prop_register(ds,
a08ee875 302 zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zsb);
34dc7c2f 303 error = error ? error : dsl_prop_register(ds,
a08ee875 304 zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zsb);
34dc7c2f 305 error = error ? error : dsl_prop_register(ds,
a08ee875 306 zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zsb);
34dc7c2f 307 error = error ? error : dsl_prop_register(ds,
a08ee875 308 zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zsb);
34dc7c2f 309 error = error ? error : dsl_prop_register(ds,
a08ee875 310 zfs_prop_to_name(ZFS_PROP_ACLTYPE), acltype_changed_cb, zsb);
34dc7c2f 311 error = error ? error : dsl_prop_register(ds,
a08ee875 312 zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb, zsb);
2cf7f52b 313 error = error ? error : dsl_prop_register(ds,
a08ee875
LG
314 zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zsb);
315 error = error ? error : dsl_prop_register(ds,
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
e10b0808
AX
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);
c06d4368 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 */
a08ee875
LG
349 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ATIME),
350 atime_changed_cb, zsb);
351 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_RELATIME),
352 relatime_changed_cb, zsb);
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);
367 (void) dsl_prop_unregister(ds, zfs_prop_to_name(ZFS_PROP_ACLTYPE),
368 acltype_changed_cb, zsb);
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)
a08ee875 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)
a08ee875 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 }
a08ee875 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:
a08ee875 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))
a08ee875 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)
a08ee875 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))
a08ee875 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)
a08ee875 580 return (SET_ERROR(EINVAL));
9babb374 581
3558fd73 582 if (zsb->z_version < ZPL_VERSION_USERSPACE)
a08ee875 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
e10b0808
AX
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
e10b0808 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 696 uint64_t zval;
4d815aed 697 int i, size, error;
428870ff 698 uint64_t sa_obj;
9babb374 699
ea04106b 700 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_SLEEP);
9babb374 701
428870ff 702 /*
68d83c55 703 * Optional temporary mount options, free'd in zfs_sb_free().
428870ff 704 */
68d83c55 705 zsb->z_mntopts = (zmo ? zmo : zfs_mntopts_alloc());
9babb374 706
e10b0808 707 /*
68d83c55
AX
708 * We claim to always be readonly so we can open snapshots;
709 * other ZPL code will prevent us from writing to snapshots.
e10b0808 710 */
68d83c55
AX
711 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zsb, &os);
712 if (error)
713 goto out_zmo;
e10b0808 714
9babb374
BB
715 /*
716 * Initialize the zfs-specific filesystem structure.
4d815aed 717 * Should probably make this a kmem cache, shuffle fields.
9babb374 718 */
2cf7f52b 719 zsb->z_sb = NULL;
3558fd73 720 zsb->z_parent = zsb;
e10b0808 721 zsb->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
3558fd73
BB
722 zsb->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
723 zsb->z_os = os;
9babb374 724
3558fd73 725 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zsb->z_version);
9babb374
BB
726 if (error) {
727 goto out;
ea04106b 728 } else if (zsb->z_version > ZPL_VERSION) {
a08ee875 729 error = SET_ERROR(ENOTSUP);
9babb374
BB
730 goto out;
731 }
9babb374
BB
732 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
733 goto out;
3558fd73 734 zsb->z_norm = (int)zval;
9babb374
BB
735
736 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
737 goto out;
3558fd73 738 zsb->z_utf8 = (zval != 0);
9babb374
BB
739
740 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
741 goto out;
3558fd73 742 zsb->z_case = (uint_t)zval;
9babb374 743
a08ee875
LG
744 if ((error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &zval)) != 0)
745 goto out;
746 zsb->z_acl_type = (uint_t)zval;
747
9babb374
BB
748 /*
749 * Fold case on file systems that are always or sometimes case
750 * insensitive.
751 */
3558fd73
BB
752 if (zsb->z_case == ZFS_CASE_INSENSITIVE ||
753 zsb->z_case == ZFS_CASE_MIXED)
754 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
9babb374 755
3558fd73
BB
756 zsb->z_use_fuids = USE_FUIDS(zsb->z_version, zsb->z_os);
757 zsb->z_use_sa = USE_SA(zsb->z_version, zsb->z_os);
428870ff 758
3558fd73 759 if (zsb->z_use_sa) {
428870ff
BB
760 /* should either have both of these objects or none */
761 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
762 &sa_obj);
763 if (error)
591fb62f 764 goto out;
82a37189
BB
765
766 error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &zval);
767 if ((error == 0) && (zval == ZFS_XATTR_SA))
768 zsb->z_xattr_sa = B_TRUE;
428870ff
BB
769 } else {
770 /*
771 * Pre SA versions file systems should never touch
772 * either the attribute registration or layout objects.
773 */
774 sa_obj = 0;
775 }
776
572e2857 777 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
3558fd73 778 &zsb->z_attr_table);
572e2857
BB
779 if (error)
780 goto out;
428870ff 781
3558fd73 782 if (zsb->z_version >= ZPL_VERSION_SA)
428870ff 783 sa_register_update_callback(os, zfs_sa_upgrade);
9babb374
BB
784
785 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
3558fd73 786 &zsb->z_root);
9babb374
BB
787 if (error)
788 goto out;
3558fd73 789 ASSERT(zsb->z_root != 0);
9babb374
BB
790
791 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
3558fd73 792 &zsb->z_unlinkedobj);
9babb374
BB
793 if (error)
794 goto out;
795
796 error = zap_lookup(os, MASTER_NODE_OBJ,
797 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
3558fd73 798 8, 1, &zsb->z_userquota_obj);
9babb374
BB
799 if (error && error != ENOENT)
800 goto out;
801
802 error = zap_lookup(os, MASTER_NODE_OBJ,
803 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
3558fd73 804 8, 1, &zsb->z_groupquota_obj);
9babb374
BB
805 if (error && error != ENOENT)
806 goto out;
807
808 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
3558fd73 809 &zsb->z_fuid_obj);
9babb374
BB
810 if (error && error != ENOENT)
811 goto out;
812
813 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
3558fd73 814 &zsb->z_shares_dir);
9babb374
BB
815 if (error && error != ENOENT)
816 goto out;
817
3558fd73
BB
818 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
819 mutex_init(&zsb->z_lock, NULL, MUTEX_DEFAULT, NULL);
820 list_create(&zsb->z_all_znodes, sizeof (znode_t),
9babb374 821 offsetof(znode_t, z_link_node));
e10b0808 822 rrm_init(&zsb->z_teardown_lock, B_FALSE);
3558fd73
BB
823 rw_init(&zsb->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
824 rw_init(&zsb->z_fuid_lock, NULL, RW_DEFAULT, NULL);
ea04106b 825
4d815aed
AX
826 size = MIN(1 << (highbit64(zfs_object_mutex_size)-1), ZFS_OBJ_MTX_MAX);
827 zsb->z_hold_size = size;
828 zsb->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size, KM_SLEEP);
829 zsb->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
830 for (i = 0; i != size; i++) {
831 avl_create(&zsb->z_hold_trees[i], zfs_znode_hold_compare,
832 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
833 mutex_init(&zsb->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
834 }
9babb374 835
3558fd73 836 *zsbp = zsb;
9babb374
BB
837 return (0);
838
839out:
3558fd73 840 dmu_objset_disown(os, zsb);
68d83c55 841out_zmo:
3558fd73 842 *zsbp = NULL;
68d83c55 843 zfs_mntopts_free(zsb->z_mntopts);
3558fd73 844 kmem_free(zsb, sizeof (zfs_sb_t));
9babb374
BB
845 return (error);
846}
86f35f34 847EXPORT_SYMBOL(zfs_sb_create);
9babb374 848
86f35f34 849int
3558fd73 850zfs_sb_setup(zfs_sb_t *zsb, boolean_t mounting)
34dc7c2f 851{
34dc7c2f
BB
852 int error;
853
3558fd73 854 error = zfs_register_callbacks(zsb);
34dc7c2f
BB
855 if (error)
856 return (error);
857
858 /*
3558fd73 859 * Set the objset user_ptr to track its zsb.
34dc7c2f 860 */
3558fd73
BB
861 mutex_enter(&zsb->z_os->os_user_ptr_lock);
862 dmu_objset_set_user(zsb->z_os, zsb);
863 mutex_exit(&zsb->z_os->os_user_ptr_lock);
34dc7c2f 864
3558fd73 865 zsb->z_log = zil_open(zsb->z_os, zfs_get_data);
9babb374 866
34dc7c2f
BB
867 /*
868 * If we are not mounting (ie: online recv), then we don't
869 * have to worry about replaying the log as we blocked all
870 * operations out since we closed the ZIL.
871 */
872 if (mounting) {
b128c09f
BB
873 boolean_t readonly;
874
34dc7c2f
BB
875 /*
876 * During replay we remove the read only flag to
877 * allow replays to succeed.
878 */
2cf7f52b 879 readonly = zfs_is_readonly(zsb);
fb5f0bc8 880 if (readonly != 0)
2cf7f52b 881 readonly_changed_cb(zsb, B_FALSE);
fb5f0bc8 882 else
3558fd73 883 zfs_unlinked_drain(zsb);
34dc7c2f 884
428870ff
BB
885 /*
886 * Parse and replay the intent log.
887 *
888 * Because of ziltest, this must be done after
889 * zfs_unlinked_drain(). (Further note: ziltest
890 * doesn't use readonly mounts, where
891 * zfs_unlinked_drain() isn't called.) This is because
892 * ziltest causes spa_sync() to think it's committed,
893 * but actually it is not, so the intent log contains
894 * many txg's worth of changes.
895 *
896 * In particular, if object N is in the unlinked set in
897 * the last txg to actually sync, then it could be
898 * actually freed in a later txg and then reallocated
899 * in a yet later txg. This would write a "create
900 * object N" record to the intent log. Normally, this
901 * would be fine because the spa_sync() would have
902 * written out the fact that object N is free, before
903 * we could write the "create object N" intent log
904 * record.
905 *
906 * But when we are in ziltest mode, we advance the "open
907 * txg" without actually spa_sync()-ing the changes to
908 * disk. So we would see that object N is still
909 * allocated and in the unlinked set, and there is an
910 * intent log record saying to allocate it.
911 */
3558fd73 912 if (spa_writeable(dmu_objset_spa(zsb->z_os))) {
572e2857 913 if (zil_replay_disable) {
3558fd73 914 zil_destroy(zsb->z_log, B_FALSE);
572e2857 915 } else {
3558fd73
BB
916 zsb->z_replay = B_TRUE;
917 zil_replay(zsb->z_os, zsb,
572e2857 918 zfs_replay_vector);
3558fd73 919 zsb->z_replay = B_FALSE;
572e2857 920 }
fb5f0bc8 921 }
2cf7f52b
BB
922
923 /* restore readonly bit */
924 if (readonly != 0)
925 readonly_changed_cb(zsb, B_TRUE);
34dc7c2f
BB
926 }
927
34dc7c2f
BB
928 return (0);
929}
86f35f34 930EXPORT_SYMBOL(zfs_sb_setup);
34dc7c2f 931
9babb374 932void
3558fd73 933zfs_sb_free(zfs_sb_t *zsb)
34dc7c2f 934{
4d815aed 935 int i, size = zsb->z_hold_size;
9babb374 936
3558fd73 937 zfs_fuid_destroy(zsb);
9babb374 938
3558fd73
BB
939 mutex_destroy(&zsb->z_znodes_lock);
940 mutex_destroy(&zsb->z_lock);
941 list_destroy(&zsb->z_all_znodes);
e10b0808 942 rrm_destroy(&zsb->z_teardown_lock);
3558fd73
BB
943 rw_destroy(&zsb->z_teardown_inactive_lock);
944 rw_destroy(&zsb->z_fuid_lock);
4d815aed
AX
945 for (i = 0; i != size; i++) {
946 avl_destroy(&zsb->z_hold_trees[i]);
947 mutex_destroy(&zsb->z_hold_locks[i]);
948 }
949 vmem_free(zsb->z_hold_trees, sizeof (avl_tree_t) * size);
950 vmem_free(zsb->z_hold_locks, sizeof (kmutex_t) * size);
e10b0808 951 zfs_mntopts_free(zsb->z_mntopts);
3558fd73 952 kmem_free(zsb, sizeof (zfs_sb_t));
34dc7c2f 953}
86f35f34 954EXPORT_SYMBOL(zfs_sb_free);
34dc7c2f 955
9babb374 956static void
3558fd73 957zfs_set_fuid_feature(zfs_sb_t *zsb)
9babb374 958{
3558fd73
BB
959 zsb->z_use_fuids = USE_FUIDS(zsb->z_version, zsb->z_os);
960 zsb->z_use_sa = USE_SA(zsb->z_version, zsb->z_os);
9babb374 961}
34dc7c2f
BB
962
963void
3558fd73 964zfs_unregister_callbacks(zfs_sb_t *zsb)
34dc7c2f 965{
3558fd73 966 objset_t *os = zsb->z_os;
34dc7c2f
BB
967 struct dsl_dataset *ds;
968
969 /*
970 * Unregister properties.
971 */
972 if (!dmu_objset_is_snapshot(os)) {
973 ds = dmu_objset_ds(os);
974 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
3558fd73 975 zsb) == 0);
34dc7c2f 976
a08ee875
LG
977 VERIFY(dsl_prop_unregister(ds, "relatime", relatime_changed_cb,
978 zsb) == 0);
979
34dc7c2f 980 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
3558fd73 981 zsb) == 0);
34dc7c2f
BB
982
983 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
3558fd73 984 zsb) == 0);
34dc7c2f
BB
985
986 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
3558fd73 987 zsb) == 0);
34dc7c2f
BB
988
989 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
3558fd73 990 zsb) == 0);
34dc7c2f
BB
991
992 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
3558fd73 993 zsb) == 0);
34dc7c2f
BB
994
995 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
3558fd73 996 zsb) == 0);
34dc7c2f
BB
997
998 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
3558fd73 999 zsb) == 0);
34dc7c2f 1000
a08ee875
LG
1001 VERIFY(dsl_prop_unregister(ds, "acltype", acltype_changed_cb,
1002 zsb) == 0);
1003
34dc7c2f 1004 VERIFY(dsl_prop_unregister(ds, "aclinherit",
3558fd73 1005 acl_inherit_changed_cb, zsb) == 0);
34dc7c2f
BB
1006
1007 VERIFY(dsl_prop_unregister(ds, "vscan",
3558fd73 1008 vscan_changed_cb, zsb) == 0);
2cf7f52b
BB
1009
1010 VERIFY(dsl_prop_unregister(ds, "nbmand",
1011 nbmand_changed_cb, zsb) == 0);
34dc7c2f
BB
1012 }
1013}
e5c39b95 1014EXPORT_SYMBOL(zfs_unregister_callbacks);
34dc7c2f 1015
bc3e15e3 1016#ifdef HAVE_MLSLABEL
428870ff 1017/*
a08ee875
LG
1018 * Check that the hex label string is appropriate for the dataset being
1019 * mounted into the global_zone proper.
428870ff 1020 *
a08ee875
LG
1021 * Return an error if the hex label string is not default or
1022 * admin_low/admin_high. For admin_low labels, the corresponding
1023 * dataset must be readonly.
428870ff
BB
1024 */
1025int
1026zfs_check_global_label(const char *dsname, const char *hexsl)
1027{
1028 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1029 return (0);
1030 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1031 return (0);
1032 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1033 /* must be readonly */
1034 uint64_t rdonly;
1035
1036 if (dsl_prop_get_integer(dsname,
1037 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
a08ee875 1038 return (SET_ERROR(EACCES));
428870ff
BB
1039 return (rdonly ? 0 : EACCES);
1040 }
a08ee875 1041 return (SET_ERROR(EACCES));
428870ff 1042}
86f35f34 1043EXPORT_SYMBOL(zfs_check_global_label);
bc3e15e3 1044#endif /* HAVE_MLSLABEL */
428870ff 1045
e5c39b95 1046int
3558fd73 1047zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
34dc7c2f 1048{
3558fd73 1049 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
34dc7c2f 1050 uint64_t refdbytes, availbytes, usedobjs, availobjs;
04f9432d 1051 uint64_t fsid;
3558fd73 1052 uint32_t bshift;
34dc7c2f 1053
3558fd73 1054 ZFS_ENTER(zsb);
34dc7c2f 1055
3558fd73 1056 dmu_objset_space(zsb->z_os,
34dc7c2f
BB
1057 &refdbytes, &availbytes, &usedobjs, &availobjs);
1058
04f9432d 1059 fsid = dmu_objset_fsid_guid(zsb->z_os);
34dc7c2f 1060 /*
05ff35c6
BB
1061 * The underlying storage pool actually uses multiple block
1062 * size. Under Solaris frsize (fragment size) is reported as
1063 * the smallest block size we support, and bsize (block size)
1064 * as the filesystem's maximum block size. Unfortunately,
1065 * under Linux the fragment size and block size are often used
1066 * interchangeably. Thus we are forced to report both of them
1067 * as the filesystem's maximum block size.
34dc7c2f 1068 */
05ff35c6 1069 statp->f_frsize = zsb->z_max_blksz;
3558fd73
BB
1070 statp->f_bsize = zsb->z_max_blksz;
1071 bshift = fls(statp->f_bsize) - 1;
34dc7c2f
BB
1072
1073 /*
3558fd73
BB
1074 * The following report "total" blocks of various kinds in
1075 * the file system, but reported in terms of f_bsize - the
1076 * "preferred" size.
34dc7c2f
BB
1077 */
1078
3558fd73
BB
1079 statp->f_blocks = (refdbytes + availbytes) >> bshift;
1080 statp->f_bfree = availbytes >> bshift;
34dc7c2f
BB
1081 statp->f_bavail = statp->f_bfree; /* no root reservation */
1082
1083 /*
1084 * statvfs() should really be called statufs(), because it assumes
1085 * static metadata. ZFS doesn't preallocate files, so the best
1086 * we can do is report the max that could possibly fit in f_files,
1087 * and that minus the number actually used in f_ffree.
1088 * For f_ffree, report the smaller of the number of object available
1089 * and the number of blocks (each object will take at least a block).
1090 */
baab0630 1091 statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
34dc7c2f 1092 statp->f_files = statp->f_ffree + usedobjs;
04f9432d
CP
1093 statp->f_fsid.val[0] = (uint32_t)fsid;
1094 statp->f_fsid.val[1] = (uint32_t)(fsid >> 32);
3558fd73
BB
1095 statp->f_type = ZFS_SUPER_MAGIC;
1096 statp->f_namelen = ZFS_MAXNAMELEN;
34dc7c2f
BB
1097
1098 /*
3558fd73 1099 * We have all of 40 characters to stuff a string here.
34dc7c2f
BB
1100 * Is there anything useful we could/should provide?
1101 */
3558fd73 1102 bzero(statp->f_spare, sizeof (statp->f_spare));
34dc7c2f 1103
3558fd73 1104 ZFS_EXIT(zsb);
34dc7c2f
BB
1105 return (0);
1106}
e5c39b95 1107EXPORT_SYMBOL(zfs_statvfs);
34dc7c2f 1108
e5c39b95 1109int
3558fd73 1110zfs_root(zfs_sb_t *zsb, struct inode **ipp)
34dc7c2f 1111{
34dc7c2f
BB
1112 znode_t *rootzp;
1113 int error;
1114
3558fd73 1115 ZFS_ENTER(zsb);
34dc7c2f 1116
3558fd73 1117 error = zfs_zget(zsb, zsb->z_root, &rootzp);
34dc7c2f 1118 if (error == 0)
3558fd73 1119 *ipp = ZTOI(rootzp);
34dc7c2f 1120
3558fd73 1121 ZFS_EXIT(zsb);
34dc7c2f
BB
1122 return (error);
1123}
e5c39b95 1124EXPORT_SYMBOL(zfs_root);
34dc7c2f 1125
87dac73d 1126#ifdef HAVE_D_PRUNE_ALIASES
ea04106b
AX
1127/*
1128 * Linux kernels older than 3.1 do not support a per-filesystem shrinker.
1129 * To accommodate this we must improvise and manually walk the list of znodes
1130 * attempting to prune dentries in order to be able to drop the inodes.
1131 *
1132 * To avoid scanning the same znodes multiple times they are always rotated
1133 * to the end of the z_all_znodes list. New znodes are inserted at the
1134 * end of the list so we're always scanning the oldest znodes first.
1135 */
1136static int
1137zfs_sb_prune_aliases(zfs_sb_t *zsb, unsigned long nr_to_scan)
1138{
1139 znode_t **zp_array, *zp;
1140 int max_array = MIN(nr_to_scan, PAGE_SIZE * 8 / sizeof (znode_t *));
1141 int objects = 0;
1142 int i = 0, j = 0;
1143
1144 zp_array = kmem_zalloc(max_array * sizeof (znode_t *), KM_SLEEP);
1145
1146 mutex_enter(&zsb->z_znodes_lock);
1147 while ((zp = list_head(&zsb->z_all_znodes)) != NULL) {
1148
1149 if ((i++ > nr_to_scan) || (j >= max_array))
1150 break;
1151
1152 ASSERT(list_link_active(&zp->z_link_node));
1153 list_remove(&zsb->z_all_znodes, zp);
1154 list_insert_tail(&zsb->z_all_znodes, zp);
1155
1156 /* Skip active znodes and .zfs entries */
1157 if (MUTEX_HELD(&zp->z_lock) || zp->z_is_ctldir)
1158 continue;
1159
1160 if (igrab(ZTOI(zp)) == NULL)
1161 continue;
1162
1163 zp_array[j] = zp;
1164 j++;
1165 }
1166 mutex_exit(&zsb->z_znodes_lock);
1167
1168 for (i = 0; i < j; i++) {
1169 zp = zp_array[i];
1170
1171 ASSERT3P(zp, !=, NULL);
1172 d_prune_aliases(ZTOI(zp));
1173
1174 if (atomic_read(&ZTOI(zp)->i_count) == 1)
1175 objects++;
1176
1177 iput(ZTOI(zp));
1178 }
1179
1180 kmem_free(zp_array, max_array * sizeof (znode_t *));
1181
1182 return (objects);
1183}
1184#endif /* HAVE_D_PRUNE_ALIASES */
1185
1186/*
1187 * The ARC has requested that the filesystem drop entries from the dentry
1188 * and inode caches. This can occur when the ARC needs to free meta data
1189 * blocks but can't because they are all pinned by entries in these caches.
1190 */
ab26409d
BB
1191int
1192zfs_sb_prune(struct super_block *sb, unsigned long nr_to_scan, int *objects)
1193{
1194 zfs_sb_t *zsb = sb->s_fs_info;
ea04106b
AX
1195 int error = 0;
1196#if defined(HAVE_SHRINK) || defined(HAVE_SPLIT_SHRINKER_CALLBACK)
ab26409d
BB
1197 struct shrinker *shrinker = &sb->s_shrink;
1198 struct shrink_control sc = {
1199 .nr_to_scan = nr_to_scan,
1200 .gfp_mask = GFP_KERNEL,
1201 };
ea04106b 1202#endif
ab26409d
BB
1203
1204 ZFS_ENTER(zsb);
ea04106b
AX
1205
1206#if defined(HAVE_SPLIT_SHRINKER_CALLBACK) && \
1207 defined(SHRINK_CONTROL_HAS_NID) && \
1208 defined(SHRINKER_NUMA_AWARE)
1209 if (sb->s_shrink.flags & SHRINKER_NUMA_AWARE) {
1210 *objects = 0;
1211 for_each_online_node(sc.nid)
1212 *objects += (*shrinker->scan_objects)(shrinker, &sc);
1213 } else {
1214 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1215 }
87dac73d 1216
ea04106b
AX
1217#elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1218 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1219#elif defined(HAVE_SHRINK)
ab26409d 1220 *objects = (*shrinker->shrink)(shrinker, &sc);
ea04106b 1221#elif defined(HAVE_D_PRUNE_ALIASES)
87dac73d 1222#define D_PRUNE_ALIASES_IS_DEFAULT
ea04106b
AX
1223 *objects = zfs_sb_prune_aliases(zsb, nr_to_scan);
1224#else
1225#error "No available dentry and inode cache pruning mechanism."
1226#endif
87dac73d
AX
1227
1228#if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1229#undef D_PRUNE_ALIASES_IS_DEFAULT
1230 /*
1231 * Fall back to zfs_sb_prune_aliases if the kernel's per-superblock
1232 * shrinker couldn't free anything, possibly due to the inodes being
1233 * allocated in a different memcg.
1234 */
1235 if (*objects == 0)
1236 *objects = zfs_sb_prune_aliases(zsb, nr_to_scan);
1237#endif
1238
ab26409d
BB
1239 ZFS_EXIT(zsb);
1240
ea04106b
AX
1241 dprintf_ds(zsb->z_os->os_dsl_dataset,
1242 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1243 nr_to_scan, *objects, error);
1244
1245 return (error);
ab26409d
BB
1246}
1247EXPORT_SYMBOL(zfs_sb_prune);
ab26409d 1248
34dc7c2f 1249/*
7b3e34ba 1250 * Teardown the zfs_sb_t.
34dc7c2f
BB
1251 *
1252 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1253 * and 'z_teardown_inactive_lock' held.
1254 */
3558fd73 1255int
86f35f34 1256zfs_sb_teardown(zfs_sb_t *zsb, boolean_t unmounting)
34dc7c2f
BB
1257{
1258 znode_t *zp;
1259
a08ee875
LG
1260 /*
1261 * If someone has not already unmounted this file system,
1262 * drain the iput_taskq to ensure all active references to the
1263 * zfs_sb_t have been handled only then can it be safely destroyed.
1264 */
ea04106b
AX
1265 if (zsb->z_os) {
1266 /*
1267 * If we're unmounting we have to wait for the list to
1268 * drain completely.
1269 *
1270 * If we're not unmounting there's no guarantee the list
1271 * will drain completely, but iputs run from the taskq
1272 * may add the parents of dir-based xattrs to the taskq
1273 * so we want to wait for these.
1274 *
1275 * We can safely read z_nr_znodes without locking because the
1276 * VFS has already blocked operations which add to the
1277 * z_all_znodes list and thus increment z_nr_znodes.
1278 */
1279 int round = 0;
1280 while (zsb->z_nr_znodes > 0) {
e10b0808
AX
1281 taskq_wait_outstanding(dsl_pool_iput_taskq(
1282 dmu_objset_pool(zsb->z_os)), 0);
ea04106b
AX
1283 if (++round > 1 && !unmounting)
1284 break;
1285 }
1286 }
a08ee875 1287
e10b0808 1288 rrm_enter(&zsb->z_teardown_lock, RW_WRITER, FTAG);
34dc7c2f
BB
1289
1290 if (!unmounting) {
1291 /*
ceb43b93
BB
1292 * We purge the parent filesystem's super block as the
1293 * parent filesystem and all of its snapshots have their
1294 * inode's super block set to the parent's filesystem's
1295 * super block. Note, 'z_parent' is self referential
1296 * for non-snapshots.
34dc7c2f 1297 */
ceb43b93 1298 shrink_dcache_sb(zsb->z_parent->z_sb);
34dc7c2f
BB
1299 }
1300
1301 /*
1302 * Close the zil. NB: Can't close the zil while zfs_inactive
1303 * threads are blocked as zil_close can call zfs_inactive.
1304 */
3558fd73
BB
1305 if (zsb->z_log) {
1306 zil_close(zsb->z_log);
1307 zsb->z_log = NULL;
34dc7c2f
BB
1308 }
1309
3558fd73 1310 rw_enter(&zsb->z_teardown_inactive_lock, RW_WRITER);
34dc7c2f
BB
1311
1312 /*
1313 * If we are not unmounting (ie: online recv) and someone already
1314 * unmounted this file system while we were doing the switcheroo,
1315 * or a reopen of z_os failed then just bail out now.
1316 */
3558fd73
BB
1317 if (!unmounting && (zsb->z_unmounted || zsb->z_os == NULL)) {
1318 rw_exit(&zsb->z_teardown_inactive_lock);
e10b0808 1319 rrm_exit(&zsb->z_teardown_lock, FTAG);
a08ee875 1320 return (SET_ERROR(EIO));
34dc7c2f
BB
1321 }
1322
1323 /*
7b3e34ba
BB
1324 * At this point there are no VFS ops active, and any new VFS ops
1325 * will fail with EIO since we have z_teardown_lock for writer (only
1326 * relevant for forced unmount).
34dc7c2f
BB
1327 *
1328 * Release all holds on dbufs.
1329 */
ea04106b
AX
1330 if (!unmounting) {
1331 mutex_enter(&zsb->z_znodes_lock);
1332 for (zp = list_head(&zsb->z_all_znodes); zp != NULL;
1333 zp = list_next(&zsb->z_all_znodes, zp)) {
1334 if (zp->z_sa_hdl)
1335 zfs_znode_dmu_fini(zp);
1336 }
1337 mutex_exit(&zsb->z_znodes_lock);
7b3e34ba 1338 }
34dc7c2f
BB
1339
1340 /*
7b3e34ba 1341 * If we are unmounting, set the unmounted flag and let new VFS ops
34dc7c2f 1342 * unblock. zfs_inactive will have the unmounted behavior, and all
7b3e34ba 1343 * other VFS ops will fail with EIO.
34dc7c2f
BB
1344 */
1345 if (unmounting) {
3558fd73 1346 zsb->z_unmounted = B_TRUE;
e10b0808 1347 rrm_exit(&zsb->z_teardown_lock, FTAG);
3558fd73 1348 rw_exit(&zsb->z_teardown_inactive_lock);
34dc7c2f
BB
1349 }
1350
1351 /*
1352 * z_os will be NULL if there was an error in attempting to reopen
3558fd73
BB
1353 * zsb, so just return as the properties had already been
1354 *
34dc7c2f
BB
1355 * unregistered and cached data had been evicted before.
1356 */
3558fd73 1357 if (zsb->z_os == NULL)
34dc7c2f
BB
1358 return (0);
1359
1360 /*
1361 * Unregister properties.
1362 */
3558fd73 1363 zfs_unregister_callbacks(zsb);
34dc7c2f
BB
1364
1365 /*
1366 * Evict cached data
1367 */
04434775
MA
1368 if (dsl_dataset_is_dirty(dmu_objset_ds(zsb->z_os)) &&
1369 !zfs_is_readonly(zsb))
1370 txg_wait_synced(dmu_objset_pool(zsb->z_os), 0);
a08ee875 1371 dmu_objset_evict_dbufs(zsb->z_os);
34dc7c2f
BB
1372
1373 return (0);
1374}
86f35f34 1375EXPORT_SYMBOL(zfs_sb_teardown);
34dc7c2f 1376
ea04106b
AX
1377#if !defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER) && \
1378 !defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
5547c2f1 1379atomic_long_t zfs_bdi_seq = ATOMIC_LONG_INIT(0);
ea04106b 1380#endif
76659dc1 1381
e5c39b95 1382int
e10b0808 1383zfs_domount(struct super_block *sb, zfs_mntopts_t *zmo, int silent)
34dc7c2f 1384{
e10b0808 1385 const char *osname = zmo->z_osname;
3558fd73
BB
1386 zfs_sb_t *zsb;
1387 struct inode *root_inode;
1388 uint64_t recordsize;
1389 int error;
34dc7c2f 1390
e10b0808 1391 error = zfs_sb_create(osname, zmo, &zsb);
3558fd73
BB
1392 if (error)
1393 return (error);
1394
1395 if ((error = dsl_prop_get_integer(osname, "recordsize",
1396 &recordsize, NULL)))
1397 goto out;
1398
1399 zsb->z_sb = sb;
3558fd73
BB
1400 sb->s_fs_info = zsb;
1401 sb->s_magic = ZFS_SUPER_MAGIC;
1402 sb->s_maxbytes = MAX_LFS_FILESIZE;
1403 sb->s_time_gran = 1;
1404 sb->s_blocksize = recordsize;
1405 sb->s_blocksize_bits = ilog2(recordsize);
5547c2f1
BB
1406 zsb->z_bdi.ra_pages = 0;
1407 sb->s_bdi = &zsb->z_bdi;
1408
22929307 1409 error = -zpl_bdi_setup(sb, "zfs");
5547c2f1
BB
1410 if (error)
1411 goto out;
3558fd73 1412
22929307
AX
1413 sb->s_bdi->ra_pages = 0;
1414
3558fd73
BB
1415 /* Set callback operations for the file system. */
1416 sb->s_op = &zpl_super_operations;
1417 sb->s_xattr = zpl_xattr_handlers;
055656d4 1418 sb->s_export_op = &zpl_export_operations;
ee930353
BB
1419#ifdef HAVE_S_D_OP
1420 sb->s_d_op = &zpl_dentry_operations;
1421#endif /* HAVE_S_D_OP */
3558fd73
BB
1422
1423 /* Set features for file system. */
1424 zfs_set_fuid_feature(zsb);
1425
1426 if (dmu_objset_is_snapshot(zsb->z_os)) {
1427 uint64_t pval;
1428
1429 atime_changed_cb(zsb, B_FALSE);
1430 readonly_changed_cb(zsb, B_TRUE);
a08ee875
LG
1431 if ((error = dsl_prop_get_integer(osname,
1432 "xattr", &pval, NULL)))
3558fd73
BB
1433 goto out;
1434 xattr_changed_cb(zsb, pval);
a08ee875
LG
1435 if ((error = dsl_prop_get_integer(osname,
1436 "acltype", &pval, NULL)))
1437 goto out;
1438 acltype_changed_cb(zsb, pval);
3558fd73
BB
1439 zsb->z_issnap = B_TRUE;
1440 zsb->z_os->os_sync = ZFS_SYNC_DISABLED;
e10b0808 1441 zsb->z_snap_defer_time = jiffies;
3558fd73
BB
1442
1443 mutex_enter(&zsb->z_os->os_user_ptr_lock);
1444 dmu_objset_set_user(zsb->z_os, zsb);
1445 mutex_exit(&zsb->z_os->os_user_ptr_lock);
1446 } else {
1447 error = zfs_sb_setup(zsb, B_TRUE);
34dc7c2f
BB
1448 }
1449
3558fd73
BB
1450 /* Allocate a root inode for the filesystem. */
1451 error = zfs_root(zsb, &root_inode);
1452 if (error) {
1453 (void) zfs_umount(sb);
1454 goto out;
34dc7c2f
BB
1455 }
1456
3558fd73 1457 /* Allocate a root dentry for the filesystem */
6a0936ba 1458 sb->s_root = d_make_root(root_inode);
3558fd73
BB
1459 if (sb->s_root == NULL) {
1460 (void) zfs_umount(sb);
a08ee875 1461 error = SET_ERROR(ENOMEM);
3558fd73
BB
1462 goto out;
1463 }
ebe7e575
BB
1464
1465 if (!zsb->z_issnap)
1466 zfsctl_create(zsb);
ea04106b
AX
1467
1468 zsb->z_arc_prune = arc_add_prune_callback(zpl_prune_sb, sb);
3558fd73
BB
1469out:
1470 if (error) {
1471 dmu_objset_disown(zsb->z_os, zsb);
1472 zfs_sb_free(zsb);
1473 }
34dc7c2f 1474
3558fd73
BB
1475 return (error);
1476}
1477EXPORT_SYMBOL(zfs_domount);
1478
ebe7e575
BB
1479/*
1480 * Called when an unmount is requested and certain sanity checks have
1481 * already passed. At this point no dentries or inodes have been reclaimed
1482 * from their respective caches. We drop the extra reference on the .zfs
1483 * control directory to allow everything to be reclaimed. All snapshots
1484 * must already have been unmounted to reach this point.
1485 */
1486void
1487zfs_preumount(struct super_block *sb)
1488{
1489 zfs_sb_t *zsb = sb->s_fs_info;
1490
e10b0808
AX
1491 if (zsb)
1492 zfsctl_destroy(sb->s_fs_info);
ebe7e575
BB
1493}
1494EXPORT_SYMBOL(zfs_preumount);
1495
1496/*
1497 * Called once all other unmount released tear down has occurred.
1498 * It is our responsibility to release any remaining infrastructure.
1499 */
3558fd73
BB
1500/*ARGSUSED*/
1501int
1502zfs_umount(struct super_block *sb)
1503{
1504 zfs_sb_t *zsb = sb->s_fs_info;
1505 objset_t *os;
1506
ea04106b 1507 arc_remove_prune_callback(zsb->z_arc_prune);
86f35f34 1508 VERIFY(zfs_sb_teardown(zsb, B_TRUE) == 0);
3558fd73 1509 os = zsb->z_os;
22929307 1510 zpl_bdi_destroy(sb);
76659dc1 1511
34dc7c2f
BB
1512 /*
1513 * z_os will be NULL if there was an error in
3558fd73 1514 * attempting to reopen zsb.
34dc7c2f
BB
1515 */
1516 if (os != NULL) {
1517 /*
1518 * Unset the objset user_ptr.
1519 */
428870ff 1520 mutex_enter(&os->os_user_ptr_lock);
34dc7c2f 1521 dmu_objset_set_user(os, NULL);
428870ff 1522 mutex_exit(&os->os_user_ptr_lock);
34dc7c2f
BB
1523
1524 /*
b128c09f 1525 * Finally release the objset
34dc7c2f 1526 */
3558fd73 1527 dmu_objset_disown(os, zsb);
34dc7c2f
BB
1528 }
1529
3558fd73 1530 zfs_sb_free(zsb);
34dc7c2f
BB
1531 return (0);
1532}
e5c39b95 1533EXPORT_SYMBOL(zfs_umount);
34dc7c2f 1534
0de19dad 1535int
e10b0808 1536zfs_remount(struct super_block *sb, int *flags, zfs_mntopts_t *zmo)
0de19dad 1537{
e10b0808
AX
1538 zfs_sb_t *zsb = sb->s_fs_info;
1539 int error;
1540
1541 zfs_unregister_callbacks(zsb);
1542 error = zfs_register_callbacks(zsb);
1543
1544 return (error);
0de19dad
BB
1545}
1546EXPORT_SYMBOL(zfs_remount);
1547
e5c39b95 1548int
2cf7f52b 1549zfs_vget(struct super_block *sb, struct inode **ipp, fid_t *fidp)
34dc7c2f 1550{
2cf7f52b 1551 zfs_sb_t *zsb = sb->s_fs_info;
34dc7c2f
BB
1552 znode_t *zp;
1553 uint64_t object = 0;
1554 uint64_t fid_gen = 0;
1555 uint64_t gen_mask;
1556 uint64_t zp_gen;
3558fd73 1557 int i, err;
34dc7c2f 1558
3558fd73 1559 *ipp = NULL;
34dc7c2f 1560
3558fd73 1561 ZFS_ENTER(zsb);
34dc7c2f
BB
1562
1563 if (fidp->fid_len == LONG_FID_LEN) {
1564 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1565 uint64_t objsetid = 0;
1566 uint64_t setgen = 0;
1567
1568 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1569 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1570
1571 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1572 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1573
3558fd73 1574 ZFS_EXIT(zsb);
34dc7c2f 1575
ebe7e575 1576 err = zfsctl_lookup_objset(sb, objsetid, &zsb);
34dc7c2f 1577 if (err)
a08ee875 1578 return (SET_ERROR(EINVAL));
ebe7e575 1579
3558fd73 1580 ZFS_ENTER(zsb);
34dc7c2f
BB
1581 }
1582
1583 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1584 zfid_short_t *zfid = (zfid_short_t *)fidp;
1585
1586 for (i = 0; i < sizeof (zfid->zf_object); i++)
1587 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1588
1589 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1590 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1591 } else {
3558fd73 1592 ZFS_EXIT(zsb);
a08ee875 1593 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1594 }
1595
1596 /* A zero fid_gen means we are in the .zfs control directories */
1597 if (fid_gen == 0 &&
1598 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
3558fd73
BB
1599 *ipp = zsb->z_ctldir;
1600 ASSERT(*ipp != NULL);
34dc7c2f 1601 if (object == ZFSCTL_INO_SNAPDIR) {
ebe7e575
BB
1602 VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp,
1603 0, kcred, NULL, NULL) == 0);
34dc7c2f 1604 } else {
3558fd73 1605 igrab(*ipp);
34dc7c2f 1606 }
3558fd73 1607 ZFS_EXIT(zsb);
34dc7c2f
BB
1608 return (0);
1609 }
1610
1611 gen_mask = -1ULL >> (64 - 8 * i);
1612
ea04106b 1613 dprintf("getting %llu [%llu mask %llx]\n", object, fid_gen, gen_mask);
3558fd73
BB
1614 if ((err = zfs_zget(zsb, object, &zp))) {
1615 ZFS_EXIT(zsb);
34dc7c2f
BB
1616 return (err);
1617 }
87dac73d
AX
1618
1619 /* Don't export xattr stuff */
1620 if (zp->z_pflags & ZFS_XATTR) {
1621 iput(ZTOI(zp));
1622 ZFS_EXIT(zsb);
1623 return (SET_ERROR(ENOENT));
1624 }
1625
3558fd73 1626 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb), &zp_gen,
428870ff
BB
1627 sizeof (uint64_t));
1628 zp_gen = zp_gen & gen_mask;
34dc7c2f
BB
1629 if (zp_gen == 0)
1630 zp_gen = 1;
e10b0808
AX
1631 if ((fid_gen == 0) && (zsb->z_root == object))
1632 fid_gen = zp_gen;
34dc7c2f 1633 if (zp->z_unlinked || zp_gen != fid_gen) {
ea04106b
AX
1634 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen,
1635 fid_gen);
3558fd73
BB
1636 iput(ZTOI(zp));
1637 ZFS_EXIT(zsb);
87dac73d 1638 return (SET_ERROR(ENOENT));
34dc7c2f
BB
1639 }
1640
3558fd73
BB
1641 *ipp = ZTOI(zp);
1642 if (*ipp)
1643 zfs_inode_update(ITOZ(*ipp));
960e08fe 1644
3558fd73 1645 ZFS_EXIT(zsb);
34dc7c2f
BB
1646 return (0);
1647}
e5c39b95 1648EXPORT_SYMBOL(zfs_vget);
34dc7c2f
BB
1649
1650/*
7b3e34ba 1651 * Block out VFS ops and close zfs_sb_t
34dc7c2f
BB
1652 *
1653 * Note, if successful, then we return with the 'z_teardown_lock' and
a08ee875
LG
1654 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1655 * dataset and objset intact so that they can be atomically handed off during
1656 * a subsequent rollback or recv operation and the resume thereafter.
34dc7c2f
BB
1657 */
1658int
3558fd73 1659zfs_suspend_fs(zfs_sb_t *zsb)
34dc7c2f
BB
1660{
1661 int error;
1662
86f35f34 1663 if ((error = zfs_sb_teardown(zsb, B_FALSE)) != 0)
34dc7c2f 1664 return (error);
7b3e34ba 1665
34dc7c2f
BB
1666 return (0);
1667}
e5c39b95 1668EXPORT_SYMBOL(zfs_suspend_fs);
34dc7c2f
BB
1669
1670/*
7b3e34ba 1671 * Reopen zfs_sb_t and release VFS ops.
34dc7c2f
BB
1672 */
1673int
3558fd73 1674zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
34dc7c2f 1675{
428870ff 1676 int err, err2;
a08ee875
LG
1677 znode_t *zp;
1678 uint64_t sa_obj = 0;
34dc7c2f 1679
e10b0808 1680 ASSERT(RRM_WRITE_HELD(&zsb->z_teardown_lock));
3558fd73 1681 ASSERT(RW_WRITE_HELD(&zsb->z_teardown_inactive_lock));
34dc7c2f 1682
a08ee875
LG
1683 /*
1684 * We already own this, so just hold and rele it to update the
1685 * objset_t, as the one we had before may have been evicted.
1686 */
1687 VERIFY0(dmu_objset_hold(osname, zsb, &zsb->z_os));
1688 VERIFY3P(zsb->z_os->os_dsl_dataset->ds_owner, ==, zsb);
1689 VERIFY(dsl_dataset_long_held(zsb->z_os->os_dsl_dataset));
1690 dmu_objset_rele(zsb->z_os, zsb);
428870ff 1691
a08ee875
LG
1692 /*
1693 * Make sure version hasn't changed
1694 */
428870ff 1695
a08ee875
LG
1696 err = zfs_get_zplprop(zsb->z_os, ZFS_PROP_VERSION,
1697 &zsb->z_version);
428870ff 1698
a08ee875
LG
1699 if (err)
1700 goto bail;
428870ff 1701
a08ee875
LG
1702 err = zap_lookup(zsb->z_os, MASTER_NODE_OBJ,
1703 ZFS_SA_ATTRS, 8, 1, &sa_obj);
34dc7c2f 1704
a08ee875
LG
1705 if (err && zsb->z_version >= ZPL_VERSION_SA)
1706 goto bail;
34dc7c2f 1707
a08ee875
LG
1708 if ((err = sa_setup(zsb->z_os, sa_obj,
1709 zfs_attr_table, ZPL_END, &zsb->z_attr_table)) != 0)
1710 goto bail;
1711
1712 if (zsb->z_version >= ZPL_VERSION_SA)
1713 sa_register_update_callback(zsb->z_os,
1714 zfs_sa_upgrade);
1715
1716 VERIFY(zfs_sb_setup(zsb, B_FALSE) == 0);
1717
1718 zfs_set_fuid_feature(zsb);
1719 zsb->z_rollback_time = jiffies;
1720
1721 /*
1722 * Attempt to re-establish all the active inodes with their
1723 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1724 * and mark it stale. This prevents a collision if a new
1725 * inode/object is created which must use the same inode
1726 * number. The stale inode will be be released when the
1727 * VFS prunes the dentry holding the remaining references
1728 * on the stale inode.
1729 */
1730 mutex_enter(&zsb->z_znodes_lock);
1731 for (zp = list_head(&zsb->z_all_znodes); zp;
1732 zp = list_next(&zsb->z_all_znodes, zp)) {
1733 err2 = zfs_rezget(zp);
1734 if (err2) {
1735 remove_inode_hash(ZTOI(zp));
1736 zp->z_is_stale = B_TRUE;
34dc7c2f 1737 }
34dc7c2f 1738 }
a08ee875 1739 mutex_exit(&zsb->z_znodes_lock);
34dc7c2f 1740
428870ff 1741bail:
7b3e34ba 1742 /* release the VFS ops */
3558fd73 1743 rw_exit(&zsb->z_teardown_inactive_lock);
e10b0808 1744 rrm_exit(&zsb->z_teardown_lock, FTAG);
34dc7c2f
BB
1745
1746 if (err) {
1747 /*
a08ee875
LG
1748 * Since we couldn't setup the sa framework, try to force
1749 * unmount this file system.
34dc7c2f 1750 */
c06d4368
AX
1751 if (zsb->z_os)
1752 (void) zfs_umount(zsb->z_sb);
34dc7c2f
BB
1753 }
1754 return (err);
1755}
e5c39b95 1756EXPORT_SYMBOL(zfs_resume_fs);
34dc7c2f 1757
34dc7c2f 1758int
3558fd73 1759zfs_set_version(zfs_sb_t *zsb, uint64_t newvers)
34dc7c2f
BB
1760{
1761 int error;
3558fd73 1762 objset_t *os = zsb->z_os;
34dc7c2f 1763 dmu_tx_t *tx;
34dc7c2f
BB
1764
1765 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
a08ee875 1766 return (SET_ERROR(EINVAL));
34dc7c2f 1767
3558fd73 1768 if (newvers < zsb->z_version)
a08ee875 1769 return (SET_ERROR(EINVAL));
34dc7c2f 1770
428870ff 1771 if (zfs_spa_version_map(newvers) >
3558fd73 1772 spa_version(dmu_objset_spa(zsb->z_os)))
a08ee875 1773 return (SET_ERROR(ENOTSUP));
428870ff 1774
34dc7c2f 1775 tx = dmu_tx_create(os);
9babb374 1776 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
3558fd73 1777 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
428870ff
BB
1778 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1779 ZFS_SA_ATTRS);
1780 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1781 }
34dc7c2f
BB
1782 error = dmu_tx_assign(tx, TXG_WAIT);
1783 if (error) {
1784 dmu_tx_abort(tx);
9babb374
BB
1785 return (error);
1786 }
428870ff 1787
9babb374
BB
1788 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1789 8, 1, &newvers, tx);
1790
1791 if (error) {
1792 dmu_tx_commit(tx);
1793 return (error);
34dc7c2f 1794 }
34dc7c2f 1795
3558fd73 1796 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
428870ff
BB
1797 uint64_t sa_obj;
1798
3558fd73 1799 ASSERT3U(spa_version(dmu_objset_spa(zsb->z_os)), >=,
428870ff
BB
1800 SPA_VERSION_SA);
1801 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1802 DMU_OT_NONE, 0, tx);
1803
1804 error = zap_add(os, MASTER_NODE_OBJ,
1805 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
c06d4368 1806 ASSERT0(error);
428870ff
BB
1807
1808 VERIFY(0 == sa_set_sa_object(os, sa_obj));
1809 sa_register_update_callback(os, zfs_sa_upgrade);
1810 }
1811
a08ee875
LG
1812 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
1813 "from %llu to %llu", zsb->z_version, newvers);
9babb374 1814
34dc7c2f
BB
1815 dmu_tx_commit(tx);
1816
3558fd73 1817 zsb->z_version = newvers;
9babb374 1818
a08ee875 1819 zfs_set_fuid_feature(zsb);
9babb374
BB
1820
1821 return (0);
34dc7c2f 1822}
e5c39b95 1823EXPORT_SYMBOL(zfs_set_version);
34dc7c2f
BB
1824
1825/*
1826 * Read a property stored within the master node.
1827 */
1828int
1829zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1830{
1831 const char *pname;
a08ee875 1832 int error = SET_ERROR(ENOENT);
34dc7c2f
BB
1833
1834 /*
1835 * Look up the file system's value for the property. For the
1836 * version property, we look up a slightly different string.
1837 */
1838 if (prop == ZFS_PROP_VERSION)
1839 pname = ZPL_VERSION_STR;
1840 else
1841 pname = zfs_prop_to_name(prop);
1842
b128c09f
BB
1843 if (os != NULL)
1844 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
34dc7c2f
BB
1845
1846 if (error == ENOENT) {
1847 /* No value set, use the default value */
1848 switch (prop) {
1849 case ZFS_PROP_VERSION:
1850 *value = ZPL_VERSION;
1851 break;
1852 case ZFS_PROP_NORMALIZE:
1853 case ZFS_PROP_UTF8ONLY:
1854 *value = 0;
1855 break;
1856 case ZFS_PROP_CASE:
1857 *value = ZFS_CASE_SENSITIVE;
1858 break;
a08ee875
LG
1859 case ZFS_PROP_ACLTYPE:
1860 *value = ZFS_ACLTYPE_OFF;
1861 break;
34dc7c2f
BB
1862 default:
1863 return (error);
1864 }
1865 error = 0;
1866 }
1867 return (error);
1868}
86f35f34 1869EXPORT_SYMBOL(zfs_get_zplprop);
3558fd73
BB
1870
1871void
1872zfs_init(void)
1873{
ebe7e575 1874 zfsctl_init();
3558fd73
BB
1875 zfs_znode_init();
1876 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
1877 register_filesystem(&zpl_fs_type);
1878}
1879
1880void
1881zfs_fini(void)
1882{
68d83c55
AX
1883 /*
1884 * we don't use outstanding because zpl_posix_acl_free might add more.
1885 */
1886 taskq_wait(system_taskq);
3558fd73
BB
1887 unregister_filesystem(&zpl_fs_type);
1888 zfs_znode_fini();
ebe7e575 1889 zfsctl_fini();
3558fd73 1890}