2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_trace.h"
17 #include "yaffs_guts.h"
18 #include "yaffs_tagsvalidity.h"
19 #include "yaffs_getblockinfo.h"
21 #include "yaffs_tagscompat.h"
23 #include "yaffs_nand.h"
25 #include "yaffs_yaffs1.h"
26 #include "yaffs_yaffs2.h"
27 #include "yaffs_bitmap.h"
28 #include "yaffs_verify.h"
30 #include "yaffs_nand.h"
31 #include "yaffs_packedtags2.h"
33 #include "yaffs_nameval.h"
34 #include "yaffs_allocator.h"
36 #include "yaffs_attribs.h"
38 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
39 #define YAFFS_GC_GOOD_ENOUGH 2
40 #define YAFFS_GC_PASSIVE_THRESHOLD 4
42 #include "yaffs_ecc.h"
44 /* Robustification (if it ever comes about...) */
45 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block);
46 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
48 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
50 const struct yaffs_ext_tags *tags);
51 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
52 const struct yaffs_ext_tags *tags);
54 /* Other local prototypes */
55 static void yaffs_update_parent(struct yaffs_obj *obj);
56 static int yaffs_unlink_obj(struct yaffs_obj *obj);
57 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj);
59 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
61 struct yaffs_ext_tags *tags, int use_reserver);
63 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
64 enum yaffs_obj_type type);
66 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
67 struct yaffs_xattr_mod *xmod);
69 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj);
70 static int yaffs_generic_obj_del(struct yaffs_obj *in);
72 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk);
74 static int yaffs_unlink_worker(struct yaffs_obj *obj);
76 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
79 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
80 struct yaffs_block_info **block_ptr);
82 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in);
84 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in);
85 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object,
88 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
89 struct yaffs_ext_tags *tags);
91 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
94 struct yaffs_ext_tags *tags);
96 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
97 const YCHAR * oh_name, int buff_size);
98 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
101 /* Function to calculate chunk and offset */
103 static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
104 int *chunk_out, u32 * offset_out)
109 chunk = (u32) (addr >> dev->chunk_shift);
111 if (dev->chunk_div == 1) {
112 /* easy power of 2 case */
113 offset = (u32) (addr & dev->chunk_mask);
115 /* Non power-of-2 case */
119 chunk /= dev->chunk_div;
121 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
122 offset = (u32) (addr - chunk_base);
126 *offset_out = offset;
129 /* Function to return the number of shifts for a power of 2 greater than or
130 * equal to the given number
131 * Note we don't try to cater for all possible numbers and this does not have to
132 * be hellishly efficient.
135 static u32 calc_shifts_ceiling(u32 x)
140 shifts = extra_bits = 0;
155 /* Function to return the number of shifts to get a 1 in bit 0
158 static u32 calc_shifts(u32 x)
176 * Temporary buffer manipulations.
179 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
184 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
186 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
187 dev->temp_buffer[i].line = 0; /* not in use */
188 dev->temp_buffer[i].buffer = buf =
189 YMALLOC_DMA(dev->param.total_bytes_per_chunk);
192 return buf ? YAFFS_OK : YAFFS_FAIL;
195 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
200 if (dev->temp_in_use > dev->max_temp)
201 dev->max_temp = dev->temp_in_use;
203 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
204 if (dev->temp_buffer[i].line == 0) {
205 dev->temp_buffer[i].line = line_no;
206 if ((i + 1) > dev->max_temp) {
207 dev->max_temp = i + 1;
208 for (j = 0; j <= i; j++)
209 dev->temp_buffer[j].max_line =
210 dev->temp_buffer[j].line;
213 return dev->temp_buffer[i].buffer;
217 T(YAFFS_TRACE_BUFFERS,
218 (TSTR("Out of temp buffers at line %d, other held by lines:"),
220 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
221 T(YAFFS_TRACE_BUFFERS,
222 (TSTR(" %d "), dev->temp_buffer[i].line));
224 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
227 * If we got here then we have to allocate an unmanaged one
231 dev->unmanaged_buffer_allocs++;
232 return YMALLOC(dev->data_bytes_per_chunk);
236 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
242 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
243 if (dev->temp_buffer[i].buffer == buffer) {
244 dev->temp_buffer[i].line = 0;
250 /* assume it is an unmanaged one. */
251 T(YAFFS_TRACE_BUFFERS,
252 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
255 dev->unmanaged_buffer_deallocs++;
261 * Determine if we have a managed buffer.
263 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
267 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
268 if (dev->temp_buffer[i].buffer == buffer)
272 for (i = 0; i < dev->param.n_caches; i++) {
273 if (dev->cache[i].data == buffer)
277 if (buffer == dev->checkpt_buffer)
280 T(YAFFS_TRACE_ALWAYS,
281 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
290 * Simple hash function. Needs to have a reasonable spread
293 static Y_INLINE int yaffs_hash_fn(int n)
296 return n % YAFFS_NOBJECT_BUCKETS;
300 * Access functions to useful fake objects.
301 * Note that root might have a presence in NAND if permissions are set.
304 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
306 return dev->root_dir;
309 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
311 return dev->lost_n_found;
315 * Erased NAND checking functions
318 int yaffs_check_ff(u8 * buffer, int n_bytes)
320 /* Horrible, slow implementation */
329 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
331 int retval = YAFFS_OK;
332 u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
333 struct yaffs_ext_tags tags;
336 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
338 if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
341 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
342 T(YAFFS_TRACE_NANDACCESS,
343 (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
347 yaffs_release_temp_buffer(dev, data, __LINE__);
353 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
356 struct yaffs_ext_tags *tags)
358 int retval = YAFFS_OK;
359 struct yaffs_ext_tags temp_tags;
360 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
363 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
364 if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
365 temp_tags.obj_id != tags->obj_id ||
366 temp_tags.chunk_id != tags->chunk_id ||
367 temp_tags.n_bytes != tags->n_bytes)
370 yaffs_release_temp_buffer(dev, buffer, __LINE__);
375 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
377 struct yaffs_ext_tags *tags, int use_reserver)
383 yaffs2_checkpt_invalidate(dev);
386 struct yaffs_block_info *bi = 0;
389 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
395 /* First check this chunk is erased, if it needs
396 * checking. The checking policy (unless forced
397 * always on) is as follows:
399 * Check the first page we try to write in a block.
400 * If the check passes then we don't need to check any
401 * more. If the check fails, we check again...
402 * If the block has been erased, we don't need to check.
404 * However, if the block has been prioritised for gc,
405 * then we think there might be something odd about
406 * this block and stop using it.
408 * Rationale: We should only ever see chunks that have
409 * not been erased if there was a partially written
410 * chunk due to power loss. This checking policy should
411 * catch that case with very few checks and thus save a
412 * lot of checks that are most likely not needed.
415 * If an erase check fails or the write fails we skip the
419 /* let's give it a try */
422 if (dev->param.always_check_erased)
423 bi->skip_erased_check = 0;
425 if (!bi->skip_erased_check) {
426 erased_ok = yaffs_check_chunk_erased(dev, chunk);
427 if (erased_ok != YAFFS_OK) {
429 (TSTR("**>> yaffs chunk %d was not erased"
432 /* If not erased, delete this one,
433 * skip rest of block and
434 * try another chunk */
435 yaffs_chunk_del(dev, chunk, 1, __LINE__);
436 yaffs_skip_rest_of_block(dev);
441 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
443 if (!bi->skip_erased_check)
445 yaffs_verify_chunk_written(dev, chunk, data, tags);
447 if (write_ok != YAFFS_OK) {
448 /* Clean up aborted write, skip to next block and
449 * try another chunk */
450 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
454 bi->skip_erased_check = 1;
456 /* Copy the data into the robustification buffer */
457 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
459 } while (write_ok != YAFFS_OK &&
460 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
467 (TSTR("**>> yaffs write required %d attempts" TENDSTR),
470 dev->n_retired_writes += (attempts - 1);
477 * Block retiring for handling a broken block.
480 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
482 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
484 yaffs2_checkpt_invalidate(dev);
486 yaffs2_clear_oldest_dirty_seq(dev, bi);
488 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
489 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
490 T(YAFFS_TRACE_ALWAYS,
492 ("yaffs: Failed to mark bad and erase block %d"
493 TENDSTR), flash_block));
495 struct yaffs_ext_tags tags;
497 flash_block * dev->param.chunks_per_block;
499 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
501 memset(buffer, 0xff, dev->data_bytes_per_chunk);
502 yaffs_init_tags(&tags);
503 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
504 if (dev->param.write_chunk_tags_fn(dev, chunk_id -
508 T(YAFFS_TRACE_ALWAYS,
511 TCONT("write bad block marker to block %d")
512 TENDSTR), flash_block));
514 yaffs_release_temp_buffer(dev, buffer, __LINE__);
518 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
519 bi->gc_prioritise = 0;
520 bi->needs_retiring = 0;
522 dev->n_retired_blocks++;
526 * Functions for robustisizing TODO
530 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
532 const struct yaffs_ext_tags *tags)
535 nand_chunk = nand_chunk;
540 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
541 const struct yaffs_ext_tags *tags)
544 nand_chunk = nand_chunk;
548 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
549 struct yaffs_block_info *bi)
551 if (!bi->gc_prioritise) {
552 bi->gc_prioritise = 1;
553 dev->has_pending_prioritised_gc = 1;
554 bi->chunk_error_strikes++;
556 if (bi->chunk_error_strikes > 3) {
557 bi->needs_retiring = 1; /* Too many stikes, so retire this */
558 T(YAFFS_TRACE_ALWAYS,
559 (TSTR("yaffs: Block struck out" TENDSTR)));
565 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
568 int flash_block = nand_chunk / dev->param.chunks_per_block;
569 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
571 yaffs_handle_chunk_error(dev, bi);
574 /* Was an actual write failure, so mark the block for retirement */
575 bi->needs_retiring = 1;
576 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
577 (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
580 /* Delete the chunk */
581 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
582 yaffs_skip_rest_of_block(dev);
585 /*---------------- Name handling functions ------------*/
587 static u16 yaffs_calc_name_sum(const YCHAR * name)
592 const YUCHAR *bname = (const YUCHAR *)name;
594 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
596 /* 0x1f mask is case insensitive */
597 sum += ((*bname) & 0x1f) * i;
605 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
607 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
608 memset(obj->short_name, 0,
609 sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH + 1));
611 && yaffs_strnlen(name,
612 YAFFS_SHORT_NAME_LENGTH + 1) <=
613 YAFFS_SHORT_NAME_LENGTH)
614 yaffs_strcpy(obj->short_name, name);
616 obj->short_name[0] = _Y('\0');
618 obj->sum = yaffs_calc_name_sum(name);
621 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
622 const struct yaffs_obj_hdr *oh)
624 #ifdef CONFIG_YAFFS_AUTO_UNICODE
625 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
626 memset(tmp_name, 0, sizeof(tmp_name));
627 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
628 YAFFS_MAX_NAME_LENGTH + 1);
629 yaffs_set_obj_name(obj, tmp_name);
631 yaffs_set_obj_name(obj, oh->name);
635 /*-------------------- TNODES -------------------
637 * List of spare tnodes
638 * The list is hooked together using the first pointer
642 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
644 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
646 memset(tn, 0, dev->tnode_size);
650 dev->checkpoint_blocks_required = 0; /* force recalculation */
655 /* FreeTnode frees up a tnode and puts it back on the free list */
656 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
658 yaffs_free_raw_tnode(dev, tn);
660 dev->checkpoint_blocks_required = 0; /* force recalculation */
663 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
665 yaffs_deinit_raw_tnodes_and_objs(dev);
670 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
671 unsigned pos, unsigned val)
673 u32 *map = (u32 *) tn;
679 pos &= YAFFS_TNODES_LEVEL0_MASK;
680 val >>= dev->chunk_grp_bits;
682 bit_in_map = pos * dev->tnode_width;
683 word_in_map = bit_in_map / 32;
684 bit_in_word = bit_in_map & (32 - 1);
686 mask = dev->tnode_mask << bit_in_word;
688 map[word_in_map] &= ~mask;
689 map[word_in_map] |= (mask & (val << bit_in_word));
691 if (dev->tnode_width > (32 - bit_in_word)) {
692 bit_in_word = (32 - bit_in_word);
695 dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
696 map[word_in_map] &= ~mask;
697 map[word_in_map] |= (mask & (val >> bit_in_word));
701 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
704 u32 *map = (u32 *) tn;
710 pos &= YAFFS_TNODES_LEVEL0_MASK;
712 bit_in_map = pos * dev->tnode_width;
713 word_in_map = bit_in_map / 32;
714 bit_in_word = bit_in_map & (32 - 1);
716 val = map[word_in_map] >> bit_in_word;
718 if (dev->tnode_width > (32 - bit_in_word)) {
719 bit_in_word = (32 - bit_in_word);
721 val |= (map[word_in_map] << bit_in_word);
724 val &= dev->tnode_mask;
725 val <<= dev->chunk_grp_bits;
730 /* ------------------- End of individual tnode manipulation -----------------*/
732 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
733 * The look up tree is represented by the top tnode and the number of top_level
734 * in the tree. 0 means only the level 0 tnode is in the tree.
737 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
738 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
739 struct yaffs_file_var *file_struct,
742 struct yaffs_tnode *tn = file_struct->top;
745 int level = file_struct->top_level;
749 /* Check sane level and chunk Id */
750 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
753 if (chunk_id > YAFFS_MAX_CHUNK_ID)
756 /* First check we're tall enough (ie enough top_level) */
758 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
761 i >>= YAFFS_TNODES_INTERNAL_BITS;
765 if (required_depth > file_struct->top_level)
766 return NULL; /* Not tall enough, so we can't find it */
768 /* Traverse down to level 0 */
769 while (level > 0 && tn) {
770 tn = tn->internal[(chunk_id >>
771 (YAFFS_TNODES_LEVEL0_BITS +
773 YAFFS_TNODES_INTERNAL_BITS)) &
774 YAFFS_TNODES_INTERNAL_MASK];
781 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
782 * This happens in two steps:
783 * 1. If the tree isn't tall enough, then make it taller.
784 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
786 * Used when modifying the tree.
788 * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
789 * be plugged into the ttree.
792 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
793 struct yaffs_file_var *file_struct,
795 struct yaffs_tnode *passed_tn)
800 struct yaffs_tnode *tn;
804 /* Check sane level and page Id */
805 if (file_struct->top_level < 0
806 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
809 if (chunk_id > YAFFS_MAX_CHUNK_ID)
812 /* First check we're tall enough (ie enough top_level) */
814 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
817 x >>= YAFFS_TNODES_INTERNAL_BITS;
821 if (required_depth > file_struct->top_level) {
822 /* Not tall enough, gotta make the tree taller */
823 for (i = file_struct->top_level; i < required_depth; i++) {
825 tn = yaffs_get_tnode(dev);
828 tn->internal[0] = file_struct->top;
829 file_struct->top = tn;
830 file_struct->top_level++;
833 (TSTR("yaffs: no more tnodes" TENDSTR)));
839 /* Traverse down to level 0, adding anything we need */
841 l = file_struct->top_level;
842 tn = file_struct->top;
845 while (l > 0 && tn) {
847 (YAFFS_TNODES_LEVEL0_BITS +
848 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
849 YAFFS_TNODES_INTERNAL_MASK;
851 if ((l > 1) && !tn->internal[x]) {
852 /* Add missing non-level-zero tnode */
853 tn->internal[x] = yaffs_get_tnode(dev);
854 if (!tn->internal[x])
857 /* Looking from level 1 at level 0 */
859 /* If we already have one, then release it. */
861 yaffs_free_tnode(dev,
864 tn->internal[x] = passed_tn;
866 } else if (!tn->internal[x]) {
867 /* Don't have one, none passed in */
868 tn->internal[x] = yaffs_get_tnode(dev);
869 if (!tn->internal[x])
874 tn = tn->internal[x];
880 memcpy(tn, passed_tn,
881 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
882 yaffs_free_tnode(dev, passed_tn);
889 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
890 struct yaffs_ext_tags *tags, int obj_id,
895 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
896 if (yaffs_check_chunk_bit
897 (dev, the_chunk / dev->param.chunks_per_block,
898 the_chunk % dev->param.chunks_per_block)) {
900 if (dev->chunk_grp_size == 1)
903 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
905 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
916 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
918 struct yaffs_block_info *the_block;
921 T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
923 block_no = chunk / dev->param.chunks_per_block;
924 the_block = yaffs_get_block_info(dev, block_no);
926 the_block->soft_del_pages++;
927 dev->n_free_chunks++;
928 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
932 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
933 * All soft deleting does is increment the block's softdelete count and pulls the chunk out
935 * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
938 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
939 u32 level, int chunk_offset)
944 struct yaffs_dev *dev = in->my_dev;
949 for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
951 if (tn->internal[i]) {
953 yaffs_soft_del_worker(in,
959 YAFFS_TNODES_INTERNAL_BITS)
962 yaffs_free_tnode(dev,
965 tn->internal[i] = NULL;
967 /* Hoosterman... how could this happen? */
971 return (all_done) ? 1 : 0;
972 } else if (level == 0) {
974 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
975 the_chunk = yaffs_get_group_base(dev, tn, i);
977 /* Note this does not find the real chunk, only the chunk group.
978 * We make an assumption that a chunk group is not larger than
981 yaffs_soft_del_chunk(dev, the_chunk);
982 yaffs_load_tnode_0(dev, tn, i, 0);
996 static void yaffs_soft_del_file(struct yaffs_obj *obj)
999 obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1000 if (obj->n_data_chunks <= 0) {
1001 /* Empty file with no duplicate object headers, just delete it immediately */
1002 yaffs_free_tnode(obj->my_dev,
1003 obj->variant.file_variant.top);
1004 obj->variant.file_variant.top = NULL;
1005 T(YAFFS_TRACE_TRACING,
1006 (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1008 yaffs_generic_obj_del(obj);
1010 yaffs_soft_del_worker(obj,
1011 obj->variant.file_variant.top,
1013 file_variant.top_level, 0);
1019 /* Pruning removes any part of the file structure tree that is beyond the
1020 * bounds of the file (ie that does not point to chunks).
1022 * A file should only get pruned when its size is reduced.
1024 * Before pruning, the chunks must be pulled from the tree and the
1025 * level 0 tnode entries must be zeroed out.
1026 * Could also use this for file deletion, but that's probably better handled
1027 * by a special case.
1029 * This function is recursive. For levels > 0 the function is called again on
1030 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1031 * If there is no data in a subtree then it is pruned.
1034 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1035 struct yaffs_tnode *tn, u32 level,
1045 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1046 if (tn->internal[i]) {
1048 yaffs_prune_worker(dev,
1055 if (tn->internal[i])
1059 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1060 u32 *map = (u32 *) tn;
1062 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1068 if (has_data == 0 && del0) {
1069 /* Free and return NULL */
1071 yaffs_free_tnode(dev, tn);
1081 static int yaffs_prune_tree(struct yaffs_dev *dev,
1082 struct yaffs_file_var *file_struct)
1087 struct yaffs_tnode *tn;
1089 if (file_struct->top_level > 0) {
1091 yaffs_prune_worker(dev, file_struct->top,
1092 file_struct->top_level, 0);
1094 /* Now we have a tree with all the non-zero branches NULL but the height
1095 * is the same as it was.
1096 * Let's see if we can trim internal tnodes to shorten the tree.
1097 * We can do this if only the 0th element in the tnode is in use
1098 * (ie all the non-zero are NULL)
1101 while (file_struct->top_level && !done) {
1102 tn = file_struct->top;
1105 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1106 if (tn->internal[i])
1111 file_struct->top = tn->internal[0];
1112 file_struct->top_level--;
1113 yaffs_free_tnode(dev, tn);
1123 /*-------------------- End of File Structure functions.-------------------*/
1125 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1126 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1128 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1133 /* Now sweeten it up... */
1135 memset(obj, 0, sizeof(struct yaffs_obj));
1136 obj->being_created = 1;
1140 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1141 INIT_LIST_HEAD(&(obj->hard_links));
1142 INIT_LIST_HEAD(&(obj->hash_link));
1143 INIT_LIST_HEAD(&obj->siblings);
1145 /* Now make the directory sane */
1146 if (dev->root_dir) {
1147 obj->parent = dev->root_dir;
1148 list_add(&(obj->siblings),
1149 &dev->root_dir->variant.dir_variant.children);
1152 /* Add it to the lost and found directory.
1153 * NB Can't put root or lost-n-found in lost-n-found so
1154 * check if lost-n-found exists first
1156 if (dev->lost_n_found)
1157 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1159 obj->being_created = 0;
1162 dev->checkpoint_blocks_required = 0; /* force recalculation */
1167 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1168 int number, u32 mode)
1171 struct yaffs_obj *obj =
1172 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1174 obj->fake = 1; /* it is fake so it might have no NAND presence... */
1175 obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
1176 obj->unlink_allowed = 0; /* ... or unlink it */
1179 obj->yst_mode = mode;
1181 obj->hdr_chunk = 0; /* Not a valid chunk. */
1188 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1191 struct yaffs_dev *dev = obj->my_dev;
1193 /* If it is still linked into the bucket list, free from the list */
1194 if (!list_empty(&obj->hash_link)) {
1195 list_del_init(&obj->hash_link);
1196 bucket = yaffs_hash_fn(obj->obj_id);
1197 dev->obj_bucket[bucket].count--;
1201 /* FreeObject frees up a Object and puts it back on the free list */
1202 static void yaffs_free_obj(struct yaffs_obj *obj)
1204 struct yaffs_dev *dev = obj->my_dev;
1207 (TSTR("FreeObject %p inode %p" TENDSTR), obj, obj->my_inode));
1213 if (!list_empty(&obj->siblings))
1216 if (obj->my_inode) {
1217 /* We're still hooked up to a cached inode.
1218 * Don't delete now, but mark for later deletion
1220 obj->defered_free = 1;
1224 yaffs_unhash_obj(obj);
1226 yaffs_free_raw_obj(dev, obj);
1228 dev->checkpoint_blocks_required = 0; /* force recalculation */
1231 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1233 if (obj->defered_free)
1234 yaffs_free_obj(obj);
1237 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
1244 yaffs_init_raw_tnodes_and_objs(dev);
1246 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1247 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
1248 dev->obj_bucket[i].count = 0;
1252 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1256 int lowest = 999999;
1258 /* Search for the shortest list or one that
1262 for (i = 0; i < 10 && lowest > 4; i++) {
1263 dev->bucket_finder++;
1264 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1265 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1266 lowest = dev->obj_bucket[dev->bucket_finder].count;
1267 l = dev->bucket_finder;
1275 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1277 int bucket = yaffs_find_nice_bucket(dev);
1279 /* Now find an object value that has not already been taken
1280 * by scanning the list.
1284 struct list_head *i;
1286 u32 n = (u32) bucket;
1288 /* yaffs_check_obj_hash_sane(); */
1292 n += YAFFS_NOBJECT_BUCKETS;
1293 if (1 || dev->obj_bucket[bucket].count > 0) {
1294 list_for_each(i, &dev->obj_bucket[bucket].list) {
1295 /* If there is already one in the list */
1296 if (i && list_entry(i, struct yaffs_obj,
1297 hash_link)->obj_id == n) {
1307 static void yaffs_hash_obj(struct yaffs_obj *in)
1309 int bucket = yaffs_hash_fn(in->obj_id);
1310 struct yaffs_dev *dev = in->my_dev;
1312 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1313 dev->obj_bucket[bucket].count++;
1316 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1318 int bucket = yaffs_hash_fn(number);
1319 struct list_head *i;
1320 struct yaffs_obj *in;
1322 list_for_each(i, &dev->obj_bucket[bucket].list) {
1323 /* Look if it is in the list */
1325 in = list_entry(i, struct yaffs_obj, hash_link);
1326 if (in->obj_id == number) {
1328 /* Don't tell the VFS about this one if it is defered free */
1329 if (in->defered_free)
1340 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1341 enum yaffs_obj_type type)
1343 struct yaffs_obj *the_obj = NULL;
1344 struct yaffs_tnode *tn = NULL;
1347 number = yaffs_new_obj_id(dev);
1349 if (type == YAFFS_OBJECT_TYPE_FILE) {
1350 tn = yaffs_get_tnode(dev);
1355 the_obj = yaffs_alloc_empty_obj(dev);
1358 yaffs_free_tnode(dev, tn);
1364 the_obj->rename_allowed = 1;
1365 the_obj->unlink_allowed = 1;
1366 the_obj->obj_id = number;
1367 yaffs_hash_obj(the_obj);
1368 the_obj->variant_type = type;
1369 yaffs_load_current_time(the_obj, 1, 1);
1372 case YAFFS_OBJECT_TYPE_FILE:
1373 the_obj->variant.file_variant.file_size = 0;
1374 the_obj->variant.file_variant.scanned_size = 0;
1375 the_obj->variant.file_variant.shrink_size = ~0; /* max */
1376 the_obj->variant.file_variant.top_level = 0;
1377 the_obj->variant.file_variant.top = tn;
1379 case YAFFS_OBJECT_TYPE_DIRECTORY:
1380 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
1381 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
1383 case YAFFS_OBJECT_TYPE_SYMLINK:
1384 case YAFFS_OBJECT_TYPE_HARDLINK:
1385 case YAFFS_OBJECT_TYPE_SPECIAL:
1386 /* No action required */
1388 case YAFFS_OBJECT_TYPE_UNKNOWN:
1389 /* todo this should not happen */
1397 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
1399 enum yaffs_obj_type type)
1401 struct yaffs_obj *the_obj = NULL;
1404 the_obj = yaffs_find_by_number(dev, number);
1407 the_obj = yaffs_new_obj(dev, number, type);
1413 YCHAR *yaffs_clone_str(const YCHAR * str)
1415 YCHAR *new_str = NULL;
1421 len = yaffs_strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
1422 new_str = YMALLOC((len + 1) * sizeof(YCHAR));
1424 yaffs_strncpy(new_str, str, len);
1432 * Mknod (create) a new object.
1433 * equiv_obj only has meaning for a hard link;
1434 * alias_str only has meaning for a symlink.
1435 * rdev only has meaning for devices (a subset of special objects)
1438 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
1439 struct yaffs_obj *parent,
1444 struct yaffs_obj *equiv_obj,
1445 const YCHAR * alias_str, u32 rdev)
1447 struct yaffs_obj *in;
1450 struct yaffs_dev *dev = parent->my_dev;
1452 /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
1453 if (yaffs_find_by_name(parent, name))
1456 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
1457 str = yaffs_clone_str(alias_str);
1462 in = yaffs_new_obj(dev, -1, type);
1473 in->variant_type = type;
1475 in->yst_mode = mode;
1477 yaffs_attribs_init(in, gid, uid, rdev);
1479 in->n_data_chunks = 0;
1481 yaffs_set_obj_name(in, name);
1484 yaffs_add_obj_to_dir(parent, in);
1486 in->my_dev = parent->my_dev;
1489 case YAFFS_OBJECT_TYPE_SYMLINK:
1490 in->variant.symlink_variant.alias = str;
1492 case YAFFS_OBJECT_TYPE_HARDLINK:
1493 in->variant.hardlink_variant.equiv_obj = equiv_obj;
1494 in->variant.hardlink_variant.equiv_id =
1496 list_add(&in->hard_links, &equiv_obj->hard_links);
1498 case YAFFS_OBJECT_TYPE_FILE:
1499 case YAFFS_OBJECT_TYPE_DIRECTORY:
1500 case YAFFS_OBJECT_TYPE_SPECIAL:
1501 case YAFFS_OBJECT_TYPE_UNKNOWN:
1506 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
1507 /* Could not create the object header, fail the creation */
1512 yaffs_update_parent(parent);
1518 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
1519 const YCHAR * name, u32 mode, u32 uid,
1522 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
1523 uid, gid, NULL, NULL, 0);
1526 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
1527 u32 mode, u32 uid, u32 gid)
1529 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
1530 mode, uid, gid, NULL, NULL, 0);
1533 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
1534 const YCHAR * name, u32 mode, u32 uid,
1537 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
1538 uid, gid, NULL, NULL, rdev);
1541 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
1542 const YCHAR * name, u32 mode, u32 uid,
1543 u32 gid, const YCHAR * alias)
1545 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
1546 uid, gid, NULL, alias, 0);
1549 /* yaffs_link_obj returns the object id of the equivalent object.*/
1550 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
1551 struct yaffs_obj *equiv_obj)
1553 /* Get the real object in case we were fed a hard link as an equivalent object */
1554 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
1556 if (yaffs_create_obj
1557 (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
1558 equiv_obj, NULL, 0)) {
1566 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1567 struct yaffs_obj *new_dir,
1568 const YCHAR * new_name, int force, int shadows)
1573 struct yaffs_obj *existing_target;
1575 if (new_dir == NULL)
1576 new_dir = obj->parent; /* use the old directory */
1578 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1579 T(YAFFS_TRACE_ALWAYS,
1581 ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
1586 /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1587 if (obj->my_dev->param.is_yaffs2)
1588 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1590 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1591 && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1593 del_op = (new_dir == obj->my_dev->del_dir);
1595 existing_target = yaffs_find_by_name(new_dir, new_name);
1597 /* If the object is a file going into the unlinked directory,
1598 * then it is OK to just stuff it in since duplicate names are allowed.
1599 * else only proceed if the new name does not exist and if we're putting
1600 * it into a directory.
1606 !existing_target) &&
1607 new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1608 yaffs_set_obj_name(obj, new_name);
1611 yaffs_add_obj_to_dir(new_dir, obj);
1616 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1617 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
1625 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
1626 struct yaffs_obj *new_dir, const YCHAR * new_name)
1628 struct yaffs_obj *obj = NULL;
1629 struct yaffs_obj *existing_target = NULL;
1632 struct yaffs_dev *dev;
1634 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1636 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1639 dev = old_dir->my_dev;
1641 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1642 /* Special case for case insemsitive systems.
1643 * While look-up is case insensitive, the name isn't.
1644 * Therefore we might want to change x.txt to X.txt
1646 if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0)
1650 if (yaffs_strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
1651 YAFFS_MAX_NAME_LENGTH)
1655 obj = yaffs_find_by_name(old_dir, old_name);
1657 if (obj && obj->rename_allowed) {
1659 /* Now do the handling for an existing target, if there is one */
1661 existing_target = yaffs_find_by_name(new_dir, new_name);
1662 if (existing_target &&
1663 existing_target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY
1664 && !list_empty(&existing_target->variant.dir_variant.
1666 /* There is a target that is a non-empty directory, so we fail */
1667 return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
1668 } else if (existing_target && existing_target != obj) {
1669 /* Nuke the target first, using shadowing,
1670 * but only if it isn't the same object.
1672 * Note we must disable gc otherwise it can mess up the shadowing.
1675 dev->gc_disable = 1;
1676 yaffs_change_obj_name(obj, new_dir, new_name, force,
1677 existing_target->obj_id);
1678 existing_target->is_shadowed = 1;
1679 yaffs_unlink_obj(existing_target);
1680 dev->gc_disable = 0;
1683 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
1685 yaffs_update_parent(old_dir);
1686 if (new_dir != old_dir)
1687 yaffs_update_parent(new_dir);
1694 /*------------------------- Block Management and Page Allocation ----------------*/
1696 static int yaffs_init_blocks(struct yaffs_dev *dev)
1698 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
1700 dev->block_info = NULL;
1701 dev->chunk_bits = NULL;
1703 dev->alloc_block = -1; /* force it to get a new one */
1705 /* If the first allocation strategy fails, thry the alternate one */
1706 dev->block_info = YMALLOC(n_blocks * sizeof(struct yaffs_block_info));
1707 if (!dev->block_info) {
1709 YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_info));
1710 dev->block_info_alt = 1;
1712 dev->block_info_alt = 0;
1714 if (dev->block_info) {
1715 /* Set up dynamic blockinfo stuff. */
1716 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */
1717 dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * n_blocks);
1718 if (!dev->chunk_bits) {
1720 YMALLOC_ALT(dev->chunk_bit_stride * n_blocks);
1721 dev->chunk_bits_alt = 1;
1723 dev->chunk_bits_alt = 0;
1726 if (dev->block_info && dev->chunk_bits) {
1727 memset(dev->block_info, 0,
1728 n_blocks * sizeof(struct yaffs_block_info));
1729 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
1736 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
1738 if (dev->block_info_alt && dev->block_info)
1739 YFREE_ALT(dev->block_info);
1740 else if (dev->block_info)
1741 YFREE(dev->block_info);
1743 dev->block_info_alt = 0;
1745 dev->block_info = NULL;
1747 if (dev->chunk_bits_alt && dev->chunk_bits)
1748 YFREE_ALT(dev->chunk_bits);
1749 else if (dev->chunk_bits)
1750 YFREE(dev->chunk_bits);
1751 dev->chunk_bits_alt = 0;
1752 dev->chunk_bits = NULL;
1755 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
1757 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
1761 /* If the block is still healthy erase it and mark as clean.
1762 * If the block has had a data failure, then retire it.
1765 T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
1766 (TSTR("yaffs_block_became_dirty block %d state %d %s" TENDSTR),
1767 block_no, bi->block_state,
1768 (bi->needs_retiring) ? "needs retiring" : ""));
1770 yaffs2_clear_oldest_dirty_seq(dev, bi);
1772 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
1774 /* If this is the block being garbage collected then stop gc'ing this block */
1775 if (block_no == dev->gc_block)
1778 /* If this block is currently the best candidate for gc then drop as a candidate */
1779 if (block_no == dev->gc_dirtiest) {
1780 dev->gc_dirtiest = 0;
1781 dev->gc_pages_in_use = 0;
1784 if (!bi->needs_retiring) {
1785 yaffs2_checkpt_invalidate(dev);
1786 erased_ok = yaffs_erase_block(dev, block_no);
1788 dev->n_erase_failures++;
1789 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1790 (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
1795 ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
1796 || !yaffs_skip_verification(dev))) {
1798 for (i = 0; i < dev->param.chunks_per_block; i++) {
1799 if (!yaffs_check_chunk_erased
1800 (dev, block_no * dev->param.chunks_per_block + i)) {
1801 T(YAFFS_TRACE_ERROR,
1803 (">>Block %d erasure supposedly OK, but chunk %d not erased"
1804 TENDSTR), block_no, i));
1810 /* Clean it up... */
1811 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
1813 dev->n_erased_blocks++;
1814 bi->pages_in_use = 0;
1815 bi->soft_del_pages = 0;
1816 bi->has_shrink_hdr = 0;
1817 bi->skip_erased_check = 1; /* This is clean, so no need to check */
1818 bi->gc_prioritise = 0;
1819 yaffs_clear_chunk_bits(dev, block_no);
1821 T(YAFFS_TRACE_ERASE,
1822 (TSTR("Erased block %d" TENDSTR), block_no));
1824 dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */
1826 yaffs_retire_block(dev, block_no);
1827 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1828 (TSTR("**>> Block %d retired" TENDSTR), block_no));
1832 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
1836 struct yaffs_block_info *bi;
1838 if (dev->n_erased_blocks < 1) {
1839 /* Hoosterman we've got a problem.
1840 * Can't get space to gc
1842 T(YAFFS_TRACE_ERROR,
1843 (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
1848 /* Find an empty block. */
1850 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
1851 dev->alloc_block_finder++;
1852 if (dev->alloc_block_finder < dev->internal_start_block
1853 || dev->alloc_block_finder > dev->internal_end_block) {
1854 dev->alloc_block_finder = dev->internal_start_block;
1857 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
1859 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1860 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
1862 bi->seq_number = dev->seq_number;
1863 dev->n_erased_blocks--;
1864 T(YAFFS_TRACE_ALLOCATE,
1865 (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
1866 dev->alloc_block_finder, dev->seq_number,
1867 dev->n_erased_blocks));
1868 return dev->alloc_block_finder;
1872 T(YAFFS_TRACE_ALWAYS,
1874 ("yaffs tragedy: no more erased blocks, but there should have been %d"
1875 TENDSTR), dev->n_erased_blocks));
1881 * Check if there's space to allocate...
1882 * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()?
1884 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
1886 int reserved_chunks;
1887 int reserved_blocks = dev->param.n_reserved_blocks;
1890 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
1893 ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
1895 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
1898 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
1899 struct yaffs_block_info **block_ptr)
1902 struct yaffs_block_info *bi;
1904 if (dev->alloc_block < 0) {
1905 /* Get next block to allocate off */
1906 dev->alloc_block = yaffs_find_alloc_block(dev);
1907 dev->alloc_page = 0;
1910 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
1911 /* Not enough space to allocate unless we're allowed to use the reserve. */
1915 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
1916 && dev->alloc_page == 0) {
1917 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
1920 /* Next page please.... */
1921 if (dev->alloc_block >= 0) {
1922 bi = yaffs_get_block_info(dev, dev->alloc_block);
1924 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
1927 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
1931 dev->n_free_chunks--;
1933 /* If the block is full set the state to full */
1934 if (dev->alloc_page >= dev->param.chunks_per_block) {
1935 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1936 dev->alloc_block = -1;
1945 T(YAFFS_TRACE_ERROR,
1946 (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
1951 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
1955 n = dev->n_erased_blocks * dev->param.chunks_per_block;
1957 if (dev->alloc_block > 0)
1958 n += (dev->param.chunks_per_block - dev->alloc_page);
1965 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
1966 * if we don't want to write to it.
1968 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
1970 if (dev->alloc_block > 0) {
1971 struct yaffs_block_info *bi =
1972 yaffs_get_block_info(dev, dev->alloc_block);
1973 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
1974 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1975 dev->alloc_block = -1;
1980 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
1985 int ret_val = YAFFS_OK;
1987 int is_checkpt_block;
1991 int chunks_before = yaffs_get_erased_chunks(dev);
1994 struct yaffs_ext_tags tags;
1996 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
1998 struct yaffs_obj *object;
2000 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2002 T(YAFFS_TRACE_TRACING,
2004 ("Collecting block %d, in use %d, shrink %d, whole_block %d"
2005 TENDSTR), block, bi->pages_in_use, bi->has_shrink_hdr,
2008 /*yaffs_verify_free_chunks(dev); */
2010 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2011 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2013 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2015 dev->gc_disable = 1;
2017 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2018 T(YAFFS_TRACE_TRACING,
2020 ("Collecting block %d that has no chunks in use" TENDSTR),
2022 yaffs_block_became_dirty(dev, block);
2025 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2027 yaffs_verify_blk(dev, bi, block);
2029 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2030 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2032 for ( /* init already done */ ;
2033 ret_val == YAFFS_OK &&
2034 dev->gc_chunk < dev->param.chunks_per_block &&
2035 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2036 max_copies > 0; dev->gc_chunk++, old_chunk++) {
2037 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2039 /* This page is in use and might need to be copied off */
2045 yaffs_init_tags(&tags);
2047 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2050 object = yaffs_find_by_number(dev, tags.obj_id);
2052 T(YAFFS_TRACE_GC_DETAIL,
2054 ("Collecting chunk in block %d, %d %d %d "
2055 TENDSTR), dev->gc_chunk, tags.obj_id,
2056 tags.chunk_id, tags.n_bytes));
2058 if (object && !yaffs_skip_verification(dev)) {
2059 if (tags.chunk_id == 0)
2062 else if (object->soft_del)
2063 matching_chunk = old_chunk; /* Defeat the test */
2066 yaffs_find_chunk_in_file
2067 (object, tags.chunk_id,
2070 if (old_chunk != matching_chunk)
2071 T(YAFFS_TRACE_ERROR,
2073 ("gc: page in gc mismatch: %d %d %d %d"
2074 TENDSTR), old_chunk,
2075 matching_chunk, tags.obj_id,
2081 T(YAFFS_TRACE_ERROR,
2083 ("page %d in gc has no object: %d %d %d "
2084 TENDSTR), old_chunk,
2085 tags.obj_id, tags.chunk_id,
2091 object->soft_del && tags.chunk_id != 0) {
2092 /* Data chunk in a soft deleted file, throw it away
2093 * It's a soft deleted data chunk,
2094 * No need to copy this, just forget about it and
2095 * fix up the object.
2098 /* Free chunks already includes softdeleted chunks.
2099 * How ever this chunk is going to soon be really deleted
2100 * which will increment free chunks.
2101 * We have to decrement free chunks so this works out properly.
2103 dev->n_free_chunks--;
2104 bi->soft_del_pages--;
2106 object->n_data_chunks--;
2108 if (object->n_data_chunks <= 0) {
2109 /* remeber to clean up the object */
2110 dev->gc_cleanup_list[dev->
2117 /* Todo object && object->deleted && object->n_data_chunks == 0 */
2118 /* Deleted object header with no data chunks.
2119 * Can be discarded and the file deleted.
2121 object->hdr_chunk = 0;
2122 yaffs_free_tnode(object->my_dev,
2124 variant.file_variant.
2126 object->variant.file_variant.top = NULL;
2127 yaffs_generic_obj_del(object);
2129 } else if (object) {
2130 /* It's either a data chunk in a live file or
2131 * an ObjectHeader, so we're interested in it.
2132 * NB Need to keep the ObjectHeaders of deleted files
2133 * until the whole file has been deleted off
2135 tags.serial_number++;
2139 if (tags.chunk_id == 0) {
2140 /* It is an object Id,
2141 * We need to nuke the shrinkheader flags first
2142 * Also need to clean up shadowing.
2143 * We no longer want the shrink_header flag since its work is done
2144 * and if it is left in place it will mess up scanning.
2147 struct yaffs_obj_hdr *oh;
2148 oh = (struct yaffs_obj_hdr *)
2152 tags.extra_is_shrink = 0;
2154 oh->shadows_obj = 0;
2155 oh->inband_shadowed_obj_id = 0;
2156 tags.extra_shadows = 0;
2158 /* Update file size */
2159 if (object->variant_type ==
2160 YAFFS_OBJECT_TYPE_FILE) {
2169 yaffs_verify_oh(object, oh,
2172 yaffs_write_new_chunk(dev,
2179 yaffs_write_new_chunk(dev,
2184 if (new_chunk < 0) {
2185 ret_val = YAFFS_FAIL;
2188 /* Ok, now fix up the Tnodes etc. */
2190 if (tags.chunk_id == 0) {
2197 /* It's a data chunk */
2199 ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
2204 if (ret_val == YAFFS_OK)
2205 yaffs_chunk_del(dev, old_chunk,
2206 mark_flash, __LINE__);
2211 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2215 yaffs_verify_collected_blk(dev, bi, block);
2217 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2219 * The gc did not complete. Set block state back to FULL
2220 * because checkpointing does not restore gc.
2222 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2224 /* The gc completed. */
2225 /* Do any required cleanups */
2226 for (i = 0; i < dev->n_clean_ups; i++) {
2227 /* Time to delete the file too */
2229 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2231 yaffs_free_tnode(dev,
2234 object->variant.file_variant.top = NULL;
2237 ("yaffs: About to finally delete object %d"
2238 TENDSTR), object->obj_id));
2239 yaffs_generic_obj_del(object);
2240 object->my_dev->n_deleted_files--;
2245 chunks_after = yaffs_get_erased_chunks(dev);
2246 if (chunks_before >= chunks_after) {
2249 ("gc did not increase free chunks before %d after %d"
2250 TENDSTR), chunks_before, chunks_after));
2254 dev->n_clean_ups = 0;
2257 dev->gc_disable = 0;
2263 * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2264 * for garbage collection.
2267 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2268 int aggressive, int background)
2272 unsigned selected = 0;
2273 int prioritised = 0;
2274 int prioritised_exist = 0;
2275 struct yaffs_block_info *bi;
2278 /* First let's see if we need to grab a prioritised block */
2279 if (dev->has_pending_prioritised_gc && !aggressive) {
2280 dev->gc_dirtiest = 0;
2281 bi = dev->block_info;
2282 for (i = dev->internal_start_block;
2283 i <= dev->internal_end_block && !selected; i++) {
2285 if (bi->gc_prioritise) {
2286 prioritised_exist = 1;
2287 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2288 yaffs_block_ok_for_gc(dev, bi)) {
2297 * If there is a prioritised block and none was selected then
2298 * this happened because there is at least one old dirty block gumming
2299 * up the works. Let's gc the oldest dirty block.
2302 if (prioritised_exist &&
2303 !selected && dev->oldest_dirty_block > 0)
2304 selected = dev->oldest_dirty_block;
2306 if (!prioritised_exist) /* None found, so we can clear this */
2307 dev->has_pending_prioritised_gc = 0;
2310 /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2312 * else (we're doing a leasurely gc), then we only bother to do this if the
2313 * block has only a few pages in use.
2319 dev->internal_end_block - dev->internal_start_block + 1;
2321 threshold = dev->param.chunks_per_block;
2322 iterations = n_blocks;
2327 max_threshold = dev->param.chunks_per_block / 2;
2329 max_threshold = dev->param.chunks_per_block / 8;
2331 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2332 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2334 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2335 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2336 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2337 if (threshold > max_threshold)
2338 threshold = max_threshold;
2340 iterations = n_blocks / 16 + 1;
2341 if (iterations > 100)
2347 (dev->gc_dirtiest < 1 ||
2348 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
2349 dev->gc_block_finder++;
2350 if (dev->gc_block_finder < dev->internal_start_block ||
2351 dev->gc_block_finder > dev->internal_end_block)
2352 dev->gc_block_finder =
2353 dev->internal_start_block;
2355 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2357 pages_used = bi->pages_in_use - bi->soft_del_pages;
2359 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2360 pages_used < dev->param.chunks_per_block &&
2361 (dev->gc_dirtiest < 1
2362 || pages_used < dev->gc_pages_in_use)
2363 && yaffs_block_ok_for_gc(dev, bi)) {
2364 dev->gc_dirtiest = dev->gc_block_finder;
2365 dev->gc_pages_in_use = pages_used;
2369 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2370 selected = dev->gc_dirtiest;
2374 * If nothing has been selected for a while, try selecting the oldest dirty
2375 * because that's gumming up the works.
2378 if (!selected && dev->param.is_yaffs2 &&
2379 dev->gc_not_done >= (background ? 10 : 20)) {
2380 yaffs2_find_oldest_dirty_seq(dev);
2381 if (dev->oldest_dirty_block > 0) {
2382 selected = dev->oldest_dirty_block;
2383 dev->gc_dirtiest = selected;
2384 dev->oldest_dirty_gc_count++;
2385 bi = yaffs_get_block_info(dev, selected);
2386 dev->gc_pages_in_use =
2387 bi->pages_in_use - bi->soft_del_pages;
2389 dev->gc_not_done = 0;
2395 ("GC Selected block %d with %d free, prioritised:%d"
2397 dev->param.chunks_per_block - dev->gc_pages_in_use,
2404 dev->gc_dirtiest = 0;
2405 dev->gc_pages_in_use = 0;
2406 dev->gc_not_done = 0;
2407 if (dev->refresh_skip > 0)
2408 dev->refresh_skip--;
2413 ("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s"
2414 TENDSTR), dev->gc_block_finder, dev->gc_not_done, threshold,
2415 dev->gc_dirtiest, dev->gc_pages_in_use,
2416 dev->oldest_dirty_block, background ? " bg" : ""));
2422 /* New garbage collector
2423 * If we're very low on erased blocks then we do aggressive garbage collection
2424 * otherwise we do "leasurely" garbage collection.
2425 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2426 * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2428 * The idea is to help clear out space in a more spread-out manner.
2429 * Dunno if it really does anything useful.
2431 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2434 int gc_ok = YAFFS_OK;
2438 int checkpt_block_adjust;
2440 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2443 if (dev->gc_disable) {
2444 /* Bail out so we don't get recursive gc */
2448 /* This loop should pass the first time.
2449 * We'll only see looping here if the collection does not increase space.
2455 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2458 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2460 dev->n_erased_blocks * dev->param.chunks_per_block;
2462 /* If we need a block soon then do aggressive gc. */
2463 if (dev->n_erased_blocks < min_erased)
2467 && erased_chunks > (dev->n_free_chunks / 4))
2470 if (dev->gc_skip > 20)
2472 if (erased_chunks < dev->n_free_chunks / 2 ||
2473 dev->gc_skip < 1 || background)
2483 /* If we don't already have a block being gc'd then see if we should start another */
2485 if (dev->gc_block < 1 && !aggressive) {
2486 dev->gc_block = yaffs2_find_refresh_block(dev);
2488 dev->n_clean_ups = 0;
2490 if (dev->gc_block < 1) {
2492 yaffs_find_gc_block(dev, aggressive, background);
2494 dev->n_clean_ups = 0;
2497 if (dev->gc_block > 0) {
2500 dev->passive_gc_count++;
2504 ("yaffs: GC n_erased_blocks %d aggressive %d"
2505 TENDSTR), dev->n_erased_blocks, aggressive));
2507 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2510 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
2511 && dev->gc_block > 0) {
2514 ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d"
2515 TENDSTR), dev->n_erased_blocks, max_tries,
2518 } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2519 (dev->gc_block > 0) && (max_tries < 2));
2521 return aggressive ? gc_ok : YAFFS_OK;
2526 * Garbage collects. Intended to be called from a background thread.
2527 * Returns non-zero if at least half the free chunks are erased.
2529 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2531 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2533 T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR), urgency));
2535 yaffs_check_gc(dev, 1);
2536 return erased_chunks > dev->n_free_chunks / 2;
2539 /*------------------------- TAGS --------------------------------*/
2541 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
2544 return (tags->chunk_id == chunk_obj &&
2545 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
2549 /*-------------------- Data file manipulation -----------------*/
2551 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
2552 struct yaffs_ext_tags *tags)
2554 /*Get the Tnode, then get the level 0 offset chunk offset */
2555 struct yaffs_tnode *tn;
2557 struct yaffs_ext_tags local_tags;
2560 struct yaffs_dev *dev = in->my_dev;
2563 /* Passed a NULL, so use our own tags space */
2567 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2570 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2573 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2579 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
2580 struct yaffs_ext_tags *tags)
2582 /* Get the Tnode, then get the level 0 offset chunk offset */
2583 struct yaffs_tnode *tn;
2585 struct yaffs_ext_tags local_tags;
2587 struct yaffs_dev *dev = in->my_dev;
2591 /* Passed a NULL, so use our own tags space */
2595 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2599 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2602 yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2605 /* Delete the entry in the filestructure (if found) */
2607 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
2613 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
2614 int nand_chunk, int in_scan)
2616 /* NB in_scan is zero unless scanning.
2617 * For forward scanning, in_scan is > 0;
2618 * for backward scanning in_scan is < 0
2620 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
2623 struct yaffs_tnode *tn;
2624 struct yaffs_dev *dev = in->my_dev;
2626 struct yaffs_ext_tags existing_tags;
2627 struct yaffs_ext_tags new_tags;
2628 unsigned existing_serial, new_serial;
2630 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
2631 /* Just ignore an attempt at putting a chunk into a non-file during scanning
2632 * If it is not during Scanning then something went wrong!
2635 T(YAFFS_TRACE_ERROR,
2637 ("yaffs tragedy:attempt to put data chunk into a non-file"
2642 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
2646 tn = yaffs_add_find_tnode_0(dev,
2647 &in->variant.file_variant,
2653 /* Dummy insert, bail now */
2656 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
2659 /* If we're scanning then we need to test for duplicates
2660 * NB This does not need to be efficient since it should only ever
2661 * happen when the power fails during a write, then only one
2662 * chunk should ever be affected.
2664 * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
2665 * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
2668 if (existing_cunk > 0) {
2669 /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
2670 * thus we have to do a FindChunkInFile to get the real chunk id.
2672 * We have a duplicate now we need to decide which one to use:
2674 * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
2675 * Forward scanning YAFFS2: The new one is what we use, dump the old one.
2676 * YAFFS1: Get both sets of tags and compare serial numbers.
2680 /* Only do this for forward scanning */
2681 yaffs_rd_chunk_tags_nand(dev,
2685 /* Do a proper find */
2687 yaffs_find_chunk_in_file(in, inode_chunk,
2691 if (existing_cunk <= 0) {
2692 /*Hoosterman - how did this happen? */
2694 T(YAFFS_TRACE_ERROR,
2696 ("yaffs tragedy: existing chunk < 0 in scan"
2701 /* NB The deleted flags should be false, otherwise the chunks will
2702 * not be loaded during a scan
2706 new_serial = new_tags.serial_number;
2707 existing_serial = existing_tags.serial_number;
2710 if ((in_scan > 0) &&
2711 (existing_cunk <= 0 ||
2712 ((existing_serial + 1) & 3) == new_serial)) {
2713 /* Forward scanning.
2715 * Delete the old one and drop through to update the tnode
2717 yaffs_chunk_del(dev, existing_cunk, 1,
2720 /* Backward scanning or we want to use the existing one
2722 * Delete the new one and return early so that the tnode isn't changed
2724 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
2731 if (existing_cunk == 0)
2732 in->n_data_chunks++;
2734 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
2739 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2741 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2743 if (nand_chunk >= 0)
2744 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2747 T(YAFFS_TRACE_NANDACCESS,
2748 (TSTR("Chunk %d not found zero instead" TENDSTR),
2750 /* get sane (zero) data if you read a hole */
2751 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2757 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2762 struct yaffs_ext_tags tags;
2763 struct yaffs_block_info *bi;
2769 block = chunk_id / dev->param.chunks_per_block;
2770 page = chunk_id % dev->param.chunks_per_block;
2772 if (!yaffs_check_chunk_bit(dev, block, page))
2773 T(YAFFS_TRACE_VERIFY,
2774 (TSTR("Deleting invalid chunk %d" TENDSTR), chunk_id));
2776 bi = yaffs_get_block_info(dev, block);
2778 yaffs2_update_oldest_dirty_seq(dev, block, bi);
2780 T(YAFFS_TRACE_DELETION,
2781 (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
2783 if (!dev->param.is_yaffs2 && mark_flash &&
2784 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2786 yaffs_init_tags(&tags);
2788 tags.is_deleted = 1;
2790 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2791 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2793 dev->n_unmarked_deletions++;
2796 /* Pull out of the management area.
2797 * If the whole block became dirty, this will kick off an erasure.
2799 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2800 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2801 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
2802 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2803 dev->n_free_chunks++;
2805 yaffs_clear_chunk_bit(dev, block, page);
2809 if (bi->pages_in_use == 0 &&
2810 !bi->has_shrink_hdr &&
2811 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2812 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
2813 yaffs_block_became_dirty(dev, block);
2820 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
2821 const u8 * buffer, int n_bytes, int use_reserve)
2823 /* Find old chunk Need to do this to get serial number
2824 * Write new one and patch into tree.
2825 * Invalidate old tags.
2829 struct yaffs_ext_tags prev_tags;
2832 struct yaffs_ext_tags new_tags;
2834 struct yaffs_dev *dev = in->my_dev;
2836 yaffs_check_gc(dev, 0);
2838 /* Get the previous chunk at this location in the file if it exists.
2839 * If it does not exist then put a zero into the tree. This creates
2840 * the tnode now, rather than later when it is harder to clean up.
2842 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
2843 if (prev_chunk_id < 1 &&
2844 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
2847 /* Set up new tags */
2848 yaffs_init_tags(&new_tags);
2850 new_tags.chunk_id = inode_chunk;
2851 new_tags.obj_id = in->obj_id;
2852 new_tags.serial_number =
2853 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
2854 new_tags.n_bytes = n_bytes;
2856 if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
2857 T(YAFFS_TRACE_ERROR,
2858 (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR),
2864 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
2866 if (new_chunk_id > 0) {
2867 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
2869 if (prev_chunk_id > 0)
2870 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
2872 yaffs_verify_file_sane(in);
2874 return new_chunk_id;
2878 /* UpdateObjectHeader updates the header on NAND for an object.
2879 * If name is not NULL, then that new name is used.
2881 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
2882 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
2885 struct yaffs_block_info *bi;
2887 struct yaffs_dev *dev = in->my_dev;
2894 struct yaffs_ext_tags new_tags;
2895 struct yaffs_ext_tags old_tags;
2896 const YCHAR *alias = NULL;
2899 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
2901 struct yaffs_obj_hdr *oh = NULL;
2903 yaffs_strcpy(old_name, _Y("silly old name"));
2905 if (!in->fake || in == dev->root_dir || /* The root_dir should also be saved */
2908 yaffs_check_gc(dev, 0);
2909 yaffs_check_obj_details_loaded(in);
2911 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
2912 oh = (struct yaffs_obj_hdr *)buffer;
2914 prev_chunk_id = in->hdr_chunk;
2916 if (prev_chunk_id > 0) {
2917 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
2920 yaffs_verify_oh(in, oh, &old_tags, 0);
2922 memcpy(old_name, oh->name, sizeof(oh->name));
2923 memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
2925 memset(buffer, 0xFF, dev->data_bytes_per_chunk);
2927 oh->type = in->variant_type;
2928 oh->yst_mode = in->yst_mode;
2929 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
2931 yaffs_load_attribs_oh(oh, in);
2934 oh->parent_obj_id = in->parent->obj_id;
2936 oh->parent_obj_id = 0;
2938 if (name && *name) {
2939 memset(oh->name, 0, sizeof(oh->name));
2940 yaffs_load_oh_from_name(dev, oh->name, name);
2941 } else if (prev_chunk_id > 0)
2942 memcpy(oh->name, old_name, sizeof(oh->name));
2944 memset(oh->name, 0, sizeof(oh->name));
2946 oh->is_shrink = is_shrink;
2948 switch (in->variant_type) {
2949 case YAFFS_OBJECT_TYPE_UNKNOWN:
2950 /* Should not happen */
2952 case YAFFS_OBJECT_TYPE_FILE:
2954 (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
2955 || oh->parent_obj_id ==
2956 YAFFS_OBJECTID_UNLINKED) ? 0 : in->
2957 variant.file_variant.file_size;
2959 case YAFFS_OBJECT_TYPE_HARDLINK:
2960 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
2962 case YAFFS_OBJECT_TYPE_SPECIAL:
2965 case YAFFS_OBJECT_TYPE_DIRECTORY:
2968 case YAFFS_OBJECT_TYPE_SYMLINK:
2969 alias = in->variant.symlink_variant.alias;
2971 alias = _Y("no alias");
2972 yaffs_strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
2973 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
2977 /* process any xattrib modifications */
2979 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
2982 yaffs_init_tags(&new_tags);
2984 new_tags.chunk_id = 0;
2985 new_tags.obj_id = in->obj_id;
2986 new_tags.serial_number = in->serial;
2988 /* Add extra info for file header */
2990 new_tags.extra_available = 1;
2991 new_tags.extra_parent_id = oh->parent_obj_id;
2992 new_tags.extra_length = oh->file_size;
2993 new_tags.extra_is_shrink = oh->is_shrink;
2994 new_tags.extra_equiv_id = oh->equiv_id;
2995 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
2996 new_tags.extra_obj_type = in->variant_type;
2998 yaffs_verify_oh(in, oh, &new_tags, 1);
3000 /* Create new chunk in NAND */
3002 yaffs_write_new_chunk(dev, buffer, &new_tags,
3003 (prev_chunk_id > 0) ? 1 : 0);
3005 if (new_chunk_id >= 0) {
3007 in->hdr_chunk = new_chunk_id;
3009 if (prev_chunk_id > 0) {
3010 yaffs_chunk_del(dev, prev_chunk_id, 1,
3014 if (!yaffs_obj_cache_dirty(in))
3017 /* If this was a shrink, then mark the block that the chunk lives on */
3019 bi = yaffs_get_block_info(in->my_dev,
3023 bi->has_shrink_hdr = 1;
3028 ret_val = new_chunk_id;
3033 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3038 /*------------------------ Short Operations Cache ----------------------------------------
3039 * In many situations where there is no high level buffering a lot of
3040 * reads might be short sequential reads, and a lot of writes may be short
3041 * sequential writes. eg. scanning/writing a jpeg file.
3042 * In these cases, a short read/write cache can provide a huge perfomance
3043 * benefit with dumb-as-a-rock code.
3044 * In Linux, the page cache provides read buffering and the short op cache
3045 * provides write buffering.
3047 * There are a limited number (~10) of cache chunks per device so that we don't
3048 * need a very intelligent search.
3051 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
3053 struct yaffs_dev *dev = obj->my_dev;
3055 struct yaffs_cache *cache;
3056 int n_caches = obj->my_dev->param.n_caches;
3058 for (i = 0; i < n_caches; i++) {
3059 cache = &dev->cache[i];
3060 if (cache->object == obj && cache->dirty)
3067 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
3069 struct yaffs_dev *dev = obj->my_dev;
3070 int lowest = -99; /* Stop compiler whining. */
3072 struct yaffs_cache *cache;
3073 int chunk_written = 0;
3074 int n_caches = obj->my_dev->param.n_caches;
3080 /* Find the dirty cache for this object with the lowest chunk id. */
3081 for (i = 0; i < n_caches; i++) {
3082 if (dev->cache[i].object == obj &&
3083 dev->cache[i].dirty) {
3085 || dev->cache[i].chunk_id <
3087 cache = &dev->cache[i];
3088 lowest = cache->chunk_id;
3093 if (cache && !cache->locked) {
3094 /* Write it out and free it up */
3097 yaffs_wr_data_obj(cache->object,
3102 cache->object = NULL;
3105 } while (cache && chunk_written > 0);
3108 /* Hoosterman, disk full while writing cache out. */
3109 T(YAFFS_TRACE_ERROR,
3111 ("yaffs tragedy: no space during cache write"
3119 /*yaffs_flush_whole_cache(dev)
3124 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
3126 struct yaffs_obj *obj;
3127 int n_caches = dev->param.n_caches;
3130 /* Find a dirty object in the cache and flush it...
3131 * until there are no further dirty objects.
3135 for (i = 0; i < n_caches && !obj; i++) {
3136 if (dev->cache[i].object && dev->cache[i].dirty)
3137 obj = dev->cache[i].object;
3141 yaffs_flush_file_cache(obj);
3147 /* Grab us a cache chunk for use.
3148 * First look for an empty one.
3149 * Then look for the least recently used non-dirty one.
3150 * Then look for the least recently used dirty one...., flush and look again.
3152 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
3156 if (dev->param.n_caches > 0) {
3157 for (i = 0; i < dev->param.n_caches; i++) {
3158 if (!dev->cache[i].object)
3159 return &dev->cache[i];
3166 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
3168 struct yaffs_cache *cache;
3169 struct yaffs_obj *the_obj;
3174 if (dev->param.n_caches > 0) {
3175 /* Try find a non-dirty one... */
3177 cache = yaffs_grab_chunk_worker(dev);
3180 /* They were all dirty, find the last recently used object and flush
3181 * its cache, then find again.
3182 * NB what's here is not very accurate, we actually flush the object
3183 * the last recently used page.
3186 /* With locking we can't assume we can use entry zero */
3193 for (i = 0; i < dev->param.n_caches; i++) {
3194 if (dev->cache[i].object &&
3195 !dev->cache[i].locked &&
3196 (dev->cache[i].last_use < usage
3198 usage = dev->cache[i].last_use;
3199 the_obj = dev->cache[i].object;
3200 cache = &dev->cache[i];
3205 if (!cache || cache->dirty) {
3206 /* Flush and try again */
3207 yaffs_flush_file_cache(the_obj);
3208 cache = yaffs_grab_chunk_worker(dev);
3218 /* Find a cached chunk */
3219 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
3222 struct yaffs_dev *dev = obj->my_dev;
3224 if (dev->param.n_caches > 0) {
3225 for (i = 0; i < dev->param.n_caches; i++) {
3226 if (dev->cache[i].object == obj &&
3227 dev->cache[i].chunk_id == chunk_id) {
3230 return &dev->cache[i];
3237 /* Mark the chunk for the least recently used algorithym */
3238 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
3242 if (dev->param.n_caches > 0) {
3243 if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
3244 /* Reset the cache usages */
3246 for (i = 1; i < dev->param.n_caches; i++)
3247 dev->cache[i].last_use = 0;
3249 dev->cache_last_use = 0;
3252 dev->cache_last_use++;
3254 cache->last_use = dev->cache_last_use;
3261 /* Invalidate a single cache page.
3262 * Do this when a whole page gets written,
3263 * ie the short cache for this page is no longer valid.
3265 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
3267 if (object->my_dev->param.n_caches > 0) {
3268 struct yaffs_cache *cache =
3269 yaffs_find_chunk_cache(object, chunk_id);
3272 cache->object = NULL;
3276 /* Invalidate all the cache pages associated with this object
3277 * Do this whenever ther file is deleted or resized.
3279 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
3282 struct yaffs_dev *dev = in->my_dev;
3284 if (dev->param.n_caches > 0) {
3285 /* Invalidate it. */
3286 for (i = 0; i < dev->param.n_caches; i++) {
3287 if (dev->cache[i].object == in)
3288 dev->cache[i].object = NULL;
3293 /*--------------------- File read/write ------------------------
3294 * Read and write have very similar structures.
3295 * In general the read/write has three parts to it
3296 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3297 * Some complete chunks
3298 * An incomplete chunk to end off with
3300 * Curve-balls: the first chunk might also be the last chunk.
3303 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3311 struct yaffs_cache *cache;
3313 struct yaffs_dev *dev;
3318 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3319 /* start = offset % dev->data_bytes_per_chunk; */
3320 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3323 /* OK now check for the curveball where the start and end are in
3326 if ((start + n) < dev->data_bytes_per_chunk)
3329 n_copy = dev->data_bytes_per_chunk - start;
3331 cache = yaffs_find_chunk_cache(in, chunk);
3333 /* If the chunk is already in the cache or it is less than a whole chunk
3334 * or we're using inband tags then use the cache (if there is caching)
3335 * else bypass the cache.
3337 if (cache || n_copy != dev->data_bytes_per_chunk
3338 || dev->param.inband_tags) {
3339 if (dev->param.n_caches > 0) {
3341 /* If we can't find the data in the cache, then load it up. */
3345 yaffs_grab_chunk_cache(in->my_dev);
3347 cache->chunk_id = chunk;
3350 yaffs_rd_data_obj(in, chunk,
3355 yaffs_use_cache(dev, cache, 0);
3359 memcpy(buffer, &cache->data[start], n_copy);
3363 /* Read into the local buffer then copy.. */
3366 yaffs_get_temp_buffer(dev, __LINE__);
3367 yaffs_rd_data_obj(in, chunk, local_buffer);
3369 memcpy(buffer, &local_buffer[start], n_copy);
3371 yaffs_release_temp_buffer(dev, local_buffer,
3377 /* A full chunk. Read directly into the supplied buffer. */
3378 yaffs_rd_data_obj(in, chunk, buffer);
3392 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3393 int n_bytes, int write_trhrough)
3402 int start_write = offset;
3403 int chunk_written = 0;
3407 struct yaffs_dev *dev;
3411 while (n > 0 && chunk_written >= 0) {
3412 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3414 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3415 start >= dev->data_bytes_per_chunk) {
3416 T(YAFFS_TRACE_ERROR,
3418 ("AddrToChunk of offset %d gives chunk %d start %d"
3419 TENDSTR), (int)offset, chunk, start));
3421 chunk++; /* File pos to chunk in file offset */
3423 /* OK now check for the curveball where the start and end are in
3427 if ((start + n) < dev->data_bytes_per_chunk) {
3430 /* Now folks, to calculate how many bytes to write back....
3431 * If we're overwriting and not writing to then end of file then
3432 * we need to write back as much as was there before.
3435 chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3437 if (chunk_start > in->variant.file_variant.file_size)
3438 n_bytes_read = 0; /* Past end of file */
3441 in->variant.file_variant.file_size -
3444 if (n_bytes_read > dev->data_bytes_per_chunk)
3445 n_bytes_read = dev->data_bytes_per_chunk;
3449 (start + n)) ? n_bytes_read : (start + n);
3452 || n_writeback > dev->data_bytes_per_chunk)
3456 n_copy = dev->data_bytes_per_chunk - start;
3457 n_writeback = dev->data_bytes_per_chunk;
3460 if (n_copy != dev->data_bytes_per_chunk
3461 || dev->param.inband_tags) {
3462 /* An incomplete start or end chunk (or maybe both start and end chunk),
3463 * or we're using inband tags, so we want to use the cache buffers.
3465 if (dev->param.n_caches > 0) {
3466 struct yaffs_cache *cache;
3467 /* If we can't find the data in the cache, then load the cache */
3468 cache = yaffs_find_chunk_cache(in, chunk);
3471 && yaffs_check_alloc_available(dev, 1)) {
3472 cache = yaffs_grab_chunk_cache(dev);
3474 cache->chunk_id = chunk;
3477 yaffs_rd_data_obj(in, chunk,
3481 !yaffs_check_alloc_available(dev,
3483 /* Drop the cache if it was a read cache item and
3484 * no space check has been made for it.
3490 yaffs_use_cache(dev, cache, 1);
3493 memcpy(&cache->data[start], buffer,
3497 cache->n_bytes = n_writeback;
3499 if (write_trhrough) {
3510 chunk_written = -1; /* fail the write */
3513 /* An incomplete start or end chunk (or maybe both start and end chunk)
3514 * Read into the local buffer then copy, then copy over and write back.
3518 yaffs_get_temp_buffer(dev, __LINE__);
3520 yaffs_rd_data_obj(in, chunk, local_buffer);
3522 memcpy(&local_buffer[start], buffer, n_copy);
3525 yaffs_wr_data_obj(in, chunk,
3529 yaffs_release_temp_buffer(dev, local_buffer,
3535 /* A full chunk. Write directly from the supplied buffer. */
3538 yaffs_wr_data_obj(in, chunk, buffer,
3539 dev->data_bytes_per_chunk, 0);
3541 /* Since we've overwritten the cached data, we better invalidate it. */
3542 yaffs_invalidate_chunk_cache(in, chunk);
3545 if (chunk_written >= 0) {
3554 /* Update file object */
3556 if ((start_write + n_done) > in->variant.file_variant.file_size)
3557 in->variant.file_variant.file_size = (start_write + n_done);
3564 int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3565 int n_bytes, int write_trhrough)
3567 yaffs2_handle_hole(in, offset);
3568 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3571 /* ---------------------- File resizing stuff ------------------ */
3573 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3576 struct yaffs_dev *dev = in->my_dev;
3577 int old_size = in->variant.file_variant.file_size;
3579 int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3581 int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3582 dev->data_bytes_per_chunk;
3586 /* Delete backwards so that we don't end up with holes if
3587 * power is lost part-way through the operation.
3589 for (i = last_del; i >= start_del; i--) {
3590 /* NB this could be optimised somewhat,
3591 * eg. could retrieve the tags and write them without
3592 * using yaffs_chunk_del
3595 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3598 (dev->internal_start_block *
3599 dev->param.chunks_per_block)
3601 ((dev->internal_end_block +
3602 1) * dev->param.chunks_per_block)) {
3603 T(YAFFS_TRACE_ALWAYS,
3605 ("Found daft chunk_id %d for %d" TENDSTR),
3608 in->n_data_chunks--;
3609 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3616 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3620 struct yaffs_dev *dev = obj->my_dev;
3622 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3624 yaffs_prune_chunks(obj, new_size);
3626 if (new_partial != 0) {
3627 int last_chunk = 1 + new_full;
3628 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3630 /* Got to read and rewrite the last chunk with its new size and zero pad */
3631 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3632 memset(local_buffer + new_partial, 0,
3633 dev->data_bytes_per_chunk - new_partial);
3635 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3638 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3641 obj->variant.file_variant.file_size = new_size;
3643 yaffs_prune_tree(dev, &obj->variant.file_variant);
3646 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3648 struct yaffs_dev *dev = in->my_dev;
3649 int old_size = in->variant.file_variant.file_size;
3651 yaffs_flush_file_cache(in);
3652 yaffs_invalidate_whole_cache(in);
3654 yaffs_check_gc(dev, 0);
3656 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3659 if (new_size == old_size)
3662 if (new_size > old_size) {
3663 yaffs2_handle_hole(in, new_size);
3664 in->variant.file_variant.file_size = new_size;
3666 /* new_size < old_size */
3667 yaffs_resize_file_down(in, new_size);
3670 /* Write a new object header to reflect the resize.
3671 * show we've shrunk the file, if need be
3672 * Do this only if the file is not in the deleted directories
3673 * and is not shadowed.
3677 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3678 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3679 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3684 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3688 yaffs_flush_file_cache(in);
3689 if (data_sync) /* Only sync data */
3693 yaffs_load_current_time(in, 0, 0);
3695 ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3696 0) ? YAFFS_OK : YAFFS_FAIL;
3706 static int yaffs_generic_obj_del(struct yaffs_obj *in)
3709 /* First off, invalidate the file's data in the cache, without flushing. */
3710 yaffs_invalidate_whole_cache(in);
3712 if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
3713 /* Move to the unlinked directory so we have a record that it was deleted. */
3714 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
3719 yaffs_remove_obj_from_dir(in);
3720 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
3728 /* yaffs_del_file deletes the whole file data
3729 * and the inode associated with the file.
3730 * It does not delete the links associated with the file.
3732 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3737 struct yaffs_dev *dev = in->my_dev;
3744 yaffs_change_obj_name(in, in->my_dev->del_dir,
3745 _Y("deleted"), 0, 0);
3746 T(YAFFS_TRACE_TRACING,
3747 (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
3750 in->my_dev->n_deleted_files++;
3751 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3752 yaffs_resize_file(in, 0);
3753 yaffs_soft_del_file(in);
3756 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3757 _Y("unlinked"), 0, 0);
3763 int yaffs_del_file(struct yaffs_obj *in)
3765 int ret_val = YAFFS_OK;
3766 int deleted; /* Need to cache value on stack if in is freed */
3767 struct yaffs_dev *dev = in->my_dev;
3769 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3770 yaffs_resize_file(in, 0);
3772 if (in->n_data_chunks > 0) {
3773 /* Use soft deletion if there is data in the file.
3774 * That won't be the case if it has been resized to zero.
3777 ret_val = yaffs_unlink_file_if_needed(in);
3779 deleted = in->deleted;
3781 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3784 in->my_dev->n_deleted_files++;
3785 yaffs_soft_del_file(in);
3787 return deleted ? YAFFS_OK : YAFFS_FAIL;
3789 /* The file has no data chunks so we toss it immediately */
3790 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3791 in->variant.file_variant.top = NULL;
3792 yaffs_generic_obj_del(in);
3798 static int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3800 return (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3801 !(list_empty(&obj->variant.dir_variant.children));
3804 static int yaffs_del_dir(struct yaffs_obj *obj)
3806 /* First check that the directory is empty. */
3807 if (yaffs_is_non_empty_dir(obj))
3810 return yaffs_generic_obj_del(obj);
3813 static int yaffs_del_symlink(struct yaffs_obj *in)
3815 if (in->variant.symlink_variant.alias)
3816 YFREE(in->variant.symlink_variant.alias);
3817 in->variant.symlink_variant.alias = NULL;
3819 return yaffs_generic_obj_del(in);
3822 static int yaffs_del_link(struct yaffs_obj *in)
3824 /* remove this hardlink from the list assocaited with the equivalent
3827 list_del_init(&in->hard_links);
3828 return yaffs_generic_obj_del(in);
3831 int yaffs_del_obj(struct yaffs_obj *obj)
3834 switch (obj->variant_type) {
3835 case YAFFS_OBJECT_TYPE_FILE:
3836 ret_val = yaffs_del_file(obj);
3838 case YAFFS_OBJECT_TYPE_DIRECTORY:
3839 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3840 T(YAFFS_TRACE_BACKGROUND,
3842 ("Remove object %d from dirty directories" TENDSTR),
3844 list_del_init(&obj->variant.dir_variant.dirty);
3846 return yaffs_del_dir(obj);
3848 case YAFFS_OBJECT_TYPE_SYMLINK:
3849 ret_val = yaffs_del_symlink(obj);
3851 case YAFFS_OBJECT_TYPE_HARDLINK:
3852 ret_val = yaffs_del_link(obj);
3854 case YAFFS_OBJECT_TYPE_SPECIAL:
3855 ret_val = yaffs_generic_obj_del(obj);
3857 case YAFFS_OBJECT_TYPE_UNKNOWN:
3859 break; /* should not happen. */
3865 static int yaffs_unlink_worker(struct yaffs_obj *obj)
3874 yaffs_update_parent(obj->parent);
3876 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
3877 return yaffs_del_link(obj);
3878 } else if (!list_empty(&obj->hard_links)) {
3879 /* Curve ball: We're unlinking an object that has a hardlink.
3881 * This problem arises because we are not strictly following
3882 * The Linux link/inode model.
3884 * We can't really delete the object.
3885 * Instead, we do the following:
3886 * - Select a hardlink.
3887 * - Unhook it from the hard links
3888 * - Move it from its parent directory (so that the rename can work)
3889 * - Rename the object to the hardlink's name.
3890 * - Delete the hardlink
3893 struct yaffs_obj *hl;
3894 struct yaffs_obj *parent;
3896 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
3898 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3901 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3902 parent = hl->parent;
3904 list_del_init(&hl->hard_links);
3906 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
3908 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
3910 if (ret_val == YAFFS_OK)
3911 ret_val = yaffs_generic_obj_del(hl);
3915 } else if (del_now) {
3916 switch (obj->variant_type) {
3917 case YAFFS_OBJECT_TYPE_FILE:
3918 return yaffs_del_file(obj);
3920 case YAFFS_OBJECT_TYPE_DIRECTORY:
3921 list_del_init(&obj->variant.dir_variant.dirty);
3922 return yaffs_del_dir(obj);
3924 case YAFFS_OBJECT_TYPE_SYMLINK:
3925 return yaffs_del_symlink(obj);
3927 case YAFFS_OBJECT_TYPE_SPECIAL:
3928 return yaffs_generic_obj_del(obj);
3930 case YAFFS_OBJECT_TYPE_HARDLINK:
3931 case YAFFS_OBJECT_TYPE_UNKNOWN:
3935 } else if (yaffs_is_non_empty_dir(obj))
3938 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
3939 _Y("unlinked"), 0, 0);
3942 static int yaffs_unlink_obj(struct yaffs_obj *obj)
3945 if (obj && obj->unlink_allowed)
3946 return yaffs_unlink_worker(obj);
3952 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
3954 struct yaffs_obj *obj;
3956 obj = yaffs_find_by_name(dir, name);
3957 return yaffs_unlink_obj(obj);
3960 /*----------------------- Initialisation Scanning ---------------------- */
3962 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
3963 int backward_scanning)
3965 struct yaffs_obj *obj;
3967 if (!backward_scanning) {
3968 /* Handle YAFFS1 forward scanning case
3969 * For YAFFS1 we always do the deletion
3973 /* Handle YAFFS2 case (backward scanning)
3974 * If the shadowed object exists then ignore.
3976 obj = yaffs_find_by_number(dev, obj_id);
3981 /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
3982 * We put it in unlinked dir to be cleaned up after the scanning
3985 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
3988 obj->is_shadowed = 1;
3989 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
3990 obj->variant.file_variant.shrink_size = 0;
3991 obj->valid = 1; /* So that we don't read any other info for this file */
3995 void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
3997 struct yaffs_obj *hl;
3998 struct yaffs_obj *in;
4002 hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
4004 in = yaffs_find_by_number(dev,
4006 hardlink_variant.equiv_id);
4009 /* Add the hardlink pointers */
4010 hl->variant.hardlink_variant.equiv_obj = in;
4011 list_add(&hl->hard_links, &in->hard_links);
4013 /* Todo Need to report/handle this better.
4014 * Got a problem... hardlink to a non-existant object
4016 hl->variant.hardlink_variant.equiv_obj = NULL;
4017 INIT_LIST_HEAD(&hl->hard_links);
4023 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4026 * Sort out state of unlinked and deleted objects after scanning.
4028 struct list_head *i;
4029 struct list_head *n;
4030 struct yaffs_obj *l;
4035 /* Soft delete all the unlinked files */
4036 list_for_each_safe(i, n,
4037 &dev->unlinked_dir->variant.dir_variant.children) {
4039 l = list_entry(i, struct yaffs_obj, siblings);
4044 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4046 l = list_entry(i, struct yaffs_obj, siblings);
4054 * This code iterates through all the objects making sure that they are rooted.
4055 * Any unrooted objects are re-rooted in lost+found.
4056 * An object needs to be in one of:
4057 * - Directly under deleted, unlinked
4058 * - Directly or indirectly under root.
4061 * This code assumes that we don't ever change the current relationships between
4063 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4064 * lost-n-found->parent == root_dir
4066 * This fixes the problem where directories might have inadvertently been deleted
4067 * leaving the object "hanging" without being rooted in the directory tree.
4070 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4072 return (obj == dev->del_dir ||
4073 obj == dev->unlinked_dir || obj == dev->root_dir);
4076 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4078 struct yaffs_obj *obj;
4079 struct yaffs_obj *parent;
4081 struct list_head *lh;
4082 struct list_head *n;
4089 /* Iterate through the objects in each hash entry,
4090 * looking at each object.
4091 * Make sure it is rooted.
4094 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4095 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4098 list_entry(lh, struct yaffs_obj, hash_link);
4099 parent = obj->parent;
4101 if (yaffs_has_null_parent(dev, obj)) {
4102 /* These directories are not hanging */
4105 || parent->variant_type !=
4106 YAFFS_OBJECT_TYPE_DIRECTORY)
4108 else if (yaffs_has_null_parent(dev, parent))
4112 * Need to follow the parent chain to see if it is hanging.
4117 while (parent != dev->root_dir &&
4119 parent->parent->variant_type ==
4120 YAFFS_OBJECT_TYPE_DIRECTORY
4121 && depth_limit > 0) {
4122 parent = parent->parent;
4125 if (parent != dev->root_dir)
4131 ("Hanging object %d moved to lost and found"
4132 TENDSTR), obj->obj_id));
4133 yaffs_add_obj_to_dir(dev->lost_n_found,
4142 * Delete directory contents for cleaning up lost and found.
4144 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4146 struct yaffs_obj *obj;
4147 struct list_head *lh;
4148 struct list_head *n;
4150 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4153 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4155 obj = list_entry(lh, struct yaffs_obj, siblings);
4156 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4157 yaffs_del_dir_contents(obj);
4160 (TSTR("Deleting lost_found object %d" TENDSTR),
4163 /* Need to use UnlinkObject since Delete would not handle
4164 * hardlinked objects correctly.
4166 yaffs_unlink_obj(obj);
4172 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4174 yaffs_del_dir_contents(dev->lost_n_found);
4177 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
4180 struct yaffs_obj_hdr *oh;
4181 struct yaffs_dev *dev;
4182 struct yaffs_ext_tags tags;
4184 int alloc_failed = 0;
4191 if (in->lazy_loaded && in->hdr_chunk > 0) {
4192 in->lazy_loaded = 0;
4193 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
4196 yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
4198 oh = (struct yaffs_obj_hdr *)chunk_data;
4200 in->yst_mode = oh->yst_mode;
4201 yaffs_load_attribs(in, oh);
4202 yaffs_set_obj_name_from_oh(in, oh);
4204 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4205 in->variant.symlink_variant.alias =
4206 yaffs_clone_str(oh->alias);
4207 if (!in->variant.symlink_variant.alias)
4208 alloc_failed = 1; /* Not returned to caller */
4211 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
4215 /*------------------------------ Directory Functions ----------------------------- */
4218 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
4219 * link (ie. name) is created or deleted in the directory.
4222 * create dir/a : update dir's mtime/ctime
4223 * rm dir/a: update dir's mtime/ctime
4224 * modify dir/a: don't update dir's mtimme/ctime
4226 * This can be handled immediately or defered. Defering helps reduce the number
4227 * of updates when many files in a directory are changed within a brief period.
4229 * If the directory updating is defered then yaffs_update_dirty_dirs must be
4230 * called periodically.
4233 static void yaffs_update_parent(struct yaffs_obj *obj)
4235 struct yaffs_dev *dev;
4240 yaffs_load_current_time(obj, 0, 1);
4241 if (dev->param.defered_dir_update) {
4242 struct list_head *link = &obj->variant.dir_variant.dirty;
4244 if (list_empty(link)) {
4245 list_add(link, &dev->dirty_dirs);
4246 T(YAFFS_TRACE_BACKGROUND,
4247 (TSTR("Added object %d to dirty directories" TENDSTR),
4252 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
4255 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
4257 struct list_head *link;
4258 struct yaffs_obj *obj;
4259 struct yaffs_dir_var *d_s;
4260 union yaffs_obj_var *o_v;
4262 T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
4264 while (!list_empty(&dev->dirty_dirs)) {
4265 link = dev->dirty_dirs.next;
4266 list_del_init(link);
4268 d_s = list_entry(link, struct yaffs_dir_var, dirty);
4269 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
4270 obj = list_entry(o_v, struct yaffs_obj, variant);
4272 T(YAFFS_TRACE_BACKGROUND,
4273 (TSTR("Update directory %d" TENDSTR), obj->obj_id));
4276 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
4280 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
4282 struct yaffs_dev *dev = obj->my_dev;
4283 struct yaffs_obj *parent;
4285 yaffs_verify_obj_in_dir(obj);
4286 parent = obj->parent;
4288 yaffs_verify_dir(parent);
4290 if (dev && dev->param.remove_obj_fn)
4291 dev->param.remove_obj_fn(obj);
4293 list_del_init(&obj->siblings);
4296 yaffs_verify_dir(parent);
4299 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
4302 T(YAFFS_TRACE_ALWAYS,
4304 ("tragedy: Trying to add an object to a null pointer directory"
4309 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4310 T(YAFFS_TRACE_ALWAYS,
4312 ("tragedy: Trying to add an object to a non-directory"
4317 if (obj->siblings.prev == NULL) {
4318 /* Not initialised */
4322 yaffs_verify_dir(directory);
4324 yaffs_remove_obj_from_dir(obj);
4327 list_add(&obj->siblings, &directory->variant.dir_variant.children);
4328 obj->parent = directory;
4330 if (directory == obj->my_dev->unlinked_dir
4331 || directory == obj->my_dev->del_dir) {
4333 obj->my_dev->n_unlinked_files++;
4334 obj->rename_allowed = 0;
4337 yaffs_verify_dir(directory);
4338 yaffs_verify_obj_in_dir(obj);
4341 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4346 struct list_head *i;
4347 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4349 struct yaffs_obj *l;
4355 T(YAFFS_TRACE_ALWAYS,
4357 ("tragedy: yaffs_find_by_name: null pointer directory"
4362 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4363 T(YAFFS_TRACE_ALWAYS,
4364 (TSTR("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
4368 sum = yaffs_calc_name_sum(name);
4370 list_for_each(i, &directory->variant.dir_variant.children) {
4372 l = list_entry(i, struct yaffs_obj, siblings);
4374 if (l->parent != directory)
4377 yaffs_check_obj_details_loaded(l);
4379 /* Special case for lost-n-found */
4380 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4381 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) ==
4384 } else if (yaffs_sum_cmp(l->sum, sum)
4385 || l->hdr_chunk <= 0) {
4386 /* LostnFound chunk called Objxxx
4389 yaffs_get_obj_name(l, buffer,
4390 YAFFS_MAX_NAME_LENGTH + 1);
4392 (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4401 /* GetEquivalentObject dereferences any hard links to get to the
4405 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4407 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4408 /* We want the object id of the equivalent object, not this one */
4409 obj = obj->variant.hardlink_variant.equiv_obj;
4410 yaffs_check_obj_details_loaded(obj);
4416 * A note or two on object names.
4417 * * If the object name is missing, we then make one up in the form objnnn
4419 * * ASCII names are stored in the object header's name field from byte zero
4420 * * Unicode names are historically stored starting from byte zero.
4422 * Then there are automatic Unicode names...
4423 * The purpose of these is to save names in a way that can be read as
4424 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4425 * system to share files.
4427 * These automatic unicode are stored slightly differently...
4428 * - If the name can fit in the ASCII character space then they are saved as
4429 * ascii names as per above.
4430 * - If the name needs Unicode then the name is saved in Unicode
4431 * starting at oh->name[1].
4434 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
4437 /* Create an object name if we could not find one. */
4438 if (yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4439 YCHAR local_name[20];
4440 YCHAR num_string[20];
4441 YCHAR *x = &num_string[19];
4442 unsigned v = obj->obj_id;
4446 *x = '0' + (v % 10);
4449 /* make up a name */
4450 yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4451 yaffs_strcat(local_name, x);
4452 yaffs_strncpy(name, local_name, buffer_size - 1);
4456 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
4457 const YCHAR * oh_name, int buff_size)
4459 #ifdef CONFIG_YAFFS_AUTO_UNICODE
4460 if (dev->param.auto_unicode) {
4462 /* It is an ASCII name, so do an ASCII to unicode conversion */
4463 const char *ascii_oh_name = (const char *)oh_name;
4464 int n = buff_size - 1;
4465 while (n > 0 && *ascii_oh_name) {
4466 *name = *ascii_oh_name;
4472 yaffs_strncpy(name, oh_name + 1, buff_size - 1);
4475 yaffs_strncpy(name, oh_name, buff_size - 1);
4478 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
4481 #ifdef CONFIG_YAFFS_AUTO_UNICODE
4486 if (dev->param.auto_unicode) {
4491 /* Figure out if the name will fit in ascii character set */
4492 while (is_ascii && *w) {
4499 /* It is an ASCII name, so do a unicode to ascii conversion */
4500 char *ascii_oh_name = (char *)oh_name;
4501 int n = YAFFS_MAX_NAME_LENGTH - 1;
4502 while (n > 0 && *name) {
4503 *ascii_oh_name = *name;
4509 /* It is a unicode name, so save starting at the second YCHAR */
4511 yaffs_strncpy(oh_name + 1, name,
4512 YAFFS_MAX_NAME_LENGTH - 2);
4516 yaffs_strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
4520 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
4522 memset(name, 0, buffer_size * sizeof(YCHAR));
4524 yaffs_check_obj_details_loaded(obj);
4526 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4527 yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4529 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4530 else if (obj->short_name[0]) {
4531 yaffs_strcpy(name, obj->short_name);
4534 else if (obj->hdr_chunk > 0) {
4536 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4538 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4540 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4542 if (obj->hdr_chunk > 0) {
4543 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4547 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4550 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4553 yaffs_fix_null_name(obj, name, buffer_size);
4555 return yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH);
4558 int yaffs_get_obj_length(struct yaffs_obj *obj)
4560 /* Dereference any hard linking */
4561 obj = yaffs_get_equivalent_obj(obj);
4563 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4564 return obj->variant.file_variant.file_size;
4565 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4566 if (!obj->variant.symlink_variant.alias)
4568 return yaffs_strnlen(obj->variant.symlink_variant.alias,
4569 YAFFS_MAX_ALIAS_LENGTH);
4571 /* Only a directory should drop through to here */
4572 return obj->my_dev->data_bytes_per_chunk;
4576 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4579 struct list_head *i;
4582 count++; /* the object itself */
4584 list_for_each(i, &obj->hard_links)
4585 count++; /* add the hard links; */
4590 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4592 obj = yaffs_get_equivalent_obj(obj);
4597 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4599 obj = yaffs_get_equivalent_obj(obj);
4601 switch (obj->variant_type) {
4602 case YAFFS_OBJECT_TYPE_FILE:
4605 case YAFFS_OBJECT_TYPE_DIRECTORY:
4608 case YAFFS_OBJECT_TYPE_SYMLINK:
4611 case YAFFS_OBJECT_TYPE_HARDLINK:
4614 case YAFFS_OBJECT_TYPE_SPECIAL:
4615 if (S_ISFIFO(obj->yst_mode))
4617 if (S_ISCHR(obj->yst_mode))
4619 if (S_ISBLK(obj->yst_mode))
4621 if (S_ISSOCK(obj->yst_mode))
4629 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4631 obj = yaffs_get_equivalent_obj(obj);
4632 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4633 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4635 return yaffs_clone_str(_Y(""));
4638 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
4639 const YCHAR * name, const void *value, int size,
4642 struct yaffs_xattr_mod xmod;
4651 xmod.result = -ENOSPC;
4653 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
4661 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
4662 struct yaffs_xattr_mod *xmod)
4665 int x_offs = sizeof(struct yaffs_obj_hdr);
4666 struct yaffs_dev *dev = obj->my_dev;
4667 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
4669 char *x_buffer = buffer + x_offs;
4673 nval_set(x_buffer, x_size, xmod->name, xmod->data,
4674 xmod->size, xmod->flags);
4676 retval = nval_del(x_buffer, x_size, xmod->name);
4678 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4679 obj->xattr_known = 1;
4681 xmod->result = retval;
4686 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
4687 void *value, int size)
4689 char *buffer = NULL;
4691 struct yaffs_ext_tags tags;
4692 struct yaffs_dev *dev = obj->my_dev;
4693 int x_offs = sizeof(struct yaffs_obj_hdr);
4694 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
4700 if (obj->hdr_chunk < 1)
4703 /* If we know that the object has no xattribs then don't do all the
4704 * reading and parsing.
4706 if (obj->xattr_known && !obj->has_xattr) {
4713 buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
4718 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
4720 if (result != YAFFS_OK)
4723 x_buffer = buffer + x_offs;
4725 if (!obj->xattr_known) {
4726 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4727 obj->xattr_known = 1;
4731 retval = nval_get(x_buffer, x_size, name, value, size);
4733 retval = nval_list(x_buffer, x_size, value, size);
4735 yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
4739 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
4740 const void *value, int size, int flags)
4742 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
4745 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
4747 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
4750 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
4753 return yaffs_do_xattrib_fetch(obj, name, value, size);
4756 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
4758 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
4761 /*---------------------------- Initialisation code -------------------------------------- */
4763 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4766 /* Common functions, gotta have */
4767 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4770 #ifdef CONFIG_YAFFS_YAFFS2
4772 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4773 if (dev->param.write_chunk_tags_fn &&
4774 dev->param.read_chunk_tags_fn &&
4775 !dev->param.write_chunk_fn &&
4776 !dev->param.read_chunk_fn &&
4777 dev->param.bad_block_fn && dev->param.query_block_fn)
4781 /* Can use the "spare" style interface for yaffs1 */
4782 if (!dev->param.is_yaffs2 &&
4783 !dev->param.write_chunk_tags_fn &&
4784 !dev->param.read_chunk_tags_fn &&
4785 dev->param.write_chunk_fn &&
4786 dev->param.read_chunk_fn &&
4787 !dev->param.bad_block_fn && !dev->param.query_block_fn)
4793 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4795 /* Initialise the unlinked, deleted, root and lost and found directories */
4797 dev->lost_n_found = dev->root_dir = NULL;
4798 dev->unlinked_dir = dev->del_dir = NULL;
4801 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4804 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4807 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4808 YAFFS_ROOT_MODE | S_IFDIR);
4810 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4811 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4813 if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4815 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4822 int yaffs_guts_initialise(struct yaffs_dev *dev)
4824 int init_failed = 0;
4828 T(YAFFS_TRACE_TRACING,
4829 (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR)));
4831 /* Check stuff that must be set */
4834 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
4838 dev->internal_start_block = dev->param.start_block;
4839 dev->internal_end_block = dev->param.end_block;
4840 dev->block_offset = 0;
4841 dev->chunk_offset = 0;
4842 dev->n_free_chunks = 0;
4846 if (dev->param.start_block == 0) {
4847 dev->internal_start_block = dev->param.start_block + 1;
4848 dev->internal_end_block = dev->param.end_block + 1;
4849 dev->block_offset = 1;
4850 dev->chunk_offset = dev->param.chunks_per_block;
4853 /* Check geometry parameters. */
4855 if ((!dev->param.inband_tags && dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 1024) || (!dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 512) || (dev->param.inband_tags && !dev->param.is_yaffs2) || dev->param.chunks_per_block < 2 || dev->param.n_reserved_blocks < 2 || dev->internal_start_block <= 0 || dev->internal_end_block <= 0 || dev->internal_end_block <= (dev->internal_start_block + dev->param.n_reserved_blocks + 2)) { /* otherwise it is too small */
4856 T(YAFFS_TRACE_ALWAYS,
4858 ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d "
4859 TENDSTR), dev->param.total_bytes_per_chunk,
4860 dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags));
4864 if (yaffs_init_nand(dev) != YAFFS_OK) {
4865 T(YAFFS_TRACE_ALWAYS,
4866 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
4870 /* Sort out space for inband tags, if required */
4871 if (dev->param.inband_tags)
4872 dev->data_bytes_per_chunk =
4873 dev->param.total_bytes_per_chunk -
4874 sizeof(struct yaffs_packed_tags2_tags_only);
4876 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4878 /* Got the right mix of functions? */
4879 if (!yaffs_check_dev_fns(dev)) {
4880 /* Function missing */
4881 T(YAFFS_TRACE_ALWAYS,
4883 ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
4888 if (dev->is_mounted) {
4889 T(YAFFS_TRACE_ALWAYS,
4890 (TSTR("yaffs: device already mounted\n" TENDSTR)));
4894 /* Finished with most checks. One or two more checks happen later on too. */
4896 dev->is_mounted = 1;
4898 /* OK now calculate a few things for the device */
4901 * Calculate all the chunk size manipulation numbers:
4903 x = dev->data_bytes_per_chunk;
4904 /* We always use dev->chunk_shift and dev->chunk_div */
4905 dev->chunk_shift = calc_shifts(x);
4906 x >>= dev->chunk_shift;
4908 /* We only use chunk mask if chunk_div is 1 */
4909 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4912 * Calculate chunk_grp_bits.
4913 * We need to find the next power of 2 > than internal_end_block
4916 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4918 bits = calc_shifts_ceiling(x);
4920 /* Set up tnode width if wide tnodes are enabled. */
4921 if (!dev->param.wide_tnodes_disabled) {
4922 /* bits must be even so that we end up with 32-bit words */
4926 dev->tnode_width = 16;
4928 dev->tnode_width = bits;
4930 dev->tnode_width = 16;
4932 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4934 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4935 * so if the bitwidth of the
4936 * chunk range we're using is greater than 16 we need
4937 * to figure out chunk shift and chunk_grp_size
4940 if (bits <= dev->tnode_width)
4941 dev->chunk_grp_bits = 0;
4943 dev->chunk_grp_bits = bits - dev->tnode_width;
4945 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4946 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4947 dev->tnode_size = sizeof(struct yaffs_tnode);
4949 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4951 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4952 /* We have a problem because the soft delete won't work if
4953 * the chunk group size > chunks per block.
4954 * This can be remedied by using larger "virtual blocks".
4956 T(YAFFS_TRACE_ALWAYS,
4957 (TSTR("yaffs: chunk group too large\n" TENDSTR)));
4962 /* OK, we've finished verifying the device, lets continue with initialisation */
4964 /* More device initialisation */
4966 dev->passive_gc_count = 0;
4967 dev->oldest_dirty_gc_count = 0;
4969 dev->gc_block_finder = 0;
4970 dev->buffered_block = -1;
4971 dev->doing_buffered_block_rewrite = 0;
4972 dev->n_deleted_files = 0;
4973 dev->n_bg_deletions = 0;
4974 dev->n_unlinked_files = 0;
4975 dev->n_ecc_fixed = 0;
4976 dev->n_ecc_unfixed = 0;
4977 dev->n_tags_ecc_fixed = 0;
4978 dev->n_tags_ecc_unfixed = 0;
4979 dev->n_erase_failures = 0;
4980 dev->n_erased_blocks = 0;
4981 dev->gc_disable = 0;
4982 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
4983 INIT_LIST_HEAD(&dev->dirty_dirs);
4984 dev->oldest_dirty_seq = 0;
4985 dev->oldest_dirty_block = 0;
4987 /* Initialise temporary buffers and caches. */
4988 if (!yaffs_init_tmp_buffers(dev))
4992 dev->gc_cleanup_list = NULL;
4994 if (!init_failed && dev->param.n_caches > 0) {
4998 dev->param.n_caches * sizeof(struct yaffs_cache);
5000 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
5001 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
5003 dev->cache = YMALLOC(cache_bytes);
5005 buf = (u8 *) dev->cache;
5008 memset(dev->cache, 0, cache_bytes);
5010 for (i = 0; i < dev->param.n_caches && buf; i++) {
5011 dev->cache[i].object = NULL;
5012 dev->cache[i].last_use = 0;
5013 dev->cache[i].dirty = 0;
5014 dev->cache[i].data = buf =
5015 YMALLOC_DMA(dev->param.total_bytes_per_chunk);
5020 dev->cache_last_use = 0;
5023 dev->cache_hits = 0;
5026 dev->gc_cleanup_list =
5027 YMALLOC(dev->param.chunks_per_block * sizeof(u32));
5028 if (!dev->gc_cleanup_list)
5032 if (dev->param.is_yaffs2)
5033 dev->param.use_header_file_size = 1;
5035 if (!init_failed && !yaffs_init_blocks(dev))
5038 yaffs_init_tnodes_and_objs(dev);
5040 if (!init_failed && !yaffs_create_initial_dir(dev))
5044 /* Now scan the flash. */
5045 if (dev->param.is_yaffs2) {
5046 if (yaffs2_checkpt_restore(dev)) {
5047 yaffs_check_obj_details_loaded(dev->root_dir);
5048 T(YAFFS_TRACE_ALWAYS,
5050 ("yaffs: restored from checkpoint"
5054 /* Clean up the mess caused by an aborted checkpoint load
5055 * and scan backwards.
5057 yaffs_deinit_blocks(dev);
5059 yaffs_deinit_tnodes_and_objs(dev);
5061 dev->n_erased_blocks = 0;
5062 dev->n_free_chunks = 0;
5063 dev->alloc_block = -1;
5064 dev->alloc_page = -1;
5065 dev->n_deleted_files = 0;
5066 dev->n_unlinked_files = 0;
5067 dev->n_bg_deletions = 0;
5069 if (!init_failed && !yaffs_init_blocks(dev))
5072 yaffs_init_tnodes_and_objs(dev);
5075 && !yaffs_create_initial_dir(dev))
5078 if (!init_failed && !yaffs2_scan_backwards(dev))
5081 } else if (!yaffs1_scan(dev))
5084 yaffs_strip_deleted_objs(dev);
5085 yaffs_fix_hanging_objs(dev);
5086 if (dev->param.empty_lost_n_found)
5087 yaffs_empty_l_n_f(dev);
5091 /* Clean up the mess */
5092 T(YAFFS_TRACE_TRACING,
5093 (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR)));
5095 yaffs_deinitialise(dev);
5099 /* Zero out stats */
5100 dev->n_page_reads = 0;
5101 dev->n_page_writes = 0;
5102 dev->n_erasures = 0;
5103 dev->n_gc_copies = 0;
5104 dev->n_retired_writes = 0;
5106 dev->n_retired_blocks = 0;
5108 yaffs_verify_free_chunks(dev);
5109 yaffs_verify_blocks(dev);
5111 /* Clean up any aborted checkpoint data */
5112 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5113 yaffs2_checkpt_invalidate(dev);
5115 T(YAFFS_TRACE_TRACING,
5116 (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR)));
5121 void yaffs_deinitialise(struct yaffs_dev *dev)
5123 if (dev->is_mounted) {
5126 yaffs_deinit_blocks(dev);
5127 yaffs_deinit_tnodes_and_objs(dev);
5128 if (dev->param.n_caches > 0 && dev->cache) {
5130 for (i = 0; i < dev->param.n_caches; i++) {
5131 if (dev->cache[i].data)
5132 YFREE(dev->cache[i].data);
5133 dev->cache[i].data = NULL;
5140 YFREE(dev->gc_cleanup_list);
5142 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5143 YFREE(dev->temp_buffer[i].buffer);
5145 dev->is_mounted = 0;
5147 if (dev->param.deinitialise_flash_fn)
5148 dev->param.deinitialise_flash_fn(dev);
5152 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5157 struct yaffs_block_info *blk;
5159 blk = dev->block_info;
5160 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5161 switch (blk->block_state) {
5162 case YAFFS_BLOCK_STATE_EMPTY:
5163 case YAFFS_BLOCK_STATE_ALLOCATING:
5164 case YAFFS_BLOCK_STATE_COLLECTING:
5165 case YAFFS_BLOCK_STATE_FULL:
5167 (dev->param.chunks_per_block - blk->pages_in_use +
5168 blk->soft_del_pages);
5179 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5181 /* This is what we report to the outside world */
5185 int blocks_for_checkpt;
5188 n_free = dev->n_free_chunks;
5189 n_free += dev->n_deleted_files;
5191 /* Now count the number of dirty chunks in the cache and subtract those */
5193 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5194 if (dev->cache[i].dirty)
5198 n_free -= n_dirty_caches;
5201 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5203 /* Now we figure out how much to reserve for the checkpoint and report that... */
5204 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5206 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);