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)
3025 oh = (struct yaffs_obj_hdr *)buf;
3027 yaffs_do_endian_oh(dev, oh);
3029 in->lazy_loaded = 0;
3030 in->yst_mode = oh->yst_mode;
3031 yaffs_load_attribs(in, oh);
3032 yaffs_set_obj_name_from_oh(in, oh);
3034 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
3035 in->variant.symlink_variant.alias =
3036 yaffs_clone_str(oh->alias);
3037 yaffs_release_temp_buffer(dev, buf);
3040 /* UpdateObjectHeader updates the header on NAND for an object.
3041 * If name is not NULL, then that new name is used.
3043 * We're always creating the obj header from scratch (except reading
3044 * the old name) so first set up in cpu endianness then run it through
3045 * endian fixing at the end.
3047 * However, a twist: If there are xattribs we leave them as they were.
3049 * Careful! The buffer holds the whole chunk. Part of the chunk holds the
3050 * object header and the rest holds the xattribs, therefore we use a buffer
3051 * pointer and an oh pointer to point to the same memory.
3054 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3055 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3058 struct yaffs_block_info *bi;
3059 struct yaffs_dev *dev = in->my_dev;
3064 struct yaffs_ext_tags new_tags;
3065 struct yaffs_ext_tags old_tags;
3066 const YCHAR *alias = NULL;
3068 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3069 struct yaffs_obj_hdr *oh = NULL;
3070 loff_t file_size = 0;
3072 strcpy(old_name, _Y("silly old name"));
3074 if (in->fake && in != dev->root_dir && !force && !xmod)
3077 yaffs_check_gc(dev, 0);
3078 yaffs_check_obj_details_loaded(in);
3080 buffer = yaffs_get_temp_buffer(in->my_dev);
3081 oh = (struct yaffs_obj_hdr *)buffer;
3083 prev_chunk_id = in->hdr_chunk;
3085 if (prev_chunk_id > 0) {
3086 /* Access the old obj header just to read the name. */
3087 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3089 if (result == YAFFS_OK) {
3090 yaffs_verify_oh(in, oh, &old_tags, 0);
3091 memcpy(old_name, oh->name, sizeof(oh->name));
3094 * NB We only wipe the object header area because the rest of
3095 * the buffer might contain xattribs.
3097 memset(oh, 0xff, sizeof(*oh));
3100 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3103 oh->type = in->variant_type;
3104 oh->yst_mode = in->yst_mode;
3105 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3107 yaffs_load_attribs_oh(oh, in);
3110 oh->parent_obj_id = in->parent->obj_id;
3112 oh->parent_obj_id = 0;
3114 if (name && *name) {
3115 memset(oh->name, 0, sizeof(oh->name));
3116 yaffs_load_oh_from_name(dev, oh->name, name);
3117 } else if (prev_chunk_id > 0) {
3118 memcpy(oh->name, old_name, sizeof(oh->name));
3120 memset(oh->name, 0, sizeof(oh->name));
3123 oh->is_shrink = is_shrink;
3125 switch (in->variant_type) {
3126 case YAFFS_OBJECT_TYPE_UNKNOWN:
3127 /* Should not happen */
3129 case YAFFS_OBJECT_TYPE_FILE:
3130 if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
3131 oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
3132 file_size = in->variant.file_variant.stored_size;
3133 yaffs_oh_size_load(dev, oh, file_size, 0);
3135 case YAFFS_OBJECT_TYPE_HARDLINK:
3136 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3138 case YAFFS_OBJECT_TYPE_SPECIAL:
3141 case YAFFS_OBJECT_TYPE_DIRECTORY:
3144 case YAFFS_OBJECT_TYPE_SYMLINK:
3145 alias = in->variant.symlink_variant.alias;
3147 alias = _Y("no alias");
3148 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3149 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3153 /* process any xattrib modifications */
3155 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3158 memset(&new_tags, 0, sizeof(new_tags));
3160 new_tags.chunk_id = 0;
3161 new_tags.obj_id = in->obj_id;
3162 new_tags.serial_number = in->serial;
3164 /* Add extra info for file header */
3165 new_tags.extra_available = 1;
3166 new_tags.extra_parent_id = oh->parent_obj_id;
3167 new_tags.extra_file_size = file_size;
3168 new_tags.extra_is_shrink = oh->is_shrink;
3169 new_tags.extra_equiv_id = oh->equiv_id;
3170 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3171 new_tags.extra_obj_type = in->variant_type;
3173 /* Now endian swizzle the oh if needed. */
3174 yaffs_do_endian_oh(dev, oh);
3176 yaffs_verify_oh(in, oh, &new_tags, 1);
3178 /* Create new chunk in NAND */
3180 yaffs_write_new_chunk(dev, buffer, &new_tags,
3181 (prev_chunk_id > 0) ? 1 : 0);
3184 yaffs_release_temp_buffer(dev, buffer);
3186 if (new_chunk_id < 0)
3187 return new_chunk_id;
3189 in->hdr_chunk = new_chunk_id;
3191 if (prev_chunk_id > 0)
3192 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3194 if (!yaffs_obj_cache_dirty(in))
3197 /* If this was a shrink, then mark the block
3198 * that the chunk lives on */
3200 bi = yaffs_get_block_info(in->my_dev,
3202 in->my_dev->param.chunks_per_block);
3203 bi->has_shrink_hdr = 1;
3207 return new_chunk_id;
3210 /*--------------------- File read/write ------------------------
3211 * Read and write have very similar structures.
3212 * In general the read/write has three parts to it
3213 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3214 * Some complete chunks
3215 * An incomplete chunk to end off with
3217 * Curve-balls: the first chunk might also be the last chunk.
3220 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3227 struct yaffs_cache *cache;
3228 struct yaffs_dev *dev;
3233 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3236 /* OK now check for the curveball where the start and end are in
3239 if ((start + n) < dev->data_bytes_per_chunk)
3242 n_copy = dev->data_bytes_per_chunk - start;
3244 cache = yaffs_find_chunk_cache(in, chunk);
3246 /* If the chunk is already in the cache or it is less than
3247 * a whole chunk or we're using inband tags then use the cache
3248 * (if there is caching) else bypass the cache.
3250 if (cache || n_copy != (int)dev->data_bytes_per_chunk ||
3251 dev->param.inband_tags) {
3252 if (dev->param.n_caches > 0) {
3254 /* If we can't find the data in the cache,
3255 * then load it up. */
3259 yaffs_grab_chunk_cache(in->my_dev);
3261 cache->chunk_id = chunk;
3264 yaffs_rd_data_obj(in, chunk,
3269 yaffs_use_cache(dev, cache, 0);
3273 memcpy(buffer, &cache->data[start], n_copy);
3277 /* Read into the local buffer then copy.. */
3280 yaffs_get_temp_buffer(dev);
3281 yaffs_rd_data_obj(in, chunk, local_buffer);
3283 memcpy(buffer, &local_buffer[start], n_copy);
3285 yaffs_release_temp_buffer(dev, local_buffer);
3288 /* A full chunk. Read directly into the buffer. */
3289 yaffs_rd_data_obj(in, chunk, buffer);
3299 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3300 int n_bytes, int write_through)
3309 loff_t start_write = offset;
3310 int chunk_written = 0;
3313 struct yaffs_dev *dev;
3317 while (n > 0 && chunk_written >= 0) {
3318 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3320 if (((loff_t)chunk) *
3321 dev->data_bytes_per_chunk + start != offset ||
3322 start >= dev->data_bytes_per_chunk) {
3323 yaffs_trace(YAFFS_TRACE_ERROR,
3324 "AddrToChunk of offset %lld gives chunk %d start %d",
3325 (long long)offset, chunk, start);
3327 chunk++; /* File pos to chunk in file offset */
3329 /* OK now check for the curveball where the start and end are in
3333 if ((start + n) < dev->data_bytes_per_chunk) {
3336 /* Now calculate how many bytes to write back....
3337 * If we're overwriting and not writing to then end of
3338 * file then we need to write back as much as was there
3342 chunk_start = (((loff_t)(chunk - 1)) *
3343 dev->data_bytes_per_chunk);
3345 if (chunk_start > in->variant.file_variant.file_size)
3346 n_bytes_read = 0; /* Past end of file */
3349 in->variant.file_variant.file_size -
3352 if (n_bytes_read > dev->data_bytes_per_chunk)
3353 n_bytes_read = dev->data_bytes_per_chunk;
3357 (start + n)) ? n_bytes_read : (start + n);
3359 if (n_writeback < 0 ||
3360 n_writeback > (int)dev->data_bytes_per_chunk)
3364 n_copy = dev->data_bytes_per_chunk - start;
3365 n_writeback = dev->data_bytes_per_chunk;
3368 if (n_copy != (int)dev->data_bytes_per_chunk ||
3369 !dev->param.cache_bypass_aligned ||
3370 dev->param.inband_tags) {
3371 /* An incomplete start or end chunk (or maybe both
3372 * start and end chunk), or we're using inband tags,
3373 * or we're forcing writes through the cache,
3374 * so we want to use the cache buffers.
3376 if (dev->param.n_caches > 0) {
3377 struct yaffs_cache *cache;
3379 /* If we can't find the data in the cache, then
3381 cache = yaffs_find_chunk_cache(in, chunk);
3384 yaffs_check_alloc_available(dev, 1)) {
3385 cache = yaffs_grab_chunk_cache(dev);
3387 cache->chunk_id = chunk;
3390 yaffs_rd_data_obj(in, chunk,
3394 !yaffs_check_alloc_available(dev,
3396 /* Drop the cache if it was a read cache
3397 * item and no space check has been made
3404 yaffs_use_cache(dev, cache, 1);
3407 memcpy(&cache->data[start], buffer,
3411 cache->n_bytes = n_writeback;
3413 if (write_through) {
3423 chunk_written = -1; /* fail write */
3426 /* An incomplete start or end chunk (or maybe
3427 * both start and end chunk). Read into the
3428 * local buffer then copy over and write back.
3431 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3433 yaffs_rd_data_obj(in, chunk, local_buffer);
3434 memcpy(&local_buffer[start], buffer, n_copy);
3437 yaffs_wr_data_obj(in, chunk,
3441 yaffs_release_temp_buffer(dev, local_buffer);
3444 /* A full chunk. Write directly from the buffer. */
3447 yaffs_wr_data_obj(in, chunk, buffer,
3448 dev->data_bytes_per_chunk, 0);
3450 /* Since we've overwritten the cached data,
3451 * we better invalidate it. */
3452 yaffs_invalidate_chunk_cache(in, chunk);
3455 if (chunk_written >= 0) {
3463 /* Update file object */
3465 if ((start_write + n_done) > in->variant.file_variant.file_size)
3466 in->variant.file_variant.file_size = (start_write + n_done);
3472 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3473 int n_bytes, int write_through)
3475 yaffs2_handle_hole(in, offset);
3476 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
3479 /* ---------------------- File resizing stuff ------------------ */
3481 static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
3484 struct yaffs_dev *dev = in->my_dev;
3485 loff_t old_size = in->variant.file_variant.file_size;
3493 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3497 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3498 &start_del, &dummy);
3502 /* Delete backwards so that we don't end up with holes if
3503 * power is lost part-way through the operation.
3505 for (i = last_del; i >= start_del; i--) {
3506 /* NB this could be optimised somewhat,
3507 * eg. could retrieve the tags and write them without
3508 * using yaffs_chunk_del
3511 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3517 (dev->internal_start_block * dev->param.chunks_per_block) ||
3519 ((dev->internal_end_block + 1) *
3520 dev->param.chunks_per_block)) {
3521 yaffs_trace(YAFFS_TRACE_ALWAYS,
3522 "Found daft chunk_id %d for %d",
3525 in->n_data_chunks--;
3526 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3531 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3535 struct yaffs_dev *dev = obj->my_dev;
3537 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3539 yaffs_prune_chunks(obj, new_size);
3541 if (new_partial != 0) {
3542 int last_chunk = 1 + new_full;
3543 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3545 /* Rewrite the last chunk with its new size and zero pad */
3546 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3547 memset(local_buffer + new_partial, 0,
3548 dev->data_bytes_per_chunk - new_partial);
3550 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3553 yaffs_release_temp_buffer(dev, local_buffer);
3556 obj->variant.file_variant.file_size = new_size;
3557 obj->variant.file_variant.stored_size = new_size;
3559 yaffs_prune_tree(dev, &obj->variant.file_variant);
3562 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3564 struct yaffs_dev *dev = in->my_dev;
3565 loff_t old_size = in->variant.file_variant.file_size;
3567 yaffs_flush_file_cache(in, 1);
3568 yaffs_invalidate_file_cache(in);
3570 yaffs_check_gc(dev, 0);
3572 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3575 if (new_size == old_size)
3578 if (new_size > old_size) {
3579 yaffs2_handle_hole(in, new_size);
3580 in->variant.file_variant.file_size = new_size;
3582 /* new_size < old_size */
3583 yaffs_resize_file_down(in, new_size);
3586 /* Write a new object header to reflect the resize.
3587 * show we've shrunk the file, if need be
3588 * Do this only if the file is not in the deleted directories
3589 * and is not shadowed.
3593 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3594 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3595 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3600 int yaffs_flush_file(struct yaffs_obj *in,
3608 yaffs_flush_file_cache(in, discard_cache);
3614 yaffs_load_current_time(in, 0, 0);
3616 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3617 YAFFS_OK : YAFFS_FAIL;
3621 /* yaffs_del_file deletes the whole file data
3622 * and the inode associated with the file.
3623 * It does not delete the links associated with the file.
3625 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3629 struct yaffs_dev *dev = in->my_dev;
3636 yaffs_change_obj_name(in, in->my_dev->del_dir,
3637 _Y("deleted"), 0, 0);
3638 yaffs_trace(YAFFS_TRACE_TRACING,
3639 "yaffs: immediate deletion of file %d",
3642 in->my_dev->n_deleted_files++;
3643 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3644 yaffs_resize_file(in, 0);
3645 yaffs_soft_del_file(in);
3648 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3649 _Y("unlinked"), 0, 0);
3654 static int yaffs_del_file(struct yaffs_obj *in)
3656 int ret_val = YAFFS_OK;
3657 int deleted; /* Need to cache value on stack if in is freed */
3658 struct yaffs_dev *dev = in->my_dev;
3660 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3661 yaffs_resize_file(in, 0);
3663 if (in->n_data_chunks > 0) {
3664 /* Use soft deletion if there is data in the file.
3665 * That won't be the case if it has been resized to zero.
3668 ret_val = yaffs_unlink_file_if_needed(in);
3670 deleted = in->deleted;
3672 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3675 in->my_dev->n_deleted_files++;
3676 yaffs_soft_del_file(in);
3678 return deleted ? YAFFS_OK : YAFFS_FAIL;
3680 /* The file has no data chunks so we toss it immediately */
3681 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3682 in->variant.file_variant.top = NULL;
3683 yaffs_generic_obj_del(in);
3689 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3692 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3693 !(list_empty(&obj->variant.dir_variant.children));
3696 static int yaffs_del_dir(struct yaffs_obj *obj)
3698 /* First check that the directory is empty. */
3699 if (yaffs_is_non_empty_dir(obj))
3702 return yaffs_generic_obj_del(obj);
3705 static int yaffs_del_symlink(struct yaffs_obj *in)
3707 kfree(in->variant.symlink_variant.alias);
3708 in->variant.symlink_variant.alias = NULL;
3710 return yaffs_generic_obj_del(in);
3713 static int yaffs_del_link(struct yaffs_obj *in)
3715 /* remove this hardlink from the list associated with the equivalent
3718 list_del_init(&in->hard_links);
3719 return yaffs_generic_obj_del(in);
3722 int yaffs_del_obj(struct yaffs_obj *obj)
3726 switch (obj->variant_type) {
3727 case YAFFS_OBJECT_TYPE_FILE:
3728 ret_val = yaffs_del_file(obj);
3730 case YAFFS_OBJECT_TYPE_DIRECTORY:
3731 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3732 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3733 "Remove object %d from dirty directories",
3735 list_del_init(&obj->variant.dir_variant.dirty);
3737 return yaffs_del_dir(obj);
3739 case YAFFS_OBJECT_TYPE_SYMLINK:
3740 ret_val = yaffs_del_symlink(obj);
3742 case YAFFS_OBJECT_TYPE_HARDLINK:
3743 ret_val = yaffs_del_link(obj);
3745 case YAFFS_OBJECT_TYPE_SPECIAL:
3746 ret_val = yaffs_generic_obj_del(obj);
3748 case YAFFS_OBJECT_TYPE_UNKNOWN:
3750 break; /* should not happen. */
3756 static void yaffs_empty_dir_to_dir(struct yaffs_obj *from_dir,
3757 struct yaffs_obj *to_dir)
3759 struct yaffs_obj *obj;
3760 struct list_head *lh;
3761 struct list_head *n;
3763 list_for_each_safe(lh, n, &from_dir->variant.dir_variant.children) {
3764 obj = list_entry(lh, struct yaffs_obj, siblings);
3765 yaffs_add_obj_to_dir(to_dir, obj);
3769 struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
3770 enum yaffs_obj_type type)
3772 /* Tear down the old variant */
3773 switch (obj->variant_type) {
3774 case YAFFS_OBJECT_TYPE_FILE:
3775 /* Nuke file data */
3776 yaffs_resize_file(obj, 0);
3777 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
3778 obj->variant.file_variant.top = NULL;
3780 case YAFFS_OBJECT_TYPE_DIRECTORY:
3781 /* Put the children in lost and found. */
3782 yaffs_empty_dir_to_dir(obj, obj->my_dev->lost_n_found);
3783 if (!list_empty(&obj->variant.dir_variant.dirty))
3784 list_del_init(&obj->variant.dir_variant.dirty);
3786 case YAFFS_OBJECT_TYPE_SYMLINK:
3787 /* Nuke symplink data */
3788 kfree(obj->variant.symlink_variant.alias);
3789 obj->variant.symlink_variant.alias = NULL;
3791 case YAFFS_OBJECT_TYPE_HARDLINK:
3792 list_del_init(&obj->hard_links);
3798 memset(&obj->variant, 0, sizeof(obj->variant));
3800 /*Set up new variant if the memset is not enough. */
3802 case YAFFS_OBJECT_TYPE_DIRECTORY:
3803 INIT_LIST_HEAD(&obj->variant.dir_variant.children);
3804 INIT_LIST_HEAD(&obj->variant.dir_variant.dirty);
3806 case YAFFS_OBJECT_TYPE_FILE:
3807 case YAFFS_OBJECT_TYPE_SYMLINK:
3808 case YAFFS_OBJECT_TYPE_HARDLINK:
3813 obj->variant_type = type;
3819 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3829 yaffs_update_parent(obj->parent);
3831 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3832 return yaffs_del_link(obj);
3833 } else if (!list_empty(&obj->hard_links)) {
3834 /* Curve ball: We're unlinking an object that has a hardlink.
3836 * This problem arises because we are not strictly following
3837 * The Linux link/inode model.
3839 * We can't really delete the object.
3840 * Instead, we do the following:
3841 * - Select a hardlink.
3842 * - Unhook it from the hard links
3843 * - Move it from its parent directory so that the rename works.
3844 * - Rename the object to the hardlink's name.
3845 * - Delete the hardlink
3848 struct yaffs_obj *hl;
3849 struct yaffs_obj *parent;
3851 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3853 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3856 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3857 parent = hl->parent;
3859 list_del_init(&hl->hard_links);
3861 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3863 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
3865 if (ret_val == YAFFS_OK)
3866 ret_val = yaffs_generic_obj_del(hl);
3870 } else if (del_now) {
3871 switch (obj->variant_type) {
3872 case YAFFS_OBJECT_TYPE_FILE:
3873 return yaffs_del_file(obj);
3875 case YAFFS_OBJECT_TYPE_DIRECTORY:
3876 list_del_init(&obj->variant.dir_variant.dirty);
3877 return yaffs_del_dir(obj);
3879 case YAFFS_OBJECT_TYPE_SYMLINK:
3880 return yaffs_del_symlink(obj);
3882 case YAFFS_OBJECT_TYPE_SPECIAL:
3883 return yaffs_generic_obj_del(obj);
3885 case YAFFS_OBJECT_TYPE_HARDLINK:
3886 case YAFFS_OBJECT_TYPE_UNKNOWN:
3890 } else if (yaffs_is_non_empty_dir(obj)) {
3893 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
3894 _Y("unlinked"), 0, 0);
3898 int yaffs_unlink_obj(struct yaffs_obj *obj)
3900 if (obj && obj->unlink_allowed)
3901 return yaffs_unlink_worker(obj);
3906 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
3908 struct yaffs_obj *obj;
3910 obj = yaffs_find_by_name(dir, name);
3911 return yaffs_unlink_obj(obj);
3915 * If old_name is NULL then we take old_dir as the object to be renamed.
3917 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
3918 struct yaffs_obj *new_dir, const YCHAR *new_name)
3920 struct yaffs_obj *obj = NULL;
3921 struct yaffs_obj *existing_target = NULL;
3924 struct yaffs_dev *dev;
3926 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3930 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3935 dev = old_dir->my_dev;
3937 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
3938 /* Special case for case insemsitive systems.
3939 * While look-up is case insensitive, the name isn't.
3940 * Therefore we might want to change x.txt to X.txt
3942 if (old_dir == new_dir &&
3943 old_name && new_name &&
3944 strcmp(old_name, new_name) == 0)
3948 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
3949 YAFFS_MAX_NAME_LENGTH)
3954 obj = yaffs_find_by_name(old_dir, old_name);
3957 old_dir = obj->parent;
3960 if (obj && obj->rename_allowed) {
3961 /* Now handle an existing target, if there is one */
3962 existing_target = yaffs_find_by_name(new_dir, new_name);
3963 if (yaffs_is_non_empty_dir(existing_target)) {
3964 return YAFFS_FAIL; /* ENOTEMPTY */
3965 } else if (existing_target && existing_target != obj) {
3966 /* Nuke the target first, using shadowing,
3967 * but only if it isn't the same object.
3969 * Note we must disable gc here otherwise it can mess
3973 dev->gc_disable = 1;
3974 yaffs_change_obj_name(obj, new_dir, new_name, force,
3975 existing_target->obj_id);
3976 existing_target->is_shadowed = 1;
3977 yaffs_unlink_obj(existing_target);
3978 dev->gc_disable = 0;
3981 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
3983 yaffs_update_parent(old_dir);
3984 if (new_dir != old_dir)
3985 yaffs_update_parent(new_dir);
3992 /*----------------------- Initialisation Scanning ---------------------- */
3994 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
3995 int backward_scanning)
3997 struct yaffs_obj *obj;
3999 if (backward_scanning) {
4000 /* Handle YAFFS2 case (backward scanning)
4001 * If the shadowed object exists then ignore.
4003 obj = yaffs_find_by_number(dev, obj_id);
4008 /* Let's create it (if it does not exist) assuming it is a file so that
4009 * it can do shrinking etc.
4010 * We put it in unlinked dir to be cleaned up after the scanning
4013 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4016 obj->is_shadowed = 1;
4017 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4018 obj->variant.file_variant.shrink_size = 0;
4019 obj->valid = 1; /* So that we don't read any other info. */
4022 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4024 struct list_head *lh;
4025 struct list_head *save;
4026 struct yaffs_obj *hl;
4027 struct yaffs_obj *in;
4029 list_for_each_safe(lh, save, hard_list) {
4030 hl = list_entry(lh, struct yaffs_obj, hard_links);
4031 in = yaffs_find_by_number(dev,
4032 hl->variant.hardlink_variant.equiv_id);
4035 /* Add the hardlink pointers */
4036 hl->variant.hardlink_variant.equiv_obj = in;
4037 list_add(&hl->hard_links, &in->hard_links);
4039 /* Todo Need to report/handle this better.
4040 * Got a problem... hardlink to a non-existant object
4042 hl->variant.hardlink_variant.equiv_obj = NULL;
4043 INIT_LIST_HEAD(&hl->hard_links);
4048 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4051 * Sort out state of unlinked and deleted objects after scanning.
4053 struct list_head *i;
4054 struct list_head *n;
4055 struct yaffs_obj *l;
4060 /* Soft delete all the unlinked files */
4061 list_for_each_safe(i, n,
4062 &dev->unlinked_dir->variant.dir_variant.children) {
4063 l = list_entry(i, struct yaffs_obj, siblings);
4067 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4068 l = list_entry(i, struct yaffs_obj, siblings);
4074 * This code iterates through all the objects making sure that they are rooted.
4075 * Any unrooted objects are re-rooted in lost+found.
4076 * An object needs to be in one of:
4077 * - Directly under deleted, unlinked
4078 * - Directly or indirectly under root.
4081 * This code assumes that we don't ever change the current relationships
4082 * between directories:
4083 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4084 * lost-n-found->parent == root_dir
4086 * This fixes the problem where directories might have inadvertently been
4087 * deleted leaving the object "hanging" without being rooted in the
4091 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4093 return (obj == dev->del_dir ||
4094 obj == dev->unlinked_dir || obj == dev->root_dir);
4097 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4099 struct yaffs_obj *obj;
4100 struct yaffs_obj *parent;
4102 struct list_head *lh;
4103 struct list_head *n;
4110 /* Iterate through the objects in each hash entry,
4111 * looking at each object.
4112 * Make sure it is rooted.
4115 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4116 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4117 obj = list_entry(lh, struct yaffs_obj, hash_link);
4118 parent = obj->parent;
4120 if (yaffs_has_null_parent(dev, obj)) {
4121 /* These directories are not hanging */
4123 } else if (!parent ||
4124 parent->variant_type !=
4125 YAFFS_OBJECT_TYPE_DIRECTORY) {
4127 } else if (yaffs_has_null_parent(dev, parent)) {
4131 * Need to follow the parent chain to
4132 * see if it is hanging.
4137 while (parent != dev->root_dir &&
4139 parent->parent->variant_type ==
4140 YAFFS_OBJECT_TYPE_DIRECTORY &&
4142 parent = parent->parent;
4145 if (parent != dev->root_dir)
4149 yaffs_trace(YAFFS_TRACE_SCAN,
4150 "Hanging object %d moved to lost and found",
4152 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4159 * Delete directory contents for cleaning up lost and found.
4161 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4163 struct yaffs_obj *obj;
4164 struct list_head *lh;
4165 struct list_head *n;
4167 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4170 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4171 obj = list_entry(lh, struct yaffs_obj, siblings);
4172 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4173 yaffs_del_dir_contents(obj);
4174 yaffs_trace(YAFFS_TRACE_SCAN,
4175 "Deleting lost_found object %d",
4177 yaffs_unlink_obj(obj);
4181 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4183 yaffs_del_dir_contents(dev->lost_n_found);
4187 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4191 struct list_head *i;
4192 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4193 struct yaffs_obj *l;
4199 yaffs_trace(YAFFS_TRACE_ALWAYS,
4200 "tragedy: yaffs_find_by_name: null pointer directory"
4205 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4206 yaffs_trace(YAFFS_TRACE_ALWAYS,
4207 "tragedy: yaffs_find_by_name: non-directory"
4212 sum = yaffs_calc_name_sum(name);
4214 list_for_each(i, &directory->variant.dir_variant.children) {
4215 l = list_entry(i, struct yaffs_obj, siblings);
4217 if (l->parent != directory)
4220 yaffs_check_obj_details_loaded(l);
4222 /* Special case for lost-n-found */
4223 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4224 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4226 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4227 /* LostnFound chunk called Objxxx
4230 yaffs_get_obj_name(l, buffer,
4231 YAFFS_MAX_NAME_LENGTH + 1);
4232 if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
4239 /* GetEquivalentObject dereferences any hard links to get to the
4243 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4245 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4246 obj = obj->variant.hardlink_variant.equiv_obj;
4247 yaffs_check_obj_details_loaded(obj);
4253 * A note or two on object names.
4254 * * If the object name is missing, we then make one up in the form objnnn
4256 * * ASCII names are stored in the object header's name field from byte zero
4257 * * Unicode names are historically stored starting from byte zero.
4259 * Then there are automatic Unicode names...
4260 * The purpose of these is to save names in a way that can be read as
4261 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4262 * system to share files.
4264 * These automatic unicode are stored slightly differently...
4265 * - If the name can fit in the ASCII character space then they are saved as
4266 * ascii names as per above.
4267 * - If the name needs Unicode then the name is saved in Unicode
4268 * starting at oh->name[1].
4271 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4274 /* Create an object name if we could not find one. */
4275 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4276 YCHAR local_name[20];
4277 YCHAR num_string[20];
4278 YCHAR *x = &num_string[19];
4279 unsigned v = obj->obj_id;
4283 *x = '0' + (v % 10);
4286 /* make up a name */
4287 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4288 strcat(local_name, x);
4289 strncpy(name, local_name, buffer_size - 1);
4293 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4295 memset(name, 0, buffer_size * sizeof(YCHAR));
4296 yaffs_check_obj_details_loaded(obj);
4297 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4298 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4299 } else if (obj->short_name[0]) {
4300 strcpy(name, obj->short_name);
4301 } else if (obj->hdr_chunk > 0) {
4303 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4305 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4307 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4309 if (obj->hdr_chunk > 0) {
4310 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4311 obj->hdr_chunk, buffer, NULL);
4312 if (result == YAFFS_OK)
4313 yaffs_load_name_from_oh(obj->my_dev, name,
4314 oh->name, buffer_size);
4316 yaffs_release_temp_buffer(obj->my_dev, buffer);
4319 yaffs_fix_null_name(obj, name, buffer_size);
4321 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4324 loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
4326 /* Dereference any hard linking */
4327 obj = yaffs_get_equivalent_obj(obj);
4329 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4330 return obj->variant.file_variant.file_size;
4331 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4332 if (!obj->variant.symlink_variant.alias)
4334 return strnlen(obj->variant.symlink_variant.alias,
4335 YAFFS_MAX_ALIAS_LENGTH);
4337 /* Only a directory should drop through to here */
4338 return obj->my_dev->data_bytes_per_chunk;
4342 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4345 struct list_head *i;
4348 count++; /* the object itself */
4350 list_for_each(i, &obj->hard_links)
4351 count++; /* add the hard links; */
4356 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4358 obj = yaffs_get_equivalent_obj(obj);
4363 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4365 obj = yaffs_get_equivalent_obj(obj);
4367 switch (obj->variant_type) {
4368 case YAFFS_OBJECT_TYPE_FILE:
4371 case YAFFS_OBJECT_TYPE_DIRECTORY:
4374 case YAFFS_OBJECT_TYPE_SYMLINK:
4377 case YAFFS_OBJECT_TYPE_HARDLINK:
4380 case YAFFS_OBJECT_TYPE_SPECIAL:
4381 if (S_ISFIFO(obj->yst_mode))
4383 if (S_ISCHR(obj->yst_mode))
4385 if (S_ISBLK(obj->yst_mode))
4387 if (S_ISSOCK(obj->yst_mode))
4397 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4399 obj = yaffs_get_equivalent_obj(obj);
4400 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4401 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4403 return yaffs_clone_str(_Y(""));
4406 /*--------------------------- Initialisation code -------------------------- */
4408 static int yaffs_check_dev_fns(struct yaffs_dev *dev)
4410 struct yaffs_driver *drv = &dev->drv;
4411 struct yaffs_tags_handler *tagger = &dev->tagger;
4413 /* Common functions, gotta have */
4414 if (!drv->drv_read_chunk_fn ||
4415 !drv->drv_write_chunk_fn ||
4419 if (dev->param.is_yaffs2 &&
4420 (!drv->drv_mark_bad_fn || !drv->drv_check_bad_fn))
4423 /* Install the default tags marshalling functions if needed. */
4424 yaffs_tags_compat_install(dev);
4425 yaffs_tags_marshall_install(dev);
4427 /* Check we now have the marshalling functions required. */
4428 if (!tagger->write_chunk_tags_fn ||
4429 !tagger->read_chunk_tags_fn ||
4430 !tagger->query_block_fn ||
4431 !tagger->mark_bad_fn)
4437 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4439 /* Initialise the unlinked, deleted, root and lost+found directories */
4440 dev->lost_n_found = NULL;
4441 dev->root_dir = NULL;
4442 dev->unlinked_dir = NULL;
4443 dev->del_dir = NULL;
4446 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4448 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4450 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4451 YAFFS_ROOT_MODE | S_IFDIR);
4453 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4454 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4456 if (dev->lost_n_found &&
4458 dev->unlinked_dir &&
4460 /* If lost-n-found is hidden then yank it out of the directory tree. */
4461 if (dev->param.hide_lost_n_found)
4462 list_del_init(&dev->lost_n_found->siblings);
4464 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4471 * Typically only used by yaffs_guts_initialise, but also used by the
4472 * Low level yaffs driver tests.
4475 int yaffs_guts_ll_init(struct yaffs_dev *dev)
4479 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_ll_init()");
4482 yaffs_trace(YAFFS_TRACE_ALWAYS,
4483 "yaffs: Need a device"
4491 dev->internal_start_block = dev->param.start_block;
4492 dev->internal_end_block = dev->param.end_block;
4493 dev->block_offset = 0;
4494 dev->chunk_offset = 0;
4495 dev->n_free_chunks = 0;
4499 if (dev->param.start_block == 0) {
4500 dev->internal_start_block = dev->param.start_block + 1;
4501 dev->internal_end_block = dev->param.end_block + 1;
4502 dev->block_offset = 1;
4503 dev->chunk_offset = dev->param.chunks_per_block;
4506 /* Check geometry parameters. */
4508 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4509 dev->param.total_bytes_per_chunk < 1024) ||
4510 (!dev->param.is_yaffs2 &&
4511 dev->param.total_bytes_per_chunk < 512) ||
4512 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4513 dev->param.chunks_per_block < 2 ||
4514 dev->param.n_reserved_blocks < 2 ||
4515 dev->internal_start_block <= 0 ||
4516 dev->internal_end_block <= 0 ||
4517 dev->internal_end_block <=
4518 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4520 /* otherwise it is too small */
4521 yaffs_trace(YAFFS_TRACE_ALWAYS,
4522 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4523 dev->param.total_bytes_per_chunk,
4524 dev->param.is_yaffs2 ? "2" : "",
4525 dev->param.inband_tags);
4529 /* Sort out space for inband tags, if required */
4530 if (dev->param.inband_tags)
4531 dev->data_bytes_per_chunk =
4532 dev->param.total_bytes_per_chunk -
4533 sizeof(struct yaffs_packed_tags2_tags_only);
4535 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4537 /* Got the right mix of functions? */
4538 if (!yaffs_check_dev_fns(dev)) {
4539 /* Function missing */
4540 yaffs_trace(YAFFS_TRACE_ALWAYS,
4541 "device function(s) missing or wrong");
4546 if (!yaffs_init_tmp_buffers(dev))
4549 if (yaffs_init_nand(dev) != YAFFS_OK) {
4550 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4560 int yaffs_guts_format_dev(struct yaffs_dev *dev)
4563 enum yaffs_block_state state;
4566 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4572 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
4573 yaffs_query_init_block_state(dev, i, &state, &dummy);
4574 if (state != YAFFS_BLOCK_STATE_DEAD)
4575 yaffs_erase_block(dev, i);
4582 * If the dev is mounted r/w then the cleanup will happen during
4583 * yaffs_guts_initialise. However if the dev is mounted ro then
4584 * the cleanup will be dfered until yaffs is remounted r/w.
4586 void yaffs_guts_cleanup(struct yaffs_dev *dev)
4588 yaffs_strip_deleted_objs(dev);
4589 yaffs_fix_hanging_objs(dev);
4590 if (dev->param.empty_lost_n_found)
4591 yaffs_empty_l_n_f(dev);
4594 int yaffs_guts_initialise(struct yaffs_dev *dev)
4596 int init_failed = 0;
4600 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4603 if (dev->is_mounted) {
4604 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4608 dev->is_mounted = 1;
4610 /* OK now calculate a few things for the device */
4613 * Calculate all the chunk size manipulation numbers:
4615 x = dev->data_bytes_per_chunk;
4616 /* We always use dev->chunk_shift and dev->chunk_div */
4617 dev->chunk_shift = calc_shifts(x);
4618 x >>= dev->chunk_shift;
4620 /* We only use chunk mask if chunk_div is 1 */
4621 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4624 * Calculate chunk_grp_bits.
4625 * We need to find the next power of 2 > than internal_end_block
4628 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4630 bits = calc_shifts_ceiling(x);
4632 /* Set up tnode width if wide tnodes are enabled. */
4633 if (!dev->param.wide_tnodes_disabled) {
4634 /* bits must be even so that we end up with 32-bit words */
4638 dev->tnode_width = 16;
4640 dev->tnode_width = bits;
4642 dev->tnode_width = 16;
4645 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4647 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4648 * so if the bitwidth of the
4649 * chunk range we're using is greater than 16 we need
4650 * to figure out chunk shift and chunk_grp_size
4653 if (bits <= dev->tnode_width)
4654 dev->chunk_grp_bits = 0;
4656 dev->chunk_grp_bits = bits - dev->tnode_width;
4658 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4659 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4660 dev->tnode_size = sizeof(struct yaffs_tnode);
4662 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4664 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4665 /* We have a problem because the soft delete won't work if
4666 * the chunk group size > chunks per block.
4667 * This can be remedied by using larger "virtual blocks".
4669 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4674 /* Finished verifying the device, continue with initialisation */
4676 /* More device initialisation */
4678 dev->passive_gc_count = 0;
4679 dev->oldest_dirty_gc_count = 0;
4681 dev->gc_block_finder = 0;
4682 dev->buffered_block = -1;
4683 dev->doing_buffered_block_rewrite = 0;
4684 dev->n_deleted_files = 0;
4685 dev->n_bg_deletions = 0;
4686 dev->n_unlinked_files = 0;
4687 dev->n_ecc_fixed = 0;
4688 dev->n_ecc_unfixed = 0;
4689 dev->n_tags_ecc_fixed = 0;
4690 dev->n_tags_ecc_unfixed = 0;
4691 dev->n_erase_failures = 0;
4692 dev->n_erased_blocks = 0;
4693 dev->gc_disable = 0;
4694 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now,
4695 * will get fixed on first GC */
4696 INIT_LIST_HEAD(&dev->dirty_dirs);
4697 dev->oldest_dirty_seq = 0;
4698 dev->oldest_dirty_block = 0;
4700 yaffs_endian_config(dev);
4702 /* Initialise temporary caches. */
4703 dev->gc_cleanup_list = NULL;
4706 init_failed = yaffs_cache_init(dev) < 0;
4708 dev->cache_hits = 0;
4711 dev->gc_cleanup_list =
4712 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4714 if (!dev->gc_cleanup_list)
4718 if (dev->param.is_yaffs2)
4719 dev->param.use_header_file_size = 1;
4721 if (!init_failed && !yaffs_init_blocks(dev))
4724 yaffs_init_tnodes_and_objs(dev);
4726 if (!init_failed && !yaffs_create_initial_dir(dev))
4729 if (!init_failed && dev->param.is_yaffs2 &&
4730 !dev->param.disable_summary &&
4731 !yaffs_summary_init(dev))
4735 /* Now scan the flash. */
4736 if (dev->param.is_yaffs2) {
4737 if (yaffs2_checkpt_restore(dev)) {
4738 yaffs_check_obj_details_loaded(dev->root_dir);
4739 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4741 "yaffs: restored from checkpoint"
4745 /* Clean up the mess caused by an aborted
4746 * checkpoint load then scan backwards.
4748 yaffs_deinit_blocks(dev);
4750 yaffs_deinit_tnodes_and_objs(dev);
4752 dev->n_erased_blocks = 0;
4753 dev->n_free_chunks = 0;
4754 dev->alloc_block = -1;
4755 dev->alloc_page = -1;
4756 dev->n_deleted_files = 0;
4757 dev->n_unlinked_files = 0;
4758 dev->n_bg_deletions = 0;
4760 if (!init_failed && !yaffs_init_blocks(dev))
4763 yaffs_init_tnodes_and_objs(dev);
4766 && !yaffs_create_initial_dir(dev))
4769 if (!init_failed && !yaffs2_scan_backwards(dev))
4772 } else if (!yaffs1_scan(dev)) {
4776 yaffs_guts_cleanup(dev);
4780 /* Clean up the mess */
4781 yaffs_trace(YAFFS_TRACE_TRACING,
4782 "yaffs: yaffs_guts_initialise() aborted.");
4784 yaffs_deinitialise(dev);
4788 /* Zero out stats */
4789 dev->n_page_reads = 0;
4790 dev->n_page_writes = 0;
4791 dev->n_erasures = 0;
4792 dev->n_gc_copies = 0;
4793 dev->n_retried_writes = 0;
4795 dev->n_retired_blocks = 0;
4797 yaffs_verify_free_chunks(dev);
4798 yaffs_verify_blocks(dev);
4800 /* Clean up any aborted checkpoint data */
4801 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4802 yaffs2_checkpt_invalidate(dev);
4804 yaffs_trace(YAFFS_TRACE_TRACING,
4805 "yaffs: yaffs_guts_initialise() done.");
4809 void yaffs_deinitialise(struct yaffs_dev *dev)
4811 if (dev->is_mounted) {
4814 yaffs_deinit_blocks(dev);
4815 yaffs_deinit_tnodes_and_objs(dev);
4816 yaffs_summary_deinit(dev);
4817 yaffs_cache_deinit(dev);
4819 kfree(dev->gc_cleanup_list);
4821 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4822 kfree(dev->temp_buffer[i].buffer);
4823 dev->temp_buffer[i].buffer = NULL;
4826 kfree(dev->checkpt_buffer);
4827 dev->checkpt_buffer = NULL;
4828 kfree(dev->checkpt_block_list);
4829 dev->checkpt_block_list = NULL;
4832 dev->is_mounted = 0;
4834 yaffs_deinit_nand(dev);
4838 int yaffs_count_free_chunks(struct yaffs_dev *dev)
4842 struct yaffs_block_info *blk;
4844 blk = dev->block_info;
4845 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4846 switch (blk->block_state) {
4847 case YAFFS_BLOCK_STATE_EMPTY:
4848 case YAFFS_BLOCK_STATE_ALLOCATING:
4849 case YAFFS_BLOCK_STATE_COLLECTING:
4850 case YAFFS_BLOCK_STATE_FULL:
4852 (dev->param.chunks_per_block - blk->pages_in_use +
4853 blk->soft_del_pages);
4863 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
4865 /* This is what we report to the outside world */
4868 int blocks_for_checkpt;
4870 n_free = dev->n_free_chunks;
4871 n_free += dev->n_deleted_files;
4873 /* Now count and subtract the number of dirty chunks in the cache. */
4874 n_dirty_caches = yaffs_count_dirty_caches(dev);
4875 n_free -= n_dirty_caches;
4878 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
4880 /* Now figure checkpoint space and report that... */
4881 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
4883 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
4893 * Marshalling functions to get loff_t file sizes into and out of
4896 void yaffs_oh_size_load(struct yaffs_dev *dev,
4897 struct yaffs_obj_hdr *oh,
4901 oh->file_size_low = FSIZE_LOW(fsize);
4903 oh->file_size_high = FSIZE_HIGH(fsize);
4906 yaffs_do_endian_u32(dev, &oh->file_size_low);
4907 yaffs_do_endian_u32(dev, &oh->file_size_high);
4911 loff_t yaffs_oh_to_size(struct yaffs_dev *dev, struct yaffs_obj_hdr *oh,
4917 if (sizeof(loff_t) >= 8 && ~(oh->file_size_high)) {
4918 u32 low = oh->file_size_low;
4919 u32 high = oh->file_size_high;
4922 yaffs_do_endian_u32 (dev, &low);
4923 yaffs_do_endian_u32 (dev, &high);
4925 retval = FSIZE_COMBINE(high, low);
4927 u32 low = oh->file_size_low;
4930 yaffs_do_endian_u32(dev, &low);
4931 retval = (loff_t)low;
4938 void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
4941 struct yaffs_block_info *bi;
4944 for(i = 0; i < 10; i++)
4947 for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
4948 bi = yaffs_get_block_info(dev, i);
4949 s = bi->block_state;
4950 if(s > YAFFS_BLOCK_STATE_DEAD || s < YAFFS_BLOCK_STATE_UNKNOWN)