scan.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * Authors: Adrian Hunter
  9. * Artem Bityutskiy (Битюцкий Артём)
  10. */
  11. /*
  12. * This file implements the scan which is a general-purpose function for
  13. * determining what nodes are in an eraseblock. The scan is used to replay the
  14. * journal, to do garbage collection. for the TNC in-the-gaps method, and by
  15. * debugging functions.
  16. */
  17. #ifdef __UBOOT__
  18. #include <linux/err.h>
  19. #endif
  20. #include "ubifs.h"
  21. /**
  22. * scan_padding_bytes - scan for padding bytes.
  23. * @buf: buffer to scan
  24. * @len: length of buffer
  25. *
  26. * This function returns the number of padding bytes on success and
  27. * %SCANNED_GARBAGE on failure.
  28. */
  29. static int scan_padding_bytes(void *buf, int len)
  30. {
  31. int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
  32. uint8_t *p = buf;
  33. dbg_scan("not a node");
  34. while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
  35. pad_len += 1;
  36. if (!pad_len || (pad_len & 7))
  37. return SCANNED_GARBAGE;
  38. dbg_scan("%d padding bytes", pad_len);
  39. return pad_len;
  40. }
  41. /**
  42. * ubifs_scan_a_node - scan for a node or padding.
  43. * @c: UBIFS file-system description object
  44. * @buf: buffer to scan
  45. * @len: length of buffer
  46. * @lnum: logical eraseblock number
  47. * @offs: offset within the logical eraseblock
  48. * @quiet: print no messages
  49. *
  50. * This function returns a scanning code to indicate what was scanned.
  51. */
  52. int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
  53. int offs, int quiet)
  54. {
  55. struct ubifs_ch *ch = buf;
  56. uint32_t magic;
  57. magic = le32_to_cpu(ch->magic);
  58. if (magic == 0xFFFFFFFF) {
  59. dbg_scan("hit empty space at LEB %d:%d", lnum, offs);
  60. return SCANNED_EMPTY_SPACE;
  61. }
  62. if (magic != UBIFS_NODE_MAGIC)
  63. return scan_padding_bytes(buf, len);
  64. if (len < UBIFS_CH_SZ)
  65. return SCANNED_GARBAGE;
  66. dbg_scan("scanning %s at LEB %d:%d",
  67. dbg_ntype(ch->node_type), lnum, offs);
  68. if (ubifs_check_node(c, buf, lnum, offs, quiet, 1))
  69. return SCANNED_A_CORRUPT_NODE;
  70. if (ch->node_type == UBIFS_PAD_NODE) {
  71. struct ubifs_pad_node *pad = buf;
  72. int pad_len = le32_to_cpu(pad->pad_len);
  73. int node_len = le32_to_cpu(ch->len);
  74. /* Validate the padding node */
  75. if (pad_len < 0 ||
  76. offs + node_len + pad_len > c->leb_size) {
  77. if (!quiet) {
  78. ubifs_err("bad pad node at LEB %d:%d",
  79. lnum, offs);
  80. ubifs_dump_node(c, pad);
  81. }
  82. return SCANNED_A_BAD_PAD_NODE;
  83. }
  84. /* Make the node pads to 8-byte boundary */
  85. if ((node_len + pad_len) & 7) {
  86. if (!quiet)
  87. ubifs_err("bad padding length %d - %d",
  88. offs, offs + node_len + pad_len);
  89. return SCANNED_A_BAD_PAD_NODE;
  90. }
  91. dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len,
  92. lnum, offs, ALIGN(offs + node_len + pad_len, 8));
  93. return node_len + pad_len;
  94. }
  95. return SCANNED_A_NODE;
  96. }
  97. /**
  98. * ubifs_start_scan - create LEB scanning information at start of scan.
  99. * @c: UBIFS file-system description object
  100. * @lnum: logical eraseblock number
  101. * @offs: offset to start at (usually zero)
  102. * @sbuf: scan buffer (must be c->leb_size)
  103. *
  104. * This function returns %0 on success and a negative error code on failure.
  105. */
  106. struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
  107. int offs, void *sbuf)
  108. {
  109. struct ubifs_scan_leb *sleb;
  110. int err;
  111. dbg_scan("scan LEB %d:%d", lnum, offs);
  112. sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
  113. if (!sleb)
  114. return ERR_PTR(-ENOMEM);
  115. sleb->lnum = lnum;
  116. INIT_LIST_HEAD(&sleb->nodes);
  117. sleb->buf = sbuf;
  118. err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0);
  119. if (err && err != -EBADMSG) {
  120. ubifs_err("cannot read %d bytes from LEB %d:%d, error %d",
  121. c->leb_size - offs, lnum, offs, err);
  122. kfree(sleb);
  123. return ERR_PTR(err);
  124. }
  125. if (err == -EBADMSG)
  126. sleb->ecc = 1;
  127. return sleb;
  128. }
  129. /**
  130. * ubifs_end_scan - update LEB scanning information at end of scan.
  131. * @c: UBIFS file-system description object
  132. * @sleb: scanning information
  133. * @lnum: logical eraseblock number
  134. * @offs: offset to start at (usually zero)
  135. *
  136. * This function returns %0 on success and a negative error code on failure.
  137. */
  138. void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  139. int lnum, int offs)
  140. {
  141. lnum = lnum;
  142. dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
  143. ubifs_assert(offs % c->min_io_size == 0);
  144. sleb->endpt = ALIGN(offs, c->min_io_size);
  145. }
  146. /**
  147. * ubifs_add_snod - add a scanned node to LEB scanning information.
  148. * @c: UBIFS file-system description object
  149. * @sleb: scanning information
  150. * @buf: buffer containing node
  151. * @offs: offset of node on flash
  152. *
  153. * This function returns %0 on success and a negative error code on failure.
  154. */
  155. int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  156. void *buf, int offs)
  157. {
  158. struct ubifs_ch *ch = buf;
  159. struct ubifs_ino_node *ino = buf;
  160. struct ubifs_scan_node *snod;
  161. snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
  162. if (!snod)
  163. return -ENOMEM;
  164. snod->sqnum = le64_to_cpu(ch->sqnum);
  165. snod->type = ch->node_type;
  166. snod->offs = offs;
  167. snod->len = le32_to_cpu(ch->len);
  168. snod->node = buf;
  169. switch (ch->node_type) {
  170. case UBIFS_INO_NODE:
  171. case UBIFS_DENT_NODE:
  172. case UBIFS_XENT_NODE:
  173. case UBIFS_DATA_NODE:
  174. /*
  175. * The key is in the same place in all keyed
  176. * nodes.
  177. */
  178. key_read(c, &ino->key, &snod->key);
  179. break;
  180. default:
  181. invalid_key_init(c, &snod->key);
  182. break;
  183. }
  184. list_add_tail(&snod->list, &sleb->nodes);
  185. sleb->nodes_cnt += 1;
  186. return 0;
  187. }
  188. /**
  189. * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
  190. * @c: UBIFS file-system description object
  191. * @lnum: LEB number of corruption
  192. * @offs: offset of corruption
  193. * @buf: buffer containing corruption
  194. */
  195. void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
  196. void *buf)
  197. {
  198. int len;
  199. ubifs_err("corruption at LEB %d:%d", lnum, offs);
  200. len = c->leb_size - offs;
  201. if (len > 8192)
  202. len = 8192;
  203. ubifs_err("first %d bytes from LEB %d:%d", len, lnum, offs);
  204. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
  205. }
  206. /**
  207. * ubifs_scan - scan a logical eraseblock.
  208. * @c: UBIFS file-system description object
  209. * @lnum: logical eraseblock number
  210. * @offs: offset to start at (usually zero)
  211. * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
  212. * @quiet: print no messages
  213. *
  214. * This function scans LEB number @lnum and returns complete information about
  215. * its contents. Returns the scaned information in case of success and,
  216. * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
  217. * of failure.
  218. *
  219. * If @quiet is non-zero, this function does not print large and scary
  220. * error messages and flash dumps in case of errors.
  221. */
  222. struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
  223. int offs, void *sbuf, int quiet)
  224. {
  225. void *buf = sbuf + offs;
  226. int err, len = c->leb_size - offs;
  227. struct ubifs_scan_leb *sleb;
  228. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  229. if (IS_ERR(sleb))
  230. return sleb;
  231. while (len >= 8) {
  232. struct ubifs_ch *ch = buf;
  233. int node_len, ret;
  234. dbg_scan("look at LEB %d:%d (%d bytes left)",
  235. lnum, offs, len);
  236. cond_resched();
  237. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  238. if (ret > 0) {
  239. /* Padding bytes or a valid padding node */
  240. offs += ret;
  241. buf += ret;
  242. len -= ret;
  243. continue;
  244. }
  245. if (ret == SCANNED_EMPTY_SPACE)
  246. /* Empty space is checked later */
  247. break;
  248. switch (ret) {
  249. case SCANNED_GARBAGE:
  250. ubifs_err("garbage");
  251. goto corrupted;
  252. case SCANNED_A_NODE:
  253. break;
  254. case SCANNED_A_CORRUPT_NODE:
  255. case SCANNED_A_BAD_PAD_NODE:
  256. ubifs_err("bad node");
  257. goto corrupted;
  258. default:
  259. ubifs_err("unknown");
  260. err = -EINVAL;
  261. goto error;
  262. }
  263. err = ubifs_add_snod(c, sleb, buf, offs);
  264. if (err)
  265. goto error;
  266. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  267. offs += node_len;
  268. buf += node_len;
  269. len -= node_len;
  270. }
  271. if (offs % c->min_io_size) {
  272. if (!quiet)
  273. ubifs_err("empty space starts at non-aligned offset %d",
  274. offs);
  275. goto corrupted;
  276. }
  277. ubifs_end_scan(c, sleb, lnum, offs);
  278. for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
  279. if (*(uint32_t *)buf != 0xffffffff)
  280. break;
  281. for (; len; offs++, buf++, len--)
  282. if (*(uint8_t *)buf != 0xff) {
  283. if (!quiet)
  284. ubifs_err("corrupt empty space at LEB %d:%d",
  285. lnum, offs);
  286. goto corrupted;
  287. }
  288. return sleb;
  289. corrupted:
  290. if (!quiet) {
  291. ubifs_scanned_corruption(c, lnum, offs, buf);
  292. ubifs_err("LEB %d scanning failed", lnum);
  293. }
  294. err = -EUCLEAN;
  295. ubifs_scan_destroy(sleb);
  296. return ERR_PTR(err);
  297. error:
  298. ubifs_err("LEB %d scanning failed, error %d", lnum, err);
  299. ubifs_scan_destroy(sleb);
  300. return ERR_PTR(err);
  301. }
  302. /**
  303. * ubifs_scan_destroy - destroy LEB scanning information.
  304. * @sleb: scanning information to free
  305. */
  306. void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
  307. {
  308. struct ubifs_scan_node *node;
  309. struct list_head *head;
  310. head = &sleb->nodes;
  311. while (!list_empty(head)) {
  312. node = list_entry(head->next, struct ubifs_scan_node, list);
  313. list_del(&node->list);
  314. kfree(node);
  315. }
  316. kfree(sleb);
  317. }