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(yaffs_dev_t *dev, int flash_block);
46 static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk,
48 static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
50 const yaffs_ext_tags *tags);
51 static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
52 const yaffs_ext_tags *tags);
54 /* Other local prototypes */
55 static void yaffs_update_parent(yaffs_obj_t *obj);
56 static int yaffs_unlink_obj(yaffs_obj_t *obj);
57 static int yaffs_obj_cache_dirty(yaffs_obj_t *obj);
59 static int yaffs_write_new_chunk(yaffs_dev_t *dev,
65 static yaffs_obj_t *yaffs_new_obj(yaffs_dev_t *dev, int number,
69 static int yaffs_apply_xattrib_mod(yaffs_obj_t *obj, char *buffer, yaffs_xattr_mod *xmod);
71 static void yaffs_remove_obj_from_dir(yaffs_obj_t *obj);
72 static int yaffs_check_structures(void);
73 static int yaffs_generic_obj_del(yaffs_obj_t *in);
75 static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
78 static int yaffs_unlink_worker(yaffs_obj_t *obj);
80 static int yaffs_tags_match(const yaffs_ext_tags *tags, int obj_id,
83 static int yaffs_alloc_chunk(yaffs_dev_t *dev, int use_reserver,
84 yaffs_block_info_t **block_ptr);
86 static void yaffs_check_obj_details_loaded(yaffs_obj_t *in);
88 static void yaffs_invalidate_whole_cache(yaffs_obj_t *in);
89 static void yaffs_invalidate_chunk_cache(yaffs_obj_t *object, int chunk_id);
91 static int yaffs_find_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
92 yaffs_ext_tags *tags);
94 static int yaffs_verify_chunk_written(yaffs_dev_t *dev,
97 yaffs_ext_tags *tags);
100 static void yaffs_load_name_from_oh(yaffs_dev_t *dev,YCHAR *name, const YCHAR *oh_name, int buff_size);
101 static void yaffs_load_oh_from_name(yaffs_dev_t *dev,YCHAR *oh_name, const YCHAR *name);
104 /* Function to calculate chunk and offset */
106 static void yaffs_addr_to_chunk(yaffs_dev_t *dev, loff_t addr, int *chunk_out,
112 chunk = (__u32)(addr >> dev->chunk_shift);
114 if (dev->chunk_div == 1) {
115 /* easy power of 2 case */
116 offset = (__u32)(addr & dev->chunk_mask);
118 /* Non power-of-2 case */
122 chunk /= dev->chunk_div;
124 chunk_base = ((loff_t)chunk) * dev->data_bytes_per_chunk;
125 offset = (__u32)(addr - chunk_base);
129 *offset_out = offset;
132 /* Function to return the number of shifts for a power of 2 greater than or
133 * equal to the given number
134 * Note we don't try to cater for all possible numbers and this does not have to
135 * be hellishly efficient.
138 static __u32 calc_shifts_ceiling(__u32 x)
143 shifts = extra_bits = 0;
158 /* Function to return the number of shifts to get a 1 in bit 0
161 static __u32 calc_shifts(__u32 x)
181 * Temporary buffer manipulations.
184 static int yaffs_init_tmp_buffers(yaffs_dev_t *dev)
187 __u8 *buf = (__u8 *)1;
189 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
191 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
192 dev->temp_buffer[i].line = 0; /* not in use */
193 dev->temp_buffer[i].buffer = buf =
194 YMALLOC_DMA(dev->param.total_bytes_per_chunk);
197 return buf ? YAFFS_OK : YAFFS_FAIL;
200 __u8 *yaffs_get_temp_buffer(yaffs_dev_t *dev, int line_no)
205 if (dev->temp_in_use > dev->max_temp)
206 dev->max_temp = dev->temp_in_use;
208 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
209 if (dev->temp_buffer[i].line == 0) {
210 dev->temp_buffer[i].line = line_no;
211 if ((i + 1) > dev->max_temp) {
212 dev->max_temp = i + 1;
213 for (j = 0; j <= i; j++)
214 dev->temp_buffer[j].max_line =
215 dev->temp_buffer[j].line;
218 return dev->temp_buffer[i].buffer;
222 T(YAFFS_TRACE_BUFFERS,
223 (TSTR("Out of temp buffers at line %d, other held by lines:"),
225 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
226 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->temp_buffer[i].line));
228 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
231 * If we got here then we have to allocate an unmanaged one
235 dev->unmanaged_buffer_allocs++;
236 return YMALLOC(dev->data_bytes_per_chunk);
240 void yaffs_release_temp_buffer(yaffs_dev_t *dev, __u8 *buffer,
247 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
248 if (dev->temp_buffer[i].buffer == buffer) {
249 dev->temp_buffer[i].line = 0;
255 /* assume it is an unmanaged one. */
256 T(YAFFS_TRACE_BUFFERS,
257 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
260 dev->unmanaged_buffer_deallocs++;
266 * Determine if we have a managed buffer.
268 int yaffs_is_managed_tmp_buffer(yaffs_dev_t *dev, const __u8 *buffer)
272 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
273 if (dev->temp_buffer[i].buffer == buffer)
277 for (i = 0; i < dev->param.n_caches; i++) {
278 if (dev->cache[i].data == buffer)
282 if (buffer == dev->checkpt_buffer)
285 T(YAFFS_TRACE_ALWAYS,
286 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
298 * Simple hash function. Needs to have a reasonable spread
301 static Y_INLINE int yaffs_hash_fn(int n)
304 return n % YAFFS_NOBJECT_BUCKETS;
308 * Access functions to useful fake objects.
309 * Note that root might have a presence in NAND if permissions are set.
312 yaffs_obj_t *yaffs_root(yaffs_dev_t *dev)
314 return dev->root_dir;
317 yaffs_obj_t *yaffs_lost_n_found(yaffs_dev_t *dev)
319 return dev->lost_n_found;
324 * Erased NAND checking functions
327 int yaffs_check_ff(__u8 *buffer, int n_bytes)
329 /* Horrible, slow implementation */
338 static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
341 int retval = YAFFS_OK;
342 __u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
346 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
348 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
351 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
352 T(YAFFS_TRACE_NANDACCESS,
353 (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
357 yaffs_release_temp_buffer(dev, data, __LINE__);
364 static int yaffs_verify_chunk_written(yaffs_dev_t *dev,
367 yaffs_ext_tags *tags)
369 int retval = YAFFS_OK;
370 yaffs_ext_tags temp_tags;
371 __u8 *buffer = yaffs_get_temp_buffer(dev,__LINE__);
374 result = yaffs_rd_chunk_tags_nand(dev,nand_chunk,buffer,&temp_tags);
375 if(memcmp(buffer,data,dev->data_bytes_per_chunk) ||
376 temp_tags.obj_id != tags->obj_id ||
377 temp_tags.chunk_id != tags->chunk_id ||
378 temp_tags.n_bytes != tags->n_bytes)
381 yaffs_release_temp_buffer(dev, buffer, __LINE__);
386 static int yaffs_write_new_chunk(struct yaffs_dev_s *dev,
388 yaffs_ext_tags *tags,
395 yaffs2_checkpt_invalidate(dev);
398 yaffs_block_info_t *bi = 0;
401 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
407 /* First check this chunk is erased, if it needs
408 * checking. The checking policy (unless forced
409 * always on) is as follows:
411 * Check the first page we try to write in a block.
412 * If the check passes then we don't need to check any
413 * more. If the check fails, we check again...
414 * If the block has been erased, we don't need to check.
416 * However, if the block has been prioritised for gc,
417 * then we think there might be something odd about
418 * this block and stop using it.
420 * Rationale: We should only ever see chunks that have
421 * not been erased if there was a partially written
422 * chunk due to power loss. This checking policy should
423 * catch that case with very few checks and thus save a
424 * lot of checks that are most likely not needed.
427 * If an erase check fails or the write fails we skip the
431 /* let's give it a try */
434 if(dev->param.always_check_erased)
435 bi->skip_erased_check = 0;
437 if (!bi->skip_erased_check) {
438 erased_ok = yaffs_check_chunk_erased(dev, chunk);
439 if (erased_ok != YAFFS_OK) {
441 (TSTR("**>> yaffs chunk %d was not erased"
444 /* If not erased, delete this one,
445 * skip rest of block and
446 * try another chunk */
447 yaffs_chunk_del(dev,chunk,1,__LINE__);
448 yaffs_skip_rest_of_block(dev);
453 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk,
456 if(!bi->skip_erased_check)
457 write_ok = yaffs_verify_chunk_written(dev, chunk, data, tags);
459 if (write_ok != YAFFS_OK) {
460 /* Clean up aborted write, skip to next block and
461 * try another chunk */
462 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
466 bi->skip_erased_check = 1;
468 /* Copy the data into the robustification buffer */
469 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
471 } while (write_ok != YAFFS_OK &&
472 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
479 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
482 dev->n_retired_writes += (attempts - 1);
491 * Block retiring for handling a broken block.
494 static void yaffs_retire_block(yaffs_dev_t *dev, int flash_block)
496 yaffs_block_info_t *bi = yaffs_get_block_info(dev, flash_block);
498 yaffs2_checkpt_invalidate(dev);
500 yaffs2_clear_oldest_dirty_seq(dev,bi);
502 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
503 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
504 T(YAFFS_TRACE_ALWAYS, (TSTR(
505 "yaffs: Failed to mark bad and erase block %d"
506 TENDSTR), flash_block));
509 int chunk_id = flash_block * dev->param.chunks_per_block;
511 __u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
513 memset(buffer, 0xff, dev->data_bytes_per_chunk);
514 yaffs_init_tags(&tags);
515 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
516 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
517 dev->chunk_offset, buffer, &tags) != YAFFS_OK)
518 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
519 TCONT("write bad block marker to block %d")
520 TENDSTR), flash_block));
522 yaffs_release_temp_buffer(dev, buffer, __LINE__);
526 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
527 bi->gc_prioritise = 0;
528 bi->needs_retiring = 0;
530 dev->n_retired_blocks++;
534 * Functions for robustisizing TODO
538 static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
540 const yaffs_ext_tags *tags)
543 nand_chunk=nand_chunk;
548 static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
549 const yaffs_ext_tags *tags)
552 nand_chunk=nand_chunk;
556 void yaffs_handle_chunk_error(yaffs_dev_t *dev, yaffs_block_info_t *bi)
558 if (!bi->gc_prioritise) {
559 bi->gc_prioritise = 1;
560 dev->has_pending_prioritised_gc = 1;
561 bi->chunk_error_strikes++;
563 if (bi->chunk_error_strikes > 3) {
564 bi->needs_retiring = 1; /* Too many stikes, so retire this */
565 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
571 static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk,
574 int flash_block = nand_chunk / dev->param.chunks_per_block;
575 yaffs_block_info_t *bi = yaffs_get_block_info(dev, flash_block);
577 yaffs_handle_chunk_error(dev, bi);
580 /* Was an actual write failure, so mark the block for retirement */
581 bi->needs_retiring = 1;
582 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
583 (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
586 /* Delete the chunk */
587 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
588 yaffs_skip_rest_of_block(dev);
592 /*---------------- Name handling functions ------------*/
594 static __u16 yaffs_calc_name_sum(const YCHAR *name)
599 const YUCHAR *bname = (const YUCHAR *) name;
601 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
603 /* 0x1f mask is case insensitive */
604 sum += ((*bname) & 0x1f) * i;
612 void yaffs_set_obj_name(yaffs_obj_t *obj, const YCHAR *name)
614 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
615 memset(obj->short_name, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
616 if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
617 yaffs_strcpy(obj->short_name, name);
619 obj->short_name[0] = _Y('\0');
621 obj->sum = yaffs_calc_name_sum(name);
624 void yaffs_set_obj_name_from_oh(yaffs_obj_t *obj, const yaffs_obj_header *oh)
626 #ifdef CONFIG_YAFFS_AUTO_UNICODE
627 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH+1];
628 memset(tmp_name,0,sizeof(tmp_name));
629 yaffs_load_name_from_oh(obj->my_dev,tmp_name,oh->name,YAFFS_MAX_NAME_LENGTH+1);
630 yaffs_set_obj_name(obj,tmp_name);
632 yaffs_set_obj_name(obj,oh->name);
636 /*-------------------- TNODES -------------------
638 * List of spare tnodes
639 * The list is hooked together using the first pointer
644 yaffs_tnode_t *yaffs_get_tnode(yaffs_dev_t *dev)
646 yaffs_tnode_t *tn = yaffs_alloc_raw_tnode(dev);
648 memset(tn, 0, dev->tnode_size);
652 dev->checkpoint_blocks_required = 0; /* force recalculation*/
657 /* FreeTnode frees up a tnode and puts it back on the free list */
658 static void yaffs_free_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
660 yaffs_free_raw_tnode(dev,tn);
662 dev->checkpoint_blocks_required = 0; /* force recalculation*/
665 static void yaffs_deinit_tnodes_and_objs(yaffs_dev_t *dev)
667 yaffs_deinit_raw_tnodes_and_objs(dev);
673 void yaffs_load_tnode_0(yaffs_dev_t *dev, yaffs_tnode_t *tn, unsigned pos,
676 __u32 *map = (__u32 *)tn;
682 pos &= YAFFS_TNODES_LEVEL0_MASK;
683 val >>= dev->chunk_grp_bits;
685 bit_in_map = pos * dev->tnode_width;
686 word_in_map = bit_in_map / 32;
687 bit_in_word = bit_in_map & (32 - 1);
689 mask = dev->tnode_mask << bit_in_word;
691 map[word_in_map] &= ~mask;
692 map[word_in_map] |= (mask & (val << bit_in_word));
694 if (dev->tnode_width > (32 - bit_in_word)) {
695 bit_in_word = (32 - bit_in_word);
697 mask = dev->tnode_mask >> (/*dev->tnode_width -*/ bit_in_word);
698 map[word_in_map] &= ~mask;
699 map[word_in_map] |= (mask & (val >> bit_in_word));
703 __u32 yaffs_get_group_base(yaffs_dev_t *dev, yaffs_tnode_t *tn,
706 __u32 *map = (__u32 *)tn;
712 pos &= YAFFS_TNODES_LEVEL0_MASK;
714 bit_in_map = pos * dev->tnode_width;
715 word_in_map = bit_in_map / 32;
716 bit_in_word = bit_in_map & (32 - 1);
718 val = map[word_in_map] >> bit_in_word;
720 if (dev->tnode_width > (32 - bit_in_word)) {
721 bit_in_word = (32 - bit_in_word);
723 val |= (map[word_in_map] << bit_in_word);
726 val &= dev->tnode_mask;
727 val <<= dev->chunk_grp_bits;
732 /* ------------------- End of individual tnode manipulation -----------------*/
734 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
735 * The look up tree is represented by the top tnode and the number of top_level
736 * in the tree. 0 means only the level 0 tnode is in the tree.
739 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
740 yaffs_tnode_t *yaffs_find_tnode_0(yaffs_dev_t *dev,
741 yaffs_file_s *file_struct,
744 yaffs_tnode_t *tn = file_struct->top;
747 int level = file_struct->top_level;
751 /* Check sane level and chunk Id */
752 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
755 if (chunk_id > YAFFS_MAX_CHUNK_ID)
758 /* First check we're tall enough (ie enough top_level) */
760 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
763 i >>= YAFFS_TNODES_INTERNAL_BITS;
767 if (required_depth > file_struct->top_level)
768 return NULL; /* Not tall enough, so we can't find it */
770 /* Traverse down to level 0 */
771 while (level > 0 && tn) {
772 tn = tn->internal[(chunk_id >>
773 (YAFFS_TNODES_LEVEL0_BITS +
775 YAFFS_TNODES_INTERNAL_BITS)) &
776 YAFFS_TNODES_INTERNAL_MASK];
783 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
784 * This happens in two steps:
785 * 1. If the tree isn't tall enough, then make it taller.
786 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
788 * Used when modifying the tree.
790 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
791 * be plugged into the ttree.
794 yaffs_tnode_t *yaffs_add_find_tnode_0(yaffs_dev_t *dev,
795 yaffs_file_s *file_struct,
797 yaffs_tnode_t *passed_tn)
807 /* Check sane level and page Id */
808 if (file_struct->top_level < 0 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
811 if (chunk_id > YAFFS_MAX_CHUNK_ID)
814 /* First check we're tall enough (ie enough top_level) */
816 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
819 x >>= YAFFS_TNODES_INTERNAL_BITS;
824 if (required_depth > file_struct->top_level) {
825 /* Not tall enough, gotta make the tree taller */
826 for (i = file_struct->top_level; i < required_depth; i++) {
828 tn = yaffs_get_tnode(dev);
831 tn->internal[0] = file_struct->top;
832 file_struct->top = tn;
833 file_struct->top_level++;
836 (TSTR("yaffs: no more tnodes" TENDSTR)));
842 /* Traverse down to level 0, adding anything we need */
844 l = file_struct->top_level;
845 tn = file_struct->top;
848 while (l > 0 && tn) {
850 (YAFFS_TNODES_LEVEL0_BITS +
851 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
852 YAFFS_TNODES_INTERNAL_MASK;
855 if ((l > 1) && !tn->internal[x]) {
856 /* Add missing non-level-zero tnode */
857 tn->internal[x] = yaffs_get_tnode(dev);
861 /* Looking from level 1 at level 0 */
863 /* If we already have one, then release it.*/
865 yaffs_free_tnode(dev, tn->internal[x]);
866 tn->internal[x] = passed_tn;
868 } else if (!tn->internal[x]) {
869 /* Don't have one, none passed in */
870 tn->internal[x] = yaffs_get_tnode(dev);
876 tn = tn->internal[x];
882 memcpy(tn, passed_tn, (dev->tnode_width * YAFFS_NTNODES_LEVEL0)/8);
883 yaffs_free_tnode(dev, passed_tn);
890 static int yaffs_find_chunk_in_group(yaffs_dev_t *dev, int the_chunk,
891 yaffs_ext_tags *tags, int obj_id,
896 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
897 if (yaffs_check_chunk_bit(dev, the_chunk / dev->param.chunks_per_block,
898 the_chunk % dev->param.chunks_per_block)) {
900 if(dev->chunk_grp_size == 1)
903 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
905 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
917 static void yaffs_soft_del_chunk(yaffs_dev_t *dev, int chunk)
919 yaffs_block_info_t *the_block;
922 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
924 block_no = chunk / dev->param.chunks_per_block;
925 the_block = yaffs_get_block_info(dev, block_no);
927 the_block->soft_del_pages++;
928 dev->n_free_chunks++;
929 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
933 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
934 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
936 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
939 static int yaffs_soft_del_worker(yaffs_obj_t *in, yaffs_tnode_t *tn,
940 __u32 level, int chunk_offset)
945 yaffs_dev_t *dev = in->my_dev;
950 for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
952 if (tn->internal[i]) {
954 yaffs_soft_del_worker(in,
960 YAFFS_TNODES_INTERNAL_BITS)
963 yaffs_free_tnode(dev,
966 tn->internal[i] = NULL;
968 /* Hoosterman... how could this happen? */
972 return (all_done) ? 1 : 0;
973 } else if (level == 0) {
975 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
976 the_chunk = yaffs_get_group_base(dev, tn, i);
978 /* Note this does not find the real chunk, only the chunk group.
979 * We make an assumption that a chunk group is not larger than
982 yaffs_soft_del_chunk(dev, the_chunk);
983 yaffs_load_tnode_0(dev, tn, i, 0);
997 static void yaffs_soft_del_file(yaffs_obj_t *obj)
1000 obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1001 if (obj->n_data_chunks <= 0) {
1002 /* Empty file with no duplicate object headers, just delete it immediately */
1003 yaffs_free_tnode(obj->my_dev,
1004 obj->variant.file_variant.top);
1005 obj->variant.file_variant.top = NULL;
1006 T(YAFFS_TRACE_TRACING,
1007 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1009 yaffs_generic_obj_del(obj);
1011 yaffs_soft_del_worker(obj,
1012 obj->variant.file_variant.top,
1013 obj->variant.file_variant.
1020 /* Pruning removes any part of the file structure tree that is beyond the
1021 * bounds of the file (ie that does not point to chunks).
1023 * A file should only get pruned when its size is reduced.
1025 * Before pruning, the chunks must be pulled from the tree and the
1026 * level 0 tnode entries must be zeroed out.
1027 * Could also use this for file deletion, but that's probably better handled
1028 * by a special case.
1030 * This function is recursive. For levels > 0 the function is called again on
1031 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1032 * If there is no data in a subtree then it is pruned.
1035 static yaffs_tnode_t *yaffs_prune_worker(yaffs_dev_t *dev, yaffs_tnode_t *tn,
1036 __u32 level, int del0)
1045 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1046 if (tn->internal[i]) {
1048 yaffs_prune_worker(dev, tn->internal[i],
1050 (i == 0) ? del0 : 1);
1053 if (tn->internal[i])
1057 int tnode_size_u32 = dev->tnode_size/sizeof(__u32);
1058 __u32 *map = (__u32 *)tn;
1060 for(i = 0; !has_data && i < tnode_size_u32; i++){
1066 if (has_data == 0 && del0) {
1067 /* Free and return NULL */
1069 yaffs_free_tnode(dev, tn);
1079 static int yaffs_prune_tree(yaffs_dev_t *dev,
1080 yaffs_file_s *file_struct)
1087 if (file_struct->top_level > 0) {
1089 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1091 /* Now we have a tree with all the non-zero branches NULL but the height
1092 * is the same as it was.
1093 * Let's see if we can trim internal tnodes to shorten the tree.
1094 * We can do this if only the 0th element in the tnode is in use
1095 * (ie all the non-zero are NULL)
1098 while (file_struct->top_level && !done) {
1099 tn = file_struct->top;
1102 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1103 if (tn->internal[i])
1108 file_struct->top = tn->internal[0];
1109 file_struct->top_level--;
1110 yaffs_free_tnode(dev, tn);
1120 /*-------------------- End of File Structure functions.-------------------*/
1123 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1124 static yaffs_obj_t *yaffs_alloc_empty_obj(yaffs_dev_t *dev)
1126 yaffs_obj_t *obj = yaffs_alloc_raw_obj(dev);
1131 /* Now sweeten it up... */
1133 memset(obj, 0, sizeof(yaffs_obj_t));
1134 obj->being_created = 1;
1138 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1139 YINIT_LIST_HEAD(&(obj->hard_links));
1140 YINIT_LIST_HEAD(&(obj->hash_link));
1141 YINIT_LIST_HEAD(&obj->siblings);
1144 /* Now make the directory sane */
1145 if (dev->root_dir) {
1146 obj->parent = dev->root_dir;
1147 ylist_add(&(obj->siblings), &dev->root_dir->variant.dir_variant.children);
1150 /* Add it to the lost and found directory.
1151 * NB Can't put root or lost-n-found in lost-n-found so
1152 * check if lost-n-found exists first
1154 if (dev->lost_n_found)
1155 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1157 obj->being_created = 0;
1160 dev->checkpoint_blocks_required = 0; /* force recalculation*/
1165 static yaffs_obj_t *yaffs_create_fake_dir(yaffs_dev_t *dev, int number,
1170 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1172 obj->fake = 1; /* it is fake so it might have no NAND presence... */
1173 obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
1174 obj->unlink_allowed = 0; /* ... or unlink it */
1177 obj->yst_mode = mode;
1179 obj->hdr_chunk = 0; /* Not a valid chunk. */
1186 static void yaffs_unhash_obj(yaffs_obj_t *obj)
1189 yaffs_dev_t *dev = obj->my_dev;
1191 /* If it is still linked into the bucket list, free from the list */
1192 if (!ylist_empty(&obj->hash_link)) {
1193 ylist_del_init(&obj->hash_link);
1194 bucket = yaffs_hash_fn(obj->obj_id);
1195 dev->obj_bucket[bucket].count--;
1199 /* FreeObject frees up a Object and puts it back on the free list */
1200 static void yaffs_free_obj(yaffs_obj_t *obj)
1202 yaffs_dev_t *dev = obj->my_dev;
1204 T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), obj, obj->my_inode));
1210 if (!ylist_empty(&obj->siblings))
1214 if (obj->my_inode) {
1215 /* We're still hooked up to a cached inode.
1216 * Don't delete now, but mark for later deletion
1218 obj->defered_free = 1;
1222 yaffs_unhash_obj(obj);
1224 yaffs_free_raw_obj(dev,obj);
1226 dev->checkpoint_blocks_required = 0; /* force recalculation*/
1230 void yaffs_handle_defered_free(yaffs_obj_t *obj)
1232 if (obj->defered_free)
1233 yaffs_free_obj(obj);
1236 static void yaffs_init_tnodes_and_objs(yaffs_dev_t *dev)
1243 yaffs_init_raw_tnodes_and_objs(dev);
1245 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1246 YINIT_LIST_HEAD(&dev->obj_bucket[i].list);
1247 dev->obj_bucket[i].count = 0;
1251 static int yaffs_find_nice_bucket(yaffs_dev_t *dev)
1255 int lowest = 999999;
1258 /* Search for the shortest list or one that
1262 for (i = 0; i < 10 && lowest > 4; i++) {
1263 dev->bucket_finder++;
1264 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1265 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1266 lowest = dev->obj_bucket[dev->bucket_finder].count;
1267 l = dev->bucket_finder;
1275 static int yaffs_new_obj_id(yaffs_dev_t *dev)
1277 int bucket = yaffs_find_nice_bucket(dev);
1279 /* Now find an object value that has not already been taken
1280 * by scanning the list.
1284 struct ylist_head *i;
1286 __u32 n = (__u32) bucket;
1288 /* yaffs_check_obj_hash_sane(); */
1292 n += YAFFS_NOBJECT_BUCKETS;
1293 if (1 || dev->obj_bucket[bucket].count > 0) {
1294 ylist_for_each(i, &dev->obj_bucket[bucket].list) {
1295 /* If there is already one in the list */
1296 if (i && ylist_entry(i, yaffs_obj_t,
1297 hash_link)->obj_id == n) {
1307 static void yaffs_hash_obj(yaffs_obj_t *in)
1309 int bucket = yaffs_hash_fn(in->obj_id);
1310 yaffs_dev_t *dev = in->my_dev;
1312 ylist_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1313 dev->obj_bucket[bucket].count++;
1316 yaffs_obj_t *yaffs_find_by_number(yaffs_dev_t *dev, __u32 number)
1318 int bucket = yaffs_hash_fn(number);
1319 struct ylist_head *i;
1322 ylist_for_each(i, &dev->obj_bucket[bucket].list) {
1323 /* Look if it is in the list */
1325 in = ylist_entry(i, yaffs_obj_t, hash_link);
1326 if (in->obj_id == number) {
1328 /* Don't tell the VFS about this one if it is defered free */
1329 if (in->defered_free)
1340 yaffs_obj_t *yaffs_new_obj(yaffs_dev_t *dev, int number,
1341 yaffs_obj_type type)
1343 yaffs_obj_t *the_obj=NULL;
1344 yaffs_tnode_t *tn = NULL;
1347 number = yaffs_new_obj_id(dev);
1349 if (type == YAFFS_OBJECT_TYPE_FILE) {
1350 tn = yaffs_get_tnode(dev);
1355 the_obj = yaffs_alloc_empty_obj(dev);
1358 yaffs_free_tnode(dev,tn);
1365 the_obj->rename_allowed = 1;
1366 the_obj->unlink_allowed = 1;
1367 the_obj->obj_id = number;
1368 yaffs_hash_obj(the_obj);
1369 the_obj->variant_type = type;
1370 #ifdef CONFIG_YAFFS_WINCE
1371 yfsd_win_file_time_now(the_obj->win_atime);
1372 the_obj->win_ctime[0] = the_obj->win_mtime[0] =
1373 the_obj->win_atime[0];
1374 the_obj->win_ctime[1] = the_obj->win_mtime[1] =
1375 the_obj->win_atime[1];
1379 the_obj->yst_atime = the_obj->yst_mtime =
1380 the_obj->yst_ctime = Y_CURRENT_TIME;
1383 case YAFFS_OBJECT_TYPE_FILE:
1384 the_obj->variant.file_variant.file_size = 0;
1385 the_obj->variant.file_variant.scanned_size = 0;
1386 the_obj->variant.file_variant.shrink_size = 0xFFFFFFFF; /* max __u32 */
1387 the_obj->variant.file_variant.top_level = 0;
1388 the_obj->variant.file_variant.top = tn;
1390 case YAFFS_OBJECT_TYPE_DIRECTORY:
1391 YINIT_LIST_HEAD(&the_obj->variant.dir_variant.
1393 YINIT_LIST_HEAD(&the_obj->variant.dir_variant.
1396 case YAFFS_OBJECT_TYPE_SYMLINK:
1397 case YAFFS_OBJECT_TYPE_HARDLINK:
1398 case YAFFS_OBJECT_TYPE_SPECIAL:
1399 /* No action required */
1401 case YAFFS_OBJECT_TYPE_UNKNOWN:
1402 /* todo this should not happen */
1410 yaffs_obj_t *yaffs_find_or_create_by_number(yaffs_dev_t *dev,
1412 yaffs_obj_type type)
1414 yaffs_obj_t *the_obj = NULL;
1417 the_obj = yaffs_find_by_number(dev, number);
1420 the_obj = yaffs_new_obj(dev, number, type);
1427 YCHAR *yaffs_clone_str(const YCHAR *str)
1429 YCHAR *new_str = NULL;
1435 len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
1436 new_str = YMALLOC((len + 1) * sizeof(YCHAR));
1438 yaffs_strncpy(new_str, str,len);
1446 * Mknod (create) a new object.
1447 * equiv_obj only has meaning for a hard link;
1448 * alias_str only has meaning for a symlink.
1449 * rdev only has meaning for devices (a subset of special objects)
1452 static yaffs_obj_t *yaffs_create_obj(yaffs_obj_type type,
1453 yaffs_obj_t *parent,
1458 yaffs_obj_t *equiv_obj,
1459 const YCHAR *alias_str, __u32 rdev)
1464 yaffs_dev_t *dev = parent->my_dev;
1466 /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
1467 if (yaffs_find_by_name(parent, name))
1470 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
1471 str = yaffs_clone_str(alias_str);
1476 in = yaffs_new_obj(dev, -1, type);
1491 in->variant_type = type;
1493 in->yst_mode = mode;
1495 #ifdef CONFIG_YAFFS_WINCE
1496 yfsd_win_file_time_now(in->win_atime);
1497 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1498 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1501 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
1503 in->yst_rdev = rdev;
1507 in->n_data_chunks = 0;
1509 yaffs_set_obj_name(in, name);
1512 yaffs_add_obj_to_dir(parent, in);
1514 in->my_dev = parent->my_dev;
1517 case YAFFS_OBJECT_TYPE_SYMLINK:
1518 in->variant.symlink_variant.alias = str;
1520 case YAFFS_OBJECT_TYPE_HARDLINK:
1521 in->variant.hardlink_variant.equiv_obj =
1523 in->variant.hardlink_variant.equiv_id =
1525 ylist_add(&in->hard_links, &equiv_obj->hard_links);
1527 case YAFFS_OBJECT_TYPE_FILE:
1528 case YAFFS_OBJECT_TYPE_DIRECTORY:
1529 case YAFFS_OBJECT_TYPE_SPECIAL:
1530 case YAFFS_OBJECT_TYPE_UNKNOWN:
1535 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
1536 /* Could not create the object header, fail the creation */
1541 yaffs_update_parent(parent);
1547 yaffs_obj_t *yaffs_create_file(yaffs_obj_t *parent, const YCHAR *name,
1548 __u32 mode, __u32 uid, __u32 gid)
1550 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
1551 uid, gid, NULL, NULL, 0);
1554 yaffs_obj_t *yaffs_create_dir(yaffs_obj_t *parent, const YCHAR *name,
1555 __u32 mode, __u32 uid, __u32 gid)
1557 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
1558 mode, uid, gid, NULL, NULL, 0);
1561 yaffs_obj_t *yaffs_create_special(yaffs_obj_t *parent, const YCHAR *name,
1562 __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1564 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
1565 uid, gid, NULL, NULL, rdev);
1568 yaffs_obj_t *yaffs_create_symlink(yaffs_obj_t *parent, const YCHAR *name,
1569 __u32 mode, __u32 uid, __u32 gid,
1572 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
1573 uid, gid, NULL, alias, 0);
1576 /* yaffs_link_obj returns the object id of the equivalent object.*/
1577 yaffs_obj_t *yaffs_link_obj(yaffs_obj_t *parent, const YCHAR *name,
1578 yaffs_obj_t *equiv_obj)
1580 /* Get the real object in case we were fed a hard link as an equivalent object */
1581 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
1583 if (yaffs_create_obj
1584 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
1585 equiv_obj, NULL, 0)) {
1593 static int yaffs_change_obj_name(yaffs_obj_t *obj, yaffs_obj_t *new_dir,
1594 const YCHAR *new_name, int force, int shadows)
1599 yaffs_obj_t *existing_target;
1601 if (new_dir == NULL)
1602 new_dir = obj->parent; /* use the old directory */
1604 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1605 T(YAFFS_TRACE_ALWAYS,
1607 ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
1612 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1613 if (obj->my_dev->param.is_yaffs2)
1614 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1616 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1617 && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1619 del_op = (new_dir == obj->my_dev->del_dir);
1621 existing_target = yaffs_find_by_name(new_dir, new_name);
1623 /* If the object is a file going into the unlinked directory,
1624 * then it is OK to just stuff it in since duplicate names are allowed.
1625 * else only proceed if the new name does not exist and if we're putting
1626 * it into a directory.
1632 !existing_target) &&
1633 new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1634 yaffs_set_obj_name(obj, new_name);
1637 yaffs_add_obj_to_dir(new_dir, obj);
1642 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1643 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1650 int yaffs_rename_obj(yaffs_obj_t *old_dir, const YCHAR *old_name,
1651 yaffs_obj_t *new_dir, const YCHAR *new_name)
1653 yaffs_obj_t *obj = NULL;
1654 yaffs_obj_t *existing_target = NULL;
1660 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1662 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1665 dev = old_dir->my_dev;
1667 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1668 /* Special case for case insemsitive systems (eg. WinCE).
1669 * While look-up is case insensitive, the name isn't.
1670 * Therefore we might want to change x.txt to X.txt
1672 if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0)
1676 if(yaffs_strnlen(new_name,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
1680 obj = yaffs_find_by_name(old_dir, old_name);
1682 if (obj && obj->rename_allowed) {
1684 /* Now do the handling for an existing target, if there is one */
1686 existing_target = yaffs_find_by_name(new_dir, new_name);
1687 if (existing_target &&
1688 existing_target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1689 !ylist_empty(&existing_target->variant.dir_variant.children)) {
1690 /* There is a target that is a non-empty directory, so we fail */
1691 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
1692 } else if (existing_target && existing_target != obj) {
1693 /* Nuke the target first, using shadowing,
1694 * but only if it isn't the same object.
1696 * Note we must disable gc otherwise it can mess up the shadowing.
1700 yaffs_change_obj_name(obj, new_dir, new_name, force,
1701 existing_target->obj_id);
1702 existing_target->is_shadowed = 1;
1703 yaffs_unlink_obj(existing_target);
1707 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
1709 yaffs_update_parent(old_dir);
1710 if(new_dir != old_dir)
1711 yaffs_update_parent(new_dir);
1718 /*------------------------- Block Management and Page Allocation ----------------*/
1720 static int yaffs_init_blocks(yaffs_dev_t *dev)
1722 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
1724 dev->block_info = NULL;
1725 dev->chunk_bits = NULL;
1727 dev->alloc_block = -1; /* force it to get a new one */
1729 /* If the first allocation strategy fails, thry the alternate one */
1730 dev->block_info = YMALLOC(n_blocks * sizeof(yaffs_block_info_t));
1731 if (!dev->block_info) {
1732 dev->block_info = YMALLOC_ALT(n_blocks * sizeof(yaffs_block_info_t));
1733 dev->block_info_alt = 1;
1735 dev->block_info_alt = 0;
1737 if (dev->block_info) {
1738 /* Set up dynamic blockinfo stuff. */
1739 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */
1740 dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * n_blocks);
1741 if (!dev->chunk_bits) {
1742 dev->chunk_bits = YMALLOC_ALT(dev->chunk_bit_stride * n_blocks);
1743 dev->chunk_bits_alt = 1;
1745 dev->chunk_bits_alt = 0;
1748 if (dev->block_info && dev->chunk_bits) {
1749 memset(dev->block_info, 0, n_blocks * sizeof(yaffs_block_info_t));
1750 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
1757 static void yaffs_deinit_blocks(yaffs_dev_t *dev)
1759 if (dev->block_info_alt && dev->block_info)
1760 YFREE_ALT(dev->block_info);
1761 else if (dev->block_info)
1762 YFREE(dev->block_info);
1764 dev->block_info_alt = 0;
1766 dev->block_info = NULL;
1768 if (dev->chunk_bits_alt && dev->chunk_bits)
1769 YFREE_ALT(dev->chunk_bits);
1770 else if (dev->chunk_bits)
1771 YFREE(dev->chunk_bits);
1772 dev->chunk_bits_alt = 0;
1773 dev->chunk_bits = NULL;
1776 void yaffs_block_became_dirty(yaffs_dev_t *dev, int block_no)
1778 yaffs_block_info_t *bi = yaffs_get_block_info(dev, block_no);
1782 /* If the block is still healthy erase it and mark as clean.
1783 * If the block has had a data failure, then retire it.
1786 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
1787 (TSTR("yaffs_block_became_dirty block %d state %d %s"TENDSTR),
1788 block_no, bi->block_state, (bi->needs_retiring) ? "needs retiring" : ""));
1790 yaffs2_clear_oldest_dirty_seq(dev,bi);
1792 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
1794 /* If this is the block being garbage collected then stop gc'ing this block */
1795 if(block_no == dev->gc_block)
1798 /* If this block is currently the best candidate for gc then drop as a candidate */
1799 if(block_no == dev->gc_dirtiest){
1800 dev->gc_dirtiest = 0;
1801 dev->gc_pages_in_use = 0;
1804 if (!bi->needs_retiring) {
1805 yaffs2_checkpt_invalidate(dev);
1806 erased_ok = yaffs_erase_block(dev, block_no);
1808 dev->n_erase_failures++;
1809 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1810 (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
1815 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) || !yaffs_skip_verification(dev))) {
1817 for (i = 0; i < dev->param.chunks_per_block; i++) {
1818 if (!yaffs_check_chunk_erased
1819 (dev, block_no * dev->param.chunks_per_block + i)) {
1820 T(YAFFS_TRACE_ERROR,
1822 (">>Block %d erasure supposedly OK, but chunk %d not erased"
1823 TENDSTR), block_no, i));
1829 /* Clean it up... */
1830 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
1832 dev->n_erased_blocks++;
1833 bi->pages_in_use = 0;
1834 bi->soft_del_pages = 0;
1835 bi->has_shrink_hdr = 0;
1836 bi->skip_erased_check = 1; /* This is clean, so no need to check */
1837 bi->gc_prioritise = 0;
1838 yaffs_clear_chunk_bits(dev, block_no);
1840 T(YAFFS_TRACE_ERASE,
1841 (TSTR("Erased block %d" TENDSTR), block_no));
1843 dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */
1845 yaffs_retire_block(dev, block_no);
1846 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1847 (TSTR("**>> Block %d retired" TENDSTR), block_no));
1851 static int yaffs_find_alloc_block(yaffs_dev_t *dev)
1855 yaffs_block_info_t *bi;
1857 if (dev->n_erased_blocks < 1) {
1858 /* Hoosterman we've got a problem.
1859 * Can't get space to gc
1861 T(YAFFS_TRACE_ERROR,
1862 (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
1867 /* Find an empty block. */
1869 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
1870 dev->alloc_block_finder++;
1871 if (dev->alloc_block_finder < dev->internal_start_block
1872 || dev->alloc_block_finder > dev->internal_end_block) {
1873 dev->alloc_block_finder = dev->internal_start_block;
1876 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
1878 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1879 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
1881 bi->seq_number = dev->seq_number;
1882 dev->n_erased_blocks--;
1883 T(YAFFS_TRACE_ALLOCATE,
1884 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
1885 dev->alloc_block_finder, dev->seq_number,
1886 dev->n_erased_blocks));
1887 return dev->alloc_block_finder;
1891 T(YAFFS_TRACE_ALWAYS,
1893 ("yaffs tragedy: no more erased blocks, but there should have been %d"
1894 TENDSTR), dev->n_erased_blocks));
1901 * Check if there's space to allocate...
1902 * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()?
1904 int yaffs_check_alloc_available(yaffs_dev_t *dev, int n_chunks)
1906 int reserved_chunks;
1907 int reserved_blocks = dev->param.n_reserved_blocks;
1910 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
1912 reserved_chunks = ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
1914 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
1917 static int yaffs_alloc_chunk(yaffs_dev_t *dev, int use_reserver,
1918 yaffs_block_info_t **block_ptr)
1921 yaffs_block_info_t *bi;
1923 if (dev->alloc_block < 0) {
1924 /* Get next block to allocate off */
1925 dev->alloc_block = yaffs_find_alloc_block(dev);
1926 dev->alloc_page = 0;
1929 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
1930 /* Not enough space to allocate unless we're allowed to use the reserve. */
1934 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
1935 && dev->alloc_page == 0) {
1936 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
1939 /* Next page please.... */
1940 if (dev->alloc_block >= 0) {
1941 bi = yaffs_get_block_info(dev, dev->alloc_block);
1943 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
1946 yaffs_set_chunk_bit(dev, dev->alloc_block,
1951 dev->n_free_chunks--;
1953 /* If the block is full set the state to full */
1954 if (dev->alloc_page >= dev->param.chunks_per_block) {
1955 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1956 dev->alloc_block = -1;
1965 T(YAFFS_TRACE_ERROR,
1966 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
1971 static int yaffs_get_erased_chunks(yaffs_dev_t *dev)
1975 n = dev->n_erased_blocks * dev->param.chunks_per_block;
1977 if (dev->alloc_block > 0)
1978 n += (dev->param.chunks_per_block - dev->alloc_page);
1985 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
1986 * if we don't want to write to it.
1988 void yaffs_skip_rest_of_block(yaffs_dev_t *dev)
1990 if(dev->alloc_block > 0){
1991 yaffs_block_info_t *bi = yaffs_get_block_info(dev, dev->alloc_block);
1992 if(bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING){
1993 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1994 dev->alloc_block = -1;
2000 static int yaffs_gc_block(yaffs_dev_t *dev, int block,
2006 int ret_val = YAFFS_OK;
2008 int is_checkpt_block;
2012 int chunks_before = yaffs_get_erased_chunks(dev);
2015 yaffs_ext_tags tags;
2017 yaffs_block_info_t *bi = yaffs_get_block_info(dev, block);
2019 yaffs_obj_t *object;
2021 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2024 T(YAFFS_TRACE_TRACING,
2025 (TSTR("Collecting block %d, in use %d, shrink %d, whole_block %d" TENDSTR),
2031 /*yaffs_verify_free_chunks(dev); */
2033 if(bi->block_state == YAFFS_BLOCK_STATE_FULL)
2034 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2036 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2038 dev->gc_disable = 1;
2040 if (is_checkpt_block ||
2041 !yaffs_still_some_chunks(dev, block)) {
2042 T(YAFFS_TRACE_TRACING,
2044 ("Collecting block %d that has no chunks in use" TENDSTR),
2046 yaffs_block_became_dirty(dev, block);
2049 __u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2051 yaffs_verify_blk(dev, bi, block);
2053 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2054 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2056 for (/* init already done */;
2057 ret_val == YAFFS_OK &&
2058 dev->gc_chunk < dev->param.chunks_per_block &&
2059 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2061 dev->gc_chunk++, old_chunk++) {
2062 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2064 /* This page is in use and might need to be copied off */
2070 yaffs_init_tags(&tags);
2072 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2076 yaffs_find_by_number(dev,
2079 T(YAFFS_TRACE_GC_DETAIL,
2081 ("Collecting chunk in block %d, %d %d %d " TENDSTR),
2082 dev->gc_chunk, tags.obj_id, tags.chunk_id,
2085 if (object && !yaffs_skip_verification(dev)) {
2086 if (tags.chunk_id == 0)
2087 matching_chunk = object->hdr_chunk;
2088 else if (object->soft_del)
2089 matching_chunk = old_chunk; /* Defeat the test */
2091 matching_chunk = yaffs_find_chunk_in_file(object, tags.chunk_id, NULL);
2093 if (old_chunk != matching_chunk)
2094 T(YAFFS_TRACE_ERROR,
2095 (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
2096 old_chunk, matching_chunk, tags.obj_id, tags.chunk_id));
2101 T(YAFFS_TRACE_ERROR,
2103 ("page %d in gc has no object: %d %d %d "
2104 TENDSTR), old_chunk,
2105 tags.obj_id, tags.chunk_id, tags.n_bytes));
2111 tags.chunk_id != 0) {
2112 /* Data chunk in a soft deleted file, throw it away
2113 * It's a soft deleted data chunk,
2114 * No need to copy this, just forget about it and
2115 * fix up the object.
2118 /* Free chunks already includes softdeleted chunks.
2119 * How ever this chunk is going to soon be really deleted
2120 * which will increment free chunks.
2121 * We have to decrement free chunks so this works out properly.
2123 dev->n_free_chunks--;
2124 bi->soft_del_pages--;
2126 object->n_data_chunks--;
2128 if (object->n_data_chunks <= 0) {
2129 /* remeber to clean up the object */
2130 dev->gc_cleanup_list[dev->n_clean_ups] =
2136 /* Todo object && object->deleted && object->n_data_chunks == 0 */
2137 /* Deleted object header with no data chunks.
2138 * Can be discarded and the file deleted.
2140 object->hdr_chunk = 0;
2141 yaffs_free_tnode(object->my_dev,
2144 object->variant.file_variant.top = NULL;
2145 yaffs_generic_obj_del(object);
2147 } else if (object) {
2148 /* It's either a data chunk in a live file or
2149 * an ObjectHeader, so we're interested in it.
2150 * NB Need to keep the ObjectHeaders of deleted files
2151 * until the whole file has been deleted off
2153 tags.serial_number++;
2157 if (tags.chunk_id == 0) {
2158 /* It is an object Id,
2159 * We need to nuke the shrinkheader flags first
2160 * Also need to clean up shadowing.
2161 * We no longer want the shrink_header flag since its work is done
2162 * and if it is left in place it will mess up scanning.
2165 yaffs_obj_header *oh;
2166 oh = (yaffs_obj_header *)buffer;
2169 tags.extra_is_shrink = 0;
2171 oh->shadows_obj = 0;
2172 oh->inband_shadowed_obj_id = 0;
2173 tags.extra_shadows = 0;
2175 /* Update file size */
2176 if(object->variant_type == YAFFS_OBJECT_TYPE_FILE){
2177 oh->file_size = object->variant.file_variant.file_size;
2178 tags.extra_length = oh->file_size;
2181 yaffs_verify_oh(object, oh, &tags, 1);
2183 yaffs_write_new_chunk(dev,(__u8 *) oh, &tags, 1);
2186 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2188 if (new_chunk < 0) {
2189 ret_val = YAFFS_FAIL;
2192 /* Ok, now fix up the Tnodes etc. */
2194 if (tags.chunk_id == 0) {
2196 object->hdr_chunk = new_chunk;
2197 object->serial = tags.serial_number;
2199 /* It's a data chunk */
2201 ok = yaffs_put_chunk_in_file
2209 if (ret_val == YAFFS_OK)
2210 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2215 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2221 yaffs_verify_collected_blk(dev, bi, block);
2225 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2227 * The gc did not complete. Set block state back to FULL
2228 * because checkpointing does not restore gc.
2230 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2232 /* The gc completed. */
2233 /* Do any required cleanups */
2234 for (i = 0; i < dev->n_clean_ups; i++) {
2235 /* Time to delete the file too */
2237 yaffs_find_by_number(dev,
2238 dev->gc_cleanup_list[i]);
2240 yaffs_free_tnode(dev,
2241 object->variant.file_variant.
2243 object->variant.file_variant.top = NULL;
2246 ("yaffs: About to finally delete object %d"
2247 TENDSTR), object->obj_id));
2248 yaffs_generic_obj_del(object);
2249 object->my_dev->n_deleted_files--;
2255 chunks_after = yaffs_get_erased_chunks(dev);
2256 if (chunks_before >= chunks_after) {
2259 ("gc did not increase free chunks before %d after %d"
2260 TENDSTR), chunks_before, chunks_after));
2264 dev->n_clean_ups = 0;
2267 dev->gc_disable = 0;
2273 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2274 * for garbage collection.
2277 static unsigned yaffs_find_gc_block(yaffs_dev_t *dev,
2283 unsigned selected = 0;
2284 int prioritised = 0;
2285 int prioritised_exist = 0;
2286 yaffs_block_info_t *bi;
2289 /* First let's see if we need to grab a prioritised block */
2290 if (dev->has_pending_prioritised_gc && !aggressive) {
2291 dev->gc_dirtiest = 0;
2292 bi = dev->block_info;
2293 for (i = dev->internal_start_block;
2294 i <= dev->internal_end_block && !selected;
2297 if (bi->gc_prioritise) {
2298 prioritised_exist = 1;
2299 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2300 yaffs_block_ok_for_gc(dev, bi)) {
2309 * If there is a prioritised block and none was selected then
2310 * this happened because there is at least one old dirty block gumming
2311 * up the works. Let's gc the oldest dirty block.
2314 if(prioritised_exist &&
2316 dev->oldest_dirty_block > 0)
2317 selected = dev->oldest_dirty_block;
2319 if (!prioritised_exist) /* None found, so we can clear this */
2320 dev->has_pending_prioritised_gc = 0;
2323 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2325 * else (we're doing a leasurely gc), then we only bother to do this if the
2326 * block has only a few pages in use.
2331 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2333 threshold = dev->param.chunks_per_block;
2334 iterations = n_blocks;
2339 max_threshold = dev->param.chunks_per_block/2;
2341 max_threshold = dev->param.chunks_per_block/8;
2343 if(max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2344 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2346 threshold = background ?
2347 (dev->gc_not_done + 2) * 2 : 0;
2348 if(threshold <YAFFS_GC_PASSIVE_THRESHOLD)
2349 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2350 if(threshold > max_threshold)
2351 threshold = max_threshold;
2353 iterations = n_blocks / 16 + 1;
2354 if (iterations > 100)
2360 (dev->gc_dirtiest < 1 ||
2361 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2363 dev->gc_block_finder++;
2364 if (dev->gc_block_finder < dev->internal_start_block ||
2365 dev->gc_block_finder > dev->internal_end_block)
2366 dev->gc_block_finder = dev->internal_start_block;
2368 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2370 pages_used = bi->pages_in_use - bi->soft_del_pages;
2372 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2373 pages_used < dev->param.chunks_per_block &&
2374 (dev->gc_dirtiest < 1 || pages_used < dev->gc_pages_in_use) &&
2375 yaffs_block_ok_for_gc(dev, bi)) {
2376 dev->gc_dirtiest = dev->gc_block_finder;
2377 dev->gc_pages_in_use = pages_used;
2381 if(dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2382 selected = dev->gc_dirtiest;
2386 * If nothing has been selected for a while, try selecting the oldest dirty
2387 * because that's gumming up the works.
2390 if(!selected && dev->param.is_yaffs2 &&
2391 dev->gc_not_done >= ( background ? 10 : 20)){
2392 yaffs2_find_oldest_dirty_seq(dev);
2393 if(dev->oldest_dirty_block > 0) {
2394 selected = dev->oldest_dirty_block;
2395 dev->gc_dirtiest = selected;
2396 dev->oldest_dirty_gc_count++;
2397 bi = yaffs_get_block_info(dev, selected);
2398 dev->gc_pages_in_use = bi->pages_in_use - bi->soft_del_pages;
2400 dev->gc_not_done = 0;
2405 (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR),
2407 dev->param.chunks_per_block - dev->gc_pages_in_use,
2414 dev->gc_dirtiest = 0;
2415 dev->gc_pages_in_use = 0;
2416 dev->gc_not_done = 0;
2417 if(dev->refresh_skip > 0)
2418 dev->refresh_skip--;
2422 (TSTR("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" TENDSTR),
2423 dev->gc_block_finder, dev->gc_not_done,
2425 dev->gc_dirtiest, dev->gc_pages_in_use,
2426 dev->oldest_dirty_block,
2427 background ? " bg" : ""));
2433 /* New garbage collector
2434 * If we're very low on erased blocks then we do aggressive garbage collection
2435 * otherwise we do "leasurely" garbage collection.
2436 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2437 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2439 * The idea is to help clear out space in a more spread-out manner.
2440 * Dunno if it really does anything useful.
2442 static int yaffs_check_gc(yaffs_dev_t *dev, int background)
2445 int gc_ok = YAFFS_OK;
2449 int checkpt_block_adjust;
2451 if(dev->param.gc_control &&
2452 (dev->param.gc_control(dev) & 1) == 0)
2455 if (dev->gc_disable) {
2456 /* Bail out so we don't get recursive gc */
2460 /* This loop should pass the first time.
2461 * We'll only see looping here if the collection does not increase space.
2467 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2469 min_erased = dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2470 erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2472 /* If we need a block soon then do aggressive gc.*/
2473 if (dev->n_erased_blocks < min_erased)
2476 if(!background && erased_chunks > (dev->n_free_chunks / 4))
2479 if(dev->gc_skip > 20)
2481 if(erased_chunks < dev->n_free_chunks/2 ||
2493 /* If we don't already have a block being gc'd then see if we should start another */
2495 if (dev->gc_block < 1 && !aggressive) {
2496 dev->gc_block = yaffs2_find_refresh_block(dev);
2500 if (dev->gc_block < 1) {
2501 dev->gc_block = yaffs_find_gc_block(dev, aggressive, background);
2506 if (dev->gc_block > 0) {
2509 dev->passive_gc_count++;
2513 ("yaffs: GC n_erased_blocks %d aggressive %d" TENDSTR),
2514 dev->n_erased_blocks, aggressive));
2516 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2519 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) && dev->gc_block > 0) {
2522 ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d"
2523 TENDSTR), dev->n_erased_blocks, max_tries, dev->gc_block));
2525 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2526 (dev->gc_block > 0) &&
2529 return aggressive ? gc_ok : YAFFS_OK;
2534 * Garbage collects. Intended to be called from a background thread.
2535 * Returns non-zero if at least half the free chunks are erased.
2537 int yaffs_bg_gc(yaffs_dev_t *dev, unsigned urgency)
2539 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2541 T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR),urgency));
2543 yaffs_check_gc(dev, 1);
2544 return erased_chunks > dev->n_free_chunks/2;
2547 /*------------------------- TAGS --------------------------------*/
2549 static int yaffs_tags_match(const yaffs_ext_tags *tags, int obj_id,
2552 return (tags->chunk_id == chunk_obj &&
2553 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
2558 /*-------------------- Data file manipulation -----------------*/
2560 static int yaffs_find_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
2561 yaffs_ext_tags *tags)
2563 /*Get the Tnode, then get the level 0 offset chunk offset */
2566 yaffs_ext_tags local_tags;
2569 yaffs_dev_t *dev = in->my_dev;
2572 /* Passed a NULL, so use our own tags space */
2576 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2579 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2582 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2588 static int yaffs_find_del_file_chunk(yaffs_obj_t *in, int inode_chunk,
2589 yaffs_ext_tags *tags)
2591 /* Get the Tnode, then get the level 0 offset chunk offset */
2594 yaffs_ext_tags local_tags;
2596 yaffs_dev_t *dev = in->my_dev;
2600 /* Passed a NULL, so use our own tags space */
2604 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2608 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2611 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2614 /* Delete the entry in the filestructure (if found) */
2616 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
2623 int yaffs_put_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
2624 int nand_chunk, int in_scan)
2626 /* NB in_scan is zero unless scanning.
2627 * For forward scanning, in_scan is > 0;
2628 * for backward scanning in_scan is < 0
2630 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
2634 yaffs_dev_t *dev = in->my_dev;
2636 yaffs_ext_tags existing_tags;
2637 yaffs_ext_tags new_tags;
2638 unsigned existing_serial, new_serial;
2640 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
2641 /* Just ignore an attempt at putting a chunk into a non-file during scanning
2642 * If it is not during Scanning then something went wrong!
2645 T(YAFFS_TRACE_ERROR,
2647 ("yaffs tragedy:attempt to put data chunk into a non-file"
2652 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
2656 tn = yaffs_add_find_tnode_0(dev,
2657 &in->variant.file_variant,
2664 /* Dummy insert, bail now */
2667 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
2670 /* If we're scanning then we need to test for duplicates
2671 * NB This does not need to be efficient since it should only ever
2672 * happen when the power fails during a write, then only one
2673 * chunk should ever be affected.
2675 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
2676 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
2679 if (existing_cunk > 0) {
2680 /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
2681 * thus we have to do a FindChunkInFile to get the real chunk id.
2683 * We have a duplicate now we need to decide which one to use:
2685 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
2686 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
2687 * YAFFS1: Get both sets of tags and compare serial numbers.
2691 /* Only do this for forward scanning */
2692 yaffs_rd_chunk_tags_nand(dev,
2696 /* Do a proper find */
2698 yaffs_find_chunk_in_file(in, inode_chunk,
2702 if (existing_cunk <= 0) {
2703 /*Hoosterman - how did this happen? */
2705 T(YAFFS_TRACE_ERROR,
2707 ("yaffs tragedy: existing chunk < 0 in scan"
2712 /* NB The deleted flags should be false, otherwise the chunks will
2713 * not be loaded during a scan
2717 new_serial = new_tags.serial_number;
2718 existing_serial = existing_tags.serial_number;
2721 if ((in_scan > 0) &&
2722 (existing_cunk <= 0 ||
2723 ((existing_serial + 1) & 3) == new_serial)) {
2724 /* Forward scanning.
2726 * Delete the old one and drop through to update the tnode
2728 yaffs_chunk_del(dev, existing_cunk, 1,
2731 /* Backward scanning or we want to use the existing one
2733 * Delete the new one and return early so that the tnode isn't changed
2735 yaffs_chunk_del(dev, nand_chunk, 1,
2743 if (existing_cunk == 0)
2744 in->n_data_chunks++;
2746 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
2751 static int yaffs_rd_data_obj(yaffs_obj_t *in, int inode_chunk,
2754 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2756 if (nand_chunk >= 0)
2757 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2760 T(YAFFS_TRACE_NANDACCESS,
2761 (TSTR("Chunk %d not found zero instead" TENDSTR),
2763 /* get sane (zero) data if you read a hole */
2764 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2770 void yaffs_chunk_del(yaffs_dev_t *dev, int chunk_id, int mark_flash, int lyn)
2774 yaffs_ext_tags tags;
2775 yaffs_block_info_t *bi;
2781 block = chunk_id / dev->param.chunks_per_block;
2782 page = chunk_id % dev->param.chunks_per_block;
2785 if (!yaffs_check_chunk_bit(dev, block, page))
2786 T(YAFFS_TRACE_VERIFY,
2787 (TSTR("Deleting invalid chunk %d"TENDSTR),
2790 bi = yaffs_get_block_info(dev, block);
2792 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2794 T(YAFFS_TRACE_DELETION,
2795 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
2797 if (!dev->param.is_yaffs2 && mark_flash &&
2798 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2800 yaffs_init_tags(&tags);
2802 tags.is_deleted = 1;
2804 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2805 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2807 dev->n_unmarked_deletions++;
2810 /* Pull out of the management area.
2811 * If the whole block became dirty, this will kick off an erasure.
2813 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2814 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2815 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
2816 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2817 dev->n_free_chunks++;
2819 yaffs_clear_chunk_bit(dev, block, page);
2823 if (bi->pages_in_use == 0 &&
2824 !bi->has_shrink_hdr &&
2825 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2826 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
2827 yaffs_block_became_dirty(dev, block);
2834 static int yaffs_wr_data_obj(yaffs_obj_t *in, int inode_chunk,
2835 const __u8 *buffer, int n_bytes,
2838 /* Find old chunk Need to do this to get serial number
2839 * Write new one and patch into tree.
2840 * Invalidate old tags.
2844 yaffs_ext_tags prev_tags;
2847 yaffs_ext_tags new_tags;
2849 yaffs_dev_t *dev = in->my_dev;
2851 yaffs_check_gc(dev,0);
2853 /* Get the previous chunk at this location in the file if it exists.
2854 * If it does not exist then put a zero into the tree. This creates
2855 * the tnode now, rather than later when it is harder to clean up.
2857 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
2858 if(prev_chunk_id < 1 &&
2859 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
2862 /* Set up new tags */
2863 yaffs_init_tags(&new_tags);
2865 new_tags.chunk_id = inode_chunk;
2866 new_tags.obj_id = in->obj_id;
2867 new_tags.serial_number =
2868 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
2869 new_tags.n_bytes = n_bytes;
2871 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
2872 T(YAFFS_TRACE_ERROR,
2873 (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), n_bytes));
2879 yaffs_write_new_chunk(dev, buffer, &new_tags,
2882 if (new_chunk_id > 0) {
2883 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
2885 if (prev_chunk_id > 0)
2886 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
2888 yaffs_verify_file_sane(in);
2890 return new_chunk_id;
2894 /* UpdateObjectHeader updates the header on NAND for an object.
2895 * If name is not NULL, then that new name is used.
2897 int yaffs_update_oh(yaffs_obj_t *in, const YCHAR *name, int force,
2898 int is_shrink, int shadows, yaffs_xattr_mod *xmod)
2901 yaffs_block_info_t *bi;
2903 yaffs_dev_t *dev = in->my_dev;
2910 yaffs_ext_tags new_tags;
2911 yaffs_ext_tags old_tags;
2912 const YCHAR *alias = NULL;
2914 __u8 *buffer = NULL;
2915 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
2917 yaffs_obj_header *oh = NULL;
2919 yaffs_strcpy(old_name, _Y("silly old name"));
2923 in == dev->root_dir || /* The root_dir should also be saved */
2926 yaffs_check_gc(dev,0);
2927 yaffs_check_obj_details_loaded(in);
2929 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
2930 oh = (yaffs_obj_header *) buffer;
2932 prev_chunk_id = in->hdr_chunk;
2934 if (prev_chunk_id > 0) {
2935 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
2938 yaffs_verify_oh(in, oh, &old_tags, 0);
2940 memcpy(old_name, oh->name, sizeof(oh->name));
2941 memset(buffer, 0xFF, sizeof(yaffs_obj_header));
2943 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
2945 oh->type = in->variant_type;
2946 oh->yst_mode = in->yst_mode;
2947 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
2949 #ifdef CONFIG_YAFFS_WINCE
2950 oh->win_atime[0] = in->win_atime[0];
2951 oh->win_ctime[0] = in->win_ctime[0];
2952 oh->win_mtime[0] = in->win_mtime[0];
2953 oh->win_atime[1] = in->win_atime[1];
2954 oh->win_ctime[1] = in->win_ctime[1];
2955 oh->win_mtime[1] = in->win_mtime[1];
2957 oh->yst_uid = in->yst_uid;
2958 oh->yst_gid = in->yst_gid;
2959 oh->yst_atime = in->yst_atime;
2960 oh->yst_mtime = in->yst_mtime;
2961 oh->yst_ctime = in->yst_ctime;
2962 oh->yst_rdev = in->yst_rdev;
2965 oh->parent_obj_id = in->parent->obj_id;
2967 oh->parent_obj_id = 0;
2969 if (name && *name) {
2970 memset(oh->name, 0, sizeof(oh->name));
2971 yaffs_load_oh_from_name(dev,oh->name,name);
2972 } else if (prev_chunk_id > 0)
2973 memcpy(oh->name, old_name, sizeof(oh->name));
2975 memset(oh->name, 0, sizeof(oh->name));
2977 oh->is_shrink = is_shrink;
2979 switch (in->variant_type) {
2980 case YAFFS_OBJECT_TYPE_UNKNOWN:
2981 /* Should not happen */
2983 case YAFFS_OBJECT_TYPE_FILE:
2985 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
2986 || oh->parent_obj_id ==
2987 YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
2988 file_variant.file_size;
2990 case YAFFS_OBJECT_TYPE_HARDLINK:
2992 in->variant.hardlink_variant.equiv_id;
2994 case YAFFS_OBJECT_TYPE_SPECIAL:
2997 case YAFFS_OBJECT_TYPE_DIRECTORY:
3000 case YAFFS_OBJECT_TYPE_SYMLINK:
3001 alias = in->variant.symlink_variant.alias;
3003 alias = _Y("no alias");
3004 yaffs_strncpy(oh->alias,
3006 YAFFS_MAX_ALIAS_LENGTH);
3007 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3011 /* process any xattrib modifications */
3013 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3017 yaffs_init_tags(&new_tags);
3019 new_tags.chunk_id = 0;
3020 new_tags.obj_id = in->obj_id;
3021 new_tags.serial_number = in->serial;
3023 /* Add extra info for file header */
3025 new_tags.extra_available = 1;
3026 new_tags.extra_parent_id = oh->parent_obj_id;
3027 new_tags.extra_length = oh->file_size;
3028 new_tags.extra_is_shrink = oh->is_shrink;
3029 new_tags.extra_equiv_id = oh->equiv_id;
3030 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3031 new_tags.extra_obj_type = in->variant_type;
3033 yaffs_verify_oh(in, oh, &new_tags, 1);
3035 /* Create new chunk in NAND */
3037 yaffs_write_new_chunk(dev, buffer, &new_tags,
3038 (prev_chunk_id > 0) ? 1 : 0);
3040 if (new_chunk_id >= 0) {
3042 in->hdr_chunk = new_chunk_id;
3044 if (prev_chunk_id > 0) {
3045 yaffs_chunk_del(dev, prev_chunk_id, 1,
3049 if (!yaffs_obj_cache_dirty(in))
3052 /* If this was a shrink, then mark the block that the chunk lives on */
3054 bi = yaffs_get_block_info(in->my_dev,
3055 new_chunk_id / in->my_dev->param.chunks_per_block);
3056 bi->has_shrink_hdr = 1;
3061 ret_val = new_chunk_id;
3066 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3071 /*------------------------ Short Operations Cache ----------------------------------------
3072 * In many situations where there is no high level buffering (eg WinCE) a lot of
3073 * reads might be short sequential reads, and a lot of writes may be short
3074 * sequential writes. eg. scanning/writing a jpeg file.
3075 * In these cases, a short read/write cache can provide a huge perfomance benefit
3076 * with dumb-as-a-rock code.
3077 * In Linux, the page cache provides read buffering aand the short op cache provides write
3080 * There are a limited number (~10) of cache chunks per device so that we don't
3081 * need a very intelligent search.
3084 static int yaffs_obj_cache_dirty(yaffs_obj_t *obj)
3086 yaffs_dev_t *dev = obj->my_dev;
3088 yaffs_cache_t *cache;
3089 int n_caches = obj->my_dev->param.n_caches;
3091 for (i = 0; i < n_caches; i++) {
3092 cache = &dev->cache[i];
3093 if (cache->object == obj &&
3102 static void yaffs_flush_file_cache(yaffs_obj_t *obj)
3104 yaffs_dev_t *dev = obj->my_dev;
3105 int lowest = -99; /* Stop compiler whining. */
3107 yaffs_cache_t *cache;
3108 int chunk_written = 0;
3109 int n_caches = obj->my_dev->param.n_caches;
3115 /* Find the dirty cache for this object with the lowest chunk id. */
3116 for (i = 0; i < n_caches; i++) {
3117 if (dev->cache[i].object == obj &&
3118 dev->cache[i].dirty) {
3120 || dev->cache[i].chunk_id <
3122 cache = &dev->cache[i];
3123 lowest = cache->chunk_id;
3128 if (cache && !cache->locked) {
3129 /* Write it out and free it up */
3132 yaffs_wr_data_obj(cache->object,
3138 cache->object = NULL;
3141 } while (cache && chunk_written > 0);
3144 /* Hoosterman, disk full while writing cache out. */
3145 T(YAFFS_TRACE_ERROR,
3146 (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3153 /*yaffs_flush_whole_cache(dev)
3158 void yaffs_flush_whole_cache(yaffs_dev_t *dev)
3161 int n_caches = dev->param.n_caches;
3164 /* Find a dirty object in the cache and flush it...
3165 * until there are no further dirty objects.
3169 for (i = 0; i < n_caches && !obj; i++) {
3170 if (dev->cache[i].object &&
3171 dev->cache[i].dirty)
3172 obj = dev->cache[i].object;
3176 yaffs_flush_file_cache(obj);
3183 /* Grab us a cache chunk for use.
3184 * First look for an empty one.
3185 * Then look for the least recently used non-dirty one.
3186 * Then look for the least recently used dirty one...., flush and look again.
3188 static yaffs_cache_t *yaffs_grab_chunk_worker(yaffs_dev_t *dev)
3192 if (dev->param.n_caches > 0) {
3193 for (i = 0; i < dev->param.n_caches; i++) {
3194 if (!dev->cache[i].object)
3195 return &dev->cache[i];
3202 static yaffs_cache_t *yaffs_grab_chunk_cache(yaffs_dev_t *dev)
3204 yaffs_cache_t *cache;
3205 yaffs_obj_t *the_obj;
3210 if (dev->param.n_caches > 0) {
3211 /* Try find a non-dirty one... */
3213 cache = yaffs_grab_chunk_worker(dev);
3216 /* They were all dirty, find the last recently used object and flush
3217 * its cache, then find again.
3218 * NB what's here is not very accurate, we actually flush the object
3219 * the last recently used page.
3222 /* With locking we can't assume we can use entry zero */
3229 for (i = 0; i < dev->param.n_caches; i++) {
3230 if (dev->cache[i].object &&
3231 !dev->cache[i].locked &&
3232 (dev->cache[i].last_use < usage || !cache)) {
3233 usage = dev->cache[i].last_use;
3234 the_obj = dev->cache[i].object;
3235 cache = &dev->cache[i];
3240 if (!cache || cache->dirty) {
3241 /* Flush and try again */
3242 yaffs_flush_file_cache(the_obj);
3243 cache = yaffs_grab_chunk_worker(dev);
3253 /* Find a cached chunk */
3254 static yaffs_cache_t *yaffs_find_chunk_cache(const yaffs_obj_t *obj,
3257 yaffs_dev_t *dev = obj->my_dev;
3259 if (dev->param.n_caches > 0) {
3260 for (i = 0; i < dev->param.n_caches; i++) {
3261 if (dev->cache[i].object == obj &&
3262 dev->cache[i].chunk_id == chunk_id) {
3265 return &dev->cache[i];
3272 /* Mark the chunk for the least recently used algorithym */
3273 static void yaffs_use_cache(yaffs_dev_t *dev, yaffs_cache_t *cache,
3277 if (dev->param.n_caches > 0) {
3278 if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
3279 /* Reset the cache usages */
3281 for (i = 1; i < dev->param.n_caches; i++)
3282 dev->cache[i].last_use = 0;
3284 dev->cache_last_use = 0;
3287 dev->cache_last_use++;
3289 cache->last_use = dev->cache_last_use;
3296 /* Invalidate a single cache page.
3297 * Do this when a whole page gets written,
3298 * ie the short cache for this page is no longer valid.
3300 static void yaffs_invalidate_chunk_cache(yaffs_obj_t *object, int chunk_id)
3302 if (object->my_dev->param.n_caches > 0) {
3303 yaffs_cache_t *cache = yaffs_find_chunk_cache(object, chunk_id);
3306 cache->object = NULL;
3310 /* Invalidate all the cache pages associated with this object
3311 * Do this whenever ther file is deleted or resized.
3313 static void yaffs_invalidate_whole_cache(yaffs_obj_t *in)
3316 yaffs_dev_t *dev = in->my_dev;
3318 if (dev->param.n_caches > 0) {
3319 /* Invalidate it. */
3320 for (i = 0; i < dev->param.n_caches; i++) {
3321 if (dev->cache[i].object == in)
3322 dev->cache[i].object = NULL;
3328 /*--------------------- File read/write ------------------------
3329 * Read and write have very similar structures.
3330 * In general the read/write has three parts to it
3331 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3332 * Some complete chunks
3333 * An incomplete chunk to end off with
3335 * Curve-balls: the first chunk might also be the last chunk.
3338 int yaffs_file_rd(yaffs_obj_t *in, __u8 *buffer, loff_t offset,
3347 yaffs_cache_t *cache;
3354 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3355 /* start = offset % dev->data_bytes_per_chunk; */
3356 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3359 /* OK now check for the curveball where the start and end are in
3362 if ((start + n) < dev->data_bytes_per_chunk)
3365 n_copy = dev->data_bytes_per_chunk - start;
3367 cache = yaffs_find_chunk_cache(in, chunk);
3369 /* If the chunk is already in the cache or it is less than a whole chunk
3370 * or we're using inband tags then use the cache (if there is caching)
3371 * else bypass the cache.
3373 if (cache || n_copy != dev->data_bytes_per_chunk || dev->param.inband_tags) {
3374 if (dev->param.n_caches > 0) {
3376 /* If we can't find the data in the cache, then load it up. */
3379 cache = yaffs_grab_chunk_cache(in->my_dev);
3381 cache->chunk_id = chunk;
3384 yaffs_rd_data_obj(in, chunk,
3390 yaffs_use_cache(dev, cache, 0);
3395 memcpy(buffer, &cache->data[start], n_copy);
3399 /* Read into the local buffer then copy..*/
3401 __u8 *local_buffer =
3402 yaffs_get_temp_buffer(dev, __LINE__);
3403 yaffs_rd_data_obj(in, chunk,
3406 memcpy(buffer, &local_buffer[start], n_copy);
3409 yaffs_release_temp_buffer(dev, local_buffer,
3415 /* A full chunk. Read directly into the supplied buffer. */
3416 yaffs_rd_data_obj(in, chunk, buffer);
3430 int yaffs_do_file_wr(yaffs_obj_t *in, const __u8 *buffer, loff_t offset,
3431 int n_bytes, int write_trhrough)
3440 int start_write = offset;
3441 int chunk_written = 0;
3449 while (n > 0 && chunk_written >= 0) {
3450 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3452 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3453 start >= dev->data_bytes_per_chunk) {
3454 T(YAFFS_TRACE_ERROR, (
3455 TSTR("AddrToChunk of offset %d gives chunk %d start %d"
3457 (int)offset, chunk, start));
3459 chunk++; /* File pos to chunk in file offset */
3461 /* OK now check for the curveball where the start and end are in
3465 if ((start + n) < dev->data_bytes_per_chunk) {
3468 /* Now folks, to calculate how many bytes to write back....
3469 * If we're overwriting and not writing to then end of file then
3470 * we need to write back as much as was there before.
3473 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3475 if (chunk_start > in->variant.file_variant.file_size)
3476 n_bytes_read = 0; /* Past end of file */
3478 n_bytes_read = in->variant.file_variant.file_size - chunk_start;
3480 if (n_bytes_read > dev->data_bytes_per_chunk)
3481 n_bytes_read = dev->data_bytes_per_chunk;
3485 (start + n)) ? n_bytes_read : (start + n);
3487 if (n_writeback < 0 || n_writeback > dev->data_bytes_per_chunk)
3491 n_copy = dev->data_bytes_per_chunk - start;
3492 n_writeback = dev->data_bytes_per_chunk;
3495 if (n_copy != dev->data_bytes_per_chunk || dev->param.inband_tags) {
3496 /* An incomplete start or end chunk (or maybe both start and end chunk),
3497 * or we're using inband tags, so we want to use the cache buffers.
3499 if (dev->param.n_caches > 0) {
3500 yaffs_cache_t *cache;
3501 /* If we can't find the data in the cache, then load the cache */
3502 cache = yaffs_find_chunk_cache(in, chunk);
3505 && yaffs_check_alloc_available(dev, 1)) {
3506 cache = yaffs_grab_chunk_cache(dev);
3508 cache->chunk_id = chunk;
3511 yaffs_rd_data_obj(in, chunk,
3515 !yaffs_check_alloc_available(dev, 1)) {
3516 /* Drop the cache if it was a read cache item and
3517 * no space check has been made for it.
3523 yaffs_use_cache(dev, cache, 1);
3527 memcpy(&cache->data[start], buffer,
3532 cache->n_bytes = n_writeback;
3534 if (write_trhrough) {
3539 cache->data, cache->n_bytes,
3545 chunk_written = -1; /* fail the write */
3548 /* An incomplete start or end chunk (or maybe both start and end chunk)
3549 * Read into the local buffer then copy, then copy over and write back.
3552 __u8 *local_buffer =
3553 yaffs_get_temp_buffer(dev, __LINE__);
3555 yaffs_rd_data_obj(in, chunk,
3560 memcpy(&local_buffer[start], buffer, n_copy);
3563 yaffs_wr_data_obj(in, chunk,
3568 yaffs_release_temp_buffer(dev, local_buffer,
3574 /* A full chunk. Write directly from the supplied buffer. */
3579 yaffs_wr_data_obj(in, chunk, buffer,
3580 dev->data_bytes_per_chunk,
3583 /* Since we've overwritten the cached data, we better invalidate it. */
3584 yaffs_invalidate_chunk_cache(in, chunk);
3587 if (chunk_written >= 0) {
3596 /* Update file object */
3598 if ((start_write + n_done) > in->variant.file_variant.file_size)
3599 in->variant.file_variant.file_size = (start_write + n_done);
3606 int yaffs_wr_file(yaffs_obj_t *in, const __u8 *buffer, loff_t offset,
3607 int n_bytes, int write_trhrough)
3609 yaffs2_handle_hole(in,offset);
3610 return yaffs_do_file_wr(in,buffer,offset,n_bytes,write_trhrough);
3615 /* ---------------------- File resizing stuff ------------------ */
3617 static void yaffs_prune_chunks(yaffs_obj_t *in, int new_size)
3620 yaffs_dev_t *dev = in->my_dev;
3621 int old_size = in->variant.file_variant.file_size;
3623 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3625 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3626 dev->data_bytes_per_chunk;
3630 /* Delete backwards so that we don't end up with holes if
3631 * power is lost part-way through the operation.
3633 for (i = last_del; i >= start_del; i--) {
3634 /* NB this could be optimised somewhat,
3635 * eg. could retrieve the tags and write them without
3636 * using yaffs_chunk_del
3639 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3642 (dev->internal_start_block * dev->param.chunks_per_block)
3644 ((dev->internal_end_block +
3645 1) * dev->param.chunks_per_block)) {
3646 T(YAFFS_TRACE_ALWAYS,
3647 (TSTR("Found daft chunk_id %d for %d" TENDSTR),
3650 in->n_data_chunks--;
3651 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3659 void yaffs_resize_file_down( yaffs_obj_t *obj, loff_t new_size)
3663 yaffs_dev_t *dev = obj->my_dev;
3665 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3667 yaffs_prune_chunks(obj, new_size);
3669 if (new_partial != 0) {
3670 int last_chunk = 1 + new_full;
3671 __u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3673 /* Got to read and rewrite the last chunk with its new size and zero pad */
3674 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3675 memset(local_buffer + new_partial, 0,
3676 dev->data_bytes_per_chunk - new_partial);
3678 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3681 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3684 obj->variant.file_variant.file_size = new_size;
3686 yaffs_prune_tree(dev, &obj->variant.file_variant);
3690 int yaffs_resize_file(yaffs_obj_t *in, loff_t new_size)
3692 yaffs_dev_t *dev = in->my_dev;
3693 int old_size = in->variant.file_variant.file_size;
3695 yaffs_flush_file_cache(in);
3696 yaffs_invalidate_whole_cache(in);
3698 yaffs_check_gc(dev,0);
3700 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3703 if (new_size == old_size)
3706 if(new_size > old_size){
3707 yaffs2_handle_hole(in,new_size);
3708 in->variant.file_variant.file_size = new_size;
3710 /* new_size < old_size */
3711 yaffs_resize_file_down(in, new_size);
3714 /* Write a new object header to reflect the resize.
3715 * show we've shrunk the file, if need be
3716 * Do this only if the file is not in the deleted directories
3717 * and is not shadowed.
3721 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3722 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3723 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3729 loff_t yaffs_get_file_size(yaffs_obj_t *obj)
3731 YCHAR *alias = NULL;
3732 obj = yaffs_get_equivalent_obj(obj);
3734 switch (obj->variant_type) {
3735 case YAFFS_OBJECT_TYPE_FILE:
3736 return obj->variant.file_variant.file_size;
3737 case YAFFS_OBJECT_TYPE_SYMLINK:
3738 alias = obj->variant.symlink_variant.alias;
3741 return yaffs_strnlen(alias,YAFFS_MAX_ALIAS_LENGTH);
3749 int yaffs_flush_file(yaffs_obj_t *in, int update_time, int data_sync)
3753 yaffs_flush_file_cache(in);
3754 if(data_sync) /* Only sync data */
3758 #ifdef CONFIG_YAFFS_WINCE
3759 yfsd_win_file_time_now(in->win_mtime);
3762 in->yst_mtime = Y_CURRENT_TIME;
3767 ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3768 0) ? YAFFS_OK : YAFFS_FAIL;
3778 static int yaffs_generic_obj_del(yaffs_obj_t *in)
3781 /* First off, invalidate the file's data in the cache, without flushing. */
3782 yaffs_invalidate_whole_cache(in);
3784 if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
3785 /* Move to the unlinked directory so we have a record that it was deleted. */
3786 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0, 0);
3790 yaffs_remove_obj_from_dir(in);
3791 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
3799 /* yaffs_del_file deletes the whole file data
3800 * and the inode associated with the file.
3801 * It does not delete the links associated with the file.
3803 static int yaffs_unlink_file_if_needed(yaffs_obj_t *in)
3808 yaffs_dev_t *dev = in->my_dev;
3815 yaffs_change_obj_name(in, in->my_dev->del_dir,
3816 _Y("deleted"), 0, 0);
3817 T(YAFFS_TRACE_TRACING,
3818 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
3821 in->my_dev->n_deleted_files++;
3822 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3823 yaffs_resize_file(in, 0);
3824 yaffs_soft_del_file(in);
3827 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3828 _Y("unlinked"), 0, 0);
3835 int yaffs_del_file(yaffs_obj_t *in)
3837 int ret_val = YAFFS_OK;
3838 int deleted; /* Need to cache value on stack if in is freed */
3839 yaffs_dev_t *dev = in->my_dev;
3841 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3842 yaffs_resize_file(in, 0);
3844 if (in->n_data_chunks > 0) {
3845 /* Use soft deletion if there is data in the file.
3846 * That won't be the case if it has been resized to zero.
3849 ret_val = yaffs_unlink_file_if_needed(in);
3851 deleted = in->deleted;
3853 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3856 in->my_dev->n_deleted_files++;
3857 yaffs_soft_del_file(in);
3859 return deleted ? YAFFS_OK : YAFFS_FAIL;
3861 /* The file has no data chunks so we toss it immediately */
3862 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3863 in->variant.file_variant.top = NULL;
3864 yaffs_generic_obj_del(in);
3870 static int yaffs_is_non_empty_dir(yaffs_obj_t *obj)
3872 return (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3873 !(ylist_empty(&obj->variant.dir_variant.children));
3876 static int yaffs_del_dir(yaffs_obj_t *obj)
3878 /* First check that the directory is empty. */
3879 if (yaffs_is_non_empty_dir(obj))
3882 return yaffs_generic_obj_del(obj);
3885 static int yaffs_del_symlink(yaffs_obj_t *in)
3887 if(in->variant.symlink_variant.alias)
3888 YFREE(in->variant.symlink_variant.alias);
3889 in->variant.symlink_variant.alias=NULL;
3891 return yaffs_generic_obj_del(in);
3894 static int yaffs_del_link(yaffs_obj_t *in)
3896 /* remove this hardlink from the list assocaited with the equivalent
3899 ylist_del_init(&in->hard_links);
3900 return yaffs_generic_obj_del(in);
3903 int yaffs_del_obj(yaffs_obj_t *obj)
3906 switch (obj->variant_type) {
3907 case YAFFS_OBJECT_TYPE_FILE:
3908 ret_val = yaffs_del_file(obj);
3910 case YAFFS_OBJECT_TYPE_DIRECTORY:
3911 if(!ylist_empty(&obj->variant.dir_variant.dirty)){
3912 T(YAFFS_TRACE_BACKGROUND, (TSTR("Remove object %d from dirty directories" TENDSTR),obj->obj_id));
3913 ylist_del_init(&obj->variant.dir_variant.dirty);
3915 return yaffs_del_dir(obj);
3917 case YAFFS_OBJECT_TYPE_SYMLINK:
3918 ret_val = yaffs_del_symlink(obj);
3920 case YAFFS_OBJECT_TYPE_HARDLINK:
3921 ret_val = yaffs_del_link(obj);
3923 case YAFFS_OBJECT_TYPE_SPECIAL:
3924 ret_val = yaffs_generic_obj_del(obj);
3926 case YAFFS_OBJECT_TYPE_UNKNOWN:
3928 break; /* should not happen. */
3934 static int yaffs_unlink_worker(yaffs_obj_t *obj)
3943 yaffs_update_parent(obj->parent);
3945 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3946 return yaffs_del_link(obj);
3947 } else if (!ylist_empty(&obj->hard_links)) {
3948 /* Curve ball: We're unlinking an object that has a hardlink.
3950 * This problem arises because we are not strictly following
3951 * The Linux link/inode model.
3953 * We can't really delete the object.
3954 * Instead, we do the following:
3955 * - Select a hardlink.
3956 * - Unhook it from the hard links
3957 * - Move it from its parent directory (so that the rename can work)
3958 * - Rename the object to the hardlink's name.
3959 * - Delete the hardlink
3963 yaffs_obj_t *parent;
3965 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3967 hl = ylist_entry(obj->hard_links.next, yaffs_obj_t, hard_links);
3969 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3970 parent = hl->parent;
3972 ylist_del_init(&hl->hard_links);
3974 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3976 ret_val = yaffs_change_obj_name(obj,parent, name, 0, 0);
3978 if (ret_val == YAFFS_OK)
3979 ret_val = yaffs_generic_obj_del(hl);
3983 } else if (del_now) {
3984 switch (obj->variant_type) {
3985 case YAFFS_OBJECT_TYPE_FILE:
3986 return yaffs_del_file(obj);
3988 case YAFFS_OBJECT_TYPE_DIRECTORY:
3989 ylist_del_init(&obj->variant.dir_variant.dirty);
3990 return yaffs_del_dir(obj);
3992 case YAFFS_OBJECT_TYPE_SYMLINK:
3993 return yaffs_del_symlink(obj);
3995 case YAFFS_OBJECT_TYPE_SPECIAL:
3996 return yaffs_generic_obj_del(obj);
3998 case YAFFS_OBJECT_TYPE_HARDLINK:
3999 case YAFFS_OBJECT_TYPE_UNKNOWN:
4003 } else if(yaffs_is_non_empty_dir(obj))
4006 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4007 _Y("unlinked"), 0, 0);
4011 static int yaffs_unlink_obj(yaffs_obj_t *obj)
4014 if (obj && obj->unlink_allowed)
4015 return yaffs_unlink_worker(obj);
4020 int yaffs_unlinker(yaffs_obj_t *dir, const YCHAR *name)
4024 obj = yaffs_find_by_name(dir, name);
4025 return yaffs_unlink_obj(obj);
4028 /*----------------------- Initialisation Scanning ---------------------- */
4030 void yaffs_handle_shadowed_obj(yaffs_dev_t *dev, int obj_id,
4031 int backward_scanning)
4035 if (!backward_scanning) {
4036 /* Handle YAFFS1 forward scanning case
4037 * For YAFFS1 we always do the deletion
4041 /* Handle YAFFS2 case (backward scanning)
4042 * If the shadowed object exists then ignore.
4044 obj = yaffs_find_by_number(dev, obj_id);
4049 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4050 * We put it in unlinked dir to be cleaned up after the scanning
4053 yaffs_find_or_create_by_number(dev, obj_id,
4054 YAFFS_OBJECT_TYPE_FILE);
4057 obj->is_shadowed = 1;
4058 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4059 obj->variant.file_variant.shrink_size = 0;
4060 obj->valid = 1; /* So that we don't read any other info for this file */
4065 void yaffs_link_fixup(yaffs_dev_t *dev, yaffs_obj_t *hard_list)
4072 hard_list = (yaffs_obj_t *) (hard_list->hard_links.next);
4074 in = yaffs_find_by_number(dev,
4075 hl->variant.hardlink_variant.
4079 /* Add the hardlink pointers */
4080 hl->variant.hardlink_variant.equiv_obj = in;
4081 ylist_add(&hl->hard_links, &in->hard_links);
4083 /* Todo Need to report/handle this better.
4084 * Got a problem... hardlink to a non-existant object
4086 hl->variant.hardlink_variant.equiv_obj = NULL;
4087 YINIT_LIST_HEAD(&hl->hard_links);
4094 static void yaffs_strip_deleted_objs(yaffs_dev_t *dev)
4097 * Sort out state of unlinked and deleted objects after scanning.
4099 struct ylist_head *i;
4100 struct ylist_head *n;
4106 /* Soft delete all the unlinked files */
4107 ylist_for_each_safe(i, n,
4108 &dev->unlinked_dir->variant.dir_variant.children) {
4110 l = ylist_entry(i, yaffs_obj_t, siblings);
4115 ylist_for_each_safe(i, n,
4116 &dev->del_dir->variant.dir_variant.children) {
4118 l = ylist_entry(i, yaffs_obj_t, siblings);
4126 * This code iterates through all the objects making sure that they are rooted.
4127 * Any unrooted objects are re-rooted in lost+found.
4128 * An object needs to be in one of:
4129 * - Directly under deleted, unlinked
4130 * - Directly or indirectly under root.
4133 * This code assumes that we don't ever change the current relationships between
4135 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4136 * lost-n-found->parent == root_dir
4138 * This fixes the problem where directories might have inadvertently been deleted
4139 * leaving the object "hanging" without being rooted in the directory tree.
4142 static int yaffs_has_null_parent(yaffs_dev_t *dev, yaffs_obj_t *obj)
4144 return (obj == dev->del_dir ||
4145 obj == dev->unlinked_dir||
4146 obj == dev->root_dir);
4149 static void yaffs_fix_hanging_objs(yaffs_dev_t *dev)
4152 yaffs_obj_t *parent;
4154 struct ylist_head *lh;
4155 struct ylist_head *n;
4162 /* Iterate through the objects in each hash entry,
4163 * looking at each object.
4164 * Make sure it is rooted.
4167 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4168 ylist_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4170 obj = ylist_entry(lh, yaffs_obj_t, hash_link);
4171 parent= obj->parent;
4173 if(yaffs_has_null_parent(dev,obj)){
4174 /* These directories are not hanging */
4177 else if(!parent || parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4179 else if(yaffs_has_null_parent(dev,parent))
4183 * Need to follow the parent chain to see if it is hanging.
4188 while(parent != dev->root_dir &&
4190 parent->parent->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
4192 parent = parent->parent;
4195 if(parent != dev->root_dir)
4200 (TSTR("Hanging object %d moved to lost and found" TENDSTR),
4202 yaffs_add_obj_to_dir(dev->lost_n_found,obj);
4211 * Delete directory contents for cleaning up lost and found.
4213 static void yaffs_del_dir_contents(yaffs_obj_t *dir)
4216 struct ylist_head *lh;
4217 struct ylist_head *n;
4219 if(dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4222 ylist_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4224 obj = ylist_entry(lh, yaffs_obj_t, siblings);
4225 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4226 yaffs_del_dir_contents(obj);
4229 (TSTR("Deleting lost_found object %d" TENDSTR),
4232 /* Need to use UnlinkObject since Delete would not handle
4233 * hardlinked objects correctly.
4235 yaffs_unlink_obj(obj);
4241 static void yaffs_empty_l_n_f(yaffs_dev_t *dev)
4243 yaffs_del_dir_contents(dev->lost_n_found);
4246 static void yaffs_check_obj_details_loaded(yaffs_obj_t *in)
4249 yaffs_obj_header *oh;
4251 yaffs_ext_tags tags;
4253 int alloc_failed = 0;
4260 if (in->lazy_loaded && in->hdr_chunk > 0) {
4261 in->lazy_loaded = 0;
4262 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
4264 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data, &tags);
4265 oh = (yaffs_obj_header *) chunk_data;
4267 in->yst_mode = oh->yst_mode;
4268 #ifdef CONFIG_YAFFS_WINCE
4269 in->win_atime[0] = oh->win_atime[0];
4270 in->win_ctime[0] = oh->win_ctime[0];
4271 in->win_mtime[0] = oh->win_mtime[0];
4272 in->win_atime[1] = oh->win_atime[1];
4273 in->win_ctime[1] = oh->win_ctime[1];
4274 in->win_mtime[1] = oh->win_mtime[1];
4276 in->yst_uid = oh->yst_uid;
4277 in->yst_gid = oh->yst_gid;
4278 in->yst_atime = oh->yst_atime;
4279 in->yst_mtime = oh->yst_mtime;
4280 in->yst_ctime = oh->yst_ctime;
4281 in->yst_rdev = oh->yst_rdev;
4284 yaffs_set_obj_name_from_oh(in, oh);
4286 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4287 in->variant.symlink_variant.alias =
4288 yaffs_clone_str(oh->alias);
4289 if (!in->variant.symlink_variant.alias)
4290 alloc_failed = 1; /* Not returned to caller */
4293 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
4297 /*------------------------------ Directory Functions ----------------------------- */
4300 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
4301 * link (ie. name) is created or deleted in the directory.
4304 * create dir/a : update dir's mtime/ctime
4305 * rm dir/a: update dir's mtime/ctime
4306 * modify dir/a: don't update dir's mtimme/ctime
4308 * This can be handled immediately or defered. Defering helps reduce the number
4309 * of updates when many files in a directory are changed within a brief period.
4311 * If the directory updating is defered then yaffs_update_dirty_dirs must be
4312 * called periodically.
4315 static void yaffs_update_parent(yaffs_obj_t *obj)
4320 #ifndef CONFIG_YAFFS_WINCE
4324 obj->yst_mtime = obj->yst_ctime = Y_CURRENT_TIME;
4325 if(dev->param.defered_dir_update){
4326 struct ylist_head *link = &obj->variant.dir_variant.dirty;
4328 if(ylist_empty(link)){
4329 ylist_add(link,&dev->dirty_dirs);
4330 T(YAFFS_TRACE_BACKGROUND, (TSTR("Added object %d to dirty directories" TENDSTR),obj->obj_id));
4334 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
4338 void yaffs_update_dirty_dirs(yaffs_dev_t *dev)
4340 struct ylist_head *link;
4343 yaffs_obj_variant *o_v;
4345 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
4347 while(!ylist_empty(&dev->dirty_dirs)){
4348 link = dev->dirty_dirs.next;
4349 ylist_del_init(link);
4351 d_s=ylist_entry(link,yaffs_dir_s,dirty);
4352 o_v = ylist_entry(d_s,yaffs_obj_variant,dir_variant);
4353 obj = ylist_entry(o_v,yaffs_obj_t,variant);
4355 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update directory %d" TENDSTR), obj->obj_id));
4358 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
4362 static void yaffs_remove_obj_from_dir(yaffs_obj_t *obj)
4364 yaffs_dev_t *dev = obj->my_dev;
4365 yaffs_obj_t *parent;
4367 yaffs_verify_obj_in_dir(obj);
4368 parent = obj->parent;
4370 yaffs_verify_dir(parent);
4372 if (dev && dev->param.remove_obj_fn)
4373 dev->param.remove_obj_fn(obj);
4376 ylist_del_init(&obj->siblings);
4379 yaffs_verify_dir(parent);
4382 void yaffs_add_obj_to_dir(yaffs_obj_t *directory,
4386 T(YAFFS_TRACE_ALWAYS,
4388 ("tragedy: Trying to add an object to a null pointer directory"
4393 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4394 T(YAFFS_TRACE_ALWAYS,
4396 ("tragedy: Trying to add an object to a non-directory"
4401 if (obj->siblings.prev == NULL) {
4402 /* Not initialised */
4407 yaffs_verify_dir(directory);
4409 yaffs_remove_obj_from_dir(obj);
4413 ylist_add(&obj->siblings, &directory->variant.dir_variant.children);
4414 obj->parent = directory;
4416 if (directory == obj->my_dev->unlinked_dir
4417 || directory == obj->my_dev->del_dir) {
4419 obj->my_dev->n_unlinked_files++;
4420 obj->rename_allowed = 0;
4423 yaffs_verify_dir(directory);
4424 yaffs_verify_obj_in_dir(obj);
4427 yaffs_obj_t *yaffs_find_by_name(yaffs_obj_t *directory,
4432 struct ylist_head *i;
4433 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4441 T(YAFFS_TRACE_ALWAYS,
4443 ("tragedy: yaffs_find_by_name: null pointer directory"
4448 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4449 T(YAFFS_TRACE_ALWAYS,
4451 ("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
4455 sum = yaffs_calc_name_sum(name);
4457 ylist_for_each(i, &directory->variant.dir_variant.children) {
4459 l = ylist_entry(i, yaffs_obj_t, siblings);
4461 if (l->parent != directory)
4464 yaffs_check_obj_details_loaded(l);
4466 /* Special case for lost-n-found */
4467 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4468 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
4470 } else if (yaffs_sum_cmp(l->sum, sum) || l->hdr_chunk <= 0) {
4471 /* LostnFound chunk called Objxxx
4474 yaffs_get_obj_name(l, buffer,
4475 YAFFS_MAX_NAME_LENGTH + 1);
4476 if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4486 /* GetEquivalentObject dereferences any hard links to get to the
4490 yaffs_obj_t *yaffs_get_equivalent_obj(yaffs_obj_t *obj)
4492 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4493 /* We want the object id of the equivalent object, not this one */
4494 obj = obj->variant.hardlink_variant.equiv_obj;
4495 yaffs_check_obj_details_loaded(obj);
4501 * A note or two on object names.
4502 * * If the object name is missing, we then make one up in the form objnnn
4504 * * ASCII names are stored in the object header's name field from byte zero
4505 * * Unicode names are historically stored starting from byte zero.
4507 * Then there are automatic Unicode names...
4508 * The purpose of these is to save names in a way that can be read as
4509 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4510 * system to share files.
4512 * These automatic unicode are stored slightly differently...
4513 * - If the name can fit in the ASCII character space then they are saved as
4514 * ascii names as per above.
4515 * - If the name needs Unicode then the name is saved in Unicode
4516 * starting at oh->name[1].
4519 static void yaffs_fix_null_name(yaffs_obj_t * obj,YCHAR * name, int buffer_size)
4521 /* Create an object name if we could not find one. */
4522 if(yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH) == 0){
4523 YCHAR local_name[20];
4524 YCHAR num_string[20];
4525 YCHAR *x = &num_string[19];
4526 unsigned v = obj->obj_id;
4530 *x = '0' + (v % 10);
4533 /* make up a name */
4534 yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4535 yaffs_strcat(local_name,x);
4536 yaffs_strncpy(name, local_name, buffer_size - 1);
4540 static void yaffs_load_name_from_oh(yaffs_dev_t *dev,YCHAR *name, const YCHAR *oh_name, int buff_size)
4542 #ifdef CONFIG_YAFFS_AUTO_UNICODE
4543 if(dev->param.auto_unicode){
4545 /* It is an ASCII name, so do an ASCII to unicode conversion */
4546 const char *ascii_oh_name = (const char *)oh_name;
4547 int n = buff_size - 1;
4548 while(n > 0 && *ascii_oh_name){
4549 *name = *ascii_oh_name;
4555 yaffs_strncpy(name,oh_name+1, buff_size -1);
4558 yaffs_strncpy(name, oh_name, buff_size - 1);
4562 static void yaffs_load_oh_from_name(yaffs_dev_t *dev, YCHAR *oh_name, const YCHAR *name)
4564 #ifdef CONFIG_YAFFS_AUTO_UNICODE
4569 if(dev->param.auto_unicode){
4574 /* Figure out if the name will fit in ascii character set */
4575 while(is_ascii && *w){
4582 /* It is an ASCII name, so do a unicode to ascii conversion */
4583 char *ascii_oh_name = (char *)oh_name;
4584 int n = YAFFS_MAX_NAME_LENGTH - 1;
4585 while(n > 0 && *name){
4586 *ascii_oh_name= *name;
4592 /* It is a unicode name, so save starting at the second YCHAR */
4594 yaffs_strncpy(oh_name+1,name, YAFFS_MAX_NAME_LENGTH -2);
4599 yaffs_strncpy(oh_name,name, YAFFS_MAX_NAME_LENGTH - 1);
4603 int yaffs_get_obj_name(yaffs_obj_t * obj, YCHAR * name, int buffer_size)
4605 memset(name, 0, buffer_size * sizeof(YCHAR));
4607 yaffs_check_obj_details_loaded(obj);
4609 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4610 yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4612 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4613 else if (obj->short_name[0]) {
4614 yaffs_strcpy(name, obj->short_name);
4617 else if(obj->hdr_chunk > 0) {
4619 __u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4621 yaffs_obj_header *oh = (yaffs_obj_header *) buffer;
4623 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4625 if (obj->hdr_chunk > 0) {
4626 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4627 obj->hdr_chunk, buffer,
4630 yaffs_load_name_from_oh(obj->my_dev,name,oh->name,buffer_size);
4632 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4635 yaffs_fix_null_name(obj,name,buffer_size);
4637 return yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH);
4641 int yaffs_get_obj_length(yaffs_obj_t *obj)
4643 /* Dereference any hard linking */
4644 obj = yaffs_get_equivalent_obj(obj);
4646 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4647 return obj->variant.file_variant.file_size;
4648 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
4649 if(!obj->variant.symlink_variant.alias)
4651 return yaffs_strnlen(obj->variant.symlink_variant.alias,YAFFS_MAX_ALIAS_LENGTH);
4653 /* Only a directory should drop through to here */
4654 return obj->my_dev->data_bytes_per_chunk;
4658 int yaffs_get_obj_link_count(yaffs_obj_t *obj)
4661 struct ylist_head *i;
4664 count++; /* the object itself */
4666 ylist_for_each(i, &obj->hard_links)
4667 count++; /* add the hard links; */
4672 int yaffs_get_obj_inode(yaffs_obj_t *obj)
4674 obj = yaffs_get_equivalent_obj(obj);
4679 unsigned yaffs_get_obj_type(yaffs_obj_t *obj)
4681 obj = yaffs_get_equivalent_obj(obj);
4683 switch (obj->variant_type) {
4684 case YAFFS_OBJECT_TYPE_FILE:
4687 case YAFFS_OBJECT_TYPE_DIRECTORY:
4690 case YAFFS_OBJECT_TYPE_SYMLINK:
4693 case YAFFS_OBJECT_TYPE_HARDLINK:
4696 case YAFFS_OBJECT_TYPE_SPECIAL:
4697 if (S_ISFIFO(obj->yst_mode))
4699 if (S_ISCHR(obj->yst_mode))
4701 if (S_ISBLK(obj->yst_mode))
4703 if (S_ISSOCK(obj->yst_mode))
4711 YCHAR *yaffs_get_symlink_alias(yaffs_obj_t *obj)
4713 obj = yaffs_get_equivalent_obj(obj);
4714 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4715 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4717 return yaffs_clone_str(_Y(""));
4720 #ifndef CONFIG_YAFFS_WINCE
4722 int yaffs_set_attribs(yaffs_obj_t *obj, struct iattr *attr)
4724 unsigned int valid = attr->ia_valid;
4726 if (valid & ATTR_MODE)
4727 obj->yst_mode = attr->ia_mode;
4728 if (valid & ATTR_UID)
4729 obj->yst_uid = attr->ia_uid;
4730 if (valid & ATTR_GID)
4731 obj->yst_gid = attr->ia_gid;
4733 if (valid & ATTR_ATIME)
4734 obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
4735 if (valid & ATTR_CTIME)
4736 obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
4737 if (valid & ATTR_MTIME)
4738 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
4740 if (valid & ATTR_SIZE)
4741 yaffs_resize_file(obj, attr->ia_size);
4743 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
4748 int yaffs_get_attribs(yaffs_obj_t *obj, struct iattr *attr)
4750 unsigned int valid = 0;
4752 attr->ia_mode = obj->yst_mode;
4754 attr->ia_uid = obj->yst_uid;
4756 attr->ia_gid = obj->yst_gid;
4759 Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
4760 valid |= ATTR_ATIME;
4761 Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
4762 valid |= ATTR_CTIME;
4763 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
4764 valid |= ATTR_MTIME;
4766 attr->ia_size = yaffs_get_file_size(obj);
4769 attr->ia_valid = valid;
4777 static int yaffs_do_xattrib_mod(yaffs_obj_t *obj, int set, const YCHAR *name, const void *value, int size, int flags)
4779 yaffs_xattr_mod xmod;
4788 xmod.result = -ENOSPC;
4790 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
4798 static int yaffs_apply_xattrib_mod(yaffs_obj_t *obj, char *buffer, yaffs_xattr_mod *xmod)
4801 int x_offs = sizeof(yaffs_obj_header);
4802 yaffs_dev_t *dev = obj->my_dev;
4803 int x_size = dev->data_bytes_per_chunk - sizeof(yaffs_obj_header);
4805 char * x_buffer = buffer + x_offs;
4808 retval = nval_set(x_buffer, x_size, xmod->name, xmod->data, xmod->size, xmod->flags);
4810 retval = nval_del(x_buffer, x_size, xmod->name);
4812 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4813 obj->xattr_known = 1;
4815 xmod->result = retval;
4820 static int yaffs_do_xattrib_fetch(yaffs_obj_t *obj, const YCHAR *name, void *value, int size)
4822 char *buffer = NULL;
4824 yaffs_ext_tags tags;
4825 yaffs_dev_t *dev = obj->my_dev;
4826 int x_offs = sizeof(yaffs_obj_header);
4827 int x_size = dev->data_bytes_per_chunk - sizeof(yaffs_obj_header);
4833 if(obj->hdr_chunk < 1)
4836 /* If we know that the object has no xattribs then don't do all the
4837 * reading and parsing.
4839 if(obj->xattr_known && !obj->has_xattr){
4846 buffer = (char *) yaffs_get_temp_buffer(dev, __LINE__);
4850 result = yaffs_rd_chunk_tags_nand(dev,obj->hdr_chunk, (__u8 *)buffer, &tags);
4852 if(result != YAFFS_OK)
4855 x_buffer = buffer + x_offs;
4857 if (!obj->xattr_known){
4858 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4859 obj->xattr_known = 1;
4863 retval = nval_get(x_buffer, x_size, name, value, size);
4865 retval = nval_list(x_buffer, x_size, value,size);
4867 yaffs_release_temp_buffer(dev,(__u8 *)buffer,__LINE__);
4871 int yaffs_set_xattrib(yaffs_obj_t *obj, const YCHAR *name, const void * value, int size, int flags)
4873 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
4876 int yaffs_remove_xattrib(yaffs_obj_t *obj, const YCHAR *name)
4878 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
4881 int yaffs_get_xattrib(yaffs_obj_t *obj, const YCHAR *name, void *value, int size)
4883 return yaffs_do_xattrib_fetch(obj, name, value, size);
4886 int yaffs_list_xattrib(yaffs_obj_t *obj, char *buffer, int size)
4888 return yaffs_do_xattrib_fetch(obj, NULL, buffer,size);
4891 /*---------------------------- Initialisation code -------------------------------------- */
4893 static int yaffs_check_dev_fns(const yaffs_dev_t *dev)
4896 /* Common functions, gotta have */
4897 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4900 #ifdef CONFIG_YAFFS_YAFFS2
4902 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4903 if (dev->param.write_chunk_tags_fn &&
4904 dev->param.read_chunk_tags_fn &&
4905 !dev->param.write_chunk_fn &&
4906 !dev->param.read_chunk_fn &&
4907 dev->param.bad_block_fn &&
4908 dev->param.query_block_fn)
4912 /* Can use the "spare" style interface for yaffs1 */
4913 if (!dev->param.is_yaffs2 &&
4914 !dev->param.write_chunk_tags_fn &&
4915 !dev->param.read_chunk_tags_fn &&
4916 dev->param.write_chunk_fn &&
4917 dev->param.read_chunk_fn &&
4918 !dev->param.bad_block_fn &&
4919 !dev->param.query_block_fn)
4926 static int yaffs_create_initial_dir(yaffs_dev_t *dev)
4928 /* Initialise the unlinked, deleted, root and lost and found directories */
4930 dev->lost_n_found = dev->root_dir = NULL;
4931 dev->unlinked_dir = dev->del_dir = NULL;
4934 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4937 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4940 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4941 YAFFS_ROOT_MODE | S_IFDIR);
4943 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4944 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4946 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir && dev->del_dir) {
4947 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4954 int yaffs_guts_initialise(yaffs_dev_t *dev)
4956 int init_failed = 0;
4960 T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR)));
4962 /* Check stuff that must be set */
4965 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
4969 dev->internal_start_block = dev->param.start_block;
4970 dev->internal_end_block = dev->param.end_block;
4971 dev->block_offset = 0;
4972 dev->chunk_offset = 0;
4973 dev->n_free_chunks = 0;
4977 if (dev->param.start_block == 0) {
4978 dev->internal_start_block = dev->param.start_block + 1;
4979 dev->internal_end_block = dev->param.end_block + 1;
4980 dev->block_offset = 1;
4981 dev->chunk_offset = dev->param.chunks_per_block;
4984 /* Check geometry parameters. */
4986 if ((!dev->param.inband_tags && dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 1024) ||
4987 (!dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 512) ||
4988 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4989 dev->param.chunks_per_block < 2 ||
4990 dev->param.n_reserved_blocks < 2 ||
4991 dev->internal_start_block <= 0 ||
4992 dev->internal_end_block <= 0 ||
4993 dev->internal_end_block <= (dev->internal_start_block + dev->param.n_reserved_blocks + 2)) { /* otherwise it is too small */
4994 T(YAFFS_TRACE_ALWAYS,
4996 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d "
4997 TENDSTR), dev->param.total_bytes_per_chunk, dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags));
5001 if (yaffs_init_nand(dev) != YAFFS_OK) {
5002 T(YAFFS_TRACE_ALWAYS,
5003 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
5007 /* Sort out space for inband tags, if required */
5008 if (dev->param.inband_tags)
5009 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk - sizeof(yaffs_packed_tags2_tags_only);
5011 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
5013 /* Got the right mix of functions? */
5014 if (!yaffs_check_dev_fns(dev)) {
5015 /* Function missing */
5016 T(YAFFS_TRACE_ALWAYS,
5018 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5023 /* This is really a compilation check. */
5024 if (!yaffs_check_structures()) {
5025 T(YAFFS_TRACE_ALWAYS,
5026 (TSTR("yaffs_check_structures failed\n" TENDSTR)));
5030 if (dev->is_mounted) {
5031 T(YAFFS_TRACE_ALWAYS,
5032 (TSTR("yaffs: device already mounted\n" TENDSTR)));
5036 /* Finished with most checks. One or two more checks happen later on too. */
5038 dev->is_mounted = 1;
5040 /* OK now calculate a few things for the device */
5043 * Calculate all the chunk size manipulation numbers:
5045 x = dev->data_bytes_per_chunk;
5046 /* We always use dev->chunk_shift and dev->chunk_div */
5047 dev->chunk_shift = calc_shifts(x);
5048 x >>= dev->chunk_shift;
5050 /* We only use chunk mask if chunk_div is 1 */
5051 dev->chunk_mask = (1<<dev->chunk_shift) - 1;
5054 * Calculate chunk_grp_bits.
5055 * We need to find the next power of 2 > than internal_end_block
5058 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
5060 bits = calc_shifts_ceiling(x);
5062 /* Set up tnode width if wide tnodes are enabled. */
5063 if (!dev->param.wide_tnodes_disabled) {
5064 /* bits must be even so that we end up with 32-bit words */
5068 dev->tnode_width = 16;
5070 dev->tnode_width = bits;
5072 dev->tnode_width = 16;
5074 dev->tnode_mask = (1<<dev->tnode_width)-1;
5076 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
5077 * so if the bitwidth of the
5078 * chunk range we're using is greater than 16 we need
5079 * to figure out chunk shift and chunk_grp_size
5082 if (bits <= dev->tnode_width)
5083 dev->chunk_grp_bits = 0;
5085 dev->chunk_grp_bits = bits - dev->tnode_width;
5087 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0)/8;
5088 if(dev->tnode_size < sizeof(yaffs_tnode_t))
5089 dev->tnode_size = sizeof(yaffs_tnode_t);
5091 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
5093 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
5094 /* We have a problem because the soft delete won't work if
5095 * the chunk group size > chunks per block.
5096 * This can be remedied by using larger "virtual blocks".
5098 T(YAFFS_TRACE_ALWAYS,
5099 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
5104 /* OK, we've finished verifying the device, lets continue with initialisation */
5106 /* More device initialisation */
5108 dev->passive_gc_count = 0;
5109 dev->oldest_dirty_gc_count = 0;
5111 dev->gc_block_finder = 0;
5112 dev->buffered_block = -1;
5113 dev->doing_buffered_block_rewrite = 0;
5114 dev->n_deleted_files = 0;
5115 dev->n_bg_deletions = 0;
5116 dev->n_unlinked_files = 0;
5117 dev->n_ecc_fixed = 0;
5118 dev->n_ecc_unfixed = 0;
5119 dev->n_tags_ecc_fixed = 0;
5120 dev->n_tags_ecc_unfixed = 0;
5121 dev->n_erase_failures = 0;
5122 dev->n_erased_blocks = 0;
5124 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
5125 YINIT_LIST_HEAD(&dev->dirty_dirs);
5126 dev->oldest_dirty_seq = 0;
5127 dev->oldest_dirty_block = 0;
5129 /* Initialise temporary buffers and caches. */
5130 if (!yaffs_init_tmp_buffers(dev))
5134 dev->gc_cleanup_list = NULL;
5138 dev->param.n_caches > 0) {
5141 int cache_bytes = dev->param.n_caches * sizeof(yaffs_cache_t);
5143 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
5144 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
5146 dev->cache = YMALLOC(cache_bytes);
5148 buf = (__u8 *) dev->cache;
5151 memset(dev->cache, 0, cache_bytes);
5153 for (i = 0; i < dev->param.n_caches && buf; i++) {
5154 dev->cache[i].object = NULL;
5155 dev->cache[i].last_use = 0;
5156 dev->cache[i].dirty = 0;
5157 dev->cache[i].data = buf = YMALLOC_DMA(dev->param.total_bytes_per_chunk);
5162 dev->cache_last_use = 0;
5165 dev->cache_hits = 0;
5168 dev->gc_cleanup_list = YMALLOC(dev->param.chunks_per_block * sizeof(__u32));
5169 if (!dev->gc_cleanup_list)
5173 if (dev->param.is_yaffs2)
5174 dev->param.use_header_file_size = 1;
5176 if (!init_failed && !yaffs_init_blocks(dev))
5179 yaffs_init_tnodes_and_objs(dev);
5181 if (!init_failed && !yaffs_create_initial_dir(dev))
5186 /* Now scan the flash. */
5187 if (dev->param.is_yaffs2) {
5188 if (yaffs2_checkpt_restore(dev)) {
5189 yaffs_check_obj_details_loaded(dev->root_dir);
5190 T(YAFFS_TRACE_ALWAYS,
5191 (TSTR("yaffs: restored from checkpoint" TENDSTR)));
5194 /* Clean up the mess caused by an aborted checkpoint load
5195 * and scan backwards.
5197 yaffs_deinit_blocks(dev);
5199 yaffs_deinit_tnodes_and_objs(dev);
5201 dev->n_erased_blocks = 0;
5202 dev->n_free_chunks = 0;
5203 dev->alloc_block = -1;
5204 dev->alloc_page = -1;
5205 dev->n_deleted_files = 0;
5206 dev->n_unlinked_files = 0;
5207 dev->n_bg_deletions = 0;
5209 if (!init_failed && !yaffs_init_blocks(dev))
5212 yaffs_init_tnodes_and_objs(dev);
5214 if (!init_failed && !yaffs_create_initial_dir(dev))
5217 if (!init_failed && !yaffs2_scan_backwards(dev))
5220 } else if (!yaffs1_scan(dev))
5223 yaffs_strip_deleted_objs(dev);
5224 yaffs_fix_hanging_objs(dev);
5225 if(dev->param.empty_lost_n_found)
5226 yaffs_empty_l_n_f(dev);
5230 /* Clean up the mess */
5231 T(YAFFS_TRACE_TRACING,
5232 (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR)));
5234 yaffs_deinitialise(dev);
5238 /* Zero out stats */
5239 dev->n_page_reads = 0;
5240 dev->n_page_writes = 0;
5241 dev->n_erasures = 0;
5242 dev->n_gc_copies = 0;
5243 dev->n_retired_writes = 0;
5245 dev->n_retired_blocks = 0;
5247 yaffs_verify_free_chunks(dev);
5248 yaffs_verify_blocks(dev);
5250 /* Clean up any aborted checkpoint data */
5251 if(!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5252 yaffs2_checkpt_invalidate(dev);
5254 T(YAFFS_TRACE_TRACING,
5255 (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR)));
5260 void yaffs_deinitialise(yaffs_dev_t *dev)
5262 if (dev->is_mounted) {
5265 yaffs_deinit_blocks(dev);
5266 yaffs_deinit_tnodes_and_objs(dev);
5267 if (dev->param.n_caches > 0 &&
5270 for (i = 0; i < dev->param.n_caches; i++) {
5271 if (dev->cache[i].data)
5272 YFREE(dev->cache[i].data);
5273 dev->cache[i].data = NULL;
5280 YFREE(dev->gc_cleanup_list);
5282 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5283 YFREE(dev->temp_buffer[i].buffer);
5285 dev->is_mounted = 0;
5287 if (dev->param.deinitialise_flash_fn)
5288 dev->param.deinitialise_flash_fn(dev);
5292 int yaffs_count_free_chunks(yaffs_dev_t *dev)
5297 yaffs_block_info_t *blk;
5299 blk = dev->block_info;
5300 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5301 switch (blk->block_state) {
5302 case YAFFS_BLOCK_STATE_EMPTY:
5303 case YAFFS_BLOCK_STATE_ALLOCATING:
5304 case YAFFS_BLOCK_STATE_COLLECTING:
5305 case YAFFS_BLOCK_STATE_FULL:
5307 (dev->param.chunks_per_block - blk->pages_in_use +
5308 blk->soft_del_pages);
5319 int yaffs_get_n_free_chunks(yaffs_dev_t *dev)
5321 /* This is what we report to the outside world */
5325 int blocks_for_checkpt;
5328 n_free = dev->n_free_chunks;
5329 n_free += dev->n_deleted_files;
5331 /* Now count the number of dirty chunks in the cache and subtract those */
5333 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5334 if (dev->cache[i].dirty)
5338 n_free -= n_dirty_caches;
5340 n_free -= ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5342 /* Now we figure out how much to reserve for the checkpoint and report that... */
5343 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5345 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
5355 /*---------------------------------------- YAFFS test code ----------------------*/
5357 #define yaffs_check_struct(structure, syze, name) \
5359 if (sizeof(structure) != syze) { \
5360 T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
5361 name, syze, (int) sizeof(structure))); \
5362 return YAFFS_FAIL; \
5366 static int yaffs_check_structures(void)
5368 /* yaffs_check_struct(yaffs_tags_t,8,"yaffs_tags_t"); */
5369 /* yaffs_check_struct(yaffs_tags_union_t,8,"yaffs_tags_union_t"); */
5370 /* yaffs_check_struct(yaffs_spare,16,"yaffs_spare"); */
5371 /* yaffs_check_struct(yaffs_tnode_t, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_tnode_t"); */
5373 #ifndef CONFIG_YAFFS_WINCE
5374 yaffs_check_struct(yaffs_obj_header, 512, "yaffs_obj_header");