2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 #include "yaffs_trace.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_tagsvalidity.h"
19 #include "yaffs_getblockinfo.h"
21 #include "yaffs_tagscompat.h"
23 #include "yaffs_nand.h"
25 #include "yaffs_yaffs1.h"
26 #include "yaffs_yaffs2.h"
27 #include "yaffs_bitmap.h"
28 #include "yaffs_verify.h"
30 #include "yaffs_nand.h"
31 #include "yaffs_packedtags2.h"
33 #include "yaffs_nameval.h"
34 #include "yaffs_allocator.h"
36 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
37 #define YAFFS_GC_GOOD_ENOUGH 2
38 #define YAFFS_GC_PASSIVE_THRESHOLD 4
40 #include "yaffs_ecc.h"
44 /* Robustification (if it ever comes about...) */
45 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block);
46 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
48 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
50 const struct yaffs_ext_tags *tags);
51 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
52 const struct yaffs_ext_tags *tags);
54 /* Other local prototypes */
55 static void yaffs_update_parent(struct yaffs_obj *obj);
56 static int yaffs_unlink_obj(struct yaffs_obj *obj);
57 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj);
59 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
61 struct yaffs_ext_tags *tags,
65 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
66 enum yaffs_obj_type type);
69 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer, struct yaffs_xattr_mod *xmod);
71 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj);
72 static int yaffs_generic_obj_del(struct yaffs_obj *in);
74 static int yaffs_check_chunk_erased(struct yaffs_dev *dev,
77 static int yaffs_unlink_worker(struct yaffs_obj *obj);
79 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
82 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
83 struct yaffs_block_info **block_ptr);
85 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in);
87 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in);
88 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id);
90 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
91 struct yaffs_ext_tags *tags);
93 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
96 struct yaffs_ext_tags *tags);
99 static void yaffs_load_name_from_oh(struct yaffs_dev *dev,YCHAR *name, const YCHAR *oh_name, int buff_size);
100 static void yaffs_load_oh_from_name(struct yaffs_dev *dev,YCHAR *oh_name, const YCHAR *name);
103 /* Function to calculate chunk and offset */
105 static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr, int *chunk_out,
111 chunk = (u32)(addr >> dev->chunk_shift);
113 if (dev->chunk_div == 1) {
114 /* easy power of 2 case */
115 offset = (u32)(addr & dev->chunk_mask);
117 /* Non power-of-2 case */
121 chunk /= dev->chunk_div;
123 chunk_base = ((loff_t)chunk) * dev->data_bytes_per_chunk;
124 offset = (u32)(addr - chunk_base);
128 *offset_out = offset;
131 /* Function to return the number of shifts for a power of 2 greater than or
132 * equal to the given number
133 * Note we don't try to cater for all possible numbers and this does not have to
134 * be hellishly efficient.
137 static u32 calc_shifts_ceiling(u32 x)
142 shifts = extra_bits = 0;
157 /* Function to return the number of shifts to get a 1 in bit 0
160 static u32 calc_shifts(u32 x)
180 * Temporary buffer manipulations.
183 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
188 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
190 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
191 dev->temp_buffer[i].line = 0; /* not in use */
192 dev->temp_buffer[i].buffer = buf =
193 YMALLOC_DMA(dev->param.total_bytes_per_chunk);
196 return buf ? YAFFS_OK : YAFFS_FAIL;
199 u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev, int line_no)
204 if (dev->temp_in_use > dev->max_temp)
205 dev->max_temp = dev->temp_in_use;
207 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
208 if (dev->temp_buffer[i].line == 0) {
209 dev->temp_buffer[i].line = line_no;
210 if ((i + 1) > dev->max_temp) {
211 dev->max_temp = i + 1;
212 for (j = 0; j <= i; j++)
213 dev->temp_buffer[j].max_line =
214 dev->temp_buffer[j].line;
217 return dev->temp_buffer[i].buffer;
221 T(YAFFS_TRACE_BUFFERS,
222 (TSTR("Out of temp buffers at line %d, other held by lines:"),
224 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
225 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->temp_buffer[i].line));
227 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
230 * If we got here then we have to allocate an unmanaged one
234 dev->unmanaged_buffer_allocs++;
235 return YMALLOC(dev->data_bytes_per_chunk);
239 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer,
246 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
247 if (dev->temp_buffer[i].buffer == buffer) {
248 dev->temp_buffer[i].line = 0;
254 /* assume it is an unmanaged one. */
255 T(YAFFS_TRACE_BUFFERS,
256 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
259 dev->unmanaged_buffer_deallocs++;
265 * Determine if we have a managed buffer.
267 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer)
271 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
272 if (dev->temp_buffer[i].buffer == buffer)
276 for (i = 0; i < dev->param.n_caches; i++) {
277 if (dev->cache[i].data == buffer)
281 if (buffer == dev->checkpt_buffer)
284 T(YAFFS_TRACE_ALWAYS,
285 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
297 * Simple hash function. Needs to have a reasonable spread
300 static Y_INLINE int yaffs_hash_fn(int n)
303 return n % YAFFS_NOBJECT_BUCKETS;
307 * Access functions to useful fake objects.
308 * Note that root might have a presence in NAND if permissions are set.
311 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
313 return dev->root_dir;
316 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
318 return dev->lost_n_found;
323 * Erased NAND checking functions
326 int yaffs_check_ff(u8 *buffer, int n_bytes)
328 /* Horrible, slow implementation */
337 static int yaffs_check_chunk_erased(struct yaffs_dev *dev,
340 int retval = YAFFS_OK;
341 u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
342 struct yaffs_ext_tags tags;
345 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
347 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
350 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
351 T(YAFFS_TRACE_NANDACCESS,
352 (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
356 yaffs_release_temp_buffer(dev, data, __LINE__);
363 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
366 struct yaffs_ext_tags *tags)
368 int retval = YAFFS_OK;
369 struct yaffs_ext_tags temp_tags;
370 u8 *buffer = yaffs_get_temp_buffer(dev,__LINE__);
373 result = yaffs_rd_chunk_tags_nand(dev,nand_chunk,buffer,&temp_tags);
374 if(memcmp(buffer,data,dev->data_bytes_per_chunk) ||
375 temp_tags.obj_id != tags->obj_id ||
376 temp_tags.chunk_id != tags->chunk_id ||
377 temp_tags.n_bytes != tags->n_bytes)
380 yaffs_release_temp_buffer(dev, buffer, __LINE__);
385 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
387 struct yaffs_ext_tags *tags,
394 yaffs2_checkpt_invalidate(dev);
397 struct yaffs_block_info *bi = 0;
400 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
406 /* First check this chunk is erased, if it needs
407 * checking. The checking policy (unless forced
408 * always on) is as follows:
410 * Check the first page we try to write in a block.
411 * If the check passes then we don't need to check any
412 * more. If the check fails, we check again...
413 * If the block has been erased, we don't need to check.
415 * However, if the block has been prioritised for gc,
416 * then we think there might be something odd about
417 * this block and stop using it.
419 * Rationale: We should only ever see chunks that have
420 * not been erased if there was a partially written
421 * chunk due to power loss. This checking policy should
422 * catch that case with very few checks and thus save a
423 * lot of checks that are most likely not needed.
426 * If an erase check fails or the write fails we skip the
430 /* let's give it a try */
433 if(dev->param.always_check_erased)
434 bi->skip_erased_check = 0;
436 if (!bi->skip_erased_check) {
437 erased_ok = yaffs_check_chunk_erased(dev, chunk);
438 if (erased_ok != YAFFS_OK) {
440 (TSTR("**>> yaffs chunk %d was not erased"
443 /* If not erased, delete this one,
444 * skip rest of block and
445 * try another chunk */
446 yaffs_chunk_del(dev,chunk,1,__LINE__);
447 yaffs_skip_rest_of_block(dev);
452 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk,
455 if(!bi->skip_erased_check)
456 write_ok = yaffs_verify_chunk_written(dev, chunk, data, tags);
458 if (write_ok != YAFFS_OK) {
459 /* Clean up aborted write, skip to next block and
460 * try another chunk */
461 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
465 bi->skip_erased_check = 1;
467 /* Copy the data into the robustification buffer */
468 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
470 } while (write_ok != YAFFS_OK &&
471 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
478 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
481 dev->n_retired_writes += (attempts - 1);
490 * Block retiring for handling a broken block.
493 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
495 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
497 yaffs2_checkpt_invalidate(dev);
499 yaffs2_clear_oldest_dirty_seq(dev,bi);
501 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
502 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
503 T(YAFFS_TRACE_ALWAYS, (TSTR(
504 "yaffs: Failed to mark bad and erase block %d"
505 TENDSTR), flash_block));
507 struct yaffs_ext_tags tags;
508 int chunk_id = flash_block * dev->param.chunks_per_block;
510 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
512 memset(buffer, 0xff, dev->data_bytes_per_chunk);
513 yaffs_init_tags(&tags);
514 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
515 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
516 dev->chunk_offset, buffer, &tags) != YAFFS_OK)
517 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
518 TCONT("write bad block marker to block %d")
519 TENDSTR), flash_block));
521 yaffs_release_temp_buffer(dev, buffer, __LINE__);
525 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
526 bi->gc_prioritise = 0;
527 bi->needs_retiring = 0;
529 dev->n_retired_blocks++;
533 * Functions for robustisizing TODO
537 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
539 const struct yaffs_ext_tags *tags)
542 nand_chunk=nand_chunk;
547 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
548 const struct yaffs_ext_tags *tags)
551 nand_chunk=nand_chunk;
555 void yaffs_handle_chunk_error(struct yaffs_dev *dev, struct yaffs_block_info *bi)
557 if (!bi->gc_prioritise) {
558 bi->gc_prioritise = 1;
559 dev->has_pending_prioritised_gc = 1;
560 bi->chunk_error_strikes++;
562 if (bi->chunk_error_strikes > 3) {
563 bi->needs_retiring = 1; /* Too many stikes, so retire this */
564 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
570 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
573 int flash_block = nand_chunk / dev->param.chunks_per_block;
574 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
576 yaffs_handle_chunk_error(dev, bi);
579 /* Was an actual write failure, so mark the block for retirement */
580 bi->needs_retiring = 1;
581 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
582 (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
585 /* Delete the chunk */
586 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
587 yaffs_skip_rest_of_block(dev);
591 /*---------------- Name handling functions ------------*/
593 static u16 yaffs_calc_name_sum(const YCHAR *name)
598 const YUCHAR *bname = (const YUCHAR *) name;
600 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
602 /* 0x1f mask is case insensitive */
603 sum += ((*bname) & 0x1f) * i;
611 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR *name)
613 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
614 memset(obj->short_name, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
615 if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
616 yaffs_strcpy(obj->short_name, name);
618 obj->short_name[0] = _Y('\0');
620 obj->sum = yaffs_calc_name_sum(name);
623 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj, const struct yaffs_obj_hdr *oh)
625 #ifdef CONFIG_YAFFS_AUTO_UNICODE
626 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH+1];
627 memset(tmp_name,0,sizeof(tmp_name));
628 yaffs_load_name_from_oh(obj->my_dev,tmp_name,oh->name,YAFFS_MAX_NAME_LENGTH+1);
629 yaffs_set_obj_name(obj,tmp_name);
631 yaffs_set_obj_name(obj,oh->name);
635 /*-------------------- TNODES -------------------
637 * List of spare tnodes
638 * The list is hooked together using the first pointer
643 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
645 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
647 memset(tn, 0, dev->tnode_size);
651 dev->checkpoint_blocks_required = 0; /* force recalculation*/
656 /* FreeTnode frees up a tnode and puts it back on the free list */
657 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
659 yaffs_free_raw_tnode(dev,tn);
661 dev->checkpoint_blocks_required = 0; /* force recalculation*/
664 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
666 yaffs_deinit_raw_tnodes_and_objs(dev);
672 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn, unsigned pos,
675 u32 *map = (u32 *)tn;
681 pos &= YAFFS_TNODES_LEVEL0_MASK;
682 val >>= dev->chunk_grp_bits;
684 bit_in_map = pos * dev->tnode_width;
685 word_in_map = bit_in_map / 32;
686 bit_in_word = bit_in_map & (32 - 1);
688 mask = dev->tnode_mask << bit_in_word;
690 map[word_in_map] &= ~mask;
691 map[word_in_map] |= (mask & (val << bit_in_word));
693 if (dev->tnode_width > (32 - bit_in_word)) {
694 bit_in_word = (32 - bit_in_word);
696 mask = dev->tnode_mask >> (/*dev->tnode_width -*/ bit_in_word);
697 map[word_in_map] &= ~mask;
698 map[word_in_map] |= (mask & (val >> bit_in_word));
702 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
705 u32 *map = (u32 *)tn;
711 pos &= YAFFS_TNODES_LEVEL0_MASK;
713 bit_in_map = pos * dev->tnode_width;
714 word_in_map = bit_in_map / 32;
715 bit_in_word = bit_in_map & (32 - 1);
717 val = map[word_in_map] >> bit_in_word;
719 if (dev->tnode_width > (32 - bit_in_word)) {
720 bit_in_word = (32 - bit_in_word);
722 val |= (map[word_in_map] << bit_in_word);
725 val &= dev->tnode_mask;
726 val <<= dev->chunk_grp_bits;
731 /* ------------------- End of individual tnode manipulation -----------------*/
733 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
734 * The look up tree is represented by the top tnode and the number of top_level
735 * in the tree. 0 means only the level 0 tnode is in the tree.
738 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
739 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
740 struct yaffs_file_var *file_struct,
743 struct yaffs_tnode *tn = file_struct->top;
746 int level = file_struct->top_level;
750 /* Check sane level and chunk Id */
751 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
754 if (chunk_id > YAFFS_MAX_CHUNK_ID)
757 /* First check we're tall enough (ie enough top_level) */
759 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
762 i >>= YAFFS_TNODES_INTERNAL_BITS;
766 if (required_depth > file_struct->top_level)
767 return NULL; /* Not tall enough, so we can't find it */
769 /* Traverse down to level 0 */
770 while (level > 0 && tn) {
771 tn = tn->internal[(chunk_id >>
772 (YAFFS_TNODES_LEVEL0_BITS +
774 YAFFS_TNODES_INTERNAL_BITS)) &
775 YAFFS_TNODES_INTERNAL_MASK];
782 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
783 * This happens in two steps:
784 * 1. If the tree isn't tall enough, then make it taller.
785 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
787 * Used when modifying the tree.
789 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
790 * be plugged into the ttree.
793 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
794 struct yaffs_file_var *file_struct,
796 struct yaffs_tnode *passed_tn)
801 struct yaffs_tnode *tn;
806 /* Check sane level and page Id */
807 if (file_struct->top_level < 0 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
810 if (chunk_id > YAFFS_MAX_CHUNK_ID)
813 /* First check we're tall enough (ie enough top_level) */
815 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
818 x >>= YAFFS_TNODES_INTERNAL_BITS;
823 if (required_depth > file_struct->top_level) {
824 /* Not tall enough, gotta make the tree taller */
825 for (i = file_struct->top_level; i < required_depth; i++) {
827 tn = yaffs_get_tnode(dev);
830 tn->internal[0] = file_struct->top;
831 file_struct->top = tn;
832 file_struct->top_level++;
835 (TSTR("yaffs: no more tnodes" TENDSTR)));
841 /* Traverse down to level 0, adding anything we need */
843 l = file_struct->top_level;
844 tn = file_struct->top;
847 while (l > 0 && tn) {
849 (YAFFS_TNODES_LEVEL0_BITS +
850 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
851 YAFFS_TNODES_INTERNAL_MASK;
854 if ((l > 1) && !tn->internal[x]) {
855 /* Add missing non-level-zero tnode */
856 tn->internal[x] = yaffs_get_tnode(dev);
860 /* Looking from level 1 at level 0 */
862 /* If we already have one, then release it.*/
864 yaffs_free_tnode(dev, tn->internal[x]);
865 tn->internal[x] = passed_tn;
867 } else if (!tn->internal[x]) {
868 /* Don't have one, none passed in */
869 tn->internal[x] = yaffs_get_tnode(dev);
875 tn = tn->internal[x];
881 memcpy(tn, passed_tn, (dev->tnode_width * YAFFS_NTNODES_LEVEL0)/8);
882 yaffs_free_tnode(dev, passed_tn);
889 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
890 struct yaffs_ext_tags *tags, int obj_id,
895 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
896 if (yaffs_check_chunk_bit(dev, the_chunk / dev->param.chunks_per_block,
897 the_chunk % dev->param.chunks_per_block)) {
899 if(dev->chunk_grp_size == 1)
902 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
904 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
916 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
918 struct yaffs_block_info *the_block;
921 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
923 block_no = chunk / dev->param.chunks_per_block;
924 the_block = yaffs_get_block_info(dev, block_no);
926 the_block->soft_del_pages++;
927 dev->n_free_chunks++;
928 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
932 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
933 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
935 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
938 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
939 u32 level, int chunk_offset)
944 struct yaffs_dev *dev = in->my_dev;
949 for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
951 if (tn->internal[i]) {
953 yaffs_soft_del_worker(in,
959 YAFFS_TNODES_INTERNAL_BITS)
962 yaffs_free_tnode(dev,
965 tn->internal[i] = NULL;
967 /* Hoosterman... how could this happen? */
971 return (all_done) ? 1 : 0;
972 } else if (level == 0) {
974 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
975 the_chunk = yaffs_get_group_base(dev, tn, i);
977 /* Note this does not find the real chunk, only the chunk group.
978 * We make an assumption that a chunk group is not larger than
981 yaffs_soft_del_chunk(dev, the_chunk);
982 yaffs_load_tnode_0(dev, tn, i, 0);
996 static void yaffs_soft_del_file(struct yaffs_obj *obj)
999 obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1000 if (obj->n_data_chunks <= 0) {
1001 /* Empty file with no duplicate object headers, just delete it immediately */
1002 yaffs_free_tnode(obj->my_dev,
1003 obj->variant.file_variant.top);
1004 obj->variant.file_variant.top = NULL;
1005 T(YAFFS_TRACE_TRACING,
1006 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1008 yaffs_generic_obj_del(obj);
1010 yaffs_soft_del_worker(obj,
1011 obj->variant.file_variant.top,
1012 obj->variant.file_variant.
1019 /* Pruning removes any part of the file structure tree that is beyond the
1020 * bounds of the file (ie that does not point to chunks).
1022 * A file should only get pruned when its size is reduced.
1024 * Before pruning, the chunks must be pulled from the tree and the
1025 * level 0 tnode entries must be zeroed out.
1026 * Could also use this for file deletion, but that's probably better handled
1027 * by a special case.
1029 * This function is recursive. For levels > 0 the function is called again on
1030 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1031 * If there is no data in a subtree then it is pruned.
1034 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev, struct yaffs_tnode *tn,
1035 u32 level, int del0)
1044 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1045 if (tn->internal[i]) {
1047 yaffs_prune_worker(dev, tn->internal[i],
1049 (i == 0) ? del0 : 1);
1052 if (tn->internal[i])
1056 int tnode_size_u32 = dev->tnode_size/sizeof(u32);
1057 u32 *map = (u32 *)tn;
1059 for(i = 0; !has_data && i < tnode_size_u32; i++){
1065 if (has_data == 0 && del0) {
1066 /* Free and return NULL */
1068 yaffs_free_tnode(dev, tn);
1078 static int yaffs_prune_tree(struct yaffs_dev *dev,
1079 struct yaffs_file_var *file_struct)
1084 struct yaffs_tnode *tn;
1086 if (file_struct->top_level > 0) {
1088 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1090 /* Now we have a tree with all the non-zero branches NULL but the height
1091 * is the same as it was.
1092 * Let's see if we can trim internal tnodes to shorten the tree.
1093 * We can do this if only the 0th element in the tnode is in use
1094 * (ie all the non-zero are NULL)
1097 while (file_struct->top_level && !done) {
1098 tn = file_struct->top;
1101 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1102 if (tn->internal[i])
1107 file_struct->top = tn->internal[0];
1108 file_struct->top_level--;
1109 yaffs_free_tnode(dev, tn);
1119 /*-------------------- End of File Structure functions.-------------------*/
1122 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1123 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1125 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1130 /* Now sweeten it up... */
1132 memset(obj, 0, sizeof(struct yaffs_obj));
1133 obj->being_created = 1;
1137 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1138 INIT_LIST_HEAD(&(obj->hard_links));
1139 INIT_LIST_HEAD(&(obj->hash_link));
1140 INIT_LIST_HEAD(&obj->siblings);
1143 /* Now make the directory sane */
1144 if (dev->root_dir) {
1145 obj->parent = dev->root_dir;
1146 list_add(&(obj->siblings), &dev->root_dir->variant.dir_variant.children);
1149 /* Add it to the lost and found directory.
1150 * NB Can't put root or lost-n-found in lost-n-found so
1151 * check if lost-n-found exists first
1153 if (dev->lost_n_found)
1154 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1156 obj->being_created = 0;
1159 dev->checkpoint_blocks_required = 0; /* force recalculation*/
1164 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev, int number,
1168 struct yaffs_obj *obj =
1169 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1171 obj->fake = 1; /* it is fake so it might have no NAND presence... */
1172 obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
1173 obj->unlink_allowed = 0; /* ... or unlink it */
1176 obj->yst_mode = mode;
1178 obj->hdr_chunk = 0; /* Not a valid chunk. */
1185 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1188 struct yaffs_dev *dev = obj->my_dev;
1190 /* If it is still linked into the bucket list, free from the list */
1191 if (!list_empty(&obj->hash_link)) {
1192 list_del_init(&obj->hash_link);
1193 bucket = yaffs_hash_fn(obj->obj_id);
1194 dev->obj_bucket[bucket].count--;
1198 /* FreeObject frees up a Object and puts it back on the free list */
1199 static void yaffs_free_obj(struct yaffs_obj *obj)
1201 struct yaffs_dev *dev = obj->my_dev;
1203 T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), obj, obj->my_inode));
1209 if (!list_empty(&obj->siblings))
1213 if (obj->my_inode) {
1214 /* We're still hooked up to a cached inode.
1215 * Don't delete now, but mark for later deletion
1217 obj->defered_free = 1;
1221 yaffs_unhash_obj(obj);
1223 yaffs_free_raw_obj(dev,obj);
1225 dev->checkpoint_blocks_required = 0; /* force recalculation*/
1229 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1231 if (obj->defered_free)
1232 yaffs_free_obj(obj);
1235 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
1242 yaffs_init_raw_tnodes_and_objs(dev);
1244 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1245 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
1246 dev->obj_bucket[i].count = 0;
1250 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1254 int lowest = 999999;
1257 /* Search for the shortest list or one that
1261 for (i = 0; i < 10 && lowest > 4; i++) {
1262 dev->bucket_finder++;
1263 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1264 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1265 lowest = dev->obj_bucket[dev->bucket_finder].count;
1266 l = dev->bucket_finder;
1274 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1276 int bucket = yaffs_find_nice_bucket(dev);
1278 /* Now find an object value that has not already been taken
1279 * by scanning the list.
1283 struct list_head *i;
1285 u32 n = (u32) bucket;
1287 /* yaffs_check_obj_hash_sane(); */
1291 n += YAFFS_NOBJECT_BUCKETS;
1292 if (1 || dev->obj_bucket[bucket].count > 0) {
1293 list_for_each(i, &dev->obj_bucket[bucket].list) {
1294 /* If there is already one in the list */
1295 if (i && list_entry(i, struct yaffs_obj,
1296 hash_link)->obj_id == n) {
1306 static void yaffs_hash_obj(struct yaffs_obj *in)
1308 int bucket = yaffs_hash_fn(in->obj_id);
1309 struct yaffs_dev *dev = in->my_dev;
1311 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1312 dev->obj_bucket[bucket].count++;
1315 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1317 int bucket = yaffs_hash_fn(number);
1318 struct list_head *i;
1319 struct yaffs_obj *in;
1321 list_for_each(i, &dev->obj_bucket[bucket].list) {
1322 /* Look if it is in the list */
1324 in = list_entry(i, struct yaffs_obj, hash_link);
1325 if (in->obj_id == number) {
1327 /* Don't tell the VFS about this one if it is defered free */
1328 if (in->defered_free)
1339 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1340 enum yaffs_obj_type type)
1342 struct yaffs_obj *the_obj=NULL;
1343 struct yaffs_tnode *tn = NULL;
1346 number = yaffs_new_obj_id(dev);
1348 if (type == YAFFS_OBJECT_TYPE_FILE) {
1349 tn = yaffs_get_tnode(dev);
1354 the_obj = yaffs_alloc_empty_obj(dev);
1357 yaffs_free_tnode(dev,tn);
1364 the_obj->rename_allowed = 1;
1365 the_obj->unlink_allowed = 1;
1366 the_obj->obj_id = number;
1367 yaffs_hash_obj(the_obj);
1368 the_obj->variant_type = type;
1369 #ifdef CONFIG_YAFFS_WINCE
1370 yfsd_win_file_time_now(the_obj->win_atime);
1371 the_obj->win_ctime[0] = the_obj->win_mtime[0] =
1372 the_obj->win_atime[0];
1373 the_obj->win_ctime[1] = the_obj->win_mtime[1] =
1374 the_obj->win_atime[1];
1378 the_obj->yst_atime = the_obj->yst_mtime =
1379 the_obj->yst_ctime = Y_CURRENT_TIME;
1382 case YAFFS_OBJECT_TYPE_FILE:
1383 the_obj->variant.file_variant.file_size = 0;
1384 the_obj->variant.file_variant.scanned_size = 0;
1385 the_obj->variant.file_variant.shrink_size = 0xFFFFFFFF; /* max u32 */
1386 the_obj->variant.file_variant.top_level = 0;
1387 the_obj->variant.file_variant.top = tn;
1389 case YAFFS_OBJECT_TYPE_DIRECTORY:
1390 INIT_LIST_HEAD(&the_obj->variant.dir_variant.
1392 INIT_LIST_HEAD(&the_obj->variant.dir_variant.
1395 case YAFFS_OBJECT_TYPE_SYMLINK:
1396 case YAFFS_OBJECT_TYPE_HARDLINK:
1397 case YAFFS_OBJECT_TYPE_SPECIAL:
1398 /* No action required */
1400 case YAFFS_OBJECT_TYPE_UNKNOWN:
1401 /* todo this should not happen */
1409 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
1411 enum yaffs_obj_type type)
1413 struct yaffs_obj *the_obj = NULL;
1416 the_obj = yaffs_find_by_number(dev, number);
1419 the_obj = yaffs_new_obj(dev, number, type);
1426 YCHAR *yaffs_clone_str(const YCHAR *str)
1428 YCHAR *new_str = NULL;
1434 len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
1435 new_str = YMALLOC((len + 1) * sizeof(YCHAR));
1437 yaffs_strncpy(new_str, str,len);
1445 * Mknod (create) a new object.
1446 * equiv_obj only has meaning for a hard link;
1447 * alias_str only has meaning for a symlink.
1448 * rdev only has meaning for devices (a subset of special objects)
1451 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
1452 struct yaffs_obj *parent,
1457 struct yaffs_obj *equiv_obj,
1458 const YCHAR *alias_str, u32 rdev)
1460 struct yaffs_obj *in;
1463 struct yaffs_dev *dev = parent->my_dev;
1465 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
1466 if (yaffs_find_by_name(parent, name))
1469 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
1470 str = yaffs_clone_str(alias_str);
1475 in = yaffs_new_obj(dev, -1, type);
1490 in->variant_type = type;
1492 in->yst_mode = mode;
1494 #ifdef CONFIG_YAFFS_WINCE
1495 yfsd_win_file_time_now(in->win_atime);
1496 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1497 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1500 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
1502 in->yst_rdev = rdev;
1506 in->n_data_chunks = 0;
1508 yaffs_set_obj_name(in, name);
1511 yaffs_add_obj_to_dir(parent, in);
1513 in->my_dev = parent->my_dev;
1516 case YAFFS_OBJECT_TYPE_SYMLINK:
1517 in->variant.symlink_variant.alias = str;
1519 case YAFFS_OBJECT_TYPE_HARDLINK:
1520 in->variant.hardlink_variant.equiv_obj =
1522 in->variant.hardlink_variant.equiv_id =
1524 list_add(&in->hard_links, &equiv_obj->hard_links);
1526 case YAFFS_OBJECT_TYPE_FILE:
1527 case YAFFS_OBJECT_TYPE_DIRECTORY:
1528 case YAFFS_OBJECT_TYPE_SPECIAL:
1529 case YAFFS_OBJECT_TYPE_UNKNOWN:
1534 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
1535 /* Could not create the object header, fail the creation */
1540 yaffs_update_parent(parent);
1546 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent, const YCHAR *name,
1547 u32 mode, u32 uid, u32 gid)
1549 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
1550 uid, gid, NULL, NULL, 0);
1553 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
1554 u32 mode, u32 uid, u32 gid)
1556 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
1557 mode, uid, gid, NULL, NULL, 0);
1560 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent, const YCHAR *name,
1561 u32 mode, u32 uid, u32 gid, u32 rdev)
1563 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
1564 uid, gid, NULL, NULL, rdev);
1567 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent, const YCHAR *name,
1568 u32 mode, u32 uid, u32 gid,
1571 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
1572 uid, gid, NULL, alias, 0);
1575 /* yaffs_link_obj returns the object id of the equivalent object.*/
1576 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR *name,
1577 struct yaffs_obj *equiv_obj)
1579 /* Get the real object in case we were fed a hard link as an equivalent object */
1580 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
1582 if (yaffs_create_obj
1583 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
1584 equiv_obj, NULL, 0)) {
1592 static int yaffs_change_obj_name(struct yaffs_obj *obj, struct yaffs_obj *new_dir,
1593 const YCHAR *new_name, int force, int shadows)
1598 struct yaffs_obj *existing_target;
1600 if (new_dir == NULL)
1601 new_dir = obj->parent; /* use the old directory */
1603 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1604 T(YAFFS_TRACE_ALWAYS,
1606 ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
1611 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1612 if (obj->my_dev->param.is_yaffs2)
1613 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1615 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1616 && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1618 del_op = (new_dir == obj->my_dev->del_dir);
1620 existing_target = yaffs_find_by_name(new_dir, new_name);
1622 /* If the object is a file going into the unlinked directory,
1623 * then it is OK to just stuff it in since duplicate names are allowed.
1624 * else only proceed if the new name does not exist and if we're putting
1625 * it into a directory.
1631 !existing_target) &&
1632 new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1633 yaffs_set_obj_name(obj, new_name);
1636 yaffs_add_obj_to_dir(new_dir, obj);
1641 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1642 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1649 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
1650 struct yaffs_obj *new_dir, const YCHAR *new_name)
1652 struct yaffs_obj *obj = NULL;
1653 struct yaffs_obj *existing_target = NULL;
1656 struct yaffs_dev *dev;
1659 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1661 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1664 dev = old_dir->my_dev;
1666 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1667 /* Special case for case insemsitive systems (eg. WinCE).
1668 * While look-up is case insensitive, the name isn't.
1669 * Therefore we might want to change x.txt to X.txt
1671 if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0)
1675 if(yaffs_strnlen(new_name,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
1679 obj = yaffs_find_by_name(old_dir, old_name);
1681 if (obj && obj->rename_allowed) {
1683 /* Now do the handling for an existing target, if there is one */
1685 existing_target = yaffs_find_by_name(new_dir, new_name);
1686 if (existing_target &&
1687 existing_target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1688 !list_empty(&existing_target->variant.dir_variant.children)) {
1689 /* There is a target that is a non-empty directory, so we fail */
1690 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
1691 } else if (existing_target && existing_target != obj) {
1692 /* Nuke the target first, using shadowing,
1693 * but only if it isn't the same object.
1695 * Note we must disable gc otherwise it can mess up the shadowing.
1699 yaffs_change_obj_name(obj, new_dir, new_name, force,
1700 existing_target->obj_id);
1701 existing_target->is_shadowed = 1;
1702 yaffs_unlink_obj(existing_target);
1706 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
1708 yaffs_update_parent(old_dir);
1709 if(new_dir != old_dir)
1710 yaffs_update_parent(new_dir);
1717 /*------------------------- Block Management and Page Allocation ----------------*/
1719 static int yaffs_init_blocks(struct yaffs_dev *dev)
1721 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
1723 dev->block_info = NULL;
1724 dev->chunk_bits = NULL;
1726 dev->alloc_block = -1; /* force it to get a new one */
1728 /* If the first allocation strategy fails, thry the alternate one */
1729 dev->block_info = YMALLOC(n_blocks * sizeof(struct yaffs_block_info));
1730 if (!dev->block_info) {
1731 dev->block_info = YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_info));
1732 dev->block_info_alt = 1;
1734 dev->block_info_alt = 0;
1736 if (dev->block_info) {
1737 /* Set up dynamic blockinfo stuff. */
1738 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */
1739 dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * n_blocks);
1740 if (!dev->chunk_bits) {
1741 dev->chunk_bits = YMALLOC_ALT(dev->chunk_bit_stride * n_blocks);
1742 dev->chunk_bits_alt = 1;
1744 dev->chunk_bits_alt = 0;
1747 if (dev->block_info && dev->chunk_bits) {
1748 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
1749 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
1756 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
1758 if (dev->block_info_alt && dev->block_info)
1759 YFREE_ALT(dev->block_info);
1760 else if (dev->block_info)
1761 YFREE(dev->block_info);
1763 dev->block_info_alt = 0;
1765 dev->block_info = NULL;
1767 if (dev->chunk_bits_alt && dev->chunk_bits)
1768 YFREE_ALT(dev->chunk_bits);
1769 else if (dev->chunk_bits)
1770 YFREE(dev->chunk_bits);
1771 dev->chunk_bits_alt = 0;
1772 dev->chunk_bits = NULL;
1775 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
1777 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
1781 /* If the block is still healthy erase it and mark as clean.
1782 * If the block has had a data failure, then retire it.
1785 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
1786 (TSTR("yaffs_block_became_dirty block %d state %d %s"TENDSTR),
1787 block_no, bi->block_state, (bi->needs_retiring) ? "needs retiring" : ""));
1789 yaffs2_clear_oldest_dirty_seq(dev,bi);
1791 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
1793 /* If this is the block being garbage collected then stop gc'ing this block */
1794 if(block_no == dev->gc_block)
1797 /* If this block is currently the best candidate for gc then drop as a candidate */
1798 if(block_no == dev->gc_dirtiest){
1799 dev->gc_dirtiest = 0;
1800 dev->gc_pages_in_use = 0;
1803 if (!bi->needs_retiring) {
1804 yaffs2_checkpt_invalidate(dev);
1805 erased_ok = yaffs_erase_block(dev, block_no);
1807 dev->n_erase_failures++;
1808 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1809 (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
1814 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) || !yaffs_skip_verification(dev))) {
1816 for (i = 0; i < dev->param.chunks_per_block; i++) {
1817 if (!yaffs_check_chunk_erased
1818 (dev, block_no * dev->param.chunks_per_block + i)) {
1819 T(YAFFS_TRACE_ERROR,
1821 (">>Block %d erasure supposedly OK, but chunk %d not erased"
1822 TENDSTR), block_no, i));
1828 /* Clean it up... */
1829 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
1831 dev->n_erased_blocks++;
1832 bi->pages_in_use = 0;
1833 bi->soft_del_pages = 0;
1834 bi->has_shrink_hdr = 0;
1835 bi->skip_erased_check = 1; /* This is clean, so no need to check */
1836 bi->gc_prioritise = 0;
1837 yaffs_clear_chunk_bits(dev, block_no);
1839 T(YAFFS_TRACE_ERASE,
1840 (TSTR("Erased block %d" TENDSTR), block_no));
1842 dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */
1844 yaffs_retire_block(dev, block_no);
1845 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1846 (TSTR("**>> Block %d retired" TENDSTR), block_no));
1850 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
1854 struct yaffs_block_info *bi;
1856 if (dev->n_erased_blocks < 1) {
1857 /* Hoosterman we've got a problem.
1858 * Can't get space to gc
1860 T(YAFFS_TRACE_ERROR,
1861 (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
1866 /* Find an empty block. */
1868 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
1869 dev->alloc_block_finder++;
1870 if (dev->alloc_block_finder < dev->internal_start_block
1871 || dev->alloc_block_finder > dev->internal_end_block) {
1872 dev->alloc_block_finder = dev->internal_start_block;
1875 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
1877 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1878 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
1880 bi->seq_number = dev->seq_number;
1881 dev->n_erased_blocks--;
1882 T(YAFFS_TRACE_ALLOCATE,
1883 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
1884 dev->alloc_block_finder, dev->seq_number,
1885 dev->n_erased_blocks));
1886 return dev->alloc_block_finder;
1890 T(YAFFS_TRACE_ALWAYS,
1892 ("yaffs tragedy: no more erased blocks, but there should have been %d"
1893 TENDSTR), dev->n_erased_blocks));
1900 * Check if there's space to allocate...
1901 * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()?
1903 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
1905 int reserved_chunks;
1906 int reserved_blocks = dev->param.n_reserved_blocks;
1909 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
1911 reserved_chunks = ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
1913 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
1916 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
1917 struct yaffs_block_info **block_ptr)
1920 struct yaffs_block_info *bi;
1922 if (dev->alloc_block < 0) {
1923 /* Get next block to allocate off */
1924 dev->alloc_block = yaffs_find_alloc_block(dev);
1925 dev->alloc_page = 0;
1928 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
1929 /* Not enough space to allocate unless we're allowed to use the reserve. */
1933 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
1934 && dev->alloc_page == 0) {
1935 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
1938 /* Next page please.... */
1939 if (dev->alloc_block >= 0) {
1940 bi = yaffs_get_block_info(dev, dev->alloc_block);
1942 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
1945 yaffs_set_chunk_bit(dev, dev->alloc_block,
1950 dev->n_free_chunks--;
1952 /* If the block is full set the state to full */
1953 if (dev->alloc_page >= dev->param.chunks_per_block) {
1954 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1955 dev->alloc_block = -1;
1964 T(YAFFS_TRACE_ERROR,
1965 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
1970 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
1974 n = dev->n_erased_blocks * dev->param.chunks_per_block;
1976 if (dev->alloc_block > 0)
1977 n += (dev->param.chunks_per_block - dev->alloc_page);
1984 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
1985 * if we don't want to write to it.
1987 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
1989 if(dev->alloc_block > 0){
1990 struct yaffs_block_info *bi = yaffs_get_block_info(dev, dev->alloc_block);
1991 if(bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING){
1992 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1993 dev->alloc_block = -1;
1999 static int yaffs_gc_block(struct yaffs_dev *dev, int block,
2005 int ret_val = YAFFS_OK;
2007 int is_checkpt_block;
2011 int chunks_before = yaffs_get_erased_chunks(dev);
2014 struct yaffs_ext_tags tags;
2016 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2018 struct yaffs_obj *object;
2020 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2023 T(YAFFS_TRACE_TRACING,
2024 (TSTR("Collecting block %d, in use %d, shrink %d, whole_block %d" TENDSTR),
2030 /*yaffs_verify_free_chunks(dev); */
2032 if(bi->block_state == YAFFS_BLOCK_STATE_FULL)
2033 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2035 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2037 dev->gc_disable = 1;
2039 if (is_checkpt_block ||
2040 !yaffs_still_some_chunks(dev, block)) {
2041 T(YAFFS_TRACE_TRACING,
2043 ("Collecting block %d that has no chunks in use" TENDSTR),
2045 yaffs_block_became_dirty(dev, block);
2048 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2050 yaffs_verify_blk(dev, bi, block);
2052 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2053 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2055 for (/* init already done */;
2056 ret_val == YAFFS_OK &&
2057 dev->gc_chunk < dev->param.chunks_per_block &&
2058 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2060 dev->gc_chunk++, old_chunk++) {
2061 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2063 /* This page is in use and might need to be copied off */
2069 yaffs_init_tags(&tags);
2071 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2075 yaffs_find_by_number(dev,
2078 T(YAFFS_TRACE_GC_DETAIL,
2080 ("Collecting chunk in block %d, %d %d %d " TENDSTR),
2081 dev->gc_chunk, tags.obj_id, tags.chunk_id,
2084 if (object && !yaffs_skip_verification(dev)) {
2085 if (tags.chunk_id == 0)
2086 matching_chunk = object->hdr_chunk;
2087 else if (object->soft_del)
2088 matching_chunk = old_chunk; /* Defeat the test */
2090 matching_chunk = yaffs_find_chunk_in_file(object, tags.chunk_id, NULL);
2092 if (old_chunk != matching_chunk)
2093 T(YAFFS_TRACE_ERROR,
2094 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
2095 old_chunk, matching_chunk, tags.obj_id, tags.chunk_id));
2100 T(YAFFS_TRACE_ERROR,
2102 ("page %d in gc has no object: %d %d %d "
2103 TENDSTR), old_chunk,
2104 tags.obj_id, tags.chunk_id, tags.n_bytes));
2110 tags.chunk_id != 0) {
2111 /* Data chunk in a soft deleted file, throw it away
2112 * It's a soft deleted data chunk,
2113 * No need to copy this, just forget about it and
2114 * fix up the object.
2117 /* Free chunks already includes softdeleted chunks.
2118 * How ever this chunk is going to soon be really deleted
2119 * which will increment free chunks.
2120 * We have to decrement free chunks so this works out properly.
2122 dev->n_free_chunks--;
2123 bi->soft_del_pages--;
2125 object->n_data_chunks--;
2127 if (object->n_data_chunks <= 0) {
2128 /* remeber to clean up the object */
2129 dev->gc_cleanup_list[dev->n_clean_ups] =
2135 /* Todo object && object->deleted && object->n_data_chunks == 0 */
2136 /* Deleted object header with no data chunks.
2137 * Can be discarded and the file deleted.
2139 object->hdr_chunk = 0;
2140 yaffs_free_tnode(object->my_dev,
2143 object->variant.file_variant.top = NULL;
2144 yaffs_generic_obj_del(object);
2146 } else if (object) {
2147 /* It's either a data chunk in a live file or
2148 * an ObjectHeader, so we're interested in it.
2149 * NB Need to keep the ObjectHeaders of deleted files
2150 * until the whole file has been deleted off
2152 tags.serial_number++;
2156 if (tags.chunk_id == 0) {
2157 /* It is an object Id,
2158 * We need to nuke the shrinkheader flags first
2159 * Also need to clean up shadowing.
2160 * We no longer want the shrink_header flag since its work is done
2161 * and if it is left in place it will mess up scanning.
2164 struct yaffs_obj_hdr *oh;
2165 oh = (struct yaffs_obj_hdr *)buffer;
2168 tags.extra_is_shrink = 0;
2170 oh->shadows_obj = 0;
2171 oh->inband_shadowed_obj_id = 0;
2172 tags.extra_shadows = 0;
2174 /* Update file size */
2175 if(object->variant_type == YAFFS_OBJECT_TYPE_FILE){
2176 oh->file_size = object->variant.file_variant.file_size;
2177 tags.extra_length = oh->file_size;
2180 yaffs_verify_oh(object, oh, &tags, 1);
2182 yaffs_write_new_chunk(dev,(u8 *) oh, &tags, 1);
2185 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2187 if (new_chunk < 0) {
2188 ret_val = YAFFS_FAIL;
2191 /* Ok, now fix up the Tnodes etc. */
2193 if (tags.chunk_id == 0) {
2195 object->hdr_chunk = new_chunk;
2196 object->serial = tags.serial_number;
2198 /* It's a data chunk */
2200 ok = yaffs_put_chunk_in_file
2208 if (ret_val == YAFFS_OK)
2209 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2214 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2220 yaffs_verify_collected_blk(dev, bi, block);
2224 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2226 * The gc did not complete. Set block state back to FULL
2227 * because checkpointing does not restore gc.
2229 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2231 /* The gc completed. */
2232 /* Do any required cleanups */
2233 for (i = 0; i < dev->n_clean_ups; i++) {
2234 /* Time to delete the file too */
2236 yaffs_find_by_number(dev,
2237 dev->gc_cleanup_list[i]);
2239 yaffs_free_tnode(dev,
2240 object->variant.file_variant.
2242 object->variant.file_variant.top = NULL;
2245 ("yaffs: About to finally delete object %d"
2246 TENDSTR), object->obj_id));
2247 yaffs_generic_obj_del(object);
2248 object->my_dev->n_deleted_files--;
2254 chunks_after = yaffs_get_erased_chunks(dev);
2255 if (chunks_before >= chunks_after) {
2258 ("gc did not increase free chunks before %d after %d"
2259 TENDSTR), chunks_before, chunks_after));
2263 dev->n_clean_ups = 0;
2266 dev->gc_disable = 0;
2272 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2273 * for garbage collection.
2276 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2282 unsigned selected = 0;
2283 int prioritised = 0;
2284 int prioritised_exist = 0;
2285 struct yaffs_block_info *bi;
2288 /* First let's see if we need to grab a prioritised block */
2289 if (dev->has_pending_prioritised_gc && !aggressive) {
2290 dev->gc_dirtiest = 0;
2291 bi = dev->block_info;
2292 for (i = dev->internal_start_block;
2293 i <= dev->internal_end_block && !selected;
2296 if (bi->gc_prioritise) {
2297 prioritised_exist = 1;
2298 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2299 yaffs_block_ok_for_gc(dev, bi)) {
2308 * If there is a prioritised block and none was selected then
2309 * this happened because there is at least one old dirty block gumming
2310 * up the works. Let's gc the oldest dirty block.
2313 if(prioritised_exist &&
2315 dev->oldest_dirty_block > 0)
2316 selected = dev->oldest_dirty_block;
2318 if (!prioritised_exist) /* None found, so we can clear this */
2319 dev->has_pending_prioritised_gc = 0;
2322 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2324 * else (we're doing a leasurely gc), then we only bother to do this if the
2325 * block has only a few pages in use.
2330 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2332 threshold = dev->param.chunks_per_block;
2333 iterations = n_blocks;
2338 max_threshold = dev->param.chunks_per_block/2;
2340 max_threshold = dev->param.chunks_per_block/8;
2342 if(max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2343 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2345 threshold = background ?
2346 (dev->gc_not_done + 2) * 2 : 0;
2347 if(threshold <YAFFS_GC_PASSIVE_THRESHOLD)
2348 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2349 if(threshold > max_threshold)
2350 threshold = max_threshold;
2352 iterations = n_blocks / 16 + 1;
2353 if (iterations > 100)
2359 (dev->gc_dirtiest < 1 ||
2360 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2362 dev->gc_block_finder++;
2363 if (dev->gc_block_finder < dev->internal_start_block ||
2364 dev->gc_block_finder > dev->internal_end_block)
2365 dev->gc_block_finder = dev->internal_start_block;
2367 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2369 pages_used = bi->pages_in_use - bi->soft_del_pages;
2371 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2372 pages_used < dev->param.chunks_per_block &&
2373 (dev->gc_dirtiest < 1 || pages_used < dev->gc_pages_in_use) &&
2374 yaffs_block_ok_for_gc(dev, bi)) {
2375 dev->gc_dirtiest = dev->gc_block_finder;
2376 dev->gc_pages_in_use = pages_used;
2380 if(dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2381 selected = dev->gc_dirtiest;
2385 * If nothing has been selected for a while, try selecting the oldest dirty
2386 * because that's gumming up the works.
2389 if(!selected && dev->param.is_yaffs2 &&
2390 dev->gc_not_done >= ( background ? 10 : 20)){
2391 yaffs2_find_oldest_dirty_seq(dev);
2392 if(dev->oldest_dirty_block > 0) {
2393 selected = dev->oldest_dirty_block;
2394 dev->gc_dirtiest = selected;
2395 dev->oldest_dirty_gc_count++;
2396 bi = yaffs_get_block_info(dev, selected);
2397 dev->gc_pages_in_use = bi->pages_in_use - bi->soft_del_pages;
2399 dev->gc_not_done = 0;
2404 (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR),
2406 dev->param.chunks_per_block - dev->gc_pages_in_use,
2413 dev->gc_dirtiest = 0;
2414 dev->gc_pages_in_use = 0;
2415 dev->gc_not_done = 0;
2416 if(dev->refresh_skip > 0)
2417 dev->refresh_skip--;
2421 (TSTR("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" TENDSTR),
2422 dev->gc_block_finder, dev->gc_not_done,
2424 dev->gc_dirtiest, dev->gc_pages_in_use,
2425 dev->oldest_dirty_block,
2426 background ? " bg" : ""));
2432 /* New garbage collector
2433 * If we're very low on erased blocks then we do aggressive garbage collection
2434 * otherwise we do "leasurely" garbage collection.
2435 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2436 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2438 * The idea is to help clear out space in a more spread-out manner.
2439 * Dunno if it really does anything useful.
2441 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2444 int gc_ok = YAFFS_OK;
2448 int checkpt_block_adjust;
2450 if(dev->param.gc_control &&
2451 (dev->param.gc_control(dev) & 1) == 0)
2454 if (dev->gc_disable) {
2455 /* Bail out so we don't get recursive gc */
2459 /* This loop should pass the first time.
2460 * We'll only see looping here if the collection does not increase space.
2466 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2468 min_erased = dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2469 erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2471 /* If we need a block soon then do aggressive gc.*/
2472 if (dev->n_erased_blocks < min_erased)
2475 if(!background && erased_chunks > (dev->n_free_chunks / 4))
2478 if(dev->gc_skip > 20)
2480 if(erased_chunks < dev->n_free_chunks/2 ||
2492 /* If we don't already have a block being gc'd then see if we should start another */
2494 if (dev->gc_block < 1 && !aggressive) {
2495 dev->gc_block = yaffs2_find_refresh_block(dev);
2499 if (dev->gc_block < 1) {
2500 dev->gc_block = yaffs_find_gc_block(dev, aggressive, background);
2505 if (dev->gc_block > 0) {
2508 dev->passive_gc_count++;
2512 ("yaffs: GC n_erased_blocks %d aggressive %d" TENDSTR),
2513 dev->n_erased_blocks, aggressive));
2515 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2518 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) && dev->gc_block > 0) {
2521 ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d"
2522 TENDSTR), dev->n_erased_blocks, max_tries, dev->gc_block));
2524 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2525 (dev->gc_block > 0) &&
2528 return aggressive ? gc_ok : YAFFS_OK;
2533 * Garbage collects. Intended to be called from a background thread.
2534 * Returns non-zero if at least half the free chunks are erased.
2536 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2538 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2540 T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR),urgency));
2542 yaffs_check_gc(dev, 1);
2543 return erased_chunks > dev->n_free_chunks/2;
2546 /*------------------------- TAGS --------------------------------*/
2548 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
2551 return (tags->chunk_id == chunk_obj &&
2552 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
2557 /*-------------------- Data file manipulation -----------------*/
2559 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
2560 struct yaffs_ext_tags *tags)
2562 /*Get the Tnode, then get the level 0 offset chunk offset */
2563 struct yaffs_tnode *tn;
2565 struct yaffs_ext_tags local_tags;
2568 struct yaffs_dev *dev = in->my_dev;
2571 /* Passed a NULL, so use our own tags space */
2575 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2578 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2581 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2587 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
2588 struct yaffs_ext_tags *tags)
2590 /* Get the Tnode, then get the level 0 offset chunk offset */
2591 struct yaffs_tnode *tn;
2593 struct yaffs_ext_tags local_tags;
2595 struct yaffs_dev *dev = in->my_dev;
2599 /* Passed a NULL, so use our own tags space */
2603 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2607 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2610 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2613 /* Delete the entry in the filestructure (if found) */
2615 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
2622 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
2623 int nand_chunk, int in_scan)
2625 /* NB in_scan is zero unless scanning.
2626 * For forward scanning, in_scan is > 0;
2627 * for backward scanning in_scan is < 0
2629 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
2632 struct yaffs_tnode *tn;
2633 struct yaffs_dev *dev = in->my_dev;
2635 struct yaffs_ext_tags existing_tags;
2636 struct yaffs_ext_tags new_tags;
2637 unsigned existing_serial, new_serial;
2639 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
2640 /* Just ignore an attempt at putting a chunk into a non-file during scanning
2641 * If it is not during Scanning then something went wrong!
2644 T(YAFFS_TRACE_ERROR,
2646 ("yaffs tragedy:attempt to put data chunk into a non-file"
2651 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
2655 tn = yaffs_add_find_tnode_0(dev,
2656 &in->variant.file_variant,
2663 /* Dummy insert, bail now */
2666 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
2669 /* If we're scanning then we need to test for duplicates
2670 * NB This does not need to be efficient since it should only ever
2671 * happen when the power fails during a write, then only one
2672 * chunk should ever be affected.
2674 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
2675 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
2678 if (existing_cunk > 0) {
2679 /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
2680 * thus we have to do a FindChunkInFile to get the real chunk id.
2682 * We have a duplicate now we need to decide which one to use:
2684 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
2685 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
2686 * YAFFS1: Get both sets of tags and compare serial numbers.
2690 /* Only do this for forward scanning */
2691 yaffs_rd_chunk_tags_nand(dev,
2695 /* Do a proper find */
2697 yaffs_find_chunk_in_file(in, inode_chunk,
2701 if (existing_cunk <= 0) {
2702 /*Hoosterman - how did this happen? */
2704 T(YAFFS_TRACE_ERROR,
2706 ("yaffs tragedy: existing chunk < 0 in scan"
2711 /* NB The deleted flags should be false, otherwise the chunks will
2712 * not be loaded during a scan
2716 new_serial = new_tags.serial_number;
2717 existing_serial = existing_tags.serial_number;
2720 if ((in_scan > 0) &&
2721 (existing_cunk <= 0 ||
2722 ((existing_serial + 1) & 3) == new_serial)) {
2723 /* Forward scanning.
2725 * Delete the old one and drop through to update the tnode
2727 yaffs_chunk_del(dev, existing_cunk, 1,
2730 /* Backward scanning or we want to use the existing one
2732 * Delete the new one and return early so that the tnode isn't changed
2734 yaffs_chunk_del(dev, nand_chunk, 1,
2742 if (existing_cunk == 0)
2743 in->n_data_chunks++;
2745 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
2750 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk,
2753 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2755 if (nand_chunk >= 0)
2756 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2759 T(YAFFS_TRACE_NANDACCESS,
2760 (TSTR("Chunk %d not found zero instead" TENDSTR),
2762 /* get sane (zero) data if you read a hole */
2763 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2769 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash, int lyn)
2773 struct yaffs_ext_tags tags;
2774 struct yaffs_block_info *bi;
2780 block = chunk_id / dev->param.chunks_per_block;
2781 page = chunk_id % dev->param.chunks_per_block;
2784 if (!yaffs_check_chunk_bit(dev, block, page))
2785 T(YAFFS_TRACE_VERIFY,
2786 (TSTR("Deleting invalid chunk %d"TENDSTR),
2789 bi = yaffs_get_block_info(dev, block);
2791 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2793 T(YAFFS_TRACE_DELETION,
2794 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
2796 if (!dev->param.is_yaffs2 && mark_flash &&
2797 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2799 yaffs_init_tags(&tags);
2801 tags.is_deleted = 1;
2803 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2804 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2806 dev->n_unmarked_deletions++;
2809 /* Pull out of the management area.
2810 * If the whole block became dirty, this will kick off an erasure.
2812 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2813 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2814 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
2815 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2816 dev->n_free_chunks++;
2818 yaffs_clear_chunk_bit(dev, block, page);
2822 if (bi->pages_in_use == 0 &&
2823 !bi->has_shrink_hdr &&
2824 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2825 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
2826 yaffs_block_became_dirty(dev, block);
2833 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2834 const u8 *buffer, int n_bytes,
2837 /* Find old chunk Need to do this to get serial number
2838 * Write new one and patch into tree.
2839 * Invalidate old tags.
2843 struct yaffs_ext_tags prev_tags;
2846 struct yaffs_ext_tags new_tags;
2848 struct yaffs_dev *dev = in->my_dev;
2850 yaffs_check_gc(dev,0);
2852 /* Get the previous chunk at this location in the file if it exists.
2853 * If it does not exist then put a zero into the tree. This creates
2854 * the tnode now, rather than later when it is harder to clean up.
2856 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
2857 if(prev_chunk_id < 1 &&
2858 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
2861 /* Set up new tags */
2862 yaffs_init_tags(&new_tags);
2864 new_tags.chunk_id = inode_chunk;
2865 new_tags.obj_id = in->obj_id;
2866 new_tags.serial_number =
2867 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
2868 new_tags.n_bytes = n_bytes;
2870 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
2871 T(YAFFS_TRACE_ERROR,
2872 (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), n_bytes));
2878 yaffs_write_new_chunk(dev, buffer, &new_tags,
2881 if (new_chunk_id > 0) {
2882 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
2884 if (prev_chunk_id > 0)
2885 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
2887 yaffs_verify_file_sane(in);
2889 return new_chunk_id;
2893 /* UpdateObjectHeader updates the header on NAND for an object.
2894 * If name is not NULL, then that new name is used.
2896 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
2897 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
2900 struct yaffs_block_info *bi;
2902 struct yaffs_dev *dev = in->my_dev;
2909 struct yaffs_ext_tags new_tags;
2910 struct yaffs_ext_tags old_tags;
2911 const YCHAR *alias = NULL;
2914 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
2916 struct yaffs_obj_hdr *oh = NULL;
2918 yaffs_strcpy(old_name, _Y("silly old name"));
2922 in == dev->root_dir || /* The root_dir should also be saved */
2925 yaffs_check_gc(dev,0);
2926 yaffs_check_obj_details_loaded(in);
2928 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
2929 oh = (struct yaffs_obj_hdr *) buffer;
2931 prev_chunk_id = in->hdr_chunk;
2933 if (prev_chunk_id > 0) {
2934 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
2937 yaffs_verify_oh(in, oh, &old_tags, 0);
2939 memcpy(old_name, oh->name, sizeof(oh->name));
2940 memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
2942 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
2944 oh->type = in->variant_type;
2945 oh->yst_mode = in->yst_mode;
2946 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
2948 #ifdef CONFIG_YAFFS_WINCE
2949 oh->win_atime[0] = in->win_atime[0];
2950 oh->win_ctime[0] = in->win_ctime[0];
2951 oh->win_mtime[0] = in->win_mtime[0];
2952 oh->win_atime[1] = in->win_atime[1];
2953 oh->win_ctime[1] = in->win_ctime[1];
2954 oh->win_mtime[1] = in->win_mtime[1];
2956 oh->yst_uid = in->yst_uid;
2957 oh->yst_gid = in->yst_gid;
2958 oh->yst_atime = in->yst_atime;
2959 oh->yst_mtime = in->yst_mtime;
2960 oh->yst_ctime = in->yst_ctime;
2961 oh->yst_rdev = in->yst_rdev;
2964 oh->parent_obj_id = in->parent->obj_id;
2966 oh->parent_obj_id = 0;
2968 if (name && *name) {
2969 memset(oh->name, 0, sizeof(oh->name));
2970 yaffs_load_oh_from_name(dev,oh->name,name);
2971 } else if (prev_chunk_id > 0)
2972 memcpy(oh->name, old_name, sizeof(oh->name));
2974 memset(oh->name, 0, sizeof(oh->name));
2976 oh->is_shrink = is_shrink;
2978 switch (in->variant_type) {
2979 case YAFFS_OBJECT_TYPE_UNKNOWN:
2980 /* Should not happen */
2982 case YAFFS_OBJECT_TYPE_FILE:
2984 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
2985 || oh->parent_obj_id ==
2986 YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
2987 file_variant.file_size;
2989 case YAFFS_OBJECT_TYPE_HARDLINK:
2991 in->variant.hardlink_variant.equiv_id;
2993 case YAFFS_OBJECT_TYPE_SPECIAL:
2996 case YAFFS_OBJECT_TYPE_DIRECTORY:
2999 case YAFFS_OBJECT_TYPE_SYMLINK:
3000 alias = in->variant.symlink_variant.alias;
3002 alias = _Y("no alias");
3003 yaffs_strncpy(oh->alias,
3005 YAFFS_MAX_ALIAS_LENGTH);
3006 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3010 /* process any xattrib modifications */
3012 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3016 yaffs_init_tags(&new_tags);
3018 new_tags.chunk_id = 0;
3019 new_tags.obj_id = in->obj_id;
3020 new_tags.serial_number = in->serial;
3022 /* Add extra info for file header */
3024 new_tags.extra_available = 1;
3025 new_tags.extra_parent_id = oh->parent_obj_id;
3026 new_tags.extra_length = oh->file_size;
3027 new_tags.extra_is_shrink = oh->is_shrink;
3028 new_tags.extra_equiv_id = oh->equiv_id;
3029 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3030 new_tags.extra_obj_type = in->variant_type;
3032 yaffs_verify_oh(in, oh, &new_tags, 1);
3034 /* Create new chunk in NAND */
3036 yaffs_write_new_chunk(dev, buffer, &new_tags,
3037 (prev_chunk_id > 0) ? 1 : 0);
3039 if (new_chunk_id >= 0) {
3041 in->hdr_chunk = new_chunk_id;
3043 if (prev_chunk_id > 0) {
3044 yaffs_chunk_del(dev, prev_chunk_id, 1,
3048 if (!yaffs_obj_cache_dirty(in))
3051 /* If this was a shrink, then mark the block that the chunk lives on */
3053 bi = yaffs_get_block_info(in->my_dev,
3054 new_chunk_id / in->my_dev->param.chunks_per_block);
3055 bi->has_shrink_hdr = 1;
3060 ret_val = new_chunk_id;
3065 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3070 /*------------------------ Short Operations Cache ----------------------------------------
3071 * In many situations where there is no high level buffering (eg WinCE) a lot of
3072 * reads might be short sequential reads, and a lot of writes may be short
3073 * sequential writes. eg. scanning/writing a jpeg file.
3074 * In these cases, a short read/write cache can provide a huge perfomance benefit
3075 * with dumb-as-a-rock code.
3076 * In Linux, the page cache provides read buffering aand the short op cache provides write
3079 * There are a limited number (~10) of cache chunks per device so that we don't
3080 * need a very intelligent search.
3083 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
3085 struct yaffs_dev *dev = obj->my_dev;
3087 struct yaffs_cache *cache;
3088 int n_caches = obj->my_dev->param.n_caches;
3090 for (i = 0; i < n_caches; i++) {
3091 cache = &dev->cache[i];
3092 if (cache->object == obj &&
3101 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
3103 struct yaffs_dev *dev = obj->my_dev;
3104 int lowest = -99; /* Stop compiler whining. */
3106 struct yaffs_cache *cache;
3107 int chunk_written = 0;
3108 int n_caches = obj->my_dev->param.n_caches;
3114 /* Find the dirty cache for this object with the lowest chunk id. */
3115 for (i = 0; i < n_caches; i++) {
3116 if (dev->cache[i].object == obj &&
3117 dev->cache[i].dirty) {
3119 || dev->cache[i].chunk_id <
3121 cache = &dev->cache[i];
3122 lowest = cache->chunk_id;
3127 if (cache && !cache->locked) {
3128 /* Write it out and free it up */
3131 yaffs_wr_data_obj(cache->object,
3137 cache->object = NULL;
3140 } while (cache && chunk_written > 0);
3143 /* Hoosterman, disk full while writing cache out. */
3144 T(YAFFS_TRACE_ERROR,
3145 (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3152 /*yaffs_flush_whole_cache(dev)
3157 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
3159 struct yaffs_obj *obj;
3160 int n_caches = dev->param.n_caches;
3163 /* Find a dirty object in the cache and flush it...
3164 * until there are no further dirty objects.
3168 for (i = 0; i < n_caches && !obj; i++) {
3169 if (dev->cache[i].object &&
3170 dev->cache[i].dirty)
3171 obj = dev->cache[i].object;
3175 yaffs_flush_file_cache(obj);
3182 /* Grab us a cache chunk for use.
3183 * First look for an empty one.
3184 * Then look for the least recently used non-dirty one.
3185 * Then look for the least recently used dirty one...., flush and look again.
3187 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
3191 if (dev->param.n_caches > 0) {
3192 for (i = 0; i < dev->param.n_caches; i++) {
3193 if (!dev->cache[i].object)
3194 return &dev->cache[i];
3201 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
3203 struct yaffs_cache *cache;
3204 struct yaffs_obj *the_obj;
3209 if (dev->param.n_caches > 0) {
3210 /* Try find a non-dirty one... */
3212 cache = yaffs_grab_chunk_worker(dev);
3215 /* They were all dirty, find the last recently used object and flush
3216 * its cache, then find again.
3217 * NB what's here is not very accurate, we actually flush the object
3218 * the last recently used page.
3221 /* With locking we can't assume we can use entry zero */
3228 for (i = 0; i < dev->param.n_caches; i++) {
3229 if (dev->cache[i].object &&
3230 !dev->cache[i].locked &&
3231 (dev->cache[i].last_use < usage || !cache)) {
3232 usage = dev->cache[i].last_use;
3233 the_obj = dev->cache[i].object;
3234 cache = &dev->cache[i];
3239 if (!cache || cache->dirty) {
3240 /* Flush and try again */
3241 yaffs_flush_file_cache(the_obj);
3242 cache = yaffs_grab_chunk_worker(dev);
3252 /* Find a cached chunk */
3253 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
3256 struct yaffs_dev *dev = obj->my_dev;
3258 if (dev->param.n_caches > 0) {
3259 for (i = 0; i < dev->param.n_caches; i++) {
3260 if (dev->cache[i].object == obj &&
3261 dev->cache[i].chunk_id == chunk_id) {
3264 return &dev->cache[i];
3271 /* Mark the chunk for the least recently used algorithym */
3272 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
3276 if (dev->param.n_caches > 0) {
3277 if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
3278 /* Reset the cache usages */
3280 for (i = 1; i < dev->param.n_caches; i++)
3281 dev->cache[i].last_use = 0;
3283 dev->cache_last_use = 0;
3286 dev->cache_last_use++;
3288 cache->last_use = dev->cache_last_use;
3295 /* Invalidate a single cache page.
3296 * Do this when a whole page gets written,
3297 * ie the short cache for this page is no longer valid.
3299 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
3301 if (object->my_dev->param.n_caches > 0) {
3302 struct yaffs_cache *cache = yaffs_find_chunk_cache(object, chunk_id);
3305 cache->object = NULL;
3309 /* Invalidate all the cache pages associated with this object
3310 * Do this whenever ther file is deleted or resized.
3312 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
3315 struct yaffs_dev *dev = in->my_dev;
3317 if (dev->param.n_caches > 0) {
3318 /* Invalidate it. */
3319 for (i = 0; i < dev->param.n_caches; i++) {
3320 if (dev->cache[i].object == in)
3321 dev->cache[i].object = NULL;
3327 /*--------------------- File read/write ------------------------
3328 * Read and write have very similar structures.
3329 * In general the read/write has three parts to it
3330 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3331 * Some complete chunks
3332 * An incomplete chunk to end off with
3334 * Curve-balls: the first chunk might also be the last chunk.
3337 int yaffs_file_rd(struct yaffs_obj *in, u8 *buffer, loff_t offset,
3346 struct yaffs_cache *cache;
3348 struct yaffs_dev *dev;
3353 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3354 /* start = offset % dev->data_bytes_per_chunk; */
3355 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3358 /* OK now check for the curveball where the start and end are in
3361 if ((start + n) < dev->data_bytes_per_chunk)
3364 n_copy = dev->data_bytes_per_chunk - start;
3366 cache = yaffs_find_chunk_cache(in, chunk);
3368 /* If the chunk is already in the cache or it is less than a whole chunk
3369 * or we're using inband tags then use the cache (if there is caching)
3370 * else bypass the cache.
3372 if (cache || n_copy != dev->data_bytes_per_chunk || dev->param.inband_tags) {
3373 if (dev->param.n_caches > 0) {
3375 /* If we can't find the data in the cache, then load it up. */
3378 cache = yaffs_grab_chunk_cache(in->my_dev);
3380 cache->chunk_id = chunk;
3383 yaffs_rd_data_obj(in, chunk,
3389 yaffs_use_cache(dev, cache, 0);
3394 memcpy(buffer, &cache->data[start], n_copy);
3398 /* Read into the local buffer then copy..*/
3401 yaffs_get_temp_buffer(dev, __LINE__);
3402 yaffs_rd_data_obj(in, chunk,
3405 memcpy(buffer, &local_buffer[start], n_copy);
3408 yaffs_release_temp_buffer(dev, local_buffer,
3414 /* A full chunk. Read directly into the supplied buffer. */
3415 yaffs_rd_data_obj(in, chunk, buffer);
3429 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3430 int n_bytes, int write_trhrough)
3439 int start_write = offset;
3440 int chunk_written = 0;
3444 struct yaffs_dev *dev;
3448 while (n > 0 && chunk_written >= 0) {
3449 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3451 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3452 start >= dev->data_bytes_per_chunk) {
3453 T(YAFFS_TRACE_ERROR, (
3454 TSTR("AddrToChunk of offset %d gives chunk %d start %d"
3456 (int)offset, chunk, start));
3458 chunk++; /* File pos to chunk in file offset */
3460 /* OK now check for the curveball where the start and end are in
3464 if ((start + n) < dev->data_bytes_per_chunk) {
3467 /* Now folks, to calculate how many bytes to write back....
3468 * If we're overwriting and not writing to then end of file then
3469 * we need to write back as much as was there before.
3472 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3474 if (chunk_start > in->variant.file_variant.file_size)
3475 n_bytes_read = 0; /* Past end of file */
3477 n_bytes_read = in->variant.file_variant.file_size - chunk_start;
3479 if (n_bytes_read > dev->data_bytes_per_chunk)
3480 n_bytes_read = dev->data_bytes_per_chunk;
3484 (start + n)) ? n_bytes_read : (start + n);
3486 if (n_writeback < 0 || n_writeback > dev->data_bytes_per_chunk)
3490 n_copy = dev->data_bytes_per_chunk - start;
3491 n_writeback = dev->data_bytes_per_chunk;
3494 if (n_copy != dev->data_bytes_per_chunk || dev->param.inband_tags) {
3495 /* An incomplete start or end chunk (or maybe both start and end chunk),
3496 * or we're using inband tags, so we want to use the cache buffers.
3498 if (dev->param.n_caches > 0) {
3499 struct yaffs_cache *cache;
3500 /* If we can't find the data in the cache, then load the cache */
3501 cache = yaffs_find_chunk_cache(in, chunk);
3504 && yaffs_check_alloc_available(dev, 1)) {
3505 cache = yaffs_grab_chunk_cache(dev);
3507 cache->chunk_id = chunk;
3510 yaffs_rd_data_obj(in, chunk,
3514 !yaffs_check_alloc_available(dev, 1)) {
3515 /* Drop the cache if it was a read cache item and
3516 * no space check has been made for it.
3522 yaffs_use_cache(dev, cache, 1);
3526 memcpy(&cache->data[start], buffer,
3531 cache->n_bytes = n_writeback;
3533 if (write_trhrough) {
3538 cache->data, cache->n_bytes,
3544 chunk_written = -1; /* fail the write */
3547 /* An incomplete start or end chunk (or maybe both start and end chunk)
3548 * Read into the local buffer then copy, then copy over and write back.
3552 yaffs_get_temp_buffer(dev, __LINE__);
3554 yaffs_rd_data_obj(in, chunk,
3559 memcpy(&local_buffer[start], buffer, n_copy);
3562 yaffs_wr_data_obj(in, chunk,
3567 yaffs_release_temp_buffer(dev, local_buffer,
3573 /* A full chunk. Write directly from the supplied buffer. */
3578 yaffs_wr_data_obj(in, chunk, buffer,
3579 dev->data_bytes_per_chunk,
3582 /* Since we've overwritten the cached data, we better invalidate it. */
3583 yaffs_invalidate_chunk_cache(in, chunk);
3586 if (chunk_written >= 0) {
3595 /* Update file object */
3597 if ((start_write + n_done) > in->variant.file_variant.file_size)
3598 in->variant.file_variant.file_size = (start_write + n_done);
3605 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3606 int n_bytes, int write_trhrough)
3608 yaffs2_handle_hole(in,offset);
3609 return yaffs_do_file_wr(in,buffer,offset,n_bytes,write_trhrough);
3614 /* ---------------------- File resizing stuff ------------------ */
3616 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3619 struct yaffs_dev *dev = in->my_dev;
3620 int old_size = in->variant.file_variant.file_size;
3622 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3624 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3625 dev->data_bytes_per_chunk;
3629 /* Delete backwards so that we don't end up with holes if
3630 * power is lost part-way through the operation.
3632 for (i = last_del; i >= start_del; i--) {
3633 /* NB this could be optimised somewhat,
3634 * eg. could retrieve the tags and write them without
3635 * using yaffs_chunk_del
3638 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3641 (dev->internal_start_block * dev->param.chunks_per_block)
3643 ((dev->internal_end_block +
3644 1) * dev->param.chunks_per_block)) {
3645 T(YAFFS_TRACE_ALWAYS,
3646 (TSTR("Found daft chunk_id %d for %d" TENDSTR),
3649 in->n_data_chunks--;
3650 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3658 void yaffs_resize_file_down( struct yaffs_obj *obj, loff_t new_size)
3662 struct yaffs_dev *dev = obj->my_dev;
3664 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3666 yaffs_prune_chunks(obj, new_size);
3668 if (new_partial != 0) {
3669 int last_chunk = 1 + new_full;
3670 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3672 /* Got to read and rewrite the last chunk with its new size and zero pad */
3673 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3674 memset(local_buffer + new_partial, 0,
3675 dev->data_bytes_per_chunk - new_partial);
3677 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3680 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3683 obj->variant.file_variant.file_size = new_size;
3685 yaffs_prune_tree(dev, &obj->variant.file_variant);
3689 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3691 struct yaffs_dev *dev = in->my_dev;
3692 int old_size = in->variant.file_variant.file_size;
3694 yaffs_flush_file_cache(in);
3695 yaffs_invalidate_whole_cache(in);
3697 yaffs_check_gc(dev,0);
3699 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3702 if (new_size == old_size)
3705 if(new_size > old_size){
3706 yaffs2_handle_hole(in,new_size);
3707 in->variant.file_variant.file_size = new_size;
3709 /* new_size < old_size */
3710 yaffs_resize_file_down(in, new_size);
3713 /* Write a new object header to reflect the resize.
3714 * show we've shrunk the file, if need be
3715 * Do this only if the file is not in the deleted directories
3716 * and is not shadowed.
3720 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3721 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3722 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3728 loff_t yaffs_get_file_size(struct yaffs_obj *obj)
3730 YCHAR *alias = NULL;
3731 obj = yaffs_get_equivalent_obj(obj);
3733 switch (obj->variant_type) {
3734 case YAFFS_OBJECT_TYPE_FILE:
3735 return obj->variant.file_variant.file_size;
3736 case YAFFS_OBJECT_TYPE_SYMLINK:
3737 alias = obj->variant.symlink_variant.alias;
3740 return yaffs_strnlen(alias,YAFFS_MAX_ALIAS_LENGTH);
3748 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3752 yaffs_flush_file_cache(in);
3753 if(data_sync) /* Only sync data */
3757 #ifdef CONFIG_YAFFS_WINCE
3758 yfsd_win_file_time_now(in->win_mtime);
3761 in->yst_mtime = Y_CURRENT_TIME;
3766 ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3767 0) ? YAFFS_OK : YAFFS_FAIL;
3777 static int yaffs_generic_obj_del(struct yaffs_obj *in)
3780 /* First off, invalidate the file's data in the cache, without flushing. */
3781 yaffs_invalidate_whole_cache(in);
3783 if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
3784 /* Move to the unlinked directory so we have a record that it was deleted. */
3785 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0, 0);
3789 yaffs_remove_obj_from_dir(in);
3790 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
3798 /* yaffs_del_file deletes the whole file data
3799 * and the inode associated with the file.
3800 * It does not delete the links associated with the file.
3802 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3807 struct yaffs_dev *dev = in->my_dev;
3814 yaffs_change_obj_name(in, in->my_dev->del_dir,
3815 _Y("deleted"), 0, 0);
3816 T(YAFFS_TRACE_TRACING,
3817 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
3820 in->my_dev->n_deleted_files++;
3821 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3822 yaffs_resize_file(in, 0);
3823 yaffs_soft_del_file(in);
3826 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3827 _Y("unlinked"), 0, 0);
3834 int yaffs_del_file(struct yaffs_obj *in)
3836 int ret_val = YAFFS_OK;
3837 int deleted; /* Need to cache value on stack if in is freed */
3838 struct yaffs_dev *dev = in->my_dev;
3840 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3841 yaffs_resize_file(in, 0);
3843 if (in->n_data_chunks > 0) {
3844 /* Use soft deletion if there is data in the file.
3845 * That won't be the case if it has been resized to zero.
3848 ret_val = yaffs_unlink_file_if_needed(in);
3850 deleted = in->deleted;
3852 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3855 in->my_dev->n_deleted_files++;
3856 yaffs_soft_del_file(in);
3858 return deleted ? YAFFS_OK : YAFFS_FAIL;
3860 /* The file has no data chunks so we toss it immediately */
3861 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3862 in->variant.file_variant.top = NULL;
3863 yaffs_generic_obj_del(in);
3869 static int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3871 return (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3872 !(list_empty(&obj->variant.dir_variant.children));
3875 static int yaffs_del_dir(struct yaffs_obj *obj)
3877 /* First check that the directory is empty. */
3878 if (yaffs_is_non_empty_dir(obj))
3881 return yaffs_generic_obj_del(obj);
3884 static int yaffs_del_symlink(struct yaffs_obj *in)
3886 if(in->variant.symlink_variant.alias)
3887 YFREE(in->variant.symlink_variant.alias);
3888 in->variant.symlink_variant.alias=NULL;
3890 return yaffs_generic_obj_del(in);
3893 static int yaffs_del_link(struct yaffs_obj *in)
3895 /* remove this hardlink from the list assocaited with the equivalent
3898 list_del_init(&in->hard_links);
3899 return yaffs_generic_obj_del(in);
3902 int yaffs_del_obj(struct yaffs_obj *obj)
3905 switch (obj->variant_type) {
3906 case YAFFS_OBJECT_TYPE_FILE:
3907 ret_val = yaffs_del_file(obj);
3909 case YAFFS_OBJECT_TYPE_DIRECTORY:
3910 if(!list_empty(&obj->variant.dir_variant.dirty)){
3911 T(YAFFS_TRACE_BACKGROUND, (TSTR("Remove object %d from dirty directories" TENDSTR),obj->obj_id));
3912 list_del_init(&obj->variant.dir_variant.dirty);
3914 return yaffs_del_dir(obj);
3916 case YAFFS_OBJECT_TYPE_SYMLINK:
3917 ret_val = yaffs_del_symlink(obj);
3919 case YAFFS_OBJECT_TYPE_HARDLINK:
3920 ret_val = yaffs_del_link(obj);
3922 case YAFFS_OBJECT_TYPE_SPECIAL:
3923 ret_val = yaffs_generic_obj_del(obj);
3925 case YAFFS_OBJECT_TYPE_UNKNOWN:
3927 break; /* should not happen. */
3933 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3942 yaffs_update_parent(obj->parent);
3944 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3945 return yaffs_del_link(obj);
3946 } else if (!list_empty(&obj->hard_links)) {
3947 /* Curve ball: We're unlinking an object that has a hardlink.
3949 * This problem arises because we are not strictly following
3950 * The Linux link/inode model.
3952 * We can't really delete the object.
3953 * Instead, we do the following:
3954 * - Select a hardlink.
3955 * - Unhook it from the hard links
3956 * - Move it from its parent directory (so that the rename can work)
3957 * - Rename the object to the hardlink's name.
3958 * - Delete the hardlink
3961 struct yaffs_obj *hl;
3962 struct yaffs_obj *parent;
3964 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3966 hl = list_entry(obj->hard_links.next, struct yaffs_obj, hard_links);
3968 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3969 parent = hl->parent;
3971 list_del_init(&hl->hard_links);
3973 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3975 ret_val = yaffs_change_obj_name(obj,parent, name, 0, 0);
3977 if (ret_val == YAFFS_OK)
3978 ret_val = yaffs_generic_obj_del(hl);
3982 } else if (del_now) {
3983 switch (obj->variant_type) {
3984 case YAFFS_OBJECT_TYPE_FILE:
3985 return yaffs_del_file(obj);
3987 case YAFFS_OBJECT_TYPE_DIRECTORY:
3988 list_del_init(&obj->variant.dir_variant.dirty);
3989 return yaffs_del_dir(obj);
3991 case YAFFS_OBJECT_TYPE_SYMLINK:
3992 return yaffs_del_symlink(obj);
3994 case YAFFS_OBJECT_TYPE_SPECIAL:
3995 return yaffs_generic_obj_del(obj);
3997 case YAFFS_OBJECT_TYPE_HARDLINK:
3998 case YAFFS_OBJECT_TYPE_UNKNOWN:
4002 } else if(yaffs_is_non_empty_dir(obj))
4005 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4006 _Y("unlinked"), 0, 0);
4010 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4013 if (obj && obj->unlink_allowed)
4014 return yaffs_unlink_worker(obj);
4019 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4021 struct yaffs_obj *obj;
4023 obj = yaffs_find_by_name(dir, name);
4024 return yaffs_unlink_obj(obj);
4027 /*----------------------- Initialisation Scanning ---------------------- */
4029 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4030 int backward_scanning)
4032 struct yaffs_obj *obj;
4034 if (!backward_scanning) {
4035 /* Handle YAFFS1 forward scanning case
4036 * For YAFFS1 we always do the deletion
4040 /* Handle YAFFS2 case (backward scanning)
4041 * If the shadowed object exists then ignore.
4043 obj = yaffs_find_by_number(dev, obj_id);
4048 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4049 * We put it in unlinked dir to be cleaned up after the scanning
4052 yaffs_find_or_create_by_number(dev, obj_id,
4053 YAFFS_OBJECT_TYPE_FILE);
4056 obj->is_shadowed = 1;
4057 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4058 obj->variant.file_variant.shrink_size = 0;
4059 obj->valid = 1; /* So that we don't read any other info for this file */
4064 void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
4066 struct yaffs_obj *hl;
4067 struct yaffs_obj *in;
4071 hard_list = (struct yaffs_obj *) (hard_list->hard_links.next);
4073 in = yaffs_find_by_number(dev,
4074 hl->variant.hardlink_variant.
4078 /* Add the hardlink pointers */
4079 hl->variant.hardlink_variant.equiv_obj = in;
4080 list_add(&hl->hard_links, &in->hard_links);
4082 /* Todo Need to report/handle this better.
4083 * Got a problem... hardlink to a non-existant object
4085 hl->variant.hardlink_variant.equiv_obj = NULL;
4086 INIT_LIST_HEAD(&hl->hard_links);
4093 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4096 * Sort out state of unlinked and deleted objects after scanning.
4098 struct list_head *i;
4099 struct list_head *n;
4100 struct yaffs_obj *l;
4105 /* Soft delete all the unlinked files */
4106 list_for_each_safe(i, n,
4107 &dev->unlinked_dir->variant.dir_variant.children) {
4109 l = list_entry(i, struct yaffs_obj, siblings);
4114 list_for_each_safe(i, n,
4115 &dev->del_dir->variant.dir_variant.children) {
4117 l = list_entry(i, struct yaffs_obj, siblings);
4125 * This code iterates through all the objects making sure that they are rooted.
4126 * Any unrooted objects are re-rooted in lost+found.
4127 * An object needs to be in one of:
4128 * - Directly under deleted, unlinked
4129 * - Directly or indirectly under root.
4132 * This code assumes that we don't ever change the current relationships between
4134 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4135 * lost-n-found->parent == root_dir
4137 * This fixes the problem where directories might have inadvertently been deleted
4138 * leaving the object "hanging" without being rooted in the directory tree.
4141 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4143 return (obj == dev->del_dir ||
4144 obj == dev->unlinked_dir||
4145 obj == dev->root_dir);
4148 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4150 struct yaffs_obj *obj;
4151 struct yaffs_obj *parent;
4153 struct list_head *lh;
4154 struct list_head *n;
4161 /* Iterate through the objects in each hash entry,
4162 * looking at each object.
4163 * Make sure it is rooted.
4166 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4167 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4169 obj = list_entry(lh, struct yaffs_obj, hash_link);
4170 parent= obj->parent;
4172 if(yaffs_has_null_parent(dev,obj)){
4173 /* These directories are not hanging */
4176 else if(!parent || parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4178 else if(yaffs_has_null_parent(dev,parent))
4182 * Need to follow the parent chain to see if it is hanging.
4187 while(parent != dev->root_dir &&
4189 parent->parent->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
4191 parent = parent->parent;
4194 if(parent != dev->root_dir)
4199 (TSTR("Hanging object %d moved to lost and found" TENDSTR),
4201 yaffs_add_obj_to_dir(dev->lost_n_found,obj);
4210 * Delete directory contents for cleaning up lost and found.
4212 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4214 struct yaffs_obj *obj;
4215 struct list_head *lh;
4216 struct list_head *n;
4218 if(dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4221 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4223 obj = list_entry(lh, struct yaffs_obj, siblings);
4224 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4225 yaffs_del_dir_contents(obj);
4228 (TSTR("Deleting lost_found object %d" TENDSTR),
4231 /* Need to use UnlinkObject since Delete would not handle
4232 * hardlinked objects correctly.
4234 yaffs_unlink_obj(obj);
4240 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4242 yaffs_del_dir_contents(dev->lost_n_found);
4245 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
4248 struct yaffs_obj_hdr *oh;
4249 struct yaffs_dev *dev;
4250 struct yaffs_ext_tags tags;
4252 int alloc_failed = 0;
4259 if (in->lazy_loaded && in->hdr_chunk > 0) {
4260 in->lazy_loaded = 0;
4261 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
4263 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data, &tags);
4264 oh = (struct yaffs_obj_hdr *) chunk_data;
4266 in->yst_mode = oh->yst_mode;
4267 #ifdef CONFIG_YAFFS_WINCE
4268 in->win_atime[0] = oh->win_atime[0];
4269 in->win_ctime[0] = oh->win_ctime[0];
4270 in->win_mtime[0] = oh->win_mtime[0];
4271 in->win_atime[1] = oh->win_atime[1];
4272 in->win_ctime[1] = oh->win_ctime[1];
4273 in->win_mtime[1] = oh->win_mtime[1];
4275 in->yst_uid = oh->yst_uid;
4276 in->yst_gid = oh->yst_gid;
4277 in->yst_atime = oh->yst_atime;
4278 in->yst_mtime = oh->yst_mtime;
4279 in->yst_ctime = oh->yst_ctime;
4280 in->yst_rdev = oh->yst_rdev;
4283 yaffs_set_obj_name_from_oh(in, oh);
4285 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4286 in->variant.symlink_variant.alias =
4287 yaffs_clone_str(oh->alias);
4288 if (!in->variant.symlink_variant.alias)
4289 alloc_failed = 1; /* Not returned to caller */
4292 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
4296 /*------------------------------ Directory Functions ----------------------------- */
4299 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
4300 * link (ie. name) is created or deleted in the directory.
4303 * create dir/a : update dir's mtime/ctime
4304 * rm dir/a: update dir's mtime/ctime
4305 * modify dir/a: don't update dir's mtimme/ctime
4307 * This can be handled immediately or defered. Defering helps reduce the number
4308 * of updates when many files in a directory are changed within a brief period.
4310 * If the directory updating is defered then yaffs_update_dirty_dirs must be
4311 * called periodically.
4314 static void yaffs_update_parent(struct yaffs_obj *obj)
4316 struct yaffs_dev *dev;
4319 #ifndef CONFIG_YAFFS_WINCE
4323 obj->yst_mtime = obj->yst_ctime = Y_CURRENT_TIME;
4324 if(dev->param.defered_dir_update){
4325 struct list_head *link = &obj->variant.dir_variant.dirty;
4327 if(list_empty(link)){
4328 list_add(link,&dev->dirty_dirs);
4329 T(YAFFS_TRACE_BACKGROUND, (TSTR("Added object %d to dirty directories" TENDSTR),obj->obj_id));
4333 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
4337 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
4339 struct list_head *link;
4340 struct yaffs_obj *obj;
4341 struct yaffs_dir_var *d_s;
4342 union yaffs_obj_var *o_v;
4344 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
4346 while(!list_empty(&dev->dirty_dirs)){
4347 link = dev->dirty_dirs.next;
4348 list_del_init(link);
4350 d_s=list_entry(link,struct yaffs_dir_var,dirty);
4351 o_v = list_entry(d_s,union yaffs_obj_var,dir_variant);
4352 obj = list_entry(o_v,struct yaffs_obj,variant);
4354 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update directory %d" TENDSTR), obj->obj_id));
4357 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
4361 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
4363 struct yaffs_dev *dev = obj->my_dev;
4364 struct yaffs_obj *parent;
4366 yaffs_verify_obj_in_dir(obj);
4367 parent = obj->parent;
4369 yaffs_verify_dir(parent);
4371 if (dev && dev->param.remove_obj_fn)
4372 dev->param.remove_obj_fn(obj);
4375 list_del_init(&obj->siblings);
4378 yaffs_verify_dir(parent);
4381 void yaffs_add_obj_to_dir(struct yaffs_obj *directory,
4382 struct yaffs_obj *obj)
4385 T(YAFFS_TRACE_ALWAYS,
4387 ("tragedy: Trying to add an object to a null pointer directory"
4392 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4393 T(YAFFS_TRACE_ALWAYS,
4395 ("tragedy: Trying to add an object to a non-directory"
4400 if (obj->siblings.prev == NULL) {
4401 /* Not initialised */
4406 yaffs_verify_dir(directory);
4408 yaffs_remove_obj_from_dir(obj);
4412 list_add(&obj->siblings, &directory->variant.dir_variant.children);
4413 obj->parent = directory;
4415 if (directory == obj->my_dev->unlinked_dir
4416 || directory == obj->my_dev->del_dir) {
4418 obj->my_dev->n_unlinked_files++;
4419 obj->rename_allowed = 0;
4422 yaffs_verify_dir(directory);
4423 yaffs_verify_obj_in_dir(obj);
4426 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4431 struct list_head *i;
4432 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4434 struct yaffs_obj *l;
4440 T(YAFFS_TRACE_ALWAYS,
4442 ("tragedy: yaffs_find_by_name: null pointer directory"
4447 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4448 T(YAFFS_TRACE_ALWAYS,
4450 ("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
4454 sum = yaffs_calc_name_sum(name);
4456 list_for_each(i, &directory->variant.dir_variant.children) {
4458 l = list_entry(i, struct yaffs_obj, siblings);
4460 if (l->parent != directory)
4463 yaffs_check_obj_details_loaded(l);
4465 /* Special case for lost-n-found */
4466 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4467 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
4469 } else if (yaffs_sum_cmp(l->sum, sum) || l->hdr_chunk <= 0) {
4470 /* LostnFound chunk called Objxxx
4473 yaffs_get_obj_name(l, buffer,
4474 YAFFS_MAX_NAME_LENGTH + 1);
4475 if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4485 /* GetEquivalentObject dereferences any hard links to get to the
4489 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4491 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4492 /* We want the object id of the equivalent object, not this one */
4493 obj = obj->variant.hardlink_variant.equiv_obj;
4494 yaffs_check_obj_details_loaded(obj);
4500 * A note or two on object names.
4501 * * If the object name is missing, we then make one up in the form objnnn
4503 * * ASCII names are stored in the object header's name field from byte zero
4504 * * Unicode names are historically stored starting from byte zero.
4506 * Then there are automatic Unicode names...
4507 * The purpose of these is to save names in a way that can be read as
4508 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4509 * system to share files.
4511 * These automatic unicode are stored slightly differently...
4512 * - If the name can fit in the ASCII character space then they are saved as
4513 * ascii names as per above.
4514 * - If the name needs Unicode then the name is saved in Unicode
4515 * starting at oh->name[1].
4518 static void yaffs_fix_null_name(struct yaffs_obj * obj,YCHAR * name, int buffer_size)
4520 /* Create an object name if we could not find one. */
4521 if(yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH) == 0){
4522 YCHAR local_name[20];
4523 YCHAR num_string[20];
4524 YCHAR *x = &num_string[19];
4525 unsigned v = obj->obj_id;
4529 *x = '0' + (v % 10);
4532 /* make up a name */
4533 yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4534 yaffs_strcat(local_name,x);
4535 yaffs_strncpy(name, local_name, buffer_size - 1);
4539 static void yaffs_load_name_from_oh(struct yaffs_dev *dev,YCHAR *name, const YCHAR *oh_name, int buff_size)
4541 #ifdef CONFIG_YAFFS_AUTO_UNICODE
4542 if(dev->param.auto_unicode){
4544 /* It is an ASCII name, so do an ASCII to unicode conversion */
4545 const char *ascii_oh_name = (const char *)oh_name;
4546 int n = buff_size - 1;
4547 while(n > 0 && *ascii_oh_name){
4548 *name = *ascii_oh_name;
4554 yaffs_strncpy(name,oh_name+1, buff_size -1);
4557 yaffs_strncpy(name, oh_name, buff_size - 1);
4561 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name, const YCHAR *name)
4563 #ifdef CONFIG_YAFFS_AUTO_UNICODE
4568 if(dev->param.auto_unicode){
4573 /* Figure out if the name will fit in ascii character set */
4574 while(is_ascii && *w){
4581 /* It is an ASCII name, so do a unicode to ascii conversion */
4582 char *ascii_oh_name = (char *)oh_name;
4583 int n = YAFFS_MAX_NAME_LENGTH - 1;
4584 while(n > 0 && *name){
4585 *ascii_oh_name= *name;
4591 /* It is a unicode name, so save starting at the second YCHAR */
4593 yaffs_strncpy(oh_name+1,name, YAFFS_MAX_NAME_LENGTH -2);
4598 yaffs_strncpy(oh_name,name, YAFFS_MAX_NAME_LENGTH - 1);
4602 int yaffs_get_obj_name(struct yaffs_obj * obj, YCHAR * name, int buffer_size)
4604 memset(name, 0, buffer_size * sizeof(YCHAR));
4606 yaffs_check_obj_details_loaded(obj);
4608 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4609 yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4611 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4612 else if (obj->short_name[0]) {
4613 yaffs_strcpy(name, obj->short_name);
4616 else if(obj->hdr_chunk > 0) {
4618 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4620 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *) buffer;
4622 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4624 if (obj->hdr_chunk > 0) {
4625 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4626 obj->hdr_chunk, buffer,
4629 yaffs_load_name_from_oh(obj->my_dev,name,oh->name,buffer_size);
4631 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4634 yaffs_fix_null_name(obj,name,buffer_size);
4636 return yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH);
4640 int yaffs_get_obj_length(struct yaffs_obj *obj)
4642 /* Dereference any hard linking */
4643 obj = yaffs_get_equivalent_obj(obj);
4645 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4646 return obj->variant.file_variant.file_size;
4647 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
4648 if(!obj->variant.symlink_variant.alias)
4650 return yaffs_strnlen(obj->variant.symlink_variant.alias,YAFFS_MAX_ALIAS_LENGTH);
4652 /* Only a directory should drop through to here */
4653 return obj->my_dev->data_bytes_per_chunk;
4657 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4660 struct list_head *i;
4663 count++; /* the object itself */
4665 list_for_each(i, &obj->hard_links)
4666 count++; /* add the hard links; */
4671 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4673 obj = yaffs_get_equivalent_obj(obj);
4678 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4680 obj = yaffs_get_equivalent_obj(obj);
4682 switch (obj->variant_type) {
4683 case YAFFS_OBJECT_TYPE_FILE:
4686 case YAFFS_OBJECT_TYPE_DIRECTORY:
4689 case YAFFS_OBJECT_TYPE_SYMLINK:
4692 case YAFFS_OBJECT_TYPE_HARDLINK:
4695 case YAFFS_OBJECT_TYPE_SPECIAL:
4696 if (S_ISFIFO(obj->yst_mode))
4698 if (S_ISCHR(obj->yst_mode))
4700 if (S_ISBLK(obj->yst_mode))
4702 if (S_ISSOCK(obj->yst_mode))
4710 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4712 obj = yaffs_get_equivalent_obj(obj);
4713 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4714 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4716 return yaffs_clone_str(_Y(""));
4719 #ifndef CONFIG_YAFFS_WINCE
4721 int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
4723 unsigned int valid = attr->ia_valid;
4725 if (valid & ATTR_MODE)
4726 obj->yst_mode = attr->ia_mode;
4727 if (valid & ATTR_UID)
4728 obj->yst_uid = attr->ia_uid;
4729 if (valid & ATTR_GID)
4730 obj->yst_gid = attr->ia_gid;
4732 if (valid & ATTR_ATIME)
4733 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
4734 if (valid & ATTR_CTIME)
4735 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
4736 if (valid & ATTR_MTIME)
4737 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
4739 if (valid & ATTR_SIZE)
4740 yaffs_resize_file(obj, attr->ia_size);
4742 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
4747 int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
4749 unsigned int valid = 0;
4751 attr->ia_mode = obj->yst_mode;
4753 attr->ia_uid = obj->yst_uid;
4755 attr->ia_gid = obj->yst_gid;
4758 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
4759 valid |= ATTR_ATIME;
4760 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
4761 valid |= ATTR_CTIME;
4762 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
4763 valid |= ATTR_MTIME;
4765 attr->ia_size = yaffs_get_file_size(obj);
4768 attr->ia_valid = valid;
4776 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set, const YCHAR *name, const void *value, int size, int flags)
4778 struct yaffs_xattr_mod xmod;
4787 xmod.result = -ENOSPC;
4789 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
4797 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer, struct yaffs_xattr_mod *xmod)
4800 int x_offs = sizeof(struct yaffs_obj_hdr);
4801 struct yaffs_dev *dev = obj->my_dev;
4802 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
4804 char * x_buffer = buffer + x_offs;
4807 retval = nval_set(x_buffer, x_size, xmod->name, xmod->data, xmod->size, xmod->flags);
4809 retval = nval_del(x_buffer, x_size, xmod->name);
4811 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4812 obj->xattr_known = 1;
4814 xmod->result = retval;
4819 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name, void *value, int size)
4821 char *buffer = NULL;
4823 struct yaffs_ext_tags tags;
4824 struct yaffs_dev *dev = obj->my_dev;
4825 int x_offs = sizeof(struct yaffs_obj_hdr);
4826 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
4832 if(obj->hdr_chunk < 1)
4835 /* If we know that the object has no xattribs then don't do all the
4836 * reading and parsing.
4838 if(obj->xattr_known && !obj->has_xattr){
4845 buffer = (char *) yaffs_get_temp_buffer(dev, __LINE__);
4849 result = yaffs_rd_chunk_tags_nand(dev,obj->hdr_chunk, (u8 *)buffer, &tags);
4851 if(result != YAFFS_OK)
4854 x_buffer = buffer + x_offs;
4856 if (!obj->xattr_known){
4857 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4858 obj->xattr_known = 1;
4862 retval = nval_get(x_buffer, x_size, name, value, size);
4864 retval = nval_list(x_buffer, x_size, value,size);
4866 yaffs_release_temp_buffer(dev,(u8 *)buffer,__LINE__);
4870 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR *name, const void * value, int size, int flags)
4872 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
4875 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR *name)
4877 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
4880 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR *name, void *value, int size)
4882 return yaffs_do_xattrib_fetch(obj, name, value, size);
4885 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
4887 return yaffs_do_xattrib_fetch(obj, NULL, buffer,size);
4890 /*---------------------------- Initialisation code -------------------------------------- */
4892 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4895 /* Common functions, gotta have */
4896 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4899 #ifdef CONFIG_YAFFS_YAFFS2
4901 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4902 if (dev->param.write_chunk_tags_fn &&
4903 dev->param.read_chunk_tags_fn &&
4904 !dev->param.write_chunk_fn &&
4905 !dev->param.read_chunk_fn &&
4906 dev->param.bad_block_fn &&
4907 dev->param.query_block_fn)
4911 /* Can use the "spare" style interface for yaffs1 */
4912 if (!dev->param.is_yaffs2 &&
4913 !dev->param.write_chunk_tags_fn &&
4914 !dev->param.read_chunk_tags_fn &&
4915 dev->param.write_chunk_fn &&
4916 dev->param.read_chunk_fn &&
4917 !dev->param.bad_block_fn &&
4918 !dev->param.query_block_fn)
4925 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4927 /* Initialise the unlinked, deleted, root and lost and found directories */
4929 dev->lost_n_found = dev->root_dir = NULL;
4930 dev->unlinked_dir = dev->del_dir = NULL;
4933 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4936 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4939 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4940 YAFFS_ROOT_MODE | S_IFDIR);
4942 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4943 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4945 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir && dev->del_dir) {
4946 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4953 int yaffs_guts_initialise(struct yaffs_dev *dev)
4955 int init_failed = 0;
4959 T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR)));
4961 /* Check stuff that must be set */
4964 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
4968 dev->internal_start_block = dev->param.start_block;
4969 dev->internal_end_block = dev->param.end_block;
4970 dev->block_offset = 0;
4971 dev->chunk_offset = 0;
4972 dev->n_free_chunks = 0;
4976 if (dev->param.start_block == 0) {
4977 dev->internal_start_block = dev->param.start_block + 1;
4978 dev->internal_end_block = dev->param.end_block + 1;
4979 dev->block_offset = 1;
4980 dev->chunk_offset = dev->param.chunks_per_block;
4983 /* Check geometry parameters. */
4985 if ((!dev->param.inband_tags && dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 1024) ||
4986 (!dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 512) ||
4987 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4988 dev->param.chunks_per_block < 2 ||
4989 dev->param.n_reserved_blocks < 2 ||
4990 dev->internal_start_block <= 0 ||
4991 dev->internal_end_block <= 0 ||
4992 dev->internal_end_block <= (dev->internal_start_block + dev->param.n_reserved_blocks + 2)) { /* otherwise it is too small */
4993 T(YAFFS_TRACE_ALWAYS,
4995 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d "
4996 TENDSTR), dev->param.total_bytes_per_chunk, dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags));
5000 if (yaffs_init_nand(dev) != YAFFS_OK) {
5001 T(YAFFS_TRACE_ALWAYS,
5002 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
5006 /* Sort out space for inband tags, if required */
5007 if (dev->param.inband_tags)
5008 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk - sizeof(struct yaffs_packed_tags2_tags_only);
5010 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
5012 /* Got the right mix of functions? */
5013 if (!yaffs_check_dev_fns(dev)) {
5014 /* Function missing */
5015 T(YAFFS_TRACE_ALWAYS,
5017 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5022 if (dev->is_mounted) {
5023 T(YAFFS_TRACE_ALWAYS,
5024 (TSTR("yaffs: device already mounted\n" TENDSTR)));
5028 /* Finished with most checks. One or two more checks happen later on too. */
5030 dev->is_mounted = 1;
5032 /* OK now calculate a few things for the device */
5035 * Calculate all the chunk size manipulation numbers:
5037 x = dev->data_bytes_per_chunk;
5038 /* We always use dev->chunk_shift and dev->chunk_div */
5039 dev->chunk_shift = calc_shifts(x);
5040 x >>= dev->chunk_shift;
5042 /* We only use chunk mask if chunk_div is 1 */
5043 dev->chunk_mask = (1<<dev->chunk_shift) - 1;
5046 * Calculate chunk_grp_bits.
5047 * We need to find the next power of 2 > than internal_end_block
5050 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
5052 bits = calc_shifts_ceiling(x);
5054 /* Set up tnode width if wide tnodes are enabled. */
5055 if (!dev->param.wide_tnodes_disabled) {
5056 /* bits must be even so that we end up with 32-bit words */
5060 dev->tnode_width = 16;
5062 dev->tnode_width = bits;
5064 dev->tnode_width = 16;
5066 dev->tnode_mask = (1<<dev->tnode_width)-1;
5068 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
5069 * so if the bitwidth of the
5070 * chunk range we're using is greater than 16 we need
5071 * to figure out chunk shift and chunk_grp_size
5074 if (bits <= dev->tnode_width)
5075 dev->chunk_grp_bits = 0;
5077 dev->chunk_grp_bits = bits - dev->tnode_width;
5079 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0)/8;
5080 if(dev->tnode_size < sizeof(struct yaffs_tnode))
5081 dev->tnode_size = sizeof(struct yaffs_tnode);
5083 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
5085 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
5086 /* We have a problem because the soft delete won't work if
5087 * the chunk group size > chunks per block.
5088 * This can be remedied by using larger "virtual blocks".
5090 T(YAFFS_TRACE_ALWAYS,
5091 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
5096 /* OK, we've finished verifying the device, lets continue with initialisation */
5098 /* More device initialisation */
5100 dev->passive_gc_count = 0;
5101 dev->oldest_dirty_gc_count = 0;
5103 dev->gc_block_finder = 0;
5104 dev->buffered_block = -1;
5105 dev->doing_buffered_block_rewrite = 0;
5106 dev->n_deleted_files = 0;
5107 dev->n_bg_deletions = 0;
5108 dev->n_unlinked_files = 0;
5109 dev->n_ecc_fixed = 0;
5110 dev->n_ecc_unfixed = 0;
5111 dev->n_tags_ecc_fixed = 0;
5112 dev->n_tags_ecc_unfixed = 0;
5113 dev->n_erase_failures = 0;
5114 dev->n_erased_blocks = 0;
5116 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
5117 INIT_LIST_HEAD(&dev->dirty_dirs);
5118 dev->oldest_dirty_seq = 0;
5119 dev->oldest_dirty_block = 0;
5121 /* Initialise temporary buffers and caches. */
5122 if (!yaffs_init_tmp_buffers(dev))
5126 dev->gc_cleanup_list = NULL;
5130 dev->param.n_caches > 0) {
5133 int cache_bytes = dev->param.n_caches * sizeof(struct yaffs_cache);
5135 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
5136 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
5138 dev->cache = YMALLOC(cache_bytes);
5140 buf = (u8 *) dev->cache;
5143 memset(dev->cache, 0, cache_bytes);
5145 for (i = 0; i < dev->param.n_caches && buf; i++) {
5146 dev->cache[i].object = NULL;
5147 dev->cache[i].last_use = 0;
5148 dev->cache[i].dirty = 0;
5149 dev->cache[i].data = buf = YMALLOC_DMA(dev->param.total_bytes_per_chunk);
5154 dev->cache_last_use = 0;
5157 dev->cache_hits = 0;
5160 dev->gc_cleanup_list = YMALLOC(dev->param.chunks_per_block * sizeof(u32));
5161 if (!dev->gc_cleanup_list)
5165 if (dev->param.is_yaffs2)
5166 dev->param.use_header_file_size = 1;
5168 if (!init_failed && !yaffs_init_blocks(dev))
5171 yaffs_init_tnodes_and_objs(dev);
5173 if (!init_failed && !yaffs_create_initial_dir(dev))
5178 /* Now scan the flash. */
5179 if (dev->param.is_yaffs2) {
5180 if (yaffs2_checkpt_restore(dev)) {
5181 yaffs_check_obj_details_loaded(dev->root_dir);
5182 T(YAFFS_TRACE_ALWAYS,
5183 (TSTR("yaffs: restored from checkpoint" TENDSTR)));
5186 /* Clean up the mess caused by an aborted checkpoint load
5187 * and scan backwards.
5189 yaffs_deinit_blocks(dev);
5191 yaffs_deinit_tnodes_and_objs(dev);
5193 dev->n_erased_blocks = 0;
5194 dev->n_free_chunks = 0;
5195 dev->alloc_block = -1;
5196 dev->alloc_page = -1;
5197 dev->n_deleted_files = 0;
5198 dev->n_unlinked_files = 0;
5199 dev->n_bg_deletions = 0;
5201 if (!init_failed && !yaffs_init_blocks(dev))
5204 yaffs_init_tnodes_and_objs(dev);
5206 if (!init_failed && !yaffs_create_initial_dir(dev))
5209 if (!init_failed && !yaffs2_scan_backwards(dev))
5212 } else if (!yaffs1_scan(dev))
5215 yaffs_strip_deleted_objs(dev);
5216 yaffs_fix_hanging_objs(dev);
5217 if(dev->param.empty_lost_n_found)
5218 yaffs_empty_l_n_f(dev);
5222 /* Clean up the mess */
5223 T(YAFFS_TRACE_TRACING,
5224 (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR)));
5226 yaffs_deinitialise(dev);
5230 /* Zero out stats */
5231 dev->n_page_reads = 0;
5232 dev->n_page_writes = 0;
5233 dev->n_erasures = 0;
5234 dev->n_gc_copies = 0;
5235 dev->n_retired_writes = 0;
5237 dev->n_retired_blocks = 0;
5239 yaffs_verify_free_chunks(dev);
5240 yaffs_verify_blocks(dev);
5242 /* Clean up any aborted checkpoint data */
5243 if(!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5244 yaffs2_checkpt_invalidate(dev);
5246 T(YAFFS_TRACE_TRACING,
5247 (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR)));
5252 void yaffs_deinitialise(struct yaffs_dev *dev)
5254 if (dev->is_mounted) {
5257 yaffs_deinit_blocks(dev);
5258 yaffs_deinit_tnodes_and_objs(dev);
5259 if (dev->param.n_caches > 0 &&
5262 for (i = 0; i < dev->param.n_caches; i++) {
5263 if (dev->cache[i].data)
5264 YFREE(dev->cache[i].data);
5265 dev->cache[i].data = NULL;
5272 YFREE(dev->gc_cleanup_list);
5274 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5275 YFREE(dev->temp_buffer[i].buffer);
5277 dev->is_mounted = 0;
5279 if (dev->param.deinitialise_flash_fn)
5280 dev->param.deinitialise_flash_fn(dev);
5284 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5289 struct yaffs_block_info *blk;
5291 blk = dev->block_info;
5292 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5293 switch (blk->block_state) {
5294 case YAFFS_BLOCK_STATE_EMPTY:
5295 case YAFFS_BLOCK_STATE_ALLOCATING:
5296 case YAFFS_BLOCK_STATE_COLLECTING:
5297 case YAFFS_BLOCK_STATE_FULL:
5299 (dev->param.chunks_per_block - blk->pages_in_use +
5300 blk->soft_del_pages);
5311 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5313 /* This is what we report to the outside world */
5317 int blocks_for_checkpt;
5320 n_free = dev->n_free_chunks;
5321 n_free += dev->n_deleted_files;
5323 /* Now count the number of dirty chunks in the cache and subtract those */
5325 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5326 if (dev->cache[i].dirty)
5330 n_free -= n_dirty_caches;
5332 n_free -= ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5334 /* Now we figure out how much to reserve for the checkpoint and report that... */
5335 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5337 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);