2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include "yaffs_trace.h"
16 #include "yaffs_guts.h"
18 #include "yaffs_cache.h"
19 #include "yaffs_endian.h"
20 #include "yaffs_getblockinfo.h"
21 #include "yaffs_tagscompat.h"
22 #include "yaffs_tagsmarshall.h"
23 #include "yaffs_nand.h"
24 #include "yaffs_yaffs1.h"
25 #include "yaffs_yaffs2.h"
26 #include "yaffs_bitmap.h"
27 #include "yaffs_verify.h"
28 #include "yaffs_nand.h"
29 #include "yaffs_packedtags2.h"
30 #include "yaffs_nameval.h"
31 #include "yaffs_allocator.h"
32 #include "yaffs_attribs.h"
33 #include "yaffs_summary.h"
35 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
36 #define YAFFS_GC_GOOD_ENOUGH 2
37 #define YAFFS_GC_PASSIVE_THRESHOLD 4
39 #include "yaffs_ecc.h"
41 /* Forward declarations */
43 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
46 /* Function to calculate chunk and offset */
48 void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
49 int *chunk_out, u32 *offset_out)
54 chunk = (u32) (addr >> dev->chunk_shift);
56 if (dev->chunk_div == 1) {
57 /* easy power of 2 case */
58 offset = (u32) (addr & dev->chunk_mask);
60 /* Non power-of-2 case */
64 chunk /= dev->chunk_div;
66 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
67 offset = (u32) (addr - chunk_base);
74 /* Function to return the number of shifts for a power of 2 greater than or
75 * equal to the given number
76 * Note we don't try to cater for all possible numbers and this does not have to
77 * be hellishly efficient.
80 static inline u32 calc_shifts_ceiling(u32 x)
85 shifts = extra_bits = 0;
100 /* Function to return the number of shifts to get a 1 in bit 0
103 static inline u32 calc_shifts(u32 x)
121 * Temporary buffer manipulations.
124 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
129 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
131 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
132 dev->temp_buffer[i].in_use = 0;
133 buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
134 dev->temp_buffer[i].buffer = buf;
137 return buf ? YAFFS_OK : YAFFS_FAIL;
140 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
145 if (dev->temp_in_use > dev->max_temp)
146 dev->max_temp = dev->temp_in_use;
148 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
149 if (dev->temp_buffer[i].in_use == 0) {
150 dev->temp_buffer[i].in_use = 1;
151 return dev->temp_buffer[i].buffer;
155 yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
157 * If we got here then we have to allocate an unmanaged one
161 dev->unmanaged_buffer_allocs++;
162 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
166 /* Frees all the temp_buffer objects in the yaffs_dev instance
168 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
174 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
175 if (dev->temp_buffer[i].buffer == buffer) {
176 dev->temp_buffer[i].in_use = 0;
182 /* assume it is an unmanaged one. */
183 yaffs_trace(YAFFS_TRACE_BUFFERS,
184 "Releasing unmanaged temp buffer");
186 dev->unmanaged_buffer_deallocs++;
192 * Functions for robustisizing TODO
196 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
198 const struct yaffs_ext_tags *tags)
206 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
207 const struct yaffs_ext_tags *tags)
214 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
215 struct yaffs_block_info *bi)
217 if (!bi->gc_prioritise) {
218 bi->gc_prioritise = 1;
219 dev->has_pending_prioritised_gc = 1;
220 bi->chunk_error_strikes++;
222 if (bi->chunk_error_strikes > 3) {
223 bi->needs_retiring = 1; /* Too many stikes, so retire */
224 yaffs_trace(YAFFS_TRACE_ALWAYS,
225 "yaffs: Block struck out");
231 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
234 int flash_block = nand_chunk / dev->param.chunks_per_block;
235 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
237 yaffs_handle_chunk_error(dev, bi);
240 /* Was an actual write failure,
241 * so mark the block for retirement.*/
242 bi->needs_retiring = 1;
243 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
244 "**>> Block %d needs retiring", flash_block);
247 /* Delete the chunk */
248 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
249 yaffs_skip_rest_of_block(dev);
257 * Simple hash function. Needs to have a reasonable spread
260 static inline int yaffs_hash_fn(int n)
264 return n % YAFFS_NOBJECT_BUCKETS;
268 * Access functions to useful fake objects.
269 * Note that root might have a presence in NAND if permissions are set.
272 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
274 return dev->root_dir;
277 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
279 return dev->lost_n_found;
283 * Erased NAND checking functions
286 int yaffs_check_ff(u8 *buffer, int n_bytes)
288 /* Horrible, slow implementation */
297 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
299 int retval = YAFFS_OK;
300 u8 *data = yaffs_get_temp_buffer(dev);
301 struct yaffs_ext_tags tags;
304 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
306 if (result == YAFFS_FAIL ||
307 tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
310 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
312 yaffs_trace(YAFFS_TRACE_NANDACCESS,
313 "Chunk %d not erased", nand_chunk);
317 yaffs_release_temp_buffer(dev, data);
323 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
326 struct yaffs_ext_tags *tags)
328 int retval = YAFFS_OK;
329 struct yaffs_ext_tags temp_tags;
330 u8 *buffer = yaffs_get_temp_buffer(dev);
333 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
334 if (result == YAFFS_FAIL ||
335 memcmp(buffer, data, dev->data_bytes_per_chunk) ||
336 temp_tags.obj_id != tags->obj_id ||
337 temp_tags.chunk_id != tags->chunk_id ||
338 temp_tags.n_bytes != tags->n_bytes)
341 yaffs_release_temp_buffer(dev, buffer);
347 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
350 int reserved_blocks = dev->param.n_reserved_blocks;
353 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
356 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
358 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
361 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
364 struct yaffs_block_info *bi;
366 if (dev->n_erased_blocks < 1) {
367 /* Hoosterman we've got a problem.
368 * Can't get space to gc
370 yaffs_trace(YAFFS_TRACE_ERROR,
371 "yaffs tragedy: no more erased blocks");
376 /* Find an empty block. */
378 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
379 dev->alloc_block_finder++;
380 if (dev->alloc_block_finder < (int)dev->internal_start_block
381 || dev->alloc_block_finder > (int)dev->internal_end_block) {
382 dev->alloc_block_finder = dev->internal_start_block;
385 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
387 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
388 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
390 bi->seq_number = dev->seq_number;
391 dev->n_erased_blocks--;
392 yaffs_trace(YAFFS_TRACE_ALLOCATE,
393 "Allocated block %d, seq %d, %d left" ,
394 dev->alloc_block_finder, dev->seq_number,
395 dev->n_erased_blocks);
396 return dev->alloc_block_finder;
400 yaffs_trace(YAFFS_TRACE_ALWAYS,
401 "yaffs tragedy: no more erased blocks, but there should have been %d",
402 dev->n_erased_blocks);
407 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
408 struct yaffs_block_info **block_ptr)
411 struct yaffs_block_info *bi;
413 if (dev->alloc_block < 0) {
414 /* Get next block to allocate off */
415 dev->alloc_block = yaffs_find_alloc_block(dev);
419 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
420 /* No space unless we're allowed to use the reserve. */
424 if (dev->n_erased_blocks < (int)dev->param.n_reserved_blocks
425 && dev->alloc_page == 0)
426 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
428 /* Next page please.... */
429 if (dev->alloc_block >= 0) {
430 bi = yaffs_get_block_info(dev, dev->alloc_block);
432 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
435 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
439 dev->n_free_chunks--;
441 /* If the block is full set the state to full */
442 if (dev->alloc_page >= dev->param.chunks_per_block) {
443 bi->block_state = YAFFS_BLOCK_STATE_FULL;
444 dev->alloc_block = -1;
453 yaffs_trace(YAFFS_TRACE_ERROR,
454 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
459 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
463 n = dev->n_erased_blocks * dev->param.chunks_per_block;
465 if (dev->alloc_block > 0)
466 n += (dev->param.chunks_per_block - dev->alloc_page);
473 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
474 * if we don't want to write to it.
476 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
478 struct yaffs_block_info *bi;
480 if (dev->alloc_block > 0) {
481 bi = yaffs_get_block_info(dev, dev->alloc_block);
482 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
483 bi->block_state = YAFFS_BLOCK_STATE_FULL;
484 dev->alloc_block = -1;
489 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
491 struct yaffs_ext_tags *tags, int use_reserver)
497 yaffs2_checkpt_invalidate(dev);
500 struct yaffs_block_info *bi = 0;
503 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
509 /* First check this chunk is erased, if it needs
510 * checking. The checking policy (unless forced
511 * always on) is as follows:
513 * Check the first page we try to write in a block.
514 * If the check passes then we don't need to check any
515 * more. If the check fails, we check again...
516 * If the block has been erased, we don't need to check.
518 * However, if the block has been prioritised for gc,
519 * then we think there might be something odd about
520 * this block and stop using it.
522 * Rationale: We should only ever see chunks that have
523 * not been erased if there was a partially written
524 * chunk due to power loss. This checking policy should
525 * catch that case with very few checks and thus save a
526 * lot of checks that are most likely not needed.
529 * If an erase check fails or the write fails we skip the
533 /* let's give it a try */
536 if (dev->param.always_check_erased)
537 bi->skip_erased_check = 0;
539 if (!bi->skip_erased_check) {
540 erased_ok = yaffs_check_chunk_erased(dev, chunk);
541 if (erased_ok != YAFFS_OK) {
542 yaffs_trace(YAFFS_TRACE_ERROR,
543 "**>> yaffs chunk %d was not erased",
546 /* If not erased, delete this one,
547 * skip rest of block and
548 * try another chunk */
549 yaffs_chunk_del(dev, chunk, 1, __LINE__);
550 yaffs_skip_rest_of_block(dev);
555 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
557 if (!bi->skip_erased_check)
559 yaffs_verify_chunk_written(dev, chunk, data, tags);
561 if (write_ok != YAFFS_OK) {
562 /* Clean up aborted write, skip to next block and
563 * try another chunk */
564 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
568 bi->skip_erased_check = 1;
570 /* Copy the data into the robustification buffer */
571 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
573 } while (write_ok != YAFFS_OK &&
574 (yaffs_wr_attempts == 0 || attempts <= yaffs_wr_attempts));
580 yaffs_trace(YAFFS_TRACE_ERROR,
581 "**>> yaffs write required %d attempts",
583 dev->n_retried_writes += (attempts - 1);
590 * Block retiring for handling a broken block.
593 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
595 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
597 yaffs2_checkpt_invalidate(dev);
599 yaffs2_clear_oldest_dirty_seq(dev, bi);
601 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
602 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
603 yaffs_trace(YAFFS_TRACE_ALWAYS,
604 "yaffs: Failed to mark bad and erase block %d",
607 struct yaffs_ext_tags tags;
609 flash_block * dev->param.chunks_per_block;
611 u8 *buffer = yaffs_get_temp_buffer(dev);
613 memset(buffer, 0xff, dev->data_bytes_per_chunk);
614 memset(&tags, 0, sizeof(tags));
615 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
616 if (dev->tagger.write_chunk_tags_fn(dev, chunk_id -
620 yaffs_trace(YAFFS_TRACE_ALWAYS,
621 "yaffs: Failed to write bad block marker to block %d",
624 yaffs_release_temp_buffer(dev, buffer);
628 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
629 bi->gc_prioritise = 0;
630 bi->needs_retiring = 0;
632 dev->n_retired_blocks++;
635 /*---------------- Name handling functions ------------*/
637 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
638 const YCHAR *oh_name, int buff_size)
640 #ifdef CONFIG_YAFFS_AUTO_UNICODE
641 if (dev->param.auto_unicode) {
643 /* It is an ASCII name, do an ASCII to
644 * unicode conversion */
645 const char *ascii_oh_name = (const char *)oh_name;
646 int n = buff_size - 1;
647 while (n > 0 && *ascii_oh_name) {
648 *name = *ascii_oh_name;
654 strncpy(name, oh_name + 1, buff_size - 1);
661 strncpy(name, oh_name, buff_size - 1);
665 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
668 #ifdef CONFIG_YAFFS_AUTO_UNICODE
673 if (dev->param.auto_unicode) {
678 /* Figure out if the name will fit in ascii character set */
679 while (is_ascii && *w) {
686 /* It is an ASCII name, so convert unicode to ascii */
687 char *ascii_oh_name = (char *)oh_name;
688 int n = YAFFS_MAX_NAME_LENGTH - 1;
689 while (n > 0 && *name) {
690 *ascii_oh_name = *name;
696 /* Unicode name, so save starting at the second YCHAR */
698 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
705 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
709 static u16 yaffs_calc_name_sum(const YCHAR *name)
717 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
719 /* 0x1f mask is case insensitive */
720 sum += ((*name) & 0x1f) * i;
728 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
730 memset(obj->short_name, 0, sizeof(obj->short_name));
732 if (name && !name[0]) {
733 yaffs_fix_null_name(obj, obj->short_name,
734 YAFFS_SHORT_NAME_LENGTH);
735 name = obj->short_name;
737 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
738 YAFFS_SHORT_NAME_LENGTH) {
739 strcpy(obj->short_name, name);
742 obj->sum = yaffs_calc_name_sum(name);
745 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
746 const struct yaffs_obj_hdr *oh)
748 #ifdef CONFIG_YAFFS_AUTO_UNICODE
749 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
750 memset(tmp_name, 0, sizeof(tmp_name));
751 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
752 YAFFS_MAX_NAME_LENGTH + 1);
753 yaffs_set_obj_name(obj, tmp_name);
755 yaffs_set_obj_name(obj, oh->name);
759 loff_t yaffs_max_file_size(struct yaffs_dev *dev)
761 if (sizeof(loff_t) < 8)
762 return YAFFS_MAX_FILE_SIZE_32;
764 return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
767 /*-------------------- TNODES -------------------
769 * List of spare tnodes
770 * The list is hooked together using the first pointer
774 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
776 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
779 memset(tn, 0, dev->tnode_size);
783 dev->checkpoint_blocks_required = 0; /* force recalculation */
788 /* FreeTnode frees up a tnode and puts it back on the free list */
789 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
791 yaffs_free_raw_tnode(dev, tn);
793 dev->checkpoint_blocks_required = 0; /* force recalculation */
796 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
798 yaffs_deinit_raw_tnodes_and_objs(dev);
803 static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
804 unsigned pos, unsigned val)
806 u32 *map = (u32 *) tn;
812 pos &= YAFFS_TNODES_LEVEL0_MASK;
813 val >>= dev->chunk_grp_bits;
815 bit_in_map = pos * dev->tnode_width;
816 word_in_map = bit_in_map / 32;
817 bit_in_word = bit_in_map & (32 - 1);
819 mask = dev->tnode_mask << bit_in_word;
821 map[word_in_map] &= ~mask;
822 map[word_in_map] |= (mask & (val << bit_in_word));
824 if (dev->tnode_width > (32 - bit_in_word)) {
825 bit_in_word = (32 - bit_in_word);
828 dev->tnode_mask >> bit_in_word;
829 map[word_in_map] &= ~mask;
830 map[word_in_map] |= (mask & (val >> bit_in_word));
834 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
837 u32 *map = (u32 *) tn;
843 pos &= YAFFS_TNODES_LEVEL0_MASK;
845 bit_in_map = pos * dev->tnode_width;
846 word_in_map = bit_in_map / 32;
847 bit_in_word = bit_in_map & (32 - 1);
849 val = map[word_in_map] >> bit_in_word;
851 if (dev->tnode_width > (32 - bit_in_word)) {
852 bit_in_word = (32 - bit_in_word);
854 val |= (map[word_in_map] << bit_in_word);
857 val &= dev->tnode_mask;
858 val <<= dev->chunk_grp_bits;
863 /* ------------------- End of individual tnode manipulation -----------------*/
865 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
866 * The look up tree is represented by the top tnode and the number of top_level
867 * in the tree. 0 means only the level 0 tnode is in the tree.
870 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
871 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
872 struct yaffs_file_var *file_struct,
875 struct yaffs_tnode *tn = file_struct->top;
878 int level = file_struct->top_level;
882 /* Check sane level and chunk Id */
883 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
886 if (chunk_id > YAFFS_MAX_CHUNK_ID)
889 /* First check we're tall enough (ie enough top_level) */
891 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
894 i >>= YAFFS_TNODES_INTERNAL_BITS;
898 if (required_depth > file_struct->top_level)
899 return NULL; /* Not tall enough, so we can't find it */
901 /* Traverse down to level 0 */
902 while (level > 0 && tn) {
903 tn = tn->internal[(chunk_id >>
904 (YAFFS_TNODES_LEVEL0_BITS +
906 YAFFS_TNODES_INTERNAL_BITS)) &
907 YAFFS_TNODES_INTERNAL_MASK];
914 /* add_find_tnode_0 finds the level 0 tnode if it exists,
915 * otherwise first expands the tree.
916 * This happens in two steps:
917 * 1. If the tree isn't tall enough, then make it taller.
918 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
920 * Used when modifying the tree.
922 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
923 * specified tn will be plugged into the ttree.
926 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
927 struct yaffs_file_var *file_struct,
929 struct yaffs_tnode *passed_tn)
934 struct yaffs_tnode *tn;
937 /* Check sane level and page Id */
938 if (file_struct->top_level < 0 ||
939 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
942 if (chunk_id > YAFFS_MAX_CHUNK_ID)
945 /* First check we're tall enough (ie enough top_level) */
947 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
950 x >>= YAFFS_TNODES_INTERNAL_BITS;
954 if (required_depth > file_struct->top_level) {
955 /* Not tall enough, gotta make the tree taller */
956 for (i = file_struct->top_level; i < required_depth; i++) {
958 tn = yaffs_get_tnode(dev);
961 tn->internal[0] = file_struct->top;
962 file_struct->top = tn;
963 file_struct->top_level++;
965 yaffs_trace(YAFFS_TRACE_ERROR,
966 "yaffs: no more tnodes");
972 /* Traverse down to level 0, adding anything we need */
974 l = file_struct->top_level;
975 tn = file_struct->top;
978 while (l > 0 && tn) {
980 (YAFFS_TNODES_LEVEL0_BITS +
981 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
982 YAFFS_TNODES_INTERNAL_MASK;
984 if ((l > 1) && !tn->internal[x]) {
985 /* Add missing non-level-zero tnode */
986 tn->internal[x] = yaffs_get_tnode(dev);
987 if (!tn->internal[x])
990 /* Looking from level 1 at level 0 */
992 /* If we already have one, release it */
994 yaffs_free_tnode(dev,
996 tn->internal[x] = passed_tn;
998 } else if (!tn->internal[x]) {
999 /* Don't have one, none passed in */
1000 tn->internal[x] = yaffs_get_tnode(dev);
1001 if (!tn->internal[x])
1006 tn = tn->internal[x];
1010 /* top is level 0 */
1012 memcpy(tn, passed_tn,
1013 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
1014 yaffs_free_tnode(dev, passed_tn);
1021 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
1024 return (tags->chunk_id == (u32)chunk_obj &&
1025 tags->obj_id == (u32)obj_id &&
1026 !tags->is_deleted) ? 1 : 0;
1030 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
1031 struct yaffs_ext_tags *tags, int obj_id,
1036 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
1037 if (yaffs_check_chunk_bit
1038 (dev, the_chunk / dev->param.chunks_per_block,
1039 the_chunk % dev->param.chunks_per_block)) {
1041 if (dev->chunk_grp_size == 1)
1044 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
1046 if (yaffs_tags_match(tags,
1047 obj_id, inode_chunk)) {
1058 int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1059 struct yaffs_ext_tags *tags)
1061 /*Get the Tnode, then get the level 0 offset chunk offset */
1062 struct yaffs_tnode *tn;
1064 struct yaffs_ext_tags local_tags;
1066 struct yaffs_dev *dev = in->my_dev;
1069 /* Passed a NULL, so use our own tags space */
1073 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1078 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1080 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1085 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1086 struct yaffs_ext_tags *tags)
1088 /* Get the Tnode, then get the level 0 offset chunk offset */
1089 struct yaffs_tnode *tn;
1091 struct yaffs_ext_tags local_tags;
1092 struct yaffs_dev *dev = in->my_dev;
1096 /* Passed a NULL, so use our own tags space */
1100 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1105 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1107 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1110 /* Delete the entry in the filestructure (if found) */
1112 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1117 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1118 int nand_chunk, int in_scan)
1120 /* NB in_scan is zero unless scanning.
1121 * For forward scanning, in_scan is > 0;
1122 * for backward scanning in_scan is < 0
1124 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1127 struct yaffs_tnode *tn;
1128 struct yaffs_dev *dev = in->my_dev;
1130 struct yaffs_ext_tags existing_tags;
1131 struct yaffs_ext_tags new_tags;
1132 unsigned existing_serial, new_serial;
1134 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1135 /* Just ignore an attempt at putting a chunk into a non-file
1137 * If it is not during Scanning then something went wrong!
1140 yaffs_trace(YAFFS_TRACE_ERROR,
1141 "yaffs tragedy:attempt to put data chunk into a non-file"
1146 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1150 tn = yaffs_add_find_tnode_0(dev,
1151 &in->variant.file_variant,
1157 /* Dummy insert, bail now */
1160 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1163 /* If we're scanning then we need to test for duplicates
1164 * NB This does not need to be efficient since it should only
1165 * happen when the power fails during a write, then only one
1166 * chunk should ever be affected.
1168 * Correction for YAFFS2: This could happen quite a lot and we
1169 * need to think about efficiency! TODO
1170 * Update: For backward scanning we don't need to re-read tags
1171 * so this is quite cheap.
1174 if (existing_cunk > 0) {
1175 /* NB Right now existing chunk will not be real
1176 * chunk_id if the chunk group size > 1
1177 * thus we have to do a FindChunkInFile to get the
1180 * We have a duplicate now we need to decide which
1183 * Backwards scanning YAFFS2: The old one is what
1184 * we use, dump the new one.
1185 * YAFFS1: Get both sets of tags and compare serial
1190 /* Only do this for forward scanning */
1191 yaffs_rd_chunk_tags_nand(dev,
1195 /* Do a proper find */
1197 yaffs_find_chunk_in_file(in, inode_chunk,
1201 if (existing_cunk <= 0) {
1202 /*Hoosterman - how did this happen? */
1204 yaffs_trace(YAFFS_TRACE_ERROR,
1205 "yaffs tragedy: existing chunk < 0 in scan"
1210 /* NB The deleted flags should be false, otherwise
1211 * the chunks will not be loaded during a scan
1215 new_serial = new_tags.serial_number;
1216 existing_serial = existing_tags.serial_number;
1219 if ((in_scan > 0) &&
1220 (existing_cunk <= 0 ||
1221 ((existing_serial + 1) & 3) == new_serial)) {
1222 /* Forward scanning.
1224 * Delete the old one and drop through to
1227 yaffs_chunk_del(dev, existing_cunk, 1,
1230 /* Backward scanning or we want to use the
1232 * Delete the new one and return early so that
1233 * the tnode isn't changed
1235 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1242 if (existing_cunk == 0)
1243 in->n_data_chunks++;
1245 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1250 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1252 struct yaffs_block_info *the_block;
1255 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1257 block_no = chunk / dev->param.chunks_per_block;
1258 the_block = yaffs_get_block_info(dev, block_no);
1260 the_block->soft_del_pages++;
1261 dev->n_free_chunks++;
1262 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1266 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1267 * the chunks in the file.
1268 * All soft deleting does is increment the block's softdelete count and pulls
1269 * the chunk out of the tnode.
1270 * Thus, essentially this is the same as DeleteWorker except that the chunks
1274 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1275 u32 level, int chunk_offset)
1280 struct yaffs_dev *dev = in->my_dev;
1286 for (i = YAFFS_NTNODES_INTERNAL - 1;
1289 if (tn->internal[i]) {
1291 yaffs_soft_del_worker(in,
1295 YAFFS_TNODES_INTERNAL_BITS)
1298 yaffs_free_tnode(dev,
1300 tn->internal[i] = NULL;
1302 /* Can this happen? */
1306 return (all_done) ? 1 : 0;
1310 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1311 the_chunk = yaffs_get_group_base(dev, tn, i);
1313 yaffs_soft_del_chunk(dev, the_chunk);
1314 yaffs_load_tnode_0(dev, tn, i, 0);
1320 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1322 struct yaffs_dev *dev = obj->my_dev;
1323 struct yaffs_obj *parent;
1325 yaffs_verify_obj_in_dir(obj);
1326 parent = obj->parent;
1328 yaffs_verify_dir(parent);
1330 if (dev && dev->param.remove_obj_fn)
1331 dev->param.remove_obj_fn(obj);
1333 list_del_init(&obj->siblings);
1336 yaffs_verify_dir(parent);
1339 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1342 yaffs_trace(YAFFS_TRACE_ALWAYS,
1343 "tragedy: Trying to add an object to a null pointer directory"
1348 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1349 yaffs_trace(YAFFS_TRACE_ALWAYS,
1350 "tragedy: Trying to add an object to a non-directory"
1355 if (obj->siblings.prev == NULL) {
1356 /* Not initialised */
1360 yaffs_verify_dir(directory);
1362 yaffs_remove_obj_from_dir(obj);
1365 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1366 obj->parent = directory;
1368 if (directory == obj->my_dev->unlinked_dir
1369 || directory == obj->my_dev->del_dir) {
1371 obj->my_dev->n_unlinked_files++;
1372 obj->rename_allowed = 0;
1375 yaffs_verify_dir(directory);
1376 yaffs_verify_obj_in_dir(obj);
1379 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1380 struct yaffs_obj *new_dir,
1381 const YCHAR *new_name, int force, int shadows)
1385 struct yaffs_obj *existing_target;
1387 if (new_dir == NULL)
1388 new_dir = obj->parent; /* use the old directory */
1390 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1391 yaffs_trace(YAFFS_TRACE_ALWAYS,
1392 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1397 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1398 del_op = (new_dir == obj->my_dev->del_dir);
1400 existing_target = yaffs_find_by_name(new_dir, new_name);
1402 /* If the object is a file going into the unlinked directory,
1403 * then it is OK to just stuff it in since duplicate names are OK.
1404 * else only proceed if the new name does not exist and we're putting
1405 * it into a directory.
1407 if (!(unlink_op || del_op || force ||
1408 shadows > 0 || !existing_target) ||
1409 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1412 yaffs_set_obj_name(obj, new_name);
1414 yaffs_add_obj_to_dir(new_dir, obj);
1419 /* If it is a deletion then we mark it as a shrink for gc */
1420 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1427 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1430 struct yaffs_dev *dev = obj->my_dev;
1432 /* If it is still linked into the bucket list, free from the list */
1433 if (!list_empty(&obj->hash_link)) {
1434 list_del_init(&obj->hash_link);
1435 bucket = yaffs_hash_fn(obj->obj_id);
1436 dev->obj_bucket[bucket].count--;
1440 /* FreeObject frees up a Object and puts it back on the free list */
1441 static void yaffs_free_obj(struct yaffs_obj *obj)
1443 struct yaffs_dev *dev;
1450 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1451 obj, obj->my_inode);
1454 if (!list_empty(&obj->siblings))
1457 if (obj->my_inode) {
1458 /* We're still hooked up to a cached inode.
1459 * Don't delete now, but mark for later deletion
1461 obj->defered_free = 1;
1465 yaffs_unhash_obj(obj);
1467 yaffs_free_raw_obj(dev, obj);
1469 dev->checkpoint_blocks_required = 0; /* force recalculation */
1472 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1474 if (obj->defered_free)
1475 yaffs_free_obj(obj);
1478 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1480 /* Iinvalidate the file's data in the cache, without flushing. */
1481 yaffs_invalidate_file_cache(in);
1483 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1484 /* Move to unlinked directory so we have a deletion record */
1485 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1489 yaffs_remove_obj_from_dir(in);
1490 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1498 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1500 if (!obj->deleted ||
1501 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1505 if (obj->n_data_chunks <= 0) {
1506 /* Empty file with no duplicate object headers,
1507 * just delete it immediately */
1508 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1509 obj->variant.file_variant.top = NULL;
1510 yaffs_trace(YAFFS_TRACE_TRACING,
1511 "yaffs: Deleting empty file %d",
1513 yaffs_generic_obj_del(obj);
1515 yaffs_soft_del_worker(obj,
1516 obj->variant.file_variant.top,
1518 file_variant.top_level, 0);
1523 /* Pruning removes any part of the file structure tree that is beyond the
1524 * bounds of the file (ie that does not point to chunks).
1526 * A file should only get pruned when its size is reduced.
1528 * Before pruning, the chunks must be pulled from the tree and the
1529 * level 0 tnode entries must be zeroed out.
1530 * Could also use this for file deletion, but that's probably better handled
1531 * by a special case.
1533 * This function is recursive. For levels > 0 the function is called again on
1534 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1535 * If there is no data in a subtree then it is pruned.
1538 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1539 struct yaffs_tnode *tn, u32 level,
1551 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1552 if (tn->internal[i]) {
1554 yaffs_prune_worker(dev,
1557 (i == 0) ? del0 : 1);
1560 if (tn->internal[i])
1564 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1565 u32 *map = (u32 *) tn;
1567 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1573 if (has_data == 0 && del0) {
1574 /* Free and return NULL */
1575 yaffs_free_tnode(dev, tn);
1581 static int yaffs_prune_tree(struct yaffs_dev *dev,
1582 struct yaffs_file_var *file_struct)
1587 struct yaffs_tnode *tn;
1589 if (file_struct->top_level < 1)
1593 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1595 /* Now we have a tree with all the non-zero branches NULL but
1596 * the height is the same as it was.
1597 * Let's see if we can trim internal tnodes to shorten the tree.
1598 * We can do this if only the 0th element in the tnode is in use
1599 * (ie all the non-zero are NULL)
1602 while (file_struct->top_level && !done) {
1603 tn = file_struct->top;
1606 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1607 if (tn->internal[i])
1612 file_struct->top = tn->internal[0];
1613 file_struct->top_level--;
1614 yaffs_free_tnode(dev, tn);
1623 /*-------------------- End of File Structure functions.-------------------*/
1625 /* alloc_empty_obj gets us a clean Object.*/
1626 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1628 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1635 /* Now sweeten it up... */
1637 memset(obj, 0, sizeof(struct yaffs_obj));
1638 obj->being_created = 1;
1642 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1643 INIT_LIST_HEAD(&(obj->hard_links));
1644 INIT_LIST_HEAD(&(obj->hash_link));
1645 INIT_LIST_HEAD(&obj->siblings);
1647 /* Now make the directory sane */
1648 if (dev->root_dir) {
1649 obj->parent = dev->root_dir;
1650 list_add(&(obj->siblings),
1651 &dev->root_dir->variant.dir_variant.children);
1654 /* Add it to the lost and found directory.
1655 * NB Can't put root or lost-n-found in lost-n-found so
1656 * check if lost-n-found exists first
1658 if (dev->lost_n_found)
1659 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1661 obj->being_created = 0;
1663 dev->checkpoint_blocks_required = 0; /* force recalculation */
1668 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1672 int lowest = 999999;
1674 /* Search for the shortest list or one that
1678 for (i = 0; i < 10 && lowest > 4; i++) {
1679 dev->bucket_finder++;
1680 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1681 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1682 lowest = dev->obj_bucket[dev->bucket_finder].count;
1683 l = dev->bucket_finder;
1690 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1692 int bucket = yaffs_find_nice_bucket(dev);
1694 struct list_head *i;
1695 u32 n = (u32) bucket;
1698 * Now find an object value that has not already been taken
1699 * by scanning the list, incrementing each time by number of buckets.
1703 n += YAFFS_NOBJECT_BUCKETS;
1704 list_for_each(i, &dev->obj_bucket[bucket].list) {
1705 /* Check if this value is already taken. */
1706 if (i && list_entry(i, struct yaffs_obj,
1707 hash_link)->obj_id == n)
1714 static void yaffs_hash_obj(struct yaffs_obj *in)
1716 int bucket = yaffs_hash_fn(in->obj_id);
1717 struct yaffs_dev *dev = in->my_dev;
1719 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1720 dev->obj_bucket[bucket].count++;
1723 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1725 int bucket = yaffs_hash_fn(number);
1726 struct list_head *i;
1727 struct yaffs_obj *in;
1729 list_for_each(i, &dev->obj_bucket[bucket].list) {
1730 /* Look if it is in the list */
1731 in = list_entry(i, struct yaffs_obj, hash_link);
1732 if (in->obj_id == number) {
1733 /* Don't show if it is defered free */
1734 if (in->defered_free)
1743 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1744 enum yaffs_obj_type type)
1746 struct yaffs_obj *the_obj = NULL;
1747 struct yaffs_tnode *tn = NULL;
1750 number = yaffs_new_obj_id(dev);
1752 if (type == YAFFS_OBJECT_TYPE_FILE) {
1753 tn = yaffs_get_tnode(dev);
1758 the_obj = yaffs_alloc_empty_obj(dev);
1761 yaffs_free_tnode(dev, tn);
1766 the_obj->rename_allowed = 1;
1767 the_obj->unlink_allowed = 1;
1768 the_obj->obj_id = number;
1769 yaffs_hash_obj(the_obj);
1770 the_obj->variant_type = type;
1771 yaffs_load_current_time(the_obj, 1, 1);
1774 case YAFFS_OBJECT_TYPE_FILE:
1775 the_obj->variant.file_variant.file_size = 0;
1776 the_obj->variant.file_variant.stored_size = 0;
1777 the_obj->variant.file_variant.shrink_size =
1778 yaffs_max_file_size(dev);
1779 the_obj->variant.file_variant.top_level = 0;
1780 the_obj->variant.file_variant.top = tn;
1782 case YAFFS_OBJECT_TYPE_DIRECTORY:
1783 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1784 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1786 case YAFFS_OBJECT_TYPE_SYMLINK:
1787 case YAFFS_OBJECT_TYPE_HARDLINK:
1788 case YAFFS_OBJECT_TYPE_SPECIAL:
1789 /* No action required */
1791 case YAFFS_OBJECT_TYPE_UNKNOWN:
1792 /* todo this should not happen */
1798 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1799 int number, u32 mode)
1802 struct yaffs_obj *obj =
1803 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1808 obj->fake = 1; /* it is fake so it might not use NAND */
1809 obj->rename_allowed = 0;
1810 obj->unlink_allowed = 0;
1813 obj->yst_mode = mode;
1815 obj->hdr_chunk = 0; /* Not a valid chunk. */
1821 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
1827 yaffs_init_raw_tnodes_and_objs(dev);
1829 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1830 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
1831 dev->obj_bucket[i].count = 0;
1835 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
1837 enum yaffs_obj_type type)
1839 struct yaffs_obj *the_obj = NULL;
1842 the_obj = yaffs_find_by_number(dev, number);
1845 the_obj = yaffs_new_obj(dev, number, type);
1851 YCHAR *yaffs_clone_str(const YCHAR *str)
1853 YCHAR *new_str = NULL;
1859 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
1860 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
1862 strncpy(new_str, str, len);
1869 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
1870 * link (ie. name) is created or deleted in the directory.
1873 * create dir/a : update dir's mtime/ctime
1874 * rm dir/a: update dir's mtime/ctime
1875 * modify dir/a: don't update dir's mtimme/ctime
1877 * This can be handled immediately or defered. Defering helps reduce the number
1878 * of updates when many files in a directory are changed within a brief period.
1880 * If the directory updating is defered then yaffs_update_dirty_dirs must be
1881 * called periodically.
1884 static void yaffs_update_parent(struct yaffs_obj *obj)
1886 struct yaffs_dev *dev;
1892 yaffs_load_current_time(obj, 0, 1);
1893 if (dev->param.defered_dir_update) {
1894 struct list_head *link = &obj->variant.dir_variant.dirty;
1896 if (list_empty(link)) {
1897 list_add(link, &dev->dirty_dirs);
1898 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1899 "Added object %d to dirty directories",
1904 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
1908 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
1910 struct list_head *link;
1911 struct yaffs_obj *obj;
1912 struct yaffs_dir_var *d_s;
1913 union yaffs_obj_var *o_v;
1915 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
1917 while (!list_empty(&dev->dirty_dirs)) {
1918 link = dev->dirty_dirs.next;
1919 list_del_init(link);
1921 d_s = list_entry(link, struct yaffs_dir_var, dirty);
1922 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
1923 obj = list_entry(o_v, struct yaffs_obj, variant);
1925 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
1929 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
1934 * Mknod (create) a new object.
1935 * equiv_obj only has meaning for a hard link;
1936 * alias_str only has meaning for a symlink.
1937 * rdev only has meaning for devices (a subset of special objects)
1940 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
1941 struct yaffs_obj *parent,
1946 struct yaffs_obj *equiv_obj,
1947 const YCHAR *alias_str, u32 rdev)
1949 struct yaffs_obj *in;
1951 struct yaffs_dev *dev = parent->my_dev;
1953 /* Check if the entry exists.
1954 * If it does then fail the call since we don't want a dup. */
1955 if (yaffs_find_by_name(parent, name))
1958 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
1959 str = yaffs_clone_str(alias_str);
1964 in = yaffs_new_obj(dev, -1, type);
1973 in->variant_type = type;
1975 in->yst_mode = mode;
1977 yaffs_attribs_init(in, gid, uid, rdev);
1979 in->n_data_chunks = 0;
1981 yaffs_set_obj_name(in, name);
1984 yaffs_add_obj_to_dir(parent, in);
1986 in->my_dev = parent->my_dev;
1989 case YAFFS_OBJECT_TYPE_SYMLINK:
1990 in->variant.symlink_variant.alias = str;
1992 case YAFFS_OBJECT_TYPE_HARDLINK:
1993 in->variant.hardlink_variant.equiv_obj = equiv_obj;
1994 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
1995 list_add(&in->hard_links, &equiv_obj->hard_links);
1997 case YAFFS_OBJECT_TYPE_FILE:
1998 case YAFFS_OBJECT_TYPE_DIRECTORY:
1999 case YAFFS_OBJECT_TYPE_SPECIAL:
2000 case YAFFS_OBJECT_TYPE_UNKNOWN:
2005 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2006 /* Could not create the object header, fail */
2012 yaffs_update_parent(parent);
2017 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2018 const YCHAR *name, u32 mode, u32 uid,
2021 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2022 uid, gid, NULL, NULL, 0);
2025 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2026 u32 mode, u32 uid, u32 gid)
2028 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2029 mode, uid, gid, NULL, NULL, 0);
2032 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2033 const YCHAR *name, u32 mode, u32 uid,
2036 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2037 uid, gid, NULL, NULL, rdev);
2040 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2041 const YCHAR *name, u32 mode, u32 uid,
2042 u32 gid, const YCHAR *alias)
2044 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2045 uid, gid, NULL, alias, 0);
2048 /* yaffs_link_obj returns the object id of the equivalent object.*/
2049 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2050 struct yaffs_obj *equiv_obj)
2052 /* Get the real object in case we were fed a hard link obj */
2053 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2055 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2056 parent, name, 0, 0, 0,
2057 equiv_obj, NULL, 0))
2066 /*---------------------- Block Management and Page Allocation -------------*/
2068 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2070 if (dev->block_info_alt && dev->block_info)
2071 vfree(dev->block_info);
2073 kfree(dev->block_info);
2075 dev->block_info_alt = 0;
2077 dev->block_info = NULL;
2079 if (dev->chunk_bits_alt && dev->chunk_bits)
2080 vfree(dev->chunk_bits);
2082 kfree(dev->chunk_bits);
2083 dev->chunk_bits_alt = 0;
2084 dev->chunk_bits = NULL;
2087 static int yaffs_init_blocks(struct yaffs_dev *dev)
2089 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2091 dev->block_info = NULL;
2092 dev->chunk_bits = NULL;
2093 dev->alloc_block = -1; /* force it to get a new one */
2095 /* If the first allocation strategy fails, thry the alternate one */
2097 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2098 if (!dev->block_info) {
2100 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2101 dev->block_info_alt = 1;
2103 dev->block_info_alt = 0;
2106 if (!dev->block_info)
2109 /* Set up dynamic blockinfo stuff. Round up bytes. */
2110 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2112 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2113 if (!dev->chunk_bits) {
2115 vmalloc(dev->chunk_bit_stride * n_blocks);
2116 dev->chunk_bits_alt = 1;
2118 dev->chunk_bits_alt = 0;
2120 if (!dev->chunk_bits)
2124 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2125 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2129 yaffs_deinit_blocks(dev);
2134 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2136 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2140 /* If the block is still healthy erase it and mark as clean.
2141 * If the block has had a data failure, then retire it.
2144 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2145 "yaffs_block_became_dirty block %d state %d %s",
2146 block_no, bi->block_state,
2147 (bi->needs_retiring) ? "needs retiring" : "");
2149 yaffs2_clear_oldest_dirty_seq(dev, bi);
2151 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2153 /* If this is the block being garbage collected then stop gc'ing */
2154 if (block_no == (int)dev->gc_block)
2157 /* If this block is currently the best candidate for gc
2158 * then drop as a candidate */
2159 if (block_no == (int)dev->gc_dirtiest) {
2160 dev->gc_dirtiest = 0;
2161 dev->gc_pages_in_use = 0;
2164 if (!bi->needs_retiring) {
2165 yaffs2_checkpt_invalidate(dev);
2166 erased_ok = yaffs_erase_block(dev, block_no);
2168 dev->n_erase_failures++;
2169 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2170 "**>> Erasure failed %d", block_no);
2174 /* Verify erasure if needed */
2176 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2177 !yaffs_skip_verification(dev))) {
2178 for (i = 0; i < dev->param.chunks_per_block; i++) {
2179 if (!yaffs_check_chunk_erased(dev,
2180 block_no * dev->param.chunks_per_block + i)) {
2181 yaffs_trace(YAFFS_TRACE_ERROR,
2182 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2189 /* We lost a block of free space */
2190 dev->n_free_chunks -= dev->param.chunks_per_block;
2191 yaffs_retire_block(dev, block_no);
2192 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2193 "**>> Block %d retired", block_no);
2197 /* Clean it up... */
2198 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2200 dev->n_erased_blocks++;
2201 bi->pages_in_use = 0;
2202 bi->soft_del_pages = 0;
2203 bi->has_shrink_hdr = 0;
2204 bi->skip_erased_check = 1; /* Clean, so no need to check */
2205 bi->gc_prioritise = 0;
2206 bi->has_summary = 0;
2208 yaffs_clear_chunk_bits(dev, block_no);
2210 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2213 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2214 struct yaffs_block_info *bi,
2215 int old_chunk, u8 *buffer)
2219 struct yaffs_ext_tags tags;
2220 struct yaffs_obj *object;
2222 int ret_val = YAFFS_OK;
2224 memset(&tags, 0, sizeof(tags));
2225 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2227 object = yaffs_find_by_number(dev, tags.obj_id);
2229 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2230 "Collecting chunk in block %d, %d %d %d ",
2231 dev->gc_chunk, tags.obj_id,
2232 tags.chunk_id, tags.n_bytes);
2234 if (object && !yaffs_skip_verification(dev)) {
2235 if (tags.chunk_id == 0)
2238 else if (object->soft_del)
2239 /* Defeat the test */
2240 matching_chunk = old_chunk;
2243 yaffs_find_chunk_in_file
2244 (object, tags.chunk_id,
2247 if (old_chunk != matching_chunk)
2248 yaffs_trace(YAFFS_TRACE_ERROR,
2249 "gc: page in gc mismatch: %d %d %d %d",
2257 yaffs_trace(YAFFS_TRACE_ERROR,
2258 "page %d in gc has no object: %d %d %d ",
2260 tags.obj_id, tags.chunk_id,
2266 object->soft_del && tags.chunk_id != 0) {
2267 /* Data chunk in a soft deleted file,
2269 * It's a soft deleted data chunk,
2270 * No need to copy this, just forget
2271 * about it and fix up the object.
2274 /* Free chunks already includes
2275 * softdeleted chunks, how ever this
2276 * chunk is going to soon be really
2277 * deleted which will increment free
2278 * chunks. We have to decrement free
2279 * chunks so this works out properly.
2281 dev->n_free_chunks--;
2282 bi->soft_del_pages--;
2284 object->n_data_chunks--;
2285 if (object->n_data_chunks <= 0) {
2286 /* remeber to clean up obj */
2287 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2291 } else if (object) {
2292 /* It's either a data chunk in a live
2293 * file or an ObjectHeader, so we're
2295 * NB Need to keep the ObjectHeaders of
2296 * deleted files until the whole file
2297 * has been deleted off
2299 tags.serial_number++;
2302 if (tags.chunk_id == 0) {
2303 /* It is an object Id,
2304 * We need to nuke the shrinkheader flags since its
2306 * Also need to clean up shadowing.
2307 * NB We don't want to do all the work of translating
2308 * object header endianism back and forth so we leave
2309 * the oh endian in its stored order.
2312 struct yaffs_obj_hdr *oh;
2313 oh = (struct yaffs_obj_hdr *) buffer;
2316 tags.extra_is_shrink = 0;
2317 oh->shadows_obj = 0;
2318 oh->inband_shadowed_obj_id = 0;
2319 tags.extra_shadows = 0;
2321 /* Update file size */
2322 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2323 yaffs_oh_size_load(dev, oh,
2324 object->variant.file_variant.stored_size, 1);
2325 tags.extra_file_size =
2326 object->variant.file_variant.stored_size;
2329 yaffs_verify_oh(object, oh, &tags, 1);
2331 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2334 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2337 if (new_chunk < 0) {
2338 ret_val = YAFFS_FAIL;
2341 /* Now fix up the Tnodes etc. */
2343 if (tags.chunk_id == 0) {
2345 object->hdr_chunk = new_chunk;
2346 object->serial = tags.serial_number;
2348 /* It's a data chunk */
2349 yaffs_put_chunk_in_file(object, tags.chunk_id,
2354 if (ret_val == YAFFS_OK)
2355 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2359 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2362 int ret_val = YAFFS_OK;
2364 int is_checkpt_block;
2366 int chunks_before = yaffs_get_erased_chunks(dev);
2368 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2370 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2372 yaffs_trace(YAFFS_TRACE_TRACING,
2373 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2374 block, bi->pages_in_use, bi->has_shrink_hdr,
2377 /*yaffs_verify_free_chunks(dev); */
2379 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2380 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2382 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2384 dev->gc_disable = 1;
2386 yaffs_summary_gc(dev, block);
2388 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2389 yaffs_trace(YAFFS_TRACE_TRACING,
2390 "Collecting block %d that has no chunks in use",
2392 yaffs_block_became_dirty(dev, block);
2395 u8 *buffer = yaffs_get_temp_buffer(dev);
2397 yaffs_verify_blk(dev, bi, block);
2399 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2400 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2402 for (/* init already done */ ;
2403 ret_val == YAFFS_OK &&
2404 dev->gc_chunk < dev->param.chunks_per_block &&
2405 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2407 dev->gc_chunk++, old_chunk++) {
2408 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2409 /* Page is in use and might need to be copied */
2411 ret_val = yaffs_gc_process_chunk(dev, bi,
2415 yaffs_release_temp_buffer(dev, buffer);
2418 yaffs_verify_collected_blk(dev, bi, block);
2420 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2422 * The gc did not complete. Set block state back to FULL
2423 * because checkpointing does not restore gc.
2425 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2427 /* The gc completed. */
2428 /* Do any required cleanups */
2429 for (i = 0; i < dev->n_clean_ups; i++) {
2430 /* Time to delete the file too */
2431 struct yaffs_obj *object =
2432 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2434 yaffs_free_tnode(dev,
2435 object->variant.file_variant.top);
2436 object->variant.file_variant.top = NULL;
2437 yaffs_trace(YAFFS_TRACE_GC,
2438 "yaffs: About to finally delete object %d",
2440 yaffs_generic_obj_del(object);
2441 object->my_dev->n_deleted_files--;
2445 chunks_after = yaffs_get_erased_chunks(dev);
2446 if (chunks_before >= chunks_after)
2447 yaffs_trace(YAFFS_TRACE_GC,
2448 "gc did not increase free chunks before %d after %d",
2449 chunks_before, chunks_after);
2452 dev->n_clean_ups = 0;
2455 dev->gc_disable = 0;
2461 * find_gc_block() selects the dirtiest block (or close enough)
2462 * for garbage collection.
2465 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2466 int aggressive, int background)
2471 int prioritised = 0;
2472 int prioritised_exist = 0;
2473 struct yaffs_block_info *bi;
2474 u32 threshold = dev->param.chunks_per_block;
2478 /* First let's see if we need to grab a prioritised block */
2479 if (dev->has_pending_prioritised_gc && !aggressive) {
2480 dev->gc_dirtiest = 0;
2481 bi = dev->block_info;
2482 for (i = dev->internal_start_block;
2483 i <= dev->internal_end_block && !selected; i++) {
2485 if (bi->gc_prioritise) {
2486 prioritised_exist = 1;
2487 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2488 yaffs_block_ok_for_gc(dev, bi)) {
2497 * If there is a prioritised block and none was selected then
2498 * this happened because there is at least one old dirty block
2499 * gumming up the works. Let's gc the oldest dirty block.
2502 if (prioritised_exist &&
2503 !selected && dev->oldest_dirty_block > 0)
2504 selected = dev->oldest_dirty_block;
2506 if (!prioritised_exist) /* None found, so we can clear this */
2507 dev->has_pending_prioritised_gc = 0;
2510 /* If we're doing aggressive GC then we are happy to take a less-dirty
2511 * block, and search harder.
2512 * else (leasurely gc), then we only bother to do this if the
2513 * block has only a few pages in use.
2519 dev->internal_end_block - dev->internal_start_block + 1;
2521 threshold = dev->param.chunks_per_block;
2522 iterations = n_blocks;
2527 max_threshold = dev->param.chunks_per_block / 2;
2529 max_threshold = dev->param.chunks_per_block / 8;
2531 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2532 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2534 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2535 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2536 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2537 if (threshold > max_threshold)
2538 threshold = max_threshold;
2540 iterations = n_blocks / 16 + 1;
2541 if (iterations > 100)
2547 (dev->gc_dirtiest < 1 ||
2548 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2550 dev->gc_block_finder++;
2551 if (dev->gc_block_finder < dev->internal_start_block ||
2552 dev->gc_block_finder > dev->internal_end_block)
2553 dev->gc_block_finder =
2554 dev->internal_start_block;
2556 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2558 pages_used = bi->pages_in_use - bi->soft_del_pages;
2560 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2561 pages_used < dev->param.chunks_per_block &&
2562 (dev->gc_dirtiest < 1 ||
2563 pages_used < dev->gc_pages_in_use) &&
2564 yaffs_block_ok_for_gc(dev, bi)) {
2565 dev->gc_dirtiest = dev->gc_block_finder;
2566 dev->gc_pages_in_use = pages_used;
2570 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2571 selected = dev->gc_dirtiest;
2575 * If nothing has been selected for a while, try the oldest dirty
2576 * because that's gumming up the works.
2579 if (!selected && dev->param.is_yaffs2 &&
2580 dev->gc_not_done >= (background ? 10 : 20)) {
2581 yaffs2_find_oldest_dirty_seq(dev);
2582 if (dev->oldest_dirty_block > 0) {
2583 selected = dev->oldest_dirty_block;
2584 dev->gc_dirtiest = selected;
2585 dev->oldest_dirty_gc_count++;
2586 bi = yaffs_get_block_info(dev, selected);
2587 dev->gc_pages_in_use =
2588 bi->pages_in_use - bi->soft_del_pages;
2590 dev->gc_not_done = 0;
2595 yaffs_trace(YAFFS_TRACE_GC,
2596 "GC Selected block %d with %d free, prioritised:%d",
2598 dev->param.chunks_per_block - dev->gc_pages_in_use,
2605 dev->gc_dirtiest = 0;
2606 dev->gc_pages_in_use = 0;
2607 dev->gc_not_done = 0;
2608 if (dev->refresh_skip > 0)
2609 dev->refresh_skip--;
2612 yaffs_trace(YAFFS_TRACE_GC,
2613 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2614 dev->gc_block_finder, dev->gc_not_done, threshold,
2615 dev->gc_dirtiest, dev->gc_pages_in_use,
2616 dev->oldest_dirty_block, background ? " bg" : "");
2622 /* New garbage collector
2623 * If we're very low on erased blocks then we do aggressive garbage collection
2624 * otherwise we do "leasurely" garbage collection.
2625 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2626 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2628 * The idea is to help clear out space in a more spread-out manner.
2629 * Dunno if it really does anything useful.
2631 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2634 int gc_ok = YAFFS_OK;
2638 int checkpt_block_adjust;
2640 if (dev->param.gc_control_fn &&
2641 (dev->param.gc_control_fn(dev) & 1) == 0)
2644 if (dev->gc_disable)
2645 /* Bail out so we don't get recursive gc */
2648 /* This loop should pass the first time.
2649 * Only loops here if the collection does not increase space.
2655 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2658 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2660 dev->n_erased_blocks * dev->param.chunks_per_block;
2662 /* If we need a block soon then do aggressive gc. */
2663 if (dev->n_erased_blocks < min_erased)
2667 && erased_chunks > (dev->n_free_chunks / 4))
2670 if (dev->gc_skip > 20)
2672 if (erased_chunks < dev->n_free_chunks / 2 ||
2673 dev->gc_skip < 1 || background)
2683 /* If we don't already have a block being gc'd then see if we
2684 * should start another */
2686 if (dev->gc_block < 1 && !aggressive) {
2687 dev->gc_block = yaffs2_find_refresh_block(dev);
2689 dev->n_clean_ups = 0;
2691 if (dev->gc_block < 1) {
2693 yaffs_find_gc_block(dev, aggressive, background);
2695 dev->n_clean_ups = 0;
2698 if (dev->gc_block > 0) {
2701 dev->passive_gc_count++;
2703 yaffs_trace(YAFFS_TRACE_GC,
2704 "yaffs: GC n_erased_blocks %d aggressive %d",
2705 dev->n_erased_blocks, aggressive);
2707 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2710 if (dev->n_erased_blocks < (int)dev->param.n_reserved_blocks &&
2711 dev->gc_block > 0) {
2712 yaffs_trace(YAFFS_TRACE_GC,
2713 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2714 dev->n_erased_blocks, max_tries,
2717 } while ((dev->n_erased_blocks < (int)dev->param.n_reserved_blocks) &&
2718 (dev->gc_block > 0) && (max_tries < 2));
2720 return aggressive ? gc_ok : YAFFS_OK;
2725 * Garbage collects. Intended to be called from a background thread.
2726 * Returns non-zero if at least half the free chunks are erased.
2728 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2730 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2733 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2735 yaffs_check_gc(dev, 1);
2736 return erased_chunks > dev->n_free_chunks / 2;
2739 /*-------------------- Data file manipulation -----------------*/
2741 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2743 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2745 if (nand_chunk >= 0)
2746 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2749 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2750 "Chunk %d not found zero instead",
2752 /* get sane (zero) data if you read a hole */
2753 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2759 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2764 struct yaffs_ext_tags tags;
2765 struct yaffs_block_info *bi;
2772 block = chunk_id / dev->param.chunks_per_block;
2773 page = chunk_id % dev->param.chunks_per_block;
2775 if (!yaffs_check_chunk_bit(dev, block, page))
2776 yaffs_trace(YAFFS_TRACE_VERIFY,
2777 "Deleting invalid chunk %d", chunk_id);
2779 bi = yaffs_get_block_info(dev, block);
2781 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2783 yaffs_trace(YAFFS_TRACE_DELETION,
2784 "line %d delete of chunk %d",
2787 if (!dev->param.is_yaffs2 && mark_flash &&
2788 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2790 memset(&tags, 0, sizeof(tags));
2791 tags.is_deleted = 1;
2792 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2793 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2795 dev->n_unmarked_deletions++;
2798 /* Pull out of the management area.
2799 * If the whole block became dirty, this will kick off an erasure.
2801 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2802 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2803 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
2804 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2805 dev->n_free_chunks++;
2806 yaffs_clear_chunk_bit(dev, block, page);
2809 if (bi->pages_in_use == 0 &&
2810 !bi->has_shrink_hdr &&
2811 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2812 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
2813 yaffs_block_became_dirty(dev, block);
2818 int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2819 const u8 *buffer, int n_bytes, int use_reserve)
2821 /* Find old chunk Need to do this to get serial number
2822 * Write new one and patch into tree.
2823 * Invalidate old tags.
2827 struct yaffs_ext_tags prev_tags;
2829 struct yaffs_ext_tags new_tags;
2830 struct yaffs_dev *dev = in->my_dev;
2833 yaffs_check_gc(dev, 0);
2835 /* Get the previous chunk at this location in the file if it exists.
2836 * If it does not exist then put a zero into the tree. This creates
2837 * the tnode now, rather than later when it is harder to clean up.
2839 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
2840 if (prev_chunk_id < 1 &&
2841 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
2844 /* Set up new tags */
2845 memset(&new_tags, 0, sizeof(new_tags));
2847 new_tags.chunk_id = inode_chunk;
2848 new_tags.obj_id = in->obj_id;
2849 new_tags.serial_number =
2850 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
2851 new_tags.n_bytes = n_bytes;
2853 if (n_bytes < 1 || n_bytes > (int)dev->data_bytes_per_chunk) {
2854 yaffs_trace(YAFFS_TRACE_ERROR,
2855 "Writing %d bytes to chunk!!!!!!!!!",
2861 * If this is a data chunk and the write goes past the end of the stored
2862 * size then update the stored_size.
2864 if (inode_chunk > 0) {
2865 endpos = (inode_chunk - 1) * dev->data_bytes_per_chunk +
2867 if (in->variant.file_variant.stored_size < endpos)
2868 in->variant.file_variant.stored_size = endpos;
2872 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
2874 if (new_chunk_id > 0) {
2875 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
2877 if (prev_chunk_id > 0)
2878 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
2880 yaffs_verify_file_sane(in);
2882 return new_chunk_id;
2887 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
2888 const YCHAR *name, const void *value, int size,
2891 struct yaffs_xattr_mod xmod;
2899 xmod.result = -ENOSPC;
2901 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
2909 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
2910 struct yaffs_xattr_mod *xmod)
2913 int x_offs = sizeof(struct yaffs_obj_hdr);
2914 struct yaffs_dev *dev = obj->my_dev;
2915 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
2916 char *x_buffer = buffer + x_offs;
2920 nval_set(dev, x_buffer, x_size, xmod->name, xmod->data,
2921 xmod->size, xmod->flags);
2923 retval = nval_del(dev, x_buffer, x_size, xmod->name);
2925 obj->has_xattr = nval_hasvalues(dev, x_buffer, x_size);
2926 obj->xattr_known = 1;
2927 xmod->result = retval;
2932 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
2933 void *value, int size)
2935 char *buffer = NULL;
2937 struct yaffs_ext_tags tags;
2938 struct yaffs_dev *dev = obj->my_dev;
2939 int x_offs = sizeof(struct yaffs_obj_hdr);
2940 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
2944 if (obj->hdr_chunk < 1)
2947 /* If we know that the object has no xattribs then don't do all the
2948 * reading and parsing.
2950 if (obj->xattr_known && !obj->has_xattr) {
2957 buffer = (char *)yaffs_get_temp_buffer(dev);
2962 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
2964 if (result != YAFFS_OK)
2967 x_buffer = buffer + x_offs;
2969 if (!obj->xattr_known) {
2970 obj->has_xattr = nval_hasvalues(dev, x_buffer, x_size);
2971 obj->xattr_known = 1;
2975 retval = nval_get(dev, x_buffer, x_size,
2978 retval = nval_list(dev, x_buffer, x_size, value, size);
2980 yaffs_release_temp_buffer(dev, (u8 *) buffer);
2984 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
2985 const void *value, int size, int flags)
2987 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
2990 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
2992 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
2995 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
2998 return yaffs_do_xattrib_fetch(obj, name, value, size);
3001 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3003 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3006 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3009 struct yaffs_obj_hdr *oh;
3010 struct yaffs_dev *dev;
3011 struct yaffs_ext_tags tags;
3014 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3018 buf = yaffs_get_temp_buffer(dev);
3020 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3022 if (result == YAFFS_FAIL) {
3023 yaffs_release_temp_buffer(dev, buf);
3027 oh = (struct yaffs_obj_hdr *)buf;
3029 yaffs_do_endian_oh(dev, oh);
3031 in->lazy_loaded = 0;
3032 in->yst_mode = oh->yst_mode;
3033 yaffs_load_attribs(in, oh);
3034 yaffs_set_obj_name_from_oh(in, oh);
3036 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
3037 in->variant.symlink_variant.alias =
3038 yaffs_clone_str(oh->alias);
3039 yaffs_release_temp_buffer(dev, buf);
3042 /* UpdateObjectHeader updates the header on NAND for an object.
3043 * If name is not NULL, then that new name is used.
3045 * We're always creating the obj header from scratch (except reading
3046 * the old name) so first set up in cpu endianness then run it through
3047 * endian fixing at the end.
3049 * However, a twist: If there are xattribs we leave them as they were.
3051 * Careful! The buffer holds the whole chunk. Part of the chunk holds the
3052 * object header and the rest holds the xattribs, therefore we use a buffer
3053 * pointer and an oh pointer to point to the same memory.
3056 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3057 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3060 struct yaffs_block_info *bi;
3061 struct yaffs_dev *dev = in->my_dev;
3066 struct yaffs_ext_tags new_tags;
3067 struct yaffs_ext_tags old_tags;
3068 const YCHAR *alias = NULL;
3070 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3071 struct yaffs_obj_hdr *oh = NULL;
3072 loff_t file_size = 0;
3074 strcpy(old_name, _Y("silly old name"));
3076 if (in->fake && in != dev->root_dir && !force && !xmod)
3079 yaffs_check_gc(dev, 0);
3080 yaffs_check_obj_details_loaded(in);
3082 buffer = yaffs_get_temp_buffer(in->my_dev);
3083 oh = (struct yaffs_obj_hdr *)buffer;
3085 prev_chunk_id = in->hdr_chunk;
3087 if (prev_chunk_id > 0) {
3088 /* Access the old obj header just to read the name. */
3089 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3091 if (result == YAFFS_OK) {
3092 yaffs_verify_oh(in, oh, &old_tags, 0);
3093 memcpy(old_name, oh->name, sizeof(oh->name));
3096 * NB We only wipe the object header area because the rest of
3097 * the buffer might contain xattribs.
3099 memset(oh, 0xff, sizeof(*oh));
3102 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3105 oh->type = in->variant_type;
3106 oh->yst_mode = in->yst_mode;
3107 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3109 yaffs_load_attribs_oh(oh, in);
3112 oh->parent_obj_id = in->parent->obj_id;
3114 oh->parent_obj_id = 0;
3116 if (name && *name) {
3117 memset(oh->name, 0, sizeof(oh->name));
3118 yaffs_load_oh_from_name(dev, oh->name, name);
3119 } else if (prev_chunk_id > 0) {
3120 memcpy(oh->name, old_name, sizeof(oh->name));
3122 memset(oh->name, 0, sizeof(oh->name));
3125 oh->is_shrink = is_shrink;
3127 switch (in->variant_type) {
3128 case YAFFS_OBJECT_TYPE_UNKNOWN:
3129 /* Should not happen */
3131 case YAFFS_OBJECT_TYPE_FILE:
3132 if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
3133 oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
3134 file_size = in->variant.file_variant.stored_size;
3135 yaffs_oh_size_load(dev, oh, file_size, 0);
3137 case YAFFS_OBJECT_TYPE_HARDLINK:
3138 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3140 case YAFFS_OBJECT_TYPE_SPECIAL:
3143 case YAFFS_OBJECT_TYPE_DIRECTORY:
3146 case YAFFS_OBJECT_TYPE_SYMLINK:
3147 alias = in->variant.symlink_variant.alias;
3149 alias = _Y("no alias");
3150 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3151 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3155 /* process any xattrib modifications */
3157 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3160 memset(&new_tags, 0, sizeof(new_tags));
3162 new_tags.chunk_id = 0;
3163 new_tags.obj_id = in->obj_id;
3164 new_tags.serial_number = in->serial;
3166 /* Add extra info for file header */
3167 new_tags.extra_available = 1;
3168 new_tags.extra_parent_id = oh->parent_obj_id;
3169 new_tags.extra_file_size = file_size;
3170 new_tags.extra_is_shrink = oh->is_shrink;
3171 new_tags.extra_equiv_id = oh->equiv_id;
3172 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3173 new_tags.extra_obj_type = in->variant_type;
3175 /* Now endian swizzle the oh if needed. */
3176 yaffs_do_endian_oh(dev, oh);
3178 yaffs_verify_oh(in, oh, &new_tags, 1);
3180 /* Create new chunk in NAND */
3182 yaffs_write_new_chunk(dev, buffer, &new_tags,
3183 (prev_chunk_id > 0) ? 1 : 0);
3186 yaffs_release_temp_buffer(dev, buffer);
3188 if (new_chunk_id < 0)
3189 return new_chunk_id;
3191 in->hdr_chunk = new_chunk_id;
3193 if (prev_chunk_id > 0)
3194 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3196 if (!yaffs_obj_cache_dirty(in))
3199 /* If this was a shrink, then mark the block
3200 * that the chunk lives on */
3202 bi = yaffs_get_block_info(in->my_dev,
3204 in->my_dev->param.chunks_per_block);
3205 bi->has_shrink_hdr = 1;
3208 return new_chunk_id;
3211 /*--------------------- File read/write ------------------------
3212 * Read and write have very similar structures.
3213 * In general the read/write has three parts to it
3214 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3215 * Some complete chunks
3216 * An incomplete chunk to end off with
3218 * Curve-balls: the first chunk might also be the last chunk.
3221 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3228 struct yaffs_cache *cache;
3229 struct yaffs_dev *dev;
3234 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3237 /* OK now check for the curveball where the start and end are in
3240 if ((start + n) < dev->data_bytes_per_chunk)
3243 n_copy = dev->data_bytes_per_chunk - start;
3245 cache = yaffs_find_chunk_cache(in, chunk);
3247 /* If the chunk is already in the cache or it is less than
3248 * a whole chunk or we're using inband tags then use the cache
3249 * (if there is caching) else bypass the cache.
3251 if (cache || n_copy != (int)dev->data_bytes_per_chunk ||
3252 dev->param.inband_tags) {
3253 if (dev->param.n_caches > 0) {
3255 /* If we can't find the data in the cache,
3256 * then load it up. */
3260 yaffs_grab_chunk_cache(in->my_dev);
3262 cache->chunk_id = chunk;
3265 yaffs_rd_data_obj(in, chunk,
3270 yaffs_use_cache(dev, cache, 0);
3274 memcpy(buffer, &cache->data[start], n_copy);
3278 /* Read into the local buffer then copy.. */
3281 yaffs_get_temp_buffer(dev);
3282 yaffs_rd_data_obj(in, chunk, local_buffer);
3284 memcpy(buffer, &local_buffer[start], n_copy);
3286 yaffs_release_temp_buffer(dev, local_buffer);
3289 /* A full chunk. Read directly into the buffer. */
3290 yaffs_rd_data_obj(in, chunk, buffer);
3300 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3301 int n_bytes, int write_through)
3310 loff_t start_write = offset;
3311 int chunk_written = 0;
3314 struct yaffs_dev *dev;
3318 while (n > 0 && chunk_written >= 0) {
3319 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3321 if (((loff_t)chunk) *
3322 dev->data_bytes_per_chunk + start != offset ||
3323 start >= dev->data_bytes_per_chunk) {
3324 yaffs_trace(YAFFS_TRACE_ERROR,
3325 "AddrToChunk of offset %lld gives chunk %d start %d",
3326 (long long)offset, chunk, start);
3328 chunk++; /* File pos to chunk in file offset */
3330 /* OK now check for the curveball where the start and end are in
3334 if ((start + n) < dev->data_bytes_per_chunk) {
3337 /* Now calculate how many bytes to write back....
3338 * If we're overwriting and not writing to then end of
3339 * file then we need to write back as much as was there
3343 chunk_start = (((loff_t)(chunk - 1)) *
3344 dev->data_bytes_per_chunk);
3346 if (chunk_start > in->variant.file_variant.file_size)
3347 n_bytes_read = 0; /* Past end of file */
3350 in->variant.file_variant.file_size -
3353 if (n_bytes_read > dev->data_bytes_per_chunk)
3354 n_bytes_read = dev->data_bytes_per_chunk;
3358 (start + n)) ? n_bytes_read : (start + n);
3360 if (n_writeback < 0 ||
3361 n_writeback > (int)dev->data_bytes_per_chunk)
3365 n_copy = dev->data_bytes_per_chunk - start;
3366 n_writeback = dev->data_bytes_per_chunk;
3369 if (n_copy != (int)dev->data_bytes_per_chunk ||
3370 !dev->param.cache_bypass_aligned ||
3371 dev->param.inband_tags) {
3372 /* An incomplete start or end chunk (or maybe both
3373 * start and end chunk), or we're using inband tags,
3374 * or we're forcing writes through the cache,
3375 * so we want to use the cache buffers.
3377 if (dev->param.n_caches > 0) {
3378 struct yaffs_cache *cache;
3380 /* If we can't find the data in the cache, then
3382 cache = yaffs_find_chunk_cache(in, chunk);
3385 yaffs_check_alloc_available(dev, 1)) {
3386 cache = yaffs_grab_chunk_cache(dev);
3388 cache->chunk_id = chunk;
3391 yaffs_rd_data_obj(in, chunk,
3395 !yaffs_check_alloc_available(dev,
3397 /* Drop the cache if it was a read cache
3398 * item and no space check has been made
3405 yaffs_use_cache(dev, cache, 1);
3408 memcpy(&cache->data[start], buffer,
3412 cache->n_bytes = n_writeback;
3414 if (write_through) {
3424 chunk_written = -1; /* fail write */
3427 /* An incomplete start or end chunk (or maybe
3428 * both start and end chunk). Read into the
3429 * local buffer then copy over and write back.
3432 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3434 yaffs_rd_data_obj(in, chunk, local_buffer);
3435 memcpy(&local_buffer[start], buffer, n_copy);
3438 yaffs_wr_data_obj(in, chunk,
3442 yaffs_release_temp_buffer(dev, local_buffer);
3445 /* A full chunk. Write directly from the buffer. */
3448 yaffs_wr_data_obj(in, chunk, buffer,
3449 dev->data_bytes_per_chunk, 0);
3451 /* Since we've overwritten the cached data,
3452 * we better invalidate it. */
3453 yaffs_invalidate_chunk_cache(in, chunk);
3456 if (chunk_written >= 0) {
3464 /* Update file object */
3466 if ((start_write + n_done) > in->variant.file_variant.file_size)
3467 in->variant.file_variant.file_size = (start_write + n_done);
3473 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3474 int n_bytes, int write_through)
3476 yaffs2_handle_hole(in, offset);
3477 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
3480 /* ---------------------- File resizing stuff ------------------ */
3482 static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
3485 struct yaffs_dev *dev = in->my_dev;
3486 loff_t old_size = in->variant.file_variant.file_size;
3494 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3498 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3499 &start_del, &dummy);
3503 /* Delete backwards so that we don't end up with holes if
3504 * power is lost part-way through the operation.
3506 for (i = last_del; i >= start_del; i--) {
3507 /* NB this could be optimised somewhat,
3508 * eg. could retrieve the tags and write them without
3509 * using yaffs_chunk_del
3512 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3518 (dev->internal_start_block * dev->param.chunks_per_block) ||
3520 ((dev->internal_end_block + 1) *
3521 dev->param.chunks_per_block)) {
3522 yaffs_trace(YAFFS_TRACE_ALWAYS,
3523 "Found daft chunk_id %d for %d",
3526 in->n_data_chunks--;
3527 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3532 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3536 struct yaffs_dev *dev = obj->my_dev;
3538 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3540 yaffs_prune_chunks(obj, new_size);
3542 if (new_partial != 0) {
3543 int last_chunk = 1 + new_full;
3544 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3546 /* Rewrite the last chunk with its new size and zero pad */
3547 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3548 memset(local_buffer + new_partial, 0,
3549 dev->data_bytes_per_chunk - new_partial);
3551 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3554 yaffs_release_temp_buffer(dev, local_buffer);
3557 obj->variant.file_variant.file_size = new_size;
3558 obj->variant.file_variant.stored_size = new_size;
3560 yaffs_prune_tree(dev, &obj->variant.file_variant);
3563 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3565 struct yaffs_dev *dev = in->my_dev;
3566 loff_t old_size = in->variant.file_variant.file_size;
3568 yaffs_flush_file_cache(in, 1);
3569 yaffs_invalidate_file_cache(in);
3571 yaffs_check_gc(dev, 0);
3573 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3576 if (new_size == old_size)
3579 if (new_size > old_size) {
3580 yaffs2_handle_hole(in, new_size);
3581 in->variant.file_variant.file_size = new_size;
3583 /* new_size < old_size */
3584 yaffs_resize_file_down(in, new_size);
3587 /* Write a new object header to reflect the resize.
3588 * show we've shrunk the file, if need be
3589 * Do this only if the file is not in the deleted directories
3590 * and is not shadowed.
3594 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3595 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3596 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3601 int yaffs_flush_file(struct yaffs_obj *in,
3609 yaffs_flush_file_cache(in, discard_cache);
3615 yaffs_load_current_time(in, 0, 0);
3617 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3618 YAFFS_OK : YAFFS_FAIL;
3622 /* yaffs_del_file deletes the whole file data
3623 * and the inode associated with the file.
3624 * It does not delete the links associated with the file.
3626 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3630 struct yaffs_dev *dev = in->my_dev;
3637 yaffs_change_obj_name(in, in->my_dev->del_dir,
3638 _Y("deleted"), 0, 0);
3639 yaffs_trace(YAFFS_TRACE_TRACING,
3640 "yaffs: immediate deletion of file %d",
3643 in->my_dev->n_deleted_files++;
3644 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3645 yaffs_resize_file(in, 0);
3646 yaffs_soft_del_file(in);
3649 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3650 _Y("unlinked"), 0, 0);
3655 static int yaffs_del_file(struct yaffs_obj *in)
3657 int ret_val = YAFFS_OK;
3658 int deleted; /* Need to cache value on stack if in is freed */
3659 struct yaffs_dev *dev = in->my_dev;
3661 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3662 yaffs_resize_file(in, 0);
3664 if (in->n_data_chunks > 0) {
3665 /* Use soft deletion if there is data in the file.
3666 * That won't be the case if it has been resized to zero.
3669 ret_val = yaffs_unlink_file_if_needed(in);
3671 deleted = in->deleted;
3673 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3676 in->my_dev->n_deleted_files++;
3677 yaffs_soft_del_file(in);
3679 return deleted ? YAFFS_OK : YAFFS_FAIL;
3681 /* The file has no data chunks so we toss it immediately */
3682 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3683 in->variant.file_variant.top = NULL;
3684 yaffs_generic_obj_del(in);
3690 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3693 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3694 !(list_empty(&obj->variant.dir_variant.children));
3697 static int yaffs_del_dir(struct yaffs_obj *obj)
3699 /* First check that the directory is empty. */
3700 if (yaffs_is_non_empty_dir(obj))
3703 return yaffs_generic_obj_del(obj);
3706 static int yaffs_del_symlink(struct yaffs_obj *in)
3708 kfree(in->variant.symlink_variant.alias);
3709 in->variant.symlink_variant.alias = NULL;
3711 return yaffs_generic_obj_del(in);
3714 static int yaffs_del_link(struct yaffs_obj *in)
3716 /* remove this hardlink from the list associated with the equivalent
3719 list_del_init(&in->hard_links);
3720 return yaffs_generic_obj_del(in);
3723 int yaffs_del_obj(struct yaffs_obj *obj)
3727 switch (obj->variant_type) {
3728 case YAFFS_OBJECT_TYPE_FILE:
3729 ret_val = yaffs_del_file(obj);
3731 case YAFFS_OBJECT_TYPE_DIRECTORY:
3732 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3733 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3734 "Remove object %d from dirty directories",
3736 list_del_init(&obj->variant.dir_variant.dirty);
3738 return yaffs_del_dir(obj);
3740 case YAFFS_OBJECT_TYPE_SYMLINK:
3741 ret_val = yaffs_del_symlink(obj);
3743 case YAFFS_OBJECT_TYPE_HARDLINK:
3744 ret_val = yaffs_del_link(obj);
3746 case YAFFS_OBJECT_TYPE_SPECIAL:
3747 ret_val = yaffs_generic_obj_del(obj);
3749 case YAFFS_OBJECT_TYPE_UNKNOWN:
3751 break; /* should not happen. */
3757 static void yaffs_empty_dir_to_dir(struct yaffs_obj *from_dir,
3758 struct yaffs_obj *to_dir)
3760 struct yaffs_obj *obj;
3761 struct list_head *lh;
3762 struct list_head *n;
3764 list_for_each_safe(lh, n, &from_dir->variant.dir_variant.children) {
3765 obj = list_entry(lh, struct yaffs_obj, siblings);
3766 yaffs_add_obj_to_dir(to_dir, obj);
3770 struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
3771 enum yaffs_obj_type type)
3773 /* Tear down the old variant */
3774 switch (obj->variant_type) {
3775 case YAFFS_OBJECT_TYPE_FILE:
3776 /* Nuke file data */
3777 yaffs_resize_file(obj, 0);
3778 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
3779 obj->variant.file_variant.top = NULL;
3781 case YAFFS_OBJECT_TYPE_DIRECTORY:
3782 /* Put the children in lost and found. */
3783 yaffs_empty_dir_to_dir(obj, obj->my_dev->lost_n_found);
3784 if (!list_empty(&obj->variant.dir_variant.dirty))
3785 list_del_init(&obj->variant.dir_variant.dirty);
3787 case YAFFS_OBJECT_TYPE_SYMLINK:
3788 /* Nuke symplink data */
3789 kfree(obj->variant.symlink_variant.alias);
3790 obj->variant.symlink_variant.alias = NULL;
3792 case YAFFS_OBJECT_TYPE_HARDLINK:
3793 list_del_init(&obj->hard_links);
3799 memset(&obj->variant, 0, sizeof(obj->variant));
3801 /*Set up new variant if the memset is not enough. */
3803 case YAFFS_OBJECT_TYPE_DIRECTORY:
3804 INIT_LIST_HEAD(&obj->variant.dir_variant.children);
3805 INIT_LIST_HEAD(&obj->variant.dir_variant.dirty);
3807 case YAFFS_OBJECT_TYPE_FILE:
3808 case YAFFS_OBJECT_TYPE_SYMLINK:
3809 case YAFFS_OBJECT_TYPE_HARDLINK:
3814 obj->variant_type = type;
3820 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3830 yaffs_update_parent(obj->parent);
3832 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3833 return yaffs_del_link(obj);
3834 } else if (!list_empty(&obj->hard_links)) {
3835 /* Curve ball: We're unlinking an object that has a hardlink.
3837 * This problem arises because we are not strictly following
3838 * The Linux link/inode model.
3840 * We can't really delete the object.
3841 * Instead, we do the following:
3842 * - Select a hardlink.
3843 * - Unhook it from the hard links
3844 * - Move it from its parent directory so that the rename works.
3845 * - Rename the object to the hardlink's name.
3846 * - Delete the hardlink
3849 struct yaffs_obj *hl;
3850 struct yaffs_obj *parent;
3852 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3854 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3857 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3858 parent = hl->parent;
3860 list_del_init(&hl->hard_links);
3862 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3864 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
3866 if (ret_val == YAFFS_OK)
3867 ret_val = yaffs_generic_obj_del(hl);
3871 } else if (del_now) {
3872 switch (obj->variant_type) {
3873 case YAFFS_OBJECT_TYPE_FILE:
3874 return yaffs_del_file(obj);
3876 case YAFFS_OBJECT_TYPE_DIRECTORY:
3877 list_del_init(&obj->variant.dir_variant.dirty);
3878 return yaffs_del_dir(obj);
3880 case YAFFS_OBJECT_TYPE_SYMLINK:
3881 return yaffs_del_symlink(obj);
3883 case YAFFS_OBJECT_TYPE_SPECIAL:
3884 return yaffs_generic_obj_del(obj);
3886 case YAFFS_OBJECT_TYPE_HARDLINK:
3887 case YAFFS_OBJECT_TYPE_UNKNOWN:
3891 } else if (yaffs_is_non_empty_dir(obj)) {
3894 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
3895 _Y("unlinked"), 0, 0);
3899 int yaffs_unlink_obj(struct yaffs_obj *obj)
3901 if (obj && obj->unlink_allowed)
3902 return yaffs_unlink_worker(obj);
3907 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
3909 struct yaffs_obj *obj;
3911 obj = yaffs_find_by_name(dir, name);
3912 return yaffs_unlink_obj(obj);
3916 * If old_name is NULL then we take old_dir as the object to be renamed.
3918 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
3919 struct yaffs_obj *new_dir, const YCHAR *new_name)
3921 struct yaffs_obj *obj = NULL;
3922 struct yaffs_obj *existing_target = NULL;
3925 struct yaffs_dev *dev;
3927 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3931 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3936 dev = old_dir->my_dev;
3938 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
3939 /* Special case for case insemsitive systems.
3940 * While look-up is case insensitive, the name isn't.
3941 * Therefore we might want to change x.txt to X.txt
3943 if (old_dir == new_dir &&
3944 old_name && new_name &&
3945 strcmp(old_name, new_name) == 0)
3949 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
3950 YAFFS_MAX_NAME_LENGTH)
3955 obj = yaffs_find_by_name(old_dir, old_name);
3958 old_dir = obj->parent;
3961 if (obj && obj->rename_allowed) {
3962 /* Now handle an existing target, if there is one */
3963 existing_target = yaffs_find_by_name(new_dir, new_name);
3964 if (yaffs_is_non_empty_dir(existing_target)) {
3965 return YAFFS_FAIL; /* ENOTEMPTY */
3966 } else if (existing_target && existing_target != obj) {
3967 /* Nuke the target first, using shadowing,
3968 * but only if it isn't the same object.
3970 * Note we must disable gc here otherwise it can mess
3974 dev->gc_disable = 1;
3975 yaffs_change_obj_name(obj, new_dir, new_name, force,
3976 existing_target->obj_id);
3977 existing_target->is_shadowed = 1;
3978 yaffs_unlink_obj(existing_target);
3979 dev->gc_disable = 0;
3982 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
3984 yaffs_update_parent(old_dir);
3985 if (new_dir != old_dir)
3986 yaffs_update_parent(new_dir);
3993 /*----------------------- Initialisation Scanning ---------------------- */
3995 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
3996 int backward_scanning)
3998 struct yaffs_obj *obj;
4000 if (backward_scanning) {
4001 /* Handle YAFFS2 case (backward scanning)
4002 * If the shadowed object exists then ignore.
4004 obj = yaffs_find_by_number(dev, obj_id);
4009 /* Let's create it (if it does not exist) assuming it is a file so that
4010 * it can do shrinking etc.
4011 * We put it in unlinked dir to be cleaned up after the scanning
4014 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4017 obj->is_shadowed = 1;
4018 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4019 obj->variant.file_variant.shrink_size = 0;
4020 obj->valid = 1; /* So that we don't read any other info. */
4023 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4025 struct list_head *lh;
4026 struct list_head *save;
4027 struct yaffs_obj *hl;
4028 struct yaffs_obj *in;
4030 list_for_each_safe(lh, save, hard_list) {
4031 hl = list_entry(lh, struct yaffs_obj, hard_links);
4032 in = yaffs_find_by_number(dev,
4033 hl->variant.hardlink_variant.equiv_id);
4036 /* Add the hardlink pointers */
4037 hl->variant.hardlink_variant.equiv_obj = in;
4038 list_add(&hl->hard_links, &in->hard_links);
4040 /* Todo Need to report/handle this better.
4041 * Got a problem... hardlink to a non-existant object
4043 hl->variant.hardlink_variant.equiv_obj = NULL;
4044 INIT_LIST_HEAD(&hl->hard_links);
4049 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4052 * Sort out state of unlinked and deleted objects after scanning.
4054 struct list_head *i;
4055 struct list_head *n;
4056 struct yaffs_obj *l;
4061 /* Soft delete all the unlinked files */
4062 list_for_each_safe(i, n,
4063 &dev->unlinked_dir->variant.dir_variant.children) {
4064 l = list_entry(i, struct yaffs_obj, siblings);
4068 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4069 l = list_entry(i, struct yaffs_obj, siblings);
4075 * This code iterates through all the objects making sure that they are rooted.
4076 * Any unrooted objects are re-rooted in lost+found.
4077 * An object needs to be in one of:
4078 * - Directly under deleted, unlinked
4079 * - Directly or indirectly under root.
4082 * This code assumes that we don't ever change the current relationships
4083 * between directories:
4084 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4085 * lost-n-found->parent == root_dir
4087 * This fixes the problem where directories might have inadvertently been
4088 * deleted leaving the object "hanging" without being rooted in the
4092 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4094 return (obj == dev->del_dir ||
4095 obj == dev->unlinked_dir || obj == dev->root_dir);
4098 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4100 struct yaffs_obj *obj;
4101 struct yaffs_obj *parent;
4103 struct list_head *lh;
4104 struct list_head *n;
4111 /* Iterate through the objects in each hash entry,
4112 * looking at each object.
4113 * Make sure it is rooted.
4116 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4117 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4118 obj = list_entry(lh, struct yaffs_obj, hash_link);
4119 parent = obj->parent;
4121 if (yaffs_has_null_parent(dev, obj)) {
4122 /* These directories are not hanging */
4124 } else if (!parent ||
4125 parent->variant_type !=
4126 YAFFS_OBJECT_TYPE_DIRECTORY) {
4128 } else if (yaffs_has_null_parent(dev, parent)) {
4132 * Need to follow the parent chain to
4133 * see if it is hanging.
4138 while (parent != dev->root_dir &&
4140 parent->parent->variant_type ==
4141 YAFFS_OBJECT_TYPE_DIRECTORY &&
4143 parent = parent->parent;
4146 if (parent != dev->root_dir)
4150 yaffs_trace(YAFFS_TRACE_SCAN,
4151 "Hanging object %d moved to lost and found",
4153 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4160 * Delete directory contents for cleaning up lost and found.
4162 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4164 struct yaffs_obj *obj;
4165 struct list_head *lh;
4166 struct list_head *n;
4168 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4171 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4172 obj = list_entry(lh, struct yaffs_obj, siblings);
4173 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4174 yaffs_del_dir_contents(obj);
4175 yaffs_trace(YAFFS_TRACE_SCAN,
4176 "Deleting lost_found object %d",
4178 yaffs_unlink_obj(obj);
4182 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4184 yaffs_del_dir_contents(dev->lost_n_found);
4188 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4192 struct list_head *i;
4193 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4194 struct yaffs_obj *l;
4200 yaffs_trace(YAFFS_TRACE_ALWAYS,
4201 "tragedy: yaffs_find_by_name: null pointer directory"
4206 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4207 yaffs_trace(YAFFS_TRACE_ALWAYS,
4208 "tragedy: yaffs_find_by_name: non-directory"
4213 sum = yaffs_calc_name_sum(name);
4215 list_for_each(i, &directory->variant.dir_variant.children) {
4216 l = list_entry(i, struct yaffs_obj, siblings);
4218 if (l->parent != directory)
4221 yaffs_check_obj_details_loaded(l);
4223 /* Special case for lost-n-found */
4224 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4225 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4227 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4228 /* LostnFound chunk called Objxxx
4231 yaffs_get_obj_name(l, buffer,
4232 YAFFS_MAX_NAME_LENGTH + 1);
4233 if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
4240 /* GetEquivalentObject dereferences any hard links to get to the
4244 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4246 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4247 obj = obj->variant.hardlink_variant.equiv_obj;
4248 yaffs_check_obj_details_loaded(obj);
4254 * A note or two on object names.
4255 * * If the object name is missing, we then make one up in the form objnnn
4257 * * ASCII names are stored in the object header's name field from byte zero
4258 * * Unicode names are historically stored starting from byte zero.
4260 * Then there are automatic Unicode names...
4261 * The purpose of these is to save names in a way that can be read as
4262 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4263 * system to share files.
4265 * These automatic unicode are stored slightly differently...
4266 * - If the name can fit in the ASCII character space then they are saved as
4267 * ascii names as per above.
4268 * - If the name needs Unicode then the name is saved in Unicode
4269 * starting at oh->name[1].
4272 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4275 /* Create an object name if we could not find one. */
4276 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4277 YCHAR local_name[20];
4278 YCHAR num_string[20];
4279 YCHAR *x = &num_string[19];
4280 unsigned v = obj->obj_id;
4284 *x = '0' + (v % 10);
4287 /* make up a name */
4288 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4289 strcat(local_name, x);
4290 strncpy(name, local_name, buffer_size - 1);
4294 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4296 memset(name, 0, buffer_size * sizeof(YCHAR));
4297 yaffs_check_obj_details_loaded(obj);
4298 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4299 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4300 } else if (obj->short_name[0]) {
4301 strcpy(name, obj->short_name);
4302 } else if (obj->hdr_chunk > 0) {
4304 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4306 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4308 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4310 if (obj->hdr_chunk > 0) {
4311 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4312 obj->hdr_chunk, buffer, NULL);
4313 if (result == YAFFS_OK)
4314 yaffs_load_name_from_oh(obj->my_dev, name,
4315 oh->name, buffer_size);
4317 yaffs_release_temp_buffer(obj->my_dev, buffer);
4320 yaffs_fix_null_name(obj, name, buffer_size);
4322 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4325 loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
4327 /* Dereference any hard linking */
4328 obj = yaffs_get_equivalent_obj(obj);
4330 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4331 return obj->variant.file_variant.file_size;
4332 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4333 if (!obj->variant.symlink_variant.alias)
4335 return strnlen(obj->variant.symlink_variant.alias,
4336 YAFFS_MAX_ALIAS_LENGTH);
4338 /* Only a directory should drop through to here */
4339 return obj->my_dev->data_bytes_per_chunk;
4343 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4346 struct list_head *i;
4349 count++; /* the object itself */
4351 list_for_each(i, &obj->hard_links)
4352 count++; /* add the hard links; */
4357 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4359 obj = yaffs_get_equivalent_obj(obj);
4364 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4366 obj = yaffs_get_equivalent_obj(obj);
4368 switch (obj->variant_type) {
4369 case YAFFS_OBJECT_TYPE_FILE:
4372 case YAFFS_OBJECT_TYPE_DIRECTORY:
4375 case YAFFS_OBJECT_TYPE_SYMLINK:
4378 case YAFFS_OBJECT_TYPE_HARDLINK:
4381 case YAFFS_OBJECT_TYPE_SPECIAL:
4382 if (S_ISFIFO(obj->yst_mode))
4384 if (S_ISCHR(obj->yst_mode))
4386 if (S_ISBLK(obj->yst_mode))
4388 if (S_ISSOCK(obj->yst_mode))
4398 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4400 obj = yaffs_get_equivalent_obj(obj);
4401 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4402 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4404 return yaffs_clone_str(_Y(""));
4407 /*--------------------------- Initialisation code -------------------------- */
4409 static int yaffs_check_dev_fns(struct yaffs_dev *dev)
4411 struct yaffs_driver *drv = &dev->drv;
4412 struct yaffs_tags_handler *tagger = &dev->tagger;
4414 /* Common functions, gotta have */
4415 if (!drv->drv_read_chunk_fn ||
4416 !drv->drv_write_chunk_fn ||
4420 if (dev->param.is_yaffs2 &&
4421 (!drv->drv_mark_bad_fn || !drv->drv_check_bad_fn))
4424 /* Install the default tags marshalling functions if needed. */
4425 yaffs_tags_compat_install(dev);
4426 yaffs_tags_marshall_install(dev);
4428 /* Check we now have the marshalling functions required. */
4429 if (!tagger->write_chunk_tags_fn ||
4430 !tagger->read_chunk_tags_fn ||
4431 !tagger->query_block_fn ||
4432 !tagger->mark_bad_fn)
4438 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4440 /* Initialise the unlinked, deleted, root and lost+found directories */
4441 dev->lost_n_found = NULL;
4442 dev->root_dir = NULL;
4443 dev->unlinked_dir = NULL;
4444 dev->del_dir = NULL;
4447 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4449 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4451 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4452 YAFFS_ROOT_MODE | S_IFDIR);
4454 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4455 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4457 if (dev->lost_n_found &&
4459 dev->unlinked_dir &&
4461 /* If lost-n-found is hidden then yank it out of the directory tree. */
4462 if (dev->param.hide_lost_n_found)
4463 list_del_init(&dev->lost_n_found->siblings);
4465 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4472 * Typically only used by yaffs_guts_initialise, but also used by the
4473 * Low level yaffs driver tests.
4476 int yaffs_guts_ll_init(struct yaffs_dev *dev)
4480 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_ll_init()");
4483 yaffs_trace(YAFFS_TRACE_ALWAYS,
4484 "yaffs: Need a device"
4492 dev->internal_start_block = dev->param.start_block;
4493 dev->internal_end_block = dev->param.end_block;
4494 dev->block_offset = 0;
4495 dev->chunk_offset = 0;
4496 dev->n_free_chunks = 0;
4500 if (dev->param.start_block == 0) {
4501 dev->internal_start_block = dev->param.start_block + 1;
4502 dev->internal_end_block = dev->param.end_block + 1;
4503 dev->block_offset = 1;
4504 dev->chunk_offset = dev->param.chunks_per_block;
4507 /* Check geometry parameters. */
4509 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4510 dev->param.total_bytes_per_chunk < 1024) ||
4511 (!dev->param.is_yaffs2 &&
4512 dev->param.total_bytes_per_chunk < 512) ||
4513 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4514 dev->param.chunks_per_block < 2 ||
4515 dev->param.n_reserved_blocks < 2 ||
4516 dev->internal_start_block <= 0 ||
4517 dev->internal_end_block <= 0 ||
4518 dev->internal_end_block <=
4519 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4521 /* otherwise it is too small */
4522 yaffs_trace(YAFFS_TRACE_ALWAYS,
4523 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4524 dev->param.total_bytes_per_chunk,
4525 dev->param.is_yaffs2 ? "2" : "",
4526 dev->param.inband_tags);
4530 /* Sort out space for inband tags, if required */
4531 if (dev->param.inband_tags)
4532 dev->data_bytes_per_chunk =
4533 dev->param.total_bytes_per_chunk -
4534 sizeof(struct yaffs_packed_tags2_tags_only);
4536 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4538 /* Got the right mix of functions? */
4539 if (!yaffs_check_dev_fns(dev)) {
4540 /* Function missing */
4541 yaffs_trace(YAFFS_TRACE_ALWAYS,
4542 "device function(s) missing or wrong");
4547 if (!yaffs_init_tmp_buffers(dev))
4550 if (yaffs_init_nand(dev) != YAFFS_OK) {
4551 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4561 int yaffs_guts_format_dev(struct yaffs_dev *dev)
4564 enum yaffs_block_state state;
4567 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4573 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
4574 yaffs_query_init_block_state(dev, i, &state, &dummy);
4575 if (state != YAFFS_BLOCK_STATE_DEAD)
4576 yaffs_erase_block(dev, i);
4583 * If the dev is mounted r/w then the cleanup will happen during
4584 * yaffs_guts_initialise. However if the dev is mounted ro then
4585 * the cleanup will be dfered until yaffs is remounted r/w.
4587 void yaffs_guts_cleanup(struct yaffs_dev *dev)
4589 yaffs_strip_deleted_objs(dev);
4590 yaffs_fix_hanging_objs(dev);
4591 if (dev->param.empty_lost_n_found)
4592 yaffs_empty_l_n_f(dev);
4595 int yaffs_guts_initialise(struct yaffs_dev *dev)
4597 int init_failed = 0;
4601 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4604 if (dev->is_mounted) {
4605 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4609 dev->is_mounted = 1;
4611 /* OK now calculate a few things for the device */
4614 * Calculate all the chunk size manipulation numbers:
4616 x = dev->data_bytes_per_chunk;
4617 /* We always use dev->chunk_shift and dev->chunk_div */
4618 dev->chunk_shift = calc_shifts(x);
4619 x >>= dev->chunk_shift;
4621 /* We only use chunk mask if chunk_div is 1 */
4622 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4625 * Calculate chunk_grp_bits.
4626 * We need to find the next power of 2 > than internal_end_block
4629 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4631 bits = calc_shifts_ceiling(x);
4633 /* Set up tnode width if wide tnodes are enabled. */
4634 if (!dev->param.wide_tnodes_disabled) {
4635 /* bits must be even so that we end up with 32-bit words */
4639 dev->tnode_width = 16;
4641 dev->tnode_width = bits;
4643 dev->tnode_width = 16;
4646 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4648 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4649 * so if the bitwidth of the
4650 * chunk range we're using is greater than 16 we need
4651 * to figure out chunk shift and chunk_grp_size
4654 if (bits <= dev->tnode_width)
4655 dev->chunk_grp_bits = 0;
4657 dev->chunk_grp_bits = bits - dev->tnode_width;
4659 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4660 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4661 dev->tnode_size = sizeof(struct yaffs_tnode);
4663 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4665 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4666 /* We have a problem because the soft delete won't work if
4667 * the chunk group size > chunks per block.
4668 * This can be remedied by using larger "virtual blocks".
4670 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4675 /* Finished verifying the device, continue with initialisation */
4677 /* More device initialisation */
4679 dev->passive_gc_count = 0;
4680 dev->oldest_dirty_gc_count = 0;
4682 dev->gc_block_finder = 0;
4683 dev->buffered_block = -1;
4684 dev->doing_buffered_block_rewrite = 0;
4685 dev->n_deleted_files = 0;
4686 dev->n_bg_deletions = 0;
4687 dev->n_unlinked_files = 0;
4688 dev->n_ecc_fixed = 0;
4689 dev->n_ecc_unfixed = 0;
4690 dev->n_tags_ecc_fixed = 0;
4691 dev->n_tags_ecc_unfixed = 0;
4692 dev->n_erase_failures = 0;
4693 dev->n_erased_blocks = 0;
4694 dev->gc_disable = 0;
4695 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now,
4696 * will get fixed on first GC */
4697 INIT_LIST_HEAD(&dev->dirty_dirs);
4698 dev->oldest_dirty_seq = 0;
4699 dev->oldest_dirty_block = 0;
4701 yaffs_endian_config(dev);
4703 /* Initialise temporary caches. */
4704 dev->gc_cleanup_list = NULL;
4707 init_failed = yaffs_cache_init(dev) < 0;
4709 dev->cache_hits = 0;
4712 dev->gc_cleanup_list =
4713 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4715 if (!dev->gc_cleanup_list)
4719 if (dev->param.is_yaffs2)
4720 dev->param.use_header_file_size = 1;
4722 if (!init_failed && !yaffs_init_blocks(dev))
4725 yaffs_init_tnodes_and_objs(dev);
4727 if (!init_failed && !yaffs_create_initial_dir(dev))
4730 if (!init_failed && dev->param.is_yaffs2 &&
4731 !dev->param.disable_summary &&
4732 !yaffs_summary_init(dev))
4736 /* Now scan the flash. */
4737 if (dev->param.is_yaffs2) {
4738 if (yaffs2_checkpt_restore(dev)) {
4739 yaffs_check_obj_details_loaded(dev->root_dir);
4740 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4742 "yaffs: restored from checkpoint"
4746 /* Clean up the mess caused by an aborted
4747 * checkpoint load then scan backwards.
4749 yaffs_deinit_blocks(dev);
4751 yaffs_deinit_tnodes_and_objs(dev);
4753 dev->n_erased_blocks = 0;
4754 dev->n_free_chunks = 0;
4755 dev->alloc_block = -1;
4756 dev->alloc_page = -1;
4757 dev->n_deleted_files = 0;
4758 dev->n_unlinked_files = 0;
4759 dev->n_bg_deletions = 0;
4761 if (!init_failed && !yaffs_init_blocks(dev))
4764 yaffs_init_tnodes_and_objs(dev);
4767 && !yaffs_create_initial_dir(dev))
4770 if (!init_failed && !yaffs2_scan_backwards(dev))
4773 } else if (!yaffs1_scan(dev)) {
4777 yaffs_guts_cleanup(dev);
4781 /* Clean up the mess */
4782 yaffs_trace(YAFFS_TRACE_TRACING,
4783 "yaffs: yaffs_guts_initialise() aborted.");
4785 yaffs_deinitialise(dev);
4789 /* Zero out stats */
4790 dev->n_page_reads = 0;
4791 dev->n_page_writes = 0;
4792 dev->n_erasures = 0;
4793 dev->n_gc_copies = 0;
4794 dev->n_retried_writes = 0;
4796 dev->n_retired_blocks = 0;
4798 yaffs_verify_free_chunks(dev);
4799 yaffs_verify_blocks(dev);
4801 /* Clean up any aborted checkpoint data */
4802 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4803 yaffs2_checkpt_invalidate(dev);
4805 yaffs_trace(YAFFS_TRACE_TRACING,
4806 "yaffs: yaffs_guts_initialise() done.");
4810 void yaffs_deinitialise(struct yaffs_dev *dev)
4812 if (dev->is_mounted) {
4815 yaffs_deinit_blocks(dev);
4816 yaffs_deinit_tnodes_and_objs(dev);
4817 yaffs_summary_deinit(dev);
4818 yaffs_cache_deinit(dev);
4820 kfree(dev->gc_cleanup_list);
4822 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4823 kfree(dev->temp_buffer[i].buffer);
4824 dev->temp_buffer[i].buffer = NULL;
4827 kfree(dev->checkpt_buffer);
4828 dev->checkpt_buffer = NULL;
4829 kfree(dev->checkpt_block_list);
4830 dev->checkpt_block_list = NULL;
4833 dev->is_mounted = 0;
4835 yaffs_deinit_nand(dev);
4839 int yaffs_count_free_chunks(struct yaffs_dev *dev)
4843 struct yaffs_block_info *blk;
4845 blk = dev->block_info;
4846 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4847 switch (blk->block_state) {
4848 case YAFFS_BLOCK_STATE_EMPTY:
4849 case YAFFS_BLOCK_STATE_ALLOCATING:
4850 case YAFFS_BLOCK_STATE_COLLECTING:
4851 case YAFFS_BLOCK_STATE_FULL:
4853 (dev->param.chunks_per_block - blk->pages_in_use +
4854 blk->soft_del_pages);
4864 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
4866 /* This is what we report to the outside world */
4869 int blocks_for_checkpt;
4871 n_free = dev->n_free_chunks;
4872 n_free += dev->n_deleted_files;
4874 /* Now count and subtract the number of dirty chunks in the cache. */
4875 n_dirty_caches = yaffs_count_dirty_caches(dev);
4876 n_free -= n_dirty_caches;
4879 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
4881 /* Now figure checkpoint space and report that... */
4882 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
4884 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
4893 * Marshalling functions to get the appropriate time values saved
4894 * and restored to/from obj headers.
4896 * Note that the WinCE time fields are used to store the 32-bit values.
4899 static void yaffs_oh_time_load(u32 *yst_time, u32 *win_time, YTIME_T timeval)
4904 lower = timeval & 0xffffffff;
4905 if (sizeof(YTIME_T) > sizeof(u32))
4906 upper = (timeval >> 32) & 0xffffffff;
4911 win_time[0] = lower;
4912 win_time[1] = upper;
4915 static YTIME_T yaffs_oh_time_fetch(const u32 *yst_time, const u32 *win_time)
4920 if (win_time[1] == 0xffffffff) {
4924 upper = win_time[1];
4925 lower = win_time[0];
4927 if (sizeof(YTIME_T) > sizeof(u32)) {
4929 ret = (((u64)upper) << 32) | lower;
4930 return (YTIME_T) ret;
4933 return (YTIME_T) lower;
4936 YTIME_T yaffs_oh_ctime_fetch(struct yaffs_obj_hdr *oh)
4938 return yaffs_oh_time_fetch(&oh->yst_ctime, oh->win_ctime);
4941 YTIME_T yaffs_oh_mtime_fetch(struct yaffs_obj_hdr *oh)
4943 return yaffs_oh_time_fetch(&oh->yst_mtime, oh->win_mtime);
4946 YTIME_T yaffs_oh_atime_fetch(struct yaffs_obj_hdr *oh)
4948 return yaffs_oh_time_fetch(&oh->yst_atime, oh->win_atime);
4951 void yaffs_oh_ctime_load(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
4953 yaffs_oh_time_load(&oh->yst_ctime, oh->win_ctime, obj->yst_ctime);
4956 void yaffs_oh_mtime_load(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
4958 yaffs_oh_time_load(&oh->yst_mtime, oh->win_mtime, obj->yst_mtime);
4961 void yaffs_oh_atime_load(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
4963 yaffs_oh_time_load(&oh->yst_atime, oh->win_atime, obj->yst_atime);
4968 * Marshalling functions to get loff_t file sizes into and out of
4971 void yaffs_oh_size_load(struct yaffs_dev *dev,
4972 struct yaffs_obj_hdr *oh,
4976 oh->file_size_low = FSIZE_LOW(fsize);
4978 oh->file_size_high = FSIZE_HIGH(fsize);
4981 yaffs_do_endian_u32(dev, &oh->file_size_low);
4982 yaffs_do_endian_u32(dev, &oh->file_size_high);
4986 loff_t yaffs_oh_to_size(struct yaffs_dev *dev, struct yaffs_obj_hdr *oh,
4992 if (sizeof(loff_t) >= 8 && ~(oh->file_size_high)) {
4993 u32 low = oh->file_size_low;
4994 u32 high = oh->file_size_high;
4997 yaffs_do_endian_u32 (dev, &low);
4998 yaffs_do_endian_u32 (dev, &high);
5000 retval = FSIZE_COMBINE(high, low);
5002 u32 low = oh->file_size_low;
5005 yaffs_do_endian_u32(dev, &low);
5006 retval = (loff_t)low;
5013 void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
5016 struct yaffs_block_info *bi;
5019 for(i = 0; i < 10; i++)
5022 for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
5023 bi = yaffs_get_block_info(dev, i);
5024 s = bi->block_state;
5025 if(s > YAFFS_BLOCK_STATE_DEAD || s < YAFFS_BLOCK_STATE_UNKNOWN)