scan.c 9.0 KB

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