Lines Matching refs:dev
49 void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
55 chunk = (u32) (addr >> dev->chunk_shift);
57 if (dev->chunk_div == 1) {
59 offset = (u32) (addr & dev->chunk_mask);
65 chunk /= dev->chunk_div;
67 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
125 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
130 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
133 dev->temp_buffer[i].in_use = 0;
134 buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
135 dev->temp_buffer[i].buffer = buf;
141 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
145 dev->temp_in_use++;
146 if (dev->temp_in_use > dev->max_temp)
147 dev->max_temp = dev->temp_in_use;
150 if (dev->temp_buffer[i].in_use == 0) {
151 dev->temp_buffer[i].in_use = 1;
152 return dev->temp_buffer[i].buffer;
162 dev->unmanaged_buffer_allocs++;
163 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
167 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
171 dev->temp_in_use--;
174 if (dev->temp_buffer[i].buffer == buffer) {
175 dev->temp_buffer[i].in_use = 0;
185 dev->unmanaged_buffer_deallocs++;
195 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
199 (void) dev;
205 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
208 (void) dev;
213 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
218 dev->has_pending_prioritised_gc = 1;
230 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
233 int flash_block = nand_chunk / dev->param.chunks_per_block;
234 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
236 yaffs_handle_chunk_error(dev, bi);
247 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
248 yaffs_skip_rest_of_block(dev);
271 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
273 return dev->root_dir;
276 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
278 return dev->lost_n_found;
296 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
299 u8 *data = yaffs_get_temp_buffer(dev);
303 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
308 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
315 yaffs_release_temp_buffer(dev, data);
321 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
328 u8 *buffer = yaffs_get_temp_buffer(dev);
331 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
332 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
338 yaffs_release_temp_buffer(dev, buffer);
344 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
347 int reserved_blocks = dev->param.n_reserved_blocks;
350 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
353 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
355 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
358 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
363 if (dev->n_erased_blocks < 1) {
375 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
376 dev->alloc_block_finder++;
377 if (dev->alloc_block_finder < dev->internal_start_block
378 || dev->alloc_block_finder > dev->internal_end_block) {
379 dev->alloc_block_finder = dev->internal_start_block;
382 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
386 dev->seq_number++;
387 bi->seq_number = dev->seq_number;
388 dev->n_erased_blocks--;
391 dev->alloc_block_finder, dev->seq_number,
392 dev->n_erased_blocks);
393 return dev->alloc_block_finder;
399 dev->n_erased_blocks);
404 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
410 if (dev->alloc_block < 0) {
412 dev->alloc_block = yaffs_find_alloc_block(dev);
413 dev->alloc_page = 0;
416 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
421 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
422 && dev->alloc_page == 0)
426 if (dev->alloc_block >= 0) {
427 bi = yaffs_get_block_info(dev, dev->alloc_block);
429 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
430 dev->alloc_page;
432 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
434 dev->alloc_page++;
436 dev->n_free_chunks--;
439 if (dev->alloc_page >= dev->param.chunks_per_block) {
441 dev->alloc_block = -1;
456 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
460 n = dev->n_erased_blocks * dev->param.chunks_per_block;
462 if (dev->alloc_block > 0)
463 n += (dev->param.chunks_per_block - dev->alloc_page);
473 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
477 if (dev->alloc_block > 0) {
478 bi = yaffs_get_block_info(dev, dev->alloc_block);
481 dev->alloc_block = -1;
486 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
494 yaffs2_checkpt_invalidate(dev);
500 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
533 if (dev->param.always_check_erased)
537 erased_ok = yaffs_check_chunk_erased(dev, chunk);
546 yaffs_chunk_del(dev, chunk, 1, __LINE__);
547 yaffs_skip_rest_of_block(dev);
552 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
556 yaffs_verify_chunk_written(dev, chunk, data, tags);
561 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
568 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
580 dev->n_retried_writes += (attempts - 1);
590 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
592 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
594 yaffs2_checkpt_invalidate(dev);
596 yaffs2_clear_oldest_dirty_seq(dev, bi);
598 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
599 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
606 flash_block * dev->param.chunks_per_block;
608 u8 *buffer = yaffs_get_temp_buffer(dev);
610 memset(buffer, 0xff, dev->data_bytes_per_chunk);
613 if (dev->tagger.write_chunk_tags_fn(dev, chunk_id -
614 dev->chunk_offset,
621 yaffs_release_temp_buffer(dev, buffer);
629 dev->n_retired_blocks++;
634 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
638 if (dev->param.auto_unicode) {
655 (void) dev;
662 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
670 if (dev->param.auto_unicode) {
699 dev = dev;
756 loff_t yaffs_max_file_size(struct yaffs_dev *dev)
761 return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
771 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
773 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
776 memset(tn, 0, dev->tnode_size);
777 dev->n_tnodes++;
780 dev->checkpoint_blocks_required = 0; /* force recalculation */
786 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
788 yaffs_free_raw_tnode(dev, tn);
789 dev->n_tnodes--;
790 dev->checkpoint_blocks_required = 0; /* force recalculation */
793 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
795 yaffs_deinit_raw_tnodes_and_objs(dev);
796 dev->n_obj = 0;
797 dev->n_tnodes = 0;
800 static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
810 val >>= dev->chunk_grp_bits;
812 bit_in_map = pos * dev->tnode_width;
816 mask = dev->tnode_mask << bit_in_word;
821 if (dev->tnode_width > (32 - bit_in_word)) {
825 dev->tnode_mask >> bit_in_word;
831 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
842 bit_in_map = pos * dev->tnode_width;
848 if (dev->tnode_width > (32 - bit_in_word)) {
854 val &= dev->tnode_mask;
855 val <<= dev->chunk_grp_bits;
868 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
877 (void) dev;
923 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
955 tn = yaffs_get_tnode(dev);
983 tn->internal[x] = yaffs_get_tnode(dev);
991 yaffs_free_tnode(dev,
997 tn->internal[x] = yaffs_get_tnode(dev);
1010 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
1011 yaffs_free_tnode(dev, passed_tn);
1027 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
1033 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
1035 (dev, the_chunk / dev->param.chunks_per_block,
1036 the_chunk % dev->param.chunks_per_block)) {
1038 if (dev->chunk_grp_size == 1)
1041 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
1063 struct yaffs_dev *dev = in->my_dev;
1070 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1075 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1077 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1089 struct yaffs_dev *dev = in->my_dev;
1097 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1102 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1104 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1109 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1125 struct yaffs_dev *dev = in->my_dev;
1143 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1147 tn = yaffs_add_find_tnode_0(dev,
1157 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1188 yaffs_rd_chunk_tags_nand(dev,
1224 yaffs_chunk_del(dev, existing_cunk, 1,
1232 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1242 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1247 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1254 block_no = chunk / dev->param.chunks_per_block;
1255 the_block = yaffs_get_block_info(dev, block_no);
1258 dev->n_free_chunks++;
1259 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1277 struct yaffs_dev *dev = in->my_dev;
1295 yaffs_free_tnode(dev,
1308 the_chunk = yaffs_get_group_base(dev, tn, i);
1310 yaffs_soft_del_chunk(dev, the_chunk);
1311 yaffs_load_tnode_0(dev, tn, i, 0);
1319 struct yaffs_dev *dev = obj->my_dev;
1327 if (dev && dev->param.remove_obj_fn)
1328 dev->param.remove_obj_fn(obj);
1438 struct yaffs_dev *dev = obj->my_dev;
1444 cache = &dev->cache[i];
1475 struct yaffs_dev *dev = obj->my_dev;
1486 cache = &dev->cache[i];
1494 void yaffs_flush_whole_cache(struct yaffs_dev *dev, int discard)
1497 int n_caches = dev->param.n_caches;
1506 if (dev->cache[i].object && dev->cache[i].dirty)
1507 obj = dev->cache[i].object;
1520 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1524 if (dev->param.n_caches > 0) {
1525 for (i = 0; i < dev->param.n_caches; i++) {
1526 if (!dev->cache[i].object)
1527 return &dev->cache[i];
1534 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1540 if (dev->param.n_caches < 1)
1545 cache = yaffs_grab_chunk_worker(dev);
1558 for (i = 0; i < dev->param.n_caches; i++) {
1559 if (dev->cache[i].object &&
1560 !dev->cache[i].locked &&
1561 (dev->cache[i].last_use < usage || !cache)) {
1562 usage = dev->cache[i].last_use;
1563 cache = &dev->cache[i];
1571 cache = yaffs_grab_chunk_worker(dev);
1581 struct yaffs_dev *dev = obj->my_dev;
1584 if (dev->param.n_caches < 1)
1587 for (i = 0; i < dev->param.n_caches; i++) {
1588 if (dev->cache[i].object == obj &&
1589 dev->cache[i].chunk_id == chunk_id) {
1590 dev->cache_hits++;
1592 return &dev->cache[i];
1599 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1604 if (dev->param.n_caches < 1)
1607 if (dev->cache_last_use < 0 ||
1608 dev->cache_last_use > 100000000) {
1610 for (i = 1; i < dev->param.n_caches; i++)
1611 dev->cache[i].last_use = 0;
1613 dev->cache_last_use = 0;
1615 dev->cache_last_use++;
1616 cache->last_use = dev->cache_last_use;
1644 struct yaffs_dev *dev = in->my_dev;
1646 if (dev->param.n_caches > 0) {
1648 for (i = 0; i < dev->param.n_caches; i++) {
1649 if (dev->cache[i].object == in)
1650 dev->cache[i].object = NULL;
1658 struct yaffs_dev *dev = obj->my_dev;
1664 dev->obj_bucket[bucket].count--;
1671 struct yaffs_dev *dev;
1677 dev = obj->my_dev;
1695 yaffs_free_raw_obj(dev, obj);
1696 dev->n_obj--;
1697 dev->checkpoint_blocks_required = 0; /* force recalculation */
1766 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1782 yaffs_prune_worker(dev,
1792 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1803 yaffs_free_tnode(dev, tn);
1809 static int yaffs_prune_tree(struct yaffs_dev *dev,
1821 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1842 yaffs_free_tnode(dev, tn);
1854 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1856 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1861 dev->n_obj++;
1868 obj->my_dev = dev;
1876 if (dev->root_dir) {
1877 obj->parent = dev->root_dir;
1879 &dev->root_dir->variant.dir_variant.children);
1886 if (dev->lost_n_found)
1887 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1891 dev->checkpoint_blocks_required = 0; /* force recalculation */
1896 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1907 dev->bucket_finder++;
1908 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1909 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1910 lowest = dev->obj_bucket[dev->bucket_finder].count;
1911 l = dev->bucket_finder;
1918 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1920 int bucket = yaffs_find_nice_bucket(dev);
1932 list_for_each(i, &dev->obj_bucket[bucket].list) {
1945 struct yaffs_dev *dev = in->my_dev;
1947 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1948 dev->obj_bucket[bucket].count++;
1951 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1957 list_for_each(i, &dev->obj_bucket[bucket].list) {
1971 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1978 number = yaffs_new_obj_id(dev);
1981 tn = yaffs_get_tnode(dev);
1986 the_obj = yaffs_alloc_empty_obj(dev);
1989 yaffs_free_tnode(dev, tn);
2006 yaffs_max_file_size(dev);
2026 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2031 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2042 obj->my_dev = dev;
2049 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2053 dev->n_obj = 0;
2054 dev->n_tnodes = 0;
2055 yaffs_init_raw_tnodes_and_objs(dev);
2058 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2059 dev->obj_bucket[i].count = 0;
2063 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2070 the_obj = yaffs_find_by_number(dev, number);
2073 the_obj = yaffs_new_obj(dev, number, type);
2114 struct yaffs_dev *dev;
2118 dev = obj->my_dev;
2121 if (dev->param.defered_dir_update) {
2125 list_add(link, &dev->dirty_dirs);
2136 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2145 while (!list_empty(&dev->dirty_dirs)) {
2146 link = dev->dirty_dirs.next;
2179 struct yaffs_dev *dev = parent->my_dev;
2192 in = yaffs_new_obj(dev, -1, type);
2296 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2298 if (dev->block_info_alt && dev->block_info)
2299 vfree(dev->block_info);
2301 kfree(dev->block_info);
2303 dev->block_info_alt = 0;
2305 dev->block_info = NULL;
2307 if (dev->chunk_bits_alt && dev->chunk_bits)
2308 vfree(dev->chunk_bits);
2310 kfree(dev->chunk_bits);
2311 dev->chunk_bits_alt = 0;
2312 dev->chunk_bits = NULL;
2315 static int yaffs_init_blocks(struct yaffs_dev *dev)
2317 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2319 dev->block_info = NULL;
2320 dev->chunk_bits = NULL;
2321 dev->alloc_block = -1; /* force it to get a new one */
2324 dev->block_info =
2326 if (!dev->block_info) {
2327 dev->block_info =
2329 dev->block_info_alt = 1;
2331 dev->block_info_alt = 0;
2334 if (!dev->block_info)
2338 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2339 dev->chunk_bits =
2340 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2341 if (!dev->chunk_bits) {
2342 dev->chunk_bits =
2343 vmalloc(dev->chunk_bit_stride * n_blocks);
2344 dev->chunk_bits_alt = 1;
2346 dev->chunk_bits_alt = 0;
2348 if (!dev->chunk_bits)
2352 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2353 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2357 yaffs_deinit_blocks(dev);
2362 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2364 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2377 yaffs2_clear_oldest_dirty_seq(dev, bi);
2382 if (block_no == dev->gc_block)
2383 dev->gc_block = 0;
2387 if (block_no == dev->gc_dirtiest) {
2388 dev->gc_dirtiest = 0;
2389 dev->gc_pages_in_use = 0;
2393 yaffs2_checkpt_invalidate(dev);
2394 erased_ok = yaffs_erase_block(dev, block_no);
2396 dev->n_erase_failures++;
2405 !yaffs_skip_verification(dev))) {
2406 for (i = 0; i < dev->param.chunks_per_block; i++) {
2407 if (!yaffs_check_chunk_erased(dev,
2408 block_no * dev->param.chunks_per_block + i)) {
2418 dev->n_free_chunks -= dev->param.chunks_per_block;
2419 yaffs_retire_block(dev, block_no);
2428 dev->n_erased_blocks++;
2436 yaffs_clear_chunk_bits(dev, block_no);
2441 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2453 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2455 object = yaffs_find_by_number(dev, tags.obj_id);
2459 dev->gc_chunk, tags.obj_id,
2462 if (object && !yaffs_skip_verification(dev)) {
2509 dev->n_free_chunks--;
2515 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2516 dev->n_clean_ups++;
2528 dev->n_gc_copies++;
2557 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2560 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2581 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2585 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2592 int chunks_before = yaffs_get_erased_chunks(dev);
2594 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2603 /*yaffs_verify_free_chunks(dev); */
2610 dev->gc_disable = 1;
2612 yaffs_summary_gc(dev, block);
2614 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2618 yaffs_block_became_dirty(dev, block);
2621 u8 *buffer = yaffs_get_temp_buffer(dev);
2623 yaffs_verify_blk(dev, bi, block);
2625 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2626 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2630 dev->gc_chunk < dev->param.chunks_per_block &&
2633 dev->gc_chunk++, old_chunk++) {
2634 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2637 ret_val = yaffs_gc_process_chunk(dev, bi,
2641 yaffs_release_temp_buffer(dev, buffer);
2644 yaffs_verify_collected_blk(dev, bi, block);
2655 for (i = 0; i < dev->n_clean_ups; i++) {
2658 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2660 yaffs_free_tnode(dev,
2671 chunks_after = yaffs_get_erased_chunks(dev);
2676 dev->gc_block = 0;
2677 dev->gc_chunk = 0;
2678 dev->n_clean_ups = 0;
2681 dev->gc_disable = 0;
2691 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2703 if (dev->has_pending_prioritised_gc && !aggressive) {
2704 dev->gc_dirtiest = 0;
2705 bi = dev->block_info;
2706 for (i = dev->internal_start_block;
2707 i <= dev->internal_end_block && !selected; i++) {
2712 yaffs_block_ok_for_gc(dev, bi)) {
2727 !selected && dev->oldest_dirty_block > 0)
2728 selected = dev->oldest_dirty_block;
2731 dev->has_pending_prioritised_gc = 0;
2743 dev->internal_end_block - dev->internal_start_block + 1;
2745 threshold = dev->param.chunks_per_block;
2751 max_threshold = dev->param.chunks_per_block / 2;
2753 max_threshold = dev->param.chunks_per_block / 8;
2758 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2771 (dev->gc_dirtiest < 1 ||
2772 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2774 dev->gc_block_finder++;
2775 if (dev->gc_block_finder < dev->internal_start_block ||
2776 dev->gc_block_finder > dev->internal_end_block)
2777 dev->gc_block_finder =
2778 dev->internal_start_block;
2780 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2785 pages_used < dev->param.chunks_per_block &&
2786 (dev->gc_dirtiest < 1 ||
2787 pages_used < dev->gc_pages_in_use) &&
2788 yaffs_block_ok_for_gc(dev, bi)) {
2789 dev->gc_dirtiest = dev->gc_block_finder;
2790 dev->gc_pages_in_use = pages_used;
2794 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2795 selected = dev->gc_dirtiest;
2803 if (!selected && dev->param.is_yaffs2 &&
2804 dev->gc_not_done >= (background ? 10 : 20)) {
2805 yaffs2_find_oldest_dirty_seq(dev);
2806 if (dev->oldest_dirty_block > 0) {
2807 selected = dev->oldest_dirty_block;
2808 dev->gc_dirtiest = selected;
2809 dev->oldest_dirty_gc_count++;
2810 bi = yaffs_get_block_info(dev, selected);
2811 dev->gc_pages_in_use =
2814 dev->gc_not_done = 0;
2822 dev->param.chunks_per_block - dev->gc_pages_in_use,
2825 dev->n_gc_blocks++;
2827 dev->bg_gcs++;
2829 dev->gc_dirtiest = 0;
2830 dev->gc_pages_in_use = 0;
2831 dev->gc_not_done = 0;
2832 if (dev->refresh_skip > 0)
2833 dev->refresh_skip--;
2835 dev->gc_not_done++;
2838 dev->gc_block_finder, dev->gc_not_done, threshold,
2839 dev->gc_dirtiest, dev->gc_pages_in_use,
2840 dev->oldest_dirty_block, background ? " bg" : "");
2855 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2864 if (dev->param.gc_control_fn &&
2865 (dev->param.gc_control_fn(dev) & 1) == 0)
2868 if (dev->gc_disable)
2879 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2882 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2884 dev->n_erased_blocks * dev->param.chunks_per_block;
2887 if (dev->n_erased_blocks < min_erased)
2891 && erased_chunks > (dev->n_free_chunks / 4))
2894 if (dev->gc_skip > 20)
2895 dev->gc_skip = 20;
2896 if (erased_chunks < dev->n_free_chunks / 2 ||
2897 dev->gc_skip < 1 || background)
2900 dev->gc_skip--;
2905 dev->gc_skip = 5;
2910 if (dev->gc_block < 1 && !aggressive) {
2911 dev->gc_block = yaffs2_find_refresh_block(dev);
2912 dev->gc_chunk = 0;
2913 dev->n_clean_ups = 0;
2915 if (dev->gc_block < 1) {
2916 dev->gc_block =
2917 yaffs_find_gc_block(dev, aggressive, background);
2918 dev->gc_chunk = 0;
2919 dev->n_clean_ups = 0;
2922 if (dev->gc_block > 0) {
2923 dev->all_gcs++;
2925 dev->passive_gc_count++;
2929 dev->n_erased_blocks, aggressive);
2931 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2934 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
2935 dev->gc_block > 0) {
2938 dev->n_erased_blocks, max_tries,
2939 dev->gc_block);
2941 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2942 (dev->gc_block > 0) && (max_tries < 2));
2952 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2954 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2958 yaffs_check_gc(dev, 1);
2959 return erased_chunks > dev->n_free_chunks / 2;
2982 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2993 dev->n_deletions++;
2994 block = chunk_id / dev->param.chunks_per_block;
2995 page = chunk_id % dev->param.chunks_per_block;
2997 if (!yaffs_check_chunk_bit(dev, block, page))
3001 bi = yaffs_get_block_info(dev, block);
3003 yaffs2_update_oldest_dirty_seq(dev, block, bi);
3009 if (!dev->param.is_yaffs2 && mark_flash &&
3014 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3015 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3017 dev->n_unmarked_deletions++;
3027 dev->n_free_chunks++;
3028 yaffs_clear_chunk_bit(dev, block, page);
3035 yaffs_block_became_dirty(dev, block);
3052 struct yaffs_dev *dev = in->my_dev;
3054 yaffs_check_gc(dev, 0);
3074 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3082 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3088 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3125 struct yaffs_dev *dev = obj->my_dev;
3126 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3149 struct yaffs_dev *dev = obj->my_dev;
3151 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3168 buffer = (char *)yaffs_get_temp_buffer(dev);
3173 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3190 yaffs_release_temp_buffer(dev, (u8 *) buffer);
3220 struct yaffs_dev *dev;
3228 dev = in->my_dev;
3230 buf = yaffs_get_temp_buffer(dev);
3232 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3245 yaffs_release_temp_buffer(dev, buf);
3256 struct yaffs_dev *dev = in->my_dev;
3271 if (in->fake && in != dev->root_dir && !force && !xmod)
3274 yaffs_check_gc(dev, 0);
3283 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3290 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3306 yaffs_load_oh_from_name(dev, oh->name, name);
3366 yaffs_write_new_chunk(dev, buffer, &new_tags,
3370 yaffs_release_temp_buffer(dev, buffer);
3378 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3414 struct yaffs_dev *dev;
3416 dev = in->my_dev;
3419 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3425 if ((start + n) < dev->data_bytes_per_chunk)
3428 n_copy = dev->data_bytes_per_chunk - start;
3436 if (cache || n_copy != dev->data_bytes_per_chunk ||
3437 dev->param.inband_tags) {
3438 if (dev->param.n_caches > 0) {
3455 yaffs_use_cache(dev, cache, 0);
3466 yaffs_get_temp_buffer(dev);
3471 yaffs_release_temp_buffer(dev, local_buffer);
3499 struct yaffs_dev *dev;
3501 dev = in->my_dev;
3504 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3507 dev->data_bytes_per_chunk + start != offset ||
3508 start >= dev->data_bytes_per_chunk) {
3519 if ((start + n) < dev->data_bytes_per_chunk) {
3529 dev->data_bytes_per_chunk);
3538 if (n_bytes_read > dev->data_bytes_per_chunk)
3539 n_bytes_read = dev->data_bytes_per_chunk;
3546 n_writeback > dev->data_bytes_per_chunk)
3550 n_copy = dev->data_bytes_per_chunk - start;
3551 n_writeback = dev->data_bytes_per_chunk;
3554 if (n_copy != dev->data_bytes_per_chunk ||
3555 !dev->param.cache_bypass_aligned ||
3556 dev->param.inband_tags) {
3562 if (dev->param.n_caches > 0) {
3570 yaffs_check_alloc_available(dev, 1)) {
3571 cache = yaffs_grab_chunk_cache(dev);
3580 !yaffs_check_alloc_available(dev,
3590 yaffs_use_cache(dev, cache, 1);
3617 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3627 yaffs_release_temp_buffer(dev, local_buffer);
3634 dev->data_bytes_per_chunk, 0);
3670 struct yaffs_dev *dev = in->my_dev;
3679 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3683 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3703 (dev->internal_start_block * dev->param.chunks_per_block) ||
3705 ((dev->internal_end_block + 1) *
3706 dev->param.chunks_per_block)) {
3712 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3721 struct yaffs_dev *dev = obj->my_dev;
3723 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3729 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3734 dev->data_bytes_per_chunk - new_partial);
3739 yaffs_release_temp_buffer(dev, local_buffer);
3744 yaffs_prune_tree(dev, &obj->variant.file_variant);
3749 struct yaffs_dev *dev = in->my_dev;
3755 yaffs_check_gc(dev, 0);
3814 struct yaffs_dev *dev = in->my_dev;
3828 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3843 struct yaffs_dev *dev = in->my_dev;
3845 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
4109 struct yaffs_dev *dev;
4120 dev = old_dir->my_dev;
4158 dev->gc_disable = 1;
4163 dev->gc_disable = 0;
4179 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4188 obj = yaffs_find_by_number(dev, obj_id);
4198 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4202 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4207 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4216 in = yaffs_find_by_number(dev,
4233 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4242 if (dev->read_only)
4247 &dev->unlinked_dir->variant.dir_variant.children) {
4252 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4276 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4278 return (obj == dev->del_dir ||
4279 obj == dev->unlinked_dir || obj == dev->root_dir);
4282 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4292 if (dev->read_only)
4301 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4305 if (yaffs_has_null_parent(dev, obj)) {
4312 } else if (yaffs_has_null_parent(dev, parent)) {
4322 while (parent != dev->root_dir &&
4330 if (parent != dev->root_dir)
4337 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4366 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4368 yaffs_del_dir_contents(dev->lost_n_found);
4594 static int yaffs_check_dev_fns(struct yaffs_dev *dev)
4596 struct yaffs_driver *drv = &dev->drv;
4597 struct yaffs_tags_handler *tagger = &dev->tagger;
4605 if (dev->param.is_yaffs2 &&
4610 yaffs_tags_compat_install(dev);
4611 yaffs_tags_marshall_install(dev);
4623 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4626 dev->lost_n_found = dev->root_dir = NULL;
4627 dev->unlinked_dir = dev->del_dir = NULL;
4628 dev->unlinked_dir =
4629 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4630 dev->del_dir =
4631 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4632 dev->root_dir =
4633 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4635 dev->lost_n_found =
4636 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4639 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4640 && dev->del_dir) {
4641 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4652 int yaffs_guts_ll_init(struct yaffs_dev *dev)
4658 if (!dev) {
4665 if (dev->ll_init)
4668 dev->internal_start_block = dev->param.start_block;
4669 dev->internal_end_block = dev->param.end_block;
4670 dev->block_offset = 0;
4671 dev->chunk_offset = 0;
4672 dev->n_free_chunks = 0;
4674 dev->gc_block = 0;
4676 if (dev->param.start_block == 0) {
4677 dev->internal_start_block = dev->param.start_block + 1;
4678 dev->internal_end_block = dev->param.end_block + 1;
4679 dev->block_offset = 1;
4680 dev->chunk_offset = dev->param.chunks_per_block;
4685 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4686 dev->param.total_bytes_per_chunk < 1024) ||
4687 (!dev->param.is_yaffs2 &&
4688 dev->param.total_bytes_per_chunk < 512) ||
4689 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4690 dev->param.chunks_per_block < 2 ||
4691 dev->param.n_reserved_blocks < 2 ||
4692 dev->internal_start_block <= 0 ||
4693 dev->internal_end_block <= 0 ||
4694 dev->internal_end_block <=
4695 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4700 dev->param.total_bytes_per_chunk,
4701 dev->param.is_yaffs2 ? "2" : "",
4702 dev->param.inband_tags);
4707 if (dev->param.inband_tags)
4708 dev->data_bytes_per_chunk =
4709 dev->param.total_bytes_per_chunk -
4712 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4715 if (!yaffs_check_dev_fns(dev)) {
4723 if (yaffs_init_nand(dev) != YAFFS_OK) {
4732 int yaffs_guts_format_dev(struct yaffs_dev *dev)
4738 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4741 if(dev->is_mounted)
4744 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
4745 yaffs_query_init_block_state(dev, i, &state, &dummy);
4747 yaffs_erase_block(dev, i);
4754 int yaffs_guts_initialise(struct yaffs_dev *dev)
4760 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4763 if (dev->is_mounted) {
4768 dev->is_mounted = 1;
4775 x = dev->data_bytes_per_chunk;
4776 /* We always use dev->chunk_shift and dev->chunk_div */
4777 dev->chunk_shift = calc_shifts(x);
4778 x >>= dev->chunk_shift;
4779 dev->chunk_div = x;
4781 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4788 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4793 if (!dev->param.wide_tnodes_disabled) {
4798 dev->tnode_width = 16;
4800 dev->tnode_width = bits;
4802 dev->tnode_width = 16;
4805 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4813 if (bits <= dev->tnode_width)
4814 dev->chunk_grp_bits = 0;
4816 dev->chunk_grp_bits = bits - dev->tnode_width;
4818 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4819 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4820 dev->tnode_size = sizeof(struct yaffs_tnode);
4822 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4824 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4837 dev->all_gcs = 0;
4838 dev->passive_gc_count = 0;
4839 dev->oldest_dirty_gc_count = 0;
4840 dev->bg_gcs = 0;
4841 dev->gc_block_finder = 0;
4842 dev->buffered_block = -1;
4843 dev->doing_buffered_block_rewrite = 0;
4844 dev->n_deleted_files = 0;
4845 dev->n_bg_deletions = 0;
4846 dev->n_unlinked_files = 0;
4847 dev->n_ecc_fixed = 0;
4848 dev->n_ecc_unfixed = 0;
4849 dev->n_tags_ecc_fixed = 0;
4850 dev->n_tags_ecc_unfixed = 0;
4851 dev->n_erase_failures = 0;
4852 dev->n_erased_blocks = 0;
4853 dev->gc_disable = 0;
4854 dev->has_pending_prioritised_gc = 1;
4856 INIT_LIST_HEAD(&dev->dirty_dirs);
4857 dev->oldest_dirty_seq = 0;
4858 dev->oldest_dirty_block = 0;
4861 if (!yaffs_init_tmp_buffers(dev))
4864 dev->cache = NULL;
4865 dev->gc_cleanup_list = NULL;
4867 if (!init_failed && dev->param.n_caches > 0) {
4871 dev->param.n_caches * sizeof(struct yaffs_cache);
4873 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4874 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4876 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4878 buf = (u8 *) dev->cache;
4880 if (dev->cache)
4881 memset(dev->cache, 0, cache_bytes);
4883 for (i = 0; i < dev->param.n_caches && buf; i++) {
4884 dev->cache[i].object = NULL;
4885 dev->cache[i].last_use = 0;
4886 dev->cache[i].dirty = 0;
4887 dev->cache[i].data = buf =
4888 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4893 dev->cache_last_use = 0;
4896 dev->cache_hits = 0;
4899 dev->gc_cleanup_list =
4900 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4902 if (!dev->gc_cleanup_list)
4906 if (dev->param.is_yaffs2)
4907 dev->param.use_header_file_size = 1;
4909 if (!init_failed && !yaffs_init_blocks(dev))
4912 yaffs_init_tnodes_and_objs(dev);
4914 if (!init_failed && !yaffs_create_initial_dir(dev))
4917 if (!init_failed && dev->param.is_yaffs2 &&
4918 !dev->param.disable_summary &&
4919 !yaffs_summary_init(dev))
4924 if (dev->param.is_yaffs2) {
4925 if (yaffs2_checkpt_restore(dev)) {
4926 yaffs_check_obj_details_loaded(dev->root_dir);
4936 yaffs_deinit_blocks(dev);
4938 yaffs_deinit_tnodes_and_objs(dev);
4940 dev->n_erased_blocks = 0;
4941 dev->n_free_chunks = 0;
4942 dev->alloc_block = -1;
4943 dev->alloc_page = -1;
4944 dev->n_deleted_files = 0;
4945 dev->n_unlinked_files = 0;
4946 dev->n_bg_deletions = 0;
4948 if (!init_failed && !yaffs_init_blocks(dev))
4951 yaffs_init_tnodes_and_objs(dev);
4954 && !yaffs_create_initial_dir(dev))
4957 if (!init_failed && !yaffs2_scan_backwards(dev))
4960 } else if (!yaffs1_scan(dev)) {
4964 yaffs_strip_deleted_objs(dev);
4965 yaffs_fix_hanging_objs(dev);
4966 if (dev->param.empty_lost_n_found)
4967 yaffs_empty_l_n_f(dev);
4975 yaffs_deinitialise(dev);
4980 dev->n_page_reads = 0;
4981 dev->n_page_writes = 0;
4982 dev->n_erasures = 0;
4983 dev->n_gc_copies = 0;
4984 dev->n_retried_writes = 0;
4986 dev->n_retired_blocks = 0;
4988 yaffs_verify_free_chunks(dev);
4989 yaffs_verify_blocks(dev);
4992 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4993 yaffs2_checkpt_invalidate(dev);
5000 void yaffs_deinitialise(struct yaffs_dev *dev)
5002 if (dev->is_mounted) {
5005 yaffs_deinit_blocks(dev);
5006 yaffs_deinit_tnodes_and_objs(dev);
5007 yaffs_summary_deinit(dev);
5009 if (dev->param.n_caches > 0 && dev->cache) {
5011 for (i = 0; i < dev->param.n_caches; i++) {
5012 kfree(dev->cache[i].data);
5013 dev->cache[i].data = NULL;
5016 kfree(dev->cache);
5017 dev->cache = NULL;
5020 kfree(dev->gc_cleanup_list);
5023 kfree(dev->temp_buffer[i].buffer);
5024 dev->temp_buffer[i].buffer = NULL;
5027 kfree(dev->checkpt_buffer);
5028 dev->checkpt_buffer = NULL;
5029 kfree(dev->checkpt_block_list);
5030 dev->checkpt_block_list = NULL;
5032 dev->is_mounted = 0;
5034 yaffs_deinit_nand(dev);
5038 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5044 blk = dev->block_info;
5045 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5052 (dev->param.chunks_per_block - blk->pages_in_use +
5063 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5071 n_free = dev->n_free_chunks;
5072 n_free += dev->n_deleted_files;
5076 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5077 if (dev->cache[i].dirty)
5084 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5087 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5089 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
5123 void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
5132 for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
5133 bi = yaffs_get_block_info(dev, i);