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 #include "yaffs_attribs.h"
38 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
39 #define YAFFS_GC_GOOD_ENOUGH 2
40 #define YAFFS_GC_PASSIVE_THRESHOLD 4
42 #include "yaffs_ecc.h"
44 /* Forward declarations */
47 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
48 const u8 * buffer, int n_bytes, int use_reserve);
52 /* Function to calculate chunk and offset */
54 static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
55 int *chunk_out, u32 * offset_out)
60 chunk = (u32) (addr >> dev->chunk_shift);
62 if (dev->chunk_div == 1) {
63 /* easy power of 2 case */
64 offset = (u32) (addr & dev->chunk_mask);
66 /* Non power-of-2 case */
70 chunk /= dev->chunk_div;
72 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
73 offset = (u32) (addr - chunk_base);
80 /* Function to return the number of shifts for a power of 2 greater than or
81 * equal to the given number
82 * Note we don't try to cater for all possible numbers and this does not have to
83 * be hellishly efficient.
86 static u32 calc_shifts_ceiling(u32 x)
91 shifts = extra_bits = 0;
106 /* Function to return the number of shifts to get a 1 in bit 0
109 static u32 calc_shifts(u32 x)
127 * Temporary buffer manipulations.
130 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
135 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
137 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
138 dev->temp_buffer[i].line = 0; /* not in use */
139 dev->temp_buffer[i].buffer = buf =
140 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
143 return buf ? YAFFS_OK : YAFFS_FAIL;
146 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
151 if (dev->temp_in_use > dev->max_temp)
152 dev->max_temp = dev->temp_in_use;
154 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
155 if (dev->temp_buffer[i].line == 0) {
156 dev->temp_buffer[i].line = line_no;
157 if ((i + 1) > dev->max_temp) {
158 dev->max_temp = i + 1;
159 for (j = 0; j <= i; j++)
160 dev->temp_buffer[j].max_line =
161 dev->temp_buffer[j].line;
164 return dev->temp_buffer[i].buffer;
168 T(YAFFS_TRACE_BUFFERS,
169 (TSTR("Out of temp buffers at line %d, other held by lines:"),
171 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
172 T(YAFFS_TRACE_BUFFERS,
173 (TSTR(" %d "), dev->temp_buffer[i].line));
175 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
178 * If we got here then we have to allocate an unmanaged one
182 dev->unmanaged_buffer_allocs++;
183 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
187 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
193 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
194 if (dev->temp_buffer[i].buffer == buffer) {
195 dev->temp_buffer[i].line = 0;
201 /* assume it is an unmanaged one. */
202 T(YAFFS_TRACE_BUFFERS,
203 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
206 dev->unmanaged_buffer_deallocs++;
212 * Determine if we have a managed buffer.
214 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
218 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
219 if (dev->temp_buffer[i].buffer == buffer)
223 for (i = 0; i < dev->param.n_caches; i++) {
224 if (dev->cache[i].data == buffer)
228 if (buffer == dev->checkpt_buffer)
231 T(YAFFS_TRACE_ALWAYS,
232 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
237 * Functions for robustisizing TODO
241 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
243 const struct yaffs_ext_tags *tags)
246 nand_chunk = nand_chunk;
251 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
252 const struct yaffs_ext_tags *tags)
255 nand_chunk = nand_chunk;
259 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
260 struct yaffs_block_info *bi)
262 if (!bi->gc_prioritise) {
263 bi->gc_prioritise = 1;
264 dev->has_pending_prioritised_gc = 1;
265 bi->chunk_error_strikes++;
267 if (bi->chunk_error_strikes > 3) {
268 bi->needs_retiring = 1; /* Too many stikes, so retire this */
269 T(YAFFS_TRACE_ALWAYS,
270 (TSTR("yaffs: Block struck out" TENDSTR)));
276 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
279 int flash_block = nand_chunk / dev->param.chunks_per_block;
280 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
282 yaffs_handle_chunk_error(dev, bi);
285 /* Was an actual write failure, so mark the block for retirement */
286 bi->needs_retiring = 1;
287 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
288 (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
291 /* Delete the chunk */
292 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
293 yaffs_skip_rest_of_block(dev);
301 * Simple hash function. Needs to have a reasonable spread
304 static Y_INLINE int yaffs_hash_fn(int n)
307 return n % YAFFS_NOBJECT_BUCKETS;
311 * Access functions to useful fake objects.
312 * Note that root might have a presence in NAND if permissions are set.
315 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
317 return dev->root_dir;
320 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
322 return dev->lost_n_found;
326 * Erased NAND checking functions
329 int yaffs_check_ff(u8 * buffer, int n_bytes)
331 /* Horrible, slow implementation */
340 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
342 int retval = YAFFS_OK;
343 u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
344 struct yaffs_ext_tags tags;
347 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
349 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
352 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
353 T(YAFFS_TRACE_NANDACCESS,
354 (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
358 yaffs_release_temp_buffer(dev, data, __LINE__);
364 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
367 struct yaffs_ext_tags *tags)
369 int retval = YAFFS_OK;
370 struct 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__);
387 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
390 int reserved_blocks = dev->param.n_reserved_blocks;
393 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
396 ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
398 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
401 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
405 struct yaffs_block_info *bi;
407 if (dev->n_erased_blocks < 1) {
408 /* Hoosterman we've got a problem.
409 * Can't get space to gc
412 (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
417 /* Find an empty block. */
419 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
420 dev->alloc_block_finder++;
421 if (dev->alloc_block_finder < dev->internal_start_block
422 || dev->alloc_block_finder > dev->internal_end_block) {
423 dev->alloc_block_finder = dev->internal_start_block;
426 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
428 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
429 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
431 bi->seq_number = dev->seq_number;
432 dev->n_erased_blocks--;
433 T(YAFFS_TRACE_ALLOCATE,
434 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
435 dev->alloc_block_finder, dev->seq_number,
436 dev->n_erased_blocks));
437 return dev->alloc_block_finder;
441 T(YAFFS_TRACE_ALWAYS,
443 ("yaffs tragedy: no more erased blocks, but there should have been %d"
444 TENDSTR), dev->n_erased_blocks));
448 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
449 struct yaffs_block_info **block_ptr)
452 struct yaffs_block_info *bi;
454 if (dev->alloc_block < 0) {
455 /* Get next block to allocate off */
456 dev->alloc_block = yaffs_find_alloc_block(dev);
460 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
461 /* Not enough space to allocate unless we're allowed to use the reserve. */
465 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
466 && dev->alloc_page == 0) {
467 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
470 /* Next page please.... */
471 if (dev->alloc_block >= 0) {
472 bi = yaffs_get_block_info(dev, dev->alloc_block);
474 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
477 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
481 dev->n_free_chunks--;
483 /* If the block is full set the state to full */
484 if (dev->alloc_page >= dev->param.chunks_per_block) {
485 bi->block_state = YAFFS_BLOCK_STATE_FULL;
486 dev->alloc_block = -1;
496 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
501 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
505 n = dev->n_erased_blocks * dev->param.chunks_per_block;
507 if (dev->alloc_block > 0)
508 n += (dev->param.chunks_per_block - dev->alloc_page);
515 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
516 * if we don't want to write to it.
518 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
520 if (dev->alloc_block > 0) {
521 struct yaffs_block_info *bi =
522 yaffs_get_block_info(dev, dev->alloc_block);
523 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
524 bi->block_state = YAFFS_BLOCK_STATE_FULL;
525 dev->alloc_block = -1;
530 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
532 struct yaffs_ext_tags *tags, int use_reserver)
538 yaffs2_checkpt_invalidate(dev);
541 struct yaffs_block_info *bi = 0;
544 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
550 /* First check this chunk is erased, if it needs
551 * checking. The checking policy (unless forced
552 * always on) is as follows:
554 * Check the first page we try to write in a block.
555 * If the check passes then we don't need to check any
556 * more. If the check fails, we check again...
557 * If the block has been erased, we don't need to check.
559 * However, if the block has been prioritised for gc,
560 * then we think there might be something odd about
561 * this block and stop using it.
563 * Rationale: We should only ever see chunks that have
564 * not been erased if there was a partially written
565 * chunk due to power loss. This checking policy should
566 * catch that case with very few checks and thus save a
567 * lot of checks that are most likely not needed.
570 * If an erase check fails or the write fails we skip the
574 /* let's give it a try */
577 if (dev->param.always_check_erased)
578 bi->skip_erased_check = 0;
580 if (!bi->skip_erased_check) {
581 erased_ok = yaffs_check_chunk_erased(dev, chunk);
582 if (erased_ok != YAFFS_OK) {
584 (TSTR("**>> yaffs chunk %d was not erased"
587 /* If not erased, delete this one,
588 * skip rest of block and
589 * try another chunk */
590 yaffs_chunk_del(dev, chunk, 1, __LINE__);
591 yaffs_skip_rest_of_block(dev);
596 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
598 if (!bi->skip_erased_check)
600 yaffs_verify_chunk_written(dev, chunk, data, tags);
602 if (write_ok != YAFFS_OK) {
603 /* Clean up aborted write, skip to next block and
604 * try another chunk */
605 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
609 bi->skip_erased_check = 1;
611 /* Copy the data into the robustification buffer */
612 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
614 } while (write_ok != YAFFS_OK &&
615 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
622 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
625 dev->n_retired_writes += (attempts - 1);
632 * Block retiring for handling a broken block.
635 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
637 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
639 yaffs2_checkpt_invalidate(dev);
641 yaffs2_clear_oldest_dirty_seq(dev, bi);
643 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
644 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
645 T(YAFFS_TRACE_ALWAYS,
647 ("yaffs: Failed to mark bad and erase block %d"
648 TENDSTR), flash_block));
650 struct yaffs_ext_tags tags;
652 flash_block * dev->param.chunks_per_block;
654 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
656 memset(buffer, 0xff, dev->data_bytes_per_chunk);
657 yaffs_init_tags(&tags);
658 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
659 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
663 T(YAFFS_TRACE_ALWAYS,
666 TCONT("write bad block marker to block %d")
667 TENDSTR), flash_block));
669 yaffs_release_temp_buffer(dev, buffer, __LINE__);
673 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
674 bi->gc_prioritise = 0;
675 bi->needs_retiring = 0;
677 dev->n_retired_blocks++;
680 /*---------------- Name handling functions ------------*/
682 static u16 yaffs_calc_name_sum(const YCHAR * name)
687 const YUCHAR *bname = (const YUCHAR *)name;
689 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
691 /* 0x1f mask is case insensitive */
692 sum += ((*bname) & 0x1f) * i;
700 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
702 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
703 memset(obj->short_name, 0, sizeof(obj->short_name));
705 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
706 YAFFS_SHORT_NAME_LENGTH)
707 strcpy(obj->short_name, name);
709 obj->short_name[0] = _Y('\0');
711 obj->sum = yaffs_calc_name_sum(name);
714 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
715 const struct yaffs_obj_hdr *oh)
717 #ifdef CONFIG_YAFFS_AUTO_UNICODE
718 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
719 memset(tmp_name, 0, sizeof(tmp_name));
720 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
721 YAFFS_MAX_NAME_LENGTH + 1);
722 yaffs_set_obj_name(obj, tmp_name);
724 yaffs_set_obj_name(obj, oh->name);
728 /*-------------------- TNODES -------------------
730 * List of spare tnodes
731 * The list is hooked together using the first pointer
735 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
737 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
739 memset(tn, 0, dev->tnode_size);
743 dev->checkpoint_blocks_required = 0; /* force recalculation */
748 /* FreeTnode frees up a tnode and puts it back on the free list */
749 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
751 yaffs_free_raw_tnode(dev, tn);
753 dev->checkpoint_blocks_required = 0; /* force recalculation */
756 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
758 yaffs_deinit_raw_tnodes_and_objs(dev);
763 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
764 unsigned pos, unsigned val)
766 u32 *map = (u32 *) tn;
772 pos &= YAFFS_TNODES_LEVEL0_MASK;
773 val >>= dev->chunk_grp_bits;
775 bit_in_map = pos * dev->tnode_width;
776 word_in_map = bit_in_map / 32;
777 bit_in_word = bit_in_map & (32 - 1);
779 mask = dev->tnode_mask << bit_in_word;
781 map[word_in_map] &= ~mask;
782 map[word_in_map] |= (mask & (val << bit_in_word));
784 if (dev->tnode_width > (32 - bit_in_word)) {
785 bit_in_word = (32 - bit_in_word);
788 dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
789 map[word_in_map] &= ~mask;
790 map[word_in_map] |= (mask & (val >> bit_in_word));
794 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
797 u32 *map = (u32 *) tn;
803 pos &= YAFFS_TNODES_LEVEL0_MASK;
805 bit_in_map = pos * dev->tnode_width;
806 word_in_map = bit_in_map / 32;
807 bit_in_word = bit_in_map & (32 - 1);
809 val = map[word_in_map] >> bit_in_word;
811 if (dev->tnode_width > (32 - bit_in_word)) {
812 bit_in_word = (32 - bit_in_word);
814 val |= (map[word_in_map] << bit_in_word);
817 val &= dev->tnode_mask;
818 val <<= dev->chunk_grp_bits;
823 /* ------------------- End of individual tnode manipulation -----------------*/
825 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
826 * The look up tree is represented by the top tnode and the number of top_level
827 * in the tree. 0 means only the level 0 tnode is in the tree.
830 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
831 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
832 struct yaffs_file_var *file_struct,
835 struct yaffs_tnode *tn = file_struct->top;
838 int level = file_struct->top_level;
842 /* Check sane level and chunk Id */
843 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
846 if (chunk_id > YAFFS_MAX_CHUNK_ID)
849 /* First check we're tall enough (ie enough top_level) */
851 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
854 i >>= YAFFS_TNODES_INTERNAL_BITS;
858 if (required_depth > file_struct->top_level)
859 return NULL; /* Not tall enough, so we can't find it */
861 /* Traverse down to level 0 */
862 while (level > 0 && tn) {
863 tn = tn->internal[(chunk_id >>
864 (YAFFS_TNODES_LEVEL0_BITS +
866 YAFFS_TNODES_INTERNAL_BITS)) &
867 YAFFS_TNODES_INTERNAL_MASK];
874 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
875 * This happens in two steps:
876 * 1. If the tree isn't tall enough, then make it taller.
877 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
879 * Used when modifying the tree.
881 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
882 * be plugged into the ttree.
885 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
886 struct yaffs_file_var *file_struct,
888 struct yaffs_tnode *passed_tn)
893 struct yaffs_tnode *tn;
897 /* Check sane level and page Id */
898 if (file_struct->top_level < 0
899 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
902 if (chunk_id > YAFFS_MAX_CHUNK_ID)
905 /* First check we're tall enough (ie enough top_level) */
907 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
910 x >>= YAFFS_TNODES_INTERNAL_BITS;
914 if (required_depth > file_struct->top_level) {
915 /* Not tall enough, gotta make the tree taller */
916 for (i = file_struct->top_level; i < required_depth; i++) {
918 tn = yaffs_get_tnode(dev);
921 tn->internal[0] = file_struct->top;
922 file_struct->top = tn;
923 file_struct->top_level++;
926 (TSTR("yaffs: no more tnodes" TENDSTR)));
932 /* Traverse down to level 0, adding anything we need */
934 l = file_struct->top_level;
935 tn = file_struct->top;
938 while (l > 0 && tn) {
940 (YAFFS_TNODES_LEVEL0_BITS +
941 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
942 YAFFS_TNODES_INTERNAL_MASK;
944 if ((l > 1) && !tn->internal[x]) {
945 /* Add missing non-level-zero tnode */
946 tn->internal[x] = yaffs_get_tnode(dev);
947 if (!tn->internal[x])
950 /* Looking from level 1 at level 0 */
952 /* If we already have one, then release it. */
954 yaffs_free_tnode(dev,
957 tn->internal[x] = passed_tn;
959 } else if (!tn->internal[x]) {
960 /* Don't have one, none passed in */
961 tn->internal[x] = yaffs_get_tnode(dev);
962 if (!tn->internal[x])
967 tn = tn->internal[x];
973 memcpy(tn, passed_tn,
974 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
975 yaffs_free_tnode(dev, passed_tn);
982 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
985 return (tags->chunk_id == chunk_obj &&
986 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
990 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
991 struct yaffs_ext_tags *tags, int obj_id,
996 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
997 if (yaffs_check_chunk_bit
998 (dev, the_chunk / dev->param.chunks_per_block,
999 the_chunk % dev->param.chunks_per_block)) {
1001 if (dev->chunk_grp_size == 1)
1004 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
1006 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
1017 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1018 struct yaffs_ext_tags *tags)
1020 /*Get the Tnode, then get the level 0 offset chunk offset */
1021 struct yaffs_tnode *tn;
1023 struct yaffs_ext_tags local_tags;
1026 struct yaffs_dev *dev = in->my_dev;
1029 /* Passed a NULL, so use our own tags space */
1033 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1036 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1039 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1045 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1046 struct yaffs_ext_tags *tags)
1048 /* Get the Tnode, then get the level 0 offset chunk offset */
1049 struct yaffs_tnode *tn;
1051 struct yaffs_ext_tags local_tags;
1053 struct yaffs_dev *dev = in->my_dev;
1057 /* Passed a NULL, so use our own tags space */
1061 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1065 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1068 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1071 /* Delete the entry in the filestructure (if found) */
1073 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1079 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1080 int nand_chunk, int in_scan)
1082 /* NB in_scan is zero unless scanning.
1083 * For forward scanning, in_scan is > 0;
1084 * for backward scanning in_scan is < 0
1086 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1089 struct yaffs_tnode *tn;
1090 struct yaffs_dev *dev = in->my_dev;
1092 struct yaffs_ext_tags existing_tags;
1093 struct yaffs_ext_tags new_tags;
1094 unsigned existing_serial, new_serial;
1096 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1097 /* Just ignore an attempt at putting a chunk into a non-file during scanning
1098 * If it is not during Scanning then something went wrong!
1101 T(YAFFS_TRACE_ERROR,
1103 ("yaffs tragedy:attempt to put data chunk into a non-file"
1108 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1112 tn = yaffs_add_find_tnode_0(dev,
1113 &in->variant.file_variant,
1119 /* Dummy insert, bail now */
1122 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1125 /* If we're scanning then we need to test for duplicates
1126 * NB This does not need to be efficient since it should only ever
1127 * happen when the power fails during a write, then only one
1128 * chunk should ever be affected.
1130 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
1131 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
1134 if (existing_cunk > 0) {
1135 /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
1136 * thus we have to do a FindChunkInFile to get the real chunk id.
1138 * We have a duplicate now we need to decide which one to use:
1140 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
1141 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
1142 * YAFFS1: Get both sets of tags and compare serial numbers.
1146 /* Only do this for forward scanning */
1147 yaffs_rd_chunk_tags_nand(dev,
1151 /* Do a proper find */
1153 yaffs_find_chunk_in_file(in, inode_chunk,
1157 if (existing_cunk <= 0) {
1158 /*Hoosterman - how did this happen? */
1160 T(YAFFS_TRACE_ERROR,
1162 ("yaffs tragedy: existing chunk < 0 in scan"
1167 /* NB The deleted flags should be false, otherwise the chunks will
1168 * not be loaded during a scan
1172 new_serial = new_tags.serial_number;
1173 existing_serial = existing_tags.serial_number;
1176 if ((in_scan > 0) &&
1177 (existing_cunk <= 0 ||
1178 ((existing_serial + 1) & 3) == new_serial)) {
1179 /* Forward scanning.
1181 * Delete the old one and drop through to update the tnode
1183 yaffs_chunk_del(dev, existing_cunk, 1,
1186 /* Backward scanning or we want to use the existing one
1188 * Delete the new one and return early so that the tnode isn't changed
1190 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1197 if (existing_cunk == 0)
1198 in->n_data_chunks++;
1200 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1205 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1207 struct yaffs_block_info *the_block;
1210 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
1212 block_no = chunk / dev->param.chunks_per_block;
1213 the_block = yaffs_get_block_info(dev, block_no);
1215 the_block->soft_del_pages++;
1216 dev->n_free_chunks++;
1217 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1221 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1222 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1224 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1227 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1228 u32 level, int chunk_offset)
1233 struct yaffs_dev *dev = in->my_dev;
1238 for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
1240 if (tn->internal[i]) {
1242 yaffs_soft_del_worker(in,
1248 YAFFS_TNODES_INTERNAL_BITS)
1251 yaffs_free_tnode(dev,
1254 tn->internal[i] = NULL;
1256 /* Hoosterman... how could this happen? */
1260 return (all_done) ? 1 : 0;
1261 } else if (level == 0) {
1263 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1264 the_chunk = yaffs_get_group_base(dev, tn, i);
1266 /* Note this does not find the real chunk, only the chunk group.
1267 * We make an assumption that a chunk group is not larger than
1270 yaffs_soft_del_chunk(dev, the_chunk);
1271 yaffs_load_tnode_0(dev, tn, i, 0);
1285 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1287 struct yaffs_dev *dev = obj->my_dev;
1288 struct yaffs_obj *parent;
1290 yaffs_verify_obj_in_dir(obj);
1291 parent = obj->parent;
1293 yaffs_verify_dir(parent);
1295 if (dev && dev->param.remove_obj_fn)
1296 dev->param.remove_obj_fn(obj);
1298 list_del_init(&obj->siblings);
1301 yaffs_verify_dir(parent);
1304 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1307 T(YAFFS_TRACE_ALWAYS,
1309 ("tragedy: Trying to add an object to a null pointer directory"
1314 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1315 T(YAFFS_TRACE_ALWAYS,
1317 ("tragedy: Trying to add an object to a non-directory"
1322 if (obj->siblings.prev == NULL) {
1323 /* Not initialised */
1327 yaffs_verify_dir(directory);
1329 yaffs_remove_obj_from_dir(obj);
1332 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1333 obj->parent = directory;
1335 if (directory == obj->my_dev->unlinked_dir
1336 || directory == obj->my_dev->del_dir) {
1338 obj->my_dev->n_unlinked_files++;
1339 obj->rename_allowed = 0;
1342 yaffs_verify_dir(directory);
1343 yaffs_verify_obj_in_dir(obj);
1346 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1347 struct yaffs_obj *new_dir,
1348 const YCHAR * new_name, int force, int shadows)
1353 struct yaffs_obj *existing_target;
1355 if (new_dir == NULL)
1356 new_dir = obj->parent; /* use the old directory */
1358 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1359 T(YAFFS_TRACE_ALWAYS,
1361 ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
1366 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1367 if (obj->my_dev->param.is_yaffs2)
1368 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1370 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1371 && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1373 del_op = (new_dir == obj->my_dev->del_dir);
1375 existing_target = yaffs_find_by_name(new_dir, new_name);
1377 /* If the object is a file going into the unlinked directory,
1378 * then it is OK to just stuff it in since duplicate names are allowed.
1379 * else only proceed if the new name does not exist and if we're putting
1380 * it into a directory.
1386 !existing_target) &&
1387 new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1388 yaffs_set_obj_name(obj, new_name);
1391 yaffs_add_obj_to_dir(new_dir, obj);
1396 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1397 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
1405 /*------------------------ Short Operations Cache ----------------------------------------
1406 * In many situations where there is no high level buffering a lot of
1407 * reads might be short sequential reads, and a lot of writes may be short
1408 * sequential writes. eg. scanning/writing a jpeg file.
1409 * In these cases, a short read/write cache can provide a huge perfomance
1410 * benefit with dumb-as-a-rock code.
1411 * In Linux, the page cache provides read buffering and the short op cache
1412 * provides write buffering.
1414 * There are a limited number (~10) of cache chunks per device so that we don't
1415 * need a very intelligent search.
1418 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1420 struct yaffs_dev *dev = obj->my_dev;
1422 struct yaffs_cache *cache;
1423 int n_caches = obj->my_dev->param.n_caches;
1425 for (i = 0; i < n_caches; i++) {
1426 cache = &dev->cache[i];
1427 if (cache->object == obj && cache->dirty)
1434 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1436 struct yaffs_dev *dev = obj->my_dev;
1437 int lowest = -99; /* Stop compiler whining. */
1439 struct yaffs_cache *cache;
1440 int chunk_written = 0;
1441 int n_caches = obj->my_dev->param.n_caches;
1447 /* Find the dirty cache for this object with the lowest chunk id. */
1448 for (i = 0; i < n_caches; i++) {
1449 if (dev->cache[i].object == obj &&
1450 dev->cache[i].dirty) {
1452 || dev->cache[i].chunk_id <
1454 cache = &dev->cache[i];
1455 lowest = cache->chunk_id;
1460 if (cache && !cache->locked) {
1461 /* Write it out and free it up */
1464 yaffs_wr_data_obj(cache->object,
1469 cache->object = NULL;
1472 } while (cache && chunk_written > 0);
1475 /* Hoosterman, disk full while writing cache out. */
1476 T(YAFFS_TRACE_ERROR,
1478 ("yaffs tragedy: no space during cache write"
1486 /*yaffs_flush_whole_cache(dev)
1491 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1493 struct yaffs_obj *obj;
1494 int n_caches = dev->param.n_caches;
1497 /* Find a dirty object in the cache and flush it...
1498 * until there are no further dirty objects.
1502 for (i = 0; i < n_caches && !obj; i++) {
1503 if (dev->cache[i].object && dev->cache[i].dirty)
1504 obj = dev->cache[i].object;
1508 yaffs_flush_file_cache(obj);
1514 /* Grab us a cache chunk for use.
1515 * First look for an empty one.
1516 * Then look for the least recently used non-dirty one.
1517 * Then look for the least recently used dirty one...., flush and look again.
1519 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1523 if (dev->param.n_caches > 0) {
1524 for (i = 0; i < dev->param.n_caches; i++) {
1525 if (!dev->cache[i].object)
1526 return &dev->cache[i];
1533 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1535 struct yaffs_cache *cache;
1536 struct yaffs_obj *the_obj;
1541 if (dev->param.n_caches > 0) {
1542 /* Try find a non-dirty one... */
1544 cache = yaffs_grab_chunk_worker(dev);
1547 /* They were all dirty, find the last recently used object and flush
1548 * its cache, then find again.
1549 * NB what's here is not very accurate, we actually flush the object
1550 * the last recently used page.
1553 /* With locking we can't assume we can use entry zero */
1560 for (i = 0; i < dev->param.n_caches; i++) {
1561 if (dev->cache[i].object &&
1562 !dev->cache[i].locked &&
1563 (dev->cache[i].last_use < usage
1565 usage = dev->cache[i].last_use;
1566 the_obj = dev->cache[i].object;
1567 cache = &dev->cache[i];
1572 if (!cache || cache->dirty) {
1573 /* Flush and try again */
1574 yaffs_flush_file_cache(the_obj);
1575 cache = yaffs_grab_chunk_worker(dev);
1585 /* Find a cached chunk */
1586 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1589 struct yaffs_dev *dev = obj->my_dev;
1591 if (dev->param.n_caches > 0) {
1592 for (i = 0; i < dev->param.n_caches; i++) {
1593 if (dev->cache[i].object == obj &&
1594 dev->cache[i].chunk_id == chunk_id) {
1597 return &dev->cache[i];
1604 /* Mark the chunk for the least recently used algorithym */
1605 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1609 if (dev->param.n_caches > 0) {
1610 if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
1611 /* Reset the cache usages */
1613 for (i = 1; i < dev->param.n_caches; i++)
1614 dev->cache[i].last_use = 0;
1616 dev->cache_last_use = 0;
1619 dev->cache_last_use++;
1621 cache->last_use = dev->cache_last_use;
1628 /* Invalidate a single cache page.
1629 * Do this when a whole page gets written,
1630 * ie the short cache for this page is no longer valid.
1632 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1634 if (object->my_dev->param.n_caches > 0) {
1635 struct yaffs_cache *cache =
1636 yaffs_find_chunk_cache(object, chunk_id);
1639 cache->object = NULL;
1643 /* Invalidate all the cache pages associated with this object
1644 * Do this whenever ther file is deleted or resized.
1646 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1649 struct yaffs_dev *dev = in->my_dev;
1651 if (dev->param.n_caches > 0) {
1652 /* Invalidate it. */
1653 for (i = 0; i < dev->param.n_caches; i++) {
1654 if (dev->cache[i].object == in)
1655 dev->cache[i].object = NULL;
1660 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1663 struct yaffs_dev *dev = obj->my_dev;
1665 /* If it is still linked into the bucket list, free from the list */
1666 if (!list_empty(&obj->hash_link)) {
1667 list_del_init(&obj->hash_link);
1668 bucket = yaffs_hash_fn(obj->obj_id);
1669 dev->obj_bucket[bucket].count--;
1673 /* FreeObject frees up a Object and puts it back on the free list */
1674 static void yaffs_free_obj(struct yaffs_obj *obj)
1676 struct yaffs_dev *dev = obj->my_dev;
1679 (TSTR("FreeObject %p inode %p" TENDSTR), obj, obj->my_inode));
1685 if (!list_empty(&obj->siblings))
1688 if (obj->my_inode) {
1689 /* We're still hooked up to a cached inode.
1690 * Don't delete now, but mark for later deletion
1692 obj->defered_free = 1;
1696 yaffs_unhash_obj(obj);
1698 yaffs_free_raw_obj(dev, obj);
1700 dev->checkpoint_blocks_required = 0; /* force recalculation */
1703 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1705 if (obj->defered_free)
1706 yaffs_free_obj(obj);
1709 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1712 /* First off, invalidate the file's data in the cache, without flushing. */
1713 yaffs_invalidate_whole_cache(in);
1715 if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
1716 /* Move to the unlinked directory so we have a record that it was deleted. */
1717 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1722 yaffs_remove_obj_from_dir(in);
1723 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1731 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1734 obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1735 if (obj->n_data_chunks <= 0) {
1736 /* Empty file with no duplicate object headers, just delete it immediately */
1737 yaffs_free_tnode(obj->my_dev,
1738 obj->variant.file_variant.top);
1739 obj->variant.file_variant.top = NULL;
1740 T(YAFFS_TRACE_TRACING,
1741 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1743 yaffs_generic_obj_del(obj);
1745 yaffs_soft_del_worker(obj,
1746 obj->variant.file_variant.top,
1748 file_variant.top_level, 0);
1754 /* Pruning removes any part of the file structure tree that is beyond the
1755 * bounds of the file (ie that does not point to chunks).
1757 * A file should only get pruned when its size is reduced.
1759 * Before pruning, the chunks must be pulled from the tree and the
1760 * level 0 tnode entries must be zeroed out.
1761 * Could also use this for file deletion, but that's probably better handled
1762 * by a special case.
1764 * This function is recursive. For levels > 0 the function is called again on
1765 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1766 * If there is no data in a subtree then it is pruned.
1769 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1770 struct yaffs_tnode *tn, u32 level,
1780 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1781 if (tn->internal[i]) {
1783 yaffs_prune_worker(dev,
1790 if (tn->internal[i])
1794 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1795 u32 *map = (u32 *) tn;
1797 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1803 if (has_data == 0 && del0) {
1804 /* Free and return NULL */
1806 yaffs_free_tnode(dev, tn);
1816 static int yaffs_prune_tree(struct yaffs_dev *dev,
1817 struct yaffs_file_var *file_struct)
1822 struct yaffs_tnode *tn;
1824 if (file_struct->top_level > 0) {
1826 yaffs_prune_worker(dev, file_struct->top,
1827 file_struct->top_level, 0);
1829 /* Now we have a tree with all the non-zero branches NULL but the height
1830 * is the same as it was.
1831 * Let's see if we can trim internal tnodes to shorten the tree.
1832 * We can do this if only the 0th element in the tnode is in use
1833 * (ie all the non-zero are NULL)
1836 while (file_struct->top_level && !done) {
1837 tn = file_struct->top;
1840 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1841 if (tn->internal[i])
1846 file_struct->top = tn->internal[0];
1847 file_struct->top_level--;
1848 yaffs_free_tnode(dev, tn);
1858 /*-------------------- End of File Structure functions.-------------------*/
1860 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1861 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1863 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1868 /* Now sweeten it up... */
1870 memset(obj, 0, sizeof(struct yaffs_obj));
1871 obj->being_created = 1;
1875 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1876 INIT_LIST_HEAD(&(obj->hard_links));
1877 INIT_LIST_HEAD(&(obj->hash_link));
1878 INIT_LIST_HEAD(&obj->siblings);
1880 /* Now make the directory sane */
1881 if (dev->root_dir) {
1882 obj->parent = dev->root_dir;
1883 list_add(&(obj->siblings),
1884 &dev->root_dir->variant.dir_variant.children);
1887 /* Add it to the lost and found directory.
1888 * NB Can't put root or lost-n-found in lost-n-found so
1889 * check if lost-n-found exists first
1891 if (dev->lost_n_found)
1892 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1894 obj->being_created = 0;
1897 dev->checkpoint_blocks_required = 0; /* force recalculation */
1902 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1906 int lowest = 999999;
1908 /* Search for the shortest list or one that
1912 for (i = 0; i < 10 && lowest > 4; i++) {
1913 dev->bucket_finder++;
1914 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1915 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1916 lowest = dev->obj_bucket[dev->bucket_finder].count;
1917 l = dev->bucket_finder;
1925 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1927 int bucket = yaffs_find_nice_bucket(dev);
1929 /* Now find an object value that has not already been taken
1930 * by scanning the list.
1934 struct list_head *i;
1936 u32 n = (u32) bucket;
1938 /* yaffs_check_obj_hash_sane(); */
1942 n += YAFFS_NOBJECT_BUCKETS;
1943 if (1 || dev->obj_bucket[bucket].count > 0) {
1944 list_for_each(i, &dev->obj_bucket[bucket].list) {
1945 /* If there is already one in the list */
1946 if (i && list_entry(i, struct yaffs_obj,
1947 hash_link)->obj_id == n) {
1957 static void yaffs_hash_obj(struct yaffs_obj *in)
1959 int bucket = yaffs_hash_fn(in->obj_id);
1960 struct yaffs_dev *dev = in->my_dev;
1962 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1963 dev->obj_bucket[bucket].count++;
1966 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1968 int bucket = yaffs_hash_fn(number);
1969 struct list_head *i;
1970 struct yaffs_obj *in;
1972 list_for_each(i, &dev->obj_bucket[bucket].list) {
1973 /* Look if it is in the list */
1975 in = list_entry(i, struct yaffs_obj, hash_link);
1976 if (in->obj_id == number) {
1978 /* Don't tell the VFS about this one if it is defered free */
1979 if (in->defered_free)
1990 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1991 enum yaffs_obj_type type)
1993 struct yaffs_obj *the_obj = NULL;
1994 struct yaffs_tnode *tn = NULL;
1997 number = yaffs_new_obj_id(dev);
1999 if (type == YAFFS_OBJECT_TYPE_FILE) {
2000 tn = yaffs_get_tnode(dev);
2005 the_obj = yaffs_alloc_empty_obj(dev);
2008 yaffs_free_tnode(dev, tn);
2014 the_obj->rename_allowed = 1;
2015 the_obj->unlink_allowed = 1;
2016 the_obj->obj_id = number;
2017 yaffs_hash_obj(the_obj);
2018 the_obj->variant_type = type;
2019 yaffs_load_current_time(the_obj, 1, 1);
2022 case YAFFS_OBJECT_TYPE_FILE:
2023 the_obj->variant.file_variant.file_size = 0;
2024 the_obj->variant.file_variant.scanned_size = 0;
2025 the_obj->variant.file_variant.shrink_size = ~0; /* max */
2026 the_obj->variant.file_variant.top_level = 0;
2027 the_obj->variant.file_variant.top = tn;
2029 case YAFFS_OBJECT_TYPE_DIRECTORY:
2030 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
2031 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
2033 case YAFFS_OBJECT_TYPE_SYMLINK:
2034 case YAFFS_OBJECT_TYPE_HARDLINK:
2035 case YAFFS_OBJECT_TYPE_SPECIAL:
2036 /* No action required */
2038 case YAFFS_OBJECT_TYPE_UNKNOWN:
2039 /* todo this should not happen */
2047 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2048 int number, u32 mode)
2051 struct yaffs_obj *obj =
2052 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2054 obj->fake = 1; /* it is fake so it might have no NAND presence... */
2055 obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
2056 obj->unlink_allowed = 0; /* ... or unlink it */
2059 obj->yst_mode = mode;
2061 obj->hdr_chunk = 0; /* Not a valid chunk. */
2069 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2076 yaffs_init_raw_tnodes_and_objs(dev);
2078 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2079 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2080 dev->obj_bucket[i].count = 0;
2084 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2086 enum yaffs_obj_type type)
2088 struct yaffs_obj *the_obj = NULL;
2091 the_obj = yaffs_find_by_number(dev, number);
2094 the_obj = yaffs_new_obj(dev, number, type);
2100 YCHAR *yaffs_clone_str(const YCHAR * str)
2102 YCHAR *new_str = NULL;
2108 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2109 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2111 strncpy(new_str, str, len);
2118 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2119 * link (ie. name) is created or deleted in the directory.
2122 * create dir/a : update dir's mtime/ctime
2123 * rm dir/a: update dir's mtime/ctime
2124 * modify dir/a: don't update dir's mtimme/ctime
2126 * This can be handled immediately or defered. Defering helps reduce the number
2127 * of updates when many files in a directory are changed within a brief period.
2129 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2130 * called periodically.
2133 static void yaffs_update_parent(struct yaffs_obj *obj)
2135 struct yaffs_dev *dev;
2140 yaffs_load_current_time(obj, 0, 1);
2141 if (dev->param.defered_dir_update) {
2142 struct list_head *link = &obj->variant.dir_variant.dirty;
2144 if (list_empty(link)) {
2145 list_add(link, &dev->dirty_dirs);
2146 T(YAFFS_TRACE_BACKGROUND,
2147 (TSTR("Added object %d to dirty directories" TENDSTR),
2152 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2156 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2158 struct list_head *link;
2159 struct yaffs_obj *obj;
2160 struct yaffs_dir_var *d_s;
2161 union yaffs_obj_var *o_v;
2163 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
2165 while (!list_empty(&dev->dirty_dirs)) {
2166 link = dev->dirty_dirs.next;
2167 list_del_init(link);
2169 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2170 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2171 obj = list_entry(o_v, struct yaffs_obj, variant);
2173 T(YAFFS_TRACE_BACKGROUND,
2174 (TSTR("Update directory %d" TENDSTR), obj->obj_id));
2177 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2182 * Mknod (create) a new object.
2183 * equiv_obj only has meaning for a hard link;
2184 * alias_str only has meaning for a symlink.
2185 * rdev only has meaning for devices (a subset of special objects)
2188 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2189 struct yaffs_obj *parent,
2194 struct yaffs_obj *equiv_obj,
2195 const YCHAR * alias_str, u32 rdev)
2197 struct yaffs_obj *in;
2200 struct yaffs_dev *dev = parent->my_dev;
2202 /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
2203 if (yaffs_find_by_name(parent, name))
2206 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2207 str = yaffs_clone_str(alias_str);
2212 in = yaffs_new_obj(dev, -1, type);
2223 in->variant_type = type;
2225 in->yst_mode = mode;
2227 yaffs_attribs_init(in, gid, uid, rdev);
2229 in->n_data_chunks = 0;
2231 yaffs_set_obj_name(in, name);
2234 yaffs_add_obj_to_dir(parent, in);
2236 in->my_dev = parent->my_dev;
2239 case YAFFS_OBJECT_TYPE_SYMLINK:
2240 in->variant.symlink_variant.alias = str;
2242 case YAFFS_OBJECT_TYPE_HARDLINK:
2243 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2244 in->variant.hardlink_variant.equiv_id =
2246 list_add(&in->hard_links, &equiv_obj->hard_links);
2248 case YAFFS_OBJECT_TYPE_FILE:
2249 case YAFFS_OBJECT_TYPE_DIRECTORY:
2250 case YAFFS_OBJECT_TYPE_SPECIAL:
2251 case YAFFS_OBJECT_TYPE_UNKNOWN:
2256 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2257 /* Could not create the object header, fail the creation */
2262 yaffs_update_parent(parent);
2268 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2269 const YCHAR * name, u32 mode, u32 uid,
2272 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2273 uid, gid, NULL, NULL, 0);
2276 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
2277 u32 mode, u32 uid, u32 gid)
2279 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2280 mode, uid, gid, NULL, NULL, 0);
2283 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2284 const YCHAR * name, u32 mode, u32 uid,
2287 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2288 uid, gid, NULL, NULL, rdev);
2291 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2292 const YCHAR * name, u32 mode, u32 uid,
2293 u32 gid, const YCHAR * alias)
2295 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2296 uid, gid, NULL, alias, 0);
2299 /* yaffs_link_obj returns the object id of the equivalent object.*/
2300 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2301 struct yaffs_obj *equiv_obj)
2303 /* Get the real object in case we were fed a hard link as an equivalent object */
2304 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2306 if (yaffs_create_obj
2307 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2308 equiv_obj, NULL, 0)) {
2318 /*------------------------- Block Management and Page Allocation ----------------*/
2320 static int yaffs_init_blocks(struct yaffs_dev *dev)
2322 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2324 dev->block_info = NULL;
2325 dev->chunk_bits = NULL;
2327 dev->alloc_block = -1; /* force it to get a new one */
2329 /* If the first allocation strategy fails, thry the alternate one */
2331 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2332 if (!dev->block_info) {
2334 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2335 dev->block_info_alt = 1;
2337 dev->block_info_alt = 0;
2340 if (dev->block_info) {
2341 /* Set up dynamic blockinfo stuff. Round up bytes. */
2342 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2344 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2345 if (!dev->chunk_bits) {
2347 vmalloc(dev->chunk_bit_stride * n_blocks);
2348 dev->chunk_bits_alt = 1;
2350 dev->chunk_bits_alt = 0;
2354 if (dev->block_info && dev->chunk_bits) {
2355 memset(dev->block_info, 0,
2356 n_blocks * sizeof(struct yaffs_block_info));
2357 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2364 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2366 if (dev->block_info_alt && dev->block_info)
2367 vfree(dev->block_info);
2368 else if (dev->block_info)
2369 kfree(dev->block_info);
2371 dev->block_info_alt = 0;
2373 dev->block_info = NULL;
2375 if (dev->chunk_bits_alt && dev->chunk_bits)
2376 vfree(dev->chunk_bits);
2377 else if (dev->chunk_bits)
2378 kfree(dev->chunk_bits);
2379 dev->chunk_bits_alt = 0;
2380 dev->chunk_bits = NULL;
2383 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2385 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2389 /* If the block is still healthy erase it and mark as clean.
2390 * If the block has had a data failure, then retire it.
2393 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2394 (TSTR("yaffs_block_became_dirty block %d state %d %s" TENDSTR),
2395 block_no, bi->block_state,
2396 (bi->needs_retiring) ? "needs retiring" : ""));
2398 yaffs2_clear_oldest_dirty_seq(dev, bi);
2400 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2402 /* If this is the block being garbage collected then stop gc'ing this block */
2403 if (block_no == dev->gc_block)
2406 /* If this block is currently the best candidate for gc then drop as a candidate */
2407 if (block_no == dev->gc_dirtiest) {
2408 dev->gc_dirtiest = 0;
2409 dev->gc_pages_in_use = 0;
2412 if (!bi->needs_retiring) {
2413 yaffs2_checkpt_invalidate(dev);
2414 erased_ok = yaffs_erase_block(dev, block_no);
2416 dev->n_erase_failures++;
2417 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2418 (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
2423 ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
2424 || !yaffs_skip_verification(dev))) {
2426 for (i = 0; i < dev->param.chunks_per_block; i++) {
2427 if (!yaffs_check_chunk_erased
2428 (dev, block_no * dev->param.chunks_per_block + i)) {
2429 T(YAFFS_TRACE_ERROR,
2431 (">>Block %d erasure supposedly OK, but chunk %d not erased"
2432 TENDSTR), block_no, i));
2438 /* Clean it up... */
2439 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2441 dev->n_erased_blocks++;
2442 bi->pages_in_use = 0;
2443 bi->soft_del_pages = 0;
2444 bi->has_shrink_hdr = 0;
2445 bi->skip_erased_check = 1; /* This is clean, so no need to check */
2446 bi->gc_prioritise = 0;
2447 yaffs_clear_chunk_bits(dev, block_no);
2449 T(YAFFS_TRACE_ERASE,
2450 (TSTR("Erased block %d" TENDSTR), block_no));
2452 dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */
2454 yaffs_retire_block(dev, block_no);
2455 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2456 (TSTR("**>> Block %d retired" TENDSTR), block_no));
2462 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2467 int ret_val = YAFFS_OK;
2469 int is_checkpt_block;
2473 int chunks_before = yaffs_get_erased_chunks(dev);
2476 struct yaffs_ext_tags tags;
2478 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2480 struct yaffs_obj *object;
2482 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2484 T(YAFFS_TRACE_TRACING,
2486 ("Collecting block %d, in use %d, shrink %d, whole_block %d"
2487 TENDSTR), block, bi->pages_in_use, bi->has_shrink_hdr,
2490 /*yaffs_verify_free_chunks(dev); */
2492 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2493 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2495 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2497 dev->gc_disable = 1;
2499 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2500 T(YAFFS_TRACE_TRACING,
2502 ("Collecting block %d that has no chunks in use" TENDSTR),
2504 yaffs_block_became_dirty(dev, block);
2507 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2509 yaffs_verify_blk(dev, bi, block);
2511 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2512 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2514 for ( /* init already done */ ;
2515 ret_val == YAFFS_OK &&
2516 dev->gc_chunk < dev->param.chunks_per_block &&
2517 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2518 max_copies > 0; dev->gc_chunk++, old_chunk++) {
2519 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2521 /* This page is in use and might need to be copied off */
2527 yaffs_init_tags(&tags);
2529 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2532 object = yaffs_find_by_number(dev, tags.obj_id);
2534 T(YAFFS_TRACE_GC_DETAIL,
2536 ("Collecting chunk in block %d, %d %d %d "
2537 TENDSTR), dev->gc_chunk, tags.obj_id,
2538 tags.chunk_id, tags.n_bytes));
2540 if (object && !yaffs_skip_verification(dev)) {
2541 if (tags.chunk_id == 0)
2544 else if (object->soft_del)
2545 matching_chunk = old_chunk; /* Defeat the test */
2548 yaffs_find_chunk_in_file
2549 (object, tags.chunk_id,
2552 if (old_chunk != matching_chunk)
2553 T(YAFFS_TRACE_ERROR,
2555 ("gc: page in gc mismatch: %d %d %d %d"
2556 TENDSTR), old_chunk,
2557 matching_chunk, tags.obj_id,
2563 T(YAFFS_TRACE_ERROR,
2565 ("page %d in gc has no object: %d %d %d "
2566 TENDSTR), old_chunk,
2567 tags.obj_id, tags.chunk_id,
2573 object->soft_del && tags.chunk_id != 0) {
2574 /* Data chunk in a soft deleted file, throw it away
2575 * It's a soft deleted data chunk,
2576 * No need to copy this, just forget about it and
2577 * fix up the object.
2580 /* Free chunks already includes softdeleted chunks.
2581 * How ever this chunk is going to soon be really deleted
2582 * which will increment free chunks.
2583 * We have to decrement free chunks so this works out properly.
2585 dev->n_free_chunks--;
2586 bi->soft_del_pages--;
2588 object->n_data_chunks--;
2590 if (object->n_data_chunks <= 0) {
2591 /* remeber to clean up the object */
2592 dev->gc_cleanup_list[dev->
2599 /* Todo object && object->deleted && object->n_data_chunks == 0 */
2600 /* Deleted object header with no data chunks.
2601 * Can be discarded and the file deleted.
2603 object->hdr_chunk = 0;
2604 yaffs_free_tnode(object->my_dev,
2606 variant.file_variant.
2608 object->variant.file_variant.top = NULL;
2609 yaffs_generic_obj_del(object);
2611 } else if (object) {
2612 /* It's either a data chunk in a live file or
2613 * an ObjectHeader, so we're interested in it.
2614 * NB Need to keep the ObjectHeaders of deleted files
2615 * until the whole file has been deleted off
2617 tags.serial_number++;
2621 if (tags.chunk_id == 0) {
2622 /* It is an object Id,
2623 * We need to nuke the shrinkheader flags first
2624 * Also need to clean up shadowing.
2625 * We no longer want the shrink_header flag since its work is done
2626 * and if it is left in place it will mess up scanning.
2629 struct yaffs_obj_hdr *oh;
2630 oh = (struct yaffs_obj_hdr *)
2634 tags.extra_is_shrink = 0;
2636 oh->shadows_obj = 0;
2637 oh->inband_shadowed_obj_id = 0;
2638 tags.extra_shadows = 0;
2640 /* Update file size */
2641 if (object->variant_type ==
2642 YAFFS_OBJECT_TYPE_FILE) {
2651 yaffs_verify_oh(object, oh,
2654 yaffs_write_new_chunk(dev,
2661 yaffs_write_new_chunk(dev,
2667 if (new_chunk < 0) {
2668 ret_val = YAFFS_FAIL;
2671 /* Ok, now fix up the Tnodes etc. */
2673 if (tags.chunk_id == 0) {
2680 /* It's a data chunk */
2682 ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
2687 if (ret_val == YAFFS_OK)
2688 yaffs_chunk_del(dev, old_chunk,
2689 mark_flash, __LINE__);
2694 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2698 yaffs_verify_collected_blk(dev, bi, block);
2700 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2702 * The gc did not complete. Set block state back to FULL
2703 * because checkpointing does not restore gc.
2705 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2707 /* The gc completed. */
2708 /* Do any required cleanups */
2709 for (i = 0; i < dev->n_clean_ups; i++) {
2710 /* Time to delete the file too */
2712 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2714 yaffs_free_tnode(dev,
2717 object->variant.file_variant.top = NULL;
2720 ("yaffs: About to finally delete object %d"
2721 TENDSTR), object->obj_id));
2722 yaffs_generic_obj_del(object);
2723 object->my_dev->n_deleted_files--;
2728 chunks_after = yaffs_get_erased_chunks(dev);
2729 if (chunks_before >= chunks_after) {
2732 ("gc did not increase free chunks before %d after %d"
2733 TENDSTR), chunks_before, chunks_after));
2737 dev->n_clean_ups = 0;
2740 dev->gc_disable = 0;
2746 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2747 * for garbage collection.
2750 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2751 int aggressive, int background)
2755 unsigned selected = 0;
2756 int prioritised = 0;
2757 int prioritised_exist = 0;
2758 struct yaffs_block_info *bi;
2761 /* First let's see if we need to grab a prioritised block */
2762 if (dev->has_pending_prioritised_gc && !aggressive) {
2763 dev->gc_dirtiest = 0;
2764 bi = dev->block_info;
2765 for (i = dev->internal_start_block;
2766 i <= dev->internal_end_block && !selected; i++) {
2768 if (bi->gc_prioritise) {
2769 prioritised_exist = 1;
2770 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2771 yaffs_block_ok_for_gc(dev, bi)) {
2780 * If there is a prioritised block and none was selected then
2781 * this happened because there is at least one old dirty block gumming
2782 * up the works. Let's gc the oldest dirty block.
2785 if (prioritised_exist &&
2786 !selected && dev->oldest_dirty_block > 0)
2787 selected = dev->oldest_dirty_block;
2789 if (!prioritised_exist) /* None found, so we can clear this */
2790 dev->has_pending_prioritised_gc = 0;
2793 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2795 * else (we're doing a leasurely gc), then we only bother to do this if the
2796 * block has only a few pages in use.
2802 dev->internal_end_block - dev->internal_start_block + 1;
2804 threshold = dev->param.chunks_per_block;
2805 iterations = n_blocks;
2810 max_threshold = dev->param.chunks_per_block / 2;
2812 max_threshold = dev->param.chunks_per_block / 8;
2814 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2815 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2817 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2818 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2819 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2820 if (threshold > max_threshold)
2821 threshold = max_threshold;
2823 iterations = n_blocks / 16 + 1;
2824 if (iterations > 100)
2830 (dev->gc_dirtiest < 1 ||
2831 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
2832 dev->gc_block_finder++;
2833 if (dev->gc_block_finder < dev->internal_start_block ||
2834 dev->gc_block_finder > dev->internal_end_block)
2835 dev->gc_block_finder =
2836 dev->internal_start_block;
2838 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2840 pages_used = bi->pages_in_use - bi->soft_del_pages;
2842 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2843 pages_used < dev->param.chunks_per_block &&
2844 (dev->gc_dirtiest < 1
2845 || pages_used < dev->gc_pages_in_use)
2846 && yaffs_block_ok_for_gc(dev, bi)) {
2847 dev->gc_dirtiest = dev->gc_block_finder;
2848 dev->gc_pages_in_use = pages_used;
2852 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2853 selected = dev->gc_dirtiest;
2857 * If nothing has been selected for a while, try selecting the oldest dirty
2858 * because that's gumming up the works.
2861 if (!selected && dev->param.is_yaffs2 &&
2862 dev->gc_not_done >= (background ? 10 : 20)) {
2863 yaffs2_find_oldest_dirty_seq(dev);
2864 if (dev->oldest_dirty_block > 0) {
2865 selected = dev->oldest_dirty_block;
2866 dev->gc_dirtiest = selected;
2867 dev->oldest_dirty_gc_count++;
2868 bi = yaffs_get_block_info(dev, selected);
2869 dev->gc_pages_in_use =
2870 bi->pages_in_use - bi->soft_del_pages;
2872 dev->gc_not_done = 0;
2879 ("GC Selected block %d with %d free, prioritised:%d"
2881 dev->param.chunks_per_block - dev->gc_pages_in_use,
2888 dev->gc_dirtiest = 0;
2889 dev->gc_pages_in_use = 0;
2890 dev->gc_not_done = 0;
2891 if (dev->refresh_skip > 0)
2892 dev->refresh_skip--;
2897 ("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s"
2898 TENDSTR), dev->gc_block_finder, dev->gc_not_done, threshold,
2899 dev->gc_dirtiest, dev->gc_pages_in_use,
2900 dev->oldest_dirty_block, background ? " bg" : ""));
2906 /* New garbage collector
2907 * If we're very low on erased blocks then we do aggressive garbage collection
2908 * otherwise we do "leasurely" garbage collection.
2909 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2910 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2912 * The idea is to help clear out space in a more spread-out manner.
2913 * Dunno if it really does anything useful.
2915 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2918 int gc_ok = YAFFS_OK;
2922 int checkpt_block_adjust;
2924 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2927 if (dev->gc_disable) {
2928 /* Bail out so we don't get recursive gc */
2932 /* This loop should pass the first time.
2933 * We'll only see looping here if the collection does not increase space.
2939 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2942 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2944 dev->n_erased_blocks * dev->param.chunks_per_block;
2946 /* If we need a block soon then do aggressive gc. */
2947 if (dev->n_erased_blocks < min_erased)
2951 && erased_chunks > (dev->n_free_chunks / 4))
2954 if (dev->gc_skip > 20)
2956 if (erased_chunks < dev->n_free_chunks / 2 ||
2957 dev->gc_skip < 1 || background)
2967 /* If we don't already have a block being gc'd then see if we should start another */
2969 if (dev->gc_block < 1 && !aggressive) {
2970 dev->gc_block = yaffs2_find_refresh_block(dev);
2972 dev->n_clean_ups = 0;
2974 if (dev->gc_block < 1) {
2976 yaffs_find_gc_block(dev, aggressive, background);
2978 dev->n_clean_ups = 0;
2981 if (dev->gc_block > 0) {
2984 dev->passive_gc_count++;
2988 ("yaffs: GC n_erased_blocks %d aggressive %d"
2989 TENDSTR), dev->n_erased_blocks, aggressive));
2991 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2994 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
2995 && dev->gc_block > 0) {
2998 ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d"
2999 TENDSTR), dev->n_erased_blocks, max_tries,
3002 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
3003 (dev->gc_block > 0) && (max_tries < 2));
3005 return aggressive ? gc_ok : YAFFS_OK;
3010 * Garbage collects. Intended to be called from a background thread.
3011 * Returns non-zero if at least half the free chunks are erased.
3013 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
3015 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
3017 T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR), urgency));
3019 yaffs_check_gc(dev, 1);
3020 return erased_chunks > dev->n_free_chunks / 2;
3023 /*-------------------- Data file manipulation -----------------*/
3025 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
3027 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
3029 if (nand_chunk >= 0)
3030 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
3033 T(YAFFS_TRACE_NANDACCESS,
3034 (TSTR("Chunk %d not found zero instead" TENDSTR),
3036 /* get sane (zero) data if you read a hole */
3037 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
3043 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
3048 struct yaffs_ext_tags tags;
3049 struct yaffs_block_info *bi;
3055 block = chunk_id / dev->param.chunks_per_block;
3056 page = chunk_id % dev->param.chunks_per_block;
3058 if (!yaffs_check_chunk_bit(dev, block, page))
3059 T(YAFFS_TRACE_VERIFY,
3060 (TSTR("Deleting invalid chunk %d" TENDSTR), chunk_id));
3062 bi = yaffs_get_block_info(dev, block);
3064 yaffs2_update_oldest_dirty_seq(dev, block, bi);
3066 T(YAFFS_TRACE_DELETION,
3067 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
3069 if (!dev->param.is_yaffs2 && mark_flash &&
3070 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
3072 yaffs_init_tags(&tags);
3074 tags.is_deleted = 1;
3076 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3077 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3079 dev->n_unmarked_deletions++;
3082 /* Pull out of the management area.
3083 * If the whole block became dirty, this will kick off an erasure.
3085 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
3086 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
3087 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3088 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
3089 dev->n_free_chunks++;
3091 yaffs_clear_chunk_bit(dev, block, page);
3095 if (bi->pages_in_use == 0 &&
3096 !bi->has_shrink_hdr &&
3097 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
3098 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3099 yaffs_block_became_dirty(dev, block);
3106 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3107 const u8 * buffer, int n_bytes, int use_reserve)
3109 /* Find old chunk Need to do this to get serial number
3110 * Write new one and patch into tree.
3111 * Invalidate old tags.
3115 struct yaffs_ext_tags prev_tags;
3118 struct yaffs_ext_tags new_tags;
3120 struct yaffs_dev *dev = in->my_dev;
3122 yaffs_check_gc(dev, 0);
3124 /* Get the previous chunk at this location in the file if it exists.
3125 * If it does not exist then put a zero into the tree. This creates
3126 * the tnode now, rather than later when it is harder to clean up.
3128 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3129 if (prev_chunk_id < 1 &&
3130 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3133 /* Set up new tags */
3134 yaffs_init_tags(&new_tags);
3136 new_tags.chunk_id = inode_chunk;
3137 new_tags.obj_id = in->obj_id;
3138 new_tags.serial_number =
3139 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3140 new_tags.n_bytes = n_bytes;
3142 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3143 T(YAFFS_TRACE_ERROR,
3144 (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR),
3150 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3152 if (new_chunk_id > 0) {
3153 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3155 if (prev_chunk_id > 0)
3156 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3158 yaffs_verify_file_sane(in);
3160 return new_chunk_id;
3166 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3167 const YCHAR * name, const void *value, int size,
3170 struct yaffs_xattr_mod xmod;
3179 xmod.result = -ENOSPC;
3181 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3189 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3190 struct yaffs_xattr_mod *xmod)
3193 int x_offs = sizeof(struct yaffs_obj_hdr);
3194 struct yaffs_dev *dev = obj->my_dev;
3195 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3197 char *x_buffer = buffer + x_offs;
3201 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3202 xmod->size, xmod->flags);
3204 retval = nval_del(x_buffer, x_size, xmod->name);
3206 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3207 obj->xattr_known = 1;
3209 xmod->result = retval;
3214 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
3215 void *value, int size)
3217 char *buffer = NULL;
3219 struct yaffs_ext_tags tags;
3220 struct yaffs_dev *dev = obj->my_dev;
3221 int x_offs = sizeof(struct yaffs_obj_hdr);
3222 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3228 if (obj->hdr_chunk < 1)
3231 /* If we know that the object has no xattribs then don't do all the
3232 * reading and parsing.
3234 if (obj->xattr_known && !obj->has_xattr) {
3241 buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
3246 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3248 if (result != YAFFS_OK)
3251 x_buffer = buffer + x_offs;
3253 if (!obj->xattr_known) {
3254 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3255 obj->xattr_known = 1;
3259 retval = nval_get(x_buffer, x_size, name, value, size);
3261 retval = nval_list(x_buffer, x_size, value, size);
3263 yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
3267 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3268 const void *value, int size, int flags)
3270 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3273 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3275 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3278 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3281 return yaffs_do_xattrib_fetch(obj, name, value, size);
3284 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3286 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3289 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3292 struct yaffs_obj_hdr *oh;
3293 struct yaffs_dev *dev;
3294 struct yaffs_ext_tags tags;
3296 int alloc_failed = 0;
3303 if (in->lazy_loaded && in->hdr_chunk > 0) {
3304 in->lazy_loaded = 0;
3305 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
3308 yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
3310 oh = (struct yaffs_obj_hdr *)chunk_data;
3312 in->yst_mode = oh->yst_mode;
3313 yaffs_load_attribs(in, oh);
3314 yaffs_set_obj_name_from_oh(in, oh);
3316 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3317 in->variant.symlink_variant.alias =
3318 yaffs_clone_str(oh->alias);
3319 if (!in->variant.symlink_variant.alias)
3320 alloc_failed = 1; /* Not returned to caller */
3323 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
3327 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
3328 const YCHAR * oh_name, int buff_size)
3330 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3331 if (dev->param.auto_unicode) {
3333 /* It is an ASCII name, so do an ASCII to unicode conversion */
3334 const char *ascii_oh_name = (const char *)oh_name;
3335 int n = buff_size - 1;
3336 while (n > 0 && *ascii_oh_name) {
3337 *name = *ascii_oh_name;
3343 strncpy(name, oh_name + 1, buff_size - 1);
3349 strncpy(name, oh_name, buff_size - 1);
3353 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
3356 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3361 if (dev->param.auto_unicode) {
3366 /* Figure out if the name will fit in ascii character set */
3367 while (is_ascii && *w) {
3374 /* It is an ASCII name, so do a unicode to ascii conversion */
3375 char *ascii_oh_name = (char *)oh_name;
3376 int n = YAFFS_MAX_NAME_LENGTH - 1;
3377 while (n > 0 && *name) {
3378 *ascii_oh_name = *name;
3384 /* It is a unicode name, so save starting at the second YCHAR */
3386 strncpy(oh_name + 1, name,
3387 YAFFS_MAX_NAME_LENGTH - 2);
3393 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3398 /* UpdateObjectHeader updates the header on NAND for an object.
3399 * If name is not NULL, then that new name is used.
3401 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
3402 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3405 struct yaffs_block_info *bi;
3407 struct yaffs_dev *dev = in->my_dev;
3414 struct yaffs_ext_tags new_tags;
3415 struct yaffs_ext_tags old_tags;
3416 const YCHAR *alias = NULL;
3419 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3421 struct yaffs_obj_hdr *oh = NULL;
3423 strcpy(old_name, _Y("silly old name"));
3425 if (!in->fake || in == dev->root_dir || /* The root_dir should also be saved */
3428 yaffs_check_gc(dev, 0);
3429 yaffs_check_obj_details_loaded(in);
3431 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
3432 oh = (struct yaffs_obj_hdr *)buffer;
3434 prev_chunk_id = in->hdr_chunk;
3436 if (prev_chunk_id > 0) {
3437 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3440 yaffs_verify_oh(in, oh, &old_tags, 0);
3442 memcpy(old_name, oh->name, sizeof(oh->name));
3443 memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
3445 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
3448 oh->type = in->variant_type;
3449 oh->yst_mode = in->yst_mode;
3450 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3452 yaffs_load_attribs_oh(oh, in);
3455 oh->parent_obj_id = in->parent->obj_id;
3457 oh->parent_obj_id = 0;
3459 if (name && *name) {
3460 memset(oh->name, 0, sizeof(oh->name));
3461 yaffs_load_oh_from_name(dev, oh->name, name);
3462 } else if (prev_chunk_id > 0) {
3463 memcpy(oh->name, old_name, sizeof(oh->name));
3465 memset(oh->name, 0, sizeof(oh->name));
3468 oh->is_shrink = is_shrink;
3470 switch (in->variant_type) {
3471 case YAFFS_OBJECT_TYPE_UNKNOWN:
3472 /* Should not happen */
3474 case YAFFS_OBJECT_TYPE_FILE:
3476 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
3477 || oh->parent_obj_id ==
3478 YAFFS_OBJECTID_UNLINKED) ? 0 : in->
3479 variant.file_variant.file_size;
3481 case YAFFS_OBJECT_TYPE_HARDLINK:
3482 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3484 case YAFFS_OBJECT_TYPE_SPECIAL:
3487 case YAFFS_OBJECT_TYPE_DIRECTORY:
3490 case YAFFS_OBJECT_TYPE_SYMLINK:
3491 alias = in->variant.symlink_variant.alias;
3493 alias = _Y("no alias");
3494 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3495 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3499 /* process any xattrib modifications */
3501 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3504 yaffs_init_tags(&new_tags);
3506 new_tags.chunk_id = 0;
3507 new_tags.obj_id = in->obj_id;
3508 new_tags.serial_number = in->serial;
3510 /* Add extra info for file header */
3512 new_tags.extra_available = 1;
3513 new_tags.extra_parent_id = oh->parent_obj_id;
3514 new_tags.extra_length = oh->file_size;
3515 new_tags.extra_is_shrink = oh->is_shrink;
3516 new_tags.extra_equiv_id = oh->equiv_id;
3517 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3518 new_tags.extra_obj_type = in->variant_type;
3520 yaffs_verify_oh(in, oh, &new_tags, 1);
3522 /* Create new chunk in NAND */
3524 yaffs_write_new_chunk(dev, buffer, &new_tags,
3525 (prev_chunk_id > 0) ? 1 : 0);
3527 if (new_chunk_id >= 0) {
3529 in->hdr_chunk = new_chunk_id;
3531 if (prev_chunk_id > 0) {
3532 yaffs_chunk_del(dev, prev_chunk_id, 1,
3536 if (!yaffs_obj_cache_dirty(in))
3539 /* If this was a shrink, then mark the block that the chunk lives on */
3541 bi = yaffs_get_block_info(in->my_dev,
3545 bi->has_shrink_hdr = 1;
3550 ret_val = new_chunk_id;
3555 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3560 /*--------------------- File read/write ------------------------
3561 * Read and write have very similar structures.
3562 * In general the read/write has three parts to it
3563 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3564 * Some complete chunks
3565 * An incomplete chunk to end off with
3567 * Curve-balls: the first chunk might also be the last chunk.
3570 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3578 struct yaffs_cache *cache;
3580 struct yaffs_dev *dev;
3585 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3586 /* start = offset % dev->data_bytes_per_chunk; */
3587 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3590 /* OK now check for the curveball where the start and end are in
3593 if ((start + n) < dev->data_bytes_per_chunk)
3596 n_copy = dev->data_bytes_per_chunk - start;
3598 cache = yaffs_find_chunk_cache(in, chunk);
3600 /* If the chunk is already in the cache or it is less than a whole chunk
3601 * or we're using inband tags then use the cache (if there is caching)
3602 * else bypass the cache.
3604 if (cache || n_copy != dev->data_bytes_per_chunk
3605 || dev->param.inband_tags) {
3606 if (dev->param.n_caches > 0) {
3608 /* If we can't find the data in the cache, then load it up. */
3612 yaffs_grab_chunk_cache(in->my_dev);
3614 cache->chunk_id = chunk;
3617 yaffs_rd_data_obj(in, chunk,
3622 yaffs_use_cache(dev, cache, 0);
3626 memcpy(buffer, &cache->data[start], n_copy);
3630 /* Read into the local buffer then copy.. */
3633 yaffs_get_temp_buffer(dev, __LINE__);
3634 yaffs_rd_data_obj(in, chunk, local_buffer);
3636 memcpy(buffer, &local_buffer[start], n_copy);
3638 yaffs_release_temp_buffer(dev, local_buffer,
3644 /* A full chunk. Read directly into the supplied buffer. */
3645 yaffs_rd_data_obj(in, chunk, buffer);
3659 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3660 int n_bytes, int write_trhrough)
3669 int start_write = offset;
3670 int chunk_written = 0;
3674 struct yaffs_dev *dev;
3678 while (n > 0 && chunk_written >= 0) {
3679 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3681 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3682 start >= dev->data_bytes_per_chunk) {
3683 T(YAFFS_TRACE_ERROR,
3685 ("AddrToChunk of offset %d gives chunk %d start %d"
3686 TENDSTR), (int)offset, chunk, start));
3688 chunk++; /* File pos to chunk in file offset */
3690 /* OK now check for the curveball where the start and end are in
3694 if ((start + n) < dev->data_bytes_per_chunk) {
3697 /* Now folks, to calculate how many bytes to write back....
3698 * If we're overwriting and not writing to then end of file then
3699 * we need to write back as much as was there before.
3702 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3704 if (chunk_start > in->variant.file_variant.file_size)
3705 n_bytes_read = 0; /* Past end of file */
3708 in->variant.file_variant.file_size -
3711 if (n_bytes_read > dev->data_bytes_per_chunk)
3712 n_bytes_read = dev->data_bytes_per_chunk;
3716 (start + n)) ? n_bytes_read : (start + n);
3719 || n_writeback > dev->data_bytes_per_chunk)
3723 n_copy = dev->data_bytes_per_chunk - start;
3724 n_writeback = dev->data_bytes_per_chunk;
3727 if (n_copy != dev->data_bytes_per_chunk
3728 || dev->param.inband_tags) {
3729 /* An incomplete start or end chunk (or maybe both start and end chunk),
3730 * or we're using inband tags, so we want to use the cache buffers.
3732 if (dev->param.n_caches > 0) {
3733 struct yaffs_cache *cache;
3734 /* If we can't find the data in the cache, then load the cache */
3735 cache = yaffs_find_chunk_cache(in, chunk);
3738 && yaffs_check_alloc_available(dev, 1)) {
3739 cache = yaffs_grab_chunk_cache(dev);
3741 cache->chunk_id = chunk;
3744 yaffs_rd_data_obj(in, chunk,
3748 !yaffs_check_alloc_available(dev,
3750 /* Drop the cache if it was a read cache item and
3751 * no space check has been made for it.
3757 yaffs_use_cache(dev, cache, 1);
3760 memcpy(&cache->data[start], buffer,
3764 cache->n_bytes = n_writeback;
3766 if (write_trhrough) {
3777 chunk_written = -1; /* fail the write */
3780 /* An incomplete start or end chunk (or maybe both start and end chunk)
3781 * Read into the local buffer then copy, then copy over and write back.
3785 yaffs_get_temp_buffer(dev, __LINE__);
3787 yaffs_rd_data_obj(in, chunk, local_buffer);
3789 memcpy(&local_buffer[start], buffer, n_copy);
3792 yaffs_wr_data_obj(in, chunk,
3796 yaffs_release_temp_buffer(dev, local_buffer,
3802 /* A full chunk. Write directly from the supplied buffer. */
3805 yaffs_wr_data_obj(in, chunk, buffer,
3806 dev->data_bytes_per_chunk, 0);
3808 /* Since we've overwritten the cached data, we better invalidate it. */
3809 yaffs_invalidate_chunk_cache(in, chunk);
3812 if (chunk_written >= 0) {
3821 /* Update file object */
3823 if ((start_write + n_done) > in->variant.file_variant.file_size)
3824 in->variant.file_variant.file_size = (start_write + n_done);
3831 int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3832 int n_bytes, int write_trhrough)
3834 yaffs2_handle_hole(in, offset);
3835 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3838 /* ---------------------- File resizing stuff ------------------ */
3840 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3843 struct yaffs_dev *dev = in->my_dev;
3844 int old_size = in->variant.file_variant.file_size;
3846 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3848 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3849 dev->data_bytes_per_chunk;
3853 /* Delete backwards so that we don't end up with holes if
3854 * power is lost part-way through the operation.
3856 for (i = last_del; i >= start_del; i--) {
3857 /* NB this could be optimised somewhat,
3858 * eg. could retrieve the tags and write them without
3859 * using yaffs_chunk_del
3862 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3865 (dev->internal_start_block *
3866 dev->param.chunks_per_block)
3868 ((dev->internal_end_block +
3869 1) * dev->param.chunks_per_block)) {
3870 T(YAFFS_TRACE_ALWAYS,
3872 ("Found daft chunk_id %d for %d" TENDSTR),
3875 in->n_data_chunks--;
3876 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3883 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3887 struct yaffs_dev *dev = obj->my_dev;
3889 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3891 yaffs_prune_chunks(obj, new_size);
3893 if (new_partial != 0) {
3894 int last_chunk = 1 + new_full;
3895 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3897 /* Got to read and rewrite the last chunk with its new size and zero pad */
3898 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3899 memset(local_buffer + new_partial, 0,
3900 dev->data_bytes_per_chunk - new_partial);
3902 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3905 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3908 obj->variant.file_variant.file_size = new_size;
3910 yaffs_prune_tree(dev, &obj->variant.file_variant);
3913 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3915 struct yaffs_dev *dev = in->my_dev;
3916 int old_size = in->variant.file_variant.file_size;
3918 yaffs_flush_file_cache(in);
3919 yaffs_invalidate_whole_cache(in);
3921 yaffs_check_gc(dev, 0);
3923 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3926 if (new_size == old_size)
3929 if (new_size > old_size) {
3930 yaffs2_handle_hole(in, new_size);
3931 in->variant.file_variant.file_size = new_size;
3933 /* new_size < old_size */
3934 yaffs_resize_file_down(in, new_size);
3937 /* Write a new object header to reflect the resize.
3938 * show we've shrunk the file, if need be
3939 * Do this only if the file is not in the deleted directories
3940 * and is not shadowed.
3944 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3945 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3946 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3951 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3955 yaffs_flush_file_cache(in);
3956 if (data_sync) /* Only sync data */
3960 yaffs_load_current_time(in, 0, 0);
3962 ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3963 0) ? YAFFS_OK : YAFFS_FAIL;
3974 /* yaffs_del_file deletes the whole file data
3975 * and the inode associated with the file.
3976 * It does not delete the links associated with the file.
3978 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3983 struct yaffs_dev *dev = in->my_dev;
3990 yaffs_change_obj_name(in, in->my_dev->del_dir,
3991 _Y("deleted"), 0, 0);
3992 T(YAFFS_TRACE_TRACING,
3993 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
3996 in->my_dev->n_deleted_files++;
3997 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3998 yaffs_resize_file(in, 0);
3999 yaffs_soft_del_file(in);
4002 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
4003 _Y("unlinked"), 0, 0);
4009 int yaffs_del_file(struct yaffs_obj *in)
4011 int ret_val = YAFFS_OK;
4012 int deleted; /* Need to cache value on stack if in is freed */
4013 struct yaffs_dev *dev = in->my_dev;
4015 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
4016 yaffs_resize_file(in, 0);
4018 if (in->n_data_chunks > 0) {
4019 /* Use soft deletion if there is data in the file.
4020 * That won't be the case if it has been resized to zero.
4023 ret_val = yaffs_unlink_file_if_needed(in);
4025 deleted = in->deleted;
4027 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
4030 in->my_dev->n_deleted_files++;
4031 yaffs_soft_del_file(in);
4033 return deleted ? YAFFS_OK : YAFFS_FAIL;
4035 /* The file has no data chunks so we toss it immediately */
4036 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
4037 in->variant.file_variant.top = NULL;
4038 yaffs_generic_obj_del(in);
4044 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
4047 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
4048 !(list_empty(&obj->variant.dir_variant.children));
4051 static int yaffs_del_dir(struct yaffs_obj *obj)
4053 /* First check that the directory is empty. */
4054 if (yaffs_is_non_empty_dir(obj))
4057 return yaffs_generic_obj_del(obj);
4060 static int yaffs_del_symlink(struct yaffs_obj *in)
4062 if (in->variant.symlink_variant.alias)
4063 kfree(in->variant.symlink_variant.alias);
4064 in->variant.symlink_variant.alias = NULL;
4066 return yaffs_generic_obj_del(in);
4069 static int yaffs_del_link(struct yaffs_obj *in)
4071 /* remove this hardlink from the list assocaited with the equivalent
4074 list_del_init(&in->hard_links);
4075 return yaffs_generic_obj_del(in);
4078 int yaffs_del_obj(struct yaffs_obj *obj)
4081 switch (obj->variant_type) {
4082 case YAFFS_OBJECT_TYPE_FILE:
4083 ret_val = yaffs_del_file(obj);
4085 case YAFFS_OBJECT_TYPE_DIRECTORY:
4086 if (!list_empty(&obj->variant.dir_variant.dirty)) {
4087 T(YAFFS_TRACE_BACKGROUND,
4089 ("Remove object %d from dirty directories" TENDSTR),
4091 list_del_init(&obj->variant.dir_variant.dirty);
4093 return yaffs_del_dir(obj);
4095 case YAFFS_OBJECT_TYPE_SYMLINK:
4096 ret_val = yaffs_del_symlink(obj);
4098 case YAFFS_OBJECT_TYPE_HARDLINK:
4099 ret_val = yaffs_del_link(obj);
4101 case YAFFS_OBJECT_TYPE_SPECIAL:
4102 ret_val = yaffs_generic_obj_del(obj);
4104 case YAFFS_OBJECT_TYPE_UNKNOWN:
4106 break; /* should not happen. */
4112 static int yaffs_unlink_worker(struct yaffs_obj *obj)
4121 yaffs_update_parent(obj->parent);
4123 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4124 return yaffs_del_link(obj);
4125 } else if (!list_empty(&obj->hard_links)) {
4126 /* Curve ball: We're unlinking an object that has a hardlink.
4128 * This problem arises because we are not strictly following
4129 * The Linux link/inode model.
4131 * We can't really delete the object.
4132 * Instead, we do the following:
4133 * - Select a hardlink.
4134 * - Unhook it from the hard links
4135 * - Move it from its parent directory (so that the rename can work)
4136 * - Rename the object to the hardlink's name.
4137 * - Delete the hardlink
4140 struct yaffs_obj *hl;
4141 struct yaffs_obj *parent;
4143 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4145 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
4148 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4149 parent = hl->parent;
4151 list_del_init(&hl->hard_links);
4153 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4155 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4157 if (ret_val == YAFFS_OK)
4158 ret_val = yaffs_generic_obj_del(hl);
4162 } else if (del_now) {
4163 switch (obj->variant_type) {
4164 case YAFFS_OBJECT_TYPE_FILE:
4165 return yaffs_del_file(obj);
4167 case YAFFS_OBJECT_TYPE_DIRECTORY:
4168 list_del_init(&obj->variant.dir_variant.dirty);
4169 return yaffs_del_dir(obj);
4171 case YAFFS_OBJECT_TYPE_SYMLINK:
4172 return yaffs_del_symlink(obj);
4174 case YAFFS_OBJECT_TYPE_SPECIAL:
4175 return yaffs_generic_obj_del(obj);
4177 case YAFFS_OBJECT_TYPE_HARDLINK:
4178 case YAFFS_OBJECT_TYPE_UNKNOWN:
4182 } else if (yaffs_is_non_empty_dir(obj)) {
4185 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4186 _Y("unlinked"), 0, 0);
4190 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4193 if (obj && obj->unlink_allowed)
4194 return yaffs_unlink_worker(obj);
4200 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
4202 struct yaffs_obj *obj;
4204 obj = yaffs_find_by_name(dir, name);
4205 return yaffs_unlink_obj(obj);
4209 * If old_name is NULL then we take old_dir as the object to be renamed.
4211 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
4212 struct yaffs_obj *new_dir, const YCHAR * new_name)
4214 struct yaffs_obj *obj = NULL;
4215 struct yaffs_obj *existing_target = NULL;
4218 struct yaffs_dev *dev;
4220 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4222 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4225 dev = old_dir->my_dev;
4227 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4228 /* Special case for case insemsitive systems.
4229 * While look-up is case insensitive, the name isn't.
4230 * Therefore we might want to change x.txt to X.txt
4232 if (old_dir == new_dir &&
4233 old_name && new_name &&
4234 strcmp(old_name, new_name) == 0)
4238 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4239 YAFFS_MAX_NAME_LENGTH)
4244 obj = yaffs_find_by_name(old_dir, old_name);
4247 old_dir = obj->parent;
4251 if (obj && obj->rename_allowed) {
4253 /* Now do the handling for an existing target, if there is one */
4255 existing_target = yaffs_find_by_name(new_dir, new_name);
4256 if (yaffs_is_non_empty_dir(existing_target)){
4257 return YAFFS_FAIL; /* ENOTEMPTY */
4258 } else if (existing_target && existing_target != obj) {
4259 /* Nuke the target first, using shadowing,
4260 * but only if it isn't the same object.
4262 * Note we must disable gc otherwise it can mess up the shadowing.
4265 dev->gc_disable = 1;
4266 yaffs_change_obj_name(obj, new_dir, new_name, force,
4267 existing_target->obj_id);
4268 existing_target->is_shadowed = 1;
4269 yaffs_unlink_obj(existing_target);
4270 dev->gc_disable = 0;
4273 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4275 yaffs_update_parent(old_dir);
4276 if (new_dir != old_dir)
4277 yaffs_update_parent(new_dir);
4284 /*----------------------- Initialisation Scanning ---------------------- */
4286 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4287 int backward_scanning)
4289 struct yaffs_obj *obj;
4291 if (!backward_scanning) {
4292 /* Handle YAFFS1 forward scanning case
4293 * For YAFFS1 we always do the deletion
4297 /* Handle YAFFS2 case (backward scanning)
4298 * If the shadowed object exists then ignore.
4300 obj = yaffs_find_by_number(dev, obj_id);
4305 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4306 * We put it in unlinked dir to be cleaned up after the scanning
4309 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4312 obj->is_shadowed = 1;
4313 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4314 obj->variant.file_variant.shrink_size = 0;
4315 obj->valid = 1; /* So that we don't read any other info for this file */
4319 void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
4321 struct yaffs_obj *hl;
4322 struct yaffs_obj *in;
4326 hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
4328 in = yaffs_find_by_number(dev,
4330 hardlink_variant.equiv_id);
4333 /* Add the hardlink pointers */
4334 hl->variant.hardlink_variant.equiv_obj = in;
4335 list_add(&hl->hard_links, &in->hard_links);
4337 /* Todo Need to report/handle this better.
4338 * Got a problem... hardlink to a non-existant object
4340 hl->variant.hardlink_variant.equiv_obj = NULL;
4341 INIT_LIST_HEAD(&hl->hard_links);
4347 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4350 * Sort out state of unlinked and deleted objects after scanning.
4352 struct list_head *i;
4353 struct list_head *n;
4354 struct yaffs_obj *l;
4359 /* Soft delete all the unlinked files */
4360 list_for_each_safe(i, n,
4361 &dev->unlinked_dir->variant.dir_variant.children) {
4363 l = list_entry(i, struct yaffs_obj, siblings);
4368 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4370 l = list_entry(i, struct yaffs_obj, siblings);
4378 * This code iterates through all the objects making sure that they are rooted.
4379 * Any unrooted objects are re-rooted in lost+found.
4380 * An object needs to be in one of:
4381 * - Directly under deleted, unlinked
4382 * - Directly or indirectly under root.
4385 * This code assumes that we don't ever change the current relationships between
4387 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4388 * lost-n-found->parent == root_dir
4390 * This fixes the problem where directories might have inadvertently been deleted
4391 * leaving the object "hanging" without being rooted in the directory tree.
4394 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4396 return (obj == dev->del_dir ||
4397 obj == dev->unlinked_dir || obj == dev->root_dir);
4400 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4402 struct yaffs_obj *obj;
4403 struct yaffs_obj *parent;
4405 struct list_head *lh;
4406 struct list_head *n;
4413 /* Iterate through the objects in each hash entry,
4414 * looking at each object.
4415 * Make sure it is rooted.
4418 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4419 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4422 list_entry(lh, struct yaffs_obj, hash_link);
4423 parent = obj->parent;
4425 if (yaffs_has_null_parent(dev, obj)) {
4426 /* These directories are not hanging */
4429 || parent->variant_type !=
4430 YAFFS_OBJECT_TYPE_DIRECTORY) {
4432 } else if (yaffs_has_null_parent(dev, parent)) {
4436 * Need to follow the parent chain to see if it is hanging.
4441 while (parent != dev->root_dir &&
4443 parent->parent->variant_type ==
4444 YAFFS_OBJECT_TYPE_DIRECTORY
4445 && depth_limit > 0) {
4446 parent = parent->parent;
4449 if (parent != dev->root_dir)
4455 ("Hanging object %d moved to lost and found"
4456 TENDSTR), obj->obj_id));
4457 yaffs_add_obj_to_dir(dev->lost_n_found,
4466 * Delete directory contents for cleaning up lost and found.
4468 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4470 struct yaffs_obj *obj;
4471 struct list_head *lh;
4472 struct list_head *n;
4474 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4477 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4479 obj = list_entry(lh, struct yaffs_obj, siblings);
4480 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4481 yaffs_del_dir_contents(obj);
4484 (TSTR("Deleting lost_found object %d" TENDSTR),
4487 /* Need to use UnlinkObject since Delete would not handle
4488 * hardlinked objects correctly.
4490 yaffs_unlink_obj(obj);
4496 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4498 yaffs_del_dir_contents(dev->lost_n_found);
4502 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4507 struct list_head *i;
4508 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4510 struct yaffs_obj *l;
4516 T(YAFFS_TRACE_ALWAYS,
4518 ("tragedy: yaffs_find_by_name: null pointer directory"
4523 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4524 T(YAFFS_TRACE_ALWAYS,
4525 (TSTR("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
4529 sum = yaffs_calc_name_sum(name);
4531 list_for_each(i, &directory->variant.dir_variant.children) {
4533 l = list_entry(i, struct yaffs_obj, siblings);
4535 if (l->parent != directory)
4538 yaffs_check_obj_details_loaded(l);
4540 /* Special case for lost-n-found */
4541 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4542 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4544 } else if (l->sum == sum
4545 || l->hdr_chunk <= 0) {
4546 /* LostnFound chunk called Objxxx
4549 yaffs_get_obj_name(l, buffer,
4550 YAFFS_MAX_NAME_LENGTH + 1);
4552 (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4561 /* GetEquivalentObject dereferences any hard links to get to the
4565 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4567 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4568 /* We want the object id of the equivalent object, not this one */
4569 obj = obj->variant.hardlink_variant.equiv_obj;
4570 yaffs_check_obj_details_loaded(obj);
4576 * A note or two on object names.
4577 * * If the object name is missing, we then make one up in the form objnnn
4579 * * ASCII names are stored in the object header's name field from byte zero
4580 * * Unicode names are historically stored starting from byte zero.
4582 * Then there are automatic Unicode names...
4583 * The purpose of these is to save names in a way that can be read as
4584 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4585 * system to share files.
4587 * These automatic unicode are stored slightly differently...
4588 * - If the name can fit in the ASCII character space then they are saved as
4589 * ascii names as per above.
4590 * - If the name needs Unicode then the name is saved in Unicode
4591 * starting at oh->name[1].
4594 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
4597 /* Create an object name if we could not find one. */
4598 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4599 YCHAR local_name[20];
4600 YCHAR num_string[20];
4601 YCHAR *x = &num_string[19];
4602 unsigned v = obj->obj_id;
4606 *x = '0' + (v % 10);
4609 /* make up a name */
4610 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4611 strcat(local_name, x);
4612 strncpy(name, local_name, buffer_size - 1);
4616 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
4618 memset(name, 0, buffer_size * sizeof(YCHAR));
4620 yaffs_check_obj_details_loaded(obj);
4622 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4623 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4625 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
4626 else if (obj->short_name[0]) {
4627 strcpy(name, obj->short_name);
4630 else if (obj->hdr_chunk > 0) {
4632 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4634 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4636 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4638 if (obj->hdr_chunk > 0) {
4639 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4643 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4646 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4649 yaffs_fix_null_name(obj, name, buffer_size);
4651 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4654 int yaffs_get_obj_length(struct yaffs_obj *obj)
4656 /* Dereference any hard linking */
4657 obj = yaffs_get_equivalent_obj(obj);
4659 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4660 return obj->variant.file_variant.file_size;
4661 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4662 if (!obj->variant.symlink_variant.alias)
4664 return strnlen(obj->variant.symlink_variant.alias,
4665 YAFFS_MAX_ALIAS_LENGTH);
4667 /* Only a directory should drop through to here */
4668 return obj->my_dev->data_bytes_per_chunk;
4672 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4675 struct list_head *i;
4678 count++; /* the object itself */
4680 list_for_each(i, &obj->hard_links)
4681 count++; /* add the hard links; */
4686 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4688 obj = yaffs_get_equivalent_obj(obj);
4693 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4695 obj = yaffs_get_equivalent_obj(obj);
4697 switch (obj->variant_type) {
4698 case YAFFS_OBJECT_TYPE_FILE:
4701 case YAFFS_OBJECT_TYPE_DIRECTORY:
4704 case YAFFS_OBJECT_TYPE_SYMLINK:
4707 case YAFFS_OBJECT_TYPE_HARDLINK:
4710 case YAFFS_OBJECT_TYPE_SPECIAL:
4711 if (S_ISFIFO(obj->yst_mode))
4713 if (S_ISCHR(obj->yst_mode))
4715 if (S_ISBLK(obj->yst_mode))
4717 if (S_ISSOCK(obj->yst_mode))
4725 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4727 obj = yaffs_get_equivalent_obj(obj);
4728 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4729 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4731 return yaffs_clone_str(_Y(""));
4734 /*--------------------------- Initialisation code -------------------------- */
4736 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4739 /* Common functions, gotta have */
4740 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4743 #ifdef CONFIG_YAFFS_YAFFS2
4745 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4746 if (dev->param.write_chunk_tags_fn &&
4747 dev->param.read_chunk_tags_fn &&
4748 !dev->param.write_chunk_fn &&
4749 !dev->param.read_chunk_fn &&
4750 dev->param.bad_block_fn && dev->param.query_block_fn)
4754 /* Can use the "spare" style interface for yaffs1 */
4755 if (!dev->param.is_yaffs2 &&
4756 !dev->param.write_chunk_tags_fn &&
4757 !dev->param.read_chunk_tags_fn &&
4758 dev->param.write_chunk_fn &&
4759 dev->param.read_chunk_fn &&
4760 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4766 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4768 /* Initialise the unlinked, deleted, root and lost and found directories */
4770 dev->lost_n_found = dev->root_dir = NULL;
4771 dev->unlinked_dir = dev->del_dir = NULL;
4774 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4777 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4780 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4781 YAFFS_ROOT_MODE | S_IFDIR);
4783 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4784 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4786 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4788 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4795 int yaffs_guts_initialise(struct yaffs_dev *dev)
4797 int init_failed = 0;
4801 T(YAFFS_TRACE_TRACING,
4802 (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR)));
4804 /* Check stuff that must be set */
4807 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
4811 dev->internal_start_block = dev->param.start_block;
4812 dev->internal_end_block = dev->param.end_block;
4813 dev->block_offset = 0;
4814 dev->chunk_offset = 0;
4815 dev->n_free_chunks = 0;
4819 if (dev->param.start_block == 0) {
4820 dev->internal_start_block = dev->param.start_block + 1;
4821 dev->internal_end_block = dev->param.end_block + 1;
4822 dev->block_offset = 1;
4823 dev->chunk_offset = dev->param.chunks_per_block;
4826 /* Check geometry parameters. */
4828 if ((!dev->param.inband_tags && dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 1024) || (!dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 512) || (dev->param.inband_tags && !dev->param.is_yaffs2) || dev->param.chunks_per_block < 2 || dev->param.n_reserved_blocks < 2 || dev->internal_start_block <= 0 || dev->internal_end_block <= 0 || dev->internal_end_block <= (dev->internal_start_block + dev->param.n_reserved_blocks + 2)) { /* otherwise it is too small */
4829 T(YAFFS_TRACE_ALWAYS,
4831 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d "
4832 TENDSTR), dev->param.total_bytes_per_chunk,
4833 dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags));
4837 if (yaffs_init_nand(dev) != YAFFS_OK) {
4838 T(YAFFS_TRACE_ALWAYS,
4839 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
4843 /* Sort out space for inband tags, if required */
4844 if (dev->param.inband_tags)
4845 dev->data_bytes_per_chunk =
4846 dev->param.total_bytes_per_chunk -
4847 sizeof(struct yaffs_packed_tags2_tags_only);
4849 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4851 /* Got the right mix of functions? */
4852 if (!yaffs_check_dev_fns(dev)) {
4853 /* Function missing */
4854 T(YAFFS_TRACE_ALWAYS,
4856 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
4861 if (dev->is_mounted) {
4862 T(YAFFS_TRACE_ALWAYS,
4863 (TSTR("yaffs: device already mounted\n" TENDSTR)));
4867 /* Finished with most checks. One or two more checks happen later on too. */
4869 dev->is_mounted = 1;
4871 /* OK now calculate a few things for the device */
4874 * Calculate all the chunk size manipulation numbers:
4876 x = dev->data_bytes_per_chunk;
4877 /* We always use dev->chunk_shift and dev->chunk_div */
4878 dev->chunk_shift = calc_shifts(x);
4879 x >>= dev->chunk_shift;
4881 /* We only use chunk mask if chunk_div is 1 */
4882 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4885 * Calculate chunk_grp_bits.
4886 * We need to find the next power of 2 > than internal_end_block
4889 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4891 bits = calc_shifts_ceiling(x);
4893 /* Set up tnode width if wide tnodes are enabled. */
4894 if (!dev->param.wide_tnodes_disabled) {
4895 /* bits must be even so that we end up with 32-bit words */
4899 dev->tnode_width = 16;
4901 dev->tnode_width = bits;
4903 dev->tnode_width = 16;
4906 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4908 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4909 * so if the bitwidth of the
4910 * chunk range we're using is greater than 16 we need
4911 * to figure out chunk shift and chunk_grp_size
4914 if (bits <= dev->tnode_width)
4915 dev->chunk_grp_bits = 0;
4917 dev->chunk_grp_bits = bits - dev->tnode_width;
4919 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4920 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4921 dev->tnode_size = sizeof(struct yaffs_tnode);
4923 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4925 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4926 /* We have a problem because the soft delete won't work if
4927 * the chunk group size > chunks per block.
4928 * This can be remedied by using larger "virtual blocks".
4930 T(YAFFS_TRACE_ALWAYS,
4931 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
4936 /* OK, we've finished verifying the device, lets continue with initialisation */
4938 /* More device initialisation */
4940 dev->passive_gc_count = 0;
4941 dev->oldest_dirty_gc_count = 0;
4943 dev->gc_block_finder = 0;
4944 dev->buffered_block = -1;
4945 dev->doing_buffered_block_rewrite = 0;
4946 dev->n_deleted_files = 0;
4947 dev->n_bg_deletions = 0;
4948 dev->n_unlinked_files = 0;
4949 dev->n_ecc_fixed = 0;
4950 dev->n_ecc_unfixed = 0;
4951 dev->n_tags_ecc_fixed = 0;
4952 dev->n_tags_ecc_unfixed = 0;
4953 dev->n_erase_failures = 0;
4954 dev->n_erased_blocks = 0;
4955 dev->gc_disable = 0;
4956 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
4957 INIT_LIST_HEAD(&dev->dirty_dirs);
4958 dev->oldest_dirty_seq = 0;
4959 dev->oldest_dirty_block = 0;
4961 /* Initialise temporary buffers and caches. */
4962 if (!yaffs_init_tmp_buffers(dev))
4966 dev->gc_cleanup_list = NULL;
4968 if (!init_failed && dev->param.n_caches > 0) {
4972 dev->param.n_caches * sizeof(struct yaffs_cache);
4974 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4975 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4977 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4979 buf = (u8 *) dev->cache;
4982 memset(dev->cache, 0, cache_bytes);
4984 for (i = 0; i < dev->param.n_caches && buf; i++) {
4985 dev->cache[i].object = NULL;
4986 dev->cache[i].last_use = 0;
4987 dev->cache[i].dirty = 0;
4988 dev->cache[i].data = buf =
4989 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4994 dev->cache_last_use = 0;
4997 dev->cache_hits = 0;
5000 dev->gc_cleanup_list =
5001 kmalloc(dev->param.chunks_per_block * sizeof(u32),
5003 if (!dev->gc_cleanup_list)
5007 if (dev->param.is_yaffs2)
5008 dev->param.use_header_file_size = 1;
5010 if (!init_failed && !yaffs_init_blocks(dev))
5013 yaffs_init_tnodes_and_objs(dev);
5015 if (!init_failed && !yaffs_create_initial_dir(dev))
5019 /* Now scan the flash. */
5020 if (dev->param.is_yaffs2) {
5021 if (yaffs2_checkpt_restore(dev)) {
5022 yaffs_check_obj_details_loaded(dev->root_dir);
5023 T(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
5025 ("yaffs: restored from checkpoint"
5029 /* Clean up the mess caused by an aborted checkpoint load
5030 * and scan backwards.
5032 yaffs_deinit_blocks(dev);
5034 yaffs_deinit_tnodes_and_objs(dev);
5036 dev->n_erased_blocks = 0;
5037 dev->n_free_chunks = 0;
5038 dev->alloc_block = -1;
5039 dev->alloc_page = -1;
5040 dev->n_deleted_files = 0;
5041 dev->n_unlinked_files = 0;
5042 dev->n_bg_deletions = 0;
5044 if (!init_failed && !yaffs_init_blocks(dev))
5047 yaffs_init_tnodes_and_objs(dev);
5050 && !yaffs_create_initial_dir(dev))
5053 if (!init_failed && !yaffs2_scan_backwards(dev))
5056 } else if (!yaffs1_scan(dev)) {
5060 yaffs_strip_deleted_objs(dev);
5061 yaffs_fix_hanging_objs(dev);
5062 if (dev->param.empty_lost_n_found)
5063 yaffs_empty_l_n_f(dev);
5067 /* Clean up the mess */
5068 T(YAFFS_TRACE_TRACING,
5069 (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR)));
5071 yaffs_deinitialise(dev);
5075 /* Zero out stats */
5076 dev->n_page_reads = 0;
5077 dev->n_page_writes = 0;
5078 dev->n_erasures = 0;
5079 dev->n_gc_copies = 0;
5080 dev->n_retired_writes = 0;
5082 dev->n_retired_blocks = 0;
5084 yaffs_verify_free_chunks(dev);
5085 yaffs_verify_blocks(dev);
5087 /* Clean up any aborted checkpoint data */
5088 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5089 yaffs2_checkpt_invalidate(dev);
5091 T(YAFFS_TRACE_TRACING,
5092 (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR)));
5097 void yaffs_deinitialise(struct yaffs_dev *dev)
5099 if (dev->is_mounted) {
5102 yaffs_deinit_blocks(dev);
5103 yaffs_deinit_tnodes_and_objs(dev);
5104 if (dev->param.n_caches > 0 && dev->cache) {
5106 for (i = 0; i < dev->param.n_caches; i++) {
5107 if (dev->cache[i].data)
5108 kfree(dev->cache[i].data);
5109 dev->cache[i].data = NULL;
5116 kfree(dev->gc_cleanup_list);
5118 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5119 kfree(dev->temp_buffer[i].buffer);
5121 dev->is_mounted = 0;
5123 if (dev->param.deinitialise_flash_fn)
5124 dev->param.deinitialise_flash_fn(dev);
5128 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5133 struct yaffs_block_info *blk;
5135 blk = dev->block_info;
5136 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5137 switch (blk->block_state) {
5138 case YAFFS_BLOCK_STATE_EMPTY:
5139 case YAFFS_BLOCK_STATE_ALLOCATING:
5140 case YAFFS_BLOCK_STATE_COLLECTING:
5141 case YAFFS_BLOCK_STATE_FULL:
5143 (dev->param.chunks_per_block - blk->pages_in_use +
5144 blk->soft_del_pages);
5155 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5157 /* This is what we report to the outside world */
5161 int blocks_for_checkpt;
5164 n_free = dev->n_free_chunks;
5165 n_free += dev->n_deleted_files;
5167 /* Now count the number of dirty chunks in the cache and subtract those */
5169 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5170 if (dev->cache[i].dirty)
5174 n_free -= n_dirty_caches;
5177 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5179 /* Now we figure out how much to reserve for the checkpoint and report that... */
5180 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5182 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);