sysfs.c 31 KB

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