filecheck.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * filecheck.c
  4. *
  5. * Code which implements online file check.
  6. *
  7. * Copyright (C) 2016 SuSE. All rights reserved.
  8. */
  9. #include <linux/list.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/kmod.h>
  14. #include <linux/fs.h>
  15. #include <linux/kobject.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/sysctl.h>
  18. #include <cluster/masklog.h>
  19. #include "ocfs2.h"
  20. #include "ocfs2_fs.h"
  21. #include "stackglue.h"
  22. #include "inode.h"
  23. #include "filecheck.h"
  24. /* File check error strings,
  25. * must correspond with error number in header file.
  26. */
  27. static const char * const ocfs2_filecheck_errs[] = {
  28. "SUCCESS",
  29. "FAILED",
  30. "INPROGRESS",
  31. "READONLY",
  32. "INJBD",
  33. "INVALIDINO",
  34. "BLOCKECC",
  35. "BLOCKNO",
  36. "VALIDFLAG",
  37. "GENERATION",
  38. "UNSUPPORTED"
  39. };
  40. struct ocfs2_filecheck_entry {
  41. struct list_head fe_list;
  42. unsigned long fe_ino;
  43. unsigned int fe_type;
  44. unsigned int fe_done:1;
  45. unsigned int fe_status:31;
  46. };
  47. struct ocfs2_filecheck_args {
  48. unsigned int fa_type;
  49. union {
  50. unsigned long fa_ino;
  51. unsigned int fa_len;
  52. };
  53. };
  54. static const char *
  55. ocfs2_filecheck_error(int errno)
  56. {
  57. if (!errno)
  58. return ocfs2_filecheck_errs[errno];
  59. BUG_ON(errno < OCFS2_FILECHECK_ERR_START ||
  60. errno > OCFS2_FILECHECK_ERR_END);
  61. return ocfs2_filecheck_errs[errno - OCFS2_FILECHECK_ERR_START + 1];
  62. }
  63. static ssize_t ocfs2_filecheck_attr_show(struct kobject *kobj,
  64. struct kobj_attribute *attr,
  65. char *buf);
  66. static ssize_t ocfs2_filecheck_attr_store(struct kobject *kobj,
  67. struct kobj_attribute *attr,
  68. const char *buf, size_t count);
  69. static struct kobj_attribute ocfs2_filecheck_attr_chk =
  70. __ATTR(check, S_IRUSR | S_IWUSR,
  71. ocfs2_filecheck_attr_show,
  72. ocfs2_filecheck_attr_store);
  73. static struct kobj_attribute ocfs2_filecheck_attr_fix =
  74. __ATTR(fix, S_IRUSR | S_IWUSR,
  75. ocfs2_filecheck_attr_show,
  76. ocfs2_filecheck_attr_store);
  77. static struct kobj_attribute ocfs2_filecheck_attr_set =
  78. __ATTR(set, S_IRUSR | S_IWUSR,
  79. ocfs2_filecheck_attr_show,
  80. ocfs2_filecheck_attr_store);
  81. static struct attribute *ocfs2_filecheck_attrs[] = {
  82. &ocfs2_filecheck_attr_chk.attr,
  83. &ocfs2_filecheck_attr_fix.attr,
  84. &ocfs2_filecheck_attr_set.attr,
  85. NULL
  86. };
  87. ATTRIBUTE_GROUPS(ocfs2_filecheck);
  88. static void ocfs2_filecheck_release(struct kobject *kobj)
  89. {
  90. struct ocfs2_filecheck_sysfs_entry *entry = container_of(kobj,
  91. struct ocfs2_filecheck_sysfs_entry, fs_kobj);
  92. complete(&entry->fs_kobj_unregister);
  93. }
  94. static ssize_t
  95. ocfs2_filecheck_show(struct kobject *kobj, struct attribute *attr, char *buf)
  96. {
  97. ssize_t ret = -EIO;
  98. struct kobj_attribute *kattr = container_of(attr,
  99. struct kobj_attribute, attr);
  100. kobject_get(kobj);
  101. if (kattr->show)
  102. ret = kattr->show(kobj, kattr, buf);
  103. kobject_put(kobj);
  104. return ret;
  105. }
  106. static ssize_t
  107. ocfs2_filecheck_store(struct kobject *kobj, struct attribute *attr,
  108. const char *buf, size_t count)
  109. {
  110. ssize_t ret = -EIO;
  111. struct kobj_attribute *kattr = container_of(attr,
  112. struct kobj_attribute, attr);
  113. kobject_get(kobj);
  114. if (kattr->store)
  115. ret = kattr->store(kobj, kattr, buf, count);
  116. kobject_put(kobj);
  117. return ret;
  118. }
  119. static const struct sysfs_ops ocfs2_filecheck_ops = {
  120. .show = ocfs2_filecheck_show,
  121. .store = ocfs2_filecheck_store,
  122. };
  123. static struct kobj_type ocfs2_ktype_filecheck = {
  124. .default_groups = ocfs2_filecheck_groups,
  125. .sysfs_ops = &ocfs2_filecheck_ops,
  126. .release = ocfs2_filecheck_release,
  127. };
  128. static void
  129. ocfs2_filecheck_sysfs_free(struct ocfs2_filecheck_sysfs_entry *entry)
  130. {
  131. struct ocfs2_filecheck_entry *p;
  132. spin_lock(&entry->fs_fcheck->fc_lock);
  133. while (!list_empty(&entry->fs_fcheck->fc_head)) {
  134. p = list_first_entry(&entry->fs_fcheck->fc_head,
  135. struct ocfs2_filecheck_entry, fe_list);
  136. list_del(&p->fe_list);
  137. BUG_ON(!p->fe_done); /* To free a undone file check entry */
  138. kfree(p);
  139. }
  140. spin_unlock(&entry->fs_fcheck->fc_lock);
  141. kfree(entry->fs_fcheck);
  142. entry->fs_fcheck = NULL;
  143. }
  144. int ocfs2_filecheck_create_sysfs(struct ocfs2_super *osb)
  145. {
  146. int ret;
  147. struct ocfs2_filecheck *fcheck;
  148. struct ocfs2_filecheck_sysfs_entry *entry = &osb->osb_fc_ent;
  149. fcheck = kmalloc(sizeof(struct ocfs2_filecheck), GFP_NOFS);
  150. if (!fcheck)
  151. return -ENOMEM;
  152. INIT_LIST_HEAD(&fcheck->fc_head);
  153. spin_lock_init(&fcheck->fc_lock);
  154. fcheck->fc_max = OCFS2_FILECHECK_MINSIZE;
  155. fcheck->fc_size = 0;
  156. fcheck->fc_done = 0;
  157. entry->fs_kobj.kset = osb->osb_dev_kset;
  158. init_completion(&entry->fs_kobj_unregister);
  159. ret = kobject_init_and_add(&entry->fs_kobj, &ocfs2_ktype_filecheck,
  160. NULL, "filecheck");
  161. if (ret) {
  162. kobject_put(&entry->fs_kobj);
  163. kfree(fcheck);
  164. return ret;
  165. }
  166. entry->fs_fcheck = fcheck;
  167. return 0;
  168. }
  169. void ocfs2_filecheck_remove_sysfs(struct ocfs2_super *osb)
  170. {
  171. if (!osb->osb_fc_ent.fs_fcheck)
  172. return;
  173. kobject_del(&osb->osb_fc_ent.fs_kobj);
  174. kobject_put(&osb->osb_fc_ent.fs_kobj);
  175. wait_for_completion(&osb->osb_fc_ent.fs_kobj_unregister);
  176. ocfs2_filecheck_sysfs_free(&osb->osb_fc_ent);
  177. }
  178. static int
  179. ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
  180. unsigned int count);
  181. static int
  182. ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent,
  183. unsigned int len)
  184. {
  185. int ret;
  186. if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE))
  187. return -EINVAL;
  188. spin_lock(&ent->fs_fcheck->fc_lock);
  189. if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) {
  190. mlog(ML_NOTICE,
  191. "Cannot set online file check maximum entry number "
  192. "to %u due to too many pending entries(%u)\n",
  193. len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done);
  194. ret = -EBUSY;
  195. } else {
  196. if (len < ent->fs_fcheck->fc_size)
  197. BUG_ON(!ocfs2_filecheck_erase_entries(ent,
  198. ent->fs_fcheck->fc_size - len));
  199. ent->fs_fcheck->fc_max = len;
  200. ret = 0;
  201. }
  202. spin_unlock(&ent->fs_fcheck->fc_lock);
  203. return ret;
  204. }
  205. #define OCFS2_FILECHECK_ARGS_LEN 24
  206. static int
  207. ocfs2_filecheck_args_get_long(const char *buf, size_t count,
  208. unsigned long *val)
  209. {
  210. char buffer[OCFS2_FILECHECK_ARGS_LEN];
  211. memcpy(buffer, buf, count);
  212. buffer[count] = '\0';
  213. if (kstrtoul(buffer, 0, val))
  214. return 1;
  215. return 0;
  216. }
  217. static int
  218. ocfs2_filecheck_type_parse(const char *name, unsigned int *type)
  219. {
  220. if (!strncmp(name, "fix", 4))
  221. *type = OCFS2_FILECHECK_TYPE_FIX;
  222. else if (!strncmp(name, "check", 6))
  223. *type = OCFS2_FILECHECK_TYPE_CHK;
  224. else if (!strncmp(name, "set", 4))
  225. *type = OCFS2_FILECHECK_TYPE_SET;
  226. else
  227. return 1;
  228. return 0;
  229. }
  230. static int
  231. ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count,
  232. struct ocfs2_filecheck_args *args)
  233. {
  234. unsigned long val = 0;
  235. unsigned int type;
  236. /* too short/long args length */
  237. if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN))
  238. return 1;
  239. if (ocfs2_filecheck_type_parse(name, &type))
  240. return 1;
  241. if (ocfs2_filecheck_args_get_long(buf, count, &val))
  242. return 1;
  243. if (val <= 0)
  244. return 1;
  245. args->fa_type = type;
  246. if (type == OCFS2_FILECHECK_TYPE_SET)
  247. args->fa_len = (unsigned int)val;
  248. else
  249. args->fa_ino = val;
  250. return 0;
  251. }
  252. static ssize_t ocfs2_filecheck_attr_show(struct kobject *kobj,
  253. struct kobj_attribute *attr,
  254. char *buf)
  255. {
  256. ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
  257. unsigned int type;
  258. struct ocfs2_filecheck_entry *p;
  259. struct ocfs2_filecheck_sysfs_entry *ent = container_of(kobj,
  260. struct ocfs2_filecheck_sysfs_entry, fs_kobj);
  261. if (ocfs2_filecheck_type_parse(attr->attr.name, &type))
  262. return -EINVAL;
  263. if (type == OCFS2_FILECHECK_TYPE_SET) {
  264. spin_lock(&ent->fs_fcheck->fc_lock);
  265. total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max);
  266. spin_unlock(&ent->fs_fcheck->fc_lock);
  267. goto exit;
  268. }
  269. ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n");
  270. total += ret;
  271. remain -= ret;
  272. spin_lock(&ent->fs_fcheck->fc_lock);
  273. list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
  274. if (p->fe_type != type)
  275. continue;
  276. ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n",
  277. p->fe_ino, p->fe_done,
  278. ocfs2_filecheck_error(p->fe_status));
  279. if (ret >= remain) {
  280. /* snprintf() didn't fit */
  281. total = -E2BIG;
  282. break;
  283. }
  284. total += ret;
  285. remain -= ret;
  286. }
  287. spin_unlock(&ent->fs_fcheck->fc_lock);
  288. exit:
  289. return total;
  290. }
  291. static inline int
  292. ocfs2_filecheck_is_dup_entry(struct ocfs2_filecheck_sysfs_entry *ent,
  293. unsigned long ino)
  294. {
  295. struct ocfs2_filecheck_entry *p;
  296. list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
  297. if (!p->fe_done) {
  298. if (p->fe_ino == ino)
  299. return 1;
  300. }
  301. }
  302. return 0;
  303. }
  304. static inline int
  305. ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent)
  306. {
  307. struct ocfs2_filecheck_entry *p;
  308. list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
  309. if (p->fe_done) {
  310. list_del(&p->fe_list);
  311. kfree(p);
  312. ent->fs_fcheck->fc_size--;
  313. ent->fs_fcheck->fc_done--;
  314. return 1;
  315. }
  316. }
  317. return 0;
  318. }
  319. static int
  320. ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
  321. unsigned int count)
  322. {
  323. unsigned int i = 0;
  324. unsigned int ret = 0;
  325. while (i++ < count) {
  326. if (ocfs2_filecheck_erase_entry(ent))
  327. ret++;
  328. else
  329. break;
  330. }
  331. return (ret == count ? 1 : 0);
  332. }
  333. static void
  334. ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent,
  335. struct ocfs2_filecheck_entry *entry)
  336. {
  337. spin_lock(&ent->fs_fcheck->fc_lock);
  338. entry->fe_done = 1;
  339. ent->fs_fcheck->fc_done++;
  340. spin_unlock(&ent->fs_fcheck->fc_lock);
  341. }
  342. static unsigned int
  343. ocfs2_filecheck_handle(struct ocfs2_super *osb,
  344. unsigned long ino, unsigned int flags)
  345. {
  346. unsigned int ret = OCFS2_FILECHECK_ERR_SUCCESS;
  347. struct inode *inode = NULL;
  348. int rc;
  349. inode = ocfs2_iget(osb, ino, flags, 0);
  350. if (IS_ERR(inode)) {
  351. rc = (int)(-(long)inode);
  352. if (rc >= OCFS2_FILECHECK_ERR_START &&
  353. rc < OCFS2_FILECHECK_ERR_END)
  354. ret = rc;
  355. else
  356. ret = OCFS2_FILECHECK_ERR_FAILED;
  357. } else
  358. iput(inode);
  359. return ret;
  360. }
  361. static void
  362. ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent,
  363. struct ocfs2_filecheck_entry *entry)
  364. {
  365. struct ocfs2_super *osb = container_of(ent, struct ocfs2_super,
  366. osb_fc_ent);
  367. if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK)
  368. entry->fe_status = ocfs2_filecheck_handle(osb,
  369. entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK);
  370. else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX)
  371. entry->fe_status = ocfs2_filecheck_handle(osb,
  372. entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX);
  373. else
  374. entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED;
  375. ocfs2_filecheck_done_entry(ent, entry);
  376. }
  377. static ssize_t ocfs2_filecheck_attr_store(struct kobject *kobj,
  378. struct kobj_attribute *attr,
  379. const char *buf, size_t count)
  380. {
  381. ssize_t ret = 0;
  382. struct ocfs2_filecheck_args args;
  383. struct ocfs2_filecheck_entry *entry;
  384. struct ocfs2_filecheck_sysfs_entry *ent = container_of(kobj,
  385. struct ocfs2_filecheck_sysfs_entry, fs_kobj);
  386. if (count == 0)
  387. return count;
  388. if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args))
  389. return -EINVAL;
  390. if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) {
  391. ret = ocfs2_filecheck_adjust_max(ent, args.fa_len);
  392. goto exit;
  393. }
  394. entry = kmalloc(sizeof(struct ocfs2_filecheck_entry), GFP_NOFS);
  395. if (!entry) {
  396. ret = -ENOMEM;
  397. goto exit;
  398. }
  399. spin_lock(&ent->fs_fcheck->fc_lock);
  400. if (ocfs2_filecheck_is_dup_entry(ent, args.fa_ino)) {
  401. ret = -EEXIST;
  402. kfree(entry);
  403. } else if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
  404. (ent->fs_fcheck->fc_done == 0)) {
  405. mlog(ML_NOTICE,
  406. "Cannot do more file check "
  407. "since file check queue(%u) is full now\n",
  408. ent->fs_fcheck->fc_max);
  409. ret = -EAGAIN;
  410. kfree(entry);
  411. } else {
  412. if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
  413. (ent->fs_fcheck->fc_done > 0)) {
  414. /* Delete the oldest entry which was done,
  415. * make sure the entry size in list does
  416. * not exceed maximum value
  417. */
  418. BUG_ON(!ocfs2_filecheck_erase_entry(ent));
  419. }
  420. entry->fe_ino = args.fa_ino;
  421. entry->fe_type = args.fa_type;
  422. entry->fe_done = 0;
  423. entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS;
  424. list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head);
  425. ent->fs_fcheck->fc_size++;
  426. }
  427. spin_unlock(&ent->fs_fcheck->fc_lock);
  428. if (!ret)
  429. ocfs2_filecheck_handle_entry(ent, entry);
  430. exit:
  431. return (!ret ? count : ret);
  432. }