yaffs_yaffs1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 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 chunk;
  25. int c;
  26. int deleted;
  27. enum yaffs_block_state state;
  28. LIST_HEAD(hard_list);
  29. struct yaffs_block_info *bi;
  30. u32 seq_number;
  31. struct yaffs_obj_hdr *oh;
  32. struct yaffs_obj *in;
  33. struct yaffs_obj *parent;
  34. int alloc_failed = 0;
  35. struct yaffs_shadow_fixer *shadow_fixers = NULL;
  36. u8 *chunk_data;
  37. yaffs_trace(YAFFS_TRACE_SCAN,
  38. "yaffs1_scan starts intstartblk %d intendblk %d...",
  39. dev->internal_start_block, dev->internal_end_block);
  40. chunk_data = yaffs_get_temp_buffer(dev);
  41. dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  42. /* Scan all the blocks to determine their state */
  43. bi = dev->block_info;
  44. for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  45. blk++) {
  46. yaffs_clear_chunk_bits(dev, blk);
  47. bi->pages_in_use = 0;
  48. bi->soft_del_pages = 0;
  49. yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  50. bi->block_state = state;
  51. bi->seq_number = seq_number;
  52. if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  53. bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  54. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  55. "Block scanning block %d state %d seq %d",
  56. blk, state, seq_number);
  57. if (state == YAFFS_BLOCK_STATE_DEAD) {
  58. yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  59. "block %d is bad", blk);
  60. } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  61. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  62. dev->n_erased_blocks++;
  63. dev->n_free_chunks += dev->param.chunks_per_block;
  64. }
  65. bi++;
  66. }
  67. /* For each block.... */
  68. for (blk = dev->internal_start_block;
  69. !alloc_failed && blk <= dev->internal_end_block; blk++) {
  70. cond_resched();
  71. bi = yaffs_get_block_info(dev, blk);
  72. state = bi->block_state;
  73. deleted = 0;
  74. /* For each chunk in each block that needs scanning.... */
  75. for (c = 0;
  76. !alloc_failed && c < dev->param.chunks_per_block &&
  77. state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
  78. /* Read the tags and decide what to do */
  79. chunk = blk * dev->param.chunks_per_block + c;
  80. yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
  81. /* Let's have a good look at this chunk... */
  82. if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
  83. tags.is_deleted) {
  84. /* YAFFS1 only...
  85. * A deleted chunk
  86. */
  87. deleted++;
  88. dev->n_free_chunks++;
  89. } else if (!tags.chunk_used) {
  90. /* An unassigned chunk in the block
  91. * This means that either the block is empty or
  92. * this is the one being allocated from
  93. */
  94. if (c == 0) {
  95. /* We're looking at the first chunk in
  96. *the block so the block is unused */
  97. state = YAFFS_BLOCK_STATE_EMPTY;
  98. dev->n_erased_blocks++;
  99. } else {
  100. /* this is the block being allocated */
  101. yaffs_trace(YAFFS_TRACE_SCAN,
  102. " Allocating from %d %d",
  103. blk, c);
  104. state = YAFFS_BLOCK_STATE_ALLOCATING;
  105. dev->alloc_block = blk;
  106. dev->alloc_page = c;
  107. dev->alloc_block_finder = blk;
  108. }
  109. dev->n_free_chunks +=
  110. (dev->param.chunks_per_block - c);
  111. } else if (tags.chunk_id > 0) {
  112. /* chunk_id > 0 so it is a data chunk... */
  113. unsigned int endpos;
  114. yaffs_set_chunk_bit(dev, blk, c);
  115. bi->pages_in_use++;
  116. in = yaffs_find_or_create_by_number(dev,
  117. tags.obj_id,
  118. YAFFS_OBJECT_TYPE_FILE);
  119. /* PutChunkIntoFile checks for a clash
  120. * (two data chunks with the same chunk_id).
  121. */
  122. if (!in)
  123. alloc_failed = 1;
  124. if (in) {
  125. if (!yaffs_put_chunk_in_file
  126. (in, tags.chunk_id, chunk, 1))
  127. alloc_failed = 1;
  128. }
  129. endpos =
  130. (tags.chunk_id - 1) *
  131. dev->data_bytes_per_chunk +
  132. tags.n_bytes;
  133. if (in &&
  134. in->variant_type ==
  135. YAFFS_OBJECT_TYPE_FILE &&
  136. in->variant.file_variant.scanned_size <
  137. endpos) {
  138. in->variant.file_variant.scanned_size =
  139. endpos;
  140. if (!dev->param.use_header_file_size) {
  141. in->variant.
  142. file_variant.file_size =
  143. in->variant.
  144. file_variant.scanned_size;
  145. }
  146. }
  147. } else {
  148. /* chunk_id == 0, so it is an ObjectHeader.
  149. * Make the object
  150. */
  151. yaffs_set_chunk_bit(dev, blk, c);
  152. bi->pages_in_use++;
  153. yaffs_rd_chunk_tags_nand(dev, chunk,
  154. chunk_data, NULL);
  155. oh = (struct yaffs_obj_hdr *)chunk_data;
  156. in = yaffs_find_by_number(dev, tags.obj_id);
  157. if (in && in->variant_type != oh->type) {
  158. /* This should not happen, but somehow
  159. * Wev'e ended up with an obj_id that
  160. * has been reused but not yet deleted,
  161. * and worse still it has changed type.
  162. * Delete the old object.
  163. */
  164. yaffs_del_obj(in);
  165. in = NULL;
  166. }
  167. in = yaffs_find_or_create_by_number(dev,
  168. tags.obj_id,
  169. oh->type);
  170. if (!in)
  171. alloc_failed = 1;
  172. if (in && oh->shadows_obj > 0) {
  173. struct yaffs_shadow_fixer *fixer;
  174. fixer =
  175. kmalloc(sizeof
  176. (struct yaffs_shadow_fixer),
  177. GFP_NOFS);
  178. if (fixer) {
  179. fixer->next = shadow_fixers;
  180. shadow_fixers = fixer;
  181. fixer->obj_id = tags.obj_id;
  182. fixer->shadowed_id =
  183. oh->shadows_obj;
  184. yaffs_trace(YAFFS_TRACE_SCAN,
  185. " Shadow fixer: %d shadows %d",
  186. fixer->obj_id,
  187. fixer->shadowed_id);
  188. }
  189. }
  190. if (in && in->valid) {
  191. /* We have already filled this one.
  192. * We have a duplicate and need to
  193. * 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
  200. * exisiting one */
  201. yaffs_chunk_del(dev,
  202. in->hdr_chunk,
  203. 1, __LINE__);
  204. in->valid = 0;
  205. } else {
  206. /* Use existing - destroy
  207. * this one. */
  208. yaffs_chunk_del(dev, chunk, 1,
  209. __LINE__);
  210. }
  211. }
  212. if (in && !in->valid &&
  213. (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  214. tags.obj_id ==
  215. YAFFS_OBJECTID_LOSTNFOUND)) {
  216. /* We only load some info, don't fiddle
  217. * 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.
  250. children);
  251. } else if (!parent ||
  252. parent->variant_type !=
  253. YAFFS_OBJECT_TYPE_DIRECTORY) {
  254. /* Hoosterman, a problem....
  255. * We're trying to use a
  256. * non-directory as a directory
  257. */
  258. yaffs_trace(YAFFS_TRACE_ERROR,
  259. "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  260. );
  261. parent = dev->lost_n_found;
  262. }
  263. yaffs_add_obj_to_dir(parent, in);
  264. switch (in->variant_type) {
  265. case YAFFS_OBJECT_TYPE_UNKNOWN:
  266. /* Todo got a problem */
  267. break;
  268. case YAFFS_OBJECT_TYPE_FILE:
  269. if (dev->param.
  270. use_header_file_size)
  271. in->variant.
  272. file_variant.file_size
  273. = yaffs_oh_to_size(oh);
  274. break;
  275. case YAFFS_OBJECT_TYPE_HARDLINK:
  276. in->variant.
  277. hardlink_variant.equiv_id =
  278. oh->equiv_id;
  279. list_add(&in->hard_links,
  280. &hard_list);
  281. break;
  282. case YAFFS_OBJECT_TYPE_DIRECTORY:
  283. /* Do nothing */
  284. break;
  285. case YAFFS_OBJECT_TYPE_SPECIAL:
  286. /* Do nothing */
  287. break;
  288. case YAFFS_OBJECT_TYPE_SYMLINK:
  289. in->variant.symlink_variant.
  290. alias =
  291. yaffs_clone_str(oh->alias);
  292. if (!in->variant.
  293. symlink_variant.alias)
  294. alloc_failed = 1;
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  301. /* If we got this far while scanning,
  302. * then the block is fully allocated. */
  303. state = YAFFS_BLOCK_STATE_FULL;
  304. }
  305. if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  306. /* If the block was partially allocated then
  307. * treat it as fully allocated. */
  308. state = YAFFS_BLOCK_STATE_FULL;
  309. dev->alloc_block = -1;
  310. }
  311. bi->block_state = state;
  312. /* Now let's see if it was dirty */
  313. if (bi->pages_in_use == 0 &&
  314. !bi->has_shrink_hdr &&
  315. bi->block_state == YAFFS_BLOCK_STATE_FULL)
  316. yaffs_block_became_dirty(dev, blk);
  317. }
  318. /* Ok, we've done all the scanning.
  319. * Fix up the hard link chains.
  320. * We should now have scanned all the objects, now it's time to add
  321. * these hardlinks.
  322. */
  323. yaffs_link_fixup(dev, &hard_list);
  324. /*
  325. * Fix up any shadowed objects.
  326. * There should not be more than one of these.
  327. */
  328. {
  329. struct yaffs_shadow_fixer *fixer;
  330. struct yaffs_obj *obj;
  331. while (shadow_fixers) {
  332. fixer = shadow_fixers;
  333. shadow_fixers = fixer->next;
  334. /* Complete the rename transaction by deleting the
  335. * shadowed object then setting the object header
  336. to unshadowed.
  337. */
  338. obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  339. if (obj)
  340. yaffs_del_obj(obj);
  341. obj = yaffs_find_by_number(dev, fixer->obj_id);
  342. if (obj)
  343. yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  344. kfree(fixer);
  345. }
  346. }
  347. yaffs_release_temp_buffer(dev, chunk_data);
  348. if (alloc_failed)
  349. return YAFFS_FAIL;
  350. yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
  351. return YAFFS_OK;
  352. }