xfs_sysfs.c 14 KB

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