yaffs_yaffs1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. continue;
  151. }
  152. /* chunk_id == 0, so it is an ObjectHeader.
  153. * Thus, we read in the object header and make the object
  154. */
  155. yaffs_set_chunk_bit(dev, blk, c);
  156. bi->pages_in_use++;
  157. result = yaffs_rd_chunk_tags_nand(dev, chunk,
  158. chunk_data,
  159. NULL);
  160. oh = (struct yaffs_obj_hdr *)chunk_data;
  161. in = yaffs_find_by_number(dev, tags.obj_id);
  162. if (in && in->variant_type != oh->type) {
  163. /* This should not happen, but somehow
  164. * Wev'e ended up with an obj_id that has been reused but not yet
  165. * deleted, and worse still it has changed type. Delete the old object.
  166. */
  167. yaffs_del_obj(in);
  168. in = 0;
  169. }
  170. in = yaffs_find_or_create_by_number(dev,
  171. tags.obj_id,
  172. oh->type);
  173. if (!in)
  174. alloc_failed = 1;
  175. if (in && oh->shadows_obj > 0) {
  176. struct yaffs_shadow_fixer *fixer;
  177. fixer =
  178. kmalloc(sizeof
  179. (struct yaffs_shadow_fixer),
  180. GFP_NOFS);
  181. if (fixer) {
  182. fixer->next = shadow_fixers;
  183. shadow_fixers = fixer;
  184. fixer->obj_id = tags.obj_id;
  185. fixer->shadowed_id =
  186. oh->shadows_obj;
  187. yaffs_trace(YAFFS_TRACE_SCAN,
  188. " Shadow fixer: %d shadows %d",
  189. fixer->obj_id,
  190. fixer->shadowed_id);
  191. }
  192. }
  193. if (in && in->valid) {
  194. /* We have already filled this one. We have a
  195. *duplicate and need to resolve it.
  196. */
  197. unsigned existing_serial = in->serial;
  198. unsigned new_serial =
  199. tags.serial_number;
  200. if (((existing_serial + 1) & 3) ==
  201. new_serial) {
  202. /* Use new one - destroy the exisiting one */
  203. yaffs_chunk_del(dev,
  204. in->hdr_chunk,
  205. 1, __LINE__);
  206. in->valid = 0;
  207. } else {
  208. /* Use existing - destroy this one. */
  209. yaffs_chunk_del(dev, chunk, 1,
  210. __LINE__);
  211. }
  212. }
  213. if (in && !in->valid &&
  214. (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  215. tags.obj_id ==
  216. YAFFS_OBJECTID_LOSTNFOUND)) {
  217. /* We only load some info, don't fiddle with directory structure */
  218. in->valid = 1;
  219. in->variant_type = oh->type;
  220. in->yst_mode = oh->yst_mode;
  221. yaffs_load_attribs(in, oh);
  222. in->hdr_chunk = chunk;
  223. in->serial = tags.serial_number;
  224. } else if (in && !in->valid) {
  225. /* we need to load this info */
  226. in->valid = 1;
  227. in->variant_type = oh->type;
  228. in->yst_mode = oh->yst_mode;
  229. yaffs_load_attribs(in, oh);
  230. in->hdr_chunk = chunk;
  231. in->serial = tags.serial_number;
  232. yaffs_set_obj_name_from_oh(in, oh);
  233. in->dirty = 0;
  234. /* directory stuff...
  235. * hook up to parent
  236. */
  237. parent =
  238. yaffs_find_or_create_by_number
  239. (dev, oh->parent_obj_id,
  240. YAFFS_OBJECT_TYPE_DIRECTORY);
  241. if (!parent)
  242. alloc_failed = 1;
  243. if (parent && parent->variant_type ==
  244. YAFFS_OBJECT_TYPE_UNKNOWN) {
  245. /* Set up as a directory */
  246. parent->variant_type =
  247. YAFFS_OBJECT_TYPE_DIRECTORY;
  248. INIT_LIST_HEAD(&parent->
  249. variant.dir_variant.children);
  250. } else if (!parent
  251. || parent->variant_type !=
  252. YAFFS_OBJECT_TYPE_DIRECTORY) {
  253. /* Hoosterman, another problem....
  254. * We're trying to use a non-directory as a directory
  255. */
  256. yaffs_trace(YAFFS_TRACE_ERROR,
  257. "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  258. );
  259. parent = dev->lost_n_found;
  260. }
  261. yaffs_add_obj_to_dir(parent, in);
  262. if (0 && (parent == dev->del_dir ||
  263. parent ==
  264. dev->unlinked_dir)) {
  265. /* If it is unlinked at start up then it wants deleting */
  266. in->deleted = 1;
  267. dev->n_deleted_files++;
  268. }
  269. /* Note re hardlinks.
  270. * Since we might scan a hardlink before its equivalent object is scanned
  271. * we put them all in a list.
  272. * After scanning is complete, we should have all the objects, so we run
  273. * through this list and fix up all the chains.
  274. */
  275. switch (in->variant_type) {
  276. case YAFFS_OBJECT_TYPE_UNKNOWN:
  277. /* Todo got a problem */
  278. break;
  279. case YAFFS_OBJECT_TYPE_FILE:
  280. if (dev->param.
  281. use_header_file_size)
  282. in->variant.
  283. file_variant.file_size
  284. = oh->file_size;
  285. break;
  286. case YAFFS_OBJECT_TYPE_HARDLINK:
  287. in->variant.
  288. hardlink_variant.equiv_id =
  289. oh->equiv_id;
  290. in->hard_links.next =
  291. (struct list_head *)
  292. hard_list;
  293. hard_list = in;
  294. break;
  295. case YAFFS_OBJECT_TYPE_DIRECTORY:
  296. /* Do nothing */
  297. break;
  298. case YAFFS_OBJECT_TYPE_SPECIAL:
  299. /* Do nothing */
  300. break;
  301. case YAFFS_OBJECT_TYPE_SYMLINK:
  302. in->variant.symlink_variant.
  303. alias =
  304. yaffs_clone_str(oh->alias);
  305. if (!in->variant.
  306. symlink_variant.alias)
  307. alloc_failed = 1;
  308. break;
  309. }
  310. }
  311. }
  312. if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  313. /* If we got this far while scanning, then the block is fully allocated. */
  314. state = YAFFS_BLOCK_STATE_FULL;
  315. }
  316. if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  317. /* If the block was partially allocated then treat it as fully allocated. */
  318. state = YAFFS_BLOCK_STATE_FULL;
  319. dev->alloc_block = -1;
  320. }
  321. bi->block_state = state;
  322. /* Now let's see if it was dirty */
  323. if (bi->pages_in_use == 0 &&
  324. !bi->has_shrink_hdr &&
  325. bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  326. yaffs_block_became_dirty(dev, blk);
  327. }
  328. }
  329. /* Ok, we've done all the scanning.
  330. * Fix up the hard link chains.
  331. * We should now have scanned all the objects, now it's time to add these
  332. * hardlinks.
  333. */
  334. yaffs_link_fixup(dev, hard_list);
  335. /* Fix up any shadowed objects */
  336. {
  337. struct yaffs_shadow_fixer *fixer;
  338. struct yaffs_obj *obj;
  339. while (shadow_fixers) {
  340. fixer = shadow_fixers;
  341. shadow_fixers = fixer->next;
  342. /* Complete the rename transaction by deleting the shadowed object
  343. * then setting the object header to unshadowed.
  344. */
  345. obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  346. if (obj)
  347. yaffs_del_obj(obj);
  348. obj = yaffs_find_by_number(dev, fixer->obj_id);
  349. if (obj)
  350. yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  351. kfree(fixer);
  352. }
  353. }
  354. yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  355. if (alloc_failed)
  356. return YAFFS_FAIL;
  357. yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
  358. return YAFFS_OK;
  359. }