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