2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_trace.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_getblockinfo.h"
19 #include "yaffs_tagscompat.h"
20 #include "yaffs_tagsmarshall.h"
21 #include "yaffs_nand.h"
22 #include "yaffs_yaffs1.h"
23 #include "yaffs_yaffs2.h"
24 #include "yaffs_bitmap.h"
25 #include "yaffs_verify.h"
26 #include "yaffs_nand.h"
27 #include "yaffs_packedtags2.h"
28 #include "yaffs_nameval.h"
29 #include "yaffs_allocator.h"
30 #include "yaffs_attribs.h"
31 #include "yaffs_summary.h"
33 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
34 #define YAFFS_GC_GOOD_ENOUGH 2
35 #define YAFFS_GC_PASSIVE_THRESHOLD 4
37 #include "yaffs_ecc.h"
39 /* Forward declarations */
41 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
42 const u8 *buffer, int n_bytes, int use_reserve);
44 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
47 /* Function to calculate chunk and offset */
49 void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
50 int *chunk_out, u32 *offset_out)
55 chunk = (u32) (addr >> dev->chunk_shift);
57 if (dev->chunk_div == 1) {
58 /* easy power of 2 case */
59 offset = (u32) (addr & dev->chunk_mask);
61 /* Non power-of-2 case */
65 chunk /= dev->chunk_div;
67 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
68 offset = (u32) (addr - chunk_base);
75 /* Function to return the number of shifts for a power of 2 greater than or
76 * equal to the given number
77 * Note we don't try to cater for all possible numbers and this does not have to
78 * be hellishly efficient.
81 static inline u32 calc_shifts_ceiling(u32 x)
86 shifts = extra_bits = 0;
101 /* Function to return the number of shifts to get a 1 in bit 0
104 static inline u32 calc_shifts(u32 x)
122 * Temporary buffer manipulations.
125 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
130 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
132 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
133 dev->temp_buffer[i].in_use = 0;
134 buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
135 dev->temp_buffer[i].buffer = buf;
138 return buf ? YAFFS_OK : YAFFS_FAIL;
141 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
146 if (dev->temp_in_use > dev->max_temp)
147 dev->max_temp = dev->temp_in_use;
149 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
150 if (dev->temp_buffer[i].in_use == 0) {
151 dev->temp_buffer[i].in_use = 1;
152 return dev->temp_buffer[i].buffer;
156 yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
158 * If we got here then we have to allocate an unmanaged one
162 dev->unmanaged_buffer_allocs++;
163 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
167 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
173 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
174 if (dev->temp_buffer[i].buffer == buffer) {
175 dev->temp_buffer[i].in_use = 0;
181 /* assume it is an unmanaged one. */
182 yaffs_trace(YAFFS_TRACE_BUFFERS,
183 "Releasing unmanaged temp buffer");
185 dev->unmanaged_buffer_deallocs++;
191 * Functions for robustisizing TODO
195 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
197 const struct yaffs_ext_tags *tags)
205 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
206 const struct yaffs_ext_tags *tags)
213 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
214 struct yaffs_block_info *bi,
215 enum yaffs_ecc_result err_type)
217 if (bi->gc_prioritise)
220 /* We need to refresh this data by gc'ing the block soon. */
221 bi->gc_prioritise = 1;
222 dev->has_pending_prioritised_gc = 1;
224 /* If it was more than just refresh request then consider retirement. */
225 if (err_type > YAFFS_ECC_RESULT_REFRESH) {
226 bi->chunk_error_strikes++;
228 if (bi->chunk_error_strikes > 3) {
229 bi->needs_retiring = 1; /* Too many stikes, so retire */
230 yaffs_trace(YAFFS_TRACE_ALWAYS,
231 "yaffs: Block struck out");
237 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
240 int flash_block = nand_chunk / dev->param.chunks_per_block;
241 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
243 yaffs_handle_chunk_error(dev, bi, YAFFS_ECC_RESULT_FIXED);
246 /* Was an actual write failure,
247 * so mark the block for retirement.*/
248 bi->needs_retiring = 1;
249 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
250 "**>> Block %d needs retiring", flash_block);
253 /* Delete the chunk */
254 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
255 yaffs_skip_rest_of_block(dev);
263 * Simple hash function. Needs to have a reasonable spread
266 static inline int yaffs_hash_fn(int n)
270 return n % YAFFS_NOBJECT_BUCKETS;
274 * Access functions to useful fake objects.
275 * Note that root might have a presence in NAND if permissions are set.
278 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
280 return dev->root_dir;
283 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
285 return dev->lost_n_found;
289 * Erased NAND checking functions
292 int yaffs_check_ff(u8 *buffer, int n_bytes)
294 /* Horrible, slow implementation */
303 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
305 int retval = YAFFS_OK;
306 u8 *data = yaffs_get_temp_buffer(dev);
307 struct yaffs_ext_tags tags;
310 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
312 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
315 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
317 yaffs_trace(YAFFS_TRACE_NANDACCESS,
318 "Chunk %d not erased", nand_chunk);
322 yaffs_release_temp_buffer(dev, data);
328 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
331 struct yaffs_ext_tags *tags)
333 int retval = YAFFS_OK;
334 struct yaffs_ext_tags temp_tags;
335 u8 *buffer = yaffs_get_temp_buffer(dev);
338 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
339 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
340 temp_tags.obj_id != tags->obj_id ||
341 temp_tags.chunk_id != tags->chunk_id ||
342 temp_tags.n_bytes != tags->n_bytes)
345 yaffs_release_temp_buffer(dev, buffer);
351 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
354 int reserved_blocks = dev->param.n_reserved_blocks;
357 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
360 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
362 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
365 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
368 struct yaffs_block_info *bi;
370 if (dev->n_erased_blocks < 1) {
371 /* Hoosterman we've got a problem.
372 * Can't get space to gc
374 yaffs_trace(YAFFS_TRACE_ERROR,
375 "yaffs tragedy: no more erased blocks");
380 /* Find an empty block. */
382 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
383 dev->alloc_block_finder++;
384 if (dev->alloc_block_finder < dev->internal_start_block
385 || dev->alloc_block_finder > dev->internal_end_block) {
386 dev->alloc_block_finder = dev->internal_start_block;
389 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
391 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
392 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
394 bi->seq_number = dev->seq_number;
395 dev->n_erased_blocks--;
396 yaffs_trace(YAFFS_TRACE_ALLOCATE,
397 "Allocated block %d, seq %d, %d left" ,
398 dev->alloc_block_finder, dev->seq_number,
399 dev->n_erased_blocks);
400 return dev->alloc_block_finder;
404 yaffs_trace(YAFFS_TRACE_ALWAYS,
405 "yaffs tragedy: no more erased blocks, but there should have been %d",
406 dev->n_erased_blocks);
411 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
412 struct yaffs_block_info **block_ptr)
415 struct yaffs_block_info *bi;
417 if (dev->alloc_block < 0) {
418 /* Get next block to allocate off */
419 dev->alloc_block = yaffs_find_alloc_block(dev);
423 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
424 /* No space unless we're allowed to use the reserve. */
428 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
429 && dev->alloc_page == 0)
430 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
432 /* Next page please.... */
433 if (dev->alloc_block >= 0) {
434 bi = yaffs_get_block_info(dev, dev->alloc_block);
436 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
439 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
443 dev->n_free_chunks--;
445 /* If the block is full set the state to full */
446 if (dev->alloc_page >= dev->param.chunks_per_block) {
447 bi->block_state = YAFFS_BLOCK_STATE_FULL;
448 dev->alloc_block = -1;
457 yaffs_trace(YAFFS_TRACE_ERROR,
458 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
463 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
467 n = dev->n_erased_blocks * dev->param.chunks_per_block;
469 if (dev->alloc_block > 0)
470 n += (dev->param.chunks_per_block - dev->alloc_page);
477 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
478 * if we don't want to write to it.
480 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
482 struct yaffs_block_info *bi;
484 if (dev->alloc_block > 0) {
485 bi = yaffs_get_block_info(dev, dev->alloc_block);
486 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
487 bi->block_state = YAFFS_BLOCK_STATE_FULL;
488 dev->alloc_block = -1;
493 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
495 struct yaffs_ext_tags *tags, int use_reserver)
501 yaffs2_checkpt_invalidate(dev);
504 struct yaffs_block_info *bi = 0;
507 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
513 /* First check this chunk is erased, if it needs
514 * checking. The checking policy (unless forced
515 * always on) is as follows:
517 * Check the first page we try to write in a block.
518 * If the check passes then we don't need to check any
519 * more. If the check fails, we check again...
520 * If the block has been erased, we don't need to check.
522 * However, if the block has been prioritised for gc,
523 * then we think there might be something odd about
524 * this block and stop using it.
526 * Rationale: We should only ever see chunks that have
527 * not been erased if there was a partially written
528 * chunk due to power loss. This checking policy should
529 * catch that case with very few checks and thus save a
530 * lot of checks that are most likely not needed.
533 * If an erase check fails or the write fails we skip the
537 /* let's give it a try */
540 if (dev->param.always_check_erased)
541 bi->skip_erased_check = 0;
543 if (!bi->skip_erased_check) {
544 erased_ok = yaffs_check_chunk_erased(dev, chunk);
545 if (erased_ok != YAFFS_OK) {
546 yaffs_trace(YAFFS_TRACE_ERROR,
547 "**>> yaffs chunk %d was not erased",
550 /* If not erased, delete this one,
551 * skip rest of block and
552 * try another chunk */
553 yaffs_chunk_del(dev, chunk, 1, __LINE__);
554 yaffs_skip_rest_of_block(dev);
559 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
561 if (!bi->skip_erased_check)
563 yaffs_verify_chunk_written(dev, chunk, data, tags);
565 if (write_ok != YAFFS_OK) {
566 /* Clean up aborted write, skip to next block and
567 * try another chunk */
568 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
572 bi->skip_erased_check = 1;
574 /* Copy the data into the robustification buffer */
575 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
577 } while (write_ok != YAFFS_OK &&
578 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
584 yaffs_trace(YAFFS_TRACE_ERROR,
585 "**>> yaffs write required %d attempts",
587 dev->n_retried_writes += (attempts - 1);
594 * Block retiring for handling a broken block.
597 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
599 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
601 yaffs2_checkpt_invalidate(dev);
603 yaffs2_clear_oldest_dirty_seq(dev, bi);
605 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
606 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
607 yaffs_trace(YAFFS_TRACE_ALWAYS,
608 "yaffs: Failed to mark bad and erase block %d",
611 struct yaffs_ext_tags tags;
613 flash_block * dev->param.chunks_per_block;
615 u8 *buffer = yaffs_get_temp_buffer(dev);
617 memset(buffer, 0xff, dev->data_bytes_per_chunk);
618 memset(&tags, 0, sizeof(tags));
619 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
620 if (dev->tagger.write_chunk_tags_fn(dev, chunk_id -
624 yaffs_trace(YAFFS_TRACE_ALWAYS,
625 "yaffs: Failed to write bad block marker to block %d",
628 yaffs_release_temp_buffer(dev, buffer);
632 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
633 bi->gc_prioritise = 0;
634 bi->needs_retiring = 0;
636 dev->n_retired_blocks++;
639 /*---------------- Name handling functions ------------*/
641 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
642 const YCHAR *oh_name, int buff_size)
644 #ifdef CONFIG_YAFFS_AUTO_UNICODE
645 if (dev->param.auto_unicode) {
647 /* It is an ASCII name, do an ASCII to
648 * unicode conversion */
649 const char *ascii_oh_name = (const char *)oh_name;
650 int n = buff_size - 1;
651 while (n > 0 && *ascii_oh_name) {
652 *name = *ascii_oh_name;
658 strncpy(name, oh_name + 1, buff_size - 1);
665 strncpy(name, oh_name, buff_size - 1);
669 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
672 #ifdef CONFIG_YAFFS_AUTO_UNICODE
677 if (dev->param.auto_unicode) {
682 /* Figure out if the name will fit in ascii character set */
683 while (is_ascii && *w) {
690 /* It is an ASCII name, so convert unicode to ascii */
691 char *ascii_oh_name = (char *)oh_name;
692 int n = YAFFS_MAX_NAME_LENGTH - 1;
693 while (n > 0 && *name) {
694 *ascii_oh_name = (char)*name;
700 /* Unicode name, so save starting at the second YCHAR */
702 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
709 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
713 static u16 yaffs_calc_name_sum(const YCHAR *name)
721 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
723 /* 0x1f mask is case insensitive */
724 sum += ((*name) & 0x1f) * i;
732 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
734 memset(obj->short_name, 0, sizeof(obj->short_name));
736 if (name && !name[0]) {
737 yaffs_fix_null_name(obj, obj->short_name,
738 YAFFS_SHORT_NAME_LENGTH);
739 name = obj->short_name;
741 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
742 YAFFS_SHORT_NAME_LENGTH) {
743 strcpy(obj->short_name, name);
746 obj->sum = yaffs_calc_name_sum(name);
749 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
750 const struct yaffs_obj_hdr *oh)
752 #ifdef CONFIG_YAFFS_AUTO_UNICODE
753 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
754 memset(tmp_name, 0, sizeof(tmp_name));
755 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
756 YAFFS_MAX_NAME_LENGTH + 1);
757 yaffs_set_obj_name(obj, tmp_name);
759 yaffs_set_obj_name(obj, oh->name);
763 loff_t yaffs_max_file_size(struct yaffs_dev *dev)
765 if(sizeof(loff_t) < 8)
766 return YAFFS_MAX_FILE_SIZE_32;
768 return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
771 /*-------------------- TNODES -------------------
773 * List of spare tnodes
774 * The list is hooked together using the first pointer
778 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
780 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
783 memset(tn, 0, dev->tnode_size);
787 dev->checkpoint_blocks_required = 0; /* force recalculation */
792 /* FreeTnode frees up a tnode and puts it back on the free list */
793 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
795 yaffs_free_raw_tnode(dev, tn);
797 dev->checkpoint_blocks_required = 0; /* force recalculation */
800 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
802 yaffs_deinit_raw_tnodes_and_objs(dev);
807 static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
808 unsigned pos, unsigned val)
810 u32 *map = (u32 *) tn;
816 pos &= YAFFS_TNODES_LEVEL0_MASK;
817 val >>= dev->chunk_grp_bits;
819 bit_in_map = pos * dev->tnode_width;
820 word_in_map = bit_in_map / 32;
821 bit_in_word = bit_in_map & (32 - 1);
823 mask = dev->tnode_mask << bit_in_word;
825 map[word_in_map] &= ~mask;
826 map[word_in_map] |= (mask & (val << bit_in_word));
828 if (dev->tnode_width > (32 - bit_in_word)) {
829 bit_in_word = (32 - bit_in_word);
832 dev->tnode_mask >> bit_in_word;
833 map[word_in_map] &= ~mask;
834 map[word_in_map] |= (mask & (val >> bit_in_word));
838 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
841 u32 *map = (u32 *) tn;
847 pos &= YAFFS_TNODES_LEVEL0_MASK;
849 bit_in_map = pos * dev->tnode_width;
850 word_in_map = bit_in_map / 32;
851 bit_in_word = bit_in_map & (32 - 1);
853 val = map[word_in_map] >> bit_in_word;
855 if (dev->tnode_width > (32 - bit_in_word)) {
856 bit_in_word = (32 - bit_in_word);
858 val |= (map[word_in_map] << bit_in_word);
861 val &= dev->tnode_mask;
862 val <<= dev->chunk_grp_bits;
867 /* ------------------- End of individual tnode manipulation -----------------*/
869 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
870 * The look up tree is represented by the top tnode and the number of top_level
871 * in the tree. 0 means only the level 0 tnode is in the tree.
874 /* yaffs_find_tnode_0 finds the level 0 tnode, if one exists. */
875 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
876 struct yaffs_file_var *file_struct,
879 struct yaffs_tnode *tn = file_struct->top;
882 int level = file_struct->top_level;
886 /* Check sane level and chunk Id */
887 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
890 if (chunk_id > YAFFS_MAX_CHUNK_ID)
893 /* First check we're tall enough (ie enough top_level) */
895 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
898 i >>= YAFFS_TNODES_INTERNAL_BITS;
902 if (required_depth > file_struct->top_level)
903 return NULL; /* Not tall enough, so we can't find it */
905 /* Traverse down to level 0 */
906 while (level > 0 && tn) {
907 tn = tn->internal[(chunk_id >>
908 (YAFFS_TNODES_LEVEL0_BITS +
910 YAFFS_TNODES_INTERNAL_BITS)) &
911 YAFFS_TNODES_INTERNAL_MASK];
918 /* add_find_tnode_0 finds the level 0 tnode if it exists,
919 * otherwise first expands the tree.
920 * This happens in two steps:
921 * 1. If the tree isn't tall enough, then make it taller.
922 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
924 * Used when modifying the tree.
926 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
927 * specified tn will be plugged into the ttree.
930 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
931 struct yaffs_file_var *file_struct,
933 struct yaffs_tnode *passed_tn)
938 struct yaffs_tnode *tn;
941 /* Check sane level and page Id */
942 if (file_struct->top_level < 0 ||
943 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
946 if (chunk_id > YAFFS_MAX_CHUNK_ID)
949 /* First check we're tall enough (ie enough top_level) */
951 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
954 x >>= YAFFS_TNODES_INTERNAL_BITS;
958 if (required_depth > file_struct->top_level) {
959 /* Not tall enough, gotta make the tree taller */
960 for (i = file_struct->top_level; i < required_depth; i++) {
962 tn = yaffs_get_tnode(dev);
965 tn->internal[0] = file_struct->top;
966 file_struct->top = tn;
967 file_struct->top_level++;
969 yaffs_trace(YAFFS_TRACE_ERROR,
970 "yaffs: no more tnodes");
976 /* Traverse down to level 0, adding anything we need */
978 l = file_struct->top_level;
979 tn = file_struct->top;
982 while (l > 0 && tn) {
984 (YAFFS_TNODES_LEVEL0_BITS +
985 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
986 YAFFS_TNODES_INTERNAL_MASK;
988 if ((l > 1) && !tn->internal[x]) {
989 /* Add missing non-level-zero tnode */
990 tn->internal[x] = yaffs_get_tnode(dev);
991 if (!tn->internal[x])
994 /* Looking from level 1 at level 0 */
996 /* If we already have one, release it */
998 yaffs_free_tnode(dev,
1000 tn->internal[x] = passed_tn;
1002 } else if (!tn->internal[x]) {
1003 /* Don't have one, none passed in */
1004 tn->internal[x] = yaffs_get_tnode(dev);
1005 if (!tn->internal[x])
1010 tn = tn->internal[x];
1014 /* top is level 0 */
1016 memcpy(tn, passed_tn,
1017 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
1018 yaffs_free_tnode(dev, passed_tn);
1025 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
1028 return (tags->chunk_id == chunk_obj &&
1029 tags->obj_id == obj_id &&
1030 !tags->is_deleted) ? 1 : 0;
1034 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
1035 struct yaffs_ext_tags *tags, int obj_id,
1040 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
1041 if (yaffs_check_chunk_bit
1042 (dev, the_chunk / dev->param.chunks_per_block,
1043 the_chunk % dev->param.chunks_per_block)) {
1045 if (dev->chunk_grp_size == 1)
1048 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
1050 if (yaffs_tags_match(tags,
1051 obj_id, inode_chunk)) {
1062 int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1063 struct yaffs_ext_tags *tags)
1065 /*Get the Tnode, then get the level 0 offset chunk offset */
1066 struct yaffs_tnode *tn;
1068 struct yaffs_ext_tags local_tags;
1070 struct yaffs_dev *dev = in->my_dev;
1073 /* Passed a NULL, so use our own tags space */
1077 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1082 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1084 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1089 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1090 struct yaffs_ext_tags *tags)
1092 /* Get the Tnode, then get the level 0 offset chunk offset */
1093 struct yaffs_tnode *tn;
1095 struct yaffs_ext_tags local_tags;
1096 struct yaffs_dev *dev = in->my_dev;
1100 /* Passed a NULL, so use our own tags space */
1104 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1109 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1111 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1114 /* Delete the entry in the filestructure (if found) */
1116 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1121 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1122 int nand_chunk, int in_scan)
1124 /* NB in_scan is zero unless scanning.
1125 * For forward scanning, in_scan is > 0;
1126 * for backward scanning in_scan is < 0
1128 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1131 struct yaffs_tnode *tn;
1132 struct yaffs_dev *dev = in->my_dev;
1134 struct yaffs_ext_tags existing_tags;
1135 struct yaffs_ext_tags new_tags;
1136 unsigned existing_serial, new_serial;
1138 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1139 /* Just ignore an attempt at putting a chunk into a non-file
1141 * If it is not during Scanning then something went wrong!
1144 yaffs_trace(YAFFS_TRACE_ERROR,
1145 "yaffs tragedy:attempt to put data chunk into a non-file"
1150 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1154 tn = yaffs_add_find_tnode_0(dev,
1155 &in->variant.file_variant,
1161 /* Dummy insert, bail now */
1164 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1167 /* If we're scanning then we need to test for duplicates
1168 * NB This does not need to be efficient since it should only
1169 * happen when the power fails during a write, then only one
1170 * chunk should ever be affected.
1172 * Correction for YAFFS2: This could happen quite a lot and we
1173 * need to think about efficiency! TODO
1174 * Update: For backward scanning we don't need to re-read tags
1175 * so this is quite cheap.
1178 if (existing_cunk > 0) {
1179 /* NB Right now existing chunk will not be real
1180 * chunk_id if the chunk group size > 1
1181 * thus we have to do a FindChunkInFile to get the
1184 * We have a duplicate now we need to decide which
1187 * Backwards scanning YAFFS2: The old one is what
1188 * we use, dump the new one.
1189 * YAFFS1: Get both sets of tags and compare serial
1194 /* Only do this for forward scanning */
1195 yaffs_rd_chunk_tags_nand(dev,
1199 /* Do a proper find */
1201 yaffs_find_chunk_in_file(in, inode_chunk,
1205 if (existing_cunk <= 0) {
1206 /*Hoosterman - how did this happen? */
1208 yaffs_trace(YAFFS_TRACE_ERROR,
1209 "yaffs tragedy: existing chunk < 0 in scan"
1214 /* NB The deleted flags should be false, otherwise
1215 * the chunks will not be loaded during a scan
1219 new_serial = new_tags.serial_number;
1220 existing_serial = existing_tags.serial_number;
1223 if ((in_scan > 0) &&
1224 (existing_cunk <= 0 ||
1225 ((existing_serial + 1) & 3) == new_serial)) {
1226 /* Forward scanning.
1228 * Delete the old one and drop through to
1231 yaffs_chunk_del(dev, existing_cunk, 1,
1234 /* Backward scanning or we want to use the
1236 * Delete the new one and return early so that
1237 * the tnode isn't changed
1239 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1246 if (existing_cunk == 0)
1247 in->n_data_chunks++;
1249 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1254 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1256 struct yaffs_block_info *the_block;
1259 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1261 block_no = chunk / dev->param.chunks_per_block;
1262 the_block = yaffs_get_block_info(dev, block_no);
1264 the_block->soft_del_pages++;
1265 dev->n_free_chunks++;
1266 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1270 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1271 * the chunks in the file.
1272 * All soft deleting does is increment the block's softdelete count and pulls
1273 * the chunk out of the tnode.
1274 * Thus, essentially this is the same as DeleteWorker except that the chunks
1278 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1279 u32 level, int chunk_offset)
1284 struct yaffs_dev *dev = in->my_dev;
1290 for (i = YAFFS_NTNODES_INTERNAL - 1;
1293 if (tn->internal[i]) {
1295 yaffs_soft_del_worker(in,
1299 YAFFS_TNODES_INTERNAL_BITS)
1302 yaffs_free_tnode(dev,
1304 tn->internal[i] = NULL;
1306 /* Can this happen? */
1310 return (all_done) ? 1 : 0;
1314 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1315 the_chunk = yaffs_get_group_base(dev, tn, i);
1317 yaffs_soft_del_chunk(dev, the_chunk);
1318 yaffs_load_tnode_0(dev, tn, i, 0);
1324 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1326 struct yaffs_dev *dev = obj->my_dev;
1327 struct yaffs_obj *parent;
1329 yaffs_verify_obj_in_dir(obj);
1330 parent = obj->parent;
1332 yaffs_verify_dir(parent);
1334 if (dev && dev->param.remove_obj_fn)
1335 dev->param.remove_obj_fn(obj);
1337 list_del_init(&obj->siblings);
1340 yaffs_verify_dir(parent);
1343 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1346 yaffs_trace(YAFFS_TRACE_ALWAYS,
1347 "tragedy: Trying to add an object to a null pointer directory"
1352 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1353 yaffs_trace(YAFFS_TRACE_ALWAYS,
1354 "tragedy: Trying to add an object to a non-directory"
1359 if (obj->siblings.prev == NULL) {
1360 /* Not initialised */
1364 yaffs_verify_dir(directory);
1366 yaffs_remove_obj_from_dir(obj);
1369 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1370 obj->parent = directory;
1372 if (directory == obj->my_dev->unlinked_dir
1373 || directory == obj->my_dev->del_dir) {
1375 obj->my_dev->n_unlinked_files++;
1376 obj->rename_allowed = 0;
1379 yaffs_verify_dir(directory);
1380 yaffs_verify_obj_in_dir(obj);
1383 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1384 struct yaffs_obj *new_dir,
1385 const YCHAR *new_name, int force, int shadows)
1389 struct yaffs_obj *existing_target;
1391 if (new_dir == NULL)
1392 new_dir = obj->parent; /* use the old directory */
1394 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1395 yaffs_trace(YAFFS_TRACE_ALWAYS,
1396 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1401 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1402 del_op = (new_dir == obj->my_dev->del_dir);
1404 existing_target = yaffs_find_by_name(new_dir, new_name);
1406 /* If the object is a file going into the unlinked directory,
1407 * then it is OK to just stuff it in since duplicate names are OK.
1408 * else only proceed if the new name does not exist and we're putting
1409 * it into a directory.
1411 if (!(unlink_op || del_op || force ||
1412 shadows > 0 || !existing_target) ||
1413 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1416 yaffs_set_obj_name(obj, new_name);
1418 yaffs_add_obj_to_dir(new_dir, obj);
1423 /* If it is a deletion then we mark it as a shrink for gc */
1424 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1430 /*------------------------ Short Operations Cache ------------------------------
1431 * In many situations where there is no high level buffering a lot of
1432 * reads might be short sequential reads, and a lot of writes may be short
1433 * sequential writes. eg. scanning/writing a jpeg file.
1434 * In these cases, a short read/write cache can provide a huge perfomance
1435 * benefit with dumb-as-a-rock code.
1436 * In Linux, the page cache provides read buffering and the short op cache
1437 * provides write buffering.
1439 * There are a small number (~10) of cache chunks per device so that we don't
1440 * need a very intelligent search.
1443 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1445 struct yaffs_dev *dev = obj->my_dev;
1447 struct yaffs_cache *cache;
1448 int n_caches = obj->my_dev->param.n_caches;
1450 for (i = 0; i < n_caches; i++) {
1451 cache = &dev->cache[i];
1452 if (cache->object == obj && cache->dirty)
1459 static void yaffs_flush_single_cache(struct yaffs_cache *cache, int discard)
1462 if (!cache || cache->locked)
1465 /* Write it out and free it up if need be.*/
1467 yaffs_wr_data_obj(cache->object,
1477 cache->object = NULL;
1480 static void yaffs_flush_file_cache(struct yaffs_obj *obj, int discard)
1482 struct yaffs_dev *dev = obj->my_dev;
1484 struct yaffs_cache *cache;
1485 int n_caches = obj->my_dev->param.n_caches;
1491 /* Find the chunks for this object and flush them. */
1492 for (i = 0; i < n_caches; i++) {
1493 cache = &dev->cache[i];
1494 if (cache->object == obj)
1495 yaffs_flush_single_cache(cache, discard);
1501 void yaffs_flush_whole_cache(struct yaffs_dev *dev, int discard)
1503 struct yaffs_obj *obj;
1504 int n_caches = dev->param.n_caches;
1507 /* Find a dirty object in the cache and flush it...
1508 * until there are no further dirty objects.
1512 for (i = 0; i < n_caches && !obj; i++) {
1513 if (dev->cache[i].object && dev->cache[i].dirty)
1514 obj = dev->cache[i].object;
1517 yaffs_flush_file_cache(obj, discard);
1522 /* Grab us an unused cache chunk for use.
1523 * First look for an empty one.
1524 * Then look for the least recently used non-dirty one.
1525 * Then look for the least recently used dirty one...., flush and look again.
1527 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1531 if (dev->param.n_caches > 0) {
1532 for (i = 0; i < dev->param.n_caches; i++) {
1533 if (!dev->cache[i].object)
1534 return &dev->cache[i];
1541 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1543 struct yaffs_cache *cache;
1547 if (dev->param.n_caches < 1)
1550 /* First look for an unused cache */
1552 cache = yaffs_grab_chunk_worker(dev);
1558 * Thery were all in use.
1559 * Find the LRU cache and flush it if it is dirty.
1565 for (i = 0; i < dev->param.n_caches; i++) {
1566 if (dev->cache[i].object &&
1567 !dev->cache[i].locked &&
1568 (dev->cache[i].last_use < usage || !cache)) {
1569 usage = dev->cache[i].last_use;
1570 cache = &dev->cache[i];
1575 yaffs_flush_single_cache(cache, 1);
1577 yaffs_flush_file_cache(cache->object, 1);
1578 cache = yaffs_grab_chunk_worker(dev);
1584 /* Find a cached chunk */
1585 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1588 struct yaffs_dev *dev = obj->my_dev;
1591 if (dev->param.n_caches < 1)
1594 for (i = 0; i < dev->param.n_caches; i++) {
1595 if (dev->cache[i].object == obj &&
1596 dev->cache[i].chunk_id == chunk_id) {
1599 return &dev->cache[i];
1605 /* Mark the chunk for the least recently used algorithym */
1606 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1611 if (dev->param.n_caches < 1)
1614 if (dev->cache_last_use < 0 ||
1615 dev->cache_last_use > 100000000) {
1616 /* Reset the cache usages */
1617 for (i = 1; i < dev->param.n_caches; i++)
1618 dev->cache[i].last_use = 0;
1620 dev->cache_last_use = 0;
1622 dev->cache_last_use++;
1623 cache->last_use = dev->cache_last_use;
1629 /* Invalidate a single cache page.
1630 * Do this when a whole page gets written,
1631 * ie the short cache for this page is no longer valid.
1633 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1635 struct yaffs_cache *cache;
1637 if (object->my_dev->param.n_caches > 0) {
1638 cache = yaffs_find_chunk_cache(object, chunk_id);
1641 cache->object = NULL;
1645 /* Invalidate all the cache pages associated with this object
1646 * Do this whenever ther file is deleted or resized.
1648 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1651 struct yaffs_dev *dev = in->my_dev;
1653 if (dev->param.n_caches > 0) {
1654 /* Invalidate it. */
1655 for (i = 0; i < dev->param.n_caches; i++) {
1656 if (dev->cache[i].object == in)
1657 dev->cache[i].object = NULL;
1662 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1665 struct yaffs_dev *dev = obj->my_dev;
1667 /* If it is still linked into the bucket list, free from the list */
1668 if (!list_empty(&obj->hash_link)) {
1669 list_del_init(&obj->hash_link);
1670 bucket = yaffs_hash_fn(obj->obj_id);
1671 dev->obj_bucket[bucket].count--;
1675 /* FreeObject frees up a Object and puts it back on the free list */
1676 static void yaffs_free_obj(struct yaffs_obj *obj)
1678 struct yaffs_dev *dev;
1685 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1686 obj, obj->my_inode);
1689 if (!list_empty(&obj->siblings))
1692 if (obj->my_inode) {
1693 /* We're still hooked up to a cached inode.
1694 * Don't delete now, but mark for later deletion
1696 obj->defered_free = 1;
1700 yaffs_unhash_obj(obj);
1702 yaffs_free_raw_obj(dev, obj);
1704 dev->checkpoint_blocks_required = 0; /* force recalculation */
1707 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1709 if (obj->defered_free)
1710 yaffs_free_obj(obj);
1713 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1715 /* Iinvalidate the file's data in the cache, without flushing. */
1716 yaffs_invalidate_whole_cache(in);
1718 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1719 /* Move to unlinked directory so we have a deletion record */
1720 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1724 yaffs_remove_obj_from_dir(in);
1725 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1733 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1735 if (!obj->deleted ||
1736 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1740 if (obj->n_data_chunks <= 0) {
1741 /* Empty file with no duplicate object headers,
1742 * just delete it immediately */
1743 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1744 obj->variant.file_variant.top = NULL;
1745 yaffs_trace(YAFFS_TRACE_TRACING,
1746 "yaffs: Deleting empty file %d",
1748 yaffs_generic_obj_del(obj);
1750 yaffs_soft_del_worker(obj,
1751 obj->variant.file_variant.top,
1753 file_variant.top_level, 0);
1758 /* Pruning removes any part of the file structure tree that is beyond the
1759 * bounds of the file (ie that does not point to chunks).
1761 * A file should only get pruned when its size is reduced.
1763 * Before pruning, the chunks must be pulled from the tree and the
1764 * level 0 tnode entries must be zeroed out.
1765 * Could also use this for file deletion, but that's probably better handled
1766 * by a special case.
1768 * This function is recursive. For levels > 0 the function is called again on
1769 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1770 * If there is no data in a subtree then it is pruned.
1773 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1774 struct yaffs_tnode *tn, u32 level,
1786 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1787 if (tn->internal[i]) {
1789 yaffs_prune_worker(dev,
1792 (i == 0) ? del0 : 1);
1795 if (tn->internal[i])
1799 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1800 u32 *map = (u32 *) tn;
1802 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1808 if (has_data == 0 && del0) {
1809 /* Free and return NULL */
1810 yaffs_free_tnode(dev, tn);
1816 static int yaffs_prune_tree(struct yaffs_dev *dev,
1817 struct yaffs_file_var *file_struct)
1822 struct yaffs_tnode *tn;
1824 if (file_struct->top_level < 1)
1828 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1830 /* Now we have a tree with all the non-zero branches NULL but
1831 * the height is the same as it was.
1832 * Let's see if we can trim internal tnodes to shorten the tree.
1833 * We can do this if only the 0th element in the tnode is in use
1834 * (ie all the non-zero are NULL)
1837 while (file_struct->top_level && !done) {
1838 tn = file_struct->top;
1841 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1842 if (tn->internal[i])
1847 file_struct->top = tn->internal[0];
1848 file_struct->top_level--;
1849 yaffs_free_tnode(dev, tn);
1858 /*-------------------- End of File Structure functions.-------------------*/
1860 /* alloc_empty_obj gets us a clean Object.*/
1861 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1863 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1870 /* Now sweeten it up... */
1872 memset(obj, 0, sizeof(struct yaffs_obj));
1873 obj->being_created = 1;
1877 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1878 INIT_LIST_HEAD(&(obj->hard_links));
1879 INIT_LIST_HEAD(&(obj->hash_link));
1880 INIT_LIST_HEAD(&obj->siblings);
1882 /* Now make the directory sane */
1883 if (dev->root_dir) {
1884 obj->parent = dev->root_dir;
1885 list_add(&(obj->siblings),
1886 &dev->root_dir->variant.dir_variant.children);
1889 /* Add it to the lost and found directory.
1890 * NB Can't put root or lost-n-found in lost-n-found so
1891 * check if lost-n-found exists first
1893 if (dev->lost_n_found)
1894 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1896 obj->being_created = 0;
1898 dev->checkpoint_blocks_required = 0; /* force recalculation */
1903 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1907 int lowest = 999999;
1909 /* Search for the shortest list or one that
1913 for (i = 0; i < 10 && lowest > 4; i++) {
1914 dev->bucket_finder++;
1915 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1916 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1917 lowest = dev->obj_bucket[dev->bucket_finder].count;
1918 l = dev->bucket_finder;
1925 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1927 int bucket = yaffs_find_nice_bucket(dev);
1929 struct list_head *i;
1930 u32 n = (u32) bucket;
1932 /* Now find an object value that has not already been taken
1933 * by scanning the list.
1938 n += YAFFS_NOBJECT_BUCKETS;
1939 if (1 || dev->obj_bucket[bucket].count > 0) {
1940 list_for_each(i, &dev->obj_bucket[bucket].list) {
1941 /* If there is already one in the list */
1942 if (i && list_entry(i, struct yaffs_obj,
1943 hash_link)->obj_id == n) {
1952 static void yaffs_hash_obj(struct yaffs_obj *in)
1954 int bucket = yaffs_hash_fn(in->obj_id);
1955 struct yaffs_dev *dev = in->my_dev;
1957 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1958 dev->obj_bucket[bucket].count++;
1961 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1963 int bucket = yaffs_hash_fn(number);
1964 struct list_head *i;
1965 struct yaffs_obj *in;
1967 list_for_each(i, &dev->obj_bucket[bucket].list) {
1968 /* Look if it is in the list */
1969 in = list_entry(i, struct yaffs_obj, hash_link);
1970 if (in->obj_id == number) {
1971 /* Don't show if it is defered free */
1972 if (in->defered_free)
1981 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1982 enum yaffs_obj_type type)
1984 struct yaffs_obj *the_obj = NULL;
1985 struct yaffs_tnode *tn = NULL;
1988 number = yaffs_new_obj_id(dev);
1990 if (type == YAFFS_OBJECT_TYPE_FILE) {
1991 tn = yaffs_get_tnode(dev);
1996 the_obj = yaffs_alloc_empty_obj(dev);
1999 yaffs_free_tnode(dev, tn);
2004 the_obj->rename_allowed = 1;
2005 the_obj->unlink_allowed = 1;
2006 the_obj->obj_id = number;
2007 yaffs_hash_obj(the_obj);
2008 the_obj->variant_type = type;
2009 yaffs_load_current_time(the_obj, 1, 1);
2012 case YAFFS_OBJECT_TYPE_FILE:
2013 the_obj->variant.file_variant.file_size = 0;
2014 the_obj->variant.file_variant.scanned_size = 0;
2015 the_obj->variant.file_variant.shrink_size =
2016 yaffs_max_file_size(dev);
2017 the_obj->variant.file_variant.top_level = 0;
2018 the_obj->variant.file_variant.top = tn;
2020 case YAFFS_OBJECT_TYPE_DIRECTORY:
2021 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
2022 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
2024 case YAFFS_OBJECT_TYPE_SYMLINK:
2025 case YAFFS_OBJECT_TYPE_HARDLINK:
2026 case YAFFS_OBJECT_TYPE_SPECIAL:
2027 /* No action required */
2029 case YAFFS_OBJECT_TYPE_UNKNOWN:
2030 /* todo this should not happen */
2036 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2037 int number, u32 mode)
2040 struct yaffs_obj *obj =
2041 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2046 obj->fake = 1; /* it is fake so it might not use NAND */
2047 obj->rename_allowed = 0;
2048 obj->unlink_allowed = 0;
2051 obj->yst_mode = mode;
2053 obj->hdr_chunk = 0; /* Not a valid chunk. */
2059 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2065 yaffs_init_raw_tnodes_and_objs(dev);
2067 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2068 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2069 dev->obj_bucket[i].count = 0;
2073 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2075 enum yaffs_obj_type type)
2077 struct yaffs_obj *the_obj = NULL;
2080 the_obj = yaffs_find_by_number(dev, number);
2083 the_obj = yaffs_new_obj(dev, number, type);
2089 YCHAR *yaffs_clone_str(const YCHAR *str)
2091 YCHAR *new_str = NULL;
2097 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2098 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2100 strncpy(new_str, str, len);
2107 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2108 * link (ie. name) is created or deleted in the directory.
2111 * create dir/a : update dir's mtime/ctime
2112 * rm dir/a: update dir's mtime/ctime
2113 * modify dir/a: don't update dir's mtimme/ctime
2115 * This can be handled immediately or defered. Defering helps reduce the number
2116 * of updates when many files in a directory are changed within a brief period.
2118 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2119 * called periodically.
2122 static void yaffs_update_parent(struct yaffs_obj *obj)
2124 struct yaffs_dev *dev;
2130 yaffs_load_current_time(obj, 0, 1);
2131 if (dev->param.defered_dir_update) {
2132 struct list_head *link = &obj->variant.dir_variant.dirty;
2134 if (list_empty(link)) {
2135 list_add(link, &dev->dirty_dirs);
2136 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2137 "Added object %d to dirty directories",
2142 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2146 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2148 struct list_head *link;
2149 struct yaffs_obj *obj;
2150 struct yaffs_dir_var *d_s;
2151 union yaffs_obj_var *o_v;
2153 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2155 while (!list_empty(&dev->dirty_dirs)) {
2156 link = dev->dirty_dirs.next;
2157 list_del_init(link);
2159 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2160 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2161 obj = list_entry(o_v, struct yaffs_obj, variant);
2163 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2167 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2172 * Mknod (create) a new object.
2173 * equiv_obj only has meaning for a hard link;
2174 * alias_str only has meaning for a symlink.
2175 * rdev only has meaning for devices (a subset of special objects)
2178 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2179 struct yaffs_obj *parent,
2184 struct yaffs_obj *equiv_obj,
2185 const YCHAR *alias_str, u32 rdev)
2187 struct yaffs_obj *in;
2189 struct yaffs_dev *dev = parent->my_dev;
2191 /* Check if the entry exists.
2192 * If it does then fail the call since we don't want a dup. */
2193 if (yaffs_find_by_name(parent, name))
2196 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2197 str = yaffs_clone_str(alias_str);
2202 in = yaffs_new_obj(dev, -1, type);
2211 in->variant_type = type;
2213 in->yst_mode = mode;
2215 yaffs_attribs_init(in, gid, uid, rdev);
2217 in->n_data_chunks = 0;
2219 yaffs_set_obj_name(in, name);
2222 yaffs_add_obj_to_dir(parent, in);
2224 in->my_dev = parent->my_dev;
2227 case YAFFS_OBJECT_TYPE_SYMLINK:
2228 in->variant.symlink_variant.alias = str;
2230 case YAFFS_OBJECT_TYPE_HARDLINK:
2231 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2232 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
2233 list_add(&in->hard_links, &equiv_obj->hard_links);
2235 case YAFFS_OBJECT_TYPE_FILE:
2236 case YAFFS_OBJECT_TYPE_DIRECTORY:
2237 case YAFFS_OBJECT_TYPE_SPECIAL:
2238 case YAFFS_OBJECT_TYPE_UNKNOWN:
2243 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2244 /* Could not create the object header, fail */
2250 yaffs_update_parent(parent);
2255 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2256 const YCHAR *name, u32 mode, u32 uid,
2259 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2260 uid, gid, NULL, NULL, 0);
2263 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2264 u32 mode, u32 uid, u32 gid)
2266 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2267 mode, uid, gid, NULL, NULL, 0);
2270 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2271 const YCHAR *name, u32 mode, u32 uid,
2274 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2275 uid, gid, NULL, NULL, rdev);
2278 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2279 const YCHAR *name, u32 mode, u32 uid,
2280 u32 gid, const YCHAR *alias)
2282 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2283 uid, gid, NULL, alias, 0);
2286 /* yaffs_link_obj returns the object id of the equivalent object.*/
2287 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2288 struct yaffs_obj *equiv_obj)
2290 /* Get the real object in case we were fed a hard link obj */
2291 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2293 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2294 parent, name, 0, 0, 0,
2295 equiv_obj, NULL, 0))
2304 /*---------------------- Block Management and Page Allocation -------------*/
2306 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2308 if (dev->block_info_alt && dev->block_info)
2309 vfree(dev->block_info);
2311 kfree(dev->block_info);
2313 dev->block_info_alt = 0;
2315 dev->block_info = NULL;
2317 if (dev->chunk_bits_alt && dev->chunk_bits)
2318 vfree(dev->chunk_bits);
2320 kfree(dev->chunk_bits);
2321 dev->chunk_bits_alt = 0;
2322 dev->chunk_bits = NULL;
2325 static int yaffs_init_blocks(struct yaffs_dev *dev)
2327 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2329 dev->block_info = NULL;
2330 dev->chunk_bits = NULL;
2331 dev->alloc_block = -1; /* force it to get a new one */
2333 /* If the first allocation strategy fails, thry the alternate one */
2335 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2336 if (!dev->block_info) {
2338 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2339 dev->block_info_alt = 1;
2341 dev->block_info_alt = 0;
2344 if (!dev->block_info)
2347 /* Set up dynamic blockinfo stuff. Round up bytes. */
2348 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2350 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2351 if (!dev->chunk_bits) {
2353 vmalloc(dev->chunk_bit_stride * n_blocks);
2354 dev->chunk_bits_alt = 1;
2356 dev->chunk_bits_alt = 0;
2358 if (!dev->chunk_bits)
2362 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2363 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2367 yaffs_deinit_blocks(dev);
2372 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2374 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2378 /* If the block is still healthy erase it and mark as clean.
2379 * If the block has had a data failure, then retire it.
2382 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2383 "yaffs_block_became_dirty block %d state %d %s",
2384 block_no, bi->block_state,
2385 (bi->needs_retiring) ? "needs retiring" : "");
2387 yaffs2_clear_oldest_dirty_seq(dev, bi);
2389 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2391 /* If this is the block being garbage collected then stop gc'ing */
2392 if (block_no == dev->gc_block)
2395 /* If this block is currently the best candidate for gc
2396 * then drop as a candidate */
2397 if (block_no == dev->gc_dirtiest) {
2398 dev->gc_dirtiest = 0;
2399 dev->gc_pages_in_use = 0;
2402 if (!bi->needs_retiring) {
2403 yaffs2_checkpt_invalidate(dev);
2404 erased_ok = yaffs_erase_block(dev, block_no);
2406 dev->n_erase_failures++;
2407 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2408 "**>> Erasure failed %d", block_no);
2412 /* Verify erasure if needed */
2414 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2415 !yaffs_skip_verification(dev))) {
2416 for (i = 0; i < dev->param.chunks_per_block; i++) {
2417 if (!yaffs_check_chunk_erased(dev,
2418 block_no * dev->param.chunks_per_block + i)) {
2419 yaffs_trace(YAFFS_TRACE_ERROR,
2420 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2427 /* We lost a block of free space */
2428 dev->n_free_chunks -= dev->param.chunks_per_block;
2429 yaffs_retire_block(dev, block_no);
2430 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2431 "**>> Block %d retired", block_no);
2435 /* Clean it up... */
2436 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2438 dev->n_erased_blocks++;
2439 bi->pages_in_use = 0;
2440 bi->soft_del_pages = 0;
2441 bi->has_shrink_hdr = 0;
2442 bi->skip_erased_check = 1; /* Clean, so no need to check */
2443 bi->gc_prioritise = 0;
2444 bi->has_summary = 0;
2446 yaffs_clear_chunk_bits(dev, block_no);
2448 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2451 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2452 struct yaffs_block_info *bi,
2453 int old_chunk, u8 *buffer)
2457 struct yaffs_ext_tags tags;
2458 struct yaffs_obj *object;
2460 int ret_val = YAFFS_OK;
2462 memset(&tags, 0, sizeof(tags));
2463 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2465 object = yaffs_find_by_number(dev, tags.obj_id);
2467 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2468 "Collecting chunk in block %d, %d %d %d ",
2469 dev->gc_chunk, tags.obj_id,
2470 tags.chunk_id, tags.n_bytes);
2472 if (object && !yaffs_skip_verification(dev)) {
2473 if (tags.chunk_id == 0)
2476 else if (object->soft_del)
2477 /* Defeat the test */
2478 matching_chunk = old_chunk;
2481 yaffs_find_chunk_in_file
2482 (object, tags.chunk_id,
2485 if (old_chunk != matching_chunk)
2486 yaffs_trace(YAFFS_TRACE_ERROR,
2487 "gc: page in gc mismatch: %d %d %d %d",
2495 yaffs_trace(YAFFS_TRACE_ERROR,
2496 "page %d in gc has no object: %d %d %d ",
2498 tags.obj_id, tags.chunk_id,
2504 object->soft_del && tags.chunk_id != 0) {
2505 /* Data chunk in a soft deleted file,
2507 * It's a soft deleted data chunk,
2508 * No need to copy this, just forget
2509 * about it and fix up the object.
2512 /* Free chunks already includes
2513 * softdeleted chunks, how ever this
2514 * chunk is going to soon be really
2515 * deleted which will increment free
2516 * chunks. We have to decrement free
2517 * chunks so this works out properly.
2519 dev->n_free_chunks--;
2520 bi->soft_del_pages--;
2522 object->n_data_chunks--;
2523 if (object->n_data_chunks <= 0) {
2524 /* remeber to clean up obj */
2525 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2529 } else if (object) {
2530 /* It's either a data chunk in a live
2531 * file or an ObjectHeader, so we're
2533 * NB Need to keep the ObjectHeaders of
2534 * deleted files until the whole file
2535 * has been deleted off
2537 tags.serial_number++;
2540 if (tags.chunk_id == 0) {
2541 /* It is an object Id,
2542 * We need to nuke the
2543 * shrinkheader flags since its
2545 * Also need to clean up
2548 struct yaffs_obj_hdr *oh;
2549 oh = (struct yaffs_obj_hdr *) buffer;
2552 tags.extra_is_shrink = 0;
2553 oh->shadows_obj = 0;
2554 oh->inband_shadowed_obj_id = 0;
2555 tags.extra_shadows = 0;
2557 /* Update file size */
2558 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2559 yaffs_oh_size_load(oh,
2560 object->variant.file_variant.file_size);
2561 tags.extra_file_size =
2562 object->variant.file_variant.file_size;
2565 yaffs_verify_oh(object, oh, &tags, 1);
2567 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2570 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2573 if (new_chunk < 0) {
2574 ret_val = YAFFS_FAIL;
2577 /* Now fix up the Tnodes etc. */
2579 if (tags.chunk_id == 0) {
2581 object->hdr_chunk = new_chunk;
2582 object->serial = tags.serial_number;
2584 /* It's a data chunk */
2585 yaffs_put_chunk_in_file(object, tags.chunk_id,
2590 if (ret_val == YAFFS_OK)
2591 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2595 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2598 int ret_val = YAFFS_OK;
2600 int is_checkpt_block;
2602 int chunks_before = yaffs_get_erased_chunks(dev);
2604 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2606 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2608 yaffs_trace(YAFFS_TRACE_TRACING,
2609 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2610 block, bi->pages_in_use, bi->has_shrink_hdr,
2613 /*yaffs_verify_free_chunks(dev); */
2615 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2616 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2618 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2620 dev->gc_disable = 1;
2622 yaffs_summary_gc(dev, block);
2624 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2625 yaffs_trace(YAFFS_TRACE_TRACING,
2626 "Collecting block %d that has no chunks in use",
2628 yaffs_block_became_dirty(dev, block);
2631 u8 *buffer = yaffs_get_temp_buffer(dev);
2633 yaffs_verify_blk(dev, bi, block);
2635 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2636 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2638 for (/* init already done */ ;
2639 ret_val == YAFFS_OK &&
2640 dev->gc_chunk < dev->param.chunks_per_block &&
2641 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2643 dev->gc_chunk++, old_chunk++) {
2644 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2645 /* Page is in use and might need to be copied */
2647 ret_val = yaffs_gc_process_chunk(dev, bi,
2651 yaffs_release_temp_buffer(dev, buffer);
2654 yaffs_verify_collected_blk(dev, bi, block);
2656 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2658 * The gc did not complete. Set block state back to FULL
2659 * because checkpointing does not restore gc.
2661 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2663 /* The gc completed. */
2664 /* Do any required cleanups */
2665 for (i = 0; i < dev->n_clean_ups; i++) {
2666 /* Time to delete the file too */
2667 struct yaffs_obj *object =
2668 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2670 yaffs_free_tnode(dev,
2671 object->variant.file_variant.top);
2672 object->variant.file_variant.top = NULL;
2673 yaffs_trace(YAFFS_TRACE_GC,
2674 "yaffs: About to finally delete object %d",
2676 yaffs_generic_obj_del(object);
2677 object->my_dev->n_deleted_files--;
2681 chunks_after = yaffs_get_erased_chunks(dev);
2682 if (chunks_before >= chunks_after)
2683 yaffs_trace(YAFFS_TRACE_GC,
2684 "gc did not increase free chunks before %d after %d",
2685 chunks_before, chunks_after);
2688 dev->n_clean_ups = 0;
2691 dev->gc_disable = 0;
2697 * find_gc_block() selects the dirtiest block (or close enough)
2698 * for garbage collection.
2701 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2702 int aggressive, int background)
2706 unsigned selected = 0;
2707 int prioritised = 0;
2708 int prioritised_exist = 0;
2709 struct yaffs_block_info *bi;
2712 /* First let's see if we need to grab a prioritised block */
2713 if (dev->has_pending_prioritised_gc && !aggressive) {
2714 dev->gc_dirtiest = 0;
2715 bi = dev->block_info;
2716 for (i = dev->internal_start_block;
2717 i <= dev->internal_end_block && !selected; i++) {
2719 if (bi->gc_prioritise) {
2720 prioritised_exist = 1;
2721 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2722 yaffs_block_ok_for_gc(dev, bi)) {
2731 * If there is a prioritised block and none was selected then
2732 * this happened because there is at least one old dirty block
2733 * gumming up the works. Let's gc the oldest dirty block.
2736 if (prioritised_exist &&
2737 !selected && dev->oldest_dirty_block > 0)
2738 selected = dev->oldest_dirty_block;
2740 if (!prioritised_exist) /* None found, so we can clear this */
2741 dev->has_pending_prioritised_gc = 0;
2744 /* If we're doing aggressive GC then we are happy to take a less-dirty
2745 * block, and search harder.
2746 * else (leasurely gc), then we only bother to do this if the
2747 * block has only a few pages in use.
2753 dev->internal_end_block - dev->internal_start_block + 1;
2755 threshold = dev->param.chunks_per_block;
2756 iterations = n_blocks;
2761 max_threshold = dev->param.chunks_per_block / 2;
2763 max_threshold = dev->param.chunks_per_block / 8;
2765 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2766 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2768 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2769 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2770 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2771 if (threshold > max_threshold)
2772 threshold = max_threshold;
2774 iterations = n_blocks / 16 + 1;
2775 if (iterations > 100)
2781 (dev->gc_dirtiest < 1 ||
2782 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2784 dev->gc_block_finder++;
2785 if (dev->gc_block_finder < dev->internal_start_block ||
2786 dev->gc_block_finder > dev->internal_end_block)
2787 dev->gc_block_finder =
2788 dev->internal_start_block;
2790 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2792 pages_used = bi->pages_in_use - bi->soft_del_pages;
2794 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2795 pages_used < dev->param.chunks_per_block &&
2796 (dev->gc_dirtiest < 1 ||
2797 pages_used < dev->gc_pages_in_use) &&
2798 yaffs_block_ok_for_gc(dev, bi)) {
2799 dev->gc_dirtiest = dev->gc_block_finder;
2800 dev->gc_pages_in_use = pages_used;
2804 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2805 selected = dev->gc_dirtiest;
2809 * If nothing has been selected for a while, try the oldest dirty
2810 * because that's gumming up the works.
2813 if (!selected && dev->param.is_yaffs2 &&
2814 dev->gc_not_done >= (background ? 10 : 20)) {
2815 yaffs2_find_oldest_dirty_seq(dev);
2816 if (dev->oldest_dirty_block > 0) {
2817 selected = dev->oldest_dirty_block;
2818 dev->gc_dirtiest = selected;
2819 dev->oldest_dirty_gc_count++;
2820 bi = yaffs_get_block_info(dev, selected);
2821 dev->gc_pages_in_use =
2822 bi->pages_in_use - bi->soft_del_pages;
2824 dev->gc_not_done = 0;
2829 yaffs_trace(YAFFS_TRACE_GC,
2830 "GC Selected block %d with %d free, prioritised:%d",
2832 dev->param.chunks_per_block - dev->gc_pages_in_use,
2839 dev->gc_dirtiest = 0;
2840 dev->gc_pages_in_use = 0;
2841 dev->gc_not_done = 0;
2842 if (dev->refresh_skip > 0)
2843 dev->refresh_skip--;
2846 yaffs_trace(YAFFS_TRACE_GC,
2847 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2848 dev->gc_block_finder, dev->gc_not_done, threshold,
2849 dev->gc_dirtiest, dev->gc_pages_in_use,
2850 dev->oldest_dirty_block, background ? " bg" : "");
2856 /* New garbage collector
2857 * If we're very low on erased blocks then we do aggressive garbage collection
2858 * otherwise we do "leasurely" garbage collection.
2859 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2860 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2862 * The idea is to help clear out space in a more spread-out manner.
2863 * Dunno if it really does anything useful.
2865 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2868 int gc_ok = YAFFS_OK;
2872 int checkpt_block_adjust;
2874 if (dev->param.gc_control_fn &&
2875 (dev->param.gc_control_fn(dev) & 1) == 0)
2878 if (dev->gc_disable)
2879 /* Bail out so we don't get recursive gc */
2882 /* This loop should pass the first time.
2883 * Only loops here if the collection does not increase space.
2889 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2892 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2894 dev->n_erased_blocks * dev->param.chunks_per_block;
2896 /* If we need a block soon then do aggressive gc. */
2897 if (dev->n_erased_blocks < min_erased)
2901 && erased_chunks > (dev->n_free_chunks / 4))
2904 if (dev->gc_skip > 20)
2906 if (erased_chunks < dev->n_free_chunks / 2 ||
2907 dev->gc_skip < 1 || background)
2917 /* If we don't already have a block being gc'd then see if we
2918 * should start another */
2920 if (dev->gc_block < 1 && !aggressive) {
2921 dev->gc_block = yaffs2_find_refresh_block(dev);
2923 dev->n_clean_ups = 0;
2925 if (dev->gc_block < 1) {
2927 yaffs_find_gc_block(dev, aggressive, background);
2929 dev->n_clean_ups = 0;
2932 if (dev->gc_block > 0) {
2935 dev->passive_gc_count++;
2937 yaffs_trace(YAFFS_TRACE_GC,
2938 "yaffs: GC n_erased_blocks %d aggressive %d",
2939 dev->n_erased_blocks, aggressive);
2941 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2944 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
2945 dev->gc_block > 0) {
2946 yaffs_trace(YAFFS_TRACE_GC,
2947 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2948 dev->n_erased_blocks, max_tries,
2951 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2952 (dev->gc_block > 0) && (max_tries < 2));
2954 return aggressive ? gc_ok : YAFFS_OK;
2959 * Garbage collects. Intended to be called from a background thread.
2960 * Returns non-zero if at least half the free chunks are erased.
2962 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2964 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2966 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2968 yaffs_check_gc(dev, 1);
2969 return erased_chunks > dev->n_free_chunks / 2;
2972 /*-------------------- Data file manipulation -----------------*/
2974 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2976 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2978 if (nand_chunk >= 0)
2979 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2982 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2983 "Chunk %d not found zero instead",
2985 /* get sane (zero) data if you read a hole */
2986 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2992 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2997 struct yaffs_ext_tags tags;
2998 struct yaffs_block_info *bi;
3004 block = chunk_id / dev->param.chunks_per_block;
3005 page = chunk_id % dev->param.chunks_per_block;
3007 if (!yaffs_check_chunk_bit(dev, block, page))
3008 yaffs_trace(YAFFS_TRACE_VERIFY,
3009 "Deleting invalid chunk %d", chunk_id);
3011 bi = yaffs_get_block_info(dev, block);
3013 yaffs2_update_oldest_dirty_seq(dev, block, bi);
3015 yaffs_trace(YAFFS_TRACE_DELETION,
3016 "line %d delete of chunk %d",
3019 if (!dev->param.is_yaffs2 && mark_flash &&
3020 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
3022 memset(&tags, 0, sizeof(tags));
3023 tags.is_deleted = 1;
3024 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3025 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3027 dev->n_unmarked_deletions++;
3030 /* Pull out of the management area.
3031 * If the whole block became dirty, this will kick off an erasure.
3033 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
3034 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
3035 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
3036 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
3037 dev->n_free_chunks++;
3038 yaffs_clear_chunk_bit(dev, block, page);
3041 if (bi->pages_in_use == 0 &&
3042 !bi->has_shrink_hdr &&
3043 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
3044 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
3045 yaffs_block_became_dirty(dev, block);
3050 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3051 const u8 *buffer, int n_bytes, int use_reserve)
3053 /* Find old chunk Need to do this to get serial number
3054 * Write new one and patch into tree.
3055 * Invalidate old tags.
3059 struct yaffs_ext_tags prev_tags;
3061 struct yaffs_ext_tags new_tags;
3062 struct yaffs_dev *dev = in->my_dev;
3064 yaffs_check_gc(dev, 0);
3066 /* Get the previous chunk at this location in the file if it exists.
3067 * If it does not exist then put a zero into the tree. This creates
3068 * the tnode now, rather than later when it is harder to clean up.
3070 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3071 if (prev_chunk_id < 1 &&
3072 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3075 /* Set up new tags */
3076 memset(&new_tags, 0, sizeof(new_tags));
3078 new_tags.chunk_id = inode_chunk;
3079 new_tags.obj_id = in->obj_id;
3080 new_tags.serial_number =
3081 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3082 new_tags.n_bytes = n_bytes;
3084 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3085 yaffs_trace(YAFFS_TRACE_ERROR,
3086 "Writing %d bytes to chunk!!!!!!!!!",
3092 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3094 if (new_chunk_id > 0) {
3095 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3097 if (prev_chunk_id > 0)
3098 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3100 yaffs_verify_file_sane(in);
3102 return new_chunk_id;
3108 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3109 const YCHAR *name, const void *value, int size,
3112 struct yaffs_xattr_mod xmod;
3120 xmod.result = -ENOSPC;
3122 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3130 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3131 struct yaffs_xattr_mod *xmod)
3134 int x_offs = sizeof(struct yaffs_obj_hdr);
3135 struct yaffs_dev *dev = obj->my_dev;
3136 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3137 char *x_buffer = buffer + x_offs;
3141 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3142 xmod->size, xmod->flags);
3144 retval = nval_del(x_buffer, x_size, xmod->name);
3146 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3147 obj->xattr_known = 1;
3148 xmod->result = retval;
3153 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
3154 void *value, int size)
3156 char *buffer = NULL;
3158 struct yaffs_ext_tags tags;
3159 struct yaffs_dev *dev = obj->my_dev;
3160 int x_offs = sizeof(struct yaffs_obj_hdr);
3161 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3165 if (obj->hdr_chunk < 1)
3168 /* If we know that the object has no xattribs then don't do all the
3169 * reading and parsing.
3171 if (obj->xattr_known && !obj->has_xattr) {
3178 buffer = (char *)yaffs_get_temp_buffer(dev);
3183 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3185 if (result != YAFFS_OK)
3188 x_buffer = buffer + x_offs;
3190 if (!obj->xattr_known) {
3191 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3192 obj->xattr_known = 1;
3196 retval = nval_get(x_buffer, x_size, name, value, size);
3198 retval = nval_list(x_buffer, x_size, value, size);
3200 yaffs_release_temp_buffer(dev, (u8 *) buffer);
3204 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3205 const void *value, int size, int flags)
3207 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3210 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3212 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3215 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3218 return yaffs_do_xattrib_fetch(obj, name, value, size);
3221 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3223 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3226 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3229 struct yaffs_obj_hdr *oh;
3230 struct yaffs_dev *dev;
3231 struct yaffs_ext_tags tags;
3233 int alloc_failed = 0;
3235 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3239 in->lazy_loaded = 0;
3240 buf = yaffs_get_temp_buffer(dev);
3242 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3243 oh = (struct yaffs_obj_hdr *)buf;
3245 in->yst_mode = oh->yst_mode;
3246 yaffs_load_attribs(in, oh);
3247 yaffs_set_obj_name_from_oh(in, oh);
3249 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3250 in->variant.symlink_variant.alias =
3251 yaffs_clone_str(oh->alias);
3252 if (!in->variant.symlink_variant.alias)
3253 alloc_failed = 1; /* Not returned */
3255 yaffs_release_temp_buffer(dev, buf);
3258 /* UpdateObjectHeader updates the header on NAND for an object.
3259 * If name is not NULL, then that new name is used.
3261 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3262 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3265 struct yaffs_block_info *bi;
3266 struct yaffs_dev *dev = in->my_dev;
3271 struct yaffs_ext_tags new_tags;
3272 struct yaffs_ext_tags old_tags;
3273 const YCHAR *alias = NULL;
3275 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3276 struct yaffs_obj_hdr *oh = NULL;
3277 loff_t file_size = 0;
3279 strcpy(old_name, _Y("silly old name"));
3281 if (in->fake && in != dev->root_dir && !force && !xmod)
3284 yaffs_check_gc(dev, 0);
3285 yaffs_check_obj_details_loaded(in);
3287 buffer = yaffs_get_temp_buffer(in->my_dev);
3288 oh = (struct yaffs_obj_hdr *)buffer;
3290 prev_chunk_id = in->hdr_chunk;
3292 if (prev_chunk_id > 0) {
3293 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3296 yaffs_verify_oh(in, oh, &old_tags, 0);
3297 memcpy(old_name, oh->name, sizeof(oh->name));
3298 memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
3300 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3303 oh->type = in->variant_type;
3304 oh->yst_mode = in->yst_mode;
3305 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3307 yaffs_load_attribs_oh(oh, in);
3310 oh->parent_obj_id = in->parent->obj_id;
3312 oh->parent_obj_id = 0;
3314 if (name && *name) {
3315 memset(oh->name, 0, sizeof(oh->name));
3316 yaffs_load_oh_from_name(dev, oh->name, name);
3317 } else if (prev_chunk_id > 0) {
3318 memcpy(oh->name, old_name, sizeof(oh->name));
3320 memset(oh->name, 0, sizeof(oh->name));
3323 oh->is_shrink = is_shrink;
3325 switch (in->variant_type) {
3326 case YAFFS_OBJECT_TYPE_UNKNOWN:
3327 /* Should not happen */
3329 case YAFFS_OBJECT_TYPE_FILE:
3330 if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
3331 oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
3332 file_size = in->variant.file_variant.file_size;
3333 yaffs_oh_size_load(oh, file_size);
3335 case YAFFS_OBJECT_TYPE_HARDLINK:
3336 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3338 case YAFFS_OBJECT_TYPE_SPECIAL:
3341 case YAFFS_OBJECT_TYPE_DIRECTORY:
3344 case YAFFS_OBJECT_TYPE_SYMLINK:
3345 alias = in->variant.symlink_variant.alias;
3347 alias = _Y("no alias");
3348 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3349 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3353 /* process any xattrib modifications */
3355 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3358 memset(&new_tags, 0, sizeof(new_tags));
3360 new_tags.chunk_id = 0;
3361 new_tags.obj_id = in->obj_id;
3362 new_tags.serial_number = in->serial;
3364 /* Add extra info for file header */
3365 new_tags.extra_available = 1;
3366 new_tags.extra_parent_id = oh->parent_obj_id;
3367 new_tags.extra_file_size = file_size;
3368 new_tags.extra_is_shrink = oh->is_shrink;
3369 new_tags.extra_equiv_id = oh->equiv_id;
3370 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3371 new_tags.extra_obj_type = in->variant_type;
3372 yaffs_verify_oh(in, oh, &new_tags, 1);
3374 /* Create new chunk in NAND */
3376 yaffs_write_new_chunk(dev, buffer, &new_tags,
3377 (prev_chunk_id > 0) ? 1 : 0);
3380 yaffs_release_temp_buffer(dev, buffer);
3382 if (new_chunk_id < 0)
3383 return new_chunk_id;
3385 in->hdr_chunk = new_chunk_id;
3387 if (prev_chunk_id > 0)
3388 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3390 if (!yaffs_obj_cache_dirty(in))
3393 /* If this was a shrink, then mark the block
3394 * that the chunk lives on */
3396 bi = yaffs_get_block_info(in->my_dev,
3398 in->my_dev->param.chunks_per_block);
3399 bi->has_shrink_hdr = 1;
3403 return new_chunk_id;
3406 /*--------------------- File read/write ------------------------
3407 * Read and write have very similar structures.
3408 * In general the read/write has three parts to it
3409 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3410 * Some complete chunks
3411 * An incomplete chunk to end off with
3413 * Curve-balls: the first chunk might also be the last chunk.
3416 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3423 struct yaffs_cache *cache;
3424 struct yaffs_dev *dev;
3429 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3432 /* OK now check for the curveball where the start and end are in
3435 if ((start + n) < dev->data_bytes_per_chunk)
3438 n_copy = dev->data_bytes_per_chunk - start;
3440 cache = yaffs_find_chunk_cache(in, chunk);
3442 /* If the chunk is already in the cache or it is less than
3443 * a whole chunk or we're using inband tags then use the cache
3444 * (if there is caching) else bypass the cache.
3446 if (cache || n_copy != dev->data_bytes_per_chunk ||
3447 dev->param.inband_tags) {
3448 if (dev->param.n_caches > 0) {
3450 /* If we can't find the data in the cache,
3451 * then load it up. */
3455 yaffs_grab_chunk_cache(in->my_dev);
3457 cache->chunk_id = chunk;
3460 yaffs_rd_data_obj(in, chunk,
3465 yaffs_use_cache(dev, cache, 0);
3469 memcpy(buffer, &cache->data[start], n_copy);
3473 /* Read into the local buffer then copy.. */
3476 yaffs_get_temp_buffer(dev);
3477 yaffs_rd_data_obj(in, chunk, local_buffer);
3479 memcpy(buffer, &local_buffer[start], n_copy);
3481 yaffs_release_temp_buffer(dev, local_buffer);
3484 /* A full chunk. Read directly into the buffer. */
3485 yaffs_rd_data_obj(in, chunk, buffer);
3495 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3496 int n_bytes, int write_through)
3505 loff_t start_write = offset;
3506 int chunk_written = 0;
3509 struct yaffs_dev *dev;
3513 while (n > 0 && chunk_written >= 0) {
3514 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3516 if (((loff_t)chunk) *
3517 dev->data_bytes_per_chunk + start != offset ||
3518 start >= dev->data_bytes_per_chunk) {
3519 yaffs_trace(YAFFS_TRACE_ERROR,
3520 "AddrToChunk of offset %lld gives chunk %d start %d",
3521 offset, chunk, start);
3523 chunk++; /* File pos to chunk in file offset */
3525 /* OK now check for the curveball where the start and end are in
3529 if ((start + n) < dev->data_bytes_per_chunk) {
3532 /* Now calculate how many bytes to write back....
3533 * If we're overwriting and not writing to then end of
3534 * file then we need to write back as much as was there
3538 chunk_start = (((loff_t)(chunk - 1)) *
3539 dev->data_bytes_per_chunk);
3541 if (chunk_start > in->variant.file_variant.file_size)
3542 n_bytes_read = 0; /* Past end of file */
3545 in->variant.file_variant.file_size -
3548 if (n_bytes_read > dev->data_bytes_per_chunk)
3549 n_bytes_read = dev->data_bytes_per_chunk;
3553 (start + n)) ? n_bytes_read : (start + n);
3555 if (n_writeback < 0 ||
3556 n_writeback > dev->data_bytes_per_chunk)
3560 n_copy = dev->data_bytes_per_chunk - start;
3561 n_writeback = dev->data_bytes_per_chunk;
3564 if (n_copy != dev->data_bytes_per_chunk ||
3565 !dev->param.cache_bypass_aligned ||
3566 dev->param.inband_tags) {
3567 /* An incomplete start or end chunk (or maybe both
3568 * start and end chunk), or we're using inband tags,
3569 * or we're forcing writes through the cache,
3570 * so we want to use the cache buffers.
3572 if (dev->param.n_caches > 0) {
3573 struct yaffs_cache *cache;
3575 /* If we can't find the data in the cache, then
3577 cache = yaffs_find_chunk_cache(in, chunk);
3580 yaffs_check_alloc_available(dev, 1)) {
3581 cache = yaffs_grab_chunk_cache(dev);
3583 cache->chunk_id = chunk;
3586 yaffs_rd_data_obj(in, chunk,
3590 !yaffs_check_alloc_available(dev,
3592 /* Drop the cache if it was a read cache
3593 * item and no space check has been made
3600 yaffs_use_cache(dev, cache, 1);
3603 memcpy(&cache->data[start], buffer,
3607 cache->n_bytes = n_writeback;
3609 if (write_through) {
3619 chunk_written = -1; /* fail write */
3622 /* An incomplete start or end chunk (or maybe
3623 * both start and end chunk). Read into the
3624 * local buffer then copy over and write back.
3627 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3629 yaffs_rd_data_obj(in, chunk, local_buffer);
3630 memcpy(&local_buffer[start], buffer, n_copy);
3633 yaffs_wr_data_obj(in, chunk,
3637 yaffs_release_temp_buffer(dev, local_buffer);
3640 /* A full chunk. Write directly from the buffer. */
3643 yaffs_wr_data_obj(in, chunk, buffer,
3644 dev->data_bytes_per_chunk, 0);
3646 /* Since we've overwritten the cached data,
3647 * we better invalidate it. */
3648 yaffs_invalidate_chunk_cache(in, chunk);
3651 if (chunk_written >= 0) {
3659 /* Update file object */
3661 if ((start_write + n_done) > in->variant.file_variant.file_size)
3662 in->variant.file_variant.file_size = (start_write + n_done);
3668 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3669 int n_bytes, int write_through)
3671 yaffs2_handle_hole(in, offset);
3672 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
3675 /* ---------------------- File resizing stuff ------------------ */
3677 static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
3680 struct yaffs_dev *dev = in->my_dev;
3681 loff_t old_size = in->variant.file_variant.file_size;
3689 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3693 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3694 &start_del, &dummy);
3698 /* Delete backwards so that we don't end up with holes if
3699 * power is lost part-way through the operation.
3701 for (i = last_del; i >= start_del; i--) {
3702 /* NB this could be optimised somewhat,
3703 * eg. could retrieve the tags and write them without
3704 * using yaffs_chunk_del
3707 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3713 (dev->internal_start_block * dev->param.chunks_per_block) ||
3715 ((dev->internal_end_block + 1) *
3716 dev->param.chunks_per_block)) {
3717 yaffs_trace(YAFFS_TRACE_ALWAYS,
3718 "Found daft chunk_id %d for %d",
3721 in->n_data_chunks--;
3722 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3727 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3731 struct yaffs_dev *dev = obj->my_dev;
3733 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3735 yaffs_prune_chunks(obj, new_size);
3737 if (new_partial != 0) {
3738 int last_chunk = 1 + new_full;
3739 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3741 /* Rewrite the last chunk with its new size and zero pad */
3742 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3743 memset(local_buffer + new_partial, 0,
3744 dev->data_bytes_per_chunk - new_partial);
3746 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3749 yaffs_release_temp_buffer(dev, local_buffer);
3752 obj->variant.file_variant.file_size = new_size;
3754 yaffs_prune_tree(dev, &obj->variant.file_variant);
3757 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3759 struct yaffs_dev *dev = in->my_dev;
3760 loff_t old_size = in->variant.file_variant.file_size;
3762 yaffs_flush_file_cache(in, 1);
3763 yaffs_invalidate_whole_cache(in);
3765 yaffs_check_gc(dev, 0);
3767 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3770 if (new_size == old_size)
3773 if (new_size > old_size) {
3774 yaffs2_handle_hole(in, new_size);
3775 in->variant.file_variant.file_size = new_size;
3777 /* new_size < old_size */
3778 yaffs_resize_file_down(in, new_size);
3781 /* Write a new object header to reflect the resize.
3782 * show we've shrunk the file, if need be
3783 * Do this only if the file is not in the deleted directories
3784 * and is not shadowed.
3788 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3789 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3790 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3795 int yaffs_flush_file(struct yaffs_obj *in,
3803 yaffs_flush_file_cache(in, discard_cache);
3809 yaffs_load_current_time(in, 0, 0);
3811 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3812 YAFFS_OK : YAFFS_FAIL;
3816 /* yaffs_del_file deletes the whole file data
3817 * and the inode associated with the file.
3818 * It does not delete the links associated with the file.
3820 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3824 struct yaffs_dev *dev = in->my_dev;
3831 yaffs_change_obj_name(in, in->my_dev->del_dir,
3832 _Y("deleted"), 0, 0);
3833 yaffs_trace(YAFFS_TRACE_TRACING,
3834 "yaffs: immediate deletion of file %d",
3837 in->my_dev->n_deleted_files++;
3838 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3839 yaffs_resize_file(in, 0);
3840 yaffs_soft_del_file(in);
3843 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3844 _Y("unlinked"), 0, 0);
3849 static int yaffs_del_file(struct yaffs_obj *in)
3851 int ret_val = YAFFS_OK;
3852 int deleted; /* Need to cache value on stack if in is freed */
3853 struct yaffs_dev *dev = in->my_dev;
3855 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3856 yaffs_resize_file(in, 0);
3858 if (in->n_data_chunks > 0) {
3859 /* Use soft deletion if there is data in the file.
3860 * That won't be the case if it has been resized to zero.
3863 ret_val = yaffs_unlink_file_if_needed(in);
3865 deleted = in->deleted;
3867 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3870 in->my_dev->n_deleted_files++;
3871 yaffs_soft_del_file(in);
3873 return deleted ? YAFFS_OK : YAFFS_FAIL;
3875 /* The file has no data chunks so we toss it immediately */
3876 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3877 in->variant.file_variant.top = NULL;
3878 yaffs_generic_obj_del(in);
3884 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3887 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3888 !(list_empty(&obj->variant.dir_variant.children));
3891 static int yaffs_del_dir(struct yaffs_obj *obj)
3893 /* First check that the directory is empty. */
3894 if (yaffs_is_non_empty_dir(obj))
3897 return yaffs_generic_obj_del(obj);
3900 static int yaffs_del_symlink(struct yaffs_obj *in)
3902 kfree(in->variant.symlink_variant.alias);
3903 in->variant.symlink_variant.alias = NULL;
3905 return yaffs_generic_obj_del(in);
3908 static int yaffs_del_link(struct yaffs_obj *in)
3910 /* remove this hardlink from the list associated with the equivalent
3913 list_del_init(&in->hard_links);
3914 return yaffs_generic_obj_del(in);
3917 int yaffs_del_obj(struct yaffs_obj *obj)
3921 switch (obj->variant_type) {
3922 case YAFFS_OBJECT_TYPE_FILE:
3923 ret_val = yaffs_del_file(obj);
3925 case YAFFS_OBJECT_TYPE_DIRECTORY:
3926 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3927 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3928 "Remove object %d from dirty directories",
3930 list_del_init(&obj->variant.dir_variant.dirty);
3932 return yaffs_del_dir(obj);
3934 case YAFFS_OBJECT_TYPE_SYMLINK:
3935 ret_val = yaffs_del_symlink(obj);
3937 case YAFFS_OBJECT_TYPE_HARDLINK:
3938 ret_val = yaffs_del_link(obj);
3940 case YAFFS_OBJECT_TYPE_SPECIAL:
3941 ret_val = yaffs_generic_obj_del(obj);
3943 case YAFFS_OBJECT_TYPE_UNKNOWN:
3945 break; /* should not happen. */
3951 static void yaffs_empty_dir_to_dir(struct yaffs_obj *from_dir,
3952 struct yaffs_obj *to_dir)
3954 struct yaffs_obj *obj;
3955 struct list_head *lh;
3956 struct list_head *n;
3958 list_for_each_safe(lh, n, &from_dir->variant.dir_variant.children) {
3959 obj = list_entry(lh, struct yaffs_obj, siblings);
3960 yaffs_add_obj_to_dir(to_dir, obj);
3964 struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
3965 enum yaffs_obj_type type)
3967 /* Tear down the old variant */
3968 switch (obj->variant_type) {
3969 case YAFFS_OBJECT_TYPE_FILE:
3970 /* Nuke file data */
3971 yaffs_resize_file(obj, 0);
3972 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
3973 obj->variant.file_variant.top = NULL;
3975 case YAFFS_OBJECT_TYPE_DIRECTORY:
3976 /* Put the children in lost and found. */
3977 yaffs_empty_dir_to_dir(obj, obj->my_dev->lost_n_found);
3978 if (!list_empty(&obj->variant.dir_variant.dirty))
3979 list_del_init(&obj->variant.dir_variant.dirty);
3981 case YAFFS_OBJECT_TYPE_SYMLINK:
3982 /* Nuke symplink data */
3983 kfree(obj->variant.symlink_variant.alias);
3984 obj->variant.symlink_variant.alias = NULL;
3986 case YAFFS_OBJECT_TYPE_HARDLINK:
3987 list_del_init(&obj->hard_links);
3993 memset(&obj->variant, 0, sizeof(obj->variant));
3995 /*Set up new variant if the memset is not enough. */
3997 case YAFFS_OBJECT_TYPE_DIRECTORY:
3998 INIT_LIST_HEAD(&obj->variant.dir_variant.children);
3999 INIT_LIST_HEAD(&obj->variant.dir_variant.dirty);
4001 case YAFFS_OBJECT_TYPE_FILE:
4002 case YAFFS_OBJECT_TYPE_SYMLINK:
4003 case YAFFS_OBJECT_TYPE_HARDLINK:
4008 obj->variant_type = type;
4014 static int yaffs_unlink_worker(struct yaffs_obj *obj)
4024 yaffs_update_parent(obj->parent);
4026 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4027 return yaffs_del_link(obj);
4028 } else if (!list_empty(&obj->hard_links)) {
4029 /* Curve ball: We're unlinking an object that has a hardlink.
4031 * This problem arises because we are not strictly following
4032 * The Linux link/inode model.
4034 * We can't really delete the object.
4035 * Instead, we do the following:
4036 * - Select a hardlink.
4037 * - Unhook it from the hard links
4038 * - Move it from its parent directory so that the rename works.
4039 * - Rename the object to the hardlink's name.
4040 * - Delete the hardlink
4043 struct yaffs_obj *hl;
4044 struct yaffs_obj *parent;
4046 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4048 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
4051 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4052 parent = hl->parent;
4054 list_del_init(&hl->hard_links);
4056 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4058 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4060 if (ret_val == YAFFS_OK)
4061 ret_val = yaffs_generic_obj_del(hl);
4065 } else if (del_now) {
4066 switch (obj->variant_type) {
4067 case YAFFS_OBJECT_TYPE_FILE:
4068 return yaffs_del_file(obj);
4070 case YAFFS_OBJECT_TYPE_DIRECTORY:
4071 list_del_init(&obj->variant.dir_variant.dirty);
4072 return yaffs_del_dir(obj);
4074 case YAFFS_OBJECT_TYPE_SYMLINK:
4075 return yaffs_del_symlink(obj);
4077 case YAFFS_OBJECT_TYPE_SPECIAL:
4078 return yaffs_generic_obj_del(obj);
4080 case YAFFS_OBJECT_TYPE_HARDLINK:
4081 case YAFFS_OBJECT_TYPE_UNKNOWN:
4085 } else if (yaffs_is_non_empty_dir(obj)) {
4088 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4089 _Y("unlinked"), 0, 0);
4093 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4095 if (obj && obj->unlink_allowed)
4096 return yaffs_unlink_worker(obj);
4101 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4103 struct yaffs_obj *obj;
4105 obj = yaffs_find_by_name(dir, name);
4106 return yaffs_unlink_obj(obj);
4110 * If old_name is NULL then we take old_dir as the object to be renamed.
4112 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
4113 struct yaffs_obj *new_dir, const YCHAR *new_name)
4115 struct yaffs_obj *obj = NULL;
4116 struct yaffs_obj *existing_target = NULL;
4119 struct yaffs_dev *dev;
4121 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4125 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4130 dev = old_dir->my_dev;
4132 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4133 /* Special case for case insemsitive systems.
4134 * While look-up is case insensitive, the name isn't.
4135 * Therefore we might want to change x.txt to X.txt
4137 if (old_dir == new_dir &&
4138 old_name && new_name &&
4139 strcmp(old_name, new_name) == 0)
4143 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4144 YAFFS_MAX_NAME_LENGTH)
4149 obj = yaffs_find_by_name(old_dir, old_name);
4152 old_dir = obj->parent;
4155 if (obj && obj->rename_allowed) {
4156 /* Now handle an existing target, if there is one */
4157 existing_target = yaffs_find_by_name(new_dir, new_name);
4158 if (yaffs_is_non_empty_dir(existing_target)) {
4159 return YAFFS_FAIL; /* ENOTEMPTY */
4160 } else if (existing_target && existing_target != obj) {
4161 /* Nuke the target first, using shadowing,
4162 * but only if it isn't the same object.
4164 * Note we must disable gc here otherwise it can mess
4168 dev->gc_disable = 1;
4169 yaffs_change_obj_name(obj, new_dir, new_name, force,
4170 existing_target->obj_id);
4171 existing_target->is_shadowed = 1;
4172 yaffs_unlink_obj(existing_target);
4173 dev->gc_disable = 0;
4176 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4178 yaffs_update_parent(old_dir);
4179 if (new_dir != old_dir)
4180 yaffs_update_parent(new_dir);
4187 /*----------------------- Initialisation Scanning ---------------------- */
4189 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4190 int backward_scanning)
4192 struct yaffs_obj *obj;
4194 if (backward_scanning) {
4195 /* Handle YAFFS2 case (backward scanning)
4196 * If the shadowed object exists then ignore.
4198 obj = yaffs_find_by_number(dev, obj_id);
4203 /* Let's create it (if it does not exist) assuming it is a file so that
4204 * it can do shrinking etc.
4205 * We put it in unlinked dir to be cleaned up after the scanning
4208 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4211 obj->is_shadowed = 1;
4212 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4213 obj->variant.file_variant.shrink_size = 0;
4214 obj->valid = 1; /* So that we don't read any other info. */
4217 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4219 struct list_head *lh;
4220 struct list_head *save;
4221 struct yaffs_obj *hl;
4222 struct yaffs_obj *in;
4224 list_for_each_safe(lh, save, hard_list) {
4225 hl = list_entry(lh, struct yaffs_obj, hard_links);
4226 in = yaffs_find_by_number(dev,
4227 hl->variant.hardlink_variant.equiv_id);
4230 /* Add the hardlink pointers */
4231 hl->variant.hardlink_variant.equiv_obj = in;
4232 list_add(&hl->hard_links, &in->hard_links);
4234 /* Todo Need to report/handle this better.
4235 * Got a problem... hardlink to a non-existant object
4237 hl->variant.hardlink_variant.equiv_obj = NULL;
4238 INIT_LIST_HEAD(&hl->hard_links);
4243 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4246 * Sort out state of unlinked and deleted objects after scanning.
4248 struct list_head *i;
4249 struct list_head *n;
4250 struct yaffs_obj *l;
4255 /* Soft delete all the unlinked files */
4256 list_for_each_safe(i, n,
4257 &dev->unlinked_dir->variant.dir_variant.children) {
4258 l = list_entry(i, struct yaffs_obj, siblings);
4262 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4263 l = list_entry(i, struct yaffs_obj, siblings);
4269 * This code iterates through all the objects making sure that they are rooted.
4270 * Any unrooted objects are re-rooted in lost+found.
4271 * An object needs to be in one of:
4272 * - Directly under deleted, unlinked
4273 * - Directly or indirectly under root.
4276 * This code assumes that we don't ever change the current relationships
4277 * between directories:
4278 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4279 * lost-n-found->parent == root_dir
4281 * This fixes the problem where directories might have inadvertently been
4282 * deleted leaving the object "hanging" without being rooted in the
4286 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4288 return (obj == dev->del_dir ||
4289 obj == dev->unlinked_dir || obj == dev->root_dir);
4292 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4294 struct yaffs_obj *obj;
4295 struct yaffs_obj *parent;
4297 struct list_head *lh;
4298 struct list_head *n;
4305 /* Iterate through the objects in each hash entry,
4306 * looking at each object.
4307 * Make sure it is rooted.
4310 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4311 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4312 obj = list_entry(lh, struct yaffs_obj, hash_link);
4313 parent = obj->parent;
4315 if (yaffs_has_null_parent(dev, obj)) {
4316 /* These directories are not hanging */
4318 } else if (!parent ||
4319 parent->variant_type !=
4320 YAFFS_OBJECT_TYPE_DIRECTORY) {
4322 } else if (yaffs_has_null_parent(dev, parent)) {
4326 * Need to follow the parent chain to
4327 * see if it is hanging.
4332 while (parent != dev->root_dir &&
4334 parent->parent->variant_type ==
4335 YAFFS_OBJECT_TYPE_DIRECTORY &&
4337 parent = parent->parent;
4340 if (parent != dev->root_dir)
4344 yaffs_trace(YAFFS_TRACE_SCAN,
4345 "Hanging object %d moved to lost and found",
4347 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4354 * Delete directory contents for cleaning up lost and found.
4356 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4358 struct yaffs_obj *obj;
4359 struct list_head *lh;
4360 struct list_head *n;
4362 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4365 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4366 obj = list_entry(lh, struct yaffs_obj, siblings);
4367 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4368 yaffs_del_dir_contents(obj);
4369 yaffs_trace(YAFFS_TRACE_SCAN,
4370 "Deleting lost_found object %d",
4372 yaffs_unlink_obj(obj);
4376 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4378 yaffs_del_dir_contents(dev->lost_n_found);
4382 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4386 struct list_head *i;
4387 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4388 struct yaffs_obj *l;
4394 yaffs_trace(YAFFS_TRACE_ALWAYS,
4395 "tragedy: yaffs_find_by_name: null pointer directory"
4400 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4401 yaffs_trace(YAFFS_TRACE_ALWAYS,
4402 "tragedy: yaffs_find_by_name: non-directory"
4407 sum = yaffs_calc_name_sum(name);
4409 list_for_each(i, &directory->variant.dir_variant.children) {
4410 l = list_entry(i, struct yaffs_obj, siblings);
4412 if (l->parent != directory)
4415 yaffs_check_obj_details_loaded(l);
4417 /* Special case for lost-n-found */
4418 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4419 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4421 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4422 /* LostnFound chunk called Objxxx
4425 yaffs_get_obj_name(l, buffer,
4426 YAFFS_MAX_NAME_LENGTH + 1);
4427 if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
4434 /* GetEquivalentObject dereferences any hard links to get to the
4438 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4440 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4441 obj = obj->variant.hardlink_variant.equiv_obj;
4442 yaffs_check_obj_details_loaded(obj);
4448 * A note or two on object names.
4449 * * If the object name is missing, we then make one up in the form objnnn
4451 * * ASCII names are stored in the object header's name field from byte zero
4452 * * Unicode names are historically stored starting from byte zero.
4454 * Then there are automatic Unicode names...
4455 * The purpose of these is to save names in a way that can be read as
4456 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4457 * system to share files.
4459 * These automatic unicode are stored slightly differently...
4460 * - If the name can fit in the ASCII character space then they are saved as
4461 * ascii names as per above.
4462 * - If the name needs Unicode then the name is saved in Unicode
4463 * starting at oh->name[1].
4466 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4469 /* Create an object name if we could not find one. */
4470 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4471 YCHAR local_name[20];
4472 YCHAR num_string[20];
4473 YCHAR *x = &num_string[19];
4474 unsigned v = obj->obj_id;
4478 *x = '0' + (v % 10);
4481 /* make up a name */
4482 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4483 strcat(local_name, x);
4484 strncpy(name, local_name, buffer_size - 1);
4488 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4490 memset(name, 0, buffer_size * sizeof(YCHAR));
4491 yaffs_check_obj_details_loaded(obj);
4492 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4493 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4494 } else if (obj->short_name[0]) {
4495 strcpy(name, obj->short_name);
4496 } else if (obj->hdr_chunk > 0) {
4498 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4500 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4502 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4504 if (obj->hdr_chunk > 0) {
4505 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4509 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4512 yaffs_release_temp_buffer(obj->my_dev, buffer);
4515 yaffs_fix_null_name(obj, name, buffer_size);
4517 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4520 loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
4522 /* Dereference any hard linking */
4523 obj = yaffs_get_equivalent_obj(obj);
4525 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4526 return obj->variant.file_variant.file_size;
4527 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4528 if (!obj->variant.symlink_variant.alias)
4530 return strnlen(obj->variant.symlink_variant.alias,
4531 YAFFS_MAX_ALIAS_LENGTH);
4533 /* Only a directory should drop through to here */
4534 return obj->my_dev->data_bytes_per_chunk;
4538 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4541 struct list_head *i;
4544 count++; /* the object itself */
4546 list_for_each(i, &obj->hard_links)
4547 count++; /* add the hard links; */
4552 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4554 obj = yaffs_get_equivalent_obj(obj);
4559 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4561 obj = yaffs_get_equivalent_obj(obj);
4563 switch (obj->variant_type) {
4564 case YAFFS_OBJECT_TYPE_FILE:
4567 case YAFFS_OBJECT_TYPE_DIRECTORY:
4570 case YAFFS_OBJECT_TYPE_SYMLINK:
4573 case YAFFS_OBJECT_TYPE_HARDLINK:
4576 case YAFFS_OBJECT_TYPE_SPECIAL:
4577 if (S_ISFIFO(obj->yst_mode))
4579 if (S_ISCHR(obj->yst_mode))
4581 if (S_ISBLK(obj->yst_mode))
4583 if (S_ISSOCK(obj->yst_mode))
4593 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4595 obj = yaffs_get_equivalent_obj(obj);
4596 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4597 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4599 return yaffs_clone_str(_Y(""));
4602 /*--------------------------- Initialisation code -------------------------- */
4604 static int yaffs_check_dev_fns(struct yaffs_dev *dev)
4606 struct yaffs_driver *drv = &dev->drv;
4607 struct yaffs_tags_handler *tagger = &dev->tagger;
4609 /* Common functions, gotta have */
4610 if (!drv->drv_read_chunk_fn ||
4611 !drv->drv_write_chunk_fn ||
4615 if (dev->param.is_yaffs2 &&
4616 (!drv->drv_mark_bad_fn || !drv->drv_check_bad_fn))
4619 /* Install the default tags marshalling functions if needed. */
4620 yaffs_tags_compat_install(dev);
4621 yaffs_tags_marshall_install(dev);
4623 /* Check we now have the marshalling functions required. */
4624 if (!tagger->write_chunk_tags_fn ||
4625 !tagger->read_chunk_tags_fn ||
4626 !tagger->query_block_fn ||
4627 !tagger->mark_bad_fn)
4633 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4635 /* Initialise the unlinked, deleted, root and lost+found directories */
4636 dev->lost_n_found = dev->root_dir = NULL;
4637 dev->unlinked_dir = dev->del_dir = NULL;
4639 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4641 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4643 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4644 YAFFS_ROOT_MODE | S_IFDIR);
4646 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4647 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4649 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4651 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4658 * Typically only used by yaffs_guts_initialise, but also used by the
4659 * Low level yaffs driver tests.
4662 int yaffs_guts_ll_init(struct yaffs_dev *dev)
4666 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_ll_init()");
4669 yaffs_trace(YAFFS_TRACE_ALWAYS,
4670 "yaffs: Need a device"
4678 dev->internal_start_block = dev->param.start_block;
4679 dev->internal_end_block = dev->param.end_block;
4680 dev->block_offset = 0;
4681 dev->chunk_offset = 0;
4682 dev->n_free_chunks = 0;
4686 if (dev->param.start_block == 0) {
4687 dev->internal_start_block = dev->param.start_block + 1;
4688 dev->internal_end_block = dev->param.end_block + 1;
4689 dev->block_offset = 1;
4690 dev->chunk_offset = dev->param.chunks_per_block;
4693 /* Check geometry parameters. */
4695 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4696 dev->param.total_bytes_per_chunk < 1024) ||
4697 (!dev->param.is_yaffs2 &&
4698 dev->param.total_bytes_per_chunk < 512) ||
4699 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4700 dev->param.chunks_per_block < 2 ||
4701 dev->param.n_reserved_blocks < 2 ||
4702 dev->internal_start_block <= 0 ||
4703 dev->internal_end_block <= 0 ||
4704 dev->internal_end_block <=
4705 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4707 /* otherwise it is too small */
4708 yaffs_trace(YAFFS_TRACE_ALWAYS,
4709 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4710 dev->param.total_bytes_per_chunk,
4711 dev->param.is_yaffs2 ? "2" : "",
4712 dev->param.inband_tags);
4716 /* Sort out space for inband tags, if required */
4717 if (dev->param.inband_tags)
4718 dev->data_bytes_per_chunk =
4719 dev->param.total_bytes_per_chunk -
4720 sizeof(struct yaffs_packed_tags2_tags_only);
4722 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4724 /* Got the right mix of functions? */
4725 if (!yaffs_check_dev_fns(dev)) {
4726 /* Function missing */
4727 yaffs_trace(YAFFS_TRACE_ALWAYS,
4728 "device function(s) missing or wrong");
4733 if (yaffs_init_nand(dev) != YAFFS_OK) {
4734 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4742 int yaffs_guts_format_dev(struct yaffs_dev *dev)
4745 enum yaffs_block_state state;
4748 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4754 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
4755 yaffs_query_init_block_state(dev, i, &state, &dummy);
4756 if (state != YAFFS_BLOCK_STATE_DEAD)
4757 yaffs_erase_block(dev, i);
4764 int yaffs_guts_initialise(struct yaffs_dev *dev)
4766 int init_failed = 0;
4770 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4773 if (dev->is_mounted) {
4774 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4778 dev->is_mounted = 1;
4780 /* OK now calculate a few things for the device */
4783 * Calculate all the chunk size manipulation numbers:
4785 x = dev->data_bytes_per_chunk;
4786 /* We always use dev->chunk_shift and dev->chunk_div */
4787 dev->chunk_shift = calc_shifts(x);
4788 x >>= dev->chunk_shift;
4790 /* We only use chunk mask if chunk_div is 1 */
4791 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4794 * Calculate chunk_grp_bits.
4795 * We need to find the next power of 2 > than internal_end_block
4798 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4800 bits = calc_shifts_ceiling(x);
4802 /* Set up tnode width if wide tnodes are enabled. */
4803 if (!dev->param.wide_tnodes_disabled) {
4804 /* bits must be even so that we end up with 32-bit words */
4808 dev->tnode_width = 16;
4810 dev->tnode_width = bits;
4812 dev->tnode_width = 16;
4815 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4817 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4818 * so if the bitwidth of the
4819 * chunk range we're using is greater than 16 we need
4820 * to figure out chunk shift and chunk_grp_size
4823 if (bits <= dev->tnode_width)
4824 dev->chunk_grp_bits = 0;
4826 dev->chunk_grp_bits = (u16)(bits - dev->tnode_width);
4828 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4829 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4830 dev->tnode_size = sizeof(struct yaffs_tnode);
4832 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4834 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4835 /* We have a problem because the soft delete won't work if
4836 * the chunk group size > chunks per block.
4837 * This can be remedied by using larger "virtual blocks".
4839 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4844 /* Finished verifying the device, continue with initialisation */
4846 /* More device initialisation */
4848 dev->passive_gc_count = 0;
4849 dev->oldest_dirty_gc_count = 0;
4851 dev->gc_block_finder = 0;
4852 dev->buffered_block = -1;
4853 dev->doing_buffered_block_rewrite = 0;
4854 dev->n_deleted_files = 0;
4855 dev->n_bg_deletions = 0;
4856 dev->n_unlinked_files = 0;
4857 dev->n_ecc_fixed = 0;
4858 dev->n_ecc_unfixed = 0;
4859 dev->n_tags_ecc_fixed = 0;
4860 dev->n_tags_ecc_unfixed = 0;
4861 dev->n_erase_failures = 0;
4862 dev->n_erased_blocks = 0;
4863 dev->gc_disable = 0;
4864 dev->has_pending_prioritised_gc = 1;
4865 /* Assume the worst for now, will get fixed on first GC */
4866 INIT_LIST_HEAD(&dev->dirty_dirs);
4867 dev->oldest_dirty_seq = 0;
4868 dev->oldest_dirty_block = 0;
4870 /* Initialise temporary buffers and caches. */
4871 if (!yaffs_init_tmp_buffers(dev))
4875 dev->gc_cleanup_list = NULL;
4877 if (!init_failed && dev->param.n_caches > 0) {
4881 dev->param.n_caches * sizeof(struct yaffs_cache);
4883 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4884 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4886 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4888 buf = (u8 *) dev->cache;
4891 memset(dev->cache, 0, cache_bytes);
4893 for (i = 0; i < dev->param.n_caches && buf; i++) {
4894 dev->cache[i].object = NULL;
4895 dev->cache[i].last_use = 0;
4896 dev->cache[i].dirty = 0;
4897 dev->cache[i].data = buf =
4898 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4903 dev->cache_last_use = 0;
4906 dev->cache_hits = 0;
4909 dev->gc_cleanup_list =
4910 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4912 if (!dev->gc_cleanup_list)
4916 if (dev->param.is_yaffs2)
4917 dev->param.use_header_file_size = 1;
4919 if (!init_failed && !yaffs_init_blocks(dev))
4922 yaffs_init_tnodes_and_objs(dev);
4924 if (!init_failed && !yaffs_create_initial_dir(dev))
4927 if (!init_failed && dev->param.is_yaffs2 &&
4928 !dev->param.disable_summary &&
4929 !yaffs_summary_init(dev))
4933 /* Now scan the flash. */
4934 if (dev->param.is_yaffs2) {
4935 if (yaffs2_checkpt_restore(dev)) {
4936 yaffs_check_obj_details_loaded(dev->root_dir);
4937 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4939 "yaffs: restored from checkpoint"
4943 /* Clean up the mess caused by an aborted
4944 * checkpoint load then scan backwards.
4946 yaffs_deinit_blocks(dev);
4948 yaffs_deinit_tnodes_and_objs(dev);
4950 dev->n_erased_blocks = 0;
4951 dev->n_free_chunks = 0;
4952 dev->alloc_block = -1;
4953 dev->alloc_page = -1;
4954 dev->n_deleted_files = 0;
4955 dev->n_unlinked_files = 0;
4956 dev->n_bg_deletions = 0;
4958 if (!init_failed && !yaffs_init_blocks(dev))
4961 yaffs_init_tnodes_and_objs(dev);
4964 && !yaffs_create_initial_dir(dev))
4967 if (!init_failed && !yaffs2_scan_backwards(dev))
4970 } else if (!yaffs1_scan(dev)) {
4974 yaffs_strip_deleted_objs(dev);
4975 yaffs_fix_hanging_objs(dev);
4976 if (dev->param.empty_lost_n_found)
4977 yaffs_empty_l_n_f(dev);
4981 /* Clean up the mess */
4982 yaffs_trace(YAFFS_TRACE_TRACING,
4983 "yaffs: yaffs_guts_initialise() aborted.");
4985 yaffs_deinitialise(dev);
4989 /* Zero out stats */
4990 dev->n_page_reads = 0;
4991 dev->n_page_writes = 0;
4992 dev->n_erasures = 0;
4993 dev->n_gc_copies = 0;
4994 dev->n_retried_writes = 0;
4996 dev->n_retired_blocks = 0;
4998 yaffs_verify_free_chunks(dev);
4999 yaffs_verify_blocks(dev);
5001 /* Clean up any aborted checkpoint data */
5002 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5003 yaffs2_checkpt_invalidate(dev);
5005 yaffs_trace(YAFFS_TRACE_TRACING,
5006 "yaffs: yaffs_guts_initialise() done.");
5010 void yaffs_deinitialise(struct yaffs_dev *dev)
5012 if (dev->is_mounted) {
5015 yaffs_deinit_blocks(dev);
5016 yaffs_deinit_tnodes_and_objs(dev);
5017 yaffs_summary_deinit(dev);
5019 if (dev->param.n_caches > 0 && dev->cache) {
5021 for (i = 0; i < dev->param.n_caches; i++) {
5022 kfree(dev->cache[i].data);
5023 dev->cache[i].data = NULL;
5030 kfree(dev->gc_cleanup_list);
5032 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5033 kfree(dev->temp_buffer[i].buffer);
5035 dev->is_mounted = 0;
5037 yaffs_deinit_nand(dev);
5041 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5045 struct yaffs_block_info *blk;
5047 blk = dev->block_info;
5048 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5049 switch (blk->block_state) {
5050 case YAFFS_BLOCK_STATE_EMPTY:
5051 case YAFFS_BLOCK_STATE_ALLOCATING:
5052 case YAFFS_BLOCK_STATE_COLLECTING:
5053 case YAFFS_BLOCK_STATE_FULL:
5055 (dev->param.chunks_per_block - blk->pages_in_use +
5056 blk->soft_del_pages);
5066 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5068 /* This is what we report to the outside world */
5071 int blocks_for_checkpt;
5074 n_free = dev->n_free_chunks;
5075 n_free += dev->n_deleted_files;
5077 /* Now count and subtract the number of dirty chunks in the cache. */
5079 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5080 if (dev->cache[i].dirty)
5084 n_free -= n_dirty_caches;
5087 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5089 /* Now figure checkpoint space and report that... */
5090 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5092 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
5103 * Marshalling functions to get loff_t file sizes into and out of
5106 void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize)
5108 oh->file_size_low = (fsize & 0xFFFFFFFF);
5109 oh->file_size_high = ((fsize >> 32) & 0xFFFFFFFF);
5112 loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh)
5116 if (sizeof(loff_t) >= 8 && ~(oh->file_size_high))
5117 retval = (((loff_t) oh->file_size_high) << 32) |
5118 (((loff_t) oh->file_size_low) & 0xFFFFFFFF);
5120 retval = (loff_t) oh->file_size_low;
5126 void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
5129 struct yaffs_block_info *bi;
5132 for(i = 0; i < 10; i++)
5135 for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
5136 bi = yaffs_get_block_info(dev, i);
5137 s = bi->block_state;
5138 if(s > YAFFS_BLOCK_STATE_DEAD || s < YAFFS_BLOCK_STATE_UNKNOWN)