xfs_sysfs.c 13 KB

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