2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_trace.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_getblockinfo.h"
19 #include "yaffs_tagscompat.h"
20 #include "yaffs_tagsmarshall.h"
21 #include "yaffs_nand.h"
22 #include "yaffs_yaffs1.h"
23 #include "yaffs_yaffs2.h"
24 #include "yaffs_bitmap.h"
25 #include "yaffs_verify.h"
26 #include "yaffs_nand.h"
27 #include "yaffs_packedtags2.h"
28 #include "yaffs_nameval.h"
29 #include "yaffs_allocator.h"
30 #include "yaffs_attribs.h"
31 #include "yaffs_summary.h"
33 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
34 #define YAFFS_GC_GOOD_ENOUGH 2
35 #define YAFFS_GC_PASSIVE_THRESHOLD 4
37 #include "yaffs_ecc.h"
39 /* Forward declarations */
41 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
42 const u8 *buffer, int n_bytes, int use_reserve);
46 /* Function to calculate chunk and offset */
48 void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
49 int *chunk_out, u32 *offset_out)
54 chunk = (u32) (addr >> dev->chunk_shift);
56 if (dev->chunk_div == 1) {
57 /* easy power of 2 case */
58 offset = (u32) (addr & dev->chunk_mask);
60 /* Non power-of-2 case */
64 chunk /= dev->chunk_div;
66 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
67 offset = (u32) (addr - chunk_base);
74 /* Function to return the number of shifts for a power of 2 greater than or
75 * equal to the given number
76 * Note we don't try to cater for all possible numbers and this does not have to
77 * be hellishly efficient.
80 static inline u32 calc_shifts_ceiling(u32 x)
85 shifts = extra_bits = 0;
100 /* Function to return the number of shifts to get a 1 in bit 0
103 static inline u32 calc_shifts(u32 x)
121 * Temporary buffer manipulations.
124 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
129 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
131 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
132 dev->temp_buffer[i].in_use = 0;
133 buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
134 dev->temp_buffer[i].buffer = buf;
137 return buf ? YAFFS_OK : YAFFS_FAIL;
140 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
145 if (dev->temp_in_use > dev->max_temp)
146 dev->max_temp = dev->temp_in_use;
148 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
149 if (dev->temp_buffer[i].in_use == 0) {
150 dev->temp_buffer[i].in_use = 1;
151 return dev->temp_buffer[i].buffer;
155 yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
157 * If we got here then we have to allocate an unmanaged one
161 dev->unmanaged_buffer_allocs++;
162 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
166 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
172 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
173 if (dev->temp_buffer[i].buffer == buffer) {
174 dev->temp_buffer[i].in_use = 0;
180 /* assume it is an unmanaged one. */
181 yaffs_trace(YAFFS_TRACE_BUFFERS,
182 "Releasing unmanaged temp buffer");
184 dev->unmanaged_buffer_deallocs++;
190 * Functions for robustisizing TODO
194 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
196 const struct yaffs_ext_tags *tags)
204 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
205 const struct yaffs_ext_tags *tags)
212 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
213 struct yaffs_block_info *bi)
215 if (!bi->gc_prioritise) {
216 bi->gc_prioritise = 1;
217 dev->has_pending_prioritised_gc = 1;
218 bi->chunk_error_strikes++;
220 if (bi->chunk_error_strikes > 3) {
221 bi->needs_retiring = 1; /* Too many stikes, so retire */
222 yaffs_trace(YAFFS_TRACE_ALWAYS,
223 "yaffs: Block struck out");
229 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
232 int flash_block = nand_chunk / dev->param.chunks_per_block;
233 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
235 yaffs_handle_chunk_error(dev, bi);
238 /* Was an actual write failure,
239 * so mark the block for retirement.*/
240 bi->needs_retiring = 1;
241 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
242 "**>> Block %d needs retiring", flash_block);
245 /* Delete the chunk */
246 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
247 yaffs_skip_rest_of_block(dev);
255 * Simple hash function. Needs to have a reasonable spread
258 static inline int yaffs_hash_fn(int n)
262 return n % YAFFS_NOBJECT_BUCKETS;
266 * Access functions to useful fake objects.
267 * Note that root might have a presence in NAND if permissions are set.
270 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
272 return dev->root_dir;
275 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
277 return dev->lost_n_found;
281 * Erased NAND checking functions
284 int yaffs_check_ff(u8 *buffer, int n_bytes)
286 /* Horrible, slow implementation */
295 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
297 int retval = YAFFS_OK;
298 u8 *data = yaffs_get_temp_buffer(dev);
299 struct yaffs_ext_tags tags;
302 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
304 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
307 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
309 yaffs_trace(YAFFS_TRACE_NANDACCESS,
310 "Chunk %d not erased", nand_chunk);
314 yaffs_release_temp_buffer(dev, data);
320 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
323 struct yaffs_ext_tags *tags)
325 int retval = YAFFS_OK;
326 struct yaffs_ext_tags temp_tags;
327 u8 *buffer = yaffs_get_temp_buffer(dev);
330 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
331 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
332 temp_tags.obj_id != tags->obj_id ||
333 temp_tags.chunk_id != tags->chunk_id ||
334 temp_tags.n_bytes != tags->n_bytes)
337 yaffs_release_temp_buffer(dev, buffer);
343 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
346 int reserved_blocks = dev->param.n_reserved_blocks;
349 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
352 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
354 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
357 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
360 struct yaffs_block_info *bi;
362 if (dev->n_erased_blocks < 1) {
363 /* Hoosterman we've got a problem.
364 * Can't get space to gc
366 yaffs_trace(YAFFS_TRACE_ERROR,
367 "yaffs tragedy: no more erased blocks");
372 /* Find an empty block. */
374 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
375 dev->alloc_block_finder++;
376 if (dev->alloc_block_finder < dev->internal_start_block
377 || dev->alloc_block_finder > dev->internal_end_block) {
378 dev->alloc_block_finder = dev->internal_start_block;
381 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
383 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
384 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
386 bi->seq_number = dev->seq_number;
387 dev->n_erased_blocks--;
388 yaffs_trace(YAFFS_TRACE_ALLOCATE,
389 "Allocated block %d, seq %d, %d left" ,
390 dev->alloc_block_finder, dev->seq_number,
391 dev->n_erased_blocks);
392 return dev->alloc_block_finder;
396 yaffs_trace(YAFFS_TRACE_ALWAYS,
397 "yaffs tragedy: no more erased blocks, but there should have been %d",
398 dev->n_erased_blocks);
403 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
404 struct yaffs_block_info **block_ptr)
407 struct yaffs_block_info *bi;
409 if (dev->alloc_block < 0) {
410 /* Get next block to allocate off */
411 dev->alloc_block = yaffs_find_alloc_block(dev);
415 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
416 /* No space unless we're allowed to use the reserve. */
420 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
421 && dev->alloc_page == 0)
422 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
424 /* Next page please.... */
425 if (dev->alloc_block >= 0) {
426 bi = yaffs_get_block_info(dev, dev->alloc_block);
428 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
431 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
435 dev->n_free_chunks--;
437 /* If the block is full set the state to full */
438 if (dev->alloc_page >= dev->param.chunks_per_block) {
439 bi->block_state = YAFFS_BLOCK_STATE_FULL;
440 dev->alloc_block = -1;
449 yaffs_trace(YAFFS_TRACE_ERROR,
450 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
455 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
459 n = dev->n_erased_blocks * dev->param.chunks_per_block;
461 if (dev->alloc_block > 0)
462 n += (dev->param.chunks_per_block - dev->alloc_page);
469 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
470 * if we don't want to write to it.
472 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
474 struct yaffs_block_info *bi;
476 if (dev->alloc_block > 0) {
477 bi = yaffs_get_block_info(dev, dev->alloc_block);
478 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
479 bi->block_state = YAFFS_BLOCK_STATE_FULL;
480 dev->alloc_block = -1;
485 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
487 struct yaffs_ext_tags *tags, int use_reserver)
493 yaffs2_checkpt_invalidate(dev);
496 struct yaffs_block_info *bi = 0;
499 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
505 /* First check this chunk is erased, if it needs
506 * checking. The checking policy (unless forced
507 * always on) is as follows:
509 * Check the first page we try to write in a block.
510 * If the check passes then we don't need to check any
511 * more. If the check fails, we check again...
512 * If the block has been erased, we don't need to check.
514 * However, if the block has been prioritised for gc,
515 * then we think there might be something odd about
516 * this block and stop using it.
518 * Rationale: We should only ever see chunks that have
519 * not been erased if there was a partially written
520 * chunk due to power loss. This checking policy should
521 * catch that case with very few checks and thus save a
522 * lot of checks that are most likely not needed.
525 * If an erase check fails or the write fails we skip the
529 /* let's give it a try */
532 if (dev->param.always_check_erased)
533 bi->skip_erased_check = 0;
535 if (!bi->skip_erased_check) {
536 erased_ok = yaffs_check_chunk_erased(dev, chunk);
537 if (erased_ok != YAFFS_OK) {
538 yaffs_trace(YAFFS_TRACE_ERROR,
539 "**>> yaffs chunk %d was not erased",
542 /* If not erased, delete this one,
543 * skip rest of block and
544 * try another chunk */
545 yaffs_chunk_del(dev, chunk, 1, __LINE__);
546 yaffs_skip_rest_of_block(dev);
551 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
553 if (!bi->skip_erased_check)
555 yaffs_verify_chunk_written(dev, chunk, data, tags);
557 if (write_ok != YAFFS_OK) {
558 /* Clean up aborted write, skip to next block and
559 * try another chunk */
560 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
564 bi->skip_erased_check = 1;
566 /* Copy the data into the robustification buffer */
567 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
569 } while (write_ok != YAFFS_OK &&
570 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
576 yaffs_trace(YAFFS_TRACE_ERROR,
577 "**>> yaffs write required %d attempts",
579 dev->n_retried_writes += (attempts - 1);
586 * Block retiring for handling a broken block.
589 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
591 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
593 yaffs2_checkpt_invalidate(dev);
595 yaffs2_clear_oldest_dirty_seq(dev, bi);
597 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
598 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
599 yaffs_trace(YAFFS_TRACE_ALWAYS,
600 "yaffs: Failed to mark bad and erase block %d",
603 struct yaffs_ext_tags tags;
605 flash_block * dev->param.chunks_per_block;
607 u8 *buffer = yaffs_get_temp_buffer(dev);
609 memset(buffer, 0xff, dev->data_bytes_per_chunk);
610 memset(&tags, 0, sizeof(tags));
611 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
612 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
616 yaffs_trace(YAFFS_TRACE_ALWAYS,
617 "yaffs: Failed to write bad block marker to block %d",
620 yaffs_release_temp_buffer(dev, buffer);
624 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
625 bi->gc_prioritise = 0;
626 bi->needs_retiring = 0;
628 dev->n_retired_blocks++;
631 /*---------------- Name handling functions ------------*/
633 static u16 yaffs_calc_name_sum(const YCHAR *name)
641 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
643 /* 0x1f mask is case insensitive */
644 sum += ((*name) & 0x1f) * i;
651 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
653 memset(obj->short_name, 0, sizeof(obj->short_name));
655 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
656 YAFFS_SHORT_NAME_LENGTH)
657 strcpy(obj->short_name, name);
659 obj->short_name[0] = _Y('\0');
660 obj->sum = yaffs_calc_name_sum(name);
663 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
664 const struct yaffs_obj_hdr *oh)
666 #ifdef CONFIG_YAFFS_AUTO_UNICODE
667 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
668 memset(tmp_name, 0, sizeof(tmp_name));
669 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
670 YAFFS_MAX_NAME_LENGTH + 1);
671 yaffs_set_obj_name(obj, tmp_name);
673 yaffs_set_obj_name(obj, oh->name);
677 loff_t yaffs_max_file_size(struct yaffs_dev *dev)
679 if(sizeof(loff_t) < 8)
680 return YAFFS_MAX_FILE_SIZE_32;
682 return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
685 /*-------------------- TNODES -------------------
687 * List of spare tnodes
688 * The list is hooked together using the first pointer
692 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
694 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
697 memset(tn, 0, dev->tnode_size);
701 dev->checkpoint_blocks_required = 0; /* force recalculation */
706 /* FreeTnode frees up a tnode and puts it back on the free list */
707 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
709 yaffs_free_raw_tnode(dev, tn);
711 dev->checkpoint_blocks_required = 0; /* force recalculation */
714 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
716 yaffs_deinit_raw_tnodes_and_objs(dev);
721 static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
722 unsigned pos, unsigned val)
724 u32 *map = (u32 *) tn;
730 pos &= YAFFS_TNODES_LEVEL0_MASK;
731 val >>= dev->chunk_grp_bits;
733 bit_in_map = pos * dev->tnode_width;
734 word_in_map = bit_in_map / 32;
735 bit_in_word = bit_in_map & (32 - 1);
737 mask = dev->tnode_mask << bit_in_word;
739 map[word_in_map] &= ~mask;
740 map[word_in_map] |= (mask & (val << bit_in_word));
742 if (dev->tnode_width > (32 - bit_in_word)) {
743 bit_in_word = (32 - bit_in_word);
746 dev->tnode_mask >> bit_in_word;
747 map[word_in_map] &= ~mask;
748 map[word_in_map] |= (mask & (val >> bit_in_word));
752 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
755 u32 *map = (u32 *) tn;
761 pos &= YAFFS_TNODES_LEVEL0_MASK;
763 bit_in_map = pos * dev->tnode_width;
764 word_in_map = bit_in_map / 32;
765 bit_in_word = bit_in_map & (32 - 1);
767 val = map[word_in_map] >> bit_in_word;
769 if (dev->tnode_width > (32 - bit_in_word)) {
770 bit_in_word = (32 - bit_in_word);
772 val |= (map[word_in_map] << bit_in_word);
775 val &= dev->tnode_mask;
776 val <<= dev->chunk_grp_bits;
781 /* ------------------- End of individual tnode manipulation -----------------*/
783 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
784 * The look up tree is represented by the top tnode and the number of top_level
785 * in the tree. 0 means only the level 0 tnode is in the tree.
788 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
789 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
790 struct yaffs_file_var *file_struct,
793 struct yaffs_tnode *tn = file_struct->top;
796 int level = file_struct->top_level;
800 /* Check sane level and chunk Id */
801 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
804 if (chunk_id > YAFFS_MAX_CHUNK_ID)
807 /* First check we're tall enough (ie enough top_level) */
809 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
812 i >>= YAFFS_TNODES_INTERNAL_BITS;
816 if (required_depth > file_struct->top_level)
817 return NULL; /* Not tall enough, so we can't find it */
819 /* Traverse down to level 0 */
820 while (level > 0 && tn) {
821 tn = tn->internal[(chunk_id >>
822 (YAFFS_TNODES_LEVEL0_BITS +
824 YAFFS_TNODES_INTERNAL_BITS)) &
825 YAFFS_TNODES_INTERNAL_MASK];
832 /* add_find_tnode_0 finds the level 0 tnode if it exists,
833 * otherwise first expands the tree.
834 * This happens in two steps:
835 * 1. If the tree isn't tall enough, then make it taller.
836 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
838 * Used when modifying the tree.
840 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
841 * specified tn will be plugged into the ttree.
844 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
845 struct yaffs_file_var *file_struct,
847 struct yaffs_tnode *passed_tn)
852 struct yaffs_tnode *tn;
855 /* Check sane level and page Id */
856 if (file_struct->top_level < 0 ||
857 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
860 if (chunk_id > YAFFS_MAX_CHUNK_ID)
863 /* First check we're tall enough (ie enough top_level) */
865 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
868 x >>= YAFFS_TNODES_INTERNAL_BITS;
872 if (required_depth > file_struct->top_level) {
873 /* Not tall enough, gotta make the tree taller */
874 for (i = file_struct->top_level; i < required_depth; i++) {
876 tn = yaffs_get_tnode(dev);
879 tn->internal[0] = file_struct->top;
880 file_struct->top = tn;
881 file_struct->top_level++;
883 yaffs_trace(YAFFS_TRACE_ERROR,
884 "yaffs: no more tnodes");
890 /* Traverse down to level 0, adding anything we need */
892 l = file_struct->top_level;
893 tn = file_struct->top;
896 while (l > 0 && tn) {
898 (YAFFS_TNODES_LEVEL0_BITS +
899 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
900 YAFFS_TNODES_INTERNAL_MASK;
902 if ((l > 1) && !tn->internal[x]) {
903 /* Add missing non-level-zero tnode */
904 tn->internal[x] = yaffs_get_tnode(dev);
905 if (!tn->internal[x])
908 /* Looking from level 1 at level 0 */
910 /* If we already have one, release it */
912 yaffs_free_tnode(dev,
914 tn->internal[x] = passed_tn;
916 } else if (!tn->internal[x]) {
917 /* Don't have one, none passed in */
918 tn->internal[x] = yaffs_get_tnode(dev);
919 if (!tn->internal[x])
924 tn = tn->internal[x];
930 memcpy(tn, passed_tn,
931 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
932 yaffs_free_tnode(dev, passed_tn);
939 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
942 return (tags->chunk_id == chunk_obj &&
943 tags->obj_id == obj_id &&
944 !tags->is_deleted) ? 1 : 0;
948 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
949 struct yaffs_ext_tags *tags, int obj_id,
954 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
955 if (yaffs_check_chunk_bit
956 (dev, the_chunk / dev->param.chunks_per_block,
957 the_chunk % dev->param.chunks_per_block)) {
959 if (dev->chunk_grp_size == 1)
962 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
964 if (yaffs_tags_match(tags,
965 obj_id, inode_chunk)) {
976 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
977 struct yaffs_ext_tags *tags)
979 /*Get the Tnode, then get the level 0 offset chunk offset */
980 struct yaffs_tnode *tn;
982 struct yaffs_ext_tags local_tags;
984 struct yaffs_dev *dev = in->my_dev;
987 /* Passed a NULL, so use our own tags space */
991 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
996 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
998 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1003 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1004 struct yaffs_ext_tags *tags)
1006 /* Get the Tnode, then get the level 0 offset chunk offset */
1007 struct yaffs_tnode *tn;
1009 struct yaffs_ext_tags local_tags;
1010 struct yaffs_dev *dev = in->my_dev;
1014 /* Passed a NULL, so use our own tags space */
1018 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1023 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1025 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1028 /* Delete the entry in the filestructure (if found) */
1030 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1035 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1036 int nand_chunk, int in_scan)
1038 /* NB in_scan is zero unless scanning.
1039 * For forward scanning, in_scan is > 0;
1040 * for backward scanning in_scan is < 0
1042 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1045 struct yaffs_tnode *tn;
1046 struct yaffs_dev *dev = in->my_dev;
1048 struct yaffs_ext_tags existing_tags;
1049 struct yaffs_ext_tags new_tags;
1050 unsigned existing_serial, new_serial;
1052 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1053 /* Just ignore an attempt at putting a chunk into a non-file
1055 * If it is not during Scanning then something went wrong!
1058 yaffs_trace(YAFFS_TRACE_ERROR,
1059 "yaffs tragedy:attempt to put data chunk into a non-file"
1064 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1068 tn = yaffs_add_find_tnode_0(dev,
1069 &in->variant.file_variant,
1075 /* Dummy insert, bail now */
1078 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1081 /* If we're scanning then we need to test for duplicates
1082 * NB This does not need to be efficient since it should only
1083 * happen when the power fails during a write, then only one
1084 * chunk should ever be affected.
1086 * Correction for YAFFS2: This could happen quite a lot and we
1087 * need to think about efficiency! TODO
1088 * Update: For backward scanning we don't need to re-read tags
1089 * so this is quite cheap.
1092 if (existing_cunk > 0) {
1093 /* NB Right now existing chunk will not be real
1094 * chunk_id if the chunk group size > 1
1095 * thus we have to do a FindChunkInFile to get the
1098 * We have a duplicate now we need to decide which
1101 * Backwards scanning YAFFS2: The old one is what
1102 * we use, dump the new one.
1103 * YAFFS1: Get both sets of tags and compare serial
1108 /* Only do this for forward scanning */
1109 yaffs_rd_chunk_tags_nand(dev,
1113 /* Do a proper find */
1115 yaffs_find_chunk_in_file(in, inode_chunk,
1119 if (existing_cunk <= 0) {
1120 /*Hoosterman - how did this happen? */
1122 yaffs_trace(YAFFS_TRACE_ERROR,
1123 "yaffs tragedy: existing chunk < 0 in scan"
1128 /* NB The deleted flags should be false, otherwise
1129 * the chunks will not be loaded during a scan
1133 new_serial = new_tags.serial_number;
1134 existing_serial = existing_tags.serial_number;
1137 if ((in_scan > 0) &&
1138 (existing_cunk <= 0 ||
1139 ((existing_serial + 1) & 3) == new_serial)) {
1140 /* Forward scanning.
1142 * Delete the old one and drop through to
1145 yaffs_chunk_del(dev, existing_cunk, 1,
1148 /* Backward scanning or we want to use the
1150 * Delete the new one and return early so that
1151 * the tnode isn't changed
1153 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1160 if (existing_cunk == 0)
1161 in->n_data_chunks++;
1163 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1168 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1170 struct yaffs_block_info *the_block;
1173 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1175 block_no = chunk / dev->param.chunks_per_block;
1176 the_block = yaffs_get_block_info(dev, block_no);
1178 the_block->soft_del_pages++;
1179 dev->n_free_chunks++;
1180 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1184 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1185 * the chunks in the file.
1186 * All soft deleting does is increment the block's softdelete count and pulls
1187 * the chunk out of the tnode.
1188 * Thus, essentially this is the same as DeleteWorker except that the chunks
1192 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1193 u32 level, int chunk_offset)
1198 struct yaffs_dev *dev = in->my_dev;
1204 for (i = YAFFS_NTNODES_INTERNAL - 1;
1207 if (tn->internal[i]) {
1209 yaffs_soft_del_worker(in,
1213 YAFFS_TNODES_INTERNAL_BITS)
1216 yaffs_free_tnode(dev,
1218 tn->internal[i] = NULL;
1220 /* Can this happen? */
1224 return (all_done) ? 1 : 0;
1228 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1229 the_chunk = yaffs_get_group_base(dev, tn, i);
1231 yaffs_soft_del_chunk(dev, the_chunk);
1232 yaffs_load_tnode_0(dev, tn, i, 0);
1238 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1240 struct yaffs_dev *dev = obj->my_dev;
1241 struct yaffs_obj *parent;
1243 yaffs_verify_obj_in_dir(obj);
1244 parent = obj->parent;
1246 yaffs_verify_dir(parent);
1248 if (dev && dev->param.remove_obj_fn)
1249 dev->param.remove_obj_fn(obj);
1251 list_del_init(&obj->siblings);
1254 yaffs_verify_dir(parent);
1257 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1260 yaffs_trace(YAFFS_TRACE_ALWAYS,
1261 "tragedy: Trying to add an object to a null pointer directory"
1266 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1267 yaffs_trace(YAFFS_TRACE_ALWAYS,
1268 "tragedy: Trying to add an object to a non-directory"
1273 if (obj->siblings.prev == NULL) {
1274 /* Not initialised */
1278 yaffs_verify_dir(directory);
1280 yaffs_remove_obj_from_dir(obj);
1283 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1284 obj->parent = directory;
1286 if (directory == obj->my_dev->unlinked_dir
1287 || directory == obj->my_dev->del_dir) {
1289 obj->my_dev->n_unlinked_files++;
1290 obj->rename_allowed = 0;
1293 yaffs_verify_dir(directory);
1294 yaffs_verify_obj_in_dir(obj);
1297 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1298 struct yaffs_obj *new_dir,
1299 const YCHAR *new_name, int force, int shadows)
1303 struct yaffs_obj *existing_target;
1305 if (new_dir == NULL)
1306 new_dir = obj->parent; /* use the old directory */
1308 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1309 yaffs_trace(YAFFS_TRACE_ALWAYS,
1310 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1315 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1316 del_op = (new_dir == obj->my_dev->del_dir);
1318 existing_target = yaffs_find_by_name(new_dir, new_name);
1320 /* If the object is a file going into the unlinked directory,
1321 * then it is OK to just stuff it in since duplicate names are OK.
1322 * else only proceed if the new name does not exist and we're putting
1323 * it into a directory.
1325 if (!(unlink_op || del_op || force ||
1326 shadows > 0 || !existing_target) ||
1327 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1330 yaffs_set_obj_name(obj, new_name);
1332 yaffs_add_obj_to_dir(new_dir, obj);
1337 /* If it is a deletion then we mark it as a shrink for gc */
1338 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1344 /*------------------------ Short Operations Cache ------------------------------
1345 * In many situations where there is no high level buffering a lot of
1346 * reads might be short sequential reads, and a lot of writes may be short
1347 * sequential writes. eg. scanning/writing a jpeg file.
1348 * In these cases, a short read/write cache can provide a huge perfomance
1349 * benefit with dumb-as-a-rock code.
1350 * In Linux, the page cache provides read buffering and the short op cache
1351 * provides write buffering.
1353 * There are a small number (~10) of cache chunks per device so that we don't
1354 * need a very intelligent search.
1357 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1359 struct yaffs_dev *dev = obj->my_dev;
1361 struct yaffs_cache *cache;
1362 int n_caches = obj->my_dev->param.n_caches;
1364 for (i = 0; i < n_caches; i++) {
1365 cache = &dev->cache[i];
1366 if (cache->object == obj && cache->dirty)
1373 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1375 struct yaffs_dev *dev = obj->my_dev;
1376 int lowest = -99; /* Stop compiler whining. */
1378 struct yaffs_cache *cache;
1379 int chunk_written = 0;
1380 int n_caches = obj->my_dev->param.n_caches;
1387 /* Find the lowest dirty chunk for this object */
1388 for (i = 0; i < n_caches; i++) {
1389 if (dev->cache[i].object == obj &&
1390 dev->cache[i].dirty) {
1392 dev->cache[i].chunk_id < lowest) {
1393 cache = &dev->cache[i];
1394 lowest = cache->chunk_id;
1399 if (cache && !cache->locked) {
1400 /* Write it out and free it up */
1402 yaffs_wr_data_obj(cache->object,
1407 cache->object = NULL;
1409 } while (cache && chunk_written > 0);
1412 /* Hoosterman, disk full while writing cache out. */
1413 yaffs_trace(YAFFS_TRACE_ERROR,
1414 "yaffs tragedy: no space during cache write");
1417 /*yaffs_flush_whole_cache(dev)
1422 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1424 struct yaffs_obj *obj;
1425 int n_caches = dev->param.n_caches;
1428 /* Find a dirty object in the cache and flush it...
1429 * until there are no further dirty objects.
1433 for (i = 0; i < n_caches && !obj; i++) {
1434 if (dev->cache[i].object && dev->cache[i].dirty)
1435 obj = dev->cache[i].object;
1438 yaffs_flush_file_cache(obj);
1443 /* Grab us a cache chunk for use.
1444 * First look for an empty one.
1445 * Then look for the least recently used non-dirty one.
1446 * Then look for the least recently used dirty one...., flush and look again.
1448 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1452 if (dev->param.n_caches > 0) {
1453 for (i = 0; i < dev->param.n_caches; i++) {
1454 if (!dev->cache[i].object)
1455 return &dev->cache[i];
1461 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1463 struct yaffs_cache *cache;
1464 struct yaffs_obj *the_obj;
1469 if (dev->param.n_caches < 1)
1472 /* Try find a non-dirty one... */
1474 cache = yaffs_grab_chunk_worker(dev);
1477 /* They were all dirty, find the LRU object and flush
1478 * its cache, then find again.
1479 * NB what's here is not very accurate,
1480 * we actually flush the object with the LRU chunk.
1483 /* With locking we can't assume we can use entry zero,
1484 * Set the_obj to a valid pointer for Coverity. */
1485 the_obj = dev->cache[0].object;
1490 for (i = 0; i < dev->param.n_caches; i++) {
1491 if (dev->cache[i].object &&
1492 !dev->cache[i].locked &&
1493 (dev->cache[i].last_use < usage ||
1495 usage = dev->cache[i].last_use;
1496 the_obj = dev->cache[i].object;
1497 cache = &dev->cache[i];
1502 if (!cache || cache->dirty) {
1503 /* Flush and try again */
1504 yaffs_flush_file_cache(the_obj);
1505 cache = yaffs_grab_chunk_worker(dev);
1511 /* Find a cached chunk */
1512 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1515 struct yaffs_dev *dev = obj->my_dev;
1518 if (dev->param.n_caches < 1)
1521 for (i = 0; i < dev->param.n_caches; i++) {
1522 if (dev->cache[i].object == obj &&
1523 dev->cache[i].chunk_id == chunk_id) {
1526 return &dev->cache[i];
1532 /* Mark the chunk for the least recently used algorithym */
1533 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1538 if (dev->param.n_caches < 1)
1541 if (dev->cache_last_use < 0 ||
1542 dev->cache_last_use > 100000000) {
1543 /* Reset the cache usages */
1544 for (i = 1; i < dev->param.n_caches; i++)
1545 dev->cache[i].last_use = 0;
1547 dev->cache_last_use = 0;
1549 dev->cache_last_use++;
1550 cache->last_use = dev->cache_last_use;
1556 /* Invalidate a single cache page.
1557 * Do this when a whole page gets written,
1558 * ie the short cache for this page is no longer valid.
1560 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1562 struct yaffs_cache *cache;
1564 if (object->my_dev->param.n_caches > 0) {
1565 cache = yaffs_find_chunk_cache(object, chunk_id);
1568 cache->object = NULL;
1572 /* Invalidate all the cache pages associated with this object
1573 * Do this whenever ther file is deleted or resized.
1575 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1578 struct yaffs_dev *dev = in->my_dev;
1580 if (dev->param.n_caches > 0) {
1581 /* Invalidate it. */
1582 for (i = 0; i < dev->param.n_caches; i++) {
1583 if (dev->cache[i].object == in)
1584 dev->cache[i].object = NULL;
1589 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1592 struct yaffs_dev *dev = obj->my_dev;
1594 /* If it is still linked into the bucket list, free from the list */
1595 if (!list_empty(&obj->hash_link)) {
1596 list_del_init(&obj->hash_link);
1597 bucket = yaffs_hash_fn(obj->obj_id);
1598 dev->obj_bucket[bucket].count--;
1602 /* FreeObject frees up a Object and puts it back on the free list */
1603 static void yaffs_free_obj(struct yaffs_obj *obj)
1605 struct yaffs_dev *dev;
1612 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1613 obj, obj->my_inode);
1616 if (!list_empty(&obj->siblings))
1619 if (obj->my_inode) {
1620 /* We're still hooked up to a cached inode.
1621 * Don't delete now, but mark for later deletion
1623 obj->defered_free = 1;
1627 yaffs_unhash_obj(obj);
1629 yaffs_free_raw_obj(dev, obj);
1631 dev->checkpoint_blocks_required = 0; /* force recalculation */
1634 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1636 if (obj->defered_free)
1637 yaffs_free_obj(obj);
1640 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1642 /* Iinvalidate the file's data in the cache, without flushing. */
1643 yaffs_invalidate_whole_cache(in);
1645 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1646 /* Move to unlinked directory so we have a deletion record */
1647 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1651 yaffs_remove_obj_from_dir(in);
1652 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1660 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1662 if (!obj->deleted ||
1663 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1667 if (obj->n_data_chunks <= 0) {
1668 /* Empty file with no duplicate object headers,
1669 * just delete it immediately */
1670 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1671 obj->variant.file_variant.top = NULL;
1672 yaffs_trace(YAFFS_TRACE_TRACING,
1673 "yaffs: Deleting empty file %d",
1675 yaffs_generic_obj_del(obj);
1677 yaffs_soft_del_worker(obj,
1678 obj->variant.file_variant.top,
1680 file_variant.top_level, 0);
1685 /* Pruning removes any part of the file structure tree that is beyond the
1686 * bounds of the file (ie that does not point to chunks).
1688 * A file should only get pruned when its size is reduced.
1690 * Before pruning, the chunks must be pulled from the tree and the
1691 * level 0 tnode entries must be zeroed out.
1692 * Could also use this for file deletion, but that's probably better handled
1693 * by a special case.
1695 * This function is recursive. For levels > 0 the function is called again on
1696 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1697 * If there is no data in a subtree then it is pruned.
1700 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1701 struct yaffs_tnode *tn, u32 level,
1713 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1714 if (tn->internal[i]) {
1716 yaffs_prune_worker(dev,
1719 (i == 0) ? del0 : 1);
1722 if (tn->internal[i])
1726 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1727 u32 *map = (u32 *) tn;
1729 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1735 if (has_data == 0 && del0) {
1736 /* Free and return NULL */
1737 yaffs_free_tnode(dev, tn);
1743 static int yaffs_prune_tree(struct yaffs_dev *dev,
1744 struct yaffs_file_var *file_struct)
1749 struct yaffs_tnode *tn;
1751 if (file_struct->top_level < 1)
1755 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1757 /* Now we have a tree with all the non-zero branches NULL but
1758 * the height is the same as it was.
1759 * Let's see if we can trim internal tnodes to shorten the tree.
1760 * We can do this if only the 0th element in the tnode is in use
1761 * (ie all the non-zero are NULL)
1764 while (file_struct->top_level && !done) {
1765 tn = file_struct->top;
1768 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1769 if (tn->internal[i])
1774 file_struct->top = tn->internal[0];
1775 file_struct->top_level--;
1776 yaffs_free_tnode(dev, tn);
1785 /*-------------------- End of File Structure functions.-------------------*/
1787 /* alloc_empty_obj gets us a clean Object.*/
1788 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1790 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1797 /* Now sweeten it up... */
1799 memset(obj, 0, sizeof(struct yaffs_obj));
1800 obj->being_created = 1;
1804 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1805 INIT_LIST_HEAD(&(obj->hard_links));
1806 INIT_LIST_HEAD(&(obj->hash_link));
1807 INIT_LIST_HEAD(&obj->siblings);
1809 /* Now make the directory sane */
1810 if (dev->root_dir) {
1811 obj->parent = dev->root_dir;
1812 list_add(&(obj->siblings),
1813 &dev->root_dir->variant.dir_variant.children);
1816 /* Add it to the lost and found directory.
1817 * NB Can't put root or lost-n-found in lost-n-found so
1818 * check if lost-n-found exists first
1820 if (dev->lost_n_found)
1821 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1823 obj->being_created = 0;
1825 dev->checkpoint_blocks_required = 0; /* force recalculation */
1830 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1834 int lowest = 999999;
1836 /* Search for the shortest list or one that
1840 for (i = 0; i < 10 && lowest > 4; i++) {
1841 dev->bucket_finder++;
1842 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1843 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1844 lowest = dev->obj_bucket[dev->bucket_finder].count;
1845 l = dev->bucket_finder;
1852 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1854 int bucket = yaffs_find_nice_bucket(dev);
1856 struct list_head *i;
1857 u32 n = (u32) bucket;
1859 /* Now find an object value that has not already been taken
1860 * by scanning the list.
1865 n += YAFFS_NOBJECT_BUCKETS;
1866 if (1 || dev->obj_bucket[bucket].count > 0) {
1867 list_for_each(i, &dev->obj_bucket[bucket].list) {
1868 /* If there is already one in the list */
1869 if (i && list_entry(i, struct yaffs_obj,
1870 hash_link)->obj_id == n) {
1879 static void yaffs_hash_obj(struct yaffs_obj *in)
1881 int bucket = yaffs_hash_fn(in->obj_id);
1882 struct yaffs_dev *dev = in->my_dev;
1884 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1885 dev->obj_bucket[bucket].count++;
1888 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1890 int bucket = yaffs_hash_fn(number);
1891 struct list_head *i;
1892 struct yaffs_obj *in;
1894 list_for_each(i, &dev->obj_bucket[bucket].list) {
1895 /* Look if it is in the list */
1896 in = list_entry(i, struct yaffs_obj, hash_link);
1897 if (in->obj_id == number) {
1898 /* Don't show if it is defered free */
1899 if (in->defered_free)
1908 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1909 enum yaffs_obj_type type)
1911 struct yaffs_obj *the_obj = NULL;
1912 struct yaffs_tnode *tn = NULL;
1915 number = yaffs_new_obj_id(dev);
1917 if (type == YAFFS_OBJECT_TYPE_FILE) {
1918 tn = yaffs_get_tnode(dev);
1923 the_obj = yaffs_alloc_empty_obj(dev);
1926 yaffs_free_tnode(dev, tn);
1931 the_obj->rename_allowed = 1;
1932 the_obj->unlink_allowed = 1;
1933 the_obj->obj_id = number;
1934 yaffs_hash_obj(the_obj);
1935 the_obj->variant_type = type;
1936 yaffs_load_current_time(the_obj, 1, 1);
1939 case YAFFS_OBJECT_TYPE_FILE:
1940 the_obj->variant.file_variant.file_size = 0;
1941 the_obj->variant.file_variant.scanned_size = 0;
1942 the_obj->variant.file_variant.shrink_size =
1943 yaffs_max_file_size(dev);
1944 the_obj->variant.file_variant.top_level = 0;
1945 the_obj->variant.file_variant.top = tn;
1947 case YAFFS_OBJECT_TYPE_DIRECTORY:
1948 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1949 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1951 case YAFFS_OBJECT_TYPE_SYMLINK:
1952 case YAFFS_OBJECT_TYPE_HARDLINK:
1953 case YAFFS_OBJECT_TYPE_SPECIAL:
1954 /* No action required */
1956 case YAFFS_OBJECT_TYPE_UNKNOWN:
1957 /* todo this should not happen */
1963 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1964 int number, u32 mode)
1967 struct yaffs_obj *obj =
1968 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1973 obj->fake = 1; /* it is fake so it might not use NAND */
1974 obj->rename_allowed = 0;
1975 obj->unlink_allowed = 0;
1978 obj->yst_mode = mode;
1980 obj->hdr_chunk = 0; /* Not a valid chunk. */
1986 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
1992 yaffs_init_raw_tnodes_and_objs(dev);
1994 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1995 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
1996 dev->obj_bucket[i].count = 0;
2000 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2002 enum yaffs_obj_type type)
2004 struct yaffs_obj *the_obj = NULL;
2007 the_obj = yaffs_find_by_number(dev, number);
2010 the_obj = yaffs_new_obj(dev, number, type);
2016 YCHAR *yaffs_clone_str(const YCHAR *str)
2018 YCHAR *new_str = NULL;
2024 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2025 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2027 strncpy(new_str, str, len);
2034 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2035 * link (ie. name) is created or deleted in the directory.
2038 * create dir/a : update dir's mtime/ctime
2039 * rm dir/a: update dir's mtime/ctime
2040 * modify dir/a: don't update dir's mtimme/ctime
2042 * This can be handled immediately or defered. Defering helps reduce the number
2043 * of updates when many files in a directory are changed within a brief period.
2045 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2046 * called periodically.
2049 static void yaffs_update_parent(struct yaffs_obj *obj)
2051 struct yaffs_dev *dev;
2057 yaffs_load_current_time(obj, 0, 1);
2058 if (dev->param.defered_dir_update) {
2059 struct list_head *link = &obj->variant.dir_variant.dirty;
2061 if (list_empty(link)) {
2062 list_add(link, &dev->dirty_dirs);
2063 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2064 "Added object %d to dirty directories",
2069 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2073 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2075 struct list_head *link;
2076 struct yaffs_obj *obj;
2077 struct yaffs_dir_var *d_s;
2078 union yaffs_obj_var *o_v;
2080 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2082 while (!list_empty(&dev->dirty_dirs)) {
2083 link = dev->dirty_dirs.next;
2084 list_del_init(link);
2086 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2087 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2088 obj = list_entry(o_v, struct yaffs_obj, variant);
2090 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2094 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2099 * Mknod (create) a new object.
2100 * equiv_obj only has meaning for a hard link;
2101 * alias_str only has meaning for a symlink.
2102 * rdev only has meaning for devices (a subset of special objects)
2105 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2106 struct yaffs_obj *parent,
2111 struct yaffs_obj *equiv_obj,
2112 const YCHAR *alias_str, u32 rdev)
2114 struct yaffs_obj *in;
2116 struct yaffs_dev *dev = parent->my_dev;
2118 /* Check if the entry exists.
2119 * If it does then fail the call since we don't want a dup. */
2120 if (yaffs_find_by_name(parent, name))
2123 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2124 str = yaffs_clone_str(alias_str);
2129 in = yaffs_new_obj(dev, -1, type);
2138 in->variant_type = type;
2140 in->yst_mode = mode;
2142 yaffs_attribs_init(in, gid, uid, rdev);
2144 in->n_data_chunks = 0;
2146 yaffs_set_obj_name(in, name);
2149 yaffs_add_obj_to_dir(parent, in);
2151 in->my_dev = parent->my_dev;
2154 case YAFFS_OBJECT_TYPE_SYMLINK:
2155 in->variant.symlink_variant.alias = str;
2157 case YAFFS_OBJECT_TYPE_HARDLINK:
2158 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2159 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
2160 list_add(&in->hard_links, &equiv_obj->hard_links);
2162 case YAFFS_OBJECT_TYPE_FILE:
2163 case YAFFS_OBJECT_TYPE_DIRECTORY:
2164 case YAFFS_OBJECT_TYPE_SPECIAL:
2165 case YAFFS_OBJECT_TYPE_UNKNOWN:
2170 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2171 /* Could not create the object header, fail */
2177 yaffs_update_parent(parent);
2182 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2183 const YCHAR *name, u32 mode, u32 uid,
2186 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2187 uid, gid, NULL, NULL, 0);
2190 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2191 u32 mode, u32 uid, u32 gid)
2193 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2194 mode, uid, gid, NULL, NULL, 0);
2197 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2198 const YCHAR *name, u32 mode, u32 uid,
2201 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2202 uid, gid, NULL, NULL, rdev);
2205 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2206 const YCHAR *name, u32 mode, u32 uid,
2207 u32 gid, const YCHAR *alias)
2209 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2210 uid, gid, NULL, alias, 0);
2213 /* yaffs_link_obj returns the object id of the equivalent object.*/
2214 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2215 struct yaffs_obj *equiv_obj)
2217 /* Get the real object in case we were fed a hard link obj */
2218 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2220 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2221 parent, name, 0, 0, 0,
2222 equiv_obj, NULL, 0))
2231 /*---------------------- Block Management and Page Allocation -------------*/
2233 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2235 if (dev->block_info_alt && dev->block_info)
2236 vfree(dev->block_info);
2238 kfree(dev->block_info);
2240 dev->block_info_alt = 0;
2242 dev->block_info = NULL;
2244 if (dev->chunk_bits_alt && dev->chunk_bits)
2245 vfree(dev->chunk_bits);
2247 kfree(dev->chunk_bits);
2248 dev->chunk_bits_alt = 0;
2249 dev->chunk_bits = NULL;
2252 static int yaffs_init_blocks(struct yaffs_dev *dev)
2254 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2256 dev->block_info = NULL;
2257 dev->chunk_bits = NULL;
2258 dev->alloc_block = -1; /* force it to get a new one */
2260 /* If the first allocation strategy fails, thry the alternate one */
2262 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2263 if (!dev->block_info) {
2265 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2266 dev->block_info_alt = 1;
2268 dev->block_info_alt = 0;
2271 if (!dev->block_info)
2274 /* Set up dynamic blockinfo stuff. Round up bytes. */
2275 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2277 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2278 if (!dev->chunk_bits) {
2280 vmalloc(dev->chunk_bit_stride * n_blocks);
2281 dev->chunk_bits_alt = 1;
2283 dev->chunk_bits_alt = 0;
2285 if (!dev->chunk_bits)
2289 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2290 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2294 yaffs_deinit_blocks(dev);
2299 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2301 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2305 /* If the block is still healthy erase it and mark as clean.
2306 * If the block has had a data failure, then retire it.
2309 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2310 "yaffs_block_became_dirty block %d state %d %s",
2311 block_no, bi->block_state,
2312 (bi->needs_retiring) ? "needs retiring" : "");
2314 yaffs2_clear_oldest_dirty_seq(dev, bi);
2316 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2318 /* If this is the block being garbage collected then stop gc'ing */
2319 if (block_no == dev->gc_block)
2322 /* If this block is currently the best candidate for gc
2323 * then drop as a candidate */
2324 if (block_no == dev->gc_dirtiest) {
2325 dev->gc_dirtiest = 0;
2326 dev->gc_pages_in_use = 0;
2329 if (!bi->needs_retiring) {
2330 yaffs2_checkpt_invalidate(dev);
2331 erased_ok = yaffs_erase_block(dev, block_no);
2333 dev->n_erase_failures++;
2334 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2335 "**>> Erasure failed %d", block_no);
2339 /* Verify erasure if needed */
2341 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2342 !yaffs_skip_verification(dev))) {
2343 for (i = 0; i < dev->param.chunks_per_block; i++) {
2344 if (!yaffs_check_chunk_erased(dev,
2345 block_no * dev->param.chunks_per_block + i)) {
2346 yaffs_trace(YAFFS_TRACE_ERROR,
2347 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2354 /* We lost a block of free space */
2355 dev->n_free_chunks -= dev->param.chunks_per_block;
2356 yaffs_retire_block(dev, block_no);
2357 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2358 "**>> Block %d retired", block_no);
2362 /* Clean it up... */
2363 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2365 dev->n_erased_blocks++;
2366 bi->pages_in_use = 0;
2367 bi->soft_del_pages = 0;
2368 bi->has_shrink_hdr = 0;
2369 bi->skip_erased_check = 1; /* Clean, so no need to check */
2370 bi->gc_prioritise = 0;
2371 bi->has_summary = 0;
2373 yaffs_clear_chunk_bits(dev, block_no);
2375 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2378 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2379 struct yaffs_block_info *bi,
2380 int old_chunk, u8 *buffer)
2384 struct yaffs_ext_tags tags;
2385 struct yaffs_obj *object;
2387 int ret_val = YAFFS_OK;
2389 memset(&tags, 0, sizeof(tags));
2390 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2392 object = yaffs_find_by_number(dev, tags.obj_id);
2394 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2395 "Collecting chunk in block %d, %d %d %d ",
2396 dev->gc_chunk, tags.obj_id,
2397 tags.chunk_id, tags.n_bytes);
2399 if (object && !yaffs_skip_verification(dev)) {
2400 if (tags.chunk_id == 0)
2403 else if (object->soft_del)
2404 /* Defeat the test */
2405 matching_chunk = old_chunk;
2408 yaffs_find_chunk_in_file
2409 (object, tags.chunk_id,
2412 if (old_chunk != matching_chunk)
2413 yaffs_trace(YAFFS_TRACE_ERROR,
2414 "gc: page in gc mismatch: %d %d %d %d",
2422 yaffs_trace(YAFFS_TRACE_ERROR,
2423 "page %d in gc has no object: %d %d %d ",
2425 tags.obj_id, tags.chunk_id,
2431 object->soft_del && tags.chunk_id != 0) {
2432 /* Data chunk in a soft deleted file,
2434 * It's a soft deleted data chunk,
2435 * No need to copy this, just forget
2436 * about it and fix up the object.
2439 /* Free chunks already includes
2440 * softdeleted chunks, how ever this
2441 * chunk is going to soon be really
2442 * deleted which will increment free
2443 * chunks. We have to decrement free
2444 * chunks so this works out properly.
2446 dev->n_free_chunks--;
2447 bi->soft_del_pages--;
2449 object->n_data_chunks--;
2450 if (object->n_data_chunks <= 0) {
2451 /* remeber to clean up obj */
2452 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2456 } else if (object) {
2457 /* It's either a data chunk in a live
2458 * file or an ObjectHeader, so we're
2460 * NB Need to keep the ObjectHeaders of
2461 * deleted files until the whole file
2462 * has been deleted off
2464 tags.serial_number++;
2467 if (tags.chunk_id == 0) {
2468 /* It is an object Id,
2469 * We need to nuke the
2470 * shrinkheader flags since its
2472 * Also need to clean up
2475 struct yaffs_obj_hdr *oh;
2476 oh = (struct yaffs_obj_hdr *) buffer;
2479 tags.extra_is_shrink = 0;
2480 oh->shadows_obj = 0;
2481 oh->inband_shadowed_obj_id = 0;
2482 tags.extra_shadows = 0;
2484 /* Update file size */
2485 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2486 yaffs_oh_size_load(oh,
2487 object->variant.file_variant.file_size);
2488 tags.extra_file_size =
2489 object->variant.file_variant.file_size;
2492 yaffs_verify_oh(object, oh, &tags, 1);
2494 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2497 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2500 if (new_chunk < 0) {
2501 ret_val = YAFFS_FAIL;
2504 /* Now fix up the Tnodes etc. */
2506 if (tags.chunk_id == 0) {
2508 object->hdr_chunk = new_chunk;
2509 object->serial = tags.serial_number;
2511 /* It's a data chunk */
2512 yaffs_put_chunk_in_file(object, tags.chunk_id,
2517 if (ret_val == YAFFS_OK)
2518 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2522 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2525 int ret_val = YAFFS_OK;
2527 int is_checkpt_block;
2529 int chunks_before = yaffs_get_erased_chunks(dev);
2531 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2533 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2535 yaffs_trace(YAFFS_TRACE_TRACING,
2536 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2537 block, bi->pages_in_use, bi->has_shrink_hdr,
2540 /*yaffs_verify_free_chunks(dev); */
2542 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2543 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2545 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2547 dev->gc_disable = 1;
2549 yaffs_summary_gc(dev, block);
2551 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2552 yaffs_trace(YAFFS_TRACE_TRACING,
2553 "Collecting block %d that has no chunks in use",
2555 yaffs_block_became_dirty(dev, block);
2558 u8 *buffer = yaffs_get_temp_buffer(dev);
2560 yaffs_verify_blk(dev, bi, block);
2562 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2563 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2565 for (/* init already done */ ;
2566 ret_val == YAFFS_OK &&
2567 dev->gc_chunk < dev->param.chunks_per_block &&
2568 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2570 dev->gc_chunk++, old_chunk++) {
2571 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2572 /* Page is in use and might need to be copied */
2574 ret_val = yaffs_gc_process_chunk(dev, bi,
2578 yaffs_release_temp_buffer(dev, buffer);
2581 yaffs_verify_collected_blk(dev, bi, block);
2583 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2585 * The gc did not complete. Set block state back to FULL
2586 * because checkpointing does not restore gc.
2588 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2590 /* The gc completed. */
2591 /* Do any required cleanups */
2592 for (i = 0; i < dev->n_clean_ups; i++) {
2593 /* Time to delete the file too */
2594 struct yaffs_obj *object =
2595 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2597 yaffs_free_tnode(dev,
2598 object->variant.file_variant.top);
2599 object->variant.file_variant.top = NULL;
2600 yaffs_trace(YAFFS_TRACE_GC,
2601 "yaffs: About to finally delete object %d",
2603 yaffs_generic_obj_del(object);
2604 object->my_dev->n_deleted_files--;
2608 chunks_after = yaffs_get_erased_chunks(dev);
2609 if (chunks_before >= chunks_after)
2610 yaffs_trace(YAFFS_TRACE_GC,
2611 "gc did not increase free chunks before %d after %d",
2612 chunks_before, chunks_after);
2615 dev->n_clean_ups = 0;
2618 dev->gc_disable = 0;
2624 * find_gc_block() selects the dirtiest block (or close enough)
2625 * for garbage collection.
2628 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2629 int aggressive, int background)
2633 unsigned selected = 0;
2634 int prioritised = 0;
2635 int prioritised_exist = 0;
2636 struct yaffs_block_info *bi;
2639 /* First let's see if we need to grab a prioritised block */
2640 if (dev->has_pending_prioritised_gc && !aggressive) {
2641 dev->gc_dirtiest = 0;
2642 bi = dev->block_info;
2643 for (i = dev->internal_start_block;
2644 i <= dev->internal_end_block && !selected; i++) {
2646 if (bi->gc_prioritise) {
2647 prioritised_exist = 1;
2648 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2649 yaffs_block_ok_for_gc(dev, bi)) {
2658 * If there is a prioritised block and none was selected then
2659 * this happened because there is at least one old dirty block
2660 * gumming up the works. Let's gc the oldest dirty block.
2663 if (prioritised_exist &&
2664 !selected && dev->oldest_dirty_block > 0)
2665 selected = dev->oldest_dirty_block;
2667 if (!prioritised_exist) /* None found, so we can clear this */
2668 dev->has_pending_prioritised_gc = 0;
2671 /* If we're doing aggressive GC then we are happy to take a less-dirty
2672 * block, and search harder.
2673 * else (leasurely gc), then we only bother to do this if the
2674 * block has only a few pages in use.
2680 dev->internal_end_block - dev->internal_start_block + 1;
2682 threshold = dev->param.chunks_per_block;
2683 iterations = n_blocks;
2688 max_threshold = dev->param.chunks_per_block / 2;
2690 max_threshold = dev->param.chunks_per_block / 8;
2692 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2693 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2695 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2696 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2697 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2698 if (threshold > max_threshold)
2699 threshold = max_threshold;
2701 iterations = n_blocks / 16 + 1;
2702 if (iterations > 100)
2708 (dev->gc_dirtiest < 1 ||
2709 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2711 dev->gc_block_finder++;
2712 if (dev->gc_block_finder < dev->internal_start_block ||
2713 dev->gc_block_finder > dev->internal_end_block)
2714 dev->gc_block_finder =
2715 dev->internal_start_block;
2717 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2719 pages_used = bi->pages_in_use - bi->soft_del_pages;
2721 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2722 pages_used < dev->param.chunks_per_block &&
2723 (dev->gc_dirtiest < 1 ||
2724 pages_used < dev->gc_pages_in_use) &&
2725 yaffs_block_ok_for_gc(dev, bi)) {
2726 dev->gc_dirtiest = dev->gc_block_finder;
2727 dev->gc_pages_in_use = pages_used;
2731 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2732 selected = dev->gc_dirtiest;
2736 * If nothing has been selected for a while, try the oldest dirty
2737 * because that's gumming up the works.
2740 if (!selected && dev->param.is_yaffs2 &&
2741 dev->gc_not_done >= (background ? 10 : 20)) {
2742 yaffs2_find_oldest_dirty_seq(dev);
2743 if (dev->oldest_dirty_block > 0) {
2744 selected = dev->oldest_dirty_block;
2745 dev->gc_dirtiest = selected;
2746 dev->oldest_dirty_gc_count++;
2747 bi = yaffs_get_block_info(dev, selected);
2748 dev->gc_pages_in_use =
2749 bi->pages_in_use - bi->soft_del_pages;
2751 dev->gc_not_done = 0;
2756 yaffs_trace(YAFFS_TRACE_GC,
2757 "GC Selected block %d with %d free, prioritised:%d",
2759 dev->param.chunks_per_block - dev->gc_pages_in_use,
2766 dev->gc_dirtiest = 0;
2767 dev->gc_pages_in_use = 0;
2768 dev->gc_not_done = 0;
2769 if (dev->refresh_skip > 0)
2770 dev->refresh_skip--;
2773 yaffs_trace(YAFFS_TRACE_GC,
2774 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2775 dev->gc_block_finder, dev->gc_not_done, threshold,
2776 dev->gc_dirtiest, dev->gc_pages_in_use,
2777 dev->oldest_dirty_block, background ? " bg" : "");
2783 /* New garbage collector
2784 * If we're very low on erased blocks then we do aggressive garbage collection
2785 * otherwise we do "leasurely" garbage collection.
2786 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2787 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2789 * The idea is to help clear out space in a more spread-out manner.
2790 * Dunno if it really does anything useful.
2792 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2795 int gc_ok = YAFFS_OK;
2799 int checkpt_block_adjust;
2801 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2804 if (dev->gc_disable)
2805 /* Bail out so we don't get recursive gc */
2808 /* This loop should pass the first time.
2809 * Only loops here if the collection does not increase space.
2815 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2818 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2820 dev->n_erased_blocks * dev->param.chunks_per_block;
2822 /* If we need a block soon then do aggressive gc. */
2823 if (dev->n_erased_blocks < min_erased)
2827 && erased_chunks > (dev->n_free_chunks / 4))
2830 if (dev->gc_skip > 20)
2832 if (erased_chunks < dev->n_free_chunks / 2 ||
2833 dev->gc_skip < 1 || background)
2843 /* If we don't already have a block being gc'd then see if we
2844 * should start another */
2846 if (dev->gc_block < 1 && !aggressive) {
2847 dev->gc_block = yaffs2_find_refresh_block(dev);
2849 dev->n_clean_ups = 0;
2851 if (dev->gc_block < 1) {
2853 yaffs_find_gc_block(dev, aggressive, background);
2855 dev->n_clean_ups = 0;
2858 if (dev->gc_block > 0) {
2861 dev->passive_gc_count++;
2863 yaffs_trace(YAFFS_TRACE_GC,
2864 "yaffs: GC n_erased_blocks %d aggressive %d",
2865 dev->n_erased_blocks, aggressive);
2867 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2870 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
2871 dev->gc_block > 0) {
2872 yaffs_trace(YAFFS_TRACE_GC,
2873 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2874 dev->n_erased_blocks, max_tries,
2877 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2878 (dev->gc_block > 0) && (max_tries < 2));
2880 return aggressive ? gc_ok : YAFFS_OK;
2885 * Garbage collects. Intended to be called from a background thread.
2886 * Returns non-zero if at least half the free chunks are erased.
2888 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2890 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2892 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2894 yaffs_check_gc(dev, 1);
2895 return erased_chunks > dev->n_free_chunks / 2;
2898 /*-------------------- Data file manipulation -----------------*/
2900 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2902 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2904 if (nand_chunk >= 0)
2905 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2908 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2909 "Chunk %d not found zero instead",
2911 /* get sane (zero) data if you read a hole */
2912 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2918 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2923 struct yaffs_ext_tags tags;
2924 struct yaffs_block_info *bi;
2930 block = chunk_id / dev->param.chunks_per_block;
2931 page = chunk_id % dev->param.chunks_per_block;
2933 if (!yaffs_check_chunk_bit(dev, block, page))
2934 yaffs_trace(YAFFS_TRACE_VERIFY,
2935 "Deleting invalid chunk %d", chunk_id);
2937 bi = yaffs_get_block_info(dev, block);
2939 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2941 yaffs_trace(YAFFS_TRACE_DELETION,
2942 "line %d delete of chunk %d",
2945 if (!dev->param.is_yaffs2 && mark_flash &&
2946 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2948 memset(&tags, 0, sizeof(tags));
2949 tags.is_deleted = 1;
2950 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2951 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2953 dev->n_unmarked_deletions++;
2956 /* Pull out of the management area.
2957 * If the whole block became dirty, this will kick off an erasure.
2959 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2960 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2961 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
2962 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2963 dev->n_free_chunks++;
2964 yaffs_clear_chunk_bit(dev, block, page);
2967 if (bi->pages_in_use == 0 &&
2968 !bi->has_shrink_hdr &&
2969 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2970 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
2971 yaffs_block_became_dirty(dev, block);
2976 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2977 const u8 *buffer, int n_bytes, int use_reserve)
2979 /* Find old chunk Need to do this to get serial number
2980 * Write new one and patch into tree.
2981 * Invalidate old tags.
2985 struct yaffs_ext_tags prev_tags;
2987 struct yaffs_ext_tags new_tags;
2988 struct yaffs_dev *dev = in->my_dev;
2990 yaffs_check_gc(dev, 0);
2992 /* Get the previous chunk at this location in the file if it exists.
2993 * If it does not exist then put a zero into the tree. This creates
2994 * the tnode now, rather than later when it is harder to clean up.
2996 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
2997 if (prev_chunk_id < 1 &&
2998 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3001 /* Set up new tags */
3002 memset(&new_tags, 0, sizeof(new_tags));
3004 new_tags.chunk_id = inode_chunk;
3005 new_tags.obj_id = in->obj_id;
3006 new_tags.serial_number =
3007 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3008 new_tags.n_bytes = n_bytes;
3010 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3011 yaffs_trace(YAFFS_TRACE_ERROR,
3012 "Writing %d bytes to chunk!!!!!!!!!",
3018 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3020 if (new_chunk_id > 0) {
3021 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3023 if (prev_chunk_id > 0)
3024 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3026 yaffs_verify_file_sane(in);
3028 return new_chunk_id;
3034 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3035 const YCHAR *name, const void *value, int size,
3038 struct yaffs_xattr_mod xmod;
3046 xmod.result = -ENOSPC;
3048 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3056 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3057 struct yaffs_xattr_mod *xmod)
3060 int x_offs = sizeof(struct yaffs_obj_hdr);
3061 struct yaffs_dev *dev = obj->my_dev;
3062 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3063 char *x_buffer = buffer + x_offs;
3067 nval_set(x_buffer, x_size, xmod->name, xmod->data,
3068 xmod->size, xmod->flags);
3070 retval = nval_del(x_buffer, x_size, xmod->name);
3072 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3073 obj->xattr_known = 1;
3074 xmod->result = retval;
3079 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
3080 void *value, int size)
3082 char *buffer = NULL;
3084 struct yaffs_ext_tags tags;
3085 struct yaffs_dev *dev = obj->my_dev;
3086 int x_offs = sizeof(struct yaffs_obj_hdr);
3087 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3091 if (obj->hdr_chunk < 1)
3094 /* If we know that the object has no xattribs then don't do all the
3095 * reading and parsing.
3097 if (obj->xattr_known && !obj->has_xattr) {
3104 buffer = (char *)yaffs_get_temp_buffer(dev);
3109 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3111 if (result != YAFFS_OK)
3114 x_buffer = buffer + x_offs;
3116 if (!obj->xattr_known) {
3117 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3118 obj->xattr_known = 1;
3122 retval = nval_get(x_buffer, x_size, name, value, size);
3124 retval = nval_list(x_buffer, x_size, value, size);
3126 yaffs_release_temp_buffer(dev, (u8 *) buffer);
3130 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3131 const void *value, int size, int flags)
3133 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3136 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3138 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3141 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3144 return yaffs_do_xattrib_fetch(obj, name, value, size);
3147 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3149 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3152 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3155 struct yaffs_obj_hdr *oh;
3156 struct yaffs_dev *dev;
3157 struct yaffs_ext_tags tags;
3159 int alloc_failed = 0;
3161 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3165 in->lazy_loaded = 0;
3166 buf = yaffs_get_temp_buffer(dev);
3168 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3169 oh = (struct yaffs_obj_hdr *)buf;
3171 in->yst_mode = oh->yst_mode;
3172 yaffs_load_attribs(in, oh);
3173 yaffs_set_obj_name_from_oh(in, oh);
3175 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3176 in->variant.symlink_variant.alias =
3177 yaffs_clone_str(oh->alias);
3178 if (!in->variant.symlink_variant.alias)
3179 alloc_failed = 1; /* Not returned */
3181 yaffs_release_temp_buffer(dev, buf);
3184 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
3185 const YCHAR *oh_name, int buff_size)
3187 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3188 if (dev->param.auto_unicode) {
3190 /* It is an ASCII name, do an ASCII to
3191 * unicode conversion */
3192 const char *ascii_oh_name = (const char *)oh_name;
3193 int n = buff_size - 1;
3194 while (n > 0 && *ascii_oh_name) {
3195 *name = *ascii_oh_name;
3201 strncpy(name, oh_name + 1, buff_size - 1);
3208 strncpy(name, oh_name, buff_size - 1);
3212 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
3215 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3220 if (dev->param.auto_unicode) {
3225 /* Figure out if the name will fit in ascii character set */
3226 while (is_ascii && *w) {
3233 /* It is an ASCII name, so convert unicode to ascii */
3234 char *ascii_oh_name = (char *)oh_name;
3235 int n = YAFFS_MAX_NAME_LENGTH - 1;
3236 while (n > 0 && *name) {
3237 *ascii_oh_name = *name;
3243 /* Unicode name, so save starting at the second YCHAR */
3245 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
3252 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3256 /* UpdateObjectHeader updates the header on NAND for an object.
3257 * If name is not NULL, then that new name is used.
3259 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3260 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3263 struct yaffs_block_info *bi;
3264 struct yaffs_dev *dev = in->my_dev;
3269 struct yaffs_ext_tags new_tags;
3270 struct yaffs_ext_tags old_tags;
3271 const YCHAR *alias = NULL;
3273 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3274 struct yaffs_obj_hdr *oh = NULL;
3275 loff_t file_size = 0;
3277 strcpy(old_name, _Y("silly old name"));
3279 if (in->fake && in != dev->root_dir && !force && !xmod)
3282 yaffs_check_gc(dev, 0);
3283 yaffs_check_obj_details_loaded(in);
3285 buffer = yaffs_get_temp_buffer(in->my_dev);
3286 oh = (struct yaffs_obj_hdr *)buffer;
3288 prev_chunk_id = in->hdr_chunk;
3290 if (prev_chunk_id > 0) {
3291 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3294 yaffs_verify_oh(in, oh, &old_tags, 0);
3295 memcpy(old_name, oh->name, sizeof(oh->name));
3296 memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
3298 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3301 oh->type = in->variant_type;
3302 oh->yst_mode = in->yst_mode;
3303 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3305 yaffs_load_attribs_oh(oh, in);
3308 oh->parent_obj_id = in->parent->obj_id;
3310 oh->parent_obj_id = 0;
3312 if (name && *name) {
3313 memset(oh->name, 0, sizeof(oh->name));
3314 yaffs_load_oh_from_name(dev, oh->name, name);
3315 } else if (prev_chunk_id > 0) {
3316 memcpy(oh->name, old_name, sizeof(oh->name));
3318 memset(oh->name, 0, sizeof(oh->name));
3321 oh->is_shrink = is_shrink;
3323 switch (in->variant_type) {
3324 case YAFFS_OBJECT_TYPE_UNKNOWN:
3325 /* Should not happen */
3327 case YAFFS_OBJECT_TYPE_FILE:
3328 if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
3329 oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
3330 file_size = in->variant.file_variant.file_size;
3331 yaffs_oh_size_load(oh, file_size);
3333 case YAFFS_OBJECT_TYPE_HARDLINK:
3334 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3336 case YAFFS_OBJECT_TYPE_SPECIAL:
3339 case YAFFS_OBJECT_TYPE_DIRECTORY:
3342 case YAFFS_OBJECT_TYPE_SYMLINK:
3343 alias = in->variant.symlink_variant.alias;
3345 alias = _Y("no alias");
3346 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3347 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3351 /* process any xattrib modifications */
3353 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3356 memset(&new_tags, 0, sizeof(new_tags));
3358 new_tags.chunk_id = 0;
3359 new_tags.obj_id = in->obj_id;
3360 new_tags.serial_number = in->serial;
3362 /* Add extra info for file header */
3363 new_tags.extra_available = 1;
3364 new_tags.extra_parent_id = oh->parent_obj_id;
3365 new_tags.extra_file_size = file_size;
3366 new_tags.extra_is_shrink = oh->is_shrink;
3367 new_tags.extra_equiv_id = oh->equiv_id;
3368 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3369 new_tags.extra_obj_type = in->variant_type;
3370 yaffs_verify_oh(in, oh, &new_tags, 1);
3372 /* Create new chunk in NAND */
3374 yaffs_write_new_chunk(dev, buffer, &new_tags,
3375 (prev_chunk_id > 0) ? 1 : 0);
3378 yaffs_release_temp_buffer(dev, buffer);
3380 if (new_chunk_id < 0)
3381 return new_chunk_id;
3383 in->hdr_chunk = new_chunk_id;
3385 if (prev_chunk_id > 0)
3386 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3388 if (!yaffs_obj_cache_dirty(in))
3391 /* If this was a shrink, then mark the block
3392 * that the chunk lives on */
3394 bi = yaffs_get_block_info(in->my_dev,
3396 in->my_dev->param.chunks_per_block);
3397 bi->has_shrink_hdr = 1;
3401 return new_chunk_id;
3404 /*--------------------- File read/write ------------------------
3405 * Read and write have very similar structures.
3406 * In general the read/write has three parts to it
3407 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3408 * Some complete chunks
3409 * An incomplete chunk to end off with
3411 * Curve-balls: the first chunk might also be the last chunk.
3414 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3421 struct yaffs_cache *cache;
3422 struct yaffs_dev *dev;
3427 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3430 /* OK now check for the curveball where the start and end are in
3433 if ((start + n) < dev->data_bytes_per_chunk)
3436 n_copy = dev->data_bytes_per_chunk - start;
3438 cache = yaffs_find_chunk_cache(in, chunk);
3440 /* If the chunk is already in the cache or it is less than
3441 * a whole chunk or we're using inband tags then use the cache
3442 * (if there is caching) else bypass the cache.
3444 if (cache || n_copy != dev->data_bytes_per_chunk ||
3445 dev->param.inband_tags) {
3446 if (dev->param.n_caches > 0) {
3448 /* If we can't find the data in the cache,
3449 * then load it up. */
3453 yaffs_grab_chunk_cache(in->my_dev);
3455 cache->chunk_id = chunk;
3458 yaffs_rd_data_obj(in, chunk,
3463 yaffs_use_cache(dev, cache, 0);
3467 memcpy(buffer, &cache->data[start], n_copy);
3471 /* Read into the local buffer then copy.. */
3474 yaffs_get_temp_buffer(dev);
3475 yaffs_rd_data_obj(in, chunk, local_buffer);
3477 memcpy(buffer, &local_buffer[start], n_copy);
3479 yaffs_release_temp_buffer(dev, local_buffer);
3482 /* A full chunk. Read directly into the buffer. */
3483 yaffs_rd_data_obj(in, chunk, buffer);
3493 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3494 int n_bytes, int write_through)
3503 loff_t start_write = offset;
3504 int chunk_written = 0;
3507 struct yaffs_dev *dev;
3511 while (n > 0 && chunk_written >= 0) {
3512 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3514 if (((loff_t)chunk) *
3515 dev->data_bytes_per_chunk + start != offset ||
3516 start >= dev->data_bytes_per_chunk) {
3517 yaffs_trace(YAFFS_TRACE_ERROR,
3518 "AddrToChunk of offset %lld gives chunk %d start %d",
3519 offset, chunk, start);
3521 chunk++; /* File pos to chunk in file offset */
3523 /* OK now check for the curveball where the start and end are in
3527 if ((start + n) < dev->data_bytes_per_chunk) {
3530 /* Now calculate how many bytes to write back....
3531 * If we're overwriting and not writing to then end of
3532 * file then we need to write back as much as was there
3536 chunk_start = (((loff_t)(chunk - 1)) *
3537 dev->data_bytes_per_chunk);
3539 if (chunk_start > in->variant.file_variant.file_size)
3540 n_bytes_read = 0; /* Past end of file */
3543 in->variant.file_variant.file_size -
3546 if (n_bytes_read > dev->data_bytes_per_chunk)
3547 n_bytes_read = dev->data_bytes_per_chunk;
3551 (start + n)) ? n_bytes_read : (start + n);
3553 if (n_writeback < 0 ||
3554 n_writeback > dev->data_bytes_per_chunk)
3558 n_copy = dev->data_bytes_per_chunk - start;
3559 n_writeback = dev->data_bytes_per_chunk;
3562 if (n_copy != dev->data_bytes_per_chunk ||
3563 !dev->param.cache_bypass_aligned ||
3564 dev->param.inband_tags) {
3565 /* An incomplete start or end chunk (or maybe both
3566 * start and end chunk), or we're using inband tags,
3567 * or we're forcing writes through the cache,
3568 * so we want to use the cache buffers.
3570 if (dev->param.n_caches > 0) {
3571 struct yaffs_cache *cache;
3573 /* If we can't find the data in the cache, then
3575 cache = yaffs_find_chunk_cache(in, chunk);
3578 yaffs_check_alloc_available(dev, 1)) {
3579 cache = yaffs_grab_chunk_cache(dev);
3581 cache->chunk_id = chunk;
3584 yaffs_rd_data_obj(in, chunk,
3588 !yaffs_check_alloc_available(dev,
3590 /* Drop the cache if it was a read cache
3591 * item and no space check has been made
3598 yaffs_use_cache(dev, cache, 1);
3601 memcpy(&cache->data[start], buffer,
3605 cache->n_bytes = n_writeback;
3607 if (write_through) {
3617 chunk_written = -1; /* fail write */
3620 /* An incomplete start or end chunk (or maybe
3621 * both start and end chunk). Read into the
3622 * local buffer then copy over and write back.
3625 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3627 yaffs_rd_data_obj(in, chunk, local_buffer);
3628 memcpy(&local_buffer[start], buffer, n_copy);
3631 yaffs_wr_data_obj(in, chunk,
3635 yaffs_release_temp_buffer(dev, local_buffer);
3638 /* A full chunk. Write directly from the buffer. */
3641 yaffs_wr_data_obj(in, chunk, buffer,
3642 dev->data_bytes_per_chunk, 0);
3644 /* Since we've overwritten the cached data,
3645 * we better invalidate it. */
3646 yaffs_invalidate_chunk_cache(in, chunk);
3649 if (chunk_written >= 0) {
3657 /* Update file object */
3659 if ((start_write + n_done) > in->variant.file_variant.file_size)
3660 in->variant.file_variant.file_size = (start_write + n_done);
3666 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3667 int n_bytes, int write_through)
3669 yaffs2_handle_hole(in, offset);
3670 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
3673 /* ---------------------- File resizing stuff ------------------ */
3675 static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
3678 struct yaffs_dev *dev = in->my_dev;
3679 loff_t old_size = in->variant.file_variant.file_size;
3687 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3691 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3692 &start_del, &dummy);
3696 /* Delete backwards so that we don't end up with holes if
3697 * power is lost part-way through the operation.
3699 for (i = last_del; i >= start_del; i--) {
3700 /* NB this could be optimised somewhat,
3701 * eg. could retrieve the tags and write them without
3702 * using yaffs_chunk_del
3705 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3711 (dev->internal_start_block * dev->param.chunks_per_block) ||
3713 ((dev->internal_end_block + 1) *
3714 dev->param.chunks_per_block)) {
3715 yaffs_trace(YAFFS_TRACE_ALWAYS,
3716 "Found daft chunk_id %d for %d",
3719 in->n_data_chunks--;
3720 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3725 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3729 struct yaffs_dev *dev = obj->my_dev;
3731 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3733 yaffs_prune_chunks(obj, new_size);
3735 if (new_partial != 0) {
3736 int last_chunk = 1 + new_full;
3737 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3739 /* Rewrite the last chunk with its new size and zero pad */
3740 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3741 memset(local_buffer + new_partial, 0,
3742 dev->data_bytes_per_chunk - new_partial);
3744 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3747 yaffs_release_temp_buffer(dev, local_buffer);
3750 obj->variant.file_variant.file_size = new_size;
3752 yaffs_prune_tree(dev, &obj->variant.file_variant);
3755 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3757 struct yaffs_dev *dev = in->my_dev;
3758 loff_t old_size = in->variant.file_variant.file_size;
3760 yaffs_flush_file_cache(in);
3761 yaffs_invalidate_whole_cache(in);
3763 yaffs_check_gc(dev, 0);
3765 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3768 if (new_size == old_size)
3771 if (new_size > old_size) {
3772 yaffs2_handle_hole(in, new_size);
3773 in->variant.file_variant.file_size = new_size;
3775 /* new_size < old_size */
3776 yaffs_resize_file_down(in, new_size);
3779 /* Write a new object header to reflect the resize.
3780 * show we've shrunk the file, if need be
3781 * Do this only if the file is not in the deleted directories
3782 * and is not shadowed.
3786 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3787 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3788 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3793 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3798 yaffs_flush_file_cache(in);
3804 yaffs_load_current_time(in, 0, 0);
3806 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3807 YAFFS_OK : YAFFS_FAIL;
3811 /* yaffs_del_file deletes the whole file data
3812 * and the inode associated with the file.
3813 * It does not delete the links associated with the file.
3815 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3819 struct yaffs_dev *dev = in->my_dev;
3826 yaffs_change_obj_name(in, in->my_dev->del_dir,
3827 _Y("deleted"), 0, 0);
3828 yaffs_trace(YAFFS_TRACE_TRACING,
3829 "yaffs: immediate deletion of file %d",
3832 in->my_dev->n_deleted_files++;
3833 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3834 yaffs_resize_file(in, 0);
3835 yaffs_soft_del_file(in);
3838 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3839 _Y("unlinked"), 0, 0);
3844 static int yaffs_del_file(struct yaffs_obj *in)
3846 int ret_val = YAFFS_OK;
3847 int deleted; /* Need to cache value on stack if in is freed */
3848 struct yaffs_dev *dev = in->my_dev;
3850 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3851 yaffs_resize_file(in, 0);
3853 if (in->n_data_chunks > 0) {
3854 /* Use soft deletion if there is data in the file.
3855 * That won't be the case if it has been resized to zero.
3858 ret_val = yaffs_unlink_file_if_needed(in);
3860 deleted = in->deleted;
3862 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3865 in->my_dev->n_deleted_files++;
3866 yaffs_soft_del_file(in);
3868 return deleted ? YAFFS_OK : YAFFS_FAIL;
3870 /* The file has no data chunks so we toss it immediately */
3871 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3872 in->variant.file_variant.top = NULL;
3873 yaffs_generic_obj_del(in);
3879 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3882 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3883 !(list_empty(&obj->variant.dir_variant.children));
3886 static int yaffs_del_dir(struct yaffs_obj *obj)
3888 /* First check that the directory is empty. */
3889 if (yaffs_is_non_empty_dir(obj))
3892 return yaffs_generic_obj_del(obj);
3895 static int yaffs_del_symlink(struct yaffs_obj *in)
3897 kfree(in->variant.symlink_variant.alias);
3898 in->variant.symlink_variant.alias = NULL;
3900 return yaffs_generic_obj_del(in);
3903 static int yaffs_del_link(struct yaffs_obj *in)
3905 /* remove this hardlink from the list associated with the equivalent
3908 list_del_init(&in->hard_links);
3909 return yaffs_generic_obj_del(in);
3912 int yaffs_del_obj(struct yaffs_obj *obj)
3916 switch (obj->variant_type) {
3917 case YAFFS_OBJECT_TYPE_FILE:
3918 ret_val = yaffs_del_file(obj);
3920 case YAFFS_OBJECT_TYPE_DIRECTORY:
3921 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3922 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3923 "Remove object %d from dirty directories",
3925 list_del_init(&obj->variant.dir_variant.dirty);
3927 return yaffs_del_dir(obj);
3929 case YAFFS_OBJECT_TYPE_SYMLINK:
3930 ret_val = yaffs_del_symlink(obj);
3932 case YAFFS_OBJECT_TYPE_HARDLINK:
3933 ret_val = yaffs_del_link(obj);
3935 case YAFFS_OBJECT_TYPE_SPECIAL:
3936 ret_val = yaffs_generic_obj_del(obj);
3938 case YAFFS_OBJECT_TYPE_UNKNOWN:
3940 break; /* should not happen. */
3945 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3955 yaffs_update_parent(obj->parent);
3957 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3958 return yaffs_del_link(obj);
3959 } else if (!list_empty(&obj->hard_links)) {
3960 /* Curve ball: We're unlinking an object that has a hardlink.
3962 * This problem arises because we are not strictly following
3963 * The Linux link/inode model.
3965 * We can't really delete the object.
3966 * Instead, we do the following:
3967 * - Select a hardlink.
3968 * - Unhook it from the hard links
3969 * - Move it from its parent directory so that the rename works.
3970 * - Rename the object to the hardlink's name.
3971 * - Delete the hardlink
3974 struct yaffs_obj *hl;
3975 struct yaffs_obj *parent;
3977 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3979 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3982 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3983 parent = hl->parent;
3985 list_del_init(&hl->hard_links);
3987 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3989 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
3991 if (ret_val == YAFFS_OK)
3992 ret_val = yaffs_generic_obj_del(hl);
3996 } else if (del_now) {
3997 switch (obj->variant_type) {
3998 case YAFFS_OBJECT_TYPE_FILE:
3999 return yaffs_del_file(obj);
4001 case YAFFS_OBJECT_TYPE_DIRECTORY:
4002 list_del_init(&obj->variant.dir_variant.dirty);
4003 return yaffs_del_dir(obj);
4005 case YAFFS_OBJECT_TYPE_SYMLINK:
4006 return yaffs_del_symlink(obj);
4008 case YAFFS_OBJECT_TYPE_SPECIAL:
4009 return yaffs_generic_obj_del(obj);
4011 case YAFFS_OBJECT_TYPE_HARDLINK:
4012 case YAFFS_OBJECT_TYPE_UNKNOWN:
4016 } else if (yaffs_is_non_empty_dir(obj)) {
4019 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4020 _Y("unlinked"), 0, 0);
4024 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4026 if (obj && obj->unlink_allowed)
4027 return yaffs_unlink_worker(obj);
4032 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4034 struct yaffs_obj *obj;
4036 obj = yaffs_find_by_name(dir, name);
4037 return yaffs_unlink_obj(obj);
4041 * If old_name is NULL then we take old_dir as the object to be renamed.
4043 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
4044 struct yaffs_obj *new_dir, const YCHAR *new_name)
4046 struct yaffs_obj *obj = NULL;
4047 struct yaffs_obj *existing_target = NULL;
4050 struct yaffs_dev *dev;
4052 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4056 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4061 dev = old_dir->my_dev;
4063 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4064 /* Special case for case insemsitive systems.
4065 * While look-up is case insensitive, the name isn't.
4066 * Therefore we might want to change x.txt to X.txt
4068 if (old_dir == new_dir &&
4069 old_name && new_name &&
4070 strcmp(old_name, new_name) == 0)
4074 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4075 YAFFS_MAX_NAME_LENGTH)
4080 obj = yaffs_find_by_name(old_dir, old_name);
4083 old_dir = obj->parent;
4086 if (obj && obj->rename_allowed) {
4087 /* Now handle an existing target, if there is one */
4088 existing_target = yaffs_find_by_name(new_dir, new_name);
4089 if (yaffs_is_non_empty_dir(existing_target)) {
4090 return YAFFS_FAIL; /* ENOTEMPTY */
4091 } else if (existing_target && existing_target != obj) {
4092 /* Nuke the target first, using shadowing,
4093 * but only if it isn't the same object.
4095 * Note we must disable gc here otherwise it can mess
4099 dev->gc_disable = 1;
4100 yaffs_change_obj_name(obj, new_dir, new_name, force,
4101 existing_target->obj_id);
4102 existing_target->is_shadowed = 1;
4103 yaffs_unlink_obj(existing_target);
4104 dev->gc_disable = 0;
4107 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4109 yaffs_update_parent(old_dir);
4110 if (new_dir != old_dir)
4111 yaffs_update_parent(new_dir);
4118 /*----------------------- Initialisation Scanning ---------------------- */
4120 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4121 int backward_scanning)
4123 struct yaffs_obj *obj;
4125 if (backward_scanning) {
4126 /* Handle YAFFS2 case (backward scanning)
4127 * If the shadowed object exists then ignore.
4129 obj = yaffs_find_by_number(dev, obj_id);
4134 /* Let's create it (if it does not exist) assuming it is a file so that
4135 * it can do shrinking etc.
4136 * We put it in unlinked dir to be cleaned up after the scanning
4139 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4142 obj->is_shadowed = 1;
4143 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4144 obj->variant.file_variant.shrink_size = 0;
4145 obj->valid = 1; /* So that we don't read any other info. */
4148 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4150 struct list_head *lh;
4151 struct list_head *save;
4152 struct yaffs_obj *hl;
4153 struct yaffs_obj *in;
4155 list_for_each_safe(lh, save, hard_list) {
4156 hl = list_entry(lh, struct yaffs_obj, hard_links);
4157 in = yaffs_find_by_number(dev,
4158 hl->variant.hardlink_variant.equiv_id);
4161 /* Add the hardlink pointers */
4162 hl->variant.hardlink_variant.equiv_obj = in;
4163 list_add(&hl->hard_links, &in->hard_links);
4165 /* Todo Need to report/handle this better.
4166 * Got a problem... hardlink to a non-existant object
4168 hl->variant.hardlink_variant.equiv_obj = NULL;
4169 INIT_LIST_HEAD(&hl->hard_links);
4174 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4177 * Sort out state of unlinked and deleted objects after scanning.
4179 struct list_head *i;
4180 struct list_head *n;
4181 struct yaffs_obj *l;
4186 /* Soft delete all the unlinked files */
4187 list_for_each_safe(i, n,
4188 &dev->unlinked_dir->variant.dir_variant.children) {
4189 l = list_entry(i, struct yaffs_obj, siblings);
4193 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4194 l = list_entry(i, struct yaffs_obj, siblings);
4200 * This code iterates through all the objects making sure that they are rooted.
4201 * Any unrooted objects are re-rooted in lost+found.
4202 * An object needs to be in one of:
4203 * - Directly under deleted, unlinked
4204 * - Directly or indirectly under root.
4207 * This code assumes that we don't ever change the current relationships
4208 * between directories:
4209 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4210 * lost-n-found->parent == root_dir
4212 * This fixes the problem where directories might have inadvertently been
4213 * deleted leaving the object "hanging" without being rooted in the
4217 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4219 return (obj == dev->del_dir ||
4220 obj == dev->unlinked_dir || obj == dev->root_dir);
4223 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4225 struct yaffs_obj *obj;
4226 struct yaffs_obj *parent;
4228 struct list_head *lh;
4229 struct list_head *n;
4236 /* Iterate through the objects in each hash entry,
4237 * looking at each object.
4238 * Make sure it is rooted.
4241 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4242 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4243 obj = list_entry(lh, struct yaffs_obj, hash_link);
4244 parent = obj->parent;
4246 if (yaffs_has_null_parent(dev, obj)) {
4247 /* These directories are not hanging */
4249 } else if (!parent ||
4250 parent->variant_type !=
4251 YAFFS_OBJECT_TYPE_DIRECTORY) {
4253 } else if (yaffs_has_null_parent(dev, parent)) {
4257 * Need to follow the parent chain to
4258 * see if it is hanging.
4263 while (parent != dev->root_dir &&
4265 parent->parent->variant_type ==
4266 YAFFS_OBJECT_TYPE_DIRECTORY &&
4268 parent = parent->parent;
4271 if (parent != dev->root_dir)
4275 yaffs_trace(YAFFS_TRACE_SCAN,
4276 "Hanging object %d moved to lost and found",
4278 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4285 * Delete directory contents for cleaning up lost and found.
4287 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4289 struct yaffs_obj *obj;
4290 struct list_head *lh;
4291 struct list_head *n;
4293 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4296 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4297 obj = list_entry(lh, struct yaffs_obj, siblings);
4298 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4299 yaffs_del_dir_contents(obj);
4300 yaffs_trace(YAFFS_TRACE_SCAN,
4301 "Deleting lost_found object %d",
4303 yaffs_unlink_obj(obj);
4307 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4309 yaffs_del_dir_contents(dev->lost_n_found);
4313 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4317 struct list_head *i;
4318 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4319 struct yaffs_obj *l;
4325 yaffs_trace(YAFFS_TRACE_ALWAYS,
4326 "tragedy: yaffs_find_by_name: null pointer directory"
4331 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4332 yaffs_trace(YAFFS_TRACE_ALWAYS,
4333 "tragedy: yaffs_find_by_name: non-directory"
4338 sum = yaffs_calc_name_sum(name);
4340 list_for_each(i, &directory->variant.dir_variant.children) {
4341 l = list_entry(i, struct yaffs_obj, siblings);
4343 if (l->parent != directory)
4346 yaffs_check_obj_details_loaded(l);
4348 /* Special case for lost-n-found */
4349 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4350 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4352 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4353 /* LostnFound chunk called Objxxx
4356 yaffs_get_obj_name(l, buffer,
4357 YAFFS_MAX_NAME_LENGTH + 1);
4358 if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
4365 /* GetEquivalentObject dereferences any hard links to get to the
4369 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4371 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4372 obj = obj->variant.hardlink_variant.equiv_obj;
4373 yaffs_check_obj_details_loaded(obj);
4379 * A note or two on object names.
4380 * * If the object name is missing, we then make one up in the form objnnn
4382 * * ASCII names are stored in the object header's name field from byte zero
4383 * * Unicode names are historically stored starting from byte zero.
4385 * Then there are automatic Unicode names...
4386 * The purpose of these is to save names in a way that can be read as
4387 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4388 * system to share files.
4390 * These automatic unicode are stored slightly differently...
4391 * - If the name can fit in the ASCII character space then they are saved as
4392 * ascii names as per above.
4393 * - If the name needs Unicode then the name is saved in Unicode
4394 * starting at oh->name[1].
4397 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4400 /* Create an object name if we could not find one. */
4401 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4402 YCHAR local_name[20];
4403 YCHAR num_string[20];
4404 YCHAR *x = &num_string[19];
4405 unsigned v = obj->obj_id;
4409 *x = '0' + (v % 10);
4412 /* make up a name */
4413 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4414 strcat(local_name, x);
4415 strncpy(name, local_name, buffer_size - 1);
4419 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4421 memset(name, 0, buffer_size * sizeof(YCHAR));
4422 yaffs_check_obj_details_loaded(obj);
4423 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4424 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4425 } else if (obj->short_name[0]) {
4426 strcpy(name, obj->short_name);
4427 } else if (obj->hdr_chunk > 0) {
4429 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4431 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4433 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4435 if (obj->hdr_chunk > 0) {
4436 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4440 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4443 yaffs_release_temp_buffer(obj->my_dev, buffer);
4446 yaffs_fix_null_name(obj, name, buffer_size);
4448 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4451 loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
4453 /* Dereference any hard linking */
4454 obj = yaffs_get_equivalent_obj(obj);
4456 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4457 return obj->variant.file_variant.file_size;
4458 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4459 if (!obj->variant.symlink_variant.alias)
4461 return strnlen(obj->variant.symlink_variant.alias,
4462 YAFFS_MAX_ALIAS_LENGTH);
4464 /* Only a directory should drop through to here */
4465 return obj->my_dev->data_bytes_per_chunk;
4469 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4472 struct list_head *i;
4475 count++; /* the object itself */
4477 list_for_each(i, &obj->hard_links)
4478 count++; /* add the hard links; */
4483 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4485 obj = yaffs_get_equivalent_obj(obj);
4490 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4492 obj = yaffs_get_equivalent_obj(obj);
4494 switch (obj->variant_type) {
4495 case YAFFS_OBJECT_TYPE_FILE:
4498 case YAFFS_OBJECT_TYPE_DIRECTORY:
4501 case YAFFS_OBJECT_TYPE_SYMLINK:
4504 case YAFFS_OBJECT_TYPE_HARDLINK:
4507 case YAFFS_OBJECT_TYPE_SPECIAL:
4508 if (S_ISFIFO(obj->yst_mode))
4510 if (S_ISCHR(obj->yst_mode))
4512 if (S_ISBLK(obj->yst_mode))
4514 if (S_ISSOCK(obj->yst_mode))
4524 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4526 obj = yaffs_get_equivalent_obj(obj);
4527 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4528 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4530 return yaffs_clone_str(_Y(""));
4533 /*--------------------------- Initialisation code -------------------------- */
4535 static int yaffs_check_dev_fns(struct yaffs_dev *dev)
4537 struct yaffs_param *param = &dev->param;
4539 /* Common functions, gotta have */
4540 if (!param->drv_initialise_fn ||
4541 !param->drv_read_chunk_fn ||
4542 !param->drv_write_chunk_fn ||
4543 !param->drv_erase_fn ||
4544 !param->drv_initialise_fn)
4547 /* Install the default tags marshalling functions if needed. */
4548 yaffs_tags_compat_install(dev);
4549 yaffs_tags_marshall_install(dev);
4551 /* Check we now have the marshalling functions required. */
4552 if (!param->write_chunk_tags_fn ||
4553 !param->read_chunk_tags_fn ||
4554 !param->query_block_fn ||
4555 !param->mark_bad_fn)
4561 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4563 /* Initialise the unlinked, deleted, root and lost+found directories */
4564 dev->lost_n_found = dev->root_dir = NULL;
4565 dev->unlinked_dir = dev->del_dir = NULL;
4567 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4569 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4571 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4572 YAFFS_ROOT_MODE | S_IFDIR);
4574 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4575 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4577 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4579 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4585 int yaffs_guts_initialise(struct yaffs_dev *dev)
4587 int init_failed = 0;
4591 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()");
4593 /* Check stuff that must be set */
4596 yaffs_trace(YAFFS_TRACE_ALWAYS,
4597 "yaffs: Need a device"
4602 if (dev->is_mounted) {
4603 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4607 dev->internal_start_block = dev->param.start_block;
4608 dev->internal_end_block = dev->param.end_block;
4609 dev->block_offset = 0;
4610 dev->chunk_offset = 0;
4611 dev->n_free_chunks = 0;
4615 if (dev->param.start_block == 0) {
4616 dev->internal_start_block = dev->param.start_block + 1;
4617 dev->internal_end_block = dev->param.end_block + 1;
4618 dev->block_offset = 1;
4619 dev->chunk_offset = dev->param.chunks_per_block;
4622 /* Check geometry parameters. */
4624 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4625 dev->param.total_bytes_per_chunk < 1024) ||
4626 (!dev->param.is_yaffs2 &&
4627 dev->param.total_bytes_per_chunk < 512) ||
4628 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4629 dev->param.chunks_per_block < 2 ||
4630 dev->param.n_reserved_blocks < 2 ||
4631 dev->internal_start_block <= 0 ||
4632 dev->internal_end_block <= 0 ||
4633 dev->internal_end_block <=
4634 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4636 /* otherwise it is too small */
4637 yaffs_trace(YAFFS_TRACE_ALWAYS,
4638 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4639 dev->param.total_bytes_per_chunk,
4640 dev->param.is_yaffs2 ? "2" : "",
4641 dev->param.inband_tags);
4645 if (yaffs_init_nand(dev) != YAFFS_OK) {
4646 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4650 /* Sort out space for inband tags, if required */
4651 if (dev->param.inband_tags)
4652 dev->data_bytes_per_chunk =
4653 dev->param.total_bytes_per_chunk -
4654 sizeof(struct yaffs_packed_tags2_tags_only);
4656 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4658 /* Got the right mix of functions? */
4659 if (!yaffs_check_dev_fns(dev)) {
4660 /* Function missing */
4661 yaffs_trace(YAFFS_TRACE_ALWAYS,
4662 "device function(s) missing or wrong");
4667 /* Finished with most checks. Further checks happen later on too. */
4669 dev->is_mounted = 1;
4671 /* OK now calculate a few things for the device */
4674 * Calculate all the chunk size manipulation numbers:
4676 x = dev->data_bytes_per_chunk;
4677 /* We always use dev->chunk_shift and dev->chunk_div */
4678 dev->chunk_shift = calc_shifts(x);
4679 x >>= dev->chunk_shift;
4681 /* We only use chunk mask if chunk_div is 1 */
4682 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4685 * Calculate chunk_grp_bits.
4686 * We need to find the next power of 2 > than internal_end_block
4689 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4691 bits = calc_shifts_ceiling(x);
4693 /* Set up tnode width if wide tnodes are enabled. */
4694 if (!dev->param.wide_tnodes_disabled) {
4695 /* bits must be even so that we end up with 32-bit words */
4699 dev->tnode_width = 16;
4701 dev->tnode_width = bits;
4703 dev->tnode_width = 16;
4706 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4708 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4709 * so if the bitwidth of the
4710 * chunk range we're using is greater than 16 we need
4711 * to figure out chunk shift and chunk_grp_size
4714 if (bits <= dev->tnode_width)
4715 dev->chunk_grp_bits = 0;
4717 dev->chunk_grp_bits = bits - dev->tnode_width;
4719 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4720 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4721 dev->tnode_size = sizeof(struct yaffs_tnode);
4723 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4725 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4726 /* We have a problem because the soft delete won't work if
4727 * the chunk group size > chunks per block.
4728 * This can be remedied by using larger "virtual blocks".
4730 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4735 /* Finished verifying the device, continue with initialisation */
4737 /* More device initialisation */
4739 dev->passive_gc_count = 0;
4740 dev->oldest_dirty_gc_count = 0;
4742 dev->gc_block_finder = 0;
4743 dev->buffered_block = -1;
4744 dev->doing_buffered_block_rewrite = 0;
4745 dev->n_deleted_files = 0;
4746 dev->n_bg_deletions = 0;
4747 dev->n_unlinked_files = 0;
4748 dev->n_ecc_fixed = 0;
4749 dev->n_ecc_unfixed = 0;
4750 dev->n_tags_ecc_fixed = 0;
4751 dev->n_tags_ecc_unfixed = 0;
4752 dev->n_erase_failures = 0;
4753 dev->n_erased_blocks = 0;
4754 dev->gc_disable = 0;
4755 dev->has_pending_prioritised_gc = 1;
4756 /* Assume the worst for now, will get fixed on first GC */
4757 INIT_LIST_HEAD(&dev->dirty_dirs);
4758 dev->oldest_dirty_seq = 0;
4759 dev->oldest_dirty_block = 0;
4761 /* Initialise temporary buffers and caches. */
4762 if (!yaffs_init_tmp_buffers(dev))
4766 dev->gc_cleanup_list = NULL;
4768 if (!init_failed && dev->param.n_caches > 0) {
4772 dev->param.n_caches * sizeof(struct yaffs_cache);
4774 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4775 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4777 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4779 buf = (u8 *) dev->cache;
4782 memset(dev->cache, 0, cache_bytes);
4784 for (i = 0; i < dev->param.n_caches && buf; i++) {
4785 dev->cache[i].object = NULL;
4786 dev->cache[i].last_use = 0;
4787 dev->cache[i].dirty = 0;
4788 dev->cache[i].data = buf =
4789 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4794 dev->cache_last_use = 0;
4797 dev->cache_hits = 0;
4800 dev->gc_cleanup_list =
4801 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4803 if (!dev->gc_cleanup_list)
4807 if (dev->param.is_yaffs2)
4808 dev->param.use_header_file_size = 1;
4810 if (!init_failed && !yaffs_init_blocks(dev))
4813 yaffs_init_tnodes_and_objs(dev);
4815 if (!init_failed && !yaffs_create_initial_dir(dev))
4818 if (!init_failed && dev->param.is_yaffs2 &&
4819 !dev->param.disable_summary &&
4820 !yaffs_summary_init(dev))
4824 /* Now scan the flash. */
4825 if (dev->param.is_yaffs2) {
4826 if (yaffs2_checkpt_restore(dev)) {
4827 yaffs_check_obj_details_loaded(dev->root_dir);
4828 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4830 "yaffs: restored from checkpoint"
4834 /* Clean up the mess caused by an aborted
4835 * checkpoint load then scan backwards.
4837 yaffs_deinit_blocks(dev);
4839 yaffs_deinit_tnodes_and_objs(dev);
4841 dev->n_erased_blocks = 0;
4842 dev->n_free_chunks = 0;
4843 dev->alloc_block = -1;
4844 dev->alloc_page = -1;
4845 dev->n_deleted_files = 0;
4846 dev->n_unlinked_files = 0;
4847 dev->n_bg_deletions = 0;
4849 if (!init_failed && !yaffs_init_blocks(dev))
4852 yaffs_init_tnodes_and_objs(dev);
4855 && !yaffs_create_initial_dir(dev))
4858 if (!init_failed && !yaffs2_scan_backwards(dev))
4861 } else if (!yaffs1_scan(dev)) {
4865 yaffs_strip_deleted_objs(dev);
4866 yaffs_fix_hanging_objs(dev);
4867 if (dev->param.empty_lost_n_found)
4868 yaffs_empty_l_n_f(dev);
4872 /* Clean up the mess */
4873 yaffs_trace(YAFFS_TRACE_TRACING,
4874 "yaffs: yaffs_guts_initialise() aborted.");
4876 yaffs_deinitialise(dev);
4880 /* Zero out stats */
4881 dev->n_page_reads = 0;
4882 dev->n_page_writes = 0;
4883 dev->n_erasures = 0;
4884 dev->n_gc_copies = 0;
4885 dev->n_retried_writes = 0;
4887 dev->n_retired_blocks = 0;
4889 yaffs_verify_free_chunks(dev);
4890 yaffs_verify_blocks(dev);
4892 /* Clean up any aborted checkpoint data */
4893 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4894 yaffs2_checkpt_invalidate(dev);
4896 yaffs_trace(YAFFS_TRACE_TRACING,
4897 "yaffs: yaffs_guts_initialise() done.");
4901 void yaffs_deinitialise(struct yaffs_dev *dev)
4903 if (dev->is_mounted) {
4906 yaffs_deinit_blocks(dev);
4907 yaffs_deinit_tnodes_and_objs(dev);
4908 yaffs_summary_deinit(dev);
4910 if (dev->param.n_caches > 0 && dev->cache) {
4912 for (i = 0; i < dev->param.n_caches; i++) {
4913 kfree(dev->cache[i].data);
4914 dev->cache[i].data = NULL;
4921 kfree(dev->gc_cleanup_list);
4923 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
4924 kfree(dev->temp_buffer[i].buffer);
4926 dev->is_mounted = 0;
4928 if (dev->param.drv_deinitialise_fn)
4929 dev->param.drv_deinitialise_fn(dev);
4933 int yaffs_count_free_chunks(struct yaffs_dev *dev)
4937 struct yaffs_block_info *blk;
4939 blk = dev->block_info;
4940 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4941 switch (blk->block_state) {
4942 case YAFFS_BLOCK_STATE_EMPTY:
4943 case YAFFS_BLOCK_STATE_ALLOCATING:
4944 case YAFFS_BLOCK_STATE_COLLECTING:
4945 case YAFFS_BLOCK_STATE_FULL:
4947 (dev->param.chunks_per_block - blk->pages_in_use +
4948 blk->soft_del_pages);
4958 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
4960 /* This is what we report to the outside world */
4963 int blocks_for_checkpt;
4966 n_free = dev->n_free_chunks;
4967 n_free += dev->n_deleted_files;
4969 /* Now count and subtract the number of dirty chunks in the cache. */
4971 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
4972 if (dev->cache[i].dirty)
4976 n_free -= n_dirty_caches;
4979 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
4981 /* Now figure checkpoint space and report that... */
4982 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
4984 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
4993 * Marshalling functions to get loff_t file sizes into and out of
4996 void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize)
4998 oh->file_size_low = (fsize & 0xFFFFFFFF);
4999 oh->file_size_high = ((fsize >> 32) & 0xFFFFFFFF);
5002 loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh)
5006 if (sizeof(loff_t) >= 8 && ~(oh->file_size_high))
5007 retval = (((loff_t) oh->file_size_high) << 32) |
5008 (((loff_t) oh->file_size_low) & 0xFFFFFFFF);
5010 retval = (loff_t) oh->file_size_low;