xfs_sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014 Red Hat, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_shared.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_sysfs.h"
  12. #include "xfs_log.h"
  13. #include "xfs_log_priv.h"
  14. #include "xfs_stats.h"
  15. #include "xfs_mount.h"
  16. struct xfs_sysfs_attr {
  17. struct attribute attr;
  18. ssize_t (*show)(struct kobject *kobject, char *buf);
  19. ssize_t (*store)(struct kobject *kobject, const char *buf,
  20. size_t count);
  21. };
  22. static inline struct xfs_sysfs_attr *
  23. to_attr(struct attribute *attr)
  24. {
  25. return container_of(attr, struct xfs_sysfs_attr, attr);
  26. }
  27. #define XFS_SYSFS_ATTR_RW(name) \
  28. static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RW(name)
  29. #define XFS_SYSFS_ATTR_RO(name) \
  30. static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RO(name)
  31. #define XFS_SYSFS_ATTR_WO(name) \
  32. static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_WO(name)
  33. #define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr
  34. STATIC ssize_t
  35. xfs_sysfs_object_show(
  36. struct kobject *kobject,
  37. struct attribute *attr,
  38. char *buf)
  39. {
  40. struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
  41. return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0;
  42. }
  43. STATIC ssize_t
  44. xfs_sysfs_object_store(
  45. struct kobject *kobject,
  46. struct attribute *attr,
  47. const char *buf,
  48. size_t count)
  49. {
  50. struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
  51. return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0;
  52. }
  53. static const struct sysfs_ops xfs_sysfs_ops = {
  54. .show = xfs_sysfs_object_show,
  55. .store = xfs_sysfs_object_store,
  56. };
  57. /*
  58. * xfs_mount kobject. The mp kobject also serves as the per-mount parent object
  59. * that is identified by the fsname under sysfs.
  60. */
  61. static inline struct xfs_mount *
  62. to_mp(struct kobject *kobject)
  63. {
  64. struct xfs_kobj *kobj = to_kobj(kobject);
  65. return container_of(kobj, struct xfs_mount, m_kobj);
  66. }
  67. static struct attribute *xfs_mp_attrs[] = {
  68. NULL,
  69. };
  70. struct kobj_type xfs_mp_ktype = {
  71. .release = xfs_sysfs_release,
  72. .sysfs_ops = &xfs_sysfs_ops,
  73. .default_attrs = xfs_mp_attrs,
  74. };
  75. #ifdef DEBUG
  76. /* debug */
  77. STATIC ssize_t
  78. bug_on_assert_store(
  79. struct kobject *kobject,
  80. const char *buf,
  81. size_t count)
  82. {
  83. int ret;
  84. int val;
  85. ret = kstrtoint(buf, 0, &val);
  86. if (ret)
  87. return ret;
  88. if (val == 1)
  89. xfs_globals.bug_on_assert = true;
  90. else if (val == 0)
  91. xfs_globals.bug_on_assert = false;
  92. else
  93. return -EINVAL;
  94. return count;
  95. }
  96. STATIC ssize_t
  97. bug_on_assert_show(
  98. struct kobject *kobject,
  99. char *buf)
  100. {
  101. return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.bug_on_assert ? 1 : 0);
  102. }
  103. XFS_SYSFS_ATTR_RW(bug_on_assert);
  104. STATIC ssize_t
  105. log_recovery_delay_store(
  106. struct kobject *kobject,
  107. const char *buf,
  108. size_t count)
  109. {
  110. int ret;
  111. int val;
  112. ret = kstrtoint(buf, 0, &val);
  113. if (ret)
  114. return ret;
  115. if (val < 0 || val > 60)
  116. return -EINVAL;
  117. xfs_globals.log_recovery_delay = val;
  118. return count;
  119. }
  120. STATIC ssize_t
  121. log_recovery_delay_show(
  122. struct kobject *kobject,
  123. char *buf)
  124. {
  125. return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.log_recovery_delay);
  126. }
  127. XFS_SYSFS_ATTR_RW(log_recovery_delay);
  128. STATIC ssize_t
  129. mount_delay_store(
  130. struct kobject *kobject,
  131. const char *buf,
  132. size_t count)
  133. {
  134. int ret;
  135. int val;
  136. ret = kstrtoint(buf, 0, &val);
  137. if (ret)
  138. return ret;
  139. if (val < 0 || val > 60)
  140. return -EINVAL;
  141. xfs_globals.mount_delay = val;
  142. return count;
  143. }
  144. STATIC ssize_t
  145. mount_delay_show(
  146. struct kobject *kobject,
  147. char *buf)
  148. {
  149. return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.mount_delay);
  150. }
  151. XFS_SYSFS_ATTR_RW(mount_delay);
  152. static struct attribute *xfs_dbg_attrs[] = {
  153. ATTR_LIST(bug_on_assert),
  154. ATTR_LIST(log_recovery_delay),
  155. ATTR_LIST(mount_delay),
  156. NULL,
  157. };
  158. struct kobj_type xfs_dbg_ktype = {
  159. .release = xfs_sysfs_release,
  160. .sysfs_ops = &xfs_sysfs_ops,
  161. .default_attrs = xfs_dbg_attrs,
  162. };
  163. #endif /* DEBUG */
  164. /* stats */
  165. static inline struct xstats *
  166. to_xstats(struct kobject *kobject)
  167. {
  168. struct xfs_kobj *kobj = to_kobj(kobject);
  169. return container_of(kobj, struct xstats, xs_kobj);
  170. }
  171. STATIC ssize_t
  172. stats_show(
  173. struct kobject *kobject,
  174. char *buf)
  175. {
  176. struct xstats *stats = to_xstats(kobject);
  177. return xfs_stats_format(stats->xs_stats, buf);
  178. }
  179. XFS_SYSFS_ATTR_RO(stats);
  180. STATIC ssize_t
  181. stats_clear_store(
  182. struct kobject *kobject,
  183. const char *buf,
  184. size_t count)
  185. {
  186. int ret;
  187. int val;
  188. struct xstats *stats = to_xstats(kobject);
  189. ret = kstrtoint(buf, 0, &val);
  190. if (ret)
  191. return ret;
  192. if (val != 1)
  193. return -EINVAL;
  194. xfs_stats_clearall(stats->xs_stats);
  195. return count;
  196. }
  197. XFS_SYSFS_ATTR_WO(stats_clear);
  198. static struct attribute *xfs_stats_attrs[] = {
  199. ATTR_LIST(stats),
  200. ATTR_LIST(stats_clear),
  201. NULL,
  202. };
  203. struct kobj_type xfs_stats_ktype = {
  204. .release = xfs_sysfs_release,
  205. .sysfs_ops = &xfs_sysfs_ops,
  206. .default_attrs = xfs_stats_attrs,
  207. };
  208. /* xlog */
  209. static inline struct xlog *
  210. to_xlog(struct kobject *kobject)
  211. {
  212. struct xfs_kobj *kobj = to_kobj(kobject);
  213. return container_of(kobj, struct xlog, l_kobj);
  214. }
  215. STATIC ssize_t
  216. log_head_lsn_show(
  217. struct kobject *kobject,
  218. char *buf)
  219. {
  220. int cycle;
  221. int block;
  222. struct xlog *log = to_xlog(kobject);
  223. spin_lock(&log->l_icloglock);
  224. cycle = log->l_curr_cycle;
  225. block = log->l_curr_block;
  226. spin_unlock(&log->l_icloglock);
  227. return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
  228. }
  229. XFS_SYSFS_ATTR_RO(log_head_lsn);
  230. STATIC ssize_t
  231. log_tail_lsn_show(
  232. struct kobject *kobject,
  233. char *buf)
  234. {
  235. int cycle;
  236. int block;
  237. struct xlog *log = to_xlog(kobject);
  238. xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block);
  239. return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
  240. }
  241. XFS_SYSFS_ATTR_RO(log_tail_lsn);
  242. STATIC ssize_t
  243. reserve_grant_head_show(
  244. struct kobject *kobject,
  245. char *buf)
  246. {
  247. int cycle;
  248. int bytes;
  249. struct xlog *log = to_xlog(kobject);
  250. xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes);
  251. return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
  252. }
  253. XFS_SYSFS_ATTR_RO(reserve_grant_head);
  254. STATIC ssize_t
  255. write_grant_head_show(
  256. struct kobject *kobject,
  257. char *buf)
  258. {
  259. int cycle;
  260. int bytes;
  261. struct xlog *log = to_xlog(kobject);
  262. xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes);
  263. return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
  264. }
  265. XFS_SYSFS_ATTR_RO(write_grant_head);
  266. static struct attribute *xfs_log_attrs[] = {
  267. ATTR_LIST(log_head_lsn),
  268. ATTR_LIST(log_tail_lsn),
  269. ATTR_LIST(reserve_grant_head),
  270. ATTR_LIST(write_grant_head),
  271. NULL,
  272. };
  273. struct kobj_type xfs_log_ktype = {
  274. .release = xfs_sysfs_release,
  275. .sysfs_ops = &xfs_sysfs_ops,
  276. .default_attrs = xfs_log_attrs,
  277. };
  278. /*
  279. * Metadata IO error configuration
  280. *
  281. * The sysfs structure here is:
  282. * ...xfs/<dev>/error/<class>/<errno>/<error_attrs>
  283. *
  284. * where <class> allows us to discriminate between data IO and metadata IO,
  285. * and any other future type of IO (e.g. special inode or directory error
  286. * handling) we care to support.
  287. */
  288. static inline struct xfs_error_cfg *
  289. to_error_cfg(struct kobject *kobject)
  290. {
  291. struct xfs_kobj *kobj = to_kobj(kobject);
  292. return container_of(kobj, struct xfs_error_cfg, kobj);
  293. }
  294. static inline struct xfs_mount *
  295. err_to_mp(struct kobject *kobject)
  296. {
  297. struct xfs_kobj *kobj = to_kobj(kobject);
  298. return container_of(kobj, struct xfs_mount, m_error_kobj);
  299. }
  300. static ssize_t
  301. max_retries_show(
  302. struct kobject *kobject,
  303. char *buf)
  304. {
  305. int retries;
  306. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  307. if (cfg->max_retries == XFS_ERR_RETRY_FOREVER)
  308. retries = -1;
  309. else
  310. retries = cfg->max_retries;
  311. return snprintf(buf, PAGE_SIZE, "%d\n", retries);
  312. }
  313. static ssize_t
  314. max_retries_store(
  315. struct kobject *kobject,
  316. const char *buf,
  317. size_t count)
  318. {
  319. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  320. int ret;
  321. int val;
  322. ret = kstrtoint(buf, 0, &val);
  323. if (ret)
  324. return ret;
  325. if (val < -1)
  326. return -EINVAL;
  327. if (val == -1)
  328. cfg->max_retries = XFS_ERR_RETRY_FOREVER;
  329. else
  330. cfg->max_retries = val;
  331. return count;
  332. }
  333. XFS_SYSFS_ATTR_RW(max_retries);
  334. static ssize_t
  335. retry_timeout_seconds_show(
  336. struct kobject *kobject,
  337. char *buf)
  338. {
  339. int timeout;
  340. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  341. if (cfg->retry_timeout == XFS_ERR_RETRY_FOREVER)
  342. timeout = -1;
  343. else
  344. timeout = jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC;
  345. return snprintf(buf, PAGE_SIZE, "%d\n", timeout);
  346. }
  347. static ssize_t
  348. retry_timeout_seconds_store(
  349. struct kobject *kobject,
  350. const char *buf,
  351. size_t count)
  352. {
  353. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  354. int ret;
  355. int val;
  356. ret = kstrtoint(buf, 0, &val);
  357. if (ret)
  358. return ret;
  359. /* 1 day timeout maximum, -1 means infinite */
  360. if (val < -1 || val > 86400)
  361. return -EINVAL;
  362. if (val == -1)
  363. cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
  364. else {
  365. cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
  366. ASSERT(msecs_to_jiffies(val * MSEC_PER_SEC) < LONG_MAX);
  367. }
  368. return count;
  369. }
  370. XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
  371. static ssize_t
  372. fail_at_unmount_show(
  373. struct kobject *kobject,
  374. char *buf)
  375. {
  376. struct xfs_mount *mp = err_to_mp(kobject);
  377. return snprintf(buf, PAGE_SIZE, "%d\n", mp->m_fail_unmount);
  378. }
  379. static ssize_t
  380. fail_at_unmount_store(
  381. struct kobject *kobject,
  382. const char *buf,
  383. size_t count)
  384. {
  385. struct xfs_mount *mp = err_to_mp(kobject);
  386. int ret;
  387. int val;
  388. ret = kstrtoint(buf, 0, &val);
  389. if (ret)
  390. return ret;
  391. if (val < 0 || val > 1)
  392. return -EINVAL;
  393. mp->m_fail_unmount = val;
  394. return count;
  395. }
  396. XFS_SYSFS_ATTR_RW(fail_at_unmount);
  397. static struct attribute *xfs_error_attrs[] = {
  398. ATTR_LIST(max_retries),
  399. ATTR_LIST(retry_timeout_seconds),
  400. NULL,
  401. };
  402. static struct kobj_type xfs_error_cfg_ktype = {
  403. .release = xfs_sysfs_release,
  404. .sysfs_ops = &xfs_sysfs_ops,
  405. .default_attrs = xfs_error_attrs,
  406. };
  407. static struct kobj_type xfs_error_ktype = {
  408. .release = xfs_sysfs_release,
  409. .sysfs_ops = &xfs_sysfs_ops,
  410. };
  411. /*
  412. * Error initialization tables. These need to be ordered in the same
  413. * order as the enums used to index the array. All class init tables need to
  414. * define a "default" behaviour as the first entry, all other entries can be
  415. * empty.
  416. */
  417. struct xfs_error_init {
  418. char *name;
  419. int max_retries;
  420. int retry_timeout; /* in seconds */
  421. };
  422. static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
  423. { .name = "default",
  424. .max_retries = XFS_ERR_RETRY_FOREVER,
  425. .retry_timeout = XFS_ERR_RETRY_FOREVER,
  426. },
  427. { .name = "EIO",
  428. .max_retries = XFS_ERR_RETRY_FOREVER,
  429. .retry_timeout = XFS_ERR_RETRY_FOREVER,
  430. },
  431. { .name = "ENOSPC",
  432. .max_retries = XFS_ERR_RETRY_FOREVER,
  433. .retry_timeout = XFS_ERR_RETRY_FOREVER,
  434. },
  435. { .name = "ENODEV",
  436. .max_retries = 0, /* We can't recover from devices disappearing */
  437. .retry_timeout = 0,
  438. },
  439. };
  440. static int
  441. xfs_error_sysfs_init_class(
  442. struct xfs_mount *mp,
  443. int class,
  444. const char *parent_name,
  445. struct xfs_kobj *parent_kobj,
  446. const struct xfs_error_init init[])
  447. {
  448. struct xfs_error_cfg *cfg;
  449. int error;
  450. int i;
  451. ASSERT(class < XFS_ERR_CLASS_MAX);
  452. error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
  453. &mp->m_error_kobj, parent_name);
  454. if (error)
  455. return error;
  456. for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
  457. cfg = &mp->m_error_cfg[class][i];
  458. error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
  459. parent_kobj, init[i].name);
  460. if (error)
  461. goto out_error;
  462. cfg->max_retries = init[i].max_retries;
  463. if (init[i].retry_timeout == XFS_ERR_RETRY_FOREVER)
  464. cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
  465. else
  466. cfg->retry_timeout = msecs_to_jiffies(
  467. init[i].retry_timeout * MSEC_PER_SEC);
  468. }
  469. return 0;
  470. out_error:
  471. /* unwind the entries that succeeded */
  472. for (i--; i >= 0; i--) {
  473. cfg = &mp->m_error_cfg[class][i];
  474. xfs_sysfs_del(&cfg->kobj);
  475. }
  476. xfs_sysfs_del(parent_kobj);
  477. return error;
  478. }
  479. int
  480. xfs_error_sysfs_init(
  481. struct xfs_mount *mp)
  482. {
  483. int error;
  484. /* .../xfs/<dev>/error/ */
  485. error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
  486. &mp->m_kobj, "error");
  487. if (error)
  488. return error;
  489. error = sysfs_create_file(&mp->m_error_kobj.kobject,
  490. ATTR_LIST(fail_at_unmount));
  491. if (error)
  492. goto out_error;
  493. /* .../xfs/<dev>/error/metadata/ */
  494. error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
  495. "metadata", &mp->m_error_meta_kobj,
  496. xfs_error_meta_init);
  497. if (error)
  498. goto out_error;
  499. return 0;
  500. out_error:
  501. xfs_sysfs_del(&mp->m_error_kobj);
  502. return error;
  503. }
  504. void
  505. xfs_error_sysfs_del(
  506. struct xfs_mount *mp)
  507. {
  508. struct xfs_error_cfg *cfg;
  509. int i, j;
  510. for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
  511. for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
  512. cfg = &mp->m_error_cfg[i][j];
  513. xfs_sysfs_del(&cfg->kobj);
  514. }
  515. }
  516. xfs_sysfs_del(&mp->m_error_meta_kobj);
  517. xfs_sysfs_del(&mp->m_error_kobj);
  518. }
  519. struct xfs_error_cfg *
  520. xfs_error_get_cfg(
  521. struct xfs_mount *mp,
  522. int error_class,
  523. int error)
  524. {
  525. struct xfs_error_cfg *cfg;
  526. if (error < 0)
  527. error = -error;
  528. switch (error) {
  529. case EIO:
  530. cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO];
  531. break;
  532. case ENOSPC:
  533. cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC];
  534. break;
  535. case ENODEV:
  536. cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV];
  537. break;
  538. default:
  539. cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];
  540. break;
  541. }
  542. return cfg;
  543. }