scan.c 9.5 KB

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