2 * Copyright (c) 2014 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sysfs.h"
26 #include "xfs_log_priv.h"
27 #include "xfs_stats.h"
28 #include "xfs_mount.h"
30 struct xfs_sysfs_attr {
31 struct attribute attr;
32 ssize_t (*show)(struct kobject *kobject, char *buf);
33 ssize_t (*store)(struct kobject *kobject, const char *buf,
37 static inline struct xfs_sysfs_attr *
38 to_attr(struct attribute *attr)
40 return container_of(attr, struct xfs_sysfs_attr, attr);
43 #define XFS_SYSFS_ATTR_RW(name) \
44 static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RW(name)
45 #define XFS_SYSFS_ATTR_RO(name) \
46 static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RO(name)
47 #define XFS_SYSFS_ATTR_WO(name) \
48 static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_WO(name)
50 #define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr
53 xfs_sysfs_object_show(
54 struct kobject *kobject,
55 struct attribute *attr,
58 struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
60 return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0;
64 xfs_sysfs_object_store(
65 struct kobject *kobject,
66 struct attribute *attr,
70 struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
72 return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0;
75 static const struct sysfs_ops xfs_sysfs_ops = {
76 .show = xfs_sysfs_object_show,
77 .store = xfs_sysfs_object_store,
81 * xfs_mount kobject. The mp kobject also serves as the per-mount parent object
82 * that is identified by the fsname under sysfs.
85 static inline struct xfs_mount *
86 to_mp(struct kobject *kobject)
88 struct xfs_kobj *kobj = to_kobj(kobject);
90 return container_of(kobj, struct xfs_mount, m_kobj);
93 static struct attribute *xfs_mp_attrs[] = {
97 struct kobj_type xfs_mp_ktype = {
98 .release = xfs_sysfs_release,
99 .sysfs_ops = &xfs_sysfs_ops,
100 .default_attrs = xfs_mp_attrs,
108 struct kobject *kobject,
115 ret = kstrtoint(buf, 0, &val);
120 xfs_globals.bug_on_assert = true;
122 xfs_globals.bug_on_assert = false;
131 struct kobject *kobject,
134 return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.bug_on_assert ? 1 : 0);
136 XFS_SYSFS_ATTR_RW(bug_on_assert);
139 log_recovery_delay_store(
140 struct kobject *kobject,
147 ret = kstrtoint(buf, 0, &val);
151 if (val < 0 || val > 60)
154 xfs_globals.log_recovery_delay = val;
160 log_recovery_delay_show(
161 struct kobject *kobject,
164 return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.log_recovery_delay);
166 XFS_SYSFS_ATTR_RW(log_recovery_delay);
170 struct kobject *kobject,
177 ret = kstrtoint(buf, 0, &val);
181 if (val < 0 || val > 60)
184 xfs_globals.mount_delay = val;
191 struct kobject *kobject,
194 return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.mount_delay);
196 XFS_SYSFS_ATTR_RW(mount_delay);
198 static struct attribute *xfs_dbg_attrs[] = {
199 ATTR_LIST(bug_on_assert),
200 ATTR_LIST(log_recovery_delay),
201 ATTR_LIST(mount_delay),
205 struct kobj_type xfs_dbg_ktype = {
206 .release = xfs_sysfs_release,
207 .sysfs_ops = &xfs_sysfs_ops,
208 .default_attrs = xfs_dbg_attrs,
215 static inline struct xstats *
216 to_xstats(struct kobject *kobject)
218 struct xfs_kobj *kobj = to_kobj(kobject);
220 return container_of(kobj, struct xstats, xs_kobj);
225 struct kobject *kobject,
228 struct xstats *stats = to_xstats(kobject);
230 return xfs_stats_format(stats->xs_stats, buf);
232 XFS_SYSFS_ATTR_RO(stats);
236 struct kobject *kobject,
242 struct xstats *stats = to_xstats(kobject);
244 ret = kstrtoint(buf, 0, &val);
251 xfs_stats_clearall(stats->xs_stats);
254 XFS_SYSFS_ATTR_WO(stats_clear);
256 static struct attribute *xfs_stats_attrs[] = {
258 ATTR_LIST(stats_clear),
262 struct kobj_type xfs_stats_ktype = {
263 .release = xfs_sysfs_release,
264 .sysfs_ops = &xfs_sysfs_ops,
265 .default_attrs = xfs_stats_attrs,
270 static inline struct xlog *
271 to_xlog(struct kobject *kobject)
273 struct xfs_kobj *kobj = to_kobj(kobject);
275 return container_of(kobj, struct xlog, l_kobj);
280 struct kobject *kobject,
285 struct xlog *log = to_xlog(kobject);
287 spin_lock(&log->l_icloglock);
288 cycle = log->l_curr_cycle;
289 block = log->l_curr_block;
290 spin_unlock(&log->l_icloglock);
292 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
294 XFS_SYSFS_ATTR_RO(log_head_lsn);
298 struct kobject *kobject,
303 struct xlog *log = to_xlog(kobject);
305 xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block);
306 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
308 XFS_SYSFS_ATTR_RO(log_tail_lsn);
311 reserve_grant_head_show(
312 struct kobject *kobject,
318 struct xlog *log = to_xlog(kobject);
320 xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes);
321 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
323 XFS_SYSFS_ATTR_RO(reserve_grant_head);
326 write_grant_head_show(
327 struct kobject *kobject,
332 struct xlog *log = to_xlog(kobject);
334 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes);
335 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
337 XFS_SYSFS_ATTR_RO(write_grant_head);
339 static struct attribute *xfs_log_attrs[] = {
340 ATTR_LIST(log_head_lsn),
341 ATTR_LIST(log_tail_lsn),
342 ATTR_LIST(reserve_grant_head),
343 ATTR_LIST(write_grant_head),
347 struct kobj_type xfs_log_ktype = {
348 .release = xfs_sysfs_release,
349 .sysfs_ops = &xfs_sysfs_ops,
350 .default_attrs = xfs_log_attrs,
354 * Metadata IO error configuration
356 * The sysfs structure here is:
357 * ...xfs/<dev>/error/<class>/<errno>/<error_attrs>
359 * where <class> allows us to discriminate between data IO and metadata IO,
360 * and any other future type of IO (e.g. special inode or directory error
361 * handling) we care to support.
363 static inline struct xfs_error_cfg *
364 to_error_cfg(struct kobject *kobject)
366 struct xfs_kobj *kobj = to_kobj(kobject);
367 return container_of(kobj, struct xfs_error_cfg, kobj);
370 static inline struct xfs_mount *
371 err_to_mp(struct kobject *kobject)
373 struct xfs_kobj *kobj = to_kobj(kobject);
374 return container_of(kobj, struct xfs_mount, m_error_kobj);
379 struct kobject *kobject,
383 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
385 if (cfg->max_retries == XFS_ERR_RETRY_FOREVER)
388 retries = cfg->max_retries;
390 return snprintf(buf, PAGE_SIZE, "%d\n", retries);
395 struct kobject *kobject,
399 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
403 ret = kstrtoint(buf, 0, &val);
411 cfg->max_retries = XFS_ERR_RETRY_FOREVER;
413 cfg->max_retries = val;
416 XFS_SYSFS_ATTR_RW(max_retries);
419 retry_timeout_seconds_show(
420 struct kobject *kobject,
424 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
426 if (cfg->retry_timeout == XFS_ERR_RETRY_FOREVER)
429 timeout = jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC;
431 return snprintf(buf, PAGE_SIZE, "%d\n", timeout);
435 retry_timeout_seconds_store(
436 struct kobject *kobject,
440 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
444 ret = kstrtoint(buf, 0, &val);
448 /* 1 day timeout maximum, -1 means infinite */
449 if (val < -1 || val > 86400)
453 cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
455 cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
456 ASSERT(msecs_to_jiffies(val * MSEC_PER_SEC) < LONG_MAX);
460 XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
463 fail_at_unmount_show(
464 struct kobject *kobject,
467 struct xfs_mount *mp = err_to_mp(kobject);
469 return snprintf(buf, PAGE_SIZE, "%d\n", mp->m_fail_unmount);
473 fail_at_unmount_store(
474 struct kobject *kobject,
478 struct xfs_mount *mp = err_to_mp(kobject);
482 ret = kstrtoint(buf, 0, &val);
486 if (val < 0 || val > 1)
489 mp->m_fail_unmount = val;
492 XFS_SYSFS_ATTR_RW(fail_at_unmount);
494 static struct attribute *xfs_error_attrs[] = {
495 ATTR_LIST(max_retries),
496 ATTR_LIST(retry_timeout_seconds),
501 static struct kobj_type xfs_error_cfg_ktype = {
502 .release = xfs_sysfs_release,
503 .sysfs_ops = &xfs_sysfs_ops,
504 .default_attrs = xfs_error_attrs,
507 static struct kobj_type xfs_error_ktype = {
508 .release = xfs_sysfs_release,
509 .sysfs_ops = &xfs_sysfs_ops,
513 * Error initialization tables. These need to be ordered in the same
514 * order as the enums used to index the array. All class init tables need to
515 * define a "default" behaviour as the first entry, all other entries can be
518 struct xfs_error_init {
521 int retry_timeout; /* in seconds */
524 static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
526 .max_retries = XFS_ERR_RETRY_FOREVER,
527 .retry_timeout = XFS_ERR_RETRY_FOREVER,
530 .max_retries = XFS_ERR_RETRY_FOREVER,
531 .retry_timeout = XFS_ERR_RETRY_FOREVER,
534 .max_retries = XFS_ERR_RETRY_FOREVER,
535 .retry_timeout = XFS_ERR_RETRY_FOREVER,
538 .max_retries = 0, /* We can't recover from devices disappearing */
544 xfs_error_sysfs_init_class(
545 struct xfs_mount *mp,
547 const char *parent_name,
548 struct xfs_kobj *parent_kobj,
549 const struct xfs_error_init init[])
551 struct xfs_error_cfg *cfg;
555 ASSERT(class < XFS_ERR_CLASS_MAX);
557 error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
558 &mp->m_error_kobj, parent_name);
562 for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
563 cfg = &mp->m_error_cfg[class][i];
564 error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
565 parent_kobj, init[i].name);
569 cfg->max_retries = init[i].max_retries;
570 if (init[i].retry_timeout == XFS_ERR_RETRY_FOREVER)
571 cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
573 cfg->retry_timeout = msecs_to_jiffies(
574 init[i].retry_timeout * MSEC_PER_SEC);
579 /* unwind the entries that succeeded */
580 for (i--; i >= 0; i--) {
581 cfg = &mp->m_error_cfg[class][i];
582 xfs_sysfs_del(&cfg->kobj);
584 xfs_sysfs_del(parent_kobj);
589 xfs_error_sysfs_init(
590 struct xfs_mount *mp)
594 /* .../xfs/<dev>/error/ */
595 error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
596 &mp->m_kobj, "error");
600 error = sysfs_create_file(&mp->m_error_kobj.kobject,
601 ATTR_LIST(fail_at_unmount));
606 /* .../xfs/<dev>/error/metadata/ */
607 error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
608 "metadata", &mp->m_error_meta_kobj,
609 xfs_error_meta_init);
616 xfs_sysfs_del(&mp->m_error_kobj);
622 struct xfs_mount *mp)
624 struct xfs_error_cfg *cfg;
627 for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
628 for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
629 cfg = &mp->m_error_cfg[i][j];
631 xfs_sysfs_del(&cfg->kobj);
634 xfs_sysfs_del(&mp->m_error_meta_kobj);
635 xfs_sysfs_del(&mp->m_error_kobj);
638 struct xfs_error_cfg *
640 struct xfs_mount *mp,
644 struct xfs_error_cfg *cfg;
651 cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO];
654 cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC];
657 cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV];
660 cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];