yaffs_yaffs1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2010 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "yaffs_yaffs1.h"
  14. #include "yportenv.h"
  15. #include "yaffs_trace.h"
  16. #include "yaffs_bitmap.h"
  17. #include "yaffs_getblockinfo.h"
  18. #include "yaffs_nand.h"
  19. #include "yaffs_attribs.h"
  20. int yaffs1_scan(struct yaffs_dev *dev)
  21. {
  22. struct yaffs_ext_tags tags;
  23. int blk;
  24. int result;
  25. int chunk;
  26. int c;
  27. int deleted;
  28. enum yaffs_block_state state;
  29. struct yaffs_obj *hard_list = NULL;
  30. struct yaffs_block_info *bi;
  31. u32 seq_number;
  32. struct yaffs_obj_hdr *oh;
  33. struct yaffs_obj *in;
  34. struct yaffs_obj *parent;
  35. int alloc_failed = 0;
  36. struct yaffs_shadow_fixer *shadow_fixers = NULL;
  37. u8 *chunk_data;
  38. yaffs_trace(YAFFS_TRACE_SCAN,
  39. "yaffs1_scan starts intstartblk %d intendblk %d...",
  40. dev->internal_start_block, dev->internal_end_block);
  41. chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  42. dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  43. /* Scan all the blocks to determine their state */
  44. bi = dev->block_info;
  45. for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  46. blk++) {
  47. yaffs_clear_chunk_bits(dev, blk);
  48. bi->pages_in_use = 0;
  49. bi->soft_del_pages = 0;
  50. yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  51. bi->block_state = state;
  52. bi->seq_number = seq_number;
  53. if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  54. bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  55. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  56. "Block scanning block %d state %d seq %d",
  57. blk, state, seq_number);
  58. if (state == YAFFS_BLOCK_STATE_DEAD) {
  59. yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  60. "block %d is bad", blk);
  61. } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  62. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  63. dev->n_erased_blocks++;
  64. dev->n_free_chunks += dev->param.chunks_per_block;
  65. }
  66. bi++;
  67. }
  68. /* For each block.... */
  69. for (blk = dev->internal_start_block;
  70. !alloc_failed && blk <= dev->internal_end_block; blk++) {
  71. cond_resched();
  72. bi = yaffs_get_block_info(dev, blk);
  73. state = bi->block_state;
  74. deleted = 0;
  75. /* For each chunk in each block that needs scanning.... */
  76. for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
  77. state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
  78. /* Read the tags and decide what to do */
  79. chunk = blk * dev->param.chunks_per_block + c;
  80. result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  81. &tags);
  82. /* Let's have a good look at this chunk... */
  83. if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
  84. || tags.is_deleted) {
  85. /* YAFFS1 only...
  86. * A deleted chunk
  87. */
  88. deleted++;
  89. dev->n_free_chunks++;
  90. /*T((" %d %d deleted\n",blk,c)); */
  91. } else if (!tags.chunk_used) {
  92. /* An unassigned chunk in the block
  93. * This means that either the block is empty or
  94. * this is the one being allocated from
  95. */
  96. if (c == 0) {
  97. /* We're looking at the first chunk in the block so the block is unused */
  98. state = YAFFS_BLOCK_STATE_EMPTY;
  99. dev->n_erased_blocks++;
  100. } else {
  101. /* this is the block being allocated from */
  102. yaffs_trace(YAFFS_TRACE_SCAN,
  103. " Allocating from %d %d",
  104. blk, c);
  105. state = YAFFS_BLOCK_STATE_ALLOCATING;
  106. dev->alloc_block = blk;
  107. dev->alloc_page = c;
  108. dev->alloc_block_finder = blk;
  109. /* Set block finder here to encourage the allocator to go forth from here. */
  110. }
  111. dev->n_free_chunks +=
  112. (dev->param.chunks_per_block - c);
  113. } else if (tags.chunk_id > 0) {
  114. /* chunk_id > 0 so it is a data chunk... */
  115. unsigned int endpos;
  116. yaffs_set_chunk_bit(dev, blk, c);
  117. bi->pages_in_use++;
  118. in = yaffs_find_or_create_by_number(dev,
  119. tags.obj_id,
  120. YAFFS_OBJECT_TYPE_FILE);
  121. /* PutChunkIntoFile checks for a clash (two data chunks with
  122. * the same chunk_id).
  123. */
  124. if (!in)
  125. alloc_failed = 1;
  126. if (in) {
  127. if (!yaffs_put_chunk_in_file
  128. (in, tags.chunk_id, chunk, 1))
  129. alloc_failed = 1;
  130. }
  131. endpos =
  132. (tags.chunk_id -
  133. 1) * dev->data_bytes_per_chunk +
  134. tags.n_bytes;
  135. if (in
  136. && in->variant_type ==
  137. YAFFS_OBJECT_TYPE_FILE
  138. && in->variant.file_variant.scanned_size <
  139. endpos) {
  140. in->variant.file_variant.scanned_size =
  141. endpos;
  142. if (!dev->param.use_header_file_size) {
  143. in->variant.
  144. file_variant.file_size =
  145. in->variant.
  146. file_variant.scanned_size;
  147. }
  148. }
  149. /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
  150. } else {
  151. /* chunk_id == 0, so it is an ObjectHeader.
  152. * Thus, we read in the object header and make the object
  153. */
  154. yaffs_set_chunk_bit(dev, blk, c);
  155. bi->pages_in_use++;
  156. result = yaffs_rd_chunk_tags_nand(dev, chunk,
  157. chunk_data,
  158. NULL);
  159. oh = (struct yaffs_obj_hdr *)chunk_data;
  160. in = yaffs_find_by_number(dev, tags.obj_id);
  161. if (in && in->variant_type != oh->type) {
  162. /* This should not happen, but somehow
  163. * Wev'e ended up with an obj_id that has been reused but not yet
  164. * deleted, and worse still it has changed type. Delete the old object.
  165. */
  166. yaffs_del_obj(in);
  167. in = 0;
  168. }
  169. in = yaffs_find_or_create_by_number(dev,
  170. tags.obj_id,
  171. oh->type);
  172. if (!in)
  173. alloc_failed = 1;
  174. if (in && oh->shadows_obj > 0) {
  175. struct yaffs_shadow_fixer *fixer;
  176. fixer =
  177. kmalloc(sizeof
  178. (struct yaffs_shadow_fixer),
  179. GFP_NOFS);
  180. if (fixer) {
  181. fixer->next = shadow_fixers;
  182. shadow_fixers = fixer;
  183. fixer->obj_id = tags.obj_id;
  184. fixer->shadowed_id =
  185. oh->shadows_obj;
  186. yaffs_trace(YAFFS_TRACE_SCAN,
  187. " Shadow fixer: %d shadows %d",
  188. fixer->obj_id,
  189. fixer->shadowed_id);
  190. }
  191. }
  192. if (in && in->valid) {
  193. /* We have already filled this one. We have a duplicate and need to resolve it. */
  194. unsigned existing_serial = in->serial;
  195. unsigned new_serial =
  196. tags.serial_number;
  197. if (((existing_serial + 1) & 3) ==
  198. new_serial) {
  199. /* Use new one - destroy the exisiting one */
  200. yaffs_chunk_del(dev,
  201. in->hdr_chunk,
  202. 1, __LINE__);
  203. in->valid = 0;
  204. } else {
  205. /* Use existing - destroy this one. */
  206. yaffs_chunk_del(dev, chunk, 1,
  207. __LINE__);
  208. }
  209. }
  210. if (in && !in->valid &&
  211. (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  212. tags.obj_id ==
  213. YAFFS_OBJECTID_LOSTNFOUND)) {
  214. /* We only load some info, don't fiddle with directory structure */
  215. in->valid = 1;
  216. in->variant_type = oh->type;
  217. in->yst_mode = oh->yst_mode;
  218. yaffs_load_attribs(in, oh);
  219. in->hdr_chunk = chunk;
  220. in->serial = tags.serial_number;
  221. } else if (in && !in->valid) {
  222. /* we need to load this info */
  223. in->valid = 1;
  224. in->variant_type = oh->type;
  225. in->yst_mode = oh->yst_mode;
  226. yaffs_load_attribs(in, oh);
  227. in->hdr_chunk = chunk;
  228. in->serial = tags.serial_number;
  229. yaffs_set_obj_name_from_oh(in, oh);
  230. in->dirty = 0;
  231. /* directory stuff...
  232. * hook up to parent
  233. */
  234. parent =
  235. yaffs_find_or_create_by_number
  236. (dev, oh->parent_obj_id,
  237. YAFFS_OBJECT_TYPE_DIRECTORY);
  238. if (!parent)
  239. alloc_failed = 1;
  240. if (parent && parent->variant_type ==
  241. YAFFS_OBJECT_TYPE_UNKNOWN) {
  242. /* Set up as a directory */
  243. parent->variant_type =
  244. YAFFS_OBJECT_TYPE_DIRECTORY;
  245. INIT_LIST_HEAD(&parent->
  246. variant.dir_variant.children);
  247. } else if (!parent
  248. || parent->variant_type !=
  249. YAFFS_OBJECT_TYPE_DIRECTORY) {
  250. /* Hoosterman, another problem....
  251. * We're trying to use a non-directory as a directory
  252. */
  253. yaffs_trace(YAFFS_TRACE_ERROR,
  254. "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  255. );
  256. parent = dev->lost_n_found;
  257. }
  258. yaffs_add_obj_to_dir(parent, in);
  259. if (0 && (parent == dev->del_dir ||
  260. parent ==
  261. dev->unlinked_dir)) {
  262. in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
  263. dev->n_deleted_files++;
  264. }
  265. /* Note re hardlinks.
  266. * Since we might scan a hardlink before its equivalent object is scanned
  267. * we put them all in a list.
  268. * After scanning is complete, we should have all the objects, so we run through this
  269. * list and fix up all the chains.
  270. */
  271. switch (in->variant_type) {
  272. case YAFFS_OBJECT_TYPE_UNKNOWN:
  273. /* Todo got a problem */
  274. break;
  275. case YAFFS_OBJECT_TYPE_FILE:
  276. if (dev->param.
  277. use_header_file_size)
  278. in->variant.
  279. file_variant.file_size
  280. = oh->file_size;
  281. break;
  282. case YAFFS_OBJECT_TYPE_HARDLINK:
  283. in->variant.
  284. hardlink_variant.equiv_id =
  285. oh->equiv_id;
  286. in->hard_links.next =
  287. (struct list_head *)
  288. hard_list;
  289. hard_list = in;
  290. break;
  291. case YAFFS_OBJECT_TYPE_DIRECTORY:
  292. /* Do nothing */
  293. break;
  294. case YAFFS_OBJECT_TYPE_SPECIAL:
  295. /* Do nothing */
  296. break;
  297. case YAFFS_OBJECT_TYPE_SYMLINK:
  298. in->variant.symlink_variant.
  299. alias =
  300. yaffs_clone_str(oh->alias);
  301. if (!in->variant.
  302. symlink_variant.alias)
  303. alloc_failed = 1;
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  310. /* If we got this far while scanning, then the block is fully allocated. */
  311. state = YAFFS_BLOCK_STATE_FULL;
  312. }
  313. if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  314. /* If the block was partially allocated then treat it as fully allocated. */
  315. state = YAFFS_BLOCK_STATE_FULL;
  316. dev->alloc_block = -1;
  317. }
  318. bi->block_state = state;
  319. /* Now let's see if it was dirty */
  320. if (bi->pages_in_use == 0 &&
  321. !bi->has_shrink_hdr &&
  322. bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  323. yaffs_block_became_dirty(dev, blk);
  324. }
  325. }
  326. /* Ok, we've done all the scanning.
  327. * Fix up the hard link chains.
  328. * We should now have scanned all the objects, now it's time to add these
  329. * hardlinks.
  330. */
  331. yaffs_link_fixup(dev, hard_list);
  332. /* Fix up any shadowed objects */
  333. {
  334. struct yaffs_shadow_fixer *fixer;
  335. struct yaffs_obj *obj;
  336. while (shadow_fixers) {
  337. fixer = shadow_fixers;
  338. shadow_fixers = fixer->next;
  339. /* Complete the rename transaction by deleting the shadowed object
  340. * then setting the object header to unshadowed.
  341. */
  342. obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  343. if (obj)
  344. yaffs_del_obj(obj);
  345. obj = yaffs_find_by_number(dev, fixer->obj_id);
  346. if (obj)
  347. yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  348. kfree(fixer);
  349. }
  350. }
  351. yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  352. if (alloc_failed)
  353. return YAFFS_FAIL;
  354. yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
  355. return YAFFS_OK;
  356. }