yaffs_yaffs1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include <dm/devres.h>
  21. int yaffs1_scan(struct yaffs_dev *dev)
  22. {
  23. struct yaffs_ext_tags tags;
  24. int blk;
  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. yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &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. } else if (!tags.chunk_used) {
  91. /* An unassigned chunk in the block
  92. * This means that either the block is empty or
  93. * this is the one being allocated from
  94. */
  95. if (c == 0) {
  96. /* We're looking at the first chunk in
  97. *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 */
  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. }
  110. dev->n_free_chunks +=
  111. (dev->param.chunks_per_block - c);
  112. } else if (tags.chunk_id > 0) {
  113. /* chunk_id > 0 so it is a data chunk... */
  114. unsigned int endpos;
  115. yaffs_set_chunk_bit(dev, blk, c);
  116. bi->pages_in_use++;
  117. in = yaffs_find_or_create_by_number(dev,
  118. tags.obj_id,
  119. YAFFS_OBJECT_TYPE_FILE);
  120. /* PutChunkIntoFile checks for a clash
  121. * (two data chunks with the same chunk_id).
  122. */
  123. if (!in)
  124. alloc_failed = 1;
  125. if (in) {
  126. if (!yaffs_put_chunk_in_file
  127. (in, tags.chunk_id, chunk, 1))
  128. alloc_failed = 1;
  129. }
  130. endpos =
  131. (tags.chunk_id - 1) *
  132. dev->data_bytes_per_chunk +
  133. tags.n_bytes;
  134. if (in &&
  135. in->variant_type ==
  136. YAFFS_OBJECT_TYPE_FILE &&
  137. in->variant.file_variant.scanned_size <
  138. endpos) {
  139. in->variant.file_variant.scanned_size =
  140. endpos;
  141. if (!dev->param.use_header_file_size) {
  142. in->variant.
  143. file_variant.file_size =
  144. in->variant.
  145. file_variant.scanned_size;
  146. }
  147. }
  148. } else {
  149. /* chunk_id == 0, so it is an ObjectHeader.
  150. * Make the object
  151. */
  152. yaffs_set_chunk_bit(dev, blk, c);
  153. bi->pages_in_use++;
  154. yaffs_rd_chunk_tags_nand(dev, chunk,
  155. chunk_data, NULL);
  156. oh = (struct yaffs_obj_hdr *)chunk_data;
  157. in = yaffs_find_by_number(dev, tags.obj_id);
  158. if (in && in->variant_type != oh->type) {
  159. /* This should not happen, but somehow
  160. * Wev'e ended up with an obj_id that
  161. * has been reused but not yet deleted,
  162. * and worse still it has changed type.
  163. * Delete the old object.
  164. */
  165. yaffs_del_obj(in);
  166. in = NULL;
  167. }
  168. in = yaffs_find_or_create_by_number(dev,
  169. tags.obj_id,
  170. oh->type);
  171. if (!in)
  172. alloc_failed = 1;
  173. if (in && oh->shadows_obj > 0) {
  174. struct yaffs_shadow_fixer *fixer;
  175. fixer =
  176. kmalloc(sizeof
  177. (struct yaffs_shadow_fixer),
  178. GFP_NOFS);
  179. if (fixer) {
  180. fixer->next = shadow_fixers;
  181. shadow_fixers = fixer;
  182. fixer->obj_id = tags.obj_id;
  183. fixer->shadowed_id =
  184. oh->shadows_obj;
  185. yaffs_trace(YAFFS_TRACE_SCAN,
  186. " Shadow fixer: %d shadows %d",
  187. fixer->obj_id,
  188. fixer->shadowed_id);
  189. }
  190. }
  191. if (in && in->valid) {
  192. /* We have already filled this one.
  193. * We have a duplicate and need to
  194. * resolve it. */
  195. unsigned existing_serial = in->serial;
  196. unsigned new_serial =
  197. tags.serial_number;
  198. if (((existing_serial + 1) & 3) ==
  199. new_serial) {
  200. /* Use new one - destroy the
  201. * exisiting one */
  202. yaffs_chunk_del(dev,
  203. in->hdr_chunk,
  204. 1, __LINE__);
  205. in->valid = 0;
  206. } else {
  207. /* Use existing - destroy
  208. * 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
  218. * with directory structure */
  219. in->valid = 1;
  220. in->variant_type = oh->type;
  221. in->yst_mode = oh->yst_mode;
  222. yaffs_load_attribs(in, oh);
  223. in->hdr_chunk = chunk;
  224. in->serial = tags.serial_number;
  225. } else if (in && !in->valid) {
  226. /* we need to load this info */
  227. in->valid = 1;
  228. in->variant_type = oh->type;
  229. in->yst_mode = oh->yst_mode;
  230. yaffs_load_attribs(in, oh);
  231. in->hdr_chunk = chunk;
  232. in->serial = tags.serial_number;
  233. yaffs_set_obj_name_from_oh(in, oh);
  234. in->dirty = 0;
  235. /* directory stuff...
  236. * hook up to parent
  237. */
  238. parent =
  239. yaffs_find_or_create_by_number
  240. (dev, oh->parent_obj_id,
  241. YAFFS_OBJECT_TYPE_DIRECTORY);
  242. if (!parent)
  243. alloc_failed = 1;
  244. if (parent && parent->variant_type ==
  245. YAFFS_OBJECT_TYPE_UNKNOWN) {
  246. /* Set up as a directory */
  247. parent->variant_type =
  248. YAFFS_OBJECT_TYPE_DIRECTORY;
  249. INIT_LIST_HEAD(&parent->
  250. variant.dir_variant.
  251. children);
  252. } else if (!parent ||
  253. parent->variant_type !=
  254. YAFFS_OBJECT_TYPE_DIRECTORY) {
  255. /* Hoosterman, a problem....
  256. * We're trying to use a
  257. * non-directory as a directory
  258. */
  259. yaffs_trace(YAFFS_TRACE_ERROR,
  260. "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  261. );
  262. parent = dev->lost_n_found;
  263. }
  264. yaffs_add_obj_to_dir(parent, in);
  265. switch (in->variant_type) {
  266. case YAFFS_OBJECT_TYPE_UNKNOWN:
  267. /* Todo got a problem */
  268. break;
  269. case YAFFS_OBJECT_TYPE_FILE:
  270. if (dev->param.
  271. use_header_file_size)
  272. in->variant.
  273. file_variant.file_size
  274. = yaffs_oh_to_size(oh);
  275. break;
  276. case YAFFS_OBJECT_TYPE_HARDLINK:
  277. in->variant.
  278. hardlink_variant.equiv_id =
  279. oh->equiv_id;
  280. list_add(&in->hard_links,
  281. &hard_list);
  282. break;
  283. case YAFFS_OBJECT_TYPE_DIRECTORY:
  284. /* Do nothing */
  285. break;
  286. case YAFFS_OBJECT_TYPE_SPECIAL:
  287. /* Do nothing */
  288. break;
  289. case YAFFS_OBJECT_TYPE_SYMLINK:
  290. in->variant.symlink_variant.
  291. alias =
  292. yaffs_clone_str(oh->alias);
  293. if (!in->variant.
  294. symlink_variant.alias)
  295. alloc_failed = 1;
  296. break;
  297. }
  298. }
  299. }
  300. }
  301. if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  302. /* If we got this far while scanning,
  303. * then the block is fully allocated. */
  304. state = YAFFS_BLOCK_STATE_FULL;
  305. }
  306. if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  307. /* If the block was partially allocated then
  308. * treat it as fully allocated. */
  309. state = YAFFS_BLOCK_STATE_FULL;
  310. dev->alloc_block = -1;
  311. }
  312. bi->block_state = state;
  313. /* Now let's see if it was dirty */
  314. if (bi->pages_in_use == 0 &&
  315. !bi->has_shrink_hdr &&
  316. bi->block_state == YAFFS_BLOCK_STATE_FULL)
  317. yaffs_block_became_dirty(dev, blk);
  318. }
  319. /* Ok, we've done all the scanning.
  320. * Fix up the hard link chains.
  321. * We should now have scanned all the objects, now it's time to add
  322. * these hardlinks.
  323. */
  324. yaffs_link_fixup(dev, &hard_list);
  325. /*
  326. * Fix up any shadowed objects.
  327. * There should not be more than one of these.
  328. */
  329. {
  330. struct yaffs_shadow_fixer *fixer;
  331. struct yaffs_obj *obj;
  332. while (shadow_fixers) {
  333. fixer = shadow_fixers;
  334. shadow_fixers = fixer->next;
  335. /* Complete the rename transaction by deleting the
  336. * shadowed object then setting the object header
  337. to unshadowed.
  338. */
  339. obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  340. if (obj)
  341. yaffs_del_obj(obj);
  342. obj = yaffs_find_by_number(dev, fixer->obj_id);
  343. if (obj)
  344. yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  345. kfree(fixer);
  346. }
  347. }
  348. yaffs_release_temp_buffer(dev, chunk_data);
  349. if (alloc_failed)
  350. return YAFFS_FAIL;
  351. yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
  352. return YAFFS_OK;
  353. }