scan.c 8.9 KB

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