2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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_getblockinfo.h"
19 #include "yaffs_tagscompat.h"
20 #include "yaffs_nand.h"
21 #include "yaffs_yaffs1.h"
22 #include "yaffs_yaffs2.h"
23 #include "yaffs_bitmap.h"
24 #include "yaffs_verify.h"
25 #include "yaffs_nand.h"
26 #include "yaffs_packedtags2.h"
27 #include "yaffs_nameval.h"
28 #include "yaffs_allocator.h"
29 #include "yaffs_attribs.h"
31 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
32 #define YAFFS_GC_GOOD_ENOUGH 2
33 #define YAFFS_GC_PASSIVE_THRESHOLD 4
35 #include "yaffs_ecc.h"
37 /* Forward declarations */
39 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
40 const u8 *buffer, int n_bytes, int use_reserve);
44 /* Function to calculate chunk and offset */
46 static inline void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
47 int *chunk_out, u32 *offset_out)
52 chunk = (u32) (addr >> dev->chunk_shift);
54 if (dev->chunk_div == 1) {
55 /* easy power of 2 case */
56 offset = (u32) (addr & dev->chunk_mask);
58 /* Non power-of-2 case */
62 chunk /= dev->chunk_div;
64 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
65 offset = (u32) (addr - chunk_base);
72 /* Function to return the number of shifts for a power of 2 greater than or
73 * equal to the given number
74 * Note we don't try to cater for all possible numbers and this does not have to
75 * be hellishly efficient.
78 static inline u32 calc_shifts_ceiling(u32 x)
83 shifts = extra_bits = 0;
98 /* Function to return the number of shifts to get a 1 in bit 0
101 static inline u32 calc_shifts(u32 x)
119 * Temporary buffer manipulations.
122 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
127 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
129 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
130 dev->temp_buffer[i].in_use = 0;
131 buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
132 dev->temp_buffer[i].buffer = buf;
135 return buf ? YAFFS_OK : YAFFS_FAIL;
138 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
143 if (dev->temp_in_use > dev->max_temp)
144 dev->max_temp = dev->temp_in_use;
146 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
147 if (dev->temp_buffer[i].in_use == 0) {
148 dev->temp_buffer[i].in_use = 1;
149 return dev->temp_buffer[i].buffer;
153 yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
155 * If we got here then we have to allocate an unmanaged one
159 dev->unmanaged_buffer_allocs++;
160 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
164 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
170 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
171 if (dev->temp_buffer[i].buffer == buffer) {
172 dev->temp_buffer[i].in_use = 0;
178 /* assume it is an unmanaged one. */
179 yaffs_trace(YAFFS_TRACE_BUFFERS, "Releasing unmanaged temp buffer");
181 dev->unmanaged_buffer_deallocs++;
187 * Determine if we have a managed buffer.
189 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer)
193 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
194 if (dev->temp_buffer[i].buffer == buffer)
198 for (i = 0; i < dev->param.n_caches; i++) {
199 if (dev->cache[i].data == buffer)
203 if (buffer == dev->checkpt_buffer)
206 yaffs_trace(YAFFS_TRACE_ALWAYS,
207 "yaffs: unmaged buffer detected.");
212 * Functions for robustisizing TODO
216 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
218 const struct yaffs_ext_tags *tags)
221 nand_chunk = nand_chunk;
226 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
227 const struct yaffs_ext_tags *tags)
230 nand_chunk = nand_chunk;
234 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
235 struct yaffs_block_info *bi)
237 if (!bi->gc_prioritise) {
238 bi->gc_prioritise = 1;
239 dev->has_pending_prioritised_gc = 1;
240 bi->chunk_error_strikes++;
242 if (bi->chunk_error_strikes > 3) {
243 bi->needs_retiring = 1; /* Too many stikes, so retire */
244 yaffs_trace(YAFFS_TRACE_ALWAYS,
245 "yaffs: Block struck out");
251 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
254 int flash_block = nand_chunk / dev->param.chunks_per_block;
255 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
257 yaffs_handle_chunk_error(dev, bi);
260 /* Was an actual write failure,
261 * so mark the block for retirement.*/
262 bi->needs_retiring = 1;
263 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
264 "**>> Block %d needs retiring", flash_block);
267 /* Delete the chunk */
268 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
269 yaffs_skip_rest_of_block(dev);
277 * Simple hash function. Needs to have a reasonable spread
280 static inline int yaffs_hash_fn(int n)
283 return n % YAFFS_NOBJECT_BUCKETS;
287 * Access functions to useful fake objects.
288 * Note that root might have a presence in NAND if permissions are set.
291 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
293 return dev->root_dir;
296 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
298 return dev->lost_n_found;
302 * Erased NAND checking functions
305 int yaffs_check_ff(u8 *buffer, int n_bytes)
307 /* Horrible, slow implementation */
316 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
318 int retval = YAFFS_OK;
319 u8 *data = yaffs_get_temp_buffer(dev);
320 struct yaffs_ext_tags tags;
323 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
325 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
328 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
330 yaffs_trace(YAFFS_TRACE_NANDACCESS,
331 "Chunk %d not erased", nand_chunk);
335 yaffs_release_temp_buffer(dev, data);
341 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
344 struct yaffs_ext_tags *tags)
346 int retval = YAFFS_OK;
347 struct yaffs_ext_tags temp_tags;
348 u8 *buffer = yaffs_get_temp_buffer(dev);
351 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
352 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
353 temp_tags.obj_id != tags->obj_id ||
354 temp_tags.chunk_id != tags->chunk_id ||
355 temp_tags.n_bytes != tags->n_bytes)
358 yaffs_release_temp_buffer(dev, buffer);
364 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
367 int reserved_blocks = dev->param.n_reserved_blocks;
370 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
373 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
375 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
378 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
381 struct yaffs_block_info *bi;
383 if (dev->n_erased_blocks < 1) {
384 /* Hoosterman we've got a problem.
385 * Can't get space to gc
387 yaffs_trace(YAFFS_TRACE_ERROR,
388 "yaffs tragedy: no more erased blocks");
393 /* Find an empty block. */
395 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
396 dev->alloc_block_finder++;
397 if (dev->alloc_block_finder < dev->internal_start_block
398 || dev->alloc_block_finder > dev->internal_end_block) {
399 dev->alloc_block_finder = dev->internal_start_block;
402 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
404 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
405 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
407 bi->seq_number = dev->seq_number;
408 dev->n_erased_blocks--;
409 yaffs_trace(YAFFS_TRACE_ALLOCATE,
410 "Allocated block %d, seq %d, %d left" ,
411 dev->alloc_block_finder, dev->seq_number,
412 dev->n_erased_blocks);
413 return dev->alloc_block_finder;
417 yaffs_trace(YAFFS_TRACE_ALWAYS,
418 "yaffs tragedy: no more erased blocks, but there should have been %d",
419 dev->n_erased_blocks);
424 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
425 struct yaffs_block_info **block_ptr)
428 struct yaffs_block_info *bi;
430 if (dev->alloc_block < 0) {
431 /* Get next block to allocate off */
432 dev->alloc_block = yaffs_find_alloc_block(dev);
436 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
437 /* No space unless we're allowed to use the reserve. */
441 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
442 && dev->alloc_page == 0)
443 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
445 /* Next page please.... */
446 if (dev->alloc_block >= 0) {
447 bi = yaffs_get_block_info(dev, dev->alloc_block);
449 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
452 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
456 dev->n_free_chunks--;
458 /* If the block is full set the state to full */
459 if (dev->alloc_page >= dev->param.chunks_per_block) {
460 bi->block_state = YAFFS_BLOCK_STATE_FULL;
461 dev->alloc_block = -1;
470 yaffs_trace(YAFFS_TRACE_ERROR,
471 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
476 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
480 n = dev->n_erased_blocks * dev->param.chunks_per_block;
482 if (dev->alloc_block > 0)
483 n += (dev->param.chunks_per_block - dev->alloc_page);
490 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
491 * if we don't want to write to it.
493 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
495 struct yaffs_block_info *bi;
497 if (dev->alloc_block > 0) {
498 bi = yaffs_get_block_info(dev, dev->alloc_block);
499 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
500 bi->block_state = YAFFS_BLOCK_STATE_FULL;
501 dev->alloc_block = -1;
506 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
508 struct yaffs_ext_tags *tags, int use_reserver)
514 yaffs2_checkpt_invalidate(dev);
517 struct yaffs_block_info *bi = 0;
520 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
526 /* First check this chunk is erased, if it needs
527 * checking. The checking policy (unless forced
528 * always on) is as follows:
530 * Check the first page we try to write in a block.
531 * If the check passes then we don't need to check any
532 * more. If the check fails, we check again...
533 * If the block has been erased, we don't need to check.
535 * However, if the block has been prioritised for gc,
536 * then we think there might be something odd about
537 * this block and stop using it.
539 * Rationale: We should only ever see chunks that have
540 * not been erased if there was a partially written
541 * chunk due to power loss. This checking policy should
542 * catch that case with very few checks and thus save a
543 * lot of checks that are most likely not needed.
546 * If an erase check fails or the write fails we skip the
550 /* let's give it a try */
553 if (dev->param.always_check_erased)
554 bi->skip_erased_check = 0;
556 if (!bi->skip_erased_check) {
557 erased_ok = yaffs_check_chunk_erased(dev, chunk);
558 if (erased_ok != YAFFS_OK) {
559 yaffs_trace(YAFFS_TRACE_ERROR,
560 "**>> yaffs chunk %d was not erased",
563 /* If not erased, delete this one,
564 * skip rest of block and
565 * try another chunk */
566 yaffs_chunk_del(dev, chunk, 1, __LINE__);
567 yaffs_skip_rest_of_block(dev);
572 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
574 if (!bi->skip_erased_check)
576 yaffs_verify_chunk_written(dev, chunk, data, tags);
578 if (write_ok != YAFFS_OK) {
579 /* Clean up aborted write, skip to next block and
580 * try another chunk */
581 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
585 bi->skip_erased_check = 1;
587 /* Copy the data into the robustification buffer */
588 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
590 } while (write_ok != YAFFS_OK &&
591 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
597 yaffs_trace(YAFFS_TRACE_ERROR,
598 "**>> yaffs write required %d attempts",
600 dev->n_retired_writes += (attempts - 1);
607 * Block retiring for handling a broken block.
610 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
612 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
614 yaffs2_checkpt_invalidate(dev);
616 yaffs2_clear_oldest_dirty_seq(dev, bi);
618 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
619 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
620 yaffs_trace(YAFFS_TRACE_ALWAYS,
621 "yaffs: Failed to mark bad and erase block %d",
624 struct yaffs_ext_tags tags;
626 flash_block * dev->param.chunks_per_block;
628 u8 *buffer = yaffs_get_temp_buffer(dev);
630 memset(buffer, 0xff, dev->data_bytes_per_chunk);
631 memset(&tags, 0, sizeof(tags));
632 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
633 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
637 yaffs_trace(YAFFS_TRACE_ALWAYS,
638 "yaffs: Failed to write bad block marker to block %d",
641 yaffs_release_temp_buffer(dev, buffer);
645 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
646 bi->gc_prioritise = 0;
647 bi->needs_retiring = 0;
649 dev->n_retired_blocks++;
652 /*---------------- Name handling functions ------------*/
654 static u16 yaffs_calc_name_sum(const YCHAR *name)
662 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
664 /* 0x1f mask is case insensitive */
665 sum += ((*name) & 0x1f) * i;
672 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
674 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
675 memset(obj->short_name, 0, sizeof(obj->short_name));
677 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
678 YAFFS_SHORT_NAME_LENGTH)
679 strcpy(obj->short_name, name);
681 obj->short_name[0] = _Y('\0');
683 obj->sum = yaffs_calc_name_sum(name);
686 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
687 const struct yaffs_obj_hdr *oh)
689 #ifdef CONFIG_YAFFS_AUTO_UNICODE
690 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
691 memset(tmp_name, 0, sizeof(tmp_name));
692 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
693 YAFFS_MAX_NAME_LENGTH + 1);
694 yaffs_set_obj_name(obj, tmp_name);
696 yaffs_set_obj_name(obj, oh->name);
700 /*-------------------- TNODES -------------------
702 * List of spare tnodes
703 * The list is hooked together using the first pointer
707 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
709 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
712 memset(tn, 0, dev->tnode_size);
716 dev->checkpoint_blocks_required = 0; /* force recalculation */
721 /* FreeTnode frees up a tnode and puts it back on the free list */
722 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
724 yaffs_free_raw_tnode(dev, tn);
726 dev->checkpoint_blocks_required = 0; /* force recalculation */
729 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
731 yaffs_deinit_raw_tnodes_and_objs(dev);
736 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
737 unsigned pos, unsigned val)
739 u32 *map = (u32 *) tn;
745 pos &= YAFFS_TNODES_LEVEL0_MASK;
746 val >>= dev->chunk_grp_bits;
748 bit_in_map = pos * dev->tnode_width;
749 word_in_map = bit_in_map / 32;
750 bit_in_word = bit_in_map & (32 - 1);
752 mask = dev->tnode_mask << bit_in_word;
754 map[word_in_map] &= ~mask;
755 map[word_in_map] |= (mask & (val << bit_in_word));
757 if (dev->tnode_width > (32 - bit_in_word)) {
758 bit_in_word = (32 - bit_in_word);
761 dev->tnode_mask >> bit_in_word;
762 map[word_in_map] &= ~mask;
763 map[word_in_map] |= (mask & (val >> bit_in_word));
767 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
770 u32 *map = (u32 *) tn;
776 pos &= YAFFS_TNODES_LEVEL0_MASK;
778 bit_in_map = pos * dev->tnode_width;
779 word_in_map = bit_in_map / 32;
780 bit_in_word = bit_in_map & (32 - 1);
782 val = map[word_in_map] >> bit_in_word;
784 if (dev->tnode_width > (32 - bit_in_word)) {
785 bit_in_word = (32 - bit_in_word);
787 val |= (map[word_in_map] << bit_in_word);
790 val &= dev->tnode_mask;
791 val <<= dev->chunk_grp_bits;
796 /* ------------------- End of individual tnode manipulation -----------------*/
798 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
799 * The look up tree is represented by the top tnode and the number of top_level
800 * in the tree. 0 means only the level 0 tnode is in the tree.
803 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
804 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
805 struct yaffs_file_var *file_struct,
808 struct yaffs_tnode *tn = file_struct->top;
811 int level = file_struct->top_level;
815 /* Check sane level and chunk Id */
816 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
819 if (chunk_id > YAFFS_MAX_CHUNK_ID)
822 /* First check we're tall enough (ie enough top_level) */
824 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
827 i >>= YAFFS_TNODES_INTERNAL_BITS;
831 if (required_depth > file_struct->top_level)
832 return NULL; /* Not tall enough, so we can't find it */
834 /* Traverse down to level 0 */
835 while (level > 0 && tn) {
836 tn = tn->internal[(chunk_id >>
837 (YAFFS_TNODES_LEVEL0_BITS +
839 YAFFS_TNODES_INTERNAL_BITS)) &
840 YAFFS_TNODES_INTERNAL_MASK];
847 /* add_find_tnode_0 finds the level 0 tnode if it exists,
848 * otherwise first expands the tree.
849 * This happens in two steps:
850 * 1. If the tree isn't tall enough, then make it taller.
851 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
853 * Used when modifying the tree.
855 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
856 * specified tn will be plugged into the ttree.
859 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
860 struct yaffs_file_var *file_struct,
862 struct yaffs_tnode *passed_tn)
867 struct yaffs_tnode *tn;
870 /* Check sane level and page Id */
871 if (file_struct->top_level < 0 ||
872 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
875 if (chunk_id > YAFFS_MAX_CHUNK_ID)
878 /* First check we're tall enough (ie enough top_level) */
880 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
883 x >>= YAFFS_TNODES_INTERNAL_BITS;
887 if (required_depth > file_struct->top_level) {
888 /* Not tall enough, gotta make the tree taller */
889 for (i = file_struct->top_level; i < required_depth; i++) {
891 tn = yaffs_get_tnode(dev);
894 tn->internal[0] = file_struct->top;
895 file_struct->top = tn;
896 file_struct->top_level++;
898 yaffs_trace(YAFFS_TRACE_ERROR,
899 "yaffs: no more tnodes");
905 /* Traverse down to level 0, adding anything we need */
907 l = file_struct->top_level;
908 tn = file_struct->top;
911 while (l > 0 && tn) {
913 (YAFFS_TNODES_LEVEL0_BITS +
914 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
915 YAFFS_TNODES_INTERNAL_MASK;
917 if ((l > 1) && !tn->internal[x]) {
918 /* Add missing non-level-zero tnode */
919 tn->internal[x] = yaffs_get_tnode(dev);
920 if (!tn->internal[x])
923 /* Looking from level 1 at level 0 */
925 /* If we already have one, release it */
927 yaffs_free_tnode(dev,
929 tn->internal[x] = passed_tn;
931 } else if (!tn->internal[x]) {
932 /* Don't have one, none passed in */
933 tn->internal[x] = yaffs_get_tnode(dev);
934 if (!tn->internal[x])
939 tn = tn->internal[x];
945 memcpy(tn, passed_tn,
946 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
947 yaffs_free_tnode(dev, passed_tn);
954 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
957 return (tags->chunk_id == chunk_obj &&
958 tags->obj_id == obj_id &&
959 !tags->is_deleted) ? 1 : 0;
963 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
964 struct yaffs_ext_tags *tags, int obj_id,
969 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
970 if (yaffs_check_chunk_bit
971 (dev, the_chunk / dev->param.chunks_per_block,
972 the_chunk % dev->param.chunks_per_block)) {
974 if (dev->chunk_grp_size == 1)
977 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
979 if (yaffs_tags_match(tags,
980 obj_id, inode_chunk)) {
991 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
992 struct yaffs_ext_tags *tags)
994 /*Get the Tnode, then get the level 0 offset chunk offset */
995 struct yaffs_tnode *tn;
997 struct yaffs_ext_tags local_tags;
999 struct yaffs_dev *dev = in->my_dev;
1002 /* Passed a NULL, so use our own tags space */
1006 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1011 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1013 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1018 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1019 struct yaffs_ext_tags *tags)
1021 /* Get the Tnode, then get the level 0 offset chunk offset */
1022 struct yaffs_tnode *tn;
1024 struct yaffs_ext_tags local_tags;
1025 struct yaffs_dev *dev = in->my_dev;
1029 /* Passed a NULL, so use our own tags space */
1033 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1038 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1040 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1043 /* Delete the entry in the filestructure (if found) */
1045 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1050 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1051 int nand_chunk, int in_scan)
1053 /* NB in_scan is zero unless scanning.
1054 * For forward scanning, in_scan is > 0;
1055 * for backward scanning in_scan is < 0
1057 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1060 struct yaffs_tnode *tn;
1061 struct yaffs_dev *dev = in->my_dev;
1063 struct yaffs_ext_tags existing_tags;
1064 struct yaffs_ext_tags new_tags;
1065 unsigned existing_serial, new_serial;
1067 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1068 /* Just ignore an attempt at putting a chunk into a non-file
1070 * If it is not during Scanning then something went wrong!
1073 yaffs_trace(YAFFS_TRACE_ERROR,
1074 "yaffs tragedy:attempt to put data chunk into a non-file"
1079 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1083 tn = yaffs_add_find_tnode_0(dev,
1084 &in->variant.file_variant,
1090 /* Dummy insert, bail now */
1093 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1096 /* If we're scanning then we need to test for duplicates
1097 * NB This does not need to be efficient since it should only
1098 * happen when the power fails during a write, then only one
1099 * chunk should ever be affected.
1101 * Correction for YAFFS2: This could happen quite a lot and we
1102 * need to think about efficiency! TODO
1103 * Update: For backward scanning we don't need to re-read tags
1104 * so this is quite cheap.
1107 if (existing_cunk > 0) {
1108 /* NB Right now existing chunk will not be real
1109 * chunk_id if the chunk group size > 1
1110 * thus we have to do a FindChunkInFile to get the
1113 * We have a duplicate now we need to decide which
1116 * Backwards scanning YAFFS2: The old one is what
1117 * we use, dump the new one.
1118 * YAFFS1: Get both sets of tags and compare serial
1123 /* Only do this for forward scanning */
1124 yaffs_rd_chunk_tags_nand(dev,
1128 /* Do a proper find */
1130 yaffs_find_chunk_in_file(in, inode_chunk,
1134 if (existing_cunk <= 0) {
1135 /*Hoosterman - how did this happen? */
1137 yaffs_trace(YAFFS_TRACE_ERROR,
1138 "yaffs tragedy: existing chunk < 0 in scan"
1143 /* NB The deleted flags should be false, otherwise
1144 * the chunks will not be loaded during a scan
1148 new_serial = new_tags.serial_number;
1149 existing_serial = existing_tags.serial_number;
1152 if ((in_scan > 0) &&
1153 (existing_cunk <= 0 ||
1154 ((existing_serial + 1) & 3) == new_serial)) {
1155 /* Forward scanning.
1157 * Delete the old one and drop through to
1160 yaffs_chunk_del(dev, existing_cunk, 1,
1163 /* Backward scanning or we want to use the
1165 * Delete the new one and return early so that
1166 * the tnode isn't changed
1168 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1175 if (existing_cunk == 0)
1176 in->n_data_chunks++;
1178 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1183 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1185 struct yaffs_block_info *the_block;
1188 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1190 block_no = chunk / dev->param.chunks_per_block;
1191 the_block = yaffs_get_block_info(dev, block_no);
1193 the_block->soft_del_pages++;
1194 dev->n_free_chunks++;
1195 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1199 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1200 * the chunks in the file.
1201 * All soft deleting does is increment the block's softdelete count and pulls
1202 * the chunk out of the tnode.
1203 * Thus, essentially this is the same as DeleteWorker except that the chunks
1207 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1208 u32 level, int chunk_offset)
1213 struct yaffs_dev *dev = in->my_dev;
1219 for (i = YAFFS_NTNODES_INTERNAL - 1;
1222 if (tn->internal[i]) {
1224 yaffs_soft_del_worker(in,
1228 YAFFS_TNODES_INTERNAL_BITS)
1231 yaffs_free_tnode(dev,
1233 tn->internal[i] = NULL;
1235 /* Can this happen? */
1239 return (all_done) ? 1 : 0;
1243 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1244 the_chunk = yaffs_get_group_base(dev, tn, i);
1246 yaffs_soft_del_chunk(dev, the_chunk);
1247 yaffs_load_tnode_0(dev, tn, i, 0);
1253 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1255 struct yaffs_dev *dev = obj->my_dev;
1256 struct yaffs_obj *parent;
1258 yaffs_verify_obj_in_dir(obj);
1259 parent = obj->parent;
1261 yaffs_verify_dir(parent);
1263 if (dev && dev->param.remove_obj_fn)
1264 dev->param.remove_obj_fn(obj);
1266 list_del_init(&obj->siblings);
1269 yaffs_verify_dir(parent);
1272 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1275 yaffs_trace(YAFFS_TRACE_ALWAYS,
1276 "tragedy: Trying to add an object to a null pointer directory"
1281 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1282 yaffs_trace(YAFFS_TRACE_ALWAYS,
1283 "tragedy: Trying to add an object to a non-directory"
1288 if (obj->siblings.prev == NULL) {
1289 /* Not initialised */
1293 yaffs_verify_dir(directory);
1295 yaffs_remove_obj_from_dir(obj);
1298 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1299 obj->parent = directory;
1301 if (directory == obj->my_dev->unlinked_dir
1302 || directory == obj->my_dev->del_dir) {
1304 obj->my_dev->n_unlinked_files++;
1305 obj->rename_allowed = 0;
1308 yaffs_verify_dir(directory);
1309 yaffs_verify_obj_in_dir(obj);
1312 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1313 struct yaffs_obj *new_dir,
1314 const YCHAR *new_name, int force, int shadows)
1318 struct yaffs_obj *existing_target;
1320 if (new_dir == NULL)
1321 new_dir = obj->parent; /* use the old directory */
1323 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1324 yaffs_trace(YAFFS_TRACE_ALWAYS,
1325 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1330 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1331 del_op = (new_dir == obj->my_dev->del_dir);
1333 existing_target = yaffs_find_by_name(new_dir, new_name);
1335 /* If the object is a file going into the unlinked directory,
1336 * then it is OK to just stuff it in since duplicate names are OK.
1337 * else only proceed if the new name does not exist and we're putting
1338 * it into a directory.
1340 if (!(unlink_op || del_op || force ||
1341 shadows > 0 || !existing_target) ||
1342 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1345 yaffs_set_obj_name(obj, new_name);
1347 yaffs_add_obj_to_dir(new_dir, obj);
1352 /* If it is a deletion then we mark it as a shrink for gc */
1353 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1359 /*------------------------ Short Operations Cache ------------------------------
1360 * In many situations where there is no high level buffering a lot of
1361 * reads might be short sequential reads, and a lot of writes may be short
1362 * sequential writes. eg. scanning/writing a jpeg file.
1363 * In these cases, a short read/write cache can provide a huge perfomance
1364 * benefit with dumb-as-a-rock code.
1365 * In Linux, the page cache provides read buffering and the short op cache
1366 * provides write buffering.
1368 * There are a small number (~10) of cache chunks per device so that we don't
1369 * need a very intelligent search.
1372 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1374 struct yaffs_dev *dev = obj->my_dev;
1376 struct yaffs_cache *cache;
1377 int n_caches = obj->my_dev->param.n_caches;
1379 for (i = 0; i < n_caches; i++) {
1380 cache = &dev->cache[i];
1381 if (cache->object == obj && cache->dirty)
1388 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1390 struct yaffs_dev *dev = obj->my_dev;
1391 int lowest = -99; /* Stop compiler whining. */
1393 struct yaffs_cache *cache;
1394 int chunk_written = 0;
1395 int n_caches = obj->my_dev->param.n_caches;
1402 /* Find the lowest dirty chunk for this object */
1403 for (i = 0; i < n_caches; i++) {
1404 if (dev->cache[i].object == obj &&
1405 dev->cache[i].dirty) {
1407 dev->cache[i].chunk_id < lowest) {
1408 cache = &dev->cache[i];
1409 lowest = cache->chunk_id;
1414 if (cache && !cache->locked) {
1415 /* Write it out and free it up */
1417 yaffs_wr_data_obj(cache->object,
1422 cache->object = NULL;
1424 } while (cache && chunk_written > 0);
1427 /* Hoosterman, disk full while writing cache out. */
1428 yaffs_trace(YAFFS_TRACE_ERROR,
1429 "yaffs tragedy: no space during cache write");
1432 /*yaffs_flush_whole_cache(dev)
1437 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1439 struct yaffs_obj *obj;
1440 int n_caches = dev->param.n_caches;
1443 /* Find a dirty object in the cache and flush it...
1444 * until there are no further dirty objects.
1448 for (i = 0; i < n_caches && !obj; i++) {
1449 if (dev->cache[i].object && dev->cache[i].dirty)
1450 obj = dev->cache[i].object;
1453 yaffs_flush_file_cache(obj);
1458 /* Grab us a cache chunk for use.
1459 * First look for an empty one.
1460 * Then look for the least recently used non-dirty one.
1461 * Then look for the least recently used dirty one...., flush and look again.
1463 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1467 if (dev->param.n_caches > 0) {
1468 for (i = 0; i < dev->param.n_caches; i++) {
1469 if (!dev->cache[i].object)
1470 return &dev->cache[i];
1476 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1478 struct yaffs_cache *cache;
1479 struct yaffs_obj *the_obj;
1484 if (dev->param.n_caches < 1)
1487 /* Try find a non-dirty one... */
1489 cache = yaffs_grab_chunk_worker(dev);
1492 /* They were all dirty, find the LRU object and flush
1493 * its cache, then find again.
1494 * NB what's here is not very accurate,
1495 * we actually flush the object with the LRU chunk.
1498 /* With locking we can't assume we can use entry zero,
1499 * Set the_obj to a valid pointer for Coverity. */
1500 the_obj = dev->cache[0].object;
1505 for (i = 0; i < dev->param.n_caches; i++) {
1506 if (dev->cache[i].object &&
1507 !dev->cache[i].locked &&
1508 (dev->cache[i].last_use < usage ||
1510 usage = dev->cache[i].last_use;
1511 the_obj = dev->cache[i].object;
1512 cache = &dev->cache[i];
1517 if (!cache || cache->dirty) {
1518 /* Flush and try again */
1519 yaffs_flush_file_cache(the_obj);
1520 cache = yaffs_grab_chunk_worker(dev);
1526 /* Find a cached chunk */
1527 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1530 struct yaffs_dev *dev = obj->my_dev;
1533 if (dev->param.n_caches < 1)
1536 for (i = 0; i < dev->param.n_caches; i++) {
1537 if (dev->cache[i].object == obj &&
1538 dev->cache[i].chunk_id == chunk_id) {
1541 return &dev->cache[i];
1547 /* Mark the chunk for the least recently used algorithym */
1548 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1553 if (dev->param.n_caches < 1)
1556 if (dev->cache_last_use < 0 ||
1557 dev->cache_last_use > 100000000) {
1558 /* Reset the cache usages */
1559 for (i = 1; i < dev->param.n_caches; i++)
1560 dev->cache[i].last_use = 0;
1562 dev->cache_last_use = 0;
1564 dev->cache_last_use++;
1565 cache->last_use = dev->cache_last_use;
1571 /* Invalidate a single cache page.
1572 * Do this when a whole page gets written,
1573 * ie the short cache for this page is no longer valid.
1575 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1577 struct yaffs_cache *cache;
1579 if (object->my_dev->param.n_caches > 0) {
1580 cache = yaffs_find_chunk_cache(object, chunk_id);
1583 cache->object = NULL;
1587 /* Invalidate all the cache pages associated with this object
1588 * Do this whenever ther file is deleted or resized.
1590 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1593 struct yaffs_dev *dev = in->my_dev;
1595 if (dev->param.n_caches > 0) {
1596 /* Invalidate it. */
1597 for (i = 0; i < dev->param.n_caches; i++) {
1598 if (dev->cache[i].object == in)
1599 dev->cache[i].object = NULL;
1604 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1607 struct yaffs_dev *dev = obj->my_dev;
1609 /* If it is still linked into the bucket list, free from the list */
1610 if (!list_empty(&obj->hash_link)) {
1611 list_del_init(&obj->hash_link);
1612 bucket = yaffs_hash_fn(obj->obj_id);
1613 dev->obj_bucket[bucket].count--;
1617 /* FreeObject frees up a Object and puts it back on the free list */
1618 static void yaffs_free_obj(struct yaffs_obj *obj)
1620 struct yaffs_dev *dev;
1627 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1628 obj, obj->my_inode);
1631 if (!list_empty(&obj->siblings))
1634 if (obj->my_inode) {
1635 /* We're still hooked up to a cached inode.
1636 * Don't delete now, but mark for later deletion
1638 obj->defered_free = 1;
1642 yaffs_unhash_obj(obj);
1644 yaffs_free_raw_obj(dev, obj);
1646 dev->checkpoint_blocks_required = 0; /* force recalculation */
1649 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1651 if (obj->defered_free)
1652 yaffs_free_obj(obj);
1655 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1657 /* Iinvalidate the file's data in the cache, without flushing. */
1658 yaffs_invalidate_whole_cache(in);
1660 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1661 /* Move to unlinked directory so we have a deletion record */
1662 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1666 yaffs_remove_obj_from_dir(in);
1667 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1675 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1677 if (!obj->deleted ||
1678 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1682 if (obj->n_data_chunks <= 0) {
1683 /* Empty file with no duplicate object headers,
1684 * just delete it immediately */
1685 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1686 obj->variant.file_variant.top = NULL;
1687 yaffs_trace(YAFFS_TRACE_TRACING,
1688 "yaffs: Deleting empty file %d",
1690 yaffs_generic_obj_del(obj);
1692 yaffs_soft_del_worker(obj,
1693 obj->variant.file_variant.top,
1695 file_variant.top_level, 0);
1700 /* Pruning removes any part of the file structure tree that is beyond the
1701 * bounds of the file (ie that does not point to chunks).
1703 * A file should only get pruned when its size is reduced.
1705 * Before pruning, the chunks must be pulled from the tree and the
1706 * level 0 tnode entries must be zeroed out.
1707 * Could also use this for file deletion, but that's probably better handled
1708 * by a special case.
1710 * This function is recursive. For levels > 0 the function is called again on
1711 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1712 * If there is no data in a subtree then it is pruned.
1715 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1716 struct yaffs_tnode *tn, u32 level,
1728 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1729 if (tn->internal[i]) {
1731 yaffs_prune_worker(dev,
1734 (i == 0) ? del0 : 1);
1737 if (tn->internal[i])
1741 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1742 u32 *map = (u32 *) tn;
1744 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1750 if (has_data == 0 && del0) {
1751 /* Free and return NULL */
1752 yaffs_free_tnode(dev, tn);
1758 static int yaffs_prune_tree(struct yaffs_dev *dev,
1759 struct yaffs_file_var *file_struct)
1764 struct yaffs_tnode *tn;
1766 if (file_struct->top_level < 1)
1770 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1772 /* Now we have a tree with all the non-zero branches NULL but
1773 * the height is the same as it was.
1774 * Let's see if we can trim internal tnodes to shorten the tree.
1775 * We can do this if only the 0th element in the tnode is in use
1776 * (ie all the non-zero are NULL)
1779 while (file_struct->top_level && !done) {
1780 tn = file_struct->top;
1783 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1784 if (tn->internal[i])
1789 file_struct->top = tn->internal[0];
1790 file_struct->top_level--;
1791 yaffs_free_tnode(dev, tn);
1800 /*-------------------- End of File Structure functions.-------------------*/
1802 /* alloc_empty_obj gets us a clean Object.*/
1803 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1805 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1812 /* Now sweeten it up... */
1814 memset(obj, 0, sizeof(struct yaffs_obj));
1815 obj->being_created = 1;
1819 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1820 INIT_LIST_HEAD(&(obj->hard_links));
1821 INIT_LIST_HEAD(&(obj->hash_link));
1822 INIT_LIST_HEAD(&obj->siblings);
1824 /* Now make the directory sane */
1825 if (dev->root_dir) {
1826 obj->parent = dev->root_dir;
1827 list_add(&(obj->siblings),
1828 &dev->root_dir->variant.dir_variant.children);
1831 /* Add it to the lost and found directory.
1832 * NB Can't put root or lost-n-found in lost-n-found so
1833 * check if lost-n-found exists first
1835 if (dev->lost_n_found)
1836 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1838 obj->being_created = 0;
1840 dev->checkpoint_blocks_required = 0; /* force recalculation */
1845 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1849 int lowest = 999999;
1851 /* Search for the shortest list or one that
1855 for (i = 0; i < 10 && lowest > 4; i++) {
1856 dev->bucket_finder++;
1857 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1858 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1859 lowest = dev->obj_bucket[dev->bucket_finder].count;
1860 l = dev->bucket_finder;
1867 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1869 int bucket = yaffs_find_nice_bucket(dev);
1871 struct list_head *i;
1872 u32 n = (u32) bucket;
1874 /* Now find an object value that has not already been taken
1875 * by scanning the list.
1880 n += YAFFS_NOBJECT_BUCKETS;
1881 if (1 || dev->obj_bucket[bucket].count > 0) {
1882 list_for_each(i, &dev->obj_bucket[bucket].list) {
1883 /* If there is already one in the list */
1884 if (i && list_entry(i, struct yaffs_obj,
1885 hash_link)->obj_id == n) {
1894 static void yaffs_hash_obj(struct yaffs_obj *in)
1896 int bucket = yaffs_hash_fn(in->obj_id);
1897 struct yaffs_dev *dev = in->my_dev;
1899 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1900 dev->obj_bucket[bucket].count++;
1903 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1905 int bucket = yaffs_hash_fn(number);
1906 struct list_head *i;
1907 struct yaffs_obj *in;
1909 list_for_each(i, &dev->obj_bucket[bucket].list) {
1910 /* Look if it is in the list */
1911 in = list_entry(i, struct yaffs_obj, hash_link);
1912 if (in->obj_id == number) {
1913 /* Don't show if it is defered free */
1914 if (in->defered_free)
1923 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1924 enum yaffs_obj_type type)
1926 struct yaffs_obj *the_obj = NULL;
1927 struct yaffs_tnode *tn = NULL;
1930 number = yaffs_new_obj_id(dev);
1932 if (type == YAFFS_OBJECT_TYPE_FILE) {
1933 tn = yaffs_get_tnode(dev);
1938 the_obj = yaffs_alloc_empty_obj(dev);
1941 yaffs_free_tnode(dev, tn);
1946 the_obj->rename_allowed = 1;
1947 the_obj->unlink_allowed = 1;
1948 the_obj->obj_id = number;
1949 yaffs_hash_obj(the_obj);
1950 the_obj->variant_type = type;
1951 yaffs_load_current_time(the_obj, 1, 1);
1954 case YAFFS_OBJECT_TYPE_FILE:
1955 the_obj->variant.file_variant.file_size = 0;
1956 the_obj->variant.file_variant.scanned_size = 0;
1957 the_obj->variant.file_variant.shrink_size = ~0; /* max */
1958 the_obj->variant.file_variant.top_level = 0;
1959 the_obj->variant.file_variant.top = tn;
1961 case YAFFS_OBJECT_TYPE_DIRECTORY:
1962 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1963 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1965 case YAFFS_OBJECT_TYPE_SYMLINK:
1966 case YAFFS_OBJECT_TYPE_HARDLINK:
1967 case YAFFS_OBJECT_TYPE_SPECIAL:
1968 /* No action required */
1970 case YAFFS_OBJECT_TYPE_UNKNOWN:
1971 /* todo this should not happen */
1977 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1978 int number, u32 mode)
1981 struct yaffs_obj *obj =
1982 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1987 obj->fake = 1; /* it is fake so it might not use NAND */
1988 obj->rename_allowed = 0;
1989 obj->unlink_allowed = 0;
1992 obj->yst_mode = mode;
1994 obj->hdr_chunk = 0; /* Not a valid chunk. */
2000 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2006 yaffs_init_raw_tnodes_and_objs(dev);
2008 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2009 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2010 dev->obj_bucket[i].count = 0;
2014 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2016 enum yaffs_obj_type type)
2018 struct yaffs_obj *the_obj = NULL;
2021 the_obj = yaffs_find_by_number(dev, number);
2024 the_obj = yaffs_new_obj(dev, number, type);
2030 YCHAR *yaffs_clone_str(const YCHAR *str)
2032 YCHAR *new_str = NULL;
2038 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2039 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2041 strncpy(new_str, str, len);
2048 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2049 * link (ie. name) is created or deleted in the directory.
2052 * create dir/a : update dir's mtime/ctime
2053 * rm dir/a: update dir's mtime/ctime
2054 * modify dir/a: don't update dir's mtimme/ctime
2056 * This can be handled immediately or defered. Defering helps reduce the number
2057 * of updates when many files in a directory are changed within a brief period.
2059 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2060 * called periodically.
2063 static void yaffs_update_parent(struct yaffs_obj *obj)
2065 struct yaffs_dev *dev;
2071 yaffs_load_current_time(obj, 0, 1);
2072 if (dev->param.defered_dir_update) {
2073 struct list_head *link = &obj->variant.dir_variant.dirty;
2075 if (list_empty(link)) {
2076 list_add(link, &dev->dirty_dirs);
2077 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2078 "Added object %d to dirty directories",
2083 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2087 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2089 struct list_head *link;
2090 struct yaffs_obj *obj;
2091 struct yaffs_dir_var *d_s;
2092 union yaffs_obj_var *o_v;
2094 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2096 while (!list_empty(&dev->dirty_dirs)) {
2097 link = dev->dirty_dirs.next;
2098 list_del_init(link);
2100 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2101 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2102 obj = list_entry(o_v, struct yaffs_obj, variant);
2104 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2108 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2113 * Mknod (create) a new object.
2114 * equiv_obj only has meaning for a hard link;
2115 * alias_str only has meaning for a symlink.
2116 * rdev only has meaning for devices (a subset of special objects)
2119 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2120 struct yaffs_obj *parent,
2125 struct yaffs_obj *equiv_obj,
2126 const YCHAR *alias_str, u32 rdev)
2128 struct yaffs_obj *in;
2130 struct yaffs_dev *dev = parent->my_dev;
2132 /* Check if the entry exists.
2133 * If it does then fail the call since we don't want a dup. */
2134 if (yaffs_find_by_name(parent, name))
2137 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2138 str = yaffs_clone_str(alias_str);
2143 in = yaffs_new_obj(dev, -1, type);
2152 in->variant_type = type;
2154 in->yst_mode = mode;
2156 yaffs_attribs_init(in, gid, uid, rdev);
2158 in->n_data_chunks = 0;
2160 yaffs_set_obj_name(in, name);
2163 yaffs_add_obj_to_dir(parent, in);
2165 in->my_dev = parent->my_dev;
2168 case YAFFS_OBJECT_TYPE_SYMLINK:
2169 in->variant.symlink_variant.alias = str;
2171 case YAFFS_OBJECT_TYPE_HARDLINK:
2172 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2173 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
2174 list_add(&in->hard_links, &equiv_obj->hard_links);
2176 case YAFFS_OBJECT_TYPE_FILE:
2177 case YAFFS_OBJECT_TYPE_DIRECTORY:
2178 case YAFFS_OBJECT_TYPE_SPECIAL:
2179 case YAFFS_OBJECT_TYPE_UNKNOWN:
2184 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2185 /* Could not create the object header, fail */
2191 yaffs_update_parent(parent);
2196 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2197 const YCHAR *name, u32 mode, u32 uid,
2200 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2201 uid, gid, NULL, NULL, 0);
2204 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2205 u32 mode, u32 uid, u32 gid)
2207 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2208 mode, uid, gid, NULL, NULL, 0);
2211 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2212 const YCHAR *name, u32 mode, u32 uid,
2215 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2216 uid, gid, NULL, NULL, rdev);
2219 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2220 const YCHAR *name, u32 mode, u32 uid,
2221 u32 gid, const YCHAR *alias)
2223 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2224 uid, gid, NULL, alias, 0);
2227 /* yaffs_link_obj returns the object id of the equivalent object.*/
2228 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2229 struct yaffs_obj *equiv_obj)
2231 /* Get the real object in case we were fed a hard link obj */
2232 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2234 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2235 parent, name, 0, 0, 0,
2236 equiv_obj, NULL, 0))
2245 /*---------------------- Block Management and Page Allocation -------------*/
2247 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2249 if (dev->block_info_alt && dev->block_info)
2250 vfree(dev->block_info);
2252 kfree(dev->block_info);
2254 dev->block_info_alt = 0;
2256 dev->block_info = NULL;
2258 if (dev->chunk_bits_alt && dev->chunk_bits)
2259 vfree(dev->chunk_bits);
2261 kfree(dev->chunk_bits);
2262 dev->chunk_bits_alt = 0;
2263 dev->chunk_bits = NULL;
2266 static int yaffs_init_blocks(struct yaffs_dev *dev)
2268 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2270 dev->block_info = NULL;
2271 dev->chunk_bits = NULL;
2272 dev->alloc_block = -1; /* force it to get a new one */
2274 /* If the first allocation strategy fails, thry the alternate one */
2276 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2277 if (!dev->block_info) {
2279 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2280 dev->block_info_alt = 1;
2282 dev->block_info_alt = 0;
2285 if (!dev->block_info)
2288 /* Set up dynamic blockinfo stuff. Round up bytes. */
2289 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2291 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2292 if (!dev->chunk_bits) {
2294 vmalloc(dev->chunk_bit_stride * n_blocks);
2295 dev->chunk_bits_alt = 1;
2297 dev->chunk_bits_alt = 0;
2299 if (!dev->chunk_bits)
2303 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2304 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2308 yaffs_deinit_blocks(dev);
2313 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2315 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2319 /* If the block is still healthy erase it and mark as clean.
2320 * If the block has had a data failure, then retire it.
2323 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2324 "yaffs_block_became_dirty block %d state %d %s",
2325 block_no, bi->block_state,
2326 (bi->needs_retiring) ? "needs retiring" : "");
2328 yaffs2_clear_oldest_dirty_seq(dev, bi);
2330 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2332 /* If this is the block being garbage collected then stop gc'ing */
2333 if (block_no == dev->gc_block)
2336 /* If this block is currently the best candidate for gc
2337 * then drop as a candidate */
2338 if (block_no == dev->gc_dirtiest) {
2339 dev->gc_dirtiest = 0;
2340 dev->gc_pages_in_use = 0;
2343 if (!bi->needs_retiring) {
2344 yaffs2_checkpt_invalidate(dev);
2345 erased_ok = yaffs_erase_block(dev, block_no);
2347 dev->n_erase_failures++;
2348 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2349 "**>> Erasure failed %d", block_no);
2353 /* Verify erasure if needed */
2355 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2356 !yaffs_skip_verification(dev))) {
2357 for (i = 0; i < dev->param.chunks_per_block; i++) {
2358 if (!yaffs_check_chunk_erased(dev,
2359 block_no * dev->param.chunks_per_block + i)) {
2360 yaffs_trace(YAFFS_TRACE_ERROR,
2361 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2368 /* We lost a block of free space */
2369 dev->n_free_chunks -= dev->param.chunks_per_block;
2370 yaffs_retire_block(dev, block_no);
2371 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2372 "**>> Block %d retired", block_no);
2376 /* Clean it up... */
2377 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2379 dev->n_erased_blocks++;
2380 bi->pages_in_use = 0;
2381 bi->soft_del_pages = 0;
2382 bi->has_shrink_hdr = 0;
2383 bi->skip_erased_check = 1; /* Clean, so no need to check */
2384 bi->gc_prioritise = 0;
2385 yaffs_clear_chunk_bits(dev, block_no);
2387 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2390 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2391 struct yaffs_block_info *bi,
2392 int old_chunk, u8 *buffer)
2396 struct yaffs_ext_tags tags;
2397 struct yaffs_obj *object;
2399 int ret_val = YAFFS_OK;
2401 memset(&tags, 0, sizeof(tags));
2402 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2404 object = yaffs_find_by_number(dev, tags.obj_id);
2406 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2407 "Collecting chunk in block %d, %d %d %d ",
2408 dev->gc_chunk, tags.obj_id,
2409 tags.chunk_id, tags.n_bytes);
2411 if (object && !yaffs_skip_verification(dev)) {
2412 if (tags.chunk_id == 0)
2415 else if (object->soft_del)
2416 /* Defeat the test */
2417 matching_chunk = old_chunk;
2420 yaffs_find_chunk_in_file
2421 (object, tags.chunk_id,
2424 if (old_chunk != matching_chunk)
2425 yaffs_trace(YAFFS_TRACE_ERROR,
2426 "gc: page in gc mismatch: %d %d %d %d",
2434 yaffs_trace(YAFFS_TRACE_ERROR,
2435 "page %d in gc has no object: %d %d %d ",
2437 tags.obj_id, tags.chunk_id,
2443 object->soft_del && tags.chunk_id != 0) {
2444 /* Data chunk in a soft deleted file,
2446 * It's a soft deleted data chunk,
2447 * No need to copy this, just forget
2448 * about it and fix up the object.
2451 /* Free chunks already includes
2452 * softdeleted chunks, how ever this
2453 * chunk is going to soon be really
2454 * deleted which will increment free
2455 * chunks. We have to decrement free
2456 * chunks so this works out properly.
2458 dev->n_free_chunks--;
2459 bi->soft_del_pages--;
2461 object->n_data_chunks--;
2462 if (object->n_data_chunks <= 0) {
2463 /* remeber to clean up obj */
2464 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2468 } else if (object) {
2469 /* It's either a data chunk in a live
2470 * file or an ObjectHeader, so we're
2472 * NB Need to keep the ObjectHeaders of
2473 * deleted files until the whole file
2474 * has been deleted off
2476 tags.serial_number++;
2479 if (tags.chunk_id == 0) {
2480 /* It is an object Id,
2481 * We need to nuke the
2482 * shrinkheader flags since its
2484 * Also need to clean up
2487 struct yaffs_obj_hdr *oh;
2488 oh = (struct yaffs_obj_hdr *) buffer;
2491 tags.extra_is_shrink = 0;
2492 oh->shadows_obj = 0;
2493 oh->inband_shadowed_obj_id = 0;
2494 tags.extra_shadows = 0;
2496 /* Update file size */
2497 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2499 object->variant.file_variant.file_size;
2500 tags.extra_length = oh->file_size;
2503 yaffs_verify_oh(object, oh, &tags, 1);
2505 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2508 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2511 if (new_chunk < 0) {
2512 ret_val = YAFFS_FAIL;
2515 /* Now fix up the Tnodes etc. */
2517 if (tags.chunk_id == 0) {
2519 object->hdr_chunk = new_chunk;
2520 object->serial = tags.serial_number;
2522 /* It's a data chunk */
2523 yaffs_put_chunk_in_file(object, tags.chunk_id,
2528 if (ret_val == YAFFS_OK)
2529 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2533 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2536 int ret_val = YAFFS_OK;
2538 int is_checkpt_block;
2540 int chunks_before = yaffs_get_erased_chunks(dev);
2542 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2544 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2546 yaffs_trace(YAFFS_TRACE_TRACING,
2547 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2548 block, bi->pages_in_use, bi->has_shrink_hdr,
2551 /*yaffs_verify_free_chunks(dev); */
2553 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2554 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2556 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2558 dev->gc_disable = 1;
2560 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2561 yaffs_trace(YAFFS_TRACE_TRACING,
2562 "Collecting block %d that has no chunks in use",
2564 yaffs_block_became_dirty(dev, block);
2567 u8 *buffer = yaffs_get_temp_buffer(dev);
2569 yaffs_verify_blk(dev, bi, block);
2571 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2572 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2574 for (/* init already done */ ;
2575 ret_val == YAFFS_OK &&
2576 dev->gc_chunk < dev->param.chunks_per_block &&
2577 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2579 dev->gc_chunk++, old_chunk++) {
2580 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2581 /* Page is in use and might need to be copied */
2583 ret_val = yaffs_gc_process_chunk(dev, bi,
2587 yaffs_release_temp_buffer(dev, buffer);
2590 yaffs_verify_collected_blk(dev, bi, block);
2592 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2594 * The gc did not complete. Set block state back to FULL
2595 * because checkpointing does not restore gc.
2597 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2599 /* The gc completed. */
2600 /* Do any required cleanups */
2601 for (i = 0; i < dev->n_clean_ups; i++) {
2602 /* Time to delete the file too */
2603 struct yaffs_obj *object =
2604 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2606 yaffs_free_tnode(dev,
2607 object->variant.file_variant.top);
2608 object->variant.file_variant.top = NULL;
2609 yaffs_trace(YAFFS_TRACE_GC,
2610 "yaffs: About to finally delete object %d",
2612 yaffs_generic_obj_del(object);
2613 object->my_dev->n_deleted_files--;
2617 chunks_after = yaffs_get_erased_chunks(dev);
2618 if (chunks_before >= chunks_after)
2619 yaffs_trace(YAFFS_TRACE_GC,
2620 "gc did not increase free chunks before %d after %d",
2621 chunks_before, chunks_after);
2624 dev->n_clean_ups = 0;
2627 dev->gc_disable = 0;
2633 * find_gc_block() selects the dirtiest block (or close enough)
2634 * for garbage collection.
2637 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2638 int aggressive, int background)
2642 unsigned selected = 0;
2643 int prioritised = 0;
2644 int prioritised_exist = 0;
2645 struct yaffs_block_info *bi;
2648 /* First let's see if we need to grab a prioritised block */
2649 if (dev->has_pending_prioritised_gc && !aggressive) {
2650 dev->gc_dirtiest = 0;
2651 bi = dev->block_info;
2652 for (i = dev->internal_start_block;
2653 i <= dev->internal_end_block && !selected; i++) {
2655 if (bi->gc_prioritise) {
2656 prioritised_exist = 1;
2657 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2658 yaffs_block_ok_for_gc(dev, bi)) {
2667 * If there is a prioritised block and none was selected then
2668 * this happened because there is at least one old dirty block
2669 * gumming up the works. Let's gc the oldest dirty block.
2672 if (prioritised_exist &&
2673 !selected && dev->oldest_dirty_block > 0)
2674 selected = dev->oldest_dirty_block;
2676 if (!prioritised_exist) /* None found, so we can clear this */
2677 dev->has_pending_prioritised_gc = 0;
2680 /* If we're doing aggressive GC then we are happy to take a less-dirty
2681 * block, and search harder.
2682 * else (leasurely gc), then we only bother to do this if the
2683 * block has only a few pages in use.
2689 dev->internal_end_block - dev->internal_start_block + 1;
2691 threshold = dev->param.chunks_per_block;
2692 iterations = n_blocks;
2697 max_threshold = dev->param.chunks_per_block / 2;
2699 max_threshold = dev->param.chunks_per_block / 8;
2701 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2702 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2704 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2705 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2706 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2707 if (threshold > max_threshold)
2708 threshold = max_threshold;
2710 iterations = n_blocks / 16 + 1;
2711 if (iterations > 100)
2717 (dev->gc_dirtiest < 1 ||
2718 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2720 dev->gc_block_finder++;
2721 if (dev->gc_block_finder < dev->internal_start_block ||
2722 dev->gc_block_finder > dev->internal_end_block)
2723 dev->gc_block_finder =
2724 dev->internal_start_block;
2726 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2728 pages_used = bi->pages_in_use - bi->soft_del_pages;
2730 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2731 pages_used < dev->param.chunks_per_block &&
2732 (dev->gc_dirtiest < 1 ||
2733 pages_used < dev->gc_pages_in_use) &&
2734 yaffs_block_ok_for_gc(dev, bi)) {
2735 dev->gc_dirtiest = dev->gc_block_finder;
2736 dev->gc_pages_in_use = pages_used;
2740 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2741 selected = dev->gc_dirtiest;
2745 * If nothing has been selected for a while, try the oldest dirty
2746 * because that's gumming up the works.
2749 if (!selected && dev->param.is_yaffs2 &&
2750 dev->gc_not_done >= (background ? 10 : 20)) {
2751 yaffs2_find_oldest_dirty_seq(dev);
2752 if (dev->oldest_dirty_block > 0) {
2753 selected = dev->oldest_dirty_block;
2754 dev->gc_dirtiest = selected;
2755 dev->oldest_dirty_gc_count++;
2756 bi = yaffs_get_block_info(dev, selected);
2757 dev->gc_pages_in_use =
2758 bi->pages_in_use - bi->soft_del_pages;
2760 dev->gc_not_done = 0;
2765 yaffs_trace(YAFFS_TRACE_GC,
2766 "GC Selected block %d with %d free, prioritised:%d",
2768 dev->param.chunks_per_block - dev->gc_pages_in_use,
2775 dev->gc_dirtiest = 0;
2776 dev->gc_pages_in_use = 0;
2777 dev->gc_not_done = 0;
2778 if (dev->refresh_skip > 0)
2779 dev->refresh_skip--;
2782 yaffs_trace(YAFFS_TRACE_GC,
2783 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2784 dev->gc_block_finder, dev->gc_not_done, threshold,
2785 dev->gc_dirtiest, dev->gc_pages_in_use,
2786 dev->oldest_dirty_block, background ? " bg" : "");
2792 /* New garbage collector
2793 * If we're very low on erased blocks then we do aggressive garbage collection
2794 * otherwise we do "leasurely" garbage collection.
2795 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2796 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2798 * The idea is to help clear out space in a more spread-out manner.
2799 * Dunno if it really does anything useful.
2801 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2804 int gc_ok = YAFFS_OK;
2808 int checkpt_block_adjust;
2810 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2813 if (dev->gc_disable)
2814 /* Bail out so we don't get recursive gc */
2817 /* This loop should pass the first time.
2818 * Only loops here if the collection does not increase space.
2824 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2827 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2829 dev->n_erased_blocks * dev->param.chunks_per_block;
2831 /* If we need a block soon then do aggressive gc. */
2832 if (dev->n_erased_blocks < min_erased)
2836 && erased_chunks > (dev->n_free_chunks / 4))
2839 if (dev->gc_skip > 20)
2841 if (erased_chunks < dev->n_free_chunks / 2 ||
2842 dev->gc_skip < 1 || background)
2852 /* If we don't already have a block being gc'd then see if we
2853 * should start another */
2855 if (dev->gc_block < 1 && !aggressive) {
2856 dev->gc_block = yaffs2_find_refresh_block(dev);
2858 dev->n_clean_ups = 0;
2860 if (dev->gc_block < 1) {
2862 yaffs_find_gc_block(dev, aggressive, background);
2864 dev->n_clean_ups = 0;
2867 if (dev->gc_block > 0) {
2870 dev->passive_gc_count++;
2872 yaffs_trace(YAFFS_TRACE_GC,
2873 "yaffs: GC n_erased_blocks %d aggressive %d",
2874 dev->n_erased_blocks, aggressive);
2876 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2879 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
2880 dev->gc_block > 0) {
2881 yaffs_trace(YAFFS_TRACE_GC,
2882 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2883 dev->n_erased_blocks, max_tries,
2886 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2887 (dev->gc_block > 0) && (max_tries < 2));
2889 return aggressive ? gc_ok : YAFFS_OK;
2894 * Garbage collects. Intended to be called from a background thread.
2895 * Returns non-zero if at least half the free chunks are erased.
2897 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2899 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2901 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2903 yaffs_check_gc(dev, 1);
2904 return erased_chunks > dev->n_free_chunks / 2;
2907 /*-------------------- Data file manipulation -----------------*/
2909 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2911 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2913 if (nand_chunk >= 0)
2914 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2917 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2918 "Chunk %d not found zero instead",
2920 /* get sane (zero) data if you read a hole */
2921 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2927 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2932 struct yaffs_ext_tags tags;
2933 struct yaffs_block_info *bi;
2939 block = chunk_id / dev->param.chunks_per_block;
2940 page = chunk_id % dev->param.chunks_per_block;
2942 if (!yaffs_check_chunk_bit(dev, block, page))
2943 yaffs_trace(YAFFS_TRACE_VERIFY,
2944 "Deleting invalid chunk %d", chunk_id);
2946 bi = yaffs_get_block_info(dev, block);
2948 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2950 yaffs_trace(YAFFS_TRACE_DELETION,
2951 "line %d delete of chunk %d",
2954 if (!dev->param.is_yaffs2 && mark_flash &&
2955 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2957 memset(&tags, 0, sizeof(tags));
2958 tags.is_deleted = 1;
2959 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2960 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2962 dev->n_unmarked_deletions++;
2965 /* Pull out of the management area.
2966 * If the whole block became dirty, this will kick off an erasure.
2968 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2969 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2970 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
2971 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2972 dev->n_free_chunks++;
2973 yaffs_clear_chunk_bit(dev, block, page);
2976 if (bi->pages_in_use == 0 &&
2977 !bi->has_shrink_hdr &&
2978 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2979 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
2980 yaffs_block_became_dirty(dev, block);
2985 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2986 const u8 *buffer, int n_bytes, int use_reserve)
2988 /* Find old chunk Need to do this to get serial number
2989 * Write new one and patch into tree.
2990 * Invalidate old tags.
2994 struct yaffs_ext_tags prev_tags;
2996 struct yaffs_ext_tags new_tags;
2997 struct yaffs_dev *dev = in->my_dev;
2999 yaffs_check_gc(dev, 0);
3001 /* Get the previous chunk at this location in the file if it exists.
3002 * If it does not exist then put a zero into the tree. This creates
3003 * the tnode now, rather than later when it is harder to clean up.
3005 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3006 if (prev_chunk_id < 1 &&
3007 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3010 /* Set up new tags */
3011 memset(&new_tags, 0, sizeof(new_tags));
3013 new_tags.chunk_id = inode_chunk;
3014 new_tags.obj_id = in->obj_id;
3015 new_tags.serial_number =
3016 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3017 new_tags.n_bytes = n_bytes;
3019 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3020 yaffs_trace(YAFFS_TRACE_ERROR,
3021 "Writing %d bytes to chunk!!!!!!!!!",
3027 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3029 if (new_chunk_id > 0) {
3030 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3032 if (prev_chunk_id > 0)
3033 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3035 yaffs_verify_file_sane(in);
3037 return new_chunk_id;
3043 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3044 const YCHAR *name, const void *value, int size,
3047 struct yaffs_xattr_mod xmod;
3055 xmod.result = -ENOSPC;
3057 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3065 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3066 struct yaffs_xattr_mod *xmod)
3069 int x_offs = sizeof(struct yaffs_obj_hdr);
3070 struct yaffs_dev *dev = obj->my_dev;
3071 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3072 char *x_buffer = buffer + x_offs;
3076 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3077 xmod->size, xmod->flags);
3079 retval = nval_del(x_buffer, x_size, xmod->name);
3081 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3082 obj->xattr_known = 1;
3083 xmod->result = retval;
3088 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
3089 void *value, int size)
3091 char *buffer = NULL;
3093 struct yaffs_ext_tags tags;
3094 struct yaffs_dev *dev = obj->my_dev;
3095 int x_offs = sizeof(struct yaffs_obj_hdr);
3096 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3100 if (obj->hdr_chunk < 1)
3103 /* If we know that the object has no xattribs then don't do all the
3104 * reading and parsing.
3106 if (obj->xattr_known && !obj->has_xattr) {
3113 buffer = (char *)yaffs_get_temp_buffer(dev);
3118 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3120 if (result != YAFFS_OK)
3123 x_buffer = buffer + x_offs;
3125 if (!obj->xattr_known) {
3126 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3127 obj->xattr_known = 1;
3131 retval = nval_get(x_buffer, x_size, name, value, size);
3133 retval = nval_list(x_buffer, x_size, value, size);
3135 yaffs_release_temp_buffer(dev, (u8 *) buffer);
3139 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3140 const void *value, int size, int flags)
3142 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3145 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3147 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3150 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3153 return yaffs_do_xattrib_fetch(obj, name, value, size);
3156 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3158 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3161 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3164 struct yaffs_obj_hdr *oh;
3165 struct yaffs_dev *dev;
3166 struct yaffs_ext_tags tags;
3168 int alloc_failed = 0;
3170 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3174 in->lazy_loaded = 0;
3175 buf = yaffs_get_temp_buffer(dev);
3177 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3178 oh = (struct yaffs_obj_hdr *)buf;
3180 in->yst_mode = oh->yst_mode;
3181 yaffs_load_attribs(in, oh);
3182 yaffs_set_obj_name_from_oh(in, oh);
3184 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3185 in->variant.symlink_variant.alias =
3186 yaffs_clone_str(oh->alias);
3187 if (!in->variant.symlink_variant.alias)
3188 alloc_failed = 1; /* Not returned */
3190 yaffs_release_temp_buffer(dev, buf);
3193 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
3194 const YCHAR *oh_name, int buff_size)
3196 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3197 if (dev->param.auto_unicode) {
3199 /* It is an ASCII name, do an ASCII to
3200 * unicode conversion */
3201 const char *ascii_oh_name = (const char *)oh_name;
3202 int n = buff_size - 1;
3203 while (n > 0 && *ascii_oh_name) {
3204 *name = *ascii_oh_name;
3210 strncpy(name, oh_name + 1, buff_size - 1);
3216 strncpy(name, oh_name, buff_size - 1);
3220 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
3223 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3228 if (dev->param.auto_unicode) {
3233 /* Figure out if the name will fit in ascii character set */
3234 while (is_ascii && *w) {
3241 /* It is an ASCII name, so convert unicode to ascii */
3242 char *ascii_oh_name = (char *)oh_name;
3243 int n = YAFFS_MAX_NAME_LENGTH - 1;
3244 while (n > 0 && *name) {
3245 *ascii_oh_name = *name;
3251 /* Unicode name, so save starting at the second YCHAR */
3253 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
3259 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3263 /* UpdateObjectHeader updates the header on NAND for an object.
3264 * If name is not NULL, then that new name is used.
3266 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3267 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3270 struct yaffs_block_info *bi;
3271 struct yaffs_dev *dev = in->my_dev;
3276 struct yaffs_ext_tags new_tags;
3277 struct yaffs_ext_tags old_tags;
3278 const YCHAR *alias = NULL;
3280 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3281 struct yaffs_obj_hdr *oh = NULL;
3283 strcpy(old_name, _Y("silly old name"));
3285 if (in->fake && in != dev->root_dir && !force && !xmod)
3288 yaffs_check_gc(dev, 0);
3289 yaffs_check_obj_details_loaded(in);
3291 buffer = yaffs_get_temp_buffer(in->my_dev);
3292 oh = (struct yaffs_obj_hdr *)buffer;
3294 prev_chunk_id = in->hdr_chunk;
3296 if (prev_chunk_id > 0) {
3297 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3300 yaffs_verify_oh(in, oh, &old_tags, 0);
3301 memcpy(old_name, oh->name, sizeof(oh->name));
3302 memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
3304 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3307 oh->type = in->variant_type;
3308 oh->yst_mode = in->yst_mode;
3309 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3311 yaffs_load_attribs_oh(oh, in);
3314 oh->parent_obj_id = in->parent->obj_id;
3316 oh->parent_obj_id = 0;
3318 if (name && *name) {
3319 memset(oh->name, 0, sizeof(oh->name));
3320 yaffs_load_oh_from_name(dev, oh->name, name);
3321 } else if (prev_chunk_id > 0) {
3322 memcpy(oh->name, old_name, sizeof(oh->name));
3324 memset(oh->name, 0, sizeof(oh->name));
3327 oh->is_shrink = is_shrink;
3329 switch (in->variant_type) {
3330 case YAFFS_OBJECT_TYPE_UNKNOWN:
3331 /* Should not happen */
3333 case YAFFS_OBJECT_TYPE_FILE:
3335 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED ||
3336 oh->parent_obj_id == YAFFS_OBJECTID_UNLINKED) ?
3337 0 : in->variant.file_variant.file_size;
3339 case YAFFS_OBJECT_TYPE_HARDLINK:
3340 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3342 case YAFFS_OBJECT_TYPE_SPECIAL:
3345 case YAFFS_OBJECT_TYPE_DIRECTORY:
3348 case YAFFS_OBJECT_TYPE_SYMLINK:
3349 alias = in->variant.symlink_variant.alias;
3351 alias = _Y("no alias");
3352 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3353 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3357 /* process any xattrib modifications */
3359 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3362 memset(&new_tags, 0, sizeof(new_tags));
3364 new_tags.chunk_id = 0;
3365 new_tags.obj_id = in->obj_id;
3366 new_tags.serial_number = in->serial;
3368 /* Add extra info for file header */
3369 new_tags.extra_available = 1;
3370 new_tags.extra_parent_id = oh->parent_obj_id;
3371 new_tags.extra_length = oh->file_size;
3372 new_tags.extra_is_shrink = oh->is_shrink;
3373 new_tags.extra_equiv_id = oh->equiv_id;
3374 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3375 new_tags.extra_obj_type = in->variant_type;
3376 yaffs_verify_oh(in, oh, &new_tags, 1);
3378 /* Create new chunk in NAND */
3380 yaffs_write_new_chunk(dev, buffer, &new_tags,
3381 (prev_chunk_id > 0) ? 1 : 0);
3384 yaffs_release_temp_buffer(dev, buffer);
3386 if (new_chunk_id < 0)
3387 return new_chunk_id;
3389 in->hdr_chunk = new_chunk_id;
3391 if (prev_chunk_id > 0)
3392 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3394 if (!yaffs_obj_cache_dirty(in))
3397 /* If this was a shrink, then mark the block
3398 * that the chunk lives on */
3400 bi = yaffs_get_block_info(in->my_dev,
3402 in->my_dev->param.chunks_per_block);
3403 bi->has_shrink_hdr = 1;
3407 return new_chunk_id;
3410 /*--------------------- File read/write ------------------------
3411 * Read and write have very similar structures.
3412 * In general the read/write has three parts to it
3413 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3414 * Some complete chunks
3415 * An incomplete chunk to end off with
3417 * Curve-balls: the first chunk might also be the last chunk.
3420 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)