2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include "yaffs_trace.h"
16 #include "yaffs_guts.h"
17 #include "yaffs_endian.h"
18 #include "yaffs_getblockinfo.h"
19 #include "yaffs_tagscompat.h"
20 #include "yaffs_tagsmarshall.h"
21 #include "yaffs_nand.h"
22 #include "yaffs_yaffs1.h"
23 #include "yaffs_yaffs2.h"
24 #include "yaffs_bitmap.h"
25 #include "yaffs_verify.h"
26 #include "yaffs_nand.h"
27 #include "yaffs_packedtags2.h"
28 #include "yaffs_nameval.h"
29 #include "yaffs_allocator.h"
30 #include "yaffs_attribs.h"
31 #include "yaffs_summary.h"
33 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
34 #define YAFFS_GC_GOOD_ENOUGH 2
35 #define YAFFS_GC_PASSIVE_THRESHOLD 4
37 #include "yaffs_ecc.h"
39 /* Forward declarations */
41 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
42 const u8 *buffer, int n_bytes, int use_reserve);
44 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
47 /* Function to calculate chunk and offset */
49 void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
50 int *chunk_out, u32 *offset_out)
55 chunk = (u32) (addr >> dev->chunk_shift);
57 if (dev->chunk_div == 1) {
58 /* easy power of 2 case */
59 offset = (u32) (addr & dev->chunk_mask);
61 /* Non power-of-2 case */
65 chunk /= dev->chunk_div;
67 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
68 offset = (u32) (addr - chunk_base);
75 /* Function to return the number of shifts for a power of 2 greater than or
76 * equal to the given number
77 * Note we don't try to cater for all possible numbers and this does not have to
78 * be hellishly efficient.
81 static inline u32 calc_shifts_ceiling(u32 x)
86 shifts = extra_bits = 0;
101 /* Function to return the number of shifts to get a 1 in bit 0
104 static inline u32 calc_shifts(u32 x)
122 * Temporary buffer manipulations.
125 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
130 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
132 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
133 dev->temp_buffer[i].in_use = 0;
134 buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
135 dev->temp_buffer[i].buffer = buf;
138 return buf ? YAFFS_OK : YAFFS_FAIL;
141 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
146 if (dev->temp_in_use > dev->max_temp)
147 dev->max_temp = dev->temp_in_use;
149 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
150 if (dev->temp_buffer[i].in_use == 0) {
151 dev->temp_buffer[i].in_use = 1;
152 return dev->temp_buffer[i].buffer;
156 yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
158 * If we got here then we have to allocate an unmanaged one
162 dev->unmanaged_buffer_allocs++;
163 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
167 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
173 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
174 if (dev->temp_buffer[i].buffer == buffer) {
175 dev->temp_buffer[i].in_use = 0;
181 /* assume it is an unmanaged one. */
182 yaffs_trace(YAFFS_TRACE_BUFFERS,
183 "Releasing unmanaged temp buffer");
185 dev->unmanaged_buffer_deallocs++;
191 * Functions for robustisizing TODO
195 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
197 const struct yaffs_ext_tags *tags)
205 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
206 const struct yaffs_ext_tags *tags)
213 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
214 struct yaffs_block_info *bi)
216 if (!bi->gc_prioritise) {
217 bi->gc_prioritise = 1;
218 dev->has_pending_prioritised_gc = 1;
219 bi->chunk_error_strikes++;
221 if (bi->chunk_error_strikes > 3) {
222 bi->needs_retiring = 1; /* Too many stikes, so retire */
223 yaffs_trace(YAFFS_TRACE_ALWAYS,
224 "yaffs: Block struck out");
230 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
233 int flash_block = nand_chunk / dev->param.chunks_per_block;
234 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
236 yaffs_handle_chunk_error(dev, bi);
239 /* Was an actual write failure,
240 * so mark the block for retirement.*/
241 bi->needs_retiring = 1;
242 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
243 "**>> Block %d needs retiring", flash_block);
246 /* Delete the chunk */
247 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
248 yaffs_skip_rest_of_block(dev);
256 * Simple hash function. Needs to have a reasonable spread
259 static inline int yaffs_hash_fn(int n)
263 return n % YAFFS_NOBJECT_BUCKETS;
267 * Access functions to useful fake objects.
268 * Note that root might have a presence in NAND if permissions are set.
271 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
273 return dev->root_dir;
276 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
278 return dev->lost_n_found;
282 * Erased NAND checking functions
285 int yaffs_check_ff(u8 *buffer, int n_bytes)
287 /* Horrible, slow implementation */
296 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
298 int retval = YAFFS_OK;
299 u8 *data = yaffs_get_temp_buffer(dev);
300 struct yaffs_ext_tags tags;
303 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
305 if (result == YAFFS_FAIL ||
306 tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
309 if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
311 yaffs_trace(YAFFS_TRACE_NANDACCESS,
312 "Chunk %d not erased", nand_chunk);
316 yaffs_release_temp_buffer(dev, data);
322 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
325 struct yaffs_ext_tags *tags)
327 int retval = YAFFS_OK;
328 struct yaffs_ext_tags temp_tags;
329 u8 *buffer = yaffs_get_temp_buffer(dev);
332 result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
333 if (result == YAFFS_FAIL ||
334 memcmp(buffer, data, dev->data_bytes_per_chunk) ||
335 temp_tags.obj_id != tags->obj_id ||
336 temp_tags.chunk_id != tags->chunk_id ||
337 temp_tags.n_bytes != tags->n_bytes)
340 yaffs_release_temp_buffer(dev, buffer);
346 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
349 int reserved_blocks = dev->param.n_reserved_blocks;
352 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
355 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
357 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
360 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
363 struct yaffs_block_info *bi;
365 if (dev->n_erased_blocks < 1) {
366 /* Hoosterman we've got a problem.
367 * Can't get space to gc
369 yaffs_trace(YAFFS_TRACE_ERROR,
370 "yaffs tragedy: no more erased blocks");
375 /* Find an empty block. */
377 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
378 dev->alloc_block_finder++;
379 if (dev->alloc_block_finder < (int)dev->internal_start_block
380 || dev->alloc_block_finder > (int)dev->internal_end_block) {
381 dev->alloc_block_finder = dev->internal_start_block;
384 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
386 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
387 bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
389 bi->seq_number = dev->seq_number;
390 dev->n_erased_blocks--;
391 yaffs_trace(YAFFS_TRACE_ALLOCATE,
392 "Allocated block %d, seq %d, %d left" ,
393 dev->alloc_block_finder, dev->seq_number,
394 dev->n_erased_blocks);
395 return dev->alloc_block_finder;
399 yaffs_trace(YAFFS_TRACE_ALWAYS,
400 "yaffs tragedy: no more erased blocks, but there should have been %d",
401 dev->n_erased_blocks);
406 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
407 struct yaffs_block_info **block_ptr)
410 struct yaffs_block_info *bi;
412 if (dev->alloc_block < 0) {
413 /* Get next block to allocate off */
414 dev->alloc_block = yaffs_find_alloc_block(dev);
418 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
419 /* No space unless we're allowed to use the reserve. */
423 if (dev->n_erased_blocks < (int)dev->param.n_reserved_blocks
424 && dev->alloc_page == 0)
425 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
427 /* Next page please.... */
428 if (dev->alloc_block >= 0) {
429 bi = yaffs_get_block_info(dev, dev->alloc_block);
431 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
434 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
438 dev->n_free_chunks--;
440 /* If the block is full set the state to full */
441 if (dev->alloc_page >= dev->param.chunks_per_block) {
442 bi->block_state = YAFFS_BLOCK_STATE_FULL;
443 dev->alloc_block = -1;
452 yaffs_trace(YAFFS_TRACE_ERROR,
453 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
458 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
462 n = dev->n_erased_blocks * dev->param.chunks_per_block;
464 if (dev->alloc_block > 0)
465 n += (dev->param.chunks_per_block - dev->alloc_page);
472 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
473 * if we don't want to write to it.
475 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
477 struct yaffs_block_info *bi;
479 if (dev->alloc_block > 0) {
480 bi = yaffs_get_block_info(dev, dev->alloc_block);
481 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
482 bi->block_state = YAFFS_BLOCK_STATE_FULL;
483 dev->alloc_block = -1;
488 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
490 struct yaffs_ext_tags *tags, int use_reserver)
496 yaffs2_checkpt_invalidate(dev);
499 struct yaffs_block_info *bi = 0;
502 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
508 /* First check this chunk is erased, if it needs
509 * checking. The checking policy (unless forced
510 * always on) is as follows:
512 * Check the first page we try to write in a block.
513 * If the check passes then we don't need to check any
514 * more. If the check fails, we check again...
515 * If the block has been erased, we don't need to check.
517 * However, if the block has been prioritised for gc,
518 * then we think there might be something odd about
519 * this block and stop using it.
521 * Rationale: We should only ever see chunks that have
522 * not been erased if there was a partially written
523 * chunk due to power loss. This checking policy should
524 * catch that case with very few checks and thus save a
525 * lot of checks that are most likely not needed.
528 * If an erase check fails or the write fails we skip the
532 /* let's give it a try */
535 if (dev->param.always_check_erased)
536 bi->skip_erased_check = 0;
538 if (!bi->skip_erased_check) {
539 erased_ok = yaffs_check_chunk_erased(dev, chunk);
540 if (erased_ok != YAFFS_OK) {
541 yaffs_trace(YAFFS_TRACE_ERROR,
542 "**>> yaffs chunk %d was not erased",
545 /* If not erased, delete this one,
546 * skip rest of block and
547 * try another chunk */
548 yaffs_chunk_del(dev, chunk, 1, __LINE__);
549 yaffs_skip_rest_of_block(dev);
554 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
556 if (!bi->skip_erased_check)
558 yaffs_verify_chunk_written(dev, chunk, data, tags);
560 if (write_ok != YAFFS_OK) {
561 /* Clean up aborted write, skip to next block and
562 * try another chunk */
563 yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
567 bi->skip_erased_check = 1;
569 /* Copy the data into the robustification buffer */
570 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
572 } while (write_ok != YAFFS_OK &&
573 (yaffs_wr_attempts == 0 || attempts <= yaffs_wr_attempts));
579 yaffs_trace(YAFFS_TRACE_ERROR,
580 "**>> yaffs write required %d attempts",
582 dev->n_retried_writes += (attempts - 1);
589 * Block retiring for handling a broken block.
592 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
594 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
596 yaffs2_checkpt_invalidate(dev);
598 yaffs2_clear_oldest_dirty_seq(dev, bi);
600 if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
601 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
602 yaffs_trace(YAFFS_TRACE_ALWAYS,
603 "yaffs: Failed to mark bad and erase block %d",
606 struct yaffs_ext_tags tags;
608 flash_block * dev->param.chunks_per_block;
610 u8 *buffer = yaffs_get_temp_buffer(dev);
612 memset(buffer, 0xff, dev->data_bytes_per_chunk);
613 memset(&tags, 0, sizeof(tags));
614 tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
615 if (dev->tagger.write_chunk_tags_fn(dev, chunk_id -
619 yaffs_trace(YAFFS_TRACE_ALWAYS,
620 "yaffs: Failed to write bad block marker to block %d",
623 yaffs_release_temp_buffer(dev, buffer);
627 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
628 bi->gc_prioritise = 0;
629 bi->needs_retiring = 0;
631 dev->n_retired_blocks++;
634 /*---------------- Name handling functions ------------*/
636 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
637 const YCHAR *oh_name, int buff_size)
639 #ifdef CONFIG_YAFFS_AUTO_UNICODE
640 if (dev->param.auto_unicode) {
642 /* It is an ASCII name, do an ASCII to
643 * unicode conversion */
644 const char *ascii_oh_name = (const char *)oh_name;
645 int n = buff_size - 1;
646 while (n > 0 && *ascii_oh_name) {
647 *name = *ascii_oh_name;
653 strncpy(name, oh_name + 1, buff_size - 1);
660 strncpy(name, oh_name, buff_size - 1);
664 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
667 #ifdef CONFIG_YAFFS_AUTO_UNICODE
672 if (dev->param.auto_unicode) {
677 /* Figure out if the name will fit in ascii character set */
678 while (is_ascii && *w) {
685 /* It is an ASCII name, so convert unicode to ascii */
686 char *ascii_oh_name = (char *)oh_name;
687 int n = YAFFS_MAX_NAME_LENGTH - 1;
688 while (n > 0 && *name) {
689 *ascii_oh_name = *name;
695 /* Unicode name, so save starting at the second YCHAR */
697 strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
704 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
708 static u16 yaffs_calc_name_sum(const YCHAR *name)
716 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
718 /* 0x1f mask is case insensitive */
719 sum += ((*name) & 0x1f) * i;
727 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
729 memset(obj->short_name, 0, sizeof(obj->short_name));
731 if (name && !name[0]) {
732 yaffs_fix_null_name(obj, obj->short_name,
733 YAFFS_SHORT_NAME_LENGTH);
734 name = obj->short_name;
736 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
737 YAFFS_SHORT_NAME_LENGTH) {
738 strcpy(obj->short_name, name);
741 obj->sum = yaffs_calc_name_sum(name);
744 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
745 const struct yaffs_obj_hdr *oh)
747 #ifdef CONFIG_YAFFS_AUTO_UNICODE
748 YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
749 memset(tmp_name, 0, sizeof(tmp_name));
750 yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
751 YAFFS_MAX_NAME_LENGTH + 1);
752 yaffs_set_obj_name(obj, tmp_name);
754 yaffs_set_obj_name(obj, oh->name);
758 loff_t yaffs_max_file_size(struct yaffs_dev *dev)
760 if (sizeof(loff_t) < 8)
761 return YAFFS_MAX_FILE_SIZE_32;
763 return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
766 /*-------------------- TNODES -------------------
768 * List of spare tnodes
769 * The list is hooked together using the first pointer
773 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
775 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
778 memset(tn, 0, dev->tnode_size);
782 dev->checkpoint_blocks_required = 0; /* force recalculation */
787 /* FreeTnode frees up a tnode and puts it back on the free list */
788 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
790 yaffs_free_raw_tnode(dev, tn);
792 dev->checkpoint_blocks_required = 0; /* force recalculation */
795 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
797 yaffs_deinit_raw_tnodes_and_objs(dev);
802 static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
803 unsigned pos, unsigned val)
805 u32 *map = (u32 *) tn;
811 pos &= YAFFS_TNODES_LEVEL0_MASK;
812 val >>= dev->chunk_grp_bits;
814 bit_in_map = pos * dev->tnode_width;
815 word_in_map = bit_in_map / 32;
816 bit_in_word = bit_in_map & (32 - 1);
818 mask = dev->tnode_mask << bit_in_word;
820 map[word_in_map] &= ~mask;
821 map[word_in_map] |= (mask & (val << bit_in_word));
823 if (dev->tnode_width > (32 - bit_in_word)) {
824 bit_in_word = (32 - bit_in_word);
827 dev->tnode_mask >> bit_in_word;
828 map[word_in_map] &= ~mask;
829 map[word_in_map] |= (mask & (val >> bit_in_word));
833 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
836 u32 *map = (u32 *) tn;
842 pos &= YAFFS_TNODES_LEVEL0_MASK;
844 bit_in_map = pos * dev->tnode_width;
845 word_in_map = bit_in_map / 32;
846 bit_in_word = bit_in_map & (32 - 1);
848 val = map[word_in_map] >> bit_in_word;
850 if (dev->tnode_width > (32 - bit_in_word)) {
851 bit_in_word = (32 - bit_in_word);
853 val |= (map[word_in_map] << bit_in_word);
856 val &= dev->tnode_mask;
857 val <<= dev->chunk_grp_bits;
862 /* ------------------- End of individual tnode manipulation -----------------*/
864 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
865 * The look up tree is represented by the top tnode and the number of top_level
866 * in the tree. 0 means only the level 0 tnode is in the tree.
869 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
870 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
871 struct yaffs_file_var *file_struct,
874 struct yaffs_tnode *tn = file_struct->top;
877 int level = file_struct->top_level;
881 /* Check sane level and chunk Id */
882 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
885 if (chunk_id > YAFFS_MAX_CHUNK_ID)
888 /* First check we're tall enough (ie enough top_level) */
890 i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
893 i >>= YAFFS_TNODES_INTERNAL_BITS;
897 if (required_depth > file_struct->top_level)
898 return NULL; /* Not tall enough, so we can't find it */
900 /* Traverse down to level 0 */
901 while (level > 0 && tn) {
902 tn = tn->internal[(chunk_id >>
903 (YAFFS_TNODES_LEVEL0_BITS +
905 YAFFS_TNODES_INTERNAL_BITS)) &
906 YAFFS_TNODES_INTERNAL_MASK];
913 /* add_find_tnode_0 finds the level 0 tnode if it exists,
914 * otherwise first expands the tree.
915 * This happens in two steps:
916 * 1. If the tree isn't tall enough, then make it taller.
917 * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
919 * Used when modifying the tree.
921 * If the tn argument is NULL, then a fresh tnode will be added otherwise the
922 * specified tn will be plugged into the ttree.
925 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
926 struct yaffs_file_var *file_struct,
928 struct yaffs_tnode *passed_tn)
933 struct yaffs_tnode *tn;
936 /* Check sane level and page Id */
937 if (file_struct->top_level < 0 ||
938 file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
941 if (chunk_id > YAFFS_MAX_CHUNK_ID)
944 /* First check we're tall enough (ie enough top_level) */
946 x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
949 x >>= YAFFS_TNODES_INTERNAL_BITS;
953 if (required_depth > file_struct->top_level) {
954 /* Not tall enough, gotta make the tree taller */
955 for (i = file_struct->top_level; i < required_depth; i++) {
957 tn = yaffs_get_tnode(dev);
960 tn->internal[0] = file_struct->top;
961 file_struct->top = tn;
962 file_struct->top_level++;
964 yaffs_trace(YAFFS_TRACE_ERROR,
965 "yaffs: no more tnodes");
971 /* Traverse down to level 0, adding anything we need */
973 l = file_struct->top_level;
974 tn = file_struct->top;
977 while (l > 0 && tn) {
979 (YAFFS_TNODES_LEVEL0_BITS +
980 (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
981 YAFFS_TNODES_INTERNAL_MASK;
983 if ((l > 1) && !tn->internal[x]) {
984 /* Add missing non-level-zero tnode */
985 tn->internal[x] = yaffs_get_tnode(dev);
986 if (!tn->internal[x])
989 /* Looking from level 1 at level 0 */
991 /* If we already have one, release it */
993 yaffs_free_tnode(dev,
995 tn->internal[x] = passed_tn;
997 } else if (!tn->internal[x]) {
998 /* Don't have one, none passed in */
999 tn->internal[x] = yaffs_get_tnode(dev);
1000 if (!tn->internal[x])
1005 tn = tn->internal[x];
1009 /* top is level 0 */
1011 memcpy(tn, passed_tn,
1012 (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
1013 yaffs_free_tnode(dev, passed_tn);
1020 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
1023 return (tags->chunk_id == (u32)chunk_obj &&
1024 tags->obj_id == (u32)obj_id &&
1025 !tags->is_deleted) ? 1 : 0;
1029 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
1030 struct yaffs_ext_tags *tags, int obj_id,
1035 for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
1036 if (yaffs_check_chunk_bit
1037 (dev, the_chunk / dev->param.chunks_per_block,
1038 the_chunk % dev->param.chunks_per_block)) {
1040 if (dev->chunk_grp_size == 1)
1043 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
1045 if (yaffs_tags_match(tags,
1046 obj_id, inode_chunk)) {
1057 int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1058 struct yaffs_ext_tags *tags)
1060 /*Get the Tnode, then get the level 0 offset chunk offset */
1061 struct yaffs_tnode *tn;
1063 struct yaffs_ext_tags local_tags;
1065 struct yaffs_dev *dev = in->my_dev;
1068 /* Passed a NULL, so use our own tags space */
1072 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1077 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1079 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1084 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1085 struct yaffs_ext_tags *tags)
1087 /* Get the Tnode, then get the level 0 offset chunk offset */
1088 struct yaffs_tnode *tn;
1090 struct yaffs_ext_tags local_tags;
1091 struct yaffs_dev *dev = in->my_dev;
1095 /* Passed a NULL, so use our own tags space */
1099 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1104 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1106 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1109 /* Delete the entry in the filestructure (if found) */
1111 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1116 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1117 int nand_chunk, int in_scan)
1119 /* NB in_scan is zero unless scanning.
1120 * For forward scanning, in_scan is > 0;
1121 * for backward scanning in_scan is < 0
1123 * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1126 struct yaffs_tnode *tn;
1127 struct yaffs_dev *dev = in->my_dev;
1129 struct yaffs_ext_tags existing_tags;
1130 struct yaffs_ext_tags new_tags;
1131 unsigned existing_serial, new_serial;
1133 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1134 /* Just ignore an attempt at putting a chunk into a non-file
1136 * If it is not during Scanning then something went wrong!
1139 yaffs_trace(YAFFS_TRACE_ERROR,
1140 "yaffs tragedy:attempt to put data chunk into a non-file"
1145 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1149 tn = yaffs_add_find_tnode_0(dev,
1150 &in->variant.file_variant,
1156 /* Dummy insert, bail now */
1159 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1162 /* If we're scanning then we need to test for duplicates
1163 * NB This does not need to be efficient since it should only
1164 * happen when the power fails during a write, then only one
1165 * chunk should ever be affected.
1167 * Correction for YAFFS2: This could happen quite a lot and we
1168 * need to think about efficiency! TODO
1169 * Update: For backward scanning we don't need to re-read tags
1170 * so this is quite cheap.
1173 if (existing_cunk > 0) {
1174 /* NB Right now existing chunk will not be real
1175 * chunk_id if the chunk group size > 1
1176 * thus we have to do a FindChunkInFile to get the
1179 * We have a duplicate now we need to decide which
1182 * Backwards scanning YAFFS2: The old one is what
1183 * we use, dump the new one.
1184 * YAFFS1: Get both sets of tags and compare serial
1189 /* Only do this for forward scanning */
1190 yaffs_rd_chunk_tags_nand(dev,
1194 /* Do a proper find */
1196 yaffs_find_chunk_in_file(in, inode_chunk,
1200 if (existing_cunk <= 0) {
1201 /*Hoosterman - how did this happen? */
1203 yaffs_trace(YAFFS_TRACE_ERROR,
1204 "yaffs tragedy: existing chunk < 0 in scan"
1209 /* NB The deleted flags should be false, otherwise
1210 * the chunks will not be loaded during a scan
1214 new_serial = new_tags.serial_number;
1215 existing_serial = existing_tags.serial_number;
1218 if ((in_scan > 0) &&
1219 (existing_cunk <= 0 ||
1220 ((existing_serial + 1) & 3) == new_serial)) {
1221 /* Forward scanning.
1223 * Delete the old one and drop through to
1226 yaffs_chunk_del(dev, existing_cunk, 1,
1229 /* Backward scanning or we want to use the
1231 * Delete the new one and return early so that
1232 * the tnode isn't changed
1234 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1241 if (existing_cunk == 0)
1242 in->n_data_chunks++;
1244 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1249 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1251 struct yaffs_block_info *the_block;
1254 yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1256 block_no = chunk / dev->param.chunks_per_block;
1257 the_block = yaffs_get_block_info(dev, block_no);
1259 the_block->soft_del_pages++;
1260 dev->n_free_chunks++;
1261 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1265 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
1266 * the chunks in the file.
1267 * All soft deleting does is increment the block's softdelete count and pulls
1268 * the chunk out of the tnode.
1269 * Thus, essentially this is the same as DeleteWorker except that the chunks
1273 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1274 u32 level, int chunk_offset)
1279 struct yaffs_dev *dev = in->my_dev;
1285 for (i = YAFFS_NTNODES_INTERNAL - 1;
1288 if (tn->internal[i]) {
1290 yaffs_soft_del_worker(in,
1294 YAFFS_TNODES_INTERNAL_BITS)
1297 yaffs_free_tnode(dev,
1299 tn->internal[i] = NULL;
1301 /* Can this happen? */
1305 return (all_done) ? 1 : 0;
1309 for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1310 the_chunk = yaffs_get_group_base(dev, tn, i);
1312 yaffs_soft_del_chunk(dev, the_chunk);
1313 yaffs_load_tnode_0(dev, tn, i, 0);
1319 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1321 struct yaffs_dev *dev = obj->my_dev;
1322 struct yaffs_obj *parent;
1324 yaffs_verify_obj_in_dir(obj);
1325 parent = obj->parent;
1327 yaffs_verify_dir(parent);
1329 if (dev && dev->param.remove_obj_fn)
1330 dev->param.remove_obj_fn(obj);
1332 list_del_init(&obj->siblings);
1335 yaffs_verify_dir(parent);
1338 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1341 yaffs_trace(YAFFS_TRACE_ALWAYS,
1342 "tragedy: Trying to add an object to a null pointer directory"
1347 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1348 yaffs_trace(YAFFS_TRACE_ALWAYS,
1349 "tragedy: Trying to add an object to a non-directory"
1354 if (obj->siblings.prev == NULL) {
1355 /* Not initialised */
1359 yaffs_verify_dir(directory);
1361 yaffs_remove_obj_from_dir(obj);
1364 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1365 obj->parent = directory;
1367 if (directory == obj->my_dev->unlinked_dir
1368 || directory == obj->my_dev->del_dir) {
1370 obj->my_dev->n_unlinked_files++;
1371 obj->rename_allowed = 0;
1374 yaffs_verify_dir(directory);
1375 yaffs_verify_obj_in_dir(obj);
1378 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1379 struct yaffs_obj *new_dir,
1380 const YCHAR *new_name, int force, int shadows)
1384 struct yaffs_obj *existing_target;
1386 if (new_dir == NULL)
1387 new_dir = obj->parent; /* use the old directory */
1389 if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1390 yaffs_trace(YAFFS_TRACE_ALWAYS,
1391 "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1396 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1397 del_op = (new_dir == obj->my_dev->del_dir);
1399 existing_target = yaffs_find_by_name(new_dir, new_name);
1401 /* If the object is a file going into the unlinked directory,
1402 * then it is OK to just stuff it in since duplicate names are OK.
1403 * else only proceed if the new name does not exist and we're putting
1404 * it into a directory.
1406 if (!(unlink_op || del_op || force ||
1407 shadows > 0 || !existing_target) ||
1408 new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1411 yaffs_set_obj_name(obj, new_name);
1413 yaffs_add_obj_to_dir(new_dir, obj);
1418 /* If it is a deletion then we mark it as a shrink for gc */
1419 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1425 /*------------------------ Short Operations Cache ------------------------------
1426 * In many situations where there is no high level buffering a lot of
1427 * reads might be short sequential reads, and a lot of writes may be short
1428 * sequential writes. eg. scanning/writing a jpeg file.
1429 * In these cases, a short read/write cache can provide a huge perfomance
1430 * benefit with dumb-as-a-rock code.
1431 * In Linux, the page cache provides read buffering and the short op cache
1432 * provides write buffering.
1434 * There are a small number (~10) of cache chunks per device so that we don't
1435 * need a very intelligent search.
1438 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1440 struct yaffs_dev *dev = obj->my_dev;
1442 struct yaffs_cache *cache;
1443 int n_caches = obj->my_dev->param.n_caches;
1445 for (i = 0; i < n_caches; i++) {
1446 cache = &dev->cache[i];
1447 if (cache->object == obj && cache->dirty)
1454 static void yaffs_flush_single_cache(struct yaffs_cache *cache, int discard)
1457 if (!cache || cache->locked)
1460 /* Write it out and free it up if need be.*/
1462 yaffs_wr_data_obj(cache->object,
1472 cache->object = NULL;
1475 static void yaffs_flush_file_cache(struct yaffs_obj *obj, int discard)
1477 struct yaffs_dev *dev = obj->my_dev;
1479 struct yaffs_cache *cache;
1480 int n_caches = obj->my_dev->param.n_caches;
1486 /* Find the chunks for this object and flush them. */
1487 for (i = 0; i < n_caches; i++) {
1488 cache = &dev->cache[i];
1489 if (cache->object == obj)
1490 yaffs_flush_single_cache(cache, discard);
1496 void yaffs_flush_whole_cache(struct yaffs_dev *dev, int discard)
1498 struct yaffs_obj *obj;
1499 int n_caches = dev->param.n_caches;
1502 /* Find a dirty object in the cache and flush it...
1503 * until there are no further dirty objects.
1507 for (i = 0; i < n_caches && !obj; i++) {
1508 if (dev->cache[i].object && dev->cache[i].dirty)
1509 obj = dev->cache[i].object;
1512 yaffs_flush_file_cache(obj, discard);
1517 /* Grab us an unused cache chunk for use.
1518 * First look for an empty one.
1519 * Then look for the least recently used non-dirty one.
1520 * Then look for the least recently used dirty one...., flush and look again.
1522 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1526 if (dev->param.n_caches > 0) {
1527 for (i = 0; i < dev->param.n_caches; i++) {
1528 if (!dev->cache[i].object)
1529 return &dev->cache[i];
1536 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1538 struct yaffs_cache *cache;
1542 if (dev->param.n_caches < 1)
1545 /* First look for an unused cache */
1547 cache = yaffs_grab_chunk_worker(dev);
1553 * Thery were all in use.
1554 * Find the LRU cache and flush it if it is dirty.
1560 for (i = 0; i < dev->param.n_caches; i++) {
1561 if (dev->cache[i].object &&
1562 !dev->cache[i].locked &&
1563 (dev->cache[i].last_use < usage || !cache)) {
1564 usage = dev->cache[i].last_use;
1565 cache = &dev->cache[i];
1570 yaffs_flush_single_cache(cache, 1);
1572 yaffs_flush_file_cache(cache->object, 1);
1573 cache = yaffs_grab_chunk_worker(dev);
1579 /* Find a cached chunk */
1580 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1583 struct yaffs_dev *dev = obj->my_dev;
1586 if (dev->param.n_caches < 1)
1589 for (i = 0; i < dev->param.n_caches; i++) {
1590 if (dev->cache[i].object == obj &&
1591 dev->cache[i].chunk_id == chunk_id) {
1594 return &dev->cache[i];
1600 /* Mark the chunk for the least recently used algorithym */
1601 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1606 if (dev->param.n_caches < 1)
1609 if (dev->cache_last_use < 0 ||
1610 dev->cache_last_use > 100000000) {
1611 /* Reset the cache usages */
1612 for (i = 1; i < dev->param.n_caches; i++)
1613 dev->cache[i].last_use = 0;
1615 dev->cache_last_use = 0;
1617 dev->cache_last_use++;
1618 cache->last_use = dev->cache_last_use;
1624 /* Invalidate a single cache page.
1625 * Do this when a whole page gets written,
1626 * ie the short cache for this page is no longer valid.
1628 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1630 struct yaffs_cache *cache;
1632 if (object->my_dev->param.n_caches > 0) {
1633 cache = yaffs_find_chunk_cache(object, chunk_id);
1636 cache->object = NULL;
1640 /* Invalidate all the cache pages associated with this object
1641 * Do this whenever ther file is deleted or resized.
1643 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1646 struct yaffs_dev *dev = in->my_dev;
1648 if (dev->param.n_caches > 0) {
1649 /* Invalidate it. */
1650 for (i = 0; i < dev->param.n_caches; i++) {
1651 if (dev->cache[i].object == in)
1652 dev->cache[i].object = NULL;
1657 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1660 struct yaffs_dev *dev = obj->my_dev;
1662 /* If it is still linked into the bucket list, free from the list */
1663 if (!list_empty(&obj->hash_link)) {
1664 list_del_init(&obj->hash_link);
1665 bucket = yaffs_hash_fn(obj->obj_id);
1666 dev->obj_bucket[bucket].count--;
1670 /* FreeObject frees up a Object and puts it back on the free list */
1671 static void yaffs_free_obj(struct yaffs_obj *obj)
1673 struct yaffs_dev *dev;
1680 yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1681 obj, obj->my_inode);
1684 if (!list_empty(&obj->siblings))
1687 if (obj->my_inode) {
1688 /* We're still hooked up to a cached inode.
1689 * Don't delete now, but mark for later deletion
1691 obj->defered_free = 1;
1695 yaffs_unhash_obj(obj);
1697 yaffs_free_raw_obj(dev, obj);
1699 dev->checkpoint_blocks_required = 0; /* force recalculation */
1702 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1704 if (obj->defered_free)
1705 yaffs_free_obj(obj);
1708 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1710 /* Iinvalidate the file's data in the cache, without flushing. */
1711 yaffs_invalidate_whole_cache(in);
1713 if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
1714 /* Move to unlinked directory so we have a deletion record */
1715 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1719 yaffs_remove_obj_from_dir(in);
1720 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1728 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1730 if (!obj->deleted ||
1731 obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
1735 if (obj->n_data_chunks <= 0) {
1736 /* Empty file with no duplicate object headers,
1737 * just delete it immediately */
1738 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
1739 obj->variant.file_variant.top = NULL;
1740 yaffs_trace(YAFFS_TRACE_TRACING,
1741 "yaffs: Deleting empty file %d",
1743 yaffs_generic_obj_del(obj);
1745 yaffs_soft_del_worker(obj,
1746 obj->variant.file_variant.top,
1748 file_variant.top_level, 0);
1753 /* Pruning removes any part of the file structure tree that is beyond the
1754 * bounds of the file (ie that does not point to chunks).
1756 * A file should only get pruned when its size is reduced.
1758 * Before pruning, the chunks must be pulled from the tree and the
1759 * level 0 tnode entries must be zeroed out.
1760 * Could also use this for file deletion, but that's probably better handled
1761 * by a special case.
1763 * This function is recursive. For levels > 0 the function is called again on
1764 * any sub-tree. For level == 0 we just check if the sub-tree has data.
1765 * If there is no data in a subtree then it is pruned.
1768 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1769 struct yaffs_tnode *tn, u32 level,
1781 for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1782 if (tn->internal[i]) {
1784 yaffs_prune_worker(dev,
1787 (i == 0) ? del0 : 1);
1790 if (tn->internal[i])
1794 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1795 u32 *map = (u32 *) tn;
1797 for (i = 0; !has_data && i < tnode_size_u32; i++) {
1803 if (has_data == 0 && del0) {
1804 /* Free and return NULL */
1805 yaffs_free_tnode(dev, tn);
1811 static int yaffs_prune_tree(struct yaffs_dev *dev,
1812 struct yaffs_file_var *file_struct)
1817 struct yaffs_tnode *tn;
1819 if (file_struct->top_level < 1)
1823 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1825 /* Now we have a tree with all the non-zero branches NULL but
1826 * the height is the same as it was.
1827 * Let's see if we can trim internal tnodes to shorten the tree.
1828 * We can do this if only the 0th element in the tnode is in use
1829 * (ie all the non-zero are NULL)
1832 while (file_struct->top_level && !done) {
1833 tn = file_struct->top;
1836 for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1837 if (tn->internal[i])
1842 file_struct->top = tn->internal[0];
1843 file_struct->top_level--;
1844 yaffs_free_tnode(dev, tn);
1853 /*-------------------- End of File Structure functions.-------------------*/
1855 /* alloc_empty_obj gets us a clean Object.*/
1856 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1858 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1865 /* Now sweeten it up... */
1867 memset(obj, 0, sizeof(struct yaffs_obj));
1868 obj->being_created = 1;
1872 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1873 INIT_LIST_HEAD(&(obj->hard_links));
1874 INIT_LIST_HEAD(&(obj->hash_link));
1875 INIT_LIST_HEAD(&obj->siblings);
1877 /* Now make the directory sane */
1878 if (dev->root_dir) {
1879 obj->parent = dev->root_dir;
1880 list_add(&(obj->siblings),
1881 &dev->root_dir->variant.dir_variant.children);
1884 /* Add it to the lost and found directory.
1885 * NB Can't put root or lost-n-found in lost-n-found so
1886 * check if lost-n-found exists first
1888 if (dev->lost_n_found)
1889 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1891 obj->being_created = 0;
1893 dev->checkpoint_blocks_required = 0; /* force recalculation */
1898 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1902 int lowest = 999999;
1904 /* Search for the shortest list or one that
1908 for (i = 0; i < 10 && lowest > 4; i++) {
1909 dev->bucket_finder++;
1910 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1911 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1912 lowest = dev->obj_bucket[dev->bucket_finder].count;
1913 l = dev->bucket_finder;
1920 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1922 int bucket = yaffs_find_nice_bucket(dev);
1924 struct list_head *i;
1925 u32 n = (u32) bucket;
1928 * Now find an object value that has not already been taken
1929 * by scanning the list, incrementing each time by number of buckets.
1933 n += YAFFS_NOBJECT_BUCKETS;
1934 list_for_each(i, &dev->obj_bucket[bucket].list) {
1935 /* Check if this value is already taken. */
1936 if (i && list_entry(i, struct yaffs_obj,
1937 hash_link)->obj_id == n)
1944 static void yaffs_hash_obj(struct yaffs_obj *in)
1946 int bucket = yaffs_hash_fn(in->obj_id);
1947 struct yaffs_dev *dev = in->my_dev;
1949 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1950 dev->obj_bucket[bucket].count++;
1953 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1955 int bucket = yaffs_hash_fn(number);
1956 struct list_head *i;
1957 struct yaffs_obj *in;
1959 list_for_each(i, &dev->obj_bucket[bucket].list) {
1960 /* Look if it is in the list */
1961 in = list_entry(i, struct yaffs_obj, hash_link);
1962 if (in->obj_id == number) {
1963 /* Don't show if it is defered free */
1964 if (in->defered_free)
1973 static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1974 enum yaffs_obj_type type)
1976 struct yaffs_obj *the_obj = NULL;
1977 struct yaffs_tnode *tn = NULL;
1980 number = yaffs_new_obj_id(dev);
1982 if (type == YAFFS_OBJECT_TYPE_FILE) {
1983 tn = yaffs_get_tnode(dev);
1988 the_obj = yaffs_alloc_empty_obj(dev);
1991 yaffs_free_tnode(dev, tn);
1996 the_obj->rename_allowed = 1;
1997 the_obj->unlink_allowed = 1;
1998 the_obj->obj_id = number;
1999 yaffs_hash_obj(the_obj);
2000 the_obj->variant_type = type;
2001 yaffs_load_current_time(the_obj, 1, 1);
2004 case YAFFS_OBJECT_TYPE_FILE:
2005 the_obj->variant.file_variant.file_size = 0;
2006 the_obj->variant.file_variant.stored_size = 0;
2007 the_obj->variant.file_variant.shrink_size =
2008 yaffs_max_file_size(dev);
2009 the_obj->variant.file_variant.top_level = 0;
2010 the_obj->variant.file_variant.top = tn;
2012 case YAFFS_OBJECT_TYPE_DIRECTORY:
2013 INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
2014 INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
2016 case YAFFS_OBJECT_TYPE_SYMLINK:
2017 case YAFFS_OBJECT_TYPE_HARDLINK:
2018 case YAFFS_OBJECT_TYPE_SPECIAL:
2019 /* No action required */
2021 case YAFFS_OBJECT_TYPE_UNKNOWN:
2022 /* todo this should not happen */
2028 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2029 int number, u32 mode)
2032 struct yaffs_obj *obj =
2033 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2038 obj->fake = 1; /* it is fake so it might not use NAND */
2039 obj->rename_allowed = 0;
2040 obj->unlink_allowed = 0;
2043 obj->yst_mode = mode;
2045 obj->hdr_chunk = 0; /* Not a valid chunk. */
2051 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2057 yaffs_init_raw_tnodes_and_objs(dev);
2059 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2060 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2061 dev->obj_bucket[i].count = 0;
2065 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2067 enum yaffs_obj_type type)
2069 struct yaffs_obj *the_obj = NULL;
2072 the_obj = yaffs_find_by_number(dev, number);
2075 the_obj = yaffs_new_obj(dev, number, type);
2081 YCHAR *yaffs_clone_str(const YCHAR *str)
2083 YCHAR *new_str = NULL;
2089 len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2090 new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2092 strncpy(new_str, str, len);
2099 *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2100 * link (ie. name) is created or deleted in the directory.
2103 * create dir/a : update dir's mtime/ctime
2104 * rm dir/a: update dir's mtime/ctime
2105 * modify dir/a: don't update dir's mtimme/ctime
2107 * This can be handled immediately or defered. Defering helps reduce the number
2108 * of updates when many files in a directory are changed within a brief period.
2110 * If the directory updating is defered then yaffs_update_dirty_dirs must be
2111 * called periodically.
2114 static void yaffs_update_parent(struct yaffs_obj *obj)
2116 struct yaffs_dev *dev;
2122 yaffs_load_current_time(obj, 0, 1);
2123 if (dev->param.defered_dir_update) {
2124 struct list_head *link = &obj->variant.dir_variant.dirty;
2126 if (list_empty(link)) {
2127 list_add(link, &dev->dirty_dirs);
2128 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2129 "Added object %d to dirty directories",
2134 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2138 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2140 struct list_head *link;
2141 struct yaffs_obj *obj;
2142 struct yaffs_dir_var *d_s;
2143 union yaffs_obj_var *o_v;
2145 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2147 while (!list_empty(&dev->dirty_dirs)) {
2148 link = dev->dirty_dirs.next;
2149 list_del_init(link);
2151 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2152 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2153 obj = list_entry(o_v, struct yaffs_obj, variant);
2155 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2159 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2164 * Mknod (create) a new object.
2165 * equiv_obj only has meaning for a hard link;
2166 * alias_str only has meaning for a symlink.
2167 * rdev only has meaning for devices (a subset of special objects)
2170 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2171 struct yaffs_obj *parent,
2176 struct yaffs_obj *equiv_obj,
2177 const YCHAR *alias_str, u32 rdev)
2179 struct yaffs_obj *in;
2181 struct yaffs_dev *dev = parent->my_dev;
2183 /* Check if the entry exists.
2184 * If it does then fail the call since we don't want a dup. */
2185 if (yaffs_find_by_name(parent, name))
2188 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2189 str = yaffs_clone_str(alias_str);
2194 in = yaffs_new_obj(dev, -1, type);
2203 in->variant_type = type;
2205 in->yst_mode = mode;
2207 yaffs_attribs_init(in, gid, uid, rdev);
2209 in->n_data_chunks = 0;
2211 yaffs_set_obj_name(in, name);
2214 yaffs_add_obj_to_dir(parent, in);
2216 in->my_dev = parent->my_dev;
2219 case YAFFS_OBJECT_TYPE_SYMLINK:
2220 in->variant.symlink_variant.alias = str;
2222 case YAFFS_OBJECT_TYPE_HARDLINK:
2223 in->variant.hardlink_variant.equiv_obj = equiv_obj;
2224 in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
2225 list_add(&in->hard_links, &equiv_obj->hard_links);
2227 case YAFFS_OBJECT_TYPE_FILE:
2228 case YAFFS_OBJECT_TYPE_DIRECTORY:
2229 case YAFFS_OBJECT_TYPE_SPECIAL:
2230 case YAFFS_OBJECT_TYPE_UNKNOWN:
2235 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2236 /* Could not create the object header, fail */
2242 yaffs_update_parent(parent);
2247 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2248 const YCHAR *name, u32 mode, u32 uid,
2251 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2252 uid, gid, NULL, NULL, 0);
2255 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
2256 u32 mode, u32 uid, u32 gid)
2258 return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2259 mode, uid, gid, NULL, NULL, 0);
2262 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2263 const YCHAR *name, u32 mode, u32 uid,
2266 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2267 uid, gid, NULL, NULL, rdev);
2270 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2271 const YCHAR *name, u32 mode, u32 uid,
2272 u32 gid, const YCHAR *alias)
2274 return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2275 uid, gid, NULL, alias, 0);
2278 /* yaffs_link_obj returns the object id of the equivalent object.*/
2279 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2280 struct yaffs_obj *equiv_obj)
2282 /* Get the real object in case we were fed a hard link obj */
2283 equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2285 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2286 parent, name, 0, 0, 0,
2287 equiv_obj, NULL, 0))
2296 /*---------------------- Block Management and Page Allocation -------------*/
2298 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2300 if (dev->block_info_alt && dev->block_info)
2301 vfree(dev->block_info);
2303 kfree(dev->block_info);
2305 dev->block_info_alt = 0;
2307 dev->block_info = NULL;
2309 if (dev->chunk_bits_alt && dev->chunk_bits)
2310 vfree(dev->chunk_bits);
2312 kfree(dev->chunk_bits);
2313 dev->chunk_bits_alt = 0;
2314 dev->chunk_bits = NULL;
2317 static int yaffs_init_blocks(struct yaffs_dev *dev)
2319 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2321 dev->block_info = NULL;
2322 dev->chunk_bits = NULL;
2323 dev->alloc_block = -1; /* force it to get a new one */
2325 /* If the first allocation strategy fails, thry the alternate one */
2327 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2328 if (!dev->block_info) {
2330 vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2331 dev->block_info_alt = 1;
2333 dev->block_info_alt = 0;
2336 if (!dev->block_info)
2339 /* Set up dynamic blockinfo stuff. Round up bytes. */
2340 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2342 kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2343 if (!dev->chunk_bits) {
2345 vmalloc(dev->chunk_bit_stride * n_blocks);
2346 dev->chunk_bits_alt = 1;
2348 dev->chunk_bits_alt = 0;
2350 if (!dev->chunk_bits)
2354 memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
2355 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2359 yaffs_deinit_blocks(dev);
2364 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2366 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2370 /* If the block is still healthy erase it and mark as clean.
2371 * If the block has had a data failure, then retire it.
2374 yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2375 "yaffs_block_became_dirty block %d state %d %s",
2376 block_no, bi->block_state,
2377 (bi->needs_retiring) ? "needs retiring" : "");
2379 yaffs2_clear_oldest_dirty_seq(dev, bi);
2381 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2383 /* If this is the block being garbage collected then stop gc'ing */
2384 if (block_no == (int)dev->gc_block)
2387 /* If this block is currently the best candidate for gc
2388 * then drop as a candidate */
2389 if (block_no == (int)dev->gc_dirtiest) {
2390 dev->gc_dirtiest = 0;
2391 dev->gc_pages_in_use = 0;
2394 if (!bi->needs_retiring) {
2395 yaffs2_checkpt_invalidate(dev);
2396 erased_ok = yaffs_erase_block(dev, block_no);
2398 dev->n_erase_failures++;
2399 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2400 "**>> Erasure failed %d", block_no);
2404 /* Verify erasure if needed */
2406 ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
2407 !yaffs_skip_verification(dev))) {
2408 for (i = 0; i < dev->param.chunks_per_block; i++) {
2409 if (!yaffs_check_chunk_erased(dev,
2410 block_no * dev->param.chunks_per_block + i)) {
2411 yaffs_trace(YAFFS_TRACE_ERROR,
2412 ">>Block %d erasure supposedly OK, but chunk %d not erased",
2419 /* We lost a block of free space */
2420 dev->n_free_chunks -= dev->param.chunks_per_block;
2421 yaffs_retire_block(dev, block_no);
2422 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2423 "**>> Block %d retired", block_no);
2427 /* Clean it up... */
2428 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2430 dev->n_erased_blocks++;
2431 bi->pages_in_use = 0;
2432 bi->soft_del_pages = 0;
2433 bi->has_shrink_hdr = 0;
2434 bi->skip_erased_check = 1; /* Clean, so no need to check */
2435 bi->gc_prioritise = 0;
2436 bi->has_summary = 0;
2438 yaffs_clear_chunk_bits(dev, block_no);
2440 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
2443 static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2444 struct yaffs_block_info *bi,
2445 int old_chunk, u8 *buffer)
2449 struct yaffs_ext_tags tags;
2450 struct yaffs_obj *object;
2452 int ret_val = YAFFS_OK;
2454 memset(&tags, 0, sizeof(tags));
2455 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2457 object = yaffs_find_by_number(dev, tags.obj_id);
2459 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2460 "Collecting chunk in block %d, %d %d %d ",
2461 dev->gc_chunk, tags.obj_id,
2462 tags.chunk_id, tags.n_bytes);
2464 if (object && !yaffs_skip_verification(dev)) {
2465 if (tags.chunk_id == 0)
2468 else if (object->soft_del)
2469 /* Defeat the test */
2470 matching_chunk = old_chunk;
2473 yaffs_find_chunk_in_file
2474 (object, tags.chunk_id,
2477 if (old_chunk != matching_chunk)
2478 yaffs_trace(YAFFS_TRACE_ERROR,
2479 "gc: page in gc mismatch: %d %d %d %d",
2487 yaffs_trace(YAFFS_TRACE_ERROR,
2488 "page %d in gc has no object: %d %d %d ",
2490 tags.obj_id, tags.chunk_id,
2496 object->soft_del && tags.chunk_id != 0) {
2497 /* Data chunk in a soft deleted file,
2499 * It's a soft deleted data chunk,
2500 * No need to copy this, just forget
2501 * about it and fix up the object.
2504 /* Free chunks already includes
2505 * softdeleted chunks, how ever this
2506 * chunk is going to soon be really
2507 * deleted which will increment free
2508 * chunks. We have to decrement free
2509 * chunks so this works out properly.
2511 dev->n_free_chunks--;
2512 bi->soft_del_pages--;
2514 object->n_data_chunks--;
2515 if (object->n_data_chunks <= 0) {
2516 /* remeber to clean up obj */
2517 dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
2521 } else if (object) {
2522 /* It's either a data chunk in a live
2523 * file or an ObjectHeader, so we're
2525 * NB Need to keep the ObjectHeaders of
2526 * deleted files until the whole file
2527 * has been deleted off
2529 tags.serial_number++;
2532 if (tags.chunk_id == 0) {
2533 /* It is an object Id,
2534 * We need to nuke the shrinkheader flags since its
2536 * Also need to clean up shadowing.
2537 * NB We don't want to do all the work of translating
2538 * object header endianism back and forth so we leave
2539 * the oh endian in its stored order.
2542 struct yaffs_obj_hdr *oh;
2543 oh = (struct yaffs_obj_hdr *) buffer;
2546 tags.extra_is_shrink = 0;
2547 oh->shadows_obj = 0;
2548 oh->inband_shadowed_obj_id = 0;
2549 tags.extra_shadows = 0;
2551 /* Update file size */
2552 if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
2553 yaffs_oh_size_load(dev, oh,
2554 object->variant.file_variant.stored_size, 1);
2555 tags.extra_file_size =
2556 object->variant.file_variant.stored_size;
2559 yaffs_verify_oh(object, oh, &tags, 1);
2561 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
2564 yaffs_write_new_chunk(dev, buffer, &tags, 1);
2567 if (new_chunk < 0) {
2568 ret_val = YAFFS_FAIL;
2571 /* Now fix up the Tnodes etc. */
2573 if (tags.chunk_id == 0) {
2575 object->hdr_chunk = new_chunk;
2576 object->serial = tags.serial_number;
2578 /* It's a data chunk */
2579 yaffs_put_chunk_in_file(object, tags.chunk_id,
2584 if (ret_val == YAFFS_OK)
2585 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2589 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2592 int ret_val = YAFFS_OK;
2594 int is_checkpt_block;
2596 int chunks_before = yaffs_get_erased_chunks(dev);
2598 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2600 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2602 yaffs_trace(YAFFS_TRACE_TRACING,
2603 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2604 block, bi->pages_in_use, bi->has_shrink_hdr,
2607 /*yaffs_verify_free_chunks(dev); */
2609 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2610 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2612 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2614 dev->gc_disable = 1;
2616 yaffs_summary_gc(dev, block);
2618 if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2619 yaffs_trace(YAFFS_TRACE_TRACING,
2620 "Collecting block %d that has no chunks in use",
2622 yaffs_block_became_dirty(dev, block);
2625 u8 *buffer = yaffs_get_temp_buffer(dev);
2627 yaffs_verify_blk(dev, bi, block);
2629 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2630 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2632 for (/* init already done */ ;
2633 ret_val == YAFFS_OK &&
2634 dev->gc_chunk < dev->param.chunks_per_block &&
2635 (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2637 dev->gc_chunk++, old_chunk++) {
2638 if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2639 /* Page is in use and might need to be copied */
2641 ret_val = yaffs_gc_process_chunk(dev, bi,
2645 yaffs_release_temp_buffer(dev, buffer);
2648 yaffs_verify_collected_blk(dev, bi, block);
2650 if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2652 * The gc did not complete. Set block state back to FULL
2653 * because checkpointing does not restore gc.
2655 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2657 /* The gc completed. */
2658 /* Do any required cleanups */
2659 for (i = 0; i < dev->n_clean_ups; i++) {
2660 /* Time to delete the file too */
2661 struct yaffs_obj *object =
2662 yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2664 yaffs_free_tnode(dev,
2665 object->variant.file_variant.top);
2666 object->variant.file_variant.top = NULL;
2667 yaffs_trace(YAFFS_TRACE_GC,
2668 "yaffs: About to finally delete object %d",
2670 yaffs_generic_obj_del(object);
2671 object->my_dev->n_deleted_files--;
2675 chunks_after = yaffs_get_erased_chunks(dev);
2676 if (chunks_before >= chunks_after)
2677 yaffs_trace(YAFFS_TRACE_GC,
2678 "gc did not increase free chunks before %d after %d",
2679 chunks_before, chunks_after);
2682 dev->n_clean_ups = 0;
2685 dev->gc_disable = 0;
2691 * find_gc_block() selects the dirtiest block (or close enough)
2692 * for garbage collection.
2695 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2696 int aggressive, int background)
2701 int prioritised = 0;
2702 int prioritised_exist = 0;
2703 struct yaffs_block_info *bi;
2706 /* First let's see if we need to grab a prioritised block */
2707 if (dev->has_pending_prioritised_gc && !aggressive) {
2708 dev->gc_dirtiest = 0;
2709 bi = dev->block_info;
2710 for (i = dev->internal_start_block;
2711 i <= dev->internal_end_block && !selected; i++) {
2713 if (bi->gc_prioritise) {
2714 prioritised_exist = 1;
2715 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2716 yaffs_block_ok_for_gc(dev, bi)) {
2725 * If there is a prioritised block and none was selected then
2726 * this happened because there is at least one old dirty block
2727 * gumming up the works. Let's gc the oldest dirty block.
2730 if (prioritised_exist &&
2731 !selected && dev->oldest_dirty_block > 0)
2732 selected = dev->oldest_dirty_block;
2734 if (!prioritised_exist) /* None found, so we can clear this */
2735 dev->has_pending_prioritised_gc = 0;
2738 /* If we're doing aggressive GC then we are happy to take a less-dirty
2739 * block, and search harder.
2740 * else (leasurely gc), then we only bother to do this if the
2741 * block has only a few pages in use.
2747 dev->internal_end_block - dev->internal_start_block + 1;
2749 threshold = dev->param.chunks_per_block;
2750 iterations = n_blocks;
2755 max_threshold = dev->param.chunks_per_block / 2;
2757 max_threshold = dev->param.chunks_per_block / 8;
2759 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2760 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2762 threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2763 if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2764 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2765 if (threshold > max_threshold)
2766 threshold = max_threshold;
2768 iterations = n_blocks / 16 + 1;
2769 if (iterations > 100)
2775 (dev->gc_dirtiest < 1 ||
2776 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2778 dev->gc_block_finder++;
2779 if (dev->gc_block_finder < dev->internal_start_block ||
2780 dev->gc_block_finder > dev->internal_end_block)
2781 dev->gc_block_finder =
2782 dev->internal_start_block;
2784 bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2786 pages_used = bi->pages_in_use - bi->soft_del_pages;
2788 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2789 pages_used < dev->param.chunks_per_block &&
2790 (dev->gc_dirtiest < 1 ||
2791 pages_used < dev->gc_pages_in_use) &&
2792 yaffs_block_ok_for_gc(dev, bi)) {
2793 dev->gc_dirtiest = dev->gc_block_finder;
2794 dev->gc_pages_in_use = pages_used;
2798 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2799 selected = dev->gc_dirtiest;
2803 * If nothing has been selected for a while, try the oldest dirty
2804 * because that's gumming up the works.
2807 if (!selected && dev->param.is_yaffs2 &&
2808 dev->gc_not_done >= (background ? 10 : 20)) {
2809 yaffs2_find_oldest_dirty_seq(dev);
2810 if (dev->oldest_dirty_block > 0) {
2811 selected = dev->oldest_dirty_block;
2812 dev->gc_dirtiest = selected;
2813 dev->oldest_dirty_gc_count++;
2814 bi = yaffs_get_block_info(dev, selected);
2815 dev->gc_pages_in_use =
2816 bi->pages_in_use - bi->soft_del_pages;
2818 dev->gc_not_done = 0;
2823 yaffs_trace(YAFFS_TRACE_GC,
2824 "GC Selected block %d with %d free, prioritised:%d",
2826 dev->param.chunks_per_block - dev->gc_pages_in_use,
2833 dev->gc_dirtiest = 0;
2834 dev->gc_pages_in_use = 0;
2835 dev->gc_not_done = 0;
2836 if (dev->refresh_skip > 0)
2837 dev->refresh_skip--;
2840 yaffs_trace(YAFFS_TRACE_GC,
2841 "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2842 dev->gc_block_finder, dev->gc_not_done, threshold,
2843 dev->gc_dirtiest, dev->gc_pages_in_use,
2844 dev->oldest_dirty_block, background ? " bg" : "");
2850 /* New garbage collector
2851 * If we're very low on erased blocks then we do aggressive garbage collection
2852 * otherwise we do "leasurely" garbage collection.
2853 * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2854 * Passive gc only inspects smaller areas and only accepts more dirty blocks.
2856 * The idea is to help clear out space in a more spread-out manner.
2857 * Dunno if it really does anything useful.
2859 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2862 int gc_ok = YAFFS_OK;
2866 int checkpt_block_adjust;
2868 if (dev->param.gc_control_fn &&
2869 (dev->param.gc_control_fn(dev) & 1) == 0)
2872 if (dev->gc_disable)
2873 /* Bail out so we don't get recursive gc */
2876 /* This loop should pass the first time.
2877 * Only loops here if the collection does not increase space.
2883 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2886 dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2888 dev->n_erased_blocks * dev->param.chunks_per_block;
2890 /* If we need a block soon then do aggressive gc. */
2891 if (dev->n_erased_blocks < min_erased)
2895 && erased_chunks > (dev->n_free_chunks / 4))
2898 if (dev->gc_skip > 20)
2900 if (erased_chunks < dev->n_free_chunks / 2 ||
2901 dev->gc_skip < 1 || background)
2911 /* If we don't already have a block being gc'd then see if we
2912 * should start another */
2914 if (dev->gc_block < 1 && !aggressive) {
2915 dev->gc_block = yaffs2_find_refresh_block(dev);
2917 dev->n_clean_ups = 0;
2919 if (dev->gc_block < 1) {
2921 yaffs_find_gc_block(dev, aggressive, background);
2923 dev->n_clean_ups = 0;
2926 if (dev->gc_block > 0) {
2929 dev->passive_gc_count++;
2931 yaffs_trace(YAFFS_TRACE_GC,
2932 "yaffs: GC n_erased_blocks %d aggressive %d",
2933 dev->n_erased_blocks, aggressive);
2935 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2938 if (dev->n_erased_blocks < (int)dev->param.n_reserved_blocks &&
2939 dev->gc_block > 0) {
2940 yaffs_trace(YAFFS_TRACE_GC,
2941 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2942 dev->n_erased_blocks, max_tries,
2945 } while ((dev->n_erased_blocks < (int)dev->param.n_reserved_blocks) &&
2946 (dev->gc_block > 0) && (max_tries < 2));
2948 return aggressive ? gc_ok : YAFFS_OK;
2953 * Garbage collects. Intended to be called from a background thread.
2954 * Returns non-zero if at least half the free chunks are erased.
2956 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2958 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2960 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2962 yaffs_check_gc(dev, 1);
2963 return erased_chunks > dev->n_free_chunks / 2;
2966 /*-------------------- Data file manipulation -----------------*/
2968 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2970 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2972 if (nand_chunk >= 0)
2973 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2976 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2977 "Chunk %d not found zero instead",
2979 /* get sane (zero) data if you read a hole */
2980 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2986 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
2991 struct yaffs_ext_tags tags;
2992 struct yaffs_block_info *bi;
2998 block = chunk_id / dev->param.chunks_per_block;
2999 page = chunk_id % dev->param.chunks_per_block;
3001 if (!yaffs_check_chunk_bit(dev, block, page))
3002 yaffs_trace(YAFFS_TRACE_VERIFY,
3003 "Deleting invalid chunk %d", chunk_id);
3005 bi = yaffs_get_block_info(dev, block);
3007 yaffs2_update_oldest_dirty_seq(dev, block, bi);
3009 yaffs_trace(YAFFS_TRACE_DELETION,
3010 "line %d delete of chunk %d",
3013 if (!dev->param.is_yaffs2 && mark_flash &&
3014 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
3016 memset(&tags, 0, sizeof(tags));
3017 tags.is_deleted = 1;
3018 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3019 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3021 dev->n_unmarked_deletions++;
3024 /* Pull out of the management area.
3025 * If the whole block became dirty, this will kick off an erasure.
3027 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
3028 bi->block_state == YAFFS_BLOCK_STATE_FULL ||
3029 bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
3030 bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
3031 dev->n_free_chunks++;
3032 yaffs_clear_chunk_bit(dev, block, page);
3035 if (bi->pages_in_use == 0 &&
3036 !bi->has_shrink_hdr &&
3037 bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
3038 bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
3039 yaffs_block_became_dirty(dev, block);
3044 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3045 const u8 *buffer, int n_bytes, int use_reserve)
3047 /* Find old chunk Need to do this to get serial number
3048 * Write new one and patch into tree.
3049 * Invalidate old tags.
3053 struct yaffs_ext_tags prev_tags;
3055 struct yaffs_ext_tags new_tags;
3056 struct yaffs_dev *dev = in->my_dev;
3059 yaffs_check_gc(dev, 0);
3061 /* Get the previous chunk at this location in the file if it exists.
3062 * If it does not exist then put a zero into the tree. This creates
3063 * the tnode now, rather than later when it is harder to clean up.
3065 prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3066 if (prev_chunk_id < 1 &&
3067 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3070 /* Set up new tags */
3071 memset(&new_tags, 0, sizeof(new_tags));
3073 new_tags.chunk_id = inode_chunk;
3074 new_tags.obj_id = in->obj_id;
3075 new_tags.serial_number =
3076 (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3077 new_tags.n_bytes = n_bytes;
3079 if (n_bytes < 1 || n_bytes > (int)dev->data_bytes_per_chunk) {
3080 yaffs_trace(YAFFS_TRACE_ERROR,
3081 "Writing %d bytes to chunk!!!!!!!!!",
3087 * If this is a data chunk and the write goes past the end of the stored
3088 * size then update the stored_size.
3090 if (inode_chunk > 0) {
3091 endpos = (inode_chunk - 1) * dev->data_bytes_per_chunk +
3093 if (in->variant.file_variant.stored_size < endpos)
3094 in->variant.file_variant.stored_size = endpos;
3098 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3100 if (new_chunk_id > 0) {
3101 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3103 if (prev_chunk_id > 0)
3104 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3106 yaffs_verify_file_sane(in);
3108 return new_chunk_id;
3113 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3114 const YCHAR *name, const void *value, int size,
3117 struct yaffs_xattr_mod xmod;
3125 xmod.result = -ENOSPC;
3127 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3135 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3136 struct yaffs_xattr_mod *xmod)
3139 int x_offs = sizeof(struct yaffs_obj_hdr);
3140 struct yaffs_dev *dev = obj->my_dev;
3141 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3142 char *x_buffer = buffer + x_offs;
3146 nval_set(dev, x_buffer, x_size, xmod->name, xmod->data,
3147 xmod->size, xmod->flags);
3149 retval = nval_del(dev, x_buffer, x_size, xmod->name);
3151 obj->has_xattr = nval_hasvalues(dev, x_buffer, x_size);
3152 obj->xattr_known = 1;
3153 xmod->result = retval;
3158 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
3159 void *value, int size)
3161 char *buffer = NULL;
3163 struct yaffs_ext_tags tags;
3164 struct yaffs_dev *dev = obj->my_dev;
3165 int x_offs = sizeof(struct yaffs_obj_hdr);
3166 int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3170 if (obj->hdr_chunk < 1)
3173 /* If we know that the object has no xattribs then don't do all the
3174 * reading and parsing.
3176 if (obj->xattr_known && !obj->has_xattr) {
3183 buffer = (char *)yaffs_get_temp_buffer(dev);
3188 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3190 if (result != YAFFS_OK)
3193 x_buffer = buffer + x_offs;
3195 if (!obj->xattr_known) {
3196 obj->has_xattr = nval_hasvalues(dev, x_buffer, x_size);
3197 obj->xattr_known = 1;
3201 retval = nval_get(dev, x_buffer, x_size,
3204 retval = nval_list(dev, x_buffer, x_size, value, size);
3206 yaffs_release_temp_buffer(dev, (u8 *) buffer);
3210 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3211 const void *value, int size, int flags)
3213 return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3216 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3218 return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3221 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3224 return yaffs_do_xattrib_fetch(obj, name, value, size);
3227 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3229 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3232 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3235 struct yaffs_obj_hdr *oh;
3236 struct yaffs_dev *dev;
3237 struct yaffs_ext_tags tags;
3240 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3244 buf = yaffs_get_temp_buffer(dev);
3246 result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
3248 if (result == YAFFS_FAIL)
3251 oh = (struct yaffs_obj_hdr *)buf;
3253 yaffs_do_endian_oh(dev, oh);
3255 in->lazy_loaded = 0;
3256 in->yst_mode = oh->yst_mode;
3257 yaffs_load_attribs(in, oh);
3258 yaffs_set_obj_name_from_oh(in, oh);
3260 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
3261 in->variant.symlink_variant.alias =
3262 yaffs_clone_str(oh->alias);
3263 yaffs_release_temp_buffer(dev, buf);
3266 /* UpdateObjectHeader updates the header on NAND for an object.
3267 * If name is not NULL, then that new name is used.
3269 * We're always creating the obj header from scratch (except reading
3270 * the old name) so first set up in cpu endianness then run it through
3271 * endian fixing at the end.
3273 * However, a twist: If there are xattribs we leave them as they were.
3275 * Careful! The buffer holds the whole chunk. Part of the chunk holds the
3276 * object header and the rest holds the xattribs, therefore we use a buffer
3277 * pointer and an oh pointer to point to the same memory.
3280 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
3281 int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3284 struct yaffs_block_info *bi;
3285 struct yaffs_dev *dev = in->my_dev;
3290 struct yaffs_ext_tags new_tags;
3291 struct yaffs_ext_tags old_tags;
3292 const YCHAR *alias = NULL;
3294 YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3295 struct yaffs_obj_hdr *oh = NULL;
3296 loff_t file_size = 0;
3298 strcpy(old_name, _Y("silly old name"));
3300 if (in->fake && in != dev->root_dir && !force && !xmod)
3303 yaffs_check_gc(dev, 0);
3304 yaffs_check_obj_details_loaded(in);
3306 buffer = yaffs_get_temp_buffer(in->my_dev);
3307 oh = (struct yaffs_obj_hdr *)buffer;
3309 prev_chunk_id = in->hdr_chunk;
3311 if (prev_chunk_id > 0) {
3312 /* Access the old obj header just to read the name. */
3313 result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3315 if (result == YAFFS_OK) {
3316 yaffs_verify_oh(in, oh, &old_tags, 0);
3317 memcpy(old_name, oh->name, sizeof(oh->name));
3320 * NB We only wipe the object header area because the rest of
3321 * the buffer might contain xattribs.
3323 memset(oh, 0xff, sizeof(*oh));
3326 memset(buffer, 0xff, dev->data_bytes_per_chunk);
3329 oh->type = in->variant_type;
3330 oh->yst_mode = in->yst_mode;
3331 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3333 yaffs_load_attribs_oh(oh, in);
3336 oh->parent_obj_id = in->parent->obj_id;
3338 oh->parent_obj_id = 0;
3340 if (name && *name) {
3341 memset(oh->name, 0, sizeof(oh->name));
3342 yaffs_load_oh_from_name(dev, oh->name, name);
3343 } else if (prev_chunk_id > 0) {
3344 memcpy(oh->name, old_name, sizeof(oh->name));
3346 memset(oh->name, 0, sizeof(oh->name));
3349 oh->is_shrink = is_shrink;
3351 switch (in->variant_type) {
3352 case YAFFS_OBJECT_TYPE_UNKNOWN:
3353 /* Should not happen */
3355 case YAFFS_OBJECT_TYPE_FILE:
3356 if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
3357 oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
3358 file_size = in->variant.file_variant.stored_size;
3359 yaffs_oh_size_load(dev, oh, file_size, 0);
3361 case YAFFS_OBJECT_TYPE_HARDLINK:
3362 oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3364 case YAFFS_OBJECT_TYPE_SPECIAL:
3367 case YAFFS_OBJECT_TYPE_DIRECTORY:
3370 case YAFFS_OBJECT_TYPE_SYMLINK:
3371 alias = in->variant.symlink_variant.alias;
3373 alias = _Y("no alias");
3374 strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3375 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3379 /* process any xattrib modifications */
3381 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3384 memset(&new_tags, 0, sizeof(new_tags));
3386 new_tags.chunk_id = 0;
3387 new_tags.obj_id = in->obj_id;
3388 new_tags.serial_number = in->serial;
3390 /* Add extra info for file header */
3391 new_tags.extra_available = 1;
3392 new_tags.extra_parent_id = oh->parent_obj_id;
3393 new_tags.extra_file_size = file_size;
3394 new_tags.extra_is_shrink = oh->is_shrink;
3395 new_tags.extra_equiv_id = oh->equiv_id;
3396 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3397 new_tags.extra_obj_type = in->variant_type;
3399 /* Now endian swizzle the oh if needed. */
3400 yaffs_do_endian_oh(dev, oh);
3402 yaffs_verify_oh(in, oh, &new_tags, 1);
3404 /* Create new chunk in NAND */
3406 yaffs_write_new_chunk(dev, buffer, &new_tags,
3407 (prev_chunk_id > 0) ? 1 : 0);
3410 yaffs_release_temp_buffer(dev, buffer);
3412 if (new_chunk_id < 0)
3413 return new_chunk_id;
3415 in->hdr_chunk = new_chunk_id;
3417 if (prev_chunk_id > 0)
3418 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3420 if (!yaffs_obj_cache_dirty(in))
3423 /* If this was a shrink, then mark the block
3424 * that the chunk lives on */
3426 bi = yaffs_get_block_info(in->my_dev,
3428 in->my_dev->param.chunks_per_block);
3429 bi->has_shrink_hdr = 1;
3433 return new_chunk_id;
3436 /*--------------------- File read/write ------------------------
3437 * Read and write have very similar structures.
3438 * In general the read/write has three parts to it
3439 * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3440 * Some complete chunks
3441 * An incomplete chunk to end off with
3443 * Curve-balls: the first chunk might also be the last chunk.
3446 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3453 struct yaffs_cache *cache;
3454 struct yaffs_dev *dev;
3459 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3462 /* OK now check for the curveball where the start and end are in
3465 if ((start + n) < dev->data_bytes_per_chunk)
3468 n_copy = dev->data_bytes_per_chunk - start;
3470 cache = yaffs_find_chunk_cache(in, chunk);
3472 /* If the chunk is already in the cache or it is less than
3473 * a whole chunk or we're using inband tags then use the cache
3474 * (if there is caching) else bypass the cache.
3476 if (cache || n_copy != (int)dev->data_bytes_per_chunk ||
3477 dev->param.inband_tags) {
3478 if (dev->param.n_caches > 0) {
3480 /* If we can't find the data in the cache,
3481 * then load it up. */
3485 yaffs_grab_chunk_cache(in->my_dev);
3487 cache->chunk_id = chunk;
3490 yaffs_rd_data_obj(in, chunk,
3495 yaffs_use_cache(dev, cache, 0);
3499 memcpy(buffer, &cache->data[start], n_copy);
3503 /* Read into the local buffer then copy.. */
3506 yaffs_get_temp_buffer(dev);
3507 yaffs_rd_data_obj(in, chunk, local_buffer);
3509 memcpy(buffer, &local_buffer[start], n_copy);
3511 yaffs_release_temp_buffer(dev, local_buffer);
3514 /* A full chunk. Read directly into the buffer. */
3515 yaffs_rd_data_obj(in, chunk, buffer);
3525 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3526 int n_bytes, int write_through)
3535 loff_t start_write = offset;
3536 int chunk_written = 0;
3539 struct yaffs_dev *dev;
3543 while (n > 0 && chunk_written >= 0) {
3544 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3546 if (((loff_t)chunk) *
3547 dev->data_bytes_per_chunk + start != offset ||
3548 start >= dev->data_bytes_per_chunk) {
3549 yaffs_trace(YAFFS_TRACE_ERROR,
3550 "AddrToChunk of offset %lld gives chunk %d start %d",
3551 (long long)offset, chunk, start);
3553 chunk++; /* File pos to chunk in file offset */
3555 /* OK now check for the curveball where the start and end are in
3559 if ((start + n) < dev->data_bytes_per_chunk) {
3562 /* Now calculate how many bytes to write back....
3563 * If we're overwriting and not writing to then end of
3564 * file then we need to write back as much as was there
3568 chunk_start = (((loff_t)(chunk - 1)) *
3569 dev->data_bytes_per_chunk);
3571 if (chunk_start > in->variant.file_variant.file_size)
3572 n_bytes_read = 0; /* Past end of file */
3575 in->variant.file_variant.file_size -
3578 if (n_bytes_read > dev->data_bytes_per_chunk)
3579 n_bytes_read = dev->data_bytes_per_chunk;
3583 (start + n)) ? n_bytes_read : (start + n);
3585 if (n_writeback < 0 ||
3586 n_writeback > (int)dev->data_bytes_per_chunk)
3590 n_copy = dev->data_bytes_per_chunk - start;
3591 n_writeback = dev->data_bytes_per_chunk;
3594 if (n_copy != (int)dev->data_bytes_per_chunk ||
3595 !dev->param.cache_bypass_aligned ||
3596 dev->param.inband_tags) {
3597 /* An incomplete start or end chunk (or maybe both
3598 * start and end chunk), or we're using inband tags,
3599 * or we're forcing writes through the cache,
3600 * so we want to use the cache buffers.
3602 if (dev->param.n_caches > 0) {
3603 struct yaffs_cache *cache;
3605 /* If we can't find the data in the cache, then
3607 cache = yaffs_find_chunk_cache(in, chunk);
3610 yaffs_check_alloc_available(dev, 1)) {
3611 cache = yaffs_grab_chunk_cache(dev);
3613 cache->chunk_id = chunk;
3616 yaffs_rd_data_obj(in, chunk,
3620 !yaffs_check_alloc_available(dev,
3622 /* Drop the cache if it was a read cache
3623 * item and no space check has been made
3630 yaffs_use_cache(dev, cache, 1);
3633 memcpy(&cache->data[start], buffer,
3637 cache->n_bytes = n_writeback;
3639 if (write_through) {
3649 chunk_written = -1; /* fail write */
3652 /* An incomplete start or end chunk (or maybe
3653 * both start and end chunk). Read into the
3654 * local buffer then copy over and write back.
3657 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3659 yaffs_rd_data_obj(in, chunk, local_buffer);
3660 memcpy(&local_buffer[start], buffer, n_copy);
3663 yaffs_wr_data_obj(in, chunk,
3667 yaffs_release_temp_buffer(dev, local_buffer);
3670 /* A full chunk. Write directly from the buffer. */
3673 yaffs_wr_data_obj(in, chunk, buffer,
3674 dev->data_bytes_per_chunk, 0);
3676 /* Since we've overwritten the cached data,
3677 * we better invalidate it. */
3678 yaffs_invalidate_chunk_cache(in, chunk);
3681 if (chunk_written >= 0) {
3689 /* Update file object */
3691 if ((start_write + n_done) > in->variant.file_variant.file_size)
3692 in->variant.file_variant.file_size = (start_write + n_done);
3698 int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3699 int n_bytes, int write_through)
3701 yaffs2_handle_hole(in, offset);
3702 return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
3705 /* ---------------------- File resizing stuff ------------------ */
3707 static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
3710 struct yaffs_dev *dev = in->my_dev;
3711 loff_t old_size = in->variant.file_variant.file_size;
3719 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3723 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3724 &start_del, &dummy);
3728 /* Delete backwards so that we don't end up with holes if
3729 * power is lost part-way through the operation.
3731 for (i = last_del; i >= start_del; i--) {
3732 /* NB this could be optimised somewhat,
3733 * eg. could retrieve the tags and write them without
3734 * using yaffs_chunk_del
3737 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3743 (dev->internal_start_block * dev->param.chunks_per_block) ||
3745 ((dev->internal_end_block + 1) *
3746 dev->param.chunks_per_block)) {
3747 yaffs_trace(YAFFS_TRACE_ALWAYS,
3748 "Found daft chunk_id %d for %d",
3751 in->n_data_chunks--;
3752 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3757 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3761 struct yaffs_dev *dev = obj->my_dev;
3763 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3765 yaffs_prune_chunks(obj, new_size);
3767 if (new_partial != 0) {
3768 int last_chunk = 1 + new_full;
3769 u8 *local_buffer = yaffs_get_temp_buffer(dev);
3771 /* Rewrite the last chunk with its new size and zero pad */
3772 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3773 memset(local_buffer + new_partial, 0,
3774 dev->data_bytes_per_chunk - new_partial);
3776 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3779 yaffs_release_temp_buffer(dev, local_buffer);
3782 obj->variant.file_variant.file_size = new_size;
3783 obj->variant.file_variant.stored_size = new_size;
3785 yaffs_prune_tree(dev, &obj->variant.file_variant);
3788 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3790 struct yaffs_dev *dev = in->my_dev;
3791 loff_t old_size = in->variant.file_variant.file_size;
3793 yaffs_flush_file_cache(in, 1);
3794 yaffs_invalidate_whole_cache(in);
3796 yaffs_check_gc(dev, 0);
3798 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3801 if (new_size == old_size)
3804 if (new_size > old_size) {
3805 yaffs2_handle_hole(in, new_size);
3806 in->variant.file_variant.file_size = new_size;
3808 /* new_size < old_size */
3809 yaffs_resize_file_down(in, new_size);
3812 /* Write a new object header to reflect the resize.
3813 * show we've shrunk the file, if need be
3814 * Do this only if the file is not in the deleted directories
3815 * and is not shadowed.
3819 in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3820 in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3821 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3826 int yaffs_flush_file(struct yaffs_obj *in,
3834 yaffs_flush_file_cache(in, discard_cache);
3840 yaffs_load_current_time(in, 0, 0);
3842 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3843 YAFFS_OK : YAFFS_FAIL;
3847 /* yaffs_del_file deletes the whole file data
3848 * and the inode associated with the file.
3849 * It does not delete the links associated with the file.
3851 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3855 struct yaffs_dev *dev = in->my_dev;
3862 yaffs_change_obj_name(in, in->my_dev->del_dir,
3863 _Y("deleted"), 0, 0);
3864 yaffs_trace(YAFFS_TRACE_TRACING,
3865 "yaffs: immediate deletion of file %d",
3868 in->my_dev->n_deleted_files++;
3869 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3870 yaffs_resize_file(in, 0);
3871 yaffs_soft_del_file(in);
3874 yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3875 _Y("unlinked"), 0, 0);
3880 static int yaffs_del_file(struct yaffs_obj *in)
3882 int ret_val = YAFFS_OK;
3883 int deleted; /* Need to cache value on stack if in is freed */
3884 struct yaffs_dev *dev = in->my_dev;
3886 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3887 yaffs_resize_file(in, 0);
3889 if (in->n_data_chunks > 0) {
3890 /* Use soft deletion if there is data in the file.
3891 * That won't be the case if it has been resized to zero.
3894 ret_val = yaffs_unlink_file_if_needed(in);
3896 deleted = in->deleted;
3898 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3901 in->my_dev->n_deleted_files++;
3902 yaffs_soft_del_file(in);
3904 return deleted ? YAFFS_OK : YAFFS_FAIL;
3906 /* The file has no data chunks so we toss it immediately */
3907 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
3908 in->variant.file_variant.top = NULL;
3909 yaffs_generic_obj_del(in);
3915 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
3918 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
3919 !(list_empty(&obj->variant.dir_variant.children));
3922 static int yaffs_del_dir(struct yaffs_obj *obj)
3924 /* First check that the directory is empty. */
3925 if (yaffs_is_non_empty_dir(obj))
3928 return yaffs_generic_obj_del(obj);
3931 static int yaffs_del_symlink(struct yaffs_obj *in)
3933 kfree(in->variant.symlink_variant.alias);
3934 in->variant.symlink_variant.alias = NULL;
3936 return yaffs_generic_obj_del(in);
3939 static int yaffs_del_link(struct yaffs_obj *in)
3941 /* remove this hardlink from the list associated with the equivalent
3944 list_del_init(&in->hard_links);
3945 return yaffs_generic_obj_del(in);
3948 int yaffs_del_obj(struct yaffs_obj *obj)
3952 switch (obj->variant_type) {
3953 case YAFFS_OBJECT_TYPE_FILE:
3954 ret_val = yaffs_del_file(obj);
3956 case YAFFS_OBJECT_TYPE_DIRECTORY:
3957 if (!list_empty(&obj->variant.dir_variant.dirty)) {
3958 yaffs_trace(YAFFS_TRACE_BACKGROUND,
3959 "Remove object %d from dirty directories",
3961 list_del_init(&obj->variant.dir_variant.dirty);
3963 return yaffs_del_dir(obj);
3965 case YAFFS_OBJECT_TYPE_SYMLINK:
3966 ret_val = yaffs_del_symlink(obj);
3968 case YAFFS_OBJECT_TYPE_HARDLINK:
3969 ret_val = yaffs_del_link(obj);
3971 case YAFFS_OBJECT_TYPE_SPECIAL:
3972 ret_val = yaffs_generic_obj_del(obj);
3974 case YAFFS_OBJECT_TYPE_UNKNOWN:
3976 break; /* should not happen. */
3982 static void yaffs_empty_dir_to_dir(struct yaffs_obj *from_dir,
3983 struct yaffs_obj *to_dir)
3985 struct yaffs_obj *obj;
3986 struct list_head *lh;
3987 struct list_head *n;
3989 list_for_each_safe(lh, n, &from_dir->variant.dir_variant.children) {
3990 obj = list_entry(lh, struct yaffs_obj, siblings);
3991 yaffs_add_obj_to_dir(to_dir, obj);
3995 struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
3996 enum yaffs_obj_type type)
3998 /* Tear down the old variant */
3999 switch (obj->variant_type) {
4000 case YAFFS_OBJECT_TYPE_FILE:
4001 /* Nuke file data */
4002 yaffs_resize_file(obj, 0);
4003 yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
4004 obj->variant.file_variant.top = NULL;
4006 case YAFFS_OBJECT_TYPE_DIRECTORY:
4007 /* Put the children in lost and found. */
4008 yaffs_empty_dir_to_dir(obj, obj->my_dev->lost_n_found);
4009 if (!list_empty(&obj->variant.dir_variant.dirty))
4010 list_del_init(&obj->variant.dir_variant.dirty);
4012 case YAFFS_OBJECT_TYPE_SYMLINK:
4013 /* Nuke symplink data */
4014 kfree(obj->variant.symlink_variant.alias);
4015 obj->variant.symlink_variant.alias = NULL;
4017 case YAFFS_OBJECT_TYPE_HARDLINK:
4018 list_del_init(&obj->hard_links);
4024 memset(&obj->variant, 0, sizeof(obj->variant));
4026 /*Set up new variant if the memset is not enough. */
4028 case YAFFS_OBJECT_TYPE_DIRECTORY:
4029 INIT_LIST_HEAD(&obj->variant.dir_variant.children);
4030 INIT_LIST_HEAD(&obj->variant.dir_variant.dirty);
4032 case YAFFS_OBJECT_TYPE_FILE:
4033 case YAFFS_OBJECT_TYPE_SYMLINK:
4034 case YAFFS_OBJECT_TYPE_HARDLINK:
4039 obj->variant_type = type;
4045 static int yaffs_unlink_worker(struct yaffs_obj *obj)
4055 yaffs_update_parent(obj->parent);
4057 if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4058 return yaffs_del_link(obj);
4059 } else if (!list_empty(&obj->hard_links)) {
4060 /* Curve ball: We're unlinking an object that has a hardlink.
4062 * This problem arises because we are not strictly following
4063 * The Linux link/inode model.
4065 * We can't really delete the object.
4066 * Instead, we do the following:
4067 * - Select a hardlink.
4068 * - Unhook it from the hard links
4069 * - Move it from its parent directory so that the rename works.
4070 * - Rename the object to the hardlink's name.
4071 * - Delete the hardlink
4074 struct yaffs_obj *hl;
4075 struct yaffs_obj *parent;
4077 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4079 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
4082 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4083 parent = hl->parent;
4085 list_del_init(&hl->hard_links);
4087 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4089 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4091 if (ret_val == YAFFS_OK)
4092 ret_val = yaffs_generic_obj_del(hl);
4096 } else if (del_now) {
4097 switch (obj->variant_type) {
4098 case YAFFS_OBJECT_TYPE_FILE:
4099 return yaffs_del_file(obj);
4101 case YAFFS_OBJECT_TYPE_DIRECTORY:
4102 list_del_init(&obj->variant.dir_variant.dirty);
4103 return yaffs_del_dir(obj);
4105 case YAFFS_OBJECT_TYPE_SYMLINK:
4106 return yaffs_del_symlink(obj);
4108 case YAFFS_OBJECT_TYPE_SPECIAL:
4109 return yaffs_generic_obj_del(obj);
4111 case YAFFS_OBJECT_TYPE_HARDLINK:
4112 case YAFFS_OBJECT_TYPE_UNKNOWN:
4116 } else if (yaffs_is_non_empty_dir(obj)) {
4119 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4120 _Y("unlinked"), 0, 0);
4124 int yaffs_unlink_obj(struct yaffs_obj *obj)
4126 if (obj && obj->unlink_allowed)
4127 return yaffs_unlink_worker(obj);
4132 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4134 struct yaffs_obj *obj;
4136 obj = yaffs_find_by_name(dir, name);
4137 return yaffs_unlink_obj(obj);
4141 * If old_name is NULL then we take old_dir as the object to be renamed.
4143 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
4144 struct yaffs_obj *new_dir, const YCHAR *new_name)
4146 struct yaffs_obj *obj = NULL;
4147 struct yaffs_obj *existing_target = NULL;
4150 struct yaffs_dev *dev;
4152 if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4156 if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4161 dev = old_dir->my_dev;
4163 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4164 /* Special case for case insemsitive systems.
4165 * While look-up is case insensitive, the name isn't.
4166 * Therefore we might want to change x.txt to X.txt
4168 if (old_dir == new_dir &&
4169 old_name && new_name &&
4170 strcmp(old_name, new_name) == 0)
4174 if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4175 YAFFS_MAX_NAME_LENGTH)
4180 obj = yaffs_find_by_name(old_dir, old_name);
4183 old_dir = obj->parent;
4186 if (obj && obj->rename_allowed) {
4187 /* Now handle an existing target, if there is one */
4188 existing_target = yaffs_find_by_name(new_dir, new_name);
4189 if (yaffs_is_non_empty_dir(existing_target)) {
4190 return YAFFS_FAIL; /* ENOTEMPTY */
4191 } else if (existing_target && existing_target != obj) {
4192 /* Nuke the target first, using shadowing,
4193 * but only if it isn't the same object.
4195 * Note we must disable gc here otherwise it can mess
4199 dev->gc_disable = 1;
4200 yaffs_change_obj_name(obj, new_dir, new_name, force,
4201 existing_target->obj_id);
4202 existing_target->is_shadowed = 1;
4203 yaffs_unlink_obj(existing_target);
4204 dev->gc_disable = 0;
4207 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4209 yaffs_update_parent(old_dir);
4210 if (new_dir != old_dir)
4211 yaffs_update_parent(new_dir);
4218 /*----------------------- Initialisation Scanning ---------------------- */
4220 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4221 int backward_scanning)
4223 struct yaffs_obj *obj;
4225 if (backward_scanning) {
4226 /* Handle YAFFS2 case (backward scanning)
4227 * If the shadowed object exists then ignore.
4229 obj = yaffs_find_by_number(dev, obj_id);
4234 /* Let's create it (if it does not exist) assuming it is a file so that
4235 * it can do shrinking etc.
4236 * We put it in unlinked dir to be cleaned up after the scanning
4239 yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4242 obj->is_shadowed = 1;
4243 yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4244 obj->variant.file_variant.shrink_size = 0;
4245 obj->valid = 1; /* So that we don't read any other info. */
4248 void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
4250 struct list_head *lh;
4251 struct list_head *save;
4252 struct yaffs_obj *hl;
4253 struct yaffs_obj *in;
4255 list_for_each_safe(lh, save, hard_list) {
4256 hl = list_entry(lh, struct yaffs_obj, hard_links);
4257 in = yaffs_find_by_number(dev,
4258 hl->variant.hardlink_variant.equiv_id);
4261 /* Add the hardlink pointers */
4262 hl->variant.hardlink_variant.equiv_obj = in;
4263 list_add(&hl->hard_links, &in->hard_links);
4265 /* Todo Need to report/handle this better.
4266 * Got a problem... hardlink to a non-existant object
4268 hl->variant.hardlink_variant.equiv_obj = NULL;
4269 INIT_LIST_HEAD(&hl->hard_links);
4274 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4277 * Sort out state of unlinked and deleted objects after scanning.
4279 struct list_head *i;
4280 struct list_head *n;
4281 struct yaffs_obj *l;
4286 /* Soft delete all the unlinked files */
4287 list_for_each_safe(i, n,
4288 &dev->unlinked_dir->variant.dir_variant.children) {
4289 l = list_entry(i, struct yaffs_obj, siblings);
4293 list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4294 l = list_entry(i, struct yaffs_obj, siblings);
4300 * This code iterates through all the objects making sure that they are rooted.
4301 * Any unrooted objects are re-rooted in lost+found.
4302 * An object needs to be in one of:
4303 * - Directly under deleted, unlinked
4304 * - Directly or indirectly under root.
4307 * This code assumes that we don't ever change the current relationships
4308 * between directories:
4309 * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4310 * lost-n-found->parent == root_dir
4312 * This fixes the problem where directories might have inadvertently been
4313 * deleted leaving the object "hanging" without being rooted in the
4317 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4319 return (obj == dev->del_dir ||
4320 obj == dev->unlinked_dir || obj == dev->root_dir);
4323 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4325 struct yaffs_obj *obj;
4326 struct yaffs_obj *parent;
4328 struct list_head *lh;
4329 struct list_head *n;
4336 /* Iterate through the objects in each hash entry,
4337 * looking at each object.
4338 * Make sure it is rooted.
4341 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4342 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4343 obj = list_entry(lh, struct yaffs_obj, hash_link);
4344 parent = obj->parent;
4346 if (yaffs_has_null_parent(dev, obj)) {
4347 /* These directories are not hanging */
4349 } else if (!parent ||
4350 parent->variant_type !=
4351 YAFFS_OBJECT_TYPE_DIRECTORY) {
4353 } else if (yaffs_has_null_parent(dev, parent)) {
4357 * Need to follow the parent chain to
4358 * see if it is hanging.
4363 while (parent != dev->root_dir &&
4365 parent->parent->variant_type ==
4366 YAFFS_OBJECT_TYPE_DIRECTORY &&
4368 parent = parent->parent;
4371 if (parent != dev->root_dir)
4375 yaffs_trace(YAFFS_TRACE_SCAN,
4376 "Hanging object %d moved to lost and found",
4378 yaffs_add_obj_to_dir(dev->lost_n_found, obj);
4385 * Delete directory contents for cleaning up lost and found.
4387 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4389 struct yaffs_obj *obj;
4390 struct list_head *lh;
4391 struct list_head *n;
4393 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4396 list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4397 obj = list_entry(lh, struct yaffs_obj, siblings);
4398 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4399 yaffs_del_dir_contents(obj);
4400 yaffs_trace(YAFFS_TRACE_SCAN,
4401 "Deleting lost_found object %d",
4403 yaffs_unlink_obj(obj);
4407 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4409 yaffs_del_dir_contents(dev->lost_n_found);
4413 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4417 struct list_head *i;
4418 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4419 struct yaffs_obj *l;
4425 yaffs_trace(YAFFS_TRACE_ALWAYS,
4426 "tragedy: yaffs_find_by_name: null pointer directory"
4431 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4432 yaffs_trace(YAFFS_TRACE_ALWAYS,
4433 "tragedy: yaffs_find_by_name: non-directory"
4438 sum = yaffs_calc_name_sum(name);
4440 list_for_each(i, &directory->variant.dir_variant.children) {
4441 l = list_entry(i, struct yaffs_obj, siblings);
4443 if (l->parent != directory)
4446 yaffs_check_obj_details_loaded(l);
4448 /* Special case for lost-n-found */
4449 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4450 if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4452 } else if (l->sum == sum || l->hdr_chunk <= 0) {
4453 /* LostnFound chunk called Objxxx
4456 yaffs_get_obj_name(l, buffer,
4457 YAFFS_MAX_NAME_LENGTH + 1);
4458 if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
4465 /* GetEquivalentObject dereferences any hard links to get to the
4469 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4471 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4472 obj = obj->variant.hardlink_variant.equiv_obj;
4473 yaffs_check_obj_details_loaded(obj);
4479 * A note or two on object names.
4480 * * If the object name is missing, we then make one up in the form objnnn
4482 * * ASCII names are stored in the object header's name field from byte zero
4483 * * Unicode names are historically stored starting from byte zero.
4485 * Then there are automatic Unicode names...
4486 * The purpose of these is to save names in a way that can be read as
4487 * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4488 * system to share files.
4490 * These automatic unicode are stored slightly differently...
4491 * - If the name can fit in the ASCII character space then they are saved as
4492 * ascii names as per above.
4493 * - If the name needs Unicode then the name is saved in Unicode
4494 * starting at oh->name[1].
4497 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
4500 /* Create an object name if we could not find one. */
4501 if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4502 YCHAR local_name[20];
4503 YCHAR num_string[20];
4504 YCHAR *x = &num_string[19];
4505 unsigned v = obj->obj_id;
4509 *x = '0' + (v % 10);
4512 /* make up a name */
4513 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4514 strcat(local_name, x);
4515 strncpy(name, local_name, buffer_size - 1);
4519 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
4521 memset(name, 0, buffer_size * sizeof(YCHAR));
4522 yaffs_check_obj_details_loaded(obj);
4523 if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4524 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4525 } else if (obj->short_name[0]) {
4526 strcpy(name, obj->short_name);
4527 } else if (obj->hdr_chunk > 0) {
4529 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4531 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4533 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4535 if (obj->hdr_chunk > 0) {
4536 result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4540 if (result == YAFFS_OK)
4541 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4544 yaffs_release_temp_buffer(obj->my_dev, buffer);
4547 yaffs_fix_null_name(obj, name, buffer_size);
4549 return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4552 loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
4554 /* Dereference any hard linking */
4555 obj = yaffs_get_equivalent_obj(obj);
4557 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4558 return obj->variant.file_variant.file_size;
4559 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4560 if (!obj->variant.symlink_variant.alias)
4562 return strnlen(obj->variant.symlink_variant.alias,
4563 YAFFS_MAX_ALIAS_LENGTH);
4565 /* Only a directory should drop through to here */
4566 return obj->my_dev->data_bytes_per_chunk;
4570 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4573 struct list_head *i;
4576 count++; /* the object itself */
4578 list_for_each(i, &obj->hard_links)
4579 count++; /* add the hard links; */
4584 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4586 obj = yaffs_get_equivalent_obj(obj);
4591 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4593 obj = yaffs_get_equivalent_obj(obj);
4595 switch (obj->variant_type) {
4596 case YAFFS_OBJECT_TYPE_FILE:
4599 case YAFFS_OBJECT_TYPE_DIRECTORY:
4602 case YAFFS_OBJECT_TYPE_SYMLINK:
4605 case YAFFS_OBJECT_TYPE_HARDLINK:
4608 case YAFFS_OBJECT_TYPE_SPECIAL:
4609 if (S_ISFIFO(obj->yst_mode))
4611 if (S_ISCHR(obj->yst_mode))
4613 if (S_ISBLK(obj->yst_mode))
4615 if (S_ISSOCK(obj->yst_mode))
4625 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4627 obj = yaffs_get_equivalent_obj(obj);
4628 if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4629 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4631 return yaffs_clone_str(_Y(""));
4634 /*--------------------------- Initialisation code -------------------------- */
4636 static int yaffs_check_dev_fns(struct yaffs_dev *dev)
4638 struct yaffs_driver *drv = &dev->drv;
4639 struct yaffs_tags_handler *tagger = &dev->tagger;
4641 /* Common functions, gotta have */
4642 if (!drv->drv_read_chunk_fn ||
4643 !drv->drv_write_chunk_fn ||
4647 if (dev->param.is_yaffs2 &&
4648 (!drv->drv_mark_bad_fn || !drv->drv_check_bad_fn))
4651 /* Install the default tags marshalling functions if needed. */
4652 yaffs_tags_compat_install(dev);
4653 yaffs_tags_marshall_install(dev);
4655 /* Check we now have the marshalling functions required. */
4656 if (!tagger->write_chunk_tags_fn ||
4657 !tagger->read_chunk_tags_fn ||
4658 !tagger->query_block_fn ||
4659 !tagger->mark_bad_fn)
4665 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4667 /* Initialise the unlinked, deleted, root and lost+found directories */
4668 dev->lost_n_found = NULL;
4669 dev->root_dir = NULL;
4670 dev->unlinked_dir = NULL;
4671 dev->del_dir = NULL;
4674 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4676 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4678 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4679 YAFFS_ROOT_MODE | S_IFDIR);
4681 yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4682 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4684 if (dev->lost_n_found &&
4686 dev->unlinked_dir &&
4688 /* If lost-n-found is hidden then yank it out of the directory tree. */
4689 if (dev->param.hide_lost_n_found)
4690 list_del_init(&dev->lost_n_found->siblings);
4692 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4699 * Typically only used by yaffs_guts_initialise, but also used by the
4700 * Low level yaffs driver tests.
4703 int yaffs_guts_ll_init(struct yaffs_dev *dev)
4707 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_ll_init()");
4710 yaffs_trace(YAFFS_TRACE_ALWAYS,
4711 "yaffs: Need a device"
4719 dev->internal_start_block = dev->param.start_block;
4720 dev->internal_end_block = dev->param.end_block;
4721 dev->block_offset = 0;
4722 dev->chunk_offset = 0;
4723 dev->n_free_chunks = 0;
4727 if (dev->param.start_block == 0) {
4728 dev->internal_start_block = dev->param.start_block + 1;
4729 dev->internal_end_block = dev->param.end_block + 1;
4730 dev->block_offset = 1;
4731 dev->chunk_offset = dev->param.chunks_per_block;
4734 /* Check geometry parameters. */
4736 if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4737 dev->param.total_bytes_per_chunk < 1024) ||
4738 (!dev->param.is_yaffs2 &&
4739 dev->param.total_bytes_per_chunk < 512) ||
4740 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4741 dev->param.chunks_per_block < 2 ||
4742 dev->param.n_reserved_blocks < 2 ||
4743 dev->internal_start_block <= 0 ||
4744 dev->internal_end_block <= 0 ||
4745 dev->internal_end_block <=
4746 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4748 /* otherwise it is too small */
4749 yaffs_trace(YAFFS_TRACE_ALWAYS,
4750 "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4751 dev->param.total_bytes_per_chunk,
4752 dev->param.is_yaffs2 ? "2" : "",
4753 dev->param.inband_tags);
4757 /* Sort out space for inband tags, if required */
4758 if (dev->param.inband_tags)
4759 dev->data_bytes_per_chunk =
4760 dev->param.total_bytes_per_chunk -
4761 sizeof(struct yaffs_packed_tags2_tags_only);
4763 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4765 /* Got the right mix of functions? */
4766 if (!yaffs_check_dev_fns(dev)) {
4767 /* Function missing */
4768 yaffs_trace(YAFFS_TRACE_ALWAYS,
4769 "device function(s) missing or wrong");
4774 if (yaffs_init_nand(dev) != YAFFS_OK) {
4775 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4783 int yaffs_guts_format_dev(struct yaffs_dev *dev)
4786 enum yaffs_block_state state;
4789 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4795 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
4796 yaffs_query_init_block_state(dev, i, &state, &dummy);
4797 if (state != YAFFS_BLOCK_STATE_DEAD)
4798 yaffs_erase_block(dev, i);
4805 int yaffs_guts_initialise(struct yaffs_dev *dev)
4807 int init_failed = 0;
4811 if(yaffs_guts_ll_init(dev) != YAFFS_OK)
4814 if (dev->is_mounted) {
4815 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4819 dev->is_mounted = 1;
4821 /* OK now calculate a few things for the device */
4824 * Calculate all the chunk size manipulation numbers:
4826 x = dev->data_bytes_per_chunk;
4827 /* We always use dev->chunk_shift and dev->chunk_div */
4828 dev->chunk_shift = calc_shifts(x);
4829 x >>= dev->chunk_shift;
4831 /* We only use chunk mask if chunk_div is 1 */
4832 dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4835 * Calculate chunk_grp_bits.
4836 * We need to find the next power of 2 > than internal_end_block
4839 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4841 bits = calc_shifts_ceiling(x);
4843 /* Set up tnode width if wide tnodes are enabled. */
4844 if (!dev->param.wide_tnodes_disabled) {
4845 /* bits must be even so that we end up with 32-bit words */
4849 dev->tnode_width = 16;
4851 dev->tnode_width = bits;
4853 dev->tnode_width = 16;
4856 dev->tnode_mask = (1 << dev->tnode_width) - 1;
4858 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4859 * so if the bitwidth of the
4860 * chunk range we're using is greater than 16 we need
4861 * to figure out chunk shift and chunk_grp_size
4864 if (bits <= dev->tnode_width)
4865 dev->chunk_grp_bits = 0;
4867 dev->chunk_grp_bits = bits - dev->tnode_width;
4869 dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4870 if (dev->tnode_size < sizeof(struct yaffs_tnode))
4871 dev->tnode_size = sizeof(struct yaffs_tnode);
4873 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4875 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4876 /* We have a problem because the soft delete won't work if
4877 * the chunk group size > chunks per block.
4878 * This can be remedied by using larger "virtual blocks".
4880 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4885 /* Finished verifying the device, continue with initialisation */
4887 /* More device initialisation */
4889 dev->passive_gc_count = 0;
4890 dev->oldest_dirty_gc_count = 0;
4892 dev->gc_block_finder = 0;
4893 dev->buffered_block = -1;
4894 dev->doing_buffered_block_rewrite = 0;
4895 dev->n_deleted_files = 0;
4896 dev->n_bg_deletions = 0;
4897 dev->n_unlinked_files = 0;
4898 dev->n_ecc_fixed = 0;
4899 dev->n_ecc_unfixed = 0;
4900 dev->n_tags_ecc_fixed = 0;
4901 dev->n_tags_ecc_unfixed = 0;
4902 dev->n_erase_failures = 0;
4903 dev->n_erased_blocks = 0;
4904 dev->gc_disable = 0;
4905 dev->has_pending_prioritised_gc = 1; /* Assume the worst for now,
4906 * will get fixed on first GC */
4907 INIT_LIST_HEAD(&dev->dirty_dirs);
4908 dev->oldest_dirty_seq = 0;
4909 dev->oldest_dirty_block = 0;
4911 yaffs_endian_config(dev);
4913 /* Initialise temporary buffers and caches. */
4914 if (!yaffs_init_tmp_buffers(dev))
4918 dev->gc_cleanup_list = NULL;
4920 if (!init_failed && dev->param.n_caches > 0) {
4924 dev->param.n_caches * sizeof(struct yaffs_cache);
4926 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4927 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4929 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4931 buf = (u8 *) dev->cache;
4934 memset(dev->cache, 0, cache_bytes);
4936 for (i = 0; i < dev->param.n_caches && buf; i++) {
4937 dev->cache[i].object = NULL;
4938 dev->cache[i].last_use = 0;
4939 dev->cache[i].dirty = 0;
4940 dev->cache[i].data = buf =
4941 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4946 dev->cache_last_use = 0;
4949 dev->cache_hits = 0;
4952 dev->gc_cleanup_list =
4953 kmalloc(dev->param.chunks_per_block * sizeof(u32),
4955 if (!dev->gc_cleanup_list)
4959 if (dev->param.is_yaffs2)
4960 dev->param.use_header_file_size = 1;
4962 if (!init_failed && !yaffs_init_blocks(dev))
4965 yaffs_init_tnodes_and_objs(dev);
4967 if (!init_failed && !yaffs_create_initial_dir(dev))
4970 if (!init_failed && dev->param.is_yaffs2 &&
4971 !dev->param.disable_summary &&
4972 !yaffs_summary_init(dev))
4976 /* Now scan the flash. */
4977 if (dev->param.is_yaffs2) {
4978 if (yaffs2_checkpt_restore(dev)) {
4979 yaffs_check_obj_details_loaded(dev->root_dir);
4980 yaffs_trace(YAFFS_TRACE_CHECKPOINT |
4982 "yaffs: restored from checkpoint"
4986 /* Clean up the mess caused by an aborted
4987 * checkpoint load then scan backwards.
4989 yaffs_deinit_blocks(dev);
4991 yaffs_deinit_tnodes_and_objs(dev);
4993 dev->n_erased_blocks = 0;
4994 dev->n_free_chunks = 0;
4995 dev->alloc_block = -1;
4996 dev->alloc_page = -1;
4997 dev->n_deleted_files = 0;
4998 dev->n_unlinked_files = 0;
4999 dev->n_bg_deletions = 0;
5001 if (!init_failed && !yaffs_init_blocks(dev))
5004 yaffs_init_tnodes_and_objs(dev);
5007 && !yaffs_create_initial_dir(dev))
5010 if (!init_failed && !yaffs2_scan_backwards(dev))
5013 } else if (!yaffs1_scan(dev)) {
5017 yaffs_strip_deleted_objs(dev);
5018 yaffs_fix_hanging_objs(dev);
5019 if (dev->param.empty_lost_n_found)
5020 yaffs_empty_l_n_f(dev);
5024 /* Clean up the mess */
5025 yaffs_trace(YAFFS_TRACE_TRACING,
5026 "yaffs: yaffs_guts_initialise() aborted.");
5028 yaffs_deinitialise(dev);
5032 /* Zero out stats */
5033 dev->n_page_reads = 0;
5034 dev->n_page_writes = 0;
5035 dev->n_erasures = 0;
5036 dev->n_gc_copies = 0;
5037 dev->n_retried_writes = 0;
5039 dev->n_retired_blocks = 0;
5041 yaffs_verify_free_chunks(dev);
5042 yaffs_verify_blocks(dev);
5044 /* Clean up any aborted checkpoint data */
5045 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5046 yaffs2_checkpt_invalidate(dev);
5048 yaffs_trace(YAFFS_TRACE_TRACING,
5049 "yaffs: yaffs_guts_initialise() done.");
5053 void yaffs_deinitialise(struct yaffs_dev *dev)
5055 if (dev->is_mounted) {
5058 yaffs_deinit_blocks(dev);
5059 yaffs_deinit_tnodes_and_objs(dev);
5060 yaffs_summary_deinit(dev);
5062 if (dev->param.n_caches > 0 && dev->cache) {
5064 for (i = 0; i < dev->param.n_caches; i++) {
5065 kfree(dev->cache[i].data);
5066 dev->cache[i].data = NULL;
5073 kfree(dev->gc_cleanup_list);
5075 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
5076 kfree(dev->temp_buffer[i].buffer);
5077 dev->temp_buffer[i].buffer = NULL;
5080 kfree(dev->checkpt_buffer);
5081 dev->checkpt_buffer = NULL;
5082 kfree(dev->checkpt_block_list);
5083 dev->checkpt_block_list = NULL;
5085 dev->is_mounted = 0;
5087 yaffs_deinit_nand(dev);
5091 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5095 struct yaffs_block_info *blk;
5097 blk = dev->block_info;
5098 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5099 switch (blk->block_state) {
5100 case YAFFS_BLOCK_STATE_EMPTY:
5101 case YAFFS_BLOCK_STATE_ALLOCATING:
5102 case YAFFS_BLOCK_STATE_COLLECTING:
5103 case YAFFS_BLOCK_STATE_FULL:
5105 (dev->param.chunks_per_block - blk->pages_in_use +
5106 blk->soft_del_pages);
5116 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5118 /* This is what we report to the outside world */
5121 int blocks_for_checkpt;
5124 n_free = dev->n_free_chunks;
5125 n_free += dev->n_deleted_files;
5127 /* Now count and subtract the number of dirty chunks in the cache. */
5129 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5130 if (dev->cache[i].dirty)
5134 n_free -= n_dirty_caches;
5137 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5139 /* Now figure checkpoint space and report that... */
5140 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5142 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
5152 * Marshalling functions to get loff_t file sizes into and out of
5155 void yaffs_oh_size_load(struct yaffs_dev *dev,
5156 struct yaffs_obj_hdr *oh,
5160 oh->file_size_low = FSIZE_LOW(fsize);
5162 oh->file_size_high = FSIZE_HIGH(fsize);
5165 yaffs_do_endian_u32(dev, &oh->file_size_low);
5166 yaffs_do_endian_u32(dev, &oh->file_size_high);
5170 loff_t yaffs_oh_to_size(struct yaffs_dev *dev, struct yaffs_obj_hdr *oh,
5176 if (sizeof(loff_t) >= 8 && ~(oh->file_size_high)) {
5177 u32 low = oh->file_size_low;
5178 u32 high = oh->file_size_high;
5181 yaffs_do_endian_u32 (dev, &low);
5182 yaffs_do_endian_u32 (dev, &high);
5184 retval = FSIZE_COMBINE(high, low);
5186 u32 low = oh->file_size_low;
5189 yaffs_do_endian_u32(dev, &low);
5190 retval = (loff_t)low;
5197 void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
5200 struct yaffs_block_info *bi;
5203 for(i = 0; i < 10; i++)
5206 for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
5207 bi = yaffs_get_block_info(dev, i);
5208 s = bi->block_state;
5209 if(s > YAFFS_BLOCK_STATE_DEAD || s < YAFFS_BLOCK_STATE_UNKNOWN)