xfs_sysfs.c 14 KB

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