filecheck.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * filecheck.c
  5. *
  6. * Code which implements online file check.
  7. *
  8. * Copyright (C) 2016 SuSE. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation, version 2.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/list.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/kmod.h>
  24. #include <linux/fs.h>
  25. #include <linux/kobject.h>
  26. #include <linux/sysfs.h>
  27. #include <linux/sysctl.h>
  28. #include <cluster/masklog.h>
  29. #include "ocfs2.h"
  30. #include "ocfs2_fs.h"
  31. #include "stackglue.h"
  32. #include "inode.h"
  33. #include "filecheck.h"
  34. /* File check error strings,
  35. * must correspond with error number in header file.
  36. */
  37. static const char * const ocfs2_filecheck_errs[] = {
  38. "SUCCESS",
  39. "FAILED",
  40. "INPROGRESS",
  41. "READONLY",
  42. "INJBD",
  43. "INVALIDINO",
  44. "BLOCKECC",
  45. "BLOCKNO",
  46. "VALIDFLAG",
  47. "GENERATION",
  48. "UNSUPPORTED"
  49. };
  50. static DEFINE_SPINLOCK(ocfs2_filecheck_sysfs_lock);
  51. static LIST_HEAD(ocfs2_filecheck_sysfs_list);
  52. struct ocfs2_filecheck {
  53. struct list_head fc_head; /* File check entry list head */
  54. spinlock_t fc_lock;
  55. unsigned int fc_max; /* Maximum number of entry in list */
  56. unsigned int fc_size; /* Current entry count in list */
  57. unsigned int fc_done; /* Finished entry count in list */
  58. };
  59. struct ocfs2_filecheck_sysfs_entry { /* sysfs entry per mounting */
  60. struct list_head fs_list;
  61. atomic_t fs_count;
  62. struct super_block *fs_sb;
  63. struct kset *fs_devicekset;
  64. struct kset *fs_fcheckkset;
  65. struct ocfs2_filecheck *fs_fcheck;
  66. };
  67. #define OCFS2_FILECHECK_MAXSIZE 100
  68. #define OCFS2_FILECHECK_MINSIZE 10
  69. /* File check operation type */
  70. enum {
  71. OCFS2_FILECHECK_TYPE_CHK = 0, /* Check a file(inode) */
  72. OCFS2_FILECHECK_TYPE_FIX, /* Fix a file(inode) */
  73. OCFS2_FILECHECK_TYPE_SET = 100 /* Set entry list maximum size */
  74. };
  75. struct ocfs2_filecheck_entry {
  76. struct list_head fe_list;
  77. unsigned long fe_ino;
  78. unsigned int fe_type;
  79. unsigned int fe_done:1;
  80. unsigned int fe_status:31;
  81. };
  82. struct ocfs2_filecheck_args {
  83. unsigned int fa_type;
  84. union {
  85. unsigned long fa_ino;
  86. unsigned int fa_len;
  87. };
  88. };
  89. static const char *
  90. ocfs2_filecheck_error(int errno)
  91. {
  92. if (!errno)
  93. return ocfs2_filecheck_errs[errno];
  94. BUG_ON(errno < OCFS2_FILECHECK_ERR_START ||
  95. errno > OCFS2_FILECHECK_ERR_END);
  96. return ocfs2_filecheck_errs[errno - OCFS2_FILECHECK_ERR_START + 1];
  97. }
  98. static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
  99. struct kobj_attribute *attr,
  100. char *buf);
  101. static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
  102. struct kobj_attribute *attr,
  103. const char *buf, size_t count);
  104. static struct kobj_attribute ocfs2_attr_filecheck_chk =
  105. __ATTR(check, S_IRUSR | S_IWUSR,
  106. ocfs2_filecheck_show,
  107. ocfs2_filecheck_store);
  108. static struct kobj_attribute ocfs2_attr_filecheck_fix =
  109. __ATTR(fix, S_IRUSR | S_IWUSR,
  110. ocfs2_filecheck_show,
  111. ocfs2_filecheck_store);
  112. static struct kobj_attribute ocfs2_attr_filecheck_set =
  113. __ATTR(set, S_IRUSR | S_IWUSR,
  114. ocfs2_filecheck_show,
  115. ocfs2_filecheck_store);
  116. static int ocfs2_filecheck_sysfs_wait(atomic_t *p)
  117. {
  118. schedule();
  119. return 0;
  120. }
  121. static void
  122. ocfs2_filecheck_sysfs_free(struct ocfs2_filecheck_sysfs_entry *entry)
  123. {
  124. struct ocfs2_filecheck_entry *p;
  125. if (!atomic_dec_and_test(&entry->fs_count))
  126. wait_on_atomic_t(&entry->fs_count, ocfs2_filecheck_sysfs_wait,
  127. TASK_UNINTERRUPTIBLE);
  128. spin_lock(&entry->fs_fcheck->fc_lock);
  129. while (!list_empty(&entry->fs_fcheck->fc_head)) {
  130. p = list_first_entry(&entry->fs_fcheck->fc_head,
  131. struct ocfs2_filecheck_entry, fe_list);
  132. list_del(&p->fe_list);
  133. BUG_ON(!p->fe_done); /* To free a undone file check entry */
  134. kfree(p);
  135. }
  136. spin_unlock(&entry->fs_fcheck->fc_lock);
  137. kset_unregister(entry->fs_fcheckkset);
  138. kset_unregister(entry->fs_devicekset);
  139. kfree(entry->fs_fcheck);
  140. kfree(entry);
  141. }
  142. static void
  143. ocfs2_filecheck_sysfs_add(struct ocfs2_filecheck_sysfs_entry *entry)
  144. {
  145. spin_lock(&ocfs2_filecheck_sysfs_lock);
  146. list_add_tail(&entry->fs_list, &ocfs2_filecheck_sysfs_list);
  147. spin_unlock(&ocfs2_filecheck_sysfs_lock);
  148. }
  149. static int ocfs2_filecheck_sysfs_del(const char *devname)
  150. {
  151. struct ocfs2_filecheck_sysfs_entry *p;
  152. spin_lock(&ocfs2_filecheck_sysfs_lock);
  153. list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
  154. if (!strcmp(p->fs_sb->s_id, devname)) {
  155. list_del(&p->fs_list);
  156. spin_unlock(&ocfs2_filecheck_sysfs_lock);
  157. ocfs2_filecheck_sysfs_free(p);
  158. return 0;
  159. }
  160. }
  161. spin_unlock(&ocfs2_filecheck_sysfs_lock);
  162. return 1;
  163. }
  164. static void
  165. ocfs2_filecheck_sysfs_put(struct ocfs2_filecheck_sysfs_entry *entry)
  166. {
  167. if (atomic_dec_and_test(&entry->fs_count))
  168. wake_up_atomic_t(&entry->fs_count);
  169. }
  170. static struct ocfs2_filecheck_sysfs_entry *
  171. ocfs2_filecheck_sysfs_get(const char *devname)
  172. {
  173. struct ocfs2_filecheck_sysfs_entry *p = NULL;
  174. spin_lock(&ocfs2_filecheck_sysfs_lock);
  175. list_for_each_entry(p, &ocfs2_filecheck_sysfs_list, fs_list) {
  176. if (!strcmp(p->fs_sb->s_id, devname)) {
  177. atomic_inc(&p->fs_count);
  178. spin_unlock(&ocfs2_filecheck_sysfs_lock);
  179. return p;
  180. }
  181. }
  182. spin_unlock(&ocfs2_filecheck_sysfs_lock);
  183. return NULL;
  184. }
  185. int ocfs2_filecheck_create_sysfs(struct super_block *sb)
  186. {
  187. int ret = 0;
  188. struct kset *device_kset = NULL;
  189. struct kset *fcheck_kset = NULL;
  190. struct ocfs2_filecheck *fcheck = NULL;
  191. struct ocfs2_filecheck_sysfs_entry *entry = NULL;
  192. struct attribute **attrs = NULL;
  193. struct attribute_group attrgp;
  194. if (!ocfs2_kset)
  195. return -ENOMEM;
  196. attrs = kmalloc(sizeof(struct attribute *) * 4, GFP_NOFS);
  197. if (!attrs) {
  198. ret = -ENOMEM;
  199. goto error;
  200. } else {
  201. attrs[0] = &ocfs2_attr_filecheck_chk.attr;
  202. attrs[1] = &ocfs2_attr_filecheck_fix.attr;
  203. attrs[2] = &ocfs2_attr_filecheck_set.attr;
  204. attrs[3] = NULL;
  205. memset(&attrgp, 0, sizeof(attrgp));
  206. attrgp.attrs = attrs;
  207. }
  208. fcheck = kmalloc(sizeof(struct ocfs2_filecheck), GFP_NOFS);
  209. if (!fcheck) {
  210. ret = -ENOMEM;
  211. goto error;
  212. } else {
  213. INIT_LIST_HEAD(&fcheck->fc_head);
  214. spin_lock_init(&fcheck->fc_lock);
  215. fcheck->fc_max = OCFS2_FILECHECK_MINSIZE;
  216. fcheck->fc_size = 0;
  217. fcheck->fc_done = 0;
  218. }
  219. if (strlen(sb->s_id) <= 0) {
  220. mlog(ML_ERROR,
  221. "Cannot get device basename when create filecheck sysfs\n");
  222. ret = -ENODEV;
  223. goto error;
  224. }
  225. device_kset = kset_create_and_add(sb->s_id, NULL, &ocfs2_kset->kobj);
  226. if (!device_kset) {
  227. ret = -ENOMEM;
  228. goto error;
  229. }
  230. fcheck_kset = kset_create_and_add("filecheck", NULL,
  231. &device_kset->kobj);
  232. if (!fcheck_kset) {
  233. ret = -ENOMEM;
  234. goto error;
  235. }
  236. ret = sysfs_create_group(&fcheck_kset->kobj, &attrgp);
  237. if (ret)
  238. goto error;
  239. entry = kmalloc(sizeof(struct ocfs2_filecheck_sysfs_entry), GFP_NOFS);
  240. if (!entry) {
  241. ret = -ENOMEM;
  242. goto error;
  243. } else {
  244. atomic_set(&entry->fs_count, 1);
  245. entry->fs_sb = sb;
  246. entry->fs_devicekset = device_kset;
  247. entry->fs_fcheckkset = fcheck_kset;
  248. entry->fs_fcheck = fcheck;
  249. ocfs2_filecheck_sysfs_add(entry);
  250. }
  251. kfree(attrs);
  252. return 0;
  253. error:
  254. kfree(attrs);
  255. kfree(entry);
  256. kfree(fcheck);
  257. kset_unregister(fcheck_kset);
  258. kset_unregister(device_kset);
  259. return ret;
  260. }
  261. int ocfs2_filecheck_remove_sysfs(struct super_block *sb)
  262. {
  263. return ocfs2_filecheck_sysfs_del(sb->s_id);
  264. }
  265. static int
  266. ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
  267. unsigned int count);
  268. static int
  269. ocfs2_filecheck_adjust_max(struct ocfs2_filecheck_sysfs_entry *ent,
  270. unsigned int len)
  271. {
  272. int ret;
  273. if ((len < OCFS2_FILECHECK_MINSIZE) || (len > OCFS2_FILECHECK_MAXSIZE))
  274. return -EINVAL;
  275. spin_lock(&ent->fs_fcheck->fc_lock);
  276. if (len < (ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done)) {
  277. mlog(ML_ERROR,
  278. "Cannot set online file check maximum entry number "
  279. "to %u due to too many pending entries(%u)\n",
  280. len, ent->fs_fcheck->fc_size - ent->fs_fcheck->fc_done);
  281. ret = -EBUSY;
  282. } else {
  283. if (len < ent->fs_fcheck->fc_size)
  284. BUG_ON(!ocfs2_filecheck_erase_entries(ent,
  285. ent->fs_fcheck->fc_size - len));
  286. ent->fs_fcheck->fc_max = len;
  287. ret = 0;
  288. }
  289. spin_unlock(&ent->fs_fcheck->fc_lock);
  290. return ret;
  291. }
  292. #define OCFS2_FILECHECK_ARGS_LEN 24
  293. static int
  294. ocfs2_filecheck_args_get_long(const char *buf, size_t count,
  295. unsigned long *val)
  296. {
  297. char buffer[OCFS2_FILECHECK_ARGS_LEN];
  298. memcpy(buffer, buf, count);
  299. buffer[count] = '\0';
  300. if (kstrtoul(buffer, 0, val))
  301. return 1;
  302. return 0;
  303. }
  304. static int
  305. ocfs2_filecheck_type_parse(const char *name, unsigned int *type)
  306. {
  307. if (!strncmp(name, "fix", 4))
  308. *type = OCFS2_FILECHECK_TYPE_FIX;
  309. else if (!strncmp(name, "check", 6))
  310. *type = OCFS2_FILECHECK_TYPE_CHK;
  311. else if (!strncmp(name, "set", 4))
  312. *type = OCFS2_FILECHECK_TYPE_SET;
  313. else
  314. return 1;
  315. return 0;
  316. }
  317. static int
  318. ocfs2_filecheck_args_parse(const char *name, const char *buf, size_t count,
  319. struct ocfs2_filecheck_args *args)
  320. {
  321. unsigned long val = 0;
  322. unsigned int type;
  323. /* too short/long args length */
  324. if ((count < 1) || (count >= OCFS2_FILECHECK_ARGS_LEN))
  325. return 1;
  326. if (ocfs2_filecheck_type_parse(name, &type))
  327. return 1;
  328. if (ocfs2_filecheck_args_get_long(buf, count, &val))
  329. return 1;
  330. if (val <= 0)
  331. return 1;
  332. args->fa_type = type;
  333. if (type == OCFS2_FILECHECK_TYPE_SET)
  334. args->fa_len = (unsigned int)val;
  335. else
  336. args->fa_ino = val;
  337. return 0;
  338. }
  339. static ssize_t ocfs2_filecheck_show(struct kobject *kobj,
  340. struct kobj_attribute *attr,
  341. char *buf)
  342. {
  343. ssize_t ret = 0, total = 0, remain = PAGE_SIZE;
  344. unsigned int type;
  345. struct ocfs2_filecheck_entry *p;
  346. struct ocfs2_filecheck_sysfs_entry *ent;
  347. if (ocfs2_filecheck_type_parse(attr->attr.name, &type))
  348. return -EINVAL;
  349. ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
  350. if (!ent) {
  351. mlog(ML_ERROR,
  352. "Cannot get the corresponding entry via device basename %s\n",
  353. kobj->name);
  354. return -ENODEV;
  355. }
  356. if (type == OCFS2_FILECHECK_TYPE_SET) {
  357. spin_lock(&ent->fs_fcheck->fc_lock);
  358. total = snprintf(buf, remain, "%u\n", ent->fs_fcheck->fc_max);
  359. spin_unlock(&ent->fs_fcheck->fc_lock);
  360. goto exit;
  361. }
  362. ret = snprintf(buf, remain, "INO\t\tDONE\tERROR\n");
  363. total += ret;
  364. remain -= ret;
  365. spin_lock(&ent->fs_fcheck->fc_lock);
  366. list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
  367. if (p->fe_type != type)
  368. continue;
  369. ret = snprintf(buf + total, remain, "%lu\t\t%u\t%s\n",
  370. p->fe_ino, p->fe_done,
  371. ocfs2_filecheck_error(p->fe_status));
  372. if (ret < 0) {
  373. total = ret;
  374. break;
  375. }
  376. if (ret == remain) {
  377. /* snprintf() didn't fit */
  378. total = -E2BIG;
  379. break;
  380. }
  381. total += ret;
  382. remain -= ret;
  383. }
  384. spin_unlock(&ent->fs_fcheck->fc_lock);
  385. exit:
  386. ocfs2_filecheck_sysfs_put(ent);
  387. return total;
  388. }
  389. static int
  390. ocfs2_filecheck_erase_entry(struct ocfs2_filecheck_sysfs_entry *ent)
  391. {
  392. struct ocfs2_filecheck_entry *p;
  393. list_for_each_entry(p, &ent->fs_fcheck->fc_head, fe_list) {
  394. if (p->fe_done) {
  395. list_del(&p->fe_list);
  396. kfree(p);
  397. ent->fs_fcheck->fc_size--;
  398. ent->fs_fcheck->fc_done--;
  399. return 1;
  400. }
  401. }
  402. return 0;
  403. }
  404. static int
  405. ocfs2_filecheck_erase_entries(struct ocfs2_filecheck_sysfs_entry *ent,
  406. unsigned int count)
  407. {
  408. unsigned int i = 0;
  409. unsigned int ret = 0;
  410. while (i++ < count) {
  411. if (ocfs2_filecheck_erase_entry(ent))
  412. ret++;
  413. else
  414. break;
  415. }
  416. return (ret == count ? 1 : 0);
  417. }
  418. static void
  419. ocfs2_filecheck_done_entry(struct ocfs2_filecheck_sysfs_entry *ent,
  420. struct ocfs2_filecheck_entry *entry)
  421. {
  422. entry->fe_done = 1;
  423. spin_lock(&ent->fs_fcheck->fc_lock);
  424. ent->fs_fcheck->fc_done++;
  425. spin_unlock(&ent->fs_fcheck->fc_lock);
  426. }
  427. static unsigned int
  428. ocfs2_filecheck_handle(struct super_block *sb,
  429. unsigned long ino, unsigned int flags)
  430. {
  431. unsigned int ret = OCFS2_FILECHECK_ERR_SUCCESS;
  432. struct inode *inode = NULL;
  433. int rc;
  434. inode = ocfs2_iget(OCFS2_SB(sb), ino, flags, 0);
  435. if (IS_ERR(inode)) {
  436. rc = (int)(-(long)inode);
  437. if (rc >= OCFS2_FILECHECK_ERR_START &&
  438. rc < OCFS2_FILECHECK_ERR_END)
  439. ret = rc;
  440. else
  441. ret = OCFS2_FILECHECK_ERR_FAILED;
  442. } else
  443. iput(inode);
  444. return ret;
  445. }
  446. static void
  447. ocfs2_filecheck_handle_entry(struct ocfs2_filecheck_sysfs_entry *ent,
  448. struct ocfs2_filecheck_entry *entry)
  449. {
  450. if (entry->fe_type == OCFS2_FILECHECK_TYPE_CHK)
  451. entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
  452. entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_CHK);
  453. else if (entry->fe_type == OCFS2_FILECHECK_TYPE_FIX)
  454. entry->fe_status = ocfs2_filecheck_handle(ent->fs_sb,
  455. entry->fe_ino, OCFS2_FI_FLAG_FILECHECK_FIX);
  456. else
  457. entry->fe_status = OCFS2_FILECHECK_ERR_UNSUPPORTED;
  458. ocfs2_filecheck_done_entry(ent, entry);
  459. }
  460. static ssize_t ocfs2_filecheck_store(struct kobject *kobj,
  461. struct kobj_attribute *attr,
  462. const char *buf, size_t count)
  463. {
  464. struct ocfs2_filecheck_args args;
  465. struct ocfs2_filecheck_entry *entry;
  466. struct ocfs2_filecheck_sysfs_entry *ent;
  467. ssize_t ret = 0;
  468. if (count == 0)
  469. return count;
  470. if (ocfs2_filecheck_args_parse(attr->attr.name, buf, count, &args)) {
  471. mlog(ML_ERROR, "Invalid arguments for online file check\n");
  472. return -EINVAL;
  473. }
  474. ent = ocfs2_filecheck_sysfs_get(kobj->parent->name);
  475. if (!ent) {
  476. mlog(ML_ERROR,
  477. "Cannot get the corresponding entry via device basename %s\n",
  478. kobj->parent->name);
  479. return -ENODEV;
  480. }
  481. if (args.fa_type == OCFS2_FILECHECK_TYPE_SET) {
  482. ret = ocfs2_filecheck_adjust_max(ent, args.fa_len);
  483. goto exit;
  484. }
  485. entry = kmalloc(sizeof(struct ocfs2_filecheck_entry), GFP_NOFS);
  486. if (!entry) {
  487. ret = -ENOMEM;
  488. goto exit;
  489. }
  490. spin_lock(&ent->fs_fcheck->fc_lock);
  491. if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
  492. (ent->fs_fcheck->fc_done == 0)) {
  493. mlog(ML_ERROR,
  494. "Cannot do more file check "
  495. "since file check queue(%u) is full now\n",
  496. ent->fs_fcheck->fc_max);
  497. ret = -EBUSY;
  498. kfree(entry);
  499. } else {
  500. if ((ent->fs_fcheck->fc_size >= ent->fs_fcheck->fc_max) &&
  501. (ent->fs_fcheck->fc_done > 0)) {
  502. /* Delete the oldest entry which was done,
  503. * make sure the entry size in list does
  504. * not exceed maximum value
  505. */
  506. BUG_ON(!ocfs2_filecheck_erase_entry(ent));
  507. }
  508. entry->fe_ino = args.fa_ino;
  509. entry->fe_type = args.fa_type;
  510. entry->fe_done = 0;
  511. entry->fe_status = OCFS2_FILECHECK_ERR_INPROGRESS;
  512. list_add_tail(&entry->fe_list, &ent->fs_fcheck->fc_head);
  513. ent->fs_fcheck->fc_size++;
  514. }
  515. spin_unlock(&ent->fs_fcheck->fc_lock);
  516. if (!ret)
  517. ocfs2_filecheck_handle_entry(ent, entry);
  518. exit:
  519. ocfs2_filecheck_sysfs_put(ent);
  520. return (!ret ? count : ret);
  521. }