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_tagsvalidity.h"
19 #include "yaffs_getblockinfo.h"
20 #include "yaffs_tagscompat.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"
32 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
33 #define YAFFS_GC_GOOD_ENOUGH 2
34 #define YAFFS_GC_PASSIVE_THRESHOLD 4
36 #include "yaffs_ecc.h"
38 /* Forward declarations */
40 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
41 const u8 *buffer, int n_bytes, int use_reserve);
45 /* Function to calculate chunk and offset */
47 static inline void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
48 int *chunk_out, u32 *offset_out)
53 chunk = (u32) (addr >> dev->chunk_shift);
55 if (dev->chunk_div == 1) {
56 /* easy power of 2 case */
57 offset = (u32) (addr & dev->chunk_mask);
59 /* Non power-of-2 case */
63 chunk /= dev->chunk_div;
65 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
66 offset = (u32) (addr - chunk_base);
73 /* Function to return the number of shifts for a power of 2 greater than or
74 * equal to the given number
75 * Note we don't try to cater for all possible numbers and this does not have to
76 * be hellishly efficient.
79 static inline u32 calc_shifts_ceiling(u32 x)
84 shifts = extra_bits = 0;
99 /* Function to return the number of shifts to get a 1 in bit 0
102 static inline u32 calc_shifts(u32 x)
120 * Temporary buffer manipulations.
123 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
128 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
130 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
131 dev->temp_buffer[i].line = 0; /* not in use */
132 dev->temp_buffer[i].buffer = buf =
133 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
136 return buf ? YAFFS_OK : YAFFS_FAIL;
139 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
145 if (dev->temp_in_use > dev->max_temp)
146 dev->max_temp = dev->temp_in_use;
148 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
149 if (dev->temp_buffer[i].line == 0) {
150 dev->temp_buffer[i].line = line_no;
151 if ((i + 1) > dev->max_temp) {
152 dev->max_temp = i + 1;
153 for (j = 0; j <= i; j++)
154 dev->temp_buffer[j].max_line =
155 dev->temp_buffer[j].line;
158 return dev->temp_buffer[i].buffer;
162 yaffs_trace(YAFFS_TRACE_BUFFERS,
163 "Out of temp buffers at line %d, other held by lines:",
165 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
166 yaffs_trace(YAFFS_TRACE_BUFFERS,
167 " %d", dev->temp_buffer[i].line);
170 * If we got here then we have to allocate an unmanaged one
174 dev->unmanaged_buffer_allocs++;
175 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
179 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer, int line_no)
185 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
186 if (dev->temp_buffer[i].buffer == buffer) {
187 dev->temp_buffer[i].line = 0;
193 /* assume it is an unmanaged one. */
194 yaffs_trace(YAFFS_TRACE_BUFFERS,
195 "Releasing unmanaged temp buffer in line %d",
198 dev->unmanaged_buffer_deallocs++;
204 * Determine if we have a managed buffer.
206 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer)
210 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
211 if (dev->temp_buffer[i].buffer == buffer)
215 for (i = 0; i < dev->param.n_caches; i++) {
216 if (dev->cache[i].data == buffer)
220 if (buffer == dev->checkpt_buffer)
223 yaffs_trace(YAFFS_TRACE_ALWAYS,
224 "yaffs: unmaged buffer detected.");
229 * Functions for robustisizing TODO
233 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
235 const struct yaffs_ext_tags *tags)
238 nand_chunk = nand_chunk;
243 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
244 const struct yaffs_ext_tags *tags)
247 nand_chunk = nand_chunk;
251 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
252 struct yaffs_block_info *bi)
254 if (!bi->gc_prioritise) {
255 bi->gc_prioritise = 1;
256 dev->has_pending_prioritised_gc = 1;
257 bi->chunk_error_strikes++;
259 if (bi->chunk_error_strikes > 3) {
260 bi->needs_retiring = 1; /* Too many stikes, so retire */
261 yaffs_trace(YAFFS_TRACE_ALWAYS,
262 "yaffs: Block struck out");
268 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
271 int flash_block = nand_chunk / dev->param.chunks_per_block;
272 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
274 yaffs_handle_chunk_error(dev, bi);
277 /* Was an actual write failure,
278 * so mark the block for retirement.*/
279 bi->needs_retiring = 1;
280 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
281 "**>> Block %d needs retiring", flash_block);
284 /* Delete the chunk */
285 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
286 yaffs_skip_rest_of_block(dev);
294 * Simple hash function. Needs to have a reasonable spread
297 static inline int yaffs_hash_fn(int n)
300 return n % YAFFS_NOBJECT_BUCKETS;
304 * Access functions to useful fake objects.
305 * Note that root might have a presence in NAND if permissions are set.
308 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
310 return dev->root_dir;
313 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
315 return dev->lost_n_found;
319 * Erased NAND checking functions
322 int yaffs_check_ff(u8 *buffer, int n_bytes)
324 /* Horrible, slow implementation */
333 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
335 int retval = YAFFS_OK;
336 u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
337 struct yaffs_ext_tags tags;
340 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
342 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
345 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
347 yaffs_trace(YAFFS_TRACE_NANDACCESS,
348 "Chunk %d not erased", nand_chunk);
352 yaffs_release_temp_buffer(dev, data, __LINE__);
358 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
361 struct yaffs_ext_tags *tags)
363 int retval = YAFFS_OK;
364 struct yaffs_ext_tags temp_tags;
365 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
368 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
369 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
370 temp_tags.obj_id != tags->obj_id ||
371 temp_tags.chunk_id != tags->chunk_id ||
372 temp_tags.n_bytes != tags->n_bytes)
375 yaffs_release_temp_buffer(dev, buffer, __LINE__);
381 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
384 int reserved_blocks = dev->param.n_reserved_blocks;
387 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
390 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
392 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
395 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
398 struct yaffs_block_info *bi;
400 if (dev->n_erased_blocks < 1) {
401 /* Hoosterman we've got a problem.
402 * Can't get space to gc
404 yaffs_trace(YAFFS_TRACE_ERROR,
405 "yaffs tragedy: no more erased blocks");
410 /* Find an empty block. */
412 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
413 dev->alloc_block_finder++;
414 if (dev->alloc_block_finder < dev->internal_start_block
415 || dev->alloc_block_finder > dev->internal_end_block) {
416 dev->alloc_block_finder = dev->internal_start_block;
419 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
421 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
422 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
424 bi->seq_number = dev->seq_number;
425 dev->n_erased_blocks--;
426 yaffs_trace(YAFFS_TRACE_ALLOCATE,
427 "Allocated block %d, seq %d, %d left" ,
428 dev->alloc_block_finder, dev->seq_number,
429 dev->n_erased_blocks);
430 return dev->alloc_block_finder;
434 yaffs_trace(YAFFS_TRACE_ALWAYS,
435 "yaffs tragedy: no more erased blocks, but there should have been %d",
436 dev->n_erased_blocks);
441 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
442 struct yaffs_block_info **block_ptr)
445 struct yaffs_block_info *bi;
447 if (dev->alloc_block < 0) {
448 /* Get next block to allocate off */
449 dev->alloc_block = yaffs_find_alloc_block(dev);
453 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
454 /* No space unless we're allowed to use the reserve. */
458 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
459 && dev->alloc_page == 0)
460 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
462 /* Next page please.... */
463 if (dev->alloc_block >= 0) {
464 bi = yaffs_get_block_info(dev, dev->alloc_block);
466 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
469 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
473 dev->n_free_chunks--;
475 /* If the block is full set the state to full */
476 if (dev->alloc_page >= dev->param.chunks_per_block) {
477 bi->block_state = YAFFS_BLOCK_STATE_FULL;
478 dev->alloc_block = -1;
487 yaffs_trace(YAFFS_TRACE_ERROR,
488 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
493 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
497 n = dev->n_erased_blocks * dev->param.chunks_per_block;
499 if (dev->alloc_block > 0)
500 n += (dev->param.chunks_per_block - dev->alloc_page);
507 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
508 * if we don't want to write to it.
510 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
512 struct yaffs_block_info *bi;
514 if (dev->alloc_block > 0) {
515 bi = yaffs_get_block_info(dev, dev->alloc_block);
516 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
517 bi->block_state = YAFFS_BLOCK_STATE_FULL;
518 dev->alloc_block = -1;
523 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
525 struct yaffs_ext_tags *tags, int use_reserver)
531 yaffs2_checkpt_invalidate(dev);
534 struct yaffs_block_info *bi = 0;
537 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
543 /* First check this chunk is erased, if it needs
544 * checking. The checking policy (unless forced
545 * always on) is as follows:
547 * Check the first page we try to write in a block.
548 * If the check passes then we don't need to check any
549 * more. If the check fails, we check again...
550 * If the block has been erased, we don't need to check.
552 * However, if the block has been prioritised for gc,
553 * then we think there might be something odd about
554 * this block and stop using it.
556 * Rationale: We should only ever see chunks that have
557 * not been erased if there was a partially written
558 * chunk due to power loss. This checking policy should
559 * catch that case with very few checks and thus save a
560 * lot of checks that are most likely not needed.
563 * If an erase check fails or the write fails we skip the
567 /* let's give it a try */
570 if (dev->param.always_check_erased)
571 bi->skip_erased_check = 0;
573 if (!bi->skip_erased_check) {
574 erased_ok = yaffs_check_chunk_erased(dev, chunk);
575 if (erased_ok != YAFFS_OK) {
576 yaffs_trace(YAFFS_TRACE_ERROR,
577 "**>> yaffs chunk %d was not erased",
580 /* If not erased, delete this one,
581 * skip rest of block and
582 * try another chunk */
583 yaffs_chunk_del(dev, chunk, 1, __LINE__);
584 yaffs_skip_rest_of_block(dev);
589 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
591 if (!bi->skip_erased_check)
593 yaffs_verify_chunk_written(dev, chunk, data, tags);
595 if (write_ok != YAFFS_OK) {
596 /* Clean up aborted write, skip to next block and
597 * try another chunk */
598 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
602 bi->skip_erased_check = 1;
604 /* Copy the data into the robustification buffer */
605 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
607 } while (write_ok != YAFFS_OK &&
608 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
614 yaffs_trace(YAFFS_TRACE_ERROR,
615 "**>> yaffs write required %d attempts",
617 dev->n_retired_writes += (attempts - 1);
624 * Block retiring for handling a broken block.
627 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
629 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
631 yaffs2_checkpt_invalidate(dev);
633 yaffs2_clear_oldest_dirty_seq(dev, bi);
635 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
636 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
637 yaffs_trace(YAFFS_TRACE_ALWAYS,
638 "yaffs: Failed to mark bad and erase block %d",
641 struct yaffs_ext_tags tags;
643 flash_block * dev->param.chunks_per_block;
645 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
647 memset(buffer, 0xff, dev->data_bytes_per_chunk);
648 yaffs_init_tags(&tags);
649 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
650 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
654 yaffs_trace(YAFFS_TRACE_ALWAYS,
655 "yaffs: Failed to write bad block marker to block %d",
658 yaffs_release_temp_buffer(dev, buffer, __LINE__);
662 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
663 bi->gc_prioritise = 0;
664 bi->needs_retiring = 0;
666 dev->n_retired_blocks++;
669 /*---------------- Name handling functions ------------*/
671 static u16 yaffs_calc_name_sum(const YCHAR *name)
679 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
681 /* 0x1f mask is case insensitive */
682 sum += ((*name) & 0x1f) * i;
689 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
691 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
692 memset(obj->short_name, 0, sizeof(obj->short_name));
694 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
695 YAFFS_SHORT_NAME_LENGTH)
696 strcpy(obj->short_name, name);
698 obj->short_name[0] = _Y('\0');
700 obj->sum = yaffs_calc_name_sum(name);
703 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
704 const struct yaffs_obj_hdr *oh)
706 #ifdef CONFIG_YAFFS_AUTO_UNICODE
707 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
708 memset(tmp_name, 0, sizeof(tmp_name));
709 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
710 YAFFS_MAX_NAME_LENGTH + 1);
711 yaffs_set_obj_name(obj, tmp_name);
713 yaffs_set_obj_name(obj, oh->name);
717 /*-------------------- TNODES -------------------
719 * List of spare tnodes
720 * The list is hooked together using the first pointer
724 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
726 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
729 memset(tn, 0, dev->tnode_size);
733 dev->checkpoint_blocks_required = 0; /* force recalculation */
738 /* FreeTnode frees up a tnode and puts it back on the free list */
739 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
741 yaffs_free_raw_tnode(dev, tn);
743 dev->checkpoint_blocks_required = 0; /* force recalculation */
746 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
748 yaffs_deinit_raw_tnodes_and_objs(dev);
753 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
754 unsigned pos, unsigned val)
756 u32 *map = (u32 *) tn;
762 pos &= YAFFS_TNODES_LEVEL0_MASK;
763 val >>= dev->chunk_grp_bits;
765 bit_in_map = pos * dev->tnode_width;
766 word_in_map = bit_in_map / 32;
767 bit_in_word = bit_in_map & (32 - 1);
769 mask = dev->tnode_mask << bit_in_word;
771 map[word_in_map] &= ~mask;
772 map[word_in_map] |= (mask & (val << bit_in_word));
774 if (dev->tnode_width > (32 - bit_in_word)) {
775 bit_in_word = (32 - bit_in_word);
778 dev->tnode_mask >> bit_in_word;
779 map[word_in_map] &= ~mask;
780 map[word_in_map] |= (mask & (val >> bit_in_word));
784 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
787 u32 *map = (u32 *) tn;
793 pos &= YAFFS_TNODES_LEVEL0_MASK;
795 bit_in_map = pos * dev->tnode_width;
796 word_in_map = bit_in_map / 32;
797 bit_in_word = bit_in_map & (32 - 1);
799 val = map[word_in_map] >> bit_in_word;
801 if (dev->tnode_width > (32 - bit_in_word)) {
802 bit_in_word = (32 - bit_in_word);
804 val |= (map[word_in_map] << bit_in_word);
807 val &= dev->tnode_mask;
808 val <<= dev->chunk_grp_bits;
813 /* ------------------- End of individual tnode manipulation -----------------*/
815 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
816 * The look up tree is represented by the top tnode and the number of top_level
817 * in the tree. 0 means only the level 0 tnode is in the tree.
820 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
821 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
822 struct yaffs_file_var *file_struct,
825 struct yaffs_tnode *tn = file_struct->top;
828 int level = file_struct->top_level;
832 /* Check sane level and chunk Id */
833 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
836 if (chunk_id > YAFFS_MAX_CHUNK_ID)
839 /* First check we're tall enough (ie enough top_level) */
841 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
844 i >>= YAFFS_TNODES_INTERNAL_BITS;
848 if (required_depth > file_struct->top_level)
849 return NULL; /* Not tall enough, so we can't find it */
851 /* Traverse down to level 0 */
852 while (level > 0 && tn) {
853 tn = tn->internal[(chunk_id >>
854 (YAFFS_TNODES_LEVEL0_BITS +
856 YAFFS_TNODES_INTERNAL_BITS)) &
857 YAFFS_TNODES_INTERNAL_MASK];
864 /* add_find_tnode_0 finds the level 0 tnode if it exists,
865 * otherwise first expands the tree.
866 * This happens in two steps:
867 * 1. If the tree isn't tall enough, then make it taller.
868 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
870 * Used when modifying the tree.
872 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
873 * specified tn will be plugged into the ttree.
876 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
877 struct yaffs_file_var *file_struct,
879 struct yaffs_tnode *passed_tn)
884 struct yaffs_tnode *tn;
887 /* Check sane level and page Id */
888 if (file_struct->top_level < 0 ||
889 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
892 if (chunk_id > YAFFS_MAX_CHUNK_ID)
895 /* First check we're tall enough (ie enough top_level) */
897 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
900 x >>= YAFFS_TNODES_INTERNAL_BITS;
904 if (required_depth > file_struct->top_level) {
905 /* Not tall enough, gotta make the tree taller */
906 for (i = file_struct->top_level; i < required_depth; i++) {
908 tn = yaffs_get_tnode(dev);
911 tn->internal[0] = file_struct->top;
912 file_struct->top = tn;
913 file_struct->top_level++;
915 yaffs_trace(YAFFS_TRACE_ERROR,
916 "yaffs: no more tnodes");
922 /* Traverse down to level 0, adding anything we need */
924 l = file_struct->top_level;
925 tn = file_struct->top;
928 while (l > 0 && tn) {
930 (YAFFS_TNODES_LEVEL0_BITS +
931 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
932 YAFFS_TNODES_INTERNAL_MASK;
934 if ((l > 1) && !tn->internal[x]) {
935 /* Add missing non-level-zero tnode */
936 tn->internal[x] = yaffs_get_tnode(dev);
937 if (!tn->internal[x])
940 /* Looking from level 1 at level 0 */
942 /* If we already have one, release it */
944 yaffs_free_tnode(dev,
946 tn->internal[x] = passed_tn;
948 } else if (!tn->internal[x]) {
949 /* Don't have one, none passed in */
950 tn->internal[x] = yaffs_get_tnode(dev);
951 if (!tn->internal[x])
956 tn = tn->internal[x];
962 memcpy(tn, passed_tn,
963 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
964 yaffs_free_tnode(dev, passed_tn);
971 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
974 return (tags->chunk_id == chunk_obj &&
975 tags->obj_id == obj_id &&
976 !tags->is_deleted) ? 1 : 0;
980 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
981 struct yaffs_ext_tags *tags, int obj_id,
986 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
987 if (yaffs_check_chunk_bit
988 (dev, the_chunk / dev->param.chunks_per_block,
989 the_chunk % dev->param.chunks_per_block)) {
991 if (dev->chunk_grp_size == 1)
994 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
996 if (yaffs_tags_match(tags,
997 obj_id, inode_chunk)) {
1008 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1009 struct yaffs_ext_tags *tags)
1011 /*Get the Tnode, then get the level 0 offset chunk offset */
1012 struct yaffs_tnode *tn;
1014 struct yaffs_ext_tags local_tags;
1016 struct yaffs_dev *dev = in->my_dev;
1019 /* Passed a NULL, so use our own tags space */
1023 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1028 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1030 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1035 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1036 struct yaffs_ext_tags *tags)
1038 /* Get the Tnode, then get the level 0 offset chunk offset */
1039 struct yaffs_tnode *tn;
1041 struct yaffs_ext_tags local_tags;
1042 struct yaffs_dev *dev = in->my_dev;
1046 /* Passed a NULL, so use our own tags space */
1050 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1055 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1057 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1060 /* Delete the entry in the filestructure (if found) */
1062 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1067 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1068 int nand_chunk, int in_scan)
1070 /* NB in_scan is zero unless scanning.
1071 * For forward scanning, in_scan is > 0;
1072 * for backward scanning in_scan is < 0
1074 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1077 struct yaffs_tnode *tn;
1078 struct yaffs_dev *dev = in->my_dev;
1080 struct yaffs_ext_tags existing_tags;
1081 struct yaffs_ext_tags new_tags;
1082 unsigned existing_serial, new_serial;
1084 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1085 /* Just ignore an attempt at putting a chunk into a non-file
1087 * If it is not during Scanning then something went wrong!
1090 yaffs_trace(YAFFS_TRACE_ERROR,
1091 "yaffs tragedy:attempt to put data chunk into a non-file"
1096 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1100 tn = yaffs_add_find_tnode_0(dev,
1101 &in->variant.file_variant,
1107 /* Dummy insert, bail now */
1110 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1113 /* If we're scanning then we need to test for duplicates
1114 * NB This does not need to be efficient since it should only
1115 * happen when the power fails during a write, then only one
1116 * chunk should ever be affected.
1118 * Correction for YAFFS2: This could happen quite a lot and we
1119 * need to think about efficiency! TODO
1120 * Update: For backward scanning we don't need to re-read tags
1121 * so this is quite cheap.
1124 if (existing_cunk > 0) {
1125 /* NB Right now existing chunk will not be real
1126 * chunk_id if the chunk group size > 1
1127 * thus we have to do a FindChunkInFile to get the
1130 * We have a duplicate now we need to decide which
1133 * Backwards scanning YAFFS2: The old one is what
1134 * we use, dump the new one.
1135 * YAFFS1: Get both sets of tags and compare serial
1140 /* Only do this for forward scanning */
1141 yaffs_rd_chunk_tags_nand(dev,
1145 /* Do a proper find */
1147 yaffs_find_chunk_in_file(in, inode_chunk,
1151 if (existing_cunk <= 0) {
1152 /*Hoosterman - how did this happen? */
1154 yaffs_trace(YAFFS_TRACE_ERROR,
1155 "yaffs tragedy: existing chunk < 0 in scan"
1160 /* NB The deleted flags should be false, otherwise
1161 * the chunks will not be loaded during a scan
1165 new_serial = new_tags.serial_number;
1166 existing_serial = existing_tags.serial_number;
1169 if ((in_scan > 0) &&
1170 (existing_cunk <= 0 ||
1171 ((existing_serial + 1) & 3) == new_serial)) {
1172 /* Forward scanning.
1174 * Delete the old one and drop through to
1177 yaffs_chunk_del(dev, existing_cunk, 1,
1180 /* Backward scanning or we want to use the
1182 * Delete the new one and return early so that
1183 * the tnode isn't changed
1185 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1192 if (existing_cunk == 0)
1193 in->n_data_chunks++;
1195 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1200 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1202 struct yaffs_block_info *the_block;
1205 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1207 block_no = chunk / dev->param.chunks_per_block;
1208 the_block = yaffs_get_block_info(dev, block_no);
1210 the_block->soft_del_pages++;
1211 dev->n_free_chunks++;
1212 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1216 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1217 * the chunks in the file.
1218 * All soft deleting does is increment the block's softdelete count and pulls
1219 * the chunk out of the tnode.
1220 * Thus, essentially this is the same as DeleteWorker except that the chunks
1224 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1225 u32 level, int chunk_offset)
1230 struct yaffs_dev *dev = in->my_dev;
1236 for (i = YAFFS_NTNODES_INTERNAL - 1;
1239 if (tn->internal[i]) {
1241 yaffs_soft_del_worker(in,
1245 YAFFS_TNODES_INTERNAL_BITS)
1248 yaffs_free_tnode(dev,
1250 tn->internal[i] = NULL;
1252 /* Can this happen? */
1256 return (all_done) ? 1 : 0;
1260 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1261 the_chunk = yaffs_get_group_base(dev, tn, i);
1263 yaffs_soft_del_chunk(dev, the_chunk);
1264 yaffs_load_tnode_0(dev, tn, i, 0);
1270 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1272 struct yaffs_dev *dev = obj->my_dev;
1273 struct yaffs_obj *parent;
1275 yaffs_verify_obj_in_dir(obj);
1276 parent = obj->parent;
1278 yaffs_verify_dir(parent);
1280 if (dev && dev->param.remove_obj_fn)
1281 dev->param.remove_obj_fn(obj);
1283 list_del_init(&obj->siblings);
1286 yaffs_verify_dir(parent);
1289 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1292 yaffs_trace(YAFFS_TRACE_ALWAYS,
1293 "tragedy: Trying to add an object to a null pointer directory"
1298 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1299 yaffs_trace(YAFFS_TRACE_ALWAYS,
1300 "tragedy: Trying to add an object to a non-directory"
1305 if (obj->siblings.prev == NULL) {
1306 /* Not initialised */
1310 yaffs_verify_dir(directory);
1312 yaffs_remove_obj_from_dir(obj);
1315 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1316 obj->parent = directory;
1318 if (directory == obj->my_dev->unlinked_dir
1319 || directory == obj->my_dev->del_dir) {
1321 obj->my_dev->n_unlinked_files++;
1322 obj->rename_allowed = 0;
1325 yaffs_verify_dir(directory);
1326 yaffs_verify_obj_in_dir(obj);
1329 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1330 struct yaffs_obj *new_dir,
1331 const YCHAR *new_name, int force, int shadows)
1335 struct yaffs_obj *existing_target;
1337 if (new_dir == NULL)
1338 new_dir = obj->parent; /* use the old directory */
1340 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1341 yaffs_trace(YAFFS_TRACE_ALWAYS,
1342 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1347 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1348 del_op = (new_dir == obj->my_dev->del_dir);
1350 existing_target = yaffs_find_by_name(new_dir, new_name);
1352 /* If the object is a file going into the unlinked directory,
1353 * then it is OK to just stuff it in since duplicate names are OK.
1354 * else only proceed if the new name does not exist and we're putting
1355 * it into a directory.
1357 if (!(unlink_op || del_op || force ||
1358 shadows > 0 || !existing_target) ||
1359 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1362 yaffs_set_obj_name(obj, new_name);
1364 yaffs_add_obj_to_dir(new_dir, obj);
1369 /* If it is a deletion then we mark it as a shrink for gc */
1370 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1376 /*------------------------ Short Operations Cache ------------------------------
1377 * In many situations where there is no high level buffering a lot of
1378 * reads might be short sequential reads, and a lot of writes may be short
1379 * sequential writes. eg. scanning/writing a jpeg file.
1380 * In these cases, a short read/write cache can provide a huge perfomance
1381 * benefit with dumb-as-a-rock code.
1382 * In Linux, the page cache provides read buffering and the short op cache
1383 * provides write buffering.
1385 * There are a small number (~10) of cache chunks per device so that we don't
1386 * need a very intelligent search.
1389 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1391 struct yaffs_dev *dev = obj->my_dev;
1393 struct yaffs_cache *cache;
1394 int n_caches = obj->my_dev->param.n_caches;
1396 for (i = 0; i < n_caches; i++) {
1397 cache = &dev->cache[i];
1398 if (cache->object == obj && cache->dirty)
1405 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1407 struct yaffs_dev *dev = obj->my_dev;
1408 int lowest = -99; /* Stop compiler whining. */
1410 struct yaffs_cache *cache;
1411 int chunk_written = 0;
1412 int n_caches = obj->my_dev->param.n_caches;
1419 /* Find the lowest dirty chunk for this object */
1420 for (i = 0; i < n_caches; i++) {
1421 if (dev->cache[i].object == obj &&
1422 dev->cache[i].dirty) {
1424 dev->cache[i].chunk_id < lowest) {
1425 cache = &dev->cache[i];
1426 lowest = cache->chunk_id;
1431 if (cache && !cache->locked) {
1432 /* Write it out and free it up */
1434 yaffs_wr_data_obj(cache->object,
1439 cache->object = NULL;
1441 } while (cache && chunk_written > 0);
1444 /* Hoosterman, disk full while writing cache out. */
1445 yaffs_trace(YAFFS_TRACE_ERROR,
1446 "yaffs tragedy: no space during cache write");
1449 /*yaffs_flush_whole_cache(dev)
1454 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1456 struct yaffs_obj *obj;
1457 int n_caches = dev->param.n_caches;
1460 /* Find a dirty object in the cache and flush it...
1461 * until there are no further dirty objects.
1465 for (i = 0; i < n_caches && !obj; i++) {
1466 if (dev->cache[i].object && dev->cache[i].dirty)
1467 obj = dev->cache[i].object;
1470 yaffs_flush_file_cache(obj);
1475 /* Grab us a cache chunk for use.
1476 * First look for an empty one.
1477 * Then look for the least recently used non-dirty one.
1478 * Then look for the least recently used dirty one...., flush and look again.
1480 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1484 if (dev->param.n_caches > 0) {
1485 for (i = 0; i < dev->param.n_caches; i++) {
1486 if (!dev->cache[i].object)
1487 return &dev->cache[i];
1493 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1495 struct yaffs_cache *cache;
1496 struct yaffs_obj *the_obj;
1501 if (dev->param.n_caches < 1)
1504 /* Try find a non-dirty one... */
1506 cache = yaffs_grab_chunk_worker(dev);
1509 /* They were all dirty, find the LRU object and flush
1510 * its cache, then find again.
1511 * NB what's here is not very accurate,
1512 * we actually flush the object with the LRU chunk.
1515 /* With locking we can't assume we can use entry zero,
1516 * Set the_obj to a valid pointer for Coverity. */
1517 the_obj = dev->cache[0].object;
1522 for (i = 0; i < dev->param.n_caches; i++) {
1523 if (dev->cache[i].object &&
1524 !dev->cache[i].locked &&
1525 (dev->cache[i].last_use < usage ||
1527 usage = dev->cache[i].last_use;
1528 the_obj = dev->cache[i].object;
1529 cache = &dev->cache[i];
1534 if (!cache || cache->dirty) {
1535 /* Flush and try again */
1536 yaffs_flush_file_cache(the_obj);
1537 cache = yaffs_grab_chunk_worker(dev);
1543 /* Find a cached chunk */
1544 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1547 struct yaffs_dev *dev = obj->my_dev;
1550 if (dev->param.n_caches < 1)
1553 for (i = 0; i < dev->param.n_caches; i++) {
1554 if (dev->cache[i].object == obj &&
1555 dev->cache[i].chunk_id == chunk_id) {
1558 return &dev->cache[i];
1564 /* Mark the chunk for the least recently used algorithym */
1565 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1570 if (dev->param.n_caches < 1)
1573 if (dev->cache_last_use < 0 ||
1574 dev->cache_last_use > 100000000) {
1575 /* Reset the cache usages */
1576 for (i = 1; i < dev->param.n_caches; i++)
1577 dev->cache[i].last_use = 0;
1579 dev->cache_last_use = 0;
1581 dev->cache_last_use++;
1582 cache->last_use = dev->cache_last_use;
1588 /* Invalidate a single cache page.
1589 * Do this when a whole page gets written,
1590 * ie the short cache for this page is no longer valid.
1592 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1594 struct yaffs_cache *cache;
1596 if (object->my_dev->param.n_caches > 0) {
1597 cache = yaffs_find_chunk_cache(object, chunk_id);
1600 cache->object = NULL;
1604 /* Invalidate all the cache pages associated with this object
1605 * Do this whenever ther file is deleted or resized.
1607 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1610 struct yaffs_dev *dev = in->my_dev;
1612 if (dev->param.n_caches > 0) {
1613 /* Invalidate it. */
1614 for (i = 0; i < dev->param.n_caches; i++) {
1615 if (dev->cache[i].object == in)
1616 dev->cache[i].object = NULL;
1621 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1624 struct yaffs_dev *dev = obj->my_dev;
1626 /* If it is still linked into the bucket list, free from the list */
1627 if (!list_empty(&obj->hash_link)) {
1628 list_del_init(&obj->hash_link);
1629 bucket = yaffs_hash_fn(obj->obj_id);
1630 dev->obj_bucket[bucket].count--;
1634 /* FreeObject frees up a Object and puts it back on the free list */
1635 static void yaffs_free_obj(struct yaffs_obj *obj)
1637 struct yaffs_dev *dev;
1644 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1645 obj, obj->my_inode);
1648 if (!list_empty(&obj->siblings))
1651 if (obj->my_inode) {
1652 /* We're still hooked up to a cached inode.
1653 * Don't delete now, but mark for later deletion
1655 obj->defered_free = 1;
1659 yaffs_unhash_obj(obj);
1661 yaffs_free_raw_obj(dev, obj);
1663 dev->checkpoint_blocks_required = 0; /* force recalculation */
1666 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1668 if (obj->defered_free)
1669 yaffs_free_obj(obj);
1672 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1674 /* Iinvalidate the file's data in the cache, without flushing. */
1675 yaffs_invalidate_whole_cache(in);
1677 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1678 /* Move to unlinked directory so we have a deletion record */
1679 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1683 yaffs_remove_obj_from_dir(in);
1684 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1692 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1694 if (!obj->deleted ||
1695 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1699 if (obj->n_data_chunks <= 0) {
1700 /* Empty file with no duplicate object headers,
1701 * just delete it immediately */
1702 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1703 obj->variant.file_variant.top = NULL;
1704 yaffs_trace(YAFFS_TRACE_TRACING,
1705 "yaffs: Deleting empty file %d",
1707 yaffs_generic_obj_del(obj);
1709 yaffs_soft_del_worker(obj,
1710 obj->variant.file_variant.top,
1712 file_variant.top_level, 0);
1717 /* Pruning removes any part of the file structure tree that is beyond the
1718 * bounds of the file (ie that does not point to chunks).
1720 * A file should only get pruned when its size is reduced.
1722 * Before pruning, the chunks must be pulled from the tree and the
1723 * level 0 tnode entries must be zeroed out.
1724 * Could also use this for file deletion, but that's probably better handled
1725 * by a special case.
1727 * This function is recursive. For levels > 0 the function is called again on
1728 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1729 * If there is no data in a subtree then it is pruned.
1732 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1733 struct yaffs_tnode *tn, u32 level,
1745 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1746 if (tn->internal[i]) {
1748 yaffs_prune_worker(dev,
1751 (i == 0) ? del0 : 1);
1754 if (tn->internal[i])
1758 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1759 u32 *map = (u32 *) tn;
1761 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1767 if (has_data == 0 && del0) {
1768 /* Free and return NULL */
1769 yaffs_free_tnode(dev, tn);
1775 static int yaffs_prune_tree(struct yaffs_dev *dev,
1776 struct yaffs_file_var *file_struct)
1781 struct yaffs_tnode *tn;
1783 if (file_struct->top_level < 1)
1787 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1789 /* Now we have a tree with all the non-zero branches NULL but
1790 * the height is the same as it was.
1791 * Let's see if we can trim internal tnodes to shorten the tree.
1792 * We can do this if only the 0th element in the tnode is in use
1793 * (ie all the non-zero are NULL)
1796 while (file_struct->top_level && !done) {
1797 tn = file_struct->top;
1800 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1801 if (tn->internal[i])
1806 file_struct->top = tn->internal[0];
1807 file_struct->top_level--;
1808 yaffs_free_tnode(dev, tn);
1817 /*-------------------- End of File Structure functions.-------------------*/
1819 /* alloc_empty_obj gets us a clean Object.*/
1820 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1822 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1829 /* Now sweeten it up... */
1831 memset(obj, 0, sizeof(struct yaffs_obj));
1832 obj->being_created = 1;
1836 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1837 INIT_LIST_HEAD(&(obj->hard_links));
1838 INIT_LIST_HEAD(&(obj->hash_link));
1839 INIT_LIST_HEAD(&obj->siblings);
1841 /* Now make the directory sane */
1842 if (dev->root_dir) {
1843 obj->parent = dev->root_dir;
1844 list_add(&(obj->siblings),
1845 &dev->root_dir->variant.dir_variant.children);
1848 /* Add it to the lost and found directory.
1849 * NB Can't put root or lost-n-found in lost-n-found so
1850 * check if lost-n-found exists first
1852 if (dev->lost_n_found)
1853 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1855 obj->being_created = 0;
1857 dev->checkpoint_blocks_required = 0; /* force recalculation */
1862 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1866 int lowest = 999999;
1868 /* Search for the shortest list or one that
1872 for (i = 0; i < 10 && lowest > 4; i++) {
1873 dev->bucket_finder++;
1874 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1875 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1876 lowest = dev->obj_bucket[dev->bucket_finder].count;
1877 l = dev->bucket_finder;
1884 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1886 int bucket = yaffs_find_nice_bucket(dev);
1888 struct list_head *i;
1889 u32 n = (u32) bucket;
1891 /* Now find an object value that has not already been taken
1892 * by scanning the list.
1897 n += YAFFS_NOBJECT_BUCKETS;
1898 if (1 || dev->obj_bucket[bucket].count > 0) {
1899 list_for_each(i, &dev->obj_bucket[bucket].list) {
1900 /* If there is already one in the list */
1901 if (i && list_entry(i, struct yaffs_obj,
1902 hash_link)->obj_id == n) {
1911 static void yaffs_hash_obj(struct yaffs_obj *in)
1913 int bucket = yaffs_hash_fn(in->obj_id);
1914 struct yaffs_dev *dev = in->my_dev;
1916 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1917 dev->obj_bucket[bucket].count++;
1920 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1922 int bucket = yaffs_hash_fn(number);
1923 struct list_head *i;
1924 struct yaffs_obj *in;
1926 list_for_each(i, &dev->obj_bucket[bucket].list) {
1927 /* Look if it is in the list */
1928 in = list_entry(i, struct yaffs_obj, hash_link);
1929 if (in->obj_id == number) {
1930 /* Don't show if it is defered free */
1931 if (in->defered_free)
1940 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1941 enum yaffs_obj_type type)
1943 struct yaffs_obj *the_obj = NULL;
1944 struct yaffs_tnode *tn = NULL;
1947 number = yaffs_new_obj_id(dev);
1949 if (type == YAFFS_OBJECT_TYPE_FILE) {
1950 tn = yaffs_get_tnode(dev);
1955 the_obj = yaffs_alloc_empty_obj(dev);
1958 yaffs_free_tnode(dev, tn);
1963 the_obj->rename_allowed = 1;
1964 the_obj->unlink_allowed = 1;
1965 the_obj->obj_id = number;
1966 yaffs_hash_obj(the_obj);
1967 the_obj->variant_type = type;
1968 yaffs_load_current_time(the_obj, 1, 1);
1971 case YAFFS_OBJECT_TYPE_FILE:
1972 the_obj->variant.file_variant.file_size = 0;
1973 the_obj->variant.file_variant.scanned_size = 0;
1974 the_obj->variant.file_variant.shrink_size = ~0; /* max */
1975 the_obj->variant.file_variant.top_level = 0;
1976 the_obj->variant.file_variant.top = tn;
1978 case YAFFS_OBJECT_TYPE_DIRECTORY:
1979 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1980 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1982 case YAFFS_OBJECT_TYPE_SYMLINK:
1983 case YAFFS_OBJECT_TYPE_HARDLINK:
1984 case YAFFS_OBJECT_TYPE_SPECIAL:
1985 /* No action required */
1987 case YAFFS_OBJECT_TYPE_UNKNOWN:
1988 /* todo this should not happen */
1994 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1995 int number, u32 mode)
1998 struct yaffs_obj *obj =
1999 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2004 obj->fake = 1; /* it is fake so it might not use NAND */
2005 obj->rename_allowed = 0;
2006 obj->unlink_allowed = 0;
2009 obj->yst_mode = mode;
2011 obj->hdr_chunk = 0; /* Not a valid chunk. */
2017 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2023 yaffs_init_raw_tnodes_and_objs(dev);
2025 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2026 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2027 dev->obj_bucket[i].count = 0;
2031 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2033 enum yaffs_obj_type type)
2035 struct yaffs_obj *the_obj = NULL;
2038 the_obj = yaffs_find_by_number(dev, number);
2041 the_obj = yaffs_new_obj(dev, number, type);
2047 YCHAR *yaffs_clone_str(const YCHAR *str)
2049 YCHAR *new_str = NULL;
2055 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2056 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2058 strncpy(new_str, str, len);
2065 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2066 * link (ie. name) is created or deleted in the directory.
2069 * create dir/a : update dir's mtime/ctime
2070 * rm dir/a: update dir's mtime/ctime
2071 * modify dir/a: don't update dir's mtimme/ctime
2073 * This can be handled immediately or defered. Defering helps reduce the number
2074 * of updates when many files in a directory are changed within a brief period.
2076 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2077 * called periodically.
2080 static void yaffs_update_parent(struct yaffs_obj *obj)
2082 struct yaffs_dev *dev;
2088 yaffs_load_current_time(obj, 0, 1);
2089 if (dev->param.defered_dir_update) {
2090 struct list_head *link = &obj->variant.dir_variant.dirty;
2092 if (list_empty(link)) {
2093 list_add(link, &dev->dirty_dirs);
2094 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2095 "Added object %d to dirty directories",
2100 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2104 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2106 struct list_head *link;
2107 struct yaffs_obj *obj;
2108 struct yaffs_dir_var *d_s;
2109 union yaffs_obj_var *o_v;
2111 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2113 while (!list_empty(&dev->dirty_dirs)) {
2114 link = dev->dirty_dirs.next;
2115 list_del_init(link);
2117 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2118 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2119 obj = list_entry(o_v, struct yaffs_obj, variant);
2121 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2125 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2130 * Mknod (create) a new object.
2131 * equiv_obj only has meaning for a hard link;
2132 * alias_str only has meaning for a symlink.
2133 * rdev only has meaning for devices (a subset of special objects)
2136 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2137 struct yaffs_obj *parent,
2142 struct yaffs_obj *equiv_obj,
2143 const YCHAR *alias_str, u32 rdev)
2145 struct yaffs_obj *in;
2147 struct yaffs_dev *dev = parent->my_dev;
2149 /* Check if the entry exists.
2150 * If it does then fail the call since we don't want a dup. */
2151 if (yaffs_find_by_name(parent, name))
2154 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2155 str = yaffs_clone_str(alias_str);
2160 in = yaffs_new_obj(dev, -1, type);
2169 in->variant_type = type;
2171 in->yst_mode = mode;
2173 yaffs_attribs_init(in, gid, uid, rdev);
2175 in->n_data_chunks = 0;
2177 yaffs_set_obj_name(in, name);
2180 yaffs_add_obj_to_dir(parent, in);
2182 in->my_dev = parent->my_dev;
2185 case YAFFS_OBJECT_TYPE_SYMLINK:
2186 in->variant.symlink_variant.alias = str;
2188 case YAFFS_OBJECT_TYPE_HARDLINK:
2189 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2190 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
2191 list_add(&in->hard_links, &equiv_obj->hard_links);
2193 case YAFFS_OBJECT_TYPE_FILE:
2194 case YAFFS_OBJECT_TYPE_DIRECTORY:
2195 case YAFFS_OBJECT_TYPE_SPECIAL:
2196 case YAFFS_OBJECT_TYPE_UNKNOWN:
2201 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2202 /* Could not create the object header, fail */
2208 yaffs_update_parent(parent);
2213 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2214 const YCHAR *name, u32 mode, u32 uid,
2217 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2218 uid, gid, NULL, NULL, 0);
2221 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2222 u32 mode, u32 uid, u32 gid)
2224 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2225 mode, uid, gid, NULL, NULL, 0);
2228 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2229 const YCHAR *name, u32 mode, u32 uid,
2232 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2233 uid, gid, NULL, NULL, rdev);
2236 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2237 const YCHAR *name, u32 mode, u32 uid,
2238 u32 gid, const YCHAR *alias)
2240 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2241 uid, gid, NULL, alias, 0);
2244 /* yaffs_link_obj returns the object id of the equivalent object.*/
2245 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2246 struct yaffs_obj *equiv_obj)
2248 /* Get the real object in case we were fed a hard link obj */
2249 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2251 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2252 parent, name, 0, 0, 0,
2253 equiv_obj, NULL, 0))
2262 /*---------------------- Block Management and Page Allocation -------------*/
2264 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2266 if (dev->block_info_alt && dev->block_info)
2267 vfree(dev->block_info);
2269 kfree(dev->block_info);
2271 dev->block_info_alt = 0;
2273 dev->block_info = NULL;
2275 if (dev->chunk_bits_alt && dev->chunk_bits)
2276 vfree(dev->chunk_bits);
2278 kfree(dev->chunk_bits);
2279 dev->chunk_bits_alt = 0;
2280 dev->chunk_bits = NULL;
2283 static int yaffs_init_blocks(struct yaffs_dev *dev)
2285 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2287 dev->block_info = NULL;
2288 dev->chunk_bits = NULL;
2289 dev->alloc_block = -1; /* force it to get a new one */
2291 /* If the first allocation strategy fails, thry the alternate one */
2293 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2294 if (!dev->block_info) {
2296 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2297 dev->block_info_alt = 1;
2299 dev->block_info_alt = 0;
2302 if (!dev->block_info)
2305 /* Set up dynamic blockinfo stuff. Round up bytes. */
2306 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2308 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2309 if (!dev->chunk_bits) {
2311 vmalloc(dev->chunk_bit_stride * n_blocks);
2312 dev->chunk_bits_alt = 1;
2314 dev->chunk_bits_alt = 0;
2316 if (!dev->chunk_bits)
2320 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2321 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2325 yaffs_deinit_blocks(dev);
2330 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2332 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2336 /* If the block is still healthy erase it and mark as clean.
2337 * If the block has had a data failure, then retire it.
2340 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2341 "yaffs_block_became_dirty block %d state %d %s",
2342 block_no, bi->block_state,
2343 (bi->needs_retiring) ? "needs retiring" : "");
2345 yaffs2_clear_oldest_dirty_seq(dev, bi);
2347 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2349 /* If this is the block being garbage collected then stop gc'ing */
2350 if (block_no == dev->gc_block)
2353 /* If this block is currently the best candidate for gc
2354 * then drop as a candidate */
2355 if (block_no == dev->gc_dirtiest) {
2356 dev->gc_dirtiest = 0;
2357 dev->gc_pages_in_use = 0;
2360 if (!bi->needs_retiring) {
2361 yaffs2_checkpt_invalidate(dev);
2362 erased_ok = yaffs_erase_block(dev, block_no);
2364 dev->n_erase_failures++;
2365 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2366 "**>> Erasure failed %d", block_no);
2370 /* Verify erasure if needed */
2372 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2373 !yaffs_skip_verification(dev))) {
2374 for (i = 0; i < dev->param.chunks_per_block; i++) {
2375 if (!yaffs_check_chunk_erased(dev,
2376 block_no * dev->param.chunks_per_block + i)) {
2377 yaffs_trace(YAFFS_TRACE_ERROR,
2378 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2385 /* We lost a block of free space */
2386 dev->n_free_chunks -= dev->param.chunks_per_block;
2387 yaffs_retire_block(dev, block_no);
2388 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2389 "**>> Block %d retired", block_no);
2393 /* Clean it up... */
2394 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2396 dev->n_erased_blocks++;
2397 bi->pages_in_use = 0;
2398 bi->soft_del_pages = 0;
2399 bi->has_shrink_hdr = 0;
2400 bi->skip_erased_check = 1; /* Clean, so no need to check */
2401 bi->gc_prioritise = 0;
2402 yaffs_clear_chunk_bits(dev, block_no);
2404 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2407 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2408 struct yaffs_block_info *bi,
2409 int old_chunk, u8 *buffer)
2413 struct yaffs_ext_tags tags;
2414 struct yaffs_obj *object;
2416 int ret_val = YAFFS_OK;
2418 yaffs_init_tags(&tags);
2419 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2421 object = yaffs_find_by_number(dev, tags.obj_id);
2423 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2424 "Collecting chunk in block %d, %d %d %d ",
2425 dev->gc_chunk, tags.obj_id,
2426 tags.chunk_id, tags.n_bytes);
2428 if (object && !yaffs_skip_verification(dev)) {
2429 if (tags.chunk_id == 0)
2432 else if (object->soft_del)
2433 /* Defeat the test */
2434 matching_chunk = old_chunk;
2437 yaffs_find_chunk_in_file
2438 (object, tags.chunk_id,
2441 if (old_chunk != matching_chunk)
2442 yaffs_trace(YAFFS_TRACE_ERROR,
2443 "gc: page in gc mismatch: %d %d %d %d",
2451 yaffs_trace(YAFFS_TRACE_ERROR,
2452 "page %d in gc has no object: %d %d %d ",
2454 tags.obj_id, tags.chunk_id,
2460 object->soft_del && tags.chunk_id != 0) {
2461 /* Data chunk in a soft deleted file,
2463 * It's a soft deleted data chunk,
2464 * No need to copy this, just forget
2465 * about it and fix up the object.
2468 /* Free chunks already includes
2469 * softdeleted chunks, how ever this
2470 * chunk is going to soon be really
2471 * deleted which will increment free
2472 * chunks. We have to decrement free
2473 * chunks so this works out properly.
2475 dev->n_free_chunks--;
2476 bi->soft_del_pages--;
2478 object->n_data_chunks--;
2479 if (object->n_data_chunks <= 0) {
2480 /* remeber to clean up obj */
2481 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2485 } else if (object) {
2486 /* It's either a data chunk in a live
2487 * file or an ObjectHeader, so we're
2489 * NB Need to keep the ObjectHeaders of
2490 * deleted files until the whole file
2491 * has been deleted off
2493 tags.serial_number++;
2496 if (tags.chunk_id == 0) {
2497 /* It is an object Id,
2498 * We need to nuke the
2499 * shrinkheader flags since its
2501 * Also need to clean up
2504 struct yaffs_obj_hdr *oh;
2505 oh = (struct yaffs_obj_hdr *) buffer;
2508 tags.extra_is_shrink = 0;
2509 oh->shadows_obj = 0;
2510 oh->inband_shadowed_obj_id = 0;
2511 tags.extra_shadows = 0;
2513 /* Update file size */
2514 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2516 object->variant.file_variant.file_size;
2517 tags.extra_length = oh->file_size;
2520 yaffs_verify_oh(object, oh, &tags, 1);
2522 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2525 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2528 if (new_chunk < 0) {
2529 ret_val = YAFFS_FAIL;
2532 /* Now fix up the Tnodes etc. */
2534 if (tags.chunk_id == 0) {
2536 object->hdr_chunk = new_chunk;
2537 object->serial = tags.serial_number;
2539 /* It's a data chunk */
2540 yaffs_put_chunk_in_file(object, tags.chunk_id,
2545 if (ret_val == YAFFS_OK)
2546 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2550 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2553 int ret_val = YAFFS_OK;
2555 int is_checkpt_block;
2557 int chunks_before = yaffs_get_erased_chunks(dev);
2559 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2561 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2563 yaffs_trace(YAFFS_TRACE_TRACING,
2564 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2565 block, bi->pages_in_use, bi->has_shrink_hdr,
2568 /*yaffs_verify_free_chunks(dev); */
2570 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2571 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2573 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2575 dev->gc_disable = 1;
2577 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2578 yaffs_trace(YAFFS_TRACE_TRACING,
2579 "Collecting block %d that has no chunks in use",
2581 yaffs_block_became_dirty(dev, block);
2584 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2586 yaffs_verify_blk(dev, bi, block);
2588 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2589 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2591 for (/* init already done */ ;
2592 ret_val == YAFFS_OK &&
2593 dev->gc_chunk < dev->param.chunks_per_block &&
2594 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2596 dev->gc_chunk++, old_chunk++) {
2597 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2598 /* Page is in use and might need to be copied */
2600 ret_val = yaffs_gc_process_chunk(dev, bi,
2604 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2607 yaffs_verify_collected_blk(dev, bi, block);
2609 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2611 * The gc did not complete. Set block state back to FULL
2612 * because checkpointing does not restore gc.
2614 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2616 /* The gc completed. */
2617 /* Do any required cleanups */
2618 for (i = 0; i < dev->n_clean_ups; i++) {
2619 /* Time to delete the file too */
2620 struct yaffs_obj *object =
2621 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2623 yaffs_free_tnode(dev,
2624 object->variant.file_variant.top);
2625 object->variant.file_variant.top = NULL;
2626 yaffs_trace(YAFFS_TRACE_GC,
2627 "yaffs: About to finally delete object %d",
2629 yaffs_generic_obj_del(object);
2630 object->my_dev->n_deleted_files--;
2634 chunks_after = yaffs_get_erased_chunks(dev);
2635 if (chunks_before >= chunks_after)
2636 yaffs_trace(YAFFS_TRACE_GC,
2637 "gc did not increase free chunks before %d after %d",
2638 chunks_before, chunks_after);
2641 dev->n_clean_ups = 0;
2644 dev->gc_disable = 0;
2650 * find_gc_block() selects the dirtiest block (or close enough)
2651 * for garbage collection.
2654 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2655 int aggressive, int background)
2659 unsigned selected = 0;
2660 int prioritised = 0;
2661 int prioritised_exist = 0;
2662 struct yaffs_block_info *bi;
2665 /* First let's see if we need to grab a prioritised block */
2666 if (dev->has_pending_prioritised_gc && !aggressive) {
2667 dev->gc_dirtiest = 0;
2668 bi = dev->block_info;
2669 for (i = dev->internal_start_block;
2670 i <= dev->internal_end_block && !selected; i++) {
2672 if (bi->gc_prioritise) {
2673 prioritised_exist = 1;
2674 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2675 yaffs_block_ok_for_gc(dev, bi)) {
2684 * If there is a prioritised block and none was selected then
2685 * this happened because there is at least one old dirty block
2686 * gumming up the works. Let's gc the oldest dirty block.
2689 if (prioritised_exist &&
2690 !selected && dev->oldest_dirty_block > 0)
2691 selected = dev->oldest_dirty_block;
2693 if (!prioritised_exist) /* None found, so we can clear this */
2694 dev->has_pending_prioritised_gc = 0;
2697 /* If we're doing aggressive GC then we are happy to take a less-dirty
2698 * block, and search harder.
2699 * else (leasurely gc), then we only bother to do this if the
2700 * block has only a few pages in use.
2706 dev->internal_end_block - dev->internal_start_block + 1;
2708 threshold = dev->param.chunks_per_block;
2709 iterations = n_blocks;
2714 max_threshold = dev->param.chunks_per_block / 2;
2716 max_threshold = dev->param.chunks_per_block / 8;
2718 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2719 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2721 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2722 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2723 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2724 if (threshold > max_threshold)
2725 threshold = max_threshold;
2727 iterations = n_blocks / 16 + 1;
2728 if (iterations > 100)
2734 (dev->gc_dirtiest < 1 ||
2735 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2737 dev->gc_block_finder++;
2738 if (dev->gc_block_finder < dev->internal_start_block ||
2739 dev->gc_block_finder > dev->internal_end_block)
2740 dev->gc_block_finder =
2741 dev->internal_start_block;
2743 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2745 pages_used = bi->pages_in_use - bi->soft_del_pages;
2747 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2748 pages_used < dev->param.chunks_per_block &&
2749 (dev->gc_dirtiest < 1 ||
2750 pages_used < dev->gc_pages_in_use) &&
2751 yaffs_block_ok_for_gc(dev, bi)) {
2752 dev->gc_dirtiest = dev->gc_block_finder;
2753 dev->gc_pages_in_use = pages_used;
2757 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2758 selected = dev->gc_dirtiest;
2762 * If nothing has been selected for a while, try the oldest dirty
2763 * because that's gumming up the works.
2766 if (!selected && dev->param.is_yaffs2 &&
2767 dev->gc_not_done >= (background ? 10 : 20)) {
2768 yaffs2_find_oldest_dirty_seq(dev);
2769 if (dev->oldest_dirty_block > 0) {
2770 selected = dev->oldest_dirty_block;
2771 dev->gc_dirtiest = selected;
2772 dev->oldest_dirty_gc_count++;
2773 bi = yaffs_get_block_info(dev, selected);
2774 dev->gc_pages_in_use =
2775 bi->pages_in_use - bi->soft_del_pages;
2777 dev->gc_not_done = 0;
2782 yaffs_trace(YAFFS_TRACE_GC,
2783 "GC Selected block %d with %d free, prioritised:%d",
2785 dev->param.chunks_per_block - dev->gc_pages_in_use,
2792 dev->gc_dirtiest = 0;
2793 dev->gc_pages_in_use = 0;
2794 dev->gc_not_done = 0;
2795 if (dev->refresh_skip > 0)
2796 dev->refresh_skip--;
2799 yaffs_trace(YAFFS_TRACE_GC,
2800 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2801 dev->gc_block_finder, dev->gc_not_done, threshold,
2802 dev->gc_dirtiest, dev->gc_pages_in_use,
2803 dev->oldest_dirty_block, background ? " bg" : "");
2809 /* New garbage collector
2810 * If we're very low on erased blocks then we do aggressive garbage collection
2811 * otherwise we do "leasurely" garbage collection.
2812 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2813 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2815 * The idea is to help clear out space in a more spread-out manner.
2816 * Dunno if it really does anything useful.
2818 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2821 int gc_ok = YAFFS_OK;
2825 int checkpt_block_adjust;
2827 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2830 if (dev->gc_disable)
2831 /* Bail out so we don't get recursive gc */
2834 /* This loop should pass the first time.
2835 * Only loops here if the collection does not increase space.
2841 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2844 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2846 dev->n_erased_blocks * dev->param.chunks_per_block;
2848 /* If we need a block soon then do aggressive gc. */
2849 if (dev->n_erased_blocks < min_erased)
2853 && erased_chunks > (dev->n_free_chunks / 4))
2856 if (dev->gc_skip > 20)
2858 if (erased_chunks < dev->n_free_chunks / 2 ||
2859 dev->gc_skip < 1 || background)
2869 /* If we don't already have a block being gc'd then see if we
2870 * should start another */
2872 if (dev->gc_block < 1 && !aggressive) {
2873 dev->gc_block = yaffs2_find_refresh_block(dev);
2875 dev->n_clean_ups = 0;
2877 if (dev->gc_block < 1) {
2879 yaffs_find_gc_block(dev, aggressive, background);
2881 dev->n_clean_ups = 0;
2884 if (dev->gc_block > 0) {
2887 dev->passive_gc_count++;
2889 yaffs_trace(YAFFS_TRACE_GC,
2890 "yaffs: GC n_erased_blocks %d aggressive %d",
2891 dev->n_erased_blocks, aggressive);
2893 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2896 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
2897 dev->gc_block > 0) {
2898 yaffs_trace(YAFFS_TRACE_GC,
2899 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2900 dev->n_erased_blocks, max_tries,
2903 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2904 (dev->gc_block > 0) && (max_tries < 2));
2906 return aggressive ? gc_ok : YAFFS_OK;
2911 * Garbage collects. Intended to be called from a background thread.
2912 * Returns non-zero if at least half the free chunks are erased.
2914 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2916 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2918 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2920 yaffs_check_gc(dev, 1);
2921 return erased_chunks > dev->n_free_chunks / 2;
2924 /*-------------------- Data file manipulation -----------------*/
2926 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2928 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2930 if (nand_chunk >= 0)
2931 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2934 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2935 "Chunk %d not found zero instead",
2937 /* get sane (zero) data if you read a hole */
2938 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2944 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2949 struct yaffs_ext_tags tags;
2950 struct yaffs_block_info *bi;
2956 block = chunk_id / dev->param.chunks_per_block;
2957 page = chunk_id % dev->param.chunks_per_block;
2959 if (!yaffs_check_chunk_bit(dev, block, page))
2960 yaffs_trace(YAFFS_TRACE_VERIFY,
2961 "Deleting invalid chunk %d", chunk_id);
2963 bi = yaffs_get_block_info(dev, block);
2965 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2967 yaffs_trace(YAFFS_TRACE_DELETION,
2968 "line %d delete of chunk %d",
2971 if (!dev->param.is_yaffs2 && mark_flash &&
2972 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2974 yaffs_init_tags(&tags);
2975 tags.is_deleted = 1;
2976 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2977 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2979 dev->n_unmarked_deletions++;
2982 /* Pull out of the management area.
2983 * If the whole block became dirty, this will kick off an erasure.
2985 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2986 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2987 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
2988 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2989 dev->n_free_chunks++;
2990 yaffs_clear_chunk_bit(dev, block, page);
2993 if (bi->pages_in_use == 0 &&
2994 !bi->has_shrink_hdr &&
2995 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2996 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
2997 yaffs_block_became_dirty(dev, block);
3002 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3003 const u8 *buffer, int n_bytes, int use_reserve)
3005 /* Find old chunk Need to do this to get serial number
3006 * Write new one and patch into tree.
3007 * Invalidate old tags.
3011 struct yaffs_ext_tags prev_tags;
3013 struct yaffs_ext_tags new_tags;
3014 struct yaffs_dev *dev = in->my_dev;
3016 yaffs_check_gc(dev, 0);
3018 /* Get the previous chunk at this location in the file if it exists.
3019 * If it does not exist then put a zero into the tree. This creates
3020 * the tnode now, rather than later when it is harder to clean up.
3022 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3023 if (prev_chunk_id < 1 &&
3024 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3027 /* Set up new tags */
3028 yaffs_init_tags(&new_tags);
3030 new_tags.chunk_id = inode_chunk;
3031 new_tags.obj_id = in->obj_id;
3032 new_tags.serial_number =
3033 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3034 new_tags.n_bytes = n_bytes;
3036 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3037 yaffs_trace(YAFFS_TRACE_ERROR,
3038 "Writing %d bytes to chunk!!!!!!!!!",
3044 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3046 if (new_chunk_id > 0) {
3047 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3049 if (prev_chunk_id > 0)
3050 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3052 yaffs_verify_file_sane(in);
3054 return new_chunk_id;
3060 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3061 const YCHAR *name, const void *value, int size,
3064 struct yaffs_xattr_mod xmod;
3072 xmod.result = -ENOSPC;
3074 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3082 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3083 struct yaffs_xattr_mod *xmod)
3086 int x_offs = sizeof(struct yaffs_obj_hdr);
3087 struct yaffs_dev *dev = obj->my_dev;
3088 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3089 char *x_buffer = buffer + x_offs;
3093 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3094 xmod->size, xmod->flags);
3096 retval = nval_del(x_buffer, x_size, xmod->name);
3098 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3099 obj->xattr_known = 1;
3100 xmod->result = retval;
3105 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
3106 void *value, int size)
3108 char *buffer = NULL;
3110 struct yaffs_ext_tags tags;
3111 struct yaffs_dev *dev = obj->my_dev;
3112 int x_offs = sizeof(struct yaffs_obj_hdr);
3113 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3117 if (obj->hdr_chunk < 1)
3120 /* If we know that the object has no xattribs then don't do all the
3121 * reading and parsing.
3123 if (obj->xattr_known && !obj->has_xattr) {
3130 buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
3135 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3137 if (result != YAFFS_OK)
3140 x_buffer = buffer + x_offs;
3142 if (!obj->xattr_known) {
3143 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3144 obj->xattr_known = 1;
3148 retval = nval_get(x_buffer, x_size, name, value, size);
3150 retval = nval_list(x_buffer, x_size, value, size);
3152 yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
3156 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3157 const void *value, int size, int flags)
3159 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3162 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3164 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3167 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3170 return yaffs_do_xattrib_fetch(obj, name, value, size);
3173 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3175 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3178 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3181 struct yaffs_obj_hdr *oh;
3182 struct yaffs_dev *dev;
3183 struct yaffs_ext_tags tags;
3185 int alloc_failed = 0;
3187 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3191 in->lazy_loaded = 0;
3192 buf = yaffs_get_temp_buffer(dev, __LINE__);
3194 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3195 oh = (struct yaffs_obj_hdr *)buf;
3197 in->yst_mode = oh->yst_mode;
3198 yaffs_load_attribs(in, oh);
3199 yaffs_set_obj_name_from_oh(in, oh);
3201 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3202 in->variant.symlink_variant.alias =
3203 yaffs_clone_str(oh->alias);
3204 if (!in->variant.symlink_variant.alias)
3205 alloc_failed = 1; /* Not returned */
3207 yaffs_release_temp_buffer(dev, buf, __LINE__);
3210 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
3211 const YCHAR *oh_name, int buff_size)
3213 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3214 if (dev->param.auto_unicode) {
3216 /* It is an ASCII name, do an ASCII to
3217 * unicode conversion */
3218 const char *ascii_oh_name = (const char *)oh_name;
3219 int n = buff_size - 1;
3220 while (n > 0 && *ascii_oh_name) {
3221 *name = *ascii_oh_name;
3227 strncpy(name, oh_name + 1, buff_size - 1);
3233 strncpy(name, oh_name, buff_size - 1);
3237 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
3240 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3245 if (dev->param.auto_unicode) {
3250 /* Figure out if the name will fit in ascii character set */
3251 while (is_ascii && *w) {
3258 /* It is an ASCII name, so convert unicode to ascii */
3259 char *ascii_oh_name = (char *)oh_name;
3260 int n = YAFFS_MAX_NAME_LENGTH - 1;
3261 while (n > 0 && *name) {
3262 *ascii_oh_name = *name;
3268 /* Unicode name, so save starting at the second YCHAR */
3270 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
3276 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3280 /* UpdateObjectHeader updates the header on NAND for an object.
3281 * If name is not NULL, then that new name is used.
3283 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3284 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3287 struct yaffs_block_info *bi;
3288 struct yaffs_dev *dev = in->my_dev;
3293 struct yaffs_ext_tags new_tags;
3294 struct yaffs_ext_tags old_tags;
3295 const YCHAR *alias = NULL;
3297 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3298 struct yaffs_obj_hdr *oh = NULL;
3300 strcpy(old_name, _Y("silly old name"));
3302 if (in->fake && in != dev->root_dir && !force && !xmod)
3305 yaffs_check_gc(dev, 0);
3306 yaffs_check_obj_details_loaded(in);
3308 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
3309 oh = (struct yaffs_obj_hdr *)buffer;
3311 prev_chunk_id = in->hdr_chunk;
3313 if (prev_chunk_id > 0) {
3314 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3317 yaffs_verify_oh(in, oh, &old_tags, 0);
3318 memcpy(old_name, oh->name, sizeof(oh->name));
3319 memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
3321 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
3324 oh->type = in->variant_type;
3325 oh->yst_mode = in->yst_mode;
3326 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3328 yaffs_load_attribs_oh(oh, in);
3331 oh->parent_obj_id = in->parent->obj_id;
3333 oh->parent_obj_id = 0;
3335 if (name && *name) {
3336 memset(oh->name, 0, sizeof(oh->name));
3337 yaffs_load_oh_from_name(dev, oh->name, name);
3338 } else if (prev_chunk_id > 0) {
3339 memcpy(oh->name, old_name, sizeof(oh->name));
3341 memset(oh->name, 0, sizeof(oh->name));
3344 oh->is_shrink = is_shrink;
3346 switch (in->variant_type) {
3347 case YAFFS_OBJECT_TYPE_UNKNOWN:
3348 /* Should not happen */
3350 case YAFFS_OBJECT_TYPE_FILE:
3352 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED ||
3353 oh->parent_obj_id == YAFFS_OBJECTID_UNLINKED) ?
3354 0 : in->variant.file_variant.file_size;
3356 case YAFFS_OBJECT_TYPE_HARDLINK:
3357 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3359 case YAFFS_OBJECT_TYPE_SPECIAL:
3362 case YAFFS_OBJECT_TYPE_DIRECTORY:
3365 case YAFFS_OBJECT_TYPE_SYMLINK:
3366 alias = in->variant.symlink_variant.alias;
3368 alias = _Y("no alias");
3369 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3370 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3374 /* process any xattrib modifications */
3376 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3379 yaffs_init_tags(&new_tags);
3381 new_tags.chunk_id = 0;
3382 new_tags.obj_id = in->obj_id;
3383 new_tags.serial_number = in->serial;
3385 /* Add extra info for file header */
3386 new_tags.extra_available = 1;
3387 new_tags.extra_parent_id = oh->parent_obj_id;
3388 new_tags.extra_length = oh->file_size;
3389 new_tags.extra_is_shrink = oh->is_shrink;
3390 new_tags.extra_equiv_id = oh->equiv_id;
3391 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3392 new_tags.extra_obj_type = in->variant_type;
3393 yaffs_verify_oh(in, oh, &new_tags, 1);
3395 /* Create new chunk in NAND */
3397 yaffs_write_new_chunk(dev, buffer, &new_tags,
3398 (prev_chunk_id > 0) ? 1 : 0);
3401 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3403 if (new_chunk_id < 0)
3404 return new_chunk_id;
3406 in->hdr_chunk = new_chunk_id;
3408 if (prev_chunk_id > 0)
3409 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3411 if (!yaffs_obj_cache_dirty(in))
3414 /* If this was a shrink, then mark the block
3415 * that the chunk lives on */
3417 bi = yaffs_get_block_info(in->my_dev,
3419 in->my_dev->param.chunks_per_block);
3420 bi->has_shrink_hdr = 1;
3424 return new_chunk_id;
3427 /*--------------------- File read/write ------------------------
3428 * Read and write have very similar structures.
3429 * In general the read/write has three parts to it
3430 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3431 * Some complete chunks
3432 * An incomplete chunk to end off with
3434 * Curve-balls: the first chunk might also be the last chunk.
3437 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3444 struct yaffs_cache *cache;
3445 struct yaffs_dev *dev;
3450 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3453 /* OK now check for the curveball where the start and end are in
3456 if ((start + n) < dev->data_bytes_per_chunk)
3459 n_copy = dev->data_bytes_per_chunk - start;
3461 cache = yaffs_find_chunk_cache(in, chunk);
3463 /* If the chunk is already in the cache or it is less than
3464 * a whole chunk or we're using inband tags then use the cache
3465 * (if there is caching) else bypass the cache.
3467 if (cache || n_copy != dev->data_bytes_per_chunk ||
3468 dev->param.inband_tags) {
3469 if (dev->param.n_caches > 0) {
3471 /* If we can't find the data in the cache,
3472 * then load it up. */
3476 yaffs_grab_chunk_cache(in->my_dev);
3478 cache->chunk_id = chunk;
3481 yaffs_rd_data_obj(in, chunk,
3486 yaffs_use_cache(dev, cache, 0);
3490 memcpy(buffer, &cache->data[start], n_copy);
3494 /* Read into the local buffer then copy.. */
3497 yaffs_get_temp_buffer(dev, __LINE__);
3498 yaffs_rd_data_obj(in, chunk, local_buffer);
3500 memcpy(buffer, &local_buffer[start], n_copy);
3502 yaffs_release_temp_buffer(dev, local_buffer,
3506 /* A full chunk. Read directly into the buffer. */
3507 yaffs_rd_data_obj(in, chunk, buffer);
3517 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3518 int n_bytes, int write_trhrough)
3527 int start_write = offset;
3528 int chunk_written = 0;
3531 struct yaffs_dev *dev;
3535 while (n > 0 && chunk_written >= 0) {
3536 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3538 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3539 start >= dev->data_bytes_per_chunk) {
3540 yaffs_trace(YAFFS_TRACE_ERROR,
3541 "AddrToChunk of offset %d gives chunk %d start %d",
3542 (int)offset, chunk, start);
3544 chunk++; /* File pos to chunk in file offset */
3546 /* OK now check for the curveball where the start and end are in
3550 if ((start + n) < dev->data_bytes_per_chunk) {
3553 /* Now calculate how many bytes to write back....
3554 * If we're overwriting and not writing to then end of
3555 * file then we need to write back as much as was there
3559 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3561 if (chunk_start > in->variant.file_variant.file_size)
3562 n_bytes_read = 0; /* Past end of file */
3565 in->variant.file_variant.file_size -
3568 if (n_bytes_read > dev->data_bytes_per_chunk)
3569 n_bytes_read = dev->data_bytes_per_chunk;
3573 (start + n)) ? n_bytes_read : (start + n);
3575 if (n_writeback < 0 ||
3576 n_writeback > dev->data_bytes_per_chunk)
3580 n_copy = dev->data_bytes_per_chunk - start;
3581 n_writeback = dev->data_bytes_per_chunk;
3584 if (n_copy != dev->data_bytes_per_chunk ||
3585 dev->param.inband_tags) {
3586 /* An incomplete start or end chunk (or maybe both
3587 * start and end chunk), or we're using inband tags,
3588 * so we want to use the cache buffers.
3590 if (dev->param.n_caches > 0) {
3591 struct yaffs_cache *cache;
3593 /* If we can't find the data in the cache, then
3595 cache = yaffs_find_chunk_cache(in, chunk);
3598 yaffs_check_alloc_available(dev, 1)) {
3599 cache = yaffs_grab_chunk_cache(dev);
3601 cache->chunk_id = chunk;
3604 yaffs_rd_data_obj(in, chunk,
3608 !yaffs_check_alloc_available(dev,
3610 /* Drop the cache if it was a read cache
3611 * item and no space check has been made
3618 yaffs_use_cache(dev, cache, 1);
3621 memcpy(&cache->data[start], buffer,
3625 cache->n_bytes = n_writeback;
3627 if (write_trhrough) {
3637 chunk_written = -1; /* fail write */
3640 /* An incomplete start or end chunk (or maybe
3641 * both start and end chunk). Read into the
3642 * local buffer then copy over and write back.
3646 yaffs_get_temp_buffer(dev, __LINE__);
3648 yaffs_rd_data_obj(in, chunk, local_buffer);
3649 memcpy(&local_buffer[start], buffer, n_copy);
3652 yaffs_wr_data_obj(in, chunk,
3656 yaffs_release_temp_buffer(dev, local_buffer,
3660 /* A full chunk. Write directly from the buffer. */
3663 yaffs_wr_data_obj(in, chunk, buffer,
3664 dev->data_bytes_per_chunk, 0);
3666 /* Since we've overwritten the cached data,
3667 * we better invalidate it. */
3668 yaffs_invalidate_chunk_cache(in, chunk);
3671 if (chunk_written >= 0) {
3679 /* Update file object */
3681 if ((start_write + n_done) > in->variant.file_variant.file_size)
3682 in->variant.file_variant.file_size = (start_write + n_done);
3688 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3689 int n_bytes, int write_trhrough)
3691 yaffs2_handle_hole(in, offset);
3692 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3695 /* ---------------------- File resizing stuff ------------------ */
3697 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3700 struct yaffs_dev *dev = in->my_dev;
3701 int old_size = in->variant.file_variant.file_size;
3704 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3705 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3706 dev->data_bytes_per_chunk;
3709 /* Delete backwards so that we don't end up with holes if
3710 * power is lost part-way through the operation.
3712 for (i = last_del; i >= start_del; i--) {
3713 /* NB this could be optimised somewhat,
3714 * eg. could retrieve the tags and write them without
3715 * using yaffs_chunk_del
3718 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3724 (dev->internal_start_block * dev->param.chunks_per_block) ||
3726 ((dev->internal_end_block + 1) *
3727 dev->param.chunks_per_block)) {
3728 yaffs_trace(YAFFS_TRACE_ALWAYS,
3729 "Found daft chunk_id %d for %d",
3732 in->n_data_chunks--;
3733 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3738 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3742 struct yaffs_dev *dev = obj->my_dev;
3744 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3746 yaffs_prune_chunks(obj, new_size);
3748 if (new_partial != 0) {
3749 int last_chunk = 1 + new_full;
3750 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3752 /* Rewrite the last chunk with its new size and zero pad */
3753 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3754 memset(local_buffer + new_partial, 0,
3755 dev->data_bytes_per_chunk - new_partial);
3757 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3760 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3763 obj->variant.file_variant.file_size = new_size;
3765 yaffs_prune_tree(dev, &obj->variant.file_variant);
3768 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3770 struct yaffs_dev *dev = in->my_dev;
3771 int old_size = in->variant.file_variant.file_size;
3773 yaffs_flush_file_cache(in);
3774 yaffs_invalidate_whole_cache(in);
3776 yaffs_check_gc(dev, 0);
3778 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3781 if (new_size == old_size)
3784 if (new_size > old_size) {
3785 yaffs2_handle_hole(in, new_size);
3786 in->variant.file_variant.file_size = new_size;
3788 /* new_size < old_size */
3789 yaffs_resize_file_down(in, new_size);
3792 /* Write a new object header to reflect the resize.
3793 * show we've shrunk the file, if need be
3794 * Do this only if the file is not in the deleted directories
3795 * and is not shadowed.
3799 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3800 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3801 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3806 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3811 yaffs_flush_file_cache(in);
3817 yaffs_load_current_time(in, 0, 0);
3819 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3820 YAFFS_OK : YAFFS_FAIL;
3824 /* yaffs_del_file deletes the whole file data
3825 * and the inode associated with the file.
3826 * It does not delete the links associated with the file.
3828 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3832 struct yaffs_dev *dev = in->my_dev;
3839 yaffs_change_obj_name(in, in->my_dev->del_dir,
3840 _Y("deleted"), 0, 0);
3841 yaffs_trace(YAFFS_TRACE_TRACING,
3842 "yaffs: immediate deletion of file %d",
3845 in->my_dev->n_deleted_files++;
3846 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3847 yaffs_resize_file(in, 0);
3848 yaffs_soft_del_file(in);
3851 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3852 _Y("unlinked"), 0, 0);
3857 int yaffs_del_file(struct yaffs_obj *in)
3859 int ret_val = YAFFS_OK;
3860 int deleted; /* Need to cache value on stack if in is freed */
3861 struct yaffs_dev *dev = in->my_dev;
3863 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3864 yaffs_resize_file(in, 0);
3866 if (in->n_data_chunks > 0) {
3867 /* Use soft deletion if there is data in the file.
3868 * That won't be the case if it has been resized to zero.
3871 ret_val = yaffs_unlink_file_if_needed(in);
3873 deleted = in->deleted;
3875 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3878 in->my_dev->n_deleted_files++;
3879 yaffs_soft_del_file(in);
3881 return deleted ? YAFFS_OK : YAFFS_FAIL;
3883 /* The file has no data chunks so we toss it immediately */
3884 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3885 in->variant.file_variant.top = NULL;
3886 yaffs_generic_obj_del(in);
3892 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3895 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3896 !(list_empty(&obj->variant.dir_variant.children));
3899 static int yaffs_del_dir(struct yaffs_obj *obj)
3901 /* First check that the directory is empty. */
3902 if (yaffs_is_non_empty_dir(obj))
3905 return yaffs_generic_obj_del(obj);
3908 static int yaffs_del_symlink(struct yaffs_obj *in)
3910 kfree(in->variant.symlink_variant.alias);
3911 in->variant.symlink_variant.alias = NULL;
3913 return yaffs_generic_obj_del(in);
3916 static int yaffs_del_link(struct yaffs_obj *in)
3918 /* remove this hardlink from the list associated with the equivalent
3921 list_del_init(&in->hard_links);
3922 return yaffs_generic_obj_del(in);
3925 int yaffs_del_obj(struct yaffs_obj *obj)
3929 switch (obj->variant_type) {
3930 case YAFFS_OBJECT_TYPE_FILE:
3931 ret_val = yaffs_del_file(obj);
3933 case YAFFS_OBJECT_TYPE_DIRECTORY:
3934 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3935 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3936 "Remove object %d from dirty directories",
3938 list_del_init(&obj->variant.dir_variant.dirty);
3940 return yaffs_del_dir(obj);
3942 case YAFFS_OBJECT_TYPE_SYMLINK:
3943 ret_val = yaffs_del_symlink(obj);
3945 case YAFFS_OBJECT_TYPE_HARDLINK:
3946 ret_val = yaffs_del_link(obj);
3948 case YAFFS_OBJECT_TYPE_SPECIAL:
3949 ret_val = yaffs_generic_obj_del(obj);
3951 case YAFFS_OBJECT_TYPE_UNKNOWN:
3953 break; /* should not happen. */
3958 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3968 yaffs_update_parent(obj->parent);
3970 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3971 return yaffs_del_link(obj);
3972 } else if (!list_empty(&obj->hard_links)) {
3973 /* Curve ball: We're unlinking an object that has a hardlink.
3975 * This problem arises because we are not strictly following
3976 * The Linux link/inode model.
3978 * We can't really delete the object.
3979 * Instead, we do the following:
3980 * - Select a hardlink.
3981 * - Unhook it from the hard links
3982 * - Move it from its parent directory so that the rename works.
3983 * - Rename the object to the hardlink's name.
3984 * - Delete the hardlink
3987 struct yaffs_obj *hl;
3988 struct yaffs_obj *parent;
3990 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3992 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3995 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3996 parent = hl->parent;
3998 list_del_init(&hl->hard_links);
4000 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4002 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4004 if (ret_val == YAFFS_OK)
4005 ret_val = yaffs_generic_obj_del(hl);
4009 } else if (del_now) {
4010 switch (obj->variant_type) {
4011 case YAFFS_OBJECT_TYPE_FILE:
4012 return yaffs_del_file(obj);
4014 case YAFFS_OBJECT_TYPE_DIRECTORY:
4015 list_del_init(&obj->variant.dir_variant.dirty);
4016 return yaffs_del_dir(obj);
4018 case YAFFS_OBJECT_TYPE_SYMLINK:
4019 return yaffs_del_symlink(obj);
4021 case YAFFS_OBJECT_TYPE_SPECIAL:
4022 return yaffs_generic_obj_del(obj);
4024 case YAFFS_OBJECT_TYPE_HARDLINK:
4025 case YAFFS_OBJECT_TYPE_UNKNOWN:
4029 } else if (yaffs_is_non_empty_dir(obj)) {
4032 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4033 _Y("unlinked"), 0, 0);
4037 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4039 if (obj && obj->unlink_allowed)
4040 return yaffs_unlink_worker(obj);
4045 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4047 struct yaffs_obj *obj;
4049 obj = yaffs_find_by_name(dir, name);
4050 return yaffs_unlink_obj(obj);
4054 * If old_name is NULL then we take old_dir as the object to be renamed.
4056 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
4057 struct yaffs_obj *new_dir, const YCHAR *new_name)
4059 struct yaffs_obj *obj = NULL;
4060 struct yaffs_obj *existing_target = NULL;
4063 struct yaffs_dev *dev;
4065 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4069 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4074 dev = old_dir->my_dev;
4076 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4077 /* Special case for case insemsitive systems.
4078 * While look-up is case insensitive, the name isn't.
4079 * Therefore we might want to change x.txt to X.txt
4081 if (old_dir == new_dir &&
4082 old_name && new_name &&
4083 strcmp(old_name, new_name) == 0)
4087 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4088 YAFFS_MAX_NAME_LENGTH)
4093 obj = yaffs_find_by_name(old_dir, old_name);
4096 old_dir = obj->parent;
4099 if (obj && obj->rename_allowed) {
4100 /* Now handle an existing target, if there is one */
4101 existing_target = yaffs_find_by_name(new_dir, new_name);
4102 if (yaffs_is_non_empty_dir(existing_target)) {
4103 return YAFFS_FAIL; /* ENOTEMPTY */
4104 } else if (existing_target && existing_target != obj) {
4105 /* Nuke the target first, using shadowing,
4106 * but only if it isn't the same object.
4108 * Note we must disable gc here otherwise it can mess
4112 dev->gc_disable = 1;
4113 yaffs_change_obj_name(obj, new_dir, new_name, force,
4114 existing_target->obj_id);
4115 existing_target->is_shadowed = 1;
4116 yaffs_unlink_obj(existing_target);
4117 dev->gc_disable = 0;
4120 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4122 yaffs_update_parent(old_dir);
4123 if (new_dir != old_dir)
4124 yaffs_update_parent(new_dir);
4131 /*----------------------- Initialisation Scanning ---------------------- */
4133 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4134 int backward_scanning)
4136 struct yaffs_obj *obj;
4138 if (backward_scanning) {
4139 /* Handle YAFFS2 case (backward scanning)
4140 * If the shadowed object exists then ignore.
4142 obj = yaffs_find_by_number(dev, obj_id);
4147 /* Let's create it (if it does not exist) assuming it is a file so that
4148 * it can do shrinking etc.
4149 * We put it in unlinked dir to be cleaned up after the scanning
4152 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4155 obj->is_shadowed = 1;
4156 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4157 obj->variant.file_variant.shrink_size = 0;
4158 obj->valid = 1; /* So that we don't read any other info. */
4161 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4163 struct list_head *lh;
4164 struct list_head *save;
4165 struct yaffs_obj *hl;
4166 struct yaffs_obj *in;
4168 list_for_each_safe(lh, save, hard_list) {
4169 hl = list_entry(lh, struct yaffs_obj, hard_links);
4170 in = yaffs_find_by_number(dev,
4171 hl->variant.hardlink_variant.equiv_id);
4174 /* Add the hardlink pointers */
4175 hl->variant.hardlink_variant.equiv_obj = in;
4176 list_add(&hl->hard_links, &in->hard_links);
4178 /* Todo Need to report/handle this better.
4179 * Got a problem... hardlink to a non-existant object
4181 hl->variant.hardlink_variant.equiv_obj = NULL;
4182 INIT_LIST_HEAD(&hl->hard_links);
4187 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4190 * Sort out state of unlinked and deleted objects after scanning.
4192 struct list_head *i;
4193 struct list_head *n;
4194 struct yaffs_obj *l;
4199 /* Soft delete all the unlinked files */
4200 list_for_each_safe(i, n,
4201 &dev->unlinked_dir->variant.dir_variant.children) {
4202 l = list_entry(i, struct yaffs_obj, siblings);
4206 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4207 l = list_entry(i, struct yaffs_obj, siblings);
4213 * This code iterates through all the objects making sure that they are rooted.
4214 * Any unrooted objects are re-rooted in lost+found.
4215 * An object needs to be in one of:
4216 * - Directly under deleted, unlinked
4217 * - Directly or indirectly under root.
4220 * This code assumes that we don't ever change the current relationships
4221 * between directories:
4222 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4223 * lost-n-found->parent == root_dir
4225 * This fixes the problem where directories might have inadvertently been
4226 * deleted leaving the object "hanging" without being rooted in the
4230 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4232 return (obj == dev->del_dir ||
4233 obj == dev->unlinked_dir || obj == dev->root_dir);
4236 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4238 struct yaffs_obj *obj;
4239 struct yaffs_obj *parent;
4241 struct list_head *lh;
4242 struct list_head *n;
4249 /* Iterate through the objects in each hash entry,
4250 * looking at each object.
4251 * Make sure it is rooted.
4254 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4255 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4256 obj = list_entry(lh, struct yaffs_obj, hash_link);
4257 parent = obj->parent;
4259 if (yaffs_has_null_parent(dev, obj)) {
4260 /* These directories are not hanging */
4262 } else if (!parent ||
4263 parent->variant_type !=
4264 YAFFS_OBJECT_TYPE_DIRECTORY) {
4266 } else if (yaffs_has_null_parent(dev, parent)) {
4270 * Need to follow the parent chain to
4271 * see if it is hanging.
4276 while (parent != dev->root_dir &&
4278 parent->parent->variant_type ==
4279 YAFFS_OBJECT_TYPE_DIRECTORY &&
4281 parent = parent->parent;
4284 if (parent != dev->root_dir)
4288 yaffs_trace(YAFFS_TRACE_SCAN,
4289 "Hanging object %d moved to lost and found",
4291 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4298 * Delete directory contents for cleaning up lost and found.
4300 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4302 struct yaffs_obj *obj;
4303 struct list_head *lh;
4304 struct list_head *n;
4306 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4309 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4310 obj = list_entry(lh, struct yaffs_obj, siblings);
4311 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4312 yaffs_del_dir_contents(obj);
4313 yaffs_trace(YAFFS_TRACE_SCAN,
4314 "Deleting lost_found object %d",
4316 yaffs_unlink_obj(obj);
4320 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4322 yaffs_del_dir_contents(dev->lost_n_found);
4326 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4330 struct list_head *i;
4331 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4332 struct yaffs_obj *l;
4338 yaffs_trace(YAFFS_TRACE_ALWAYS,
4339 "tragedy: yaffs_find_by_name: null pointer directory"
4344 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4345 yaffs_trace(YAFFS_TRACE_ALWAYS,
4346 "tragedy: yaffs_find_by_name: non-directory"
4351 sum = yaffs_calc_name_sum(name);
4353 list_for_each(i, &directory->variant.dir_variant.children) {
4354 l = list_entry(i, struct yaffs_obj, siblings);
4356 if (l->parent != directory)
4359 yaffs_check_obj_details_loaded(l);
4361 /* Special case for lost-n-found */
4362 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4363 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4365 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4366 /* LostnFound chunk called Objxxx
4369 yaffs_get_obj_name(l, buffer,
4370 YAFFS_MAX_NAME_LENGTH + 1);
4371 if (strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4378 /* GetEquivalentObject dereferences any hard links to get to the
4382 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4384 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4385 obj = obj->variant.hardlink_variant.equiv_obj;
4386 yaffs_check_obj_details_loaded(obj);
4392 * A note or two on object names.
4393 * * If the object name is missing, we then make one up in the form objnnn
4395 * * ASCII names are stored in the object header's name field from byte zero
4396 * * Unicode names are historically stored starting from byte zero.
4398 * Then there are automatic Unicode names...
4399 * The purpose of these is to save names in a way that can be read as
4400 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4401 * system to share files.
4403 * These automatic unicode are stored slightly differently...
4404 * - If the name can fit in the ASCII character space then they are saved as
4405 * ascii names as per above.
4406 * - If the name needs Unicode then the name is saved in Unicode
4407 * starting at oh->name[1].
4410 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4413 /* Create an object name if we could not find one. */
4414 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4415 YCHAR local_name[20];
4416 YCHAR num_string[20];
4417 YCHAR *x = &num_string[19];
4418 unsigned v = obj->obj_id;
4422 *x = '0' + (v % 10);
4425 /* make up a name */
4426 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4427 strcat(local_name, x);
4428 strncpy(name, local_name, buffer_size - 1);
4432 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4434 memset(name, 0, buffer_size * sizeof(YCHAR));
4435 yaffs_check_obj_details_loaded(obj);
4436 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND)
4437 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4438 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
4439 else if (obj->short_name[0])
4440 strcpy(name, obj->short_name);
4442 else if (obj->hdr_chunk > 0) {
4444 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4446 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4448 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4450 if (obj->hdr_chunk > 0) {
4451 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4455 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4458 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4461 yaffs_fix_null_name(obj, name, buffer_size);
4463 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4466 int yaffs_get_obj_length(struct yaffs_obj *obj)
4468 /* Dereference any hard linking */
4469 obj = yaffs_get_equivalent_obj(obj);
4471 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4472 return obj->variant.file_variant.file_size;
4473 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4474 if (!obj->variant.symlink_variant.alias)
4476 return strnlen(obj->variant.symlink_variant.alias,
4477 YAFFS_MAX_ALIAS_LENGTH);
4479 /* Only a directory should drop through to here */
4480 return obj->my_dev->data_bytes_per_chunk;
4484 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4487 struct list_head *i;
4490 count++; /* the object itself */
4492 list_for_each(i, &obj->hard_links)
4493 count++; /* add the hard links; */
4498 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4500 obj = yaffs_get_equivalent_obj(obj);
4505 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4507 obj = yaffs_get_equivalent_obj(obj);
4509 switch (obj->variant_type) {
4510 case YAFFS_OBJECT_TYPE_FILE:
4513 case YAFFS_OBJECT_TYPE_DIRECTORY:
4516 case YAFFS_OBJECT_TYPE_SYMLINK:
4519 case YAFFS_OBJECT_TYPE_HARDLINK:
4522 case YAFFS_OBJECT_TYPE_SPECIAL:
4523 if (S_ISFIFO(obj->yst_mode))
4525 if (S_ISCHR(obj->yst_mode))
4527 if (S_ISBLK(obj->yst_mode))
4529 if (S_ISSOCK(obj->yst_mode))
4539 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4541 obj = yaffs_get_equivalent_obj(obj);
4542 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4543 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4545 return yaffs_clone_str(_Y(""));
4548 /*--------------------------- Initialisation code -------------------------- */
4550 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4552 /* Common functions, gotta have */
4553 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4556 #ifdef CONFIG_YAFFS_YAFFS2
4558 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4559 if (dev->param.write_chunk_tags_fn &&
4560 dev->param.read_chunk_tags_fn &&
4561 !dev->param.write_chunk_fn &&
4562 !dev->param.read_chunk_fn &&
4563 dev->param.bad_block_fn && dev->param.query_block_fn)
4567 /* Can use the "spare" style interface for yaffs1 */
4568 if (!dev->param.is_yaffs2 &&
4569 !dev->param.write_chunk_tags_fn &&
4570 !dev->param.read_chunk_tags_fn &&
4571 dev->param.write_chunk_fn &&
4572 dev->param.read_chunk_fn &&
4573 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4579 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4581 /* Initialise the unlinked, deleted, root and lost+found directories */
4582 dev->lost_n_found = dev->root_dir = NULL;
4583 dev->unlinked_dir = dev->del_dir = NULL;
4585 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4587 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4589 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4590 YAFFS_ROOT_MODE | S_IFDIR);
4592 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4593 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4595 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4597 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4603 int yaffs_guts_initialise(struct yaffs_dev *dev)
4605 int init_failed = 0;
4609 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()");
4611 /* Check stuff that must be set */
4614 yaffs_trace(YAFFS_TRACE_ALWAYS,
4615 "yaffs: Need a device"
4620 if (dev->is_mounted) {
4621 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4625 dev->internal_start_block = dev->param.start_block;
4626 dev->internal_end_block = dev->param.end_block;
4627 dev->block_offset = 0;
4628 dev->chunk_offset = 0;
4629 dev->n_free_chunks = 0;
4633 if (dev->param.start_block == 0) {
4634 dev->internal_start_block = dev->param.start_block + 1;
4635 dev->internal_end_block = dev->param.end_block + 1;
4636 dev->block_offset = 1;
4637 dev->chunk_offset = dev->param.chunks_per_block;
4640 /* Check geometry parameters. */
4642 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4643 dev->param.total_bytes_per_chunk < 1024) ||
4644 (!dev->param.is_yaffs2 &&
4645 dev->param.total_bytes_per_chunk < 512) ||
4646 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4647 dev->param.chunks_per_block < 2 ||
4648 dev->param.n_reserved_blocks < 2 ||
4649 dev->internal_start_block <= 0 ||
4650 dev->internal_end_block <= 0 ||
4651 dev->internal_end_block <=
4652 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4654 /* otherwise it is too small */
4655 yaffs_trace(YAFFS_TRACE_ALWAYS,
4656 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4657 dev->param.total_bytes_per_chunk,
4658 dev->param.is_yaffs2 ? "2" : "",
4659 dev->param.inband_tags);
4663 if (yaffs_init_nand(dev) != YAFFS_OK) {
4664 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4668 /* Sort out space for inband tags, if required */
4669 if (dev->param.inband_tags)
4670 dev->data_bytes_per_chunk =
4671 dev->param.total_bytes_per_chunk -
4672 sizeof(struct yaffs_packed_tags2_tags_only);
4674 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4676 /* Got the right mix of functions? */
4677 if (!yaffs_check_dev_fns(dev)) {
4678 /* Function missing */
4679 yaffs_trace(YAFFS_TRACE_ALWAYS,
4680 "device function(s) missing or wrong");
4685 /* Finished with most checks. Further checks happen later on too. */
4687 dev->is_mounted = 1;
4689 /* OK now calculate a few things for the device */
4692 * Calculate all the chunk size manipulation numbers:
4694 x = dev->data_bytes_per_chunk;
4695 /* We always use dev->chunk_shift and dev->chunk_div */
4696 dev->chunk_shift = calc_shifts(x);
4697 x >>= dev->chunk_shift;
4699 /* We only use chunk mask if chunk_div is 1 */
4700 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4703 * Calculate chunk_grp_bits.
4704 * We need to find the next power of 2 > than internal_end_block
4707 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4709 bits = calc_shifts_ceiling(x);
4711 /* Set up tnode width if wide tnodes are enabled. */
4712 if (!dev->param.wide_tnodes_disabled) {
4713 /* bits must be even so that we end up with 32-bit words */
4717 dev->tnode_width = 16;
4719 dev->tnode_width = bits;
4721 dev->tnode_width = 16;
4724 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4726 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4727 * so if the bitwidth of the
4728 * chunk range we're using is greater than 16 we need
4729 * to figure out chunk shift and chunk_grp_size
4732 if (bits <= dev->tnode_width)
4733 dev->chunk_grp_bits = 0;
4735 dev->chunk_grp_bits = bits - dev->tnode_width;
4737 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4738 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4739 dev->tnode_size = sizeof(struct yaffs_tnode);
4741 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4743 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4744 /* We have a problem because the soft delete won't work if
4745 * the chunk group size > chunks per block.
4746 * This can be remedied by using larger "virtual blocks".
4748 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4753 /* Finished verifying the device, continue with initialisation */
4755 /* More device initialisation */
4757 dev->passive_gc_count = 0;
4758 dev->oldest_dirty_gc_count = 0;
4760 dev->gc_block_finder = 0;
4761 dev->buffered_block = -1;
4762 dev->doing_buffered_block_rewrite = 0;
4763 dev->n_deleted_files = 0;
4764 dev->n_bg_deletions = 0;
4765 dev->n_unlinked_files = 0;
4766 dev->n_ecc_fixed = 0;
4767 dev->n_ecc_unfixed = 0;
4768 dev->n_tags_ecc_fixed = 0;
4769 dev->n_tags_ecc_unfixed = 0;
4770 dev->n_erase_failures = 0;
4771 dev->n_erased_blocks = 0;
4772 dev->gc_disable = 0;
4773 dev->has_pending_prioritised_gc = 1;
4774 /* Assume the worst for now, will get fixed on first GC */
4775 INIT_LIST_HEAD(&dev->dirty_dirs);
4776 dev->oldest_dirty_seq = 0;
4777 dev->oldest_dirty_block = 0;
4779 /* Initialise temporary buffers and caches. */
4780 if (!yaffs_init_tmp_buffers(dev))
4784 dev->gc_cleanup_list = NULL;
4786 if (!init_failed && dev->param.n_caches > 0) {
4790 dev->param.n_caches * sizeof(struct yaffs_cache);
4792 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4793 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4795 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4797 buf = (u8 *) dev->cache;
4800 memset(dev->cache, 0, cache_bytes);
4802 for (i = 0; i < dev->param.n_caches && buf; i++) {
4803 dev->cache[i].object = NULL;
4804 dev->cache[i].last_use = 0;
4805 dev->cache[i].dirty = 0;
4806 dev->cache[i].data = buf =
4807 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4812 dev->cache_last_use = 0;
4815 dev->cache_hits = 0;
4818 dev->gc_cleanup_list =
4819 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4821 if (!dev->gc_cleanup_list)
4825 if (dev->param.is_yaffs2)
4826 dev->param.use_header_file_size = 1;
4828 if (!init_failed && !yaffs_init_blocks(dev))
4831 yaffs_init_tnodes_and_objs(dev);
4833 if (!init_failed && !yaffs_create_initial_dir(dev))
4837 /* Now scan the flash. */
4838 if (dev->param.is_yaffs2) {
4839 if (yaffs2_checkpt_restore(dev)) {
4840 yaffs_check_obj_details_loaded(dev->root_dir);
4841 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4843 "yaffs: restored from checkpoint"
4847 /* Clean up the mess caused by an aborted
4848 * checkpoint load then scan backwards.
4850 yaffs_deinit_blocks(dev);
4852 yaffs_deinit_tnodes_and_objs(dev);
4854 dev->n_erased_blocks = 0;
4855 dev->n_free_chunks = 0;
4856 dev->alloc_block = -1;
4857 dev->alloc_page = -1;
4858 dev->n_deleted_files = 0;
4859 dev->n_unlinked_files = 0;
4860 dev->n_bg_deletions = 0;
4862 if (!init_failed && !yaffs_init_blocks(dev))
4865 yaffs_init_tnodes_and_objs(dev);
4868 && !yaffs_create_initial_dir(dev))
4871 if (!init_failed && !yaffs2_scan_backwards(dev))
4874 } else if (!yaffs1_scan(dev)) {
4878 yaffs_strip_deleted_objs(dev);
4879 yaffs_fix_hanging_objs(dev);
4880 if (dev->param.empty_lost_n_found)
4881 yaffs_empty_l_n_f(dev);
4885 /* Clean up the mess */
4886 yaffs_trace(YAFFS_TRACE_TRACING,
4887 "yaffs: yaffs_guts_initialise() aborted.");
4889 yaffs_deinitialise(dev);
4893 /* Zero out stats */
4894 dev->n_page_reads = 0;
4895 dev->n_page_writes = 0;
4896 dev->n_erasures = 0;
4897 dev->n_gc_copies = 0;
4898 dev->n_retired_writes = 0;
4900 dev->n_retired_blocks = 0;
4902 yaffs_verify_free_chunks(dev);
4903 yaffs_verify_blocks(dev);
4905 /* Clean up any aborted checkpoint data */
4906 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4907 yaffs2_checkpt_invalidate(dev);
4909 yaffs_trace(YAFFS_TRACE_TRACING,
4910 "yaffs: yaffs_guts_initialise() done.");
4914 void yaffs_deinitialise(struct yaffs_dev *dev)
4916 if (dev->is_mounted) {
4919 yaffs_deinit_blocks(dev);
4920 yaffs_deinit_tnodes_and_objs(dev);
4921 if (dev->param.n_caches > 0 && dev->cache) {
4923 for (i = 0; i < dev->param.n_caches; i++) {
4924 kfree(dev->cache[i].data);
4925 dev->cache[i].data = NULL;
4932 kfree(dev->gc_cleanup_list);
4934 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
4935 kfree(dev->temp_buffer[i].buffer);
4937 dev->is_mounted = 0;
4939 if (dev->param.deinitialise_flash_fn)
4940 dev->param.deinitialise_flash_fn(dev);
4944 int yaffs_count_free_chunks(struct yaffs_dev *dev)
4948 struct yaffs_block_info *blk;
4950 blk = dev->block_info;
4951 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4952 switch (blk->block_state) {
4953 case YAFFS_BLOCK_STATE_EMPTY:
4954 case YAFFS_BLOCK_STATE_ALLOCATING:
4955 case YAFFS_BLOCK_STATE_COLLECTING:
4956 case YAFFS_BLOCK_STATE_FULL:
4958 (dev->param.chunks_per_block - blk->pages_in_use +
4959 blk->soft_del_pages);
4969 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
4971 /* This is what we report to the outside world */
4974 int blocks_for_checkpt;
4977 n_free = dev->n_free_chunks;
4978 n_free += dev->n_deleted_files;
4980 /* Now count and subtract the number of dirty chunks in the cache. */
4982 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
4983 if (dev->cache[i].dirty)
4987 n_free -= n_dirty_caches;
4990 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
4992 /* Now figure checkpoint space and report that... */
4993 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
4995 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);