blob: 918d144febd9eb097b04b442be9eb5bcf7a4db78 [file] [log] [blame]
Brian Fostera31b1d32014-07-15 08:07:01 +10001/*
2 * Copyright (c) 2014 Red Hat, Inc.
3 * All Rights Reserved.
4 *
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.
8 *
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.
13 *
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
17 */
18
19#include "xfs.h"
Carlos Maiolino192852b2016-05-18 10:58:51 +100020#include "xfs_shared.h"
Brian Foster801cc4e2016-03-15 11:42:44 +110021#include "xfs_format.h"
Brian Fosterbaff4e42014-07-15 08:07:29 +100022#include "xfs_log_format.h"
Brian Foster801cc4e2016-03-15 11:42:44 +110023#include "xfs_trans_resv.h"
Carlos Maiolino192852b2016-05-18 10:58:51 +100024#include "xfs_sysfs.h"
Brian Fosterbaff4e42014-07-15 08:07:29 +100025#include "xfs_log.h"
26#include "xfs_log_priv.h"
Bill O'Donnellbb230c12015-10-12 05:15:45 +110027#include "xfs_stats.h"
Brian Foster801cc4e2016-03-15 11:42:44 +110028#include "xfs_mount.h"
Brian Fostera31b1d32014-07-15 08:07:01 +100029
30struct xfs_sysfs_attr {
31 struct attribute attr;
Bill O'Donnella27c26402015-10-12 05:18:45 +110032 ssize_t (*show)(struct kobject *kobject, char *buf);
33 ssize_t (*store)(struct kobject *kobject, const char *buf,
34 size_t count);
Brian Fostera31b1d32014-07-15 08:07:01 +100035};
36
37static inline struct xfs_sysfs_attr *
38to_attr(struct attribute *attr)
39{
40 return container_of(attr, struct xfs_sysfs_attr, attr);
41}
42
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)
Bill O'Donnellbb230c12015-10-12 05:15:45 +110047#define XFS_SYSFS_ATTR_WO(name) \
48 static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_WO(name)
Brian Fostera31b1d32014-07-15 08:07:01 +100049
50#define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr
51
Bill O'Donnella27c26402015-10-12 05:18:45 +110052STATIC ssize_t
53xfs_sysfs_object_show(
54 struct kobject *kobject,
55 struct attribute *attr,
56 char *buf)
57{
58 struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
59
60 return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0;
61}
62
63STATIC ssize_t
64xfs_sysfs_object_store(
65 struct kobject *kobject,
66 struct attribute *attr,
67 const char *buf,
68 size_t count)
69{
70 struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
71
72 return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0;
73}
74
75static const struct sysfs_ops xfs_sysfs_ops = {
76 .show = xfs_sysfs_object_show,
77 .store = xfs_sysfs_object_store,
78};
79
Brian Foster801cc4e2016-03-15 11:42:44 +110080/*
81 * xfs_mount kobject. The mp kobject also serves as the per-mount parent object
82 * that is identified by the fsname under sysfs.
83 */
84
85static inline struct xfs_mount *
86to_mp(struct kobject *kobject)
87{
88 struct xfs_kobj *kobj = to_kobj(kobject);
89
90 return container_of(kobj, struct xfs_mount, m_kobj);
91}
92
93#ifdef DEBUG
94
95STATIC ssize_t
96fail_writes_store(
97 struct kobject *kobject,
98 const char *buf,
99 size_t count)
100{
101 struct xfs_mount *mp = to_mp(kobject);
102 int ret;
103 int val;
104
105 ret = kstrtoint(buf, 0, &val);
106 if (ret)
107 return ret;
108
109 if (val == 1)
110 mp->m_fail_writes = true;
111 else if (val == 0)
112 mp->m_fail_writes = false;
113 else
114 return -EINVAL;
115
116 return count;
117}
118
119STATIC ssize_t
120fail_writes_show(
121 struct kobject *kobject,
122 char *buf)
123{
124 struct xfs_mount *mp = to_mp(kobject);
125
126 return snprintf(buf, PAGE_SIZE, "%d\n", mp->m_fail_writes ? 1 : 0);
127}
128XFS_SYSFS_ATTR_RW(fail_writes);
129
130#endif /* DEBUG */
131
132static struct attribute *xfs_mp_attrs[] = {
133#ifdef DEBUG
134 ATTR_LIST(fail_writes),
135#endif
136 NULL,
137};
138
139struct kobj_type xfs_mp_ktype = {
140 .release = xfs_sysfs_release,
141 .sysfs_ops = &xfs_sysfs_ops,
142 .default_attrs = xfs_mp_attrs,
143};
144
Brian Foster65b65732014-09-09 11:52:42 +1000145#ifdef DEBUG
146/* debug */
147
Brian Foster2e227172014-09-09 11:56:13 +1000148STATIC ssize_t
149log_recovery_delay_store(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100150 struct kobject *kobject,
Brian Foster2e227172014-09-09 11:56:13 +1000151 const char *buf,
Bill O'Donnella27c26402015-10-12 05:18:45 +1100152 size_t count)
Brian Foster2e227172014-09-09 11:56:13 +1000153{
154 int ret;
155 int val;
156
157 ret = kstrtoint(buf, 0, &val);
158 if (ret)
159 return ret;
160
161 if (val < 0 || val > 60)
162 return -EINVAL;
163
164 xfs_globals.log_recovery_delay = val;
165
166 return count;
167}
168
169STATIC ssize_t
170log_recovery_delay_show(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100171 struct kobject *kobject,
172 char *buf)
Brian Foster2e227172014-09-09 11:56:13 +1000173{
174 return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.log_recovery_delay);
175}
176XFS_SYSFS_ATTR_RW(log_recovery_delay);
177
Brian Foster65b65732014-09-09 11:52:42 +1000178static struct attribute *xfs_dbg_attrs[] = {
Brian Foster2e227172014-09-09 11:56:13 +1000179 ATTR_LIST(log_recovery_delay),
Brian Foster65b65732014-09-09 11:52:42 +1000180 NULL,
181};
182
Brian Foster65b65732014-09-09 11:52:42 +1000183struct kobj_type xfs_dbg_ktype = {
184 .release = xfs_sysfs_release,
Bill O'Donnella27c26402015-10-12 05:18:45 +1100185 .sysfs_ops = &xfs_sysfs_ops,
Brian Foster65b65732014-09-09 11:52:42 +1000186 .default_attrs = xfs_dbg_attrs,
187};
188
189#endif /* DEBUG */
190
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100191/* stats */
192
Bill O'Donnell80529c42015-10-12 05:19:45 +1100193static inline struct xstats *
194to_xstats(struct kobject *kobject)
195{
196 struct xfs_kobj *kobj = to_kobj(kobject);
197
198 return container_of(kobj, struct xstats, xs_kobj);
199}
200
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100201STATIC ssize_t
202stats_show(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100203 struct kobject *kobject,
204 char *buf)
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100205{
Bill O'Donnell80529c42015-10-12 05:19:45 +1100206 struct xstats *stats = to_xstats(kobject);
207
208 return xfs_stats_format(stats->xs_stats, buf);
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100209}
210XFS_SYSFS_ATTR_RO(stats);
211
212STATIC ssize_t
213stats_clear_store(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100214 struct kobject *kobject,
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100215 const char *buf,
Bill O'Donnella27c26402015-10-12 05:18:45 +1100216 size_t count)
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100217{
218 int ret;
219 int val;
Bill O'Donnell80529c42015-10-12 05:19:45 +1100220 struct xstats *stats = to_xstats(kobject);
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100221
222 ret = kstrtoint(buf, 0, &val);
223 if (ret)
224 return ret;
225
226 if (val != 1)
227 return -EINVAL;
Bill O'Donnell80529c42015-10-12 05:19:45 +1100228
229 xfs_stats_clearall(stats->xs_stats);
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100230 return count;
231}
232XFS_SYSFS_ATTR_WO(stats_clear);
233
234static struct attribute *xfs_stats_attrs[] = {
235 ATTR_LIST(stats),
236 ATTR_LIST(stats_clear),
237 NULL,
238};
239
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100240struct kobj_type xfs_stats_ktype = {
241 .release = xfs_sysfs_release,
Bill O'Donnella27c26402015-10-12 05:18:45 +1100242 .sysfs_ops = &xfs_sysfs_ops,
Bill O'Donnellbb230c12015-10-12 05:15:45 +1100243 .default_attrs = xfs_stats_attrs,
244};
245
Brian Fosterbaff4e42014-07-15 08:07:29 +1000246/* xlog */
247
Bill O'Donnella27c26402015-10-12 05:18:45 +1100248static inline struct xlog *
249to_xlog(struct kobject *kobject)
250{
251 struct xfs_kobj *kobj = to_kobj(kobject);
252
253 return container_of(kobj, struct xlog, l_kobj);
254}
255
Brian Foster80d6d692014-07-15 08:07:48 +1000256STATIC ssize_t
257log_head_lsn_show(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100258 struct kobject *kobject,
259 char *buf)
Brian Foster80d6d692014-07-15 08:07:48 +1000260{
Brian Foster80d6d692014-07-15 08:07:48 +1000261 int cycle;
262 int block;
Bill O'Donnella27c26402015-10-12 05:18:45 +1100263 struct xlog *log = to_xlog(kobject);
Brian Foster80d6d692014-07-15 08:07:48 +1000264
265 spin_lock(&log->l_icloglock);
266 cycle = log->l_curr_cycle;
267 block = log->l_curr_block;
268 spin_unlock(&log->l_icloglock);
269
270 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
271}
272XFS_SYSFS_ATTR_RO(log_head_lsn);
273
274STATIC ssize_t
275log_tail_lsn_show(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100276 struct kobject *kobject,
277 char *buf)
Brian Foster80d6d692014-07-15 08:07:48 +1000278{
Brian Foster80d6d692014-07-15 08:07:48 +1000279 int cycle;
280 int block;
Bill O'Donnella27c26402015-10-12 05:18:45 +1100281 struct xlog *log = to_xlog(kobject);
Brian Foster80d6d692014-07-15 08:07:48 +1000282
283 xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block);
284 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
285}
286XFS_SYSFS_ATTR_RO(log_tail_lsn);
287
288STATIC ssize_t
289reserve_grant_head_show(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100290 struct kobject *kobject,
291 char *buf)
292
Brian Foster80d6d692014-07-15 08:07:48 +1000293{
Brian Foster80d6d692014-07-15 08:07:48 +1000294 int cycle;
295 int bytes;
Bill O'Donnella27c26402015-10-12 05:18:45 +1100296 struct xlog *log = to_xlog(kobject);
Brian Foster80d6d692014-07-15 08:07:48 +1000297
298 xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes);
299 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
300}
301XFS_SYSFS_ATTR_RO(reserve_grant_head);
302
303STATIC ssize_t
304write_grant_head_show(
Bill O'Donnella27c26402015-10-12 05:18:45 +1100305 struct kobject *kobject,
306 char *buf)
Brian Foster80d6d692014-07-15 08:07:48 +1000307{
Brian Foster80d6d692014-07-15 08:07:48 +1000308 int cycle;
309 int bytes;
Bill O'Donnella27c26402015-10-12 05:18:45 +1100310 struct xlog *log = to_xlog(kobject);
Brian Foster80d6d692014-07-15 08:07:48 +1000311
312 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes);
313 return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
314}
315XFS_SYSFS_ATTR_RO(write_grant_head);
316
Brian Foster609adfc2016-01-05 07:41:16 +1100317#ifdef DEBUG
318STATIC ssize_t
319log_badcrc_factor_store(
320 struct kobject *kobject,
321 const char *buf,
322 size_t count)
323{
324 struct xlog *log = to_xlog(kobject);
325 int ret;
326 uint32_t val;
327
328 ret = kstrtouint(buf, 0, &val);
329 if (ret)
330 return ret;
331
332 log->l_badcrc_factor = val;
333
334 return count;
335}
336
337STATIC ssize_t
338log_badcrc_factor_show(
339 struct kobject *kobject,
340 char *buf)
341{
342 struct xlog *log = to_xlog(kobject);
343
344 return snprintf(buf, PAGE_SIZE, "%d\n", log->l_badcrc_factor);
345}
346
347XFS_SYSFS_ATTR_RW(log_badcrc_factor);
348#endif /* DEBUG */
349
Brian Fosterbaff4e42014-07-15 08:07:29 +1000350static struct attribute *xfs_log_attrs[] = {
Brian Foster80d6d692014-07-15 08:07:48 +1000351 ATTR_LIST(log_head_lsn),
352 ATTR_LIST(log_tail_lsn),
353 ATTR_LIST(reserve_grant_head),
354 ATTR_LIST(write_grant_head),
Brian Foster609adfc2016-01-05 07:41:16 +1100355#ifdef DEBUG
356 ATTR_LIST(log_badcrc_factor),
357#endif
Brian Fosterbaff4e42014-07-15 08:07:29 +1000358 NULL,
359};
360
Brian Fosterbaff4e42014-07-15 08:07:29 +1000361struct kobj_type xfs_log_ktype = {
362 .release = xfs_sysfs_release,
Bill O'Donnella27c26402015-10-12 05:18:45 +1100363 .sysfs_ops = &xfs_sysfs_ops,
Brian Fosterbaff4e42014-07-15 08:07:29 +1000364 .default_attrs = xfs_log_attrs,
365};
Carlos Maiolino192852b2016-05-18 10:58:51 +1000366
367/*
368 * Metadata IO error configuration
369 *
370 * The sysfs structure here is:
371 * ...xfs/<dev>/error/<class>/<errno>/<error_attrs>
372 *
373 * where <class> allows us to discriminate between data IO and metadata IO,
374 * and any other future type of IO (e.g. special inode or directory error
375 * handling) we care to support.
376 */
Carlos Maiolino192852b2016-05-18 10:58:51 +1000377static inline struct xfs_error_cfg *
378to_error_cfg(struct kobject *kobject)
379{
380 struct xfs_kobj *kobj = to_kobj(kobject);
381 return container_of(kobj, struct xfs_error_cfg, kobj);
382}
383
Carlos Maiolinoa5ea70d2016-05-18 11:08:15 +1000384static ssize_t
385max_retries_show(
386 struct kobject *kobject,
387 char *buf)
388{
389 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
390
391 return snprintf(buf, PAGE_SIZE, "%d\n", cfg->max_retries);
392}
393
394static ssize_t
395max_retries_store(
396 struct kobject *kobject,
397 const char *buf,
398 size_t count)
399{
400 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
401 int ret;
402 int val;
403
404 ret = kstrtoint(buf, 0, &val);
405 if (ret)
406 return ret;
407
408 if (val < -1)
409 return -EINVAL;
410
411 cfg->max_retries = val;
412 return count;
413}
414XFS_SYSFS_ATTR_RW(max_retries);
415
416static ssize_t
417retry_timeout_seconds_show(
418 struct kobject *kobject,
419 char *buf)
420{
421 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
422
423 return snprintf(buf, PAGE_SIZE, "%ld\n",
424 jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC);
425}
426
427static ssize_t
428retry_timeout_seconds_store(
429 struct kobject *kobject,
430 const char *buf,
431 size_t count)
432{
433 struct xfs_error_cfg *cfg = to_error_cfg(kobject);
434 int ret;
435 int val;
436
437 ret = kstrtoint(buf, 0, &val);
438 if (ret)
439 return ret;
440
441 /* 1 day timeout maximum */
442 if (val < 0 || val > 86400)
443 return -EINVAL;
444
445 cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
446 return count;
447}
448XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
449
450static struct attribute *xfs_error_attrs[] = {
451 ATTR_LIST(max_retries),
452 ATTR_LIST(retry_timeout_seconds),
453 NULL,
454};
455
456
Carlos Maiolino192852b2016-05-18 10:58:51 +1000457struct kobj_type xfs_error_cfg_ktype = {
458 .release = xfs_sysfs_release,
459 .sysfs_ops = &xfs_sysfs_ops,
460 .default_attrs = xfs_error_attrs,
461};
462
463struct kobj_type xfs_error_ktype = {
464 .release = xfs_sysfs_release,
465};
466
Carlos Maiolinoef6a50f2016-05-18 11:06:44 +1000467/*
468 * Error initialization tables. These need to be ordered in the same
469 * order as the enums used to index the array. All class init tables need to
470 * define a "default" behaviour as the first entry, all other entries can be
471 * empty.
472 */
473struct xfs_error_init {
474 char *name;
475 int max_retries;
Carlos Maiolinoa5ea70d2016-05-18 11:08:15 +1000476 int retry_timeout; /* in seconds */
Carlos Maiolinoef6a50f2016-05-18 11:06:44 +1000477};
478
479static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
480 { .name = "default",
481 .max_retries = -1,
Carlos Maiolinoa5ea70d2016-05-18 11:08:15 +1000482 .retry_timeout = 0,
Carlos Maiolinoef6a50f2016-05-18 11:06:44 +1000483 },
484};
485
486static int
487xfs_error_sysfs_init_class(
488 struct xfs_mount *mp,
489 int class,
490 const char *parent_name,
491 struct xfs_kobj *parent_kobj,
492 const struct xfs_error_init init[])
493{
494 struct xfs_error_cfg *cfg;
495 int error;
496 int i;
497
498 ASSERT(class < XFS_ERR_CLASS_MAX);
499
500 error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
501 &mp->m_error_kobj, parent_name);
502 if (error)
503 return error;
504
505 for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
506 cfg = &mp->m_error_cfg[class][i];
507 error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
508 parent_kobj, init[i].name);
509 if (error)
510 goto out_error;
511
512 cfg->max_retries = init[i].max_retries;
Carlos Maiolinoa5ea70d2016-05-18 11:08:15 +1000513 cfg->retry_timeout = msecs_to_jiffies(
514 init[i].retry_timeout * MSEC_PER_SEC);
Carlos Maiolinoef6a50f2016-05-18 11:06:44 +1000515 }
516 return 0;
517
518out_error:
519 /* unwind the entries that succeeded */
520 for (i--; i >= 0; i--) {
521 cfg = &mp->m_error_cfg[class][i];
522 xfs_sysfs_del(&cfg->kobj);
523 }
524 xfs_sysfs_del(parent_kobj);
525 return error;
526}
527
Carlos Maiolino192852b2016-05-18 10:58:51 +1000528int
529xfs_error_sysfs_init(
530 struct xfs_mount *mp)
531{
532 int error;
533
534 /* .../xfs/<dev>/error/ */
535 error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
536 &mp->m_kobj, "error");
Carlos Maiolinoffd40ef2016-05-18 11:01:00 +1000537 if (error)
538 return error;
539
540 /* .../xfs/<dev>/error/metadata/ */
Carlos Maiolinoef6a50f2016-05-18 11:06:44 +1000541 error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
542 "metadata", &mp->m_error_meta_kobj,
543 xfs_error_meta_init);
Carlos Maiolinoffd40ef2016-05-18 11:01:00 +1000544 if (error)
545 goto out_error;
546
Carlos Maiolinoffd40ef2016-05-18 11:01:00 +1000547 return 0;
548
Carlos Maiolinoffd40ef2016-05-18 11:01:00 +1000549out_error:
550 xfs_sysfs_del(&mp->m_error_kobj);
Carlos Maiolino192852b2016-05-18 10:58:51 +1000551 return error;
552}
553
554void
555xfs_error_sysfs_del(
556 struct xfs_mount *mp)
557{
Carlos Maiolinoffd40ef2016-05-18 11:01:00 +1000558 struct xfs_error_cfg *cfg;
559 int i, j;
560
561 for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
562 for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
563 cfg = &mp->m_error_cfg[i][j];
564
565 xfs_sysfs_del(&cfg->kobj);
566 }
567 }
568 xfs_sysfs_del(&mp->m_error_meta_kobj);
Carlos Maiolino192852b2016-05-18 10:58:51 +1000569 xfs_sysfs_del(&mp->m_error_kobj);
570}
Carlos Maiolinodf309392016-05-18 11:05:33 +1000571
572struct xfs_error_cfg *
573xfs_error_get_cfg(
574 struct xfs_mount *mp,
575 int error_class,
576 int error)
577{
578 struct xfs_error_cfg *cfg;
579
580 switch (error) {
581 default:
582 cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];
583 break;
584 }
585
586 return cfg;
587}