1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014 Red Hat, Inc.
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"
14 #include "xfs_log_priv.h"
15 #include "xfs_mount.h"
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,
24 static inline struct xfs_sysfs_attr *
25 to_attr(struct attribute *attr)
27 return container_of(attr, struct xfs_sysfs_attr, attr);
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)
37 #define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr
40 xfs_sysfs_object_show(
41 struct kobject *kobject,
42 struct attribute *attr,
45 struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
47 return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0;
51 xfs_sysfs_object_store(
52 struct kobject *kobject,
53 struct attribute *attr,
57 struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
59 return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0;
62 static const struct sysfs_ops xfs_sysfs_ops = {
63 .show = xfs_sysfs_object_show,
64 .store = xfs_sysfs_object_store,
67 static struct attribute *xfs_mp_attrs[] = {
70 ATTRIBUTE_GROUPS(xfs_mp);
72 struct kobj_type xfs_mp_ktype = {
73 .release = xfs_sysfs_release,
74 .sysfs_ops = &xfs_sysfs_ops,
75 .default_groups = xfs_mp_groups,
83 struct kobject *kobject,
90 ret = kstrtoint(buf, 0, &val);
95 xfs_globals.bug_on_assert = true;
97 xfs_globals.bug_on_assert = false;
106 struct kobject *kobject,
109 return sysfs_emit(buf, "%d\n", xfs_globals.bug_on_assert);
111 XFS_SYSFS_ATTR_RW(bug_on_assert);
114 log_recovery_delay_store(
115 struct kobject *kobject,
122 ret = kstrtoint(buf, 0, &val);
126 if (val < 0 || val > 60)
129 xfs_globals.log_recovery_delay = val;
135 log_recovery_delay_show(
136 struct kobject *kobject,
139 return sysfs_emit(buf, "%d\n", xfs_globals.log_recovery_delay);
141 XFS_SYSFS_ATTR_RW(log_recovery_delay);
145 struct kobject *kobject,
152 ret = kstrtoint(buf, 0, &val);
156 if (val < 0 || val > 60)
159 xfs_globals.mount_delay = val;
166 struct kobject *kobject,
169 return sysfs_emit(buf, "%d\n", xfs_globals.mount_delay);
171 XFS_SYSFS_ATTR_RW(mount_delay);
175 struct kobject *kobject,
181 ret = kstrtobool(buf, &xfs_globals.always_cow);
189 struct kobject *kobject,
192 return sysfs_emit(buf, "%d\n", xfs_globals.always_cow);
194 XFS_SYSFS_ATTR_RW(always_cow);
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.
204 struct kobject *kobject,
211 ret = kstrtoint(buf, 0, &val);
215 if (val < -1 || val > num_possible_cpus())
218 xfs_globals.pwork_threads = val;
225 struct kobject *kobject,
228 return sysfs_emit(buf, "%d\n", xfs_globals.pwork_threads);
230 XFS_SYSFS_ATTR_RW(pwork_threads);
233 static struct attribute *xfs_dbg_attrs[] = {
234 ATTR_LIST(bug_on_assert),
235 ATTR_LIST(log_recovery_delay),
236 ATTR_LIST(mount_delay),
237 ATTR_LIST(always_cow),
239 ATTR_LIST(pwork_threads),
243 ATTRIBUTE_GROUPS(xfs_dbg);
245 struct kobj_type xfs_dbg_ktype = {
246 .release = xfs_sysfs_release,
247 .sysfs_ops = &xfs_sysfs_ops,
248 .default_groups = xfs_dbg_groups,
255 static inline struct xstats *
256 to_xstats(struct kobject *kobject)
258 struct xfs_kobj *kobj = to_kobj(kobject);
260 return container_of(kobj, struct xstats, xs_kobj);
265 struct kobject *kobject,
268 struct xstats *stats = to_xstats(kobject);
270 return xfs_stats_format(stats->xs_stats, buf);
272 XFS_SYSFS_ATTR_RO(stats);
276 struct kobject *kobject,
282 struct xstats *stats = to_xstats(kobject);
284 ret = kstrtoint(buf, 0, &val);
291 xfs_stats_clearall(stats->xs_stats);
294 XFS_SYSFS_ATTR_WO(stats_clear);
296 static struct attribute *xfs_stats_attrs[] = {
298 ATTR_LIST(stats_clear),
301 ATTRIBUTE_GROUPS(xfs_stats);
303 struct kobj_type xfs_stats_ktype = {
304 .release = xfs_sysfs_release,
305 .sysfs_ops = &xfs_sysfs_ops,
306 .default_groups = xfs_stats_groups,
311 static inline struct xlog *
312 to_xlog(struct kobject *kobject)
314 struct xfs_kobj *kobj = to_kobj(kobject);
316 return container_of(kobj, struct xlog, l_kobj);
321 struct kobject *kobject,
326 struct xlog *log = to_xlog(kobject);
328 spin_lock(&log->l_icloglock);
329 cycle = log->l_curr_cycle;
330 block = log->l_curr_block;
331 spin_unlock(&log->l_icloglock);
333 return sysfs_emit(buf, "%d:%d\n", cycle, block);
335 XFS_SYSFS_ATTR_RO(log_head_lsn);
339 struct kobject *kobject,
344 struct xlog *log = to_xlog(kobject);
346 xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block);
347 return sysfs_emit(buf, "%d:%d\n", cycle, block);
349 XFS_SYSFS_ATTR_RO(log_tail_lsn);
352 reserve_grant_head_show(
353 struct kobject *kobject,
359 struct xlog *log = to_xlog(kobject);
361 xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes);
362 return sysfs_emit(buf, "%d:%d\n", cycle, bytes);
364 XFS_SYSFS_ATTR_RO(reserve_grant_head);
367 write_grant_head_show(
368 struct kobject *kobject,
373 struct xlog *log = to_xlog(kobject);
375 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes);
376 return sysfs_emit(buf, "%d:%d\n", cycle, bytes);
378 XFS_SYSFS_ATTR_RO(write_grant_head);
380 static struct attribute *xfs_log_attrs[] = {
381 ATTR_LIST(log_head_lsn),
382 ATTR_LIST(log_tail_lsn),
383 ATTR_LIST(reserve_grant_head),
384 ATTR_LIST(write_grant_head),
387 ATTRIBUTE_GROUPS(xfs_log);
389 struct kobj_type xfs_log_ktype = {
390 .release = xfs_sysfs_release,
391 .sysfs_ops = &xfs_sysfs_ops,
392 .default_groups = xfs_log_groups,
396 * Metadata IO error configuration
398 * The sysfs structure here is:
399 * ...xfs/<dev>/error/<class>/<errno>/<error_attrs>
401 * where <class> allows us to discriminate between data IO and metadata IO,
402 * and any other future type of IO (e.g. special inode or directory error
403 * handling) we care to support.
405 static inline struct xfs_error_cfg *
406 to_error_cfg(struct kobject *kobject)
408 struct xfs_kobj *kobj = to_kobj(kobject);
409 return container_of(kobj, struct xfs_error_cfg, kobj);
412 static inline struct xfs_mount *
413 err_to_mp(struct kobject *kobject)
415 struct xfs_kobj *kobj = to_kobj(kobject);
416 return container_of(kobj, struct xfs_mount, m_error_kobj);
421 struct kobject *kobject,
425 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
427 if (cfg->max_retries == XFS_ERR_RETRY_FOREVER)
430 retries = cfg->max_retries;
432 return sysfs_emit(buf, "%d\n", retries);
437 struct kobject *kobject,
441 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
445 ret = kstrtoint(buf, 0, &val);
453 cfg->max_retries = XFS_ERR_RETRY_FOREVER;
455 cfg->max_retries = val;
458 XFS_SYSFS_ATTR_RW(max_retries);
461 retry_timeout_seconds_show(
462 struct kobject *kobject,
466 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
468 if (cfg->retry_timeout == XFS_ERR_RETRY_FOREVER)
471 timeout = jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC;
473 return sysfs_emit(buf, "%d\n", timeout);
477 retry_timeout_seconds_store(
478 struct kobject *kobject,
482 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
486 ret = kstrtoint(buf, 0, &val);
490 /* 1 day timeout maximum, -1 means infinite */
491 if (val < -1 || val > 86400)
495 cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
497 cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
498 ASSERT(msecs_to_jiffies(val * MSEC_PER_SEC) < LONG_MAX);
502 XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
505 fail_at_unmount_show(
506 struct kobject *kobject,
509 struct xfs_mount *mp = err_to_mp(kobject);
511 return sysfs_emit(buf, "%d\n", mp->m_fail_unmount);
515 fail_at_unmount_store(
516 struct kobject *kobject,
520 struct xfs_mount *mp = err_to_mp(kobject);
524 ret = kstrtoint(buf, 0, &val);
528 if (val < 0 || val > 1)
531 mp->m_fail_unmount = val;
534 XFS_SYSFS_ATTR_RW(fail_at_unmount);
536 static struct attribute *xfs_error_attrs[] = {
537 ATTR_LIST(max_retries),
538 ATTR_LIST(retry_timeout_seconds),
541 ATTRIBUTE_GROUPS(xfs_error);
543 static struct kobj_type xfs_error_cfg_ktype = {
544 .release = xfs_sysfs_release,
545 .sysfs_ops = &xfs_sysfs_ops,
546 .default_groups = xfs_error_groups,
549 static struct kobj_type xfs_error_ktype = {
550 .release = xfs_sysfs_release,
551 .sysfs_ops = &xfs_sysfs_ops,
555 * Error initialization tables. These need to be ordered in the same
556 * order as the enums used to index the array. All class init tables need to
557 * define a "default" behaviour as the first entry, all other entries can be
560 struct xfs_error_init {
563 int retry_timeout; /* in seconds */
566 static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
568 .max_retries = XFS_ERR_RETRY_FOREVER,
569 .retry_timeout = XFS_ERR_RETRY_FOREVER,
572 .max_retries = XFS_ERR_RETRY_FOREVER,
573 .retry_timeout = XFS_ERR_RETRY_FOREVER,
576 .max_retries = XFS_ERR_RETRY_FOREVER,
577 .retry_timeout = XFS_ERR_RETRY_FOREVER,
580 .max_retries = 0, /* We can't recover from devices disappearing */
586 xfs_error_sysfs_init_class(
587 struct xfs_mount *mp,
589 const char *parent_name,
590 struct xfs_kobj *parent_kobj,
591 const struct xfs_error_init init[])
593 struct xfs_error_cfg *cfg;
597 ASSERT(class < XFS_ERR_CLASS_MAX);
599 error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
600 &mp->m_error_kobj, parent_name);
604 for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
605 cfg = &mp->m_error_cfg[class][i];
606 error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
607 parent_kobj, init[i].name);
611 cfg->max_retries = init[i].max_retries;
612 if (init[i].retry_timeout == XFS_ERR_RETRY_FOREVER)
613 cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
615 cfg->retry_timeout = msecs_to_jiffies(
616 init[i].retry_timeout * MSEC_PER_SEC);
621 /* unwind the entries that succeeded */
622 for (i--; i >= 0; i--) {
623 cfg = &mp->m_error_cfg[class][i];
624 xfs_sysfs_del(&cfg->kobj);
626 xfs_sysfs_del(parent_kobj);
631 xfs_error_sysfs_init(
632 struct xfs_mount *mp)
636 /* .../xfs/<dev>/error/ */
637 error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
638 &mp->m_kobj, "error");
642 error = sysfs_create_file(&mp->m_error_kobj.kobject,
643 ATTR_LIST(fail_at_unmount));
648 /* .../xfs/<dev>/error/metadata/ */
649 error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
650 "metadata", &mp->m_error_meta_kobj,
651 xfs_error_meta_init);
658 xfs_sysfs_del(&mp->m_error_kobj);
664 struct xfs_mount *mp)
666 struct xfs_error_cfg *cfg;
669 for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
670 for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
671 cfg = &mp->m_error_cfg[i][j];
673 xfs_sysfs_del(&cfg->kobj);
676 xfs_sysfs_del(&mp->m_error_meta_kobj);
677 xfs_sysfs_del(&mp->m_error_kobj);
680 struct xfs_error_cfg *
682 struct xfs_mount *mp,
686 struct xfs_error_cfg *cfg;
693 cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO];
696 cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC];
699 cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV];
702 cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];