scan.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. }
  97. return SCANNED_A_BAD_PAD_NODE;
  98. }
  99. dbg_scan("%d bytes padded, offset now %d",
  100. pad_len, 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 %0 on success and a negative error code on failure.
  113. */
  114. struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
  115. int offs, void *sbuf)
  116. {
  117. struct ubifs_scan_leb *sleb;
  118. int err;
  119. dbg_scan("scan LEB %d:%d", lnum, offs);
  120. sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
  121. if (!sleb)
  122. return ERR_PTR(-ENOMEM);
  123. sleb->lnum = lnum;
  124. INIT_LIST_HEAD(&sleb->nodes);
  125. sleb->buf = sbuf;
  126. err = ubi_read(c->ubi, lnum, sbuf + offs, offs, c->leb_size - offs);
  127. if (err && err != -EBADMSG) {
  128. ubifs_err("cannot read %d bytes from LEB %d:%d,"
  129. " error %d", c->leb_size - offs, lnum, offs, err);
  130. kfree(sleb);
  131. return ERR_PTR(err);
  132. }
  133. if (err == -EBADMSG)
  134. sleb->ecc = 1;
  135. return sleb;
  136. }
  137. /**
  138. * ubifs_end_scan - update LEB scanning information at end of scan.
  139. * @c: UBIFS file-system description object
  140. * @sleb: scanning information
  141. * @lnum: logical eraseblock number
  142. * @offs: offset to start at (usually zero)
  143. *
  144. * This function returns %0 on success and a negative error code on failure.
  145. */
  146. void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  147. int lnum, int offs)
  148. {
  149. lnum = lnum;
  150. dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
  151. ubifs_assert(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 = kzalloc(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. case UBIFS_TRUN_NODE:
  183. /*
  184. * The key is in the same place in all keyed
  185. * nodes.
  186. */
  187. key_read(c, &ino->key, &snod->key);
  188. break;
  189. }
  190. list_add_tail(&snod->list, &sleb->nodes);
  191. sleb->nodes_cnt += 1;
  192. return 0;
  193. }
  194. /**
  195. * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
  196. * @c: UBIFS file-system description object
  197. * @lnum: LEB number of corruption
  198. * @offs: offset of corruption
  199. * @buf: buffer containing corruption
  200. */
  201. void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
  202. void *buf)
  203. {
  204. int len;
  205. ubifs_err("corrupted data at LEB %d:%d", lnum, offs);
  206. if (dbg_failure_mode)
  207. return;
  208. len = c->leb_size - offs;
  209. if (len > 4096)
  210. len = 4096;
  211. dbg_err("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 c->leb_size)
  220. *
  221. * This function scans LEB number @lnum and returns complete information about
  222. * its contents. Returns an error code in case of failure.
  223. */
  224. struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
  225. int offs, void *sbuf)
  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, 0);
  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. dbg_err("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. dbg_err("bad node");
  259. goto corrupted;
  260. default:
  261. dbg_err("unknown");
  262. goto corrupted;
  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. goto corrupted;
  274. ubifs_end_scan(c, sleb, lnum, offs);
  275. for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
  276. if (*(uint32_t *)buf != 0xffffffff)
  277. break;
  278. for (; len; offs++, buf++, len--)
  279. if (*(uint8_t *)buf != 0xff) {
  280. ubifs_err("corrupt empty space at LEB %d:%d",
  281. lnum, offs);
  282. goto corrupted;
  283. }
  284. return sleb;
  285. corrupted:
  286. ubifs_scanned_corruption(c, lnum, offs, buf);
  287. err = -EUCLEAN;
  288. error:
  289. ubifs_err("LEB %d scanning failed", lnum);
  290. ubifs_scan_destroy(sleb);
  291. return ERR_PTR(err);
  292. }
  293. /**
  294. * ubifs_scan_destroy - destroy LEB scanning information.
  295. * @sleb: scanning information to free
  296. */
  297. void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
  298. {
  299. struct ubifs_scan_node *node;
  300. struct list_head *head;
  301. head = &sleb->nodes;
  302. while (!list_empty(head)) {
  303. node = list_entry(head->next, struct ubifs_scan_node, list);
  304. list_del(&node->list);
  305. kfree(node);
  306. }
  307. kfree(sleb);
  308. }