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 memset(obj->short_name, 0, sizeof(obj->short_name));
676 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
677 YAFFS_SHORT_NAME_LENGTH)
678 strcpy(obj->short_name, name);
680 obj->short_name[0] = _Y('\0');
681 obj->sum = yaffs_calc_name_sum(name);
684 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
685 const struct yaffs_obj_hdr *oh)
687 #ifdef CONFIG_YAFFS_AUTO_UNICODE
688 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
689 memset(tmp_name, 0, sizeof(tmp_name));
690 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
691 YAFFS_MAX_NAME_LENGTH + 1);
692 yaffs_set_obj_name(obj, tmp_name);
694 yaffs_set_obj_name(obj, oh->name);
698 /*-------------------- TNODES -------------------
700 * List of spare tnodes
701 * The list is hooked together using the first pointer
705 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
707 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
710 memset(tn, 0, dev->tnode_size);
714 dev->checkpoint_blocks_required = 0; /* force recalculation */
719 /* FreeTnode frees up a tnode and puts it back on the free list */
720 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
722 yaffs_free_raw_tnode(dev, tn);
724 dev->checkpoint_blocks_required = 0; /* force recalculation */
727 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
729 yaffs_deinit_raw_tnodes_and_objs(dev);
734 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
735 unsigned pos, unsigned val)
737 u32 *map = (u32 *) tn;
743 pos &= YAFFS_TNODES_LEVEL0_MASK;
744 val >>= dev->chunk_grp_bits;
746 bit_in_map = pos * dev->tnode_width;
747 word_in_map = bit_in_map / 32;
748 bit_in_word = bit_in_map & (32 - 1);
750 mask = dev->tnode_mask << bit_in_word;
752 map[word_in_map] &= ~mask;
753 map[word_in_map] |= (mask & (val << bit_in_word));
755 if (dev->tnode_width > (32 - bit_in_word)) {
756 bit_in_word = (32 - bit_in_word);
759 dev->tnode_mask >> bit_in_word;
760 map[word_in_map] &= ~mask;
761 map[word_in_map] |= (mask & (val >> bit_in_word));
765 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
768 u32 *map = (u32 *) tn;
774 pos &= YAFFS_TNODES_LEVEL0_MASK;
776 bit_in_map = pos * dev->tnode_width;
777 word_in_map = bit_in_map / 32;
778 bit_in_word = bit_in_map & (32 - 1);
780 val = map[word_in_map] >> bit_in_word;
782 if (dev->tnode_width > (32 - bit_in_word)) {
783 bit_in_word = (32 - bit_in_word);
785 val |= (map[word_in_map] << bit_in_word);
788 val &= dev->tnode_mask;
789 val <<= dev->chunk_grp_bits;
794 /* ------------------- End of individual tnode manipulation -----------------*/
796 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
797 * The look up tree is represented by the top tnode and the number of top_level
798 * in the tree. 0 means only the level 0 tnode is in the tree.
801 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
802 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
803 struct yaffs_file_var *file_struct,
806 struct yaffs_tnode *tn = file_struct->top;
809 int level = file_struct->top_level;
813 /* Check sane level and chunk Id */
814 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
817 if (chunk_id > YAFFS_MAX_CHUNK_ID)
820 /* First check we're tall enough (ie enough top_level) */
822 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
825 i >>= YAFFS_TNODES_INTERNAL_BITS;
829 if (required_depth > file_struct->top_level)
830 return NULL; /* Not tall enough, so we can't find it */
832 /* Traverse down to level 0 */
833 while (level > 0 && tn) {
834 tn = tn->internal[(chunk_id >>
835 (YAFFS_TNODES_LEVEL0_BITS +
837 YAFFS_TNODES_INTERNAL_BITS)) &
838 YAFFS_TNODES_INTERNAL_MASK];
845 /* add_find_tnode_0 finds the level 0 tnode if it exists,
846 * otherwise first expands the tree.
847 * This happens in two steps:
848 * 1. If the tree isn't tall enough, then make it taller.
849 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
851 * Used when modifying the tree.
853 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
854 * specified tn will be plugged into the ttree.
857 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
858 struct yaffs_file_var *file_struct,
860 struct yaffs_tnode *passed_tn)
865 struct yaffs_tnode *tn;
868 /* Check sane level and page Id */
869 if (file_struct->top_level < 0 ||
870 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
873 if (chunk_id > YAFFS_MAX_CHUNK_ID)
876 /* First check we're tall enough (ie enough top_level) */
878 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
881 x >>= YAFFS_TNODES_INTERNAL_BITS;
885 if (required_depth > file_struct->top_level) {
886 /* Not tall enough, gotta make the tree taller */
887 for (i = file_struct->top_level; i < required_depth; i++) {
889 tn = yaffs_get_tnode(dev);
892 tn->internal[0] = file_struct->top;
893 file_struct->top = tn;
894 file_struct->top_level++;
896 yaffs_trace(YAFFS_TRACE_ERROR,
897 "yaffs: no more tnodes");
903 /* Traverse down to level 0, adding anything we need */
905 l = file_struct->top_level;
906 tn = file_struct->top;
909 while (l > 0 && tn) {
911 (YAFFS_TNODES_LEVEL0_BITS +
912 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
913 YAFFS_TNODES_INTERNAL_MASK;
915 if ((l > 1) && !tn->internal[x]) {
916 /* Add missing non-level-zero tnode */
917 tn->internal[x] = yaffs_get_tnode(dev);
918 if (!tn->internal[x])
921 /* Looking from level 1 at level 0 */
923 /* If we already have one, release it */
925 yaffs_free_tnode(dev,
927 tn->internal[x] = passed_tn;
929 } else if (!tn->internal[x]) {
930 /* Don't have one, none passed in */
931 tn->internal[x] = yaffs_get_tnode(dev);
932 if (!tn->internal[x])
937 tn = tn->internal[x];
943 memcpy(tn, passed_tn,
944 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
945 yaffs_free_tnode(dev, passed_tn);
952 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
955 return (tags->chunk_id == chunk_obj &&
956 tags->obj_id == obj_id &&
957 !tags->is_deleted) ? 1 : 0;
961 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
962 struct yaffs_ext_tags *tags, int obj_id,
967 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
968 if (yaffs_check_chunk_bit
969 (dev, the_chunk / dev->param.chunks_per_block,
970 the_chunk % dev->param.chunks_per_block)) {
972 if (dev->chunk_grp_size == 1)
975 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
977 if (yaffs_tags_match(tags,
978 obj_id, inode_chunk)) {
989 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
990 struct yaffs_ext_tags *tags)
992 /*Get the Tnode, then get the level 0 offset chunk offset */
993 struct yaffs_tnode *tn;
995 struct yaffs_ext_tags local_tags;
997 struct yaffs_dev *dev = in->my_dev;
1000 /* Passed a NULL, so use our own tags space */
1004 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1009 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1011 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1016 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1017 struct yaffs_ext_tags *tags)
1019 /* Get the Tnode, then get the level 0 offset chunk offset */
1020 struct yaffs_tnode *tn;
1022 struct yaffs_ext_tags local_tags;
1023 struct yaffs_dev *dev = in->my_dev;
1027 /* Passed a NULL, so use our own tags space */
1031 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1036 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1038 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1041 /* Delete the entry in the filestructure (if found) */
1043 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1048 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1049 int nand_chunk, int in_scan)
1051 /* NB in_scan is zero unless scanning.
1052 * For forward scanning, in_scan is > 0;
1053 * for backward scanning in_scan is < 0
1055 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1058 struct yaffs_tnode *tn;
1059 struct yaffs_dev *dev = in->my_dev;
1061 struct yaffs_ext_tags existing_tags;
1062 struct yaffs_ext_tags new_tags;
1063 unsigned existing_serial, new_serial;
1065 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1066 /* Just ignore an attempt at putting a chunk into a non-file
1068 * If it is not during Scanning then something went wrong!
1071 yaffs_trace(YAFFS_TRACE_ERROR,
1072 "yaffs tragedy:attempt to put data chunk into a non-file"
1077 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1081 tn = yaffs_add_find_tnode_0(dev,
1082 &in->variant.file_variant,
1088 /* Dummy insert, bail now */
1091 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1094 /* If we're scanning then we need to test for duplicates
1095 * NB This does not need to be efficient since it should only
1096 * happen when the power fails during a write, then only one
1097 * chunk should ever be affected.
1099 * Correction for YAFFS2: This could happen quite a lot and we
1100 * need to think about efficiency! TODO
1101 * Update: For backward scanning we don't need to re-read tags
1102 * so this is quite cheap.
1105 if (existing_cunk > 0) {
1106 /* NB Right now existing chunk will not be real
1107 * chunk_id if the chunk group size > 1
1108 * thus we have to do a FindChunkInFile to get the
1111 * We have a duplicate now we need to decide which
1114 * Backwards scanning YAFFS2: The old one is what
1115 * we use, dump the new one.
1116 * YAFFS1: Get both sets of tags and compare serial
1121 /* Only do this for forward scanning */
1122 yaffs_rd_chunk_tags_nand(dev,
1126 /* Do a proper find */
1128 yaffs_find_chunk_in_file(in, inode_chunk,
1132 if (existing_cunk <= 0) {
1133 /*Hoosterman - how did this happen? */
1135 yaffs_trace(YAFFS_TRACE_ERROR,
1136 "yaffs tragedy: existing chunk < 0 in scan"
1141 /* NB The deleted flags should be false, otherwise
1142 * the chunks will not be loaded during a scan
1146 new_serial = new_tags.serial_number;
1147 existing_serial = existing_tags.serial_number;
1150 if ((in_scan > 0) &&
1151 (existing_cunk <= 0 ||
1152 ((existing_serial + 1) & 3) == new_serial)) {
1153 /* Forward scanning.
1155 * Delete the old one and drop through to
1158 yaffs_chunk_del(dev, existing_cunk, 1,
1161 /* Backward scanning or we want to use the
1163 * Delete the new one and return early so that
1164 * the tnode isn't changed
1166 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1173 if (existing_cunk == 0)
1174 in->n_data_chunks++;
1176 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1181 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1183 struct yaffs_block_info *the_block;
1186 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1188 block_no = chunk / dev->param.chunks_per_block;
1189 the_block = yaffs_get_block_info(dev, block_no);
1191 the_block->soft_del_pages++;
1192 dev->n_free_chunks++;
1193 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1197 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1198 * the chunks in the file.
1199 * All soft deleting does is increment the block's softdelete count and pulls
1200 * the chunk out of the tnode.
1201 * Thus, essentially this is the same as DeleteWorker except that the chunks
1205 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1206 u32 level, int chunk_offset)
1211 struct yaffs_dev *dev = in->my_dev;
1217 for (i = YAFFS_NTNODES_INTERNAL - 1;
1220 if (tn->internal[i]) {
1222 yaffs_soft_del_worker(in,
1226 YAFFS_TNODES_INTERNAL_BITS)
1229 yaffs_free_tnode(dev,
1231 tn->internal[i] = NULL;
1233 /* Can this happen? */
1237 return (all_done) ? 1 : 0;
1241 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1242 the_chunk = yaffs_get_group_base(dev, tn, i);
1244 yaffs_soft_del_chunk(dev, the_chunk);
1245 yaffs_load_tnode_0(dev, tn, i, 0);
1251 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1253 struct yaffs_dev *dev = obj->my_dev;
1254 struct yaffs_obj *parent;
1256 yaffs_verify_obj_in_dir(obj);
1257 parent = obj->parent;
1259 yaffs_verify_dir(parent);
1261 if (dev && dev->param.remove_obj_fn)
1262 dev->param.remove_obj_fn(obj);
1264 list_del_init(&obj->siblings);
1267 yaffs_verify_dir(parent);
1270 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1273 yaffs_trace(YAFFS_TRACE_ALWAYS,
1274 "tragedy: Trying to add an object to a null pointer directory"
1279 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1280 yaffs_trace(YAFFS_TRACE_ALWAYS,
1281 "tragedy: Trying to add an object to a non-directory"
1286 if (obj->siblings.prev == NULL) {
1287 /* Not initialised */
1291 yaffs_verify_dir(directory);
1293 yaffs_remove_obj_from_dir(obj);
1296 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1297 obj->parent = directory;
1299 if (directory == obj->my_dev->unlinked_dir
1300 || directory == obj->my_dev->del_dir) {
1302 obj->my_dev->n_unlinked_files++;
1303 obj->rename_allowed = 0;
1306 yaffs_verify_dir(directory);
1307 yaffs_verify_obj_in_dir(obj);
1310 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1311 struct yaffs_obj *new_dir,
1312 const YCHAR *new_name, int force, int shadows)
1316 struct yaffs_obj *existing_target;
1318 if (new_dir == NULL)
1319 new_dir = obj->parent; /* use the old directory */
1321 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1322 yaffs_trace(YAFFS_TRACE_ALWAYS,
1323 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1328 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1329 del_op = (new_dir == obj->my_dev->del_dir);
1331 existing_target = yaffs_find_by_name(new_dir, new_name);
1333 /* If the object is a file going into the unlinked directory,
1334 * then it is OK to just stuff it in since duplicate names are OK.
1335 * else only proceed if the new name does not exist and we're putting
1336 * it into a directory.
1338 if (!(unlink_op || del_op || force ||
1339 shadows > 0 || !existing_target) ||
1340 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1343 yaffs_set_obj_name(obj, new_name);
1345 yaffs_add_obj_to_dir(new_dir, obj);
1350 /* If it is a deletion then we mark it as a shrink for gc */
1351 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1357 /*------------------------ Short Operations Cache ------------------------------
1358 * In many situations where there is no high level buffering a lot of
1359 * reads might be short sequential reads, and a lot of writes may be short
1360 * sequential writes. eg. scanning/writing a jpeg file.
1361 * In these cases, a short read/write cache can provide a huge perfomance
1362 * benefit with dumb-as-a-rock code.
1363 * In Linux, the page cache provides read buffering and the short op cache
1364 * provides write buffering.
1366 * There are a small number (~10) of cache chunks per device so that we don't
1367 * need a very intelligent search.
1370 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1372 struct yaffs_dev *dev = obj->my_dev;
1374 struct yaffs_cache *cache;
1375 int n_caches = obj->my_dev->param.n_caches;
1377 for (i = 0; i < n_caches; i++) {
1378 cache = &dev->cache[i];
1379 if (cache->object == obj && cache->dirty)
1386 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1388 struct yaffs_dev *dev = obj->my_dev;
1389 int lowest = -99; /* Stop compiler whining. */
1391 struct yaffs_cache *cache;
1392 int chunk_written = 0;
1393 int n_caches = obj->my_dev->param.n_caches;
1400 /* Find the lowest dirty chunk for this object */
1401 for (i = 0; i < n_caches; i++) {
1402 if (dev->cache[i].object == obj &&
1403 dev->cache[i].dirty) {
1405 dev->cache[i].chunk_id < lowest) {
1406 cache = &dev->cache[i];
1407 lowest = cache->chunk_id;
1412 if (cache && !cache->locked) {
1413 /* Write it out and free it up */
1415 yaffs_wr_data_obj(cache->object,
1420 cache->object = NULL;
1422 } while (cache && chunk_written > 0);
1425 /* Hoosterman, disk full while writing cache out. */
1426 yaffs_trace(YAFFS_TRACE_ERROR,
1427 "yaffs tragedy: no space during cache write");
1430 /*yaffs_flush_whole_cache(dev)
1435 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1437 struct yaffs_obj *obj;
1438 int n_caches = dev->param.n_caches;
1441 /* Find a dirty object in the cache and flush it...
1442 * until there are no further dirty objects.
1446 for (i = 0; i < n_caches && !obj; i++) {
1447 if (dev->cache[i].object && dev->cache[i].dirty)
1448 obj = dev->cache[i].object;
1451 yaffs_flush_file_cache(obj);
1456 /* Grab us a cache chunk for use.
1457 * First look for an empty one.
1458 * Then look for the least recently used non-dirty one.
1459 * Then look for the least recently used dirty one...., flush and look again.
1461 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1465 if (dev->param.n_caches > 0) {
1466 for (i = 0; i < dev->param.n_caches; i++) {
1467 if (!dev->cache[i].object)
1468 return &dev->cache[i];
1474 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1476 struct yaffs_cache *cache;
1477 struct yaffs_obj *the_obj;
1482 if (dev->param.n_caches < 1)
1485 /* Try find a non-dirty one... */
1487 cache = yaffs_grab_chunk_worker(dev);
1490 /* They were all dirty, find the LRU object and flush
1491 * its cache, then find again.
1492 * NB what's here is not very accurate,
1493 * we actually flush the object with the LRU chunk.
1496 /* With locking we can't assume we can use entry zero,
1497 * Set the_obj to a valid pointer for Coverity. */
1498 the_obj = dev->cache[0].object;
1503 for (i = 0; i < dev->param.n_caches; i++) {
1504 if (dev->cache[i].object &&
1505 !dev->cache[i].locked &&
1506 (dev->cache[i].last_use < usage ||
1508 usage = dev->cache[i].last_use;
1509 the_obj = dev->cache[i].object;
1510 cache = &dev->cache[i];
1515 if (!cache || cache->dirty) {
1516 /* Flush and try again */
1517 yaffs_flush_file_cache(the_obj);
1518 cache = yaffs_grab_chunk_worker(dev);
1524 /* Find a cached chunk */
1525 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1528 struct yaffs_dev *dev = obj->my_dev;
1531 if (dev->param.n_caches < 1)
1534 for (i = 0; i < dev->param.n_caches; i++) {
1535 if (dev->cache[i].object == obj &&
1536 dev->cache[i].chunk_id == chunk_id) {
1539 return &dev->cache[i];
1545 /* Mark the chunk for the least recently used algorithym */
1546 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1551 if (dev->param.n_caches < 1)
1554 if (dev->cache_last_use < 0 ||
1555 dev->cache_last_use > 100000000) {
1556 /* Reset the cache usages */
1557 for (i = 1; i < dev->param.n_caches; i++)
1558 dev->cache[i].last_use = 0;
1560 dev->cache_last_use = 0;
1562 dev->cache_last_use++;
1563 cache->last_use = dev->cache_last_use;
1569 /* Invalidate a single cache page.
1570 * Do this when a whole page gets written,
1571 * ie the short cache for this page is no longer valid.
1573 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1575 struct yaffs_cache *cache;
1577 if (object->my_dev->param.n_caches > 0) {
1578 cache = yaffs_find_chunk_cache(object, chunk_id);
1581 cache->object = NULL;
1585 /* Invalidate all the cache pages associated with this object
1586 * Do this whenever ther file is deleted or resized.
1588 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1591 struct yaffs_dev *dev = in->my_dev;
1593 if (dev->param.n_caches > 0) {
1594 /* Invalidate it. */
1595 for (i = 0; i < dev->param.n_caches; i++) {
1596 if (dev->cache[i].object == in)
1597 dev->cache[i].object = NULL;
1602 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1605 struct yaffs_dev *dev = obj->my_dev;
1607 /* If it is still linked into the bucket list, free from the list */
1608 if (!list_empty(&obj->hash_link)) {
1609 list_del_init(&obj->hash_link);
1610 bucket = yaffs_hash_fn(obj->obj_id);
1611 dev->obj_bucket[bucket].count--;
1615 /* FreeObject frees up a Object and puts it back on the free list */
1616 static void yaffs_free_obj(struct yaffs_obj *obj)
1618 struct yaffs_dev *dev;
1625 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1626 obj, obj->my_inode);
1629 if (!list_empty(&obj->siblings))
1632 if (obj->my_inode) {
1633 /* We're still hooked up to a cached inode.
1634 * Don't delete now, but mark for later deletion
1636 obj->defered_free = 1;
1640 yaffs_unhash_obj(obj);
1642 yaffs_free_raw_obj(dev, obj);
1644 dev->checkpoint_blocks_required = 0; /* force recalculation */
1647 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1649 if (obj->defered_free)
1650 yaffs_free_obj(obj);
1653 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1655 /* Iinvalidate the file's data in the cache, without flushing. */
1656 yaffs_invalidate_whole_cache(in);
1658 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1659 /* Move to unlinked directory so we have a deletion record */
1660 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1664 yaffs_remove_obj_from_dir(in);
1665 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1673 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1675 if (!obj->deleted ||
1676 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1680 if (obj->n_data_chunks <= 0) {
1681 /* Empty file with no duplicate object headers,
1682 * just delete it immediately */
1683 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1684 obj->variant.file_variant.top = NULL;
1685 yaffs_trace(YAFFS_TRACE_TRACING,
1686 "yaffs: Deleting empty file %d",
1688 yaffs_generic_obj_del(obj);
1690 yaffs_soft_del_worker(obj,
1691 obj->variant.file_variant.top,
1693 file_variant.top_level, 0);
1698 /* Pruning removes any part of the file structure tree that is beyond the
1699 * bounds of the file (ie that does not point to chunks).
1701 * A file should only get pruned when its size is reduced.
1703 * Before pruning, the chunks must be pulled from the tree and the
1704 * level 0 tnode entries must be zeroed out.
1705 * Could also use this for file deletion, but that's probably better handled
1706 * by a special case.
1708 * This function is recursive. For levels > 0 the function is called again on
1709 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1710 * If there is no data in a subtree then it is pruned.
1713 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1714 struct yaffs_tnode *tn, u32 level,
1726 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1727 if (tn->internal[i]) {
1729 yaffs_prune_worker(dev,
1732 (i == 0) ? del0 : 1);
1735 if (tn->internal[i])
1739 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1740 u32 *map = (u32 *) tn;
1742 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1748 if (has_data == 0 && del0) {
1749 /* Free and return NULL */
1750 yaffs_free_tnode(dev, tn);
1756 static int yaffs_prune_tree(struct yaffs_dev *dev,
1757 struct yaffs_file_var *file_struct)
1762 struct yaffs_tnode *tn;
1764 if (file_struct->top_level < 1)
1768 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1770 /* Now we have a tree with all the non-zero branches NULL but
1771 * the height is the same as it was.
1772 * Let's see if we can trim internal tnodes to shorten the tree.
1773 * We can do this if only the 0th element in the tnode is in use
1774 * (ie all the non-zero are NULL)
1777 while (file_struct->top_level && !done) {
1778 tn = file_struct->top;
1781 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1782 if (tn->internal[i])
1787 file_struct->top = tn->internal[0];
1788 file_struct->top_level--;
1789 yaffs_free_tnode(dev, tn);
1798 /*-------------------- End of File Structure functions.-------------------*/
1800 /* alloc_empty_obj gets us a clean Object.*/
1801 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1803 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1810 /* Now sweeten it up... */
1812 memset(obj, 0, sizeof(struct yaffs_obj));
1813 obj->being_created = 1;
1817 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1818 INIT_LIST_HEAD(&(obj->hard_links));
1819 INIT_LIST_HEAD(&(obj->hash_link));
1820 INIT_LIST_HEAD(&obj->siblings);
1822 /* Now make the directory sane */
1823 if (dev->root_dir) {
1824 obj->parent = dev->root_dir;
1825 list_add(&(obj->siblings),
1826 &dev->root_dir->variant.dir_variant.children);
1829 /* Add it to the lost and found directory.
1830 * NB Can't put root or lost-n-found in lost-n-found so
1831 * check if lost-n-found exists first
1833 if (dev->lost_n_found)
1834 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1836 obj->being_created = 0;
1838 dev->checkpoint_blocks_required = 0; /* force recalculation */
1843 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1847 int lowest = 999999;
1849 /* Search for the shortest list or one that
1853 for (i = 0; i < 10 && lowest > 4; i++) {
1854 dev->bucket_finder++;
1855 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1856 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1857 lowest = dev->obj_bucket[dev->bucket_finder].count;
1858 l = dev->bucket_finder;
1865 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1867 int bucket = yaffs_find_nice_bucket(dev);
1869 struct list_head *i;
1870 u32 n = (u32) bucket;
1872 /* Now find an object value that has not already been taken
1873 * by scanning the list.
1878 n += YAFFS_NOBJECT_BUCKETS;
1879 if (1 || dev->obj_bucket[bucket].count > 0) {
1880 list_for_each(i, &dev->obj_bucket[bucket].list) {
1881 /* If there is already one in the list */
1882 if (i && list_entry(i, struct yaffs_obj,
1883 hash_link)->obj_id == n) {
1892 static void yaffs_hash_obj(struct yaffs_obj *in)
1894 int bucket = yaffs_hash_fn(in->obj_id);
1895 struct yaffs_dev *dev = in->my_dev;
1897 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1898 dev->obj_bucket[bucket].count++;
1901 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1903 int bucket = yaffs_hash_fn(number);
1904 struct list_head *i;
1905 struct yaffs_obj *in;
1907 list_for_each(i, &dev->obj_bucket[bucket].list) {
1908 /* Look if it is in the list */
1909 in = list_entry(i, struct yaffs_obj, hash_link);
1910 if (in->obj_id == number) {
1911 /* Don't show if it is defered free */
1912 if (in->defered_free)
1921 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1922 enum yaffs_obj_type type)
1924 struct yaffs_obj *the_obj = NULL;
1925 struct yaffs_tnode *tn = NULL;
1928 number = yaffs_new_obj_id(dev);
1930 if (type == YAFFS_OBJECT_TYPE_FILE) {
1931 tn = yaffs_get_tnode(dev);
1936 the_obj = yaffs_alloc_empty_obj(dev);
1939 yaffs_free_tnode(dev, tn);
1944 the_obj->rename_allowed = 1;
1945 the_obj->unlink_allowed = 1;
1946 the_obj->obj_id = number;
1947 yaffs_hash_obj(the_obj);
1948 the_obj->variant_type = type;
1949 yaffs_load_current_time(the_obj, 1, 1);
1952 case YAFFS_OBJECT_TYPE_FILE:
1953 the_obj->variant.file_variant.file_size = 0;
1954 the_obj->variant.file_variant.scanned_size = 0;
1955 the_obj->variant.file_variant.shrink_size = ~0; /* max */
1956 the_obj->variant.file_variant.top_level = 0;
1957 the_obj->variant.file_variant.top = tn;
1959 case YAFFS_OBJECT_TYPE_DIRECTORY:
1960 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1961 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1963 case YAFFS_OBJECT_TYPE_SYMLINK:
1964 case YAFFS_OBJECT_TYPE_HARDLINK:
1965 case YAFFS_OBJECT_TYPE_SPECIAL:
1966 /* No action required */
1968 case YAFFS_OBJECT_TYPE_UNKNOWN:
1969 /* todo this should not happen */
1975 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1976 int number, u32 mode)
1979 struct yaffs_obj *obj =
1980 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1985 obj->fake = 1; /* it is fake so it might not use NAND */
1986 obj->rename_allowed = 0;
1987 obj->unlink_allowed = 0;
1990 obj->yst_mode = mode;
1992 obj->hdr_chunk = 0; /* Not a valid chunk. */
1998 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2004 yaffs_init_raw_tnodes_and_objs(dev);
2006 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2007 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2008 dev->obj_bucket[i].count = 0;
2012 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2014 enum yaffs_obj_type type)
2016 struct yaffs_obj *the_obj = NULL;
2019 the_obj = yaffs_find_by_number(dev, number);
2022 the_obj = yaffs_new_obj(dev, number, type);
2028 YCHAR *yaffs_clone_str(const YCHAR *str)
2030 YCHAR *new_str = NULL;
2036 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2037 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2039 strncpy(new_str, str, len);
2046 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2047 * link (ie. name) is created or deleted in the directory.
2050 * create dir/a : update dir's mtime/ctime
2051 * rm dir/a: update dir's mtime/ctime
2052 * modify dir/a: don't update dir's mtimme/ctime
2054 * This can be handled immediately or defered. Defering helps reduce the number
2055 * of updates when many files in a directory are changed within a brief period.
2057 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2058 * called periodically.
2061 static void yaffs_update_parent(struct yaffs_obj *obj)
2063 struct yaffs_dev *dev;
2069 yaffs_load_current_time(obj, 0, 1);
2070 if (dev->param.defered_dir_update) {
2071 struct list_head *link = &obj->variant.dir_variant.dirty;
2073 if (list_empty(link)) {
2074 list_add(link, &dev->dirty_dirs);
2075 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2076 "Added object %d to dirty directories",
2081 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2085 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2087 struct list_head *link;
2088 struct yaffs_obj *obj;
2089 struct yaffs_dir_var *d_s;
2090 union yaffs_obj_var *o_v;
2092 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2094 while (!list_empty(&dev->dirty_dirs)) {
2095 link = dev->dirty_dirs.next;
2096 list_del_init(link);
2098 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2099 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2100 obj = list_entry(o_v, struct yaffs_obj, variant);
2102 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2106 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2111 * Mknod (create) a new object.
2112 * equiv_obj only has meaning for a hard link;
2113 * alias_str only has meaning for a symlink.
2114 * rdev only has meaning for devices (a subset of special objects)
2117 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2118 struct yaffs_obj *parent,
2123 struct yaffs_obj *equiv_obj,
2124 const YCHAR *alias_str, u32 rdev)
2126 struct yaffs_obj *in;
2128 struct yaffs_dev *dev = parent->my_dev;
2130 /* Check if the entry exists.
2131 * If it does then fail the call since we don't want a dup. */
2132 if (yaffs_find_by_name(parent, name))
2135 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2136 str = yaffs_clone_str(alias_str);
2141 in = yaffs_new_obj(dev, -1, type);
2150 in->variant_type = type;
2152 in->yst_mode = mode;
2154 yaffs_attribs_init(in, gid, uid, rdev);
2156 in->n_data_chunks = 0;
2158 yaffs_set_obj_name(in, name);
2161 yaffs_add_obj_to_dir(parent, in);
2163 in->my_dev = parent->my_dev;
2166 case YAFFS_OBJECT_TYPE_SYMLINK:
2167 in->variant.symlink_variant.alias = str;
2169 case YAFFS_OBJECT_TYPE_HARDLINK:
2170 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2171 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
2172 list_add(&in->hard_links, &equiv_obj->hard_links);
2174 case YAFFS_OBJECT_TYPE_FILE:
2175 case YAFFS_OBJECT_TYPE_DIRECTORY:
2176 case YAFFS_OBJECT_TYPE_SPECIAL:
2177 case YAFFS_OBJECT_TYPE_UNKNOWN:
2182 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2183 /* Could not create the object header, fail */
2189 yaffs_update_parent(parent);
2194 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2195 const YCHAR *name, u32 mode, u32 uid,
2198 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2199 uid, gid, NULL, NULL, 0);
2202 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2203 u32 mode, u32 uid, u32 gid)
2205 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2206 mode, uid, gid, NULL, NULL, 0);
2209 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2210 const YCHAR *name, u32 mode, u32 uid,
2213 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2214 uid, gid, NULL, NULL, rdev);
2217 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2218 const YCHAR *name, u32 mode, u32 uid,
2219 u32 gid, const YCHAR *alias)
2221 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2222 uid, gid, NULL, alias, 0);
2225 /* yaffs_link_obj returns the object id of the equivalent object.*/
2226 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2227 struct yaffs_obj *equiv_obj)
2229 /* Get the real object in case we were fed a hard link obj */
2230 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2232 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2233 parent, name, 0, 0, 0,
2234 equiv_obj, NULL, 0))
2243 /*---------------------- Block Management and Page Allocation -------------*/
2245 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2247 if (dev->block_info_alt && dev->block_info)
2248 vfree(dev->block_info);
2250 kfree(dev->block_info);
2252 dev->block_info_alt = 0;
2254 dev->block_info = NULL;
2256 if (dev->chunk_bits_alt && dev->chunk_bits)
2257 vfree(dev->chunk_bits);
2259 kfree(dev->chunk_bits);
2260 dev->chunk_bits_alt = 0;
2261 dev->chunk_bits = NULL;
2264 static int yaffs_init_blocks(struct yaffs_dev *dev)
2266 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2268 dev->block_info = NULL;
2269 dev->chunk_bits = NULL;
2270 dev->alloc_block = -1; /* force it to get a new one */
2272 /* If the first allocation strategy fails, thry the alternate one */
2274 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2275 if (!dev->block_info) {
2277 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2278 dev->block_info_alt = 1;
2280 dev->block_info_alt = 0;
2283 if (!dev->block_info)
2286 /* Set up dynamic blockinfo stuff. Round up bytes. */
2287 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2289 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2290 if (!dev->chunk_bits) {
2292 vmalloc(dev->chunk_bit_stride * n_blocks);
2293 dev->chunk_bits_alt = 1;
2295 dev->chunk_bits_alt = 0;
2297 if (!dev->chunk_bits)
2301 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2302 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2306 yaffs_deinit_blocks(dev);
2311 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2313 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2317 /* If the block is still healthy erase it and mark as clean.
2318 * If the block has had a data failure, then retire it.
2321 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2322 "yaffs_block_became_dirty block %d state %d %s",
2323 block_no, bi->block_state,
2324 (bi->needs_retiring) ? "needs retiring" : "");
2326 yaffs2_clear_oldest_dirty_seq(dev, bi);
2328 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2330 /* If this is the block being garbage collected then stop gc'ing */
2331 if (block_no == dev->gc_block)
2334 /* If this block is currently the best candidate for gc
2335 * then drop as a candidate */
2336 if (block_no == dev->gc_dirtiest) {
2337 dev->gc_dirtiest = 0;
2338 dev->gc_pages_in_use = 0;
2341 if (!bi->needs_retiring) {
2342 yaffs2_checkpt_invalidate(dev);
2343 erased_ok = yaffs_erase_block(dev, block_no);
2345 dev->n_erase_failures++;
2346 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2347 "**>> Erasure failed %d", block_no);
2351 /* Verify erasure if needed */
2353 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2354 !yaffs_skip_verification(dev))) {
2355 for (i = 0; i < dev->param.chunks_per_block; i++) {
2356 if (!yaffs_check_chunk_erased(dev,
2357 block_no * dev->param.chunks_per_block + i)) {
2358 yaffs_trace(YAFFS_TRACE_ERROR,
2359 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2366 /* We lost a block of free space */
2367 dev->n_free_chunks -= dev->param.chunks_per_block;
2368 yaffs_retire_block(dev, block_no);
2369 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2370 "**>> Block %d retired", block_no);
2374 /* Clean it up... */
2375 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2377 dev->n_erased_blocks++;
2378 bi->pages_in_use = 0;
2379 bi->soft_del_pages = 0;
2380 bi->has_shrink_hdr = 0;
2381 bi->skip_erased_check = 1; /* Clean, so no need to check */
2382 bi->gc_prioritise = 0;
2383 yaffs_clear_chunk_bits(dev, block_no);
2385 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2388 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2389 struct yaffs_block_info *bi,
2390 int old_chunk, u8 *buffer)
2394 struct yaffs_ext_tags tags;
2395 struct yaffs_obj *object;
2397 int ret_val = YAFFS_OK;
2399 memset(&tags, 0, sizeof(tags));
2400 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2402 object = yaffs_find_by_number(dev, tags.obj_id);
2404 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2405 "Collecting chunk in block %d, %d %d %d ",
2406 dev->gc_chunk, tags.obj_id,
2407 tags.chunk_id, tags.n_bytes);
2409 if (object && !yaffs_skip_verification(dev)) {
2410 if (tags.chunk_id == 0)
2413 else if (object->soft_del)
2414 /* Defeat the test */
2415 matching_chunk = old_chunk;
2418 yaffs_find_chunk_in_file
2419 (object, tags.chunk_id,
2422 if (old_chunk != matching_chunk)
2423 yaffs_trace(YAFFS_TRACE_ERROR,
2424 "gc: page in gc mismatch: %d %d %d %d",
2432 yaffs_trace(YAFFS_TRACE_ERROR,
2433 "page %d in gc has no object: %d %d %d ",
2435 tags.obj_id, tags.chunk_id,
2441 object->soft_del && tags.chunk_id != 0) {
2442 /* Data chunk in a soft deleted file,
2444 * It's a soft deleted data chunk,
2445 * No need to copy this, just forget
2446 * about it and fix up the object.
2449 /* Free chunks already includes
2450 * softdeleted chunks, how ever this
2451 * chunk is going to soon be really
2452 * deleted which will increment free
2453 * chunks. We have to decrement free
2454 * chunks so this works out properly.
2456 dev->n_free_chunks--;
2457 bi->soft_del_pages--;
2459 object->n_data_chunks--;
2460 if (object->n_data_chunks <= 0) {
2461 /* remeber to clean up obj */
2462 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2466 } else if (object) {
2467 /* It's either a data chunk in a live
2468 * file or an ObjectHeader, so we're
2470 * NB Need to keep the ObjectHeaders of
2471 * deleted files until the whole file
2472 * has been deleted off
2474 tags.serial_number++;
2477 if (tags.chunk_id == 0) {
2478 /* It is an object Id,
2479 * We need to nuke the
2480 * shrinkheader flags since its
2482 * Also need to clean up
2485 struct yaffs_obj_hdr *oh;
2486 oh = (struct yaffs_obj_hdr *) buffer;
2489 tags.extra_is_shrink = 0;
2490 oh->shadows_obj = 0;
2491 oh->inband_shadowed_obj_id = 0;
2492 tags.extra_shadows = 0;
2494 /* Update file size */
2495 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2497 object->variant.file_variant.file_size;
2498 tags.extra_length = oh->file_size;
2501 yaffs_verify_oh(object, oh, &tags, 1);
2503 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2506 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2509 if (new_chunk < 0) {
2510 ret_val = YAFFS_FAIL;
2513 /* Now fix up the Tnodes etc. */
2515 if (tags.chunk_id == 0) {
2517 object->hdr_chunk = new_chunk;
2518 object->serial = tags.serial_number;
2520 /* It's a data chunk */
2521 yaffs_put_chunk_in_file(object, tags.chunk_id,
2526 if (ret_val == YAFFS_OK)
2527 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2531 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2534 int ret_val = YAFFS_OK;
2536 int is_checkpt_block;
2538 int chunks_before = yaffs_get_erased_chunks(dev);
2540 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2542 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2544 yaffs_trace(YAFFS_TRACE_TRACING,
2545 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2546 block, bi->pages_in_use, bi->has_shrink_hdr,
2549 /*yaffs_verify_free_chunks(dev); */
2551 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2552 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2554 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2556 dev->gc_disable = 1;
2558 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2559 yaffs_trace(YAFFS_TRACE_TRACING,
2560 "Collecting block %d that has no chunks in use",
2562 yaffs_block_became_dirty(dev, block);
2565 u8 *buffer = yaffs_get_temp_buffer(dev);
2567 yaffs_verify_blk(dev, bi, block);
2569 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2570 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2572 for (/* init already done */ ;
2573 ret_val == YAFFS_OK &&
2574 dev->gc_chunk < dev->param.chunks_per_block &&
2575 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2577 dev->gc_chunk++, old_chunk++) {
2578 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2579 /* Page is in use and might need to be copied */
2581 ret_val = yaffs_gc_process_chunk(dev, bi,
2585 yaffs_release_temp_buffer(dev, buffer);
2588 yaffs_verify_collected_blk(dev, bi, block);
2590 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2592 * The gc did not complete. Set block state back to FULL
2593 * because checkpointing does not restore gc.
2595 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2597 /* The gc completed. */
2598 /* Do any required cleanups */
2599 for (i = 0; i < dev->n_clean_ups; i++) {
2600 /* Time to delete the file too */
2601 struct yaffs_obj *object =
2602 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2604 yaffs_free_tnode(dev,
2605 object->variant.file_variant.top);
2606 object->variant.file_variant.top = NULL;
2607 yaffs_trace(YAFFS_TRACE_GC,
2608 "yaffs: About to finally delete object %d",
2610 yaffs_generic_obj_del(object);
2611 object->my_dev->n_deleted_files--;
2615 chunks_after = yaffs_get_erased_chunks(dev);
2616 if (chunks_before >= chunks_after)
2617 yaffs_trace(YAFFS_TRACE_GC,
2618 "gc did not increase free chunks before %d after %d",
2619 chunks_before, chunks_after);
2622 dev->n_clean_ups = 0;
2625 dev->gc_disable = 0;
2631 * find_gc_block() selects the dirtiest block (or close enough)
2632 * for garbage collection.
2635 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2636 int aggressive, int background)
2640 unsigned selected = 0;
2641 int prioritised = 0;
2642 int prioritised_exist = 0;
2643 struct yaffs_block_info *bi;
2646 /* First let's see if we need to grab a prioritised block */
2647 if (dev->has_pending_prioritised_gc && !aggressive) {
2648 dev->gc_dirtiest = 0;
2649 bi = dev->block_info;
2650 for (i = dev->internal_start_block;
2651 i <= dev->internal_end_block && !selected; i++) {
2653 if (bi->gc_prioritise) {
2654 prioritised_exist = 1;
2655 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2656 yaffs_block_ok_for_gc(dev, bi)) {
2665 * If there is a prioritised block and none was selected then
2666 * this happened because there is at least one old dirty block
2667 * gumming up the works. Let's gc the oldest dirty block.
2670 if (prioritised_exist &&
2671 !selected && dev->oldest_dirty_block > 0)
2672 selected = dev->oldest_dirty_block;
2674 if (!prioritised_exist) /* None found, so we can clear this */
2675 dev->has_pending_prioritised_gc = 0;
2678 /* If we're doing aggressive GC then we are happy to take a less-dirty
2679 * block, and search harder.
2680 * else (leasurely gc), then we only bother to do this if the
2681 * block has only a few pages in use.
2687 dev->internal_end_block - dev->internal_start_block + 1;
2689 threshold = dev->param.chunks_per_block;
2690 iterations = n_blocks;
2695 max_threshold = dev->param.chunks_per_block / 2;
2697 max_threshold = dev->param.chunks_per_block / 8;
2699 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2700 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2702 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2703 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2704 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2705 if (threshold > max_threshold)
2706 threshold = max_threshold;
2708 iterations = n_blocks / 16 + 1;
2709 if (iterations > 100)
2715 (dev->gc_dirtiest < 1 ||
2716 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2718 dev->gc_block_finder++;
2719 if (dev->gc_block_finder < dev->internal_start_block ||
2720 dev->gc_block_finder > dev->internal_end_block)
2721 dev->gc_block_finder =
2722 dev->internal_start_block;
2724 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2726 pages_used = bi->pages_in_use - bi->soft_del_pages;
2728 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2729 pages_used < dev->param.chunks_per_block &&
2730 (dev->gc_dirtiest < 1 ||
2731 pages_used < dev->gc_pages_in_use) &&
2732 yaffs_block_ok_for_gc(dev, bi)) {
2733 dev->gc_dirtiest = dev->gc_block_finder;
2734 dev->gc_pages_in_use = pages_used;
2738 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2739 selected = dev->gc_dirtiest;
2743 * If nothing has been selected for a while, try the oldest dirty
2744 * because that's gumming up the works.
2747 if (!selected && dev->param.is_yaffs2 &&
2748 dev->gc_not_done >= (background ? 10 : 20)) {
2749 yaffs2_find_oldest_dirty_seq(dev);
2750 if (dev->oldest_dirty_block > 0) {
2751 selected = dev->oldest_dirty_block;
2752 dev->gc_dirtiest = selected;
2753 dev->oldest_dirty_gc_count++;
2754 bi = yaffs_get_block_info(dev, selected);
2755 dev->gc_pages_in_use =
2756 bi->pages_in_use - bi->soft_del_pages;
2758 dev->gc_not_done = 0;
2763 yaffs_trace(YAFFS_TRACE_GC,
2764 "GC Selected block %d with %d free, prioritised:%d",
2766 dev->param.chunks_per_block - dev->gc_pages_in_use,
2773 dev->gc_dirtiest = 0;
2774 dev->gc_pages_in_use = 0;
2775 dev->gc_not_done = 0;
2776 if (dev->refresh_skip > 0)
2777 dev->refresh_skip--;
2780 yaffs_trace(YAFFS_TRACE_GC,
2781 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2782 dev->gc_block_finder, dev->gc_not_done, threshold,
2783 dev->gc_dirtiest, dev->gc_pages_in_use,
2784 dev->oldest_dirty_block, background ? " bg" : "");
2790 /* New garbage collector
2791 * If we're very low on erased blocks then we do aggressive garbage collection
2792 * otherwise we do "leasurely" garbage collection.
2793 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2794 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2796 * The idea is to help clear out space in a more spread-out manner.
2797 * Dunno if it really does anything useful.
2799 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2802 int gc_ok = YAFFS_OK;
2806 int checkpt_block_adjust;
2808 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2811 if (dev->gc_disable)
2812 /* Bail out so we don't get recursive gc */
2815 /* This loop should pass the first time.
2816 * Only loops here if the collection does not increase space.
2822 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2825 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2827 dev->n_erased_blocks * dev->param.chunks_per_block;
2829 /* If we need a block soon then do aggressive gc. */
2830 if (dev->n_erased_blocks < min_erased)
2834 && erased_chunks > (dev->n_free_chunks / 4))
2837 if (dev->gc_skip > 20)
2839 if (erased_chunks < dev->n_free_chunks / 2 ||
2840 dev->gc_skip < 1 || background)
2850 /* If we don't already have a block being gc'd then see if we
2851 * should start another */
2853 if (dev->gc_block < 1 && !aggressive) {
2854 dev->gc_block = yaffs2_find_refresh_block(dev);
2856 dev->n_clean_ups = 0;
2858 if (dev->gc_block < 1) {
2860 yaffs_find_gc_block(dev, aggressive, background);
2862 dev->n_clean_ups = 0;
2865 if (dev->gc_block > 0) {
2868 dev->passive_gc_count++;
2870 yaffs_trace(YAFFS_TRACE_GC,
2871 "yaffs: GC n_erased_blocks %d aggressive %d",
2872 dev->n_erased_blocks, aggressive);
2874 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2877 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
2878 dev->gc_block > 0) {
2879 yaffs_trace(YAFFS_TRACE_GC,
2880 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2881 dev->n_erased_blocks, max_tries,
2884 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2885 (dev->gc_block > 0) && (max_tries < 2));
2887 return aggressive ? gc_ok : YAFFS_OK;
2892 * Garbage collects. Intended to be called from a background thread.
2893 * Returns non-zero if at least half the free chunks are erased.
2895 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2897 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2899 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2901 yaffs_check_gc(dev, 1);
2902 return erased_chunks > dev->n_free_chunks / 2;
2905 /*-------------------- Data file manipulation -----------------*/
2907 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2909 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2911 if (nand_chunk >= 0)
2912 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2915 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2916 "Chunk %d not found zero instead",
2918 /* get sane (zero) data if you read a hole */
2919 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2925 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2930 struct yaffs_ext_tags tags;
2931 struct yaffs_block_info *bi;
2937 block = chunk_id / dev->param.chunks_per_block;
2938 page = chunk_id % dev->param.chunks_per_block;
2940 if (!yaffs_check_chunk_bit(dev, block, page))
2941 yaffs_trace(YAFFS_TRACE_VERIFY,
2942 "Deleting invalid chunk %d", chunk_id);
2944 bi = yaffs_get_block_info(dev, block);
2946 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2948 yaffs_trace(YAFFS_TRACE_DELETION,
2949 "line %d delete of chunk %d",
2952 if (!dev->param.is_yaffs2 && mark_flash &&
2953 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2955 memset(&tags, 0, sizeof(tags));
2956 tags.is_deleted = 1;
2957 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2958 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2960 dev->n_unmarked_deletions++;
2963 /* Pull out of the management area.
2964 * If the whole block became dirty, this will kick off an erasure.
2966 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2967 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2968 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
2969 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2970 dev->n_free_chunks++;
2971 yaffs_clear_chunk_bit(dev, block, page);
2974 if (bi->pages_in_use == 0 &&
2975 !bi->has_shrink_hdr &&
2976 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2977 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
2978 yaffs_block_became_dirty(dev, block);
2983 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2984 const u8 *buffer, int n_bytes, int use_reserve)
2986 /* Find old chunk Need to do this to get serial number
2987 * Write new one and patch into tree.
2988 * Invalidate old tags.
2992 struct yaffs_ext_tags prev_tags;
2994 struct yaffs_ext_tags new_tags;
2995 struct yaffs_dev *dev = in->my_dev;
2997 yaffs_check_gc(dev, 0);
2999 /* Get the previous chunk at this location in the file if it exists.
3000 * If it does not exist then put a zero into the tree. This creates
3001 * the tnode now, rather than later when it is harder to clean up.
3003 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3004 if (prev_chunk_id < 1 &&
3005 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3008 /* Set up new tags */
3009 memset(&new_tags, 0, sizeof(new_tags));
3011 new_tags.chunk_id = inode_chunk;
3012 new_tags.obj_id = in->obj_id;
3013 new_tags.serial_number =
3014 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3015 new_tags.n_bytes = n_bytes;
3017 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3018 yaffs_trace(YAFFS_TRACE_ERROR,
3019 "Writing %d bytes to chunk!!!!!!!!!",
3025 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3027 if (new_chunk_id > 0) {
3028 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3030 if (prev_chunk_id > 0)
3031 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3033 yaffs_verify_file_sane(in);
3035 return new_chunk_id;
3041 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3042 const YCHAR *name, const void *value, int size,
3045 struct yaffs_xattr_mod xmod;
3053 xmod.result = -ENOSPC;
3055 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3063 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3064 struct yaffs_xattr_mod *xmod)
3067 int x_offs = sizeof(struct yaffs_obj_hdr);
3068 struct yaffs_dev *dev = obj->my_dev;
3069 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3070 char *x_buffer = buffer + x_offs;
3074 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3075 xmod->size, xmod->flags);
3077 retval = nval_del(x_buffer, x_size, xmod->name);
3079 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3080 obj->xattr_known = 1;
3081 xmod->result = retval;
3086 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
3087 void *value, int size)
3089 char *buffer = NULL;
3091 struct yaffs_ext_tags tags;
3092 struct yaffs_dev *dev = obj->my_dev;
3093 int x_offs = sizeof(struct yaffs_obj_hdr);
3094 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3098 if (obj->hdr_chunk < 1)
3101 /* If we know that the object has no xattribs then don't do all the
3102 * reading and parsing.
3104 if (obj->xattr_known && !obj->has_xattr) {
3111 buffer = (char *)yaffs_get_temp_buffer(dev);
3116 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3118 if (result != YAFFS_OK)
3121 x_buffer = buffer + x_offs;
3123 if (!obj->xattr_known) {
3124 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3125 obj->xattr_known = 1;
3129 retval = nval_get(x_buffer, x_size, name, value, size);
3131 retval = nval_list(x_buffer, x_size, value, size);
3133 yaffs_release_temp_buffer(dev, (u8 *) buffer);
3137 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3138 const void *value, int size, int flags)
3140 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3143 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3145 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3148 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3151 return yaffs_do_xattrib_fetch(obj, name, value, size);
3154 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3156 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3159 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3162 struct yaffs_obj_hdr *oh;
3163 struct yaffs_dev *dev;
3164 struct yaffs_ext_tags tags;
3166 int alloc_failed = 0;
3168 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3172 in->lazy_loaded = 0;
3173 buf = yaffs_get_temp_buffer(dev);
3175 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3176 oh = (struct yaffs_obj_hdr *)buf;
3178 in->yst_mode = oh->yst_mode;
3179 yaffs_load_attribs(in, oh);
3180 yaffs_set_obj_name_from_oh(in, oh);
3182 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3183 in->variant.symlink_variant.alias =
3184 yaffs_clone_str(oh->alias);
3185 if (!in->variant.symlink_variant.alias)
3186 alloc_failed = 1; /* Not returned */
3188 yaffs_release_temp_buffer(dev, buf);
3191 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
3192 const YCHAR *oh_name, int buff_size)
3194 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3195 if (dev->param.auto_unicode) {
3197 /* It is an ASCII name, do an ASCII to
3198 * unicode conversion */
3199 const char *ascii_oh_name = (const char *)oh_name;
3200 int n = buff_size - 1;
3201 while (n > 0 && *ascii_oh_name) {
3202 *name = *ascii_oh_name;
3208 strncpy(name, oh_name + 1, buff_size - 1);
3214 strncpy(name, oh_name, buff_size - 1);
3218 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
3221 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3226 if (dev->param.auto_unicode) {
3231 /* Figure out if the name will fit in ascii character set */
3232 while (is_ascii && *w) {
3239 /* It is an ASCII name, so convert unicode to ascii */
3240 char *ascii_oh_name = (char *)oh_name;
3241 int n = YAFFS_MAX_NAME_LENGTH - 1;
3242 while (n > 0 && *name) {
3243 *ascii_oh_name = *name;
3249 /* Unicode name, so save starting at the second YCHAR */
3251 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
3257 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3261 /* UpdateObjectHeader updates the header on NAND for an object.
3262 * If name is not NULL, then that new name is used.
3264 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3265 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3268 struct yaffs_block_info *bi;
3269 struct yaffs_dev *dev = in->my_dev;
3274 struct yaffs_ext_tags new_tags;
3275 struct yaffs_ext_tags old_tags;
3276 const YCHAR *alias = NULL;
3278 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3279 struct yaffs_obj_hdr *oh = NULL;
3281 strcpy(old_name, _Y("silly old name"));
3283 if (in->fake && in != dev->root_dir && !force && !xmod)
3286 yaffs_check_gc(dev, 0);
3287 yaffs_check_obj_details_loaded(in);
3289 buffer = yaffs_get_temp_buffer(in->my_dev);
3290 oh = (struct yaffs_obj_hdr *)buffer;
3292 prev_chunk_id = in->hdr_chunk;
3294 if (prev_chunk_id > 0) {
3295 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3298 yaffs_verify_oh(in, oh, &old_tags, 0);
3299 memcpy(old_name, oh->name, sizeof(oh->name));
3300 memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
3302 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3305 oh->type = in->variant_type;
3306 oh->yst_mode = in->yst_mode;
3307 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3309 yaffs_load_attribs_oh(oh, in);
3312 oh->parent_obj_id = in->parent->obj_id;
3314 oh->parent_obj_id = 0;
3316 if (name && *name) {
3317 memset(oh->name, 0, sizeof(oh->name));
3318 yaffs_load_oh_from_name(dev, oh->name, name);
3319 } else if (prev_chunk_id > 0) {
3320 memcpy(oh->name, old_name, sizeof(oh->name));
3322 memset(oh->name, 0, sizeof(oh->name));
3325 oh->is_shrink = is_shrink;
3327 switch (in->variant_type) {
3328 case YAFFS_OBJECT_TYPE_UNKNOWN:
3329 /* Should not happen */
3331 case YAFFS_OBJECT_TYPE_FILE:
3333 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED ||
3334 oh->parent_obj_id == YAFFS_OBJECTID_UNLINKED) ?
3335 0 : in->variant.file_variant.file_size;
3337 case YAFFS_OBJECT_TYPE_HARDLINK:
3338 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3340 case YAFFS_OBJECT_TYPE_SPECIAL:
3343 case YAFFS_OBJECT_TYPE_DIRECTORY:
3346 case YAFFS_OBJECT_TYPE_SYMLINK:
3347 alias = in->variant.symlink_variant.alias;
3349 alias = _Y("no alias");
3350 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3351 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3355 /* process any xattrib modifications */
3357 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3360 memset(&new_tags, 0, sizeof(new_tags));
3362 new_tags.chunk_id = 0;
3363 new_tags.obj_id = in->obj_id;
3364 new_tags.serial_number = in->serial;
3366 /* Add extra info for file header */
3367 new_tags.extra_available = 1;
3368 new_tags.extra_parent_id = oh->parent_obj_id;
3369 new_tags.extra_length = oh->file_size;
3370 new_tags.extra_is_shrink = oh->is_shrink;
3371 new_tags.extra_equiv_id = oh->equiv_id;
3372 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3373 new_tags.extra_obj_type = in->variant_type;
3374 yaffs_verify_oh(in, oh, &new_tags, 1);
3376 /* Create new chunk in NAND */
3378 yaffs_write_new_chunk(dev, buffer, &new_tags,
3379 (prev_chunk_id > 0) ? 1 : 0);
3382 yaffs_release_temp_buffer(dev, buffer);
3384 if (new_chunk_id < 0)
3385 return new_chunk_id;
3387 in->hdr_chunk = new_chunk_id;
3389 if (prev_chunk_id > 0)
3390 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3392 if (!yaffs_obj_cache_dirty(in))
3395 /* If this was a shrink, then mark the block
3396 * that the chunk lives on */
3398 bi = yaffs_get_block_info(in->my_dev,
3400 in->my_dev->param.chunks_per_block);
3401 bi->has_shrink_hdr = 1;
3405 return new_chunk_id;
3408 /*--------------------- File read/write ------------------------
3409 * Read and write have very similar structures.
3410 * In general the read/write has three parts to it
3411 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3412 * Some complete chunks
3413 * An incomplete chunk to end off with
3415 * Curve-balls: the first chunk might also be the last chunk.
3418 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3425 struct yaffs_cache *cache;
3426 struct yaffs_dev *dev;
3431 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3434 /* OK now check for the curveball where the start and end are in
3437 if ((start + n) < dev->data_bytes_per_chunk)
3440 n_copy = dev->data_bytes_per_chunk - start;
3442 cache = yaffs_find_chunk_cache(in, chunk);
3444 /* If the chunk is already in the cache or it is less than
3445 * a whole chunk or we're using inband tags then use the cache
3446 * (if there is caching) else bypass the cache.
3448 if (cache || n_copy != dev->data_bytes_per_chunk ||
3449 dev->param.inband_tags) {
3450 if (dev->param.n_caches > 0) {
3452 /* If we can't find the data in the cache,
3453 * then load it up. */
3457 yaffs_grab_chunk_cache(in->my_dev);
3459 cache->chunk_id = chunk;
3462 yaffs_rd_data_obj(in, chunk,
3467 yaffs_use_cache(dev, cache, 0);
3471 memcpy(buffer, &cache->data[start], n_copy);
3475 /* Read into the local buffer then copy.. */
3478 yaffs_get_temp_buffer(dev);
3479 yaffs_rd_data_obj(in, chunk, local_buffer);
3481 memcpy(buffer, &local_buffer[start], n_copy);
3483 yaffs_release_temp_buffer(dev, local_buffer);
3486 /* A full chunk. Read directly into the buffer. */
3487 yaffs_rd_data_obj(in, chunk, buffer);
3497 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3498 int n_bytes, int write_trhrough)
3507 int start_write = offset;
3508 int chunk_written = 0;
3511 struct yaffs_dev *dev;
3515 while (n > 0 && chunk_written >= 0) {
3516 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3518 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3519 start >= dev->data_bytes_per_chunk) {
3520 yaffs_trace(YAFFS_TRACE_ERROR,
3521 "AddrToChunk of offset %d gives chunk %d start %d",
3522 (int)offset, chunk, start);
3524 chunk++; /* File pos to chunk in file offset */
3526 /* OK now check for the curveball where the start and end are in
3530 if ((start + n) < dev->data_bytes_per_chunk) {
3533 /* Now calculate how many bytes to write back....
3534 * If we're overwriting and not writing to then end of
3535 * file then we need to write back as much as was there
3539 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3541 if (chunk_start > in->variant.file_variant.file_size)
3542 n_bytes_read = 0; /* Past end of file */
3545 in->variant.file_variant.file_size -
3548 if (n_bytes_read > dev->data_bytes_per_chunk)
3549 n_bytes_read = dev->data_bytes_per_chunk;
3553 (start + n)) ? n_bytes_read : (start + n);
3555 if (n_writeback < 0 ||
3556 n_writeback > dev->data_bytes_per_chunk)
3560 n_copy = dev->data_bytes_per_chunk - start;
3561 n_writeback = dev->data_bytes_per_chunk;
3564 if (n_copy != dev->data_bytes_per_chunk ||
3565 dev->param.inband_tags) {
3566 /* An incomplete start or end chunk (or maybe both
3567 * start and end chunk), or we're using inband tags,
3568 * so we want to use the cache buffers.
3570 if (dev->param.n_caches > 0) {
3571 struct yaffs_cache *cache;
3573 /* If we can't find the data in the cache, then
3575 cache = yaffs_find_chunk_cache(in, chunk);
3578 yaffs_check_alloc_available(dev, 1)) {
3579 cache = yaffs_grab_chunk_cache(dev);
3581 cache->chunk_id = chunk;
3584 yaffs_rd_data_obj(in, chunk,
3588 !yaffs_check_alloc_available(dev,
3590 /* Drop the cache if it was a read cache
3591 * item and no space check has been made
3598 yaffs_use_cache(dev, cache, 1);
3601 memcpy(&cache->data[start], buffer,
3605 cache->n_bytes = n_writeback;
3607 if (write_trhrough) {
3617 chunk_written = -1; /* fail write */
3620 /* An incomplete start or end chunk (or maybe
3621 * both start and end chunk). Read into the
3622 * local buffer then copy over and write back.
3625 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3627 yaffs_rd_data_obj(in, chunk, local_buffer);
3628 memcpy(&local_buffer[start], buffer, n_copy);
3631 yaffs_wr_data_obj(in, chunk,
3635 yaffs_release_temp_buffer(dev, local_buffer);
3638 /* A full chunk. Write directly from the buffer. */
3641 yaffs_wr_data_obj(in, chunk, buffer,
3642 dev->data_bytes_per_chunk, 0);
3644 /* Since we've overwritten the cached data,
3645 * we better invalidate it. */
3646 yaffs_invalidate_chunk_cache(in, chunk);
3649 if (chunk_written >= 0) {
3657 /* Update file object */
3659 if ((start_write + n_done) > in->variant.file_variant.file_size)
3660 in->variant.file_variant.file_size = (start_write + n_done);
3666 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3667 int n_bytes, int write_trhrough)
3669 yaffs2_handle_hole(in, offset);
3670 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3673 /* ---------------------- File resizing stuff ------------------ */
3675 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3678 struct yaffs_dev *dev = in->my_dev;
3679 int old_size = in->variant.file_variant.file_size;
3682 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3683 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3684 dev->data_bytes_per_chunk;
3687 /* Delete backwards so that we don't end up with holes if
3688 * power is lost part-way through the operation.
3690 for (i = last_del; i >= start_del; i--) {
3691 /* NB this could be optimised somewhat,
3692 * eg. could retrieve the tags and write them without
3693 * using yaffs_chunk_del
3696 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3702 (dev->internal_start_block * dev->param.chunks_per_block) ||
3704 ((dev->internal_end_block + 1) *
3705 dev->param.chunks_per_block)) {
3706 yaffs_trace(YAFFS_TRACE_ALWAYS,
3707 "Found daft chunk_id %d for %d",
3710 in->n_data_chunks--;
3711 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3716 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3720 struct yaffs_dev *dev = obj->my_dev;
3722 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3724 yaffs_prune_chunks(obj, new_size);
3726 if (new_partial != 0) {
3727 int last_chunk = 1 + new_full;
3728 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3730 /* Rewrite the last chunk with its new size and zero pad */
3731 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3732 memset(local_buffer + new_partial, 0,
3733 dev->data_bytes_per_chunk - new_partial);
3735 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3738 yaffs_release_temp_buffer(dev, local_buffer);
3741 obj->variant.file_variant.file_size = new_size;
3743 yaffs_prune_tree(dev, &obj->variant.file_variant);
3746 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3748 struct yaffs_dev *dev = in->my_dev;
3749 int old_size = in->variant.file_variant.file_size;
3751 yaffs_flush_file_cache(in);
3752 yaffs_invalidate_whole_cache(in);
3754 yaffs_check_gc(dev, 0);
3756 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3759 if (new_size == old_size)
3762 if (new_size > old_size) {
3763 yaffs2_handle_hole(in, new_size);
3764 in->variant.file_variant.file_size = new_size;
3766 /* new_size < old_size */
3767 yaffs_resize_file_down(in, new_size);
3770 /* Write a new object header to reflect the resize.
3771 * show we've shrunk the file, if need be
3772 * Do this only if the file is not in the deleted directories
3773 * and is not shadowed.
3777 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3778 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3779 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3784 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3789 yaffs_flush_file_cache(in);
3795 yaffs_load_current_time(in, 0, 0);
3797 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3798 YAFFS_OK : YAFFS_FAIL;
3802 /* yaffs_del_file deletes the whole file data
3803 * and the inode associated with the file.
3804 * It does not delete the links associated with the file.
3806 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3810 struct yaffs_dev *dev = in->my_dev;
3817 yaffs_change_obj_name(in, in->my_dev->del_dir,
3818 _Y("deleted"), 0, 0);
3819 yaffs_trace(YAFFS_TRACE_TRACING,
3820 "yaffs: immediate deletion of file %d",
3823 in->my_dev->n_deleted_files++;
3824 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3825 yaffs_resize_file(in, 0);
3826 yaffs_soft_del_file(in);
3829 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3830 _Y("unlinked"), 0, 0);
3835 int yaffs_del_file(struct yaffs_obj *in)
3837 int ret_val = YAFFS_OK;
3838 int deleted; /* Need to cache value on stack if in is freed */
3839 struct yaffs_dev *dev = in->my_dev;
3841 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3842 yaffs_resize_file(in, 0);
3844 if (in->n_data_chunks > 0) {
3845 /* Use soft deletion if there is data in the file.
3846 * That won't be the case if it has been resized to zero.
3849 ret_val = yaffs_unlink_file_if_needed(in);
3851 deleted = in->deleted;
3853 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3856 in->my_dev->n_deleted_files++;
3857 yaffs_soft_del_file(in);
3859 return deleted ? YAFFS_OK : YAFFS_FAIL;
3861 /* The file has no data chunks so we toss it immediately */
3862 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3863 in->variant.file_variant.top = NULL;
3864 yaffs_generic_obj_del(in);
3870 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3873 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3874 !(list_empty(&obj->variant.dir_variant.children));
3877 static int yaffs_del_dir(struct yaffs_obj *obj)
3879 /* First check that the directory is empty. */
3880 if (yaffs_is_non_empty_dir(obj))
3883 return yaffs_generic_obj_del(obj);
3886 static int yaffs_del_symlink(struct yaffs_obj *in)
3888 kfree(in->variant.symlink_variant.alias);
3889 in->variant.symlink_variant.alias = NULL;
3891 return yaffs_generic_obj_del(in);
3894 static int yaffs_del_link(struct yaffs_obj *in)
3896 /* remove this hardlink from the list associated with the equivalent
3899 list_del_init(&in->hard_links);
3900 return yaffs_generic_obj_del(in);
3903 int yaffs_del_obj(struct yaffs_obj *obj)
3907 switch (obj->variant_type) {
3908 case YAFFS_OBJECT_TYPE_FILE:
3909 ret_val = yaffs_del_file(obj);
3911 case YAFFS_OBJECT_TYPE_DIRECTORY:
3912 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3913 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3914 "Remove object %d from dirty directories",
3916 list_del_init(&obj->variant.dir_variant.dirty);
3918 return yaffs_del_dir(obj);
3920 case YAFFS_OBJECT_TYPE_SYMLINK:
3921 ret_val = yaffs_del_symlink(obj);
3923 case YAFFS_OBJECT_TYPE_HARDLINK:
3924 ret_val = yaffs_del_link(obj);
3926 case YAFFS_OBJECT_TYPE_SPECIAL:
3927 ret_val = yaffs_generic_obj_del(obj);
3929 case YAFFS_OBJECT_TYPE_UNKNOWN:
3931 break; /* should not happen. */
3936 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3946 yaffs_update_parent(obj->parent);
3948 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3949 return yaffs_del_link(obj);
3950 } else if (!list_empty(&obj->hard_links)) {
3951 /* Curve ball: We're unlinking an object that has a hardlink.
3953 * This problem arises because we are not strictly following
3954 * The Linux link/inode model.
3956 * We can't really delete the object.
3957 * Instead, we do the following:
3958 * - Select a hardlink.
3959 * - Unhook it from the hard links
3960 * - Move it from its parent directory so that the rename works.
3961 * - Rename the object to the hardlink's name.
3962 * - Delete the hardlink
3965 struct yaffs_obj *hl;
3966 struct yaffs_obj *parent;
3968 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3970 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3973 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3974 parent = hl->parent;
3976 list_del_init(&hl->hard_links);
3978 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3980 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
3982 if (ret_val == YAFFS_OK)
3983 ret_val = yaffs_generic_obj_del(hl);
3987 } else if (del_now) {
3988 switch (obj->variant_type) {
3989 case YAFFS_OBJECT_TYPE_FILE:
3990 return yaffs_del_file(obj);
3992 case YAFFS_OBJECT_TYPE_DIRECTORY:
3993 list_del_init(&obj->variant.dir_variant.dirty);
3994 return yaffs_del_dir(obj);
3996 case YAFFS_OBJECT_TYPE_SYMLINK:
3997 return yaffs_del_symlink(obj);
3999 case YAFFS_OBJECT_TYPE_SPECIAL:
4000 return yaffs_generic_obj_del(obj);
4002 case YAFFS_OBJECT_TYPE_HARDLINK:
4003 case YAFFS_OBJECT_TYPE_UNKNOWN:
4007 } else if (yaffs_is_non_empty_dir(obj)) {
4010 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4011 _Y("unlinked"), 0, 0);
4015 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4017 if (obj && obj->unlink_allowed)
4018 return yaffs_unlink_worker(obj);
4023 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4025 struct yaffs_obj *obj;
4027 obj = yaffs_find_by_name(dir, name);
4028 return yaffs_unlink_obj(obj);
4032 * If old_name is NULL then we take old_dir as the object to be renamed.
4034 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
4035 struct yaffs_obj *new_dir, const YCHAR *new_name)
4037 struct yaffs_obj *obj = NULL;
4038 struct yaffs_obj *existing_target = NULL;
4041 struct yaffs_dev *dev;
4043 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4047 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4052 dev = old_dir->my_dev;
4054 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4055 /* Special case for case insemsitive systems.
4056 * While look-up is case insensitive, the name isn't.
4057 * Therefore we might want to change x.txt to X.txt
4059 if (old_dir == new_dir &&
4060 old_name && new_name &&
4061 strcmp(old_name, new_name) == 0)
4065 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4066 YAFFS_MAX_NAME_LENGTH)
4071 obj = yaffs_find_by_name(old_dir, old_name);
4074 old_dir = obj->parent;
4077 if (obj && obj->rename_allowed) {
4078 /* Now handle an existing target, if there is one */
4079 existing_target = yaffs_find_by_name(new_dir, new_name);
4080 if (yaffs_is_non_empty_dir(existing_target)) {
4081 return YAFFS_FAIL; /* ENOTEMPTY */
4082 } else if (existing_target && existing_target != obj) {
4083 /* Nuke the target first, using shadowing,
4084 * but only if it isn't the same object.
4086 * Note we must disable gc here otherwise it can mess
4090 dev->gc_disable = 1;
4091 yaffs_change_obj_name(obj, new_dir, new_name, force,
4092 existing_target->obj_id);
4093 existing_target->is_shadowed = 1;
4094 yaffs_unlink_obj(existing_target);
4095 dev->gc_disable = 0;
4098 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4100 yaffs_update_parent(old_dir);
4101 if (new_dir != old_dir)
4102 yaffs_update_parent(new_dir);
4109 /*----------------------- Initialisation Scanning ---------------------- */
4111 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4112 int backward_scanning)
4114 struct yaffs_obj *obj;
4116 if (backward_scanning) {
4117 /* Handle YAFFS2 case (backward scanning)
4118 * If the shadowed object exists then ignore.
4120 obj = yaffs_find_by_number(dev, obj_id);
4125 /* Let's create it (if it does not exist) assuming it is a file so that
4126 * it can do shrinking etc.
4127 * We put it in unlinked dir to be cleaned up after the scanning
4130 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4133 obj->is_shadowed = 1;
4134 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4135 obj->variant.file_variant.shrink_size = 0;
4136 obj->valid = 1; /* So that we don't read any other info. */
4139 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4141 struct list_head *lh;
4142 struct list_head *save;
4143 struct yaffs_obj *hl;
4144 struct yaffs_obj *in;
4146 list_for_each_safe(lh, save, hard_list) {
4147 hl = list_entry(lh, struct yaffs_obj, hard_links);
4148 in = yaffs_find_by_number(dev,
4149 hl->variant.hardlink_variant.equiv_id);
4152 /* Add the hardlink pointers */
4153 hl->variant.hardlink_variant.equiv_obj = in;
4154 list_add(&hl->hard_links, &in->hard_links);
4156 /* Todo Need to report/handle this better.
4157 * Got a problem... hardlink to a non-existant object
4159 hl->variant.hardlink_variant.equiv_obj = NULL;
4160 INIT_LIST_HEAD(&hl->hard_links);
4165 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4168 * Sort out state of unlinked and deleted objects after scanning.
4170 struct list_head *i;
4171 struct list_head *n;
4172 struct yaffs_obj *l;
4177 /* Soft delete all the unlinked files */
4178 list_for_each_safe(i, n,
4179 &dev->unlinked_dir->variant.dir_variant.children) {
4180 l = list_entry(i, struct yaffs_obj, siblings);
4184 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4185 l = list_entry(i, struct yaffs_obj, siblings);
4191 * This code iterates through all the objects making sure that they are rooted.
4192 * Any unrooted objects are re-rooted in lost+found.
4193 * An object needs to be in one of:
4194 * - Directly under deleted, unlinked
4195 * - Directly or indirectly under root.
4198 * This code assumes that we don't ever change the current relationships
4199 * between directories:
4200 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4201 * lost-n-found->parent == root_dir
4203 * This fixes the problem where directories might have inadvertently been
4204 * deleted leaving the object "hanging" without being rooted in the
4208 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4210 return (obj == dev->del_dir ||
4211 obj == dev->unlinked_dir || obj == dev->root_dir);
4214 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4216 struct yaffs_obj *obj;
4217 struct yaffs_obj *parent;
4219 struct list_head *lh;
4220 struct list_head *n;
4227 /* Iterate through the objects in each hash entry,
4228 * looking at each object.
4229 * Make sure it is rooted.
4232 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4233 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4234 obj = list_entry(lh, struct yaffs_obj, hash_link);
4235 parent = obj->parent;
4237 if (yaffs_has_null_parent(dev, obj)) {
4238 /* These directories are not hanging */
4240 } else if (!parent ||
4241 parent->variant_type !=
4242 YAFFS_OBJECT_TYPE_DIRECTORY) {
4244 } else if (yaffs_has_null_parent(dev, parent)) {
4248 * Need to follow the parent chain to
4249 * see if it is hanging.
4254 while (parent != dev->root_dir &&
4256 parent->parent->variant_type ==
4257 YAFFS_OBJECT_TYPE_DIRECTORY &&
4259 parent = parent->parent;
4262 if (parent != dev->root_dir)
4266 yaffs_trace(YAFFS_TRACE_SCAN,
4267 "Hanging object %d moved to lost and found",
4269 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4276 * Delete directory contents for cleaning up lost and found.
4278 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4280 struct yaffs_obj *obj;
4281 struct list_head *lh;
4282 struct list_head *n;
4284 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4287 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4288 obj = list_entry(lh, struct yaffs_obj, siblings);
4289 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4290 yaffs_del_dir_contents(obj);
4291 yaffs_trace(YAFFS_TRACE_SCAN,
4292 "Deleting lost_found object %d",
4294 yaffs_unlink_obj(obj);
4298 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4300 yaffs_del_dir_contents(dev->lost_n_found);
4304 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4308 struct list_head *i;
4309 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4310 struct yaffs_obj *l;
4316 yaffs_trace(YAFFS_TRACE_ALWAYS,
4317 "tragedy: yaffs_find_by_name: null pointer directory"
4322 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4323 yaffs_trace(YAFFS_TRACE_ALWAYS,
4324 "tragedy: yaffs_find_by_name: non-directory"
4329 sum = yaffs_calc_name_sum(name);
4331 list_for_each(i, &directory->variant.dir_variant.children) {
4332 l = list_entry(i, struct yaffs_obj, siblings);
4334 if (l->parent != directory)
4337 yaffs_check_obj_details_loaded(l);
4339 /* Special case for lost-n-found */
4340 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4341 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4343 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4344 /* LostnFound chunk called Objxxx
4347 yaffs_get_obj_name(l, buffer,
4348 YAFFS_MAX_NAME_LENGTH + 1);
4349 if (strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4356 /* GetEquivalentObject dereferences any hard links to get to the
4360 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4362 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4363 obj = obj->variant.hardlink_variant.equiv_obj;
4364 yaffs_check_obj_details_loaded(obj);
4370 * A note or two on object names.
4371 * * If the object name is missing, we then make one up in the form objnnn
4373 * * ASCII names are stored in the object header's name field from byte zero
4374 * * Unicode names are historically stored starting from byte zero.
4376 * Then there are automatic Unicode names...
4377 * The purpose of these is to save names in a way that can be read as
4378 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4379 * system to share files.
4381 * These automatic unicode are stored slightly differently...
4382 * - If the name can fit in the ASCII character space then they are saved as
4383 * ascii names as per above.
4384 * - If the name needs Unicode then the name is saved in Unicode
4385 * starting at oh->name[1].
4388 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4391 /* Create an object name if we could not find one. */
4392 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4393 YCHAR local_name[20];
4394 YCHAR num_string[20];
4395 YCHAR *x = &num_string[19];
4396 unsigned v = obj->obj_id;
4400 *x = '0' + (v % 10);
4403 /* make up a name */
4404 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4405 strcat(local_name, x);
4406 strncpy(name, local_name, buffer_size - 1);
4410 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4412 memset(name, 0, buffer_size * sizeof(YCHAR));
4413 yaffs_check_obj_details_loaded(obj);
4414 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4415 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4416 } else if (obj->short_name[0]) {
4417 strcpy(name, obj->short_name);
4418 } else if (obj->hdr_chunk > 0) {
4420 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4422 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4424 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4426 if (obj->hdr_chunk > 0) {
4427 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4431 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4434 yaffs_release_temp_buffer(obj->my_dev, buffer);
4437 yaffs_fix_null_name(obj, name, buffer_size);
4439 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4442 int yaffs_get_obj_length(struct yaffs_obj *obj)
4444 /* Dereference any hard linking */
4445 obj = yaffs_get_equivalent_obj(obj);
4447 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4448 return obj->variant.file_variant.file_size;
4449 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4450 if (!obj->variant.symlink_variant.alias)
4452 return strnlen(obj->variant.symlink_variant.alias,
4453 YAFFS_MAX_ALIAS_LENGTH);
4455 /* Only a directory should drop through to here */
4456 return obj->my_dev->data_bytes_per_chunk;
4460 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4463 struct list_head *i;
4466 count++; /* the object itself */
4468 list_for_each(i, &obj->hard_links)
4469 count++; /* add the hard links; */
4474 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4476 obj = yaffs_get_equivalent_obj(obj);
4481 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4483 obj = yaffs_get_equivalent_obj(obj);
4485 switch (obj->variant_type) {
4486 case YAFFS_OBJECT_TYPE_FILE:
4489 case YAFFS_OBJECT_TYPE_DIRECTORY:
4492 case YAFFS_OBJECT_TYPE_SYMLINK:
4495 case YAFFS_OBJECT_TYPE_HARDLINK:
4498 case YAFFS_OBJECT_TYPE_SPECIAL:
4499 if (S_ISFIFO(obj->yst_mode))
4501 if (S_ISCHR(obj->yst_mode))
4503 if (S_ISBLK(obj->yst_mode))
4505 if (S_ISSOCK(obj->yst_mode))
4515 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4517 obj = yaffs_get_equivalent_obj(obj);
4518 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4519 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4521 return yaffs_clone_str(_Y(""));
4524 /*--------------------------- Initialisation code -------------------------- */
4526 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4528 /* Common functions, gotta have */
4529 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4532 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4533 if (dev->param.write_chunk_tags_fn &&
4534 dev->param.read_chunk_tags_fn &&
4535 !dev->param.write_chunk_fn &&
4536 !dev->param.read_chunk_fn &&
4537 dev->param.bad_block_fn && dev->param.query_block_fn)
4540 /* Can use the "spare" style interface for yaffs1 */
4541 if (!dev->param.is_yaffs2 &&
4542 !dev->param.write_chunk_tags_fn &&
4543 !dev->param.read_chunk_tags_fn &&
4544 dev->param.write_chunk_fn &&
4545 dev->param.read_chunk_fn &&
4546 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4552 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4554 /* Initialise the unlinked, deleted, root and lost+found directories */
4555 dev->lost_n_found = dev->root_dir = NULL;
4556 dev->unlinked_dir = dev->del_dir = NULL;
4558 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4560 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4562 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4563 YAFFS_ROOT_MODE | S_IFDIR);
4565 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4566 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4568 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4570 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4576 int yaffs_guts_initialise(struct yaffs_dev *dev)
4578 int init_failed = 0;
4582 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()");
4584 /* Check stuff that must be set */
4587 yaffs_trace(YAFFS_TRACE_ALWAYS,
4588 "yaffs: Need a device"
4593 if (dev->is_mounted) {
4594 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4598 dev->internal_start_block = dev->param.start_block;
4599 dev->internal_end_block = dev->param.end_block;
4600 dev->block_offset = 0;
4601 dev->chunk_offset = 0;
4602 dev->n_free_chunks = 0;
4606 if (dev->param.start_block == 0) {
4607 dev->internal_start_block = dev->param.start_block + 1;
4608 dev->internal_end_block = dev->param.end_block + 1;
4609 dev->block_offset = 1;
4610 dev->chunk_offset = dev->param.chunks_per_block;
4613 /* Check geometry parameters. */
4615 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4616 dev->param.total_bytes_per_chunk < 1024) ||
4617 (!dev->param.is_yaffs2 &&
4618 dev->param.total_bytes_per_chunk < 512) ||
4619 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4620 dev->param.chunks_per_block < 2 ||
4621 dev->param.n_reserved_blocks < 2 ||
4622 dev->internal_start_block <= 0 ||
4623 dev->internal_end_block <= 0 ||
4624 dev->internal_end_block <=
4625 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4627 /* otherwise it is too small */
4628 yaffs_trace(YAFFS_TRACE_ALWAYS,
4629 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4630 dev->param.total_bytes_per_chunk,
4631 dev->param.is_yaffs2 ? "2" : "",
4632 dev->param.inband_tags);
4636 if (yaffs_init_nand(dev) != YAFFS_OK) {
4637 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4641 /* Sort out space for inband tags, if required */
4642 if (dev->param.inband_tags)
4643 dev->data_bytes_per_chunk =
4644 dev->param.total_bytes_per_chunk -
4645 sizeof(struct yaffs_packed_tags2_tags_only);
4647 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4649 /* Got the right mix of functions? */
4650 if (!yaffs_check_dev_fns(dev)) {
4651 /* Function missing */
4652 yaffs_trace(YAFFS_TRACE_ALWAYS,
4653 "device function(s) missing or wrong");
4658 /* Finished with most checks. Further checks happen later on too. */
4660 dev->is_mounted = 1;
4662 /* OK now calculate a few things for the device */
4665 * Calculate all the chunk size manipulation numbers:
4667 x = dev->data_bytes_per_chunk;
4668 /* We always use dev->chunk_shift and dev->chunk_div */
4669 dev->chunk_shift = calc_shifts(x);
4670 x >>= dev->chunk_shift;
4672 /* We only use chunk mask if chunk_div is 1 */
4673 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4676 * Calculate chunk_grp_bits.
4677 * We need to find the next power of 2 > than internal_end_block
4680 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4682 bits = calc_shifts_ceiling(x);
4684 /* Set up tnode width if wide tnodes are enabled. */
4685 if (!dev->param.wide_tnodes_disabled) {
4686 /* bits must be even so that we end up with 32-bit words */
4690 dev->tnode_width = 16;
4692 dev->tnode_width = bits;
4694 dev->tnode_width = 16;
4697 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4699 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4700 * so if the bitwidth of the
4701 * chunk range we're using is greater than 16 we need
4702 * to figure out chunk shift and chunk_grp_size
4705 if (bits <= dev->tnode_width)
4706 dev->chunk_grp_bits = 0;
4708 dev->chunk_grp_bits = bits - dev->tnode_width;
4710 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4711 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4712 dev->tnode_size = sizeof(struct yaffs_tnode);
4714 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4716 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4717 /* We have a problem because the soft delete won't work if
4718 * the chunk group size > chunks per block.
4719 * This can be remedied by using larger "virtual blocks".
4721 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4726 /* Finished verifying the device, continue with initialisation */
4728 /* More device initialisation */
4730 dev->passive_gc_count = 0;
4731 dev->oldest_dirty_gc_count = 0;
4733 dev->gc_block_finder = 0;
4734 dev->buffered_block = -1;
4735 dev->doing_buffered_block_rewrite = 0;
4736 dev->n_deleted_files = 0;
4737 dev->n_bg_deletions = 0;
4738 dev->n_unlinked_files = 0;
4739 dev->n_ecc_fixed = 0;
4740 dev->n_ecc_unfixed = 0;
4741 dev->n_tags_ecc_fixed = 0;
4742 dev->n_tags_ecc_unfixed = 0;
4743 dev->n_erase_failures = 0;
4744 dev->n_erased_blocks = 0;
4745 dev->gc_disable = 0;
4746 dev->has_pending_prioritised_gc = 1;
4747 /* Assume the worst for now, will get fixed on first GC */
4748 INIT_LIST_HEAD(&dev->dirty_dirs);
4749 dev->oldest_dirty_seq = 0;
4750 dev->oldest_dirty_block = 0;
4752 /* Initialise temporary buffers and caches. */
4753 if (!yaffs_init_tmp_buffers(dev))
4757 dev->gc_cleanup_list = NULL;
4759 if (!init_failed && dev->param.n_caches > 0) {
4763 dev->param.n_caches * sizeof(struct yaffs_cache);
4765 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4766 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4768 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4770 buf = (u8 *) dev->cache;
4773 memset(dev->cache, 0, cache_bytes);
4775 for (i = 0; i < dev->param.n_caches && buf; i++) {
4776 dev->cache[i].object = NULL;
4777 dev->cache[i].last_use = 0;
4778 dev->cache[i].dirty = 0;
4779 dev->cache[i].data = buf =
4780 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4785 dev->cache_last_use = 0;
4788 dev->cache_hits = 0;
4791 dev->gc_cleanup_list =
4792 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4794 if (!dev->gc_cleanup_list)
4798 if (dev->param.is_yaffs2)
4799 dev->param.use_header_file_size = 1;
4801 if (!init_failed && !yaffs_init_blocks(dev))
4804 yaffs_init_tnodes_and_objs(dev);
4806 if (!init_failed && !yaffs_create_initial_dir(dev))
4810 /* Now scan the flash. */
4811 if (dev->param.is_yaffs2) {
4812 if (yaffs2_checkpt_restore(dev)) {
4813 yaffs_check_obj_details_loaded(dev->root_dir);
4814 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4816 "yaffs: restored from checkpoint"
4820 /* Clean up the mess caused by an aborted
4821 * checkpoint load then scan backwards.
4823 yaffs_deinit_blocks(dev);
4825 yaffs_deinit_tnodes_and_objs(dev);
4827 dev->n_erased_blocks = 0;
4828 dev->n_free_chunks = 0;
4829 dev->alloc_block = -1;
4830 dev->alloc_page = -1;
4831 dev->n_deleted_files = 0;
4832 dev->n_unlinked_files = 0;
4833 dev->n_bg_deletions = 0;
4835 if (!init_failed && !yaffs_init_blocks(dev))
4838 yaffs_init_tnodes_and_objs(dev);
4841 && !yaffs_create_initial_dir(dev))
4844 if (!init_failed && !yaffs2_scan_backwards(dev))
4847 } else if (!yaffs1_scan(dev)) {
4851 yaffs_strip_deleted_objs(dev);
4852 yaffs_fix_hanging_objs(dev);
4853 if (dev->param.empty_lost_n_found)
4854 yaffs_empty_l_n_f(dev);
4858 /* Clean up the mess */
4859 yaffs_trace(YAFFS_TRACE_TRACING,
4860 "yaffs: yaffs_guts_initialise() aborted.");
4862 yaffs_deinitialise(dev);
4866 /* Zero out stats */
4867 dev->n_page_reads = 0;
4868 dev->n_page_writes = 0;
4869 dev->n_erasures = 0;
4870 dev->n_gc_copies = 0;
4871 dev->n_retired_writes = 0;
4873 dev->n_retired_blocks = 0;
4875 yaffs_verify_free_chunks(dev);
4876 yaffs_verify_blocks(dev);
4878 /* Clean up any aborted checkpoint data */
4879 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4880 yaffs2_checkpt_invalidate(dev);
4882 yaffs_trace(YAFFS_TRACE_TRACING,
4883 "yaffs: yaffs_guts_initialise() done.");
4887 void yaffs_deinitialise(struct yaffs_dev *dev)
4889 if (dev->is_mounted) {
4892 yaffs_deinit_blocks(dev);
4893 yaffs_deinit_tnodes_and_objs(dev);
4894 if (dev->param.n_caches > 0 && dev->cache) {
4896 for (i = 0; i < dev->param.n_caches; i++) {
4897 kfree(dev->cache[i].data);
4898 dev->cache[i].data = NULL;
4905 kfree(dev->gc_cleanup_list);
4907 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
4908 kfree(dev->temp_buffer[i].buffer);
4910 dev->is_mounted = 0;
4912 if (dev->param.deinitialise_flash_fn)
4913 dev->param.deinitialise_flash_fn(dev);
4917 int yaffs_count_free_chunks(struct yaffs_dev *dev)
4921 struct yaffs_block_info *blk;
4923 blk = dev->block_info;
4924 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4925 switch (blk->block_state) {
4926 case YAFFS_BLOCK_STATE_EMPTY:
4927 case YAFFS_BLOCK_STATE_ALLOCATING:
4928 case YAFFS_BLOCK_STATE_COLLECTING:
4929 case YAFFS_BLOCK_STATE_FULL:
4931 (dev->param.chunks_per_block - blk->pages_in_use +
4932 blk->soft_del_pages);
4942 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
4944 /* This is what we report to the outside world */
4947 int blocks_for_checkpt;
4950 n_free = dev->n_free_chunks;
4951 n_free += dev->n_deleted_files;
4953 /* Now count and subtract the number of dirty chunks in the cache. */
4955 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
4956 if (dev->cache[i].dirty)
4960 n_free -= n_dirty_caches;
4963 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
4965 /* Now figure checkpoint space and report that... */
4966 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
4968 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);