xref: /linux/fs/xfs/xfs_sysfs.c (revision 011f129fee4bd064a3db30ca1a0139548a619482)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Red Hat, Inc.
4  * All Rights Reserved.
5  */
6 
7 #include "xfs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_sysfs.h"
13 #include "xfs_log.h"
14 #include "xfs_log_priv.h"
15 #include "xfs_mount.h"
16 
17 struct xfs_sysfs_attr {
18 	struct attribute attr;
19 	ssize_t (*show)(struct kobject *kobject, char *buf);
20 	ssize_t (*store)(struct kobject *kobject, const char *buf,
21 			 size_t count);
22 };
23 
24 static inline struct xfs_sysfs_attr *
25 to_attr(struct attribute *attr)
26 {
27 	return container_of(attr, struct xfs_sysfs_attr, attr);
28 }
29 
30 #define XFS_SYSFS_ATTR_RW(name) \
31 	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RW(name)
32 #define XFS_SYSFS_ATTR_RO(name) \
33 	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RO(name)
34 #define XFS_SYSFS_ATTR_WO(name) \
35 	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_WO(name)
36 
37 #define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr
38 
39 STATIC ssize_t
40 xfs_sysfs_object_show(
41 	struct kobject		*kobject,
42 	struct attribute	*attr,
43 	char			*buf)
44 {
45 	struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
46 
47 	return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0;
48 }
49 
50 STATIC ssize_t
51 xfs_sysfs_object_store(
52 	struct kobject		*kobject,
53 	struct attribute	*attr,
54 	const char		*buf,
55 	size_t			count)
56 {
57 	struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
58 
59 	return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0;
60 }
61 
62 static const struct sysfs_ops xfs_sysfs_ops = {
63 	.show = xfs_sysfs_object_show,
64 	.store = xfs_sysfs_object_store,
65 };
66 
67 static struct attribute *xfs_mp_attrs[] = {
68 	NULL,
69 };
70 ATTRIBUTE_GROUPS(xfs_mp);
71 
72 const struct kobj_type xfs_mp_ktype = {
73 	.release = xfs_sysfs_release,
74 	.sysfs_ops = &xfs_sysfs_ops,
75 	.default_groups = xfs_mp_groups,
76 };
77 
78 #ifdef DEBUG
79 /* debug */
80 
81 STATIC ssize_t
82 bug_on_assert_store(
83 	struct kobject		*kobject,
84 	const char		*buf,
85 	size_t			count)
86 {
87 	int			ret;
88 	int			val;
89 
90 	ret = kstrtoint(buf, 0, &val);
91 	if (ret)
92 		return ret;
93 
94 	if (val == 1)
95 		xfs_globals.bug_on_assert = true;
96 	else if (val == 0)
97 		xfs_globals.bug_on_assert = false;
98 	else
99 		return -EINVAL;
100 
101 	return count;
102 }
103 
104 STATIC ssize_t
105 bug_on_assert_show(
106 	struct kobject		*kobject,
107 	char			*buf)
108 {
109 	return sysfs_emit(buf, "%d\n", xfs_globals.bug_on_assert);
110 }
111 XFS_SYSFS_ATTR_RW(bug_on_assert);
112 
113 STATIC ssize_t
114 log_recovery_delay_store(
115 	struct kobject	*kobject,
116 	const char	*buf,
117 	size_t		count)
118 {
119 	int		ret;
120 	int		val;
121 
122 	ret = kstrtoint(buf, 0, &val);
123 	if (ret)
124 		return ret;
125 
126 	if (val < 0 || val > 60)
127 		return -EINVAL;
128 
129 	xfs_globals.log_recovery_delay = val;
130 
131 	return count;
132 }
133 
134 STATIC ssize_t
135 log_recovery_delay_show(
136 	struct kobject	*kobject,
137 	char		*buf)
138 {
139 	return sysfs_emit(buf, "%d\n", xfs_globals.log_recovery_delay);
140 }
141 XFS_SYSFS_ATTR_RW(log_recovery_delay);
142 
143 STATIC ssize_t
144 mount_delay_store(
145 	struct kobject	*kobject,
146 	const char	*buf,
147 	size_t		count)
148 {
149 	int		ret;
150 	int		val;
151 
152 	ret = kstrtoint(buf, 0, &val);
153 	if (ret)
154 		return ret;
155 
156 	if (val < 0 || val > 60)
157 		return -EINVAL;
158 
159 	xfs_globals.mount_delay = val;
160 
161 	return count;
162 }
163 
164 STATIC ssize_t
165 mount_delay_show(
166 	struct kobject	*kobject,
167 	char		*buf)
168 {
169 	return sysfs_emit(buf, "%d\n", xfs_globals.mount_delay);
170 }
171 XFS_SYSFS_ATTR_RW(mount_delay);
172 
173 static ssize_t
174 always_cow_store(
175 	struct kobject	*kobject,
176 	const char	*buf,
177 	size_t		count)
178 {
179 	ssize_t		ret;
180 
181 	ret = kstrtobool(buf, &xfs_globals.always_cow);
182 	if (ret < 0)
183 		return ret;
184 	return count;
185 }
186 
187 static ssize_t
188 always_cow_show(
189 	struct kobject	*kobject,
190 	char		*buf)
191 {
192 	return sysfs_emit(buf, "%d\n", xfs_globals.always_cow);
193 }
194 XFS_SYSFS_ATTR_RW(always_cow);
195 
196 #ifdef DEBUG
197 /*
198  * Override how many threads the parallel work queue is allowed to create.
199  * This has to be a debug-only global (instead of an errortag) because one of
200  * the main users of parallel workqueues is mount time quotacheck.
201  */
202 STATIC ssize_t
203 pwork_threads_store(
204 	struct kobject	*kobject,
205 	const char	*buf,
206 	size_t		count)
207 {
208 	int		ret;
209 	int		val;
210 
211 	ret = kstrtoint(buf, 0, &val);
212 	if (ret)
213 		return ret;
214 
215 	if (val < -1 || val > num_possible_cpus())
216 		return -EINVAL;
217 
218 	xfs_globals.pwork_threads = val;
219 
220 	return count;
221 }
222 
223 STATIC ssize_t
224 pwork_threads_show(
225 	struct kobject	*kobject,
226 	char		*buf)
227 {
228 	return sysfs_emit(buf, "%d\n", xfs_globals.pwork_threads);
229 }
230 XFS_SYSFS_ATTR_RW(pwork_threads);
231 
232 /*
233  * The "LARP" (Logged extended Attribute Recovery Persistence) debugging knob
234  * sets the XFS_DA_OP_LOGGED flag on all xfs_attr_set operations performed on
235  * V5 filesystems.  As a result, the intermediate progress of all setxattr and
236  * removexattr operations are tracked via the log and can be restarted during
237  * recovery.  This is useful for testing xattr recovery prior to merging of the
238  * parent pointer feature which requires it to maintain consistency, and may be
239  * enabled for userspace xattrs in the future.
240  */
241 static ssize_t
242 larp_store(
243 	struct kobject	*kobject,
244 	const char	*buf,
245 	size_t		count)
246 {
247 	ssize_t		ret;
248 
249 	ret = kstrtobool(buf, &xfs_globals.larp);
250 	if (ret < 0)
251 		return ret;
252 	return count;
253 }
254 
255 STATIC ssize_t
256 larp_show(
257 	struct kobject	*kobject,
258 	char		*buf)
259 {
260 	return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.larp);
261 }
262 XFS_SYSFS_ATTR_RW(larp);
263 #endif /* DEBUG */
264 
265 static struct attribute *xfs_dbg_attrs[] = {
266 	ATTR_LIST(bug_on_assert),
267 	ATTR_LIST(log_recovery_delay),
268 	ATTR_LIST(mount_delay),
269 	ATTR_LIST(always_cow),
270 #ifdef DEBUG
271 	ATTR_LIST(pwork_threads),
272 	ATTR_LIST(larp),
273 #endif
274 	NULL,
275 };
276 ATTRIBUTE_GROUPS(xfs_dbg);
277 
278 const struct kobj_type xfs_dbg_ktype = {
279 	.release = xfs_sysfs_release,
280 	.sysfs_ops = &xfs_sysfs_ops,
281 	.default_groups = xfs_dbg_groups,
282 };
283 
284 #endif /* DEBUG */
285 
286 /* stats */
287 
288 static inline struct xstats *
289 to_xstats(struct kobject *kobject)
290 {
291 	struct xfs_kobj *kobj = to_kobj(kobject);
292 
293 	return container_of(kobj, struct xstats, xs_kobj);
294 }
295 
296 STATIC ssize_t
297 stats_show(
298 	struct kobject	*kobject,
299 	char		*buf)
300 {
301 	struct xstats	*stats = to_xstats(kobject);
302 
303 	return xfs_stats_format(stats->xs_stats, buf);
304 }
305 XFS_SYSFS_ATTR_RO(stats);
306 
307 STATIC ssize_t
308 stats_clear_store(
309 	struct kobject	*kobject,
310 	const char	*buf,
311 	size_t		count)
312 {
313 	int		ret;
314 	int		val;
315 	struct xstats	*stats = to_xstats(kobject);
316 
317 	ret = kstrtoint(buf, 0, &val);
318 	if (ret)
319 		return ret;
320 
321 	if (val != 1)
322 		return -EINVAL;
323 
324 	xfs_stats_clearall(stats->xs_stats);
325 	return count;
326 }
327 XFS_SYSFS_ATTR_WO(stats_clear);
328 
329 static struct attribute *xfs_stats_attrs[] = {
330 	ATTR_LIST(stats),
331 	ATTR_LIST(stats_clear),
332 	NULL,
333 };
334 ATTRIBUTE_GROUPS(xfs_stats);
335 
336 const struct kobj_type xfs_stats_ktype = {
337 	.release = xfs_sysfs_release,
338 	.sysfs_ops = &xfs_sysfs_ops,
339 	.default_groups = xfs_stats_groups,
340 };
341 
342 /* xlog */
343 
344 static inline struct xlog *
345 to_xlog(struct kobject *kobject)
346 {
347 	struct xfs_kobj *kobj = to_kobj(kobject);
348 
349 	return container_of(kobj, struct xlog, l_kobj);
350 }
351 
352 STATIC ssize_t
353 log_head_lsn_show(
354 	struct kobject	*kobject,
355 	char		*buf)
356 {
357 	int cycle;
358 	int block;
359 	struct xlog *log = to_xlog(kobject);
360 
361 	spin_lock(&log->l_icloglock);
362 	cycle = log->l_curr_cycle;
363 	block = log->l_curr_block;
364 	spin_unlock(&log->l_icloglock);
365 
366 	return sysfs_emit(buf, "%d:%d\n", cycle, block);
367 }
368 XFS_SYSFS_ATTR_RO(log_head_lsn);
369 
370 STATIC ssize_t
371 log_tail_lsn_show(
372 	struct kobject	*kobject,
373 	char		*buf)
374 {
375 	int cycle;
376 	int block;
377 	struct xlog *log = to_xlog(kobject);
378 
379 	xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block);
380 	return sysfs_emit(buf, "%d:%d\n", cycle, block);
381 }
382 XFS_SYSFS_ATTR_RO(log_tail_lsn);
383 
384 STATIC ssize_t
385 reserve_grant_head_show(
386 	struct kobject	*kobject,
387 	char		*buf)
388 
389 {
390 	int cycle;
391 	int bytes;
392 	struct xlog *log = to_xlog(kobject);
393 
394 	xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes);
395 	return sysfs_emit(buf, "%d:%d\n", cycle, bytes);
396 }
397 XFS_SYSFS_ATTR_RO(reserve_grant_head);
398 
399 STATIC ssize_t
400 write_grant_head_show(
401 	struct kobject	*kobject,
402 	char		*buf)
403 {
404 	int cycle;
405 	int bytes;
406 	struct xlog *log = to_xlog(kobject);
407 
408 	xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes);
409 	return sysfs_emit(buf, "%d:%d\n", cycle, bytes);
410 }
411 XFS_SYSFS_ATTR_RO(write_grant_head);
412 
413 static struct attribute *xfs_log_attrs[] = {
414 	ATTR_LIST(log_head_lsn),
415 	ATTR_LIST(log_tail_lsn),
416 	ATTR_LIST(reserve_grant_head),
417 	ATTR_LIST(write_grant_head),
418 	NULL,
419 };
420 ATTRIBUTE_GROUPS(xfs_log);
421 
422 const struct kobj_type xfs_log_ktype = {
423 	.release = xfs_sysfs_release,
424 	.sysfs_ops = &xfs_sysfs_ops,
425 	.default_groups = xfs_log_groups,
426 };
427 
428 /*
429  * Metadata IO error configuration
430  *
431  * The sysfs structure here is:
432  *	...xfs/<dev>/error/<class>/<errno>/<error_attrs>
433  *
434  * where <class> allows us to discriminate between data IO and metadata IO,
435  * and any other future type of IO (e.g. special inode or directory error
436  * handling) we care to support.
437  */
438 static inline struct xfs_error_cfg *
439 to_error_cfg(struct kobject *kobject)
440 {
441 	struct xfs_kobj *kobj = to_kobj(kobject);
442 	return container_of(kobj, struct xfs_error_cfg, kobj);
443 }
444 
445 static inline struct xfs_mount *
446 err_to_mp(struct kobject *kobject)
447 {
448 	struct xfs_kobj *kobj = to_kobj(kobject);
449 	return container_of(kobj, struct xfs_mount, m_error_kobj);
450 }
451 
452 static ssize_t
453 max_retries_show(
454 	struct kobject	*kobject,
455 	char		*buf)
456 {
457 	int		retries;
458 	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
459 
460 	if (cfg->max_retries == XFS_ERR_RETRY_FOREVER)
461 		retries = -1;
462 	else
463 		retries = cfg->max_retries;
464 
465 	return sysfs_emit(buf, "%d\n", retries);
466 }
467 
468 static ssize_t
469 max_retries_store(
470 	struct kobject	*kobject,
471 	const char	*buf,
472 	size_t		count)
473 {
474 	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
475 	int		ret;
476 	int		val;
477 
478 	ret = kstrtoint(buf, 0, &val);
479 	if (ret)
480 		return ret;
481 
482 	if (val < -1)
483 		return -EINVAL;
484 
485 	if (val == -1)
486 		cfg->max_retries = XFS_ERR_RETRY_FOREVER;
487 	else
488 		cfg->max_retries = val;
489 	return count;
490 }
491 XFS_SYSFS_ATTR_RW(max_retries);
492 
493 static ssize_t
494 retry_timeout_seconds_show(
495 	struct kobject	*kobject,
496 	char		*buf)
497 {
498 	int		timeout;
499 	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
500 
501 	if (cfg->retry_timeout == XFS_ERR_RETRY_FOREVER)
502 		timeout = -1;
503 	else
504 		timeout = jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC;
505 
506 	return sysfs_emit(buf, "%d\n", timeout);
507 }
508 
509 static ssize_t
510 retry_timeout_seconds_store(
511 	struct kobject	*kobject,
512 	const char	*buf,
513 	size_t		count)
514 {
515 	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
516 	int		ret;
517 	int		val;
518 
519 	ret = kstrtoint(buf, 0, &val);
520 	if (ret)
521 		return ret;
522 
523 	/* 1 day timeout maximum, -1 means infinite */
524 	if (val < -1 || val > 86400)
525 		return -EINVAL;
526 
527 	if (val == -1)
528 		cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
529 	else {
530 		cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
531 		ASSERT(msecs_to_jiffies(val * MSEC_PER_SEC) < LONG_MAX);
532 	}
533 	return count;
534 }
535 XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
536 
537 static ssize_t
538 fail_at_unmount_show(
539 	struct kobject	*kobject,
540 	char		*buf)
541 {
542 	struct xfs_mount	*mp = err_to_mp(kobject);
543 
544 	return sysfs_emit(buf, "%d\n", mp->m_fail_unmount);
545 }
546 
547 static ssize_t
548 fail_at_unmount_store(
549 	struct kobject	*kobject,
550 	const char	*buf,
551 	size_t		count)
552 {
553 	struct xfs_mount	*mp = err_to_mp(kobject);
554 	int		ret;
555 	int		val;
556 
557 	ret = kstrtoint(buf, 0, &val);
558 	if (ret)
559 		return ret;
560 
561 	if (val < 0 || val > 1)
562 		return -EINVAL;
563 
564 	mp->m_fail_unmount = val;
565 	return count;
566 }
567 XFS_SYSFS_ATTR_RW(fail_at_unmount);
568 
569 static struct attribute *xfs_error_attrs[] = {
570 	ATTR_LIST(max_retries),
571 	ATTR_LIST(retry_timeout_seconds),
572 	NULL,
573 };
574 ATTRIBUTE_GROUPS(xfs_error);
575 
576 static const struct kobj_type xfs_error_cfg_ktype = {
577 	.release = xfs_sysfs_release,
578 	.sysfs_ops = &xfs_sysfs_ops,
579 	.default_groups = xfs_error_groups,
580 };
581 
582 static const struct kobj_type xfs_error_ktype = {
583 	.release = xfs_sysfs_release,
584 	.sysfs_ops = &xfs_sysfs_ops,
585 };
586 
587 /*
588  * Error initialization tables. These need to be ordered in the same
589  * order as the enums used to index the array. All class init tables need to
590  * define a "default" behaviour as the first entry, all other entries can be
591  * empty.
592  */
593 struct xfs_error_init {
594 	char		*name;
595 	int		max_retries;
596 	int		retry_timeout;	/* in seconds */
597 };
598 
599 static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
600 	{ .name = "default",
601 	  .max_retries = XFS_ERR_RETRY_FOREVER,
602 	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
603 	},
604 	{ .name = "EIO",
605 	  .max_retries = XFS_ERR_RETRY_FOREVER,
606 	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
607 	},
608 	{ .name = "ENOSPC",
609 	  .max_retries = XFS_ERR_RETRY_FOREVER,
610 	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
611 	},
612 	{ .name = "ENODEV",
613 	  .max_retries = 0,	/* We can't recover from devices disappearing */
614 	  .retry_timeout = 0,
615 	},
616 };
617 
618 static int
619 xfs_error_sysfs_init_class(
620 	struct xfs_mount	*mp,
621 	int			class,
622 	const char		*parent_name,
623 	struct xfs_kobj		*parent_kobj,
624 	const struct xfs_error_init init[])
625 {
626 	struct xfs_error_cfg	*cfg;
627 	int			error;
628 	int			i;
629 
630 	ASSERT(class < XFS_ERR_CLASS_MAX);
631 
632 	error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
633 				&mp->m_error_kobj, parent_name);
634 	if (error)
635 		return error;
636 
637 	for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
638 		cfg = &mp->m_error_cfg[class][i];
639 		error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
640 					parent_kobj, init[i].name);
641 		if (error)
642 			goto out_error;
643 
644 		cfg->max_retries = init[i].max_retries;
645 		if (init[i].retry_timeout == XFS_ERR_RETRY_FOREVER)
646 			cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
647 		else
648 			cfg->retry_timeout = msecs_to_jiffies(
649 					init[i].retry_timeout * MSEC_PER_SEC);
650 	}
651 	return 0;
652 
653 out_error:
654 	/* unwind the entries that succeeded */
655 	for (i--; i >= 0; i--) {
656 		cfg = &mp->m_error_cfg[class][i];
657 		xfs_sysfs_del(&cfg->kobj);
658 	}
659 	xfs_sysfs_del(parent_kobj);
660 	return error;
661 }
662 
663 int
664 xfs_error_sysfs_init(
665 	struct xfs_mount	*mp)
666 {
667 	int			error;
668 
669 	/* .../xfs/<dev>/error/ */
670 	error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
671 				&mp->m_kobj, "error");
672 	if (error)
673 		return error;
674 
675 	error = sysfs_create_file(&mp->m_error_kobj.kobject,
676 				  ATTR_LIST(fail_at_unmount));
677 
678 	if (error)
679 		goto out_error;
680 
681 	/* .../xfs/<dev>/error/metadata/ */
682 	error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
683 				"metadata", &mp->m_error_meta_kobj,
684 				xfs_error_meta_init);
685 	if (error)
686 		goto out_error;
687 
688 	return 0;
689 
690 out_error:
691 	xfs_sysfs_del(&mp->m_error_kobj);
692 	return error;
693 }
694 
695 void
696 xfs_error_sysfs_del(
697 	struct xfs_mount	*mp)
698 {
699 	struct xfs_error_cfg	*cfg;
700 	int			i, j;
701 
702 	for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
703 		for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
704 			cfg = &mp->m_error_cfg[i][j];
705 
706 			xfs_sysfs_del(&cfg->kobj);
707 		}
708 	}
709 	xfs_sysfs_del(&mp->m_error_meta_kobj);
710 	xfs_sysfs_del(&mp->m_error_kobj);
711 }
712 
713 struct xfs_error_cfg *
714 xfs_error_get_cfg(
715 	struct xfs_mount	*mp,
716 	int			error_class,
717 	int			error)
718 {
719 	struct xfs_error_cfg	*cfg;
720 
721 	if (error < 0)
722 		error = -error;
723 
724 	switch (error) {
725 	case EIO:
726 		cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO];
727 		break;
728 	case ENOSPC:
729 		cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC];
730 		break;
731 	case ENODEV:
732 		cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV];
733 		break;
734 	default:
735 		cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];
736 		break;
737 	}
738 
739 	return cfg;
740 }
741