PageRenderTime 72ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/yaffs2/yaffs_guts.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 4685 lines | 3499 code | 1146 blank | 40 comment | 785 complexity | 6796f9e580737180b321dac8dd942a36 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  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 "yportenv.h"
  14. #include "yaffs_trace.h"
  15. #include "yaffs_guts.h"
  16. #include "yaffs_tagsvalidity.h"
  17. #include "yaffs_getblockinfo.h"
  18. #include "yaffs_tagscompat.h"
  19. #include "yaffs_nand.h"
  20. #include "yaffs_yaffs1.h"
  21. #include "yaffs_yaffs2.h"
  22. #include "yaffs_bitmap.h"
  23. #include "yaffs_verify.h"
  24. #include "yaffs_nand.h"
  25. #include "yaffs_packedtags2.h"
  26. #include "yaffs_nameval.h"
  27. #include "yaffs_allocator.h"
  28. #include "yaffs_attribs.h"
  29. #define YAFFS_GC_GOOD_ENOUGH 2
  30. #define YAFFS_GC_PASSIVE_THRESHOLD 4
  31. #include "yaffs_ecc.h"
  32. static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  33. const u8 * buffer, int n_bytes, int use_reserve);
  34. static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  35. int *chunk_out, u32 * offset_out)
  36. {
  37. int chunk;
  38. u32 offset;
  39. chunk = (u32) (addr >> dev->chunk_shift);
  40. if (dev->chunk_div == 1) {
  41. offset = (u32) (addr & dev->chunk_mask);
  42. } else {
  43. loff_t chunk_base;
  44. chunk /= dev->chunk_div;
  45. chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
  46. offset = (u32) (addr - chunk_base);
  47. }
  48. *chunk_out = chunk;
  49. *offset_out = offset;
  50. }
  51. static u32 calc_shifts_ceiling(u32 x)
  52. {
  53. int extra_bits;
  54. int shifts;
  55. shifts = extra_bits = 0;
  56. while (x > 1) {
  57. if (x & 1)
  58. extra_bits++;
  59. x >>= 1;
  60. shifts++;
  61. }
  62. if (extra_bits)
  63. shifts++;
  64. return shifts;
  65. }
  66. static u32 calc_shifts(u32 x)
  67. {
  68. u32 shifts;
  69. shifts = 0;
  70. if (!x)
  71. return 0;
  72. while (!(x & 1)) {
  73. x >>= 1;
  74. shifts++;
  75. }
  76. return shifts;
  77. }
  78. static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
  79. {
  80. int i;
  81. u8 *buf = (u8 *) 1;
  82. memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
  83. for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  84. dev->temp_buffer[i].line = 0;
  85. dev->temp_buffer[i].buffer = buf =
  86. kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  87. }
  88. return buf ? YAFFS_OK : YAFFS_FAIL;
  89. }
  90. u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
  91. {
  92. int i, j;
  93. dev->temp_in_use++;
  94. if (dev->temp_in_use > dev->max_temp)
  95. dev->max_temp = dev->temp_in_use;
  96. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  97. if (dev->temp_buffer[i].line == 0) {
  98. dev->temp_buffer[i].line = line_no;
  99. if ((i + 1) > dev->max_temp) {
  100. dev->max_temp = i + 1;
  101. for (j = 0; j <= i; j++)
  102. dev->temp_buffer[j].max_line =
  103. dev->temp_buffer[j].line;
  104. }
  105. return dev->temp_buffer[i].buffer;
  106. }
  107. }
  108. yaffs_trace(YAFFS_TRACE_BUFFERS,
  109. "Out of temp buffers at line %d, other held by lines:",
  110. line_no);
  111. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  112. yaffs_trace(YAFFS_TRACE_BUFFERS," %d", dev->temp_buffer[i].line);
  113. dev->unmanaged_buffer_allocs++;
  114. return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
  115. }
  116. void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
  117. {
  118. int i;
  119. dev->temp_in_use--;
  120. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  121. if (dev->temp_buffer[i].buffer == buffer) {
  122. dev->temp_buffer[i].line = 0;
  123. return;
  124. }
  125. }
  126. if (buffer) {
  127. yaffs_trace(YAFFS_TRACE_BUFFERS,
  128. "Releasing unmanaged temp buffer in line %d",
  129. line_no);
  130. kfree(buffer);
  131. dev->unmanaged_buffer_deallocs++;
  132. }
  133. }
  134. int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
  135. {
  136. int i;
  137. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  138. if (dev->temp_buffer[i].buffer == buffer)
  139. return 1;
  140. }
  141. for (i = 0; i < dev->param.n_caches; i++) {
  142. if (dev->cache[i].data == buffer)
  143. return 1;
  144. }
  145. if (buffer == dev->checkpt_buffer)
  146. return 1;
  147. yaffs_trace(YAFFS_TRACE_ALWAYS,
  148. "yaffs: unmaged buffer detected.");
  149. return 0;
  150. }
  151. static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  152. const u8 * data,
  153. const struct yaffs_ext_tags *tags)
  154. {
  155. dev = dev;
  156. nand_chunk = nand_chunk;
  157. data = data;
  158. tags = tags;
  159. }
  160. static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  161. const struct yaffs_ext_tags *tags)
  162. {
  163. dev = dev;
  164. nand_chunk = nand_chunk;
  165. tags = tags;
  166. }
  167. void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  168. struct yaffs_block_info *bi)
  169. {
  170. if (!bi->gc_prioritise) {
  171. bi->gc_prioritise = 1;
  172. dev->has_pending_prioritised_gc = 1;
  173. bi->chunk_error_strikes++;
  174. if (bi->chunk_error_strikes > 3) {
  175. bi->needs_retiring = 1;
  176. yaffs_trace(YAFFS_TRACE_ALWAYS, "yaffs: Block struck out");
  177. }
  178. }
  179. }
  180. static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  181. int erased_ok)
  182. {
  183. int flash_block = nand_chunk / dev->param.chunks_per_block;
  184. struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  185. yaffs_handle_chunk_error(dev, bi);
  186. if (erased_ok) {
  187. bi->needs_retiring = 1;
  188. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  189. "**>> Block %d needs retiring", flash_block);
  190. }
  191. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  192. yaffs_skip_rest_of_block(dev);
  193. }
  194. static inline int yaffs_hash_fn(int n)
  195. {
  196. n = abs(n);
  197. return n % YAFFS_NOBJECT_BUCKETS;
  198. }
  199. struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
  200. {
  201. return dev->root_dir;
  202. }
  203. struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
  204. {
  205. return dev->lost_n_found;
  206. }
  207. int yaffs_check_ff(u8 * buffer, int n_bytes)
  208. {
  209. while (n_bytes--) {
  210. if (*buffer != 0xFF)
  211. return 0;
  212. buffer++;
  213. }
  214. return 1;
  215. }
  216. static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
  217. {
  218. int retval = YAFFS_OK;
  219. u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
  220. struct yaffs_ext_tags tags;
  221. int result;
  222. result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
  223. if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
  224. retval = YAFFS_FAIL;
  225. if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
  226. tags.chunk_used) {
  227. yaffs_trace(YAFFS_TRACE_NANDACCESS, "Chunk %d not erased", nand_chunk);
  228. retval = YAFFS_FAIL;
  229. }
  230. yaffs_release_temp_buffer(dev, data, __LINE__);
  231. return retval;
  232. }
  233. static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  234. int nand_chunk,
  235. const u8 * data,
  236. struct yaffs_ext_tags *tags)
  237. {
  238. int retval = YAFFS_OK;
  239. struct yaffs_ext_tags temp_tags;
  240. u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  241. int result;
  242. result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
  243. if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
  244. temp_tags.obj_id != tags->obj_id ||
  245. temp_tags.chunk_id != tags->chunk_id ||
  246. temp_tags.n_bytes != tags->n_bytes)
  247. retval = YAFFS_FAIL;
  248. yaffs_release_temp_buffer(dev, buffer, __LINE__);
  249. return retval;
  250. }
  251. int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
  252. {
  253. int reserved_chunks;
  254. int reserved_blocks = dev->param.n_reserved_blocks;
  255. int checkpt_blocks;
  256. checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
  257. reserved_chunks =
  258. ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
  259. return (dev->n_free_chunks > (reserved_chunks + n_chunks));
  260. }
  261. static int yaffs_find_alloc_block(struct yaffs_dev *dev)
  262. {
  263. int i;
  264. struct yaffs_block_info *bi;
  265. if (dev->n_erased_blocks < 1) {
  266. yaffs_trace(YAFFS_TRACE_ERROR,
  267. "yaffs tragedy: no more erased blocks" );
  268. return -1;
  269. }
  270. for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  271. dev->alloc_block_finder++;
  272. if (dev->alloc_block_finder < dev->internal_start_block
  273. || dev->alloc_block_finder > dev->internal_end_block) {
  274. dev->alloc_block_finder = dev->internal_start_block;
  275. }
  276. bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
  277. if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  278. bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
  279. dev->seq_number++;
  280. bi->seq_number = dev->seq_number;
  281. dev->n_erased_blocks--;
  282. yaffs_trace(YAFFS_TRACE_ALLOCATE,
  283. "Allocated block %d, seq %d, %d left" ,
  284. dev->alloc_block_finder, dev->seq_number,
  285. dev->n_erased_blocks);
  286. return dev->alloc_block_finder;
  287. }
  288. }
  289. yaffs_trace(YAFFS_TRACE_ALWAYS,
  290. "yaffs tragedy: no more erased blocks, but there should have been %d",
  291. dev->n_erased_blocks);
  292. return -1;
  293. }
  294. static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  295. struct yaffs_block_info **block_ptr)
  296. {
  297. int ret_val;
  298. struct yaffs_block_info *bi;
  299. if (dev->alloc_block < 0) {
  300. dev->alloc_block = yaffs_find_alloc_block(dev);
  301. dev->alloc_page = 0;
  302. }
  303. if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
  304. return -1;
  305. }
  306. if (dev->n_erased_blocks < dev->param.n_reserved_blocks
  307. && dev->alloc_page == 0)
  308. yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
  309. if (dev->alloc_block >= 0) {
  310. bi = yaffs_get_block_info(dev, dev->alloc_block);
  311. ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
  312. dev->alloc_page;
  313. bi->pages_in_use++;
  314. yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
  315. dev->alloc_page++;
  316. dev->n_free_chunks--;
  317. if (dev->alloc_page >= dev->param.chunks_per_block) {
  318. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  319. dev->alloc_block = -1;
  320. }
  321. if (block_ptr)
  322. *block_ptr = bi;
  323. return ret_val;
  324. }
  325. yaffs_trace(YAFFS_TRACE_ERROR, "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" );
  326. return -1;
  327. }
  328. static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
  329. {
  330. int n;
  331. n = dev->n_erased_blocks * dev->param.chunks_per_block;
  332. if (dev->alloc_block > 0)
  333. n += (dev->param.chunks_per_block - dev->alloc_page);
  334. return n;
  335. }
  336. void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
  337. {
  338. if (dev->alloc_block > 0) {
  339. struct yaffs_block_info *bi =
  340. yaffs_get_block_info(dev, dev->alloc_block);
  341. if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  342. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  343. dev->alloc_block = -1;
  344. }
  345. }
  346. }
  347. static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  348. const u8 * data,
  349. struct yaffs_ext_tags *tags, int use_reserver)
  350. {
  351. int attempts = 0;
  352. int write_ok = 0;
  353. int chunk;
  354. yaffs2_checkpt_invalidate(dev);
  355. do {
  356. struct yaffs_block_info *bi = 0;
  357. int erased_ok = 0;
  358. chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
  359. if (chunk < 0) {
  360. break;
  361. }
  362. /* First check this chunk is erased, if it needs
  363. * checking. The checking policy (unless forced
  364. * always on) is as follows:
  365. *
  366. * Check the first page we try to write in a block.
  367. * If the check passes then we don't need to check any
  368. * more. If the check fails, we check again...
  369. * If the block has been erased, we don't need to check.
  370. *
  371. * However, if the block has been prioritised for gc,
  372. * then we think there might be something odd about
  373. * this block and stop using it.
  374. *
  375. * Rationale: We should only ever see chunks that have
  376. * not been erased if there was a partially written
  377. * chunk due to power loss. This checking policy should
  378. * catch that case with very few checks and thus save a
  379. * lot of checks that are most likely not needed.
  380. *
  381. * Mods to the above
  382. * If an erase check fails or the write fails we skip the
  383. * rest of the block.
  384. */
  385. attempts++;
  386. if (dev->param.always_check_erased)
  387. bi->skip_erased_check = 0;
  388. if (!bi->skip_erased_check) {
  389. erased_ok = yaffs_check_chunk_erased(dev, chunk);
  390. if (erased_ok != YAFFS_OK) {
  391. yaffs_trace(YAFFS_TRACE_ERROR,
  392. "**>> yaffs chunk %d was not erased",
  393. chunk);
  394. yaffs_chunk_del(dev, chunk, 1, __LINE__);
  395. yaffs_skip_rest_of_block(dev);
  396. continue;
  397. }
  398. }
  399. write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
  400. if (!bi->skip_erased_check)
  401. write_ok =
  402. yaffs_verify_chunk_written(dev, chunk, data, tags);
  403. if (write_ok != YAFFS_OK) {
  404. yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
  405. continue;
  406. }
  407. bi->skip_erased_check = 1;
  408. yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
  409. } while (write_ok != YAFFS_OK &&
  410. (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  411. if (!write_ok)
  412. chunk = -1;
  413. if (attempts > 1) {
  414. yaffs_trace(YAFFS_TRACE_ERROR,
  415. "**>> yaffs write required %d attempts",
  416. attempts);
  417. dev->n_retired_writes += (attempts - 1);
  418. }
  419. return chunk;
  420. }
  421. static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
  422. {
  423. struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  424. yaffs2_checkpt_invalidate(dev);
  425. yaffs2_clear_oldest_dirty_seq(dev, bi);
  426. if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
  427. if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
  428. yaffs_trace(YAFFS_TRACE_ALWAYS,
  429. "yaffs: Failed to mark bad and erase block %d",
  430. flash_block);
  431. } else {
  432. struct yaffs_ext_tags tags;
  433. int chunk_id =
  434. flash_block * dev->param.chunks_per_block;
  435. u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  436. memset(buffer, 0xff, dev->data_bytes_per_chunk);
  437. yaffs_init_tags(&tags);
  438. tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
  439. if (dev->param.write_chunk_tags_fn(dev, chunk_id -
  440. dev->chunk_offset,
  441. buffer,
  442. &tags) != YAFFS_OK)
  443. yaffs_trace(YAFFS_TRACE_ALWAYS,
  444. "yaffs: Failed to write bad block marker to block %d",
  445. flash_block);
  446. yaffs_release_temp_buffer(dev, buffer, __LINE__);
  447. }
  448. }
  449. bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  450. bi->gc_prioritise = 0;
  451. bi->needs_retiring = 0;
  452. dev->n_retired_blocks++;
  453. }
  454. static u16 yaffs_calc_name_sum(const YCHAR * name)
  455. {
  456. u16 sum = 0;
  457. u16 i = 1;
  458. const YUCHAR *bname = (const YUCHAR *)name;
  459. if (bname) {
  460. while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
  461. sum += ((*bname) & 0x1f) * i;
  462. i++;
  463. bname++;
  464. }
  465. }
  466. return sum;
  467. }
  468. void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
  469. {
  470. #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  471. memset(obj->short_name, 0, sizeof(obj->short_name));
  472. if (name &&
  473. strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
  474. YAFFS_SHORT_NAME_LENGTH)
  475. strcpy(obj->short_name, name);
  476. else
  477. obj->short_name[0] = _Y('\0');
  478. #endif
  479. obj->sum = yaffs_calc_name_sum(name);
  480. }
  481. void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  482. const struct yaffs_obj_hdr *oh)
  483. {
  484. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  485. YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
  486. memset(tmp_name, 0, sizeof(tmp_name));
  487. yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
  488. YAFFS_MAX_NAME_LENGTH + 1);
  489. yaffs_set_obj_name(obj, tmp_name);
  490. #else
  491. yaffs_set_obj_name(obj, oh->name);
  492. #endif
  493. }
  494. struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
  495. {
  496. struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
  497. if (tn) {
  498. memset(tn, 0, dev->tnode_size);
  499. dev->n_tnodes++;
  500. }
  501. dev->checkpoint_blocks_required = 0;
  502. return tn;
  503. }
  504. static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  505. {
  506. yaffs_free_raw_tnode(dev, tn);
  507. dev->n_tnodes--;
  508. dev->checkpoint_blocks_required = 0;
  509. }
  510. static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
  511. {
  512. yaffs_deinit_raw_tnodes_and_objs(dev);
  513. dev->n_obj = 0;
  514. dev->n_tnodes = 0;
  515. }
  516. void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  517. unsigned pos, unsigned val)
  518. {
  519. u32 *map = (u32 *) tn;
  520. u32 bit_in_map;
  521. u32 bit_in_word;
  522. u32 word_in_map;
  523. u32 mask;
  524. pos &= YAFFS_TNODES_LEVEL0_MASK;
  525. val >>= dev->chunk_grp_bits;
  526. bit_in_map = pos * dev->tnode_width;
  527. word_in_map = bit_in_map / 32;
  528. bit_in_word = bit_in_map & (32 - 1);
  529. mask = dev->tnode_mask << bit_in_word;
  530. map[word_in_map] &= ~mask;
  531. map[word_in_map] |= (mask & (val << bit_in_word));
  532. if (dev->tnode_width > (32 - bit_in_word)) {
  533. bit_in_word = (32 - bit_in_word);
  534. word_in_map++;;
  535. mask =
  536. dev->tnode_mask >> ( bit_in_word);
  537. map[word_in_map] &= ~mask;
  538. map[word_in_map] |= (mask & (val >> bit_in_word));
  539. }
  540. }
  541. u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  542. unsigned pos)
  543. {
  544. u32 *map = (u32 *) tn;
  545. u32 bit_in_map;
  546. u32 bit_in_word;
  547. u32 word_in_map;
  548. u32 val;
  549. pos &= YAFFS_TNODES_LEVEL0_MASK;
  550. bit_in_map = pos * dev->tnode_width;
  551. word_in_map = bit_in_map / 32;
  552. bit_in_word = bit_in_map & (32 - 1);
  553. val = map[word_in_map] >> bit_in_word;
  554. if (dev->tnode_width > (32 - bit_in_word)) {
  555. bit_in_word = (32 - bit_in_word);
  556. word_in_map++;;
  557. val |= (map[word_in_map] << bit_in_word);
  558. }
  559. val &= dev->tnode_mask;
  560. val <<= dev->chunk_grp_bits;
  561. return val;
  562. }
  563. struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  564. struct yaffs_file_var *file_struct,
  565. u32 chunk_id)
  566. {
  567. struct yaffs_tnode *tn = file_struct->top;
  568. u32 i;
  569. int required_depth;
  570. int level = file_struct->top_level;
  571. dev = dev;
  572. if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  573. return NULL;
  574. if (chunk_id > YAFFS_MAX_CHUNK_ID)
  575. return NULL;
  576. i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  577. required_depth = 0;
  578. while (i) {
  579. i >>= YAFFS_TNODES_INTERNAL_BITS;
  580. required_depth++;
  581. }
  582. if (required_depth > file_struct->top_level)
  583. return NULL;
  584. while (level > 0 && tn) {
  585. tn = tn->internal[(chunk_id >>
  586. (YAFFS_TNODES_LEVEL0_BITS +
  587. (level - 1) *
  588. YAFFS_TNODES_INTERNAL_BITS)) &
  589. YAFFS_TNODES_INTERNAL_MASK];
  590. level--;
  591. }
  592. return tn;
  593. }
  594. struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  595. struct yaffs_file_var *file_struct,
  596. u32 chunk_id,
  597. struct yaffs_tnode *passed_tn)
  598. {
  599. int required_depth;
  600. int i;
  601. int l;
  602. struct yaffs_tnode *tn;
  603. u32 x;
  604. if (file_struct->top_level < 0
  605. || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
  606. return NULL;
  607. if (chunk_id > YAFFS_MAX_CHUNK_ID)
  608. return NULL;
  609. x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  610. required_depth = 0;
  611. while (x) {
  612. x >>= YAFFS_TNODES_INTERNAL_BITS;
  613. required_depth++;
  614. }
  615. if (required_depth > file_struct->top_level) {
  616. for (i = file_struct->top_level; i < required_depth; i++) {
  617. tn = yaffs_get_tnode(dev);
  618. if (tn) {
  619. tn->internal[0] = file_struct->top;
  620. file_struct->top = tn;
  621. file_struct->top_level++;
  622. } else {
  623. yaffs_trace(YAFFS_TRACE_ERROR, "yaffs: no more tnodes");
  624. return NULL;
  625. }
  626. }
  627. }
  628. l = file_struct->top_level;
  629. tn = file_struct->top;
  630. if (l > 0) {
  631. while (l > 0 && tn) {
  632. x = (chunk_id >>
  633. (YAFFS_TNODES_LEVEL0_BITS +
  634. (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  635. YAFFS_TNODES_INTERNAL_MASK;
  636. if ((l > 1) && !tn->internal[x]) {
  637. tn->internal[x] = yaffs_get_tnode(dev);
  638. if (!tn->internal[x])
  639. return NULL;
  640. } else if (l == 1) {
  641. if (passed_tn) {
  642. if (tn->internal[x])
  643. yaffs_free_tnode(dev,
  644. tn->
  645. internal[x]);
  646. tn->internal[x] = passed_tn;
  647. } else if (!tn->internal[x]) {
  648. tn->internal[x] = yaffs_get_tnode(dev);
  649. if (!tn->internal[x])
  650. return NULL;
  651. }
  652. }
  653. tn = tn->internal[x];
  654. l--;
  655. }
  656. } else {
  657. if (passed_tn) {
  658. memcpy(tn, passed_tn,
  659. (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
  660. yaffs_free_tnode(dev, passed_tn);
  661. }
  662. }
  663. return tn;
  664. }
  665. static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  666. int chunk_obj)
  667. {
  668. return (tags->chunk_id == chunk_obj &&
  669. tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
  670. }
  671. static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
  672. struct yaffs_ext_tags *tags, int obj_id,
  673. int inode_chunk)
  674. {
  675. int j;
  676. for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
  677. if (yaffs_check_chunk_bit
  678. (dev, the_chunk / dev->param.chunks_per_block,
  679. the_chunk % dev->param.chunks_per_block)) {
  680. if (dev->chunk_grp_size == 1)
  681. return the_chunk;
  682. else {
  683. yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  684. tags);
  685. if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
  686. return the_chunk;
  687. }
  688. }
  689. }
  690. the_chunk++;
  691. }
  692. return -1;
  693. }
  694. static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  695. struct yaffs_ext_tags *tags)
  696. {
  697. struct yaffs_tnode *tn;
  698. int the_chunk = -1;
  699. struct yaffs_ext_tags local_tags;
  700. int ret_val = -1;
  701. struct yaffs_dev *dev = in->my_dev;
  702. if (!tags) {
  703. tags = &local_tags;
  704. }
  705. tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  706. if (tn) {
  707. the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  708. ret_val =
  709. yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  710. inode_chunk);
  711. }
  712. return ret_val;
  713. }
  714. static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
  715. struct yaffs_ext_tags *tags)
  716. {
  717. struct yaffs_tnode *tn;
  718. int the_chunk = -1;
  719. struct yaffs_ext_tags local_tags;
  720. struct yaffs_dev *dev = in->my_dev;
  721. int ret_val = -1;
  722. if (!tags) {
  723. tags = &local_tags;
  724. }
  725. tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  726. if (tn) {
  727. the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  728. ret_val =
  729. yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  730. inode_chunk);
  731. if (ret_val != -1)
  732. yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
  733. }
  734. return ret_val;
  735. }
  736. int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  737. int nand_chunk, int in_scan)
  738. {
  739. struct yaffs_tnode *tn;
  740. struct yaffs_dev *dev = in->my_dev;
  741. int existing_cunk;
  742. struct yaffs_ext_tags existing_tags;
  743. struct yaffs_ext_tags new_tags;
  744. unsigned existing_serial, new_serial;
  745. if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
  746. if (!in_scan) {
  747. yaffs_trace(YAFFS_TRACE_ERROR,
  748. "yaffs tragedy:attempt to put data chunk into a non-file"
  749. );
  750. YBUG();
  751. }
  752. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  753. return YAFFS_OK;
  754. }
  755. tn = yaffs_add_find_tnode_0(dev,
  756. &in->variant.file_variant,
  757. inode_chunk, NULL);
  758. if (!tn)
  759. return YAFFS_FAIL;
  760. if (!nand_chunk)
  761. return YAFFS_OK;
  762. existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
  763. if (in_scan != 0) {
  764. if (existing_cunk > 0) {
  765. if (in_scan > 0) {
  766. yaffs_rd_chunk_tags_nand(dev,
  767. nand_chunk,
  768. NULL, &new_tags);
  769. existing_cunk =
  770. yaffs_find_chunk_in_file(in, inode_chunk,
  771. &existing_tags);
  772. }
  773. if (existing_cunk <= 0) {
  774. yaffs_trace(YAFFS_TRACE_ERROR,
  775. "yaffs tragedy: existing chunk < 0 in scan"
  776. );
  777. }
  778. if (in_scan > 0) {
  779. new_serial = new_tags.serial_number;
  780. existing_serial = existing_tags.serial_number;
  781. }
  782. if ((in_scan > 0) &&
  783. (existing_cunk <= 0 ||
  784. ((existing_serial + 1) & 3) == new_serial)) {
  785. yaffs_chunk_del(dev, existing_cunk, 1,
  786. __LINE__);
  787. } else {
  788. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  789. return YAFFS_OK;
  790. }
  791. }
  792. }
  793. if (existing_cunk == 0)
  794. in->n_data_chunks++;
  795. yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
  796. return YAFFS_OK;
  797. }
  798. static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
  799. {
  800. struct yaffs_block_info *the_block;
  801. unsigned block_no;
  802. yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
  803. block_no = chunk / dev->param.chunks_per_block;
  804. the_block = yaffs_get_block_info(dev, block_no);
  805. if (the_block) {
  806. the_block->soft_del_pages++;
  807. dev->n_free_chunks++;
  808. yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
  809. }
  810. }
  811. static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
  812. u32 level, int chunk_offset)
  813. {
  814. int i;
  815. int the_chunk;
  816. int all_done = 1;
  817. struct yaffs_dev *dev = in->my_dev;
  818. if (tn) {
  819. if (level > 0) {
  820. for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
  821. i--) {
  822. if (tn->internal[i]) {
  823. all_done =
  824. yaffs_soft_del_worker(in,
  825. tn->internal
  826. [i],
  827. level - 1,
  828. (chunk_offset
  829. <<
  830. YAFFS_TNODES_INTERNAL_BITS)
  831. + i);
  832. if (all_done) {
  833. yaffs_free_tnode(dev,
  834. tn->internal
  835. [i]);
  836. tn->internal[i] = NULL;
  837. } else {
  838. }
  839. }
  840. }
  841. return (all_done) ? 1 : 0;
  842. } else if (level == 0) {
  843. for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  844. the_chunk = yaffs_get_group_base(dev, tn, i);
  845. if (the_chunk) {
  846. yaffs_soft_del_chunk(dev, the_chunk);
  847. yaffs_load_tnode_0(dev, tn, i, 0);
  848. }
  849. }
  850. return 1;
  851. }
  852. }
  853. return 1;
  854. }
  855. static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
  856. {
  857. struct yaffs_dev *dev = obj->my_dev;
  858. struct yaffs_obj *parent;
  859. yaffs_verify_obj_in_dir(obj);
  860. parent = obj->parent;
  861. yaffs_verify_dir(parent);
  862. if (dev && dev->param.remove_obj_fn)
  863. dev->param.remove_obj_fn(obj);
  864. list_del_init(&obj->siblings);
  865. obj->parent = NULL;
  866. yaffs_verify_dir(parent);
  867. }
  868. void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
  869. {
  870. if (!directory) {
  871. yaffs_trace(YAFFS_TRACE_ALWAYS,
  872. "tragedy: Trying to add an object to a null pointer directory"
  873. );
  874. YBUG();
  875. return;
  876. }
  877. if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  878. yaffs_trace(YAFFS_TRACE_ALWAYS,
  879. "tragedy: Trying to add an object to a non-directory"
  880. );
  881. YBUG();
  882. }
  883. if (obj->siblings.prev == NULL) {
  884. YBUG();
  885. }
  886. yaffs_verify_dir(directory);
  887. yaffs_remove_obj_from_dir(obj);
  888. list_add(&obj->siblings, &directory->variant.dir_variant.children);
  889. obj->parent = directory;
  890. if (directory == obj->my_dev->unlinked_dir
  891. || directory == obj->my_dev->del_dir) {
  892. obj->unlinked = 1;
  893. obj->my_dev->n_unlinked_files++;
  894. obj->rename_allowed = 0;
  895. }
  896. yaffs_verify_dir(directory);
  897. yaffs_verify_obj_in_dir(obj);
  898. }
  899. static int yaffs_change_obj_name(struct yaffs_obj *obj,
  900. struct yaffs_obj *new_dir,
  901. const YCHAR * new_name, int force, int shadows)
  902. {
  903. int unlink_op;
  904. int del_op;
  905. struct yaffs_obj *existing_target;
  906. if (new_dir == NULL)
  907. new_dir = obj->parent;
  908. if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  909. yaffs_trace(YAFFS_TRACE_ALWAYS,
  910. "tragedy: yaffs_change_obj_name: new_dir is not a directory"
  911. );
  912. YBUG();
  913. }
  914. if (obj->my_dev->param.is_yaffs2)
  915. unlink_op = (new_dir == obj->my_dev->unlinked_dir);
  916. else
  917. unlink_op = (new_dir == obj->my_dev->unlinked_dir
  918. && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
  919. del_op = (new_dir == obj->my_dev->del_dir);
  920. existing_target = yaffs_find_by_name(new_dir, new_name);
  921. if ((unlink_op ||
  922. del_op ||
  923. force ||
  924. (shadows > 0) ||
  925. !existing_target) &&
  926. new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  927. yaffs_set_obj_name(obj, new_name);
  928. obj->dirty = 1;
  929. yaffs_add_obj_to_dir(new_dir, obj);
  930. if (unlink_op)
  931. obj->unlinked = 1;
  932. if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
  933. 0)
  934. return YAFFS_OK;
  935. }
  936. return YAFFS_FAIL;
  937. }
  938. static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
  939. {
  940. struct yaffs_dev *dev = obj->my_dev;
  941. int i;
  942. struct yaffs_cache *cache;
  943. int n_caches = obj->my_dev->param.n_caches;
  944. for (i = 0; i < n_caches; i++) {
  945. cache = &dev->cache[i];
  946. if (cache->object == obj && cache->dirty)
  947. return 1;
  948. }
  949. return 0;
  950. }
  951. static void yaffs_flush_file_cache(struct yaffs_obj *obj)
  952. {
  953. struct yaffs_dev *dev = obj->my_dev;
  954. int lowest = -99;
  955. int i;
  956. struct yaffs_cache *cache;
  957. int chunk_written = 0;
  958. int n_caches = obj->my_dev->param.n_caches;
  959. if (n_caches > 0) {
  960. do {
  961. cache = NULL;
  962. for (i = 0; i < n_caches; i++) {
  963. if (dev->cache[i].object == obj &&
  964. dev->cache[i].dirty) {
  965. if (!cache
  966. || dev->cache[i].chunk_id <
  967. lowest) {
  968. cache = &dev->cache[i];
  969. lowest = cache->chunk_id;
  970. }
  971. }
  972. }
  973. if (cache && !cache->locked) {
  974. chunk_written =
  975. yaffs_wr_data_obj(cache->object,
  976. cache->chunk_id,
  977. cache->data,
  978. cache->n_bytes, 1);
  979. cache->dirty = 0;
  980. cache->object = NULL;
  981. }
  982. } while (cache && chunk_written > 0);
  983. if (cache)
  984. yaffs_trace(YAFFS_TRACE_ERROR,
  985. "yaffs tragedy: no space during cache write");
  986. }
  987. }
  988. void yaffs_flush_whole_cache(struct yaffs_dev *dev)
  989. {
  990. struct yaffs_obj *obj;
  991. int n_caches = dev->param.n_caches;
  992. int i;
  993. do {
  994. obj = NULL;
  995. for (i = 0; i < n_caches && !obj; i++) {
  996. if (dev->cache[i].object && dev->cache[i].dirty)
  997. obj = dev->cache[i].object;
  998. }
  999. if (obj)
  1000. yaffs_flush_file_cache(obj);
  1001. } while (obj);
  1002. }
  1003. static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
  1004. {
  1005. int i;
  1006. if (dev->param.n_caches > 0) {
  1007. for (i = 0; i < dev->param.n_caches; i++) {
  1008. if (!dev->cache[i].object)
  1009. return &dev->cache[i];
  1010. }
  1011. }
  1012. return NULL;
  1013. }
  1014. static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
  1015. {
  1016. struct yaffs_cache *cache;
  1017. struct yaffs_obj *the_obj;
  1018. int usage;
  1019. int i;
  1020. int pushout;
  1021. if (dev->param.n_caches > 0) {
  1022. cache = yaffs_grab_chunk_worker(dev);
  1023. if (!cache) {
  1024. the_obj = NULL;
  1025. usage = -1;
  1026. cache = NULL;
  1027. pushout = -1;
  1028. for (i = 0; i < dev->param.n_caches; i++) {
  1029. if (dev->cache[i].object &&
  1030. !dev->cache[i].locked &&
  1031. (dev->cache[i].last_use < usage
  1032. || !cache)) {
  1033. usage = dev->cache[i].last_use;
  1034. the_obj = dev->cache[i].object;
  1035. cache = &dev->cache[i];
  1036. pushout = i;
  1037. }
  1038. }
  1039. if (!cache || cache->dirty) {
  1040. yaffs_flush_file_cache(the_obj);
  1041. cache = yaffs_grab_chunk_worker(dev);
  1042. }
  1043. }
  1044. return cache;
  1045. } else {
  1046. return NULL;
  1047. }
  1048. }
  1049. static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
  1050. int chunk_id)
  1051. {
  1052. struct yaffs_dev *dev = obj->my_dev;
  1053. int i;
  1054. if (dev->param.n_caches > 0) {
  1055. for (i = 0; i < dev->param.n_caches; i++) {
  1056. if (dev->cache[i].object == obj &&
  1057. dev->cache[i].chunk_id == chunk_id) {
  1058. dev->cache_hits++;
  1059. return &dev->cache[i];
  1060. }
  1061. }
  1062. }
  1063. return NULL;
  1064. }
  1065. static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
  1066. int is_write)
  1067. {
  1068. if (dev->param.n_caches > 0) {
  1069. if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
  1070. int i;
  1071. for (i = 1; i < dev->param.n_caches; i++)
  1072. dev->cache[i].last_use = 0;
  1073. dev->cache_last_use = 0;
  1074. }
  1075. dev->cache_last_use++;
  1076. cache->last_use = dev->cache_last_use;
  1077. if (is_write)
  1078. cache->dirty = 1;
  1079. }
  1080. }
  1081. /* Invalidate a single cache page.
  1082. * Do this when a whole page gets written,
  1083. * ie the short cache for this page is no longer valid.
  1084. */
  1085. static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
  1086. {
  1087. if (object->my_dev->param.n_caches > 0) {
  1088. struct yaffs_cache *cache =
  1089. yaffs_find_chunk_cache(object, chunk_id);
  1090. if (cache)
  1091. cache->object = NULL;
  1092. }
  1093. }
  1094. static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
  1095. {
  1096. int i;
  1097. struct yaffs_dev *dev = in->my_dev;
  1098. if (dev->param.n_caches > 0) {
  1099. for (i = 0; i < dev->param.n_caches; i++) {
  1100. if (dev->cache[i].object == in)
  1101. dev->cache[i].object = NULL;
  1102. }
  1103. }
  1104. }
  1105. static void yaffs_unhash_obj(struct yaffs_obj *obj)
  1106. {
  1107. int bucket;
  1108. struct yaffs_dev *dev = obj->my_dev;
  1109. if (!list_empty(&obj->hash_link)) {
  1110. list_del_init(&obj->hash_link);
  1111. bucket = yaffs_hash_fn(obj->obj_id);
  1112. dev->obj_bucket[bucket].count--;
  1113. }
  1114. }
  1115. static void yaffs_free_obj(struct yaffs_obj *obj)
  1116. {
  1117. struct yaffs_dev *dev = obj->my_dev;
  1118. yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
  1119. obj, obj->my_inode);
  1120. if (!obj)
  1121. YBUG();
  1122. if (obj->parent)
  1123. YBUG();
  1124. if (!list_empty(&obj->siblings))
  1125. YBUG();
  1126. if (obj->my_inode) {
  1127. obj->defered_free = 1;
  1128. return;
  1129. }
  1130. yaffs_unhash_obj(obj);
  1131. yaffs_free_raw_obj(dev, obj);
  1132. dev->n_obj--;
  1133. dev->checkpoint_blocks_required = 0;
  1134. }
  1135. void yaffs_handle_defered_free(struct yaffs_obj *obj)
  1136. {
  1137. if (obj->defered_free)
  1138. yaffs_free_obj(obj);
  1139. }
  1140. static int yaffs_generic_obj_del(struct yaffs_obj *in)
  1141. {
  1142. yaffs_invalidate_whole_cache(in);
  1143. if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
  1144. yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
  1145. 0);
  1146. }
  1147. yaffs_remove_obj_from_dir(in);
  1148. yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
  1149. in->hdr_chunk = 0;
  1150. yaffs_free_obj(in);
  1151. return YAFFS_OK;
  1152. }
  1153. static void yaffs_soft_del_file(struct yaffs_obj *obj)
  1154. {
  1155. if (obj->deleted &&
  1156. obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
  1157. if (obj->n_data_chunks <= 0) {
  1158. yaffs_free_tnode(obj->my_dev,
  1159. obj->variant.file_variant.top);
  1160. obj->variant.file_variant.top = NULL;
  1161. yaffs_trace(YAFFS_TRACE_TRACING,
  1162. "yaffs: Deleting empty file %d",
  1163. obj->obj_id);
  1164. yaffs_generic_obj_del(obj);
  1165. } else {
  1166. yaffs_soft_del_worker(obj,
  1167. obj->variant.file_variant.top,
  1168. obj->variant.
  1169. file_variant.top_level, 0);
  1170. obj->soft_del = 1;
  1171. }
  1172. }
  1173. }
  1174. static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
  1175. struct yaffs_tnode *tn, u32 level,
  1176. int del0)
  1177. {
  1178. int i;
  1179. int has_data;
  1180. if (tn) {
  1181. has_data = 0;
  1182. if (level > 0) {
  1183. for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  1184. if (tn->internal[i]) {
  1185. tn->internal[i] =
  1186. yaffs_prune_worker(dev,
  1187. tn->internal[i],
  1188. level - 1,
  1189. (i ==
  1190. 0) ? del0 : 1);
  1191. }
  1192. if (tn->internal[i])
  1193. has_data++;
  1194. }
  1195. } else {
  1196. int tnode_size_u32 = dev->tnode_size / sizeof(u32);
  1197. u32 *map = (u32 *) tn;
  1198. for (i = 0; !has_data && i < tnode_size_u32; i++) {
  1199. if (map[i])
  1200. has_data++;
  1201. }
  1202. }
  1203. if (has_data == 0 && del0) {
  1204. yaffs_free_tnode(dev, tn);
  1205. tn = NULL;
  1206. }
  1207. }
  1208. return tn;
  1209. }
  1210. static int yaffs_prune_tree(struct yaffs_dev *dev,
  1211. struct yaffs_file_var *file_struct)
  1212. {
  1213. int i;
  1214. int has_data;
  1215. int done = 0;
  1216. struct yaffs_tnode *tn;
  1217. if (file_struct->top_level > 0) {
  1218. file_struct->top =
  1219. yaffs_prune_worker(dev, file_struct->top,
  1220. file_struct->top_level, 0);
  1221. while (file_struct->top_level && !done) {
  1222. tn = file_struct->top;
  1223. has_data = 0;
  1224. for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  1225. if (tn->internal[i])
  1226. has_data++;
  1227. }
  1228. if (!has_data) {
  1229. file_struct->top = tn->internal[0];
  1230. file_struct->top_level--;
  1231. yaffs_free_tnode(dev, tn);
  1232. } else {
  1233. done = 1;
  1234. }
  1235. }
  1236. }
  1237. return YAFFS_OK;
  1238. }
  1239. static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
  1240. {
  1241. struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
  1242. if (obj) {
  1243. dev->n_obj++;
  1244. memset(obj, 0, sizeof(struct yaffs_obj));
  1245. obj->being_created = 1;
  1246. obj->my_dev = dev;
  1247. obj->hdr_chunk = 0;
  1248. obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
  1249. INIT_LIST_HEAD(&(obj->hard_links));
  1250. INIT_LIST_HEAD(&(obj->hash_link));
  1251. INIT_LIST_HEAD(&obj->siblings);
  1252. if (dev->root_dir) {
  1253. obj->parent = dev->root_dir;
  1254. list_add(&(obj->siblings),
  1255. &dev->root_dir->variant.dir_variant.children);
  1256. }
  1257. if (dev->lost_n_found)
  1258. yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  1259. obj->being_created = 0;
  1260. }
  1261. dev->checkpoint_blocks_required = 0;
  1262. return obj;
  1263. }
  1264. static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
  1265. {
  1266. int i;
  1267. int l = 999;
  1268. int lowest = 999999;
  1269. for (i = 0; i < 10 && lowest > 4; i++) {
  1270. dev->bucket_finder++;
  1271. dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
  1272. if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
  1273. lowest = dev->obj_bucket[dev->bucket_finder].count;
  1274. l = dev->bucket_finder;
  1275. }
  1276. }
  1277. return l;
  1278. }
  1279. static int yaffs_new_obj_id(struct yaffs_dev *dev)
  1280. {
  1281. int bucket = yaffs_find_nice_bucket(dev);
  1282. int found = 0;
  1283. struct list_head *i;
  1284. u32 n = (u32) bucket;
  1285. while (!found) {
  1286. found = 1;
  1287. n += YAFFS_NOBJECT_BUCKETS;
  1288. if (1 || dev->obj_bucket[bucket].count > 0) {
  1289. list_for_each(i, &dev->obj_bucket[bucket].list) {
  1290. if (i && list_entry(i, struct yaffs_obj,
  1291. hash_link)->obj_id == n) {
  1292. found = 0;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. return n;
  1298. }
  1299. static void yaffs_hash_obj(struct yaffs_obj *in)
  1300. {
  1301. int bucket = yaffs_hash_fn(in->obj_id);
  1302. struct yaffs_dev *dev = in->my_dev;
  1303. list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
  1304. dev->obj_bucket[bucket].count++;
  1305. }
  1306. struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
  1307. {
  1308. int bucket = yaffs_hash_fn(number);
  1309. struct list_head *i;
  1310. struct yaffs_obj *in;
  1311. list_for_each(i, &dev->obj_bucket[bucket].list) {
  1312. if (i) {
  1313. in = list_entry(i, struct yaffs_obj, hash_link);
  1314. if (in->obj_id == number) {
  1315. if (in->defered_free)
  1316. return NULL;
  1317. return in;
  1318. }
  1319. }
  1320. }
  1321. return NULL;
  1322. }
  1323. struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  1324. enum yaffs_obj_type type)
  1325. {
  1326. struct yaffs_obj *the_obj = NULL;
  1327. struct yaffs_tnode *tn = NULL;
  1328. if (number < 0)
  1329. number = yaffs_new_obj_id(dev);
  1330. if (type == YAFFS_OBJECT_TYPE_FILE) {
  1331. tn = yaffs_get_tnode(dev);
  1332. if (!tn)
  1333. return NULL;
  1334. }
  1335. the_obj = yaffs_alloc_empty_obj(dev);
  1336. if (!the_obj) {
  1337. if (tn)
  1338. yaffs_free_tnode(dev, tn);
  1339. return NULL;
  1340. }
  1341. if (the_obj) {
  1342. the_obj->fake = 0;
  1343. the_obj->rename_allowed = 1;
  1344. the_obj->unlink_allowed = 1;
  1345. the_obj->obj_id = number;
  1346. yaffs_hash_obj(the_obj);
  1347. the_obj->variant_type = type;
  1348. yaffs_load_current_time(the_obj, 1, 1);
  1349. switch (type) {
  1350. case YAFFS_OBJECT_TYPE_FILE:
  1351. the_obj->variant.file_variant.file_size = 0;
  1352. the_obj->variant.file_variant.scanned_size = 0;
  1353. the_obj->variant.file_variant.shrink_size = ~0;
  1354. the_obj->variant.file_variant.top_level = 0;
  1355. the_obj->variant.file_variant.top = tn;
  1356. break;
  1357. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1358. INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
  1359. INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
  1360. break;
  1361. case YAFFS_OBJECT_TYPE_SYMLINK:
  1362. case YAFFS_OBJECT_TYPE_HARDLINK:
  1363. case YAFFS_OBJECT_TYPE_SPECIAL:
  1364. break;
  1365. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1366. break;
  1367. }
  1368. }
  1369. return the_obj;
  1370. }
  1371. static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
  1372. int number, u32 mode)
  1373. {
  1374. struct yaffs_obj *obj =
  1375. yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  1376. if (obj) {
  1377. obj->fake = 1;
  1378. obj->rename_allowed = 0;
  1379. obj->unlink_allowed = 0;
  1380. obj->deleted = 0;
  1381. obj->unlinked = 0;
  1382. obj->yst_mode = mode;
  1383. obj->my_dev = dev;
  1384. obj->hdr_chunk = 0;
  1385. }
  1386. return obj;
  1387. }
  1388. static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
  1389. {
  1390. int i;
  1391. dev->n_obj = 0;
  1392. dev->n_tnodes = 0;
  1393. yaffs_init_raw_tnodes_and_objs(dev);
  1394. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  1395. INIT_LIST_HEAD(&dev->obj_bucket[i].list);
  1396. dev->obj_bucket[i].count = 0;
  1397. }
  1398. }
  1399. struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  1400. int number,
  1401. enum yaffs_obj_type type)
  1402. {
  1403. struct yaffs_obj *the_obj = NULL;
  1404. if (number > 0)
  1405. the_obj = yaffs_find_by_number(dev, number);
  1406. if (!the_obj)
  1407. the_obj = yaffs_new_obj(dev, number, type);
  1408. return the_obj;
  1409. }
  1410. YCHAR *yaffs_clone_str(const YCHAR * str)
  1411. {
  1412. YCHAR *new_str = NULL;
  1413. int len;
  1414. if (!str)
  1415. str = _Y("");
  1416. len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
  1417. new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
  1418. if (new_str) {
  1419. strncpy(new_str, str, len);
  1420. new_str[len] = 0;
  1421. }
  1422. return new_str;
  1423. }
  1424. static void yaffs_update_parent(struct yaffs_obj *obj)
  1425. {
  1426. struct yaffs_dev *dev;
  1427. if (!obj)
  1428. return;
  1429. dev = obj->my_dev;
  1430. obj->dirty = 1;
  1431. yaffs_load_current_time(obj, 0, 1);
  1432. if (dev->param.defered_dir_update) {
  1433. struct list_head *link = &obj->variant.dir_variant.dirty;
  1434. if (list_empty(link)) {
  1435. list_add(link, &dev->dirty_dirs);
  1436. yaffs_trace(YAFFS_TRACE_BACKGROUND,
  1437. "Added object %d to dirty directories",
  1438. obj->obj_id);
  1439. }
  1440. } else {
  1441. yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  1442. }
  1443. }
  1444. void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
  1445. {
  1446. struct list_head *link;
  1447. struct yaffs_obj *obj;
  1448. struct yaffs_dir_var *d_s;
  1449. union yaffs_obj_var *o_v;
  1450. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
  1451. while (!list_empty(&dev->dirty_dirs)) {
  1452. link = dev->dirty_dirs.next;
  1453. list_del_init(link);
  1454. d_s = list_entry(link, struct yaffs_dir_var, dirty);
  1455. o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
  1456. obj = list_entry(o_v, struct yaffs_obj, variant);
  1457. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
  1458. obj->obj_id);
  1459. if (obj->dirty)
  1460. yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  1461. }
  1462. }
  1463. static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
  1464. struct yaffs_obj *parent,
  1465. const YCHAR * name,
  1466. u32 mode,
  1467. u32 uid,
  1468. u32 gid,
  1469. struct yaffs_obj *equiv_obj,
  1470. const YCHAR * alias_str, u32 rdev)
  1471. {
  1472. struct yaffs_obj *in;
  1473. YCHAR *str = NULL;
  1474. struct yaffs_dev *dev = parent->my_dev;
  1475. if (yaffs_find_by_name(parent, name))
  1476. return NULL;
  1477. if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  1478. str = yaffs_clone_str(alias_str);
  1479. if (!str)
  1480. return NULL;
  1481. }
  1482. in = yaffs_new_obj(dev, -1, type);
  1483. if (!in) {
  1484. if (str)
  1485. kfree(str);
  1486. return NULL;
  1487. }
  1488. if (in) {
  1489. in->hdr_chunk = 0;
  1490. in->valid = 1;
  1491. in->variant_type = type;
  1492. in->yst_mode = mode;
  1493. yaffs_attribs_init(in, gid, uid, rdev);
  1494. in->n_data_chunks = 0;
  1495. yaffs_set_obj_name(in, name);
  1496. in->dirty = 1;
  1497. yaffs_add_obj_to_dir(parent, in);
  1498. in->my_dev = parent->my_dev;
  1499. switch (type) {
  1500. case YAFFS_OBJECT_TYPE_SYMLINK:
  1501. in->variant.symlink_variant.alias = str;
  1502. break;
  1503. case YAFFS_OBJECT_TYPE_HARDLINK:
  1504. in->variant.hardlink_variant.equiv_obj = equiv_obj;
  1505. in->variant.hardlink_variant.equiv_id =
  1506. equiv_obj->obj_id;
  1507. list_add(&in->hard_links, &equiv_obj->hard_links);
  1508. break;
  1509. case YAFFS_OBJECT_TYPE_FILE:
  1510. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1511. case YAFFS_OBJECT_TYPE_SPECIAL:
  1512. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1513. break;
  1514. }
  1515. if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
  1516. yaffs_del_obj(in);
  1517. in = NULL;
  1518. }
  1519. yaffs_update_parent(parent);
  1520. }
  1521. return in;
  1522. }
  1523. struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  1524. const YCHAR * name, u32 mode, u32 uid,
  1525. u32 gid)
  1526. {
  1527. return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  1528. uid, gid, NULL, NULL, 0);
  1529. }
  1530. struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
  1531. u32 mode, u32 uid, u32 gid)
  1532. {
  1533. return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  1534. mode, uid, gid, NULL, NULL, 0);
  1535. }
  1536. struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  1537. const YCHAR * name, u32 mode, u32 uid,
  1538. u32 gid, u32 rdev)
  1539. {
  1540. return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  1541. uid, gid, NULL, NULL, rdev);
  1542. }
  1543. struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  1544. const YCHAR * name, u32 mode, u32 uid,
  1545. u32 gid, const YCHAR * alias)
  1546. {
  1547. return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  1548. uid, gid, NULL, alias, 0);
  1549. }
  1550. struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  1551. struct yaffs_obj *equiv_obj)
  1552. {
  1553. equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
  1554. if (yaffs_create_obj
  1555. (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
  1556. equiv_obj, NULL, 0)) {
  1557. return equiv_obj;
  1558. } else {
  1559. return NULL;
  1560. }
  1561. }
  1562. static int yaffs_init_blocks(struct yaffs_dev *dev)
  1563. {
  1564. int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  1565. dev->block_info = NULL;
  1566. dev->chunk_bits = NULL;
  1567. dev->alloc_block = -1;
  1568. dev->block_info =
  1569. kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
  1570. if (!dev->block_info) {
  1571. dev->block_info =
  1572. vmalloc(n_blocks * sizeof(struct yaffs_block_info));
  1573. dev->block_info_alt = 1;
  1574. } else {
  1575. dev->block_info_alt = 0;
  1576. }
  1577. if (dev->block_info) {
  1578. dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
  1579. dev->chunk_bits =
  1580. kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
  1581. if (!dev->chunk_bits) {
  1582. dev->chunk_bits =
  1583. vmalloc(dev->chunk_bit_stride * n_blocks);
  1584. dev->chunk_bits_alt = 1;
  1585. } else {
  1586. dev->chunk_bits_alt = 0;
  1587. }
  1588. }
  1589. if (dev->block_info && dev->chunk_bits) {
  1590. memset(dev->block_info, 0,
  1591. n_blocks * sizeof(struct yaffs_block_info));
  1592. memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
  1593. return YAFFS_OK;
  1594. }
  1595. return YAFFS_FAIL;
  1596. }
  1597. static void yaffs_deinit_blocks(struct yaffs_dev *dev)
  1598. {
  1599. if (dev->block_info_alt && dev->block_info)
  1600. vfree(dev->block_info);
  1601. else if (dev->block_info)
  1602. kfree(dev->block_info);
  1603. dev->block_info_alt = 0;
  1604. dev->block_info = NULL;
  1605. if (dev->chunk_bits_alt && dev->chunk_bits)
  1606. vfree(dev->chunk_bits);
  1607. else if (dev->chunk_bits)
  1608. kfree(dev->chunk_bits);
  1609. dev->chunk_bits_alt = 0;
  1610. dev->chunk_bits = NULL;
  1611. }
  1612. void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
  1613. {
  1614. struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
  1615. int erased_ok = 0;
  1616. yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  1617. "yaffs_block_became_dirty block %d state %d %s",
  1618. block_no, bi->block_state,
  1619. (bi->needs_retiring) ? "needs retiring" : "");
  1620. yaffs2_clear_oldest_dirty_seq(dev, bi);
  1621. bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
  1622. if (block_no == dev->gc_block)
  1623. dev->gc_block = 0;
  1624. if (block_no == dev->gc_dirtiest) {
  1625. dev->gc_dirtiest = 0;
  1626. dev->gc_pages_in_use = 0;
  1627. }
  1628. if (!bi->needs_retiring) {
  1629. yaffs2_checkpt_invalidate(dev);
  1630. erased_ok = yaffs_erase_block(dev, block_no);
  1631. if (!erased_ok) {
  1632. dev->n_erase_failures++;
  1633. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  1634. "**>> Erasure failed %d", block_no);
  1635. }
  1636. }
  1637. if (erased_ok &&
  1638. ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
  1639. || !yaffs_skip_verification(dev))) {
  1640. int i;
  1641. for (i = 0; i < dev->param.chunks_per_block; i++) {
  1642. if (!yaffs_check_chunk_erased
  1643. (dev, block_no * dev->param.chunks_per_block + i)) {
  1644. yaffs_trace(YAFFS_TRACE_ERROR,
  1645. ">>Block %d erasure supposedly OK, but chunk %d not erased",
  1646. block_no, i);
  1647. }
  1648. }
  1649. }
  1650. if (erased_ok) {
  1651. bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  1652. bi->seq_number = 0;
  1653. dev->n_erased_blocks++;
  1654. bi->pages_in_use = 0;
  1655. bi->soft_del_pages = 0;
  1656. bi->has_shrink_hdr = 0;
  1657. bi->skip_erased_check = 1;
  1658. bi->gc_prioritise = 0;
  1659. yaffs_clear_chunk_bits(dev, block_no);
  1660. yaffs_trace(YAFFS_TRACE_ERASE,
  1661. "Erased block %d", block_no);
  1662. } else {
  1663. dev->n_free_chunks -= dev->param.chunks_per_block;
  1664. yaffs_retire_block(dev, block_no);
  1665. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  1666. "**>> Block %d retired", block_no);
  1667. }
  1668. }
  1669. static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
  1670. {
  1671. int old_chunk;
  1672. int new_chunk;
  1673. int mark_flash;
  1674. int ret_val = YAFFS_OK;
  1675. int i;
  1676. int is_checkpt_block;
  1677. int matching_chunk;
  1678. int max_copies;
  1679. int chunks_before = yaffs_get_erased_chunks(dev);
  1680. int chunks_after;
  1681. struct yaffs_ext_tags tags;
  1682. struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
  1683. struct yaffs_obj *object;
  1684. is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
  1685. yaffs_trace(YAFFS_TRACE_TRACING,
  1686. "Collecting block %d, in use %d, shrink %d, whole_block %d",
  1687. block, bi->pages_in_use, bi->has_shrink_hdr,
  1688. whole_block);
  1689. if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
  1690. bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
  1691. bi->has_shrink_hdr = 0;
  1692. dev->gc_disable = 1;
  1693. if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
  1694. yaffs_trace(YAFFS_TRACE_TRACING,
  1695. "Collecting block %d that has no chunks in use",
  1696. block);
  1697. yaffs_block_became_dirty(dev, block);
  1698. } else {
  1699. u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  1700. yaffs_verify_blk(dev, bi, block);
  1701. max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
  1702. old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
  1703. for ( ;
  1704. ret_val == YAFFS_OK &&
  1705. dev->gc_chunk < dev->param.chunks_per_block &&
  1706. (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
  1707. max_copies > 0; dev->gc_chunk++, old_chunk++) {
  1708. if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
  1709. max_copies--;
  1710. mark_flash = 1;
  1711. yaffs_init_tags(&tags);
  1712. yaffs_rd_chunk_tags_nand(dev, old_chunk,
  1713. buffer, &tags);
  1714. object = yaffs_find_by_number(dev, tags.obj_id);
  1715. yaffs_trace(YAFFS_TRACE_GC_DETAIL,
  1716. "Collecting chunk in block %d, %d %d %d ",
  1717. dev->gc_chunk, tags.obj_id,
  1718. tags.chunk_id, tags.n_bytes);
  1719. if (object && !yaffs_skip_verification(dev)) {
  1720. if (tags.chunk_id == 0)
  1721. matching_chunk =
  1722. object->hdr_chunk;
  1723. else if (object->soft_del)
  1724. matching_chunk = old_chunk;
  1725. else
  1726. matching_chunk =
  1727. yaffs_find_chunk_in_file
  1728. (object, tags.chunk_id,
  1729. NULL);
  1730. if (old_chunk != matching_chunk)
  1731. yaffs_trace(YAFFS_TRACE_ERROR,
  1732. "gc: page in gc mismatch: %d %d %d %d",
  1733. old_chunk,
  1734. matching_chunk,
  1735. tags.obj_id,
  1736. tags.chunk_id);
  1737. }
  1738. if (!object) {
  1739. yaffs_trace(YAFFS_TRACE_ERROR,
  1740. "page %d in gc has no object: %d %d %d ",
  1741. old_chunk,
  1742. tags.obj_id, tags.chunk_id,
  1743. tags.n_bytes);
  1744. }
  1745. if (object &&
  1746. object->deleted &&
  1747. object->soft_del && tags.chunk_id != 0) {
  1748. dev->n_free_chunks--;
  1749. bi->soft_del_pages--;
  1750. object->n_data_chunks--;
  1751. if (object->n_data_chunks <= 0) {
  1752. dev->gc_cleanup_list[dev->
  1753. n_clean_ups]
  1754. = tags.obj_id;
  1755. dev->n_clean_ups++;
  1756. }
  1757. mark_flash = 0;
  1758. } else if (0) {
  1759. object->hdr_chunk = 0;
  1760. yaffs_free_tnode(object->my_dev,
  1761. object->
  1762. variant.file_variant.
  1763. top);
  1764. object->variant.file_variant.top = NULL;
  1765. yaffs_generic_obj_del(object);
  1766. } else if (object) {
  1767. tags.serial_number++;
  1768. dev->n_gc_copies++;
  1769. if (tags.chunk_id == 0) {
  1770. struct yaffs_obj_hdr *oh;
  1771. oh = (struct yaffs_obj_hdr *)
  1772. buffer;
  1773. oh->is_shrink = 0;
  1774. tags.extra_is_shrink = 0;
  1775. oh->shadows_obj = 0;
  1776. oh->inband_shadowed_obj_id = 0;
  1777. tags.extra_shadows = 0;
  1778. if (object->variant_type ==
  1779. YAFFS_OBJECT_TYPE_FILE) {
  1780. oh->file_size =
  1781. object->variant.
  1782. file_variant.
  1783. file_size;
  1784. tags.extra_length =
  1785. oh->file_size;
  1786. }
  1787. yaffs_verify_oh(object, oh,
  1788. &tags, 1);
  1789. new_chunk =
  1790. yaffs_write_new_chunk(dev,
  1791. (u8 *)

Large files files are truncated, but you can click here to view the full file