]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_vfsops.c
2577860acb0e6f5c96bd2ed474321420b3d16c14
[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, uint64_t *projectp)
540 {
541 sa_hdr_phys_t sa;
542 sa_hdr_phys_t *sap = data;
543 uint64_t flags;
544 int hdrsize;
545 boolean_t swap = B_FALSE;
546
547 /*
548 * Is it a valid type of object to track?
549 */
550 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
551 return (SET_ERROR(ENOENT));
552
553 /*
554 * If we have a NULL data pointer
555 * then assume the id's aren't changing and
556 * return EEXIST to the dmu to let it know to
557 * use the same ids
558 */
559 if (data == NULL)
560 return (SET_ERROR(EEXIST));
561
562 if (bonustype == DMU_OT_ZNODE) {
563 znode_phys_t *znp = data;
564 *userp = znp->zp_uid;
565 *groupp = znp->zp_gid;
566 *projectp = ZFS_DEFAULT_PROJID;
567 return (0);
568 }
569
570 if (sap->sa_magic == 0) {
571 /*
572 * This should only happen for newly created files
573 * that haven't had the znode data filled in yet.
574 */
575 *userp = 0;
576 *groupp = 0;
577 *projectp = ZFS_DEFAULT_PROJID;
578 return (0);
579 }
580
581 sa = *sap;
582 if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
583 sa.sa_magic = SA_MAGIC;
584 sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
585 swap = B_TRUE;
586 } else {
587 VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
588 }
589
590 hdrsize = sa_hdrsize(&sa);
591 VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
592
593 *userp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_UID_OFFSET));
594 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_GID_OFFSET));
595 flags = *((uint64_t *)((uintptr_t)data + hdrsize + SA_FLAGS_OFFSET));
596 if (swap)
597 flags = BSWAP_64(flags);
598
599 if (flags & ZFS_PROJID)
600 *projectp = *((uint64_t *)((uintptr_t)data + hdrsize +
601 SA_PROJID_OFFSET));
602 else
603 *projectp = ZFS_DEFAULT_PROJID;
604
605 if (swap) {
606 *userp = BSWAP_64(*userp);
607 *groupp = BSWAP_64(*groupp);
608 *projectp = BSWAP_64(*projectp);
609 }
610 return (0);
611 }
612
613 static void
614 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
615 char *domainbuf, int buflen, uid_t *ridp)
616 {
617 uint64_t fuid;
618 const char *domain;
619
620 fuid = zfs_strtonum(fuidstr, NULL);
621
622 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
623 if (domain)
624 (void) strlcpy(domainbuf, domain, buflen);
625 else
626 domainbuf[0] = '\0';
627 *ridp = FUID_RID(fuid);
628 }
629
630 static uint64_t
631 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
632 {
633 switch (type) {
634 case ZFS_PROP_USERUSED:
635 case ZFS_PROP_USEROBJUSED:
636 return (DMU_USERUSED_OBJECT);
637 case ZFS_PROP_GROUPUSED:
638 case ZFS_PROP_GROUPOBJUSED:
639 return (DMU_GROUPUSED_OBJECT);
640 case ZFS_PROP_PROJECTUSED:
641 case ZFS_PROP_PROJECTOBJUSED:
642 return (DMU_PROJECTUSED_OBJECT);
643 case ZFS_PROP_USERQUOTA:
644 return (zfsvfs->z_userquota_obj);
645 case ZFS_PROP_GROUPQUOTA:
646 return (zfsvfs->z_groupquota_obj);
647 case ZFS_PROP_USEROBJQUOTA:
648 return (zfsvfs->z_userobjquota_obj);
649 case ZFS_PROP_GROUPOBJQUOTA:
650 return (zfsvfs->z_groupobjquota_obj);
651 case ZFS_PROP_PROJECTQUOTA:
652 return (zfsvfs->z_projectquota_obj);
653 case ZFS_PROP_PROJECTOBJQUOTA:
654 return (zfsvfs->z_projectobjquota_obj);
655 default:
656 return (ZFS_NO_OBJECT);
657 }
658 }
659
660 int
661 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
662 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
663 {
664 int error;
665 zap_cursor_t zc;
666 zap_attribute_t za;
667 zfs_useracct_t *buf = vbuf;
668 uint64_t obj;
669 int offset = 0;
670
671 if (!dmu_objset_userspace_present(zfsvfs->z_os))
672 return (SET_ERROR(ENOTSUP));
673
674 if ((type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
675 type == ZFS_PROP_PROJECTOBJQUOTA ||
676 type == ZFS_PROP_PROJECTOBJUSED) &&
677 !dmu_objset_projectquota_present(zfsvfs->z_os))
678 return (SET_ERROR(ENOTSUP));
679
680 if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
681 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
682 type == ZFS_PROP_PROJECTOBJUSED ||
683 type == ZFS_PROP_PROJECTOBJQUOTA) &&
684 !dmu_objset_userobjspace_present(zfsvfs->z_os))
685 return (SET_ERROR(ENOTSUP));
686
687 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
688 if (obj == ZFS_NO_OBJECT) {
689 *bufsizep = 0;
690 return (0);
691 }
692
693 if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
694 type == ZFS_PROP_PROJECTOBJUSED)
695 offset = DMU_OBJACCT_PREFIX_LEN;
696
697 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
698 (error = zap_cursor_retrieve(&zc, &za)) == 0;
699 zap_cursor_advance(&zc)) {
700 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
701 *bufsizep)
702 break;
703
704 /*
705 * skip object quota (with zap name prefix DMU_OBJACCT_PREFIX)
706 * when dealing with block quota and vice versa.
707 */
708 if ((offset > 0) != (strncmp(za.za_name, DMU_OBJACCT_PREFIX,
709 DMU_OBJACCT_PREFIX_LEN) == 0))
710 continue;
711
712 fuidstr_to_sid(zfsvfs, za.za_name + offset,
713 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
714
715 buf->zu_space = za.za_first_integer;
716 buf++;
717 }
718 if (error == ENOENT)
719 error = 0;
720
721 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
722 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
723 *cookiep = zap_cursor_serialize(&zc);
724 zap_cursor_fini(&zc);
725 return (error);
726 }
727
728 /*
729 * buf must be big enough (eg, 32 bytes)
730 */
731 static int
732 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
733 char *buf, boolean_t addok)
734 {
735 uint64_t fuid;
736 int domainid = 0;
737
738 if (domain && domain[0]) {
739 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
740 if (domainid == -1)
741 return (SET_ERROR(ENOENT));
742 }
743 fuid = FUID_ENCODE(domainid, rid);
744 (void) sprintf(buf, "%llx", (longlong_t)fuid);
745 return (0);
746 }
747
748 int
749 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
750 const char *domain, uint64_t rid, uint64_t *valp)
751 {
752 char buf[20 + DMU_OBJACCT_PREFIX_LEN];
753 int offset = 0;
754 int err;
755 uint64_t obj;
756
757 *valp = 0;
758
759 if (!dmu_objset_userspace_present(zfsvfs->z_os))
760 return (SET_ERROR(ENOTSUP));
761
762 if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
763 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
764 type == ZFS_PROP_PROJECTOBJUSED ||
765 type == ZFS_PROP_PROJECTOBJQUOTA) &&
766 !dmu_objset_userobjspace_present(zfsvfs->z_os))
767 return (SET_ERROR(ENOTSUP));
768
769 if (type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
770 type == ZFS_PROP_PROJECTOBJQUOTA ||
771 type == ZFS_PROP_PROJECTOBJUSED) {
772 if (!dmu_objset_projectquota_present(zfsvfs->z_os))
773 return (SET_ERROR(ENOTSUP));
774 if (!zpl_is_valid_projid(rid))
775 return (SET_ERROR(EINVAL));
776 }
777
778 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
779 if (obj == ZFS_NO_OBJECT)
780 return (0);
781
782 if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
783 type == ZFS_PROP_PROJECTOBJUSED) {
784 strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
785 offset = DMU_OBJACCT_PREFIX_LEN;
786 }
787
788 err = id_to_fuidstr(zfsvfs, domain, rid, buf + offset, B_FALSE);
789 if (err)
790 return (err);
791
792 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
793 if (err == ENOENT)
794 err = 0;
795 return (err);
796 }
797
798 int
799 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
800 const char *domain, uint64_t rid, uint64_t quota)
801 {
802 char buf[32];
803 int err;
804 dmu_tx_t *tx;
805 uint64_t *objp;
806 boolean_t fuid_dirtied;
807
808 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
809 return (SET_ERROR(ENOTSUP));
810
811 switch (type) {
812 case ZFS_PROP_USERQUOTA:
813 objp = &zfsvfs->z_userquota_obj;
814 break;
815 case ZFS_PROP_GROUPQUOTA:
816 objp = &zfsvfs->z_groupquota_obj;
817 break;
818 case ZFS_PROP_USEROBJQUOTA:
819 objp = &zfsvfs->z_userobjquota_obj;
820 break;
821 case ZFS_PROP_GROUPOBJQUOTA:
822 objp = &zfsvfs->z_groupobjquota_obj;
823 break;
824 case ZFS_PROP_PROJECTQUOTA:
825 if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
826 return (SET_ERROR(ENOTSUP));
827 if (!zpl_is_valid_projid(rid))
828 return (SET_ERROR(EINVAL));
829
830 objp = &zfsvfs->z_projectquota_obj;
831 break;
832 case ZFS_PROP_PROJECTOBJQUOTA:
833 if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
834 return (SET_ERROR(ENOTSUP));
835 if (!zpl_is_valid_projid(rid))
836 return (SET_ERROR(EINVAL));
837
838 objp = &zfsvfs->z_projectobjquota_obj;
839 break;
840 default:
841 return (SET_ERROR(EINVAL));
842 }
843
844 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
845 if (err)
846 return (err);
847 fuid_dirtied = zfsvfs->z_fuid_dirty;
848
849 tx = dmu_tx_create(zfsvfs->z_os);
850 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
851 if (*objp == 0) {
852 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
853 zfs_userquota_prop_prefixes[type]);
854 }
855 if (fuid_dirtied)
856 zfs_fuid_txhold(zfsvfs, tx);
857 err = dmu_tx_assign(tx, TXG_WAIT);
858 if (err) {
859 dmu_tx_abort(tx);
860 return (err);
861 }
862
863 mutex_enter(&zfsvfs->z_lock);
864 if (*objp == 0) {
865 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
866 DMU_OT_NONE, 0, tx);
867 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
868 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
869 }
870 mutex_exit(&zfsvfs->z_lock);
871
872 if (quota == 0) {
873 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
874 if (err == ENOENT)
875 err = 0;
876 } else {
877 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
878 }
879 ASSERT(err == 0);
880 if (fuid_dirtied)
881 zfs_fuid_sync(zfsvfs, tx);
882 dmu_tx_commit(tx);
883 return (err);
884 }
885
886 boolean_t
887 zfs_id_overobjquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
888 {
889 char buf[20 + DMU_OBJACCT_PREFIX_LEN];
890 uint64_t used, quota, quotaobj;
891 int err;
892
893 if (!dmu_objset_userobjspace_present(zfsvfs->z_os)) {
894 if (dmu_objset_userobjspace_upgradable(zfsvfs->z_os)) {
895 dsl_pool_config_enter(
896 dmu_objset_pool(zfsvfs->z_os), FTAG);
897 dmu_objset_id_quota_upgrade(zfsvfs->z_os);
898 dsl_pool_config_exit(
899 dmu_objset_pool(zfsvfs->z_os), FTAG);
900 }
901 return (B_FALSE);
902 }
903
904 if (usedobj == DMU_PROJECTUSED_OBJECT) {
905 if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
906 if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
907 dsl_pool_config_enter(
908 dmu_objset_pool(zfsvfs->z_os), FTAG);
909 dmu_objset_id_quota_upgrade(zfsvfs->z_os);
910 dsl_pool_config_exit(
911 dmu_objset_pool(zfsvfs->z_os), FTAG);
912 }
913 return (B_FALSE);
914 }
915 quotaobj = zfsvfs->z_projectobjquota_obj;
916 } else if (usedobj == DMU_USERUSED_OBJECT) {
917 quotaobj = zfsvfs->z_userobjquota_obj;
918 } else if (usedobj == DMU_GROUPUSED_OBJECT) {
919 quotaobj = zfsvfs->z_groupobjquota_obj;
920 } else {
921 return (B_FALSE);
922 }
923 if (quotaobj == 0 || zfsvfs->z_replay)
924 return (B_FALSE);
925
926 (void) sprintf(buf, "%llx", (longlong_t)id);
927 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
928 if (err != 0)
929 return (B_FALSE);
930
931 (void) sprintf(buf, DMU_OBJACCT_PREFIX "%llx", (longlong_t)id);
932 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
933 if (err != 0)
934 return (B_FALSE);
935 return (used >= quota);
936 }
937
938 boolean_t
939 zfs_id_overblockquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
940 {
941 char buf[20];
942 uint64_t used, quota, quotaobj;
943 int err;
944
945 if (usedobj == DMU_PROJECTUSED_OBJECT) {
946 if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
947 if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
948 dsl_pool_config_enter(
949 dmu_objset_pool(zfsvfs->z_os), FTAG);
950 dmu_objset_id_quota_upgrade(zfsvfs->z_os);
951 dsl_pool_config_exit(
952 dmu_objset_pool(zfsvfs->z_os), FTAG);
953 }
954 return (B_FALSE);
955 }
956 quotaobj = zfsvfs->z_projectquota_obj;
957 } else if (usedobj == DMU_USERUSED_OBJECT) {
958 quotaobj = zfsvfs->z_userquota_obj;
959 } else if (usedobj == DMU_GROUPUSED_OBJECT) {
960 quotaobj = zfsvfs->z_groupquota_obj;
961 } else {
962 return (B_FALSE);
963 }
964 if (quotaobj == 0 || zfsvfs->z_replay)
965 return (B_FALSE);
966
967 (void) sprintf(buf, "%llx", (longlong_t)id);
968 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
969 if (err != 0)
970 return (B_FALSE);
971
972 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
973 if (err != 0)
974 return (B_FALSE);
975 return (used >= quota);
976 }
977
978 boolean_t
979 zfs_id_overquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
980 {
981 return (zfs_id_overblockquota(zfsvfs, usedobj, id) ||
982 zfs_id_overobjquota(zfsvfs, usedobj, id));
983 }
984
985 /*
986 * Associate this zfsvfs with the given objset, which must be owned.
987 * This will cache a bunch of on-disk state from the objset in the
988 * zfsvfs.
989 */
990 static int
991 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
992 {
993 int error;
994 uint64_t val;
995
996 zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
997 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
998 zfsvfs->z_os = os;
999
1000 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
1001 if (error != 0)
1002 return (error);
1003 if (zfsvfs->z_version >
1004 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
1005 (void) printk("Can't mount a version %lld file system "
1006 "on a version %lld pool\n. Pool must be upgraded to mount "
1007 "this file system.", (u_longlong_t)zfsvfs->z_version,
1008 (u_longlong_t)spa_version(dmu_objset_spa(os)));
1009 return (SET_ERROR(ENOTSUP));
1010 }
1011 error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
1012 if (error != 0)
1013 return (error);
1014 zfsvfs->z_norm = (int)val;
1015
1016 error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
1017 if (error != 0)
1018 return (error);
1019 zfsvfs->z_utf8 = (val != 0);
1020
1021 error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
1022 if (error != 0)
1023 return (error);
1024 zfsvfs->z_case = (uint_t)val;
1025
1026 if ((error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &val)) != 0)
1027 return (error);
1028 zfsvfs->z_acl_type = (uint_t)val;
1029
1030 /*
1031 * Fold case on file systems that are always or sometimes case
1032 * insensitive.
1033 */
1034 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
1035 zfsvfs->z_case == ZFS_CASE_MIXED)
1036 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1037
1038 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1039 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1040
1041 uint64_t sa_obj = 0;
1042 if (zfsvfs->z_use_sa) {
1043 /* should either have both of these objects or none */
1044 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
1045 &sa_obj);
1046 if (error != 0)
1047 return (error);
1048
1049 error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &val);
1050 if ((error == 0) && (val == ZFS_XATTR_SA))
1051 zfsvfs->z_xattr_sa = B_TRUE;
1052 }
1053
1054 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1055 &zfsvfs->z_attr_table);
1056 if (error != 0)
1057 return (error);
1058
1059 if (zfsvfs->z_version >= ZPL_VERSION_SA)
1060 sa_register_update_callback(os, zfs_sa_upgrade);
1061
1062 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
1063 &zfsvfs->z_root);
1064 if (error != 0)
1065 return (error);
1066 ASSERT(zfsvfs->z_root != 0);
1067
1068 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
1069 &zfsvfs->z_unlinkedobj);
1070 if (error != 0)
1071 return (error);
1072
1073 error = zap_lookup(os, MASTER_NODE_OBJ,
1074 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
1075 8, 1, &zfsvfs->z_userquota_obj);
1076 if (error == ENOENT)
1077 zfsvfs->z_userquota_obj = 0;
1078 else if (error != 0)
1079 return (error);
1080
1081 error = zap_lookup(os, MASTER_NODE_OBJ,
1082 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
1083 8, 1, &zfsvfs->z_groupquota_obj);
1084 if (error == ENOENT)
1085 zfsvfs->z_groupquota_obj = 0;
1086 else if (error != 0)
1087 return (error);
1088
1089 error = zap_lookup(os, MASTER_NODE_OBJ,
1090 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
1091 8, 1, &zfsvfs->z_projectquota_obj);
1092 if (error == ENOENT)
1093 zfsvfs->z_projectquota_obj = 0;
1094 else if (error != 0)
1095 return (error);
1096
1097 error = zap_lookup(os, MASTER_NODE_OBJ,
1098 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
1099 8, 1, &zfsvfs->z_userobjquota_obj);
1100 if (error == ENOENT)
1101 zfsvfs->z_userobjquota_obj = 0;
1102 else if (error != 0)
1103 return (error);
1104
1105 error = zap_lookup(os, MASTER_NODE_OBJ,
1106 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
1107 8, 1, &zfsvfs->z_groupobjquota_obj);
1108 if (error == ENOENT)
1109 zfsvfs->z_groupobjquota_obj = 0;
1110 else if (error != 0)
1111 return (error);
1112
1113 error = zap_lookup(os, MASTER_NODE_OBJ,
1114 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
1115 8, 1, &zfsvfs->z_projectobjquota_obj);
1116 if (error == ENOENT)
1117 zfsvfs->z_projectobjquota_obj = 0;
1118 else if (error != 0)
1119 return (error);
1120
1121 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
1122 &zfsvfs->z_fuid_obj);
1123 if (error == ENOENT)
1124 zfsvfs->z_fuid_obj = 0;
1125 else if (error != 0)
1126 return (error);
1127
1128 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
1129 &zfsvfs->z_shares_dir);
1130 if (error == ENOENT)
1131 zfsvfs->z_shares_dir = 0;
1132 else if (error != 0)
1133 return (error);
1134
1135 return (0);
1136 }
1137
1138 int
1139 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
1140 {
1141 objset_t *os;
1142 zfsvfs_t *zfsvfs;
1143 int error;
1144 boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
1145
1146 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1147
1148 error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs, &os);
1149 if (error != 0) {
1150 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1151 return (error);
1152 }
1153
1154 error = zfsvfs_create_impl(zfvp, zfsvfs, os);
1155 if (error != 0) {
1156 dmu_objset_disown(os, B_TRUE, zfsvfs);
1157 }
1158 return (error);
1159 }
1160
1161 int
1162 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
1163 {
1164 int error;
1165
1166 zfsvfs->z_vfs = NULL;
1167 zfsvfs->z_sb = NULL;
1168 zfsvfs->z_parent = zfsvfs;
1169
1170 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1171 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
1172 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1173 offsetof(znode_t, z_link_node));
1174 rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
1175 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
1176 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
1177
1178 int size = MIN(1 << (highbit64(zfs_object_mutex_size) - 1),
1179 ZFS_OBJ_MTX_MAX);
1180 zfsvfs->z_hold_size = size;
1181 zfsvfs->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size,
1182 KM_SLEEP);
1183 zfsvfs->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP);
1184 for (int i = 0; i != size; i++) {
1185 avl_create(&zfsvfs->z_hold_trees[i], zfs_znode_hold_compare,
1186 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node));
1187 mutex_init(&zfsvfs->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL);
1188 }
1189
1190 error = zfsvfs_init(zfsvfs, os);
1191 if (error != 0) {
1192 *zfvp = NULL;
1193 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1194 return (error);
1195 }
1196
1197 *zfvp = zfsvfs;
1198 return (0);
1199 }
1200
1201 static int
1202 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1203 {
1204 int error;
1205 boolean_t readonly = zfs_is_readonly(zfsvfs);
1206
1207 error = zfs_register_callbacks(zfsvfs->z_vfs);
1208 if (error)
1209 return (error);
1210
1211 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1212
1213 /*
1214 * If we are not mounting (ie: online recv), then we don't
1215 * have to worry about replaying the log as we blocked all
1216 * operations out since we closed the ZIL.
1217 */
1218 if (mounting) {
1219 /*
1220 * During replay we remove the read only flag to
1221 * allow replays to succeed.
1222 */
1223 if (readonly != 0)
1224 readonly_changed_cb(zfsvfs, B_FALSE);
1225 else
1226 zfs_unlinked_drain(zfsvfs);
1227
1228 /*
1229 * Parse and replay the intent log.
1230 *
1231 * Because of ziltest, this must be done after
1232 * zfs_unlinked_drain(). (Further note: ziltest
1233 * doesn't use readonly mounts, where
1234 * zfs_unlinked_drain() isn't called.) This is because
1235 * ziltest causes spa_sync() to think it's committed,
1236 * but actually it is not, so the intent log contains
1237 * many txg's worth of changes.
1238 *
1239 * In particular, if object N is in the unlinked set in
1240 * the last txg to actually sync, then it could be
1241 * actually freed in a later txg and then reallocated
1242 * in a yet later txg. This would write a "create
1243 * object N" record to the intent log. Normally, this
1244 * would be fine because the spa_sync() would have
1245 * written out the fact that object N is free, before
1246 * we could write the "create object N" intent log
1247 * record.
1248 *
1249 * But when we are in ziltest mode, we advance the "open
1250 * txg" without actually spa_sync()-ing the changes to
1251 * disk. So we would see that object N is still
1252 * allocated and in the unlinked set, and there is an
1253 * intent log record saying to allocate it.
1254 */
1255 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1256 if (zil_replay_disable) {
1257 zil_destroy(zfsvfs->z_log, B_FALSE);
1258 } else {
1259 zfsvfs->z_replay = B_TRUE;
1260 zil_replay(zfsvfs->z_os, zfsvfs,
1261 zfs_replay_vector);
1262 zfsvfs->z_replay = B_FALSE;
1263 }
1264 }
1265
1266 /* restore readonly bit */
1267 if (readonly != 0)
1268 readonly_changed_cb(zfsvfs, B_TRUE);
1269 }
1270
1271 /*
1272 * Set the objset user_ptr to track its zfsvfs.
1273 */
1274 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1275 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1276 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1277
1278 return (0);
1279 }
1280
1281 void
1282 zfsvfs_free(zfsvfs_t *zfsvfs)
1283 {
1284 int i, size = zfsvfs->z_hold_size;
1285
1286 zfs_fuid_destroy(zfsvfs);
1287
1288 mutex_destroy(&zfsvfs->z_znodes_lock);
1289 mutex_destroy(&zfsvfs->z_lock);
1290 list_destroy(&zfsvfs->z_all_znodes);
1291 rrm_destroy(&zfsvfs->z_teardown_lock);
1292 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1293 rw_destroy(&zfsvfs->z_fuid_lock);
1294 for (i = 0; i != size; i++) {
1295 avl_destroy(&zfsvfs->z_hold_trees[i]);
1296 mutex_destroy(&zfsvfs->z_hold_locks[i]);
1297 }
1298 vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size);
1299 vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size);
1300 zfsvfs_vfs_free(zfsvfs->z_vfs);
1301 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1302 }
1303
1304 static void
1305 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1306 {
1307 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1308 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1309 }
1310
1311 void
1312 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1313 {
1314 objset_t *os = zfsvfs->z_os;
1315
1316 if (!dmu_objset_is_snapshot(os))
1317 dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1318 }
1319
1320 #ifdef HAVE_MLSLABEL
1321 /*
1322 * Check that the hex label string is appropriate for the dataset being
1323 * mounted into the global_zone proper.
1324 *
1325 * Return an error if the hex label string is not default or
1326 * admin_low/admin_high. For admin_low labels, the corresponding
1327 * dataset must be readonly.
1328 */
1329 int
1330 zfs_check_global_label(const char *dsname, const char *hexsl)
1331 {
1332 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1333 return (0);
1334 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1335 return (0);
1336 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1337 /* must be readonly */
1338 uint64_t rdonly;
1339
1340 if (dsl_prop_get_integer(dsname,
1341 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1342 return (SET_ERROR(EACCES));
1343 return (rdonly ? 0 : EACCES);
1344 }
1345 return (SET_ERROR(EACCES));
1346 }
1347 #endif /* HAVE_MLSLABEL */
1348
1349 static int
1350 zfs_statfs_project(zfsvfs_t *zfsvfs, znode_t *zp, struct kstatfs *statp,
1351 uint32_t bshift)
1352 {
1353 char buf[20 + DMU_OBJACCT_PREFIX_LEN];
1354 uint64_t offset = DMU_OBJACCT_PREFIX_LEN;
1355 uint64_t quota;
1356 uint64_t used;
1357 int err;
1358
1359 strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
1360 err = id_to_fuidstr(zfsvfs, NULL, zp->z_projid, buf + offset, B_FALSE);
1361 if (err)
1362 return (err);
1363
1364 if (zfsvfs->z_projectquota_obj == 0)
1365 goto objs;
1366
1367 err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectquota_obj,
1368 buf + offset, 8, 1, &quota);
1369 if (err == ENOENT)
1370 goto objs;
1371 else if (err)
1372 return (err);
1373
1374 err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1375 buf + offset, 8, 1, &used);
1376 if (unlikely(err == ENOENT)) {
1377 uint32_t blksize;
1378 u_longlong_t nblocks;
1379
1380 /*
1381 * Quota accounting is async, so it is possible race case.
1382 * There is at least one object with the given project ID.
1383 */
1384 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
1385 if (unlikely(zp->z_blksz == 0))
1386 blksize = zfsvfs->z_max_blksz;
1387
1388 used = blksize * nblocks;
1389 } else if (err) {
1390 return (err);
1391 }
1392
1393 statp->f_blocks = quota >> bshift;
1394 statp->f_bfree = (quota > used) ? ((quota - used) >> bshift) : 0;
1395 statp->f_bavail = statp->f_bfree;
1396
1397 objs:
1398 if (zfsvfs->z_projectobjquota_obj == 0)
1399 return (0);
1400
1401 err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectobjquota_obj,
1402 buf + offset, 8, 1, &quota);
1403 if (err == ENOENT)
1404 return (0);
1405 else if (err)
1406 return (err);
1407
1408 err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1409 buf, 8, 1, &used);
1410 if (unlikely(err == ENOENT)) {
1411 /*
1412 * Quota accounting is async, so it is possible race case.
1413 * There is at least one object with the given project ID.
1414 */
1415 used = 1;
1416 } else if (err) {
1417 return (err);
1418 }
1419
1420 statp->f_files = quota;
1421 statp->f_ffree = (quota > used) ? (quota - used) : 0;
1422
1423 return (0);
1424 }
1425
1426 int
1427 zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
1428 {
1429 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
1430 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1431 uint64_t fsid;
1432 uint32_t bshift;
1433 int err = 0;
1434
1435 ZFS_ENTER(zfsvfs);
1436
1437 dmu_objset_space(zfsvfs->z_os,
1438 &refdbytes, &availbytes, &usedobjs, &availobjs);
1439
1440 fsid = dmu_objset_fsid_guid(zfsvfs->z_os);
1441 /*
1442 * The underlying storage pool actually uses multiple block
1443 * size. Under Solaris frsize (fragment size) is reported as
1444 * the smallest block size we support, and bsize (block size)
1445 * as the filesystem's maximum block size. Unfortunately,
1446 * under Linux the fragment size and block size are often used
1447 * interchangeably. Thus we are forced to report both of them
1448 * as the filesystem's maximum block size.
1449 */
1450 statp->f_frsize = zfsvfs->z_max_blksz;
1451 statp->f_bsize = zfsvfs->z_max_blksz;
1452 bshift = fls(statp->f_bsize) - 1;
1453
1454 /*
1455 * The following report "total" blocks of various kinds in
1456 * the file system, but reported in terms of f_bsize - the
1457 * "preferred" size.
1458 */
1459
1460 statp->f_blocks = (refdbytes + availbytes) >> bshift;
1461 statp->f_bfree = availbytes >> bshift;
1462 statp->f_bavail = statp->f_bfree; /* no root reservation */
1463
1464 /*
1465 * statvfs() should really be called statufs(), because it assumes
1466 * static metadata. ZFS doesn't preallocate files, so the best
1467 * we can do is report the max that could possibly fit in f_files,
1468 * and that minus the number actually used in f_ffree.
1469 * For f_ffree, report the smaller of the number of object available
1470 * and the number of blocks (each object will take at least a block).
1471 */
1472 statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
1473 statp->f_files = statp->f_ffree + usedobjs;
1474 statp->f_fsid.val[0] = (uint32_t)fsid;
1475 statp->f_fsid.val[1] = (uint32_t)(fsid >> 32);
1476 statp->f_type = ZFS_SUPER_MAGIC;
1477 statp->f_namelen = MAXNAMELEN - 1;
1478
1479 /*
1480 * We have all of 40 characters to stuff a string here.
1481 * Is there anything useful we could/should provide?
1482 */
1483 bzero(statp->f_spare, sizeof (statp->f_spare));
1484
1485 if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
1486 dmu_objset_projectquota_present(zfsvfs->z_os)) {
1487 znode_t *zp = ITOZ(dentry->d_inode);
1488
1489 if (zp->z_pflags & ZFS_PROJINHERIT && zp->z_projid &&
1490 zpl_is_valid_projid(zp->z_projid))
1491 err = zfs_statfs_project(zfsvfs, zp, statp, bshift);
1492 }
1493
1494 ZFS_EXIT(zfsvfs);
1495 return (err);
1496 }
1497
1498 int
1499 zfs_root(zfsvfs_t *zfsvfs, struct inode **ipp)
1500 {
1501 znode_t *rootzp;
1502 int error;
1503
1504 ZFS_ENTER(zfsvfs);
1505
1506 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
1507 if (error == 0)
1508 *ipp = ZTOI(rootzp);
1509
1510 ZFS_EXIT(zfsvfs);
1511 return (error);
1512 }
1513
1514 #ifdef HAVE_D_PRUNE_ALIASES
1515 /*
1516 * Linux kernels older than 3.1 do not support a per-filesystem shrinker.
1517 * To accommodate this we must improvise and manually walk the list of znodes
1518 * attempting to prune dentries in order to be able to drop the inodes.
1519 *
1520 * To avoid scanning the same znodes multiple times they are always rotated
1521 * to the end of the z_all_znodes list. New znodes are inserted at the
1522 * end of the list so we're always scanning the oldest znodes first.
1523 */
1524 static int
1525 zfs_prune_aliases(zfsvfs_t *zfsvfs, unsigned long nr_to_scan)
1526 {
1527 znode_t **zp_array, *zp;
1528 int max_array = MIN(nr_to_scan, PAGE_SIZE * 8 / sizeof (znode_t *));
1529 int objects = 0;
1530 int i = 0, j = 0;
1531
1532 zp_array = kmem_zalloc(max_array * sizeof (znode_t *), KM_SLEEP);
1533
1534 mutex_enter(&zfsvfs->z_znodes_lock);
1535 while ((zp = list_head(&zfsvfs->z_all_znodes)) != NULL) {
1536
1537 if ((i++ > nr_to_scan) || (j >= max_array))
1538 break;
1539
1540 ASSERT(list_link_active(&zp->z_link_node));
1541 list_remove(&zfsvfs->z_all_znodes, zp);
1542 list_insert_tail(&zfsvfs->z_all_znodes, zp);
1543
1544 /* Skip active znodes and .zfs entries */
1545 if (MUTEX_HELD(&zp->z_lock) || zp->z_is_ctldir)
1546 continue;
1547
1548 if (igrab(ZTOI(zp)) == NULL)
1549 continue;
1550
1551 zp_array[j] = zp;
1552 j++;
1553 }
1554 mutex_exit(&zfsvfs->z_znodes_lock);
1555
1556 for (i = 0; i < j; i++) {
1557 zp = zp_array[i];
1558
1559 ASSERT3P(zp, !=, NULL);
1560 d_prune_aliases(ZTOI(zp));
1561
1562 if (atomic_read(&ZTOI(zp)->i_count) == 1)
1563 objects++;
1564
1565 iput(ZTOI(zp));
1566 }
1567
1568 kmem_free(zp_array, max_array * sizeof (znode_t *));
1569
1570 return (objects);
1571 }
1572 #endif /* HAVE_D_PRUNE_ALIASES */
1573
1574 /*
1575 * The ARC has requested that the filesystem drop entries from the dentry
1576 * and inode caches. This can occur when the ARC needs to free meta data
1577 * blocks but can't because they are all pinned by entries in these caches.
1578 */
1579 int
1580 zfs_prune(struct super_block *sb, unsigned long nr_to_scan, int *objects)
1581 {
1582 zfsvfs_t *zfsvfs = sb->s_fs_info;
1583 int error = 0;
1584 #if defined(HAVE_SHRINK) || defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1585 struct shrinker *shrinker = &sb->s_shrink;
1586 struct shrink_control sc = {
1587 .nr_to_scan = nr_to_scan,
1588 .gfp_mask = GFP_KERNEL,
1589 };
1590 #endif
1591
1592 ZFS_ENTER(zfsvfs);
1593
1594 #if defined(HAVE_SPLIT_SHRINKER_CALLBACK) && \
1595 defined(SHRINK_CONTROL_HAS_NID) && \
1596 defined(SHRINKER_NUMA_AWARE)
1597 if (sb->s_shrink.flags & SHRINKER_NUMA_AWARE) {
1598 *objects = 0;
1599 for_each_online_node(sc.nid) {
1600 *objects += (*shrinker->scan_objects)(shrinker, &sc);
1601 }
1602 } else {
1603 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1604 }
1605
1606 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1607 *objects = (*shrinker->scan_objects)(shrinker, &sc);
1608 #elif defined(HAVE_SHRINK)
1609 *objects = (*shrinker->shrink)(shrinker, &sc);
1610 #elif defined(HAVE_D_PRUNE_ALIASES)
1611 #define D_PRUNE_ALIASES_IS_DEFAULT
1612 *objects = zfs_prune_aliases(zfsvfs, nr_to_scan);
1613 #else
1614 #error "No available dentry and inode cache pruning mechanism."
1615 #endif
1616
1617 #if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1618 #undef D_PRUNE_ALIASES_IS_DEFAULT
1619 /*
1620 * Fall back to zfs_prune_aliases if the kernel's per-superblock
1621 * shrinker couldn't free anything, possibly due to the inodes being
1622 * allocated in a different memcg.
1623 */
1624 if (*objects == 0)
1625 *objects = zfs_prune_aliases(zfsvfs, nr_to_scan);
1626 #endif
1627
1628 ZFS_EXIT(zfsvfs);
1629
1630 dprintf_ds(zfsvfs->z_os->os_dsl_dataset,
1631 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1632 nr_to_scan, *objects, error);
1633
1634 return (error);
1635 }
1636
1637 /*
1638 * Teardown the zfsvfs_t.
1639 *
1640 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1641 * and 'z_teardown_inactive_lock' held.
1642 */
1643 static int
1644 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
1645 {
1646 znode_t *zp;
1647
1648 /*
1649 * If someone has not already unmounted this file system,
1650 * drain the iput_taskq to ensure all active references to the
1651 * zfsvfs_t have been handled only then can it be safely destroyed.
1652 */
1653 if (zfsvfs->z_os) {
1654 /*
1655 * If we're unmounting we have to wait for the list to
1656 * drain completely.
1657 *
1658 * If we're not unmounting there's no guarantee the list
1659 * will drain completely, but iputs run from the taskq
1660 * may add the parents of dir-based xattrs to the taskq
1661 * so we want to wait for these.
1662 *
1663 * We can safely read z_nr_znodes without locking because the
1664 * VFS has already blocked operations which add to the
1665 * z_all_znodes list and thus increment z_nr_znodes.
1666 */
1667 int round = 0;
1668 while (zfsvfs->z_nr_znodes > 0) {
1669 taskq_wait_outstanding(dsl_pool_iput_taskq(
1670 dmu_objset_pool(zfsvfs->z_os)), 0);
1671 if (++round > 1 && !unmounting)
1672 break;
1673 }
1674 }
1675
1676 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1677
1678 if (!unmounting) {
1679 /*
1680 * We purge the parent filesystem's super block as the
1681 * parent filesystem and all of its snapshots have their
1682 * inode's super block set to the parent's filesystem's
1683 * super block. Note, 'z_parent' is self referential
1684 * for non-snapshots.
1685 */
1686 shrink_dcache_sb(zfsvfs->z_parent->z_sb);
1687 }
1688
1689 /*
1690 * Close the zil. NB: Can't close the zil while zfs_inactive
1691 * threads are blocked as zil_close can call zfs_inactive.
1692 */
1693 if (zfsvfs->z_log) {
1694 zil_close(zfsvfs->z_log);
1695 zfsvfs->z_log = NULL;
1696 }
1697
1698 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
1699
1700 /*
1701 * If we are not unmounting (ie: online recv) and someone already
1702 * unmounted this file system while we were doing the switcheroo,
1703 * or a reopen of z_os failed then just bail out now.
1704 */
1705 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
1706 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1707 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1708 return (SET_ERROR(EIO));
1709 }
1710
1711 /*
1712 * At this point there are no VFS ops active, and any new VFS ops
1713 * will fail with EIO since we have z_teardown_lock for writer (only
1714 * relevant for forced unmount).
1715 *
1716 * Release all holds on dbufs.
1717 */
1718 if (!unmounting) {
1719 mutex_enter(&zfsvfs->z_znodes_lock);
1720 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
1721 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
1722 if (zp->z_sa_hdl)
1723 zfs_znode_dmu_fini(zp);
1724 }
1725 mutex_exit(&zfsvfs->z_znodes_lock);
1726 }
1727
1728 /*
1729 * If we are unmounting, set the unmounted flag and let new VFS ops
1730 * unblock. zfs_inactive will have the unmounted behavior, and all
1731 * other VFS ops will fail with EIO.
1732 */
1733 if (unmounting) {
1734 zfsvfs->z_unmounted = B_TRUE;
1735 rw_exit(&zfsvfs->z_teardown_inactive_lock);
1736 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1737 }
1738
1739 /*
1740 * z_os will be NULL if there was an error in attempting to reopen
1741 * zfsvfs, so just return as the properties had already been
1742 *
1743 * unregistered and cached data had been evicted before.
1744 */
1745 if (zfsvfs->z_os == NULL)
1746 return (0);
1747
1748 /*
1749 * Unregister properties.
1750 */
1751 zfs_unregister_callbacks(zfsvfs);
1752
1753 /*
1754 * Evict cached data
1755 */
1756 if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
1757 !zfs_is_readonly(zfsvfs))
1758 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
1759 dmu_objset_evict_dbufs(zfsvfs->z_os);
1760
1761 return (0);
1762 }
1763
1764 #if !defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER) && \
1765 !defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
1766 atomic_long_t zfs_bdi_seq = ATOMIC_LONG_INIT(0);
1767 #endif
1768
1769 int
1770 zfs_domount(struct super_block *sb, zfs_mnt_t *zm, int silent)
1771 {
1772 const char *osname = zm->mnt_osname;
1773 struct inode *root_inode;
1774 uint64_t recordsize;
1775 int error = 0;
1776 zfsvfs_t *zfsvfs = NULL;
1777 vfs_t *vfs = NULL;
1778
1779 ASSERT(zm);
1780 ASSERT(osname);
1781
1782 error = zfsvfs_parse_options(zm->mnt_data, &vfs);
1783 if (error)
1784 return (error);
1785
1786 error = zfsvfs_create(osname, vfs->vfs_readonly, &zfsvfs);
1787 if (error) {
1788 zfsvfs_vfs_free(vfs);
1789 goto out;
1790 }
1791
1792 if ((error = dsl_prop_get_integer(osname, "recordsize",
1793 &recordsize, NULL))) {
1794 zfsvfs_vfs_free(vfs);
1795 goto out;
1796 }
1797
1798 vfs->vfs_data = zfsvfs;
1799 zfsvfs->z_vfs = vfs;
1800 zfsvfs->z_sb = sb;
1801 sb->s_fs_info = zfsvfs;
1802 sb->s_magic = ZFS_SUPER_MAGIC;
1803 sb->s_maxbytes = MAX_LFS_FILESIZE;
1804 sb->s_time_gran = 1;
1805 sb->s_blocksize = recordsize;
1806 sb->s_blocksize_bits = ilog2(recordsize);
1807
1808 error = -zpl_bdi_setup(sb, "zfs");
1809 if (error)
1810 goto out;
1811
1812 sb->s_bdi->ra_pages = 0;
1813
1814 /* Set callback operations for the file system. */
1815 sb->s_op = &zpl_super_operations;
1816 sb->s_xattr = zpl_xattr_handlers;
1817 sb->s_export_op = &zpl_export_operations;
1818 #ifdef HAVE_S_D_OP
1819 sb->s_d_op = &zpl_dentry_operations;
1820 #endif /* HAVE_S_D_OP */
1821
1822 /* Set features for file system. */
1823 zfs_set_fuid_feature(zfsvfs);
1824
1825 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1826 uint64_t pval;
1827
1828 atime_changed_cb(zfsvfs, B_FALSE);
1829 readonly_changed_cb(zfsvfs, B_TRUE);
1830 if ((error = dsl_prop_get_integer(osname,
1831 "xattr", &pval, NULL)))
1832 goto out;
1833 xattr_changed_cb(zfsvfs, pval);
1834 if ((error = dsl_prop_get_integer(osname,
1835 "acltype", &pval, NULL)))
1836 goto out;
1837 acltype_changed_cb(zfsvfs, pval);
1838 zfsvfs->z_issnap = B_TRUE;
1839 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1840 zfsvfs->z_snap_defer_time = jiffies;
1841
1842 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1843 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1844 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1845 } else {
1846 if ((error = zfsvfs_setup(zfsvfs, B_TRUE)))
1847 goto out;
1848 }
1849
1850 /* Allocate a root inode for the filesystem. */
1851 error = zfs_root(zfsvfs, &root_inode);
1852 if (error) {
1853 (void) zfs_umount(sb);
1854 goto out;
1855 }
1856
1857 /* Allocate a root dentry for the filesystem */
1858 sb->s_root = d_make_root(root_inode);
1859 if (sb->s_root == NULL) {
1860 (void) zfs_umount(sb);
1861 error = SET_ERROR(ENOMEM);
1862 goto out;
1863 }
1864
1865 if (!zfsvfs->z_issnap)
1866 zfsctl_create(zfsvfs);
1867
1868 zfsvfs->z_arc_prune = arc_add_prune_callback(zpl_prune_sb, sb);
1869 out:
1870 if (error) {
1871 if (zfsvfs != NULL) {
1872 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1873 zfsvfs_free(zfsvfs);
1874 }
1875 /*
1876 * make sure we don't have dangling sb->s_fs_info which
1877 * zfs_preumount will use.
1878 */
1879 sb->s_fs_info = NULL;
1880 }
1881
1882 return (error);
1883 }
1884
1885 /*
1886 * Called when an unmount is requested and certain sanity checks have
1887 * already passed. At this point no dentries or inodes have been reclaimed
1888 * from their respective caches. We drop the extra reference on the .zfs
1889 * control directory to allow everything to be reclaimed. All snapshots
1890 * must already have been unmounted to reach this point.
1891 */
1892 void
1893 zfs_preumount(struct super_block *sb)
1894 {
1895 zfsvfs_t *zfsvfs = sb->s_fs_info;
1896
1897 /* zfsvfs is NULL when zfs_domount fails during mount */
1898 if (zfsvfs) {
1899 zfsctl_destroy(sb->s_fs_info);
1900 /*
1901 * Wait for iput_async before entering evict_inodes in
1902 * generic_shutdown_super. The reason we must finish before
1903 * evict_inodes is when lazytime is on, or when zfs_purgedir
1904 * calls zfs_zget, iput would bump i_count from 0 to 1. This
1905 * would race with the i_count check in evict_inodes. This means
1906 * it could destroy the inode while we are still using it.
1907 *
1908 * We wait for two passes. xattr directories in the first pass
1909 * may add xattr entries in zfs_purgedir, so in the second pass
1910 * we wait for them. We don't use taskq_wait here because it is
1911 * a pool wide taskq. Other mounted filesystems can constantly
1912 * do iput_async and there's no guarantee when taskq will be
1913 * empty.
1914 */
1915 taskq_wait_outstanding(dsl_pool_iput_taskq(
1916 dmu_objset_pool(zfsvfs->z_os)), 0);
1917 taskq_wait_outstanding(dsl_pool_iput_taskq(
1918 dmu_objset_pool(zfsvfs->z_os)), 0);
1919 }
1920 }
1921
1922 /*
1923 * Called once all other unmount released tear down has occurred.
1924 * It is our responsibility to release any remaining infrastructure.
1925 */
1926 /*ARGSUSED*/
1927 int
1928 zfs_umount(struct super_block *sb)
1929 {
1930 zfsvfs_t *zfsvfs = sb->s_fs_info;
1931 objset_t *os;
1932
1933 if (zfsvfs->z_arc_prune != NULL)
1934 arc_remove_prune_callback(zfsvfs->z_arc_prune);
1935 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
1936 os = zfsvfs->z_os;
1937 zpl_bdi_destroy(sb);
1938
1939 /*
1940 * z_os will be NULL if there was an error in
1941 * attempting to reopen zfsvfs.
1942 */
1943 if (os != NULL) {
1944 /*
1945 * Unset the objset user_ptr.
1946 */
1947 mutex_enter(&os->os_user_ptr_lock);
1948 dmu_objset_set_user(os, NULL);
1949 mutex_exit(&os->os_user_ptr_lock);
1950
1951 /*
1952 * Finally release the objset
1953 */
1954 dmu_objset_disown(os, B_TRUE, zfsvfs);
1955 }
1956
1957 zfsvfs_free(zfsvfs);
1958 return (0);
1959 }
1960
1961 int
1962 zfs_remount(struct super_block *sb, int *flags, zfs_mnt_t *zm)
1963 {
1964 zfsvfs_t *zfsvfs = sb->s_fs_info;
1965 vfs_t *vfsp;
1966 boolean_t issnap = dmu_objset_is_snapshot(zfsvfs->z_os);
1967 int error;
1968
1969 if ((issnap || !spa_writeable(dmu_objset_spa(zfsvfs->z_os))) &&
1970 !(*flags & MS_RDONLY)) {
1971 *flags |= MS_RDONLY;
1972 return (EROFS);
1973 }
1974
1975 error = zfsvfs_parse_options(zm->mnt_data, &vfsp);
1976 if (error)
1977 return (error);
1978
1979 zfs_unregister_callbacks(zfsvfs);
1980 zfsvfs_vfs_free(zfsvfs->z_vfs);
1981
1982 vfsp->vfs_data = zfsvfs;
1983 zfsvfs->z_vfs = vfsp;
1984 if (!issnap)
1985 (void) zfs_register_callbacks(vfsp);
1986
1987 return (error);
1988 }
1989
1990 int
1991 zfs_vget(struct super_block *sb, struct inode **ipp, fid_t *fidp)
1992 {
1993 zfsvfs_t *zfsvfs = sb->s_fs_info;
1994 znode_t *zp;
1995 uint64_t object = 0;
1996 uint64_t fid_gen = 0;
1997 uint64_t gen_mask;
1998 uint64_t zp_gen;
1999 int i, err;
2000
2001 *ipp = NULL;
2002
2003 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2004 zfid_short_t *zfid = (zfid_short_t *)fidp;
2005
2006 for (i = 0; i < sizeof (zfid->zf_object); i++)
2007 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2008
2009 for (i = 0; i < sizeof (zfid->zf_gen); i++)
2010 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2011 } else {
2012 return (SET_ERROR(EINVAL));
2013 }
2014
2015 /* LONG_FID_LEN means snapdirs */
2016 if (fidp->fid_len == LONG_FID_LEN) {
2017 zfid_long_t *zlfid = (zfid_long_t *)fidp;
2018 uint64_t objsetid = 0;
2019 uint64_t setgen = 0;
2020
2021 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2022 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2023
2024 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2025 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2026
2027 if (objsetid != ZFSCTL_INO_SNAPDIRS - object) {
2028 dprintf("snapdir fid: objsetid (%llu) != "
2029 "ZFSCTL_INO_SNAPDIRS (%llu) - object (%llu)\n",
2030 objsetid, ZFSCTL_INO_SNAPDIRS, object);
2031
2032 return (SET_ERROR(EINVAL));
2033 }
2034
2035 if (fid_gen > 1 || setgen != 0) {
2036 dprintf("snapdir fid: fid_gen (%llu) and setgen "
2037 "(%llu)\n", fid_gen, setgen);
2038 return (SET_ERROR(EINVAL));
2039 }
2040
2041 return (zfsctl_snapdir_vget(sb, objsetid, fid_gen, ipp));
2042 }
2043
2044 ZFS_ENTER(zfsvfs);
2045 /* A zero fid_gen means we are in the .zfs control directories */
2046 if (fid_gen == 0 &&
2047 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2048 *ipp = zfsvfs->z_ctldir;
2049 ASSERT(*ipp != NULL);
2050 if (object == ZFSCTL_INO_SNAPDIR) {
2051 VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp,
2052 0, kcred, NULL, NULL) == 0);
2053 } else {
2054 igrab(*ipp);
2055 }
2056 ZFS_EXIT(zfsvfs);
2057 return (0);
2058 }
2059
2060 gen_mask = -1ULL >> (64 - 8 * i);
2061
2062 dprintf("getting %llu [%llu mask %llx]\n", object, fid_gen, gen_mask);
2063 if ((err = zfs_zget(zfsvfs, object, &zp))) {
2064 ZFS_EXIT(zfsvfs);
2065 return (err);
2066 }
2067
2068 /* Don't export xattr stuff */
2069 if (zp->z_pflags & ZFS_XATTR) {
2070 iput(ZTOI(zp));
2071 ZFS_EXIT(zfsvfs);
2072 return (SET_ERROR(ENOENT));
2073 }
2074
2075 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2076 sizeof (uint64_t));
2077 zp_gen = zp_gen & gen_mask;
2078 if (zp_gen == 0)
2079 zp_gen = 1;
2080 if ((fid_gen == 0) && (zfsvfs->z_root == object))
2081 fid_gen = zp_gen;
2082 if (zp->z_unlinked || zp_gen != fid_gen) {
2083 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen,
2084 fid_gen);
2085 iput(ZTOI(zp));
2086 ZFS_EXIT(zfsvfs);
2087 return (SET_ERROR(ENOENT));
2088 }
2089
2090 *ipp = ZTOI(zp);
2091 if (*ipp)
2092 zfs_inode_update(ITOZ(*ipp));
2093
2094 ZFS_EXIT(zfsvfs);
2095 return (0);
2096 }
2097
2098 /*
2099 * Block out VFS ops and close zfsvfs_t
2100 *
2101 * Note, if successful, then we return with the 'z_teardown_lock' and
2102 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
2103 * dataset and objset intact so that they can be atomically handed off during
2104 * a subsequent rollback or recv operation and the resume thereafter.
2105 */
2106 int
2107 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2108 {
2109 int error;
2110
2111 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2112 return (error);
2113
2114 return (0);
2115 }
2116
2117 /*
2118 * Rebuild SA and release VOPs. Note that ownership of the underlying dataset
2119 * is an invariant across any of the operations that can be performed while the
2120 * filesystem was suspended. Whether it succeeded or failed, the preconditions
2121 * are the same: the relevant objset and associated dataset are owned by
2122 * zfsvfs, held, and long held on entry.
2123 */
2124 int
2125 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2126 {
2127 int err, err2;
2128 znode_t *zp;
2129
2130 ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2131 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2132
2133 /*
2134 * We already own this, so just update the objset_t, as the one we
2135 * had before may have been evicted.
2136 */
2137 objset_t *os;
2138 VERIFY3P(ds->ds_owner, ==, zfsvfs);
2139 VERIFY(dsl_dataset_long_held(ds));
2140 VERIFY0(dmu_objset_from_ds(ds, &os));
2141
2142 err = zfsvfs_init(zfsvfs, os);
2143 if (err != 0)
2144 goto bail;
2145
2146 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2147
2148 zfs_set_fuid_feature(zfsvfs);
2149 zfsvfs->z_rollback_time = jiffies;
2150
2151 /*
2152 * Attempt to re-establish all the active inodes with their
2153 * dbufs. If a zfs_rezget() fails, then we unhash the inode
2154 * and mark it stale. This prevents a collision if a new
2155 * inode/object is created which must use the same inode
2156 * number. The stale inode will be be released when the
2157 * VFS prunes the dentry holding the remaining references
2158 * on the stale inode.
2159 */
2160 mutex_enter(&zfsvfs->z_znodes_lock);
2161 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2162 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2163 err2 = zfs_rezget(zp);
2164 if (err2) {
2165 remove_inode_hash(ZTOI(zp));
2166 zp->z_is_stale = B_TRUE;
2167 }
2168 }
2169 mutex_exit(&zfsvfs->z_znodes_lock);
2170
2171 bail:
2172 /* release the VFS ops */
2173 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2174 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2175
2176 if (err) {
2177 /*
2178 * Since we couldn't setup the sa framework, try to force
2179 * unmount this file system.
2180 */
2181 if (zfsvfs->z_os)
2182 (void) zfs_umount(zfsvfs->z_sb);
2183 }
2184 return (err);
2185 }
2186
2187 int
2188 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2189 {
2190 int error;
2191 objset_t *os = zfsvfs->z_os;
2192 dmu_tx_t *tx;
2193
2194 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2195 return (SET_ERROR(EINVAL));
2196
2197 if (newvers < zfsvfs->z_version)
2198 return (SET_ERROR(EINVAL));
2199
2200 if (zfs_spa_version_map(newvers) >
2201 spa_version(dmu_objset_spa(zfsvfs->z_os)))
2202 return (SET_ERROR(ENOTSUP));
2203
2204 tx = dmu_tx_create(os);
2205 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2206 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2207 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2208 ZFS_SA_ATTRS);
2209 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2210 }
2211 error = dmu_tx_assign(tx, TXG_WAIT);
2212 if (error) {
2213 dmu_tx_abort(tx);
2214 return (error);
2215 }
2216
2217 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2218 8, 1, &newvers, tx);
2219
2220 if (error) {
2221 dmu_tx_commit(tx);
2222 return (error);
2223 }
2224
2225 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2226 uint64_t sa_obj;
2227
2228 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2229 SPA_VERSION_SA);
2230 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2231 DMU_OT_NONE, 0, tx);
2232
2233 error = zap_add(os, MASTER_NODE_OBJ,
2234 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2235 ASSERT0(error);
2236
2237 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2238 sa_register_update_callback(os, zfs_sa_upgrade);
2239 }
2240
2241 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2242 "from %llu to %llu", zfsvfs->z_version, newvers);
2243
2244 dmu_tx_commit(tx);
2245
2246 zfsvfs->z_version = newvers;
2247
2248 zfs_set_fuid_feature(zfsvfs);
2249
2250 return (0);
2251 }
2252
2253 /*
2254 * Read a property stored within the master node.
2255 */
2256 int
2257 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2258 {
2259 const char *pname;
2260 int error = SET_ERROR(ENOENT);
2261
2262 /*
2263 * Look up the file system's value for the property. For the
2264 * version property, we look up a slightly different string.
2265 */
2266 if (prop == ZFS_PROP_VERSION)
2267 pname = ZPL_VERSION_STR;
2268 else
2269 pname = zfs_prop_to_name(prop);
2270
2271 if (os != NULL) {
2272 ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2273 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2274 }
2275
2276 if (error == ENOENT) {
2277 /* No value set, use the default value */
2278 switch (prop) {
2279 case ZFS_PROP_VERSION:
2280 *value = ZPL_VERSION;
2281 break;
2282 case ZFS_PROP_NORMALIZE:
2283 case ZFS_PROP_UTF8ONLY:
2284 *value = 0;
2285 break;
2286 case ZFS_PROP_CASE:
2287 *value = ZFS_CASE_SENSITIVE;
2288 break;
2289 case ZFS_PROP_ACLTYPE:
2290 *value = ZFS_ACLTYPE_OFF;
2291 break;
2292 default:
2293 return (error);
2294 }
2295 error = 0;
2296 }
2297 return (error);
2298 }
2299
2300 /*
2301 * Return true if the coresponding vfs's unmounted flag is set.
2302 * Otherwise return false.
2303 * If this function returns true we know VFS unmount has been initiated.
2304 */
2305 boolean_t
2306 zfs_get_vfs_flag_unmounted(objset_t *os)
2307 {
2308 zfsvfs_t *zfvp;
2309 boolean_t unmounted = B_FALSE;
2310
2311 ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2312
2313 mutex_enter(&os->os_user_ptr_lock);
2314 zfvp = dmu_objset_get_user(os);
2315 if (zfvp != NULL && zfvp->z_unmounted)
2316 unmounted = B_TRUE;
2317 mutex_exit(&os->os_user_ptr_lock);
2318
2319 return (unmounted);
2320 }
2321
2322 void
2323 zfs_init(void)
2324 {
2325 zfsctl_init();
2326 zfs_znode_init();
2327 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2328 register_filesystem(&zpl_fs_type);
2329 }
2330
2331 void
2332 zfs_fini(void)
2333 {
2334 /*
2335 * we don't use outstanding because zpl_posix_acl_free might add more.
2336 */
2337 taskq_wait(system_delay_taskq);
2338 taskq_wait(system_taskq);
2339 unregister_filesystem(&zpl_fs_type);
2340 zfs_znode_fini();
2341 zfsctl_fini();
2342 }
2343
2344 #if defined(_KERNEL) && defined(HAVE_SPL)
2345 EXPORT_SYMBOL(zfs_suspend_fs);
2346 EXPORT_SYMBOL(zfs_resume_fs);
2347 EXPORT_SYMBOL(zfs_userspace_one);
2348 EXPORT_SYMBOL(zfs_userspace_many);
2349 EXPORT_SYMBOL(zfs_set_userquota);
2350 EXPORT_SYMBOL(zfs_id_overblockquota);
2351 EXPORT_SYMBOL(zfs_id_overobjquota);
2352 EXPORT_SYMBOL(zfs_id_overquota);
2353 EXPORT_SYMBOL(zfs_set_version);
2354 EXPORT_SYMBOL(zfsvfs_create);
2355 EXPORT_SYMBOL(zfsvfs_free);
2356 EXPORT_SYMBOL(zfs_is_readonly);
2357 EXPORT_SYMBOL(zfs_domount);
2358 EXPORT_SYMBOL(zfs_preumount);
2359 EXPORT_SYMBOL(zfs_umount);
2360 EXPORT_SYMBOL(zfs_remount);
2361 EXPORT_SYMBOL(zfs_statvfs);
2362 EXPORT_SYMBOL(zfs_vget);
2363 EXPORT_SYMBOL(zfs_prune);
2364 #endif