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