yaffs_yaffs1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 result;
  25. int chunk;
  26. int c;
  27. int deleted;
  28. enum yaffs_block_state state;
  29. LIST_HEAD(hard_list);
  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);
  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;
  77. !alloc_failed && c < dev->param.chunks_per_block &&
  78. state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
  79. /* Read the tags and decide what to do */
  80. chunk = blk * dev->param.chunks_per_block + c;
  81. result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  82. &tags);
  83. /* Let's have a good look at this chunk... */
  84. if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
  85. tags.is_deleted) {
  86. /* YAFFS1 only...
  87. * A deleted chunk
  88. */
  89. deleted++;
  90. dev->n_free_chunks++;
  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
  98. *the block so the block is unused */
  99. state = YAFFS_BLOCK_STATE_EMPTY;
  100. dev->n_erased_blocks++;
  101. } else {
  102. /* this is the block being allocated */
  103. yaffs_trace(YAFFS_TRACE_SCAN,
  104. " Allocating from %d %d",
  105. blk, c);
  106. state = YAFFS_BLOCK_STATE_ALLOCATING;
  107. dev->alloc_block = blk;
  108. dev->alloc_page = c;
  109. dev->alloc_block_finder = blk;
  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
  122. * (two data chunks with 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 - 1) *
  133. 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. } else {
  150. /* chunk_id == 0, so it is an ObjectHeader.
  151. * Make the object
  152. */
  153. yaffs_set_chunk_bit(dev, blk, c);
  154. bi->pages_in_use++;
  155. result = yaffs_rd_chunk_tags_nand(dev, chunk,
  156. chunk_data,
  157. NULL);
  158. oh = (struct yaffs_obj_hdr *)chunk_data;
  159. in = yaffs_find_by_number(dev, tags.obj_id);
  160. if (in && in->variant_type != oh->type) {
  161. /* This should not happen, but somehow
  162. * Wev'e ended up with an obj_id that
  163. * has been reused but not yet deleted,
  164. * and worse still it has changed type.
  165. * Delete the old object.
  166. */
  167. yaffs_del_obj(in);
  168. in = NULL;
  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.
  195. * We have a duplicate and need to
  196. * resolve it. */
  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
  203. * exisiting one */
  204. yaffs_chunk_del(dev,
  205. in->hdr_chunk,
  206. 1, __LINE__);
  207. in->valid = 0;
  208. } else {
  209. /* Use existing - destroy
  210. * this one. */
  211. yaffs_chunk_del(dev, chunk, 1,
  212. __LINE__);
  213. }
  214. }
  215. if (in && !in->valid &&
  216. (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  217. tags.obj_id ==
  218. YAFFS_OBJECTID_LOSTNFOUND)) {
  219. /* We only load some info, don't fiddle
  220. * with directory structure */
  221. in->valid = 1;
  222. in->variant_type = oh->type;
  223. in->yst_mode = oh->yst_mode;
  224. yaffs_load_attribs(in, oh);
  225. in->hdr_chunk = chunk;
  226. in->serial = tags.serial_number;
  227. } else if (in && !in->valid) {
  228. /* we need to load this info */
  229. in->valid = 1;
  230. in->variant_type = oh->type;
  231. in->yst_mode = oh->yst_mode;
  232. yaffs_load_attribs(in, oh);
  233. in->hdr_chunk = chunk;
  234. in->serial = tags.serial_number;
  235. yaffs_set_obj_name_from_oh(in, oh);
  236. in->dirty = 0;
  237. /* directory stuff...
  238. * hook up to parent
  239. */
  240. parent =
  241. yaffs_find_or_create_by_number
  242. (dev, oh->parent_obj_id,
  243. YAFFS_OBJECT_TYPE_DIRECTORY);
  244. if (!parent)
  245. alloc_failed = 1;
  246. if (parent && parent->variant_type ==
  247. YAFFS_OBJECT_TYPE_UNKNOWN) {
  248. /* Set up as a directory */
  249. parent->variant_type =
  250. YAFFS_OBJECT_TYPE_DIRECTORY;
  251. INIT_LIST_HEAD(&parent->
  252. variant.dir_variant.
  253. children);
  254. } else if (!parent ||
  255. parent->variant_type !=
  256. YAFFS_OBJECT_TYPE_DIRECTORY) {
  257. /* Hoosterman, a problem....
  258. * We're trying to use a
  259. * non-directory as a directory
  260. */
  261. yaffs_trace(YAFFS_TRACE_ERROR,
  262. "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  263. );
  264. parent = dev->lost_n_found;
  265. }
  266. yaffs_add_obj_to_dir(parent, in);
  267. switch (in->variant_type) {
  268. case YAFFS_OBJECT_TYPE_UNKNOWN:
  269. /* Todo got a problem */
  270. break;
  271. case YAFFS_OBJECT_TYPE_FILE:
  272. if (dev->param.
  273. use_header_file_size)
  274. in->variant.
  275. file_variant.file_size
  276. = yaffs_oh_to_size(oh);
  277. break;
  278. case YAFFS_OBJECT_TYPE_HARDLINK:
  279. in->variant.
  280. hardlink_variant.equiv_id =
  281. oh->equiv_id;
  282. list_add(&in->hard_links,
  283. &hard_list);
  284. break;
  285. case YAFFS_OBJECT_TYPE_DIRECTORY:
  286. /* Do nothing */
  287. break;
  288. case YAFFS_OBJECT_TYPE_SPECIAL:
  289. /* Do nothing */
  290. break;
  291. case YAFFS_OBJECT_TYPE_SYMLINK:
  292. in->variant.symlink_variant.
  293. alias =
  294. yaffs_clone_str(oh->alias);
  295. if (!in->variant.
  296. symlink_variant.alias)
  297. alloc_failed = 1;
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  304. /* If we got this far while scanning,
  305. * then the block is fully allocated. */
  306. state = YAFFS_BLOCK_STATE_FULL;
  307. }
  308. if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  309. /* If the block was partially allocated then
  310. * treat it as fully allocated. */
  311. state = YAFFS_BLOCK_STATE_FULL;
  312. dev->alloc_block = -1;
  313. }
  314. bi->block_state = state;
  315. /* Now let's see if it was dirty */
  316. if (bi->pages_in_use == 0 &&
  317. !bi->has_shrink_hdr &&
  318. bi->block_state == YAFFS_BLOCK_STATE_FULL)
  319. yaffs_block_became_dirty(dev, blk);
  320. }
  321. /* Ok, we've done all the scanning.
  322. * Fix up the hard link chains.
  323. * We should now have scanned all the objects, now it's time to add
  324. * these hardlinks.
  325. */
  326. yaffs_link_fixup(dev, &hard_list);
  327. /*
  328. * Fix up any shadowed objects.
  329. * There should not be more than one of these.
  330. */
  331. {
  332. struct yaffs_shadow_fixer *fixer;
  333. struct yaffs_obj *obj;
  334. while (shadow_fixers) {
  335. fixer = shadow_fixers;
  336. shadow_fixers = fixer->next;
  337. /* Complete the rename transaction by deleting the
  338. * shadowed object then setting the object header
  339. to unshadowed.
  340. */
  341. obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  342. if (obj)
  343. yaffs_del_obj(obj);
  344. obj = yaffs_find_by_number(dev, fixer->obj_id);
  345. if (obj)
  346. yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  347. kfree(fixer);
  348. }
  349. }
  350. yaffs_release_temp_buffer(dev, chunk_data);
  351. if (alloc_failed)
  352. return YAFFS_FAIL;
  353. yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
  354. return YAFFS_OK;
  355. }