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 */
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 */
1956 in = list_entry(i, struct yaffs_obj, hash_link);
1957 if (in->obj_id == number) {
1959 /* Don't tell the VFS about this one if it is defered free */
1960 if (in->defered_free)
1971 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1972 enum yaffs_obj_type type)
1974 struct yaffs_obj *the_obj = NULL;
1975 struct yaffs_tnode *tn = NULL;
1978 number = yaffs_new_obj_id(dev);
1980 if (type == YAFFS_OBJECT_TYPE_FILE) {
1981 tn = yaffs_get_tnode(dev);
1986 the_obj = yaffs_alloc_empty_obj(dev);
1989 yaffs_free_tnode(dev, tn);
1995 the_obj->rename_allowed = 1;
1996 the_obj->unlink_allowed = 1;
1997 the_obj->obj_id = number;
1998 yaffs_hash_obj(the_obj);
1999 the_obj->variant_type = type;
2000 yaffs_load_current_time(the_obj, 1, 1);
2003 case YAFFS_OBJECT_TYPE_FILE:
2004 the_obj->variant.file_variant.file_size = 0;
2005 the_obj->variant.file_variant.scanned_size = 0;
2006 the_obj->variant.file_variant.shrink_size = ~0; /* max */
2007 the_obj->variant.file_variant.top_level = 0;
2008 the_obj->variant.file_variant.top = tn;
2010 case YAFFS_OBJECT_TYPE_DIRECTORY:
2011 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
2012 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
2014 case YAFFS_OBJECT_TYPE_SYMLINK:
2015 case YAFFS_OBJECT_TYPE_HARDLINK:
2016 case YAFFS_OBJECT_TYPE_SPECIAL:
2017 /* No action required */
2019 case YAFFS_OBJECT_TYPE_UNKNOWN:
2020 /* todo this should not happen */
2028 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2029 int number, u32 mode)
2032 struct yaffs_obj *obj =
2033 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2035 obj->fake = 1; /* it is fake so it might have no NAND presence... */
2036 obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
2037 obj->unlink_allowed = 0; /* ... or unlink it */
2040 obj->yst_mode = mode;
2042 obj->hdr_chunk = 0; /* Not a valid chunk. */
2050 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2057 yaffs_init_raw_tnodes_and_objs(dev);
2059 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2060 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2061 dev->obj_bucket[i].count = 0;
2065 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2067 enum yaffs_obj_type type)
2069 struct yaffs_obj *the_obj = NULL;
2072 the_obj = yaffs_find_by_number(dev, number);
2075 the_obj = yaffs_new_obj(dev, number, type);
2081 YCHAR *yaffs_clone_str(const YCHAR * str)
2083 YCHAR *new_str = NULL;
2089 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2090 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2092 strncpy(new_str, str, len);
2099 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2100 * link (ie. name) is created or deleted in the directory.
2103 * create dir/a : update dir's mtime/ctime
2104 * rm dir/a: update dir's mtime/ctime
2105 * modify dir/a: don't update dir's mtimme/ctime
2107 * This can be handled immediately or defered. Defering helps reduce the number
2108 * of updates when many files in a directory are changed within a brief period.
2110 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2111 * called periodically.
2114 static void yaffs_update_parent(struct yaffs_obj *obj)
2116 struct yaffs_dev *dev;
2121 yaffs_load_current_time(obj, 0, 1);
2122 if (dev->param.defered_dir_update) {
2123 struct list_head *link = &obj->variant.dir_variant.dirty;
2125 if (list_empty(link)) {
2126 list_add(link, &dev->dirty_dirs);
2127 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2128 "Added object %d to dirty directories",
2133 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2137 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2139 struct list_head *link;
2140 struct yaffs_obj *obj;
2141 struct yaffs_dir_var *d_s;
2142 union yaffs_obj_var *o_v;
2144 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2146 while (!list_empty(&dev->dirty_dirs)) {
2147 link = dev->dirty_dirs.next;
2148 list_del_init(link);
2150 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2151 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2152 obj = list_entry(o_v, struct yaffs_obj, variant);
2154 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2158 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2163 * Mknod (create) a new object.
2164 * equiv_obj only has meaning for a hard link;
2165 * alias_str only has meaning for a symlink.
2166 * rdev only has meaning for devices (a subset of special objects)
2169 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2170 struct yaffs_obj *parent,
2175 struct yaffs_obj *equiv_obj,
2176 const YCHAR * alias_str, u32 rdev)
2178 struct yaffs_obj *in;
2181 struct yaffs_dev *dev = parent->my_dev;
2183 /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
2184 if (yaffs_find_by_name(parent, name))
2187 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2188 str = yaffs_clone_str(alias_str);
2193 in = yaffs_new_obj(dev, -1, type);
2204 in->variant_type = type;
2206 in->yst_mode = mode;
2208 yaffs_attribs_init(in, gid, uid, rdev);
2210 in->n_data_chunks = 0;
2212 yaffs_set_obj_name(in, name);
2215 yaffs_add_obj_to_dir(parent, in);
2217 in->my_dev = parent->my_dev;
2220 case YAFFS_OBJECT_TYPE_SYMLINK:
2221 in->variant.symlink_variant.alias = str;
2223 case YAFFS_OBJECT_TYPE_HARDLINK:
2224 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2225 in->variant.hardlink_variant.equiv_id =
2227 list_add(&in->hard_links, &equiv_obj->hard_links);
2229 case YAFFS_OBJECT_TYPE_FILE:
2230 case YAFFS_OBJECT_TYPE_DIRECTORY:
2231 case YAFFS_OBJECT_TYPE_SPECIAL:
2232 case YAFFS_OBJECT_TYPE_UNKNOWN:
2237 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2238 /* Could not create the object header, fail the creation */
2243 yaffs_update_parent(parent);
2249 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2250 const YCHAR * name, u32 mode, u32 uid,
2253 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2254 uid, gid, NULL, NULL, 0);
2257 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
2258 u32 mode, u32 uid, u32 gid)
2260 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2261 mode, uid, gid, NULL, NULL, 0);
2264 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2265 const YCHAR * name, u32 mode, u32 uid,
2268 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2269 uid, gid, NULL, NULL, rdev);
2272 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2273 const YCHAR * name, u32 mode, u32 uid,
2274 u32 gid, const YCHAR * alias)
2276 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2277 uid, gid, NULL, alias, 0);
2280 /* yaffs_link_obj returns the object id of the equivalent object.*/
2281 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2282 struct yaffs_obj *equiv_obj)
2284 /* Get the real object in case we were fed a hard link as an equivalent object */
2285 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2287 if (yaffs_create_obj
2288 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2289 equiv_obj, NULL, 0)) {
2299 /*------------------------- Block Management and Page Allocation ----------------*/
2301 static int yaffs_init_blocks(struct yaffs_dev *dev)
2303 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2305 dev->block_info = NULL;
2306 dev->chunk_bits = NULL;
2308 dev->alloc_block = -1; /* force it to get a new one */
2310 /* If the first allocation strategy fails, thry the alternate one */
2312 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2313 if (!dev->block_info) {
2315 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2316 dev->block_info_alt = 1;
2318 dev->block_info_alt = 0;
2321 if (dev->block_info) {
2322 /* Set up dynamic blockinfo stuff. Round up bytes. */
2323 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2325 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2326 if (!dev->chunk_bits) {
2328 vmalloc(dev->chunk_bit_stride * n_blocks);
2329 dev->chunk_bits_alt = 1;
2331 dev->chunk_bits_alt = 0;
2335 if (dev->block_info && dev->chunk_bits) {
2336 memset(dev->block_info, 0,
2337 n_blocks * sizeof(struct yaffs_block_info));
2338 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2345 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2347 if (dev->block_info_alt && dev->block_info)
2348 vfree(dev->block_info);
2349 else if (dev->block_info)
2350 kfree(dev->block_info);
2352 dev->block_info_alt = 0;
2354 dev->block_info = NULL;
2356 if (dev->chunk_bits_alt && dev->chunk_bits)
2357 vfree(dev->chunk_bits);
2358 else if (dev->chunk_bits)
2359 kfree(dev->chunk_bits);
2360 dev->chunk_bits_alt = 0;
2361 dev->chunk_bits = NULL;
2364 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2366 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2370 /* If the block is still healthy erase it and mark as clean.
2371 * If the block has had a data failure, then retire it.
2374 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2375 "yaffs_block_became_dirty block %d state %d %s",
2376 block_no, bi->block_state,
2377 (bi->needs_retiring) ? "needs retiring" : "");
2379 yaffs2_clear_oldest_dirty_seq(dev, bi);
2381 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2383 /* If this is the block being garbage collected then stop gc'ing this block */
2384 if (block_no == dev->gc_block)
2387 /* If this block is currently the best candidate for gc then drop as a candidate */
2388 if (block_no == dev->gc_dirtiest) {
2389 dev->gc_dirtiest = 0;
2390 dev->gc_pages_in_use = 0;
2393 if (!bi->needs_retiring) {
2394 yaffs2_checkpt_invalidate(dev);
2395 erased_ok = yaffs_erase_block(dev, block_no);
2397 dev->n_erase_failures++;
2398 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2399 "**>> Erasure failed %d", block_no);
2404 ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
2405 || !yaffs_skip_verification(dev))) {
2407 for (i = 0; i < dev->param.chunks_per_block; i++) {
2408 if (!yaffs_check_chunk_erased
2409 (dev, block_no * dev->param.chunks_per_block + i)) {
2410 yaffs_trace(YAFFS_TRACE_ERROR,
2411 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2418 /* Clean it up... */
2419 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2421 dev->n_erased_blocks++;
2422 bi->pages_in_use = 0;
2423 bi->soft_del_pages = 0;
2424 bi->has_shrink_hdr = 0;
2425 bi->skip_erased_check = 1; /* Clean, so no need to check */
2426 bi->gc_prioritise = 0;
2427 yaffs_clear_chunk_bits(dev, block_no);
2429 yaffs_trace(YAFFS_TRACE_ERASE,
2430 "Erased block %d", block_no);
2432 /* We lost a block of free space */
2433 dev->n_free_chunks -= dev->param.chunks_per_block;
2434 yaffs_retire_block(dev, block_no);
2435 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2436 "**>> Block %d retired", block_no);
2442 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2447 int ret_val = YAFFS_OK;
2449 int is_checkpt_block;
2453 int chunks_before = yaffs_get_erased_chunks(dev);
2456 struct yaffs_ext_tags tags;
2458 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2460 struct yaffs_obj *object;
2462 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2464 yaffs_trace(YAFFS_TRACE_TRACING,
2465 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2466 block, bi->pages_in_use, bi->has_shrink_hdr,
2469 /*yaffs_verify_free_chunks(dev); */
2471 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2472 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2474 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2476 dev->gc_disable = 1;
2478 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2479 yaffs_trace(YAFFS_TRACE_TRACING,
2480 "Collecting block %d that has no chunks in use",
2482 yaffs_block_became_dirty(dev, block);
2485 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2487 yaffs_verify_blk(dev, bi, block);
2489 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2490 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2492 for ( /* init already done */ ;
2493 ret_val == YAFFS_OK &&
2494 dev->gc_chunk < dev->param.chunks_per_block &&
2495 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2496 max_copies > 0; dev->gc_chunk++, old_chunk++) {
2497 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2499 /* This page is in use and might need to be copied off */
2505 yaffs_init_tags(&tags);
2507 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2510 object = yaffs_find_by_number(dev, tags.obj_id);
2512 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2513 "Collecting chunk in block %d, %d %d %d ",
2514 dev->gc_chunk, tags.obj_id,
2515 tags.chunk_id, tags.n_bytes);
2517 if (object && !yaffs_skip_verification(dev)) {
2518 if (tags.chunk_id == 0)
2521 else if (object->soft_del)
2522 matching_chunk = old_chunk; /* Defeat the test */
2525 yaffs_find_chunk_in_file
2526 (object, tags.chunk_id,
2529 if (old_chunk != matching_chunk)
2530 yaffs_trace(YAFFS_TRACE_ERROR,
2531 "gc: page in gc mismatch: %d %d %d %d",
2540 yaffs_trace(YAFFS_TRACE_ERROR,
2541 "page %d in gc has no object: %d %d %d ",
2543 tags.obj_id, tags.chunk_id,
2549 object->soft_del && tags.chunk_id != 0) {
2550 /* Data chunk in a soft deleted file, throw it away
2551 * It's a soft deleted data chunk,
2552 * No need to copy this, just forget about it and
2553 * fix up the object.
2556 /* Free chunks already includes softdeleted chunks.
2557 * How ever this chunk is going to soon be really deleted
2558 * which will increment free chunks.
2559 * We have to decrement free chunks so this works out properly.
2561 dev->n_free_chunks--;
2562 bi->soft_del_pages--;
2564 object->n_data_chunks--;
2566 if (object->n_data_chunks <= 0) {
2567 /* remeber to clean up the object */
2568 dev->gc_cleanup_list[dev->
2575 /* Todo object && object->deleted && object->n_data_chunks == 0 */
2576 /* Deleted object header with no data chunks.
2577 * Can be discarded and the file deleted.
2579 object->hdr_chunk = 0;
2580 yaffs_free_tnode(object->my_dev,
2582 variant.file_variant.
2584 object->variant.file_variant.top = NULL;
2585 yaffs_generic_obj_del(object);
2587 } else if (object) {
2588 /* It's either a data chunk in a live file or
2589 * an ObjectHeader, so we're interested in it.
2590 * NB Need to keep the ObjectHeaders of deleted files
2591 * until the whole file has been deleted off
2593 tags.serial_number++;
2597 if (tags.chunk_id == 0) {
2598 /* It is an object Id,
2599 * We need to nuke the shrinkheader flags first
2600 * Also need to clean up shadowing.
2601 * We no longer want the shrink_header flag since its work is done
2602 * and if it is left in place it will mess up scanning.
2605 struct yaffs_obj_hdr *oh;
2606 oh = (struct yaffs_obj_hdr *)
2610 tags.extra_is_shrink = 0;
2612 oh->shadows_obj = 0;
2613 oh->inband_shadowed_obj_id = 0;
2614 tags.extra_shadows = 0;
2616 /* Update file size */
2617 if (object->variant_type ==
2618 YAFFS_OBJECT_TYPE_FILE) {
2627 yaffs_verify_oh(object, oh,
2630 yaffs_write_new_chunk(dev,
2637 yaffs_write_new_chunk(dev,
2643 if (new_chunk < 0) {
2644 ret_val = YAFFS_FAIL;
2647 /* Ok, now fix up the Tnodes etc. */
2649 if (tags.chunk_id == 0) {
2656 /* It's a data chunk */
2658 ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
2663 if (ret_val == YAFFS_OK)
2664 yaffs_chunk_del(dev, old_chunk,
2665 mark_flash, __LINE__);
2670 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2674 yaffs_verify_collected_blk(dev, bi, block);
2676 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2678 * The gc did not complete. Set block state back to FULL
2679 * because checkpointing does not restore gc.
2681 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2683 /* The gc completed. */
2684 /* Do any required cleanups */
2685 for (i = 0; i < dev->n_clean_ups; i++) {
2686 /* Time to delete the file too */
2688 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2690 yaffs_free_tnode(dev,
2693 object->variant.file_variant.top = NULL;
2694 yaffs_trace(YAFFS_TRACE_GC,
2695 "yaffs: About to finally delete object %d",
2697 yaffs_generic_obj_del(object);
2698 object->my_dev->n_deleted_files--;
2703 chunks_after = yaffs_get_erased_chunks(dev);
2704 if (chunks_before >= chunks_after)
2705 yaffs_trace(YAFFS_TRACE_GC,
2706 "gc did not increase free chunks before %d after %d",
2707 chunks_before, chunks_after);
2710 dev->n_clean_ups = 0;
2713 dev->gc_disable = 0;
2719 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2720 * for garbage collection.
2723 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2724 int aggressive, int background)
2728 unsigned selected = 0;
2729 int prioritised = 0;
2730 int prioritised_exist = 0;
2731 struct yaffs_block_info *bi;
2734 /* First let's see if we need to grab a prioritised block */
2735 if (dev->has_pending_prioritised_gc && !aggressive) {
2736 dev->gc_dirtiest = 0;
2737 bi = dev->block_info;
2738 for (i = dev->internal_start_block;
2739 i <= dev->internal_end_block && !selected; i++) {
2741 if (bi->gc_prioritise) {
2742 prioritised_exist = 1;
2743 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2744 yaffs_block_ok_for_gc(dev, bi)) {
2753 * If there is a prioritised block and none was selected then
2754 * this happened because there is at least one old dirty block gumming
2755 * up the works. Let's gc the oldest dirty block.
2758 if (prioritised_exist &&
2759 !selected && dev->oldest_dirty_block > 0)
2760 selected = dev->oldest_dirty_block;
2762 if (!prioritised_exist) /* None found, so we can clear this */
2763 dev->has_pending_prioritised_gc = 0;
2766 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2768 * else (we're doing a leasurely gc), then we only bother to do this if the
2769 * block has only a few pages in use.
2775 dev->internal_end_block - dev->internal_start_block + 1;
2777 threshold = dev->param.chunks_per_block;
2778 iterations = n_blocks;
2783 max_threshold = dev->param.chunks_per_block / 2;
2785 max_threshold = dev->param.chunks_per_block / 8;
2787 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2788 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2790 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2791 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2792 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2793 if (threshold > max_threshold)
2794 threshold = max_threshold;
2796 iterations = n_blocks / 16 + 1;
2797 if (iterations > 100)
2803 (dev->gc_dirtiest < 1 ||
2804 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
2805 dev->gc_block_finder++;
2806 if (dev->gc_block_finder < dev->internal_start_block ||
2807 dev->gc_block_finder > dev->internal_end_block)
2808 dev->gc_block_finder =
2809 dev->internal_start_block;
2811 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2813 pages_used = bi->pages_in_use - bi->soft_del_pages;
2815 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2816 pages_used < dev->param.chunks_per_block &&
2817 (dev->gc_dirtiest < 1
2818 || pages_used < dev->gc_pages_in_use)
2819 && yaffs_block_ok_for_gc(dev, bi)) {
2820 dev->gc_dirtiest = dev->gc_block_finder;
2821 dev->gc_pages_in_use = pages_used;
2825 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2826 selected = dev->gc_dirtiest;
2830 * If nothing has been selected for a while, try selecting the oldest dirty
2831 * because that's gumming up the works.
2834 if (!selected && dev->param.is_yaffs2 &&
2835 dev->gc_not_done >= (background ? 10 : 20)) {
2836 yaffs2_find_oldest_dirty_seq(dev);
2837 if (dev->oldest_dirty_block > 0) {
2838 selected = dev->oldest_dirty_block;
2839 dev->gc_dirtiest = selected;
2840 dev->oldest_dirty_gc_count++;
2841 bi = yaffs_get_block_info(dev, selected);
2842 dev->gc_pages_in_use =
2843 bi->pages_in_use - bi->soft_del_pages;
2845 dev->gc_not_done = 0;
2850 yaffs_trace(YAFFS_TRACE_GC,
2851 "GC Selected block %d with %d free, prioritised:%d",
2853 dev->param.chunks_per_block - dev->gc_pages_in_use,
2860 dev->gc_dirtiest = 0;
2861 dev->gc_pages_in_use = 0;
2862 dev->gc_not_done = 0;
2863 if (dev->refresh_skip > 0)
2864 dev->refresh_skip--;
2867 yaffs_trace(YAFFS_TRACE_GC,
2868 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2869 dev->gc_block_finder, dev->gc_not_done, threshold,
2870 dev->gc_dirtiest, dev->gc_pages_in_use,
2871 dev->oldest_dirty_block, background ? " bg" : "");
2877 /* New garbage collector
2878 * If we're very low on erased blocks then we do aggressive garbage collection
2879 * otherwise we do "leasurely" garbage collection.
2880 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2881 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2883 * The idea is to help clear out space in a more spread-out manner.
2884 * Dunno if it really does anything useful.
2886 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2889 int gc_ok = YAFFS_OK;
2893 int checkpt_block_adjust;
2895 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2898 if (dev->gc_disable) {
2899 /* Bail out so we don't get recursive gc */
2903 /* This loop should pass the first time.
2904 * We'll only see looping here if the collection does not increase space.
2910 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2913 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2915 dev->n_erased_blocks * dev->param.chunks_per_block;
2917 /* If we need a block soon then do aggressive gc. */
2918 if (dev->n_erased_blocks < min_erased)
2922 && erased_chunks > (dev->n_free_chunks / 4))
2925 if (dev->gc_skip > 20)
2927 if (erased_chunks < dev->n_free_chunks / 2 ||
2928 dev->gc_skip < 1 || background)
2938 /* If we don't already have a block being gc'd then see if we should start another */
2940 if (dev->gc_block < 1 && !aggressive) {
2941 dev->gc_block = yaffs2_find_refresh_block(dev);
2943 dev->n_clean_ups = 0;
2945 if (dev->gc_block < 1) {
2947 yaffs_find_gc_block(dev, aggressive, background);
2949 dev->n_clean_ups = 0;
2952 if (dev->gc_block > 0) {
2955 dev->passive_gc_count++;
2957 yaffs_trace(YAFFS_TRACE_GC,
2958 "yaffs: GC n_erased_blocks %d aggressive %d",
2959 dev->n_erased_blocks, aggressive);
2961 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2964 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
2965 && dev->gc_block > 0) {
2966 yaffs_trace(YAFFS_TRACE_GC,
2967 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2968 dev->n_erased_blocks, max_tries,
2971 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2972 (dev->gc_block > 0) && (max_tries < 2));
2974 return aggressive ? gc_ok : YAFFS_OK;
2979 * Garbage collects. Intended to be called from a background thread.
2980 * Returns non-zero if at least half the free chunks are erased.
2982 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2984 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2986 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2988 yaffs_check_gc(dev, 1);
2989 return erased_chunks > dev->n_free_chunks / 2;
2992 /*-------------------- Data file manipulation -----------------*/
2994 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2996 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2998 if (nand_chunk >= 0)
2999 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
3002 yaffs_trace(YAFFS_TRACE_NANDACCESS,
3003 "Chunk %d not found zero instead",
3005 /* get sane (zero) data if you read a hole */
3006 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
3012 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
3017 struct yaffs_ext_tags tags;
3018 struct yaffs_block_info *bi;
3024 block = chunk_id / dev->param.chunks_per_block;
3025 page = chunk_id % dev->param.chunks_per_block;
3027 if (!yaffs_check_chunk_bit(dev, block, page))
3028 yaffs_trace(YAFFS_TRACE_VERIFY,
3029 "Deleting invalid chunk %d", chunk_id);
3031 bi = yaffs_get_block_info(dev, block);
3033 yaffs2_update_oldest_dirty_seq(dev, block, bi);
3035 yaffs_trace(YAFFS_TRACE_DELETION,
3036 "line %d delete of chunk %d",
3039 if (!dev->param.is_yaffs2 && mark_flash &&
3040 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
3042 yaffs_init_tags(&tags);
3044 tags.is_deleted = 1;
3046 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3047 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3049 dev->n_unmarked_deletions++;
3052 /* Pull out of the management area.
3053 * If the whole block became dirty, this will kick off an erasure.
3055 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
3056 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
3057 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3058 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
3059 dev->n_free_chunks++;
3061 yaffs_clear_chunk_bit(dev, block, page);
3065 if (bi->pages_in_use == 0 &&
3066 !bi->has_shrink_hdr &&
3067 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
3068 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3069 yaffs_block_became_dirty(dev, block);
3076 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3077 const u8 * buffer, int n_bytes, int use_reserve)
3079 /* Find old chunk Need to do this to get serial number
3080 * Write new one and patch into tree.
3081 * Invalidate old tags.
3085 struct yaffs_ext_tags prev_tags;
3088 struct yaffs_ext_tags new_tags;
3090 struct yaffs_dev *dev = in->my_dev;
3092 yaffs_check_gc(dev, 0);
3094 /* Get the previous chunk at this location in the file if it exists.
3095 * If it does not exist then put a zero into the tree. This creates
3096 * the tnode now, rather than later when it is harder to clean up.
3098 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3099 if (prev_chunk_id < 1 &&
3100 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3103 /* Set up new tags */
3104 yaffs_init_tags(&new_tags);
3106 new_tags.chunk_id = inode_chunk;
3107 new_tags.obj_id = in->obj_id;
3108 new_tags.serial_number =
3109 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3110 new_tags.n_bytes = n_bytes;
3112 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3113 yaffs_trace(YAFFS_TRACE_ERROR,
3114 "Writing %d bytes to chunk!!!!!!!!!",
3120 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3122 if (new_chunk_id > 0) {
3123 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3125 if (prev_chunk_id > 0)
3126 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3128 yaffs_verify_file_sane(in);
3130 return new_chunk_id;
3136 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3137 const YCHAR * name, const void *value, int size,
3140 struct yaffs_xattr_mod xmod;
3149 xmod.result = -ENOSPC;
3151 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3159 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3160 struct yaffs_xattr_mod *xmod)
3163 int x_offs = sizeof(struct yaffs_obj_hdr);
3164 struct yaffs_dev *dev = obj->my_dev;
3165 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3167 char *x_buffer = buffer + x_offs;
3171 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3172 xmod->size, xmod->flags);
3174 retval = nval_del(x_buffer, x_size, xmod->name);
3176 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3177 obj->xattr_known = 1;
3179 xmod->result = retval;
3184 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
3185 void *value, int size)
3187 char *buffer = NULL;
3189 struct yaffs_ext_tags tags;
3190 struct yaffs_dev *dev = obj->my_dev;
3191 int x_offs = sizeof(struct yaffs_obj_hdr);
3192 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3198 if (obj->hdr_chunk < 1)
3201 /* If we know that the object has no xattribs then don't do all the
3202 * reading and parsing.
3204 if (obj->xattr_known && !obj->has_xattr) {
3211 buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
3216 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3218 if (result != YAFFS_OK)
3221 x_buffer = buffer + x_offs;
3223 if (!obj->xattr_known) {
3224 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3225 obj->xattr_known = 1;
3229 retval = nval_get(x_buffer, x_size, name, value, size);
3231 retval = nval_list(x_buffer, x_size, value, size);
3233 yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
3237 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3238 const void *value, int size, int flags)
3240 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3243 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3245 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3248 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3251 return yaffs_do_xattrib_fetch(obj, name, value, size);
3254 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3256 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3259 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3262 struct yaffs_obj_hdr *oh;
3263 struct yaffs_dev *dev;
3264 struct yaffs_ext_tags tags;
3266 int alloc_failed = 0;
3273 if (in->lazy_loaded && in->hdr_chunk > 0) {
3274 in->lazy_loaded = 0;
3275 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
3278 yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
3280 oh = (struct yaffs_obj_hdr *)chunk_data;
3282 in->yst_mode = oh->yst_mode;
3283 yaffs_load_attribs(in, oh);
3284 yaffs_set_obj_name_from_oh(in, oh);
3286 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3287 in->variant.symlink_variant.alias =
3288 yaffs_clone_str(oh->alias);
3289 if (!in->variant.symlink_variant.alias)
3290 alloc_failed = 1; /* Not returned to caller */
3293 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
3297 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
3298 const YCHAR * oh_name, int buff_size)
3300 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3301 if (dev->param.auto_unicode) {
3303 /* It is an ASCII name, do an ASCII to
3304 * unicode conversion */
3305 const char *ascii_oh_name = (const char *)oh_name;
3306 int n = buff_size - 1;
3307 while (n > 0 && *ascii_oh_name) {
3308 *name = *ascii_oh_name;
3314 strncpy(name, oh_name + 1, buff_size - 1);
3320 strncpy(name, oh_name, buff_size - 1);
3324 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
3327 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3332 if (dev->param.auto_unicode) {
3337 /* Figure out if the name will fit in ascii character set */
3338 while (is_ascii && *w) {
3345 /* It is an ASCII name, so do a unicode to ascii conversion */
3346 char *ascii_oh_name = (char *)oh_name;
3347 int n = YAFFS_MAX_NAME_LENGTH - 1;
3348 while (n > 0 && *name) {
3349 *ascii_oh_name = *name;
3355 /* It is a unicode name, so save starting at the second YCHAR */
3357 strncpy(oh_name + 1, name,
3358 YAFFS_MAX_NAME_LENGTH - 2);
3364 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3369 /* UpdateObjectHeader updates the header on NAND for an object.
3370 * If name is not NULL, then that new name is used.
3372 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
3373 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3376 struct yaffs_block_info *bi;
3378 struct yaffs_dev *dev = in->my_dev;
3385 struct yaffs_ext_tags new_tags;
3386 struct yaffs_ext_tags old_tags;
3387 const YCHAR *alias = NULL;
3390 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3392 struct yaffs_obj_hdr *oh = NULL;
3394 strcpy(old_name, _Y("silly old name"));
3396 if (!in->fake || in == dev->root_dir ||
3399 yaffs_check_gc(dev, 0);
3400 yaffs_check_obj_details_loaded(in);
3402 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
3403 oh = (struct yaffs_obj_hdr *)buffer;
3405 prev_chunk_id = in->hdr_chunk;
3407 if (prev_chunk_id > 0) {
3408 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3411 yaffs_verify_oh(in, oh, &old_tags, 0);
3413 memcpy(old_name, oh->name, sizeof(oh->name));
3414 memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
3416 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
3419 oh->type = in->variant_type;
3420 oh->yst_mode = in->yst_mode;
3421 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3423 yaffs_load_attribs_oh(oh, in);
3426 oh->parent_obj_id = in->parent->obj_id;
3428 oh->parent_obj_id = 0;
3430 if (name && *name) {
3431 memset(oh->name, 0, sizeof(oh->name));
3432 yaffs_load_oh_from_name(dev, oh->name, name);
3433 } else if (prev_chunk_id > 0) {
3434 memcpy(oh->name, old_name, sizeof(oh->name));
3436 memset(oh->name, 0, sizeof(oh->name));
3439 oh->is_shrink = is_shrink;
3441 switch (in->variant_type) {
3442 case YAFFS_OBJECT_TYPE_UNKNOWN:
3443 /* Should not happen */
3445 case YAFFS_OBJECT_TYPE_FILE:
3447 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
3448 || oh->parent_obj_id ==
3449 YAFFS_OBJECTID_UNLINKED) ? 0 : in->
3450 variant.file_variant.file_size;
3452 case YAFFS_OBJECT_TYPE_HARDLINK:
3453 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3455 case YAFFS_OBJECT_TYPE_SPECIAL:
3458 case YAFFS_OBJECT_TYPE_DIRECTORY:
3461 case YAFFS_OBJECT_TYPE_SYMLINK:
3462 alias = in->variant.symlink_variant.alias;
3464 alias = _Y("no alias");
3465 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3466 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3470 /* process any xattrib modifications */
3472 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3475 yaffs_init_tags(&new_tags);
3477 new_tags.chunk_id = 0;
3478 new_tags.obj_id = in->obj_id;
3479 new_tags.serial_number = in->serial;
3481 /* Add extra info for file header */
3483 new_tags.extra_available = 1;
3484 new_tags.extra_parent_id = oh->parent_obj_id;
3485 new_tags.extra_length = oh->file_size;
3486 new_tags.extra_is_shrink = oh->is_shrink;
3487 new_tags.extra_equiv_id = oh->equiv_id;
3488 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3489 new_tags.extra_obj_type = in->variant_type;
3491 yaffs_verify_oh(in, oh, &new_tags, 1);
3493 /* Create new chunk in NAND */
3495 yaffs_write_new_chunk(dev, buffer, &new_tags,
3496 (prev_chunk_id > 0) ? 1 : 0);
3498 if (new_chunk_id >= 0) {
3500 in->hdr_chunk = new_chunk_id;
3502 if (prev_chunk_id > 0) {
3503 yaffs_chunk_del(dev, prev_chunk_id, 1,
3507 if (!yaffs_obj_cache_dirty(in))
3510 /* If this was a shrink, then mark the block that the chunk lives on */
3512 bi = yaffs_get_block_info(in->my_dev,
3516 bi->has_shrink_hdr = 1;
3521 ret_val = new_chunk_id;
3526 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3531 /*--------------------- File read/write ------------------------
3532 * Read and write have very similar structures.
3533 * In general the read/write has three parts to it
3534 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3535 * Some complete chunks
3536 * An incomplete chunk to end off with
3538 * Curve-balls: the first chunk might also be the last chunk.
3541 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3549 struct yaffs_cache *cache;
3551 struct yaffs_dev *dev;
3556 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3557 /* start = offset % dev->data_bytes_per_chunk; */
3558 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3561 /* OK now check for the curveball where the start and end are in
3564 if ((start + n) < dev->data_bytes_per_chunk)
3567 n_copy = dev->data_bytes_per_chunk - start;
3569 cache = yaffs_find_chunk_cache(in, chunk);
3571 /* If the chunk is already in the cache or it is less than a whole chunk
3572 * or we're using inband tags then use the cache (if there is caching)
3573 * else bypass the cache.
3575 if (cache || n_copy != dev->data_bytes_per_chunk
3576 || dev->param.inband_tags) {
3577 if (dev->param.n_caches > 0) {
3579 /* If we can't find the data in the cache, then load it up. */
3583 yaffs_grab_chunk_cache(in->my_dev);
3585 cache->chunk_id = chunk;
3588 yaffs_rd_data_obj(in, chunk,
3593 yaffs_use_cache(dev, cache, 0);
3597 memcpy(buffer, &cache->data[start], n_copy);
3601 /* Read into the local buffer then copy.. */
3604 yaffs_get_temp_buffer(dev, __LINE__);
3605 yaffs_rd_data_obj(in, chunk, local_buffer);
3607 memcpy(buffer, &local_buffer[start], n_copy);
3609 yaffs_release_temp_buffer(dev, local_buffer,
3615 /* A full chunk. Read directly into the supplied buffer. */
3616 yaffs_rd_data_obj(in, chunk, buffer);
3630 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3631 int n_bytes, int write_trhrough)
3640 int start_write = offset;
3641 int chunk_written = 0;
3645 struct yaffs_dev *dev;
3649 while (n > 0 && chunk_written >= 0) {
3650 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3652 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3653 start >= dev->data_bytes_per_chunk) {
3654 yaffs_trace(YAFFS_TRACE_ERROR,
3655 "AddrToChunk of offset %d gives chunk %d start %d",
3656 (int)offset, chunk, start);
3658 chunk++; /* File pos to chunk in file offset */
3660 /* OK now check for the curveball where the start and end are in
3664 if ((start + n) < dev->data_bytes_per_chunk) {
3667 /* Now folks, to calculate how many bytes to write back....
3668 * If we're overwriting and not writing to then end of file then
3669 * we need to write back as much as was there before.
3672 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3674 if (chunk_start > in->variant.file_variant.file_size)
3675 n_bytes_read = 0; /* Past end of file */
3678 in->variant.file_variant.file_size -
3681 if (n_bytes_read > dev->data_bytes_per_chunk)
3682 n_bytes_read = dev->data_bytes_per_chunk;
3686 (start + n)) ? n_bytes_read : (start + n);
3689 || n_writeback > dev->data_bytes_per_chunk)
3693 n_copy = dev->data_bytes_per_chunk - start;
3694 n_writeback = dev->data_bytes_per_chunk;
3697 if (n_copy != dev->data_bytes_per_chunk
3698 || dev->param.inband_tags) {
3699 /* An incomplete start or end chunk (or maybe both start and end chunk),
3700 * or we're using inband tags, so we want to use the cache buffers.
3702 if (dev->param.n_caches > 0) {
3703 struct yaffs_cache *cache;
3704 /* If we can't find the data in the cache, then load the cache */
3705 cache = yaffs_find_chunk_cache(in, chunk);
3708 && yaffs_check_alloc_available(dev, 1)) {
3709 cache = yaffs_grab_chunk_cache(dev);
3711 cache->chunk_id = chunk;
3714 yaffs_rd_data_obj(in, chunk,
3718 !yaffs_check_alloc_available(dev,
3720 /* Drop the cache if it was a read cache item and
3721 * no space check has been made for it.
3727 yaffs_use_cache(dev, cache, 1);
3730 memcpy(&cache->data[start], buffer,
3734 cache->n_bytes = n_writeback;
3736 if (write_trhrough) {
3747 chunk_written = -1; /* fail the write */
3750 /* An incomplete start or end chunk (or maybe both start and end chunk)
3751 * Read into the local buffer then copy, then copy over and write back.
3755 yaffs_get_temp_buffer(dev, __LINE__);
3757 yaffs_rd_data_obj(in, chunk, local_buffer);
3759 memcpy(&local_buffer[start], buffer, n_copy);
3762 yaffs_wr_data_obj(in, chunk,
3766 yaffs_release_temp_buffer(dev, local_buffer,
3772 /* A full chunk. Write directly from the supplied buffer. */
3775 yaffs_wr_data_obj(in, chunk, buffer,
3776 dev->data_bytes_per_chunk, 0);
3778 /* Since we've overwritten the cached data, we better invalidate it. */
3779 yaffs_invalidate_chunk_cache(in, chunk);
3782 if (chunk_written >= 0) {
3791 /* Update file object */
3793 if ((start_write + n_done) > in->variant.file_variant.file_size)
3794 in->variant.file_variant.file_size = (start_write + n_done);
3801 int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3802 int n_bytes, int write_trhrough)
3804 yaffs2_handle_hole(in, offset);
3805 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3808 /* ---------------------- File resizing stuff ------------------ */
3810 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3813 struct yaffs_dev *dev = in->my_dev;
3814 int old_size = in->variant.file_variant.file_size;
3816 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3818 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3819 dev->data_bytes_per_chunk;
3823 /* Delete backwards so that we don't end up with holes if
3824 * power is lost part-way through the operation.
3826 for (i = last_del; i >= start_del; i--) {
3827 /* NB this could be optimised somewhat,
3828 * eg. could retrieve the tags and write them without
3829 * using yaffs_chunk_del
3832 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3835 (dev->internal_start_block *
3836 dev->param.chunks_per_block)
3838 ((dev->internal_end_block +
3839 1) * dev->param.chunks_per_block)) {
3840 yaffs_trace(YAFFS_TRACE_ALWAYS,
3841 "Found daft chunk_id %d for %d",
3844 in->n_data_chunks--;
3845 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3852 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3856 struct yaffs_dev *dev = obj->my_dev;
3858 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3860 yaffs_prune_chunks(obj, new_size);
3862 if (new_partial != 0) {
3863 int last_chunk = 1 + new_full;
3864 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3866 /* Rewrite the last chunk with its new size and zero pad */
3867 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3868 memset(local_buffer + new_partial, 0,
3869 dev->data_bytes_per_chunk - new_partial);
3871 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3874 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3877 obj->variant.file_variant.file_size = new_size;
3879 yaffs_prune_tree(dev, &obj->variant.file_variant);
3882 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3884 struct yaffs_dev *dev = in->my_dev;
3885 int old_size = in->variant.file_variant.file_size;
3887 yaffs_flush_file_cache(in);
3888 yaffs_invalidate_whole_cache(in);
3890 yaffs_check_gc(dev, 0);
3892 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3895 if (new_size == old_size)
3898 if (new_size > old_size) {
3899 yaffs2_handle_hole(in, new_size);
3900 in->variant.file_variant.file_size = new_size;
3902 /* new_size < old_size */
3903 yaffs_resize_file_down(in, new_size);
3906 /* Write a new object header to reflect the resize.
3907 * show we've shrunk the file, if need be
3908 * Do this only if the file is not in the deleted directories
3909 * and is not shadowed.
3913 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3914 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3915 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3920 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3924 yaffs_flush_file_cache(in);
3925 if (data_sync) /* Only sync data */
3929 yaffs_load_current_time(in, 0, 0);
3931 ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3932 0) ? YAFFS_OK : YAFFS_FAIL;
3943 /* yaffs_del_file deletes the whole file data
3944 * and the inode associated with the file.
3945 * It does not delete the links associated with the file.
3947 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3952 struct yaffs_dev *dev = in->my_dev;
3959 yaffs_change_obj_name(in, in->my_dev->del_dir,
3960 _Y("deleted"), 0, 0);
3961 yaffs_trace(YAFFS_TRACE_TRACING,
3962 "yaffs: immediate deletion of file %d",
3965 in->my_dev->n_deleted_files++;
3966 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3967 yaffs_resize_file(in, 0);
3968 yaffs_soft_del_file(in);
3971 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3972 _Y("unlinked"), 0, 0);
3978 int yaffs_del_file(struct yaffs_obj *in)
3980 int ret_val = YAFFS_OK;
3981 int deleted; /* Need to cache value on stack if in is freed */
3982 struct yaffs_dev *dev = in->my_dev;
3984 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3985 yaffs_resize_file(in, 0);
3987 if (in->n_data_chunks > 0) {
3988 /* Use soft deletion if there is data in the file.
3989 * That won't be the case if it has been resized to zero.
3992 ret_val = yaffs_unlink_file_if_needed(in);
3994 deleted = in->deleted;
3996 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3999 in->my_dev->n_deleted_files++;
4000 yaffs_soft_del_file(in);
4002 return deleted ? YAFFS_OK : YAFFS_FAIL;
4004 /* The file has no data chunks so we toss it immediately */
4005 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
4006 in->variant.file_variant.top = NULL;
4007 yaffs_generic_obj_del(in);
4013 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
4016 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
4017 !(list_empty(&obj->variant.dir_variant.children));
4020 static int yaffs_del_dir(struct yaffs_obj *obj)
4022 /* First check that the directory is empty. */
4023 if (yaffs_is_non_empty_dir(obj))
4026 return yaffs_generic_obj_del(obj);
4029 static int yaffs_del_symlink(struct yaffs_obj *in)
4031 if (in->variant.symlink_variant.alias)
4032 kfree(in->variant.symlink_variant.alias);
4033 in->variant.symlink_variant.alias = NULL;
4035 return yaffs_generic_obj_del(in);
4038 static int yaffs_del_link(struct yaffs_obj *in)
4040 /* remove this hardlink from the list assocaited with the equivalent
4043 list_del_init(&in->hard_links);
4044 return yaffs_generic_obj_del(in);
4047 int yaffs_del_obj(struct yaffs_obj *obj)
4050 switch (obj->variant_type) {
4051 case YAFFS_OBJECT_TYPE_FILE:
4052 ret_val = yaffs_del_file(obj);
4054 case YAFFS_OBJECT_TYPE_DIRECTORY:
4055 if (!list_empty(&obj->variant.dir_variant.dirty)) {
4056 yaffs_trace(YAFFS_TRACE_BACKGROUND,
4057 "Remove object %d from dirty directories",
4059 list_del_init(&obj->variant.dir_variant.dirty);
4061 return yaffs_del_dir(obj);
4063 case YAFFS_OBJECT_TYPE_SYMLINK:
4064 ret_val = yaffs_del_symlink(obj);
4066 case YAFFS_OBJECT_TYPE_HARDLINK:
4067 ret_val = yaffs_del_link(obj);
4069 case YAFFS_OBJECT_TYPE_SPECIAL:
4070 ret_val = yaffs_generic_obj_del(obj);
4072 case YAFFS_OBJECT_TYPE_UNKNOWN:
4074 break; /* should not happen. */
4080 static int yaffs_unlink_worker(struct yaffs_obj *obj)
4089 yaffs_update_parent(obj->parent);
4091 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4092 return yaffs_del_link(obj);
4093 } else if (!list_empty(&obj->hard_links)) {
4094 /* Curve ball: We're unlinking an object that has a hardlink.
4096 * This problem arises because we are not strictly following
4097 * The Linux link/inode model.
4099 * We can't really delete the object.
4100 * Instead, we do the following:
4101 * - Select a hardlink.
4102 * - Unhook it from the hard links
4103 * - Move it from its parent directory (so that the rename can work)
4104 * - Rename the object to the hardlink's name.
4105 * - Delete the hardlink
4108 struct yaffs_obj *hl;
4109 struct yaffs_obj *parent;
4111 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4113 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
4116 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4117 parent = hl->parent;
4119 list_del_init(&hl->hard_links);
4121 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4123 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4125 if (ret_val == YAFFS_OK)
4126 ret_val = yaffs_generic_obj_del(hl);
4130 } else if (del_now) {
4131 switch (obj->variant_type) {
4132 case YAFFS_OBJECT_TYPE_FILE:
4133 return yaffs_del_file(obj);
4135 case YAFFS_OBJECT_TYPE_DIRECTORY:
4136 list_del_init(&obj->variant.dir_variant.dirty);
4137 return yaffs_del_dir(obj);
4139 case YAFFS_OBJECT_TYPE_SYMLINK:
4140 return yaffs_del_symlink(obj);
4142 case YAFFS_OBJECT_TYPE_SPECIAL:
4143 return yaffs_generic_obj_del(obj);
4145 case YAFFS_OBJECT_TYPE_HARDLINK:
4146 case YAFFS_OBJECT_TYPE_UNKNOWN:
4150 } else if (yaffs_is_non_empty_dir(obj)) {
4153 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4154 _Y("unlinked"), 0, 0);
4158 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4161 if (obj && obj->unlink_allowed)
4162 return yaffs_unlink_worker(obj);
4168 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
4170 struct yaffs_obj *obj;
4172 obj = yaffs_find_by_name(dir, name);
4173 return yaffs_unlink_obj(obj);
4177 * If old_name is NULL then we take old_dir as the object to be renamed.
4179 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
4180 struct yaffs_obj *new_dir, const YCHAR * new_name)
4182 struct yaffs_obj *obj = NULL;
4183 struct yaffs_obj *existing_target = NULL;
4186 struct yaffs_dev *dev;
4188 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4190 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4193 dev = old_dir->my_dev;
4195 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4196 /* Special case for case insemsitive systems.
4197 * While look-up is case insensitive, the name isn't.
4198 * Therefore we might want to change x.txt to X.txt
4200 if (old_dir == new_dir &&
4201 old_name && new_name &&
4202 strcmp(old_name, new_name) == 0)
4206 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4207 YAFFS_MAX_NAME_LENGTH)
4212 obj = yaffs_find_by_name(old_dir, old_name);
4215 old_dir = obj->parent;
4219 if (obj && obj->rename_allowed) {
4221 /* Now do the handling for an existing target, if there is one */
4223 existing_target = yaffs_find_by_name(new_dir, new_name);
4224 if (yaffs_is_non_empty_dir(existing_target)){
4225 return YAFFS_FAIL; /* ENOTEMPTY */
4226 } else if (existing_target && existing_target != obj) {
4227 /* Nuke the target first, using shadowing,
4228 * but only if it isn't the same object.
4230 * Note we must disable gc otherwise it can mess up the shadowing.
4233 dev->gc_disable = 1;
4234 yaffs_change_obj_name(obj, new_dir, new_name, force,
4235 existing_target->obj_id);
4236 existing_target->is_shadowed = 1;
4237 yaffs_unlink_obj(existing_target);
4238 dev->gc_disable = 0;
4241 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4243 yaffs_update_parent(old_dir);
4244 if (new_dir != old_dir)
4245 yaffs_update_parent(new_dir);
4252 /*----------------------- Initialisation Scanning ---------------------- */
4254 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4255 int backward_scanning)
4257 struct yaffs_obj *obj;
4259 if (!backward_scanning) {
4260 /* Handle YAFFS1 forward scanning case
4261 * For YAFFS1 we always do the deletion
4265 /* Handle YAFFS2 case (backward scanning)
4266 * If the shadowed object exists then ignore.
4268 obj = yaffs_find_by_number(dev, obj_id);
4273 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4274 * We put it in unlinked dir to be cleaned up after the scanning
4277 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4280 obj->is_shadowed = 1;
4281 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4282 obj->variant.file_variant.shrink_size = 0;
4283 obj->valid = 1; /* So that we don't read any other info for this file */
4287 void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
4289 struct yaffs_obj *hl;
4290 struct yaffs_obj *in;
4294 hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
4296 in = yaffs_find_by_number(dev,
4298 hardlink_variant.equiv_id);
4301 /* Add the hardlink pointers */
4302 hl->variant.hardlink_variant.equiv_obj = in;
4303 list_add(&hl->hard_links, &in->hard_links);
4305 /* Todo Need to report/handle this better.
4306 * Got a problem... hardlink to a non-existant object
4308 hl->variant.hardlink_variant.equiv_obj = NULL;
4309 INIT_LIST_HEAD(&hl->hard_links);
4315 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4318 * Sort out state of unlinked and deleted objects after scanning.
4320 struct list_head *i;
4321 struct list_head *n;
4322 struct yaffs_obj *l;
4327 /* Soft delete all the unlinked files */
4328 list_for_each_safe(i, n,
4329 &dev->unlinked_dir->variant.dir_variant.children) {
4331 l = list_entry(i, struct yaffs_obj, siblings);
4336 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4338 l = list_entry(i, struct yaffs_obj, siblings);
4346 * This code iterates through all the objects making sure that they are rooted.
4347 * Any unrooted objects are re-rooted in lost+found.
4348 * An object needs to be in one of:
4349 * - Directly under deleted, unlinked
4350 * - Directly or indirectly under root.
4353 * This code assumes that we don't ever change the current relationships between
4355 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4356 * lost-n-found->parent == root_dir
4358 * This fixes the problem where directories might have inadvertently been deleted
4359 * leaving the object "hanging" without being rooted in the directory tree.
4362 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4364 return (obj == dev->del_dir ||
4365 obj == dev->unlinked_dir || obj == dev->root_dir);
4368 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4370 struct yaffs_obj *obj;
4371 struct yaffs_obj *parent;
4373 struct list_head *lh;
4374 struct list_head *n;
4381 /* Iterate through the objects in each hash entry,
4382 * looking at each object.
4383 * Make sure it is rooted.
4386 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4387 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4390 list_entry(lh, struct yaffs_obj, hash_link);
4391 parent = obj->parent;
4393 if (yaffs_has_null_parent(dev, obj)) {
4394 /* These directories are not hanging */
4397 || parent->variant_type !=
4398 YAFFS_OBJECT_TYPE_DIRECTORY) {
4400 } else if (yaffs_has_null_parent(dev, parent)) {
4404 * Need to follow the parent chain to see if it is hanging.
4409 while (parent != dev->root_dir &&
4411 parent->parent->variant_type ==
4412 YAFFS_OBJECT_TYPE_DIRECTORY
4413 && depth_limit > 0) {
4414 parent = parent->parent;
4417 if (parent != dev->root_dir)
4421 yaffs_trace(YAFFS_TRACE_SCAN,
4422 "Hanging object %d moved to lost and found",
4424 yaffs_add_obj_to_dir(dev->lost_n_found,
4433 * Delete directory contents for cleaning up lost and found.
4435 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4437 struct yaffs_obj *obj;
4438 struct list_head *lh;
4439 struct list_head *n;
4441 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4444 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4446 obj = list_entry(lh, struct yaffs_obj, siblings);
4447 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4448 yaffs_del_dir_contents(obj);
4450 yaffs_trace(YAFFS_TRACE_SCAN,
4451 "Deleting lost_found object %d",
4454 /* Need to use UnlinkObject since Delete would not handle
4455 * hardlinked objects correctly.
4457 yaffs_unlink_obj(obj);
4463 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4465 yaffs_del_dir_contents(dev->lost_n_found);
4469 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4474 struct list_head *i;
4475 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4477 struct yaffs_obj *l;
4483 yaffs_trace(YAFFS_TRACE_ALWAYS,
4484 "tragedy: yaffs_find_by_name: null pointer directory"
4489 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4490 yaffs_trace(YAFFS_TRACE_ALWAYS,
4491 "tragedy: yaffs_find_by_name: non-directory"
4496 sum = yaffs_calc_name_sum(name);
4498 list_for_each(i, &directory->variant.dir_variant.children) {
4500 l = list_entry(i, struct yaffs_obj, siblings);
4502 if (l->parent != directory)
4505 yaffs_check_obj_details_loaded(l);
4507 /* Special case for lost-n-found */
4508 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4509 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4511 } else if (l->sum == sum
4512 || l->hdr_chunk <= 0) {
4513 /* LostnFound chunk called Objxxx
4516 yaffs_get_obj_name(l, buffer,
4517 YAFFS_MAX_NAME_LENGTH + 1);
4519 (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4528 /* GetEquivalentObject dereferences any hard links to get to the
4532 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4534 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4535 /* We want the object id of the equivalent object, not this one */
4536 obj = obj->variant.hardlink_variant.equiv_obj;
4537 yaffs_check_obj_details_loaded(obj);
4543 * A note or two on object names.
4544 * * If the object name is missing, we then make one up in the form objnnn
4546 * * ASCII names are stored in the object header's name field from byte zero
4547 * * Unicode names are historically stored starting from byte zero.
4549 * Then there are automatic Unicode names...
4550 * The purpose of these is to save names in a way that can be read as
4551 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4552 * system to share files.
4554 * These automatic unicode are stored slightly differently...
4555 * - If the name can fit in the ASCII character space then they are saved as
4556 * ascii names as per above.
4557 * - If the name needs Unicode then the name is saved in Unicode
4558 * starting at oh->name[1].
4561 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
4564 /* Create an object name if we could not find one. */
4565 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4566 YCHAR local_name[20];
4567 YCHAR num_string[20];
4568 YCHAR *x = &num_string[19];
4569 unsigned v = obj->obj_id;
4573 *x = '0' + (v % 10);
4576 /* make up a name */
4577 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4578 strcat(local_name, x);
4579 strncpy(name, local_name, buffer_size - 1);
4583 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
4585 memset(name, 0, buffer_size * sizeof(YCHAR));
4587 yaffs_check_obj_details_loaded(obj);
4589 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4590 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4592 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
4593 else if (obj->short_name[0]) {
4594 strcpy(name, obj->short_name);
4597 else if (obj->hdr_chunk > 0) {
4599 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4601 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4603 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4605 if (obj->hdr_chunk > 0) {
4606 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4610 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4613 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4616 yaffs_fix_null_name(obj, name, buffer_size);
4618 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4621 int yaffs_get_obj_length(struct yaffs_obj *obj)
4623 /* Dereference any hard linking */
4624 obj = yaffs_get_equivalent_obj(obj);
4626 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4627 return obj->variant.file_variant.file_size;
4628 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4629 if (!obj->variant.symlink_variant.alias)
4631 return strnlen(obj->variant.symlink_variant.alias,
4632 YAFFS_MAX_ALIAS_LENGTH);
4634 /* Only a directory should drop through to here */
4635 return obj->my_dev->data_bytes_per_chunk;
4639 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4642 struct list_head *i;
4645 count++; /* the object itself */
4647 list_for_each(i, &obj->hard_links)
4648 count++; /* add the hard links; */
4653 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4655 obj = yaffs_get_equivalent_obj(obj);
4660 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4662 obj = yaffs_get_equivalent_obj(obj);
4664 switch (obj->variant_type) {
4665 case YAFFS_OBJECT_TYPE_FILE:
4668 case YAFFS_OBJECT_TYPE_DIRECTORY:
4671 case YAFFS_OBJECT_TYPE_SYMLINK:
4674 case YAFFS_OBJECT_TYPE_HARDLINK:
4677 case YAFFS_OBJECT_TYPE_SPECIAL:
4678 if (S_ISFIFO(obj->yst_mode))
4680 if (S_ISCHR(obj->yst_mode))
4682 if (S_ISBLK(obj->yst_mode))
4684 if (S_ISSOCK(obj->yst_mode))
4692 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4694 obj = yaffs_get_equivalent_obj(obj);
4695 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4696 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4698 return yaffs_clone_str(_Y(""));
4701 /*--------------------------- Initialisation code -------------------------- */
4703 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4706 /* Common functions, gotta have */
4707 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4710 #ifdef CONFIG_YAFFS_YAFFS2
4712 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4713 if (dev->param.write_chunk_tags_fn &&
4714 dev->param.read_chunk_tags_fn &&
4715 !dev->param.write_chunk_fn &&
4716 !dev->param.read_chunk_fn &&
4717 dev->param.bad_block_fn && dev->param.query_block_fn)
4721 /* Can use the "spare" style interface for yaffs1 */
4722 if (!dev->param.is_yaffs2 &&
4723 !dev->param.write_chunk_tags_fn &&
4724 !dev->param.read_chunk_tags_fn &&
4725 dev->param.write_chunk_fn &&
4726 dev->param.read_chunk_fn &&
4727 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4733 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4735 /* Initialise the unlinked, deleted, root and lost and found directories */
4737 dev->lost_n_found = dev->root_dir = NULL;
4738 dev->unlinked_dir = dev->del_dir = NULL;
4741 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4744 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4747 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4748 YAFFS_ROOT_MODE | S_IFDIR);
4750 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4751 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4753 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4755 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4762 int yaffs_guts_initialise(struct yaffs_dev *dev)
4764 int init_failed = 0;
4768 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()" );
4770 /* Check stuff that must be set */
4773 yaffs_trace(YAFFS_TRACE_ALWAYS,
4774 "yaffs: Need a device"
4779 dev->internal_start_block = dev->param.start_block;
4780 dev->internal_end_block = dev->param.end_block;
4781 dev->block_offset = 0;
4782 dev->chunk_offset = 0;
4783 dev->n_free_chunks = 0;
4787 if (dev->param.start_block == 0) {
4788 dev->internal_start_block = dev->param.start_block + 1;
4789 dev->internal_end_block = dev->param.end_block + 1;
4790 dev->block_offset = 1;
4791 dev->chunk_offset = dev->param.chunks_per_block;
4794 /* Check geometry parameters. */
4796 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4797 dev->param.total_bytes_per_chunk < 1024) ||
4798 (!dev->param.is_yaffs2 &&
4799 dev->param.total_bytes_per_chunk < 512) ||
4800 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4801 dev->param.chunks_per_block < 2 ||
4802 dev->param.n_reserved_blocks < 2 ||
4803 dev->internal_start_block <= 0 ||
4804 dev->internal_end_block <= 0 ||
4805 dev->internal_end_block <=
4806 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4808 /* otherwise it is too small */
4809 yaffs_trace(YAFFS_TRACE_ALWAYS,
4810 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4811 dev->param.total_bytes_per_chunk,
4812 dev->param.is_yaffs2 ? "2" : "",
4813 dev->param.inband_tags);
4817 if (yaffs_init_nand(dev) != YAFFS_OK) {
4818 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4822 /* Sort out space for inband tags, if required */
4823 if (dev->param.inband_tags)
4824 dev->data_bytes_per_chunk =
4825 dev->param.total_bytes_per_chunk -
4826 sizeof(struct yaffs_packed_tags2_tags_only);
4828 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4830 /* Got the right mix of functions? */
4831 if (!yaffs_check_dev_fns(dev)) {
4832 /* Function missing */
4833 yaffs_trace(YAFFS_TRACE_ALWAYS,
4834 "device function(s) missing or wrong");
4839 if (dev->is_mounted) {
4840 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4844 /* Finished with most checks. One or two more checks happen later on too. */
4846 dev->is_mounted = 1;
4848 /* OK now calculate a few things for the device */
4851 * Calculate all the chunk size manipulation numbers:
4853 x = dev->data_bytes_per_chunk;
4854 /* We always use dev->chunk_shift and dev->chunk_div */
4855 dev->chunk_shift = calc_shifts(x);
4856 x >>= dev->chunk_shift;
4858 /* We only use chunk mask if chunk_div is 1 */
4859 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4862 * Calculate chunk_grp_bits.
4863 * We need to find the next power of 2 > than internal_end_block
4866 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4868 bits = calc_shifts_ceiling(x);
4870 /* Set up tnode width if wide tnodes are enabled. */
4871 if (!dev->param.wide_tnodes_disabled) {
4872 /* bits must be even so that we end up with 32-bit words */
4876 dev->tnode_width = 16;
4878 dev->tnode_width = bits;
4880 dev->tnode_width = 16;
4883 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4885 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4886 * so if the bitwidth of the
4887 * chunk range we're using is greater than 16 we need
4888 * to figure out chunk shift and chunk_grp_size
4891 if (bits <= dev->tnode_width)
4892 dev->chunk_grp_bits = 0;
4894 dev->chunk_grp_bits = bits - dev->tnode_width;
4896 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4897 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4898 dev->tnode_size = sizeof(struct yaffs_tnode);
4900 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4902 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4903 /* We have a problem because the soft delete won't work if
4904 * the chunk group size > chunks per block.
4905 * This can be remedied by using larger "virtual blocks".
4907 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4912 /* OK, we've finished verifying the device, lets continue with initialisation */
4914 /* More device initialisation */
4916 dev->passive_gc_count = 0;
4917 dev->oldest_dirty_gc_count = 0;
4919 dev->gc_block_finder = 0;
4920 dev->buffered_block = -1;
4921 dev->doing_buffered_block_rewrite = 0;
4922 dev->n_deleted_files = 0;
4923 dev->n_bg_deletions = 0;
4924 dev->n_unlinked_files = 0;
4925 dev->n_ecc_fixed = 0;
4926 dev->n_ecc_unfixed = 0;
4927 dev->n_tags_ecc_fixed = 0;
4928 dev->n_tags_ecc_unfixed = 0;
4929 dev->n_erase_failures = 0;
4930 dev->n_erased_blocks = 0;
4931 dev->gc_disable = 0;
4932 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
4933 INIT_LIST_HEAD(&dev->dirty_dirs);
4934 dev->oldest_dirty_seq = 0;
4935 dev->oldest_dirty_block = 0;
4937 /* Initialise temporary buffers and caches. */
4938 if (!yaffs_init_tmp_buffers(dev))
4942 dev->gc_cleanup_list = NULL;
4944 if (!init_failed && dev->param.n_caches > 0) {
4948 dev->param.n_caches * sizeof(struct yaffs_cache);
4950 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4951 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4953 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4955 buf = (u8 *) dev->cache;
4958 memset(dev->cache, 0, cache_bytes);
4960 for (i = 0; i < dev->param.n_caches && buf; i++) {
4961 dev->cache[i].object = NULL;
4962 dev->cache[i].last_use = 0;
4963 dev->cache[i].dirty = 0;
4964 dev->cache[i].data = buf =
4965 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4970 dev->cache_last_use = 0;
4973 dev->cache_hits = 0;
4976 dev->gc_cleanup_list =
4977 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4979 if (!dev->gc_cleanup_list)
4983 if (dev->param.is_yaffs2)
4984 dev->param.use_header_file_size = 1;
4986 if (!init_failed && !yaffs_init_blocks(dev))
4989 yaffs_init_tnodes_and_objs(dev);
4991 if (!init_failed && !yaffs_create_initial_dir(dev))
4995 /* Now scan the flash. */
4996 if (dev->param.is_yaffs2) {
4997 if (yaffs2_checkpt_restore(dev)) {
4998 yaffs_check_obj_details_loaded(dev->root_dir);
4999 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
5000 "yaffs: restored from checkpoint"
5004 /* Clean up the mess caused by an aborted checkpoint load
5005 * and scan backwards.
5007 yaffs_deinit_blocks(dev);
5009 yaffs_deinit_tnodes_and_objs(dev);
5011 dev->n_erased_blocks = 0;
5012 dev->n_free_chunks = 0;
5013 dev->alloc_block = -1;
5014 dev->alloc_page = -1;
5015 dev->n_deleted_files = 0;
5016 dev->n_unlinked_files = 0;
5017 dev->n_bg_deletions = 0;
5019 if (!init_failed && !yaffs_init_blocks(dev))
5022 yaffs_init_tnodes_and_objs(dev);
5025 && !yaffs_create_initial_dir(dev))
5028 if (!init_failed && !yaffs2_scan_backwards(dev))
5031 } else if (!yaffs1_scan(dev)) {
5035 yaffs_strip_deleted_objs(dev);
5036 yaffs_fix_hanging_objs(dev);
5037 if (dev->param.empty_lost_n_found)
5038 yaffs_empty_l_n_f(dev);
5042 /* Clean up the mess */
5043 yaffs_trace(YAFFS_TRACE_TRACING,
5044 "yaffs: yaffs_guts_initialise() aborted.");
5046 yaffs_deinitialise(dev);
5050 /* Zero out stats */
5051 dev->n_page_reads = 0;
5052 dev->n_page_writes = 0;
5053 dev->n_erasures = 0;
5054 dev->n_gc_copies = 0;
5055 dev->n_retired_writes = 0;
5057 dev->n_retired_blocks = 0;
5059 yaffs_verify_free_chunks(dev);
5060 yaffs_verify_blocks(dev);
5062 /* Clean up any aborted checkpoint data */
5063 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5064 yaffs2_checkpt_invalidate(dev);
5066 yaffs_trace(YAFFS_TRACE_TRACING,
5067 "yaffs: yaffs_guts_initialise() done.");
5072 void yaffs_deinitialise(struct yaffs_dev *dev)
5074 if (dev->is_mounted) {
5077 yaffs_deinit_blocks(dev);
5078 yaffs_deinit_tnodes_and_objs(dev);
5079 if (dev->param.n_caches > 0 && dev->cache) {
5081 for (i = 0; i < dev->param.n_caches; i++) {
5082 if (dev->cache[i].data)
5083 kfree(dev->cache[i].data);
5084 dev->cache[i].data = NULL;
5091 kfree(dev->gc_cleanup_list);
5093 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5094 kfree(dev->temp_buffer[i].buffer);
5096 dev->is_mounted = 0;
5098 if (dev->param.deinitialise_flash_fn)
5099 dev->param.deinitialise_flash_fn(dev);
5103 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5108 struct yaffs_block_info *blk;
5110 blk = dev->block_info;
5111 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5112 switch (blk->block_state) {
5113 case YAFFS_BLOCK_STATE_EMPTY:
5114 case YAFFS_BLOCK_STATE_ALLOCATING:
5115 case YAFFS_BLOCK_STATE_COLLECTING:
5116 case YAFFS_BLOCK_STATE_FULL:
5118 (dev->param.chunks_per_block - blk->pages_in_use +
5119 blk->soft_del_pages);
5130 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5132 /* This is what we report to the outside world */
5136 int blocks_for_checkpt;
5139 n_free = dev->n_free_chunks;
5140 n_free += dev->n_deleted_files;
5142 /* Now count the number of dirty chunks in the cache and subtract those */
5144 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5145 if (dev->cache[i].dirty)
5149 n_free -= n_dirty_caches;
5152 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5154 /* Now we figure out how much to reserve for the checkpoint and report that... */
5155 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5157 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);