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