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