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