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 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
172 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
173 if (dev->temp_buffer[i].buffer == buffer) {
174 dev->temp_buffer[i].in_use = 0;
180 /* assume it is an unmanaged one. */
181 yaffs_trace(YAFFS_TRACE_BUFFERS,
182 "Releasing unmanaged temp buffer");
184 dev->unmanaged_buffer_deallocs++;
190 * Functions for robustisizing TODO
194 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
196 const struct yaffs_ext_tags *tags)
204 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
205 const struct yaffs_ext_tags *tags)
212 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
213 struct yaffs_block_info *bi)
215 if (!bi->gc_prioritise) {
216 bi->gc_prioritise = 1;
217 dev->has_pending_prioritised_gc = 1;
218 bi->chunk_error_strikes++;
220 if (bi->chunk_error_strikes > 3) {
221 bi->needs_retiring = 1; /* Too many stikes, so retire */
222 yaffs_trace(YAFFS_TRACE_ALWAYS,
223 "yaffs: Block struck out");
229 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
232 int flash_block = nand_chunk / dev->param.chunks_per_block;
233 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
235 yaffs_handle_chunk_error(dev, bi);
238 /* Was an actual write failure,
239 * so mark the block for retirement.*/
240 bi->needs_retiring = 1;
241 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
242 "**>> Block %d needs retiring", flash_block);
245 /* Delete the chunk */
246 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
247 yaffs_skip_rest_of_block(dev);
255 * Simple hash function. Needs to have a reasonable spread
258 static inline int yaffs_hash_fn(int n)
262 return n % YAFFS_NOBJECT_BUCKETS;
266 * Access functions to useful fake objects.
267 * Note that root might have a presence in NAND if permissions are set.
270 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
272 return dev->root_dir;
275 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
277 return dev->lost_n_found;
281 * Erased NAND checking functions
284 int yaffs_check_ff(u8 *buffer, int n_bytes)
286 /* Horrible, slow implementation */
295 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
297 int retval = YAFFS_OK;
298 u8 *data = yaffs_get_temp_buffer(dev);
299 struct yaffs_ext_tags tags;
302 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
304 if (result == YAFFS_FAIL ||
305 tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
308 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
310 yaffs_trace(YAFFS_TRACE_NANDACCESS,
311 "Chunk %d not erased", nand_chunk);
315 yaffs_release_temp_buffer(dev, data);
321 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
324 struct yaffs_ext_tags *tags)
326 int retval = YAFFS_OK;
327 struct yaffs_ext_tags temp_tags;
328 u8 *buffer = yaffs_get_temp_buffer(dev);
331 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
332 if (result == YAFFS_FAIL ||
333 memcmp(buffer, data, dev->data_bytes_per_chunk) ||
334 temp_tags.obj_id != tags->obj_id ||
335 temp_tags.chunk_id != tags->chunk_id ||
336 temp_tags.n_bytes != tags->n_bytes)
339 yaffs_release_temp_buffer(dev, buffer);
345 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
348 int reserved_blocks = dev->param.n_reserved_blocks;
351 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
354 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
356 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
359 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
362 struct yaffs_block_info *bi;
364 if (dev->n_erased_blocks < 1) {
365 /* Hoosterman we've got a problem.
366 * Can't get space to gc
368 yaffs_trace(YAFFS_TRACE_ERROR,
369 "yaffs tragedy: no more erased blocks");
374 /* Find an empty block. */
376 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
377 dev->alloc_block_finder++;
378 if (dev->alloc_block_finder < (int)dev->internal_start_block
379 || dev->alloc_block_finder > (int)dev->internal_end_block) {
380 dev->alloc_block_finder = dev->internal_start_block;
383 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
385 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
386 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
388 bi->seq_number = dev->seq_number;
389 dev->n_erased_blocks--;
390 yaffs_trace(YAFFS_TRACE_ALLOCATE,
391 "Allocated block %d, seq %d, %d left" ,
392 dev->alloc_block_finder, dev->seq_number,
393 dev->n_erased_blocks);
394 return dev->alloc_block_finder;
398 yaffs_trace(YAFFS_TRACE_ALWAYS,
399 "yaffs tragedy: no more erased blocks, but there should have been %d",
400 dev->n_erased_blocks);
405 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
406 struct yaffs_block_info **block_ptr)
409 struct yaffs_block_info *bi;
411 if (dev->alloc_block < 0) {
412 /* Get next block to allocate off */
413 dev->alloc_block = yaffs_find_alloc_block(dev);
417 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
418 /* No space unless we're allowed to use the reserve. */
422 if (dev->n_erased_blocks < (int)dev->param.n_reserved_blocks
423 && dev->alloc_page == 0)
424 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
426 /* Next page please.... */
427 if (dev->alloc_block >= 0) {
428 bi = yaffs_get_block_info(dev, dev->alloc_block);
430 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
433 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
437 dev->n_free_chunks--;
439 /* If the block is full set the state to full */
440 if (dev->alloc_page >= dev->param.chunks_per_block) {
441 bi->block_state = YAFFS_BLOCK_STATE_FULL;
442 dev->alloc_block = -1;
451 yaffs_trace(YAFFS_TRACE_ERROR,
452 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
457 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
461 n = dev->n_erased_blocks * dev->param.chunks_per_block;
463 if (dev->alloc_block > 0)
464 n += (dev->param.chunks_per_block - dev->alloc_page);
471 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
472 * if we don't want to write to it.
474 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
476 struct yaffs_block_info *bi;
478 if (dev->alloc_block > 0) {
479 bi = yaffs_get_block_info(dev, dev->alloc_block);
480 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
481 bi->block_state = YAFFS_BLOCK_STATE_FULL;
482 dev->alloc_block = -1;
487 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
489 struct yaffs_ext_tags *tags, int use_reserver)
495 yaffs2_checkpt_invalidate(dev);
498 struct yaffs_block_info *bi = 0;
501 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
507 /* First check this chunk is erased, if it needs
508 * checking. The checking policy (unless forced
509 * always on) is as follows:
511 * Check the first page we try to write in a block.
512 * If the check passes then we don't need to check any
513 * more. If the check fails, we check again...
514 * If the block has been erased, we don't need to check.
516 * However, if the block has been prioritised for gc,
517 * then we think there might be something odd about
518 * this block and stop using it.
520 * Rationale: We should only ever see chunks that have
521 * not been erased if there was a partially written
522 * chunk due to power loss. This checking policy should
523 * catch that case with very few checks and thus save a
524 * lot of checks that are most likely not needed.
527 * If an erase check fails or the write fails we skip the
531 /* let's give it a try */
534 if (dev->param.always_check_erased)
535 bi->skip_erased_check = 0;
537 if (!bi->skip_erased_check) {
538 erased_ok = yaffs_check_chunk_erased(dev, chunk);
539 if (erased_ok != YAFFS_OK) {
540 yaffs_trace(YAFFS_TRACE_ERROR,
541 "**>> yaffs chunk %d was not erased",
544 /* If not erased, delete this one,
545 * skip rest of block and
546 * try another chunk */
547 yaffs_chunk_del(dev, chunk, 1, __LINE__);
548 yaffs_skip_rest_of_block(dev);
553 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
555 if (!bi->skip_erased_check)
557 yaffs_verify_chunk_written(dev, chunk, data, tags);
559 if (write_ok != YAFFS_OK) {
560 /* Clean up aborted write, skip to next block and
561 * try another chunk */
562 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
566 bi->skip_erased_check = 1;
568 /* Copy the data into the robustification buffer */
569 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
571 } while (write_ok != YAFFS_OK &&
572 (yaffs_wr_attempts == 0 || attempts <= yaffs_wr_attempts));
578 yaffs_trace(YAFFS_TRACE_ERROR,
579 "**>> yaffs write required %d attempts",
581 dev->n_retried_writes += (attempts - 1);
588 * Block retiring for handling a broken block.
591 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
593 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
595 yaffs2_checkpt_invalidate(dev);
597 yaffs2_clear_oldest_dirty_seq(dev, bi);
599 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
600 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
601 yaffs_trace(YAFFS_TRACE_ALWAYS,
602 "yaffs: Failed to mark bad and erase block %d",
605 struct yaffs_ext_tags tags;
607 flash_block * dev->param.chunks_per_block;
609 u8 *buffer = yaffs_get_temp_buffer(dev);
611 memset(buffer, 0xff, dev->data_bytes_per_chunk);
612 memset(&tags, 0, sizeof(tags));
613 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
614 if (dev->tagger.write_chunk_tags_fn(dev, chunk_id -
618 yaffs_trace(YAFFS_TRACE_ALWAYS,
619 "yaffs: Failed to write bad block marker to block %d",
622 yaffs_release_temp_buffer(dev, buffer);
626 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
627 bi->gc_prioritise = 0;
628 bi->needs_retiring = 0;
630 dev->n_retired_blocks++;
633 /*---------------- Name handling functions ------------*/
635 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
636 const YCHAR *oh_name, int buff_size)
638 #ifdef CONFIG_YAFFS_AUTO_UNICODE
639 if (dev->param.auto_unicode) {
641 /* It is an ASCII name, do an ASCII to
642 * unicode conversion */
643 const char *ascii_oh_name = (const char *)oh_name;
644 int n = buff_size - 1;
645 while (n > 0 && *ascii_oh_name) {
646 *name = *ascii_oh_name;
652 strncpy(name, oh_name + 1, buff_size - 1);
659 strncpy(name, oh_name, buff_size - 1);
663 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
666 #ifdef CONFIG_YAFFS_AUTO_UNICODE
671 if (dev->param.auto_unicode) {
676 /* Figure out if the name will fit in ascii character set */
677 while (is_ascii && *w) {
684 /* It is an ASCII name, so convert unicode to ascii */
685 char *ascii_oh_name = (char *)oh_name;
686 int n = YAFFS_MAX_NAME_LENGTH - 1;
687 while (n > 0 && *name) {
688 *ascii_oh_name = *name;
694 /* Unicode name, so save starting at the second YCHAR */
696 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
703 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
707 static u16 yaffs_calc_name_sum(const YCHAR *name)
715 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
717 /* 0x1f mask is case insensitive */
718 sum += ((*name) & 0x1f) * i;
726 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
728 memset(obj->short_name, 0, sizeof(obj->short_name));
730 if (name && !name[0]) {
731 yaffs_fix_null_name(obj, obj->short_name,
732 YAFFS_SHORT_NAME_LENGTH);
733 name = obj->short_name;
735 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
736 YAFFS_SHORT_NAME_LENGTH) {
737 strcpy(obj->short_name, name);
740 obj->sum = yaffs_calc_name_sum(name);
743 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
744 const struct yaffs_obj_hdr *oh)
746 #ifdef CONFIG_YAFFS_AUTO_UNICODE
747 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
748 memset(tmp_name, 0, sizeof(tmp_name));
749 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
750 YAFFS_MAX_NAME_LENGTH + 1);
751 yaffs_set_obj_name(obj, tmp_name);
753 yaffs_set_obj_name(obj, oh->name);
757 loff_t yaffs_max_file_size(struct yaffs_dev *dev)
759 if (sizeof(loff_t) < 8)
760 return YAFFS_MAX_FILE_SIZE_32;
762 return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
765 /*-------------------- TNODES -------------------
767 * List of spare tnodes
768 * The list is hooked together using the first pointer
772 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
774 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
777 memset(tn, 0, dev->tnode_size);
781 dev->checkpoint_blocks_required = 0; /* force recalculation */
786 /* FreeTnode frees up a tnode and puts it back on the free list */
787 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
789 yaffs_free_raw_tnode(dev, tn);
791 dev->checkpoint_blocks_required = 0; /* force recalculation */
794 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
796 yaffs_deinit_raw_tnodes_and_objs(dev);
801 static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
802 unsigned pos, unsigned val)
804 u32 *map = (u32 *) tn;
810 pos &= YAFFS_TNODES_LEVEL0_MASK;
811 val >>= dev->chunk_grp_bits;
813 bit_in_map = pos * dev->tnode_width;
814 word_in_map = bit_in_map / 32;
815 bit_in_word = bit_in_map & (32 - 1);
817 mask = dev->tnode_mask << bit_in_word;
819 map[word_in_map] &= ~mask;
820 map[word_in_map] |= (mask & (val << bit_in_word));
822 if (dev->tnode_width > (32 - bit_in_word)) {
823 bit_in_word = (32 - bit_in_word);
826 dev->tnode_mask >> bit_in_word;
827 map[word_in_map] &= ~mask;
828 map[word_in_map] |= (mask & (val >> bit_in_word));
832 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
835 u32 *map = (u32 *) tn;
841 pos &= YAFFS_TNODES_LEVEL0_MASK;
843 bit_in_map = pos * dev->tnode_width;
844 word_in_map = bit_in_map / 32;
845 bit_in_word = bit_in_map & (32 - 1);
847 val = map[word_in_map] >> bit_in_word;
849 if (dev->tnode_width > (32 - bit_in_word)) {
850 bit_in_word = (32 - bit_in_word);
852 val |= (map[word_in_map] << bit_in_word);
855 val &= dev->tnode_mask;
856 val <<= dev->chunk_grp_bits;
861 /* ------------------- End of individual tnode manipulation -----------------*/
863 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
864 * The look up tree is represented by the top tnode and the number of top_level
865 * in the tree. 0 means only the level 0 tnode is in the tree.
868 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
869 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
870 struct yaffs_file_var *file_struct,
873 struct yaffs_tnode *tn = file_struct->top;
876 int level = file_struct->top_level;
880 /* Check sane level and chunk Id */
881 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
884 if (chunk_id > YAFFS_MAX_CHUNK_ID)
887 /* First check we're tall enough (ie enough top_level) */
889 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
892 i >>= YAFFS_TNODES_INTERNAL_BITS;
896 if (required_depth > file_struct->top_level)
897 return NULL; /* Not tall enough, so we can't find it */
899 /* Traverse down to level 0 */
900 while (level > 0 && tn) {
901 tn = tn->internal[(chunk_id >>
902 (YAFFS_TNODES_LEVEL0_BITS +
904 YAFFS_TNODES_INTERNAL_BITS)) &
905 YAFFS_TNODES_INTERNAL_MASK];
912 /* add_find_tnode_0 finds the level 0 tnode if it exists,
913 * otherwise first expands the tree.
914 * This happens in two steps:
915 * 1. If the tree isn't tall enough, then make it taller.
916 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
918 * Used when modifying the tree.
920 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
921 * specified tn will be plugged into the ttree.
924 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
925 struct yaffs_file_var *file_struct,
927 struct yaffs_tnode *passed_tn)
932 struct yaffs_tnode *tn;
935 /* Check sane level and page Id */
936 if (file_struct->top_level < 0 ||
937 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
940 if (chunk_id > YAFFS_MAX_CHUNK_ID)
943 /* First check we're tall enough (ie enough top_level) */
945 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
948 x >>= YAFFS_TNODES_INTERNAL_BITS;
952 if (required_depth > file_struct->top_level) {
953 /* Not tall enough, gotta make the tree taller */
954 for (i = file_struct->top_level; i < required_depth; i++) {
956 tn = yaffs_get_tnode(dev);
959 tn->internal[0] = file_struct->top;
960 file_struct->top = tn;
961 file_struct->top_level++;
963 yaffs_trace(YAFFS_TRACE_ERROR,
964 "yaffs: no more tnodes");
970 /* Traverse down to level 0, adding anything we need */
972 l = file_struct->top_level;
973 tn = file_struct->top;
976 while (l > 0 && tn) {
978 (YAFFS_TNODES_LEVEL0_BITS +
979 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
980 YAFFS_TNODES_INTERNAL_MASK;
982 if ((l > 1) && !tn->internal[x]) {
983 /* Add missing non-level-zero tnode */
984 tn->internal[x] = yaffs_get_tnode(dev);
985 if (!tn->internal[x])
988 /* Looking from level 1 at level 0 */
990 /* If we already have one, release it */
992 yaffs_free_tnode(dev,
994 tn->internal[x] = passed_tn;
996 } else if (!tn->internal[x]) {
997 /* Don't have one, none passed in */
998 tn->internal[x] = yaffs_get_tnode(dev);
999 if (!tn->internal[x])
1004 tn = tn->internal[x];
1008 /* top is level 0 */
1010 memcpy(tn, passed_tn,
1011 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
1012 yaffs_free_tnode(dev, passed_tn);
1019 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
1022 return (tags->chunk_id == (u32)chunk_obj &&
1023 tags->obj_id == (u32)obj_id &&
1024 !tags->is_deleted) ? 1 : 0;
1028 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
1029 struct yaffs_ext_tags *tags, int obj_id,
1034 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
1035 if (yaffs_check_chunk_bit
1036 (dev, the_chunk / dev->param.chunks_per_block,
1037 the_chunk % dev->param.chunks_per_block)) {
1039 if (dev->chunk_grp_size == 1)
1042 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
1044 if (yaffs_tags_match(tags,
1045 obj_id, inode_chunk)) {
1056 int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1057 struct yaffs_ext_tags *tags)
1059 /*Get the Tnode, then get the level 0 offset chunk offset */
1060 struct yaffs_tnode *tn;
1062 struct yaffs_ext_tags local_tags;
1064 struct yaffs_dev *dev = in->my_dev;
1067 /* Passed a NULL, so use our own tags space */
1071 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1076 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1078 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1083 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1084 struct yaffs_ext_tags *tags)
1086 /* Get the Tnode, then get the level 0 offset chunk offset */
1087 struct yaffs_tnode *tn;
1089 struct yaffs_ext_tags local_tags;
1090 struct yaffs_dev *dev = in->my_dev;
1094 /* Passed a NULL, so use our own tags space */
1098 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1103 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1105 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1108 /* Delete the entry in the filestructure (if found) */
1110 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1115 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1116 int nand_chunk, int in_scan)
1118 /* NB in_scan is zero unless scanning.
1119 * For forward scanning, in_scan is > 0;
1120 * for backward scanning in_scan is < 0
1122 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1125 struct yaffs_tnode *tn;
1126 struct yaffs_dev *dev = in->my_dev;
1128 struct yaffs_ext_tags existing_tags;
1129 struct yaffs_ext_tags new_tags;
1130 unsigned existing_serial, new_serial;
1132 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1133 /* Just ignore an attempt at putting a chunk into a non-file
1135 * If it is not during Scanning then something went wrong!
1138 yaffs_trace(YAFFS_TRACE_ERROR,
1139 "yaffs tragedy:attempt to put data chunk into a non-file"
1144 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1148 tn = yaffs_add_find_tnode_0(dev,
1149 &in->variant.file_variant,
1155 /* Dummy insert, bail now */
1158 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1161 /* If we're scanning then we need to test for duplicates
1162 * NB This does not need to be efficient since it should only
1163 * happen when the power fails during a write, then only one
1164 * chunk should ever be affected.
1166 * Correction for YAFFS2: This could happen quite a lot and we
1167 * need to think about efficiency! TODO
1168 * Update: For backward scanning we don't need to re-read tags
1169 * so this is quite cheap.
1172 if (existing_cunk > 0) {
1173 /* NB Right now existing chunk will not be real
1174 * chunk_id if the chunk group size > 1
1175 * thus we have to do a FindChunkInFile to get the
1178 * We have a duplicate now we need to decide which
1181 * Backwards scanning YAFFS2: The old one is what
1182 * we use, dump the new one.
1183 * YAFFS1: Get both sets of tags and compare serial
1188 /* Only do this for forward scanning */
1189 yaffs_rd_chunk_tags_nand(dev,
1193 /* Do a proper find */
1195 yaffs_find_chunk_in_file(in, inode_chunk,
1199 if (existing_cunk <= 0) {
1200 /*Hoosterman - how did this happen? */
1202 yaffs_trace(YAFFS_TRACE_ERROR,
1203 "yaffs tragedy: existing chunk < 0 in scan"
1208 /* NB The deleted flags should be false, otherwise
1209 * the chunks will not be loaded during a scan
1213 new_serial = new_tags.serial_number;
1214 existing_serial = existing_tags.serial_number;
1217 if ((in_scan > 0) &&
1218 (existing_cunk <= 0 ||
1219 ((existing_serial + 1) & 3) == new_serial)) {
1220 /* Forward scanning.
1222 * Delete the old one and drop through to
1225 yaffs_chunk_del(dev, existing_cunk, 1,
1228 /* Backward scanning or we want to use the
1230 * Delete the new one and return early so that
1231 * the tnode isn't changed
1233 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1240 if (existing_cunk == 0)
1241 in->n_data_chunks++;
1243 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1248 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1250 struct yaffs_block_info *the_block;
1253 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1255 block_no = chunk / dev->param.chunks_per_block;
1256 the_block = yaffs_get_block_info(dev, block_no);
1258 the_block->soft_del_pages++;
1259 dev->n_free_chunks++;
1260 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1264 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1265 * the chunks in the file.
1266 * All soft deleting does is increment the block's softdelete count and pulls
1267 * the chunk out of the tnode.
1268 * Thus, essentially this is the same as DeleteWorker except that the chunks
1272 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1273 u32 level, int chunk_offset)
1278 struct yaffs_dev *dev = in->my_dev;
1284 for (i = YAFFS_NTNODES_INTERNAL - 1;
1287 if (tn->internal[i]) {
1289 yaffs_soft_del_worker(in,
1293 YAFFS_TNODES_INTERNAL_BITS)
1296 yaffs_free_tnode(dev,
1298 tn->internal[i] = NULL;
1300 /* Can this happen? */
1304 return (all_done) ? 1 : 0;
1308 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1309 the_chunk = yaffs_get_group_base(dev, tn, i);
1311 yaffs_soft_del_chunk(dev, the_chunk);
1312 yaffs_load_tnode_0(dev, tn, i, 0);
1318 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1320 struct yaffs_dev *dev = obj->my_dev;
1321 struct yaffs_obj *parent;
1323 yaffs_verify_obj_in_dir(obj);
1324 parent = obj->parent;
1326 yaffs_verify_dir(parent);
1328 if (dev && dev->param.remove_obj_fn)
1329 dev->param.remove_obj_fn(obj);
1331 list_del_init(&obj->siblings);
1334 yaffs_verify_dir(parent);
1337 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1340 yaffs_trace(YAFFS_TRACE_ALWAYS,
1341 "tragedy: Trying to add an object to a null pointer directory"
1346 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1347 yaffs_trace(YAFFS_TRACE_ALWAYS,
1348 "tragedy: Trying to add an object to a non-directory"
1353 if (obj->siblings.prev == NULL) {
1354 /* Not initialised */
1358 yaffs_verify_dir(directory);
1360 yaffs_remove_obj_from_dir(obj);
1363 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1364 obj->parent = directory;
1366 if (directory == obj->my_dev->unlinked_dir
1367 || directory == obj->my_dev->del_dir) {
1369 obj->my_dev->n_unlinked_files++;
1370 obj->rename_allowed = 0;
1373 yaffs_verify_dir(directory);
1374 yaffs_verify_obj_in_dir(obj);
1377 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1378 struct yaffs_obj *new_dir,
1379 const YCHAR *new_name, int force, int shadows)
1383 struct yaffs_obj *existing_target;
1385 if (new_dir == NULL)
1386 new_dir = obj->parent; /* use the old directory */
1388 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1389 yaffs_trace(YAFFS_TRACE_ALWAYS,
1390 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1395 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1396 del_op = (new_dir == obj->my_dev->del_dir);
1398 existing_target = yaffs_find_by_name(new_dir, new_name);
1400 /* If the object is a file going into the unlinked directory,
1401 * then it is OK to just stuff it in since duplicate names are OK.
1402 * else only proceed if the new name does not exist and we're putting
1403 * it into a directory.
1405 if (!(unlink_op || del_op || force ||
1406 shadows > 0 || !existing_target) ||
1407 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1410 yaffs_set_obj_name(obj, new_name);
1412 yaffs_add_obj_to_dir(new_dir, obj);
1417 /* If it is a deletion then we mark it as a shrink for gc */
1418 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1425 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1428 struct yaffs_dev *dev = obj->my_dev;
1430 /* If it is still linked into the bucket list, free from the list */
1431 if (!list_empty(&obj->hash_link)) {
1432 list_del_init(&obj->hash_link);
1433 bucket = yaffs_hash_fn(obj->obj_id);
1434 dev->obj_bucket[bucket].count--;
1438 /* FreeObject frees up a Object and puts it back on the free list */
1439 static void yaffs_free_obj(struct yaffs_obj *obj)
1441 struct yaffs_dev *dev;
1448 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1449 obj, obj->my_inode);
1452 if (!list_empty(&obj->siblings))
1455 if (obj->my_inode) {
1456 /* We're still hooked up to a cached inode.
1457 * Don't delete now, but mark for later deletion
1459 obj->defered_free = 1;
1463 yaffs_unhash_obj(obj);
1465 yaffs_free_raw_obj(dev, obj);
1467 dev->checkpoint_blocks_required = 0; /* force recalculation */
1470 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1472 if (obj->defered_free)
1473 yaffs_free_obj(obj);
1476 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1478 /* Iinvalidate the file's data in the cache, without flushing. */
1479 yaffs_invalidate_file_cache(in);
1481 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1482 /* Move to unlinked directory so we have a deletion record */
1483 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1487 yaffs_remove_obj_from_dir(in);
1488 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1496 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1498 if (!obj->deleted ||
1499 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1503 if (obj->n_data_chunks <= 0) {
1504 /* Empty file with no duplicate object headers,
1505 * just delete it immediately */
1506 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1507 obj->variant.file_variant.top = NULL;
1508 yaffs_trace(YAFFS_TRACE_TRACING,
1509 "yaffs: Deleting empty file %d",
1511 yaffs_generic_obj_del(obj);
1513 yaffs_soft_del_worker(obj,
1514 obj->variant.file_variant.top,
1516 file_variant.top_level, 0);
1521 /* Pruning removes any part of the file structure tree that is beyond the
1522 * bounds of the file (ie that does not point to chunks).
1524 * A file should only get pruned when its size is reduced.
1526 * Before pruning, the chunks must be pulled from the tree and the
1527 * level 0 tnode entries must be zeroed out.
1528 * Could also use this for file deletion, but that's probably better handled
1529 * by a special case.
1531 * This function is recursive. For levels > 0 the function is called again on
1532 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1533 * If there is no data in a subtree then it is pruned.
1536 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1537 struct yaffs_tnode *tn, u32 level,
1549 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1550 if (tn->internal[i]) {
1552 yaffs_prune_worker(dev,
1555 (i == 0) ? del0 : 1);
1558 if (tn->internal[i])
1562 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1563 u32 *map = (u32 *) tn;
1565 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1571 if (has_data == 0 && del0) {
1572 /* Free and return NULL */
1573 yaffs_free_tnode(dev, tn);
1579 static int yaffs_prune_tree(struct yaffs_dev *dev,
1580 struct yaffs_file_var *file_struct)
1585 struct yaffs_tnode *tn;
1587 if (file_struct->top_level < 1)
1591 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1593 /* Now we have a tree with all the non-zero branches NULL but
1594 * the height is the same as it was.
1595 * Let's see if we can trim internal tnodes to shorten the tree.
1596 * We can do this if only the 0th element in the tnode is in use
1597 * (ie all the non-zero are NULL)
1600 while (file_struct->top_level && !done) {
1601 tn = file_struct->top;
1604 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1605 if (tn->internal[i])
1610 file_struct->top = tn->internal[0];
1611 file_struct->top_level--;
1612 yaffs_free_tnode(dev, tn);
1621 /*-------------------- End of File Structure functions.-------------------*/
1623 /* alloc_empty_obj gets us a clean Object.*/
1624 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1626 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1633 /* Now sweeten it up... */
1635 memset(obj, 0, sizeof(struct yaffs_obj));
1636 obj->being_created = 1;
1640 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1641 INIT_LIST_HEAD(&(obj->hard_links));
1642 INIT_LIST_HEAD(&(obj->hash_link));
1643 INIT_LIST_HEAD(&obj->siblings);
1645 /* Now make the directory sane */
1646 if (dev->root_dir) {
1647 obj->parent = dev->root_dir;
1648 list_add(&(obj->siblings),
1649 &dev->root_dir->variant.dir_variant.children);
1652 /* Add it to the lost and found directory.
1653 * NB Can't put root or lost-n-found in lost-n-found so
1654 * check if lost-n-found exists first
1656 if (dev->lost_n_found)
1657 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1659 obj->being_created = 0;
1661 dev->checkpoint_blocks_required = 0; /* force recalculation */
1666 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1670 int lowest = 999999;
1672 /* Search for the shortest list or one that
1676 for (i = 0; i < 10 && lowest > 4; i++) {
1677 dev->bucket_finder++;
1678 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1679 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1680 lowest = dev->obj_bucket[dev->bucket_finder].count;
1681 l = dev->bucket_finder;
1688 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1690 int bucket = yaffs_find_nice_bucket(dev);
1692 struct list_head *i;
1693 u32 n = (u32) bucket;
1696 * Now find an object value that has not already been taken
1697 * by scanning the list, incrementing each time by number of buckets.
1701 n += YAFFS_NOBJECT_BUCKETS;
1702 list_for_each(i, &dev->obj_bucket[bucket].list) {
1703 /* Check if this value is already taken. */
1704 if (i && list_entry(i, struct yaffs_obj,
1705 hash_link)->obj_id == n)
1712 static void yaffs_hash_obj(struct yaffs_obj *in)
1714 int bucket = yaffs_hash_fn(in->obj_id);
1715 struct yaffs_dev *dev = in->my_dev;
1717 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1718 dev->obj_bucket[bucket].count++;
1721 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1723 int bucket = yaffs_hash_fn(number);
1724 struct list_head *i;
1725 struct yaffs_obj *in;
1727 list_for_each(i, &dev->obj_bucket[bucket].list) {
1728 /* Look if it is in the list */
1729 in = list_entry(i, struct yaffs_obj, hash_link);
1730 if (in->obj_id == number) {
1731 /* Don't show if it is defered free */
1732 if (in->defered_free)
1741 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1742 enum yaffs_obj_type type)
1744 struct yaffs_obj *the_obj = NULL;
1745 struct yaffs_tnode *tn = NULL;
1748 number = yaffs_new_obj_id(dev);
1750 if (type == YAFFS_OBJECT_TYPE_FILE) {
1751 tn = yaffs_get_tnode(dev);
1756 the_obj = yaffs_alloc_empty_obj(dev);
1759 yaffs_free_tnode(dev, tn);
1764 the_obj->rename_allowed = 1;
1765 the_obj->unlink_allowed = 1;
1766 the_obj->obj_id = number;
1767 yaffs_hash_obj(the_obj);
1768 the_obj->variant_type = type;
1769 yaffs_load_current_time(the_obj, 1, 1);
1772 case YAFFS_OBJECT_TYPE_FILE:
1773 the_obj->variant.file_variant.file_size = 0;
1774 the_obj->variant.file_variant.stored_size = 0;
1775 the_obj->variant.file_variant.shrink_size =
1776 yaffs_max_file_size(dev);
1777 the_obj->variant.file_variant.top_level = 0;
1778 the_obj->variant.file_variant.top = tn;
1780 case YAFFS_OBJECT_TYPE_DIRECTORY:
1781 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1782 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1784 case YAFFS_OBJECT_TYPE_SYMLINK:
1785 case YAFFS_OBJECT_TYPE_HARDLINK:
1786 case YAFFS_OBJECT_TYPE_SPECIAL:
1787 /* No action required */
1789 case YAFFS_OBJECT_TYPE_UNKNOWN:
1790 /* todo this should not happen */
1796 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1797 int number, u32 mode)
1800 struct yaffs_obj *obj =
1801 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1806 obj->fake = 1; /* it is fake so it might not use NAND */
1807 obj->rename_allowed = 0;
1808 obj->unlink_allowed = 0;
1811 obj->yst_mode = mode;
1813 obj->hdr_chunk = 0; /* Not a valid chunk. */
1819 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
1825 yaffs_init_raw_tnodes_and_objs(dev);
1827 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1828 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
1829 dev->obj_bucket[i].count = 0;
1833 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
1835 enum yaffs_obj_type type)
1837 struct yaffs_obj *the_obj = NULL;
1840 the_obj = yaffs_find_by_number(dev, number);
1843 the_obj = yaffs_new_obj(dev, number, type);
1849 YCHAR *yaffs_clone_str(const YCHAR *str)
1851 YCHAR *new_str = NULL;
1857 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
1858 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
1860 strncpy(new_str, str, len);
1867 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
1868 * link (ie. name) is created or deleted in the directory.
1871 * create dir/a : update dir's mtime/ctime
1872 * rm dir/a: update dir's mtime/ctime
1873 * modify dir/a: don't update dir's mtimme/ctime
1875 * This can be handled immediately or defered. Defering helps reduce the number
1876 * of updates when many files in a directory are changed within a brief period.
1878 * If the directory updating is defered then yaffs_update_dirty_dirs must be
1879 * called periodically.
1882 static void yaffs_update_parent(struct yaffs_obj *obj)
1884 struct yaffs_dev *dev;
1890 yaffs_load_current_time(obj, 0, 1);
1891 if (dev->param.defered_dir_update) {
1892 struct list_head *link = &obj->variant.dir_variant.dirty;
1894 if (list_empty(link)) {
1895 list_add(link, &dev->dirty_dirs);
1896 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1897 "Added object %d to dirty directories",
1902 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
1906 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
1908 struct list_head *link;
1909 struct yaffs_obj *obj;
1910 struct yaffs_dir_var *d_s;
1911 union yaffs_obj_var *o_v;
1913 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
1915 while (!list_empty(&dev->dirty_dirs)) {
1916 link = dev->dirty_dirs.next;
1917 list_del_init(link);
1919 d_s = list_entry(link, struct yaffs_dir_var, dirty);
1920 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
1921 obj = list_entry(o_v, struct yaffs_obj, variant);
1923 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
1927 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
1932 * Mknod (create) a new object.
1933 * equiv_obj only has meaning for a hard link;
1934 * alias_str only has meaning for a symlink.
1935 * rdev only has meaning for devices (a subset of special objects)
1938 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
1939 struct yaffs_obj *parent,
1944 struct yaffs_obj *equiv_obj,
1945 const YCHAR *alias_str, u32 rdev)
1947 struct yaffs_obj *in;
1949 struct yaffs_dev *dev = parent->my_dev;
1951 /* Check if the entry exists.
1952 * If it does then fail the call since we don't want a dup. */
1953 if (yaffs_find_by_name(parent, name))
1956 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
1957 str = yaffs_clone_str(alias_str);
1962 in = yaffs_new_obj(dev, -1, type);
1971 in->variant_type = type;
1973 in->yst_mode = mode;
1975 yaffs_attribs_init(in, gid, uid, rdev);
1977 in->n_data_chunks = 0;
1979 yaffs_set_obj_name(in, name);
1982 yaffs_add_obj_to_dir(parent, in);
1984 in->my_dev = parent->my_dev;
1987 case YAFFS_OBJECT_TYPE_SYMLINK:
1988 in->variant.symlink_variant.alias = str;
1990 case YAFFS_OBJECT_TYPE_HARDLINK:
1991 in->variant.hardlink_variant.equiv_obj = equiv_obj;
1992 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
1993 list_add(&in->hard_links, &equiv_obj->hard_links);
1995 case YAFFS_OBJECT_TYPE_FILE:
1996 case YAFFS_OBJECT_TYPE_DIRECTORY:
1997 case YAFFS_OBJECT_TYPE_SPECIAL:
1998 case YAFFS_OBJECT_TYPE_UNKNOWN:
2003 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2004 /* Could not create the object header, fail */
2010 yaffs_update_parent(parent);
2015 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2016 const YCHAR *name, u32 mode, u32 uid,
2019 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2020 uid, gid, NULL, NULL, 0);
2023 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2024 u32 mode, u32 uid, u32 gid)
2026 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2027 mode, uid, gid, NULL, NULL, 0);
2030 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2031 const YCHAR *name, u32 mode, u32 uid,
2034 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2035 uid, gid, NULL, NULL, rdev);
2038 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2039 const YCHAR *name, u32 mode, u32 uid,
2040 u32 gid, const YCHAR *alias)
2042 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2043 uid, gid, NULL, alias, 0);
2046 /* yaffs_link_obj returns the object id of the equivalent object.*/
2047 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2048 struct yaffs_obj *equiv_obj)
2050 /* Get the real object in case we were fed a hard link obj */
2051 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2053 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2054 parent, name, 0, 0, 0,
2055 equiv_obj, NULL, 0))
2064 /*---------------------- Block Management and Page Allocation -------------*/
2066 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2068 if (dev->block_info_alt && dev->block_info)
2069 vfree(dev->block_info);
2071 kfree(dev->block_info);
2073 dev->block_info_alt = 0;
2075 dev->block_info = NULL;
2077 if (dev->chunk_bits_alt && dev->chunk_bits)
2078 vfree(dev->chunk_bits);
2080 kfree(dev->chunk_bits);
2081 dev->chunk_bits_alt = 0;
2082 dev->chunk_bits = NULL;
2085 static int yaffs_init_blocks(struct yaffs_dev *dev)
2087 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2089 dev->block_info = NULL;
2090 dev->chunk_bits = NULL;
2091 dev->alloc_block = -1; /* force it to get a new one */
2093 /* If the first allocation strategy fails, thry the alternate one */
2095 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2096 if (!dev->block_info) {
2098 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2099 dev->block_info_alt = 1;
2101 dev->block_info_alt = 0;
2104 if (!dev->block_info)
2107 /* Set up dynamic blockinfo stuff. Round up bytes. */
2108 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2110 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2111 if (!dev->chunk_bits) {
2113 vmalloc(dev->chunk_bit_stride * n_blocks);
2114 dev->chunk_bits_alt = 1;
2116 dev->chunk_bits_alt = 0;
2118 if (!dev->chunk_bits)
2122 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2123 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2127 yaffs_deinit_blocks(dev);
2132 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2134 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2138 /* If the block is still healthy erase it and mark as clean.
2139 * If the block has had a data failure, then retire it.
2142 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2143 "yaffs_block_became_dirty block %d state %d %s",
2144 block_no, bi->block_state,
2145 (bi->needs_retiring) ? "needs retiring" : "");
2147 yaffs2_clear_oldest_dirty_seq(dev, bi);
2149 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2151 /* If this is the block being garbage collected then stop gc'ing */
2152 if (block_no == (int)dev->gc_block)
2155 /* If this block is currently the best candidate for gc
2156 * then drop as a candidate */
2157 if (block_no == (int)dev->gc_dirtiest) {
2158 dev->gc_dirtiest = 0;
2159 dev->gc_pages_in_use = 0;
2162 if (!bi->needs_retiring) {
2163 yaffs2_checkpt_invalidate(dev);
2164 erased_ok = yaffs_erase_block(dev, block_no);
2166 dev->n_erase_failures++;
2167 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2168 "**>> Erasure failed %d", block_no);
2172 /* Verify erasure if needed */
2174 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2175 !yaffs_skip_verification(dev))) {
2176 for (i = 0; i < dev->param.chunks_per_block; i++) {
2177 if (!yaffs_check_chunk_erased(dev,
2178 block_no * dev->param.chunks_per_block + i)) {
2179 yaffs_trace(YAFFS_TRACE_ERROR,
2180 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2187 /* We lost a block of free space */
2188 dev->n_free_chunks -= dev->param.chunks_per_block;
2189 yaffs_retire_block(dev, block_no);
2190 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2191 "**>> Block %d retired", block_no);
2195 /* Clean it up... */
2196 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2198 dev->n_erased_blocks++;
2199 bi->pages_in_use = 0;
2200 bi->soft_del_pages = 0;
2201 bi->has_shrink_hdr = 0;
2202 bi->skip_erased_check = 1; /* Clean, so no need to check */
2203 bi->gc_prioritise = 0;
2204 bi->has_summary = 0;
2206 yaffs_clear_chunk_bits(dev, block_no);
2208 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2211 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2212 struct yaffs_block_info *bi,
2213 int old_chunk, u8 *buffer)
2217 struct yaffs_ext_tags tags;
2218 struct yaffs_obj *object;
2220 int ret_val = YAFFS_OK;
2222 memset(&tags, 0, sizeof(tags));
2223 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2225 object = yaffs_find_by_number(dev, tags.obj_id);
2227 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2228 "Collecting chunk in block %d, %d %d %d ",
2229 dev->gc_chunk, tags.obj_id,
2230 tags.chunk_id, tags.n_bytes);
2232 if (object && !yaffs_skip_verification(dev)) {
2233 if (tags.chunk_id == 0)
2236 else if (object->soft_del)
2237 /* Defeat the test */
2238 matching_chunk = old_chunk;
2241 yaffs_find_chunk_in_file
2242 (object, tags.chunk_id,
2245 if (old_chunk != matching_chunk)
2246 yaffs_trace(YAFFS_TRACE_ERROR,
2247 "gc: page in gc mismatch: %d %d %d %d",
2255 yaffs_trace(YAFFS_TRACE_ERROR,
2256 "page %d in gc has no object: %d %d %d ",
2258 tags.obj_id, tags.chunk_id,
2264 object->soft_del && tags.chunk_id != 0) {
2265 /* Data chunk in a soft deleted file,
2267 * It's a soft deleted data chunk,
2268 * No need to copy this, just forget
2269 * about it and fix up the object.
2272 /* Free chunks already includes
2273 * softdeleted chunks, how ever this
2274 * chunk is going to soon be really
2275 * deleted which will increment free
2276 * chunks. We have to decrement free
2277 * chunks so this works out properly.
2279 dev->n_free_chunks--;
2280 bi->soft_del_pages--;
2282 object->n_data_chunks--;
2283 if (object->n_data_chunks <= 0) {
2284 /* remeber to clean up obj */
2285 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2289 } else if (object) {
2290 /* It's either a data chunk in a live
2291 * file or an ObjectHeader, so we're
2293 * NB Need to keep the ObjectHeaders of
2294 * deleted files until the whole file
2295 * has been deleted off
2297 tags.serial_number++;
2300 if (tags.chunk_id == 0) {
2301 /* It is an object Id,
2302 * We need to nuke the shrinkheader flags since its
2304 * Also need to clean up shadowing.
2305 * NB We don't want to do all the work of translating
2306 * object header endianism back and forth so we leave
2307 * the oh endian in its stored order.
2310 struct yaffs_obj_hdr *oh;
2311 oh = (struct yaffs_obj_hdr *) buffer;
2314 tags.extra_is_shrink = 0;
2315 oh->shadows_obj = 0;
2316 oh->inband_shadowed_obj_id = 0;
2317 tags.extra_shadows = 0;
2319 /* Update file size */
2320 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2321 yaffs_oh_size_load(dev, oh,
2322 object->variant.file_variant.stored_size, 1);
2323 tags.extra_file_size =
2324 object->variant.file_variant.stored_size;
2327 yaffs_verify_oh(object, oh, &tags, 1);
2329 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2332 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2335 if (new_chunk < 0) {
2336 ret_val = YAFFS_FAIL;
2339 /* Now fix up the Tnodes etc. */
2341 if (tags.chunk_id == 0) {
2343 object->hdr_chunk = new_chunk;
2344 object->serial = tags.serial_number;
2346 /* It's a data chunk */
2347 yaffs_put_chunk_in_file(object, tags.chunk_id,
2352 if (ret_val == YAFFS_OK)
2353 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2357 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2360 int ret_val = YAFFS_OK;
2362 int is_checkpt_block;
2364 int chunks_before = yaffs_get_erased_chunks(dev);
2366 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2368 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2370 yaffs_trace(YAFFS_TRACE_TRACING,
2371 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2372 block, bi->pages_in_use, bi->has_shrink_hdr,
2375 /*yaffs_verify_free_chunks(dev); */
2377 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2378 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2380 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2382 dev->gc_disable = 1;
2384 yaffs_summary_gc(dev, block);
2386 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2387 yaffs_trace(YAFFS_TRACE_TRACING,
2388 "Collecting block %d that has no chunks in use",
2390 yaffs_block_became_dirty(dev, block);
2393 u8 *buffer = yaffs_get_temp_buffer(dev);
2395 yaffs_verify_blk(dev, bi, block);
2397 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2398 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2400 for (/* init already done */ ;
2401 ret_val == YAFFS_OK &&
2402 dev->gc_chunk < dev->param.chunks_per_block &&
2403 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2405 dev->gc_chunk++, old_chunk++) {
2406 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2407 /* Page is in use and might need to be copied */
2409 ret_val = yaffs_gc_process_chunk(dev, bi,
2413 yaffs_release_temp_buffer(dev, buffer);
2416 yaffs_verify_collected_blk(dev, bi, block);
2418 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2420 * The gc did not complete. Set block state back to FULL
2421 * because checkpointing does not restore gc.
2423 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2425 /* The gc completed. */
2426 /* Do any required cleanups */
2427 for (i = 0; i < dev->n_clean_ups; i++) {
2428 /* Time to delete the file too */
2429 struct yaffs_obj *object =
2430 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2432 yaffs_free_tnode(dev,
2433 object->variant.file_variant.top);
2434 object->variant.file_variant.top = NULL;
2435 yaffs_trace(YAFFS_TRACE_GC,
2436 "yaffs: About to finally delete object %d",
2438 yaffs_generic_obj_del(object);
2439 object->my_dev->n_deleted_files--;
2443 chunks_after = yaffs_get_erased_chunks(dev);
2444 if (chunks_before >= chunks_after)
2445 yaffs_trace(YAFFS_TRACE_GC,
2446 "gc did not increase free chunks before %d after %d",
2447 chunks_before, chunks_after);
2450 dev->n_clean_ups = 0;
2453 dev->gc_disable = 0;
2459 * find_gc_block() selects the dirtiest block (or close enough)
2460 * for garbage collection.
2463 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2464 int aggressive, int background)
2469 int prioritised = 0;
2470 int prioritised_exist = 0;
2471 struct yaffs_block_info *bi;
2472 u32 threshold = dev->param.chunks_per_block;
2476 /* First let's see if we need to grab a prioritised block */
2477 if (dev->has_pending_prioritised_gc && !aggressive) {
2478 dev->gc_dirtiest = 0;
2479 bi = dev->block_info;
2480 for (i = dev->internal_start_block;
2481 i <= dev->internal_end_block && !selected; i++) {
2483 if (bi->gc_prioritise) {
2484 prioritised_exist = 1;
2485 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2486 yaffs_block_ok_for_gc(dev, bi)) {
2495 * If there is a prioritised block and none was selected then
2496 * this happened because there is at least one old dirty block
2497 * gumming up the works. Let's gc the oldest dirty block.
2500 if (prioritised_exist &&
2501 !selected && dev->oldest_dirty_block > 0)
2502 selected = dev->oldest_dirty_block;
2504 if (!prioritised_exist) /* None found, so we can clear this */
2505 dev->has_pending_prioritised_gc = 0;
2508 /* If we're doing aggressive GC then we are happy to take a less-dirty
2509 * block, and search harder.
2510 * else (leasurely gc), then we only bother to do this if the
2511 * block has only a few pages in use.
2517 dev->internal_end_block - dev->internal_start_block + 1;
2519 threshold = dev->param.chunks_per_block;
2520 iterations = n_blocks;
2525 max_threshold = dev->param.chunks_per_block / 2;
2527 max_threshold = dev->param.chunks_per_block / 8;
2529 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2530 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2532 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2533 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2534 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2535 if (threshold > max_threshold)
2536 threshold = max_threshold;
2538 iterations = n_blocks / 16 + 1;
2539 if (iterations > 100)
2545 (dev->gc_dirtiest < 1 ||
2546 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2548 dev->gc_block_finder++;
2549 if (dev->gc_block_finder < dev->internal_start_block ||
2550 dev->gc_block_finder > dev->internal_end_block)
2551 dev->gc_block_finder =
2552 dev->internal_start_block;
2554 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2556 pages_used = bi->pages_in_use - bi->soft_del_pages;
2558 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2559 pages_used < dev->param.chunks_per_block &&
2560 (dev->gc_dirtiest < 1 ||
2561 pages_used < dev->gc_pages_in_use) &&
2562 yaffs_block_ok_for_gc(dev, bi)) {
2563 dev->gc_dirtiest = dev->gc_block_finder;
2564 dev->gc_pages_in_use = pages_used;
2568 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2569 selected = dev->gc_dirtiest;
2573 * If nothing has been selected for a while, try the oldest dirty
2574 * because that's gumming up the works.
2577 if (!selected && dev->param.is_yaffs2 &&
2578 dev->gc_not_done >= (background ? 10 : 20)) {
2579 yaffs2_find_oldest_dirty_seq(dev);
2580 if (dev->oldest_dirty_block > 0) {
2581 selected = dev->oldest_dirty_block;
2582 dev->gc_dirtiest = selected;
2583 dev->oldest_dirty_gc_count++;
2584 bi = yaffs_get_block_info(dev, selected);
2585 dev->gc_pages_in_use =
2586 bi->pages_in_use - bi->soft_del_pages;
2588 dev->gc_not_done = 0;
2593 yaffs_trace(YAFFS_TRACE_GC,
2594 "GC Selected block %d with %d free, prioritised:%d",
2596 dev->param.chunks_per_block - dev->gc_pages_in_use,
2603 dev->gc_dirtiest = 0;
2604 dev->gc_pages_in_use = 0;
2605 dev->gc_not_done = 0;
2606 if (dev->refresh_skip > 0)
2607 dev->refresh_skip--;
2610 yaffs_trace(YAFFS_TRACE_GC,
2611 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2612 dev->gc_block_finder, dev->gc_not_done, threshold,
2613 dev->gc_dirtiest, dev->gc_pages_in_use,
2614 dev->oldest_dirty_block, background ? " bg" : "");
2620 /* New garbage collector
2621 * If we're very low on erased blocks then we do aggressive garbage collection
2622 * otherwise we do "leasurely" garbage collection.
2623 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2624 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2626 * The idea is to help clear out space in a more spread-out manner.
2627 * Dunno if it really does anything useful.
2629 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2632 int gc_ok = YAFFS_OK;
2636 int checkpt_block_adjust;
2638 if (dev->param.gc_control_fn &&
2639 (dev->param.gc_control_fn(dev) & 1) == 0)
2642 if (dev->gc_disable)
2643 /* Bail out so we don't get recursive gc */
2646 /* This loop should pass the first time.
2647 * Only loops here if the collection does not increase space.
2653 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2656 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2658 dev->n_erased_blocks * dev->param.chunks_per_block;
2660 /* If we need a block soon then do aggressive gc. */
2661 if (dev->n_erased_blocks < min_erased)
2665 && erased_chunks > (dev->n_free_chunks / 4))
2668 if (dev->gc_skip > 20)
2670 if (erased_chunks < dev->n_free_chunks / 2 ||
2671 dev->gc_skip < 1 || background)
2681 /* If we don't already have a block being gc'd then see if we
2682 * should start another */
2684 if (dev->gc_block < 1 && !aggressive) {
2685 dev->gc_block = yaffs2_find_refresh_block(dev);
2687 dev->n_clean_ups = 0;
2689 if (dev->gc_block < 1) {
2691 yaffs_find_gc_block(dev, aggressive, background);
2693 dev->n_clean_ups = 0;
2696 if (dev->gc_block > 0) {
2699 dev->passive_gc_count++;
2701 yaffs_trace(YAFFS_TRACE_GC,
2702 "yaffs: GC n_erased_blocks %d aggressive %d",
2703 dev->n_erased_blocks, aggressive);
2705 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2708 if (dev->n_erased_blocks < (int)dev->param.n_reserved_blocks &&
2709 dev->gc_block > 0) {
2710 yaffs_trace(YAFFS_TRACE_GC,
2711 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2712 dev->n_erased_blocks, max_tries,
2715 } while ((dev->n_erased_blocks < (int)dev->param.n_reserved_blocks) &&
2716 (dev->gc_block > 0) && (max_tries < 2));
2718 return aggressive ? gc_ok : YAFFS_OK;
2723 * Garbage collects. Intended to be called from a background thread.
2724 * Returns non-zero if at least half the free chunks are erased.
2726 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2728 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2731 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2733 yaffs_check_gc(dev, 1);
2734 return erased_chunks > dev->n_free_chunks / 2;
2737 /*-------------------- Data file manipulation -----------------*/
2739 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2741 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2743 if (nand_chunk >= 0)
2744 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2747 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2748 "Chunk %d not found zero instead",
2750 /* get sane (zero) data if you read a hole */
2751 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2757 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2762 struct yaffs_ext_tags tags;
2763 struct yaffs_block_info *bi;
2770 block = chunk_id / dev->param.chunks_per_block;
2771 page = chunk_id % dev->param.chunks_per_block;
2773 if (!yaffs_check_chunk_bit(dev, block, page))
2774 yaffs_trace(YAFFS_TRACE_VERIFY,
2775 "Deleting invalid chunk %d", chunk_id);
2777 bi = yaffs_get_block_info(dev, block);
2779 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2781 yaffs_trace(YAFFS_TRACE_DELETION,
2782 "line %d delete of chunk %d",
2785 if (!dev->param.is_yaffs2 && mark_flash &&
2786 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2788 memset(&tags, 0, sizeof(tags));
2789 tags.is_deleted = 1;
2790 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2791 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2793 dev->n_unmarked_deletions++;
2796 /* Pull out of the management area.
2797 * If the whole block became dirty, this will kick off an erasure.
2799 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2800 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2801 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
2802 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2803 dev->n_free_chunks++;
2804 yaffs_clear_chunk_bit(dev, block, page);
2807 if (bi->pages_in_use == 0 &&
2808 !bi->has_shrink_hdr &&
2809 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2810 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
2811 yaffs_block_became_dirty(dev, block);
2816 int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2817 const u8 *buffer, int n_bytes, int use_reserve)
2819 /* Find old chunk Need to do this to get serial number
2820 * Write new one and patch into tree.
2821 * Invalidate old tags.
2825 struct yaffs_ext_tags prev_tags;
2827 struct yaffs_ext_tags new_tags;
2828 struct yaffs_dev *dev = in->my_dev;
2831 yaffs_check_gc(dev, 0);
2833 /* Get the previous chunk at this location in the file if it exists.
2834 * If it does not exist then put a zero into the tree. This creates
2835 * the tnode now, rather than later when it is harder to clean up.
2837 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
2838 if (prev_chunk_id < 1 &&
2839 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
2842 /* Set up new tags */
2843 memset(&new_tags, 0, sizeof(new_tags));
2845 new_tags.chunk_id = inode_chunk;
2846 new_tags.obj_id = in->obj_id;
2847 new_tags.serial_number =
2848 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
2849 new_tags.n_bytes = n_bytes;
2851 if (n_bytes < 1 || n_bytes > (int)dev->data_bytes_per_chunk) {
2852 yaffs_trace(YAFFS_TRACE_ERROR,
2853 "Writing %d bytes to chunk!!!!!!!!!",
2859 * If this is a data chunk and the write goes past the end of the stored
2860 * size then update the stored_size.
2862 if (inode_chunk > 0) {
2863 endpos = (inode_chunk - 1) * dev->data_bytes_per_chunk +
2865 if (in->variant.file_variant.stored_size < endpos)
2866 in->variant.file_variant.stored_size = endpos;
2870 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
2872 if (new_chunk_id > 0) {
2873 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
2875 if (prev_chunk_id > 0)
2876 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
2878 yaffs_verify_file_sane(in);
2880 return new_chunk_id;
2885 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
2886 const YCHAR *name, const void *value, int size,
2889 struct yaffs_xattr_mod xmod;
2897 xmod.result = -ENOSPC;
2899 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
2907 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
2908 struct yaffs_xattr_mod *xmod)
2911 int x_offs = sizeof(struct yaffs_obj_hdr);
2912 struct yaffs_dev *dev = obj->my_dev;
2913 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
2914 char *x_buffer = buffer + x_offs;
2918 nval_set(dev, x_buffer, x_size, xmod->name, xmod->data,
2919 xmod->size, xmod->flags);
2921 retval = nval_del(dev, x_buffer, x_size, xmod->name);
2923 obj->has_xattr = nval_hasvalues(dev, x_buffer, x_size);
2924 obj->xattr_known = 1;
2925 xmod->result = retval;
2930 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
2931 void *value, int size)
2933 char *buffer = NULL;
2935 struct yaffs_ext_tags tags;
2936 struct yaffs_dev *dev = obj->my_dev;
2937 int x_offs = sizeof(struct yaffs_obj_hdr);
2938 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
2942 if (obj->hdr_chunk < 1)
2945 /* If we know that the object has no xattribs then don't do all the
2946 * reading and parsing.
2948 if (obj->xattr_known && !obj->has_xattr) {
2955 buffer = (char *)yaffs_get_temp_buffer(dev);
2960 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
2962 if (result != YAFFS_OK)
2965 x_buffer = buffer + x_offs;
2967 if (!obj->xattr_known) {
2968 obj->has_xattr = nval_hasvalues(dev, x_buffer, x_size);
2969 obj->xattr_known = 1;
2973 retval = nval_get(dev, x_buffer, x_size,
2976 retval = nval_list(dev, x_buffer, x_size, value, size);
2978 yaffs_release_temp_buffer(dev, (u8 *) buffer);
2982 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
2983 const void *value, int size, int flags)
2985 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
2988 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
2990 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
2993 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
2996 return yaffs_do_xattrib_fetch(obj, name, value, size);
2999 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3001 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3004 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3007 struct yaffs_obj_hdr *oh;
3008 struct yaffs_dev *dev;
3009 struct yaffs_ext_tags tags;
3012 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3016 buf = yaffs_get_temp_buffer(dev);
3018 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3020 if (result == YAFFS_FAIL) {
3021 yaffs_release_temp_buffer(dev, buf);
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;
3206 return new_chunk_id;
3209 /*--------------------- File read/write ------------------------
3210 * Read and write have very similar structures.
3211 * In general the read/write has three parts to it
3212 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3213 * Some complete chunks
3214 * An incomplete chunk to end off with
3216 * Curve-balls: the first chunk might also be the last chunk.
3219 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3226 struct yaffs_cache *cache;
3227 struct yaffs_dev *dev;
3232 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3235 /* OK now check for the curveball where the start and end are in
3238 if ((start + n) < dev->data_bytes_per_chunk)
3241 n_copy = dev->data_bytes_per_chunk - start;
3243 cache = yaffs_find_chunk_cache(in, chunk);
3245 /* If the chunk is already in the cache or it is less than
3246 * a whole chunk or we're using inband tags then use the cache
3247 * (if there is caching) else bypass the cache.
3249 if (cache || n_copy != (int)dev->data_bytes_per_chunk ||
3250 dev->param.inband_tags) {
3251 if (dev->param.n_caches > 0) {
3253 /* If we can't find the data in the cache,
3254 * then load it up. */
3258 yaffs_grab_chunk_cache(in->my_dev);
3260 cache->chunk_id = chunk;
3263 yaffs_rd_data_obj(in, chunk,
3268 yaffs_use_cache(dev, cache, 0);
3272 memcpy(buffer, &cache->data[start], n_copy);
3276 /* Read into the local buffer then copy.. */
3279 yaffs_get_temp_buffer(dev);
3280 yaffs_rd_data_obj(in, chunk, local_buffer);
3282 memcpy(buffer, &local_buffer[start], n_copy);
3284 yaffs_release_temp_buffer(dev, local_buffer);
3287 /* A full chunk. Read directly into the buffer. */
3288 yaffs_rd_data_obj(in, chunk, buffer);
3298 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3299 int n_bytes, int write_through)
3308 loff_t start_write = offset;
3309 int chunk_written = 0;
3312 struct yaffs_dev *dev;
3316 while (n > 0 && chunk_written >= 0) {
3317 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3319 if (((loff_t)chunk) *
3320 dev->data_bytes_per_chunk + start != offset ||
3321 start >= dev->data_bytes_per_chunk) {
3322 yaffs_trace(YAFFS_TRACE_ERROR,
3323 "AddrToChunk of offset %lld gives chunk %d start %d",
3324 (long long)offset, chunk, start);
3326 chunk++; /* File pos to chunk in file offset */
3328 /* OK now check for the curveball where the start and end are in
3332 if ((start + n) < dev->data_bytes_per_chunk) {
3335 /* Now calculate how many bytes to write back....
3336 * If we're overwriting and not writing to then end of
3337 * file then we need to write back as much as was there
3341 chunk_start = (((loff_t)(chunk - 1)) *
3342 dev->data_bytes_per_chunk);
3344 if (chunk_start > in->variant.file_variant.file_size)
3345 n_bytes_read = 0; /* Past end of file */
3348 in->variant.file_variant.file_size -
3351 if (n_bytes_read > dev->data_bytes_per_chunk)
3352 n_bytes_read = dev->data_bytes_per_chunk;
3356 (start + n)) ? n_bytes_read : (start + n);
3358 if (n_writeback < 0 ||
3359 n_writeback > (int)dev->data_bytes_per_chunk)
3363 n_copy = dev->data_bytes_per_chunk - start;
3364 n_writeback = dev->data_bytes_per_chunk;
3367 if (n_copy != (int)dev->data_bytes_per_chunk ||
3368 !dev->param.cache_bypass_aligned ||
3369 dev->param.inband_tags) {
3370 /* An incomplete start or end chunk (or maybe both
3371 * start and end chunk), or we're using inband tags,
3372 * or we're forcing writes through the cache,
3373 * so we want to use the cache buffers.
3375 if (dev->param.n_caches > 0) {
3376 struct yaffs_cache *cache;
3378 /* If we can't find the data in the cache, then
3380 cache = yaffs_find_chunk_cache(in, chunk);
3383 yaffs_check_alloc_available(dev, 1)) {
3384 cache = yaffs_grab_chunk_cache(dev);
3386 cache->chunk_id = chunk;
3389 yaffs_rd_data_obj(in, chunk,
3393 !yaffs_check_alloc_available(dev,
3395 /* Drop the cache if it was a read cache
3396 * item and no space check has been made
3403 yaffs_use_cache(dev, cache, 1);
3406 memcpy(&cache->data[start], buffer,
3410 cache->n_bytes = n_writeback;
3412 if (write_through) {
3422 chunk_written = -1; /* fail write */
3425 /* An incomplete start or end chunk (or maybe
3426 * both start and end chunk). Read into the
3427 * local buffer then copy over and write back.
3430 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3432 yaffs_rd_data_obj(in, chunk, local_buffer);
3433 memcpy(&local_buffer[start], buffer, n_copy);
3436 yaffs_wr_data_obj(in, chunk,
3440 yaffs_release_temp_buffer(dev, local_buffer);
3443 /* A full chunk. Write directly from the buffer. */
3446 yaffs_wr_data_obj(in, chunk, buffer,
3447 dev->data_bytes_per_chunk, 0);
3449 /* Since we've overwritten the cached data,
3450 * we better invalidate it. */
3451 yaffs_invalidate_chunk_cache(in, chunk);
3454 if (chunk_written >= 0) {
3462 /* Update file object */
3464 if ((start_write + n_done) > in->variant.file_variant.file_size)
3465 in->variant.file_variant.file_size = (start_write + n_done);
3471 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3472 int n_bytes, int write_through)
3474 yaffs2_handle_hole(in, offset);
3475 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
3478 /* ---------------------- File resizing stuff ------------------ */
3480 static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
3483 struct yaffs_dev *dev = in->my_dev;
3484 loff_t old_size = in->variant.file_variant.file_size;
3492 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3496 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3497 &start_del, &dummy);
3501 /* Delete backwards so that we don't end up with holes if
3502 * power is lost part-way through the operation.
3504 for (i = last_del; i >= start_del; i--) {
3505 /* NB this could be optimised somewhat,
3506 * eg. could retrieve the tags and write them without
3507 * using yaffs_chunk_del
3510 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3516 (dev->internal_start_block * dev->param.chunks_per_block) ||
3518 ((dev->internal_end_block + 1) *
3519 dev->param.chunks_per_block)) {
3520 yaffs_trace(YAFFS_TRACE_ALWAYS,
3521 "Found daft chunk_id %d for %d",
3524 in->n_data_chunks--;
3525 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3530 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3534 struct yaffs_dev *dev = obj->my_dev;
3536 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3538 yaffs_prune_chunks(obj, new_size);
3540 if (new_partial != 0) {
3541 int last_chunk = 1 + new_full;
3542 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3544 /* Rewrite the last chunk with its new size and zero pad */
3545 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3546 memset(local_buffer + new_partial, 0,
3547 dev->data_bytes_per_chunk - new_partial);
3549 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3552 yaffs_release_temp_buffer(dev, local_buffer);
3555 obj->variant.file_variant.file_size = new_size;
3556 obj->variant.file_variant.stored_size = new_size;
3558 yaffs_prune_tree(dev, &obj->variant.file_variant);
3561 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3563 struct yaffs_dev *dev = in->my_dev;
3564 loff_t old_size = in->variant.file_variant.file_size;
3566 yaffs_flush_file_cache(in, 1);
3567 yaffs_invalidate_file_cache(in);
3569 yaffs_check_gc(dev, 0);
3571 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3574 if (new_size == old_size)
3577 if (new_size > old_size) {
3578 yaffs2_handle_hole(in, new_size);
3579 in->variant.file_variant.file_size = new_size;
3581 /* new_size < old_size */
3582 yaffs_resize_file_down(in, new_size);
3585 /* Write a new object header to reflect the resize.
3586 * show we've shrunk the file, if need be
3587 * Do this only if the file is not in the deleted directories
3588 * and is not shadowed.
3592 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3593 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3594 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3599 int yaffs_flush_file(struct yaffs_obj *in,
3607 yaffs_flush_file_cache(in, discard_cache);
3613 yaffs_load_current_time(in, 0, 0);
3615 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3616 YAFFS_OK : YAFFS_FAIL;
3620 /* yaffs_del_file deletes the whole file data
3621 * and the inode associated with the file.
3622 * It does not delete the links associated with the file.
3624 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3628 struct yaffs_dev *dev = in->my_dev;
3635 yaffs_change_obj_name(in, in->my_dev->del_dir,
3636 _Y("deleted"), 0, 0);
3637 yaffs_trace(YAFFS_TRACE_TRACING,
3638 "yaffs: immediate deletion of file %d",
3641 in->my_dev->n_deleted_files++;
3642 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3643 yaffs_resize_file(in, 0);
3644 yaffs_soft_del_file(in);
3647 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3648 _Y("unlinked"), 0, 0);
3653 static int yaffs_del_file(struct yaffs_obj *in)
3655 int ret_val = YAFFS_OK;
3656 int deleted; /* Need to cache value on stack if in is freed */
3657 struct yaffs_dev *dev = in->my_dev;
3659 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3660 yaffs_resize_file(in, 0);
3662 if (in->n_data_chunks > 0) {
3663 /* Use soft deletion if there is data in the file.
3664 * That won't be the case if it has been resized to zero.
3667 ret_val = yaffs_unlink_file_if_needed(in);
3669 deleted = in->deleted;
3671 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3674 in->my_dev->n_deleted_files++;
3675 yaffs_soft_del_file(in);
3677 return deleted ? YAFFS_OK : YAFFS_FAIL;
3679 /* The file has no data chunks so we toss it immediately */
3680 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3681 in->variant.file_variant.top = NULL;
3682 yaffs_generic_obj_del(in);
3688 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3691 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3692 !(list_empty(&obj->variant.dir_variant.children));
3695 static int yaffs_del_dir(struct yaffs_obj *obj)
3697 /* First check that the directory is empty. */
3698 if (yaffs_is_non_empty_dir(obj))
3701 return yaffs_generic_obj_del(obj);
3704 static int yaffs_del_symlink(struct yaffs_obj *in)
3706 kfree(in->variant.symlink_variant.alias);
3707 in->variant.symlink_variant.alias = NULL;
3709 return yaffs_generic_obj_del(in);
3712 static int yaffs_del_link(struct yaffs_obj *in)
3714 /* remove this hardlink from the list associated with the equivalent
3717 list_del_init(&in->hard_links);
3718 return yaffs_generic_obj_del(in);
3721 int yaffs_del_obj(struct yaffs_obj *obj)
3725 switch (obj->variant_type) {
3726 case YAFFS_OBJECT_TYPE_FILE:
3727 ret_val = yaffs_del_file(obj);
3729 case YAFFS_OBJECT_TYPE_DIRECTORY:
3730 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3731 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3732 "Remove object %d from dirty directories",
3734 list_del_init(&obj->variant.dir_variant.dirty);
3736 return yaffs_del_dir(obj);
3738 case YAFFS_OBJECT_TYPE_SYMLINK:
3739 ret_val = yaffs_del_symlink(obj);
3741 case YAFFS_OBJECT_TYPE_HARDLINK:
3742 ret_val = yaffs_del_link(obj);
3744 case YAFFS_OBJECT_TYPE_SPECIAL:
3745 ret_val = yaffs_generic_obj_del(obj);
3747 case YAFFS_OBJECT_TYPE_UNKNOWN:
3749 break; /* should not happen. */
3755 static void yaffs_empty_dir_to_dir(struct yaffs_obj *from_dir,
3756 struct yaffs_obj *to_dir)
3758 struct yaffs_obj *obj;
3759 struct list_head *lh;
3760 struct list_head *n;
3762 list_for_each_safe(lh, n, &from_dir->variant.dir_variant.children) {
3763 obj = list_entry(lh, struct yaffs_obj, siblings);
3764 yaffs_add_obj_to_dir(to_dir, obj);
3768 struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
3769 enum yaffs_obj_type type)
3771 /* Tear down the old variant */
3772 switch (obj->variant_type) {
3773 case YAFFS_OBJECT_TYPE_FILE:
3774 /* Nuke file data */
3775 yaffs_resize_file(obj, 0);
3776 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
3777 obj->variant.file_variant.top = NULL;
3779 case YAFFS_OBJECT_TYPE_DIRECTORY:
3780 /* Put the children in lost and found. */
3781 yaffs_empty_dir_to_dir(obj, obj->my_dev->lost_n_found);
3782 if (!list_empty(&obj->variant.dir_variant.dirty))
3783 list_del_init(&obj->variant.dir_variant.dirty);
3785 case YAFFS_OBJECT_TYPE_SYMLINK:
3786 /* Nuke symplink data */
3787 kfree(obj->variant.symlink_variant.alias);
3788 obj->variant.symlink_variant.alias = NULL;
3790 case YAFFS_OBJECT_TYPE_HARDLINK:
3791 list_del_init(&obj->hard_links);
3797 memset(&obj->variant, 0, sizeof(obj->variant));
3799 /*Set up new variant if the memset is not enough. */
3801 case YAFFS_OBJECT_TYPE_DIRECTORY:
3802 INIT_LIST_HEAD(&obj->variant.dir_variant.children);
3803 INIT_LIST_HEAD(&obj->variant.dir_variant.dirty);
3805 case YAFFS_OBJECT_TYPE_FILE:
3806 case YAFFS_OBJECT_TYPE_SYMLINK:
3807 case YAFFS_OBJECT_TYPE_HARDLINK:
3812 obj->variant_type = type;
3818 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3828 yaffs_update_parent(obj->parent);
3830 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3831 return yaffs_del_link(obj);
3832 } else if (!list_empty(&obj->hard_links)) {
3833 /* Curve ball: We're unlinking an object that has a hardlink.
3835 * This problem arises because we are not strictly following
3836 * The Linux link/inode model.
3838 * We can't really delete the object.
3839 * Instead, we do the following:
3840 * - Select a hardlink.
3841 * - Unhook it from the hard links
3842 * - Move it from its parent directory so that the rename works.
3843 * - Rename the object to the hardlink's name.
3844 * - Delete the hardlink
3847 struct yaffs_obj *hl;
3848 struct yaffs_obj *parent;
3850 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3852 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3855 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3856 parent = hl->parent;
3858 list_del_init(&hl->hard_links);
3860 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3862 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
3864 if (ret_val == YAFFS_OK)
3865 ret_val = yaffs_generic_obj_del(hl);
3869 } else if (del_now) {
3870 switch (obj->variant_type) {
3871 case YAFFS_OBJECT_TYPE_FILE:
3872 return yaffs_del_file(obj);
3874 case YAFFS_OBJECT_TYPE_DIRECTORY:
3875 list_del_init(&obj->variant.dir_variant.dirty);
3876 return yaffs_del_dir(obj);
3878 case YAFFS_OBJECT_TYPE_SYMLINK:
3879 return yaffs_del_symlink(obj);
3881 case YAFFS_OBJECT_TYPE_SPECIAL:
3882 return yaffs_generic_obj_del(obj);
3884 case YAFFS_OBJECT_TYPE_HARDLINK:
3885 case YAFFS_OBJECT_TYPE_UNKNOWN:
3889 } else if (yaffs_is_non_empty_dir(obj)) {
3892 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
3893 _Y("unlinked"), 0, 0);
3897 int yaffs_unlink_obj(struct yaffs_obj *obj)
3899 if (obj && obj->unlink_allowed)
3900 return yaffs_unlink_worker(obj);
3905 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
3907 struct yaffs_obj *obj;
3909 obj = yaffs_find_by_name(dir, name);
3910 return yaffs_unlink_obj(obj);
3914 * If old_name is NULL then we take old_dir as the object to be renamed.
3916 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
3917 struct yaffs_obj *new_dir, const YCHAR *new_name)
3919 struct yaffs_obj *obj = NULL;
3920 struct yaffs_obj *existing_target = NULL;
3923 struct yaffs_dev *dev;
3925 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3929 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3934 dev = old_dir->my_dev;
3936 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
3937 /* Special case for case insemsitive systems.
3938 * While look-up is case insensitive, the name isn't.
3939 * Therefore we might want to change x.txt to X.txt
3941 if (old_dir == new_dir &&
3942 old_name && new_name &&
3943 strcmp(old_name, new_name) == 0)
3947 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
3948 YAFFS_MAX_NAME_LENGTH)
3953 obj = yaffs_find_by_name(old_dir, old_name);
3956 old_dir = obj->parent;
3959 if (obj && obj->rename_allowed) {
3960 /* Now handle an existing target, if there is one */
3961 existing_target = yaffs_find_by_name(new_dir, new_name);
3962 if (yaffs_is_non_empty_dir(existing_target)) {
3963 return YAFFS_FAIL; /* ENOTEMPTY */
3964 } else if (existing_target && existing_target != obj) {
3965 /* Nuke the target first, using shadowing,
3966 * but only if it isn't the same object.
3968 * Note we must disable gc here otherwise it can mess
3972 dev->gc_disable = 1;
3973 yaffs_change_obj_name(obj, new_dir, new_name, force,
3974 existing_target->obj_id);
3975 existing_target->is_shadowed = 1;
3976 yaffs_unlink_obj(existing_target);
3977 dev->gc_disable = 0;
3980 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
3982 yaffs_update_parent(old_dir);
3983 if (new_dir != old_dir)
3984 yaffs_update_parent(new_dir);
3991 /*----------------------- Initialisation Scanning ---------------------- */
3993 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
3994 int backward_scanning)
3996 struct yaffs_obj *obj;
3998 if (backward_scanning) {
3999 /* Handle YAFFS2 case (backward scanning)
4000 * If the shadowed object exists then ignore.
4002 obj = yaffs_find_by_number(dev, obj_id);
4007 /* Let's create it (if it does not exist) assuming it is a file so that
4008 * it can do shrinking etc.
4009 * We put it in unlinked dir to be cleaned up after the scanning
4012 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4015 obj->is_shadowed = 1;
4016 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4017 obj->variant.file_variant.shrink_size = 0;
4018 obj->valid = 1; /* So that we don't read any other info. */
4021 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4023 struct list_head *lh;
4024 struct list_head *save;
4025 struct yaffs_obj *hl;
4026 struct yaffs_obj *in;
4028 list_for_each_safe(lh, save, hard_list) {
4029 hl = list_entry(lh, struct yaffs_obj, hard_links);
4030 in = yaffs_find_by_number(dev,
4031 hl->variant.hardlink_variant.equiv_id);
4034 /* Add the hardlink pointers */
4035 hl->variant.hardlink_variant.equiv_obj = in;
4036 list_add(&hl->hard_links, &in->hard_links);
4038 /* Todo Need to report/handle this better.
4039 * Got a problem... hardlink to a non-existant object
4041 hl->variant.hardlink_variant.equiv_obj = NULL;
4042 INIT_LIST_HEAD(&hl->hard_links);
4047 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4050 * Sort out state of unlinked and deleted objects after scanning.
4052 struct list_head *i;
4053 struct list_head *n;
4054 struct yaffs_obj *l;
4059 /* Soft delete all the unlinked files */
4060 list_for_each_safe(i, n,
4061 &dev->unlinked_dir->variant.dir_variant.children) {
4062 l = list_entry(i, struct yaffs_obj, siblings);
4066 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4067 l = list_entry(i, struct yaffs_obj, siblings);
4073 * This code iterates through all the objects making sure that they are rooted.
4074 * Any unrooted objects are re-rooted in lost+found.
4075 * An object needs to be in one of:
4076 * - Directly under deleted, unlinked
4077 * - Directly or indirectly under root.
4080 * This code assumes that we don't ever change the current relationships
4081 * between directories:
4082 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4083 * lost-n-found->parent == root_dir
4085 * This fixes the problem where directories might have inadvertently been
4086 * deleted leaving the object "hanging" without being rooted in the
4090 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4092 return (obj == dev->del_dir ||
4093 obj == dev->unlinked_dir || obj == dev->root_dir);
4096 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4098 struct yaffs_obj *obj;
4099 struct yaffs_obj *parent;
4101 struct list_head *lh;
4102 struct list_head *n;
4109 /* Iterate through the objects in each hash entry,
4110 * looking at each object.
4111 * Make sure it is rooted.
4114 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4115 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4116 obj = list_entry(lh, struct yaffs_obj, hash_link);
4117 parent = obj->parent;
4119 if (yaffs_has_null_parent(dev, obj)) {
4120 /* These directories are not hanging */
4122 } else if (!parent ||
4123 parent->variant_type !=
4124 YAFFS_OBJECT_TYPE_DIRECTORY) {
4126 } else if (yaffs_has_null_parent(dev, parent)) {
4130 * Need to follow the parent chain to
4131 * see if it is hanging.
4136 while (parent != dev->root_dir &&
4138 parent->parent->variant_type ==
4139 YAFFS_OBJECT_TYPE_DIRECTORY &&
4141 parent = parent->parent;
4144 if (parent != dev->root_dir)
4148 yaffs_trace(YAFFS_TRACE_SCAN,
4149 "Hanging object %d moved to lost and found",
4151 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4158 * Delete directory contents for cleaning up lost and found.
4160 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4162 struct yaffs_obj *obj;
4163 struct list_head *lh;
4164 struct list_head *n;
4166 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4169 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4170 obj = list_entry(lh, struct yaffs_obj, siblings);
4171 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4172 yaffs_del_dir_contents(obj);
4173 yaffs_trace(YAFFS_TRACE_SCAN,
4174 "Deleting lost_found object %d",
4176 yaffs_unlink_obj(obj);
4180 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4182 yaffs_del_dir_contents(dev->lost_n_found);
4186 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4190 struct list_head *i;
4191 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4192 struct yaffs_obj *l;
4198 yaffs_trace(YAFFS_TRACE_ALWAYS,
4199 "tragedy: yaffs_find_by_name: null pointer directory"
4204 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4205 yaffs_trace(YAFFS_TRACE_ALWAYS,
4206 "tragedy: yaffs_find_by_name: non-directory"
4211 sum = yaffs_calc_name_sum(name);
4213 list_for_each(i, &directory->variant.dir_variant.children) {
4214 l = list_entry(i, struct yaffs_obj, siblings);
4216 if (l->parent != directory)
4219 yaffs_check_obj_details_loaded(l);
4221 /* Special case for lost-n-found */
4222 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4223 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4225 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4226 /* LostnFound chunk called Objxxx
4229 yaffs_get_obj_name(l, buffer,
4230 YAFFS_MAX_NAME_LENGTH + 1);
4231 if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
4238 /* GetEquivalentObject dereferences any hard links to get to the
4242 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4244 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4245 obj = obj->variant.hardlink_variant.equiv_obj;
4246 yaffs_check_obj_details_loaded(obj);
4252 * A note or two on object names.
4253 * * If the object name is missing, we then make one up in the form objnnn
4255 * * ASCII names are stored in the object header's name field from byte zero
4256 * * Unicode names are historically stored starting from byte zero.
4258 * Then there are automatic Unicode names...
4259 * The purpose of these is to save names in a way that can be read as
4260 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4261 * system to share files.
4263 * These automatic unicode are stored slightly differently...
4264 * - If the name can fit in the ASCII character space then they are saved as
4265 * ascii names as per above.
4266 * - If the name needs Unicode then the name is saved in Unicode
4267 * starting at oh->name[1].
4270 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4273 /* Create an object name if we could not find one. */
4274 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4275 YCHAR local_name[20];
4276 YCHAR num_string[20];
4277 YCHAR *x = &num_string[19];
4278 unsigned v = obj->obj_id;
4282 *x = '0' + (v % 10);
4285 /* make up a name */
4286 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4287 strcat(local_name, x);
4288 strncpy(name, local_name, buffer_size - 1);
4292 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4294 memset(name, 0, buffer_size * sizeof(YCHAR));
4295 yaffs_check_obj_details_loaded(obj);
4296 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4297 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4298 } else if (obj->short_name[0]) {
4299 strcpy(name, obj->short_name);
4300 } else if (obj->hdr_chunk > 0) {
4302 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4304 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4306 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4308 if (obj->hdr_chunk > 0) {
4309 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4310 obj->hdr_chunk, buffer, NULL);
4311 if (result == YAFFS_OK)
4312 yaffs_load_name_from_oh(obj->my_dev, name,
4313 oh->name, buffer_size);
4315 yaffs_release_temp_buffer(obj->my_dev, buffer);
4318 yaffs_fix_null_name(obj, name, buffer_size);
4320 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4323 loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
4325 /* Dereference any hard linking */
4326 obj = yaffs_get_equivalent_obj(obj);
4328 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4329 return obj->variant.file_variant.file_size;
4330 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4331 if (!obj->variant.symlink_variant.alias)
4333 return strnlen(obj->variant.symlink_variant.alias,
4334 YAFFS_MAX_ALIAS_LENGTH);
4336 /* Only a directory should drop through to here */
4337 return obj->my_dev->data_bytes_per_chunk;
4341 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4344 struct list_head *i;
4347 count++; /* the object itself */
4349 list_for_each(i, &obj->hard_links)
4350 count++; /* add the hard links; */
4355 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4357 obj = yaffs_get_equivalent_obj(obj);
4362 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4364 obj = yaffs_get_equivalent_obj(obj);
4366 switch (obj->variant_type) {
4367 case YAFFS_OBJECT_TYPE_FILE:
4370 case YAFFS_OBJECT_TYPE_DIRECTORY:
4373 case YAFFS_OBJECT_TYPE_SYMLINK:
4376 case YAFFS_OBJECT_TYPE_HARDLINK:
4379 case YAFFS_OBJECT_TYPE_SPECIAL:
4380 if (S_ISFIFO(obj->yst_mode))
4382 if (S_ISCHR(obj->yst_mode))
4384 if (S_ISBLK(obj->yst_mode))
4386 if (S_ISSOCK(obj->yst_mode))
4396 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4398 obj = yaffs_get_equivalent_obj(obj);
4399 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4400 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4402 return yaffs_clone_str(_Y(""));
4405 /*--------------------------- Initialisation code -------------------------- */
4407 static int yaffs_check_dev_fns(struct yaffs_dev *dev)
4409 struct yaffs_driver *drv = &dev->drv;
4410 struct yaffs_tags_handler *tagger = &dev->tagger;
4412 /* Common functions, gotta have */
4413 if (!drv->drv_read_chunk_fn ||
4414 !drv->drv_write_chunk_fn ||
4418 if (dev->param.is_yaffs2 &&
4419 (!drv->drv_mark_bad_fn || !drv->drv_check_bad_fn))
4422 /* Install the default tags marshalling functions if needed. */
4423 yaffs_tags_compat_install(dev);
4424 yaffs_tags_marshall_install(dev);
4426 /* Check we now have the marshalling functions required. */
4427 if (!tagger->write_chunk_tags_fn ||
4428 !tagger->read_chunk_tags_fn ||
4429 !tagger->query_block_fn ||
4430 !tagger->mark_bad_fn)
4436 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4438 /* Initialise the unlinked, deleted, root and lost+found directories */
4439 dev->lost_n_found = NULL;
4440 dev->root_dir = NULL;
4441 dev->unlinked_dir = NULL;
4442 dev->del_dir = NULL;
4445 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4447 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4449 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4450 YAFFS_ROOT_MODE | S_IFDIR);
4452 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4453 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4455 if (dev->lost_n_found &&
4457 dev->unlinked_dir &&
4459 /* If lost-n-found is hidden then yank it out of the directory tree. */
4460 if (dev->param.hide_lost_n_found)
4461 list_del_init(&dev->lost_n_found->siblings);
4463 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4470 * Typically only used by yaffs_guts_initialise, but also used by the
4471 * Low level yaffs driver tests.
4474 int yaffs_guts_ll_init(struct yaffs_dev *dev)
4478 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_ll_init()");
4481 yaffs_trace(YAFFS_TRACE_ALWAYS,
4482 "yaffs: Need a device"
4490 dev->internal_start_block = dev->param.start_block;
4491 dev->internal_end_block = dev->param.end_block;
4492 dev->block_offset = 0;
4493 dev->chunk_offset = 0;
4494 dev->n_free_chunks = 0;
4498 if (dev->param.start_block == 0) {
4499 dev->internal_start_block = dev->param.start_block + 1;
4500 dev->internal_end_block = dev->param.end_block + 1;
4501 dev->block_offset = 1;
4502 dev->chunk_offset = dev->param.chunks_per_block;
4505 /* Check geometry parameters. */
4507 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4508 dev->param.total_bytes_per_chunk < 1024) ||
4509 (!dev->param.is_yaffs2 &&
4510 dev->param.total_bytes_per_chunk < 512) ||
4511 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4512 dev->param.chunks_per_block < 2 ||
4513 dev->param.n_reserved_blocks < 2 ||
4514 dev->internal_start_block <= 0 ||
4515 dev->internal_end_block <= 0 ||
4516 dev->internal_end_block <=
4517 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4519 /* otherwise it is too small */
4520 yaffs_trace(YAFFS_TRACE_ALWAYS,
4521 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4522 dev->param.total_bytes_per_chunk,
4523 dev->param.is_yaffs2 ? "2" : "",
4524 dev->param.inband_tags);
4528 /* Sort out space for inband tags, if required */
4529 if (dev->param.inband_tags)
4530 dev->data_bytes_per_chunk =
4531 dev->param.total_bytes_per_chunk -
4532 sizeof(struct yaffs_packed_tags2_tags_only);
4534 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4536 /* Got the right mix of functions? */
4537 if (!yaffs_check_dev_fns(dev)) {
4538 /* Function missing */
4539 yaffs_trace(YAFFS_TRACE_ALWAYS,
4540 "device function(s) missing or wrong");
4545 if (!yaffs_init_tmp_buffers(dev))
4548 if (yaffs_init_nand(dev) != YAFFS_OK) {
4549 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4559 int yaffs_guts_format_dev(struct yaffs_dev *dev)
4562 enum yaffs_block_state state;
4565 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4571 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
4572 yaffs_query_init_block_state(dev, i, &state, &dummy);
4573 if (state != YAFFS_BLOCK_STATE_DEAD)
4574 yaffs_erase_block(dev, i);
4581 * If the dev is mounted r/w then the cleanup will happen during
4582 * yaffs_guts_initialise. However if the dev is mounted ro then
4583 * the cleanup will be dfered until yaffs is remounted r/w.
4585 void yaffs_guts_cleanup(struct yaffs_dev *dev)
4587 yaffs_strip_deleted_objs(dev);
4588 yaffs_fix_hanging_objs(dev);
4589 if (dev->param.empty_lost_n_found)
4590 yaffs_empty_l_n_f(dev);
4593 int yaffs_guts_initialise(struct yaffs_dev *dev)
4595 int init_failed = 0;
4599 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4602 if (dev->is_mounted) {
4603 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4607 dev->is_mounted = 1;
4609 /* OK now calculate a few things for the device */
4612 * Calculate all the chunk size manipulation numbers:
4614 x = dev->data_bytes_per_chunk;
4615 /* We always use dev->chunk_shift and dev->chunk_div */
4616 dev->chunk_shift = calc_shifts(x);
4617 x >>= dev->chunk_shift;
4619 /* We only use chunk mask if chunk_div is 1 */
4620 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4623 * Calculate chunk_grp_bits.
4624 * We need to find the next power of 2 > than internal_end_block
4627 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4629 bits = calc_shifts_ceiling(x);
4631 /* Set up tnode width if wide tnodes are enabled. */
4632 if (!dev->param.wide_tnodes_disabled) {
4633 /* bits must be even so that we end up with 32-bit words */
4637 dev->tnode_width = 16;
4639 dev->tnode_width = bits;
4641 dev->tnode_width = 16;
4644 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4646 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4647 * so if the bitwidth of the
4648 * chunk range we're using is greater than 16 we need
4649 * to figure out chunk shift and chunk_grp_size
4652 if (bits <= dev->tnode_width)
4653 dev->chunk_grp_bits = 0;
4655 dev->chunk_grp_bits = bits - dev->tnode_width;
4657 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4658 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4659 dev->tnode_size = sizeof(struct yaffs_tnode);
4661 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4663 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4664 /* We have a problem because the soft delete won't work if
4665 * the chunk group size > chunks per block.
4666 * This can be remedied by using larger "virtual blocks".
4668 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4673 /* Finished verifying the device, continue with initialisation */
4675 /* More device initialisation */
4677 dev->passive_gc_count = 0;
4678 dev->oldest_dirty_gc_count = 0;
4680 dev->gc_block_finder = 0;
4681 dev->buffered_block = -1;
4682 dev->doing_buffered_block_rewrite = 0;
4683 dev->n_deleted_files = 0;
4684 dev->n_bg_deletions = 0;
4685 dev->n_unlinked_files = 0;
4686 dev->n_ecc_fixed = 0;
4687 dev->n_ecc_unfixed = 0;
4688 dev->n_tags_ecc_fixed = 0;
4689 dev->n_tags_ecc_unfixed = 0;
4690 dev->n_erase_failures = 0;
4691 dev->n_erased_blocks = 0;
4692 dev->gc_disable = 0;
4693 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now,
4694 * will get fixed on first GC */
4695 INIT_LIST_HEAD(&dev->dirty_dirs);
4696 dev->oldest_dirty_seq = 0;
4697 dev->oldest_dirty_block = 0;
4699 yaffs_endian_config(dev);
4701 /* Initialise temporary caches. */
4702 dev->gc_cleanup_list = NULL;
4705 init_failed = yaffs_cache_init(dev) < 0;
4707 dev->cache_hits = 0;
4710 dev->gc_cleanup_list =
4711 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4713 if (!dev->gc_cleanup_list)
4717 if (dev->param.is_yaffs2)
4718 dev->param.use_header_file_size = 1;
4720 if (!init_failed && !yaffs_init_blocks(dev))
4723 yaffs_init_tnodes_and_objs(dev);
4725 if (!init_failed && !yaffs_create_initial_dir(dev))
4728 if (!init_failed && dev->param.is_yaffs2 &&
4729 !dev->param.disable_summary &&
4730 !yaffs_summary_init(dev))
4734 /* Now scan the flash. */
4735 if (dev->param.is_yaffs2) {
4736 if (yaffs2_checkpt_restore(dev)) {
4737 yaffs_check_obj_details_loaded(dev->root_dir);
4738 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4740 "yaffs: restored from checkpoint"
4744 /* Clean up the mess caused by an aborted
4745 * checkpoint load then scan backwards.
4747 yaffs_deinit_blocks(dev);
4749 yaffs_deinit_tnodes_and_objs(dev);
4751 dev->n_erased_blocks = 0;
4752 dev->n_free_chunks = 0;
4753 dev->alloc_block = -1;
4754 dev->alloc_page = -1;
4755 dev->n_deleted_files = 0;
4756 dev->n_unlinked_files = 0;
4757 dev->n_bg_deletions = 0;
4759 if (!init_failed && !yaffs_init_blocks(dev))
4762 yaffs_init_tnodes_and_objs(dev);
4765 && !yaffs_create_initial_dir(dev))
4768 if (!init_failed && !yaffs2_scan_backwards(dev))
4771 } else if (!yaffs1_scan(dev)) {
4775 yaffs_guts_cleanup(dev);
4779 /* Clean up the mess */
4780 yaffs_trace(YAFFS_TRACE_TRACING,
4781 "yaffs: yaffs_guts_initialise() aborted.");
4783 yaffs_deinitialise(dev);
4787 /* Zero out stats */
4788 dev->n_page_reads = 0;
4789 dev->n_page_writes = 0;
4790 dev->n_erasures = 0;
4791 dev->n_gc_copies = 0;
4792 dev->n_retried_writes = 0;
4794 dev->n_retired_blocks = 0;
4796 yaffs_verify_free_chunks(dev);
4797 yaffs_verify_blocks(dev);
4799 /* Clean up any aborted checkpoint data */
4800 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4801 yaffs2_checkpt_invalidate(dev);
4803 yaffs_trace(YAFFS_TRACE_TRACING,
4804 "yaffs: yaffs_guts_initialise() done.");
4808 void yaffs_deinitialise(struct yaffs_dev *dev)
4810 if (dev->is_mounted) {
4813 yaffs_deinit_blocks(dev);
4814 yaffs_deinit_tnodes_and_objs(dev);
4815 yaffs_summary_deinit(dev);
4816 yaffs_cache_deinit(dev);
4818 kfree(dev->gc_cleanup_list);
4820 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4821 kfree(dev->temp_buffer[i].buffer);
4822 dev->temp_buffer[i].buffer = NULL;
4825 kfree(dev->checkpt_buffer);
4826 dev->checkpt_buffer = NULL;
4827 kfree(dev->checkpt_block_list);
4828 dev->checkpt_block_list = NULL;
4831 dev->is_mounted = 0;
4833 yaffs_deinit_nand(dev);
4837 int yaffs_count_free_chunks(struct yaffs_dev *dev)
4841 struct yaffs_block_info *blk;
4843 blk = dev->block_info;
4844 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4845 switch (blk->block_state) {
4846 case YAFFS_BLOCK_STATE_EMPTY:
4847 case YAFFS_BLOCK_STATE_ALLOCATING:
4848 case YAFFS_BLOCK_STATE_COLLECTING:
4849 case YAFFS_BLOCK_STATE_FULL:
4851 (dev->param.chunks_per_block - blk->pages_in_use +
4852 blk->soft_del_pages);
4862 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
4864 /* This is what we report to the outside world */
4867 int blocks_for_checkpt;
4869 n_free = dev->n_free_chunks;
4870 n_free += dev->n_deleted_files;
4872 /* Now count and subtract the number of dirty chunks in the cache. */
4873 n_dirty_caches = yaffs_count_dirty_caches(dev);
4874 n_free -= n_dirty_caches;
4877 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
4879 /* Now figure checkpoint space and report that... */
4880 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
4882 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
4891 * Marshalling functions to get the appropriate time values saved
4892 * and restored to/from obj headers.
4894 * Note that the WinCE time fields are used to store the 32-bit values.
4897 static void yaffs_oh_time_load(u32 *yst_time, u32 *win_time, YTIME_T timeval)
4902 lower = timeval & 0xffffffff;
4903 if (sizeof(YTIME_T) > sizeof(u32))
4904 upper = (timeval >> 32) & 0xffffffff;
4909 win_time[0] = lower;
4910 win_time[1] = upper;
4913 static YTIME_T yaffs_oh_time_fetch(const u32 *yst_time, const u32 *win_time)
4918 if (win_time[1] == 0xffffffff) {
4922 upper = win_time[1];
4923 lower = win_time[0];
4925 if (sizeof(YTIME_T) > sizeof(u32)) {
4927 ret = (((u64)upper) << 32) | lower;
4928 return (YTIME_T) ret;
4931 return (YTIME_T) lower;
4934 YTIME_T yaffs_oh_ctime_fetch(struct yaffs_obj_hdr *oh)
4936 return yaffs_oh_time_fetch(&oh->yst_ctime, oh->win_ctime);
4939 YTIME_T yaffs_oh_mtime_fetch(struct yaffs_obj_hdr *oh)
4941 return yaffs_oh_time_fetch(&oh->yst_mtime, oh->win_mtime);
4944 YTIME_T yaffs_oh_atime_fetch(struct yaffs_obj_hdr *oh)
4946 return yaffs_oh_time_fetch(&oh->yst_atime, oh->win_atime);
4949 void yaffs_oh_ctime_load(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
4951 yaffs_oh_time_load(&oh->yst_ctime, oh->win_ctime, obj->yst_ctime);
4954 void yaffs_oh_mtime_load(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
4956 yaffs_oh_time_load(&oh->yst_mtime, oh->win_mtime, obj->yst_mtime);
4959 void yaffs_oh_atime_load(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
4961 yaffs_oh_time_load(&oh->yst_atime, oh->win_atime, obj->yst_atime);
4966 * Marshalling functions to get loff_t file sizes into and out of
4969 void yaffs_oh_size_load(struct yaffs_dev *dev,
4970 struct yaffs_obj_hdr *oh,
4974 oh->file_size_low = FSIZE_LOW(fsize);
4976 oh->file_size_high = FSIZE_HIGH(fsize);
4979 yaffs_do_endian_u32(dev, &oh->file_size_low);
4980 yaffs_do_endian_u32(dev, &oh->file_size_high);
4984 loff_t yaffs_oh_to_size(struct yaffs_dev *dev, struct yaffs_obj_hdr *oh,
4990 if (sizeof(loff_t) >= 8 && ~(oh->file_size_high)) {
4991 u32 low = oh->file_size_low;
4992 u32 high = oh->file_size_high;
4995 yaffs_do_endian_u32 (dev, &low);
4996 yaffs_do_endian_u32 (dev, &high);
4998 retval = FSIZE_COMBINE(high, low);
5000 u32 low = oh->file_size_low;
5003 yaffs_do_endian_u32(dev, &low);
5004 retval = (loff_t)low;
5011 void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
5014 struct yaffs_block_info *bi;
5017 for(i = 0; i < 10; i++)
5020 for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
5021 bi = yaffs_get_block_info(dev, i);
5022 s = bi->block_state;
5023 if(s > YAFFS_BLOCK_STATE_DEAD || s < YAFFS_BLOCK_STATE_UNKNOWN)