2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_trace.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_getblockinfo.h"
19 #include "yaffs_tagscompat.h"
20 #include "yaffs_nand.h"
21 #include "yaffs_yaffs1.h"
22 #include "yaffs_yaffs2.h"
23 #include "yaffs_bitmap.h"
24 #include "yaffs_verify.h"
25 #include "yaffs_nand.h"
26 #include "yaffs_packedtags2.h"
27 #include "yaffs_nameval.h"
28 #include "yaffs_allocator.h"
29 #include "yaffs_attribs.h"
31 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
32 #define YAFFS_GC_GOOD_ENOUGH 2
33 #define YAFFS_GC_PASSIVE_THRESHOLD 4
35 #include "yaffs_ecc.h"
37 /* Forward declarations */
39 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
40 const u8 *buffer, int n_bytes, int use_reserve);
44 /* Function to calculate chunk and offset */
46 static inline void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
47 int *chunk_out, u32 *offset_out)
52 chunk = (u32) (addr >> dev->chunk_shift);
54 if (dev->chunk_div == 1) {
55 /* easy power of 2 case */
56 offset = (u32) (addr & dev->chunk_mask);
58 /* Non power-of-2 case */
62 chunk /= dev->chunk_div;
64 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
65 offset = (u32) (addr - chunk_base);
72 /* Function to return the number of shifts for a power of 2 greater than or
73 * equal to the given number
74 * Note we don't try to cater for all possible numbers and this does not have to
75 * be hellishly efficient.
78 static inline u32 calc_shifts_ceiling(u32 x)
83 shifts = extra_bits = 0;
98 /* Function to return the number of shifts to get a 1 in bit 0
101 static inline u32 calc_shifts(u32 x)
119 * Temporary buffer manipulations.
122 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
127 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
129 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
130 dev->temp_buffer[i].in_use = 0;
131 buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
132 dev->temp_buffer[i].buffer = buf;
135 return buf ? YAFFS_OK : YAFFS_FAIL;
138 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
143 if (dev->temp_in_use > dev->max_temp)
144 dev->max_temp = dev->temp_in_use;
146 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
147 if (dev->temp_buffer[i].in_use == 0) {
148 dev->temp_buffer[i].in_use = 1;
149 return dev->temp_buffer[i].buffer;
153 yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
155 * If we got here then we have to allocate an unmanaged one
159 dev->unmanaged_buffer_allocs++;
160 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
164 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
170 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
171 if (dev->temp_buffer[i].buffer == buffer) {
172 dev->temp_buffer[i].in_use = 0;
178 /* assume it is an unmanaged one. */
179 yaffs_trace(YAFFS_TRACE_BUFFERS, "Releasing unmanaged temp buffer");
181 dev->unmanaged_buffer_deallocs++;
187 * Determine if we have a managed buffer.
189 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer)
193 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
194 if (dev->temp_buffer[i].buffer == buffer)
198 for (i = 0; i < dev->param.n_caches; i++) {
199 if (dev->cache[i].data == buffer)
203 if (buffer == dev->checkpt_buffer)
206 yaffs_trace(YAFFS_TRACE_ALWAYS,
207 "yaffs: unmaged buffer detected.");
212 * Functions for robustisizing TODO
216 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
218 const struct yaffs_ext_tags *tags)
221 nand_chunk = nand_chunk;
226 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
227 const struct yaffs_ext_tags *tags)
230 nand_chunk = nand_chunk;
234 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
235 struct yaffs_block_info *bi)
237 if (!bi->gc_prioritise) {
238 bi->gc_prioritise = 1;
239 dev->has_pending_prioritised_gc = 1;
240 bi->chunk_error_strikes++;
242 if (bi->chunk_error_strikes > 3) {
243 bi->needs_retiring = 1; /* Too many stikes, so retire */
244 yaffs_trace(YAFFS_TRACE_ALWAYS,
245 "yaffs: Block struck out");
251 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
254 int flash_block = nand_chunk / dev->param.chunks_per_block;
255 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
257 yaffs_handle_chunk_error(dev, bi);
260 /* Was an actual write failure,
261 * so mark the block for retirement.*/
262 bi->needs_retiring = 1;
263 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
264 "**>> Block %d needs retiring", flash_block);
267 /* Delete the chunk */
268 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
269 yaffs_skip_rest_of_block(dev);
277 * Simple hash function. Needs to have a reasonable spread
280 static inline int yaffs_hash_fn(int n)
283 return n % YAFFS_NOBJECT_BUCKETS;
287 * Access functions to useful fake objects.
288 * Note that root might have a presence in NAND if permissions are set.
291 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
293 return dev->root_dir;
296 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
298 return dev->lost_n_found;
302 * Erased NAND checking functions
305 int yaffs_check_ff(u8 *buffer, int n_bytes)
307 /* Horrible, slow implementation */
316 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
318 int retval = YAFFS_OK;
319 u8 *data = yaffs_get_temp_buffer(dev);
320 struct yaffs_ext_tags tags;
323 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
325 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
328 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
330 yaffs_trace(YAFFS_TRACE_NANDACCESS,
331 "Chunk %d not erased", nand_chunk);
335 yaffs_release_temp_buffer(dev, data);
341 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
344 struct yaffs_ext_tags *tags)
346 int retval = YAFFS_OK;
347 struct yaffs_ext_tags temp_tags;
348 u8 *buffer = yaffs_get_temp_buffer(dev);
351 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
352 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
353 temp_tags.obj_id != tags->obj_id ||
354 temp_tags.chunk_id != tags->chunk_id ||
355 temp_tags.n_bytes != tags->n_bytes)
358 yaffs_release_temp_buffer(dev, buffer);
364 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
367 int reserved_blocks = dev->param.n_reserved_blocks;
370 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
373 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
375 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
378 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
381 struct yaffs_block_info *bi;
383 if (dev->n_erased_blocks < 1) {
384 /* Hoosterman we've got a problem.
385 * Can't get space to gc
387 yaffs_trace(YAFFS_TRACE_ERROR,
388 "yaffs tragedy: no more erased blocks");
393 /* Find an empty block. */
395 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
396 dev->alloc_block_finder++;
397 if (dev->alloc_block_finder < dev->internal_start_block
398 || dev->alloc_block_finder > dev->internal_end_block) {
399 dev->alloc_block_finder = dev->internal_start_block;
402 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
404 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
405 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
407 bi->seq_number = dev->seq_number;
408 dev->n_erased_blocks--;
409 yaffs_trace(YAFFS_TRACE_ALLOCATE,
410 "Allocated block %d, seq %d, %d left" ,
411 dev->alloc_block_finder, dev->seq_number,
412 dev->n_erased_blocks);
413 return dev->alloc_block_finder;
417 yaffs_trace(YAFFS_TRACE_ALWAYS,
418 "yaffs tragedy: no more erased blocks, but there should have been %d",
419 dev->n_erased_blocks);
424 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
425 struct yaffs_block_info **block_ptr)
428 struct yaffs_block_info *bi;
430 if (dev->alloc_block < 0) {
431 /* Get next block to allocate off */
432 dev->alloc_block = yaffs_find_alloc_block(dev);
436 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
437 /* No space unless we're allowed to use the reserve. */
441 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
442 && dev->alloc_page == 0)
443 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
445 /* Next page please.... */
446 if (dev->alloc_block >= 0) {
447 bi = yaffs_get_block_info(dev, dev->alloc_block);
449 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
452 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
456 dev->n_free_chunks--;
458 /* If the block is full set the state to full */
459 if (dev->alloc_page >= dev->param.chunks_per_block) {
460 bi->block_state = YAFFS_BLOCK_STATE_FULL;
461 dev->alloc_block = -1;
470 yaffs_trace(YAFFS_TRACE_ERROR,
471 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
476 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
480 n = dev->n_erased_blocks * dev->param.chunks_per_block;
482 if (dev->alloc_block > 0)
483 n += (dev->param.chunks_per_block - dev->alloc_page);
490 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
491 * if we don't want to write to it.
493 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
495 struct yaffs_block_info *bi;
497 if (dev->alloc_block > 0) {
498 bi = yaffs_get_block_info(dev, dev->alloc_block);
499 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
500 bi->block_state = YAFFS_BLOCK_STATE_FULL;
501 dev->alloc_block = -1;
506 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
508 struct yaffs_ext_tags *tags, int use_reserver)
514 yaffs2_checkpt_invalidate(dev);
517 struct yaffs_block_info *bi = 0;
520 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
526 /* First check this chunk is erased, if it needs
527 * checking. The checking policy (unless forced
528 * always on) is as follows:
530 * Check the first page we try to write in a block.
531 * If the check passes then we don't need to check any
532 * more. If the check fails, we check again...
533 * If the block has been erased, we don't need to check.
535 * However, if the block has been prioritised for gc,
536 * then we think there might be something odd about
537 * this block and stop using it.
539 * Rationale: We should only ever see chunks that have
540 * not been erased if there was a partially written
541 * chunk due to power loss. This checking policy should
542 * catch that case with very few checks and thus save a
543 * lot of checks that are most likely not needed.
546 * If an erase check fails or the write fails we skip the
550 /* let's give it a try */
553 if (dev->param.always_check_erased)
554 bi->skip_erased_check = 0;
556 if (!bi->skip_erased_check) {
557 erased_ok = yaffs_check_chunk_erased(dev, chunk);
558 if (erased_ok != YAFFS_OK) {
559 yaffs_trace(YAFFS_TRACE_ERROR,
560 "**>> yaffs chunk %d was not erased",
563 /* If not erased, delete this one,
564 * skip rest of block and
565 * try another chunk */
566 yaffs_chunk_del(dev, chunk, 1, __LINE__);
567 yaffs_skip_rest_of_block(dev);
572 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
574 if (!bi->skip_erased_check)
576 yaffs_verify_chunk_written(dev, chunk, data, tags);
578 if (write_ok != YAFFS_OK) {
579 /* Clean up aborted write, skip to next block and
580 * try another chunk */
581 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
585 bi->skip_erased_check = 1;
587 /* Copy the data into the robustification buffer */
588 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
590 } while (write_ok != YAFFS_OK &&
591 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
597 yaffs_trace(YAFFS_TRACE_ERROR,
598 "**>> yaffs write required %d attempts",
600 dev->n_retired_writes += (attempts - 1);
607 * Block retiring for handling a broken block.
610 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
612 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
614 yaffs2_checkpt_invalidate(dev);
616 yaffs2_clear_oldest_dirty_seq(dev, bi);
618 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
619 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
620 yaffs_trace(YAFFS_TRACE_ALWAYS,
621 "yaffs: Failed to mark bad and erase block %d",
624 struct yaffs_ext_tags tags;
626 flash_block * dev->param.chunks_per_block;
628 u8 *buffer = yaffs_get_temp_buffer(dev);
630 memset(buffer, 0xff, dev->data_bytes_per_chunk);
631 memset(&tags, 0, sizeof(tags));
632 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
633 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
637 yaffs_trace(YAFFS_TRACE_ALWAYS,
638 "yaffs: Failed to write bad block marker to block %d",
641 yaffs_release_temp_buffer(dev, buffer);
645 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
646 bi->gc_prioritise = 0;
647 bi->needs_retiring = 0;
649 dev->n_retired_blocks++;
652 /*---------------- Name handling functions ------------*/
654 static u16 yaffs_calc_name_sum(const YCHAR *name)
662 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
664 /* 0x1f mask is case insensitive */
665 sum += ((*name) & 0x1f) * i;
672 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
674 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
675 memset(obj->short_name, 0, sizeof(obj->short_name));
677 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
678 YAFFS_SHORT_NAME_LENGTH)
679 strcpy(obj->short_name, name);
681 obj->short_name[0] = _Y('\0');
683 obj->sum = yaffs_calc_name_sum(name);
686 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
687 const struct yaffs_obj_hdr *oh)
689 #ifdef CONFIG_YAFFS_AUTO_UNICODE
690 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
691 memset(tmp_name, 0, sizeof(tmp_name));
692 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
693 YAFFS_MAX_NAME_LENGTH + 1);
694 yaffs_set_obj_name(obj, tmp_name);
696 yaffs_set_obj_name(obj, oh->name);
700 /*-------------------- TNODES -------------------
702 * List of spare tnodes
703 * The list is hooked together using the first pointer
707 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
709 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
712 memset(tn, 0, dev->tnode_size);
716 dev->checkpoint_blocks_required = 0; /* force recalculation */
721 /* FreeTnode frees up a tnode and puts it back on the free list */
722 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
724 yaffs_free_raw_tnode(dev, tn);
726 dev->checkpoint_blocks_required = 0; /* force recalculation */
729 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
731 yaffs_deinit_raw_tnodes_and_objs(dev);
736 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
737 unsigned pos, unsigned val)
739 u32 *map = (u32 *) tn;
745 pos &= YAFFS_TNODES_LEVEL0_MASK;
746 val >>= dev->chunk_grp_bits;
748 bit_in_map = pos * dev->tnode_width;
749 word_in_map = bit_in_map / 32;
750 bit_in_word = bit_in_map & (32 - 1);
752 mask = dev->tnode_mask << bit_in_word;
754 map[word_in_map] &= ~mask;
755 map[word_in_map] |= (mask & (val << bit_in_word));
757 if (dev->tnode_width > (32 - bit_in_word)) {
758 bit_in_word = (32 - bit_in_word);
761 dev->tnode_mask >> bit_in_word;
762 map[word_in_map] &= ~mask;
763 map[word_in_map] |= (mask & (val >> bit_in_word));
767 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
770 u32 *map = (u32 *) tn;
776 pos &= YAFFS_TNODES_LEVEL0_MASK;
778 bit_in_map = pos * dev->tnode_width;
779 word_in_map = bit_in_map / 32;
780 bit_in_word = bit_in_map & (32 - 1);
782 val = map[word_in_map] >> bit_in_word;
784 if (dev->tnode_width > (32 - bit_in_word)) {
785 bit_in_word = (32 - bit_in_word);
787 val |= (map[word_in_map] << bit_in_word);
790 val &= dev->tnode_mask;
791 val <<= dev->chunk_grp_bits;
796 /* ------------------- End of individual tnode manipulation -----------------*/
798 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
799 * The look up tree is represented by the top tnode and the number of top_level
800 * in the tree. 0 means only the level 0 tnode is in the tree.
803 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
804 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
805 struct yaffs_file_var *file_struct,
808 struct yaffs_tnode *tn = file_struct->top;
811 int level = file_struct->top_level;
815 /* Check sane level and chunk Id */
816 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
819 if (chunk_id > YAFFS_MAX_CHUNK_ID)
822 /* First check we're tall enough (ie enough top_level) */
824 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
827 i >>= YAFFS_TNODES_INTERNAL_BITS;
831 if (required_depth > file_struct->top_level)
832 return NULL; /* Not tall enough, so we can't find it */
834 /* Traverse down to level 0 */
835 while (level > 0 && tn) {
836 tn = tn->internal[(chunk_id >>
837 (YAFFS_TNODES_LEVEL0_BITS +
839 YAFFS_TNODES_INTERNAL_BITS)) &
840 YAFFS_TNODES_INTERNAL_MASK];
847 /* add_find_tnode_0 finds the level 0 tnode if it exists,
848 * otherwise first expands the tree.
849 * This happens in two steps:
850 * 1. If the tree isn't tall enough, then make it taller.
851 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
853 * Used when modifying the tree.
855 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
856 * specified tn will be plugged into the ttree.
859 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
860 struct yaffs_file_var *file_struct,
862 struct yaffs_tnode *passed_tn)
867 struct yaffs_tnode *tn;
870 /* Check sane level and page Id */
871 if (file_struct->top_level < 0 ||
872 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
875 if (chunk_id > YAFFS_MAX_CHUNK_ID)
878 /* First check we're tall enough (ie enough top_level) */
880 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
883 x >>= YAFFS_TNODES_INTERNAL_BITS;
887 if (required_depth > file_struct->top_level) {
888 /* Not tall enough, gotta make the tree taller */
889 for (i = file_struct->top_level; i < required_depth; i++) {
891 tn = yaffs_get_tnode(dev);
894 tn->internal[0] = file_struct->top;
895 file_struct->top = tn;
896 file_struct->top_level++;
898 yaffs_trace(YAFFS_TRACE_ERROR,
899 "yaffs: no more tnodes");
905 /* Traverse down to level 0, adding anything we need */
907 l = file_struct->top_level;
908 tn = file_struct->top;
911 while (l > 0 && tn) {
913 (YAFFS_TNODES_LEVEL0_BITS +
914 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
915 YAFFS_TNODES_INTERNAL_MASK;
917 if ((l > 1) && !tn->internal[x]) {
918 /* Add missing non-level-zero tnode */
919 tn->internal[x] = yaffs_get_tnode(dev);
920 if (!tn->internal[x])
923 /* Looking from level 1 at level 0 */
925 /* If we already have one, release it */
927 yaffs_free_tnode(dev,
929 tn->internal[x] = passed_tn;
931 } else if (!tn->internal[x]) {
932 /* Don't have one, none passed in */
933 tn->internal[x] = yaffs_get_tnode(dev);
934 if (!tn->internal[x])
939 tn = tn->internal[x];
945 memcpy(tn, passed_tn,
946 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
947 yaffs_free_tnode(dev, passed_tn);
954 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
957 return (tags->chunk_id == chunk_obj &&
958 tags->obj_id == obj_id &&
959 !tags->is_deleted) ? 1 : 0;
963 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
964 struct yaffs_ext_tags *tags, int obj_id,
969 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
970 if (yaffs_check_chunk_bit
971 (dev, the_chunk / dev->param.chunks_per_block,
972 the_chunk % dev->param.chunks_per_block)) {
974 if (dev->chunk_grp_size == 1)
977 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
979 if (yaffs_tags_match(tags,
980 obj_id, inode_chunk)) {
991 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
992 struct yaffs_ext_tags *tags)
994 /*Get the Tnode, then get the level 0 offset chunk offset */
995 struct yaffs_tnode *tn;
997 struct yaffs_ext_tags local_tags;
999 struct yaffs_dev *dev = in->my_dev;
1002 /* Passed a NULL, so use our own tags space */
1006 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1011 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1013 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1018 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1019 struct yaffs_ext_tags *tags)
1021 /* Get the Tnode, then get the level 0 offset chunk offset */
1022 struct yaffs_tnode *tn;
1024 struct yaffs_ext_tags local_tags;
1025 struct yaffs_dev *dev = in->my_dev;
1029 /* Passed a NULL, so use our own tags space */
1033 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1038 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1040 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1043 /* Delete the entry in the filestructure (if found) */
1045 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1050 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1051 int nand_chunk, int in_scan)
1053 /* NB in_scan is zero unless scanning.
1054 * For forward scanning, in_scan is > 0;
1055 * for backward scanning in_scan is < 0
1057 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1060 struct yaffs_tnode *tn;
1061 struct yaffs_dev *dev = in->my_dev;
1063 struct yaffs_ext_tags existing_tags;
1064 struct yaffs_ext_tags new_tags;
1065 unsigned existing_serial, new_serial;
1067 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1068 /* Just ignore an attempt at putting a chunk into a non-file
1070 * If it is not during Scanning then something went wrong!
1073 yaffs_trace(YAFFS_TRACE_ERROR,
1074 "yaffs tragedy:attempt to put data chunk into a non-file"
1079 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1083 tn = yaffs_add_find_tnode_0(dev,
1084 &in->variant.file_variant,
1090 /* Dummy insert, bail now */
1093 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1096 /* If we're scanning then we need to test for duplicates
1097 * NB This does not need to be efficient since it should only
1098 * happen when the power fails during a write, then only one
1099 * chunk should ever be affected.
1101 * Correction for YAFFS2: This could happen quite a lot and we
1102 * need to think about efficiency! TODO
1103 * Update: For backward scanning we don't need to re-read tags
1104 * so this is quite cheap.
1107 if (existing_cunk > 0) {
1108 /* NB Right now existing chunk will not be real
1109 * chunk_id if the chunk group size > 1
1110 * thus we have to do a FindChunkInFile to get the
1113 * We have a duplicate now we need to decide which
1116 * Backwards scanning YAFFS2: The old one is what
1117 * we use, dump the new one.
1118 * YAFFS1: Get both sets of tags and compare serial
1123 /* Only do this for forward scanning */
1124 yaffs_rd_chunk_tags_nand(dev,
1128 /* Do a proper find */
1130 yaffs_find_chunk_in_file(in, inode_chunk,
1134 if (existing_cunk <= 0) {
1135 /*Hoosterman - how did this happen? */
1137 yaffs_trace(YAFFS_TRACE_ERROR,
1138 "yaffs tragedy: existing chunk < 0 in scan"
1143 /* NB The deleted flags should be false, otherwise
1144 * the chunks will not be loaded during a scan
1148 new_serial = new_tags.serial_number;
1149 existing_serial = existing_tags.serial_number;
1152 if ((in_scan > 0) &&
1153 (existing_cunk <= 0 ||
1154 ((existing_serial + 1) & 3) == new_serial)) {
1155 /* Forward scanning.
1157 * Delete the old one and drop through to
1160 yaffs_chunk_del(dev, existing_cunk, 1,
1163 /* Backward scanning or we want to use the
1165 * Delete the new one and return early so that
1166 * the tnode isn't changed
1168 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1175 if (existing_cunk == 0)
1176 in->n_data_chunks++;
1178 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1183 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1185 struct yaffs_block_info *the_block;
1188 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1190 block_no = chunk / dev->param.chunks_per_block;
1191 the_block = yaffs_get_block_info(dev, block_no);
1193 the_block->soft_del_pages++;
1194 dev->n_free_chunks++;
1195 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1199 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1200 * the chunks in the file.
1201 * All soft deleting does is increment the block's softdelete count and pulls
1202 * the chunk out of the tnode.
1203 * Thus, essentially this is the same as DeleteWorker except that the chunks
1207 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1208 u32 level, int chunk_offset)
1213 struct yaffs_dev *dev = in->my_dev;
1219 for (i = YAFFS_NTNODES_INTERNAL - 1;
1222 if (tn->internal[i]) {
1224 yaffs_soft_del_worker(in,
1228 YAFFS_TNODES_INTERNAL_BITS)
1231 yaffs_free_tnode(dev,
1233 tn->internal[i] = NULL;
1235 /* Can this happen? */
1239 return (all_done) ? 1 : 0;
1243 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1244 the_chunk = yaffs_get_group_base(dev, tn, i);
1246 yaffs_soft_del_chunk(dev, the_chunk);
1247 yaffs_load_tnode_0(dev, tn, i, 0);
1253 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1255 struct yaffs_dev *dev = obj->my_dev;
1256 struct yaffs_obj *parent;
1258 yaffs_verify_obj_in_dir(obj);
1259 parent = obj->parent;
1261 yaffs_verify_dir(parent);
1263 if (dev && dev->param.remove_obj_fn)
1264 dev->param.remove_obj_fn(obj);
1266 list_del_init(&obj->siblings);
1269 yaffs_verify_dir(parent);
1272 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1275 yaffs_trace(YAFFS_TRACE_ALWAYS,
1276 "tragedy: Trying to add an object to a null pointer directory"
1281 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1282 yaffs_trace(YAFFS_TRACE_ALWAYS,
1283 "tragedy: Trying to add an object to a non-directory"
1288 if (obj->siblings.prev == NULL) {
1289 /* Not initialised */
1293 yaffs_verify_dir(directory);
1295 yaffs_remove_obj_from_dir(obj);
1298 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1299 obj->parent = directory;
1301 if (directory == obj->my_dev->unlinked_dir
1302 || directory == obj->my_dev->del_dir) {
1304 obj->my_dev->n_unlinked_files++;
1305 obj->rename_allowed = 0;
1308 yaffs_verify_dir(directory);
1309 yaffs_verify_obj_in_dir(obj);
1312 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1313 struct yaffs_obj *new_dir,
1314 const YCHAR *new_name, int force, int shadows)
1318 struct yaffs_obj *existing_target;
1320 if (new_dir == NULL)
1321 new_dir = obj->parent; /* use the old directory */
1323 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1324 yaffs_trace(YAFFS_TRACE_ALWAYS,
1325 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1330 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1331 del_op = (new_dir == obj->my_dev->del_dir);
1333 existing_target = yaffs_find_by_name(new_dir, new_name);
1335 /* If the object is a file going into the unlinked directory,
1336 * then it is OK to just stuff it in since duplicate names are OK.
1337 * else only proceed if the new name does not exist and we're putting
1338 * it into a directory.
1340 if (!(unlink_op || del_op || force ||
1341 shadows > 0 || !existing_target) ||
1342 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1345 yaffs_set_obj_name(obj, new_name);
1347 yaffs_add_obj_to_dir(new_dir, obj);
1352 /* If it is a deletion then we mark it as a shrink for gc */
1353 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1359 /*------------------------ Short Operations Cache ------------------------------
1360 * In many situations where there is no high level buffering a lot of
1361 * reads might be short sequential reads, and a lot of writes may be short
1362 * sequential writes. eg. scanning/writing a jpeg file.
1363 * In these cases, a short read/write cache can provide a huge perfomance
1364 * benefit with dumb-as-a-rock code.
1365 * In Linux, the page cache provides read buffering and the short op cache
1366 * provides write buffering.
1368 * There are a small number (~10) of cache chunks per device so that we don't
1369 * need a very intelligent search.
1372 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1374 struct yaffs_dev *dev = obj->my_dev;
1376 struct yaffs_cache *cache;
1377 int n_caches = obj->my_dev->param.n_caches;
1379 for (i = 0; i < n_caches; i++) {
1380 cache = &dev->cache[i];
1381 if (cache->object == obj && cache->dirty)
1388 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1390 struct yaffs_dev *dev = obj->my_dev;
1391 int lowest = -99; /* Stop compiler whining. */
1393 struct yaffs_cache *cache;
1394 int chunk_written = 0;
1395 int n_caches = obj->my_dev->param.n_caches;
1402 /* Find the lowest dirty chunk for this object */
1403 for (i = 0; i < n_caches; i++) {
1404 if (dev->cache[i].object == obj &&
1405 dev->cache[i].dirty) {
1407 dev->cache[i].chunk_id < lowest) {
1408 cache = &dev->cache[i];
1409 lowest = cache->chunk_id;
1414 if (cache && !cache->locked) {
1415 /* Write it out and free it up */
1417 yaffs_wr_data_obj(cache->object,
1422 cache->object = NULL;
1424 } while (cache && chunk_written > 0);
1427 /* Hoosterman, disk full while writing cache out. */
1428 yaffs_trace(YAFFS_TRACE_ERROR,
1429 "yaffs tragedy: no space during cache write");
1432 /*yaffs_flush_whole_cache(dev)
1437 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1439 struct yaffs_obj *obj;
1440 int n_caches = dev->param.n_caches;
1443 /* Find a dirty object in the cache and flush it...
1444 * until there are no further dirty objects.
1448 for (i = 0; i < n_caches && !obj; i++) {
1449 if (dev->cache[i].object && dev->cache[i].dirty)
1450 obj = dev->cache[i].object;
1453 yaffs_flush_file_cache(obj);
1458 /* Grab us a cache chunk for use.
1459 * First look for an empty one.
1460 * Then look for the least recently used non-dirty one.
1461 * Then look for the least recently used dirty one...., flush and look again.
1463 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1467 if (dev->param.n_caches > 0) {
1468 for (i = 0; i < dev->param.n_caches; i++) {
1469 if (!dev->cache[i].object)
1470 return &dev->cache[i];
1476 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1478 struct yaffs_cache *cache;
1479 struct yaffs_obj *the_obj;
1484 if (dev->param.n_caches < 1)
1487 /* Try find a non-dirty one... */
1489 cache = yaffs_grab_chunk_worker(dev);
1492 /* They were all dirty, find the LRU object and flush
1493 * its cache, then find again.
1494 * NB what's here is not very accurate,
1495 * we actually flush the object with the LRU chunk.
1498 /* With locking we can't assume we can use entry zero,
1499 * Set the_obj to a valid pointer for Coverity. */
1500 the_obj = dev->cache[0].object;
1505 for (i = 0; i < dev->param.n_caches; i++) {
1506 if (dev->cache[i].object &&
1507 !dev->cache[i].locked &&
1508 (dev->cache[i].last_use < usage ||
1510 usage = dev->cache[i].last_use;
1511 the_obj = dev->cache[i].object;
1512 cache = &dev->cache[i];
1517 if (!cache || cache->dirty) {
1518 /* Flush and try again */
1519 yaffs_flush_file_cache(the_obj);
1520 cache = yaffs_grab_chunk_worker(dev);
1526 /* Find a cached chunk */
1527 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1530 struct yaffs_dev *dev = obj->my_dev;
1533 if (dev->param.n_caches < 1)
1536 for (i = 0; i < dev->param.n_caches; i++) {
1537 if (dev->cache[i].object == obj &&
1538 dev->cache[i].chunk_id == chunk_id) {
1541 return &dev->cache[i];
1547 /* Mark the chunk for the least recently used algorithym */
1548 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1553 if (dev->param.n_caches < 1)
1556 if (dev->cache_last_use < 0 ||
1557 dev->cache_last_use > 100000000) {
1558 /* Reset the cache usages */
1559 for (i = 1; i < dev->param.n_caches; i++)
1560 dev->cache[i].last_use = 0;
1562 dev->cache_last_use = 0;
1564 dev->cache_last_use++;
1565 cache->last_use = dev->cache_last_use;
1571 /* Invalidate a single cache page.
1572 * Do this when a whole page gets written,
1573 * ie the short cache for this page is no longer valid.
1575 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1577 struct yaffs_cache *cache;
1579 if (object->my_dev->param.n_caches > 0) {
1580 cache = yaffs_find_chunk_cache(object, chunk_id);
1583 cache->object = NULL;
1587 /* Invalidate all the cache pages associated with this object
1588 * Do this whenever ther file is deleted or resized.
1590 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1593 struct yaffs_dev *dev = in->my_dev;
1595 if (dev->param.n_caches > 0) {
1596 /* Invalidate it. */
1597 for (i = 0; i < dev->param.n_caches; i++) {
1598 if (dev->cache[i].object == in)
1599 dev->cache[i].object = NULL;
1604 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1607 struct yaffs_dev *dev = obj->my_dev;
1609 /* If it is still linked into the bucket list, free from the list */
1610 if (!list_empty(&obj->hash_link)) {
1611 list_del_init(&obj->hash_link);
1612 bucket = yaffs_hash_fn(obj->obj_id);
1613 dev->obj_bucket[bucket].count--;
1617 /* FreeObject frees up a Object and puts it back on the free list */
1618 static void yaffs_free_obj(struct yaffs_obj *obj)
1620 struct yaffs_dev *dev;
1627 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1628 obj, obj->my_inode);
1631 if (!list_empty(&obj->siblings))
1634 if (obj->my_inode) {
1635 /* We're still hooked up to a cached inode.
1636 * Don't delete now, but mark for later deletion
1638 obj->defered_free = 1;
1642 yaffs_unhash_obj(obj);
1644 yaffs_free_raw_obj(dev, obj);
1646 dev->checkpoint_blocks_required = 0; /* force recalculation */
1649 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1651 if (obj->defered_free)
1652 yaffs_free_obj(obj);
1655 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1657 /* Iinvalidate the file's data in the cache, without flushing. */
1658 yaffs_invalidate_whole_cache(in);
1660 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1661 /* Move to unlinked directory so we have a deletion record */
1662 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1666 yaffs_remove_obj_from_dir(in);
1667 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1675 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1677 if (!obj->deleted ||
1678 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1682 if (obj->n_data_chunks <= 0) {
1683 /* Empty file with no duplicate object headers,
1684 * just delete it immediately */
1685 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1686 obj->variant.file_variant.top = NULL;
1687 yaffs_trace(YAFFS_TRACE_TRACING,
1688 "yaffs: Deleting empty file %d",
1690 yaffs_generic_obj_del(obj);
1692 yaffs_soft_del_worker(obj,
1693 obj->variant.file_variant.top,
1695 file_variant.top_level, 0);
1700 /* Pruning removes any part of the file structure tree that is beyond the
1701 * bounds of the file (ie that does not point to chunks).
1703 * A file should only get pruned when its size is reduced.
1705 * Before pruning, the chunks must be pulled from the tree and the
1706 * level 0 tnode entries must be zeroed out.
1707 * Could also use this for file deletion, but that's probably better handled
1708 * by a special case.
1710 * This function is recursive. For levels > 0 the function is called again on
1711 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1712 * If there is no data in a subtree then it is pruned.
1715 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1716 struct yaffs_tnode *tn, u32 level,
1728 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1729 if (tn->internal[i]) {
1731 yaffs_prune_worker(dev,
1734 (i == 0) ? del0 : 1);
1737 if (tn->internal[i])
1741 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1742 u32 *map = (u32 *) tn;
1744 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1750 if (has_data == 0 && del0) {
1751 /* Free and return NULL */
1752 yaffs_free_tnode(dev, tn);
1758 static int yaffs_prune_tree(struct yaffs_dev *dev,
1759 struct yaffs_file_var *file_struct)
1764 struct yaffs_tnode *tn;
1766 if (file_struct->top_level < 1)
1770 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1772 /* Now we have a tree with all the non-zero branches NULL but
1773 * the height is the same as it was.
1774 * Let's see if we can trim internal tnodes to shorten the tree.
1775 * We can do this if only the 0th element in the tnode is in use
1776 * (ie all the non-zero are NULL)
1779 while (file_struct->top_level && !done) {
1780 tn = file_struct->top;
1783 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1784 if (tn->internal[i])
1789 file_struct->top = tn->internal[0];
1790 file_struct->top_level--;
1791 yaffs_free_tnode(dev, tn);
1800 /*-------------------- End of File Structure functions.-------------------*/
1802 /* alloc_empty_obj gets us a clean Object.*/
1803 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1805 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1812 /* Now sweeten it up... */
1814 memset(obj, 0, sizeof(struct yaffs_obj));
1815 obj->being_created = 1;
1819 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1820 INIT_LIST_HEAD(&(obj->hard_links));
1821 INIT_LIST_HEAD(&(obj->hash_link));
1822 INIT_LIST_HEAD(&obj->siblings);
1824 /* Now make the directory sane */
1825 if (dev->root_dir) {
1826 obj->parent = dev->root_dir;
1827 list_add(&(obj->siblings),
1828 &dev->root_dir->variant.dir_variant.children);
1831 /* Add it to the lost and found directory.
1832 * NB Can't put root or lost-n-found in lost-n-found so
1833 * check if lost-n-found exists first
1835 if (dev->lost_n_found)
1836 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1838 obj->being_created = 0;
1840 dev->checkpoint_blocks_required = 0; /* force recalculation */
1845 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1849 int lowest = 999999;
1851 /* Search for the shortest list or one that
1855 for (i = 0; i < 10 && lowest > 4; i++) {
1856 dev->bucket_finder++;
1857 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1858 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1859 lowest = dev->obj_bucket[dev->bucket_finder].count;
1860 l = dev->bucket_finder;
1867 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1869 int bucket = yaffs_find_nice_bucket(dev);
1871 struct list_head *i;
1872 u32 n = (u32) bucket;
1874 /* Now find an object value that has not already been taken
1875 * by scanning the list.
1880 n += YAFFS_NOBJECT_BUCKETS;
1881 if (1 || dev->obj_bucket[bucket].count > 0) {
1882 list_for_each(i, &dev->obj_bucket[bucket].list) {
1883 /* If there is already one in the list */
1884 if (i && list_entry(i, struct yaffs_obj,
1885 hash_link)->obj_id == n) {
1894 static void yaffs_hash_obj(struct yaffs_obj *in)
1896 int bucket = yaffs_hash_fn(in->obj_id);
1897 struct yaffs_dev *dev = in->my_dev;
1899 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1900 dev->obj_bucket[bucket].count++;
1903 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1905 int bucket = yaffs_hash_fn(number);
1906 struct list_head *i;
1907 struct yaffs_obj *in;
1909 list_for_each(i, &dev->obj_bucket[bucket].list) {
1910 /* Look if it is in the list */
1911 in = list_entry(i, struct yaffs_obj, hash_link);
1912 if (in->obj_id == number) {
1913 /* Don't show if it is defered free */
1914 if (in->defered_free)
1923 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1924 enum yaffs_obj_type type)
1926 struct yaffs_obj *the_obj = NULL;
1927 struct yaffs_tnode *tn = NULL;
1930 number = yaffs_new_obj_id(dev);
1932 if (type == YAFFS_OBJECT_TYPE_FILE) {
1933 tn = yaffs_get_tnode(dev);
1938 the_obj = yaffs_alloc_empty_obj(dev);
1941 yaffs_free_tnode(dev, tn);
1946 the_obj->rename_allowed = 1;
1947 the_obj->unlink_allowed = 1;
1948 the_obj->obj_id = number;
1949 yaffs_hash_obj(the_obj);
1950 the_obj->variant_type = type;
1951 yaffs_load_current_time(the_obj, 1, 1);
1954 case YAFFS_OBJECT_TYPE_FILE:
1955 the_obj->variant.file_variant.file_size = 0;
1956 the_obj->variant.file_variant.scanned_size = 0;
1957 the_obj->variant.file_variant.shrink_size = ~0; /* max */
1958 the_obj->variant.file_variant.top_level = 0;
1959 the_obj->variant.file_variant.top = tn;
1961 case YAFFS_OBJECT_TYPE_DIRECTORY:
1962 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1963 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1965 case YAFFS_OBJECT_TYPE_SYMLINK:
1966 case YAFFS_OBJECT_TYPE_HARDLINK:
1967 case YAFFS_OBJECT_TYPE_SPECIAL:
1968 /* No action required */
1970 case YAFFS_OBJECT_TYPE_UNKNOWN:
1971 /* todo this should not happen */
1977 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1978 int number, u32 mode)
1981 struct yaffs_obj *obj =
1982 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1987 obj->fake = 1; /* it is fake so it might not use NAND */
1988 obj->rename_allowed = 0;
1989 obj->unlink_allowed = 0;
1992 obj->yst_mode = mode;
1994 obj->hdr_chunk = 0; /* Not a valid chunk. */
2000 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2006 yaffs_init_raw_tnodes_and_objs(dev);
2008 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2009 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2010 dev->obj_bucket[i].count = 0;
2014 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2016 enum yaffs_obj_type type)
2018 struct yaffs_obj *the_obj = NULL;
2021 the_obj = yaffs_find_by_number(dev, number);
2024 the_obj = yaffs_new_obj(dev, number, type);
2030 YCHAR *yaffs_clone_str(const YCHAR *str)
2032 YCHAR *new_str = NULL;
2038 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2039 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2041 strncpy(new_str, str, len);
2048 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2049 * link (ie. name) is created or deleted in the directory.
2052 * create dir/a : update dir's mtime/ctime
2053 * rm dir/a: update dir's mtime/ctime
2054 * modify dir/a: don't update dir's mtimme/ctime
2056 * This can be handled immediately or defered. Defering helps reduce the number
2057 * of updates when many files in a directory are changed within a brief period.
2059 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2060 * called periodically.
2063 static void yaffs_update_parent(struct yaffs_obj *obj)
2065 struct yaffs_dev *dev;
2071 yaffs_load_current_time(obj, 0, 1);
2072 if (dev->param.defered_dir_update) {
2073 struct list_head *link = &obj->variant.dir_variant.dirty;
2075 if (list_empty(link)) {
2076 list_add(link, &dev->dirty_dirs);
2077 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2078 "Added object %d to dirty directories",
2083 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2087 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2089 struct list_head *link;
2090 struct yaffs_obj *obj;
2091 struct yaffs_dir_var *d_s;
2092 union yaffs_obj_var *o_v;
2094 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2096 while (!list_empty(&dev->dirty_dirs)) {
2097 link = dev->dirty_dirs.next;
2098 list_del_init(link);
2100 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2101 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2102 obj = list_entry(o_v, struct yaffs_obj, variant);
2104 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2108 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2113 * Mknod (create) a new object.
2114 * equiv_obj only has meaning for a hard link;
2115 * alias_str only has meaning for a symlink.
2116 * rdev only has meaning for devices (a subset of special objects)
2119 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2120 struct yaffs_obj *parent,
2125 struct yaffs_obj *equiv_obj,
2126 const YCHAR *alias_str, u32 rdev)
2128 struct yaffs_obj *in;
2130 struct yaffs_dev *dev = parent->my_dev;
2132 /* Check if the entry exists.
2133 * If it does then fail the call since we don't want a dup. */
2134 if (yaffs_find_by_name(parent, name))
2137 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2138 str = yaffs_clone_str(alias_str);
2143 in = yaffs_new_obj(dev, -1, type);
2152 in->variant_type = type;
2154 in->yst_mode = mode;
2156 yaffs_attribs_init(in, gid, uid, rdev);
2158 in->n_data_chunks = 0;
2160 yaffs_set_obj_name(in, name);
2163 yaffs_add_obj_to_dir(parent, in);
2165 in->my_dev = parent->my_dev;
2168 case YAFFS_OBJECT_TYPE_SYMLINK:
2169 in->variant.symlink_variant.alias = str;
2171 case YAFFS_OBJECT_TYPE_HARDLINK:
2172 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2173 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
2174 list_add(&in->hard_links, &equiv_obj->hard_links);
2176 case YAFFS_OBJECT_TYPE_FILE:
2177 case YAFFS_OBJECT_TYPE_DIRECTORY:
2178 case YAFFS_OBJECT_TYPE_SPECIAL:
2179 case YAFFS_OBJECT_TYPE_UNKNOWN:
2184 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2185 /* Could not create the object header, fail */
2191 yaffs_update_parent(parent);
2196 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2197 const YCHAR *name, u32 mode, u32 uid,
2200 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2201 uid, gid, NULL, NULL, 0);
2204 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2205 u32 mode, u32 uid, u32 gid)
2207 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2208 mode, uid, gid, NULL, NULL, 0);
2211 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2212 const YCHAR *name, u32 mode, u32 uid,
2215 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2216 uid, gid, NULL, NULL, rdev);
2219 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2220 const YCHAR *name, u32 mode, u32 uid,
2221 u32 gid, const YCHAR *alias)
2223 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2224 uid, gid, NULL, alias, 0);
2227 /* yaffs_link_obj returns the object id of the equivalent object.*/
2228 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2229 struct yaffs_obj *equiv_obj)
2231 /* Get the real object in case we were fed a hard link obj */
2232 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2234 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2235 parent, name, 0, 0, 0,
2236 equiv_obj, NULL, 0))
2245 /*---------------------- Block Management and Page Allocation -------------*/
2247 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2249 if (dev->block_info_alt && dev->block_info)
2250 vfree(dev->block_info);
2252 kfree(dev->block_info);
2254 dev->block_info_alt = 0;
2256 dev->block_info = NULL;
2258 if (dev->chunk_bits_alt && dev->chunk_bits)
2259 vfree(dev->chunk_bits);
2261 kfree(dev->chunk_bits);
2262 dev->chunk_bits_alt = 0;
2263 dev->chunk_bits = NULL;
2266 static int yaffs_init_blocks(struct yaffs_dev *dev)
2268 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2270 dev->block_info = NULL;
2271 dev->chunk_bits = NULL;
2272 dev->alloc_block = -1; /* force it to get a new one */
2274 /* If the first allocation strategy fails, thry the alternate one */
2276 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2277 if (!dev->block_info) {
2279 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2280 dev->block_info_alt = 1;
2282 dev->block_info_alt = 0;
2285 if (!dev->block_info)
2288 /* Set up dynamic blockinfo stuff. Round up bytes. */
2289 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2291 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2292 if (!dev->chunk_bits) {
2294 vmalloc(dev->chunk_bit_stride * n_blocks);
2295 dev->chunk_bits_alt = 1;
2297 dev->chunk_bits_alt = 0;
2299 if (!dev->chunk_bits)
2303 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2304 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2308 yaffs_deinit_blocks(dev);
2313 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2315 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2319 /* If the block is still healthy erase it and mark as clean.
2320 * If the block has had a data failure, then retire it.
2323 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2324 "yaffs_block_became_dirty block %d state %d %s",
2325 block_no, bi->block_state,
2326 (bi->needs_retiring) ? "needs retiring" : "");
2328 yaffs2_clear_oldest_dirty_seq(dev, bi);
2330 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2332 /* If this is the block being garbage collected then stop gc'ing */
2333 if (block_no == dev->gc_block)
2336 /* If this block is currently the best candidate for gc
2337 * then drop as a candidate */
2338 if (block_no == dev->gc_dirtiest) {
2339 dev->gc_dirtiest = 0;
2340 dev->gc_pages_in_use = 0;
2343 if (!bi->needs_retiring) {
2344 yaffs2_checkpt_invalidate(dev);
2345 erased_ok = yaffs_erase_block(dev, block_no);
2347 dev->n_erase_failures++;
2348 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2349 "**>> Erasure failed %d", block_no);
2353 /* Verify erasure if needed */
2355 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2356 !yaffs_skip_verification(dev))) {
2357 for (i = 0; i < dev->param.chunks_per_block; i++) {
2358 if (!yaffs_check_chunk_erased(dev,
2359 block_no * dev->param.chunks_per_block + i)) {
2360 yaffs_trace(YAFFS_TRACE_ERROR,
2361 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2368 /* We lost a block of free space */
2369 dev->n_free_chunks -= dev->param.chunks_per_block;
2370 yaffs_retire_block(dev, block_no);
2371 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2372 "**>> Block %d retired", block_no);
2376 /* Clean it up... */
2377 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2379 dev->n_erased_blocks++;
2380 bi->pages_in_use = 0;
2381 bi->soft_del_pages = 0;
2382 bi->has_shrink_hdr = 0;
2383 bi->skip_erased_check = 1; /* Clean, so no need to check */
2384 bi->gc_prioritise = 0;
2385 yaffs_clear_chunk_bits(dev, block_no);
2387 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2390 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2391 struct yaffs_block_info *bi,
2392 int old_chunk, u8 *buffer)
2396 struct yaffs_ext_tags tags;
2397 struct yaffs_obj *object;
2399 int ret_val = YAFFS_OK;
2401 memset(&tags, 0, sizeof(tags));
2402 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2404 object = yaffs_find_by_number(dev, tags.obj_id);
2406 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2407 "Collecting chunk in block %d, %d %d %d ",
2408 dev->gc_chunk, tags.obj_id,
2409 tags.chunk_id, tags.n_bytes);
2411 if (object && !yaffs_skip_verification(dev)) {
2412 if (tags.chunk_id == 0)
2415 else if (object->soft_del)
2416 /* Defeat the test */
2417 matching_chunk = old_chunk;
2420 yaffs_find_chunk_in_file
2421 (object, tags.chunk_id,
2424 if (old_chunk != matching_chunk)
2425 yaffs_trace(YAFFS_TRACE_ERROR,
2426 "gc: page in gc mismatch: %d %d %d %d",
2434 yaffs_trace(YAFFS_TRACE_ERROR,
2435 "page %d in gc has no object: %d %d %d ",
2437 tags.obj_id, tags.chunk_id,
2443 object->soft_del && tags.chunk_id != 0) {
2444 /* Data chunk in a soft deleted file,
2446 * It's a soft deleted data chunk,
2447 * No need to copy this, just forget
2448 * about it and fix up the object.
2451 /* Free chunks already includes
2452 * softdeleted chunks, how ever this
2453 * chunk is going to soon be really
2454 * deleted which will increment free
2455 * chunks. We have to decrement free
2456 * chunks so this works out properly.
2458 dev->n_free_chunks--;
2459 bi->soft_del_pages--;
2461 object->n_data_chunks--;
2462 if (object->n_data_chunks <= 0) {
2463 /* remeber to clean up obj */
2464 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2468 } else if (object) {
2469 /* It's either a data chunk in a live
2470 * file or an ObjectHeader, so we're
2472 * NB Need to keep the ObjectHeaders of
2473 * deleted files until the whole file
2474 * has been deleted off
2476 tags.serial_number++;
2479 if (tags.chunk_id == 0) {
2480 /* It is an object Id,
2481 * We need to nuke the
2482 * shrinkheader flags since its
2484 * Also need to clean up
2487 struct yaffs_obj_hdr *oh;
2488 oh = (struct yaffs_obj_hdr *) buffer;
2491 tags.extra_is_shrink = 0;
2492 oh->shadows_obj = 0;
2493 oh->inband_shadowed_obj_id = 0;
2494 tags.extra_shadows = 0;
2496 /* Update file size */
2497 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2499 object->variant.file_variant.file_size;
2500 tags.extra_length = oh->file_size;
2503 yaffs_verify_oh(object, oh, &tags, 1);
2505 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2508 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2511 if (new_chunk < 0) {
2512 ret_val = YAFFS_FAIL;
2515 /* Now fix up the Tnodes etc. */
2517 if (tags.chunk_id == 0) {
2519 object->hdr_chunk = new_chunk;
2520 object->serial = tags.serial_number;
2522 /* It's a data chunk */
2523 yaffs_put_chunk_in_file(object, tags.chunk_id,
2528 if (ret_val == YAFFS_OK)
2529 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2533 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2536 int ret_val = YAFFS_OK;
2538 int is_checkpt_block;
2540 int chunks_before = yaffs_get_erased_chunks(dev);
2542 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2544 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2546 yaffs_trace(YAFFS_TRACE_TRACING,
2547 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2548 block, bi->pages_in_use, bi->has_shrink_hdr,
2551 /*yaffs_verify_free_chunks(dev); */
2553 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2554 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2556 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2558 dev->gc_disable = 1;
2560 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2561 yaffs_trace(YAFFS_TRACE_TRACING,
2562 "Collecting block %d that has no chunks in use",
2564 yaffs_block_became_dirty(dev, block);
2567 u8 *buffer = yaffs_get_temp_buffer(dev);
2569 yaffs_verify_blk(dev, bi, block);
2571 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2572 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2574 for (/* init already done */ ;
2575 ret_val == YAFFS_OK &&
2576 dev->gc_chunk < dev->param.chunks_per_block &&
2577 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2579 dev->gc_chunk++, old_chunk++) {
2580 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2581 /* Page is in use and might need to be copied */
2583 ret_val = yaffs_gc_process_chunk(dev, bi,
2587 yaffs_release_temp_buffer(dev, buffer);
2590 yaffs_verify_collected_blk(dev, bi, block);
2592 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2594 * The gc did not complete. Set block state back to FULL
2595 * because checkpointing does not restore gc.
2597 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2599 /* The gc completed. */
2600 /* Do any required cleanups */
2601 for (i = 0; i < dev->n_clean_ups; i++) {
2602 /* Time to delete the file too */
2603 struct yaffs_obj *object =
2604 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2606 yaffs_free_tnode(dev,
2607 object->variant.file_variant.top);
2608 object->variant.file_variant.top = NULL;
2609 yaffs_trace(YAFFS_TRACE_GC,
2610 "yaffs: About to finally delete object %d",
2612 yaffs_generic_obj_del(object);
2613 object->my_dev->n_deleted_files--;
2617 chunks_after = yaffs_get_erased_chunks(dev);
2618 if (chunks_before >= chunks_after)
2619 yaffs_trace(YAFFS_TRACE_GC,
2620 "gc did not increase free chunks before %d after %d",
2621 chunks_before, chunks_after);
2624 dev->n_clean_ups = 0;
2627 dev->gc_disable = 0;
2633 * find_gc_block() selects the dirtiest block (or close enough)
2634 * for garbage collection.
2637 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2638 int aggressive, int background)
2642 unsigned selected = 0;
2643 int prioritised = 0;
2644 int prioritised_exist = 0;
2645 struct yaffs_block_info *bi;
2648 /* First let's see if we need to grab a prioritised block */
2649 if (dev->has_pending_prioritised_gc && !aggressive) {
2650 dev->gc_dirtiest = 0;
2651 bi = dev->block_info;
2652 for (i = dev->internal_start_block;
2653 i <= dev->internal_end_block && !selected; i++) {
2655 if (bi->gc_prioritise) {
2656 prioritised_exist = 1;
2657 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2658 yaffs_block_ok_for_gc(dev, bi)) {
2667 * If there is a prioritised block and none was selected then
2668 * this happened because there is at least one old dirty block
2669 * gumming up the works. Let's gc the oldest dirty block.
2672 if (prioritised_exist &&
2673 !selected && dev->oldest_dirty_block > 0)
2674 selected = dev->oldest_dirty_block;
2676 if (!prioritised_exist) /* None found, so we can clear this */
2677 dev->has_pending_prioritised_gc = 0;
2680 /* If we're doing aggressive GC then we are happy to take a less-dirty
2681 * block, and search harder.
2682 * else (leasurely gc), then we only bother to do this if the
2683 * block has only a few pages in use.
2689 dev->internal_end_block - dev->internal_start_block + 1;
2691 threshold = dev->param.chunks_per_block;
2692 iterations = n_blocks;
2697 max_threshold = dev->param.chunks_per_block / 2;
2699 max_threshold = dev->param.chunks_per_block / 8;
2701 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2702 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2704 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2705 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2706 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2707 if (threshold > max_threshold)
2708 threshold = max_threshold;
2710 iterations = n_blocks / 16 + 1;
2711 if (iterations > 100)
2717 (dev->gc_dirtiest < 1 ||
2718 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2720 dev->gc_block_finder++;
2721 if (dev->gc_block_finder < dev->internal_start_block ||
2722 dev->gc_block_finder > dev->internal_end_block)
2723 dev->gc_block_finder =
2724 dev->internal_start_block;
2726 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2728 pages_used = bi->pages_in_use - bi->soft_del_pages;
2730 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2731 pages_used < dev->param.chunks_per_block &&
2732 (dev->gc_dirtiest < 1 ||
2733 pages_used < dev->gc_pages_in_use) &&
2734 yaffs_block_ok_for_gc(dev, bi)) {
2735 dev->gc_dirtiest = dev->gc_block_finder;
2736 dev->gc_pages_in_use = pages_used;
2740 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2741 selected = dev->gc_dirtiest;
2745 * If nothing has been selected for a while, try the oldest dirty
2746 * because that's gumming up the works.
2749 if (!selected && dev->param.is_yaffs2 &&
2750 dev->gc_not_done >= (background ? 10 : 20)) {
2751 yaffs2_find_oldest_dirty_seq(dev);
2752 if (dev->oldest_dirty_block > 0) {
2753 selected = dev->oldest_dirty_block;
2754 dev->gc_dirtiest = selected;
2755 dev->oldest_dirty_gc_count++;
2756 bi = yaffs_get_block_info(dev, selected);
2757 dev->gc_pages_in_use =
2758 bi->pages_in_use - bi->soft_del_pages;
2760 dev->gc_not_done = 0;
2765 yaffs_trace(YAFFS_TRACE_GC,
2766 "GC Selected block %d with %d free, prioritised:%d",
2768 dev->param.chunks_per_block - dev->gc_pages_in_use,
2775 dev->gc_dirtiest = 0;
2776 dev->gc_pages_in_use = 0;
2777 dev->gc_not_done = 0;
2778 if (dev->refresh_skip > 0)
2779 dev->refresh_skip--;
2782 yaffs_trace(YAFFS_TRACE_GC,
2783 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2784 dev->gc_block_finder, dev->gc_not_done, threshold,
2785 dev->gc_dirtiest, dev->gc_pages_in_use,
2786 dev->oldest_dirty_block, background ? " bg" : "");
2792 /* New garbage collector
2793 * If we're very low on erased blocks then we do aggressive garbage collection
2794 * otherwise we do "leasurely" garbage collection.
2795 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2796 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2798 * The idea is to help clear out space in a more spread-out manner.
2799 * Dunno if it really does anything useful.
2801 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2804 int gc_ok = YAFFS_OK;
2808 int checkpt_block_adjust;
2810 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2813 if (dev->gc_disable)
2814 /* Bail out so we don't get recursive gc */
2817 /* This loop should pass the first time.
2818 * Only loops here if the collection does not increase space.
2824 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2827 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2829 dev->n_erased_blocks * dev->param.chunks_per_block;
2831 /* If we need a block soon then do aggressive gc. */
2832 if (dev->n_erased_blocks < min_erased)
2836 && erased_chunks > (dev->n_free_chunks / 4))
2839 if (dev->gc_skip > 20)
2841 if (erased_chunks < dev->n_free_chunks / 2 ||
2842 dev->gc_skip < 1 || background)
2852 /* If we don't already have a block being gc'd then see if we
2853 * should start another */
2855 if (dev->gc_block < 1 && !aggressive) {
2856 dev->gc_block = yaffs2_find_refresh_block(dev);
2858 dev->n_clean_ups = 0;
2860 if (dev->gc_block < 1) {
2862 yaffs_find_gc_block(dev, aggressive, background);
2864 dev->n_clean_ups = 0;
2867 if (dev->gc_block > 0) {
2870 dev->passive_gc_count++;
2872 yaffs_trace(YAFFS_TRACE_GC,
2873 "yaffs: GC n_erased_blocks %d aggressive %d",
2874 dev->n_erased_blocks, aggressive);
2876 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2879 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
2880 dev->gc_block > 0) {
2881 yaffs_trace(YAFFS_TRACE_GC,
2882 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2883 dev->n_erased_blocks, max_tries,
2886 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2887 (dev->gc_block > 0) && (max_tries < 2));
2889 return aggressive ? gc_ok : YAFFS_OK;
2894 * Garbage collects. Intended to be called from a background thread.
2895 * Returns non-zero if at least half the free chunks are erased.
2897 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2899 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2901 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2903 yaffs_check_gc(dev, 1);
2904 return erased_chunks > dev->n_free_chunks / 2;
2907 /*-------------------- Data file manipulation -----------------*/
2909 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2911 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2913 if (nand_chunk >= 0)
2914 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2917 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2918 "Chunk %d not found zero instead",
2920 /* get sane (zero) data if you read a hole */
2921 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2927 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2932 struct yaffs_ext_tags tags;
2933 struct yaffs_block_info *bi;
2939 block = chunk_id / dev->param.chunks_per_block;
2940 page = chunk_id % dev->param.chunks_per_block;
2942 if (!yaffs_check_chunk_bit(dev, block, page))
2943 yaffs_trace(YAFFS_TRACE_VERIFY,
2944 "Deleting invalid chunk %d", chunk_id);
2946 bi = yaffs_get_block_info(dev, block);
2948 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2950 yaffs_trace(YAFFS_TRACE_DELETION,
2951 "line %d delete of chunk %d",
2954 if (!dev->param.is_yaffs2 && mark_flash &&
2955 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2957 memset(&tags, 0, sizeof(tags));
2958 tags.is_deleted = 1;
2959 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2960 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2962 dev->n_unmarked_deletions++;
2965 /* Pull out of the management area.
2966 * If the whole block became dirty, this will kick off an erasure.
2968 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2969 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2970 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
2971 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2972 dev->n_free_chunks++;
2973 yaffs_clear_chunk_bit(dev, block, page);
2976 if (bi->pages_in_use == 0 &&
2977 !bi->has_shrink_hdr &&
2978 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2979 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
2980 yaffs_block_became_dirty(dev, block);
2985 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2986 const u8 *buffer, int n_bytes, int use_reserve)
2988 /* Find old chunk Need to do this to get serial number
2989 * Write new one and patch into tree.
2990 * Invalidate old tags.
2994 struct yaffs_ext_tags prev_tags;
2996 struct yaffs_ext_tags new_tags;
2997 struct yaffs_dev *dev = in->my_dev;
2999 yaffs_check_gc(dev, 0);
3001 /* Get the previous chunk at this location in the file if it exists.
3002 * If it does not exist then put a zero into the tree. This creates
3003 * the tnode now, rather than later when it is harder to clean up.
3005 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3006 if (prev_chunk_id < 1 &&
3007 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3010 /* Set up new tags */
3011 memset(&new_tags, 0, sizeof(new_tags));
3013 new_tags.chunk_id = inode_chunk;
3014 new_tags.obj_id = in->obj_id;
3015 new_tags.serial_number =
3016 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3017 new_tags.n_bytes = n_bytes;
3019 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3020 yaffs_trace(YAFFS_TRACE_ERROR,
3021 "Writing %d bytes to chunk!!!!!!!!!",
3027 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3029 if (new_chunk_id > 0) {
3030 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3032 if (prev_chunk_id > 0)
3033 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3035 yaffs_verify_file_sane(in);
3037 return new_chunk_id;
3043 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3044 const YCHAR *name, const void *value, int size,
3047 struct yaffs_xattr_mod xmod;
3055 xmod.result = -ENOSPC;
3057 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3065 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3066 struct yaffs_xattr_mod *xmod)
3069 int x_offs = sizeof(struct yaffs_obj_hdr);
3070 struct yaffs_dev *dev = obj->my_dev;
3071 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3072 char *x_buffer = buffer + x_offs;
3076 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3077 xmod->size, xmod->flags);
3079 retval = nval_del(x_buffer, x_size, xmod->name);
3081 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3082 obj->xattr_known = 1;
3083 xmod->result = retval;
3088 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
3089 void *value, int size)
3091 char *buffer = NULL;
3093 struct yaffs_ext_tags tags;
3094 struct yaffs_dev *dev = obj->my_dev;
3095 int x_offs = sizeof(struct yaffs_obj_hdr);
3096 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3100 if (obj->hdr_chunk < 1)
3103 /* If we know that the object has no xattribs then don't do all the
3104 * reading and parsing.
3106 if (obj->xattr_known && !obj->has_xattr) {
3113 buffer = (char *)yaffs_get_temp_buffer(dev);
3118 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3120 if (result != YAFFS_OK)
3123 x_buffer = buffer + x_offs;
3125 if (!obj->xattr_known) {
3126 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3127 obj->xattr_known = 1;
3131 retval = nval_get(x_buffer, x_size, name, value, size);
3133 retval = nval_list(x_buffer, x_size, value, size);
3135 yaffs_release_temp_buffer(dev, (u8 *) buffer);
3139 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3140 const void *value, int size, int flags)
3142 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3145 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3147 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3150 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3153 return yaffs_do_xattrib_fetch(obj, name, value, size);
3156 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3158 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3161 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3164 struct yaffs_obj_hdr *oh;
3165 struct yaffs_dev *dev;
3166 struct yaffs_ext_tags tags;
3168 int alloc_failed = 0;
3170 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3174 in->lazy_loaded = 0;
3175 buf = yaffs_get_temp_buffer(dev);
3177 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3178 oh = (struct yaffs_obj_hdr *)buf;
3180 in->yst_mode = oh->yst_mode;
3181 yaffs_load_attribs(in, oh);
3182 yaffs_set_obj_name_from_oh(in, oh);
3184 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3185 in->variant.symlink_variant.alias =
3186 yaffs_clone_str(oh->alias);
3187 if (!in->variant.symlink_variant.alias)
3188 alloc_failed = 1; /* Not returned */
3190 yaffs_release_temp_buffer(dev, buf);
3193 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
3194 const YCHAR *oh_name, int buff_size)
3196 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3197 if (dev->param.auto_unicode) {
3199 /* It is an ASCII name, do an ASCII to
3200 * unicode conversion */
3201 const char *ascii_oh_name = (const char *)oh_name;
3202 int n = buff_size - 1;
3203 while (n > 0 && *ascii_oh_name) {
3204 *name = *ascii_oh_name;
3210 strncpy(name, oh_name + 1, buff_size - 1);
3216 strncpy(name, oh_name, buff_size - 1);
3220 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
3223 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3228 if (dev->param.auto_unicode) {
3233 /* Figure out if the name will fit in ascii character set */
3234 while (is_ascii && *w) {
3241 /* It is an ASCII name, so convert unicode to ascii */
3242 char *ascii_oh_name = (char *)oh_name;
3243 int n = YAFFS_MAX_NAME_LENGTH - 1;
3244 while (n > 0 && *name) {
3245 *ascii_oh_name = *name;
3251 /* Unicode name, so save starting at the second YCHAR */
3253 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
3259 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3263 /* UpdateObjectHeader updates the header on NAND for an object.
3264 * If name is not NULL, then that new name is used.
3266 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3267 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3270 struct yaffs_block_info *bi;
3271 struct yaffs_dev *dev = in->my_dev;
3276 struct yaffs_ext_tags new_tags;
3277 struct yaffs_ext_tags old_tags;
3278 const YCHAR *alias = NULL;
3280 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3281 struct yaffs_obj_hdr *oh = NULL;
3283 strcpy(old_name, _Y("silly old name"));
3285 if (in->fake && in != dev->root_dir && !force && !xmod)
3288 yaffs_check_gc(dev, 0);
3289 yaffs_check_obj_details_loaded(in);
3291 buffer = yaffs_get_temp_buffer(in->my_dev);
3292 oh = (struct yaffs_obj_hdr *)buffer;
3294 prev_chunk_id = in->hdr_chunk;
3296 if (prev_chunk_id > 0) {
3297 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3300 yaffs_verify_oh(in, oh, &old_tags, 0);
3301 memcpy(old_name, oh->name, sizeof(oh->name));
3302 memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
3304 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3307 oh->type = in->variant_type;
3308 oh->yst_mode = in->yst_mode;
3309 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3311 yaffs_load_attribs_oh(oh, in);
3314 oh->parent_obj_id = in->parent->obj_id;
3316 oh->parent_obj_id = 0;
3318 if (name && *name) {
3319 memset(oh->name, 0, sizeof(oh->name));
3320 yaffs_load_oh_from_name(dev, oh->name, name);
3321 } else if (prev_chunk_id > 0) {
3322 memcpy(oh->name, old_name, sizeof(oh->name));
3324 memset(oh->name, 0, sizeof(oh->name));
3327 oh->is_shrink = is_shrink;
3329 switch (in->variant_type) {
3330 case YAFFS_OBJECT_TYPE_UNKNOWN:
3331 /* Should not happen */
3333 case YAFFS_OBJECT_TYPE_FILE:
3335 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED ||
3336 oh->parent_obj_id == YAFFS_OBJECTID_UNLINKED) ?
3337 0 : in->variant.file_variant.file_size;
3339 case YAFFS_OBJECT_TYPE_HARDLINK:
3340 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3342 case YAFFS_OBJECT_TYPE_SPECIAL:
3345 case YAFFS_OBJECT_TYPE_DIRECTORY:
3348 case YAFFS_OBJECT_TYPE_SYMLINK:
3349 alias = in->variant.symlink_variant.alias;
3351 alias = _Y("no alias");
3352 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3353 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3357 /* process any xattrib modifications */
3359 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3362 memset(&new_tags, 0, sizeof(new_tags));
3364 new_tags.chunk_id = 0;
3365 new_tags.obj_id = in->obj_id;
3366 new_tags.serial_number = in->serial;
3368 /* Add extra info for file header */
3369 new_tags.extra_available = 1;
3370 new_tags.extra_parent_id = oh->parent_obj_id;
3371 new_tags.extra_length = oh->file_size;
3372 new_tags.extra_is_shrink = oh->is_shrink;
3373 new_tags.extra_equiv_id = oh->equiv_id;
3374 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3375 new_tags.extra_obj_type = in->variant_type;
3376 yaffs_verify_oh(in, oh, &new_tags, 1);
3378 /* Create new chunk in NAND */
3380 yaffs_write_new_chunk(dev, buffer, &new_tags,
3381 (prev_chunk_id > 0) ? 1 : 0);
3384 yaffs_release_temp_buffer(dev, buffer);
3386 if (new_chunk_id < 0)
3387 return new_chunk_id;
3389 in->hdr_chunk = new_chunk_id;
3391 if (prev_chunk_id > 0)
3392 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3394 if (!yaffs_obj_cache_dirty(in))
3397 /* If this was a shrink, then mark the block
3398 * that the chunk lives on */
3400 bi = yaffs_get_block_info(in->my_dev,
3402 in->my_dev->param.chunks_per_block);
3403 bi->has_shrink_hdr = 1;
3407 return new_chunk_id;
3410 /*--------------------- File read/write ------------------------
3411 * Read and write have very similar structures.
3412 * In general the read/write has three parts to it
3413 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3414 * Some complete chunks
3415 * An incomplete chunk to end off with
3417 * Curve-balls: the first chunk might also be the last chunk.
3420 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3427 struct yaffs_cache *cache;
3428 struct yaffs_dev *dev;
3433 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3436 /* OK now check for the curveball where the start and end are in
3439 if ((start + n) < dev->data_bytes_per_chunk)
3442 n_copy = dev->data_bytes_per_chunk - start;
3444 cache = yaffs_find_chunk_cache(in, chunk);
3446 /* If the chunk is already in the cache or it is less than
3447 * a whole chunk or we're using inband tags then use the cache
3448 * (if there is caching) else bypass the cache.
3450 if (cache || n_copy != dev->data_bytes_per_chunk ||
3451 dev->param.inband_tags) {
3452 if (dev->param.n_caches > 0) {
3454 /* If we can't find the data in the cache,
3455 * then load it up. */
3459 yaffs_grab_chunk_cache(in->my_dev);
3461 cache->chunk_id = chunk;
3464 yaffs_rd_data_obj(in, chunk,
3469 yaffs_use_cache(dev, cache, 0);
3473 memcpy(buffer, &cache->data[start], n_copy);
3477 /* Read into the local buffer then copy.. */
3480 yaffs_get_temp_buffer(dev);
3481 yaffs_rd_data_obj(in, chunk, local_buffer);
3483 memcpy(buffer, &local_buffer[start], n_copy);
3485 yaffs_release_temp_buffer(dev, local_buffer);
3488 /* A full chunk. Read directly into the buffer. */
3489 yaffs_rd_data_obj(in, chunk, buffer);
3499 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3500 int n_bytes, int write_trhrough)
3509 int start_write = offset;
3510 int chunk_written = 0;
3513 struct yaffs_dev *dev;
3517 while (n > 0 && chunk_written >= 0) {
3518 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3520 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3521 start >= dev->data_bytes_per_chunk) {
3522 yaffs_trace(YAFFS_TRACE_ERROR,
3523 "AddrToChunk of offset %d gives chunk %d start %d",
3524 (int)offset, chunk, start);
3526 chunk++; /* File pos to chunk in file offset */
3528 /* OK now check for the curveball where the start and end are in
3532 if ((start + n) < dev->data_bytes_per_chunk) {
3535 /* Now calculate how many bytes to write back....
3536 * If we're overwriting and not writing to then end of
3537 * file then we need to write back as much as was there
3541 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3543 if (chunk_start > in->variant.file_variant.file_size)
3544 n_bytes_read = 0; /* Past end of file */
3547 in->variant.file_variant.file_size -
3550 if (n_bytes_read > dev->data_bytes_per_chunk)
3551 n_bytes_read = dev->data_bytes_per_chunk;
3555 (start + n)) ? n_bytes_read : (start + n);
3557 if (n_writeback < 0 ||
3558 n_writeback > dev->data_bytes_per_chunk)
3562 n_copy = dev->data_bytes_per_chunk - start;
3563 n_writeback = dev->data_bytes_per_chunk;
3566 if (n_copy != dev->data_bytes_per_chunk ||
3567 dev->param.inband_tags) {
3568 /* An incomplete start or end chunk (or maybe both
3569 * start and end chunk), or we're using inband tags,
3570 * so we want to use the cache buffers.
3572 if (dev->param.n_caches > 0) {
3573 struct yaffs_cache *cache;
3575 /* If we can't find the data in the cache, then
3577 cache = yaffs_find_chunk_cache(in, chunk);
3580 yaffs_check_alloc_available(dev, 1)) {
3581 cache = yaffs_grab_chunk_cache(dev);
3583 cache->chunk_id = chunk;
3586 yaffs_rd_data_obj(in, chunk,
3590 !yaffs_check_alloc_available(dev,
3592 /* Drop the cache if it was a read cache
3593 * item and no space check has been made
3600 yaffs_use_cache(dev, cache, 1);
3603 memcpy(&cache->data[start], buffer,
3607 cache->n_bytes = n_writeback;
3609 if (write_trhrough) {
3619 chunk_written = -1; /* fail write */
3622 /* An incomplete start or end chunk (or maybe
3623 * both start and end chunk). Read into the
3624 * local buffer then copy over and write back.
3627 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3629 yaffs_rd_data_obj(in, chunk, local_buffer);
3630 memcpy(&local_buffer[start], buffer, n_copy);
3633 yaffs_wr_data_obj(in, chunk,
3637 yaffs_release_temp_buffer(dev, local_buffer);
3640 /* A full chunk. Write directly from the buffer. */
3643 yaffs_wr_data_obj(in, chunk, buffer,
3644 dev->data_bytes_per_chunk, 0);
3646 /* Since we've overwritten the cached data,
3647 * we better invalidate it. */
3648 yaffs_invalidate_chunk_cache(in, chunk);
3651 if (chunk_written >= 0) {
3659 /* Update file object */
3661 if ((start_write + n_done) > in->variant.file_variant.file_size)
3662 in->variant.file_variant.file_size = (start_write + n_done);
3668 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3669 int n_bytes, int write_trhrough)
3671 yaffs2_handle_hole(in, offset);
3672 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3675 /* ---------------------- File resizing stuff ------------------ */
3677 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3680 struct yaffs_dev *dev = in->my_dev;
3681 int old_size = in->variant.file_variant.file_size;
3684 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3685 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3686 dev->data_bytes_per_chunk;
3689 /* Delete backwards so that we don't end up with holes if
3690 * power is lost part-way through the operation.
3692 for (i = last_del; i >= start_del; i--) {
3693 /* NB this could be optimised somewhat,
3694 * eg. could retrieve the tags and write them without
3695 * using yaffs_chunk_del
3698 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3704 (dev->internal_start_block * dev->param.chunks_per_block) ||
3706 ((dev->internal_end_block + 1) *
3707 dev->param.chunks_per_block)) {
3708 yaffs_trace(YAFFS_TRACE_ALWAYS,
3709 "Found daft chunk_id %d for %d",
3712 in->n_data_chunks--;
3713 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3718 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3722 struct yaffs_dev *dev = obj->my_dev;
3724 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3726 yaffs_prune_chunks(obj, new_size);
3728 if (new_partial != 0) {
3729 int last_chunk = 1 + new_full;
3730 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3732 /* Rewrite the last chunk with its new size and zero pad */
3733 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3734 memset(local_buffer + new_partial, 0,
3735 dev->data_bytes_per_chunk - new_partial);
3737 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3740 yaffs_release_temp_buffer(dev, local_buffer);
3743 obj->variant.file_variant.file_size = new_size;
3745 yaffs_prune_tree(dev, &obj->variant.file_variant);
3748 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3750 struct yaffs_dev *dev = in->my_dev;
3751 int old_size = in->variant.file_variant.file_size;
3753 yaffs_flush_file_cache(in);
3754 yaffs_invalidate_whole_cache(in);
3756 yaffs_check_gc(dev, 0);
3758 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3761 if (new_size == old_size)
3764 if (new_size > old_size) {
3765 yaffs2_handle_hole(in, new_size);
3766 in->variant.file_variant.file_size = new_size;
3768 /* new_size < old_size */
3769 yaffs_resize_file_down(in, new_size);
3772 /* Write a new object header to reflect the resize.
3773 * show we've shrunk the file, if need be
3774 * Do this only if the file is not in the deleted directories
3775 * and is not shadowed.
3779 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3780 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3781 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3786 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3791 yaffs_flush_file_cache(in);
3797 yaffs_load_current_time(in, 0, 0);
3799 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3800 YAFFS_OK : YAFFS_FAIL;
3804 /* yaffs_del_file deletes the whole file data
3805 * and the inode associated with the file.
3806 * It does not delete the links associated with the file.
3808 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3812 struct yaffs_dev *dev = in->my_dev;
3819 yaffs_change_obj_name(in, in->my_dev->del_dir,
3820 _Y("deleted"), 0, 0);
3821 yaffs_trace(YAFFS_TRACE_TRACING,
3822 "yaffs: immediate deletion of file %d",
3825 in->my_dev->n_deleted_files++;
3826 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3827 yaffs_resize_file(in, 0);
3828 yaffs_soft_del_file(in);
3831 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3832 _Y("unlinked"), 0, 0);
3837 int yaffs_del_file(struct yaffs_obj *in)
3839 int ret_val = YAFFS_OK;
3840 int deleted; /* Need to cache value on stack if in is freed */
3841 struct yaffs_dev *dev = in->my_dev;
3843 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3844 yaffs_resize_file(in, 0);
3846 if (in->n_data_chunks > 0) {
3847 /* Use soft deletion if there is data in the file.
3848 * That won't be the case if it has been resized to zero.
3851 ret_val = yaffs_unlink_file_if_needed(in);
3853 deleted = in->deleted;
3855 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3858 in->my_dev->n_deleted_files++;
3859 yaffs_soft_del_file(in);
3861 return deleted ? YAFFS_OK : YAFFS_FAIL;
3863 /* The file has no data chunks so we toss it immediately */
3864 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3865 in->variant.file_variant.top = NULL;
3866 yaffs_generic_obj_del(in);
3872 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3875 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3876 !(list_empty(&obj->variant.dir_variant.children));
3879 static int yaffs_del_dir(struct yaffs_obj *obj)
3881 /* First check that the directory is empty. */
3882 if (yaffs_is_non_empty_dir(obj))
3885 return yaffs_generic_obj_del(obj);
3888 static int yaffs_del_symlink(struct yaffs_obj *in)
3890 kfree(in->variant.symlink_variant.alias);
3891 in->variant.symlink_variant.alias = NULL;
3893 return yaffs_generic_obj_del(in);
3896 static int yaffs_del_link(struct yaffs_obj *in)
3898 /* remove this hardlink from the list associated with the equivalent
3901 list_del_init(&in->hard_links);
3902 return yaffs_generic_obj_del(in);
3905 int yaffs_del_obj(struct yaffs_obj *obj)
3909 switch (obj->variant_type) {
3910 case YAFFS_OBJECT_TYPE_FILE:
3911 ret_val = yaffs_del_file(obj);
3913 case YAFFS_OBJECT_TYPE_DIRECTORY:
3914 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3915 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3916 "Remove object %d from dirty directories",
3918 list_del_init(&obj->variant.dir_variant.dirty);
3920 return yaffs_del_dir(obj);
3922 case YAFFS_OBJECT_TYPE_SYMLINK:
3923 ret_val = yaffs_del_symlink(obj);
3925 case YAFFS_OBJECT_TYPE_HARDLINK:
3926 ret_val = yaffs_del_link(obj);
3928 case YAFFS_OBJECT_TYPE_SPECIAL:
3929 ret_val = yaffs_generic_obj_del(obj);
3931 case YAFFS_OBJECT_TYPE_UNKNOWN:
3933 break; /* should not happen. */
3938 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3948 yaffs_update_parent(obj->parent);
3950 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3951 return yaffs_del_link(obj);
3952 } else if (!list_empty(&obj->hard_links)) {
3953 /* Curve ball: We're unlinking an object that has a hardlink.
3955 * This problem arises because we are not strictly following
3956 * The Linux link/inode model.
3958 * We can't really delete the object.
3959 * Instead, we do the following:
3960 * - Select a hardlink.
3961 * - Unhook it from the hard links
3962 * - Move it from its parent directory so that the rename works.
3963 * - Rename the object to the hardlink's name.
3964 * - Delete the hardlink
3967 struct yaffs_obj *hl;
3968 struct yaffs_obj *parent;
3970 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3972 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3975 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3976 parent = hl->parent;
3978 list_del_init(&hl->hard_links);
3980 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3982 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
3984 if (ret_val == YAFFS_OK)
3985 ret_val = yaffs_generic_obj_del(hl);
3989 } else if (del_now) {
3990 switch (obj->variant_type) {
3991 case YAFFS_OBJECT_TYPE_FILE:
3992 return yaffs_del_file(obj);
3994 case YAFFS_OBJECT_TYPE_DIRECTORY:
3995 list_del_init(&obj->variant.dir_variant.dirty);
3996 return yaffs_del_dir(obj);
3998 case YAFFS_OBJECT_TYPE_SYMLINK:
3999 return yaffs_del_symlink(obj);
4001 case YAFFS_OBJECT_TYPE_SPECIAL:
4002 return yaffs_generic_obj_del(obj);
4004 case YAFFS_OBJECT_TYPE_HARDLINK:
4005 case YAFFS_OBJECT_TYPE_UNKNOWN:
4009 } else if (yaffs_is_non_empty_dir(obj)) {
4012 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4013 _Y("unlinked"), 0, 0);
4017 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4019 if (obj && obj->unlink_allowed)
4020 return yaffs_unlink_worker(obj);
4025 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4027 struct yaffs_obj *obj;
4029 obj = yaffs_find_by_name(dir, name);
4030 return yaffs_unlink_obj(obj);
4034 * If old_name is NULL then we take old_dir as the object to be renamed.
4036 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
4037 struct yaffs_obj *new_dir, const YCHAR *new_name)
4039 struct yaffs_obj *obj = NULL;
4040 struct yaffs_obj *existing_target = NULL;
4043 struct yaffs_dev *dev;
4045 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4049 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4054 dev = old_dir->my_dev;
4056 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4057 /* Special case for case insemsitive systems.
4058 * While look-up is case insensitive, the name isn't.
4059 * Therefore we might want to change x.txt to X.txt
4061 if (old_dir == new_dir &&
4062 old_name && new_name &&
4063 strcmp(old_name, new_name) == 0)
4067 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4068 YAFFS_MAX_NAME_LENGTH)
4073 obj = yaffs_find_by_name(old_dir, old_name);
4076 old_dir = obj->parent;
4079 if (obj && obj->rename_allowed) {
4080 /* Now handle an existing target, if there is one */
4081 existing_target = yaffs_find_by_name(new_dir, new_name);
4082 if (yaffs_is_non_empty_dir(existing_target)) {
4083 return YAFFS_FAIL; /* ENOTEMPTY */
4084 } else if (existing_target && existing_target != obj) {
4085 /* Nuke the target first, using shadowing,
4086 * but only if it isn't the same object.
4088 * Note we must disable gc here otherwise it can mess
4092 dev->gc_disable = 1;
4093 yaffs_change_obj_name(obj, new_dir, new_name, force,
4094 existing_target->obj_id);
4095 existing_target->is_shadowed = 1;
4096 yaffs_unlink_obj(existing_target);
4097 dev->gc_disable = 0;
4100 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4102 yaffs_update_parent(old_dir);
4103 if (new_dir != old_dir)
4104 yaffs_update_parent(new_dir);
4111 /*----------------------- Initialisation Scanning ---------------------- */
4113 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4114 int backward_scanning)
4116 struct yaffs_obj *obj;
4118 if (backward_scanning) {
4119 /* Handle YAFFS2 case (backward scanning)
4120 * If the shadowed object exists then ignore.
4122 obj = yaffs_find_by_number(dev, obj_id);
4127 /* Let's create it (if it does not exist) assuming it is a file so that
4128 * it can do shrinking etc.
4129 * We put it in unlinked dir to be cleaned up after the scanning
4132 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4135 obj->is_shadowed = 1;
4136 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4137 obj->variant.file_variant.shrink_size = 0;
4138 obj->valid = 1; /* So that we don't read any other info. */
4141 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4143 struct list_head *lh;
4144 struct list_head *save;
4145 struct yaffs_obj *hl;
4146 struct yaffs_obj *in;
4148 list_for_each_safe(lh, save, hard_list) {
4149 hl = list_entry(lh, struct yaffs_obj, hard_links);
4150 in = yaffs_find_by_number(dev,
4151 hl->variant.hardlink_variant.equiv_id);
4154 /* Add the hardlink pointers */
4155 hl->variant.hardlink_variant.equiv_obj = in;
4156 list_add(&hl->hard_links, &in->hard_links);
4158 /* Todo Need to report/handle this better.
4159 * Got a problem... hardlink to a non-existant object
4161 hl->variant.hardlink_variant.equiv_obj = NULL;
4162 INIT_LIST_HEAD(&hl->hard_links);
4167 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4170 * Sort out state of unlinked and deleted objects after scanning.
4172 struct list_head *i;
4173 struct list_head *n;
4174 struct yaffs_obj *l;
4179 /* Soft delete all the unlinked files */
4180 list_for_each_safe(i, n,
4181 &dev->unlinked_dir->variant.dir_variant.children) {
4182 l = list_entry(i, struct yaffs_obj, siblings);
4186 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4187 l = list_entry(i, struct yaffs_obj, siblings);
4193 * This code iterates through all the objects making sure that they are rooted.
4194 * Any unrooted objects are re-rooted in lost+found.
4195 * An object needs to be in one of:
4196 * - Directly under deleted, unlinked
4197 * - Directly or indirectly under root.
4200 * This code assumes that we don't ever change the current relationships
4201 * between directories:
4202 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4203 * lost-n-found->parent == root_dir
4205 * This fixes the problem where directories might have inadvertently been
4206 * deleted leaving the object "hanging" without being rooted in the
4210 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4212 return (obj == dev->del_dir ||
4213 obj == dev->unlinked_dir || obj == dev->root_dir);
4216 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4218 struct yaffs_obj *obj;
4219 struct yaffs_obj *parent;
4221 struct list_head *lh;
4222 struct list_head *n;
4229 /* Iterate through the objects in each hash entry,
4230 * looking at each object.
4231 * Make sure it is rooted.
4234 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4235 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4236 obj = list_entry(lh, struct yaffs_obj, hash_link);
4237 parent = obj->parent;
4239 if (yaffs_has_null_parent(dev, obj)) {
4240 /* These directories are not hanging */
4242 } else if (!parent ||
4243 parent->variant_type !=
4244 YAFFS_OBJECT_TYPE_DIRECTORY) {
4246 } else if (yaffs_has_null_parent(dev, parent)) {
4250 * Need to follow the parent chain to
4251 * see if it is hanging.
4256 while (parent != dev->root_dir &&
4258 parent->parent->variant_type ==
4259 YAFFS_OBJECT_TYPE_DIRECTORY &&
4261 parent = parent->parent;
4264 if (parent != dev->root_dir)
4268 yaffs_trace(YAFFS_TRACE_SCAN,
4269 "Hanging object %d moved to lost and found",
4271 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4278 * Delete directory contents for cleaning up lost and found.
4280 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4282 struct yaffs_obj *obj;
4283 struct list_head *lh;
4284 struct list_head *n;
4286 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4289 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4290 obj = list_entry(lh, struct yaffs_obj, siblings);
4291 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4292 yaffs_del_dir_contents(obj);
4293 yaffs_trace(YAFFS_TRACE_SCAN,
4294 "Deleting lost_found object %d",
4296 yaffs_unlink_obj(obj);
4300 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4302 yaffs_del_dir_contents(dev->lost_n_found);
4306 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4310 struct list_head *i;
4311 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4312 struct yaffs_obj *l;
4318 yaffs_trace(YAFFS_TRACE_ALWAYS,
4319 "tragedy: yaffs_find_by_name: null pointer directory"
4324 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4325 yaffs_trace(YAFFS_TRACE_ALWAYS,
4326 "tragedy: yaffs_find_by_name: non-directory"
4331 sum = yaffs_calc_name_sum(name);
4333 list_for_each(i, &directory->variant.dir_variant.children) {
4334 l = list_entry(i, struct yaffs_obj, siblings);
4336 if (l->parent != directory)
4339 yaffs_check_obj_details_loaded(l);
4341 /* Special case for lost-n-found */
4342 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4343 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4345 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4346 /* LostnFound chunk called Objxxx
4349 yaffs_get_obj_name(l, buffer,
4350 YAFFS_MAX_NAME_LENGTH + 1);
4351 if (strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4358 /* GetEquivalentObject dereferences any hard links to get to the
4362 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4364 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4365 obj = obj->variant.hardlink_variant.equiv_obj;
4366 yaffs_check_obj_details_loaded(obj);
4372 * A note or two on object names.
4373 * * If the object name is missing, we then make one up in the form objnnn
4375 * * ASCII names are stored in the object header's name field from byte zero
4376 * * Unicode names are historically stored starting from byte zero.
4378 * Then there are automatic Unicode names...
4379 * The purpose of these is to save names in a way that can be read as
4380 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4381 * system to share files.
4383 * These automatic unicode are stored slightly differently...
4384 * - If the name can fit in the ASCII character space then they are saved as
4385 * ascii names as per above.
4386 * - If the name needs Unicode then the name is saved in Unicode
4387 * starting at oh->name[1].
4390 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4393 /* Create an object name if we could not find one. */
4394 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4395 YCHAR local_name[20];
4396 YCHAR num_string[20];
4397 YCHAR *x = &num_string[19];
4398 unsigned v = obj->obj_id;
4402 *x = '0' + (v % 10);
4405 /* make up a name */
4406 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4407 strcat(local_name, x);
4408 strncpy(name, local_name, buffer_size - 1);
4412 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4414 memset(name, 0, buffer_size * sizeof(YCHAR));
4415 yaffs_check_obj_details_loaded(obj);
4416 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND)
4417 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4418 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
4419 else if (obj->short_name[0])
4420 strcpy(name, obj->short_name);
4422 else if (obj->hdr_chunk > 0) {
4424 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4426 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4428 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4430 if (obj->hdr_chunk > 0) {
4431 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4435 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4438 yaffs_release_temp_buffer(obj->my_dev, buffer);
4441 yaffs_fix_null_name(obj, name, buffer_size);
4443 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4446 int yaffs_get_obj_length(struct yaffs_obj *obj)
4448 /* Dereference any hard linking */
4449 obj = yaffs_get_equivalent_obj(obj);
4451 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4452 return obj->variant.file_variant.file_size;
4453 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4454 if (!obj->variant.symlink_variant.alias)
4456 return strnlen(obj->variant.symlink_variant.alias,
4457 YAFFS_MAX_ALIAS_LENGTH);
4459 /* Only a directory should drop through to here */
4460 return obj->my_dev->data_bytes_per_chunk;
4464 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4467 struct list_head *i;
4470 count++; /* the object itself */
4472 list_for_each(i, &obj->hard_links)
4473 count++; /* add the hard links; */
4478 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4480 obj = yaffs_get_equivalent_obj(obj);
4485 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4487 obj = yaffs_get_equivalent_obj(obj);
4489 switch (obj->variant_type) {
4490 case YAFFS_OBJECT_TYPE_FILE:
4493 case YAFFS_OBJECT_TYPE_DIRECTORY:
4496 case YAFFS_OBJECT_TYPE_SYMLINK:
4499 case YAFFS_OBJECT_TYPE_HARDLINK:
4502 case YAFFS_OBJECT_TYPE_SPECIAL:
4503 if (S_ISFIFO(obj->yst_mode))
4505 if (S_ISCHR(obj->yst_mode))
4507 if (S_ISBLK(obj->yst_mode))
4509 if (S_ISSOCK(obj->yst_mode))
4519 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4521 obj = yaffs_get_equivalent_obj(obj);
4522 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4523 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4525 return yaffs_clone_str(_Y(""));
4528 /*--------------------------- Initialisation code -------------------------- */
4530 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4532 /* Common functions, gotta have */
4533 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4536 #ifdef CONFIG_YAFFS_YAFFS2
4538 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4539 if (dev->param.write_chunk_tags_fn &&
4540 dev->param.read_chunk_tags_fn &&
4541 !dev->param.write_chunk_fn &&
4542 !dev->param.read_chunk_fn &&
4543 dev->param.bad_block_fn && dev->param.query_block_fn)
4547 /* Can use the "spare" style interface for yaffs1 */
4548 if (!dev->param.is_yaffs2 &&
4549 !dev->param.write_chunk_tags_fn &&
4550 !dev->param.read_chunk_tags_fn &&
4551 dev->param.write_chunk_fn &&
4552 dev->param.read_chunk_fn &&
4553 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4559 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4561 /* Initialise the unlinked, deleted, root and lost+found directories */
4562 dev->lost_n_found = dev->root_dir = NULL;
4563 dev->unlinked_dir = dev->del_dir = NULL;
4565 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4567 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4569 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4570 YAFFS_ROOT_MODE | S_IFDIR);
4572 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4573 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4575 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4577 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4583 int yaffs_guts_initialise(struct yaffs_dev *dev)
4585 int init_failed = 0;
4589 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()");
4591 /* Check stuff that must be set */
4594 yaffs_trace(YAFFS_TRACE_ALWAYS,
4595 "yaffs: Need a device"
4600 if (dev->is_mounted) {
4601 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4605 dev->internal_start_block = dev->param.start_block;
4606 dev->internal_end_block = dev->param.end_block;
4607 dev->block_offset = 0;
4608 dev->chunk_offset = 0;
4609 dev->n_free_chunks = 0;
4613 if (dev->param.start_block == 0) {
4614 dev->internal_start_block = dev->param.start_block + 1;
4615 dev->internal_end_block = dev->param.end_block + 1;
4616 dev->block_offset = 1;
4617 dev->chunk_offset = dev->param.chunks_per_block;
4620 /* Check geometry parameters. */
4622 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4623 dev->param.total_bytes_per_chunk < 1024) ||
4624 (!dev->param.is_yaffs2 &&
4625 dev->param.total_bytes_per_chunk < 512) ||
4626 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4627 dev->param.chunks_per_block < 2 ||
4628 dev->param.n_reserved_blocks < 2 ||
4629 dev->internal_start_block <= 0 ||
4630 dev->internal_end_block <= 0 ||
4631 dev->internal_end_block <=
4632 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4634 /* otherwise it is too small */
4635 yaffs_trace(YAFFS_TRACE_ALWAYS,
4636 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4637 dev->param.total_bytes_per_chunk,
4638 dev->param.is_yaffs2 ? "2" : "",
4639 dev->param.inband_tags);
4643 if (yaffs_init_nand(dev) != YAFFS_OK) {
4644 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4648 /* Sort out space for inband tags, if required */
4649 if (dev->param.inband_tags)
4650 dev->data_bytes_per_chunk =
4651 dev->param.total_bytes_per_chunk -
4652 sizeof(struct yaffs_packed_tags2_tags_only);
4654 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4656 /* Got the right mix of functions? */
4657 if (!yaffs_check_dev_fns(dev)) {
4658 /* Function missing */
4659 yaffs_trace(YAFFS_TRACE_ALWAYS,
4660 "device function(s) missing or wrong");
4665 /* Finished with most checks. Further checks happen later on too. */
4667 dev->is_mounted = 1;
4669 /* OK now calculate a few things for the device */
4672 * Calculate all the chunk size manipulation numbers:
4674 x = dev->data_bytes_per_chunk;
4675 /* We always use dev->chunk_shift and dev->chunk_div */
4676 dev->chunk_shift = calc_shifts(x);
4677 x >>= dev->chunk_shift;
4679 /* We only use chunk mask if chunk_div is 1 */
4680 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4683 * Calculate chunk_grp_bits.
4684 * We need to find the next power of 2 > than internal_end_block
4687 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4689 bits = calc_shifts_ceiling(x);
4691 /* Set up tnode width if wide tnodes are enabled. */
4692 if (!dev->param.wide_tnodes_disabled) {
4693 /* bits must be even so that we end up with 32-bit words */
4697 dev->tnode_width = 16;
4699 dev->tnode_width = bits;
4701 dev->tnode_width = 16;
4704 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4706 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4707 * so if the bitwidth of the
4708 * chunk range we're using is greater than 16 we need
4709 * to figure out chunk shift and chunk_grp_size
4712 if (bits <= dev->tnode_width)
4713 dev->chunk_grp_bits = 0;
4715 dev->chunk_grp_bits = bits - dev->tnode_width;
4717 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4718 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4719 dev->tnode_size = sizeof(struct yaffs_tnode);
4721 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4723 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4724 /* We have a problem because the soft delete won't work if
4725 * the chunk group size > chunks per block.
4726 * This can be remedied by using larger "virtual blocks".
4728 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4733 /* Finished verifying the device, continue with initialisation */
4735 /* More device initialisation */
4737 dev->passive_gc_count = 0;
4738 dev->oldest_dirty_gc_count = 0;
4740 dev->gc_block_finder = 0;
4741 dev->buffered_block = -1;
4742 dev->doing_buffered_block_rewrite = 0;
4743 dev->n_deleted_files = 0;
4744 dev->n_bg_deletions = 0;
4745 dev->n_unlinked_files = 0;
4746 dev->n_ecc_fixed = 0;
4747 dev->n_ecc_unfixed = 0;
4748 dev->n_tags_ecc_fixed = 0;
4749 dev->n_tags_ecc_unfixed = 0;
4750 dev->n_erase_failures = 0;
4751 dev->n_erased_blocks = 0;
4752 dev->gc_disable = 0;
4753 dev->has_pending_prioritised_gc = 1;
4754 /* Assume the worst for now, will get fixed on first GC */
4755 INIT_LIST_HEAD(&dev->dirty_dirs);
4756 dev->oldest_dirty_seq = 0;
4757 dev->oldest_dirty_block = 0;
4759 /* Initialise temporary buffers and caches. */
4760 if (!yaffs_init_tmp_buffers(dev))
4764 dev->gc_cleanup_list = NULL;
4766 if (!init_failed && dev->param.n_caches > 0) {
4770 dev->param.n_caches * sizeof(struct yaffs_cache);
4772 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4773 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4775 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4777 buf = (u8 *) dev->cache;
4780 memset(dev->cache, 0, cache_bytes);
4782 for (i = 0; i < dev->param.n_caches && buf; i++) {
4783 dev->cache[i].object = NULL;
4784 dev->cache[i].last_use = 0;
4785 dev->cache[i].dirty = 0;
4786 dev->cache[i].data = buf =
4787 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4792 dev->cache_last_use = 0;
4795 dev->cache_hits = 0;
4798 dev->gc_cleanup_list =
4799 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4801 if (!dev->gc_cleanup_list)
4805 if (dev->param.is_yaffs2)
4806 dev->param.use_header_file_size = 1;
4808 if (!init_failed && !yaffs_init_blocks(dev))
4811 yaffs_init_tnodes_and_objs(dev);
4813 if (!init_failed && !yaffs_create_initial_dir(dev))
4817 /* Now scan the flash. */
4818 if (dev->param.is_yaffs2) {
4819 if (yaffs2_checkpt_restore(dev)) {
4820 yaffs_check_obj_details_loaded(dev->root_dir);
4821 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4823 "yaffs: restored from checkpoint"
4827 /* Clean up the mess caused by an aborted
4828 * checkpoint load then scan backwards.
4830 yaffs_deinit_blocks(dev);
4832 yaffs_deinit_tnodes_and_objs(dev);
4834 dev->n_erased_blocks = 0;
4835 dev->n_free_chunks = 0;
4836 dev->alloc_block = -1;
4837 dev->alloc_page = -1;
4838 dev->n_deleted_files = 0;
4839 dev->n_unlinked_files = 0;
4840 dev->n_bg_deletions = 0;
4842 if (!init_failed && !yaffs_init_blocks(dev))
4845 yaffs_init_tnodes_and_objs(dev);
4848 && !yaffs_create_initial_dir(dev))
4851 if (!init_failed && !yaffs2_scan_backwards(dev))
4854 } else if (!yaffs1_scan(dev)) {
4858 yaffs_strip_deleted_objs(dev);
4859 yaffs_fix_hanging_objs(dev);
4860 if (dev->param.empty_lost_n_found)
4861 yaffs_empty_l_n_f(dev);
4865 /* Clean up the mess */
4866 yaffs_trace(YAFFS_TRACE_TRACING,
4867 "yaffs: yaffs_guts_initialise() aborted.");
4869 yaffs_deinitialise(dev);
4873 /* Zero out stats */
4874 dev->n_page_reads = 0;
4875 dev->n_page_writes = 0;
4876 dev->n_erasures = 0;
4877 dev->n_gc_copies = 0;
4878 dev->n_retired_writes = 0;
4880 dev->n_retired_blocks = 0;
4882 yaffs_verify_free_chunks(dev);
4883 yaffs_verify_blocks(dev);
4885 /* Clean up any aborted checkpoint data */
4886 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4887 yaffs2_checkpt_invalidate(dev);
4889 yaffs_trace(YAFFS_TRACE_TRACING,
4890 "yaffs: yaffs_guts_initialise() done.");
4894 void yaffs_deinitialise(struct yaffs_dev *dev)
4896 if (dev->is_mounted) {
4899 yaffs_deinit_blocks(dev);
4900 yaffs_deinit_tnodes_and_objs(dev);
4901 if (dev->param.n_caches > 0 && dev->cache) {
4903 for (i = 0; i < dev->param.n_caches; i++) {
4904 kfree(dev->cache[i].data);
4905 dev->cache[i].data = NULL;
4912 kfree(dev->gc_cleanup_list);
4914 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
4915 kfree(dev->temp_buffer[i].buffer);
4917 dev->is_mounted = 0;
4919 if (dev->param.deinitialise_flash_fn)
4920 dev->param.deinitialise_flash_fn(dev);
4924 int yaffs_count_free_chunks(struct yaffs_dev *dev)
4928 struct yaffs_block_info *blk;
4930 blk = dev->block_info;
4931 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4932 switch (blk->block_state) {
4933 case YAFFS_BLOCK_STATE_EMPTY:
4934 case YAFFS_BLOCK_STATE_ALLOCATING:
4935 case YAFFS_BLOCK_STATE_COLLECTING:
4936 case YAFFS_BLOCK_STATE_FULL:
4938 (dev->param.chunks_per_block - blk->pages_in_use +
4939 blk->soft_del_pages);
4949 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
4951 /* This is what we report to the outside world */
4954 int blocks_for_checkpt;
4957 n_free = dev->n_free_chunks;
4958 n_free += dev->n_deleted_files;
4960 /* Now count and subtract the number of dirty chunks in the cache. */
4962 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
4963 if (dev->cache[i].dirty)
4967 n_free -= n_dirty_caches;
4970 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
4972 /* Now figure checkpoint space and report that... */
4973 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
4975 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);