]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_vfsops.c
d8b27461aacc9571275e3350ec5510793c42d43c
[mirror_zfs.git] / module / zfs / zfs_vfsops.c
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 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 */
25
26 /* Portions Copyright 2010 Robert Milkowski */
27
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/kmem.h>
33 #include <sys/pathname.h>
34 #include <sys/vnode.h>
35 #include <sys/vfs.h>
36 #include <sys/vfs_opreg.h>
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include "fs/fs_subr.h"
41 #include <sys/zfs_znode.h>
42 #include <sys/zfs_vnops.h>
43 #include <sys/zfs_dir.h>
44 #include <sys/zil.h>
45 #include <sys/fs/zfs.h>
46 #include <sys/dmu.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/spa.h>
51 #include <sys/zap.h>
52 #include <sys/sa.h>
53 #include <sys/sa_impl.h>
54 #include <sys/varargs.h>
55 #include <sys/policy.h>
56 #include <sys/atomic.h>
57 #include <sys/mkdev.h>
58 #include <sys/modctl.h>
59 #include <sys/refstr.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_fuid.h>
63 #include <sys/bootconf.h>
64 #include <sys/sunddi.h>
65 #include <sys/dnlc.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/spa_boot.h>
68 #include <sys/zpl.h>
69 #include "zfs_comutil.h"
70
71 /*ARGSUSED*/
72 int
73 zfs_sync(struct super_block *sb, int wait, cred_t *cr)
74 {
75 zfs_sb_t *zsb = sb->s_fs_info;
76
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 */
81 if (unlikely(oops_in_progress))
82 return (0);
83
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
91 if (zsb != NULL) {
92 /*
93 * Sync a specific filesystem.
94 */
95 dsl_pool_t *dp;
96
97 ZFS_ENTER(zsb);
98 dp = dmu_objset_pool(zsb->z_os);
99
100 /*
101 * If the system is shutting down, then skip any
102 * filesystems which may exist on a suspended pool.
103 */
104 if (spa_suspended(dp->dp_spa)) {
105 ZFS_EXIT(zsb);
106 return (0);
107 }
108
109 if (zsb->z_log != NULL)
110 zil_commit(zsb->z_log, 0);
111
112 ZFS_EXIT(zsb);
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 }
124 EXPORT_SYMBOL(zfs_sync);
125
126 boolean_t
127 zfs_is_readonly(zfs_sb_t *zsb)
128 {
129 return (!!(zsb->z_sb->s_flags & MS_RDONLY));
130 }
131 EXPORT_SYMBOL(zfs_is_readonly);
132
133 static void
134 atime_changed_cb(void *arg, uint64_t newval)
135 {
136 ((zfs_sb_t *)arg)->z_atime = newval;
137 }
138
139 static void
140 relatime_changed_cb(void *arg, uint64_t newval)
141 {
142 ((zfs_sb_t *)arg)->z_relatime = newval;
143 }
144
145 static void
146 xattr_changed_cb(void *arg, uint64_t newval)
147 {
148 zfs_sb_t *zsb = arg;
149
150 if (newval == ZFS_XATTR_OFF) {
151 zsb->z_flags &= ~ZSB_XATTR;
152 } else {
153 zsb->z_flags |= ZSB_XATTR;
154
155 if (newval == ZFS_XATTR_SA)
156 zsb->z_xattr_sa = B_TRUE;
157 else
158 zsb->z_xattr_sa = B_FALSE;
159 }
160 }
161
162 static void
163 acltype_changed_cb(void *arg, uint64_t newval)
164 {
165 zfs_sb_t *zsb = arg;
166
167 switch (newval) {
168 case ZFS_ACLTYPE_OFF:
169 zsb->z_acl_type = ZFS_ACLTYPE_OFF;
170 zsb->z_sb->s_flags &= ~MS_POSIXACL;
171 break;
172 case ZFS_ACLTYPE_POSIXACL:
173 #ifdef CONFIG_FS_POSIX_ACL
174 zsb->z_acl_type = ZFS_ACLTYPE_POSIXACL;
175 zsb->z_sb->s_flags |= MS_POSIXACL;
176 #else
177 zsb->z_acl_type = ZFS_ACLTYPE_OFF;
178 zsb->z_sb->s_flags &= ~MS_POSIXACL;
179 #endif /* CONFIG_FS_POSIX_ACL */
180 break;
181 default:
182 break;
183 }
184 }
185
186 static void
187 blksz_changed_cb(void *arg, uint64_t newval)
188 {
189 zfs_sb_t *zsb = arg;
190 ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zsb->z_os)));
191 ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
192 ASSERT(ISP2(newval));
193
194 zsb->z_max_blksz = newval;
195 }
196
197 static void
198 readonly_changed_cb(void *arg, uint64_t newval)
199 {
200 zfs_sb_t *zsb = arg;
201 struct super_block *sb = zsb->z_sb;
202
203 if (sb == NULL)
204 return;
205
206 if (newval)
207 sb->s_flags |= MS_RDONLY;
208 else
209 sb->s_flags &= ~MS_RDONLY;
210 }
211
212 static void
213 devices_changed_cb(void *arg, uint64_t newval)
214 {
215 }
216
217 static void
218 setuid_changed_cb(void *arg, uint64_t newval)
219 {
220 }
221
222 static void
223 exec_changed_cb(void *arg, uint64_t newval)
224 {
225 }
226
227 static void
228 nbmand_changed_cb(void *arg, uint64_t newval)
229 {
230 zfs_sb_t *zsb = arg;
231 struct super_block *sb = zsb->z_sb;
232
233 if (sb == NULL)
234 return;
235
236 if (newval == TRUE)
237 sb->s_flags |= MS_MANDLOCK;
238 else
239 sb->s_flags &= ~MS_MANDLOCK;
240 }
241
242 static void
243 snapdir_changed_cb(void *arg, uint64_t newval)
244 {
245 ((zfs_sb_t *)arg)->z_show_ctldir = newval;
246 }
247
248 static void
249 vscan_changed_cb(void *arg, uint64_t newval)
250 {
251 ((zfs_sb_t *)arg)->z_vscan = newval;
252 }
253
254 static void
255 acl_inherit_changed_cb(void *arg, uint64_t newval)
256 {
257 ((zfs_sb_t *)arg)->z_acl_inherit = newval;
258 }
259
260 int
261 zfs_register_callbacks(zfs_sb_t *zsb)
262 {
263 struct dsl_dataset *ds = NULL;
264 objset_t *os = zsb->z_os;
265 zfs_mntopts_t *zmo = zsb->z_mntopts;
266 int error = 0;
267
268 ASSERT(zsb);
269 ASSERT(zmo);
270
271 /*
272 * The act of registering our callbacks will destroy any mount
273 * options we may have. In order to enable temporary overrides
274 * of mount options, we stash away the current values and
275 * restore them after we register the callbacks.
276 */
277 if (zfs_is_readonly(zsb) || !spa_writeable(dmu_objset_spa(os))) {
278 zmo->z_do_readonly = B_TRUE;
279 zmo->z_readonly = B_TRUE;
280 }
281
282 /*
283 * Register property callbacks.
284 *
285 * It would probably be fine to just check for i/o error from
286 * the first prop_register(), but I guess I like to go
287 * overboard...
288 */
289 ds = dmu_objset_ds(os);
290 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
291 error = dsl_prop_register(ds,
292 zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zsb);
293 error = error ? error : dsl_prop_register(ds,
294 zfs_prop_to_name(ZFS_PROP_RELATIME), relatime_changed_cb, zsb);
295 error = error ? error : dsl_prop_register(ds,
296 zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zsb);
297 error = error ? error : dsl_prop_register(ds,
298 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zsb);
299 error = error ? error : dsl_prop_register(ds,
300 zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zsb);
301 error = error ? error : dsl_prop_register(ds,
302 zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zsb);
303 error = error ? error : dsl_prop_register(ds,
304 zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zsb);
305 error = error ? error : dsl_prop_register(ds,
306 zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zsb);
307 error = error ? error : dsl_prop_register(ds,
308 zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zsb);
309 error = error ? error : dsl_prop_register(ds,
310 zfs_prop_to_name(ZFS_PROP_ACLTYPE), acltype_changed_cb, zsb);
311 error = error ? error : dsl_prop_register(ds,
312 zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb, zsb);
313 error = error ? error : dsl_prop_register(ds,
314 zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zsb);
315 error = error ? error : dsl_prop_register(ds,
316 zfs_prop_to_name(ZFS_PROP_NBMAND), nbmand_changed_cb, zsb);
317 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
318 if (error)
319 goto unregister;
320
321 /*
322 * Invoke our callbacks to restore temporary mount options.
323 */
324 if (zmo->z_do_readonly)
325 readonly_changed_cb(zsb, zmo->z_readonly);
326 if (zmo->z_do_setuid)
327 setuid_changed_cb(zsb, zmo->z_setuid);
328 if (zmo->z_do_exec)
329 exec_changed_cb(zsb, zmo->z_exec);
330 if (zmo->z_do_devices)
331 devices_changed_cb(zsb, zmo->z_devices);
332 if (zmo->z_do_xattr)
333 xattr_changed_cb(zsb, zmo->z_xattr);
334 if (zmo->z_do_atime)
335 atime_changed_cb(zsb, zmo->z_atime);
336 if (zmo->z_do_relatime)
337 relatime_changed_cb(zsb, zmo->z_relatime);
338 if (zmo->z_do_nbmand)
339 nbmand_changed_cb(zsb, zmo->z_nbmand);
340
341 return (0);
342
343 unregister:
344 dsl_prop_unregister_all(ds, zsb);
345 return (error);
346 }
347 EXPORT_SYMBOL(zfs_register_callbacks);
348
349 static int
350 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
351 uint64_t *userp, uint64_t *groupp)
352 {
353 /*
354 * Is it a valid type of object to track?
355 */
356 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
357 return (SET_ERROR(ENOENT));
358
359 /*
360 * If we have a NULL data pointer
361 * then assume the id's aren't changing and
362 * return EEXIST to the dmu to let it know to
363 * use the same ids
364 */
365 if (data == NULL)
366 return (SET_ERROR(EEXIST));
367
368 if (bonustype == DMU_OT_ZNODE) {
369 znode_phys_t *znp = data;
370 *userp = znp->zp_uid;
371 *groupp = znp->zp_gid;
372 } else {
373 int hdrsize;
374 sa_hdr_phys_t *sap = data;
375 sa_hdr_phys_t sa = *sap;
376 boolean_t swap = B_FALSE;
377
378 ASSERT(bonustype == DMU_OT_SA);
379
380 if (sa.sa_magic == 0) {
381 /*
382 * This should only happen for newly created
383 * files that haven't had the znode data filled
384 * in yet.
385 */
386 *userp = 0;
387 *groupp = 0;
388 return (0);
389 }
390 if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
391 sa.sa_magic = SA_MAGIC;
392 sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
393 swap = B_TRUE;
394 } else {
395 VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
396 }
397
398 hdrsize = sa_hdrsize(&sa);
399 VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
400 *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
401 SA_UID_OFFSET));
402 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
403 SA_GID_OFFSET));
404 if (swap) {
405 *userp = BSWAP_64(*userp);
406 *groupp = BSWAP_64(*groupp);
407 }
408 }
409 return (0);
410 }
411
412 static void
413 fuidstr_to_sid(zfs_sb_t *zsb, const char *fuidstr,
414 char *domainbuf, int buflen, uid_t *ridp)
415 {
416 uint64_t fuid;
417 const char *domain;
418
419 fuid = strtonum(fuidstr, NULL);
420
421 domain = zfs_fuid_find_by_idx(zsb, FUID_INDEX(fuid));
422 if (domain)
423 (void) strlcpy(domainbuf, domain, buflen);
424 else
425 domainbuf[0] = '\0';
426 *ridp = FUID_RID(fuid);
427 }
428
429 static uint64_t
430 zfs_userquota_prop_to_obj(zfs_sb_t *zsb, zfs_userquota_prop_t type)
431 {
432 switch (type) {
433 case ZFS_PROP_USERUSED:
434 return (DMU_USERUSED_OBJECT);
435 case ZFS_PROP_GROUPUSED:
436 return (DMU_GROUPUSED_OBJECT);
437 case ZFS_PROP_USERQUOTA:
438 return (zsb->z_userquota_obj);
439 case ZFS_PROP_GROUPQUOTA:
440 return (zsb->z_groupquota_obj);
441 default:
442 return (SET_ERROR(ENOTSUP));
443 }
444 return (0);
445 }
446
447 int
448 zfs_userspace_many(zfs_sb_t *zsb, zfs_userquota_prop_t type,
449 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
450 {
451 int error;
452 zap_cursor_t zc;
453 zap_attribute_t za;
454 zfs_useracct_t *buf = vbuf;
455 uint64_t obj;
456
457 if (!dmu_objset_userspace_present(zsb->z_os))
458 return (SET_ERROR(ENOTSUP));
459
460 obj = zfs_userquota_prop_to_obj(zsb, type);
461 if (obj == 0) {
462 *bufsizep = 0;
463 return (0);
464 }
465
466 for (zap_cursor_init_serialized(&zc, zsb->z_os, obj, *cookiep);
467 (error = zap_cursor_retrieve(&zc, &za)) == 0;
468 zap_cursor_advance(&zc)) {
469 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
470 *bufsizep)
471 break;
472
473 fuidstr_to_sid(zsb, za.za_name,
474 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
475
476 buf->zu_space = za.za_first_integer;
477 buf++;
478 }
479 if (error == ENOENT)
480 error = 0;
481
482 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
483 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
484 *cookiep = zap_cursor_serialize(&zc);
485 zap_cursor_fini(&zc);
486 return (error);
487 }
488 EXPORT_SYMBOL(zfs_userspace_many);
489
490 /*
491 * buf must be big enough (eg, 32 bytes)
492 */
493 static int
494 id_to_fuidstr(zfs_sb_t *zsb, const char *domain, uid_t rid,
495 char *buf, boolean_t addok)
496 {
497 uint64_t fuid;
498 int domainid = 0;
499
500 if (domain && domain[0]) {
501 domainid = zfs_fuid_find_by_domain(zsb, domain, NULL, addok);
502 if (domainid == -1)
503 return (SET_ERROR(ENOENT));
504 }
505 fuid = FUID_ENCODE(domainid, rid);
506 (void) sprintf(buf, "%llx", (longlong_t)fuid);
507 return (0);
508 }
509
510 int
511 zfs_userspace_one(zfs_sb_t *zsb, zfs_userquota_prop_t type,
512 const char *domain, uint64_t rid, uint64_t *valp)
513 {
514 char buf[32];
515 int err;
516 uint64_t obj;
517
518 *valp = 0;
519
520 if (!dmu_objset_userspace_present(zsb->z_os))
521 return (SET_ERROR(ENOTSUP));
522
523 obj = zfs_userquota_prop_to_obj(zsb, type);
524 if (obj == 0)
525 return (0);
526
527 err = id_to_fuidstr(zsb, domain, rid, buf, B_FALSE);
528 if (err)
529 return (err);
530
531 err = zap_lookup(zsb->z_os, obj, buf, 8, 1, valp);
532 if (err == ENOENT)
533 err = 0;
534 return (err);
535 }
536 EXPORT_SYMBOL(zfs_userspace_one);
537
538 int
539 zfs_set_userquota(zfs_sb_t *zsb, zfs_userquota_prop_t type,
540 const char *domain, uint64_t rid, uint64_t quota)
541 {
542 char buf[32];
543 int err;
544 dmu_tx_t *tx;
545 uint64_t *objp;
546 boolean_t fuid_dirtied;
547
548 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
549 return (SET_ERROR(EINVAL));
550
551 if (zsb->z_version < ZPL_VERSION_USERSPACE)
552 return (SET_ERROR(ENOTSUP));
553
554 objp = (type == ZFS_PROP_USERQUOTA) ? &zsb->z_userquota_obj :
555 &zsb->z_groupquota_obj;
556
557 err = id_to_fuidstr(zsb, domain, rid, buf, B_TRUE);
558 if (err)
559 return (err);
560 fuid_dirtied = zsb->z_fuid_dirty;
561
562 tx = dmu_tx_create(zsb->z_os);
563 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
564 if (*objp == 0) {
565 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
566 zfs_userquota_prop_prefixes[type]);
567 }
568 if (fuid_dirtied)
569 zfs_fuid_txhold(zsb, tx);
570 err = dmu_tx_assign(tx, TXG_WAIT);
571 if (err) {
572 dmu_tx_abort(tx);
573 return (err);
574 }
575
576 mutex_enter(&zsb->z_lock);
577 if (*objp == 0) {
578 *objp = zap_create(zsb->z_os, DMU_OT_USERGROUP_QUOTA,
579 DMU_OT_NONE, 0, tx);
580 VERIFY(0 == zap_add(zsb->z_os, MASTER_NODE_OBJ,
581 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
582 }
583 mutex_exit(&zsb->z_lock);
584
585 if (quota == 0) {
586 err = zap_remove(zsb->z_os, *objp, buf, tx);
587 if (err == ENOENT)
588 err = 0;
589 } else {
590 err = zap_update(zsb->z_os, *objp, buf, 8, 1, &quota, tx);
591 }
592 ASSERT(err == 0);
593 if (fuid_dirtied)
594 zfs_fuid_sync(zsb, tx);
595 dmu_tx_commit(tx);
596 return (err);
597 }
598 EXPORT_SYMBOL(zfs_set_userquota);
599
600 boolean_t
601 zfs_fuid_overquota(zfs_sb_t *zsb, boolean_t isgroup, uint64_t fuid)
602 {
603 char buf[32];
604 uint64_t used, quota, usedobj, quotaobj;
605 int err;
606
607 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
608 quotaobj = isgroup ? zsb->z_groupquota_obj : zsb->z_userquota_obj;
609
610 if (quotaobj == 0 || zsb->z_replay)
611 return (B_FALSE);
612
613 (void) sprintf(buf, "%llx", (longlong_t)fuid);
614 err = zap_lookup(zsb->z_os, quotaobj, buf, 8, 1, &quota);
615 if (err != 0)
616 return (B_FALSE);
617
618 err = zap_lookup(zsb->z_os, usedobj, buf, 8, 1, &used);
619 if (err != 0)
620 return (B_FALSE);
621 return (used >= quota);
622 }
623 EXPORT_SYMBOL(zfs_fuid_overquota);
624
625 boolean_t
626 zfs_owner_overquota(zfs_sb_t *zsb, znode_t *zp, boolean_t isgroup)
627 {
628 uint64_t fuid;
629 uint64_t quotaobj;
630 struct inode *ip = ZTOI(zp);
631
632 quotaobj = isgroup ? zsb->z_groupquota_obj : zsb->z_userquota_obj;
633
634 fuid = isgroup ? KGID_TO_SGID(ip->i_gid) : KUID_TO_SUID(ip->i_uid);
635
636 if (quotaobj == 0 || zsb->z_replay)
637 return (B_FALSE);
638
639 return (zfs_fuid_overquota(zsb, isgroup, fuid));
640 }
641 EXPORT_SYMBOL(zfs_owner_overquota);
642
643 zfs_mntopts_t *
644 zfs_mntopts_alloc(void)
645 {
646 return (kmem_zalloc(sizeof (zfs_mntopts_t), KM_SLEEP));
647 }
648
649 void
650 zfs_mntopts_free(zfs_mntopts_t *zmo)
651 {
652 if (zmo->z_osname)
653 strfree(zmo->z_osname);
654
655 if (zmo->z_mntpoint)
656 strfree(zmo->z_mntpoint);
657
658 kmem_free(zmo, sizeof (zfs_mntopts_t));
659 }
660
661 int
662 zfs_sb_create(const char *osname, zfs_mntopts_t *zmo, zfs_sb_t **zsbp)
663 {
664 objset_t *os;
665 zfs_sb_t *zsb;
666 uint64_t zval;
667 int i, size, error;
668 uint64_t sa_obj;
669
670 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_SLEEP);
671
672 /*
673 * We claim to always be readonly so we can open snapshots;
674 * other ZPL code will prevent us from writing to snapshots.
675 */
676 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zsb, &os);
677 if (error) {
678 kmem_free(zsb, sizeof (zfs_sb_t));
679 return (error);
680 }
681
682 /*
683 * Optional temporary mount options, free'd in zfs_sb_free().
684 */
685 zsb->z_mntopts = (zmo ? zmo : zfs_mntopts_alloc());
686
687 /*
688 * Initialize the zfs-specific filesystem structure.
689 * Should probably make this a kmem cache, shuffle fields.
690 */
691 zsb->z_sb = NULL;
692 zsb->z_parent = zsb;
693 zsb->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
694 zsb->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
695 zsb->z_os = os;
696
697 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zsb->z_version);
698 if (error) {
699 goto out;
700 } else if (zsb->z_version > ZPL_VERSION) {
701 error = SET_ERROR(ENOTSUP);
702 goto out;
703 }
704 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
705 goto out;
706 zsb->z_norm = (int)zval;
707
708 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
709 goto out;
710 zsb->z_utf8 = (zval != 0);
711
712 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
713 goto out;
714 zsb->z_case = (uint_t)zval;
715
716 if ((error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &zval)) != 0)
717 goto out;
718 zsb->z_acl_type = (uint_t)zval;
719
720 /*
721 * Fold case on file systems that are always or sometimes case
722 * insensitive.
723 */
724 if (zsb->z_case == ZFS_CASE_INSENSITIVE ||
725 zsb->z_case == ZFS_CASE_MIXED)
726 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
727
728 zsb->z_use_fuids = USE_FUIDS(zsb->z_version, zsb->z_os);
729 zsb->z_use_sa = USE_SA(zsb->z_version, zsb->z_os);
730
731 if (zsb->z_use_sa) {
732 /* should either have both of these objects or none */
733 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
734 &sa_obj);
735 if (error)
736 goto out;
737
738 error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &zval);
739 if ((error == 0) && (zval == ZFS_XATTR_SA))
740 zsb->z_xattr_sa = B_TRUE;
741 } else {
742 /*
743 * Pre SA versions file systems should never touch
744 * either the attribute registration or layout objects.
745 */
746 sa_obj = 0;
747 }
748
749 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
750 &zsb->z_attr_table);
751 if (error)
752 goto out;
753
754 if (zsb->z_version >= ZPL_VERSION_SA)
755 sa_register_update_callback(os, zfs_sa_upgrade);
756
757 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
758 &zsb->z_root);
759 if (error)
760 goto out;
761 ASSERT(zsb->z_root != 0);
762
763 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
764 &zsb->z_unlinkedobj);
765 if (error)
766 goto out;
767
768 error = zap_lookup(os, MASTER_NODE_OBJ,
769 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
770 8, 1, &zsb->z_userquota_obj);
771 if (error && error != ENOENT)
772 goto out;
773
774 error = zap_lookup(os, MASTER_NODE_OBJ,
775 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
776 8, 1, &zsb->z_groupquota_obj);
777 if (error && error != ENOENT)
778 goto out;
779
780 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
781 &zsb->z_fuid_obj);
782 if (error && error != ENOENT)
783 goto out;
784
785 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
786 &zsb->z_shares_dir);
787 if (error && error != ENOENT)
788 goto out;
789
790 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
791 mutex_init(&zsb->z_lock, NULL, MUTEX_DEFAULT, NULL);
792 list_create(&zsb->z_all_znodes, sizeof (znode_t),
793 offsetof(znode_t, z_link_node));
794 rrm_init(&zsb->z_teardown_lock, B_FALSE);
795 rw_init(&zsb->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
796 rw_init(&zsb->z_fuid_lock, NULL, RW_DEFAULT, NULL);
797
798 size = MIN(1 << (highbit64(zfs_object_mutex_size)-1), ZFS_OBJ_MTX_MAX);
799 zsb->z_hold_size = size;
800 zsb->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size, KM_SLEEP);
801 zsb->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
802 for (i = 0; i != size; i++) {
803 avl_create(&zsb->z_hold_trees[i], zfs_znode_hold_compare,
804 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
805 mutex_init(&zsb->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
806 }
807
808 *zsbp = zsb;
809 return (0);
810
811 out:
812 dmu_objset_disown(os, zsb);
813 *zsbp = NULL;
814
815 kmem_free(zsb, sizeof (zfs_sb_t));
816 return (error);
817 }
818 EXPORT_SYMBOL(zfs_sb_create);
819
820 int
821 zfs_sb_setup(zfs_sb_t *zsb, boolean_t mounting)
822 {
823 int error;
824
825 error = zfs_register_callbacks(zsb);
826 if (error)
827 return (error);
828
829 /*
830 * Set the objset user_ptr to track its zsb.
831 */
832 mutex_enter(&zsb->z_os->os_user_ptr_lock);
833 dmu_objset_set_user(zsb->z_os, zsb);
834 mutex_exit(&zsb->z_os->os_user_ptr_lock);
835
836 zsb->z_log = zil_open(zsb->z_os, zfs_get_data);
837
838 /*
839 * If we are not mounting (ie: online recv), then we don't
840 * have to worry about replaying the log as we blocked all
841 * operations out since we closed the ZIL.
842 */
843 if (mounting) {
844 boolean_t readonly;
845
846 /*
847 * During replay we remove the read only flag to
848 * allow replays to succeed.
849 */
850 readonly = zfs_is_readonly(zsb);
851 if (readonly != 0)
852 readonly_changed_cb(zsb, B_FALSE);
853 else
854 zfs_unlinked_drain(zsb);
855
856 /*
857 * Parse and replay the intent log.
858 *
859 * Because of ziltest, this must be done after
860 * zfs_unlinked_drain(). (Further note: ziltest
861 * doesn't use readonly mounts, where
862 * zfs_unlinked_drain() isn't called.) This is because
863 * ziltest causes spa_sync() to think it's committed,
864 * but actually it is not, so the intent log contains
865 * many txg's worth of changes.
866 *
867 * In particular, if object N is in the unlinked set in
868 * the last txg to actually sync, then it could be
869 * actually freed in a later txg and then reallocated
870 * in a yet later txg. This would write a "create
871 * object N" record to the intent log. Normally, this
872 * would be fine because the spa_sync() would have
873 * written out the fact that object N is free, before
874 * we could write the "create object N" intent log
875 * record.
876 *
877 * But when we are in ziltest mode, we advance the "open
878 * txg" without actually spa_sync()-ing the changes to
879 * disk. So we would see that object N is still
880 * allocated and in the unlinked set, and there is an
881 * intent log record saying to allocate it.
882 */
883 if (spa_writeable(dmu_objset_spa(zsb->z_os))) {
884 if (zil_replay_disable) {
885 zil_destroy(zsb->z_log, B_FALSE);
886 } else {
887 zsb->z_replay = B_TRUE;
888 zil_replay(zsb->z_os, zsb,
889 zfs_replay_vector);
890 zsb->z_replay = B_FALSE;
891 }
892 }
893
894 /* restore readonly bit */
895 if (readonly != 0)
896 readonly_changed_cb(zsb, B_TRUE);
897 }
898
899 return (0);
900 }
901 EXPORT_SYMBOL(zfs_sb_setup);
902
903 void
904 zfs_sb_free(zfs_sb_t *zsb)
905 {
906 int i, size = zsb->z_hold_size;
907
908 zfs_fuid_destroy(zsb);
909
910 mutex_destroy(&zsb->z_znodes_lock);
911 mutex_destroy(&zsb->z_lock);
912 list_destroy(&zsb->z_all_znodes);
913 rrm_destroy(&zsb->z_teardown_lock);
914 rw_destroy(&zsb->z_teardown_inactive_lock);
915 rw_destroy(&zsb->z_fuid_lock);
916 for (i = 0; i != size; i++) {
917 avl_destroy(&zsb->z_hold_trees[i]);
918 mutex_destroy(&zsb->z_hold_locks[i]);
919 }
920 vmem_free(zsb->z_hold_trees, sizeof (avl_tree_t) * size);
921 vmem_free(zsb->z_hold_locks, sizeof (kmutex_t) * size);
922 zfs_mntopts_free(zsb->z_mntopts);
923 kmem_free(zsb, sizeof (zfs_sb_t));
924 }
925 EXPORT_SYMBOL(zfs_sb_free);
926
927 static void
928 zfs_set_fuid_feature(zfs_sb_t *zsb)
929 {
930 zsb->z_use_fuids = USE_FUIDS(zsb->z_version, zsb->z_os);
931 zsb->z_use_sa = USE_SA(zsb->z_version, zsb->z_os);
932 }
933
934 void
935 zfs_unregister_callbacks(zfs_sb_t *zsb)
936 {
937 objset_t *os = zsb->z_os;
938
939 if (!dmu_objset_is_snapshot(os))
940 dsl_prop_unregister_all(dmu_objset_ds(os), zsb);
941 }
942 EXPORT_SYMBOL(zfs_unregister_callbacks);
943
944 #ifdef HAVE_MLSLABEL
945 /*
946 * Check that the hex label string is appropriate for the dataset being
947 * mounted into the global_zone proper.
948 *
949 * Return an error if the hex label string is not default or
950 * admin_low/admin_high. For admin_low labels, the corresponding
951 * dataset must be readonly.
952 */
953 int
954 zfs_check_global_label(const char *dsname, const char *hexsl)
955 {
956 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
957 return (0);
958 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
959 return (0);
960 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
961 /* must be readonly */
962 uint64_t rdonly;
963
964 if (dsl_prop_get_integer(dsname,
965 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
966 return (SET_ERROR(EACCES));
967 return (rdonly ? 0 : EACCES);
968 }
969 return (SET_ERROR(EACCES));
970 }
971 EXPORT_SYMBOL(zfs_check_global_label);
972 #endif /* HAVE_MLSLABEL */
973
974 int
975 zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
976 {
977 zfs_sb_t *zsb = dentry->d_sb->s_fs_info;
978 uint64_t refdbytes, availbytes, usedobjs, availobjs;
979 uint64_t fsid;
980 uint32_t bshift;
981
982 ZFS_ENTER(zsb);
983
984 dmu_objset_space(zsb->z_os,
985 &refdbytes, &availbytes, &usedobjs, &availobjs);
986
987 fsid = dmu_objset_fsid_guid(zsb->z_os);
988 /*
989 * The underlying storage pool actually uses multiple block
990 * size. Under Solaris frsize (fragment size) is reported as
991 * the smallest block size we support, and bsize (block size)
992 * as the filesystem's maximum block size. Unfortunately,
993 * under Linux the fragment size and block size are often used
994 * interchangeably. Thus we are forced to report both of them
995 * as the filesystem's maximum block size.
996 */
997 statp->f_frsize = zsb->z_max_blksz;
998 statp->f_bsize = zsb->z_max_blksz;
999 bshift = fls(statp->f_bsize) - 1;
1000
1001 /*
1002 * The following report "total" blocks of various kinds in
1003 * the file system, but reported in terms of f_bsize - the
1004 * "preferred" size.
1005 */
1006
1007 statp->f_blocks = (refdbytes + availbytes) >> bshift;
1008 statp->f_bfree = availbytes >> bshift;
1009 statp->f_bavail = statp->f_bfree; /* no root reservation */
1010
1011 /*
1012 * statvfs() should really be called statufs(), because it assumes
1013 * static metadata. ZFS doesn't preallocate files, so the best
1014 * we can do is report the max that could possibly fit in f_files,
1015 * and that minus the number actually used in f_ffree.
1016 * For f_ffree, report the smaller of the number of object available
1017 * and the number of blocks (each object will take at least a block).
1018 */
1019 statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
1020 statp->f_files = statp->f_ffree + usedobjs;
1021 statp->f_fsid.val[0] = (uint32_t)fsid;
1022 statp->f_fsid.val[1] = (uint32_t)(fsid >> 32);
1023 statp->f_type = ZFS_SUPER_MAGIC;
1024 statp->f_namelen = MAXNAMELEN - 1;
1025
1026 /*
1027 * We have all of 40 characters to stuff a string here.
1028 * Is there anything useful we could/should provide?
1029 */
1030 bzero(statp->f_spare, sizeof (statp->f_spare));
1031
1032 ZFS_EXIT(zsb);
1033 return (0);
1034 }
1035 EXPORT_SYMBOL(zfs_statvfs);
1036
1037 int
1038 zfs_root(zfs_sb_t *zsb, struct inode **ipp)
1039 {
1040 znode_t *rootzp;
1041 int error;
1042
1043 ZFS_ENTER(zsb);
1044
1045 error = zfs_zget(zsb, zsb->z_root, &rootzp);
1046 if (error == 0)
1047 *ipp = ZTOI(rootzp);
1048
1049 ZFS_EXIT(zsb);
1050 return (error);
1051 }
1052 EXPORT_SYMBOL(zfs_root);
1053
1054 #ifdef HAVE_D_PRUNE_ALIASES
1055 /*
1056 * Linux kernels older than 3.1 do not support a per-filesystem shrinker.
1057 * To accommodate this we must improvise and manually walk the list of znodes
1058 * attempting to prune dentries in order to be able to drop the inodes.
1059 *
1060 * To avoid scanning the same znodes multiple times they are always rotated
1061 * to the end of the z_all_znodes list. New znodes are inserted at the
1062 * end of the list so we're always scanning the oldest znodes first.
1063 */
1064 static int
1065 zfs_sb_prune_aliases(zfs_sb_t *zsb, unsigned long nr_to_scan)
1066 {
1067 znode_t **zp_array, *zp;
1068 int max_array = MIN(nr_to_scan, PAGE_SIZE * 8 / sizeof (znode_t *));
1069 int objects = 0;
1070 int i = 0, j = 0;
1071
1072 zp_array = kmem_zalloc(max_array * sizeof (znode_t *), KM_SLEEP);
1073
1074 mutex_enter(&zsb->z_znodes_lock);
1075 while ((zp = list_head(&zsb->z_all_znodes)) != NULL) {
1076
1077 if ((i++ > nr_to_scan) || (j >= max_array))
1078 break;
1079
1080 ASSERT(list_link_active(&zp->z_link_node));
1081 list_remove(&zsb->z_all_znodes, zp);
1082 list_insert_tail(&zsb->z_all_znodes, zp);
1083
1084 /* Skip active znodes and .zfs entries */
1085 if (MUTEX_HELD(&zp->z_lock) || zp->z_is_ctldir)
1086 continue;
1087
1088 if (igrab(ZTOI(zp)) == NULL)
1089 continue;
1090
1091 zp_array[j] = zp;
1092 j++;
1093 }
1094 mutex_exit(&zsb->z_znodes_lock);
1095
1096 for (i = 0; i < j; i++) {
1097 zp = zp_array[i];
1098
1099 ASSERT3P(zp, !=, NULL);
1100 d_prune_aliases(ZTOI(zp));
1101
1102 if (atomic_read(&ZTOI(zp)->i_count) == 1)
1103 objects++;
1104
1105 iput(ZTOI(zp));
1106 }
1107
1108 kmem_free(zp_array, max_array * sizeof (znode_t *));
1109
1110 return (objects);
1111 }
1112 #endif /* HAVE_D_PRUNE_ALIASES */
1113
1114 /*
1115 * The ARC has requested that the filesystem drop entries from the dentry
1116 * and inode caches. This can occur when the ARC needs to free meta data
1117 * blocks but can't because they are all pinned by entries in these caches.
1118 */
1119 int
1120 zfs_sb_prune(struct super_block *sb, unsigned long nr_to_scan, int *objects)
1121 {
1122 zfs_sb_t *zsb = sb->s_fs_info;
1123 int error = 0;
1124 #if defined(HAVE_SHRINK) || defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1125 struct shrinker *shrinker = &sb->s_shrink;
1126 struct shrink_control sc = {
1127 .nr_to_scan = nr_to_scan,
1128 .gfp_mask = GFP_KERNEL,
1129 };
1130 #endif
1131
1132 ZFS_ENTER(zsb);
1133
1134 #if defined(HAVE_SPLIT_SHRINKER_CALLBACK) && \
1135 defined(SHRINK_CONTROL_HAS_NID) && \
1136 defined(SHRINKER_NUMA_AWARE)
1137 if (sb->s_shrink.flags & SHRINKER_NUMA_AWARE) {
1138 *objects = 0;
1139 for_each_online_node(sc.nid)
1140 *objects += (*shrinker->scan_objects)(shrinker, &sc);
1141 } else {
1142 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1143 }
1144
1145 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1146 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1147 #elif defined(HAVE_SHRINK)
1148 *objects = (*shrinker->shrink)(shrinker, &sc);
1149 #elif defined(HAVE_D_PRUNE_ALIASES)
1150 #define D_PRUNE_ALIASES_IS_DEFAULT
1151 *objects = zfs_sb_prune_aliases(zsb, nr_to_scan);
1152 #else
1153 #error "No available dentry and inode cache pruning mechanism."
1154 #endif
1155
1156 #if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1157 #undef D_PRUNE_ALIASES_IS_DEFAULT
1158 /*
1159 * Fall back to zfs_sb_prune_aliases if the kernel's per-superblock
1160 * shrinker couldn't free anything, possibly due to the inodes being
1161 * allocated in a different memcg.
1162 */
1163 if (*objects == 0)
1164 *objects = zfs_sb_prune_aliases(zsb, nr_to_scan);
1165 #endif
1166
1167 ZFS_EXIT(zsb);
1168
1169 dprintf_ds(zsb->z_os->os_dsl_dataset,
1170 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1171 nr_to_scan, *objects, error);
1172
1173 return (error);
1174 }
1175 EXPORT_SYMBOL(zfs_sb_prune);
1176
1177 /*
1178 * Teardown the zfs_sb_t.
1179 *
1180 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1181 * and 'z_teardown_inactive_lock' held.
1182 */
1183 int
1184 zfs_sb_teardown(zfs_sb_t *zsb, boolean_t unmounting)
1185 {
1186 znode_t *zp;
1187
1188 /*
1189 * If someone has not already unmounted this file system,
1190 * drain the iput_taskq to ensure all active references to the
1191 * zfs_sb_t have been handled only then can it be safely destroyed.
1192 */
1193 if (zsb->z_os) {
1194 /*
1195 * If we're unmounting we have to wait for the list to
1196 * drain completely.
1197 *
1198 * If we're not unmounting there's no guarantee the list
1199 * will drain completely, but iputs run from the taskq
1200 * may add the parents of dir-based xattrs to the taskq
1201 * so we want to wait for these.
1202 *
1203 * We can safely read z_nr_znodes without locking because the
1204 * VFS has already blocked operations which add to the
1205 * z_all_znodes list and thus increment z_nr_znodes.
1206 */
1207 int round = 0;
1208 while (zsb->z_nr_znodes > 0) {
1209 taskq_wait_outstanding(dsl_pool_iput_taskq(
1210 dmu_objset_pool(zsb->z_os)), 0);
1211 if (++round > 1 && !unmounting)
1212 break;
1213 }
1214 }
1215
1216 rrm_enter(&zsb->z_teardown_lock, RW_WRITER, FTAG);
1217
1218 if (!unmounting) {
1219 /*
1220 * We purge the parent filesystem's super block as the
1221 * parent filesystem and all of its snapshots have their
1222 * inode's super block set to the parent's filesystem's
1223 * super block. Note, 'z_parent' is self referential
1224 * for non-snapshots.
1225 */
1226 shrink_dcache_sb(zsb->z_parent->z_sb);
1227 }
1228
1229 /*
1230 * Close the zil. NB: Can't close the zil while zfs_inactive
1231 * threads are blocked as zil_close can call zfs_inactive.
1232 */
1233 if (zsb->z_log) {
1234 zil_close(zsb->z_log);
1235 zsb->z_log = NULL;
1236 }
1237
1238 rw_enter(&zsb->z_teardown_inactive_lock, RW_WRITER);
1239
1240 /*
1241 * If we are not unmounting (ie: online recv) and someone already
1242 * unmounted this file system while we were doing the switcheroo,
1243 * or a reopen of z_os failed then just bail out now.
1244 */
1245 if (!unmounting && (zsb->z_unmounted || zsb->z_os == NULL)) {
1246 rw_exit(&zsb->z_teardown_inactive_lock);
1247 rrm_exit(&zsb->z_teardown_lock, FTAG);
1248 return (SET_ERROR(EIO));
1249 }
1250
1251 /*
1252 * At this point there are no VFS ops active, and any new VFS ops
1253 * will fail with EIO since we have z_teardown_lock for writer (only
1254 * relevant for forced unmount).
1255 *
1256 * Release all holds on dbufs.
1257 */
1258 if (!unmounting) {
1259 mutex_enter(&zsb->z_znodes_lock);
1260 for (zp = list_head(&zsb->z_all_znodes); zp != NULL;
1261 zp = list_next(&zsb->z_all_znodes, zp)) {
1262 if (zp->z_sa_hdl)
1263 zfs_znode_dmu_fini(zp);
1264 }
1265 mutex_exit(&zsb->z_znodes_lock);
1266 }
1267
1268 /*
1269 * If we are unmounting, set the unmounted flag and let new VFS ops
1270 * unblock. zfs_inactive will have the unmounted behavior, and all
1271 * other VFS ops will fail with EIO.
1272 */
1273 if (unmounting) {
1274 zsb->z_unmounted = B_TRUE;
1275 rrm_exit(&zsb->z_teardown_lock, FTAG);
1276 rw_exit(&zsb->z_teardown_inactive_lock);
1277 }
1278
1279 /*
1280 * z_os will be NULL if there was an error in attempting to reopen
1281 * zsb, so just return as the properties had already been
1282 *
1283 * unregistered and cached data had been evicted before.
1284 */
1285 if (zsb->z_os == NULL)
1286 return (0);
1287
1288 /*
1289 * Unregister properties.
1290 */
1291 zfs_unregister_callbacks(zsb);
1292
1293 /*
1294 * Evict cached data
1295 */
1296 if (dsl_dataset_is_dirty(dmu_objset_ds(zsb->z_os)) &&
1297 !zfs_is_readonly(zsb))
1298 txg_wait_synced(dmu_objset_pool(zsb->z_os), 0);
1299 dmu_objset_evict_dbufs(zsb->z_os);
1300
1301 return (0);
1302 }
1303 EXPORT_SYMBOL(zfs_sb_teardown);
1304
1305 #if !defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER) && \
1306 !defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
1307 atomic_long_t zfs_bdi_seq = ATOMIC_LONG_INIT(0);
1308 #endif
1309
1310 int
1311 zfs_domount(struct super_block *sb, zfs_mntopts_t *zmo, int silent)
1312 {
1313 const char *osname = zmo->z_osname;
1314 zfs_sb_t *zsb;
1315 struct inode *root_inode;
1316 uint64_t recordsize;
1317 int error;
1318
1319 error = zfs_sb_create(osname, zmo, &zsb);
1320 if (error)
1321 return (error);
1322
1323 if ((error = dsl_prop_get_integer(osname, "recordsize",
1324 &recordsize, NULL)))
1325 goto out;
1326
1327 zsb->z_sb = sb;
1328 sb->s_fs_info = zsb;
1329 sb->s_magic = ZFS_SUPER_MAGIC;
1330 sb->s_maxbytes = MAX_LFS_FILESIZE;
1331 sb->s_time_gran = 1;
1332 sb->s_blocksize = recordsize;
1333 sb->s_blocksize_bits = ilog2(recordsize);
1334 zsb->z_bdi.ra_pages = 0;
1335 sb->s_bdi = &zsb->z_bdi;
1336
1337 error = -zpl_bdi_setup_and_register(&zsb->z_bdi, "zfs");
1338 if (error)
1339 goto out;
1340
1341 /* Set callback operations for the file system. */
1342 sb->s_op = &zpl_super_operations;
1343 sb->s_xattr = zpl_xattr_handlers;
1344 sb->s_export_op = &zpl_export_operations;
1345 #ifdef HAVE_S_D_OP
1346 sb->s_d_op = &zpl_dentry_operations;
1347 #endif /* HAVE_S_D_OP */
1348
1349 /* Set features for file system. */
1350 zfs_set_fuid_feature(zsb);
1351
1352 if (dmu_objset_is_snapshot(zsb->z_os)) {
1353 uint64_t pval;
1354
1355 atime_changed_cb(zsb, B_FALSE);
1356 readonly_changed_cb(zsb, B_TRUE);
1357 if ((error = dsl_prop_get_integer(osname,
1358 "xattr", &pval, NULL)))
1359 goto out;
1360 xattr_changed_cb(zsb, pval);
1361 if ((error = dsl_prop_get_integer(osname,
1362 "acltype", &pval, NULL)))
1363 goto out;
1364 acltype_changed_cb(zsb, pval);
1365 zsb->z_issnap = B_TRUE;
1366 zsb->z_os->os_sync = ZFS_SYNC_DISABLED;
1367 zsb->z_snap_defer_time = jiffies;
1368
1369 mutex_enter(&zsb->z_os->os_user_ptr_lock);
1370 dmu_objset_set_user(zsb->z_os, zsb);
1371 mutex_exit(&zsb->z_os->os_user_ptr_lock);
1372 } else {
1373 if ((error = zfs_sb_setup(zsb, B_TRUE)))
1374 goto out;
1375 }
1376
1377 /* Allocate a root inode for the filesystem. */
1378 error = zfs_root(zsb, &root_inode);
1379 if (error) {
1380 (void) zfs_umount(sb);
1381 goto out;
1382 }
1383
1384 /* Allocate a root dentry for the filesystem */
1385 sb->s_root = d_make_root(root_inode);
1386 if (sb->s_root == NULL) {
1387 (void) zfs_umount(sb);
1388 error = SET_ERROR(ENOMEM);
1389 goto out;
1390 }
1391
1392 if (!zsb->z_issnap)
1393 zfsctl_create(zsb);
1394
1395 zsb->z_arc_prune = arc_add_prune_callback(zpl_prune_sb, sb);
1396 out:
1397 if (error) {
1398 dmu_objset_disown(zsb->z_os, zsb);
1399 zfs_sb_free(zsb);
1400 /*
1401 * make sure we don't have dangling sb->s_fs_info which
1402 * zfs_preumount will use.
1403 */
1404 sb->s_fs_info = NULL;
1405 }
1406
1407 return (error);
1408 }
1409 EXPORT_SYMBOL(zfs_domount);
1410
1411 /*
1412 * Called when an unmount is requested and certain sanity checks have
1413 * already passed. At this point no dentries or inodes have been reclaimed
1414 * from their respective caches. We drop the extra reference on the .zfs
1415 * control directory to allow everything to be reclaimed. All snapshots
1416 * must already have been unmounted to reach this point.
1417 */
1418 void
1419 zfs_preumount(struct super_block *sb)
1420 {
1421 zfs_sb_t *zsb = sb->s_fs_info;
1422
1423 /* zsb is NULL when zfs_domount fails during mount */
1424 if (zsb) {
1425 zfsctl_destroy(sb->s_fs_info);
1426 /*
1427 * Wait for iput_async before entering evict_inodes in
1428 * generic_shutdown_super. The reason we must finish before
1429 * evict_inodes is when lazytime is on, or when zfs_purgedir
1430 * calls zfs_zget, iput would bump i_count from 0 to 1. This
1431 * would race with the i_count check in evict_inodes. This means
1432 * it could destroy the inode while we are still using it.
1433 *
1434 * We wait for two passes. xattr directories in the first pass
1435 * may add xattr entries in zfs_purgedir, so in the second pass
1436 * we wait for them. We don't use taskq_wait here because it is
1437 * a pool wide taskq. Other mounted filesystems can constantly
1438 * do iput_async and there's no guarantee when taskq will be
1439 * empty.
1440 */
1441 taskq_wait_outstanding(dsl_pool_iput_taskq(
1442 dmu_objset_pool(zsb->z_os)), 0);
1443 taskq_wait_outstanding(dsl_pool_iput_taskq(
1444 dmu_objset_pool(zsb->z_os)), 0);
1445 }
1446 }
1447 EXPORT_SYMBOL(zfs_preumount);
1448
1449 /*
1450 * Called once all other unmount released tear down has occurred.
1451 * It is our responsibility to release any remaining infrastructure.
1452 */
1453 /*ARGSUSED*/
1454 int
1455 zfs_umount(struct super_block *sb)
1456 {
1457 zfs_sb_t *zsb = sb->s_fs_info;
1458 objset_t *os;
1459
1460 arc_remove_prune_callback(zsb->z_arc_prune);
1461 VERIFY(zfs_sb_teardown(zsb, B_TRUE) == 0);
1462 os = zsb->z_os;
1463 bdi_destroy(sb->s_bdi);
1464
1465 /*
1466 * z_os will be NULL if there was an error in
1467 * attempting to reopen zsb.
1468 */
1469 if (os != NULL) {
1470 /*
1471 * Unset the objset user_ptr.
1472 */
1473 mutex_enter(&os->os_user_ptr_lock);
1474 dmu_objset_set_user(os, NULL);
1475 mutex_exit(&os->os_user_ptr_lock);
1476
1477 /*
1478 * Finally release the objset
1479 */
1480 dmu_objset_disown(os, zsb);
1481 }
1482
1483 zfs_sb_free(zsb);
1484 return (0);
1485 }
1486 EXPORT_SYMBOL(zfs_umount);
1487
1488 int
1489 zfs_remount(struct super_block *sb, int *flags, zfs_mntopts_t *zmo)
1490 {
1491 zfs_sb_t *zsb = sb->s_fs_info;
1492 int error;
1493
1494 zfs_unregister_callbacks(zsb);
1495 error = zfs_register_callbacks(zsb);
1496
1497 return (error);
1498 }
1499 EXPORT_SYMBOL(zfs_remount);
1500
1501 int
1502 zfs_vget(struct super_block *sb, struct inode **ipp, fid_t *fidp)
1503 {
1504 zfs_sb_t *zsb = sb->s_fs_info;
1505 znode_t *zp;
1506 uint64_t object = 0;
1507 uint64_t fid_gen = 0;
1508 uint64_t gen_mask;
1509 uint64_t zp_gen;
1510 int i, err;
1511
1512 *ipp = NULL;
1513
1514 ZFS_ENTER(zsb);
1515
1516 if (fidp->fid_len == LONG_FID_LEN) {
1517 zfid_long_t *zlfid = (zfid_long_t *)fidp;
1518 uint64_t objsetid = 0;
1519 uint64_t setgen = 0;
1520
1521 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
1522 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
1523
1524 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
1525 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
1526
1527 ZFS_EXIT(zsb);
1528
1529 err = zfsctl_lookup_objset(sb, objsetid, &zsb);
1530 if (err)
1531 return (SET_ERROR(EINVAL));
1532
1533 ZFS_ENTER(zsb);
1534 }
1535
1536 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
1537 zfid_short_t *zfid = (zfid_short_t *)fidp;
1538
1539 for (i = 0; i < sizeof (zfid->zf_object); i++)
1540 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
1541
1542 for (i = 0; i < sizeof (zfid->zf_gen); i++)
1543 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
1544 } else {
1545 ZFS_EXIT(zsb);
1546 return (SET_ERROR(EINVAL));
1547 }
1548
1549 /* A zero fid_gen means we are in the .zfs control directories */
1550 if (fid_gen == 0 &&
1551 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
1552 *ipp = zsb->z_ctldir;
1553 ASSERT(*ipp != NULL);
1554 if (object == ZFSCTL_INO_SNAPDIR) {
1555 VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp,
1556 0, kcred, NULL, NULL) == 0);
1557 } else {
1558 igrab(*ipp);
1559 }
1560 ZFS_EXIT(zsb);
1561 return (0);
1562 }
1563
1564 gen_mask = -1ULL >> (64 - 8 * i);
1565
1566 dprintf("getting %llu [%llu mask %llx]\n", object, fid_gen, gen_mask);
1567 if ((err = zfs_zget(zsb, object, &zp))) {
1568 ZFS_EXIT(zsb);
1569 return (err);
1570 }
1571
1572 /* Don't export xattr stuff */
1573 if (zp->z_pflags & ZFS_XATTR) {
1574 iput(ZTOI(zp));
1575 ZFS_EXIT(zsb);
1576 return (SET_ERROR(ENOENT));
1577 }
1578
1579 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb), &zp_gen,
1580 sizeof (uint64_t));
1581 zp_gen = zp_gen & gen_mask;
1582 if (zp_gen == 0)
1583 zp_gen = 1;
1584 if ((fid_gen == 0) && (zsb->z_root == object))
1585 fid_gen = zp_gen;
1586 if (zp->z_unlinked || zp_gen != fid_gen) {
1587 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen,
1588 fid_gen);
1589 iput(ZTOI(zp));
1590 ZFS_EXIT(zsb);
1591 return (SET_ERROR(ENOENT));
1592 }
1593
1594 *ipp = ZTOI(zp);
1595 if (*ipp)
1596 zfs_inode_update(ITOZ(*ipp));
1597
1598 ZFS_EXIT(zsb);
1599 return (0);
1600 }
1601 EXPORT_SYMBOL(zfs_vget);
1602
1603 /*
1604 * Block out VFS ops and close zfs_sb_t
1605 *
1606 * Note, if successful, then we return with the 'z_teardown_lock' and
1607 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1608 * dataset and objset intact so that they can be atomically handed off during
1609 * a subsequent rollback or recv operation and the resume thereafter.
1610 */
1611 int
1612 zfs_suspend_fs(zfs_sb_t *zsb)
1613 {
1614 int error;
1615
1616 if ((error = zfs_sb_teardown(zsb, B_FALSE)) != 0)
1617 return (error);
1618
1619 return (0);
1620 }
1621 EXPORT_SYMBOL(zfs_suspend_fs);
1622
1623 /*
1624 * Reopen zfs_sb_t and release VFS ops.
1625 */
1626 int
1627 zfs_resume_fs(zfs_sb_t *zsb, const char *osname)
1628 {
1629 int err, err2;
1630 znode_t *zp;
1631 uint64_t sa_obj = 0;
1632
1633 ASSERT(RRM_WRITE_HELD(&zsb->z_teardown_lock));
1634 ASSERT(RW_WRITE_HELD(&zsb->z_teardown_inactive_lock));
1635
1636 /*
1637 * We already own this, so just hold and rele it to update the
1638 * objset_t, as the one we had before may have been evicted.
1639 */
1640 VERIFY0(dmu_objset_hold(osname, zsb, &zsb->z_os));
1641 VERIFY3P(zsb->z_os->os_dsl_dataset->ds_owner, ==, zsb);
1642 VERIFY(dsl_dataset_long_held(zsb->z_os->os_dsl_dataset));
1643 dmu_objset_rele(zsb->z_os, zsb);
1644
1645 /*
1646 * Make sure version hasn't changed
1647 */
1648
1649 err = zfs_get_zplprop(zsb->z_os, ZFS_PROP_VERSION,
1650 &zsb->z_version);
1651
1652 if (err)
1653 goto bail;
1654
1655 err = zap_lookup(zsb->z_os, MASTER_NODE_OBJ,
1656 ZFS_SA_ATTRS, 8, 1, &sa_obj);
1657
1658 if (err && zsb->z_version >= ZPL_VERSION_SA)
1659 goto bail;
1660
1661 if ((err = sa_setup(zsb->z_os, sa_obj,
1662 zfs_attr_table, ZPL_END, &zsb->z_attr_table)) != 0)
1663 goto bail;
1664
1665 if (zsb->z_version >= ZPL_VERSION_SA)
1666 sa_register_update_callback(zsb->z_os,
1667 zfs_sa_upgrade);
1668
1669 VERIFY(zfs_sb_setup(zsb, B_FALSE) == 0);
1670
1671 zfs_set_fuid_feature(zsb);
1672 zsb->z_rollback_time = jiffies;
1673
1674 /*
1675 * Attempt to re-establish all the active inodes with their
1676 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1677 * and mark it stale. This prevents a collision if a new
1678 * inode/object is created which must use the same inode
1679 * number. The stale inode will be be released when the
1680 * VFS prunes the dentry holding the remaining references
1681 * on the stale inode.
1682 */
1683 mutex_enter(&zsb->z_znodes_lock);
1684 for (zp = list_head(&zsb->z_all_znodes); zp;
1685 zp = list_next(&zsb->z_all_znodes, zp)) {
1686 err2 = zfs_rezget(zp);
1687 if (err2) {
1688 remove_inode_hash(ZTOI(zp));
1689 zp->z_is_stale = B_TRUE;
1690 }
1691 }
1692 mutex_exit(&zsb->z_znodes_lock);
1693
1694 bail:
1695 /* release the VFS ops */
1696 rw_exit(&zsb->z_teardown_inactive_lock);
1697 rrm_exit(&zsb->z_teardown_lock, FTAG);
1698
1699 if (err) {
1700 /*
1701 * Since we couldn't setup the sa framework, try to force
1702 * unmount this file system.
1703 */
1704 if (zsb->z_os)
1705 (void) zfs_umount(zsb->z_sb);
1706 }
1707 return (err);
1708 }
1709 EXPORT_SYMBOL(zfs_resume_fs);
1710
1711 int
1712 zfs_set_version(zfs_sb_t *zsb, uint64_t newvers)
1713 {
1714 int error;
1715 objset_t *os = zsb->z_os;
1716 dmu_tx_t *tx;
1717
1718 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
1719 return (SET_ERROR(EINVAL));
1720
1721 if (newvers < zsb->z_version)
1722 return (SET_ERROR(EINVAL));
1723
1724 if (zfs_spa_version_map(newvers) >
1725 spa_version(dmu_objset_spa(zsb->z_os)))
1726 return (SET_ERROR(ENOTSUP));
1727
1728 tx = dmu_tx_create(os);
1729 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
1730 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
1731 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
1732 ZFS_SA_ATTRS);
1733 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1734 }
1735 error = dmu_tx_assign(tx, TXG_WAIT);
1736 if (error) {
1737 dmu_tx_abort(tx);
1738 return (error);
1739 }
1740
1741 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1742 8, 1, &newvers, tx);
1743
1744 if (error) {
1745 dmu_tx_commit(tx);
1746 return (error);
1747 }
1748
1749 if (newvers >= ZPL_VERSION_SA && !zsb->z_use_sa) {
1750 uint64_t sa_obj;
1751
1752 ASSERT3U(spa_version(dmu_objset_spa(zsb->z_os)), >=,
1753 SPA_VERSION_SA);
1754 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1755 DMU_OT_NONE, 0, tx);
1756
1757 error = zap_add(os, MASTER_NODE_OBJ,
1758 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1759 ASSERT0(error);
1760
1761 VERIFY(0 == sa_set_sa_object(os, sa_obj));
1762 sa_register_update_callback(os, zfs_sa_upgrade);
1763 }
1764
1765 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
1766 "from %llu to %llu", zsb->z_version, newvers);
1767
1768 dmu_tx_commit(tx);
1769
1770 zsb->z_version = newvers;
1771
1772 zfs_set_fuid_feature(zsb);
1773
1774 return (0);
1775 }
1776 EXPORT_SYMBOL(zfs_set_version);
1777
1778 /*
1779 * Read a property stored within the master node.
1780 */
1781 int
1782 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
1783 {
1784 const char *pname;
1785 int error = SET_ERROR(ENOENT);
1786
1787 /*
1788 * Look up the file system's value for the property. For the
1789 * version property, we look up a slightly different string.
1790 */
1791 if (prop == ZFS_PROP_VERSION)
1792 pname = ZPL_VERSION_STR;
1793 else
1794 pname = zfs_prop_to_name(prop);
1795
1796 if (os != NULL)
1797 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
1798
1799 if (error == ENOENT) {
1800 /* No value set, use the default value */
1801 switch (prop) {
1802 case ZFS_PROP_VERSION:
1803 *value = ZPL_VERSION;
1804 break;
1805 case ZFS_PROP_NORMALIZE:
1806 case ZFS_PROP_UTF8ONLY:
1807 *value = 0;
1808 break;
1809 case ZFS_PROP_CASE:
1810 *value = ZFS_CASE_SENSITIVE;
1811 break;
1812 case ZFS_PROP_ACLTYPE:
1813 *value = ZFS_ACLTYPE_OFF;
1814 break;
1815 default:
1816 return (error);
1817 }
1818 error = 0;
1819 }
1820 return (error);
1821 }
1822 EXPORT_SYMBOL(zfs_get_zplprop);
1823
1824 void
1825 zfs_init(void)
1826 {
1827 zfsctl_init();
1828 zfs_znode_init();
1829 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
1830 register_filesystem(&zpl_fs_type);
1831 }
1832
1833 void
1834 zfs_fini(void)
1835 {
1836 taskq_wait_outstanding(system_taskq, 0);
1837 unregister_filesystem(&zpl_fs_type);
1838 zfs_znode_fini();
1839 zfsctl_fini();
1840 }