scan.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation
  6. *
  7. * Authors: Adrian Hunter
  8. * Artem Bityutskiy (Битюцкий Артём)
  9. */
  10. /*
  11. * This file implements the scan which is a general-purpose function for
  12. * determining what nodes are in an eraseblock. The scan is used to replay the
  13. * journal, to do garbage collection. for the TNC in-the-gaps method, and by
  14. * debugging functions.
  15. */
  16. #ifdef __UBOOT__
  17. #include <hexdump.h>
  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(c, "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(c, "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 the scanned information on success and a negative error
  105. * code on failure.
  106. */
  107. struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
  108. int offs, void *sbuf)
  109. {
  110. struct ubifs_scan_leb *sleb;
  111. int err;
  112. dbg_scan("scan LEB %d:%d", lnum, offs);
  113. sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
  114. if (!sleb)
  115. return ERR_PTR(-ENOMEM);
  116. sleb->lnum = lnum;
  117. INIT_LIST_HEAD(&sleb->nodes);
  118. sleb->buf = sbuf;
  119. err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0);
  120. if (err && err != -EBADMSG) {
  121. ubifs_err(c, "cannot read %d bytes from LEB %d:%d, error %d",
  122. c->leb_size - offs, lnum, offs, err);
  123. kfree(sleb);
  124. return ERR_PTR(err);
  125. }
  126. /*
  127. * Note, we ignore integrity errors (EBASMSG) because all the nodes are
  128. * protected by CRC checksums.
  129. */
  130. return sleb;
  131. }
  132. /**
  133. * ubifs_end_scan - update LEB scanning information at end of scan.
  134. * @c: UBIFS file-system description object
  135. * @sleb: scanning information
  136. * @lnum: logical eraseblock number
  137. * @offs: offset to start at (usually zero)
  138. */
  139. void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  140. int lnum, int offs)
  141. {
  142. lnum = lnum;
  143. dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
  144. ubifs_assert(offs % c->min_io_size == 0);
  145. sleb->endpt = ALIGN(offs, c->min_io_size);
  146. }
  147. /**
  148. * ubifs_add_snod - add a scanned node to LEB scanning information.
  149. * @c: UBIFS file-system description object
  150. * @sleb: scanning information
  151. * @buf: buffer containing node
  152. * @offs: offset of node on flash
  153. *
  154. * This function returns %0 on success and a negative error code on failure.
  155. */
  156. int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  157. void *buf, int offs)
  158. {
  159. struct ubifs_ch *ch = buf;
  160. struct ubifs_ino_node *ino = buf;
  161. struct ubifs_scan_node *snod;
  162. snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
  163. if (!snod)
  164. return -ENOMEM;
  165. snod->sqnum = le64_to_cpu(ch->sqnum);
  166. snod->type = ch->node_type;
  167. snod->offs = offs;
  168. snod->len = le32_to_cpu(ch->len);
  169. snod->node = buf;
  170. switch (ch->node_type) {
  171. case UBIFS_INO_NODE:
  172. case UBIFS_DENT_NODE:
  173. case UBIFS_XENT_NODE:
  174. case UBIFS_DATA_NODE:
  175. /*
  176. * The key is in the same place in all keyed
  177. * nodes.
  178. */
  179. key_read(c, &ino->key, &snod->key);
  180. break;
  181. default:
  182. invalid_key_init(c, &snod->key);
  183. break;
  184. }
  185. list_add_tail(&snod->list, &sleb->nodes);
  186. sleb->nodes_cnt += 1;
  187. return 0;
  188. }
  189. /**
  190. * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
  191. * @c: UBIFS file-system description object
  192. * @lnum: LEB number of corruption
  193. * @offs: offset of corruption
  194. * @buf: buffer containing corruption
  195. */
  196. void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
  197. void *buf)
  198. {
  199. int len;
  200. ubifs_err(c, "corruption at LEB %d:%d", lnum, offs);
  201. len = c->leb_size - offs;
  202. if (len > 8192)
  203. len = 8192;
  204. ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs);
  205. print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
  206. }
  207. /**
  208. * ubifs_scan - scan a logical eraseblock.
  209. * @c: UBIFS file-system description object
  210. * @lnum: logical eraseblock number
  211. * @offs: offset to start at (usually zero)
  212. * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
  213. * @quiet: print no messages
  214. *
  215. * This function scans LEB number @lnum and returns complete information about
  216. * its contents. Returns the scanned information in case of success and,
  217. * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
  218. * of failure.
  219. *
  220. * If @quiet is non-zero, this function does not print large and scary
  221. * error messages and flash dumps in case of errors.
  222. */
  223. struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
  224. int offs, void *sbuf, int quiet)
  225. {
  226. void *buf = sbuf + offs;
  227. int err, len = c->leb_size - offs;
  228. struct ubifs_scan_leb *sleb;
  229. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  230. if (IS_ERR(sleb))
  231. return sleb;
  232. while (len >= 8) {
  233. struct ubifs_ch *ch = buf;
  234. int node_len, ret;
  235. dbg_scan("look at LEB %d:%d (%d bytes left)",
  236. lnum, offs, len);
  237. cond_resched();
  238. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  239. if (ret > 0) {
  240. /* Padding bytes or a valid padding node */
  241. offs += ret;
  242. buf += ret;
  243. len -= ret;
  244. continue;
  245. }
  246. if (ret == SCANNED_EMPTY_SPACE)
  247. /* Empty space is checked later */
  248. break;
  249. switch (ret) {
  250. case SCANNED_GARBAGE:
  251. ubifs_err(c, "garbage");
  252. goto corrupted;
  253. case SCANNED_A_NODE:
  254. break;
  255. case SCANNED_A_CORRUPT_NODE:
  256. case SCANNED_A_BAD_PAD_NODE:
  257. ubifs_err(c, "bad node");
  258. goto corrupted;
  259. default:
  260. ubifs_err(c, "unknown");
  261. err = -EINVAL;
  262. goto error;
  263. }
  264. err = ubifs_add_snod(c, sleb, buf, offs);
  265. if (err)
  266. goto error;
  267. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  268. offs += node_len;
  269. buf += node_len;
  270. len -= node_len;
  271. }
  272. if (offs % c->min_io_size) {
  273. if (!quiet)
  274. ubifs_err(c, "empty space starts at non-aligned offset %d",
  275. offs);
  276. goto corrupted;
  277. }
  278. ubifs_end_scan(c, sleb, lnum, offs);
  279. for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
  280. if (*(uint32_t *)buf != 0xffffffff)
  281. break;
  282. for (; len; offs++, buf++, len--)
  283. if (*(uint8_t *)buf != 0xff) {
  284. if (!quiet)
  285. ubifs_err(c, "corrupt empty space at LEB %d:%d",
  286. lnum, offs);
  287. goto corrupted;
  288. }
  289. return sleb;
  290. corrupted:
  291. if (!quiet) {
  292. ubifs_scanned_corruption(c, lnum, offs, buf);
  293. ubifs_err(c, "LEB %d scanning failed", lnum);
  294. }
  295. err = -EUCLEAN;
  296. ubifs_scan_destroy(sleb);
  297. return ERR_PTR(err);
  298. error:
  299. ubifs_err(c, "LEB %d scanning failed, error %d", lnum, err);
  300. ubifs_scan_destroy(sleb);
  301. return ERR_PTR(err);
  302. }
  303. /**
  304. * ubifs_scan_destroy - destroy LEB scanning information.
  305. * @sleb: scanning information to free
  306. */
  307. void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
  308. {
  309. struct ubifs_scan_node *node;
  310. struct list_head *head;
  311. head = &sleb->nodes;
  312. while (!list_empty(head)) {
  313. node = list_entry(head->next, struct ubifs_scan_node, list);
  314. list_del(&node->list);
  315. kfree(node);
  316. }
  317. kfree(sleb);
  318. }