sysfs.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Sysfs support implementation.
  4. *
  5. * Copyright (C) 2005-2014 Nippon Telegraph and Telephone Corporation.
  6. * Copyright (C) 2014 HGST, Inc., a Western Digital Company.
  7. *
  8. * Written by Vyacheslav Dubeyko <Vyacheslav.Dubeyko@hgst.com>
  9. */
  10. #include <linux/kobject.h>
  11. #include "nilfs.h"
  12. #include "mdt.h"
  13. #include "sufile.h"
  14. #include "cpfile.h"
  15. #include "sysfs.h"
  16. /* /sys/fs/<nilfs>/ */
  17. static struct kset *nilfs_kset;
  18. #define NILFS_DEV_INT_GROUP_OPS(name, parent_name) \
  19. static ssize_t nilfs_##name##_attr_show(struct kobject *kobj, \
  20. struct attribute *attr, char *buf) \
  21. { \
  22. struct the_nilfs *nilfs = container_of(kobj->parent, \
  23. struct the_nilfs, \
  24. ns_##parent_name##_kobj); \
  25. struct nilfs_##name##_attr *a = container_of(attr, \
  26. struct nilfs_##name##_attr, \
  27. attr); \
  28. return a->show ? a->show(a, nilfs, buf) : 0; \
  29. } \
  30. static ssize_t nilfs_##name##_attr_store(struct kobject *kobj, \
  31. struct attribute *attr, \
  32. const char *buf, size_t len) \
  33. { \
  34. struct the_nilfs *nilfs = container_of(kobj->parent, \
  35. struct the_nilfs, \
  36. ns_##parent_name##_kobj); \
  37. struct nilfs_##name##_attr *a = container_of(attr, \
  38. struct nilfs_##name##_attr, \
  39. attr); \
  40. return a->store ? a->store(a, nilfs, buf, len) : 0; \
  41. } \
  42. static const struct sysfs_ops nilfs_##name##_attr_ops = { \
  43. .show = nilfs_##name##_attr_show, \
  44. .store = nilfs_##name##_attr_store, \
  45. }
  46. #define NILFS_DEV_INT_GROUP_TYPE(name, parent_name) \
  47. static void nilfs_##name##_attr_release(struct kobject *kobj) \
  48. { \
  49. struct nilfs_sysfs_##parent_name##_subgroups *subgroups = container_of(kobj, \
  50. struct nilfs_sysfs_##parent_name##_subgroups, \
  51. sg_##name##_kobj); \
  52. complete(&subgroups->sg_##name##_kobj_unregister); \
  53. } \
  54. static const struct kobj_type nilfs_##name##_ktype = { \
  55. .default_groups = nilfs_##name##_groups, \
  56. .sysfs_ops = &nilfs_##name##_attr_ops, \
  57. .release = nilfs_##name##_attr_release, \
  58. }
  59. #define NILFS_DEV_INT_GROUP_FNS(name, parent_name) \
  60. static int nilfs_sysfs_create_##name##_group(struct the_nilfs *nilfs) \
  61. { \
  62. struct kobject *parent; \
  63. struct kobject *kobj; \
  64. struct completion *kobj_unregister; \
  65. struct nilfs_sysfs_##parent_name##_subgroups *subgroups; \
  66. int err; \
  67. subgroups = nilfs->ns_##parent_name##_subgroups; \
  68. kobj = &subgroups->sg_##name##_kobj; \
  69. kobj_unregister = &subgroups->sg_##name##_kobj_unregister; \
  70. parent = &nilfs->ns_##parent_name##_kobj; \
  71. kobj->kset = nilfs_kset; \
  72. init_completion(kobj_unregister); \
  73. err = kobject_init_and_add(kobj, &nilfs_##name##_ktype, parent, \
  74. #name); \
  75. if (err) \
  76. kobject_put(kobj); \
  77. return err; \
  78. } \
  79. static void nilfs_sysfs_delete_##name##_group(struct the_nilfs *nilfs) \
  80. { \
  81. kobject_put(&nilfs->ns_##parent_name##_subgroups->sg_##name##_kobj); \
  82. }
  83. /************************************************************************
  84. * NILFS snapshot attrs *
  85. ************************************************************************/
  86. static ssize_t
  87. nilfs_snapshot_inodes_count_show(struct nilfs_snapshot_attr *attr,
  88. struct nilfs_root *root, char *buf)
  89. {
  90. return sysfs_emit(buf, "%llu\n",
  91. (unsigned long long)atomic64_read(&root->inodes_count));
  92. }
  93. static ssize_t
  94. nilfs_snapshot_blocks_count_show(struct nilfs_snapshot_attr *attr,
  95. struct nilfs_root *root, char *buf)
  96. {
  97. return sysfs_emit(buf, "%llu\n",
  98. (unsigned long long)atomic64_read(&root->blocks_count));
  99. }
  100. static const char snapshot_readme_str[] =
  101. "The group contains details about mounted snapshot.\n\n"
  102. "(1) inodes_count\n\tshow number of inodes for snapshot.\n\n"
  103. "(2) blocks_count\n\tshow number of blocks for snapshot.\n\n";
  104. static ssize_t
  105. nilfs_snapshot_README_show(struct nilfs_snapshot_attr *attr,
  106. struct nilfs_root *root, char *buf)
  107. {
  108. return sysfs_emit(buf, snapshot_readme_str);
  109. }
  110. NILFS_SNAPSHOT_RO_ATTR(inodes_count);
  111. NILFS_SNAPSHOT_RO_ATTR(blocks_count);
  112. NILFS_SNAPSHOT_RO_ATTR(README);
  113. static struct attribute *nilfs_snapshot_attrs[] = {
  114. NILFS_SNAPSHOT_ATTR_LIST(inodes_count),
  115. NILFS_SNAPSHOT_ATTR_LIST(blocks_count),
  116. NILFS_SNAPSHOT_ATTR_LIST(README),
  117. NULL,
  118. };
  119. ATTRIBUTE_GROUPS(nilfs_snapshot);
  120. static ssize_t nilfs_snapshot_attr_show(struct kobject *kobj,
  121. struct attribute *attr, char *buf)
  122. {
  123. struct nilfs_root *root =
  124. container_of(kobj, struct nilfs_root, snapshot_kobj);
  125. struct nilfs_snapshot_attr *a =
  126. container_of(attr, struct nilfs_snapshot_attr, attr);
  127. return a->show ? a->show(a, root, buf) : 0;
  128. }
  129. static ssize_t nilfs_snapshot_attr_store(struct kobject *kobj,
  130. struct attribute *attr,
  131. const char *buf, size_t len)
  132. {
  133. struct nilfs_root *root =
  134. container_of(kobj, struct nilfs_root, snapshot_kobj);
  135. struct nilfs_snapshot_attr *a =
  136. container_of(attr, struct nilfs_snapshot_attr, attr);
  137. return a->store ? a->store(a, root, buf, len) : 0;
  138. }
  139. static void nilfs_snapshot_attr_release(struct kobject *kobj)
  140. {
  141. struct nilfs_root *root = container_of(kobj, struct nilfs_root,
  142. snapshot_kobj);
  143. complete(&root->snapshot_kobj_unregister);
  144. }
  145. static const struct sysfs_ops nilfs_snapshot_attr_ops = {
  146. .show = nilfs_snapshot_attr_show,
  147. .store = nilfs_snapshot_attr_store,
  148. };
  149. static const struct kobj_type nilfs_snapshot_ktype = {
  150. .default_groups = nilfs_snapshot_groups,
  151. .sysfs_ops = &nilfs_snapshot_attr_ops,
  152. .release = nilfs_snapshot_attr_release,
  153. };
  154. int nilfs_sysfs_create_snapshot_group(struct nilfs_root *root)
  155. {
  156. struct the_nilfs *nilfs;
  157. struct kobject *parent;
  158. int err;
  159. nilfs = root->nilfs;
  160. parent = &nilfs->ns_dev_subgroups->sg_mounted_snapshots_kobj;
  161. root->snapshot_kobj.kset = nilfs_kset;
  162. init_completion(&root->snapshot_kobj_unregister);
  163. if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
  164. err = kobject_init_and_add(&root->snapshot_kobj,
  165. &nilfs_snapshot_ktype,
  166. &nilfs->ns_dev_kobj,
  167. "current_checkpoint");
  168. } else {
  169. err = kobject_init_and_add(&root->snapshot_kobj,
  170. &nilfs_snapshot_ktype,
  171. parent,
  172. "%llu", root->cno);
  173. }
  174. if (err)
  175. kobject_put(&root->snapshot_kobj);
  176. return err;
  177. }
  178. void nilfs_sysfs_delete_snapshot_group(struct nilfs_root *root)
  179. {
  180. kobject_put(&root->snapshot_kobj);
  181. }
  182. /************************************************************************
  183. * NILFS mounted snapshots attrs *
  184. ************************************************************************/
  185. static const char mounted_snapshots_readme_str[] =
  186. "The mounted_snapshots group contains group for\n"
  187. "every mounted snapshot.\n";
  188. static ssize_t
  189. nilfs_mounted_snapshots_README_show(struct nilfs_mounted_snapshots_attr *attr,
  190. struct the_nilfs *nilfs, char *buf)
  191. {
  192. return sysfs_emit(buf, mounted_snapshots_readme_str);
  193. }
  194. NILFS_MOUNTED_SNAPSHOTS_RO_ATTR(README);
  195. static struct attribute *nilfs_mounted_snapshots_attrs[] = {
  196. NILFS_MOUNTED_SNAPSHOTS_ATTR_LIST(README),
  197. NULL,
  198. };
  199. ATTRIBUTE_GROUPS(nilfs_mounted_snapshots);
  200. NILFS_DEV_INT_GROUP_OPS(mounted_snapshots, dev);
  201. NILFS_DEV_INT_GROUP_TYPE(mounted_snapshots, dev);
  202. NILFS_DEV_INT_GROUP_FNS(mounted_snapshots, dev);
  203. /************************************************************************
  204. * NILFS checkpoints attrs *
  205. ************************************************************************/
  206. static ssize_t
  207. nilfs_checkpoints_checkpoints_number_show(struct nilfs_checkpoints_attr *attr,
  208. struct the_nilfs *nilfs,
  209. char *buf)
  210. {
  211. __u64 ncheckpoints;
  212. struct nilfs_cpstat cpstat;
  213. int err;
  214. down_read(&nilfs->ns_segctor_sem);
  215. err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
  216. up_read(&nilfs->ns_segctor_sem);
  217. if (err < 0) {
  218. nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
  219. err);
  220. return err;
  221. }
  222. ncheckpoints = cpstat.cs_ncps;
  223. return sysfs_emit(buf, "%llu\n", ncheckpoints);
  224. }
  225. static ssize_t
  226. nilfs_checkpoints_snapshots_number_show(struct nilfs_checkpoints_attr *attr,
  227. struct the_nilfs *nilfs,
  228. char *buf)
  229. {
  230. __u64 nsnapshots;
  231. struct nilfs_cpstat cpstat;
  232. int err;
  233. down_read(&nilfs->ns_segctor_sem);
  234. err = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
  235. up_read(&nilfs->ns_segctor_sem);
  236. if (err < 0) {
  237. nilfs_err(nilfs->ns_sb, "unable to get checkpoint stat: err=%d",
  238. err);
  239. return err;
  240. }
  241. nsnapshots = cpstat.cs_nsss;
  242. return sysfs_emit(buf, "%llu\n", nsnapshots);
  243. }
  244. static ssize_t
  245. nilfs_checkpoints_last_seg_checkpoint_show(struct nilfs_checkpoints_attr *attr,
  246. struct the_nilfs *nilfs,
  247. char *buf)
  248. {
  249. __u64 last_cno;
  250. spin_lock(&nilfs->ns_last_segment_lock);
  251. last_cno = nilfs->ns_last_cno;
  252. spin_unlock(&nilfs->ns_last_segment_lock);
  253. return sysfs_emit(buf, "%llu\n", last_cno);
  254. }
  255. static ssize_t
  256. nilfs_checkpoints_next_checkpoint_show(struct nilfs_checkpoints_attr *attr,
  257. struct the_nilfs *nilfs,
  258. char *buf)
  259. {
  260. __u64 cno;
  261. down_read(&nilfs->ns_segctor_sem);
  262. cno = nilfs->ns_cno;
  263. up_read(&nilfs->ns_segctor_sem);
  264. return sysfs_emit(buf, "%llu\n", cno);
  265. }
  266. static const char checkpoints_readme_str[] =
  267. "The checkpoints group contains attributes that describe\n"
  268. "details about volume's checkpoints.\n\n"
  269. "(1) checkpoints_number\n\tshow number of checkpoints on volume.\n\n"
  270. "(2) snapshots_number\n\tshow number of snapshots on volume.\n\n"
  271. "(3) last_seg_checkpoint\n"
  272. "\tshow checkpoint number of the latest segment.\n\n"
  273. "(4) next_checkpoint\n\tshow next checkpoint number.\n\n";
  274. static ssize_t
  275. nilfs_checkpoints_README_show(struct nilfs_checkpoints_attr *attr,
  276. struct the_nilfs *nilfs, char *buf)
  277. {
  278. return sysfs_emit(buf, checkpoints_readme_str);
  279. }
  280. NILFS_CHECKPOINTS_RO_ATTR(checkpoints_number);
  281. NILFS_CHECKPOINTS_RO_ATTR(snapshots_number);
  282. NILFS_CHECKPOINTS_RO_ATTR(last_seg_checkpoint);
  283. NILFS_CHECKPOINTS_RO_ATTR(next_checkpoint);
  284. NILFS_CHECKPOINTS_RO_ATTR(README);
  285. static struct attribute *nilfs_checkpoints_attrs[] = {
  286. NILFS_CHECKPOINTS_ATTR_LIST(checkpoints_number),
  287. NILFS_CHECKPOINTS_ATTR_LIST(snapshots_number),
  288. NILFS_CHECKPOINTS_ATTR_LIST(last_seg_checkpoint),
  289. NILFS_CHECKPOINTS_ATTR_LIST(next_checkpoint),
  290. NILFS_CHECKPOINTS_ATTR_LIST(README),
  291. NULL,
  292. };
  293. ATTRIBUTE_GROUPS(nilfs_checkpoints);
  294. NILFS_DEV_INT_GROUP_OPS(checkpoints, dev);
  295. NILFS_DEV_INT_GROUP_TYPE(checkpoints, dev);
  296. NILFS_DEV_INT_GROUP_FNS(checkpoints, dev);
  297. /************************************************************************
  298. * NILFS segments attrs *
  299. ************************************************************************/
  300. static ssize_t
  301. nilfs_segments_segments_number_show(struct nilfs_segments_attr *attr,
  302. struct the_nilfs *nilfs,
  303. char *buf)
  304. {
  305. return sysfs_emit(buf, "%lu\n", nilfs->ns_nsegments);
  306. }
  307. static ssize_t
  308. nilfs_segments_blocks_per_segment_show(struct nilfs_segments_attr *attr,
  309. struct the_nilfs *nilfs,
  310. char *buf)
  311. {
  312. return sysfs_emit(buf, "%lu\n", nilfs->ns_blocks_per_segment);
  313. }
  314. static ssize_t
  315. nilfs_segments_clean_segments_show(struct nilfs_segments_attr *attr,
  316. struct the_nilfs *nilfs,
  317. char *buf)
  318. {
  319. unsigned long ncleansegs;
  320. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  321. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  322. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  323. return sysfs_emit(buf, "%lu\n", ncleansegs);
  324. }
  325. static ssize_t
  326. nilfs_segments_dirty_segments_show(struct nilfs_segments_attr *attr,
  327. struct the_nilfs *nilfs,
  328. char *buf)
  329. {
  330. struct nilfs_sustat sustat;
  331. int err;
  332. down_read(&nilfs->ns_segctor_sem);
  333. err = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
  334. up_read(&nilfs->ns_segctor_sem);
  335. if (err < 0) {
  336. nilfs_err(nilfs->ns_sb, "unable to get segment stat: err=%d",
  337. err);
  338. return err;
  339. }
  340. return sysfs_emit(buf, "%llu\n", sustat.ss_ndirtysegs);
  341. }
  342. static const char segments_readme_str[] =
  343. "The segments group contains attributes that describe\n"
  344. "details about volume's segments.\n\n"
  345. "(1) segments_number\n\tshow number of segments on volume.\n\n"
  346. "(2) blocks_per_segment\n\tshow number of blocks in segment.\n\n"
  347. "(3) clean_segments\n\tshow count of clean segments.\n\n"
  348. "(4) dirty_segments\n\tshow count of dirty segments.\n\n";
  349. static ssize_t
  350. nilfs_segments_README_show(struct nilfs_segments_attr *attr,
  351. struct the_nilfs *nilfs,
  352. char *buf)
  353. {
  354. return sysfs_emit(buf, segments_readme_str);
  355. }
  356. NILFS_SEGMENTS_RO_ATTR(segments_number);
  357. NILFS_SEGMENTS_RO_ATTR(blocks_per_segment);
  358. NILFS_SEGMENTS_RO_ATTR(clean_segments);
  359. NILFS_SEGMENTS_RO_ATTR(dirty_segments);
  360. NILFS_SEGMENTS_RO_ATTR(README);
  361. static struct attribute *nilfs_segments_attrs[] = {
  362. NILFS_SEGMENTS_ATTR_LIST(segments_number),
  363. NILFS_SEGMENTS_ATTR_LIST(blocks_per_segment),
  364. NILFS_SEGMENTS_ATTR_LIST(clean_segments),
  365. NILFS_SEGMENTS_ATTR_LIST(dirty_segments),
  366. NILFS_SEGMENTS_ATTR_LIST(README),
  367. NULL,
  368. };
  369. ATTRIBUTE_GROUPS(nilfs_segments);
  370. NILFS_DEV_INT_GROUP_OPS(segments, dev);
  371. NILFS_DEV_INT_GROUP_TYPE(segments, dev);
  372. NILFS_DEV_INT_GROUP_FNS(segments, dev);
  373. /************************************************************************
  374. * NILFS segctor attrs *
  375. ************************************************************************/
  376. static ssize_t
  377. nilfs_segctor_last_pseg_block_show(struct nilfs_segctor_attr *attr,
  378. struct the_nilfs *nilfs,
  379. char *buf)
  380. {
  381. sector_t last_pseg;
  382. spin_lock(&nilfs->ns_last_segment_lock);
  383. last_pseg = nilfs->ns_last_pseg;
  384. spin_unlock(&nilfs->ns_last_segment_lock);
  385. return sysfs_emit(buf, "%llu\n",
  386. (unsigned long long)last_pseg);
  387. }
  388. static ssize_t
  389. nilfs_segctor_last_seg_sequence_show(struct nilfs_segctor_attr *attr,
  390. struct the_nilfs *nilfs,
  391. char *buf)
  392. {
  393. u64 last_seq;
  394. spin_lock(&nilfs->ns_last_segment_lock);
  395. last_seq = nilfs->ns_last_seq;
  396. spin_unlock(&nilfs->ns_last_segment_lock);
  397. return sysfs_emit(buf, "%llu\n", last_seq);
  398. }
  399. static ssize_t
  400. nilfs_segctor_last_seg_checkpoint_show(struct nilfs_segctor_attr *attr,
  401. struct the_nilfs *nilfs,
  402. char *buf)
  403. {
  404. __u64 last_cno;
  405. spin_lock(&nilfs->ns_last_segment_lock);
  406. last_cno = nilfs->ns_last_cno;
  407. spin_unlock(&nilfs->ns_last_segment_lock);
  408. return sysfs_emit(buf, "%llu\n", last_cno);
  409. }
  410. static ssize_t
  411. nilfs_segctor_current_seg_sequence_show(struct nilfs_segctor_attr *attr,
  412. struct the_nilfs *nilfs,
  413. char *buf)
  414. {
  415. u64 seg_seq;
  416. down_read(&nilfs->ns_segctor_sem);
  417. seg_seq = nilfs->ns_seg_seq;
  418. up_read(&nilfs->ns_segctor_sem);
  419. return sysfs_emit(buf, "%llu\n", seg_seq);
  420. }
  421. static ssize_t
  422. nilfs_segctor_current_last_full_seg_show(struct nilfs_segctor_attr *attr,
  423. struct the_nilfs *nilfs,
  424. char *buf)
  425. {
  426. __u64 segnum;
  427. down_read(&nilfs->ns_segctor_sem);
  428. segnum = nilfs->ns_segnum;
  429. up_read(&nilfs->ns_segctor_sem);
  430. return sysfs_emit(buf, "%llu\n", segnum);
  431. }
  432. static ssize_t
  433. nilfs_segctor_next_full_seg_show(struct nilfs_segctor_attr *attr,
  434. struct the_nilfs *nilfs,
  435. char *buf)
  436. {
  437. __u64 nextnum;
  438. down_read(&nilfs->ns_segctor_sem);
  439. nextnum = nilfs->ns_nextnum;
  440. up_read(&nilfs->ns_segctor_sem);
  441. return sysfs_emit(buf, "%llu\n", nextnum);
  442. }
  443. static ssize_t
  444. nilfs_segctor_next_pseg_offset_show(struct nilfs_segctor_attr *attr,
  445. struct the_nilfs *nilfs,
  446. char *buf)
  447. {
  448. unsigned long pseg_offset;
  449. down_read(&nilfs->ns_segctor_sem);
  450. pseg_offset = nilfs->ns_pseg_offset;
  451. up_read(&nilfs->ns_segctor_sem);
  452. return sysfs_emit(buf, "%lu\n", pseg_offset);
  453. }
  454. static ssize_t
  455. nilfs_segctor_next_checkpoint_show(struct nilfs_segctor_attr *attr,
  456. struct the_nilfs *nilfs,
  457. char *buf)
  458. {
  459. __u64 cno;
  460. down_read(&nilfs->ns_segctor_sem);
  461. cno = nilfs->ns_cno;
  462. up_read(&nilfs->ns_segctor_sem);
  463. return sysfs_emit(buf, "%llu\n", cno);
  464. }
  465. static ssize_t
  466. nilfs_segctor_last_seg_write_time_show(struct nilfs_segctor_attr *attr,
  467. struct the_nilfs *nilfs,
  468. char *buf)
  469. {
  470. time64_t ctime;
  471. down_read(&nilfs->ns_segctor_sem);
  472. ctime = nilfs->ns_ctime;
  473. up_read(&nilfs->ns_segctor_sem);
  474. return sysfs_emit(buf, "%ptTs\n", &ctime);
  475. }
  476. static ssize_t
  477. nilfs_segctor_last_seg_write_time_secs_show(struct nilfs_segctor_attr *attr,
  478. struct the_nilfs *nilfs,
  479. char *buf)
  480. {
  481. time64_t ctime;
  482. down_read(&nilfs->ns_segctor_sem);
  483. ctime = nilfs->ns_ctime;
  484. up_read(&nilfs->ns_segctor_sem);
  485. return sysfs_emit(buf, "%llu\n", ctime);
  486. }
  487. static ssize_t
  488. nilfs_segctor_last_nongc_write_time_show(struct nilfs_segctor_attr *attr,
  489. struct the_nilfs *nilfs,
  490. char *buf)
  491. {
  492. time64_t nongc_ctime;
  493. down_read(&nilfs->ns_segctor_sem);
  494. nongc_ctime = nilfs->ns_nongc_ctime;
  495. up_read(&nilfs->ns_segctor_sem);
  496. return sysfs_emit(buf, "%ptTs\n", &nongc_ctime);
  497. }
  498. static ssize_t
  499. nilfs_segctor_last_nongc_write_time_secs_show(struct nilfs_segctor_attr *attr,
  500. struct the_nilfs *nilfs,
  501. char *buf)
  502. {
  503. time64_t nongc_ctime;
  504. down_read(&nilfs->ns_segctor_sem);
  505. nongc_ctime = nilfs->ns_nongc_ctime;
  506. up_read(&nilfs->ns_segctor_sem);
  507. return sysfs_emit(buf, "%llu\n", nongc_ctime);
  508. }
  509. static ssize_t
  510. nilfs_segctor_dirty_data_blocks_count_show(struct nilfs_segctor_attr *attr,
  511. struct the_nilfs *nilfs,
  512. char *buf)
  513. {
  514. u32 ndirtyblks;
  515. down_read(&nilfs->ns_segctor_sem);
  516. ndirtyblks = atomic_read(&nilfs->ns_ndirtyblks);
  517. up_read(&nilfs->ns_segctor_sem);
  518. return sysfs_emit(buf, "%u\n", ndirtyblks);
  519. }
  520. static const char segctor_readme_str[] =
  521. "The segctor group contains attributes that describe\n"
  522. "segctor thread activity details.\n\n"
  523. "(1) last_pseg_block\n"
  524. "\tshow start block number of the latest segment.\n\n"
  525. "(2) last_seg_sequence\n"
  526. "\tshow sequence value of the latest segment.\n\n"
  527. "(3) last_seg_checkpoint\n"
  528. "\tshow checkpoint number of the latest segment.\n\n"
  529. "(4) current_seg_sequence\n\tshow segment sequence counter.\n\n"
  530. "(5) current_last_full_seg\n"
  531. "\tshow index number of the latest full segment.\n\n"
  532. "(6) next_full_seg\n"
  533. "\tshow index number of the full segment index to be used next.\n\n"
  534. "(7) next_pseg_offset\n"
  535. "\tshow offset of next partial segment in the current full segment.\n\n"
  536. "(8) next_checkpoint\n\tshow next checkpoint number.\n\n"
  537. "(9) last_seg_write_time\n"
  538. "\tshow write time of the last segment in human-readable format.\n\n"
  539. "(10) last_seg_write_time_secs\n"
  540. "\tshow write time of the last segment in seconds.\n\n"
  541. "(11) last_nongc_write_time\n"
  542. "\tshow write time of the last segment not for cleaner operation "
  543. "in human-readable format.\n\n"
  544. "(12) last_nongc_write_time_secs\n"
  545. "\tshow write time of the last segment not for cleaner operation "
  546. "in seconds.\n\n"
  547. "(13) dirty_data_blocks_count\n"
  548. "\tshow number of dirty data blocks.\n\n";
  549. static ssize_t
  550. nilfs_segctor_README_show(struct nilfs_segctor_attr *attr,
  551. struct the_nilfs *nilfs, char *buf)
  552. {
  553. return sysfs_emit(buf, segctor_readme_str);
  554. }
  555. NILFS_SEGCTOR_RO_ATTR(last_pseg_block);
  556. NILFS_SEGCTOR_RO_ATTR(last_seg_sequence);
  557. NILFS_SEGCTOR_RO_ATTR(last_seg_checkpoint);
  558. NILFS_SEGCTOR_RO_ATTR(current_seg_sequence);
  559. NILFS_SEGCTOR_RO_ATTR(current_last_full_seg);
  560. NILFS_SEGCTOR_RO_ATTR(next_full_seg);
  561. NILFS_SEGCTOR_RO_ATTR(next_pseg_offset);
  562. NILFS_SEGCTOR_RO_ATTR(next_checkpoint);
  563. NILFS_SEGCTOR_RO_ATTR(last_seg_write_time);
  564. NILFS_SEGCTOR_RO_ATTR(last_seg_write_time_secs);
  565. NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time);
  566. NILFS_SEGCTOR_RO_ATTR(last_nongc_write_time_secs);
  567. NILFS_SEGCTOR_RO_ATTR(dirty_data_blocks_count);
  568. NILFS_SEGCTOR_RO_ATTR(README);
  569. static struct attribute *nilfs_segctor_attrs[] = {
  570. NILFS_SEGCTOR_ATTR_LIST(last_pseg_block),
  571. NILFS_SEGCTOR_ATTR_LIST(last_seg_sequence),
  572. NILFS_SEGCTOR_ATTR_LIST(last_seg_checkpoint),
  573. NILFS_SEGCTOR_ATTR_LIST(current_seg_sequence),
  574. NILFS_SEGCTOR_ATTR_LIST(current_last_full_seg),
  575. NILFS_SEGCTOR_ATTR_LIST(next_full_seg),
  576. NILFS_SEGCTOR_ATTR_LIST(next_pseg_offset),
  577. NILFS_SEGCTOR_ATTR_LIST(next_checkpoint),
  578. NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time),
  579. NILFS_SEGCTOR_ATTR_LIST(last_seg_write_time_secs),
  580. NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time),
  581. NILFS_SEGCTOR_ATTR_LIST(last_nongc_write_time_secs),
  582. NILFS_SEGCTOR_ATTR_LIST(dirty_data_blocks_count),
  583. NILFS_SEGCTOR_ATTR_LIST(README),
  584. NULL,
  585. };
  586. ATTRIBUTE_GROUPS(nilfs_segctor);
  587. NILFS_DEV_INT_GROUP_OPS(segctor, dev);
  588. NILFS_DEV_INT_GROUP_TYPE(segctor, dev);
  589. NILFS_DEV_INT_GROUP_FNS(segctor, dev);
  590. /************************************************************************
  591. * NILFS superblock attrs *
  592. ************************************************************************/
  593. static ssize_t
  594. nilfs_superblock_sb_write_time_show(struct nilfs_superblock_attr *attr,
  595. struct the_nilfs *nilfs,
  596. char *buf)
  597. {
  598. time64_t sbwtime;
  599. down_read(&nilfs->ns_sem);
  600. sbwtime = nilfs->ns_sbwtime;
  601. up_read(&nilfs->ns_sem);
  602. return sysfs_emit(buf, "%ptTs\n", &sbwtime);
  603. }
  604. static ssize_t
  605. nilfs_superblock_sb_write_time_secs_show(struct nilfs_superblock_attr *attr,
  606. struct the_nilfs *nilfs,
  607. char *buf)
  608. {
  609. time64_t sbwtime;
  610. down_read(&nilfs->ns_sem);
  611. sbwtime = nilfs->ns_sbwtime;
  612. up_read(&nilfs->ns_sem);
  613. return sysfs_emit(buf, "%llu\n", sbwtime);
  614. }
  615. static ssize_t
  616. nilfs_superblock_sb_write_count_show(struct nilfs_superblock_attr *attr,
  617. struct the_nilfs *nilfs,
  618. char *buf)
  619. {
  620. unsigned int sbwcount;
  621. down_read(&nilfs->ns_sem);
  622. sbwcount = nilfs->ns_sbwcount;
  623. up_read(&nilfs->ns_sem);
  624. return sysfs_emit(buf, "%u\n", sbwcount);
  625. }
  626. static ssize_t
  627. nilfs_superblock_sb_update_frequency_show(struct nilfs_superblock_attr *attr,
  628. struct the_nilfs *nilfs,
  629. char *buf)
  630. {
  631. unsigned int sb_update_freq;
  632. down_read(&nilfs->ns_sem);
  633. sb_update_freq = nilfs->ns_sb_update_freq;
  634. up_read(&nilfs->ns_sem);
  635. return sysfs_emit(buf, "%u\n", sb_update_freq);
  636. }
  637. static ssize_t
  638. nilfs_superblock_sb_update_frequency_store(struct nilfs_superblock_attr *attr,
  639. struct the_nilfs *nilfs,
  640. const char *buf, size_t count)
  641. {
  642. unsigned int val;
  643. int err;
  644. err = kstrtouint(skip_spaces(buf), 0, &val);
  645. if (err) {
  646. nilfs_err(nilfs->ns_sb, "unable to convert string: err=%d",
  647. err);
  648. return err;
  649. }
  650. if (val < NILFS_SB_FREQ) {
  651. val = NILFS_SB_FREQ;
  652. nilfs_warn(nilfs->ns_sb,
  653. "superblock update frequency cannot be lesser than 10 seconds");
  654. }
  655. down_write(&nilfs->ns_sem);
  656. nilfs->ns_sb_update_freq = val;
  657. up_write(&nilfs->ns_sem);
  658. return count;
  659. }
  660. static const char sb_readme_str[] =
  661. "The superblock group contains attributes that describe\n"
  662. "superblock's details.\n\n"
  663. "(1) sb_write_time\n\tshow previous write time of super block "
  664. "in human-readable format.\n\n"
  665. "(2) sb_write_time_secs\n\tshow previous write time of super block "
  666. "in seconds.\n\n"
  667. "(3) sb_write_count\n\tshow write count of super block.\n\n"
  668. "(4) sb_update_frequency\n"
  669. "\tshow/set interval of periodical update of superblock (in seconds).\n\n"
  670. "\tYou can set preferable frequency of superblock update by command:\n\n"
  671. "\t'echo <val> > /sys/fs/<nilfs>/<dev>/superblock/sb_update_frequency'\n";
  672. static ssize_t
  673. nilfs_superblock_README_show(struct nilfs_superblock_attr *attr,
  674. struct the_nilfs *nilfs, char *buf)
  675. {
  676. return sysfs_emit(buf, sb_readme_str);
  677. }
  678. NILFS_SUPERBLOCK_RO_ATTR(sb_write_time);
  679. NILFS_SUPERBLOCK_RO_ATTR(sb_write_time_secs);
  680. NILFS_SUPERBLOCK_RO_ATTR(sb_write_count);
  681. NILFS_SUPERBLOCK_RW_ATTR(sb_update_frequency);
  682. NILFS_SUPERBLOCK_RO_ATTR(README);
  683. static struct attribute *nilfs_superblock_attrs[] = {
  684. NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time),
  685. NILFS_SUPERBLOCK_ATTR_LIST(sb_write_time_secs),
  686. NILFS_SUPERBLOCK_ATTR_LIST(sb_write_count),
  687. NILFS_SUPERBLOCK_ATTR_LIST(sb_update_frequency),
  688. NILFS_SUPERBLOCK_ATTR_LIST(README),
  689. NULL,
  690. };
  691. ATTRIBUTE_GROUPS(nilfs_superblock);
  692. NILFS_DEV_INT_GROUP_OPS(superblock, dev);
  693. NILFS_DEV_INT_GROUP_TYPE(superblock, dev);
  694. NILFS_DEV_INT_GROUP_FNS(superblock, dev);
  695. /************************************************************************
  696. * NILFS device attrs *
  697. ************************************************************************/
  698. static
  699. ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
  700. struct the_nilfs *nilfs,
  701. char *buf)
  702. {
  703. struct nilfs_super_block *raw_sb;
  704. u32 major;
  705. u16 minor;
  706. down_read(&nilfs->ns_sem);
  707. raw_sb = nilfs->ns_sbp[0];
  708. major = le32_to_cpu(raw_sb->s_rev_level);
  709. minor = le16_to_cpu(raw_sb->s_minor_rev_level);
  710. up_read(&nilfs->ns_sem);
  711. return sysfs_emit(buf, "%d.%d\n", major, minor);
  712. }
  713. static
  714. ssize_t nilfs_dev_blocksize_show(struct nilfs_dev_attr *attr,
  715. struct the_nilfs *nilfs,
  716. char *buf)
  717. {
  718. return sysfs_emit(buf, "%u\n", nilfs->ns_blocksize);
  719. }
  720. static
  721. ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
  722. struct the_nilfs *nilfs,
  723. char *buf)
  724. {
  725. struct nilfs_super_block *raw_sb;
  726. u64 dev_size;
  727. down_read(&nilfs->ns_sem);
  728. raw_sb = nilfs->ns_sbp[0];
  729. dev_size = le64_to_cpu(raw_sb->s_dev_size);
  730. up_read(&nilfs->ns_sem);
  731. return sysfs_emit(buf, "%llu\n", dev_size);
  732. }
  733. static
  734. ssize_t nilfs_dev_free_blocks_show(struct nilfs_dev_attr *attr,
  735. struct the_nilfs *nilfs,
  736. char *buf)
  737. {
  738. sector_t free_blocks = 0;
  739. nilfs_count_free_blocks(nilfs, &free_blocks);
  740. return sysfs_emit(buf, "%llu\n",
  741. (unsigned long long)free_blocks);
  742. }
  743. static
  744. ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
  745. struct the_nilfs *nilfs,
  746. char *buf)
  747. {
  748. struct nilfs_super_block *raw_sb;
  749. ssize_t len;
  750. down_read(&nilfs->ns_sem);
  751. raw_sb = nilfs->ns_sbp[0];
  752. len = sysfs_emit(buf, "%pUb\n", raw_sb->s_uuid);
  753. up_read(&nilfs->ns_sem);
  754. return len;
  755. }
  756. static
  757. ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
  758. struct the_nilfs *nilfs,
  759. char *buf)
  760. {
  761. struct nilfs_super_block *raw_sb;
  762. ssize_t len;
  763. down_read(&nilfs->ns_sem);
  764. raw_sb = nilfs->ns_sbp[0];
  765. len = scnprintf(buf, sizeof(raw_sb->s_volume_name), "%s\n",
  766. raw_sb->s_volume_name);
  767. up_read(&nilfs->ns_sem);
  768. return len;
  769. }
  770. static const char dev_readme_str[] =
  771. "The <device> group contains attributes that describe file system\n"
  772. "partition's details.\n\n"
  773. "(1) revision\n\tshow NILFS file system revision.\n\n"
  774. "(2) blocksize\n\tshow volume block size in bytes.\n\n"
  775. "(3) device_size\n\tshow volume size in bytes.\n\n"
  776. "(4) free_blocks\n\tshow count of free blocks on volume.\n\n"
  777. "(5) uuid\n\tshow volume's UUID.\n\n"
  778. "(6) volume_name\n\tshow volume's name.\n\n";
  779. static ssize_t nilfs_dev_README_show(struct nilfs_dev_attr *attr,
  780. struct the_nilfs *nilfs,
  781. char *buf)
  782. {
  783. return sysfs_emit(buf, dev_readme_str);
  784. }
  785. NILFS_DEV_RO_ATTR(revision);
  786. NILFS_DEV_RO_ATTR(blocksize);
  787. NILFS_DEV_RO_ATTR(device_size);
  788. NILFS_DEV_RO_ATTR(free_blocks);
  789. NILFS_DEV_RO_ATTR(uuid);
  790. NILFS_DEV_RO_ATTR(volume_name);
  791. NILFS_DEV_RO_ATTR(README);
  792. static struct attribute *nilfs_dev_attrs[] = {
  793. NILFS_DEV_ATTR_LIST(revision),
  794. NILFS_DEV_ATTR_LIST(blocksize),
  795. NILFS_DEV_ATTR_LIST(device_size),
  796. NILFS_DEV_ATTR_LIST(free_blocks),
  797. NILFS_DEV_ATTR_LIST(uuid),
  798. NILFS_DEV_ATTR_LIST(volume_name),
  799. NILFS_DEV_ATTR_LIST(README),
  800. NULL,
  801. };
  802. ATTRIBUTE_GROUPS(nilfs_dev);
  803. static ssize_t nilfs_dev_attr_show(struct kobject *kobj,
  804. struct attribute *attr, char *buf)
  805. {
  806. struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
  807. ns_dev_kobj);
  808. struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
  809. attr);
  810. return a->show ? a->show(a, nilfs, buf) : 0;
  811. }
  812. static ssize_t nilfs_dev_attr_store(struct kobject *kobj,
  813. struct attribute *attr,
  814. const char *buf, size_t len)
  815. {
  816. struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
  817. ns_dev_kobj);
  818. struct nilfs_dev_attr *a = container_of(attr, struct nilfs_dev_attr,
  819. attr);
  820. return a->store ? a->store(a, nilfs, buf, len) : 0;
  821. }
  822. static void nilfs_dev_attr_release(struct kobject *kobj)
  823. {
  824. struct the_nilfs *nilfs = container_of(kobj, struct the_nilfs,
  825. ns_dev_kobj);
  826. complete(&nilfs->ns_dev_kobj_unregister);
  827. }
  828. static const struct sysfs_ops nilfs_dev_attr_ops = {
  829. .show = nilfs_dev_attr_show,
  830. .store = nilfs_dev_attr_store,
  831. };
  832. static const struct kobj_type nilfs_dev_ktype = {
  833. .default_groups = nilfs_dev_groups,
  834. .sysfs_ops = &nilfs_dev_attr_ops,
  835. .release = nilfs_dev_attr_release,
  836. };
  837. int nilfs_sysfs_create_device_group(struct super_block *sb)
  838. {
  839. struct the_nilfs *nilfs = sb->s_fs_info;
  840. size_t devgrp_size = sizeof(struct nilfs_sysfs_dev_subgroups);
  841. int err;
  842. nilfs->ns_dev_subgroups = kzalloc(devgrp_size, GFP_KERNEL);
  843. if (unlikely(!nilfs->ns_dev_subgroups)) {
  844. err = -ENOMEM;
  845. nilfs_err(sb, "unable to allocate memory for device group");
  846. goto failed_create_device_group;
  847. }
  848. nilfs->ns_dev_kobj.kset = nilfs_kset;
  849. init_completion(&nilfs->ns_dev_kobj_unregister);
  850. err = kobject_init_and_add(&nilfs->ns_dev_kobj, &nilfs_dev_ktype, NULL,
  851. "%s", sb->s_id);
  852. if (err)
  853. goto cleanup_dev_kobject;
  854. err = nilfs_sysfs_create_mounted_snapshots_group(nilfs);
  855. if (err)
  856. goto cleanup_dev_kobject;
  857. err = nilfs_sysfs_create_checkpoints_group(nilfs);
  858. if (err)
  859. goto delete_mounted_snapshots_group;
  860. err = nilfs_sysfs_create_segments_group(nilfs);
  861. if (err)
  862. goto delete_checkpoints_group;
  863. err = nilfs_sysfs_create_superblock_group(nilfs);
  864. if (err)
  865. goto delete_segments_group;
  866. err = nilfs_sysfs_create_segctor_group(nilfs);
  867. if (err)
  868. goto delete_superblock_group;
  869. return 0;
  870. delete_superblock_group:
  871. nilfs_sysfs_delete_superblock_group(nilfs);
  872. delete_segments_group:
  873. nilfs_sysfs_delete_segments_group(nilfs);
  874. delete_checkpoints_group:
  875. nilfs_sysfs_delete_checkpoints_group(nilfs);
  876. delete_mounted_snapshots_group:
  877. nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
  878. cleanup_dev_kobject:
  879. kobject_put(&nilfs->ns_dev_kobj);
  880. kfree(nilfs->ns_dev_subgroups);
  881. failed_create_device_group:
  882. return err;
  883. }
  884. void nilfs_sysfs_delete_device_group(struct the_nilfs *nilfs)
  885. {
  886. nilfs_sysfs_delete_mounted_snapshots_group(nilfs);
  887. nilfs_sysfs_delete_checkpoints_group(nilfs);
  888. nilfs_sysfs_delete_segments_group(nilfs);
  889. nilfs_sysfs_delete_superblock_group(nilfs);
  890. nilfs_sysfs_delete_segctor_group(nilfs);
  891. kobject_del(&nilfs->ns_dev_kobj);
  892. kobject_put(&nilfs->ns_dev_kobj);
  893. kfree(nilfs->ns_dev_subgroups);
  894. }
  895. /************************************************************************
  896. * NILFS feature attrs *
  897. ************************************************************************/
  898. static ssize_t nilfs_feature_revision_show(struct kobject *kobj,
  899. struct kobj_attribute *attr, char *buf)
  900. {
  901. return sysfs_emit(buf, "%d.%d\n",
  902. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  903. }
  904. static const char features_readme_str[] =
  905. "The features group contains attributes that describe NILFS file\n"
  906. "system driver features.\n\n"
  907. "(1) revision\n\tshow current revision of NILFS file system driver.\n";
  908. static ssize_t nilfs_feature_README_show(struct kobject *kobj,
  909. struct kobj_attribute *attr,
  910. char *buf)
  911. {
  912. return sysfs_emit(buf, features_readme_str);
  913. }
  914. NILFS_FEATURE_RO_ATTR(revision);
  915. NILFS_FEATURE_RO_ATTR(README);
  916. static struct attribute *nilfs_feature_attrs[] = {
  917. NILFS_FEATURE_ATTR_LIST(revision),
  918. NILFS_FEATURE_ATTR_LIST(README),
  919. NULL,
  920. };
  921. static const struct attribute_group nilfs_feature_attr_group = {
  922. .name = "features",
  923. .attrs = nilfs_feature_attrs,
  924. };
  925. int __init nilfs_sysfs_init(void)
  926. {
  927. int err;
  928. nilfs_kset = kset_create_and_add(NILFS_ROOT_GROUP_NAME, NULL, fs_kobj);
  929. if (!nilfs_kset) {
  930. err = -ENOMEM;
  931. nilfs_err(NULL, "unable to create sysfs entry: err=%d", err);
  932. goto failed_sysfs_init;
  933. }
  934. err = sysfs_create_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
  935. if (unlikely(err)) {
  936. nilfs_err(NULL, "unable to create feature group: err=%d", err);
  937. goto cleanup_sysfs_init;
  938. }
  939. return 0;
  940. cleanup_sysfs_init:
  941. kset_unregister(nilfs_kset);
  942. failed_sysfs_init:
  943. return err;
  944. }
  945. void nilfs_sysfs_exit(void)
  946. {
  947. sysfs_remove_group(&nilfs_kset->kobj, &nilfs_feature_attr_group);
  948. kset_unregister(nilfs_kset);
  949. }