xfs_sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  328. return snprintf(buf, PAGE_SIZE, "%d\n", cfg->max_retries);
  329. }
  330. static ssize_t
  331. max_retries_store(
  332. struct kobject *kobject,
  333. const char *buf,
  334. size_t count)
  335. {
  336. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  337. int ret;
  338. int val;
  339. ret = kstrtoint(buf, 0, &val);
  340. if (ret)
  341. return ret;
  342. if (val < -1)
  343. return -EINVAL;
  344. cfg->max_retries = val;
  345. return count;
  346. }
  347. XFS_SYSFS_ATTR_RW(max_retries);
  348. static ssize_t
  349. retry_timeout_seconds_show(
  350. struct kobject *kobject,
  351. char *buf)
  352. {
  353. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  354. return snprintf(buf, PAGE_SIZE, "%ld\n",
  355. jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC);
  356. }
  357. static ssize_t
  358. retry_timeout_seconds_store(
  359. struct kobject *kobject,
  360. const char *buf,
  361. size_t count)
  362. {
  363. struct xfs_error_cfg *cfg = to_error_cfg(kobject);
  364. int ret;
  365. int val;
  366. ret = kstrtoint(buf, 0, &val);
  367. if (ret)
  368. return ret;
  369. /* 1 day timeout maximum */
  370. if (val < 0 || val > 86400)
  371. return -EINVAL;
  372. cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
  373. return count;
  374. }
  375. XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
  376. static ssize_t
  377. fail_at_unmount_show(
  378. struct kobject *kobject,
  379. char *buf)
  380. {
  381. struct xfs_mount *mp = err_to_mp(kobject);
  382. return snprintf(buf, PAGE_SIZE, "%d\n", mp->m_fail_unmount);
  383. }
  384. static ssize_t
  385. fail_at_unmount_store(
  386. struct kobject *kobject,
  387. const char *buf,
  388. size_t count)
  389. {
  390. struct xfs_mount *mp = err_to_mp(kobject);
  391. int ret;
  392. int val;
  393. ret = kstrtoint(buf, 0, &val);
  394. if (ret)
  395. return ret;
  396. if (val < 0 || val > 1)
  397. return -EINVAL;
  398. mp->m_fail_unmount = val;
  399. return count;
  400. }
  401. XFS_SYSFS_ATTR_RW(fail_at_unmount);
  402. static struct attribute *xfs_error_attrs[] = {
  403. ATTR_LIST(max_retries),
  404. ATTR_LIST(retry_timeout_seconds),
  405. NULL,
  406. };
  407. struct kobj_type xfs_error_cfg_ktype = {
  408. .release = xfs_sysfs_release,
  409. .sysfs_ops = &xfs_sysfs_ops,
  410. .default_attrs = xfs_error_attrs,
  411. };
  412. struct kobj_type xfs_error_ktype = {
  413. .release = xfs_sysfs_release,
  414. .sysfs_ops = &xfs_sysfs_ops,
  415. };
  416. /*
  417. * Error initialization tables. These need to be ordered in the same
  418. * order as the enums used to index the array. All class init tables need to
  419. * define a "default" behaviour as the first entry, all other entries can be
  420. * empty.
  421. */
  422. struct xfs_error_init {
  423. char *name;
  424. int max_retries;
  425. int retry_timeout; /* in seconds */
  426. };
  427. static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
  428. { .name = "default",
  429. .max_retries = XFS_ERR_RETRY_FOREVER,
  430. .retry_timeout = 0,
  431. },
  432. { .name = "EIO",
  433. .max_retries = XFS_ERR_RETRY_FOREVER,
  434. .retry_timeout = 0,
  435. },
  436. { .name = "ENOSPC",
  437. .max_retries = XFS_ERR_RETRY_FOREVER,
  438. .retry_timeout = 0,
  439. },
  440. { .name = "ENODEV",
  441. .max_retries = 0,
  442. },
  443. };
  444. static int
  445. xfs_error_sysfs_init_class(
  446. struct xfs_mount *mp,
  447. int class,
  448. const char *parent_name,
  449. struct xfs_kobj *parent_kobj,
  450. const struct xfs_error_init init[])
  451. {
  452. struct xfs_error_cfg *cfg;
  453. int error;
  454. int i;
  455. ASSERT(class < XFS_ERR_CLASS_MAX);
  456. error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
  457. &mp->m_error_kobj, parent_name);
  458. if (error)
  459. return error;
  460. for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
  461. cfg = &mp->m_error_cfg[class][i];
  462. error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
  463. parent_kobj, init[i].name);
  464. if (error)
  465. goto out_error;
  466. cfg->max_retries = init[i].max_retries;
  467. cfg->retry_timeout = msecs_to_jiffies(
  468. init[i].retry_timeout * MSEC_PER_SEC);
  469. }
  470. return 0;
  471. out_error:
  472. /* unwind the entries that succeeded */
  473. for (i--; i >= 0; i--) {
  474. cfg = &mp->m_error_cfg[class][i];
  475. xfs_sysfs_del(&cfg->kobj);
  476. }
  477. xfs_sysfs_del(parent_kobj);
  478. return error;
  479. }
  480. int
  481. xfs_error_sysfs_init(
  482. struct xfs_mount *mp)
  483. {
  484. int error;
  485. /* .../xfs/<dev>/error/ */
  486. error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
  487. &mp->m_kobj, "error");
  488. if (error)
  489. return error;
  490. error = sysfs_create_file(&mp->m_error_kobj.kobject,
  491. ATTR_LIST(fail_at_unmount));
  492. if (error)
  493. goto out_error;
  494. /* .../xfs/<dev>/error/metadata/ */
  495. error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
  496. "metadata", &mp->m_error_meta_kobj,
  497. xfs_error_meta_init);
  498. if (error)
  499. goto out_error;
  500. return 0;
  501. out_error:
  502. xfs_sysfs_del(&mp->m_error_kobj);
  503. return error;
  504. }
  505. void
  506. xfs_error_sysfs_del(
  507. struct xfs_mount *mp)
  508. {
  509. struct xfs_error_cfg *cfg;
  510. int i, j;
  511. for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
  512. for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
  513. cfg = &mp->m_error_cfg[i][j];
  514. xfs_sysfs_del(&cfg->kobj);
  515. }
  516. }
  517. xfs_sysfs_del(&mp->m_error_meta_kobj);
  518. xfs_sysfs_del(&mp->m_error_kobj);
  519. }
  520. struct xfs_error_cfg *
  521. xfs_error_get_cfg(
  522. struct xfs_mount *mp,
  523. int error_class,
  524. int error)
  525. {
  526. struct xfs_error_cfg *cfg;
  527. switch (error) {
  528. case EIO:
  529. cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO];
  530. break;
  531. case ENOSPC:
  532. cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC];
  533. break;
  534. case ENODEV:
  535. cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV];
  536. break;
  537. default:
  538. cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];
  539. break;
  540. }
  541. return cfg;
  542. }