eea828a480e8f7e48927ff9b1f3a2dad60711610
[yaffs2.git] / yaffs_guts.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include "yportenv.h"
15 #include "yaffs_trace.h"
16
17 #include "yaffs_guts.h"
18 #include "yaffs_tagsvalidity.h"
19 #include "yaffs_getblockinfo.h"
20
21 #include "yaffs_tagscompat.h"
22
23 #include "yaffs_nand.h"
24
25 #include "yaffs_yaffs1.h"
26 #include "yaffs_yaffs2.h"
27 #include "yaffs_bitmap.h"
28 #include "yaffs_verify.h"
29
30 #include "yaffs_nand.h"
31 #include "yaffs_packedtags2.h"
32
33 #include "yaffs_nameval.h"
34 #include "yaffs_allocator.h"
35
36 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
37 #define YAFFS_GC_GOOD_ENOUGH 2
38 #define YAFFS_GC_PASSIVE_THRESHOLD 4
39
40 #include "yaffs_ecc.h"
41
42
43
44 /* Robustification (if it ever comes about...) */
45 static void yaffs_retire_block(yaffs_dev_t *dev, int flash_block);
46 static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk,
47                 int erased_ok);
48 static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
49                                 const __u8 *data,
50                                 const yaffs_ext_tags *tags);
51 static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
52                                 const yaffs_ext_tags *tags);
53
54 /* Other local prototypes */
55 static void yaffs_update_parent(yaffs_obj_t *obj);
56 static int yaffs_unlink_obj(yaffs_obj_t *obj);
57 static int yaffs_obj_cache_dirty(yaffs_obj_t *obj);
58
59 static int yaffs_write_new_chunk(yaffs_dev_t *dev,
60                                         const __u8 *buffer,
61                                         yaffs_ext_tags *tags,
62                                         int use_reserver);
63
64
65 static yaffs_obj_t *yaffs_new_obj(yaffs_dev_t *dev, int number,
66                                         yaffs_obj_type type);
67
68
69 static int yaffs_apply_xattrib_mod(yaffs_obj_t *obj, char *buffer, yaffs_xattr_mod *xmod);
70
71 static void yaffs_remove_obj_from_dir(yaffs_obj_t *obj);
72 static int yaffs_check_structures(void);
73 static int yaffs_generic_obj_del(yaffs_obj_t *in);
74
75 static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
76                                 int nand_chunk);
77
78 static int yaffs_unlink_worker(yaffs_obj_t *obj);
79
80 static int yaffs_tags_match(const yaffs_ext_tags *tags, int obj_id,
81                         int chunk_obj);
82
83 static int yaffs_alloc_chunk(yaffs_dev_t *dev, int use_reserver,
84                                 yaffs_block_info_t **block_ptr);
85
86 static void yaffs_check_obj_details_loaded(yaffs_obj_t *in);
87
88 static void yaffs_invalidate_whole_cache(yaffs_obj_t *in);
89 static void yaffs_invalidate_chunk_cache(yaffs_obj_t *object, int chunk_id);
90
91 static int yaffs_find_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
92                                 yaffs_ext_tags *tags);
93
94 static int yaffs_verify_chunk_written(yaffs_dev_t *dev,
95                                         int nand_chunk,
96                                         const __u8 *data,
97                                         yaffs_ext_tags *tags);
98
99
100 static void yaffs_load_name_from_oh(yaffs_dev_t *dev,YCHAR *name, const YCHAR *oh_name, int buff_size);
101 static void yaffs_load_oh_from_name(yaffs_dev_t *dev,YCHAR *oh_name, const YCHAR *name);
102
103
104 /* Function to calculate chunk and offset */
105
106 static void yaffs_addr_to_chunk(yaffs_dev_t *dev, loff_t addr, int *chunk_out,
107                 __u32 *offset_out)
108 {
109         int chunk;
110         __u32 offset;
111
112         chunk  = (__u32)(addr >> dev->chunk_shift);
113
114         if (dev->chunk_div == 1) {
115                 /* easy power of 2 case */
116                 offset = (__u32)(addr & dev->chunk_mask);
117         } else {
118                 /* Non power-of-2 case */
119
120                 loff_t chunk_base;
121
122                 chunk /= dev->chunk_div;
123
124                 chunk_base = ((loff_t)chunk) * dev->data_bytes_per_chunk;
125                 offset = (__u32)(addr - chunk_base);
126         }
127
128         *chunk_out = chunk;
129         *offset_out = offset;
130 }
131
132 /* Function to return the number of shifts for a power of 2 greater than or
133  * equal to the given number
134  * Note we don't try to cater for all possible numbers and this does not have to
135  * be hellishly efficient.
136  */
137
138 static __u32 calc_shifts_ceiling(__u32 x)
139 {
140         int extra_bits;
141         int shifts;
142
143         shifts = extra_bits = 0;
144
145         while (x > 1) {
146                 if (x & 1)
147                         extra_bits++;
148                 x >>= 1;
149                 shifts++;
150         }
151
152         if (extra_bits)
153                 shifts++;
154
155         return shifts;
156 }
157
158 /* Function to return the number of shifts to get a 1 in bit 0
159  */
160
161 static __u32 calc_shifts(__u32 x)
162 {
163         __u32 shifts;
164
165         shifts =  0;
166
167         if (!x)
168                 return 0;
169
170         while (!(x&1)) {
171                 x >>= 1;
172                 shifts++;
173         }
174
175         return shifts;
176 }
177
178
179
180 /*
181  * Temporary buffer manipulations.
182  */
183
184 static int yaffs_init_tmp_buffers(yaffs_dev_t *dev)
185 {
186         int i;
187         __u8 *buf = (__u8 *)1;
188
189         memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
190
191         for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
192                 dev->temp_buffer[i].line = 0;   /* not in use */
193                 dev->temp_buffer[i].buffer = buf =
194                     YMALLOC_DMA(dev->param.total_bytes_per_chunk);
195         }
196
197         return buf ? YAFFS_OK : YAFFS_FAIL;
198 }
199
200 __u8 *yaffs_get_temp_buffer(yaffs_dev_t *dev, int line_no)
201 {
202         int i, j;
203
204         dev->temp_in_use++;
205         if (dev->temp_in_use > dev->max_temp)
206                 dev->max_temp = dev->temp_in_use;
207
208         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
209                 if (dev->temp_buffer[i].line == 0) {
210                         dev->temp_buffer[i].line = line_no;
211                         if ((i + 1) > dev->max_temp) {
212                                 dev->max_temp = i + 1;
213                                 for (j = 0; j <= i; j++)
214                                         dev->temp_buffer[j].max_line =
215                                             dev->temp_buffer[j].line;
216                         }
217
218                         return dev->temp_buffer[i].buffer;
219                 }
220         }
221
222         T(YAFFS_TRACE_BUFFERS,
223           (TSTR("Out of temp buffers at line %d, other held by lines:"),
224            line_no));
225         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
226                 T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->temp_buffer[i].line));
227
228         T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
229
230         /*
231          * If we got here then we have to allocate an unmanaged one
232          * This is not good.
233          */
234
235         dev->unmanaged_buffer_allocs++;
236         return YMALLOC(dev->data_bytes_per_chunk);
237
238 }
239
240 void yaffs_release_temp_buffer(yaffs_dev_t *dev, __u8 *buffer,
241                                     int line_no)
242 {
243         int i;
244
245         dev->temp_in_use--;
246
247         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
248                 if (dev->temp_buffer[i].buffer == buffer) {
249                         dev->temp_buffer[i].line = 0;
250                         return;
251                 }
252         }
253
254         if (buffer) {
255                 /* assume it is an unmanaged one. */
256                 T(YAFFS_TRACE_BUFFERS,
257                   (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
258                    line_no));
259                 YFREE(buffer);
260                 dev->unmanaged_buffer_deallocs++;
261         }
262
263 }
264
265 /*
266  * Determine if we have a managed buffer.
267  */
268 int yaffs_is_managed_tmp_buffer(yaffs_dev_t *dev, const __u8 *buffer)
269 {
270         int i;
271
272         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
273                 if (dev->temp_buffer[i].buffer == buffer)
274                         return 1;
275         }
276
277         for (i = 0; i < dev->param.n_caches; i++) {
278                 if (dev->cache[i].data == buffer)
279                         return 1;
280         }
281
282         if (buffer == dev->checkpt_buffer)
283                 return 1;
284
285         T(YAFFS_TRACE_ALWAYS,
286                 (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
287         return 0;
288 }
289
290 /*
291  * Verification code
292  */
293
294
295
296
297 /*
298  *  Simple hash function. Needs to have a reasonable spread
299  */
300
301 static Y_INLINE int yaffs_hash_fn(int n)
302 {
303         n = abs(n);
304         return n % YAFFS_NOBJECT_BUCKETS;
305 }
306
307 /*
308  * Access functions to useful fake objects.
309  * Note that root might have a presence in NAND if permissions are set.
310  */
311
312 yaffs_obj_t *yaffs_root(yaffs_dev_t *dev)
313 {
314         return dev->root_dir;
315 }
316
317 yaffs_obj_t *yaffs_lost_n_found(yaffs_dev_t *dev)
318 {
319         return dev->lost_n_found;
320 }
321
322
323 /*
324  *  Erased NAND checking functions
325  */
326
327 int yaffs_check_ff(__u8 *buffer, int n_bytes)
328 {
329         /* Horrible, slow implementation */
330         while (n_bytes--) {
331                 if (*buffer != 0xFF)
332                         return 0;
333                 buffer++;
334         }
335         return 1;
336 }
337
338 static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
339                                 int nand_chunk)
340 {
341         int retval = YAFFS_OK;
342         __u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
343         yaffs_ext_tags tags;
344         int result;
345
346         result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
347
348         if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
349                 retval = YAFFS_FAIL;
350
351         if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
352                 T(YAFFS_TRACE_NANDACCESS,
353                   (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
354                 retval = YAFFS_FAIL;
355         }
356
357         yaffs_release_temp_buffer(dev, data, __LINE__);
358
359         return retval;
360
361 }
362
363
364 static int yaffs_verify_chunk_written(yaffs_dev_t *dev,
365                                         int nand_chunk,
366                                         const __u8 *data,
367                                         yaffs_ext_tags *tags)
368 {
369         int retval = YAFFS_OK;
370         yaffs_ext_tags temp_tags;
371         __u8 *buffer = yaffs_get_temp_buffer(dev,__LINE__);
372         int result;
373         
374         result = yaffs_rd_chunk_tags_nand(dev,nand_chunk,buffer,&temp_tags);
375         if(memcmp(buffer,data,dev->data_bytes_per_chunk) ||
376                 temp_tags.obj_id != tags->obj_id ||
377                 temp_tags.chunk_id  != tags->chunk_id ||
378                 temp_tags.n_bytes != tags->n_bytes)
379                 retval = YAFFS_FAIL;
380
381         yaffs_release_temp_buffer(dev, buffer, __LINE__);
382
383         return retval;
384 }
385
386 static int yaffs_write_new_chunk(struct yaffs_dev_s *dev,
387                                         const __u8 *data,
388                                         yaffs_ext_tags *tags,
389                                         int use_reserver)
390 {
391         int attempts = 0;
392         int write_ok = 0;
393         int chunk;
394
395         yaffs2_checkpt_invalidate(dev);
396
397         do {
398                 yaffs_block_info_t *bi = 0;
399                 int erased_ok = 0;
400
401                 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
402                 if (chunk < 0) {
403                         /* no space */
404                         break;
405                 }
406
407                 /* First check this chunk is erased, if it needs
408                  * checking.  The checking policy (unless forced
409                  * always on) is as follows:
410                  *
411                  * Check the first page we try to write in a block.
412                  * If the check passes then we don't need to check any
413                  * more.        If the check fails, we check again...
414                  * If the block has been erased, we don't need to check.
415                  *
416                  * However, if the block has been prioritised for gc,
417                  * then we think there might be something odd about
418                  * this block and stop using it.
419                  *
420                  * Rationale: We should only ever see chunks that have
421                  * not been erased if there was a partially written
422                  * chunk due to power loss.  This checking policy should
423                  * catch that case with very few checks and thus save a
424                  * lot of checks that are most likely not needed.
425                  *
426                  * Mods to the above
427                  * If an erase check fails or the write fails we skip the 
428                  * rest of the block.
429                  */
430
431                 /* let's give it a try */
432                 attempts++;
433
434                 if(dev->param.always_check_erased)
435                         bi->skip_erased_check = 0;
436
437                 if (!bi->skip_erased_check) {
438                         erased_ok = yaffs_check_chunk_erased(dev, chunk);
439                         if (erased_ok != YAFFS_OK) {
440                                 T(YAFFS_TRACE_ERROR,
441                                 (TSTR("**>> yaffs chunk %d was not erased"
442                                 TENDSTR), chunk));
443
444                                 /* If not erased, delete this one,
445                                  * skip rest of block and
446                                  * try another chunk */
447                                  yaffs_chunk_del(dev,chunk,1,__LINE__);
448                                  yaffs_skip_rest_of_block(dev);
449                                 continue;
450                         }
451                 }
452
453                 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk,
454                                 data, tags);
455
456                 if(!bi->skip_erased_check)
457                         write_ok = yaffs_verify_chunk_written(dev, chunk, data, tags);
458
459                 if (write_ok != YAFFS_OK) {
460                         /* Clean up aborted write, skip to next block and
461                          * try another chunk */
462                         yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
463                         continue;
464                 }
465
466                 bi->skip_erased_check = 1;
467
468                 /* Copy the data into the robustification buffer */
469                 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
470
471         } while (write_ok != YAFFS_OK &&
472                 (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
473
474         if (!write_ok)
475                 chunk = -1;
476
477         if (attempts > 1) {
478                 T(YAFFS_TRACE_ERROR,
479                         (TSTR("**>> yaffs write required %d attempts" TENDSTR),
480                         attempts));
481
482                 dev->n_retired_writes += (attempts - 1);
483         }
484
485         return chunk;
486 }
487
488
489  
490 /*
491  * Block retiring for handling a broken block.
492  */
493
494 static void yaffs_retire_block(yaffs_dev_t *dev, int flash_block)
495 {
496         yaffs_block_info_t *bi = yaffs_get_block_info(dev, flash_block);
497
498         yaffs2_checkpt_invalidate(dev);
499         
500         yaffs2_clear_oldest_dirty_seq(dev,bi);
501
502         if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
503                 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
504                         T(YAFFS_TRACE_ALWAYS, (TSTR(
505                                 "yaffs: Failed to mark bad and erase block %d"
506                                 TENDSTR), flash_block));
507                 } else {
508                         yaffs_ext_tags tags;
509                         int chunk_id = flash_block * dev->param.chunks_per_block;
510
511                         __u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
512
513                         memset(buffer, 0xff, dev->data_bytes_per_chunk);
514                         yaffs_init_tags(&tags);
515                         tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
516                         if (dev->param.write_chunk_tags_fn(dev, chunk_id -
517                                 dev->chunk_offset, buffer, &tags) != YAFFS_OK)
518                                 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
519                                         TCONT("write bad block marker to block %d")
520                                         TENDSTR), flash_block));
521
522                         yaffs_release_temp_buffer(dev, buffer, __LINE__);
523                 }
524         }
525
526         bi->block_state = YAFFS_BLOCK_STATE_DEAD;
527         bi->gc_prioritise = 0;
528         bi->needs_retiring = 0;
529
530         dev->n_retired_blocks++;
531 }
532
533 /*
534  * Functions for robustisizing TODO
535  *
536  */
537
538 static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
539                                 const __u8 *data,
540                                 const yaffs_ext_tags *tags)
541 {
542         dev=dev;
543         nand_chunk=nand_chunk;
544         data=data;
545         tags=tags;
546 }
547
548 static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
549                                 const yaffs_ext_tags *tags)
550 {
551         dev=dev;
552         nand_chunk=nand_chunk;
553         tags=tags;
554 }
555
556 void yaffs_handle_chunk_error(yaffs_dev_t *dev, yaffs_block_info_t *bi)
557 {
558         if (!bi->gc_prioritise) {
559                 bi->gc_prioritise = 1;
560                 dev->has_pending_prioritised_gc = 1;
561                 bi->chunk_error_strikes++;
562
563                 if (bi->chunk_error_strikes > 3) {
564                         bi->needs_retiring = 1; /* Too many stikes, so retire this */
565                         T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
566
567                 }
568         }
569 }
570
571 static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk,
572                 int erased_ok)
573 {
574         int flash_block = nand_chunk / dev->param.chunks_per_block;
575         yaffs_block_info_t *bi = yaffs_get_block_info(dev, flash_block);
576
577         yaffs_handle_chunk_error(dev, bi);
578
579         if (erased_ok) {
580                 /* Was an actual write failure, so mark the block for retirement  */
581                 bi->needs_retiring = 1;
582                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
583                   (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
584         }
585
586         /* Delete the chunk */
587         yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
588         yaffs_skip_rest_of_block(dev);
589 }
590
591
592 /*---------------- Name handling functions ------------*/
593
594 static __u16 yaffs_calc_name_sum(const YCHAR *name)
595 {
596         __u16 sum = 0;
597         __u16 i = 1;
598
599         const YUCHAR *bname = (const YUCHAR *) name;
600         if (bname) {
601                 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
602
603 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
604                         sum += yaffs_toupper(*bname) * i;
605 #else
606                         sum += (*bname) * i;
607 #endif
608                         i++;
609                         bname++;
610                 }
611         }
612         return sum;
613 }
614
615 void yaffs_set_obj_name(yaffs_obj_t *obj, const YCHAR *name)
616 {
617 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
618         memset(obj->short_name, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
619         if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
620                 yaffs_strcpy(obj->short_name, name);
621         else
622                 obj->short_name[0] = _Y('\0');
623 #endif
624         obj->sum = yaffs_calc_name_sum(name);
625 }
626
627 void yaffs_set_obj_name_from_oh(yaffs_obj_t *obj, const yaffs_obj_header *oh)
628 {
629 #ifdef CONFIG_YAFFS_AUTO_UNICODE
630         YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH+1];
631         memset(tmp_name,0,sizeof(tmp_name));
632         yaffs_load_name_from_oh(obj->my_dev,tmp_name,oh->name,YAFFS_MAX_NAME_LENGTH+1);
633         yaffs_set_obj_name(obj,tmp_name);
634 #else
635         yaffs_set_obj_name(obj,oh->name);
636 #endif
637 }
638
639 /*-------------------- TNODES -------------------
640
641  * List of spare tnodes
642  * The list is hooked together using the first pointer
643  * in the tnode.
644  */
645
646
647 yaffs_tnode_t *yaffs_get_tnode(yaffs_dev_t *dev)
648 {
649         yaffs_tnode_t *tn = yaffs_alloc_raw_tnode(dev);
650         if (tn){
651                 memset(tn, 0, dev->tnode_size);
652                 dev->n_tnodes++;
653         }
654
655         dev->checkpoint_blocks_required = 0; /* force recalculation*/
656
657         return tn;
658 }
659
660 /* FreeTnode frees up a tnode and puts it back on the free list */
661 static void yaffs_free_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
662 {
663         yaffs_free_raw_tnode(dev,tn);
664         dev->n_tnodes--;
665         dev->checkpoint_blocks_required = 0; /* force recalculation*/
666 }
667
668 static void yaffs_deinit_tnodes_and_objs(yaffs_dev_t *dev)
669 {
670         yaffs_deinit_raw_tnodes_and_objs(dev);
671         dev->n_obj = 0;
672         dev->n_tnodes = 0;
673 }
674
675
676 void yaffs_load_tnode_0(yaffs_dev_t *dev, yaffs_tnode_t *tn, unsigned pos,
677                 unsigned val)
678 {
679         __u32 *map = (__u32 *)tn;
680         __u32 bit_in_map;
681         __u32 bit_in_word;
682         __u32 word_in_map;
683         __u32 mask;
684
685         pos &= YAFFS_TNODES_LEVEL0_MASK;
686         val >>= dev->chunk_grp_bits;
687
688         bit_in_map = pos * dev->tnode_width;
689         word_in_map = bit_in_map / 32;
690         bit_in_word = bit_in_map & (32 - 1);
691
692         mask = dev->tnode_mask << bit_in_word;
693
694         map[word_in_map] &= ~mask;
695         map[word_in_map] |= (mask & (val << bit_in_word));
696
697         if (dev->tnode_width > (32 - bit_in_word)) {
698                 bit_in_word = (32 - bit_in_word);
699                 word_in_map++;;
700                 mask = dev->tnode_mask >> (/*dev->tnode_width -*/ bit_in_word);
701                 map[word_in_map] &= ~mask;
702                 map[word_in_map] |= (mask & (val >> bit_in_word));
703         }
704 }
705
706 __u32 yaffs_get_group_base(yaffs_dev_t *dev, yaffs_tnode_t *tn,
707                 unsigned pos)
708 {
709         __u32 *map = (__u32 *)tn;
710         __u32 bit_in_map;
711         __u32 bit_in_word;
712         __u32 word_in_map;
713         __u32 val;
714
715         pos &= YAFFS_TNODES_LEVEL0_MASK;
716
717         bit_in_map = pos * dev->tnode_width;
718         word_in_map = bit_in_map / 32;
719         bit_in_word = bit_in_map & (32 - 1);
720
721         val = map[word_in_map] >> bit_in_word;
722
723         if      (dev->tnode_width > (32 - bit_in_word)) {
724                 bit_in_word = (32 - bit_in_word);
725                 word_in_map++;;
726                 val |= (map[word_in_map] << bit_in_word);
727         }
728
729         val &= dev->tnode_mask;
730         val <<= dev->chunk_grp_bits;
731
732         return val;
733 }
734
735 /* ------------------- End of individual tnode manipulation -----------------*/
736
737 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
738  * The look up tree is represented by the top tnode and the number of top_level
739  * in the tree. 0 means only the level 0 tnode is in the tree.
740  */
741
742 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
743 yaffs_tnode_t *yaffs_find_tnode_0(yaffs_dev_t *dev,
744                                         yaffs_file_s *file_struct,
745                                         __u32 chunk_id)
746 {
747         yaffs_tnode_t *tn = file_struct->top;
748         __u32 i;
749         int required_depth;
750         int level = file_struct->top_level;
751
752         dev=dev;
753
754         /* Check sane level and chunk Id */
755         if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
756                 return NULL;
757
758         if (chunk_id > YAFFS_MAX_CHUNK_ID)
759                 return NULL;
760
761         /* First check we're tall enough (ie enough top_level) */
762
763         i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
764         required_depth = 0;
765         while (i) {
766                 i >>= YAFFS_TNODES_INTERNAL_BITS;
767                 required_depth++;
768         }
769
770         if (required_depth > file_struct->top_level)
771                 return NULL; /* Not tall enough, so we can't find it */
772
773         /* Traverse down to level 0 */
774         while (level > 0 && tn) {
775                 tn = tn->internal[(chunk_id >>
776                         (YAFFS_TNODES_LEVEL0_BITS +
777                                 (level - 1) *
778                                 YAFFS_TNODES_INTERNAL_BITS)) &
779                         YAFFS_TNODES_INTERNAL_MASK];
780                 level--;
781         }
782
783         return tn;
784 }
785
786 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
787  * This happens in two steps:
788  *  1. If the tree isn't tall enough, then make it taller.
789  *  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
790  *
791  * Used when modifying the tree.
792  *
793  *  If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
794  *  be plugged into the ttree.
795  */
796
797 yaffs_tnode_t *yaffs_add_find_tnode_0(yaffs_dev_t *dev,
798                                         yaffs_file_s *file_struct,
799                                         __u32 chunk_id,
800                                         yaffs_tnode_t *passed_tn)
801 {
802         int required_depth;
803         int i;
804         int l;
805         yaffs_tnode_t *tn;
806
807         __u32 x;
808
809
810         /* Check sane level and page Id */
811         if (file_struct->top_level < 0 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
812                 return NULL;
813
814         if (chunk_id > YAFFS_MAX_CHUNK_ID)
815                 return NULL;
816
817         /* First check we're tall enough (ie enough top_level) */
818
819         x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
820         required_depth = 0;
821         while (x) {
822                 x >>= YAFFS_TNODES_INTERNAL_BITS;
823                 required_depth++;
824         }
825
826
827         if (required_depth > file_struct->top_level) {
828                 /* Not tall enough, gotta make the tree taller */
829                 for (i = file_struct->top_level; i < required_depth; i++) {
830
831                         tn = yaffs_get_tnode(dev);
832
833                         if (tn) {
834                                 tn->internal[0] = file_struct->top;
835                                 file_struct->top = tn;
836                                 file_struct->top_level++;
837                         } else {
838                                 T(YAFFS_TRACE_ERROR,
839                                         (TSTR("yaffs: no more tnodes" TENDSTR)));
840                                 return NULL;
841                         }
842                 }
843         }
844
845         /* Traverse down to level 0, adding anything we need */
846
847         l = file_struct->top_level;
848         tn = file_struct->top;
849
850         if (l > 0) {
851                 while (l > 0 && tn) {
852                         x = (chunk_id >>
853                              (YAFFS_TNODES_LEVEL0_BITS +
854                               (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
855                             YAFFS_TNODES_INTERNAL_MASK;
856
857
858                         if ((l > 1) && !tn->internal[x]) {
859                                 /* Add missing non-level-zero tnode */
860                                 tn->internal[x] = yaffs_get_tnode(dev);
861                                 if(!tn->internal[x])
862                                         return NULL;
863                         } else if (l == 1) {
864                                 /* Looking from level 1 at level 0 */
865                                 if (passed_tn) {
866                                         /* If we already have one, then release it.*/
867                                         if (tn->internal[x])
868                                                 yaffs_free_tnode(dev, tn->internal[x]);
869                                         tn->internal[x] = passed_tn;
870
871                                 } else if (!tn->internal[x]) {
872                                         /* Don't have one, none passed in */
873                                         tn->internal[x] = yaffs_get_tnode(dev);
874                                         if(!tn->internal[x])
875                                                 return NULL;
876                                 }
877                         }
878
879                         tn = tn->internal[x];
880                         l--;
881                 }
882         } else {
883                 /* top is level 0 */
884                 if (passed_tn) {
885                         memcpy(tn, passed_tn, (dev->tnode_width * YAFFS_NTNODES_LEVEL0)/8);
886                         yaffs_free_tnode(dev, passed_tn);
887                 }
888         }
889
890         return tn;
891 }
892
893 static int yaffs_find_chunk_in_group(yaffs_dev_t *dev, int the_chunk,
894                                 yaffs_ext_tags *tags, int obj_id,
895                                 int inode_chunk)
896 {
897         int j;
898
899         for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
900                 if (yaffs_check_chunk_bit(dev, the_chunk / dev->param.chunks_per_block,
901                                 the_chunk % dev->param.chunks_per_block)) {
902                         
903                         if(dev->chunk_grp_size == 1)
904                                 return the_chunk;
905                         else {
906                                 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
907                                                                 tags);
908                                 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
909                                         /* found it; */
910                                         return the_chunk;
911                                 }
912                         }
913                 }
914                 the_chunk++;
915         }
916         return -1;
917 }
918
919 #if 0
920 /* Experimental code not being used yet. Might speed up file deletion */
921 /* DeleteWorker scans backwards through the tnode tree and deletes all the
922  * chunks and tnodes in the file.
923  * Returns 1 if the tree was deleted.
924  * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
925  */
926
927 static int yaffs_del_worker(yaffs_obj_t *in, yaffs_tnode_t *tn, __u32 level,
928                               int chunk_offset, int *limit)
929 {
930         int i;
931         int inode_chunk;
932         int the_chunk;
933         yaffs_ext_tags tags;
934         int found_chunk;
935         yaffs_dev_t *dev = in->my_dev;
936
937         int all_done = 1;
938
939         if (tn) {
940                 if (level > 0) {
941                         for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
942                              i--) {
943                                 if (tn->internal[i]) {
944                                         if (limit && (*limit) < 0) {
945                                                 all_done = 0;
946                                         } else {
947                                                 all_done =
948                                                         yaffs_del_worker(in,
949                                                                 tn->
950                                                                 internal
951                                                                 [i],
952                                                                 level -
953                                                                 1,
954                                                                 (chunk_offset
955                                                                         <<
956                                                                         YAFFS_TNODES_INTERNAL_BITS)
957                                                                 + i,
958                                                                 limit);
959                                         }
960                                         if (all_done) {
961                                                 yaffs_free_tnode(dev,
962                                                                 tn->
963                                                                 internal[i]);
964                                                 tn->internal[i] = NULL;
965                                         }
966                                 }
967                         }
968                         return (all_done) ? 1 : 0;
969                 } else if (level == 0) {
970                         int hit_limit = 0;
971
972                         for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hit_limit;
973                                         i--) {
974                                 the_chunk = yaffs_get_group_base(dev, tn, i);
975                                 if (the_chunk) {
976
977                                         inode_chunk = (chunk_offset <<
978                                                 YAFFS_TNODES_LEVEL0_BITS) + i;
979
980                                         found_chunk =
981                                                 yaffs_find_chunk_in_group(dev,
982                                                                 the_chunk,
983                                                                 &tags,
984                                                                 in->obj_id,
985                                                                 inode_chunk);
986
987                                         if (found_chunk > 0) {
988                                                 yaffs_chunk_del(dev,
989                                                                   found_chunk, 1,
990                                                                   __LINE__);
991                                                 in->n_data_chunks--;
992                                                 if (limit) {
993                                                         *limit = *limit - 1;
994                                                         if (*limit <= 0)
995                                                                 hit_limit = 1;
996                                                 }
997
998                                         }
999
1000                                         yaffs_load_tnode_0(dev, tn, i, 0);
1001                                 }
1002
1003                         }
1004                         return (i < 0) ? 1 : 0;
1005
1006                 }
1007
1008         }
1009
1010         return 1;
1011
1012 }
1013
1014 #endif
1015
1016 static void yaffs_soft_del_chunk(yaffs_dev_t *dev, int chunk)
1017 {
1018         yaffs_block_info_t *the_block;
1019         unsigned block_no;
1020
1021         T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
1022
1023         block_no =  chunk / dev->param.chunks_per_block;
1024         the_block = yaffs_get_block_info(dev, block_no);
1025         if (the_block) {
1026                 the_block->soft_del_pages++;
1027                 dev->n_free_chunks++;
1028                 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1029         }
1030 }
1031
1032 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1033  * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1034  * of the tnode.
1035  * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1036  */
1037
1038 static int yaffs_soft_del_worker(yaffs_obj_t *in, yaffs_tnode_t *tn,
1039                                   __u32 level, int chunk_offset)
1040 {
1041         int i;
1042         int the_chunk;
1043         int all_done = 1;
1044         yaffs_dev_t *dev = in->my_dev;
1045
1046         if (tn) {
1047                 if (level > 0) {
1048
1049                         for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
1050                              i--) {
1051                                 if (tn->internal[i]) {
1052                                         all_done =
1053                                             yaffs_soft_del_worker(in,
1054                                                                    tn->
1055                                                                    internal[i],
1056                                                                    level - 1,
1057                                                                    (chunk_offset
1058                                                                     <<
1059                                                                     YAFFS_TNODES_INTERNAL_BITS)
1060                                                                    + i);
1061                                         if (all_done) {
1062                                                 yaffs_free_tnode(dev,
1063                                                                 tn->
1064                                                                 internal[i]);
1065                                                 tn->internal[i] = NULL;
1066                                         } else {
1067                                                 /* Hoosterman... how could this happen? */
1068                                         }
1069                                 }
1070                         }
1071                         return (all_done) ? 1 : 0;
1072                 } else if (level == 0) {
1073
1074                         for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1075                                 the_chunk = yaffs_get_group_base(dev, tn, i);
1076                                 if (the_chunk) {
1077                                         /* Note this does not find the real chunk, only the chunk group.
1078                                          * We make an assumption that a chunk group is not larger than
1079                                          * a block.
1080                                          */
1081                                         yaffs_soft_del_chunk(dev, the_chunk);
1082                                         yaffs_load_tnode_0(dev, tn, i, 0);
1083                                 }
1084
1085                         }
1086                         return 1;
1087
1088                 }
1089
1090         }
1091
1092         return 1;
1093
1094 }
1095
1096 static void yaffs_soft_del_file(yaffs_obj_t *obj)
1097 {
1098         if (obj->deleted &&
1099             obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1100                 if (obj->n_data_chunks <= 0) {
1101                         /* Empty file with no duplicate object headers, just delete it immediately */
1102                         yaffs_free_tnode(obj->my_dev,
1103                                         obj->variant.file_variant.top);
1104                         obj->variant.file_variant.top = NULL;
1105                         T(YAFFS_TRACE_TRACING,
1106                           (TSTR("yaffs: Deleting empty file %d" TENDSTR),
1107                            obj->obj_id));
1108                         yaffs_generic_obj_del(obj);
1109                 } else {
1110                         yaffs_soft_del_worker(obj,
1111                                                obj->variant.file_variant.top,
1112                                                obj->variant.file_variant.
1113                                                top_level, 0);
1114                         obj->soft_del = 1;
1115                 }
1116         }
1117 }
1118
1119 /* Pruning removes any part of the file structure tree that is beyond the
1120  * bounds of the file (ie that does not point to chunks).
1121  *
1122  * A file should only get pruned when its size is reduced.
1123  *
1124  * Before pruning, the chunks must be pulled from the tree and the
1125  * level 0 tnode entries must be zeroed out.
1126  * Could also use this for file deletion, but that's probably better handled
1127  * by a special case.
1128  *
1129  * This function is recursive. For levels > 0 the function is called again on
1130  * any sub-tree. For level == 0 we just check if the sub-tree has data.
1131  * If there is no data in a subtree then it is pruned.
1132  */
1133
1134 static yaffs_tnode_t *yaffs_prune_worker(yaffs_dev_t *dev, yaffs_tnode_t *tn,
1135                                 __u32 level, int del0)
1136 {
1137         int i;
1138         int has_data;
1139
1140         if (tn) {
1141                 has_data = 0;
1142
1143                 if(level > 0){
1144                         for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1145                                 if (tn->internal[i]) {
1146                                         tn->internal[i] =
1147                                                 yaffs_prune_worker(dev, tn->internal[i],
1148                                                         level - 1,
1149                                                         (i == 0) ? del0 : 1);
1150                                 }
1151
1152                                 if (tn->internal[i])
1153                                         has_data++;
1154                         }
1155                 } else {
1156                         int tnode_size_u32 = dev->tnode_size/sizeof(__u32);
1157                         __u32 *map = (__u32 *)tn;
1158
1159                         for(i = 0; !has_data && i < tnode_size_u32; i++){
1160                                 if(map[i])
1161                                         has_data++;
1162                         }
1163                 }
1164
1165                 if (has_data == 0 && del0) {
1166                         /* Free and return NULL */
1167
1168                         yaffs_free_tnode(dev, tn);
1169                         tn = NULL;
1170                 }
1171
1172         }
1173
1174         return tn;
1175
1176 }
1177
1178 static int yaffs_prune_tree(yaffs_dev_t *dev,
1179                                 yaffs_file_s *file_struct)
1180 {
1181         int i;
1182         int has_data;
1183         int done = 0;
1184         yaffs_tnode_t *tn;
1185
1186         if (file_struct->top_level > 0) {
1187                 file_struct->top =
1188                     yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
1189
1190                 /* Now we have a tree with all the non-zero branches NULL but the height
1191                  * is the same as it was.
1192                  * Let's see if we can trim internal tnodes to shorten the tree.
1193                  * We can do this if only the 0th element in the tnode is in use
1194                  * (ie all the non-zero are NULL)
1195                  */
1196
1197                 while (file_struct->top_level && !done) {
1198                         tn = file_struct->top;
1199
1200                         has_data = 0;
1201                         for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1202                                 if (tn->internal[i])
1203                                         has_data++;
1204                         }
1205
1206                         if (!has_data) {
1207                                 file_struct->top = tn->internal[0];
1208                                 file_struct->top_level--;
1209                                 yaffs_free_tnode(dev, tn);
1210                         } else {
1211                                 done = 1;
1212                         }
1213                 }
1214         }
1215
1216         return YAFFS_OK;
1217 }
1218
1219 /*-------------------- End of File Structure functions.-------------------*/
1220
1221
1222 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1223 static yaffs_obj_t *yaffs_alloc_empty_obj(yaffs_dev_t *dev)
1224 {
1225         yaffs_obj_t *obj = yaffs_alloc_raw_obj(dev);
1226
1227         if (obj) {
1228                 dev->n_obj++;
1229
1230                 /* Now sweeten it up... */
1231
1232                 memset(obj, 0, sizeof(yaffs_obj_t));
1233                 obj->being_created = 1;
1234
1235                 obj->my_dev = dev;
1236                 obj->hdr_chunk = 0;
1237                 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1238                 YINIT_LIST_HEAD(&(obj->hard_links));
1239                 YINIT_LIST_HEAD(&(obj->hash_link));
1240                 YINIT_LIST_HEAD(&obj->siblings);
1241
1242
1243                 /* Now make the directory sane */
1244                 if (dev->root_dir) {
1245                         obj->parent = dev->root_dir;
1246                         ylist_add(&(obj->siblings), &dev->root_dir->variant.dir_variant.children);
1247                 }
1248
1249                 /* Add it to the lost and found directory.
1250                  * NB Can't put root or lost-n-found in lost-n-found so
1251                  * check if lost-n-found exists first
1252                  */
1253                 if (dev->lost_n_found)
1254                         yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1255
1256                 obj->being_created = 0;
1257         }
1258
1259         dev->checkpoint_blocks_required = 0; /* force recalculation*/
1260
1261         return obj;
1262 }
1263
1264 static yaffs_obj_t *yaffs_create_fake_dir(yaffs_dev_t *dev, int number,
1265                                                __u32 mode)
1266 {
1267
1268         yaffs_obj_t *obj =
1269             yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
1270         if (obj) {
1271                 obj->fake = 1;          /* it is fake so it might have no NAND presence... */
1272                 obj->rename_allowed = 0;        /* ... and we're not allowed to rename it... */
1273                 obj->unlink_allowed = 0;        /* ... or unlink it */
1274                 obj->deleted = 0;
1275                 obj->unlinked = 0;
1276                 obj->yst_mode = mode;
1277                 obj->my_dev = dev;
1278                 obj->hdr_chunk = 0;     /* Not a valid chunk. */
1279         }
1280
1281         return obj;
1282
1283 }
1284
1285 static void yaffs_unhash_obj(yaffs_obj_t *obj)
1286 {
1287         int bucket;
1288         yaffs_dev_t *dev = obj->my_dev;
1289
1290         /* If it is still linked into the bucket list, free from the list */
1291         if (!ylist_empty(&obj->hash_link)) {
1292                 ylist_del_init(&obj->hash_link);
1293                 bucket = yaffs_hash_fn(obj->obj_id);
1294                 dev->obj_bucket[bucket].count--;
1295         }
1296 }
1297
1298 /*  FreeObject frees up a Object and puts it back on the free list */
1299 static void yaffs_free_obj(yaffs_obj_t *obj)
1300 {
1301         yaffs_dev_t *dev = obj->my_dev;
1302
1303         T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), obj, obj->my_inode));
1304
1305         if (!obj)
1306                 YBUG();
1307         if (obj->parent)
1308                 YBUG();
1309         if (!ylist_empty(&obj->siblings))
1310                 YBUG();
1311
1312
1313         if (obj->my_inode) {
1314                 /* We're still hooked up to a cached inode.
1315                  * Don't delete now, but mark for later deletion
1316                  */
1317                 obj->defered_free = 1;
1318                 return;
1319         }
1320
1321         yaffs_unhash_obj(obj);
1322
1323         yaffs_free_raw_obj(dev,obj);
1324         dev->n_obj--;
1325         dev->checkpoint_blocks_required = 0; /* force recalculation*/
1326 }
1327
1328
1329 void yaffs_handle_defered_free(yaffs_obj_t *obj)
1330 {
1331         if (obj->defered_free)
1332                 yaffs_free_obj(obj);
1333 }
1334
1335 static void yaffs_init_tnodes_and_objs(yaffs_dev_t *dev)
1336 {
1337         int i;
1338
1339         dev->n_obj = 0;
1340         dev->n_tnodes = 0;
1341
1342         yaffs_init_raw_tnodes_and_objs(dev);
1343
1344         for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
1345                 YINIT_LIST_HEAD(&dev->obj_bucket[i].list);
1346                 dev->obj_bucket[i].count = 0;
1347         }
1348 }
1349
1350 static int yaffs_find_nice_bucket(yaffs_dev_t *dev)
1351 {
1352         int i;
1353         int l = 999;
1354         int lowest = 999999;
1355
1356
1357         /* Search for the shortest list or one that
1358          * isn't too long.
1359          */
1360
1361         for (i = 0; i < 10 && lowest > 4; i++) {
1362                 dev->bucket_finder++;
1363                 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1364                 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1365                         lowest = dev->obj_bucket[dev->bucket_finder].count;
1366                         l = dev->bucket_finder;
1367                 }
1368
1369         }
1370
1371         return l;
1372 }
1373
1374 static int yaffs_new_obj_id(yaffs_dev_t *dev)
1375 {
1376         int bucket = yaffs_find_nice_bucket(dev);
1377
1378         /* Now find an object value that has not already been taken
1379          * by scanning the list.
1380          */
1381
1382         int found = 0;
1383         struct ylist_head *i;
1384
1385         __u32 n = (__u32) bucket;
1386
1387         /* yaffs_check_obj_hash_sane();  */
1388
1389         while (!found) {
1390                 found = 1;
1391                 n += YAFFS_NOBJECT_BUCKETS;
1392                 if (1 || dev->obj_bucket[bucket].count > 0) {
1393                         ylist_for_each(i, &dev->obj_bucket[bucket].list) {
1394                                 /* If there is already one in the list */
1395                                 if (i && ylist_entry(i, yaffs_obj_t,
1396                                                 hash_link)->obj_id == n) {
1397                                         found = 0;
1398                                 }
1399                         }
1400                 }
1401         }
1402
1403         return n;
1404 }
1405
1406 static void yaffs_hash_obj(yaffs_obj_t *in)
1407 {
1408         int bucket = yaffs_hash_fn(in->obj_id);
1409         yaffs_dev_t *dev = in->my_dev;
1410
1411         ylist_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1412         dev->obj_bucket[bucket].count++;
1413 }
1414
1415 yaffs_obj_t *yaffs_find_by_number(yaffs_dev_t *dev, __u32 number)
1416 {
1417         int bucket = yaffs_hash_fn(number);
1418         struct ylist_head *i;
1419         yaffs_obj_t *in;
1420
1421         ylist_for_each(i, &dev->obj_bucket[bucket].list) {
1422                 /* Look if it is in the list */
1423                 if (i) {
1424                         in = ylist_entry(i, yaffs_obj_t, hash_link);
1425                         if (in->obj_id == number) {
1426
1427                                 /* Don't tell the VFS about this one if it is defered free */
1428                                 if (in->defered_free)
1429                                         return NULL;
1430
1431                                 return in;
1432                         }
1433                 }
1434         }
1435
1436         return NULL;
1437 }
1438
1439 yaffs_obj_t *yaffs_new_obj(yaffs_dev_t *dev, int number,
1440                                     yaffs_obj_type type)
1441 {
1442         yaffs_obj_t *the_obj=NULL;
1443         yaffs_tnode_t *tn = NULL;
1444
1445         if (number < 0)
1446                 number = yaffs_new_obj_id(dev);
1447
1448         if (type == YAFFS_OBJECT_TYPE_FILE) {
1449                 tn = yaffs_get_tnode(dev);
1450                 if (!tn)
1451                         return NULL;
1452         }
1453
1454         the_obj = yaffs_alloc_empty_obj(dev);
1455         if (!the_obj){
1456                 if(tn)
1457                         yaffs_free_tnode(dev,tn);
1458                 return NULL;
1459         }
1460
1461
1462         if (the_obj) {
1463                 the_obj->fake = 0;
1464                 the_obj->rename_allowed = 1;
1465                 the_obj->unlink_allowed = 1;
1466                 the_obj->obj_id = number;
1467                 yaffs_hash_obj(the_obj);
1468                 the_obj->variant_type = type;
1469 #ifdef CONFIG_YAFFS_WINCE
1470                 yfsd_win_file_time_now(the_obj->win_atime);
1471                 the_obj->win_ctime[0] = the_obj->win_mtime[0] =
1472                     the_obj->win_atime[0];
1473                 the_obj->win_ctime[1] = the_obj->win_mtime[1] =
1474                     the_obj->win_atime[1];
1475
1476 #else
1477
1478                 the_obj->yst_atime = the_obj->yst_mtime =
1479                     the_obj->yst_ctime = Y_CURRENT_TIME;
1480 #endif
1481                 switch (type) {
1482                 case YAFFS_OBJECT_TYPE_FILE:
1483                         the_obj->variant.file_variant.file_size = 0;
1484                         the_obj->variant.file_variant.scanned_size = 0;
1485                         the_obj->variant.file_variant.shrink_size = 0xFFFFFFFF; /* max __u32 */
1486                         the_obj->variant.file_variant.top_level = 0;
1487                         the_obj->variant.file_variant.top = tn;
1488                         break;
1489                 case YAFFS_OBJECT_TYPE_DIRECTORY:
1490                         YINIT_LIST_HEAD(&the_obj->variant.dir_variant.
1491                                         children);
1492                         YINIT_LIST_HEAD(&the_obj->variant.dir_variant.
1493                                         dirty);
1494                         break;
1495                 case YAFFS_OBJECT_TYPE_SYMLINK:
1496                 case YAFFS_OBJECT_TYPE_HARDLINK:
1497                 case YAFFS_OBJECT_TYPE_SPECIAL:
1498                         /* No action required */
1499                         break;
1500                 case YAFFS_OBJECT_TYPE_UNKNOWN:
1501                         /* todo this should not happen */
1502                         break;
1503                 }
1504         }
1505
1506         return the_obj;
1507 }
1508
1509 yaffs_obj_t *yaffs_find_or_create_by_number(yaffs_dev_t *dev,
1510                                                 int number,
1511                                                 yaffs_obj_type type)
1512 {
1513         yaffs_obj_t *the_obj = NULL;
1514
1515         if (number > 0)
1516                 the_obj = yaffs_find_by_number(dev, number);
1517
1518         if (!the_obj)
1519                 the_obj = yaffs_new_obj(dev, number, type);
1520
1521         return the_obj;
1522
1523 }
1524
1525
1526 YCHAR *yaffs_clone_str(const YCHAR *str)
1527 {
1528         YCHAR *new_str = NULL;
1529         int len;
1530
1531         if (!str)
1532                 str = _Y("");
1533
1534         len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
1535         new_str = YMALLOC((len + 1) * sizeof(YCHAR));
1536         if (new_str){
1537                 yaffs_strncpy(new_str, str,len);
1538                 new_str[len] = 0;
1539         }
1540         return new_str;
1541
1542 }
1543
1544 /*
1545  * Mknod (create) a new object.
1546  * equiv_obj only has meaning for a hard link;
1547  * alias_str only has meaning for a symlink.
1548  * rdev only has meaning for devices (a subset of special objects)
1549  */
1550
1551 static yaffs_obj_t *yaffs_create_obj(yaffs_obj_type type,
1552                                        yaffs_obj_t *parent,
1553                                        const YCHAR *name,
1554                                        __u32 mode,
1555                                        __u32 uid,
1556                                        __u32 gid,
1557                                        yaffs_obj_t *equiv_obj,
1558                                        const YCHAR *alias_str, __u32 rdev)
1559 {
1560         yaffs_obj_t *in;
1561         YCHAR *str = NULL;
1562
1563         yaffs_dev_t *dev = parent->my_dev;
1564
1565         /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
1566         if (yaffs_find_by_name(parent, name))
1567                 return NULL;
1568
1569         if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
1570                 str = yaffs_clone_str(alias_str);
1571                 if (!str)
1572                         return NULL;
1573         }
1574
1575         in = yaffs_new_obj(dev, -1, type);
1576
1577         if (!in){
1578                 if(str)
1579                         YFREE(str);
1580                 return NULL;
1581         }
1582
1583
1584
1585
1586
1587         if (in) {
1588                 in->hdr_chunk = 0;
1589                 in->valid = 1;
1590                 in->variant_type = type;
1591
1592                 in->yst_mode = mode;
1593
1594 #ifdef CONFIG_YAFFS_WINCE
1595                 yfsd_win_file_time_now(in->win_atime);
1596                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1597                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1598
1599 #else
1600                 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
1601
1602                 in->yst_rdev = rdev;
1603                 in->yst_uid = uid;
1604                 in->yst_gid = gid;
1605 #endif
1606                 in->n_data_chunks = 0;
1607
1608                 yaffs_set_obj_name(in, name);
1609                 in->dirty = 1;
1610
1611                 yaffs_add_obj_to_dir(parent, in);
1612
1613                 in->my_dev = parent->my_dev;
1614
1615                 switch (type) {
1616                 case YAFFS_OBJECT_TYPE_SYMLINK:
1617                         in->variant.symlink_variant.alias = str;
1618                         break;
1619                 case YAFFS_OBJECT_TYPE_HARDLINK:
1620                         in->variant.hardlink_variant.equiv_obj =
1621                                 equiv_obj;
1622                         in->variant.hardlink_variant.equiv_id =
1623                                 equiv_obj->obj_id;
1624                         ylist_add(&in->hard_links, &equiv_obj->hard_links);
1625                         break;
1626                 case YAFFS_OBJECT_TYPE_FILE:
1627                 case YAFFS_OBJECT_TYPE_DIRECTORY:
1628                 case YAFFS_OBJECT_TYPE_SPECIAL:
1629                 case YAFFS_OBJECT_TYPE_UNKNOWN:
1630                         /* do nothing */
1631                         break;
1632                 }
1633
1634                 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
1635                         /* Could not create the object header, fail the creation */
1636                         yaffs_del_obj(in);
1637                         in = NULL;
1638                 }
1639
1640                 yaffs_update_parent(parent);
1641         }
1642
1643         return in;
1644 }
1645
1646 yaffs_obj_t *yaffs_create_file(yaffs_obj_t *parent, const YCHAR *name,
1647                         __u32 mode, __u32 uid, __u32 gid)
1648 {
1649         return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
1650                                 uid, gid, NULL, NULL, 0);
1651 }
1652
1653 yaffs_obj_t *yaffs_create_dir(yaffs_obj_t *parent, const YCHAR *name,
1654                                 __u32 mode, __u32 uid, __u32 gid)
1655 {
1656         return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
1657                                  mode, uid, gid, NULL, NULL, 0);
1658 }
1659
1660 yaffs_obj_t *yaffs_create_special(yaffs_obj_t *parent, const YCHAR *name,
1661                                 __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1662 {
1663         return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
1664                                  uid, gid, NULL, NULL, rdev);
1665 }
1666
1667 yaffs_obj_t *yaffs_create_symlink(yaffs_obj_t *parent, const YCHAR *name,
1668                                 __u32 mode, __u32 uid, __u32 gid,
1669                                 const YCHAR *alias)
1670 {
1671         return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
1672                                 uid, gid, NULL, alias, 0);
1673 }
1674
1675 /* yaffs_link_obj returns the object id of the equivalent object.*/
1676 yaffs_obj_t *yaffs_link_obj(yaffs_obj_t *parent, const YCHAR *name,
1677                         yaffs_obj_t *equiv_obj)
1678 {
1679         /* Get the real object in case we were fed a hard link as an equivalent object */
1680         equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
1681
1682         if (yaffs_create_obj
1683             (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
1684              equiv_obj, NULL, 0)) {
1685                 return equiv_obj;
1686         } else {
1687                 return NULL;
1688         }
1689
1690 }
1691
1692 static int yaffs_change_obj_name(yaffs_obj_t *obj, yaffs_obj_t *new_dir,
1693                                 const YCHAR *new_name, int force, int shadows)
1694 {
1695         int unlink_op;
1696         int del_op;
1697
1698         yaffs_obj_t *existing_target;
1699
1700         if (new_dir == NULL)
1701                 new_dir = obj->parent;  /* use the old directory */
1702
1703         if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1704                 T(YAFFS_TRACE_ALWAYS,
1705                   (TSTR
1706                    ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
1707                     TENDSTR)));
1708                 YBUG();
1709         }
1710
1711         /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1712         if (obj->my_dev->param.is_yaffs2)
1713                 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1714         else
1715                 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1716                             && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1717
1718         del_op = (new_dir == obj->my_dev->del_dir);
1719
1720         existing_target = yaffs_find_by_name(new_dir, new_name);
1721
1722         /* If the object is a file going into the unlinked directory,
1723          *   then it is OK to just stuff it in since duplicate names are allowed.
1724          *   else only proceed if the new name does not exist and if we're putting
1725          *   it into a directory.
1726          */
1727         if ((unlink_op ||
1728              del_op ||
1729              force ||
1730              (shadows > 0) ||
1731              !existing_target) &&
1732             new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1733                 yaffs_set_obj_name(obj, new_name);
1734                 obj->dirty = 1;
1735
1736                 yaffs_add_obj_to_dir(new_dir, obj);
1737
1738                 if (unlink_op)
1739                         obj->unlinked = 1;
1740
1741                 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1742                 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
1743                         return YAFFS_OK;
1744         }
1745
1746         return YAFFS_FAIL;
1747 }
1748
1749 int yaffs_rename_obj(yaffs_obj_t *old_dir, const YCHAR *old_name,
1750                 yaffs_obj_t *new_dir, const YCHAR *new_name)
1751 {
1752         yaffs_obj_t *obj = NULL;
1753         yaffs_obj_t *existing_target = NULL;
1754         int force = 0;
1755         int result;
1756         yaffs_dev_t *dev;
1757
1758
1759         if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1760                 YBUG();
1761         if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1762                 YBUG();
1763
1764         dev = old_dir->my_dev;
1765
1766 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1767         /* Special case for case insemsitive systems (eg. WinCE).
1768          * While look-up is case insensitive, the name isn't.
1769          * Therefore we might want to change x.txt to X.txt
1770         */
1771         if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0)
1772                 force = 1;
1773 #endif
1774
1775         if(yaffs_strnlen(new_name,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
1776                 /* ENAMETOOLONG */
1777                 return YAFFS_FAIL;
1778
1779         obj = yaffs_find_by_name(old_dir, old_name);
1780
1781         if (obj && obj->rename_allowed) {
1782
1783                 /* Now do the handling for an existing target, if there is one */
1784
1785                 existing_target = yaffs_find_by_name(new_dir, new_name);
1786                 if (existing_target &&
1787                         existing_target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1788                         !ylist_empty(&existing_target->variant.dir_variant.children)) {
1789                         /* There is a target that is a non-empty directory, so we fail */
1790                         return YAFFS_FAIL;      /* EEXIST or ENOTEMPTY */
1791                 } else if (existing_target && existing_target != obj) {
1792                         /* Nuke the target first, using shadowing,
1793                          * but only if it isn't the same object.
1794                          *
1795                          * Note we must disable gc otherwise it can mess up the shadowing.
1796                          *
1797                          */
1798                         dev->gc_disable=1;
1799                         yaffs_change_obj_name(obj, new_dir, new_name, force,
1800                                                 existing_target->obj_id);
1801                         existing_target->is_shadowed = 1;
1802                         yaffs_unlink_obj(existing_target);
1803                         dev->gc_disable=0;
1804                 }
1805
1806                 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
1807
1808                 yaffs_update_parent(old_dir);
1809                 if(new_dir != old_dir)
1810                         yaffs_update_parent(new_dir);
1811                 
1812                 return result;
1813         }
1814         return YAFFS_FAIL;
1815 }
1816
1817 /*------------------------- Block Management and Page Allocation ----------------*/
1818
1819 static int yaffs_init_blocks(yaffs_dev_t *dev)
1820 {
1821         int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
1822
1823         dev->block_info = NULL;
1824         dev->chunk_bits = NULL;
1825
1826         dev->alloc_block = -1;  /* force it to get a new one */
1827
1828         /* If the first allocation strategy fails, thry the alternate one */
1829         dev->block_info = YMALLOC(n_blocks * sizeof(yaffs_block_info_t));
1830         if (!dev->block_info) {
1831                 dev->block_info = YMALLOC_ALT(n_blocks * sizeof(yaffs_block_info_t));
1832                 dev->block_info_alt = 1;
1833         } else
1834                 dev->block_info_alt = 0;
1835
1836         if (dev->block_info) {
1837                 /* Set up dynamic blockinfo stuff. */
1838                 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */
1839                 dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * n_blocks);
1840                 if (!dev->chunk_bits) {
1841                         dev->chunk_bits = YMALLOC_ALT(dev->chunk_bit_stride * n_blocks);
1842                         dev->chunk_bits_alt = 1;
1843                 } else
1844                         dev->chunk_bits_alt = 0;
1845         }
1846
1847         if (dev->block_info && dev->chunk_bits) {
1848                 memset(dev->block_info, 0, n_blocks * sizeof(yaffs_block_info_t));
1849                 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
1850                 return YAFFS_OK;
1851         }
1852
1853         return YAFFS_FAIL;
1854 }
1855
1856 static void yaffs_deinit_blocks(yaffs_dev_t *dev)
1857 {
1858         if (dev->block_info_alt && dev->block_info)
1859                 YFREE_ALT(dev->block_info);
1860         else if (dev->block_info)
1861                 YFREE(dev->block_info);
1862
1863         dev->block_info_alt = 0;
1864
1865         dev->block_info = NULL;
1866
1867         if (dev->chunk_bits_alt && dev->chunk_bits)
1868                 YFREE_ALT(dev->chunk_bits);
1869         else if (dev->chunk_bits)
1870                 YFREE(dev->chunk_bits);
1871         dev->chunk_bits_alt = 0;
1872         dev->chunk_bits = NULL;
1873 }
1874
1875 void yaffs_block_became_dirty(yaffs_dev_t *dev, int block_no)
1876 {
1877         yaffs_block_info_t *bi = yaffs_get_block_info(dev, block_no);
1878
1879         int erased_ok = 0;
1880
1881         /* If the block is still healthy erase it and mark as clean.
1882          * If the block has had a data failure, then retire it.
1883          */
1884
1885         T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
1886                 (TSTR("yaffs_block_became_dirty block %d state %d %s"TENDSTR),
1887                 block_no, bi->block_state, (bi->needs_retiring) ? "needs retiring" : ""));
1888
1889         yaffs2_clear_oldest_dirty_seq(dev,bi);
1890
1891         bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
1892
1893         /* If this is the block being garbage collected then stop gc'ing this block */
1894         if(block_no == dev->gc_block)
1895                 dev->gc_block = 0;
1896
1897         /* If this block is currently the best candidate for gc then drop as a candidate */
1898         if(block_no == dev->gc_dirtiest){
1899                 dev->gc_dirtiest = 0;
1900                 dev->gc_pages_in_use = 0;
1901         }
1902
1903         if (!bi->needs_retiring) {
1904                 yaffs2_checkpt_invalidate(dev);
1905                 erased_ok = yaffs_erase_block(dev, block_no);
1906                 if (!erased_ok) {
1907                         dev->n_erase_failures++;
1908                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1909                           (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
1910                 }
1911         }
1912
1913         if (erased_ok &&
1914             ((yaffs_trace_mask & YAFFS_TRACE_ERASE) || !yaffs_skip_verification(dev))) {
1915                 int i;
1916                 for (i = 0; i < dev->param.chunks_per_block; i++) {
1917                         if (!yaffs_check_chunk_erased
1918                             (dev, block_no * dev->param.chunks_per_block + i)) {
1919                                 T(YAFFS_TRACE_ERROR,
1920                                   (TSTR
1921                                    (">>Block %d erasure supposedly OK, but chunk %d not erased"
1922                                     TENDSTR), block_no, i));
1923                         }
1924                 }
1925         }
1926
1927         if (erased_ok) {
1928                 /* Clean it up... */
1929                 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
1930                 bi->seq_number = 0;
1931                 dev->n_erased_blocks++;
1932                 bi->pages_in_use = 0;
1933                 bi->soft_del_pages = 0;
1934                 bi->has_shrink_hdr = 0;
1935                 bi->skip_erased_check = 1;  /* This is clean, so no need to check */
1936                 bi->gc_prioritise = 0;
1937                 yaffs_clear_chunk_bits(dev, block_no);
1938
1939                 T(YAFFS_TRACE_ERASE,
1940                   (TSTR("Erased block %d" TENDSTR), block_no));
1941         } else {
1942                 dev->n_free_chunks -= dev->param.chunks_per_block;      /* We lost a block of free space */
1943
1944                 yaffs_retire_block(dev, block_no);
1945                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
1946                   (TSTR("**>> Block %d retired" TENDSTR), block_no));
1947         }
1948 }
1949
1950 static int yaffs_find_alloc_block(yaffs_dev_t *dev)
1951 {
1952         int i;
1953
1954         yaffs_block_info_t *bi;
1955
1956         if (dev->n_erased_blocks < 1) {
1957                 /* Hoosterman we've got a problem.
1958                  * Can't get space to gc
1959                  */
1960                 T(YAFFS_TRACE_ERROR,
1961                   (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
1962
1963                 return -1;
1964         }
1965
1966         /* Find an empty block. */
1967
1968         for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
1969                 dev->alloc_block_finder++;
1970                 if (dev->alloc_block_finder < dev->internal_start_block
1971                     || dev->alloc_block_finder > dev->internal_end_block) {
1972                         dev->alloc_block_finder = dev->internal_start_block;
1973                 }
1974
1975                 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
1976
1977                 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1978                         bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
1979                         dev->seq_number++;
1980                         bi->seq_number = dev->seq_number;
1981                         dev->n_erased_blocks--;
1982                         T(YAFFS_TRACE_ALLOCATE,
1983                           (TSTR("Allocated block %d, seq  %d, %d left" TENDSTR),
1984                            dev->alloc_block_finder, dev->seq_number,
1985                            dev->n_erased_blocks));
1986                         return dev->alloc_block_finder;
1987                 }
1988         }
1989
1990         T(YAFFS_TRACE_ALWAYS,
1991           (TSTR
1992            ("yaffs tragedy: no more erased blocks, but there should have been %d"
1993             TENDSTR), dev->n_erased_blocks));
1994
1995         return -1;
1996 }
1997
1998
1999 /*
2000  * Check if there's space to allocate...
2001  * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()?
2002  */
2003 int yaffs_check_alloc_available(yaffs_dev_t *dev, int n_chunks)
2004 {
2005         int reserved_chunks;
2006         int reserved_blocks = dev->param.n_reserved_blocks;
2007         int checkpt_blocks;
2008
2009         checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
2010
2011         reserved_chunks = ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
2012
2013         return (dev->n_free_chunks > (reserved_chunks + n_chunks));
2014 }
2015
2016 static int yaffs_alloc_chunk(yaffs_dev_t *dev, int use_reserver,
2017                 yaffs_block_info_t **block_ptr)
2018 {
2019         int ret_val;
2020         yaffs_block_info_t *bi;
2021
2022         if (dev->alloc_block < 0) {
2023                 /* Get next block to allocate off */
2024                 dev->alloc_block = yaffs_find_alloc_block(dev);
2025                 dev->alloc_page = 0;
2026         }
2027
2028         if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
2029                 /* Not enough space to allocate unless we're allowed to use the reserve. */
2030                 return -1;
2031         }
2032
2033         if (dev->n_erased_blocks < dev->param.n_reserved_blocks
2034                         && dev->alloc_page == 0) {
2035                 T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
2036         }
2037
2038         /* Next page please.... */
2039         if (dev->alloc_block >= 0) {
2040                 bi = yaffs_get_block_info(dev, dev->alloc_block);
2041
2042                 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
2043                         dev->alloc_page;
2044                 bi->pages_in_use++;
2045                 yaffs_set_chunk_bit(dev, dev->alloc_block,
2046                                 dev->alloc_page);
2047
2048                 dev->alloc_page++;
2049
2050                 dev->n_free_chunks--;
2051
2052                 /* If the block is full set the state to full */
2053                 if (dev->alloc_page >= dev->param.chunks_per_block) {
2054                         bi->block_state = YAFFS_BLOCK_STATE_FULL;
2055                         dev->alloc_block = -1;
2056                 }
2057
2058                 if (block_ptr)
2059                         *block_ptr = bi;
2060
2061                 return ret_val;
2062         }
2063
2064         T(YAFFS_TRACE_ERROR,
2065                         (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2066
2067         return -1;
2068 }
2069
2070 static int yaffs_get_erased_chunks(yaffs_dev_t *dev)
2071 {
2072         int n;
2073
2074         n = dev->n_erased_blocks * dev->param.chunks_per_block;
2075
2076         if (dev->alloc_block > 0)
2077                 n += (dev->param.chunks_per_block - dev->alloc_page);
2078
2079         return n;
2080
2081 }
2082
2083 /*
2084  * yaffs_skip_rest_of_block() skips over the rest of the allocation block
2085  * if we don't want to write to it.
2086  */
2087 void yaffs_skip_rest_of_block(yaffs_dev_t *dev)
2088 {
2089         if(dev->alloc_block > 0){
2090                 yaffs_block_info_t *bi = yaffs_get_block_info(dev, dev->alloc_block);
2091                 if(bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING){
2092                         bi->block_state = YAFFS_BLOCK_STATE_FULL;
2093                         dev->alloc_block = -1;
2094                 }
2095         }
2096 }
2097
2098
2099 static int yaffs_gc_block(yaffs_dev_t *dev, int block,
2100                 int whole_block)
2101 {
2102         int old_chunk;
2103         int new_chunk;
2104         int mark_flash;
2105         int ret_val = YAFFS_OK;
2106         int i;
2107         int is_checkpt_block;
2108         int matching_chunk;
2109         int max_copies;
2110
2111         int chunks_before = yaffs_get_erased_chunks(dev);
2112         int chunks_after;
2113
2114         yaffs_ext_tags tags;
2115
2116         yaffs_block_info_t *bi = yaffs_get_block_info(dev, block);
2117
2118         yaffs_obj_t *object;
2119
2120         is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2121
2122
2123         T(YAFFS_TRACE_TRACING,
2124                         (TSTR("Collecting block %d, in use %d, shrink %d, whole_block %d" TENDSTR),
2125                          block,
2126                          bi->pages_in_use,
2127                          bi->has_shrink_hdr,
2128                          whole_block));
2129
2130         /*yaffs_verify_free_chunks(dev); */
2131
2132         if(bi->block_state == YAFFS_BLOCK_STATE_FULL)
2133                 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2134         
2135         bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2136
2137         dev->gc_disable = 1;
2138
2139         if (is_checkpt_block ||
2140                         !yaffs_still_some_chunks(dev, block)) {
2141                 T(YAFFS_TRACE_TRACING,
2142                                 (TSTR
2143                                  ("Collecting block %d that has no chunks in use" TENDSTR),
2144                                  block));
2145                 yaffs_block_became_dirty(dev, block);
2146         } else {
2147
2148                 __u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2149
2150                 yaffs_verify_blk(dev, bi, block);
2151
2152                 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2153                 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2154
2155                 for (/* init already done */;
2156                      ret_val == YAFFS_OK &&
2157                      dev->gc_chunk < dev->param.chunks_per_block &&
2158                      (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2159                      max_copies > 0;
2160                      dev->gc_chunk++, old_chunk++) {
2161                         if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2162
2163                                 /* This page is in use and might need to be copied off */
2164
2165                                 max_copies--;
2166
2167                                 mark_flash = 1;
2168
2169                                 yaffs_init_tags(&tags);
2170
2171                                 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2172                                                                 buffer, &tags);
2173
2174                                 object =
2175                                     yaffs_find_by_number(dev,
2176                                                              tags.obj_id);
2177
2178                                 T(YAFFS_TRACE_GC_DETAIL,
2179                                   (TSTR
2180                                    ("Collecting chunk in block %d, %d %d %d " TENDSTR),
2181                                    dev->gc_chunk, tags.obj_id, tags.chunk_id,
2182                                    tags.n_bytes));
2183
2184                                 if (object && !yaffs_skip_verification(dev)) {
2185                                         if (tags.chunk_id == 0)
2186                                                 matching_chunk = object->hdr_chunk;
2187                                         else if (object->soft_del)
2188                                                 matching_chunk = old_chunk; /* Defeat the test */
2189                                         else
2190                                                 matching_chunk = yaffs_find_chunk_in_file(object, tags.chunk_id, NULL);
2191
2192                                         if (old_chunk != matching_chunk)
2193                                                 T(YAFFS_TRACE_ERROR,
2194                                                   (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
2195                                                   old_chunk, matching_chunk, tags.obj_id, tags.chunk_id));
2196
2197                                 }
2198
2199                                 if (!object) {
2200                                         T(YAFFS_TRACE_ERROR,
2201                                           (TSTR
2202                                            ("page %d in gc has no object: %d %d %d "
2203                                             TENDSTR), old_chunk,
2204                                             tags.obj_id, tags.chunk_id, tags.n_bytes));
2205                                 }
2206
2207                                 if (object &&
2208                                     object->deleted &&
2209                                     object->soft_del &&
2210                                     tags.chunk_id != 0) {
2211                                         /* Data chunk in a soft deleted file, throw it away
2212                                          * It's a soft deleted data chunk,
2213                                          * No need to copy this, just forget about it and
2214                                          * fix up the object.
2215                                          */
2216                                          
2217                                         /* Free chunks already includes softdeleted chunks.
2218                                          * How ever this chunk is going to soon be really deleted
2219                                          * which will increment free chunks.
2220                                          * We have to decrement free chunks so this works out properly.
2221                                          */
2222                                         dev->n_free_chunks--;
2223                                         bi->soft_del_pages--;
2224
2225                                         object->n_data_chunks--;
2226
2227                                         if (object->n_data_chunks <= 0) {
2228                                                 /* remeber to clean up the object */
2229                                                 dev->gc_cleanup_list[dev->n_clean_ups] =
2230                                                     tags.obj_id;
2231                                                 dev->n_clean_ups++;
2232                                         }
2233                                         mark_flash = 0;
2234                                 } else if (0) {
2235                                         /* Todo object && object->deleted && object->n_data_chunks == 0 */
2236                                         /* Deleted object header with no data chunks.
2237                                          * Can be discarded and the file deleted.
2238                                          */
2239                                         object->hdr_chunk = 0;
2240                                         yaffs_free_tnode(object->my_dev,
2241                                                         object->variant.
2242                                                         file_variant.top);
2243                                         object->variant.file_variant.top = NULL;
2244                                         yaffs_generic_obj_del(object);
2245
2246                                 } else if (object) {
2247                                         /* It's either a data chunk in a live file or
2248                                          * an ObjectHeader, so we're interested in it.
2249                                          * NB Need to keep the ObjectHeaders of deleted files
2250                                          * until the whole file has been deleted off
2251                                          */
2252                                         tags.serial_number++;
2253
2254                                         dev->n_gc_copies++;
2255
2256                                         if (tags.chunk_id == 0) {
2257                                                 /* It is an object Id,
2258                                                  * We need to nuke the shrinkheader flags first
2259                                                  * Also need to clean up shadowing.
2260                                                  * We no longer want the shrink_header flag since its work is done
2261                                                  * and if it is left in place it will mess up scanning.
2262                                                  */
2263
2264                                                 yaffs_obj_header *oh;
2265                                                 oh = (yaffs_obj_header *)buffer;
2266
2267                                                 oh->is_shrink = 0;
2268                                                 tags.extra_is_shrink = 0;
2269
2270                                                 oh->shadows_obj = 0;
2271                                                 oh->inband_shadowed_obj_id = 0;
2272                                                 tags.extra_shadows = 0;
2273
2274                                                 /* Update file size */
2275                                                 if(object->variant_type == YAFFS_OBJECT_TYPE_FILE){
2276                                                         oh->file_size = object->variant.file_variant.file_size;
2277                                                         tags.extra_length = oh->file_size;
2278                                                 }
2279
2280                                                 yaffs_verify_oh(object, oh, &tags, 1);
2281                                                 new_chunk =
2282                                                     yaffs_write_new_chunk(dev,(__u8 *) oh, &tags, 1);
2283                                         } else
2284                                                 new_chunk =
2285                                                     yaffs_write_new_chunk(dev, buffer, &tags, 1);
2286
2287                                         if (new_chunk < 0) {
2288                                                 ret_val = YAFFS_FAIL;
2289                                         } else {
2290
2291                                                 /* Ok, now fix up the Tnodes etc. */
2292
2293                                                 if (tags.chunk_id == 0) {
2294                                                         /* It's a header */
2295                                                         object->hdr_chunk =  new_chunk;
2296                                                         object->serial =   tags.serial_number;
2297                                                 } else {
2298                                                         /* It's a data chunk */
2299                                                         int ok;
2300                                                         ok = yaffs_put_chunk_in_file
2301                                                             (object,
2302                                                              tags.chunk_id,
2303                                                              new_chunk, 0);
2304                                                 }
2305                                         }
2306                                 }
2307
2308                                 if (ret_val == YAFFS_OK)
2309                                         yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2310
2311                         }
2312                 }
2313
2314                 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2315
2316
2317
2318         }
2319
2320         yaffs_verify_collected_blk(dev, bi, block);
2321
2322
2323
2324         if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2325                 /*
2326                  * The gc did not complete. Set block state back to FULL
2327                  * because checkpointing does not restore gc.
2328                  */
2329                 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2330         } else {
2331                 /* The gc completed. */
2332                 /* Do any required cleanups */
2333                 for (i = 0; i < dev->n_clean_ups; i++) {
2334                         /* Time to delete the file too */
2335                         object =
2336                             yaffs_find_by_number(dev,
2337                                                      dev->gc_cleanup_list[i]);
2338                         if (object) {
2339                                 yaffs_free_tnode(dev,
2340                                                 object->variant.file_variant.
2341                                                 top);
2342                                 object->variant.file_variant.top = NULL;
2343                                 T(YAFFS_TRACE_GC,
2344                                   (TSTR
2345                                    ("yaffs: About to finally delete object %d"
2346                                     TENDSTR), object->obj_id));
2347                                 yaffs_generic_obj_del(object);
2348                                 object->my_dev->n_deleted_files--;
2349                         }
2350
2351                 }
2352
2353
2354                 chunks_after = yaffs_get_erased_chunks(dev);
2355                 if (chunks_before >= chunks_after) {
2356                         T(YAFFS_TRACE_GC,
2357                           (TSTR
2358                            ("gc did not increase free chunks before %d after %d"
2359                             TENDSTR), chunks_before, chunks_after));
2360                 }
2361                 dev->gc_block = 0;
2362                 dev->gc_chunk = 0;
2363                 dev->n_clean_ups = 0;
2364         }
2365
2366         dev->gc_disable = 0;
2367
2368         return ret_val;
2369 }
2370
2371 /*
2372  * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2373  * for garbage collection.
2374  */
2375
2376 static unsigned yaffs_find_gc_block(yaffs_dev_t *dev,
2377                                         int aggressive,
2378                                         int background)
2379 {
2380         int i;
2381         int iterations;
2382         unsigned selected = 0;
2383         int prioritised = 0;
2384         int prioritised_exist = 0;
2385         yaffs_block_info_t *bi;
2386         int threshold;
2387
2388         /* First let's see if we need to grab a prioritised block */
2389         if (dev->has_pending_prioritised_gc && !aggressive) {
2390                 dev->gc_dirtiest = 0;
2391                 bi = dev->block_info;
2392                 for (i = dev->internal_start_block;
2393                         i <= dev->internal_end_block && !selected;
2394                         i++) {
2395
2396                         if (bi->gc_prioritise) {
2397                                 prioritised_exist = 1;
2398                                 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2399                                    yaffs_block_ok_for_gc(dev, bi)) {
2400                                         selected = i;
2401                                         prioritised = 1;
2402                                 }
2403                         }
2404                         bi++;
2405                 }
2406
2407                 /*
2408                  * If there is a prioritised block and none was selected then
2409                  * this happened because there is at least one old dirty block gumming
2410                  * up the works. Let's gc the oldest dirty block.
2411                  */
2412
2413                 if(prioritised_exist &&
2414                         !selected &&
2415                         dev->oldest_dirty_block > 0)
2416                         selected = dev->oldest_dirty_block;
2417
2418                 if (!prioritised_exist) /* None found, so we can clear this */
2419                         dev->has_pending_prioritised_gc = 0;
2420         }
2421
2422         /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2423          * search harder.
2424          * else (we're doing a leasurely gc), then we only bother to do this if the
2425          * block has only a few pages in use.
2426          */
2427
2428         if (!selected){
2429                 int pages_used;
2430                 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2431                 if (aggressive){
2432                         threshold = dev->param.chunks_per_block;
2433                         iterations = n_blocks;
2434                 } else {
2435                         int max_threshold;
2436
2437                         if(background)
2438                                 max_threshold = dev->param.chunks_per_block/2;
2439                         else
2440                                 max_threshold = dev->param.chunks_per_block/8;
2441
2442                         if(max_threshold <  YAFFS_GC_PASSIVE_THRESHOLD)
2443                                 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2444
2445                         threshold = background ?
2446                                 (dev->gc_not_done + 2) * 2 : 0;
2447                         if(threshold <YAFFS_GC_PASSIVE_THRESHOLD)
2448                                 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2449                         if(threshold > max_threshold)
2450                                 threshold = max_threshold;
2451
2452                         iterations = n_blocks / 16 + 1;
2453                         if (iterations > 100)
2454                                 iterations = 100;
2455                 }
2456
2457                 for (i = 0;
2458                         i < iterations &&
2459                         (dev->gc_dirtiest < 1 ||
2460                                 dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
2461                         i++) {
2462                         dev->gc_block_finder++;
2463                         if (dev->gc_block_finder < dev->internal_start_block ||
2464                                 dev->gc_block_finder > dev->internal_end_block)
2465                                 dev->gc_block_finder = dev->internal_start_block;
2466
2467                         bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2468
2469                         pages_used = bi->pages_in_use - bi->soft_del_pages;
2470
2471                         if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2472                                 pages_used < dev->param.chunks_per_block &&
2473                                 (dev->gc_dirtiest < 1 || pages_used < dev->gc_pages_in_use) &&
2474                                 yaffs_block_ok_for_gc(dev, bi)) {
2475                                 dev->gc_dirtiest = dev->gc_block_finder;
2476                                 dev->gc_pages_in_use = pages_used;
2477                         }
2478                 }
2479
2480                 if(dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2481                         selected = dev->gc_dirtiest;
2482         }
2483
2484         /*
2485          * If nothing has been selected for a while, try selecting the oldest dirty
2486          * because that's gumming up the works.
2487          */
2488
2489         if(!selected && dev->param.is_yaffs2 &&
2490                 dev->gc_not_done >= ( background ? 10 : 20)){
2491                 yaffs2_find_oldest_dirty_seq(dev);
2492                 if(dev->oldest_dirty_block > 0) {
2493                         selected = dev->oldest_dirty_block;
2494                         dev->gc_dirtiest = selected;
2495                         dev->oldest_dirty_gc_count++;
2496                         bi = yaffs_get_block_info(dev, selected);
2497                         dev->gc_pages_in_use =  bi->pages_in_use - bi->soft_del_pages;
2498                 } else
2499                         dev->gc_not_done = 0;
2500         }
2501
2502         if(selected){
2503                 T(YAFFS_TRACE_GC,
2504                   (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR),
2505                   selected,
2506                   dev->param.chunks_per_block - dev->gc_pages_in_use,
2507                   prioritised));
2508
2509                 dev->n_gc_blocks++;
2510                 if(background)
2511                         dev->bg_gcs++;
2512
2513                 dev->gc_dirtiest = 0;
2514                 dev->gc_pages_in_use = 0;
2515                 dev->gc_not_done = 0;
2516                 if(dev->refresh_skip > 0)
2517                         dev->refresh_skip--;
2518         } else{
2519                 dev->gc_not_done++;
2520                 T(YAFFS_TRACE_GC,
2521                   (TSTR("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" TENDSTR),
2522                   dev->gc_block_finder, dev->gc_not_done,
2523                   threshold,
2524                   dev->gc_dirtiest, dev->gc_pages_in_use,
2525                   dev->oldest_dirty_block,
2526                   background ? " bg" : ""));
2527         }
2528
2529         return selected;
2530 }
2531
2532 /* New garbage collector
2533  * If we're very low on erased blocks then we do aggressive garbage collection
2534  * otherwise we do "leasurely" garbage collection.
2535  * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2536  * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2537  *
2538  * The idea is to help clear out space in a more spread-out manner.
2539  * Dunno if it really does anything useful.
2540  */
2541 static int yaffs_check_gc(yaffs_dev_t *dev, int background)
2542 {
2543         int aggressive = 0;
2544         int gc_ok = YAFFS_OK;
2545         int max_tries = 0;
2546         int min_erased;
2547         int erased_chunks;
2548         int checkpt_block_adjust;
2549
2550         if(dev->param.gc_control &&
2551                 (dev->param.gc_control(dev) & 1) == 0)
2552                 return YAFFS_OK;
2553
2554         if (dev->gc_disable) {
2555                 /* Bail out so we don't get recursive gc */
2556                 return YAFFS_OK;
2557         }
2558
2559         /* This loop should pass the first time.
2560          * We'll only see looping here if the collection does not increase space.
2561          */
2562
2563         do {
2564                 max_tries++;
2565
2566                 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2567
2568                 min_erased  = dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2569                 erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2570
2571                 /* If we need a block soon then do aggressive gc.*/
2572                 if (dev->n_erased_blocks < min_erased)
2573                         aggressive = 1;
2574                 else {
2575                         if(!background && erased_chunks > (dev->n_free_chunks / 4))
2576                                 break;
2577
2578                         if(dev->gc_skip > 20)
2579                                 dev->gc_skip = 20;
2580                         if(erased_chunks < dev->n_free_chunks/2 ||
2581                                 dev->gc_skip < 1 ||
2582                                 background)
2583                                 aggressive = 0;
2584                         else {
2585                                 dev->gc_skip--;
2586                                 break;
2587                         }
2588                 }
2589
2590                 dev->gc_skip = 5;
2591
2592                 /* If we don't already have a block being gc'd then see if we should start another */
2593
2594                 if (dev->gc_block < 1 && !aggressive) {
2595                         dev->gc_block = yaffs2_find_refresh_block(dev);
2596                         dev->gc_chunk = 0;
2597                         dev->n_clean_ups=0;
2598                 }
2599                 if (dev->gc_block < 1) {
2600                         dev->gc_block = yaffs_find_gc_block(dev, aggressive, background);
2601                         dev->gc_chunk = 0;
2602                         dev->n_clean_ups=0;
2603                 }
2604
2605                 if (dev->gc_block > 0) {
2606                         dev->all_gcs++;
2607                         if (!aggressive)
2608                                 dev->passive_gc_count++;
2609
2610                         T(YAFFS_TRACE_GC,
2611                           (TSTR
2612                            ("yaffs: GC n_erased_blocks %d aggressive %d" TENDSTR),
2613                            dev->n_erased_blocks, aggressive));
2614
2615                         gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2616                 }
2617
2618                 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) && dev->gc_block > 0) {
2619                         T(YAFFS_TRACE_GC,
2620                           (TSTR
2621                            ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d"
2622                             TENDSTR), dev->n_erased_blocks, max_tries, dev->gc_block));
2623                 }
2624         } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2625                  (dev->gc_block > 0) &&
2626                  (max_tries < 2));
2627
2628         return aggressive ? gc_ok : YAFFS_OK;
2629 }
2630
2631 /*
2632  * yaffs_bg_gc()
2633  * Garbage collects. Intended to be called from a background thread.
2634  * Returns non-zero if at least half the free chunks are erased.
2635  */
2636 int yaffs_bg_gc(yaffs_dev_t *dev, unsigned urgency)
2637 {
2638         int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2639
2640         T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR),urgency));
2641
2642         yaffs_check_gc(dev, 1);
2643         return erased_chunks > dev->n_free_chunks/2;
2644 }
2645
2646 /*-------------------------  TAGS --------------------------------*/
2647
2648 static int yaffs_tags_match(const yaffs_ext_tags *tags, int obj_id,
2649                            int chunk_obj)
2650 {
2651         return (tags->chunk_id == chunk_obj &&
2652                 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
2653
2654 }
2655
2656
2657 /*-------------------- Data file manipulation -----------------*/
2658
2659 static int yaffs_find_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
2660                                  yaffs_ext_tags *tags)
2661 {
2662         /*Get the Tnode, then get the level 0 offset chunk offset */
2663         yaffs_tnode_t *tn;
2664         int the_chunk = -1;
2665         yaffs_ext_tags local_tags;
2666         int ret_val = -1;
2667
2668         yaffs_dev_t *dev = in->my_dev;
2669
2670         if (!tags) {
2671                 /* Passed a NULL, so use our own tags space */
2672                 tags = &local_tags;
2673         }
2674
2675         tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2676
2677         if (tn) {
2678                 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2679
2680                 ret_val =
2681                     yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2682                                            inode_chunk);
2683         }
2684         return ret_val;
2685 }
2686
2687 static int yaffs_find_del_file_chunk(yaffs_obj_t *in, int inode_chunk,
2688                                           yaffs_ext_tags *tags)
2689 {
2690         /* Get the Tnode, then get the level 0 offset chunk offset */
2691         yaffs_tnode_t *tn;
2692         int the_chunk = -1;
2693         yaffs_ext_tags local_tags;
2694
2695         yaffs_dev_t *dev = in->my_dev;
2696         int ret_val = -1;
2697
2698         if (!tags) {
2699                 /* Passed a NULL, so use our own tags space */
2700                 tags = &local_tags;
2701         }
2702
2703         tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2704
2705         if (tn) {
2706
2707                 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2708
2709                 ret_val =
2710                     yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2711                                            inode_chunk);
2712
2713                 /* Delete the entry in the filestructure (if found) */
2714                 if (ret_val != -1)
2715                         yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
2716         }
2717
2718         return ret_val;
2719 }
2720
2721
2722 int yaffs_put_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
2723                                 int nand_chunk, int in_scan)
2724 {
2725         /* NB in_scan is zero unless scanning.
2726          * For forward scanning, in_scan is > 0;
2727          * for backward scanning in_scan is < 0
2728          *
2729          * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
2730          */
2731
2732         yaffs_tnode_t *tn;
2733         yaffs_dev_t *dev = in->my_dev;
2734         int existing_cunk;
2735         yaffs_ext_tags existing_tags;
2736         yaffs_ext_tags new_tags;
2737         unsigned existing_serial, new_serial;
2738
2739         if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
2740                 /* Just ignore an attempt at putting a chunk into a non-file during scanning
2741                  * If it is not during Scanning then something went wrong!
2742                  */
2743                 if (!in_scan) {
2744                         T(YAFFS_TRACE_ERROR,
2745                           (TSTR
2746                            ("yaffs tragedy:attempt to put data chunk into a non-file"
2747                             TENDSTR)));
2748                         YBUG();
2749                 }
2750
2751                 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
2752                 return YAFFS_OK;
2753         }
2754
2755         tn = yaffs_add_find_tnode_0(dev,
2756                                         &in->variant.file_variant,
2757                                         inode_chunk,
2758                                         NULL);
2759         if (!tn)
2760                 return YAFFS_FAIL;
2761         
2762         if(!nand_chunk)
2763                 /* Dummy insert, bail now */
2764                 return YAFFS_OK;
2765
2766         existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
2767
2768         if (in_scan != 0) {
2769                 /* If we're scanning then we need to test for duplicates
2770                  * NB This does not need to be efficient since it should only ever
2771                  * happen when the power fails during a write, then only one
2772                  * chunk should ever be affected.
2773                  *
2774                  * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
2775                  * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
2776                  */
2777
2778                 if (existing_cunk > 0) {
2779                         /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
2780                          *    thus we have to do a FindChunkInFile to get the real chunk id.
2781                          *
2782                          * We have a duplicate now we need to decide which one to use:
2783                          *
2784                          * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
2785                          * Forward scanning YAFFS2: The new one is what we use, dump the old one.
2786                          * YAFFS1: Get both sets of tags and compare serial numbers.
2787                          */
2788
2789                         if (in_scan > 0) {
2790                                 /* Only do this for forward scanning */
2791                                 yaffs_rd_chunk_tags_nand(dev,
2792                                                                 nand_chunk,
2793                                                                 NULL, &new_tags);
2794
2795                                 /* Do a proper find */
2796                                 existing_cunk =
2797                                     yaffs_find_chunk_in_file(in, inode_chunk,
2798                                                           &existing_tags);
2799                         }
2800
2801                         if (existing_cunk <= 0) {
2802                                 /*Hoosterman - how did this happen? */
2803
2804                                 T(YAFFS_TRACE_ERROR,
2805                                   (TSTR
2806                                    ("yaffs tragedy: existing chunk < 0 in scan"
2807                                     TENDSTR)));
2808
2809                         }
2810
2811                         /* NB The deleted flags should be false, otherwise the chunks will
2812                          * not be loaded during a scan
2813                          */
2814
2815                         if (in_scan > 0) {
2816                                 new_serial = new_tags.serial_number;
2817                                 existing_serial = existing_tags.serial_number;
2818                         }
2819
2820                         if ((in_scan > 0) &&
2821                             (existing_cunk <= 0 ||
2822                              ((existing_serial + 1) & 3) == new_serial)) {
2823                                 /* Forward scanning.
2824                                  * Use new
2825                                  * Delete the old one and drop through to update the tnode
2826                                  */
2827                                 yaffs_chunk_del(dev, existing_cunk, 1,
2828                                                   __LINE__);
2829                         } else {
2830                                 /* Backward scanning or we want to use the existing one
2831                                  * Use existing.
2832                                  * Delete the new one and return early so that the tnode isn't changed
2833                                  */
2834                                 yaffs_chunk_del(dev, nand_chunk, 1,
2835                                                   __LINE__);
2836                                 return YAFFS_OK;
2837                         }
2838                 }
2839
2840         }
2841
2842         if (existing_cunk == 0)
2843                 in->n_data_chunks++;
2844
2845         yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
2846
2847         return YAFFS_OK;
2848 }
2849
2850 static int yaffs_rd_data_obj(yaffs_obj_t *in, int inode_chunk,
2851                                         __u8 *buffer)
2852 {
2853         int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2854
2855         if (nand_chunk >= 0)
2856                 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2857                                                 buffer, NULL);
2858         else {
2859                 T(YAFFS_TRACE_NANDACCESS,
2860                   (TSTR("Chunk %d not found zero instead" TENDSTR),
2861                    nand_chunk));
2862                 /* get sane (zero) data if you read a hole */
2863                 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
2864                 return 0;
2865         }
2866
2867 }
2868
2869 void yaffs_chunk_del(yaffs_dev_t *dev, int chunk_id, int mark_flash, int lyn)
2870 {
2871         int block;
2872         int page;
2873         yaffs_ext_tags tags;
2874         yaffs_block_info_t *bi;
2875
2876         if (chunk_id <= 0)
2877                 return;
2878
2879         dev->n_deletions++;
2880         block = chunk_id / dev->param.chunks_per_block;
2881         page = chunk_id % dev->param.chunks_per_block;
2882
2883
2884         if (!yaffs_check_chunk_bit(dev, block, page))
2885                 T(YAFFS_TRACE_VERIFY,
2886                         (TSTR("Deleting invalid chunk %d"TENDSTR),
2887                          chunk_id));
2888
2889         bi = yaffs_get_block_info(dev, block);
2890         
2891         yaffs2_update_oldest_dirty_seq(dev, block, bi);
2892
2893         T(YAFFS_TRACE_DELETION,
2894           (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
2895
2896         if (!dev->param.is_yaffs2 && mark_flash &&
2897             bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
2898
2899                 yaffs_init_tags(&tags);
2900
2901                 tags.is_deleted = 1;
2902
2903                 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
2904                 yaffs_handle_chunk_update(dev, chunk_id, &tags);
2905         } else {
2906                 dev->n_unmarked_deletions++;
2907         }
2908
2909         /* Pull out of the management area.
2910          * If the whole block became dirty, this will kick off an erasure.
2911          */
2912         if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
2913             bi->block_state == YAFFS_BLOCK_STATE_FULL ||
2914             bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
2915             bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2916                 dev->n_free_chunks++;
2917
2918                 yaffs_clear_chunk_bit(dev, block, page);
2919
2920                 bi->pages_in_use--;
2921
2922                 if (bi->pages_in_use == 0 &&
2923                     !bi->has_shrink_hdr &&
2924                     bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
2925                     bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
2926                         yaffs_block_became_dirty(dev, block);
2927                 }
2928
2929         }
2930
2931 }
2932
2933 static int yaffs_wr_data_obj(yaffs_obj_t *in, int inode_chunk,
2934                                         const __u8 *buffer, int n_bytes,
2935                                         int use_reserve)
2936 {
2937         /* Find old chunk Need to do this to get serial number
2938          * Write new one and patch into tree.
2939          * Invalidate old tags.
2940          */
2941
2942         int prev_chunk_id;
2943         yaffs_ext_tags prev_tags;
2944
2945         int new_chunk_id;
2946         yaffs_ext_tags new_tags;
2947
2948         yaffs_dev_t *dev = in->my_dev;
2949
2950         yaffs_check_gc(dev,0);
2951
2952         /* Get the previous chunk at this location in the file if it exists.
2953          * If it does not exist then put a zero into the tree. This creates
2954          * the tnode now, rather than later when it is harder to clean up.
2955          */
2956         prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
2957         if(prev_chunk_id < 1 &&
2958                 !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
2959                 return 0;
2960
2961         /* Set up new tags */
2962         yaffs_init_tags(&new_tags);
2963
2964         new_tags.chunk_id = inode_chunk;
2965         new_tags.obj_id = in->obj_id;
2966         new_tags.serial_number =
2967             (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
2968         new_tags.n_bytes = n_bytes;
2969
2970         if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
2971                 T(YAFFS_TRACE_ERROR,
2972                 (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), n_bytes));
2973                 YBUG();
2974         }
2975         
2976                 
2977         new_chunk_id =
2978             yaffs_write_new_chunk(dev, buffer, &new_tags,
2979                                               use_reserve);
2980
2981         if (new_chunk_id > 0) {
2982                 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
2983
2984                 if (prev_chunk_id > 0)
2985                         yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
2986
2987                 yaffs_verify_file_sane(in);
2988         }
2989         return new_chunk_id;
2990
2991 }
2992
2993 /* UpdateObjectHeader updates the header on NAND for an object.
2994  * If name is not NULL, then that new name is used.
2995  */
2996 int yaffs_update_oh(yaffs_obj_t *in, const YCHAR *name, int force,
2997                              int is_shrink, int shadows, yaffs_xattr_mod *xmod)
2998 {
2999
3000         yaffs_block_info_t *bi;
3001
3002         yaffs_dev_t *dev = in->my_dev;
3003
3004         int prev_chunk_id;
3005         int ret_val = 0;
3006         int result = 0;
3007
3008         int new_chunk_id;
3009         yaffs_ext_tags new_tags;
3010         yaffs_ext_tags old_tags;
3011         const YCHAR *alias = NULL;
3012
3013         __u8 *buffer = NULL;
3014         YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3015
3016         yaffs_obj_header *oh = NULL;
3017
3018         yaffs_strcpy(old_name, _Y("silly old name"));
3019
3020
3021         if (!in->fake ||
3022                 in == dev->root_dir || /* The root_dir should also be saved */
3023                 force  || xmod) {
3024
3025                 yaffs_check_gc(dev,0);
3026                 yaffs_check_obj_details_loaded(in);
3027
3028                 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
3029                 oh = (yaffs_obj_header *) buffer;
3030
3031                 prev_chunk_id = in->hdr_chunk;
3032
3033                 if (prev_chunk_id > 0) {
3034                         result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3035                                                         buffer, &old_tags);
3036
3037                         yaffs_verify_oh(in, oh, &old_tags, 0);
3038
3039                         memcpy(old_name, oh->name, sizeof(oh->name));
3040                         memset(buffer, 0xFF, sizeof(yaffs_obj_header));
3041                 } else
3042                         memset(buffer, 0xFF, dev->data_bytes_per_chunk);
3043
3044                 oh->type = in->variant_type;
3045                 oh->yst_mode = in->yst_mode;
3046                 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3047
3048 #ifdef CONFIG_YAFFS_WINCE
3049                 oh->win_atime[0] = in->win_atime[0];
3050                 oh->win_ctime[0] = in->win_ctime[0];
3051                 oh->win_mtime[0] = in->win_mtime[0];
3052                 oh->win_atime[1] = in->win_atime[1];
3053                 oh->win_ctime[1] = in->win_ctime[1];
3054                 oh->win_mtime[1] = in->win_mtime[1];
3055 #else
3056                 oh->yst_uid = in->yst_uid;
3057                 oh->yst_gid = in->yst_gid;
3058                 oh->yst_atime = in->yst_atime;
3059                 oh->yst_mtime = in->yst_mtime;
3060                 oh->yst_ctime = in->yst_ctime;
3061                 oh->yst_rdev = in->yst_rdev;
3062 #endif
3063                 if (in->parent)
3064                         oh->parent_obj_id = in->parent->obj_id;
3065                 else
3066                         oh->parent_obj_id = 0;
3067
3068                 if (name && *name) {
3069                         memset(oh->name, 0, sizeof(oh->name));
3070                         yaffs_load_oh_from_name(dev,oh->name,name);
3071                 } else if (prev_chunk_id > 0)
3072                         memcpy(oh->name, old_name, sizeof(oh->name));
3073                 else
3074                         memset(oh->name, 0, sizeof(oh->name));
3075
3076                 oh->is_shrink = is_shrink;
3077
3078                 switch (in->variant_type) {
3079                 case YAFFS_OBJECT_TYPE_UNKNOWN:
3080                         /* Should not happen */
3081                         break;
3082                 case YAFFS_OBJECT_TYPE_FILE:
3083                         oh->file_size =
3084                             (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
3085                              || oh->parent_obj_id ==
3086                              YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
3087                             file_variant.file_size;
3088                         break;
3089                 case YAFFS_OBJECT_TYPE_HARDLINK:
3090                         oh->equiv_id =
3091                             in->variant.hardlink_variant.equiv_id;
3092                         break;
3093                 case YAFFS_OBJECT_TYPE_SPECIAL:
3094                         /* Do nothing */
3095                         break;
3096                 case YAFFS_OBJECT_TYPE_DIRECTORY:
3097                         /* Do nothing */
3098                         break;
3099                 case YAFFS_OBJECT_TYPE_SYMLINK:
3100                         alias = in->variant.symlink_variant.alias;
3101                         if(!alias)
3102                                 alias = _Y("no alias");
3103                         yaffs_strncpy(oh->alias,
3104                                         alias,
3105                                       YAFFS_MAX_ALIAS_LENGTH);
3106                         oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3107                         break;
3108                 }
3109
3110                 /* process any xattrib modifications */
3111                 if(xmod)
3112                         yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3113
3114
3115                 /* Tags */
3116                 yaffs_init_tags(&new_tags);
3117                 in->serial++;
3118                 new_tags.chunk_id = 0;
3119                 new_tags.obj_id = in->obj_id;
3120                 new_tags.serial_number = in->serial;
3121
3122                 /* Add extra info for file header */
3123
3124                 new_tags.extra_available = 1;
3125                 new_tags.extra_parent_id = oh->parent_obj_id;
3126                 new_tags.extra_length = oh->file_size;
3127                 new_tags.extra_is_shrink = oh->is_shrink;
3128                 new_tags.extra_equiv_id = oh->equiv_id;
3129                 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3130                 new_tags.extra_obj_type = in->variant_type;
3131
3132                 yaffs_verify_oh(in, oh, &new_tags, 1);
3133
3134                 /* Create new chunk in NAND */
3135                 new_chunk_id =
3136                     yaffs_write_new_chunk(dev, buffer, &new_tags,
3137                                                       (prev_chunk_id > 0) ? 1 : 0);
3138
3139                 if (new_chunk_id >= 0) {
3140
3141                         in->hdr_chunk = new_chunk_id;
3142
3143                         if (prev_chunk_id > 0) {
3144                                 yaffs_chunk_del(dev, prev_chunk_id, 1,
3145                                                   __LINE__);
3146                         }
3147
3148                         if (!yaffs_obj_cache_dirty(in))
3149                                 in->dirty = 0;
3150
3151                         /* If this was a shrink, then mark the block that the chunk lives on */
3152                         if (is_shrink) {
3153                                 bi = yaffs_get_block_info(in->my_dev,
3154                                         new_chunk_id / in->my_dev->param.chunks_per_block);
3155                                 bi->has_shrink_hdr = 1;
3156                         }
3157
3158                 }
3159
3160                 ret_val = new_chunk_id;
3161
3162         }
3163
3164         if (buffer)
3165                 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3166
3167         return ret_val;
3168 }
3169
3170 /*------------------------ Short Operations Cache ----------------------------------------
3171  *   In many situations where there is no high level buffering (eg WinCE) a lot of
3172  *   reads might be short sequential reads, and a lot of writes may be short
3173  *   sequential writes. eg. scanning/writing a jpeg file.
3174  *   In these cases, a short read/write cache can provide a huge perfomance benefit
3175  *   with dumb-as-a-rock code.
3176  *   In Linux, the page cache provides read buffering aand the short op cache provides write
3177  *   buffering.
3178  *
3179  *   There are a limited number (~10) of cache chunks per device so that we don't
3180  *   need a very intelligent search.
3181  */
3182
3183 static int yaffs_obj_cache_dirty(yaffs_obj_t *obj)
3184 {
3185         yaffs_dev_t *dev = obj->my_dev;
3186         int i;
3187         yaffs_cache_t *cache;
3188         int n_caches = obj->my_dev->param.n_caches;
3189
3190         for (i = 0; i < n_caches; i++) {
3191                 cache = &dev->cache[i];
3192                 if (cache->object == obj &&
3193                     cache->dirty)
3194                         return 1;
3195         }
3196
3197         return 0;
3198 }
3199
3200
3201 static void yaffs_flush_file_cache(yaffs_obj_t *obj)
3202 {
3203         yaffs_dev_t *dev = obj->my_dev;
3204         int lowest = -99;       /* Stop compiler whining. */
3205         int i;
3206         yaffs_cache_t *cache;
3207         int chunk_written = 0;
3208         int n_caches = obj->my_dev->param.n_caches;
3209
3210         if (n_caches > 0) {
3211                 do {
3212                         cache = NULL;
3213
3214                         /* Find the dirty cache for this object with the lowest chunk id. */
3215                         for (i = 0; i < n_caches; i++) {
3216                                 if (dev->cache[i].object == obj &&
3217                                     dev->cache[i].dirty) {
3218                                         if (!cache
3219                                             || dev->cache[i].chunk_id <
3220                                             lowest) {
3221                                                 cache = &dev->cache[i];
3222                                                 lowest = cache->chunk_id;
3223                                         }
3224                                 }
3225                         }
3226
3227                         if (cache && !cache->locked) {
3228                                 /* Write it out and free it up */
3229
3230                                 chunk_written =
3231                                     yaffs_wr_data_obj(cache->object,
3232                                                                  cache->chunk_id,
3233                                                                  cache->data,
3234                                                                  cache->n_bytes,
3235                                                                  1);
3236                                 cache->dirty = 0;
3237                                 cache->object = NULL;
3238                         }
3239
3240                 } while (cache && chunk_written > 0);
3241
3242                 if (cache) {
3243                         /* Hoosterman, disk full while writing cache out. */
3244                         T(YAFFS_TRACE_ERROR,
3245                           (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3246
3247                 }
3248         }
3249
3250 }
3251
3252 /*yaffs_flush_whole_cache(dev)
3253  *
3254  *
3255  */
3256
3257 void yaffs_flush_whole_cache(yaffs_dev_t *dev)
3258 {
3259         yaffs_obj_t *obj;
3260         int n_caches = dev->param.n_caches;
3261         int i;
3262
3263         /* Find a dirty object in the cache and flush it...
3264          * until there are no further dirty objects.
3265          */
3266         do {
3267                 obj = NULL;
3268                 for (i = 0; i < n_caches && !obj; i++) {
3269                         if (dev->cache[i].object &&
3270                             dev->cache[i].dirty)
3271                                 obj = dev->cache[i].object;
3272
3273                 }
3274                 if (obj)
3275                         yaffs_flush_file_cache(obj);
3276
3277         } while (obj);
3278
3279 }
3280
3281
3282 /* Grab us a cache chunk for use.