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 */
46 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
47 const u8 * buffer, int n_bytes, int use_reserve);
51 /* Function to calculate chunk and offset */
53 static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
54 int *chunk_out, u32 * offset_out)
59 chunk = (u32) (addr >> dev->chunk_shift);
61 if (dev->chunk_div == 1) {
62 /* easy power of 2 case */
63 offset = (u32) (addr & dev->chunk_mask);
65 /* Non power-of-2 case */
69 chunk /= dev->chunk_div;
71 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
72 offset = (u32) (addr - chunk_base);
79 /* Function to return the number of shifts for a power of 2 greater than or
80 * equal to the given number
81 * Note we don't try to cater for all possible numbers and this does not have to
82 * be hellishly efficient.
85 static u32 calc_shifts_ceiling(u32 x)
90 shifts = extra_bits = 0;
105 /* Function to return the number of shifts to get a 1 in bit 0
108 static u32 calc_shifts(u32 x)
126 * Temporary buffer manipulations.
129 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
134 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
136 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
137 dev->temp_buffer[i].line = 0; /* not in use */
138 dev->temp_buffer[i].buffer = buf =
139 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
142 return buf ? YAFFS_OK : YAFFS_FAIL;
145 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
150 if (dev->temp_in_use > dev->max_temp)
151 dev->max_temp = dev->temp_in_use;
153 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
154 if (dev->temp_buffer[i].line == 0) {
155 dev->temp_buffer[i].line = line_no;
156 if ((i + 1) > dev->max_temp) {
157 dev->max_temp = i + 1;
158 for (j = 0; j <= i; j++)
159 dev->temp_buffer[j].max_line =
160 dev->temp_buffer[j].line;
163 return dev->temp_buffer[i].buffer;
167 yaffs_trace(YAFFS_TRACE_BUFFERS,
168 "Out of temp buffers at line %d, other held by lines:",
170 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
171 yaffs_trace(YAFFS_TRACE_BUFFERS," %d", dev->temp_buffer[i].line);
174 * If we got here then we have to allocate an unmanaged one
178 dev->unmanaged_buffer_allocs++;
179 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
183 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
189 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
190 if (dev->temp_buffer[i].buffer == buffer) {
191 dev->temp_buffer[i].line = 0;
197 /* assume it is an unmanaged one. */
198 yaffs_trace(YAFFS_TRACE_BUFFERS,
199 "Releasing unmanaged temp buffer in line %d",
202 dev->unmanaged_buffer_deallocs++;
208 * Determine if we have a managed buffer.
210 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
214 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
215 if (dev->temp_buffer[i].buffer == buffer)
219 for (i = 0; i < dev->param.n_caches; i++) {
220 if (dev->cache[i].data == buffer)
224 if (buffer == dev->checkpt_buffer)
227 yaffs_trace(YAFFS_TRACE_ALWAYS,
228 "yaffs: unmaged buffer detected.");
233 * Functions for robustisizing TODO
237 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
239 const struct yaffs_ext_tags *tags)
242 nand_chunk = nand_chunk;
247 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
248 const struct yaffs_ext_tags *tags)
251 nand_chunk = nand_chunk;
255 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
256 struct yaffs_block_info *bi)
258 if (!bi->gc_prioritise) {
259 bi->gc_prioritise = 1;
260 dev->has_pending_prioritised_gc = 1;
261 bi->chunk_error_strikes++;
263 if (bi->chunk_error_strikes > 3) {
264 bi->needs_retiring = 1; /* Too many stikes, so retire this */
265 yaffs_trace(YAFFS_TRACE_ALWAYS, "yaffs: Block struck out");
271 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
274 int flash_block = nand_chunk / dev->param.chunks_per_block;
275 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
277 yaffs_handle_chunk_error(dev, bi);
280 /* Was an actual write failure, so mark the block for retirement */
281 bi->needs_retiring = 1;
282 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
283 "**>> Block %d needs retiring", flash_block);
286 /* Delete the chunk */
287 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
288 yaffs_skip_rest_of_block(dev);
296 * Simple hash function. Needs to have a reasonable spread
299 static inline int yaffs_hash_fn(int n)
302 return n % YAFFS_NOBJECT_BUCKETS;
306 * Access functions to useful fake objects.
307 * Note that root might have a presence in NAND if permissions are set.
310 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
312 return dev->root_dir;
315 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
317 return dev->lost_n_found;
321 * Erased NAND checking functions
324 int yaffs_check_ff(u8 * buffer, int n_bytes)
326 /* Horrible, slow implementation */
335 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
337 int retval = YAFFS_OK;
338 u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
339 struct yaffs_ext_tags tags;
342 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
344 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
347 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
349 yaffs_trace(YAFFS_TRACE_NANDACCESS, "Chunk %d not erased", nand_chunk);
353 yaffs_release_temp_buffer(dev, data, __LINE__);
359 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
362 struct yaffs_ext_tags *tags)
364 int retval = YAFFS_OK;
365 struct yaffs_ext_tags temp_tags;
366 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
369 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
370 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
371 temp_tags.obj_id != tags->obj_id ||
372 temp_tags.chunk_id != tags->chunk_id ||
373 temp_tags.n_bytes != tags->n_bytes)
376 yaffs_release_temp_buffer(dev, buffer, __LINE__);
382 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
385 int reserved_blocks = dev->param.n_reserved_blocks;
388 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
391 ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
393 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
396 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
400 struct yaffs_block_info *bi;
402 if (dev->n_erased_blocks < 1) {
403 /* Hoosterman we've got a problem.
404 * Can't get space to gc
406 yaffs_trace(YAFFS_TRACE_ERROR,
407 "yaffs tragedy: no more erased blocks" );
412 /* Find an empty block. */
414 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
415 dev->alloc_block_finder++;
416 if (dev->alloc_block_finder < dev->internal_start_block
417 || dev->alloc_block_finder > dev->internal_end_block) {
418 dev->alloc_block_finder = dev->internal_start_block;
421 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
423 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
424 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
426 bi->seq_number = dev->seq_number;
427 dev->n_erased_blocks--;
428 yaffs_trace(YAFFS_TRACE_ALLOCATE,
429 "Allocated block %d, seq %d, %d left" ,
430 dev->alloc_block_finder, dev->seq_number,
431 dev->n_erased_blocks);
432 return dev->alloc_block_finder;
436 yaffs_trace(YAFFS_TRACE_ALWAYS,
437 "yaffs tragedy: no more erased blocks, but there should have been %d",
438 dev->n_erased_blocks);
443 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
444 struct yaffs_block_info **block_ptr)
447 struct yaffs_block_info *bi;
449 if (dev->alloc_block < 0) {
450 /* Get next block to allocate off */
451 dev->alloc_block = yaffs_find_alloc_block(dev);
455 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
456 /* Not enough space to allocate unless we're allowed to use the reserve. */
460 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
461 && dev->alloc_page == 0)
462 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
464 /* Next page please.... */
465 if (dev->alloc_block >= 0) {
466 bi = yaffs_get_block_info(dev, dev->alloc_block);
468 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
471 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
475 dev->n_free_chunks--;
477 /* If the block is full set the state to full */
478 if (dev->alloc_page >= dev->param.chunks_per_block) {
479 bi->block_state = YAFFS_BLOCK_STATE_FULL;
480 dev->alloc_block = -1;
489 yaffs_trace(YAFFS_TRACE_ERROR, "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" );
494 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
498 n = dev->n_erased_blocks * dev->param.chunks_per_block;
500 if (dev->alloc_block > 0)
501 n += (dev->param.chunks_per_block - dev->alloc_page);
508 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
509 * if we don't want to write to it.
511 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
513 if (dev->alloc_block > 0) {
514 struct yaffs_block_info *bi =
515 yaffs_get_block_info(dev, dev->alloc_block);
516 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
517 bi->block_state = YAFFS_BLOCK_STATE_FULL;
518 dev->alloc_block = -1;
523 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
525 struct yaffs_ext_tags *tags, int use_reserver)
531 yaffs2_checkpt_invalidate(dev);
534 struct yaffs_block_info *bi = 0;
537 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
543 /* First check this chunk is erased, if it needs
544 * checking. The checking policy (unless forced
545 * always on) is as follows:
547 * Check the first page we try to write in a block.
548 * If the check passes then we don't need to check any
549 * more. If the check fails, we check again...
550 * If the block has been erased, we don't need to check.
552 * However, if the block has been prioritised for gc,
553 * then we think there might be something odd about
554 * this block and stop using it.
556 * Rationale: We should only ever see chunks that have
557 * not been erased if there was a partially written
558 * chunk due to power loss. This checking policy should
559 * catch that case with very few checks and thus save a
560 * lot of checks that are most likely not needed.
563 * If an erase check fails or the write fails we skip the
567 /* let's give it a try */
570 if (dev->param.always_check_erased)
571 bi->skip_erased_check = 0;
573 if (!bi->skip_erased_check) {
574 erased_ok = yaffs_check_chunk_erased(dev, chunk);
575 if (erased_ok != YAFFS_OK) {
576 yaffs_trace(YAFFS_TRACE_ERROR,
577 "**>> yaffs chunk %d was not erased",
580 /* If not erased, delete this one,
581 * skip rest of block and
582 * try another chunk */
583 yaffs_chunk_del(dev, chunk, 1, __LINE__);
584 yaffs_skip_rest_of_block(dev);
589 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
591 if (!bi->skip_erased_check)
593 yaffs_verify_chunk_written(dev, chunk, data, tags);
595 if (write_ok != YAFFS_OK) {
596 /* Clean up aborted write, skip to next block and
597 * try another chunk */
598 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
602 bi->skip_erased_check = 1;
604 /* Copy the data into the robustification buffer */
605 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
607 } while (write_ok != YAFFS_OK &&
608 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
614 yaffs_trace(YAFFS_TRACE_ERROR,
615 "**>> yaffs write required %d attempts",
617 dev->n_retired_writes += (attempts - 1);
624 * Block retiring for handling a broken block.
627 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
629 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
631 yaffs2_checkpt_invalidate(dev);
633 yaffs2_clear_oldest_dirty_seq(dev, bi);
635 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
636 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
637 yaffs_trace(YAFFS_TRACE_ALWAYS,
638 "yaffs: Failed to mark bad and erase block %d",
641 struct yaffs_ext_tags tags;
643 flash_block * dev->param.chunks_per_block;
645 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
647 memset(buffer, 0xff, dev->data_bytes_per_chunk);
648 yaffs_init_tags(&tags);
649 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
650 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
654 yaffs_trace(YAFFS_TRACE_ALWAYS,
655 "yaffs: Failed to write bad block marker to block %d",
658 yaffs_release_temp_buffer(dev, buffer, __LINE__);
662 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
663 bi->gc_prioritise = 0;
664 bi->needs_retiring = 0;
666 dev->n_retired_blocks++;
669 /*---------------- Name handling functions ------------*/
671 static u16 yaffs_calc_name_sum(const YCHAR * name)
676 const YUCHAR *bname = (const YUCHAR *)name;
678 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
680 /* 0x1f mask is case insensitive */
681 sum += ((*bname) & 0x1f) * i;
689 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
691 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
692 memset(obj->short_name, 0, sizeof(obj->short_name));
694 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
695 YAFFS_SHORT_NAME_LENGTH)
696 strcpy(obj->short_name, name);
698 obj->short_name[0] = _Y('\0');
700 obj->sum = yaffs_calc_name_sum(name);
703 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
704 const struct yaffs_obj_hdr *oh)
706 #ifdef CONFIG_YAFFS_AUTO_UNICODE
707 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
708 memset(tmp_name, 0, sizeof(tmp_name));
709 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
710 YAFFS_MAX_NAME_LENGTH + 1);
711 yaffs_set_obj_name(obj, tmp_name);
713 yaffs_set_obj_name(obj, oh->name);
717 /*-------------------- TNODES -------------------
719 * List of spare tnodes
720 * The list is hooked together using the first pointer
724 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
726 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
728 memset(tn, 0, dev->tnode_size);
732 dev->checkpoint_blocks_required = 0; /* force recalculation */
737 /* FreeTnode frees up a tnode and puts it back on the free list */
738 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
740 yaffs_free_raw_tnode(dev, tn);
742 dev->checkpoint_blocks_required = 0; /* force recalculation */
745 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
747 yaffs_deinit_raw_tnodes_and_objs(dev);
752 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
753 unsigned pos, unsigned val)
755 u32 *map = (u32 *) tn;
761 pos &= YAFFS_TNODES_LEVEL0_MASK;
762 val >>= dev->chunk_grp_bits;
764 bit_in_map = pos * dev->tnode_width;
765 word_in_map = bit_in_map / 32;
766 bit_in_word = bit_in_map & (32 - 1);
768 mask = dev->tnode_mask << bit_in_word;
770 map[word_in_map] &= ~mask;
771 map[word_in_map] |= (mask & (val << bit_in_word));
773 if (dev->tnode_width > (32 - bit_in_word)) {
774 bit_in_word = (32 - bit_in_word);
777 dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
778 map[word_in_map] &= ~mask;
779 map[word_in_map] |= (mask & (val >> bit_in_word));
783 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
786 u32 *map = (u32 *) tn;
792 pos &= YAFFS_TNODES_LEVEL0_MASK;
794 bit_in_map = pos * dev->tnode_width;
795 word_in_map = bit_in_map / 32;
796 bit_in_word = bit_in_map & (32 - 1);
798 val = map[word_in_map] >> bit_in_word;
800 if (dev->tnode_width > (32 - bit_in_word)) {
801 bit_in_word = (32 - bit_in_word);
803 val |= (map[word_in_map] << bit_in_word);
806 val &= dev->tnode_mask;
807 val <<= dev->chunk_grp_bits;
812 /* ------------------- End of individual tnode manipulation -----------------*/
814 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
815 * The look up tree is represented by the top tnode and the number of top_level
816 * in the tree. 0 means only the level 0 tnode is in the tree.
819 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
820 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
821 struct yaffs_file_var *file_struct,
824 struct yaffs_tnode *tn = file_struct->top;
827 int level = file_struct->top_level;
831 /* Check sane level and chunk Id */
832 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
835 if (chunk_id > YAFFS_MAX_CHUNK_ID)
838 /* First check we're tall enough (ie enough top_level) */
840 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
843 i >>= YAFFS_TNODES_INTERNAL_BITS;
847 if (required_depth > file_struct->top_level)
848 return NULL; /* Not tall enough, so we can't find it */
850 /* Traverse down to level 0 */
851 while (level > 0 && tn) {
852 tn = tn->internal[(chunk_id >>
853 (YAFFS_TNODES_LEVEL0_BITS +
855 YAFFS_TNODES_INTERNAL_BITS)) &
856 YAFFS_TNODES_INTERNAL_MASK];
863 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
864 * This happens in two steps:
865 * 1. If the tree isn't tall enough, then make it taller.
866 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
868 * Used when modifying the tree.
870 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
871 * be plugged into the ttree.
874 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
875 struct yaffs_file_var *file_struct,
877 struct yaffs_tnode *passed_tn)
882 struct yaffs_tnode *tn;
886 /* Check sane level and page Id */
887 if (file_struct->top_level < 0
888 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
891 if (chunk_id > YAFFS_MAX_CHUNK_ID)
894 /* First check we're tall enough (ie enough top_level) */
896 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
899 x >>= YAFFS_TNODES_INTERNAL_BITS;
903 if (required_depth > file_struct->top_level) {
904 /* Not tall enough, gotta make the tree taller */
905 for (i = file_struct->top_level; i < required_depth; i++) {
907 tn = yaffs_get_tnode(dev);
910 tn->internal[0] = file_struct->top;
911 file_struct->top = tn;
912 file_struct->top_level++;
914 yaffs_trace(YAFFS_TRACE_ERROR, "yaffs: no more tnodes");
920 /* Traverse down to level 0, adding anything we need */
922 l = file_struct->top_level;
923 tn = file_struct->top;
926 while (l > 0 && tn) {
928 (YAFFS_TNODES_LEVEL0_BITS +
929 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
930 YAFFS_TNODES_INTERNAL_MASK;
932 if ((l > 1) && !tn->internal[x]) {
933 /* Add missing non-level-zero tnode */
934 tn->internal[x] = yaffs_get_tnode(dev);
935 if (!tn->internal[x])
938 /* Looking from level 1 at level 0 */
940 /* If we already have one, then release it. */
942 yaffs_free_tnode(dev,
945 tn->internal[x] = passed_tn;
947 } else if (!tn->internal[x]) {
948 /* Don't have one, none passed in */
949 tn->internal[x] = yaffs_get_tnode(dev);
950 if (!tn->internal[x])
955 tn = tn->internal[x];
961 memcpy(tn, passed_tn,
962 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
963 yaffs_free_tnode(dev, passed_tn);
970 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
973 return (tags->chunk_id == chunk_obj &&
974 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
978 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
979 struct yaffs_ext_tags *tags, int obj_id,
984 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
985 if (yaffs_check_chunk_bit
986 (dev, the_chunk / dev->param.chunks_per_block,
987 the_chunk % dev->param.chunks_per_block)) {
989 if (dev->chunk_grp_size == 1)
992 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
994 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
1005 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1006 struct yaffs_ext_tags *tags)
1008 /*Get the Tnode, then get the level 0 offset chunk offset */
1009 struct yaffs_tnode *tn;
1011 struct yaffs_ext_tags local_tags;
1014 struct yaffs_dev *dev = in->my_dev;
1017 /* Passed a NULL, so use our own tags space */
1021 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1024 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1027 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1033 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1034 struct yaffs_ext_tags *tags)
1036 /* Get the Tnode, then get the level 0 offset chunk offset */
1037 struct yaffs_tnode *tn;
1039 struct yaffs_ext_tags local_tags;
1041 struct yaffs_dev *dev = in->my_dev;
1045 /* Passed a NULL, so use our own tags space */
1049 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1053 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1056 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1059 /* Delete the entry in the filestructure (if found) */
1061 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1067 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1068 int nand_chunk, int in_scan)
1070 /* NB in_scan is zero unless scanning.
1071 * For forward scanning, in_scan is > 0;
1072 * for backward scanning in_scan is < 0
1074 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1077 struct yaffs_tnode *tn;
1078 struct yaffs_dev *dev = in->my_dev;
1080 struct yaffs_ext_tags existing_tags;
1081 struct yaffs_ext_tags new_tags;
1082 unsigned existing_serial, new_serial;
1084 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1085 /* Just ignore an attempt at putting a chunk into a non-file during scanning
1086 * If it is not during Scanning then something went wrong!
1089 yaffs_trace(YAFFS_TRACE_ERROR,
1090 "yaffs tragedy:attempt to put data chunk into a non-file"
1095 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1099 tn = yaffs_add_find_tnode_0(dev,
1100 &in->variant.file_variant,
1106 /* Dummy insert, bail now */
1109 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1112 /* If we're scanning then we need to test for duplicates
1113 * NB This does not need to be efficient since it should only ever
1114 * happen when the power fails during a write, then only one
1115 * chunk should ever be affected.
1117 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
1118 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
1121 if (existing_cunk > 0) {
1122 /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
1123 * thus we have to do a FindChunkInFile to get the real chunk id.
1125 * We have a duplicate now we need to decide which one to use:
1127 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
1128 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
1129 * YAFFS1: Get both sets of tags and compare serial numbers.
1133 /* Only do this for forward scanning */
1134 yaffs_rd_chunk_tags_nand(dev,
1138 /* Do a proper find */
1140 yaffs_find_chunk_in_file(in, inode_chunk,
1144 if (existing_cunk <= 0) {
1145 /*Hoosterman - how did this happen? */
1147 yaffs_trace(YAFFS_TRACE_ERROR,
1148 "yaffs tragedy: existing chunk < 0 in scan"
1153 /* NB The deleted flags should be false, otherwise the chunks will
1154 * not be loaded during a scan
1158 new_serial = new_tags.serial_number;
1159 existing_serial = existing_tags.serial_number;
1162 if ((in_scan > 0) &&
1163 (existing_cunk <= 0 ||
1164 ((existing_serial + 1) & 3) == new_serial)) {
1165 /* Forward scanning.
1167 * Delete the old one and drop through to update the tnode
1169 yaffs_chunk_del(dev, existing_cunk, 1,
1172 /* Backward scanning or we want to use the existing one
1174 * Delete the new one and return early so that the tnode isn't changed
1176 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1183 if (existing_cunk == 0)
1184 in->n_data_chunks++;
1186 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1191 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1193 struct yaffs_block_info *the_block;
1196 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1198 block_no = chunk / dev->param.chunks_per_block;
1199 the_block = yaffs_get_block_info(dev, block_no);
1201 the_block->soft_del_pages++;
1202 dev->n_free_chunks++;
1203 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1207 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1208 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1210 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1213 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1214 u32 level, int chunk_offset)
1219 struct yaffs_dev *dev = in->my_dev;
1224 for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
1226 if (tn->internal[i]) {
1228 yaffs_soft_del_worker(in,
1234 YAFFS_TNODES_INTERNAL_BITS)
1237 yaffs_free_tnode(dev,
1240 tn->internal[i] = NULL;
1242 /* Hoosterman... how could this happen? */
1246 return (all_done) ? 1 : 0;
1247 } else if (level == 0) {
1249 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1250 the_chunk = yaffs_get_group_base(dev, tn, i);
1252 /* Note this does not find the real chunk, only the chunk group.
1253 * We make an assumption that a chunk group is not larger than
1256 yaffs_soft_del_chunk(dev, the_chunk);
1257 yaffs_load_tnode_0(dev, tn, i, 0);
1271 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1273 struct yaffs_dev *dev = obj->my_dev;
1274 struct yaffs_obj *parent;
1276 yaffs_verify_obj_in_dir(obj);
1277 parent = obj->parent;
1279 yaffs_verify_dir(parent);
1281 if (dev && dev->param.remove_obj_fn)
1282 dev->param.remove_obj_fn(obj);
1284 list_del_init(&obj->siblings);
1287 yaffs_verify_dir(parent);
1290 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1293 yaffs_trace(YAFFS_TRACE_ALWAYS,
1294 "tragedy: Trying to add an object to a null pointer directory"
1299 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1300 yaffs_trace(YAFFS_TRACE_ALWAYS,
1301 "tragedy: Trying to add an object to a non-directory"
1306 if (obj->siblings.prev == NULL) {
1307 /* Not initialised */
1311 yaffs_verify_dir(directory);
1313 yaffs_remove_obj_from_dir(obj);
1316 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1317 obj->parent = directory;
1319 if (directory == obj->my_dev->unlinked_dir
1320 || directory == obj->my_dev->del_dir) {
1322 obj->my_dev->n_unlinked_files++;
1323 obj->rename_allowed = 0;
1326 yaffs_verify_dir(directory);
1327 yaffs_verify_obj_in_dir(obj);
1330 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1331 struct yaffs_obj *new_dir,
1332 const YCHAR * new_name, int force, int shadows)
1337 struct yaffs_obj *existing_target;
1339 if (new_dir == NULL)
1340 new_dir = obj->parent; /* use the old directory */
1342 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1343 yaffs_trace(YAFFS_TRACE_ALWAYS,
1344 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1349 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1350 if (obj->my_dev->param.is_yaffs2)
1351 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1353 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1354 && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1356 del_op = (new_dir == obj->my_dev->del_dir);
1358 existing_target = yaffs_find_by_name(new_dir, new_name);
1360 /* If the object is a file going into the unlinked directory,
1361 * then it is OK to just stuff it in since duplicate names are allowed.
1362 * else only proceed if the new name does not exist and if we're putting
1363 * it into a directory.
1369 !existing_target) &&
1370 new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1371 yaffs_set_obj_name(obj, new_name);
1374 yaffs_add_obj_to_dir(new_dir, obj);
1379 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1380 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
1388 /*------------------------ Short Operations Cache ----------------------------------------
1389 * In many situations where there is no high level buffering a lot of
1390 * reads might be short sequential reads, and a lot of writes may be short
1391 * sequential writes. eg. scanning/writing a jpeg file.
1392 * In these cases, a short read/write cache can provide a huge perfomance
1393 * benefit with dumb-as-a-rock code.
1394 * In Linux, the page cache provides read buffering and the short op cache
1395 * provides write buffering.
1397 * There are a limited number (~10) of cache chunks per device so that we don't
1398 * need a very intelligent search.
1401 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1403 struct yaffs_dev *dev = obj->my_dev;
1405 struct yaffs_cache *cache;
1406 int n_caches = obj->my_dev->param.n_caches;
1408 for (i = 0; i < n_caches; i++) {
1409 cache = &dev->cache[i];
1410 if (cache->object == obj && cache->dirty)
1417 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1419 struct yaffs_dev *dev = obj->my_dev;
1420 int lowest = -99; /* Stop compiler whining. */
1422 struct yaffs_cache *cache;
1423 int chunk_written = 0;
1424 int n_caches = obj->my_dev->param.n_caches;
1430 /* Find the dirty cache for this object with the lowest chunk id. */
1431 for (i = 0; i < n_caches; i++) {
1432 if (dev->cache[i].object == obj &&
1433 dev->cache[i].dirty) {
1435 || dev->cache[i].chunk_id <
1437 cache = &dev->cache[i];
1438 lowest = cache->chunk_id;
1443 if (cache && !cache->locked) {
1444 /* Write it out and free it up */
1447 yaffs_wr_data_obj(cache->object,
1452 cache->object = NULL;
1455 } while (cache && chunk_written > 0);
1458 /* Hoosterman, disk full while writing cache out. */
1459 yaffs_trace(YAFFS_TRACE_ERROR,
1460 "yaffs tragedy: no space during cache write");
1466 /*yaffs_flush_whole_cache(dev)
1471 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1473 struct yaffs_obj *obj;
1474 int n_caches = dev->param.n_caches;
1477 /* Find a dirty object in the cache and flush it...
1478 * until there are no further dirty objects.
1482 for (i = 0; i < n_caches && !obj; i++) {
1483 if (dev->cache[i].object && dev->cache[i].dirty)
1484 obj = dev->cache[i].object;
1488 yaffs_flush_file_cache(obj);
1494 /* Grab us a cache chunk for use.
1495 * First look for an empty one.
1496 * Then look for the least recently used non-dirty one.
1497 * Then look for the least recently used dirty one...., flush and look again.
1499 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1503 if (dev->param.n_caches > 0) {
1504 for (i = 0; i < dev->param.n_caches; i++) {
1505 if (!dev->cache[i].object)
1506 return &dev->cache[i];
1513 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1515 struct yaffs_cache *cache;
1516 struct yaffs_obj *the_obj;
1521 if (dev->param.n_caches > 0) {
1522 /* Try find a non-dirty one... */
1524 cache = yaffs_grab_chunk_worker(dev);
1527 /* They were all dirty, find the last recently used object and flush
1528 * its cache, then find again.
1529 * NB what's here is not very accurate, we actually flush the object
1530 * the last recently used page.
1533 /* With locking we can't assume we can use entry zero */
1535 the_obj = dev->cache[0].object;
1540 for (i = 0; i < dev->param.n_caches; i++) {
1541 if (dev->cache[i].object &&
1542 !dev->cache[i].locked &&
1543 (dev->cache[i].last_use < usage
1545 usage = dev->cache[i].last_use;
1546 the_obj = dev->cache[i].object;
1547 cache = &dev->cache[i];
1552 if (!cache || cache->dirty) {
1553 /* Flush and try again */
1554 yaffs_flush_file_cache(the_obj);
1555 cache = yaffs_grab_chunk_worker(dev);
1565 /* Find a cached chunk */
1566 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1569 struct yaffs_dev *dev = obj->my_dev;
1571 if (dev->param.n_caches > 0) {
1572 for (i = 0; i < dev->param.n_caches; i++) {
1573 if (dev->cache[i].object == obj &&
1574 dev->cache[i].chunk_id == chunk_id) {
1577 return &dev->cache[i];
1584 /* Mark the chunk for the least recently used algorithym */
1585 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1589 if (dev->param.n_caches > 0) {
1590 if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
1591 /* Reset the cache usages */
1593 for (i = 1; i < dev->param.n_caches; i++)
1594 dev->cache[i].last_use = 0;
1596 dev->cache_last_use = 0;
1599 dev->cache_last_use++;
1601 cache->last_use = dev->cache_last_use;
1608 /* Invalidate a single cache page.
1609 * Do this when a whole page gets written,
1610 * ie the short cache for this page is no longer valid.
1612 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1614 if (object->my_dev->param.n_caches > 0) {
1615 struct yaffs_cache *cache =
1616 yaffs_find_chunk_cache(object, chunk_id);
1619 cache->object = NULL;
1623 /* Invalidate all the cache pages associated with this object
1624 * Do this whenever ther file is deleted or resized.
1626 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1629 struct yaffs_dev *dev = in->my_dev;
1631 if (dev->param.n_caches > 0) {
1632 /* Invalidate it. */
1633 for (i = 0; i < dev->param.n_caches; i++) {
1634 if (dev->cache[i].object == in)
1635 dev->cache[i].object = NULL;
1640 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1643 struct yaffs_dev *dev = obj->my_dev;
1645 /* If it is still linked into the bucket list, free from the list */
1646 if (!list_empty(&obj->hash_link)) {
1647 list_del_init(&obj->hash_link);
1648 bucket = yaffs_hash_fn(obj->obj_id);
1649 dev->obj_bucket[bucket].count--;
1653 /* FreeObject frees up a Object and puts it back on the free list */
1654 static void yaffs_free_obj(struct yaffs_obj *obj)
1656 struct yaffs_dev *dev = obj->my_dev;
1658 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1659 obj, obj->my_inode);
1665 if (!list_empty(&obj->siblings))
1668 if (obj->my_inode) {
1669 /* We're still hooked up to a cached inode.
1670 * Don't delete now, but mark for later deletion
1672 obj->defered_free = 1;
1676 yaffs_unhash_obj(obj);
1678 yaffs_free_raw_obj(dev, obj);
1680 dev->checkpoint_blocks_required = 0; /* force recalculation */
1683 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1685 if (obj->defered_free)
1686 yaffs_free_obj(obj);
1689 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1692 /* First off, invalidate the file's data in the cache, without flushing. */
1693 yaffs_invalidate_whole_cache(in);
1695 if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
1696 /* Move to the unlinked directory so we have a record that it was deleted. */
1697 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1702 yaffs_remove_obj_from_dir(in);
1703 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1711 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1714 obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1715 if (obj->n_data_chunks <= 0) {
1716 /* Empty file with no duplicate object headers,
1717 * just delete it immediately */
1718 yaffs_free_tnode(obj->my_dev,
1719 obj->variant.file_variant.top);
1720 obj->variant.file_variant.top = NULL;
1721 yaffs_trace(YAFFS_TRACE_TRACING,
1722 "yaffs: Deleting empty file %d",
1724 yaffs_generic_obj_del(obj);
1726 yaffs_soft_del_worker(obj,
1727 obj->variant.file_variant.top,
1729 file_variant.top_level, 0);
1735 /* Pruning removes any part of the file structure tree that is beyond the
1736 * bounds of the file (ie that does not point to chunks).
1738 * A file should only get pruned when its size is reduced.
1740 * Before pruning, the chunks must be pulled from the tree and the
1741 * level 0 tnode entries must be zeroed out.
1742 * Could also use this for file deletion, but that's probably better handled
1743 * by a special case.
1745 * This function is recursive. For levels > 0 the function is called again on
1746 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1747 * If there is no data in a subtree then it is pruned.
1750 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1751 struct yaffs_tnode *tn, u32 level,
1761 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1762 if (tn->internal[i]) {
1764 yaffs_prune_worker(dev,
1771 if (tn->internal[i])
1775 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1776 u32 *map = (u32 *) tn;
1778 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1784 if (has_data == 0 && del0) {
1785 /* Free and return NULL */
1787 yaffs_free_tnode(dev, tn);
1797 static int yaffs_prune_tree(struct yaffs_dev *dev,
1798 struct yaffs_file_var *file_struct)
1803 struct yaffs_tnode *tn;
1805 if (file_struct->top_level > 0) {
1807 yaffs_prune_worker(dev, file_struct->top,
1808 file_struct->top_level, 0);
1810 /* Now we have a tree with all the non-zero branches NULL but the height
1811 * is the same as it was.
1812 * Let's see if we can trim internal tnodes to shorten the tree.
1813 * We can do this if only the 0th element in the tnode is in use
1814 * (ie all the non-zero are NULL)
1817 while (file_struct->top_level && !done) {
1818 tn = file_struct->top;
1821 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1822 if (tn->internal[i])
1827 file_struct->top = tn->internal[0];
1828 file_struct->top_level--;
1829 yaffs_free_tnode(dev, tn);
1839 /*-------------------- End of File Structure functions.-------------------*/
1841 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1842 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1844 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1849 /* Now sweeten it up... */
1851 memset(obj, 0, sizeof(struct yaffs_obj));
1852 obj->being_created = 1;
1856 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1857 INIT_LIST_HEAD(&(obj->hard_links));
1858 INIT_LIST_HEAD(&(obj->hash_link));
1859 INIT_LIST_HEAD(&obj->siblings);
1861 /* Now make the directory sane */
1862 if (dev->root_dir) {
1863 obj->parent = dev->root_dir;
1864 list_add(&(obj->siblings),
1865 &dev->root_dir->variant.dir_variant.children);
1868 /* Add it to the lost and found directory.
1869 * NB Can't put root or lost-n-found in lost-n-found so
1870 * check if lost-n-found exists first
1872 if (dev->lost_n_found)
1873 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1875 obj->being_created = 0;
1878 dev->checkpoint_blocks_required = 0; /* force recalculation */
1883 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1887 int lowest = 999999;
1889 /* Search for the shortest list or one that
1893 for (i = 0; i < 10 && lowest > 4; i++) {
1894 dev->bucket_finder++;
1895 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1896 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1897 lowest = dev->obj_bucket[dev->bucket_finder].count;
1898 l = dev->bucket_finder;
1906 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1908 int bucket = yaffs_find_nice_bucket(dev);
1910 /* Now find an object value that has not already been taken
1911 * by scanning the list.
1915 struct list_head *i;
1917 u32 n = (u32) bucket;
1919 /* yaffs_check_obj_hash_sane(); */
1923 n += YAFFS_NOBJECT_BUCKETS;
1924 if (1 || dev->obj_bucket[bucket].count > 0) {
1925 list_for_each(i, &dev->obj_bucket[bucket].list) {
1926 /* If there is already one in the list */
1927 if (i && list_entry(i, struct yaffs_obj,
1928 hash_link)->obj_id == n) {
1938 static void yaffs_hash_obj(struct yaffs_obj *in)
1940 int bucket = yaffs_hash_fn(in->obj_id);
1941 struct yaffs_dev *dev = in->my_dev;
1943 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1944 dev->obj_bucket[bucket].count++;
1947 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1949 int bucket = yaffs_hash_fn(number);
1950 struct list_head *i;
1951 struct yaffs_obj *in;
1953 list_for_each(i, &dev->obj_bucket[bucket].list) {
1954 /* Look if it is in the list */
1955 in = list_entry(i, struct yaffs_obj, hash_link);
1956 if (in->obj_id == number) {
1957 /* Don't tell the VFS about this one if it is defered free */
1958 if (in->defered_free)
1967 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1968 enum yaffs_obj_type type)
1970 struct yaffs_obj *the_obj = NULL;
1971 struct yaffs_tnode *tn = NULL;
1974 number = yaffs_new_obj_id(dev);
1976 if (type == YAFFS_OBJECT_TYPE_FILE) {
1977 tn = yaffs_get_tnode(dev);
1982 the_obj = yaffs_alloc_empty_obj(dev);
1985 yaffs_free_tnode(dev, tn);
1991 the_obj->rename_allowed = 1;
1992 the_obj->unlink_allowed = 1;
1993 the_obj->obj_id = number;
1994 yaffs_hash_obj(the_obj);
1995 the_obj->variant_type = type;
1996 yaffs_load_current_time(the_obj, 1, 1);
1999 case YAFFS_OBJECT_TYPE_FILE:
2000 the_obj->variant.file_variant.file_size = 0;
2001 the_obj->variant.file_variant.scanned_size = 0;
2002 the_obj->variant.file_variant.shrink_size = ~0; /* max */
2003 the_obj->variant.file_variant.top_level = 0;
2004 the_obj->variant.file_variant.top = tn;
2006 case YAFFS_OBJECT_TYPE_DIRECTORY:
2007 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
2008 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
2010 case YAFFS_OBJECT_TYPE_SYMLINK:
2011 case YAFFS_OBJECT_TYPE_HARDLINK:
2012 case YAFFS_OBJECT_TYPE_SPECIAL:
2013 /* No action required */
2015 case YAFFS_OBJECT_TYPE_UNKNOWN:
2016 /* todo this should not happen */
2024 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2025 int number, u32 mode)
2028 struct yaffs_obj *obj =
2029 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2031 obj->fake = 1; /* it is fake so it might have no NAND presence... */
2032 obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
2033 obj->unlink_allowed = 0; /* ... or unlink it */
2036 obj->yst_mode = mode;
2038 obj->hdr_chunk = 0; /* Not a valid chunk. */
2046 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2053 yaffs_init_raw_tnodes_and_objs(dev);
2055 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2056 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2057 dev->obj_bucket[i].count = 0;
2061 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2063 enum yaffs_obj_type type)
2065 struct yaffs_obj *the_obj = NULL;
2068 the_obj = yaffs_find_by_number(dev, number);
2071 the_obj = yaffs_new_obj(dev, number, type);
2077 YCHAR *yaffs_clone_str(const YCHAR * str)
2079 YCHAR *new_str = NULL;
2085 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2086 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2088 strncpy(new_str, str, len);
2095 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2096 * link (ie. name) is created or deleted in the directory.
2099 * create dir/a : update dir's mtime/ctime
2100 * rm dir/a: update dir's mtime/ctime
2101 * modify dir/a: don't update dir's mtimme/ctime
2103 * This can be handled immediately or defered. Defering helps reduce the number
2104 * of updates when many files in a directory are changed within a brief period.
2106 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2107 * called periodically.
2110 static void yaffs_update_parent(struct yaffs_obj *obj)
2112 struct yaffs_dev *dev;
2117 yaffs_load_current_time(obj, 0, 1);
2118 if (dev->param.defered_dir_update) {
2119 struct list_head *link = &obj->variant.dir_variant.dirty;
2121 if (list_empty(link)) {
2122 list_add(link, &dev->dirty_dirs);
2123 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2124 "Added object %d to dirty directories",
2129 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2133 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2135 struct list_head *link;
2136 struct yaffs_obj *obj;
2137 struct yaffs_dir_var *d_s;
2138 union yaffs_obj_var *o_v;
2140 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2142 while (!list_empty(&dev->dirty_dirs)) {
2143 link = dev->dirty_dirs.next;
2144 list_del_init(link);
2146 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2147 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2148 obj = list_entry(o_v, struct yaffs_obj, variant);
2150 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2154 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2159 * Mknod (create) a new object.
2160 * equiv_obj only has meaning for a hard link;
2161 * alias_str only has meaning for a symlink.
2162 * rdev only has meaning for devices (a subset of special objects)
2165 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2166 struct yaffs_obj *parent,
2171 struct yaffs_obj *equiv_obj,
2172 const YCHAR * alias_str, u32 rdev)
2174 struct yaffs_obj *in;
2177 struct yaffs_dev *dev = parent->my_dev;
2179 /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
2180 if (yaffs_find_by_name(parent, name))
2183 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2184 str = yaffs_clone_str(alias_str);
2189 in = yaffs_new_obj(dev, -1, type);
2200 in->variant_type = type;
2202 in->yst_mode = mode;
2204 yaffs_attribs_init(in, gid, uid, rdev);
2206 in->n_data_chunks = 0;
2208 yaffs_set_obj_name(in, name);
2211 yaffs_add_obj_to_dir(parent, in);
2213 in->my_dev = parent->my_dev;
2216 case YAFFS_OBJECT_TYPE_SYMLINK:
2217 in->variant.symlink_variant.alias = str;
2219 case YAFFS_OBJECT_TYPE_HARDLINK:
2220 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2221 in->variant.hardlink_variant.equiv_id =
2223 list_add(&in->hard_links, &equiv_obj->hard_links);
2225 case YAFFS_OBJECT_TYPE_FILE:
2226 case YAFFS_OBJECT_TYPE_DIRECTORY:
2227 case YAFFS_OBJECT_TYPE_SPECIAL:
2228 case YAFFS_OBJECT_TYPE_UNKNOWN:
2233 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2234 /* Could not create the object header, fail the creation */
2239 yaffs_update_parent(parent);
2245 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2246 const YCHAR * name, u32 mode, u32 uid,
2249 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2250 uid, gid, NULL, NULL, 0);
2253 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
2254 u32 mode, u32 uid, u32 gid)
2256 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2257 mode, uid, gid, NULL, NULL, 0);
2260 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2261 const YCHAR * name, u32 mode, u32 uid,
2264 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2265 uid, gid, NULL, NULL, rdev);
2268 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2269 const YCHAR * name, u32 mode, u32 uid,
2270 u32 gid, const YCHAR * alias)
2272 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2273 uid, gid, NULL, alias, 0);
2276 /* yaffs_link_obj returns the object id of the equivalent object.*/
2277 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2278 struct yaffs_obj *equiv_obj)
2280 /* Get the real object in case we were fed a hard link as an equivalent object */
2281 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2283 if (yaffs_create_obj
2284 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2285 equiv_obj, NULL, 0)) {
2295 /*------------------------- Block Management and Page Allocation ----------------*/
2297 static int yaffs_init_blocks(struct yaffs_dev *dev)
2299 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2301 dev->block_info = NULL;
2302 dev->chunk_bits = NULL;
2304 dev->alloc_block = -1; /* force it to get a new one */
2306 /* If the first allocation strategy fails, thry the alternate one */
2308 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2309 if (!dev->block_info) {
2311 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2312 dev->block_info_alt = 1;
2314 dev->block_info_alt = 0;
2317 if (dev->block_info) {
2318 /* Set up dynamic blockinfo stuff. Round up bytes. */
2319 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2321 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2322 if (!dev->chunk_bits) {
2324 vmalloc(dev->chunk_bit_stride * n_blocks);
2325 dev->chunk_bits_alt = 1;
2327 dev->chunk_bits_alt = 0;
2331 if (dev->block_info && dev->chunk_bits) {
2332 memset(dev->block_info, 0,
2333 n_blocks * sizeof(struct yaffs_block_info));
2334 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2341 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2343 if (dev->block_info_alt && dev->block_info)
2344 vfree(dev->block_info);
2345 else if (dev->block_info)
2346 kfree(dev->block_info);
2348 dev->block_info_alt = 0;
2350 dev->block_info = NULL;
2352 if (dev->chunk_bits_alt && dev->chunk_bits)
2353 vfree(dev->chunk_bits);
2354 else if (dev->chunk_bits)
2355 kfree(dev->chunk_bits);
2356 dev->chunk_bits_alt = 0;
2357 dev->chunk_bits = NULL;
2360 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2362 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2366 /* If the block is still healthy erase it and mark as clean.
2367 * If the block has had a data failure, then retire it.
2370 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2371 "yaffs_block_became_dirty block %d state %d %s",
2372 block_no, bi->block_state,
2373 (bi->needs_retiring) ? "needs retiring" : "");
2375 yaffs2_clear_oldest_dirty_seq(dev, bi);
2377 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2379 /* If this is the block being garbage collected then stop gc'ing this block */
2380 if (block_no == dev->gc_block)
2383 /* If this block is currently the best candidate for gc then drop as a candidate */
2384 if (block_no == dev->gc_dirtiest) {
2385 dev->gc_dirtiest = 0;
2386 dev->gc_pages_in_use = 0;
2389 if (!bi->needs_retiring) {
2390 yaffs2_checkpt_invalidate(dev);
2391 erased_ok = yaffs_erase_block(dev, block_no);
2393 dev->n_erase_failures++;
2394 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2395 "**>> Erasure failed %d", block_no);
2400 ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
2401 || !yaffs_skip_verification(dev))) {
2403 for (i = 0; i < dev->param.chunks_per_block; i++) {
2404 if (!yaffs_check_chunk_erased
2405 (dev, block_no * dev->param.chunks_per_block + i)) {
2406 yaffs_trace(YAFFS_TRACE_ERROR,
2407 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2414 /* Clean it up... */
2415 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2417 dev->n_erased_blocks++;
2418 bi->pages_in_use = 0;
2419 bi->soft_del_pages = 0;
2420 bi->has_shrink_hdr = 0;
2421 bi->skip_erased_check = 1; /* Clean, so no need to check */
2422 bi->gc_prioritise = 0;
2423 yaffs_clear_chunk_bits(dev, block_no);
2425 yaffs_trace(YAFFS_TRACE_ERASE,
2426 "Erased block %d", block_no);
2428 /* We lost a block of free space */
2429 dev->n_free_chunks -= dev->param.chunks_per_block;
2430 yaffs_retire_block(dev, block_no);
2431 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2432 "**>> Block %d retired", block_no);
2438 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2443 int ret_val = YAFFS_OK;
2445 int is_checkpt_block;
2449 int chunks_before = yaffs_get_erased_chunks(dev);
2452 struct yaffs_ext_tags tags;
2454 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2456 struct yaffs_obj *object;
2458 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2460 yaffs_trace(YAFFS_TRACE_TRACING,
2461 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2462 block, bi->pages_in_use, bi->has_shrink_hdr,
2465 /*yaffs_verify_free_chunks(dev); */
2467 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2468 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2470 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2472 dev->gc_disable = 1;
2474 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2475 yaffs_trace(YAFFS_TRACE_TRACING,
2476 "Collecting block %d that has no chunks in use",
2478 yaffs_block_became_dirty(dev, block);
2481 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2483 yaffs_verify_blk(dev, bi, block);
2485 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2486 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2488 for ( /* init already done */ ;
2489 ret_val == YAFFS_OK &&
2490 dev->gc_chunk < dev->param.chunks_per_block &&
2491 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2492 max_copies > 0; dev->gc_chunk++, old_chunk++) {
2493 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2495 /* This page is in use and might need to be copied off */
2501 yaffs_init_tags(&tags);
2503 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2506 object = yaffs_find_by_number(dev, tags.obj_id);
2508 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2509 "Collecting chunk in block %d, %d %d %d ",
2510 dev->gc_chunk, tags.obj_id,
2511 tags.chunk_id, tags.n_bytes);
2513 if (object && !yaffs_skip_verification(dev)) {
2514 if (tags.chunk_id == 0)
2517 else if (object->soft_del)
2518 matching_chunk = old_chunk; /* Defeat the test */
2521 yaffs_find_chunk_in_file
2522 (object, tags.chunk_id,
2525 if (old_chunk != matching_chunk)
2526 yaffs_trace(YAFFS_TRACE_ERROR,
2527 "gc: page in gc mismatch: %d %d %d %d",
2536 yaffs_trace(YAFFS_TRACE_ERROR,
2537 "page %d in gc has no object: %d %d %d ",
2539 tags.obj_id, tags.chunk_id,
2545 object->soft_del && tags.chunk_id != 0) {
2546 /* Data chunk in a soft deleted file, throw it away
2547 * It's a soft deleted data chunk,
2548 * No need to copy this, just forget about it and
2549 * fix up the object.
2552 /* Free chunks already includes softdeleted chunks.
2553 * How ever this chunk is going to soon be really deleted
2554 * which will increment free chunks.
2555 * We have to decrement free chunks so this works out properly.
2557 dev->n_free_chunks--;
2558 bi->soft_del_pages--;
2560 object->n_data_chunks--;
2562 if (object->n_data_chunks <= 0) {
2563 /* remeber to clean up the object */
2564 dev->gc_cleanup_list[dev->
2571 /* Todo object && object->deleted && object->n_data_chunks == 0 */
2572 /* Deleted object header with no data chunks.
2573 * Can be discarded and the file deleted.
2575 object->hdr_chunk = 0;
2576 yaffs_free_tnode(object->my_dev,
2578 variant.file_variant.
2580 object->variant.file_variant.top = NULL;
2581 yaffs_generic_obj_del(object);
2583 } else if (object) {
2584 /* It's either a data chunk in a live file or
2585 * an ObjectHeader, so we're interested in it.
2586 * NB Need to keep the ObjectHeaders of deleted files
2587 * until the whole file has been deleted off
2589 tags.serial_number++;
2593 if (tags.chunk_id == 0) {
2594 /* It is an object Id,
2595 * We need to nuke the shrinkheader flags first
2596 * Also need to clean up shadowing.
2597 * We no longer want the shrink_header flag since its work is done
2598 * and if it is left in place it will mess up scanning.
2601 struct yaffs_obj_hdr *oh;
2602 oh = (struct yaffs_obj_hdr *)
2606 tags.extra_is_shrink = 0;
2608 oh->shadows_obj = 0;
2609 oh->inband_shadowed_obj_id = 0;
2610 tags.extra_shadows = 0;
2612 /* Update file size */
2613 if (object->variant_type ==
2614 YAFFS_OBJECT_TYPE_FILE) {
2623 yaffs_verify_oh(object, oh,
2626 yaffs_write_new_chunk(dev,
2633 yaffs_write_new_chunk(dev,
2639 if (new_chunk < 0) {
2640 ret_val = YAFFS_FAIL;
2643 /* Ok, now fix up the Tnodes etc. */
2645 if (tags.chunk_id == 0) {
2652 /* It's a data chunk */
2654 ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
2659 if (ret_val == YAFFS_OK)
2660 yaffs_chunk_del(dev, old_chunk,
2661 mark_flash, __LINE__);
2666 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2670 yaffs_verify_collected_blk(dev, bi, block);
2672 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2674 * The gc did not complete. Set block state back to FULL
2675 * because checkpointing does not restore gc.
2677 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2679 /* The gc completed. */
2680 /* Do any required cleanups */
2681 for (i = 0; i < dev->n_clean_ups; i++) {
2682 /* Time to delete the file too */
2684 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2686 yaffs_free_tnode(dev,
2689 object->variant.file_variant.top = NULL;
2690 yaffs_trace(YAFFS_TRACE_GC,
2691 "yaffs: About to finally delete object %d",
2693 yaffs_generic_obj_del(object);
2694 object->my_dev->n_deleted_files--;
2699 chunks_after = yaffs_get_erased_chunks(dev);
2700 if (chunks_before >= chunks_after)
2701 yaffs_trace(YAFFS_TRACE_GC,
2702 "gc did not increase free chunks before %d after %d",
2703 chunks_before, chunks_after);
2706 dev->n_clean_ups = 0;
2709 dev->gc_disable = 0;
2715 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2716 * for garbage collection.
2719 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2720 int aggressive, int background)
2724 unsigned selected = 0;
2725 int prioritised = 0;
2726 int prioritised_exist = 0;
2727 struct yaffs_block_info *bi;
2730 /* First let's see if we need to grab a prioritised block */
2731 if (dev->has_pending_prioritised_gc && !aggressive) {
2732 dev->gc_dirtiest = 0;
2733 bi = dev->block_info;
2734 for (i = dev->internal_start_block;
2735 i <= dev->internal_end_block && !selected; i++) {
2737 if (bi->gc_prioritise) {
2738 prioritised_exist = 1;
2739 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2740 yaffs_block_ok_for_gc(dev, bi)) {
2749 * If there is a prioritised block and none was selected then
2750 * this happened because there is at least one old dirty block gumming
2751 * up the works. Let's gc the oldest dirty block.
2754 if (prioritised_exist &&
2755 !selected && dev->oldest_dirty_block > 0)
2756 selected = dev->oldest_dirty_block;
2758 if (!prioritised_exist) /* None found, so we can clear this */
2759 dev->has_pending_prioritised_gc = 0;
2762 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2764 * else (we're doing a leasurely gc), then we only bother to do this if the
2765 * block has only a few pages in use.
2771 dev->internal_end_block - dev->internal_start_block + 1;
2773 threshold = dev->param.chunks_per_block;
2774 iterations = n_blocks;
2779 max_threshold = dev->param.chunks_per_block / 2;
2781 max_threshold = dev->param.chunks_per_block / 8;
2783 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2784 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2786 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2787 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2788 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2789 if (threshold > max_threshold)
2790 threshold = max_threshold;
2792 iterations = n_blocks / 16 + 1;
2793 if (iterations > 100)
2799 (dev->gc_dirtiest < 1 ||
2800 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
2801 dev->gc_block_finder++;
2802 if (dev->gc_block_finder < dev->internal_start_block ||
2803 dev->gc_block_finder > dev->internal_end_block)
2804 dev->gc_block_finder =
2805 dev->internal_start_block;
2807 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2809 pages_used = bi->pages_in_use - bi->soft_del_pages;
2811 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2812 pages_used < dev->param.chunks_per_block &&
2813 (dev->gc_dirtiest < 1
2814 || pages_used < dev->gc_pages_in_use)
2815 && yaffs_block_ok_for_gc(dev, bi)) {
2816 dev->gc_dirtiest = dev->gc_block_finder;
2817 dev->gc_pages_in_use = pages_used;
2821 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2822 selected = dev->gc_dirtiest;
2826 * If nothing has been selected for a while, try selecting the oldest dirty
2827 * because that's gumming up the works.
2830 if (!selected && dev->param.is_yaffs2 &&
2831 dev->gc_not_done >= (background ? 10 : 20)) {
2832 yaffs2_find_oldest_dirty_seq(dev);
2833 if (dev->oldest_dirty_block > 0) {
2834 selected = dev->oldest_dirty_block;
2835 dev->gc_dirtiest = selected;
2836 dev->oldest_dirty_gc_count++;
2837 bi = yaffs_get_block_info(dev, selected);
2838 dev->gc_pages_in_use =
2839 bi->pages_in_use - bi->soft_del_pages;
2841 dev->gc_not_done = 0;
2846 yaffs_trace(YAFFS_TRACE_GC,
2847 "GC Selected block %d with %d free, prioritised:%d",
2849 dev->param.chunks_per_block - dev->gc_pages_in_use,
2856 dev->gc_dirtiest = 0;
2857 dev->gc_pages_in_use = 0;
2858 dev->gc_not_done = 0;
2859 if (dev->refresh_skip > 0)
2860 dev->refresh_skip--;
2863 yaffs_trace(YAFFS_TRACE_GC,
2864 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2865 dev->gc_block_finder, dev->gc_not_done, threshold,
2866 dev->gc_dirtiest, dev->gc_pages_in_use,
2867 dev->oldest_dirty_block, background ? " bg" : "");
2873 /* New garbage collector
2874 * If we're very low on erased blocks then we do aggressive garbage collection
2875 * otherwise we do "leasurely" garbage collection.
2876 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2877 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2879 * The idea is to help clear out space in a more spread-out manner.
2880 * Dunno if it really does anything useful.
2882 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2885 int gc_ok = YAFFS_OK;
2889 int checkpt_block_adjust;
2891 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2894 if (dev->gc_disable) {
2895 /* Bail out so we don't get recursive gc */
2899 /* This loop should pass the first time.
2900 * We'll only see looping here if the collection does not increase space.
2906 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2909 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2911 dev->n_erased_blocks * dev->param.chunks_per_block;
2913 /* If we need a block soon then do aggressive gc. */
2914 if (dev->n_erased_blocks < min_erased)
2918 && erased_chunks > (dev->n_free_chunks / 4))
2921 if (dev->gc_skip > 20)
2923 if (erased_chunks < dev->n_free_chunks / 2 ||
2924 dev->gc_skip < 1 || background)
2934 /* If we don't already have a block being gc'd then see if we should start another */
2936 if (dev->gc_block < 1 && !aggressive) {
2937 dev->gc_block = yaffs2_find_refresh_block(dev);
2939 dev->n_clean_ups = 0;
2941 if (dev->gc_block < 1) {
2943 yaffs_find_gc_block(dev, aggressive, background);
2945 dev->n_clean_ups = 0;
2948 if (dev->gc_block > 0) {
2951 dev->passive_gc_count++;
2953 yaffs_trace(YAFFS_TRACE_GC,
2954 "yaffs: GC n_erased_blocks %d aggressive %d",
2955 dev->n_erased_blocks, aggressive);
2957 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2960 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
2961 && dev->gc_block > 0) {
2962 yaffs_trace(YAFFS_TRACE_GC,
2963 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2964 dev->n_erased_blocks, max_tries,
2967 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2968 (dev->gc_block > 0) && (max_tries < 2));
2970 return aggressive ? gc_ok : YAFFS_OK;
2975 * Garbage collects. Intended to be called from a background thread.
2976 * Returns non-zero if at least half the free chunks are erased.
2978 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2980 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2982 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2984 yaffs_check_gc(dev, 1);
2985 return erased_chunks > dev->n_free_chunks / 2;
2988 /*-------------------- Data file manipulation -----------------*/
2990 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2992 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2994 if (nand_chunk >= 0)
2995 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2998 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2999 "Chunk %d not found zero instead",
3001 /* get sane (zero) data if you read a hole */
3002 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
3008 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
3013 struct yaffs_ext_tags tags;
3014 struct yaffs_block_info *bi;
3020 block = chunk_id / dev->param.chunks_per_block;
3021 page = chunk_id % dev->param.chunks_per_block;
3023 if (!yaffs_check_chunk_bit(dev, block, page))
3024 yaffs_trace(YAFFS_TRACE_VERIFY,
3025 "Deleting invalid chunk %d", chunk_id);
3027 bi = yaffs_get_block_info(dev, block);
3029 yaffs2_update_oldest_dirty_seq(dev, block, bi);
3031 yaffs_trace(YAFFS_TRACE_DELETION,
3032 "line %d delete of chunk %d",
3035 if (!dev->param.is_yaffs2 && mark_flash &&
3036 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
3038 yaffs_init_tags(&tags);
3040 tags.is_deleted = 1;
3042 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3043 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3045 dev->n_unmarked_deletions++;
3048 /* Pull out of the management area.
3049 * If the whole block became dirty, this will kick off an erasure.
3051 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
3052 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
3053 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3054 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
3055 dev->n_free_chunks++;
3057 yaffs_clear_chunk_bit(dev, block, page);
3061 if (bi->pages_in_use == 0 &&
3062 !bi->has_shrink_hdr &&
3063 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
3064 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3065 yaffs_block_became_dirty(dev, block);
3072 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3073 const u8 * buffer, int n_bytes, int use_reserve)
3075 /* Find old chunk Need to do this to get serial number
3076 * Write new one and patch into tree.
3077 * Invalidate old tags.
3081 struct yaffs_ext_tags prev_tags;
3084 struct yaffs_ext_tags new_tags;
3086 struct yaffs_dev *dev = in->my_dev;
3088 yaffs_check_gc(dev, 0);
3090 /* Get the previous chunk at this location in the file if it exists.
3091 * If it does not exist then put a zero into the tree. This creates
3092 * the tnode now, rather than later when it is harder to clean up.
3094 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3095 if (prev_chunk_id < 1 &&
3096 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3099 /* Set up new tags */
3100 yaffs_init_tags(&new_tags);
3102 new_tags.chunk_id = inode_chunk;
3103 new_tags.obj_id = in->obj_id;
3104 new_tags.serial_number =
3105 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3106 new_tags.n_bytes = n_bytes;
3108 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3109 yaffs_trace(YAFFS_TRACE_ERROR,
3110 "Writing %d bytes to chunk!!!!!!!!!",
3116 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3118 if (new_chunk_id > 0) {
3119 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3121 if (prev_chunk_id > 0)
3122 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3124 yaffs_verify_file_sane(in);
3126 return new_chunk_id;
3132 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3133 const YCHAR * name, const void *value, int size,
3136 struct yaffs_xattr_mod xmod;
3145 xmod.result = -ENOSPC;
3147 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3155 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3156 struct yaffs_xattr_mod *xmod)
3159 int x_offs = sizeof(struct yaffs_obj_hdr);
3160 struct yaffs_dev *dev = obj->my_dev;
3161 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3163 char *x_buffer = buffer + x_offs;
3167 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3168 xmod->size, xmod->flags);
3170 retval = nval_del(x_buffer, x_size, xmod->name);
3172 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3173 obj->xattr_known = 1;
3175 xmod->result = retval;
3180 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
3181 void *value, int size)
3183 char *buffer = NULL;
3185 struct yaffs_ext_tags tags;
3186 struct yaffs_dev *dev = obj->my_dev;
3187 int x_offs = sizeof(struct yaffs_obj_hdr);
3188 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3194 if (obj->hdr_chunk < 1)
3197 /* If we know that the object has no xattribs then don't do all the
3198 * reading and parsing.
3200 if (obj->xattr_known && !obj->has_xattr) {
3207 buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
3212 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3214 if (result != YAFFS_OK)
3217 x_buffer = buffer + x_offs;
3219 if (!obj->xattr_known) {
3220 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3221 obj->xattr_known = 1;
3225 retval = nval_get(x_buffer, x_size, name, value, size);
3227 retval = nval_list(x_buffer, x_size, value, size);
3229 yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
3233 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3234 const void *value, int size, int flags)
3236 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3239 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3241 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3244 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3247 return yaffs_do_xattrib_fetch(obj, name, value, size);
3250 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3252 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3255 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3258 struct yaffs_obj_hdr *oh;
3259 struct yaffs_dev *dev;
3260 struct yaffs_ext_tags tags;
3262 int alloc_failed = 0;
3269 if (in->lazy_loaded && in->hdr_chunk > 0) {
3270 in->lazy_loaded = 0;
3271 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
3274 yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
3276 oh = (struct yaffs_obj_hdr *)chunk_data;
3278 in->yst_mode = oh->yst_mode;
3279 yaffs_load_attribs(in, oh);
3280 yaffs_set_obj_name_from_oh(in, oh);
3282 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3283 in->variant.symlink_variant.alias =
3284 yaffs_clone_str(oh->alias);
3285 if (!in->variant.symlink_variant.alias)
3286 alloc_failed = 1; /* Not returned to caller */
3289 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
3293 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
3294 const YCHAR * oh_name, int buff_size)
3296 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3297 if (dev->param.auto_unicode) {
3299 /* It is an ASCII name, do an ASCII to
3300 * unicode conversion */
3301 const char *ascii_oh_name = (const char *)oh_name;
3302 int n = buff_size - 1;
3303 while (n > 0 && *ascii_oh_name) {
3304 *name = *ascii_oh_name;
3310 strncpy(name, oh_name + 1, buff_size - 1);
3316 strncpy(name, oh_name, buff_size - 1);
3320 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
3323 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3328 if (dev->param.auto_unicode) {
3333 /* Figure out if the name will fit in ascii character set */
3334 while (is_ascii && *w) {
3341 /* It is an ASCII name, so do a unicode to ascii conversion */
3342 char *ascii_oh_name = (char *)oh_name;
3343 int n = YAFFS_MAX_NAME_LENGTH - 1;
3344 while (n > 0 && *name) {
3345 *ascii_oh_name = *name;
3351 /* It is a unicode name, so save starting at the second YCHAR */
3353 strncpy(oh_name + 1, name,
3354 YAFFS_MAX_NAME_LENGTH - 2);
3360 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3365 /* UpdateObjectHeader updates the header on NAND for an object.
3366 * If name is not NULL, then that new name is used.
3368 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
3369 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3372 struct yaffs_block_info *bi;
3374 struct yaffs_dev *dev = in->my_dev;
3381 struct yaffs_ext_tags new_tags;
3382 struct yaffs_ext_tags old_tags;
3383 const YCHAR *alias = NULL;
3386 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3388 struct yaffs_obj_hdr *oh = NULL;
3390 strcpy(old_name, _Y("silly old name"));
3392 if (!in->fake || in == dev->root_dir ||
3395 yaffs_check_gc(dev, 0);
3396 yaffs_check_obj_details_loaded(in);
3398 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
3399 oh = (struct yaffs_obj_hdr *)buffer;
3401 prev_chunk_id = in->hdr_chunk;
3403 if (prev_chunk_id > 0) {
3404 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3407 yaffs_verify_oh(in, oh, &old_tags, 0);
3409 memcpy(old_name, oh->name, sizeof(oh->name));
3410 memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
3412 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
3415 oh->type = in->variant_type;
3416 oh->yst_mode = in->yst_mode;
3417 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3419 yaffs_load_attribs_oh(oh, in);
3422 oh->parent_obj_id = in->parent->obj_id;
3424 oh->parent_obj_id = 0;
3426 if (name && *name) {
3427 memset(oh->name, 0, sizeof(oh->name));
3428 yaffs_load_oh_from_name(dev, oh->name, name);
3429 } else if (prev_chunk_id > 0) {
3430 memcpy(oh->name, old_name, sizeof(oh->name));
3432 memset(oh->name, 0, sizeof(oh->name));
3435 oh->is_shrink = is_shrink;
3437 switch (in->variant_type) {
3438 case YAFFS_OBJECT_TYPE_UNKNOWN:
3439 /* Should not happen */
3441 case YAFFS_OBJECT_TYPE_FILE:
3443 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
3444 || oh->parent_obj_id ==
3445 YAFFS_OBJECTID_UNLINKED) ? 0 : in->
3446 variant.file_variant.file_size;
3448 case YAFFS_OBJECT_TYPE_HARDLINK:
3449 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3451 case YAFFS_OBJECT_TYPE_SPECIAL:
3454 case YAFFS_OBJECT_TYPE_DIRECTORY:
3457 case YAFFS_OBJECT_TYPE_SYMLINK:
3458 alias = in->variant.symlink_variant.alias;
3460 alias = _Y("no alias");
3461 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3462 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3466 /* process any xattrib modifications */
3468 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3471 yaffs_init_tags(&new_tags);
3473 new_tags.chunk_id = 0;
3474 new_tags.obj_id = in->obj_id;
3475 new_tags.serial_number = in->serial;
3477 /* Add extra info for file header */
3479 new_tags.extra_available = 1;
3480 new_tags.extra_parent_id = oh->parent_obj_id;
3481 new_tags.extra_length = oh->file_size;
3482 new_tags.extra_is_shrink = oh->is_shrink;
3483 new_tags.extra_equiv_id = oh->equiv_id;
3484 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3485 new_tags.extra_obj_type = in->variant_type;
3487 yaffs_verify_oh(in, oh, &new_tags, 1);
3489 /* Create new chunk in NAND */
3491 yaffs_write_new_chunk(dev, buffer, &new_tags,
3492 (prev_chunk_id > 0) ? 1 : 0);
3494 if (new_chunk_id >= 0) {
3496 in->hdr_chunk = new_chunk_id;
3498 if (prev_chunk_id > 0) {
3499 yaffs_chunk_del(dev, prev_chunk_id, 1,
3503 if (!yaffs_obj_cache_dirty(in))
3506 /* If this was a shrink, then mark the block that the chunk lives on */
3508 bi = yaffs_get_block_info(in->my_dev,
3512 bi->has_shrink_hdr = 1;
3517 ret_val = new_chunk_id;
3522 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3527 /*--------------------- File read/write ------------------------
3528 * Read and write have very similar structures.
3529 * In general the read/write has three parts to it
3530 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3531 * Some complete chunks
3532 * An incomplete chunk to end off with
3534 * Curve-balls: the first chunk might also be the last chunk.
3537 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3545 struct yaffs_cache *cache;
3547 struct yaffs_dev *dev;
3552 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3553 /* start = offset % dev->data_bytes_per_chunk; */
3554 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3557 /* OK now check for the curveball where the start and end are in
3560 if ((start + n) < dev->data_bytes_per_chunk)
3563 n_copy = dev->data_bytes_per_chunk - start;
3565 cache = yaffs_find_chunk_cache(in, chunk);
3567 /* If the chunk is already in the cache or it is less than a whole chunk
3568 * or we're using inband tags then use the cache (if there is caching)
3569 * else bypass the cache.
3571 if (cache || n_copy != dev->data_bytes_per_chunk
3572 || dev->param.inband_tags) {
3573 if (dev->param.n_caches > 0) {
3575 /* If we can't find the data in the cache, then load it up. */
3579 yaffs_grab_chunk_cache(in->my_dev);
3581 cache->chunk_id = chunk;
3584 yaffs_rd_data_obj(in, chunk,
3589 yaffs_use_cache(dev, cache, 0);
3593 memcpy(buffer, &cache->data[start], n_copy);
3597 /* Read into the local buffer then copy.. */
3600 yaffs_get_temp_buffer(dev, __LINE__);
3601 yaffs_rd_data_obj(in, chunk, local_buffer);
3603 memcpy(buffer, &local_buffer[start], n_copy);
3605 yaffs_release_temp_buffer(dev, local_buffer,
3611 /* A full chunk. Read directly into the supplied buffer. */
3612 yaffs_rd_data_obj(in, chunk, buffer);
3626 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3627 int n_bytes, int write_trhrough)
3636 int start_write = offset;
3637 int chunk_written = 0;
3641 struct yaffs_dev *dev;
3645 while (n > 0 && chunk_written >= 0) {
3646 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3648 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3649 start >= dev->data_bytes_per_chunk) {
3650 yaffs_trace(YAFFS_TRACE_ERROR,
3651 "AddrToChunk of offset %d gives chunk %d start %d",
3652 (int)offset, chunk, start);
3654 chunk++; /* File pos to chunk in file offset */
3656 /* OK now check for the curveball where the start and end are in
3660 if ((start + n) < dev->data_bytes_per_chunk) {
3663 /* Now folks, to calculate how many bytes to write back....
3664 * If we're overwriting and not writing to then end of file then
3665 * we need to write back as much as was there before.
3668 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3670 if (chunk_start > in->variant.file_variant.file_size)
3671 n_bytes_read = 0; /* Past end of file */
3674 in->variant.file_variant.file_size -
3677 if (n_bytes_read > dev->data_bytes_per_chunk)
3678 n_bytes_read = dev->data_bytes_per_chunk;
3682 (start + n)) ? n_bytes_read : (start + n);
3685 || n_writeback > dev->data_bytes_per_chunk)
3689 n_copy = dev->data_bytes_per_chunk - start;
3690 n_writeback = dev->data_bytes_per_chunk;
3693 if (n_copy != dev->data_bytes_per_chunk
3694 || dev->param.inband_tags) {
3695 /* An incomplete start or end chunk (or maybe both start and end chunk),
3696 * or we're using inband tags, so we want to use the cache buffers.
3698 if (dev->param.n_caches > 0) {
3699 struct yaffs_cache *cache;
3700 /* If we can't find the data in the cache, then load the cache */
3701 cache = yaffs_find_chunk_cache(in, chunk);
3704 && yaffs_check_alloc_available(dev, 1)) {
3705 cache = yaffs_grab_chunk_cache(dev);
3707 cache->chunk_id = chunk;
3710 yaffs_rd_data_obj(in, chunk,
3714 !yaffs_check_alloc_available(dev,
3716 /* Drop the cache if it was a read cache item and
3717 * no space check has been made for it.
3723 yaffs_use_cache(dev, cache, 1);
3726 memcpy(&cache->data[start], buffer,
3730 cache->n_bytes = n_writeback;
3732 if (write_trhrough) {
3743 chunk_written = -1; /* fail the write */
3746 /* An incomplete start or end chunk (or maybe both start and end chunk)
3747 * Read into the local buffer then copy, then copy over and write back.
3751 yaffs_get_temp_buffer(dev, __LINE__);
3753 yaffs_rd_data_obj(in, chunk, local_buffer);
3755 memcpy(&local_buffer[start], buffer, n_copy);
3758 yaffs_wr_data_obj(in, chunk,
3762 yaffs_release_temp_buffer(dev, local_buffer,
3768 /* A full chunk. Write directly from the supplied buffer. */
3771 yaffs_wr_data_obj(in, chunk, buffer,
3772 dev->data_bytes_per_chunk, 0);
3774 /* Since we've overwritten the cached data, we better invalidate it. */
3775 yaffs_invalidate_chunk_cache(in, chunk);
3778 if (chunk_written >= 0) {
3787 /* Update file object */
3789 if ((start_write + n_done) > in->variant.file_variant.file_size)
3790 in->variant.file_variant.file_size = (start_write + n_done);
3797 int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3798 int n_bytes, int write_trhrough)
3800 yaffs2_handle_hole(in, offset);
3801 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3804 /* ---------------------- File resizing stuff ------------------ */
3806 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3809 struct yaffs_dev *dev = in->my_dev;
3810 int old_size = in->variant.file_variant.file_size;
3812 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3814 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3815 dev->data_bytes_per_chunk;
3819 /* Delete backwards so that we don't end up with holes if
3820 * power is lost part-way through the operation.
3822 for (i = last_del; i >= start_del; i--) {
3823 /* NB this could be optimised somewhat,
3824 * eg. could retrieve the tags and write them without
3825 * using yaffs_chunk_del
3828 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3831 (dev->internal_start_block *
3832 dev->param.chunks_per_block)
3834 ((dev->internal_end_block +
3835 1) * dev->param.chunks_per_block)) {
3836 yaffs_trace(YAFFS_TRACE_ALWAYS,
3837 "Found daft chunk_id %d for %d",
3840 in->n_data_chunks--;
3841 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3848 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3852 struct yaffs_dev *dev = obj->my_dev;
3854 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3856 yaffs_prune_chunks(obj, new_size);
3858 if (new_partial != 0) {
3859 int last_chunk = 1 + new_full;
3860 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3862 /* Rewrite the last chunk with its new size and zero pad */
3863 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3864 memset(local_buffer + new_partial, 0,
3865 dev->data_bytes_per_chunk - new_partial);
3867 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3870 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3873 obj->variant.file_variant.file_size = new_size;
3875 yaffs_prune_tree(dev, &obj->variant.file_variant);
3878 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3880 struct yaffs_dev *dev = in->my_dev;
3881 int old_size = in->variant.file_variant.file_size;
3883 yaffs_flush_file_cache(in);
3884 yaffs_invalidate_whole_cache(in);
3886 yaffs_check_gc(dev, 0);
3888 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3891 if (new_size == old_size)
3894 if (new_size > old_size) {
3895 yaffs2_handle_hole(in, new_size);
3896 in->variant.file_variant.file_size = new_size;
3898 /* new_size < old_size */
3899 yaffs_resize_file_down(in, new_size);
3902 /* Write a new object header to reflect the resize.
3903 * show we've shrunk the file, if need be
3904 * Do this only if the file is not in the deleted directories
3905 * and is not shadowed.
3909 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3910 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3911 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3916 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3920 yaffs_flush_file_cache(in);
3921 if (data_sync) /* Only sync data */
3925 yaffs_load_current_time(in, 0, 0);
3927 ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3928 0) ? YAFFS_OK : YAFFS_FAIL;
3939 /* yaffs_del_file deletes the whole file data
3940 * and the inode associated with the file.
3941 * It does not delete the links associated with the file.
3943 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3948 struct yaffs_dev *dev = in->my_dev;
3955 yaffs_change_obj_name(in, in->my_dev->del_dir,
3956 _Y("deleted"), 0, 0);
3957 yaffs_trace(YAFFS_TRACE_TRACING,
3958 "yaffs: immediate deletion of file %d",
3961 in->my_dev->n_deleted_files++;
3962 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3963 yaffs_resize_file(in, 0);
3964 yaffs_soft_del_file(in);
3967 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3968 _Y("unlinked"), 0, 0);
3974 int yaffs_del_file(struct yaffs_obj *in)
3976 int ret_val = YAFFS_OK;
3977 int deleted; /* Need to cache value on stack if in is freed */
3978 struct yaffs_dev *dev = in->my_dev;
3980 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3981 yaffs_resize_file(in, 0);
3983 if (in->n_data_chunks > 0) {
3984 /* Use soft deletion if there is data in the file.
3985 * That won't be the case if it has been resized to zero.
3988 ret_val = yaffs_unlink_file_if_needed(in);
3990 deleted = in->deleted;
3992 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3995 in->my_dev->n_deleted_files++;
3996 yaffs_soft_del_file(in);
3998 return deleted ? YAFFS_OK : YAFFS_FAIL;
4000 /* The file has no data chunks so we toss it immediately */
4001 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
4002 in->variant.file_variant.top = NULL;
4003 yaffs_generic_obj_del(in);
4009 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
4012 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
4013 !(list_empty(&obj->variant.dir_variant.children));
4016 static int yaffs_del_dir(struct yaffs_obj *obj)
4018 /* First check that the directory is empty. */
4019 if (yaffs_is_non_empty_dir(obj))
4022 return yaffs_generic_obj_del(obj);
4025 static int yaffs_del_symlink(struct yaffs_obj *in)
4027 if (in->variant.symlink_variant.alias)
4028 kfree(in->variant.symlink_variant.alias);
4029 in->variant.symlink_variant.alias = NULL;
4031 return yaffs_generic_obj_del(in);
4034 static int yaffs_del_link(struct yaffs_obj *in)
4036 /* remove this hardlink from the list assocaited with the equivalent
4039 list_del_init(&in->hard_links);
4040 return yaffs_generic_obj_del(in);
4043 int yaffs_del_obj(struct yaffs_obj *obj)
4046 switch (obj->variant_type) {
4047 case YAFFS_OBJECT_TYPE_FILE:
4048 ret_val = yaffs_del_file(obj);
4050 case YAFFS_OBJECT_TYPE_DIRECTORY:
4051 if (!list_empty(&obj->variant.dir_variant.dirty)) {
4052 yaffs_trace(YAFFS_TRACE_BACKGROUND,
4053 "Remove object %d from dirty directories",
4055 list_del_init(&obj->variant.dir_variant.dirty);
4057 return yaffs_del_dir(obj);
4059 case YAFFS_OBJECT_TYPE_SYMLINK:
4060 ret_val = yaffs_del_symlink(obj);
4062 case YAFFS_OBJECT_TYPE_HARDLINK:
4063 ret_val = yaffs_del_link(obj);
4065 case YAFFS_OBJECT_TYPE_SPECIAL:
4066 ret_val = yaffs_generic_obj_del(obj);
4068 case YAFFS_OBJECT_TYPE_UNKNOWN:
4070 break; /* should not happen. */
4076 static int yaffs_unlink_worker(struct yaffs_obj *obj)
4087 yaffs_update_parent(obj->parent);
4089 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4090 return yaffs_del_link(obj);
4091 } else if (!list_empty(&obj->hard_links)) {
4092 /* Curve ball: We're unlinking an object that has a hardlink.
4094 * This problem arises because we are not strictly following
4095 * The Linux link/inode model.
4097 * We can't really delete the object.
4098 * Instead, we do the following:
4099 * - Select a hardlink.
4100 * - Unhook it from the hard links
4101 * - Move it from its parent directory (so that the rename can work)
4102 * - Rename the object to the hardlink's name.
4103 * - Delete the hardlink
4106 struct yaffs_obj *hl;
4107 struct yaffs_obj *parent;
4109 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4111 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
4114 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4115 parent = hl->parent;
4117 list_del_init(&hl->hard_links);
4119 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4121 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4123 if (ret_val == YAFFS_OK)
4124 ret_val = yaffs_generic_obj_del(hl);
4128 } else if (del_now) {
4129 switch (obj->variant_type) {
4130 case YAFFS_OBJECT_TYPE_FILE:
4131 return yaffs_del_file(obj);
4133 case YAFFS_OBJECT_TYPE_DIRECTORY:
4134 list_del_init(&obj->variant.dir_variant.dirty);
4135 return yaffs_del_dir(obj);
4137 case YAFFS_OBJECT_TYPE_SYMLINK:
4138 return yaffs_del_symlink(obj);
4140 case YAFFS_OBJECT_TYPE_SPECIAL:
4141 return yaffs_generic_obj_del(obj);
4143 case YAFFS_OBJECT_TYPE_HARDLINK:
4144 case YAFFS_OBJECT_TYPE_UNKNOWN:
4148 } else if (yaffs_is_non_empty_dir(obj)) {
4151 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4152 _Y("unlinked"), 0, 0);
4156 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4159 if (obj && obj->unlink_allowed)
4160 return yaffs_unlink_worker(obj);
4166 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
4168 struct yaffs_obj *obj;
4170 obj = yaffs_find_by_name(dir, name);
4171 return yaffs_unlink_obj(obj);
4175 * If old_name is NULL then we take old_dir as the object to be renamed.
4177 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
4178 struct yaffs_obj *new_dir, const YCHAR * new_name)
4180 struct yaffs_obj *obj = NULL;
4181 struct yaffs_obj *existing_target = NULL;
4184 struct yaffs_dev *dev;
4186 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4188 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4191 dev = old_dir->my_dev;
4193 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4194 /* Special case for case insemsitive systems.
4195 * While look-up is case insensitive, the name isn't.
4196 * Therefore we might want to change x.txt to X.txt
4198 if (old_dir == new_dir &&
4199 old_name && new_name &&
4200 strcmp(old_name, new_name) == 0)
4204 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4205 YAFFS_MAX_NAME_LENGTH)
4210 obj = yaffs_find_by_name(old_dir, old_name);
4213 old_dir = obj->parent;
4217 if (obj && obj->rename_allowed) {
4219 /* Now do the handling for an existing target, if there is one */
4221 existing_target = yaffs_find_by_name(new_dir, new_name);
4222 if (yaffs_is_non_empty_dir(existing_target)){
4223 return YAFFS_FAIL; /* ENOTEMPTY */
4224 } else if (existing_target && existing_target != obj) {
4225 /* Nuke the target first, using shadowing,
4226 * but only if it isn't the same object.
4228 * Note we must disable gc otherwise it can mess up the shadowing.
4231 dev->gc_disable = 1;
4232 yaffs_change_obj_name(obj, new_dir, new_name, force,
4233 existing_target->obj_id);
4234 existing_target->is_shadowed = 1;
4235 yaffs_unlink_obj(existing_target);
4236 dev->gc_disable = 0;
4239 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4241 yaffs_update_parent(old_dir);
4242 if (new_dir != old_dir)
4243 yaffs_update_parent(new_dir);
4250 /*----------------------- Initialisation Scanning ---------------------- */
4252 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4253 int backward_scanning)
4255 struct yaffs_obj *obj;
4257 if (!backward_scanning) {
4258 /* Handle YAFFS1 forward scanning case
4259 * For YAFFS1 we always do the deletion
4263 /* Handle YAFFS2 case (backward scanning)
4264 * If the shadowed object exists then ignore.
4266 obj = yaffs_find_by_number(dev, obj_id);
4271 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4272 * We put it in unlinked dir to be cleaned up after the scanning
4275 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4278 obj->is_shadowed = 1;
4279 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4280 obj->variant.file_variant.shrink_size = 0;
4281 obj->valid = 1; /* So that we don't read any other info for this file */
4285 void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
4287 struct yaffs_obj *hl;
4288 struct yaffs_obj *in;
4292 hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
4294 in = yaffs_find_by_number(dev,
4296 hardlink_variant.equiv_id);
4299 /* Add the hardlink pointers */
4300 hl->variant.hardlink_variant.equiv_obj = in;
4301 list_add(&hl->hard_links, &in->hard_links);
4303 /* Todo Need to report/handle this better.
4304 * Got a problem... hardlink to a non-existant object
4306 hl->variant.hardlink_variant.equiv_obj = NULL;
4307 INIT_LIST_HEAD(&hl->hard_links);
4313 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4316 * Sort out state of unlinked and deleted objects after scanning.
4318 struct list_head *i;
4319 struct list_head *n;
4320 struct yaffs_obj *l;
4325 /* Soft delete all the unlinked files */
4326 list_for_each_safe(i, n,
4327 &dev->unlinked_dir->variant.dir_variant.children) {
4329 l = list_entry(i, struct yaffs_obj, siblings);
4334 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4336 l = list_entry(i, struct yaffs_obj, siblings);
4344 * This code iterates through all the objects making sure that they are rooted.
4345 * Any unrooted objects are re-rooted in lost+found.
4346 * An object needs to be in one of:
4347 * - Directly under deleted, unlinked
4348 * - Directly or indirectly under root.
4351 * This code assumes that we don't ever change the current relationships between
4353 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4354 * lost-n-found->parent == root_dir
4356 * This fixes the problem where directories might have inadvertently been deleted
4357 * leaving the object "hanging" without being rooted in the directory tree.
4360 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4362 return (obj == dev->del_dir ||
4363 obj == dev->unlinked_dir || obj == dev->root_dir);
4366 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4368 struct yaffs_obj *obj;
4369 struct yaffs_obj *parent;
4371 struct list_head *lh;
4372 struct list_head *n;
4379 /* Iterate through the objects in each hash entry,
4380 * looking at each object.
4381 * Make sure it is rooted.
4384 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4385 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4388 list_entry(lh, struct yaffs_obj, hash_link);
4389 parent = obj->parent;
4391 if (yaffs_has_null_parent(dev, obj)) {
4392 /* These directories are not hanging */
4395 || parent->variant_type !=
4396 YAFFS_OBJECT_TYPE_DIRECTORY) {
4398 } else if (yaffs_has_null_parent(dev, parent)) {
4402 * Need to follow the parent chain to see if it is hanging.
4407 while (parent != dev->root_dir &&
4409 parent->parent->variant_type ==
4410 YAFFS_OBJECT_TYPE_DIRECTORY
4411 && depth_limit > 0) {
4412 parent = parent->parent;
4415 if (parent != dev->root_dir)
4419 yaffs_trace(YAFFS_TRACE_SCAN,
4420 "Hanging object %d moved to lost and found",
4422 yaffs_add_obj_to_dir(dev->lost_n_found,
4431 * Delete directory contents for cleaning up lost and found.
4433 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4435 struct yaffs_obj *obj;
4436 struct list_head *lh;
4437 struct list_head *n;
4439 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4442 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4444 obj = list_entry(lh, struct yaffs_obj, siblings);
4445 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4446 yaffs_del_dir_contents(obj);
4448 yaffs_trace(YAFFS_TRACE_SCAN,
4449 "Deleting lost_found object %d",
4452 /* Need to use UnlinkObject since Delete would not handle
4453 * hardlinked objects correctly.
4455 yaffs_unlink_obj(obj);
4461 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4463 yaffs_del_dir_contents(dev->lost_n_found);
4467 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4472 struct list_head *i;
4473 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4475 struct yaffs_obj *l;
4481 yaffs_trace(YAFFS_TRACE_ALWAYS,
4482 "tragedy: yaffs_find_by_name: null pointer directory"
4487 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4488 yaffs_trace(YAFFS_TRACE_ALWAYS,
4489 "tragedy: yaffs_find_by_name: non-directory"
4494 sum = yaffs_calc_name_sum(name);
4496 list_for_each(i, &directory->variant.dir_variant.children) {
4497 l = list_entry(i, struct yaffs_obj, siblings);
4499 if (l->parent != directory)
4502 yaffs_check_obj_details_loaded(l);
4504 /* Special case for lost-n-found */
4505 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4506 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4508 } else if (l->sum == sum
4509 || l->hdr_chunk <= 0) {
4510 /* LostnFound chunk called Objxxx
4513 yaffs_get_obj_name(l, buffer,
4514 YAFFS_MAX_NAME_LENGTH + 1);
4515 if (strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4523 /* GetEquivalentObject dereferences any hard links to get to the
4527 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4529 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4530 /* We want the object id of the equivalent object, not this one */
4531 obj = obj->variant.hardlink_variant.equiv_obj;
4532 yaffs_check_obj_details_loaded(obj);
4538 * A note or two on object names.
4539 * * If the object name is missing, we then make one up in the form objnnn
4541 * * ASCII names are stored in the object header's name field from byte zero
4542 * * Unicode names are historically stored starting from byte zero.
4544 * Then there are automatic Unicode names...
4545 * The purpose of these is to save names in a way that can be read as
4546 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4547 * system to share files.
4549 * These automatic unicode are stored slightly differently...
4550 * - If the name can fit in the ASCII character space then they are saved as
4551 * ascii names as per above.
4552 * - If the name needs Unicode then the name is saved in Unicode
4553 * starting at oh->name[1].
4556 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
4559 /* Create an object name if we could not find one. */
4560 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4561 YCHAR local_name[20];
4562 YCHAR num_string[20];
4563 YCHAR *x = &num_string[19];
4564 unsigned v = obj->obj_id;
4568 *x = '0' + (v % 10);
4571 /* make up a name */
4572 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4573 strcat(local_name, x);
4574 strncpy(name, local_name, buffer_size - 1);
4578 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
4580 memset(name, 0, buffer_size * sizeof(YCHAR));
4582 yaffs_check_obj_details_loaded(obj);
4584 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4585 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4587 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
4588 else if (obj->short_name[0]) {
4589 strcpy(name, obj->short_name);
4592 else if (obj->hdr_chunk > 0) {
4594 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4596 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4598 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4600 if (obj->hdr_chunk > 0) {
4601 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4605 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4608 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4611 yaffs_fix_null_name(obj, name, buffer_size);
4613 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4616 int yaffs_get_obj_length(struct yaffs_obj *obj)
4618 /* Dereference any hard linking */
4619 obj = yaffs_get_equivalent_obj(obj);
4621 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4622 return obj->variant.file_variant.file_size;
4623 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4624 if (!obj->variant.symlink_variant.alias)
4626 return strnlen(obj->variant.symlink_variant.alias,
4627 YAFFS_MAX_ALIAS_LENGTH);
4629 /* Only a directory should drop through to here */
4630 return obj->my_dev->data_bytes_per_chunk;
4634 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4637 struct list_head *i;
4640 count++; /* the object itself */
4642 list_for_each(i, &obj->hard_links)
4643 count++; /* add the hard links; */
4648 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4650 obj = yaffs_get_equivalent_obj(obj);
4655 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4657 obj = yaffs_get_equivalent_obj(obj);
4659 switch (obj->variant_type) {
4660 case YAFFS_OBJECT_TYPE_FILE:
4663 case YAFFS_OBJECT_TYPE_DIRECTORY:
4666 case YAFFS_OBJECT_TYPE_SYMLINK:
4669 case YAFFS_OBJECT_TYPE_HARDLINK:
4672 case YAFFS_OBJECT_TYPE_SPECIAL:
4673 if (S_ISFIFO(obj->yst_mode))
4675 if (S_ISCHR(obj->yst_mode))
4677 if (S_ISBLK(obj->yst_mode))
4679 if (S_ISSOCK(obj->yst_mode))
4687 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4689 obj = yaffs_get_equivalent_obj(obj);
4690 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4691 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4693 return yaffs_clone_str(_Y(""));
4696 /*--------------------------- Initialisation code -------------------------- */
4698 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4701 /* Common functions, gotta have */
4702 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4705 #ifdef CONFIG_YAFFS_YAFFS2
4707 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4708 if (dev->param.write_chunk_tags_fn &&
4709 dev->param.read_chunk_tags_fn &&
4710 !dev->param.write_chunk_fn &&
4711 !dev->param.read_chunk_fn &&
4712 dev->param.bad_block_fn && dev->param.query_block_fn)
4716 /* Can use the "spare" style interface for yaffs1 */
4717 if (!dev->param.is_yaffs2 &&
4718 !dev->param.write_chunk_tags_fn &&
4719 !dev->param.read_chunk_tags_fn &&
4720 dev->param.write_chunk_fn &&
4721 dev->param.read_chunk_fn &&
4722 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4728 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4730 /* Initialise the unlinked, deleted, root and lost and found directories */
4732 dev->lost_n_found = dev->root_dir = NULL;
4733 dev->unlinked_dir = dev->del_dir = NULL;
4736 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4739 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4742 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4743 YAFFS_ROOT_MODE | S_IFDIR);
4745 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4746 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4748 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4750 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4757 int yaffs_guts_initialise(struct yaffs_dev *dev)
4759 int init_failed = 0;
4763 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()" );
4765 /* Check stuff that must be set */
4768 yaffs_trace(YAFFS_TRACE_ALWAYS,
4769 "yaffs: Need a device"
4774 dev->internal_start_block = dev->param.start_block;
4775 dev->internal_end_block = dev->param.end_block;
4776 dev->block_offset = 0;
4777 dev->chunk_offset = 0;
4778 dev->n_free_chunks = 0;
4782 if (dev->param.start_block == 0) {
4783 dev->internal_start_block = dev->param.start_block + 1;
4784 dev->internal_end_block = dev->param.end_block + 1;
4785 dev->block_offset = 1;
4786 dev->chunk_offset = dev->param.chunks_per_block;
4789 /* Check geometry parameters. */
4791 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4792 dev->param.total_bytes_per_chunk < 1024) ||
4793 (!dev->param.is_yaffs2 &&
4794 dev->param.total_bytes_per_chunk < 512) ||
4795 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4796 dev->param.chunks_per_block < 2 ||
4797 dev->param.n_reserved_blocks < 2 ||
4798 dev->internal_start_block <= 0 ||
4799 dev->internal_end_block <= 0 ||
4800 dev->internal_end_block <=
4801 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4803 /* otherwise it is too small */
4804 yaffs_trace(YAFFS_TRACE_ALWAYS,
4805 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4806 dev->param.total_bytes_per_chunk,
4807 dev->param.is_yaffs2 ? "2" : "",
4808 dev->param.inband_tags);
4812 if (yaffs_init_nand(dev) != YAFFS_OK) {
4813 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4817 /* Sort out space for inband tags, if required */
4818 if (dev->param.inband_tags)
4819 dev->data_bytes_per_chunk =
4820 dev->param.total_bytes_per_chunk -
4821 sizeof(struct yaffs_packed_tags2_tags_only);
4823 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4825 /* Got the right mix of functions? */
4826 if (!yaffs_check_dev_fns(dev)) {
4827 /* Function missing */
4828 yaffs_trace(YAFFS_TRACE_ALWAYS,
4829 "device function(s) missing or wrong");
4834 if (dev->is_mounted) {
4835 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4839 /* Finished with most checks. One or two more checks happen later on too. */
4841 dev->is_mounted = 1;
4843 /* OK now calculate a few things for the device */
4846 * Calculate all the chunk size manipulation numbers:
4848 x = dev->data_bytes_per_chunk;
4849 /* We always use dev->chunk_shift and dev->chunk_div */
4850 dev->chunk_shift = calc_shifts(x);
4851 x >>= dev->chunk_shift;
4853 /* We only use chunk mask if chunk_div is 1 */
4854 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4857 * Calculate chunk_grp_bits.
4858 * We need to find the next power of 2 > than internal_end_block
4861 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4863 bits = calc_shifts_ceiling(x);
4865 /* Set up tnode width if wide tnodes are enabled. */
4866 if (!dev->param.wide_tnodes_disabled) {
4867 /* bits must be even so that we end up with 32-bit words */
4871 dev->tnode_width = 16;
4873 dev->tnode_width = bits;
4875 dev->tnode_width = 16;
4878 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4880 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4881 * so if the bitwidth of the
4882 * chunk range we're using is greater than 16 we need
4883 * to figure out chunk shift and chunk_grp_size
4886 if (bits <= dev->tnode_width)
4887 dev->chunk_grp_bits = 0;
4889 dev->chunk_grp_bits = bits - dev->tnode_width;
4891 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4892 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4893 dev->tnode_size = sizeof(struct yaffs_tnode);
4895 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4897 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4898 /* We have a problem because the soft delete won't work if
4899 * the chunk group size > chunks per block.
4900 * This can be remedied by using larger "virtual blocks".
4902 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4907 /* OK, we've finished verifying the device, lets continue with initialisation */
4909 /* More device initialisation */
4911 dev->passive_gc_count = 0;
4912 dev->oldest_dirty_gc_count = 0;
4914 dev->gc_block_finder = 0;
4915 dev->buffered_block = -1;
4916 dev->doing_buffered_block_rewrite = 0;
4917 dev->n_deleted_files = 0;
4918 dev->n_bg_deletions = 0;
4919 dev->n_unlinked_files = 0;
4920 dev->n_ecc_fixed = 0;
4921 dev->n_ecc_unfixed = 0;
4922 dev->n_tags_ecc_fixed = 0;
4923 dev->n_tags_ecc_unfixed = 0;
4924 dev->n_erase_failures = 0;
4925 dev->n_erased_blocks = 0;
4926 dev->gc_disable = 0;
4927 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
4928 INIT_LIST_HEAD(&dev->dirty_dirs);
4929 dev->oldest_dirty_seq = 0;
4930 dev->oldest_dirty_block = 0;
4932 /* Initialise temporary buffers and caches. */
4933 if (!yaffs_init_tmp_buffers(dev))
4937 dev->gc_cleanup_list = NULL;
4939 if (!init_failed && dev->param.n_caches > 0) {
4943 dev->param.n_caches * sizeof(struct yaffs_cache);
4945 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4946 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4948 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4950 buf = (u8 *) dev->cache;
4953 memset(dev->cache, 0, cache_bytes);
4955 for (i = 0; i < dev->param.n_caches && buf; i++) {
4956 dev->cache[i].object = NULL;
4957 dev->cache[i].last_use = 0;
4958 dev->cache[i].dirty = 0;
4959 dev->cache[i].data = buf =
4960 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4965 dev->cache_last_use = 0;
4968 dev->cache_hits = 0;
4971 dev->gc_cleanup_list =
4972 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4974 if (!dev->gc_cleanup_list)
4978 if (dev->param.is_yaffs2)
4979 dev->param.use_header_file_size = 1;
4981 if (!init_failed && !yaffs_init_blocks(dev))
4984 yaffs_init_tnodes_and_objs(dev);
4986 if (!init_failed && !yaffs_create_initial_dir(dev))
4990 /* Now scan the flash. */
4991 if (dev->param.is_yaffs2) {
4992 if (yaffs2_checkpt_restore(dev)) {
4993 yaffs_check_obj_details_loaded(dev->root_dir);
4994 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
4995 "yaffs: restored from checkpoint"
4999 /* Clean up the mess caused by an aborted checkpoint load
5000 * and scan backwards.
5002 yaffs_deinit_blocks(dev);
5004 yaffs_deinit_tnodes_and_objs(dev);
5006 dev->n_erased_blocks = 0;
5007 dev->n_free_chunks = 0;
5008 dev->alloc_block = -1;
5009 dev->alloc_page = -1;
5010 dev->n_deleted_files = 0;
5011 dev->n_unlinked_files = 0;
5012 dev->n_bg_deletions = 0;
5014 if (!init_failed && !yaffs_init_blocks(dev))
5017 yaffs_init_tnodes_and_objs(dev);
5020 && !yaffs_create_initial_dir(dev))
5023 if (!init_failed && !yaffs2_scan_backwards(dev))
5026 } else if (!yaffs1_scan(dev)) {
5030 yaffs_strip_deleted_objs(dev);
5031 yaffs_fix_hanging_objs(dev);
5032 if (dev->param.empty_lost_n_found)
5033 yaffs_empty_l_n_f(dev);
5037 /* Clean up the mess */
5038 yaffs_trace(YAFFS_TRACE_TRACING,
5039 "yaffs: yaffs_guts_initialise() aborted.");
5041 yaffs_deinitialise(dev);
5045 /* Zero out stats */
5046 dev->n_page_reads = 0;
5047 dev->n_page_writes = 0;
5048 dev->n_erasures = 0;
5049 dev->n_gc_copies = 0;
5050 dev->n_retired_writes = 0;
5052 dev->n_retired_blocks = 0;
5054 yaffs_verify_free_chunks(dev);
5055 yaffs_verify_blocks(dev);
5057 /* Clean up any aborted checkpoint data */
5058 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5059 yaffs2_checkpt_invalidate(dev);
5061 yaffs_trace(YAFFS_TRACE_TRACING,
5062 "yaffs: yaffs_guts_initialise() done.");
5067 void yaffs_deinitialise(struct yaffs_dev *dev)
5069 if (dev->is_mounted) {
5072 yaffs_deinit_blocks(dev);
5073 yaffs_deinit_tnodes_and_objs(dev);
5074 if (dev->param.n_caches > 0 && dev->cache) {
5076 for (i = 0; i < dev->param.n_caches; i++) {
5077 if (dev->cache[i].data)
5078 kfree(dev->cache[i].data);
5079 dev->cache[i].data = NULL;
5086 kfree(dev->gc_cleanup_list);
5088 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5089 kfree(dev->temp_buffer[i].buffer);
5091 dev->is_mounted = 0;
5093 if (dev->param.deinitialise_flash_fn)
5094 dev->param.deinitialise_flash_fn(dev);
5098 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5103 struct yaffs_block_info *blk;
5105 blk = dev->block_info;
5106 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5107 switch (blk->block_state) {
5108 case YAFFS_BLOCK_STATE_EMPTY:
5109 case YAFFS_BLOCK_STATE_ALLOCATING:
5110 case YAFFS_BLOCK_STATE_COLLECTING:
5111 case YAFFS_BLOCK_STATE_FULL:
5113 (dev->param.chunks_per_block - blk->pages_in_use +
5114 blk->soft_del_pages);
5125 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5127 /* This is what we report to the outside world */
5131 int blocks_for_checkpt;
5134 n_free = dev->n_free_chunks;
5135 n_free += dev->n_deleted_files;
5137 /* Now count the number of dirty chunks in the cache and subtract those */
5139 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5140 if (dev->cache[i].dirty)
5144 n_free -= n_dirty_caches;
5147 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5149 /* Now we figure out how much to reserve for the checkpoint and report that... */
5150 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5152 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);