scan.c 9.4 KB

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