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