yaffs Fixing some minor warnings in direct/timothy_tests/linux_tests
[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 #include "yaffs_attribs.h"
37
38 /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
39 #define YAFFS_GC_GOOD_ENOUGH 2
40 #define YAFFS_GC_PASSIVE_THRESHOLD 4
41
42 #include "yaffs_ecc.h"
43
44 /* Forward declarations */
45
46 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
47                              const u8 * buffer, int n_bytes, int use_reserve);
48
49
50
51 /* Function to calculate chunk and offset */
52
53 static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
54                                 int *chunk_out, u32 * offset_out)
55 {
56         int chunk;
57         u32 offset;
58
59         chunk = (u32) (addr >> dev->chunk_shift);
60
61         if (dev->chunk_div == 1) {
62                 /* easy power of 2 case */
63                 offset = (u32) (addr & dev->chunk_mask);
64         } else {
65                 /* Non power-of-2 case */
66
67                 loff_t chunk_base;
68
69                 chunk /= dev->chunk_div;
70
71                 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
72                 offset = (u32) (addr - chunk_base);
73         }
74
75         *chunk_out = chunk;
76         *offset_out = offset;
77 }
78
79 /* Function to return the number of shifts for a power of 2 greater than or
80  * equal to the given number
81  * Note we don't try to cater for all possible numbers and this does not have to
82  * be hellishly efficient.
83  */
84
85 static u32 calc_shifts_ceiling(u32 x)
86 {
87         int extra_bits;
88         int shifts;
89
90         shifts = extra_bits = 0;
91
92         while (x > 1) {
93                 if (x & 1)
94                         extra_bits++;
95                 x >>= 1;
96                 shifts++;
97         }
98
99         if (extra_bits)
100                 shifts++;
101
102         return shifts;
103 }
104
105 /* Function to return the number of shifts to get a 1 in bit 0
106  */
107
108 static u32 calc_shifts(u32 x)
109 {
110         u32 shifts;
111
112         shifts = 0;
113
114         if (!x)
115                 return 0;
116
117         while (!(x & 1)) {
118                 x >>= 1;
119                 shifts++;
120         }
121
122         return shifts;
123 }
124
125 /*
126  * Temporary buffer manipulations.
127  */
128
129 static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
130 {
131         int i;
132         u8 *buf = (u8 *) 1;
133
134         memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
135
136         for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
137                 dev->temp_buffer[i].line = 0;   /* not in use */
138                 dev->temp_buffer[i].buffer = buf =
139                     kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
140         }
141
142         return buf ? YAFFS_OK : YAFFS_FAIL;
143 }
144
145 u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
146 {
147         int i, j;
148
149         dev->temp_in_use++;
150         if (dev->temp_in_use > dev->max_temp)
151                 dev->max_temp = dev->temp_in_use;
152
153         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
154                 if (dev->temp_buffer[i].line == 0) {
155                         dev->temp_buffer[i].line = line_no;
156                         if ((i + 1) > dev->max_temp) {
157                                 dev->max_temp = i + 1;
158                                 for (j = 0; j <= i; j++)
159                                         dev->temp_buffer[j].max_line =
160                                             dev->temp_buffer[j].line;
161                         }
162
163                         return dev->temp_buffer[i].buffer;
164                 }
165         }
166
167         yaffs_trace(YAFFS_TRACE_BUFFERS,
168                 "Out of temp buffers at line %d, other held by lines:",
169                 line_no);
170         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
171                 yaffs_trace(YAFFS_TRACE_BUFFERS," %d", dev->temp_buffer[i].line);
172
173         /*
174          * If we got here then we have to allocate an unmanaged one
175          * This is not good.
176          */
177
178         dev->unmanaged_buffer_allocs++;
179         return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
180
181 }
182
183 void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
184 {
185         int i;
186
187         dev->temp_in_use--;
188
189         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
190                 if (dev->temp_buffer[i].buffer == buffer) {
191                         dev->temp_buffer[i].line = 0;
192                         return;
193                 }
194         }
195
196         if (buffer) {
197                 /* assume it is an unmanaged one. */
198                 yaffs_trace(YAFFS_TRACE_BUFFERS,
199                   "Releasing unmanaged temp buffer in line %d",
200                    line_no);
201                 kfree(buffer);
202                 dev->unmanaged_buffer_deallocs++;
203         }
204
205 }
206
207 /*
208  * Determine if we have a managed buffer.
209  */
210 int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
211 {
212         int i;
213
214         for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
215                 if (dev->temp_buffer[i].buffer == buffer)
216                         return 1;
217         }
218
219         for (i = 0; i < dev->param.n_caches; i++) {
220                 if (dev->cache[i].data == buffer)
221                         return 1;
222         }
223
224         if (buffer == dev->checkpt_buffer)
225                 return 1;
226
227         yaffs_trace(YAFFS_TRACE_ALWAYS,
228           "yaffs: unmaged buffer detected.");
229         return 0;
230 }
231
232 /*
233  * Functions for robustisizing TODO
234  *
235  */
236
237 static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
238                                      const u8 * data,
239                                      const struct yaffs_ext_tags *tags)
240 {
241         dev = dev;
242         nand_chunk = nand_chunk;
243         data = data;
244         tags = tags;
245 }
246
247 static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
248                                       const struct yaffs_ext_tags *tags)
249 {
250         dev = dev;
251         nand_chunk = nand_chunk;
252         tags = tags;
253 }
254
255 void yaffs_handle_chunk_error(struct yaffs_dev *dev,
256                               struct yaffs_block_info *bi)
257 {
258         if (!bi->gc_prioritise) {
259                 bi->gc_prioritise = 1;
260                 dev->has_pending_prioritised_gc = 1;
261                 bi->chunk_error_strikes++;
262
263                 if (bi->chunk_error_strikes > 3) {
264                         bi->needs_retiring = 1; /* Too many stikes, so retire this */
265                         yaffs_trace(YAFFS_TRACE_ALWAYS, "yaffs: Block struck out");
266
267                 }
268         }
269 }
270
271 static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
272                                         int erased_ok)
273 {
274         int flash_block = nand_chunk / dev->param.chunks_per_block;
275         struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
276
277         yaffs_handle_chunk_error(dev, bi);
278
279         if (erased_ok) {
280                 /* Was an actual write failure, so mark the block for retirement  */
281                 bi->needs_retiring = 1;
282                 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
283                   "**>> Block %d needs retiring", flash_block);
284         }
285
286         /* Delete the chunk */
287         yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
288         yaffs_skip_rest_of_block(dev);
289 }
290
291 /*
292  * Verification code
293  */
294
295 /*
296  *  Simple hash function. Needs to have a reasonable spread
297  */
298
299 static inline int yaffs_hash_fn(int n)
300 {
301         n = abs(n);
302         return n % YAFFS_NOBJECT_BUCKETS;
303 }
304
305 /*
306  * Access functions to useful fake objects.
307  * Note that root might have a presence in NAND if permissions are set.
308  */
309
310 struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
311 {
312         return dev->root_dir;
313 }
314
315 struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
316 {
317         return dev->lost_n_found;
318 }
319
320 /*
321  *  Erased NAND checking functions
322  */
323
324 int yaffs_check_ff(u8 * buffer, int n_bytes)
325 {
326         /* Horrible, slow implementation */
327         while (n_bytes--) {
328                 if (*buffer != 0xFF)
329                         return 0;
330                 buffer++;
331         }
332         return 1;
333 }
334
335 static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
336 {
337         int retval = YAFFS_OK;
338         u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
339         struct yaffs_ext_tags tags;
340         int result;
341
342         result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
343
344         if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
345                 retval = YAFFS_FAIL;
346
347         if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
348                 tags.chunk_used) {
349                 yaffs_trace(YAFFS_TRACE_NANDACCESS, "Chunk %d not erased", nand_chunk);
350                 retval = YAFFS_FAIL;
351         }
352
353         yaffs_release_temp_buffer(dev, data, __LINE__);
354
355         return retval;
356
357 }
358
359 static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
360                                       int nand_chunk,
361                                       const u8 * data,
362                                       struct yaffs_ext_tags *tags)
363 {
364         int retval = YAFFS_OK;
365         struct yaffs_ext_tags temp_tags;
366         u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
367         int result;
368
369         result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
370         if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
371             temp_tags.obj_id != tags->obj_id ||
372             temp_tags.chunk_id != tags->chunk_id ||
373             temp_tags.n_bytes != tags->n_bytes)
374                 retval = YAFFS_FAIL;
375
376         yaffs_release_temp_buffer(dev, buffer, __LINE__);
377
378         return retval;
379 }
380
381
382 int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
383 {
384         int reserved_chunks;
385         int reserved_blocks = dev->param.n_reserved_blocks;
386         int checkpt_blocks;
387
388         checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
389
390         reserved_chunks =
391             ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
392
393         return (dev->n_free_chunks > (reserved_chunks + n_chunks));
394 }
395
396 static int yaffs_find_alloc_block(struct yaffs_dev *dev)
397 {
398         int i;
399
400         struct yaffs_block_info *bi;
401
402         if (dev->n_erased_blocks < 1) {
403                 /* Hoosterman we've got a problem.
404                  * Can't get space to gc
405                  */
406                 yaffs_trace(YAFFS_TRACE_ERROR,
407                   "yaffs tragedy: no more erased blocks" );
408
409                 return -1;
410         }
411
412         /* Find an empty block. */
413
414         for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
415                 dev->alloc_block_finder++;
416                 if (dev->alloc_block_finder < dev->internal_start_block
417                     || dev->alloc_block_finder > dev->internal_end_block) {
418                         dev->alloc_block_finder = dev->internal_start_block;
419                 }
420
421                 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
422
423                 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
424                         bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
425                         dev->seq_number++;
426                         bi->seq_number = dev->seq_number;
427                         dev->n_erased_blocks--;
428                         yaffs_trace(YAFFS_TRACE_ALLOCATE,
429                           "Allocated block %d, seq  %d, %d left" ,
430                            dev->alloc_block_finder, dev->seq_number,
431                            dev->n_erased_blocks);
432                         return dev->alloc_block_finder;
433                 }
434         }
435
436         yaffs_trace(YAFFS_TRACE_ALWAYS,
437                 "yaffs tragedy: no more erased blocks, but there should have been %d",
438                 dev->n_erased_blocks);
439
440         return -1;
441 }
442
443 static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
444                              struct yaffs_block_info **block_ptr)
445 {
446         int ret_val;
447         struct yaffs_block_info *bi;
448
449         if (dev->alloc_block < 0) {
450                 /* Get next block to allocate off */
451                 dev->alloc_block = yaffs_find_alloc_block(dev);
452                 dev->alloc_page = 0;
453         }
454
455         if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
456                 /* Not enough space to allocate unless we're allowed to use the reserve. */
457                 return -1;
458         }
459
460         if (dev->n_erased_blocks < dev->param.n_reserved_blocks
461             && dev->alloc_page == 0)
462                 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
463
464         /* Next page please.... */
465         if (dev->alloc_block >= 0) {
466                 bi = yaffs_get_block_info(dev, dev->alloc_block);
467
468                 ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
469                     dev->alloc_page;
470                 bi->pages_in_use++;
471                 yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
472
473                 dev->alloc_page++;
474
475                 dev->n_free_chunks--;
476
477                 /* If the block is full set the state to full */
478                 if (dev->alloc_page >= dev->param.chunks_per_block) {
479                         bi->block_state = YAFFS_BLOCK_STATE_FULL;
480                         dev->alloc_block = -1;
481                 }
482
483                 if (block_ptr)
484                         *block_ptr = bi;
485
486                 return ret_val;
487         }
488
489         yaffs_trace(YAFFS_TRACE_ERROR, "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" );
490
491         return -1;
492 }
493
494 static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
495 {
496         int n;
497
498         n = dev->n_erased_blocks * dev->param.chunks_per_block;
499
500         if (dev->alloc_block > 0)
501                 n += (dev->param.chunks_per_block - dev->alloc_page);
502
503         return n;
504
505 }
506
507 /*
508  * yaffs_skip_rest_of_block() skips over the rest of the allocation block
509  * if we don't want to write to it.
510  */
511 void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
512 {
513         if (dev->alloc_block > 0) {
514                 struct yaffs_block_info *bi =
515                     yaffs_get_block_info(dev, dev->alloc_block);
516                 if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
517                         bi->block_state = YAFFS_BLOCK_STATE_FULL;
518                         dev->alloc_block = -1;
519                 }
520         }
521 }
522
523 static int yaffs_write_new_chunk(struct yaffs_dev *dev,
524                                  const u8 * data,
525                                  struct yaffs_ext_tags *tags, int use_reserver)
526 {
527         int attempts = 0;
528         int write_ok = 0;
529         int chunk;
530
531         yaffs2_checkpt_invalidate(dev);
532
533         do {
534                 struct yaffs_block_info *bi = 0;
535                 int erased_ok = 0;
536
537                 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
538                 if (chunk < 0) {
539                         /* no space */
540                         break;
541                 }
542
543                 /* First check this chunk is erased, if it needs
544                  * checking.  The checking policy (unless forced
545                  * always on) is as follows:
546                  *
547                  * Check the first page we try to write in a block.
548                  * If the check passes then we don't need to check any
549                  * more.        If the check fails, we check again...
550                  * If the block has been erased, we don't need to check.
551                  *
552                  * However, if the block has been prioritised for gc,
553                  * then we think there might be something odd about
554                  * this block and stop using it.
555                  *
556                  * Rationale: We should only ever see chunks that have
557                  * not been erased if there was a partially written
558                  * chunk due to power loss.  This checking policy should
559                  * catch that case with very few checks and thus save a
560                  * lot of checks that are most likely not needed.
561                  *
562                  * Mods to the above
563                  * If an erase check fails or the write fails we skip the 
564                  * rest of the block.
565                  */
566
567                 /* let's give it a try */
568                 attempts++;
569
570                 if (dev->param.always_check_erased)
571                         bi->skip_erased_check = 0;
572
573                 if (!bi->skip_erased_check) {
574                         erased_ok = yaffs_check_chunk_erased(dev, chunk);
575                         if (erased_ok != YAFFS_OK) {
576                                 yaffs_trace(YAFFS_TRACE_ERROR,
577                                   "**>> yaffs chunk %d was not erased",
578                                   chunk);
579
580                                 /* If not erased, delete this one,
581                                  * skip rest of block and
582                                  * try another chunk */
583                                 yaffs_chunk_del(dev, chunk, 1, __LINE__);
584                                 yaffs_skip_rest_of_block(dev);
585                                 continue;
586                         }
587                 }
588
589                 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
590
591                 if (!bi->skip_erased_check)
592                         write_ok =
593                             yaffs_verify_chunk_written(dev, chunk, data, tags);
594
595                 if (write_ok != YAFFS_OK) {
596                         /* Clean up aborted write, skip to next block and
597                          * try another chunk */
598                         yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
599                         continue;
600                 }
601
602                 bi->skip_erased_check = 1;
603
604                 /* Copy the data into the robustification buffer */
605                 yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
606
607         } while (write_ok != YAFFS_OK &&
608                  (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
609
610         if (!write_ok)
611                 chunk = -1;
612
613         if (attempts > 1) {
614                 yaffs_trace(YAFFS_TRACE_ERROR,
615                         "**>> yaffs write required %d attempts",
616                         attempts);
617                 dev->n_retired_writes += (attempts - 1);
618         }
619
620         return chunk;
621 }
622
623 /*
624  * Block retiring for handling a broken block.
625  */
626
627 static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
628 {
629         struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
630
631         yaffs2_checkpt_invalidate(dev);
632
633         yaffs2_clear_oldest_dirty_seq(dev, bi);
634
635         if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
636                 if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
637                         yaffs_trace(YAFFS_TRACE_ALWAYS,
638                                 "yaffs: Failed to mark bad and erase block %d",
639                                 flash_block);
640                 } else {
641                         struct yaffs_ext_tags tags;
642                         int chunk_id =
643                             flash_block * dev->param.chunks_per_block;
644
645                         u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
646
647                         memset(buffer, 0xff, dev->data_bytes_per_chunk);
648                         yaffs_init_tags(&tags);
649                         tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
650                         if (dev->param.write_chunk_tags_fn(dev, chunk_id -
651                                                            dev->chunk_offset,
652                                                            buffer,
653                                                            &tags) != YAFFS_OK)
654                                 yaffs_trace(YAFFS_TRACE_ALWAYS,
655                                         "yaffs: Failed to write bad block marker to block %d",
656                                         flash_block);
657
658                         yaffs_release_temp_buffer(dev, buffer, __LINE__);
659                 }
660         }
661
662         bi->block_state = YAFFS_BLOCK_STATE_DEAD;
663         bi->gc_prioritise = 0;
664         bi->needs_retiring = 0;
665
666         dev->n_retired_blocks++;
667 }
668
669 /*---------------- Name handling functions ------------*/
670
671 static u16 yaffs_calc_name_sum(const YCHAR * name)
672 {
673         u16 sum = 0;
674         u16 i = 1;
675
676         const YUCHAR *bname = (const YUCHAR *)name;
677         if (bname) {
678                 while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
679
680                         /* 0x1f mask is case insensitive */
681                         sum += ((*bname) & 0x1f) * i;
682                         i++;
683                         bname++;
684                 }
685         }
686         return sum;
687 }
688
689 void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
690 {
691 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
692         memset(obj->short_name, 0, sizeof(obj->short_name));
693         if (name && 
694                 strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
695             YAFFS_SHORT_NAME_LENGTH)
696                 strcpy(obj->short_name, name);
697         else
698                 obj->short_name[0] = _Y('\0');
699 #endif
700         obj->sum = yaffs_calc_name_sum(name);
701 }
702
703 void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
704                                 const struct yaffs_obj_hdr *oh)
705 {
706 #ifdef CONFIG_YAFFS_AUTO_UNICODE
707         YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
708         memset(tmp_name, 0, sizeof(tmp_name));
709         yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
710                                 YAFFS_MAX_NAME_LENGTH + 1);
711         yaffs_set_obj_name(obj, tmp_name);
712 #else
713         yaffs_set_obj_name(obj, oh->name);
714 #endif
715 }
716
717 /*-------------------- TNODES -------------------
718
719  * List of spare tnodes
720  * The list is hooked together using the first pointer
721  * in the tnode.
722  */
723
724 struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
725 {
726         struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
727         if (tn) {
728                 memset(tn, 0, dev->tnode_size);
729                 dev->n_tnodes++;
730         }
731
732         dev->checkpoint_blocks_required = 0;    /* force recalculation */
733
734         return tn;
735 }
736
737 /* FreeTnode frees up a tnode and puts it back on the free list */
738 static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
739 {
740         yaffs_free_raw_tnode(dev, tn);
741         dev->n_tnodes--;
742         dev->checkpoint_blocks_required = 0;    /* force recalculation */
743 }
744
745 static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
746 {
747         yaffs_deinit_raw_tnodes_and_objs(dev);
748         dev->n_obj = 0;
749         dev->n_tnodes = 0;
750 }
751
752 void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
753                         unsigned pos, unsigned val)
754 {
755         u32 *map = (u32 *) tn;
756         u32 bit_in_map;
757         u32 bit_in_word;
758         u32 word_in_map;
759         u32 mask;
760
761         pos &= YAFFS_TNODES_LEVEL0_MASK;
762         val >>= dev->chunk_grp_bits;
763
764         bit_in_map = pos * dev->tnode_width;
765         word_in_map = bit_in_map / 32;
766         bit_in_word = bit_in_map & (32 - 1);
767
768         mask = dev->tnode_mask << bit_in_word;
769
770         map[word_in_map] &= ~mask;
771         map[word_in_map] |= (mask & (val << bit_in_word));
772
773         if (dev->tnode_width > (32 - bit_in_word)) {
774                 bit_in_word = (32 - bit_in_word);
775                 word_in_map++;;
776                 mask =
777                     dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
778                 map[word_in_map] &= ~mask;
779                 map[word_in_map] |= (mask & (val >> bit_in_word));
780         }
781 }
782
783 u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
784                          unsigned pos)
785 {
786         u32 *map = (u32 *) tn;
787         u32 bit_in_map;
788         u32 bit_in_word;
789         u32 word_in_map;
790         u32 val;
791
792         pos &= YAFFS_TNODES_LEVEL0_MASK;
793
794         bit_in_map = pos * dev->tnode_width;
795         word_in_map = bit_in_map / 32;
796         bit_in_word = bit_in_map & (32 - 1);
797
798         val = map[word_in_map] >> bit_in_word;
799
800         if (dev->tnode_width > (32 - bit_in_word)) {
801                 bit_in_word = (32 - bit_in_word);
802                 word_in_map++;;
803                 val |= (map[word_in_map] << bit_in_word);
804         }
805
806         val &= dev->tnode_mask;
807         val <<= dev->chunk_grp_bits;
808
809         return val;
810 }
811
812 /* ------------------- End of individual tnode manipulation -----------------*/
813
814 /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
815  * The look up tree is represented by the top tnode and the number of top_level
816  * in the tree. 0 means only the level 0 tnode is in the tree.
817  */
818
819 /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
820 struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
821                                        struct yaffs_file_var *file_struct,
822                                        u32 chunk_id)
823 {
824         struct yaffs_tnode *tn = file_struct->top;
825         u32 i;
826         int required_depth;
827         int level = file_struct->top_level;
828
829         dev = dev;
830
831         /* Check sane level and chunk Id */
832         if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
833                 return NULL;
834
835         if (chunk_id > YAFFS_MAX_CHUNK_ID)
836                 return NULL;
837
838         /* First check we're tall enough (ie enough top_level) */
839
840         i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
841         required_depth = 0;
842         while (i) {
843                 i >>= YAFFS_TNODES_INTERNAL_BITS;
844                 required_depth++;
845         }
846
847         if (required_depth > file_struct->top_level)
848                 return NULL;    /* Not tall enough, so we can't find it */
849
850         /* Traverse down to level 0 */
851         while (level > 0 && tn) {
852                 tn = tn->internal[(chunk_id >>
853                                    (YAFFS_TNODES_LEVEL0_BITS +
854                                     (level - 1) *
855                                     YAFFS_TNODES_INTERNAL_BITS)) &
856                                   YAFFS_TNODES_INTERNAL_MASK];
857                 level--;
858         }
859
860         return tn;
861 }
862
863 /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
864  * This happens in two steps:
865  *  1. If the tree isn't tall enough, then make it taller.
866  *  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
867  *
868  * Used when modifying the tree.
869  *
870  *  If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
871  *  be plugged into the ttree.
872  */
873
874 struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
875                                            struct yaffs_file_var *file_struct,
876                                            u32 chunk_id,
877                                            struct yaffs_tnode *passed_tn)
878 {
879         int required_depth;
880         int i;
881         int l;
882         struct yaffs_tnode *tn;
883
884         u32 x;
885
886         /* Check sane level and page Id */
887         if (file_struct->top_level < 0
888             || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
889                 return NULL;
890
891         if (chunk_id > YAFFS_MAX_CHUNK_ID)
892                 return NULL;
893
894         /* First check we're tall enough (ie enough top_level) */
895
896         x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
897         required_depth = 0;
898         while (x) {
899                 x >>= YAFFS_TNODES_INTERNAL_BITS;
900                 required_depth++;
901         }
902
903         if (required_depth > file_struct->top_level) {
904                 /* Not tall enough, gotta make the tree taller */
905                 for (i = file_struct->top_level; i < required_depth; i++) {
906
907                         tn = yaffs_get_tnode(dev);
908
909                         if (tn) {
910                                 tn->internal[0] = file_struct->top;
911                                 file_struct->top = tn;
912                                 file_struct->top_level++;
913                         } else {
914                                 yaffs_trace(YAFFS_TRACE_ERROR, "yaffs: no more tnodes");
915                                 return NULL;
916                         }
917                 }
918         }
919
920         /* Traverse down to level 0, adding anything we need */
921
922         l = file_struct->top_level;
923         tn = file_struct->top;
924
925         if (l > 0) {
926                 while (l > 0 && tn) {
927                         x = (chunk_id >>
928                              (YAFFS_TNODES_LEVEL0_BITS +
929                               (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
930                             YAFFS_TNODES_INTERNAL_MASK;
931
932                         if ((l > 1) && !tn->internal[x]) {
933                                 /* Add missing non-level-zero tnode */
934                                 tn->internal[x] = yaffs_get_tnode(dev);
935                                 if (!tn->internal[x])
936                                         return NULL;
937                         } else if (l == 1) {
938                                 /* Looking from level 1 at level 0 */
939                                 if (passed_tn) {
940                                         /* If we already have one, then release it. */
941                                         if (tn->internal[x])
942                                                 yaffs_free_tnode(dev,
943                                                                  tn->
944                                                                  internal[x]);
945                                         tn->internal[x] = passed_tn;
946
947                                 } else if (!tn->internal[x]) {
948                                         /* Don't have one, none passed in */
949                                         tn->internal[x] = yaffs_get_tnode(dev);
950                                         if (!tn->internal[x])
951                                                 return NULL;
952                                 }
953                         }
954
955                         tn = tn->internal[x];
956                         l--;
957                 }
958         } else {
959                 /* top is level 0 */
960                 if (passed_tn) {
961                         memcpy(tn, passed_tn,
962                                (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
963                         yaffs_free_tnode(dev, passed_tn);
964                 }
965         }
966
967         return tn;
968 }
969
970 static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
971                             int chunk_obj)
972 {
973         return (tags->chunk_id == chunk_obj &&
974                 tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
975
976 }
977
978 static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
979                                      struct yaffs_ext_tags *tags, int obj_id,
980                                      int inode_chunk)
981 {
982         int j;
983
984         for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
985                 if (yaffs_check_chunk_bit
986                     (dev, the_chunk / dev->param.chunks_per_block,
987                      the_chunk % dev->param.chunks_per_block)) {
988
989                         if (dev->chunk_grp_size == 1)
990                                 return the_chunk;
991                         else {
992                                 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
993                                                          tags);
994                                 if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
995                                         /* found it; */
996                                         return the_chunk;
997                                 }
998                         }
999                 }
1000                 the_chunk++;
1001         }
1002         return -1;
1003 }
1004
1005 static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1006                                     struct yaffs_ext_tags *tags)
1007 {
1008         /*Get the Tnode, then get the level 0 offset chunk offset */
1009         struct yaffs_tnode *tn;
1010         int the_chunk = -1;
1011         struct yaffs_ext_tags local_tags;
1012         int ret_val = -1;
1013
1014         struct yaffs_dev *dev = in->my_dev;
1015
1016         if (!tags) {
1017                 /* Passed a NULL, so use our own tags space */
1018                 tags = &local_tags;
1019         }
1020
1021         tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1022
1023         if (tn) {
1024                 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1025
1026                 ret_val =
1027                     yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1028                                               inode_chunk);
1029         }
1030         return ret_val;
1031 }
1032
1033 static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1034                                      struct yaffs_ext_tags *tags)
1035 {
1036         /* Get the Tnode, then get the level 0 offset chunk offset */
1037         struct yaffs_tnode *tn;
1038         int the_chunk = -1;
1039         struct yaffs_ext_tags local_tags;
1040
1041         struct yaffs_dev *dev = in->my_dev;
1042         int ret_val = -1;
1043
1044         if (!tags) {
1045                 /* Passed a NULL, so use our own tags space */
1046                 tags = &local_tags;
1047         }
1048
1049         tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
1050
1051         if (tn) {
1052
1053                 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
1054
1055                 ret_val =
1056                     yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1057                                               inode_chunk);
1058
1059                 /* Delete the entry in the filestructure (if found) */
1060                 if (ret_val != -1)
1061                         yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
1062         }
1063
1064         return ret_val;
1065 }
1066
1067 int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
1068                             int nand_chunk, int in_scan)
1069 {
1070         /* NB in_scan is zero unless scanning.
1071          * For forward scanning, in_scan is > 0;
1072          * for backward scanning in_scan is < 0
1073          *
1074          * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
1075          */
1076
1077         struct yaffs_tnode *tn;
1078         struct yaffs_dev *dev = in->my_dev;
1079         int existing_cunk;
1080         struct yaffs_ext_tags existing_tags;
1081         struct yaffs_ext_tags new_tags;
1082         unsigned existing_serial, new_serial;
1083
1084         if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
1085                 /* Just ignore an attempt at putting a chunk into a non-file during scanning
1086                  * If it is not during Scanning then something went wrong!
1087                  */
1088                 if (!in_scan) {
1089                         yaffs_trace(YAFFS_TRACE_ERROR,
1090                                 "yaffs tragedy:attempt to put data chunk into a non-file"
1091                                 );
1092                         YBUG();
1093                 }
1094
1095                 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1096                 return YAFFS_OK;
1097         }
1098
1099         tn = yaffs_add_find_tnode_0(dev,
1100                                     &in->variant.file_variant,
1101                                     inode_chunk, NULL);
1102         if (!tn)
1103                 return YAFFS_FAIL;
1104
1105         if (!nand_chunk)
1106                 /* Dummy insert, bail now */
1107                 return YAFFS_OK;
1108
1109         existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
1110
1111         if (in_scan != 0) {
1112                 /* If we're scanning then we need to test for duplicates
1113                  * NB This does not need to be efficient since it should only ever
1114                  * happen when the power fails during a write, then only one
1115                  * chunk should ever be affected.
1116                  *
1117                  * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
1118                  * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
1119                  */
1120
1121                 if (existing_cunk > 0) {
1122                         /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
1123                          *    thus we have to do a FindChunkInFile to get the real chunk id.
1124                          *
1125                          * We have a duplicate now we need to decide which one to use:
1126                          *
1127                          * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
1128                          * Forward scanning YAFFS2: The new one is what we use, dump the old one.
1129                          * YAFFS1: Get both sets of tags and compare serial numbers.
1130                          */
1131
1132                         if (in_scan > 0) {
1133                                 /* Only do this for forward scanning */
1134                                 yaffs_rd_chunk_tags_nand(dev,
1135                                                          nand_chunk,
1136                                                          NULL, &new_tags);
1137
1138                                 /* Do a proper find */
1139                                 existing_cunk =
1140                                     yaffs_find_chunk_in_file(in, inode_chunk,
1141                                                              &existing_tags);
1142                         }
1143
1144                         if (existing_cunk <= 0) {
1145                                 /*Hoosterman - how did this happen? */
1146
1147                                 yaffs_trace(YAFFS_TRACE_ERROR,
1148                                         "yaffs tragedy: existing chunk < 0 in scan"
1149                                         );
1150
1151                         }
1152
1153                         /* NB The deleted flags should be false, otherwise the chunks will
1154                          * not be loaded during a scan
1155                          */
1156
1157                         if (in_scan > 0) {
1158                                 new_serial = new_tags.serial_number;
1159                                 existing_serial = existing_tags.serial_number;
1160                         }
1161
1162                         if ((in_scan > 0) &&
1163                             (existing_cunk <= 0 ||
1164                              ((existing_serial + 1) & 3) == new_serial)) {
1165                                 /* Forward scanning.
1166                                  * Use new
1167                                  * Delete the old one and drop through to update the tnode
1168                                  */
1169                                 yaffs_chunk_del(dev, existing_cunk, 1,
1170                                                 __LINE__);
1171                         } else {
1172                                 /* Backward scanning or we want to use the existing one
1173                                  * Use existing.
1174                                  * Delete the new one and return early so that the tnode isn't changed
1175                                  */
1176                                 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1177                                 return YAFFS_OK;
1178                         }
1179                 }
1180
1181         }
1182
1183         if (existing_cunk == 0)
1184                 in->n_data_chunks++;
1185
1186         yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1187
1188         return YAFFS_OK;
1189 }
1190
1191 static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
1192 {
1193         struct yaffs_block_info *the_block;
1194         unsigned block_no;
1195
1196         yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
1197
1198         block_no = chunk / dev->param.chunks_per_block;
1199         the_block = yaffs_get_block_info(dev, block_no);
1200         if (the_block) {
1201                 the_block->soft_del_pages++;
1202                 dev->n_free_chunks++;
1203                 yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
1204         }
1205 }
1206
1207 /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1208  * All soft deleting does is increment the block's softdelete count and pulls the chunk out
1209  * of the tnode.
1210  * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1211  */
1212
1213 static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1214                                  u32 level, int chunk_offset)
1215 {
1216         int i;
1217         int the_chunk;
1218         int all_done = 1;
1219         struct yaffs_dev *dev = in->my_dev;
1220
1221         if (tn) {
1222                 if (level > 0) {
1223
1224                         for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
1225                              i--) {
1226                                 if (tn->internal[i]) {
1227                                         all_done =
1228                                             yaffs_soft_del_worker(in,
1229                                                                   tn->internal
1230                                                                   [i],
1231                                                                   level - 1,
1232                                                                   (chunk_offset
1233                                                                    <<
1234                                                                    YAFFS_TNODES_INTERNAL_BITS)
1235                                                                   + i);
1236                                         if (all_done) {
1237                                                 yaffs_free_tnode(dev,
1238                                                                  tn->internal
1239                                                                  [i]);
1240                                                 tn->internal[i] = NULL;
1241                                         } else {
1242                                                 /* Hoosterman... how could this happen? */
1243                                         }
1244                                 }
1245                         }
1246                         return (all_done) ? 1 : 0;
1247                 } else if (level == 0) {
1248
1249                         for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
1250                                 the_chunk = yaffs_get_group_base(dev, tn, i);
1251                                 if (the_chunk) {
1252                                         /* Note this does not find the real chunk, only the chunk group.
1253                                          * We make an assumption that a chunk group is not larger than
1254                                          * a block.
1255                                          */
1256                                         yaffs_soft_del_chunk(dev, the_chunk);
1257                                         yaffs_load_tnode_0(dev, tn, i, 0);
1258                                 }
1259
1260                         }
1261                         return 1;
1262
1263                 }
1264
1265         }
1266
1267         return 1;
1268
1269 }
1270
1271 static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
1272 {
1273         struct yaffs_dev *dev = obj->my_dev;
1274         struct yaffs_obj *parent;
1275
1276         yaffs_verify_obj_in_dir(obj);
1277         parent = obj->parent;
1278
1279         yaffs_verify_dir(parent);
1280
1281         if (dev && dev->param.remove_obj_fn)
1282                 dev->param.remove_obj_fn(obj);
1283
1284         list_del_init(&obj->siblings);
1285         obj->parent = NULL;
1286
1287         yaffs_verify_dir(parent);
1288 }
1289
1290 void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
1291 {
1292         if (!directory) {
1293                 yaffs_trace(YAFFS_TRACE_ALWAYS,
1294                         "tragedy: Trying to add an object to a null pointer directory"
1295                         );
1296                 YBUG();
1297                 return;
1298         }
1299         if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1300                 yaffs_trace(YAFFS_TRACE_ALWAYS,
1301                         "tragedy: Trying to add an object to a non-directory"
1302                         );
1303                 YBUG();
1304         }
1305
1306         if (obj->siblings.prev == NULL) {
1307                 /* Not initialised */
1308                 YBUG();
1309         }
1310
1311         yaffs_verify_dir(directory);
1312
1313         yaffs_remove_obj_from_dir(obj);
1314
1315         /* Now add it */
1316         list_add(&obj->siblings, &directory->variant.dir_variant.children);
1317         obj->parent = directory;
1318
1319         if (directory == obj->my_dev->unlinked_dir
1320             || directory == obj->my_dev->del_dir) {
1321                 obj->unlinked = 1;
1322                 obj->my_dev->n_unlinked_files++;
1323                 obj->rename_allowed = 0;
1324         }
1325
1326         yaffs_verify_dir(directory);
1327         yaffs_verify_obj_in_dir(obj);
1328 }
1329
1330 static int yaffs_change_obj_name(struct yaffs_obj *obj,
1331                                  struct yaffs_obj *new_dir,
1332                                  const YCHAR * new_name, int force, int shadows)
1333 {
1334         int unlink_op;
1335         int del_op;
1336
1337         struct yaffs_obj *existing_target;
1338
1339         if (new_dir == NULL)
1340                 new_dir = obj->parent;  /* use the old directory */
1341
1342         if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
1343                 yaffs_trace(YAFFS_TRACE_ALWAYS,
1344                         "tragedy: yaffs_change_obj_name: new_dir is not a directory"
1345                         );
1346                 YBUG();
1347         }
1348
1349         /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
1350         if (obj->my_dev->param.is_yaffs2)
1351                 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1352         else
1353                 unlink_op = (new_dir == obj->my_dev->unlinked_dir
1354                              && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
1355
1356         del_op = (new_dir == obj->my_dev->del_dir);
1357
1358         existing_target = yaffs_find_by_name(new_dir, new_name);
1359
1360         /* If the object is a file going into the unlinked directory,
1361          *   then it is OK to just stuff it in since duplicate names are allowed.
1362          *   else only proceed if the new name does not exist and if we're putting
1363          *   it into a directory.
1364          */
1365         if ((unlink_op ||
1366              del_op ||
1367              force ||
1368              (shadows > 0) ||
1369              !existing_target) &&
1370             new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1371                 yaffs_set_obj_name(obj, new_name);
1372                 obj->dirty = 1;
1373
1374                 yaffs_add_obj_to_dir(new_dir, obj);
1375
1376                 if (unlink_op)
1377                         obj->unlinked = 1;
1378
1379                 /* If it is a deletion then we mark it as a shrink for gc purposes. */
1380                 if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
1381                     0)
1382                         return YAFFS_OK;
1383         }
1384
1385         return YAFFS_FAIL;
1386 }
1387
1388 /*------------------------ Short Operations Cache ----------------------------------------
1389  *   In many situations where there is no high level buffering  a lot of
1390  *   reads might be short sequential reads, and a lot of writes may be short
1391  *   sequential writes. eg. scanning/writing a jpeg file.
1392  *   In these cases, a short read/write cache can provide a huge perfomance
1393  *   benefit with dumb-as-a-rock code.
1394  *   In Linux, the page cache provides read buffering and the short op cache 
1395  *   provides write buffering.
1396  *
1397  *   There are a limited number (~10) of cache chunks per device so that we don't
1398  *   need a very intelligent search.
1399  */
1400
1401 static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
1402 {
1403         struct yaffs_dev *dev = obj->my_dev;
1404         int i;
1405         struct yaffs_cache *cache;
1406         int n_caches = obj->my_dev->param.n_caches;
1407
1408         for (i = 0; i < n_caches; i++) {
1409                 cache = &dev->cache[i];
1410                 if (cache->object == obj && cache->dirty)
1411                         return 1;
1412         }
1413
1414         return 0;
1415 }
1416
1417 static void yaffs_flush_file_cache(struct yaffs_obj *obj)
1418 {
1419         struct yaffs_dev *dev = obj->my_dev;
1420         int lowest = -99;       /* Stop compiler whining. */
1421         int i;
1422         struct yaffs_cache *cache;
1423         int chunk_written = 0;
1424         int n_caches = obj->my_dev->param.n_caches;
1425
1426         if (n_caches > 0) {
1427                 do {
1428                         cache = NULL;
1429
1430                         /* Find the dirty cache for this object with the lowest chunk id. */
1431                         for (i = 0; i < n_caches; i++) {
1432                                 if (dev->cache[i].object == obj &&
1433                                     dev->cache[i].dirty) {
1434                                         if (!cache
1435                                             || dev->cache[i].chunk_id <
1436                                             lowest) {
1437                                                 cache = &dev->cache[i];
1438                                                 lowest = cache->chunk_id;
1439                                         }
1440                                 }
1441                         }
1442
1443                         if (cache && !cache->locked) {
1444                                 /* Write it out and free it up */
1445
1446                                 chunk_written =
1447                                     yaffs_wr_data_obj(cache->object,
1448                                                       cache->chunk_id,
1449                                                       cache->data,
1450                                                       cache->n_bytes, 1);
1451                                 cache->dirty = 0;
1452                                 cache->object = NULL;
1453                         }
1454
1455                 } while (cache && chunk_written > 0);
1456
1457                 if (cache)
1458                         /* Hoosterman, disk full while writing cache out. */
1459                         yaffs_trace(YAFFS_TRACE_ERROR,
1460                                 "yaffs tragedy: no space during cache write");
1461
1462         }
1463
1464 }
1465
1466 /*yaffs_flush_whole_cache(dev)
1467  *
1468  *
1469  */
1470
1471 void yaffs_flush_whole_cache(struct yaffs_dev *dev)
1472 {
1473         struct yaffs_obj *obj;
1474         int n_caches = dev->param.n_caches;
1475         int i;
1476
1477         /* Find a dirty object in the cache and flush it...
1478          * until there are no further dirty objects.
1479          */
1480         do {
1481                 obj = NULL;
1482                 for (i = 0; i < n_caches && !obj; i++) {
1483                         if (dev->cache[i].object && dev->cache[i].dirty)
1484                                 obj = dev->cache[i].object;
1485
1486                 }
1487                 if (obj)
1488                         yaffs_flush_file_cache(obj);
1489
1490         } while (obj);
1491
1492 }
1493
1494 /* Grab us a cache chunk for use.
1495  * First look for an empty one.
1496  * Then look for the least recently used non-dirty one.
1497  * Then look for the least recently used dirty one...., flush and look again.
1498  */
1499 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
1500 {
1501         int i;
1502
1503         if (dev->param.n_caches > 0) {
1504                 for (i = 0; i < dev->param.n_caches; i++) {
1505                         if (!dev->cache[i].object)
1506                                 return &dev->cache[i];
1507                 }
1508         }
1509
1510         return NULL;
1511 }
1512
1513 static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
1514 {
1515         struct yaffs_cache *cache;
1516         struct yaffs_obj *the_obj;
1517         int usage;
1518         int i;
1519         int pushout;
1520
1521         if (dev->param.n_caches > 0) {
1522                 /* Try find a non-dirty one... */
1523
1524                 cache = yaffs_grab_chunk_worker(dev);
1525
1526                 if (!cache) {
1527                         /* They were all dirty, find the last recently used object and flush
1528                          * its cache, then  find again.
1529                          * NB what's here is not very accurate, we actually flush the object
1530                          * the last recently used page.
1531                          */
1532
1533                         /* With locking we can't assume we can use entry zero */
1534
1535                         the_obj = dev->cache[0].object;
1536                         usage = -1;
1537                         cache = NULL;
1538                         pushout = -1;
1539
1540                         for (i = 0; i < dev->param.n_caches; i++) {
1541                                 if (dev->cache[i].object &&
1542                                     !dev->cache[i].locked &&
1543                                     (dev->cache[i].last_use < usage
1544                                      || !cache)) {
1545                                         usage = dev->cache[i].last_use;
1546                                         the_obj = dev->cache[i].object;
1547                                         cache = &dev->cache[i];
1548                                         pushout = i;
1549                                 }
1550                         }
1551
1552                         if (!cache || cache->dirty) {
1553                                 /* Flush and try again */
1554                                 yaffs_flush_file_cache(the_obj);
1555                                 cache = yaffs_grab_chunk_worker(dev);
1556                         }
1557
1558                 }
1559                 return cache;
1560         } else {
1561                 return NULL;
1562         }
1563 }
1564
1565 /* Find a cached chunk */
1566 static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1567                                                   int chunk_id)
1568 {
1569         struct yaffs_dev *dev = obj->my_dev;
1570         int i;
1571         if (dev->param.n_caches > 0) {
1572                 for (i = 0; i < dev->param.n_caches; i++) {
1573                         if (dev->cache[i].object == obj &&
1574                             dev->cache[i].chunk_id == chunk_id) {
1575                                 dev->cache_hits++;
1576
1577                                 return &dev->cache[i];
1578                         }
1579                 }
1580         }
1581         return NULL;
1582 }
1583
1584 /* Mark the chunk for the least recently used algorithym */
1585 static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1586                             int is_write)
1587 {
1588
1589         if (dev->param.n_caches > 0) {
1590                 if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
1591                         /* Reset the cache usages */
1592                         int i;
1593                         for (i = 1; i < dev->param.n_caches; i++)
1594                                 dev->cache[i].last_use = 0;
1595
1596                         dev->cache_last_use = 0;
1597                 }
1598
1599                 dev->cache_last_use++;
1600
1601                 cache->last_use = dev->cache_last_use;
1602
1603                 if (is_write)
1604                         cache->dirty = 1;
1605         }
1606 }
1607
1608 /* Invalidate a single cache page.
1609  * Do this when a whole page gets written,
1610  * ie the short cache for this page is no longer valid.
1611  */
1612 static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
1613 {
1614         if (object->my_dev->param.n_caches > 0) {
1615                 struct yaffs_cache *cache =
1616                     yaffs_find_chunk_cache(object, chunk_id);
1617
1618                 if (cache)
1619                         cache->object = NULL;
1620         }
1621 }
1622
1623 /* Invalidate all the cache pages associated with this object
1624  * Do this whenever ther file is deleted or resized.
1625  */
1626 static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1627 {
1628         int i;
1629         struct yaffs_dev *dev = in->my_dev;
1630
1631         if (dev->param.n_caches > 0) {
1632                 /* Invalidate it. */
1633                 for (i = 0; i < dev->param.n_caches; i++) {
1634                         if (dev->cache[i].object == in)
1635                                 dev->cache[i].object = NULL;
1636                 }
1637         }
1638 }
1639
1640 static void yaffs_unhash_obj(struct yaffs_obj *obj)
1641 {
1642         int bucket;
1643         struct yaffs_dev *dev = obj->my_dev;
1644
1645         /* If it is still linked into the bucket list, free from the list */
1646         if (!list_empty(&obj->hash_link)) {
1647                 list_del_init(&obj->hash_link);
1648                 bucket = yaffs_hash_fn(obj->obj_id);
1649                 dev->obj_bucket[bucket].count--;
1650         }
1651 }
1652
1653 /*  FreeObject frees up a Object and puts it back on the free list */
1654 static void yaffs_free_obj(struct yaffs_obj *obj)
1655 {
1656         struct yaffs_dev *dev = obj->my_dev;
1657
1658         yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
1659                 obj, obj->my_inode);
1660
1661         if (!obj)
1662                 YBUG();
1663         if (obj->parent)
1664                 YBUG();
1665         if (!list_empty(&obj->siblings))
1666                 YBUG();
1667
1668         if (obj->my_inode) {
1669                 /* We're still hooked up to a cached inode.
1670                  * Don't delete now, but mark for later deletion
1671                  */
1672                 obj->defered_free = 1;
1673                 return;
1674         }
1675
1676         yaffs_unhash_obj(obj);
1677
1678         yaffs_free_raw_obj(dev, obj);
1679         dev->n_obj--;
1680         dev->checkpoint_blocks_required = 0;    /* force recalculation */
1681 }
1682
1683 void yaffs_handle_defered_free(struct yaffs_obj *obj)
1684 {
1685         if (obj->defered_free)
1686                 yaffs_free_obj(obj);
1687 }
1688
1689 static int yaffs_generic_obj_del(struct yaffs_obj *in)
1690 {
1691
1692         /* First off, invalidate the file's data in the cache, without flushing. */
1693         yaffs_invalidate_whole_cache(in);
1694
1695         if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
1696                 /* Move to the unlinked directory so we have a record that it was deleted. */
1697                 yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
1698                                       0);
1699
1700         }
1701
1702         yaffs_remove_obj_from_dir(in);
1703         yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1704         in->hdr_chunk = 0;
1705
1706         yaffs_free_obj(in);
1707         return YAFFS_OK;
1708
1709 }
1710
1711 static void yaffs_soft_del_file(struct yaffs_obj *obj)
1712 {
1713         if (obj->deleted &&
1714             obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
1715                 if (obj->n_data_chunks <= 0) {
1716                         /* Empty file with no duplicate object headers,
1717                          * just delete it immediately */
1718                         yaffs_free_tnode(obj->my_dev,
1719                                          obj->variant.file_variant.top);
1720                         obj->variant.file_variant.top = NULL;
1721                         yaffs_trace(YAFFS_TRACE_TRACING,
1722                                 "yaffs: Deleting empty file %d",
1723                                 obj->obj_id);
1724                         yaffs_generic_obj_del(obj);
1725                 } else {
1726                         yaffs_soft_del_worker(obj,
1727                                               obj->variant.file_variant.top,
1728                                               obj->variant.
1729                                               file_variant.top_level, 0);
1730                         obj->soft_del = 1;
1731                 }
1732         }
1733 }
1734
1735 /* Pruning removes any part of the file structure tree that is beyond the
1736  * bounds of the file (ie that does not point to chunks).
1737  *
1738  * A file should only get pruned when its size is reduced.
1739  *
1740  * Before pruning, the chunks must be pulled from the tree and the
1741  * level 0 tnode entries must be zeroed out.
1742  * Could also use this for file deletion, but that's probably better handled
1743  * by a special case.
1744  *
1745  * This function is recursive. For levels > 0 the function is called again on
1746  * any sub-tree. For level == 0 we just check if the sub-tree has data.
1747  * If there is no data in a subtree then it is pruned.
1748  */
1749
1750 static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1751                                               struct yaffs_tnode *tn, u32 level,
1752                                               int del0)
1753 {
1754         int i;
1755         int has_data;
1756
1757         if (tn) {
1758                 has_data = 0;
1759
1760                 if (level > 0) {
1761                         for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
1762                                 if (tn->internal[i]) {
1763                                         tn->internal[i] =
1764                                             yaffs_prune_worker(dev,
1765                                                                tn->internal[i],
1766                                                                level - 1,
1767                                                                (i ==
1768                                                                 0) ? del0 : 1);
1769                                 }
1770
1771                                 if (tn->internal[i])
1772                                         has_data++;
1773                         }
1774                 } else {
1775                         int tnode_size_u32 = dev->tnode_size / sizeof(u32);
1776                         u32 *map = (u32 *) tn;
1777
1778                         for (i = 0; !has_data && i < tnode_size_u32; i++) {
1779                                 if (map[i])
1780                                         has_data++;
1781                         }
1782                 }
1783
1784                 if (has_data == 0 && del0) {
1785                         /* Free and return NULL */
1786
1787                         yaffs_free_tnode(dev, tn);
1788                         tn = NULL;
1789                 }
1790
1791         }
1792
1793         return tn;
1794
1795 }
1796
1797 static int yaffs_prune_tree(struct yaffs_dev *dev,
1798                             struct yaffs_file_var *file_struct)
1799 {
1800         int i;
1801         int has_data;
1802         int done = 0;
1803         struct yaffs_tnode *tn;
1804
1805         if (file_struct->top_level > 0) {
1806                 file_struct->top =
1807                     yaffs_prune_worker(dev, file_struct->top,
1808                                        file_struct->top_level, 0);
1809
1810                 /* Now we have a tree with all the non-zero branches NULL but the height
1811                  * is the same as it was.
1812                  * Let's see if we can trim internal tnodes to shorten the tree.
1813                  * We can do this if only the 0th element in the tnode is in use
1814                  * (ie all the non-zero are NULL)
1815                  */
1816
1817                 while (file_struct->top_level && !done) {
1818                         tn = file_struct->top;
1819
1820                         has_data = 0;
1821                         for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
1822                                 if (tn->internal[i])
1823                                         has_data++;
1824                         }
1825
1826                         if (!has_data) {
1827                                 file_struct->top = tn->internal[0];
1828                                 file_struct->top_level--;
1829                                 yaffs_free_tnode(dev, tn);
1830                         } else {
1831                                 done = 1;
1832                         }
1833                 }
1834         }
1835
1836         return YAFFS_OK;
1837 }
1838
1839 /*-------------------- End of File Structure functions.-------------------*/
1840
1841 /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
1842 static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
1843 {
1844         struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
1845
1846         if (obj) {
1847                 dev->n_obj++;
1848
1849                 /* Now sweeten it up... */
1850
1851                 memset(obj, 0, sizeof(struct yaffs_obj));
1852                 obj->being_created = 1;
1853
1854                 obj->my_dev = dev;
1855                 obj->hdr_chunk = 0;
1856                 obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
1857                 INIT_LIST_HEAD(&(obj->hard_links));
1858                 INIT_LIST_HEAD(&(obj->hash_link));
1859                 INIT_LIST_HEAD(&obj->siblings);
1860
1861                 /* Now make the directory sane */
1862                 if (dev->root_dir) {
1863                         obj->parent = dev->root_dir;
1864                         list_add(&(obj->siblings),
1865                                  &dev->root_dir->variant.dir_variant.children);
1866                 }
1867
1868                 /* Add it to the lost and found directory.
1869                  * NB Can't put root or lost-n-found in lost-n-found so
1870                  * check if lost-n-found exists first
1871                  */
1872                 if (dev->lost_n_found)
1873                         yaffs_add_obj_to_dir(dev->lost_n_found, obj);
1874
1875                 obj->being_created = 0;
1876         }
1877
1878         dev->checkpoint_blocks_required = 0;    /* force recalculation */
1879
1880         return obj;
1881 }
1882
1883 static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1884 {
1885         int i;
1886         int l = 999;
1887         int lowest = 999999;
1888
1889         /* Search for the shortest list or one that
1890          * isn't too long.
1891          */
1892
1893         for (i = 0; i < 10 && lowest > 4; i++) {
1894                 dev->bucket_finder++;
1895                 dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
1896                 if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
1897                         lowest = dev->obj_bucket[dev->bucket_finder].count;
1898                         l = dev->bucket_finder;
1899                 }
1900
1901         }
1902
1903         return l;
1904 }
1905
1906 static int yaffs_new_obj_id(struct yaffs_dev *dev)
1907 {
1908         int bucket = yaffs_find_nice_bucket(dev);
1909
1910         /* Now find an object value that has not already been taken
1911          * by scanning the list.
1912          */
1913
1914         int found = 0;
1915         struct list_head *i;
1916
1917         u32 n = (u32) bucket;
1918
1919         /* yaffs_check_obj_hash_sane();  */
1920
1921         while (!found) {
1922                 found = 1;
1923                 n += YAFFS_NOBJECT_BUCKETS;
1924                 if (1 || dev->obj_bucket[bucket].count > 0) {
1925                         list_for_each(i, &dev->obj_bucket[bucket].list) {
1926                                 /* If there is already one in the list */
1927                                 if (i && list_entry(i, struct yaffs_obj,
1928                                                     hash_link)->obj_id == n) {
1929                                         found = 0;
1930                                 }
1931                         }
1932                 }
1933         }
1934
1935         return n;
1936 }
1937
1938 static void yaffs_hash_obj(struct yaffs_obj *in)
1939 {
1940         int bucket = yaffs_hash_fn(in->obj_id);
1941         struct yaffs_dev *dev = in->my_dev;
1942
1943         list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1944         dev->obj_bucket[bucket].count++;
1945 }
1946
1947 struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
1948 {
1949         int bucket = yaffs_hash_fn(number);
1950         struct list_head *i;
1951         struct yaffs_obj *in;
1952
1953         list_for_each(i, &dev->obj_bucket[bucket].list) {
1954                 /* Look if it is in the list */
1955                 in = list_entry(i, struct yaffs_obj, hash_link);
1956                 if (in->obj_id == number) {
1957                         /* Don't tell the VFS about this one if it is defered free */
1958                         if (in->defered_free)
1959                                 return NULL;
1960                         return in;
1961                 }
1962         }
1963
1964         return NULL;
1965 }
1966
1967 struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
1968                                 enum yaffs_obj_type type)
1969 {
1970         struct yaffs_obj *the_obj = NULL;
1971         struct yaffs_tnode *tn = NULL;
1972
1973         if (number < 0)
1974                 number = yaffs_new_obj_id(dev);
1975
1976         if (type == YAFFS_OBJECT_TYPE_FILE) {
1977                 tn = yaffs_get_tnode(dev);
1978                 if (!tn)
1979                         return NULL;
1980         }
1981
1982         the_obj = yaffs_alloc_empty_obj(dev);
1983         if (!the_obj) {
1984                 if (tn)
1985                         yaffs_free_tnode(dev, tn);
1986                 return NULL;
1987         }
1988
1989         if (the_obj) {
1990                 the_obj->fake = 0;
1991                 the_obj->rename_allowed = 1;
1992                 the_obj->unlink_allowed = 1;
1993                 the_obj->obj_id = number;
1994                 yaffs_hash_obj(the_obj);
1995                 the_obj->variant_type = type;
1996                 yaffs_load_current_time(the_obj, 1, 1);
1997
1998                 switch (type) {
1999                 case YAFFS_OBJECT_TYPE_FILE:
2000                         the_obj->variant.file_variant.file_size = 0;
2001                         the_obj->variant.file_variant.scanned_size = 0;
2002                         the_obj->variant.file_variant.shrink_size = ~0; /* max */
2003                         the_obj->variant.file_variant.top_level = 0;
2004                         the_obj->variant.file_variant.top = tn;
2005                         break;
2006                 case YAFFS_OBJECT_TYPE_DIRECTORY:
2007                         INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
2008                         INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
2009                         break;
2010                 case YAFFS_OBJECT_TYPE_SYMLINK:
2011                 case YAFFS_OBJECT_TYPE_HARDLINK:
2012                 case YAFFS_OBJECT_TYPE_SPECIAL:
2013                         /* No action required */
2014                         break;
2015                 case YAFFS_OBJECT_TYPE_UNKNOWN:
2016                         /* todo this should not happen */
2017                         break;
2018                 }
2019         }
2020
2021         return the_obj;
2022 }
2023
2024 static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
2025                                                int number, u32 mode)
2026 {
2027
2028         struct yaffs_obj *obj =
2029             yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
2030         if (obj) {
2031                 obj->fake = 1;  /* it is fake so it might have no NAND presence... */
2032                 obj->rename_allowed = 0;        /* ... and we're not allowed to rename it... */
2033                 obj->unlink_allowed = 0;        /* ... or unlink it */
2034                 obj->deleted = 0;
2035                 obj->unlinked = 0;
2036                 obj->yst_mode = mode;
2037                 obj->my_dev = dev;
2038                 obj->hdr_chunk = 0;     /* Not a valid chunk. */
2039         }
2040
2041         return obj;
2042
2043 }
2044
2045
2046 static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
2047 {
2048         int i;
2049
2050         dev->n_obj = 0;
2051         dev->n_tnodes = 0;
2052
2053         yaffs_init_raw_tnodes_and_objs(dev);
2054
2055         for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
2056                 INIT_LIST_HEAD(&dev->obj_bucket[i].list);
2057                 dev->obj_bucket[i].count = 0;
2058         }
2059 }
2060
2061 struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
2062                                                  int number,
2063                                                  enum yaffs_obj_type type)
2064 {
2065         struct yaffs_obj *the_obj = NULL;
2066
2067         if (number > 0)
2068                 the_obj = yaffs_find_by_number(dev, number);
2069
2070         if (!the_obj)
2071                 the_obj = yaffs_new_obj(dev, number, type);
2072
2073         return the_obj;
2074
2075 }
2076
2077 YCHAR *yaffs_clone_str(const YCHAR * str)
2078 {
2079         YCHAR *new_str = NULL;
2080         int len;
2081
2082         if (!str)
2083                 str = _Y("");
2084
2085         len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
2086         new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
2087         if (new_str) {
2088                 strncpy(new_str, str, len);
2089                 new_str[len] = 0;
2090         }
2091         return new_str;
2092
2093 }
2094 /*
2095  *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
2096  * link (ie. name) is created or deleted in the directory.
2097  *
2098  * ie.
2099  *   create dir/a : update dir's mtime/ctime
2100  *   rm dir/a:   update dir's mtime/ctime
2101  *   modify dir/a: don't update dir's mtimme/ctime
2102  *
2103  * This can be handled immediately or defered. Defering helps reduce the number
2104  * of updates when many files in a directory are changed within a brief period.
2105  *
2106  * If the directory updating is defered then yaffs_update_dirty_dirs must be
2107  * called periodically.
2108  */
2109
2110 static void yaffs_update_parent(struct yaffs_obj *obj)
2111 {
2112         struct yaffs_dev *dev;
2113         if (!obj)
2114                 return;
2115         dev = obj->my_dev;
2116         obj->dirty = 1;
2117         yaffs_load_current_time(obj, 0, 1);
2118         if (dev->param.defered_dir_update) {
2119                 struct list_head *link = &obj->variant.dir_variant.dirty;
2120
2121                 if (list_empty(link)) {
2122                         list_add(link, &dev->dirty_dirs);
2123                         yaffs_trace(YAFFS_TRACE_BACKGROUND,
2124                           "Added object %d to dirty directories",
2125                            obj->obj_id);
2126                 }
2127
2128         } else {
2129                 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2130         }
2131 }
2132
2133 void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
2134 {
2135         struct list_head *link;
2136         struct yaffs_obj *obj;
2137         struct yaffs_dir_var *d_s;
2138         union yaffs_obj_var *o_v;
2139
2140         yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
2141
2142         while (!list_empty(&dev->dirty_dirs)) {
2143                 link = dev->dirty_dirs.next;
2144                 list_del_init(link);
2145
2146                 d_s = list_entry(link, struct yaffs_dir_var, dirty);
2147                 o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
2148                 obj = list_entry(o_v, struct yaffs_obj, variant);
2149
2150                 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2151                         obj->obj_id);
2152
2153                 if (obj->dirty)
2154                         yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2155         }
2156 }
2157
2158 /*
2159  * Mknod (create) a new object.
2160  * equiv_obj only has meaning for a hard link;
2161  * alias_str only has meaning for a symlink.
2162  * rdev only has meaning for devices (a subset of special objects)
2163  */
2164
2165 static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
2166                                           struct yaffs_obj *parent,
2167                                           const YCHAR * name,
2168                                           u32 mode,
2169                                           u32 uid,
2170                                           u32 gid,
2171                                           struct yaffs_obj *equiv_obj,
2172                                           const YCHAR * alias_str, u32 rdev)
2173 {
2174         struct yaffs_obj *in;
2175         YCHAR *str = NULL;
2176
2177         struct yaffs_dev *dev = parent->my_dev;
2178
2179         /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
2180         if (yaffs_find_by_name(parent, name))
2181                 return NULL;
2182
2183         if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2184                 str = yaffs_clone_str(alias_str);
2185                 if (!str)
2186                         return NULL;
2187         }
2188
2189         in = yaffs_new_obj(dev, -1, type);
2190
2191         if (!in) {
2192                 if (str)
2193                         kfree(str);
2194                 return NULL;
2195         }
2196
2197         if (in) {
2198                 in->hdr_chunk = 0;
2199                 in->valid = 1;
2200                 in->variant_type = type;
2201
2202                 in->yst_mode = mode;
2203
2204                 yaffs_attribs_init(in, gid, uid, rdev);
2205
2206                 in->n_data_chunks = 0;
2207
2208                 yaffs_set_obj_name(in, name);
2209                 in->dirty = 1;
2210
2211                 yaffs_add_obj_to_dir(parent, in);
2212
2213                 in->my_dev = parent->my_dev;
2214
2215                 switch (type) {
2216                 case YAFFS_OBJECT_TYPE_SYMLINK:
2217                         in->variant.symlink_variant.alias = str;
2218                         break;
2219                 case YAFFS_OBJECT_TYPE_HARDLINK:
2220                         in->variant.hardlink_variant.equiv_obj = equiv_obj;
2221                         in->variant.hardlink_variant.equiv_id =
2222                             equiv_obj->obj_id;
2223                         list_add(&in->hard_links, &equiv_obj->hard_links);
2224                         break;
2225                 case YAFFS_OBJECT_TYPE_FILE:
2226                 case YAFFS_OBJECT_TYPE_DIRECTORY:
2227                 case YAFFS_OBJECT_TYPE_SPECIAL:
2228                 case YAFFS_OBJECT_TYPE_UNKNOWN:
2229                         /* do nothing */
2230                         break;
2231                 }
2232
2233                 if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
2234                         /* Could not create the object header, fail the creation */
2235                         yaffs_del_obj(in);
2236                         in = NULL;
2237                 }
2238
2239                 yaffs_update_parent(parent);
2240         }
2241
2242         return in;
2243 }
2244
2245 struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2246                                     const YCHAR * name, u32 mode, u32 uid,
2247                                     u32 gid)
2248 {
2249         return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2250                                 uid, gid, NULL, NULL, 0);
2251 }
2252
2253 struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
2254                                    u32 mode, u32 uid, u32 gid)
2255 {
2256         return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
2257                                 mode, uid, gid, NULL, NULL, 0);
2258 }
2259
2260 struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
2261                                        const YCHAR * name, u32 mode, u32 uid,
2262                                        u32 gid, u32 rdev)
2263 {
2264         return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
2265                                 uid, gid, NULL, NULL, rdev);
2266 }
2267
2268 struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
2269                                        const YCHAR * name, u32 mode, u32 uid,
2270                                        u32 gid, const YCHAR * alias)
2271 {
2272         return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
2273                                 uid, gid, NULL, alias, 0);
2274 }
2275
2276 /* yaffs_link_obj returns the object id of the equivalent object.*/
2277 struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
2278                                  struct yaffs_obj *equiv_obj)
2279 {
2280         /* Get the real object in case we were fed a hard link as an equivalent object */
2281         equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
2282
2283         if (yaffs_create_obj
2284             (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
2285              equiv_obj, NULL, 0)) {
2286                 return equiv_obj;
2287         } else {
2288                 return NULL;
2289         }
2290
2291 }
2292
2293
2294
2295 /*------------------------- Block Management and Page Allocation ----------------*/
2296
2297 static int yaffs_init_blocks(struct yaffs_dev *dev)
2298 {
2299         int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
2300
2301         dev->block_info = NULL;
2302         dev->chunk_bits = NULL;
2303
2304         dev->alloc_block = -1;  /* force it to get a new one */
2305
2306         /* If the first allocation strategy fails, thry the alternate one */
2307         dev->block_info =
2308                 kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
2309         if (!dev->block_info) {
2310                 dev->block_info =
2311                     vmalloc(n_blocks * sizeof(struct yaffs_block_info));
2312                 dev->block_info_alt = 1;
2313         } else {
2314                 dev->block_info_alt = 0;
2315         }
2316
2317         if (dev->block_info) {
2318                 /* Set up dynamic blockinfo stuff. Round up bytes. */
2319                 dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
2320                 dev->chunk_bits =
2321                         kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
2322                 if (!dev->chunk_bits) {
2323                         dev->chunk_bits =
2324                             vmalloc(dev->chunk_bit_stride * n_blocks);
2325                         dev->chunk_bits_alt = 1;
2326                 } else {
2327                         dev->chunk_bits_alt = 0;
2328                 }
2329         }
2330
2331         if (dev->block_info && dev->chunk_bits) {
2332                 memset(dev->block_info, 0,
2333                        n_blocks * sizeof(struct yaffs_block_info));
2334                 memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
2335                 return YAFFS_OK;
2336         }
2337
2338         return YAFFS_FAIL;
2339 }
2340
2341 static void yaffs_deinit_blocks(struct yaffs_dev *dev)
2342 {
2343         if (dev->block_info_alt && dev->block_info)
2344                 vfree(dev->block_info);
2345         else if (dev->block_info)
2346                 kfree(dev->block_info);
2347
2348         dev->block_info_alt = 0;
2349
2350         dev->block_info = NULL;
2351
2352         if (dev->chunk_bits_alt && dev->chunk_bits)
2353                 vfree(dev->chunk_bits);
2354         else if (dev->chunk_bits)
2355                 kfree(dev->chunk_bits);
2356         dev->chunk_bits_alt = 0;
2357         dev->chunk_bits = NULL;
2358 }
2359
2360 void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
2361 {
2362         struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2363
2364         int erased_ok = 0;
2365
2366         /* If the block is still healthy erase it and mark as clean.
2367          * If the block has had a data failure, then retire it.
2368          */
2369
2370         yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
2371                 "yaffs_block_became_dirty block %d state %d %s",
2372                 block_no, bi->block_state,
2373                 (bi->needs_retiring) ? "needs retiring" : "");
2374
2375         yaffs2_clear_oldest_dirty_seq(dev, bi);
2376
2377         bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
2378
2379         /* If this is the block being garbage collected then stop gc'ing this block */
2380         if (block_no == dev->gc_block)
2381                 dev->gc_block = 0;
2382
2383         /* If this block is currently the best candidate for gc then drop as a candidate */
2384         if (block_no == dev->gc_dirtiest) {
2385                 dev->gc_dirtiest = 0;
2386                 dev->gc_pages_in_use = 0;
2387         }
2388
2389         if (!bi->needs_retiring) {
2390                 yaffs2_checkpt_invalidate(dev);
2391                 erased_ok = yaffs_erase_block(dev, block_no);
2392                 if (!erased_ok) {
2393                         dev->n_erase_failures++;
2394                         yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2395                           "**>> Erasure failed %d", block_no);
2396                 }
2397         }
2398
2399         if (erased_ok &&
2400             ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
2401              || !yaffs_skip_verification(dev))) {
2402                 int i;
2403                 for (i = 0; i < dev->param.chunks_per_block; i++) {
2404                         if (!yaffs_check_chunk_erased
2405                             (dev, block_no * dev->param.chunks_per_block + i)) {
2406                                 yaffs_trace(YAFFS_TRACE_ERROR,
2407                                         ">>Block %d erasure supposedly OK, but chunk %d not erased",
2408                                         block_no, i);
2409                         }
2410                 }
2411         }
2412
2413         if (erased_ok) {
2414                 /* Clean it up... */
2415                 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
2416                 bi->seq_number = 0;
2417                 dev->n_erased_blocks++;
2418                 bi->pages_in_use = 0;
2419                 bi->soft_del_pages = 0;
2420                 bi->has_shrink_hdr = 0;
2421                 bi->skip_erased_check = 1;      /* Clean, so no need to check */
2422                 bi->gc_prioritise = 0;
2423                 yaffs_clear_chunk_bits(dev, block_no);
2424
2425                 yaffs_trace(YAFFS_TRACE_ERASE,
2426                         "Erased block %d", block_no);
2427         } else {
2428                 /* We lost a block of free space */
2429                 dev->n_free_chunks -= dev->param.chunks_per_block;
2430                 yaffs_retire_block(dev, block_no);
2431                 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2432                         "**>> Block %d retired", block_no);
2433         }
2434 }
2435
2436
2437
2438 static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
2439 {
2440         int old_chunk;
2441         int new_chunk;
2442         int mark_flash;
2443         int ret_val = YAFFS_OK;
2444         int i;
2445         int is_checkpt_block;
2446         int matching_chunk;
2447         int max_copies;
2448
2449         int chunks_before = yaffs_get_erased_chunks(dev);
2450         int chunks_after;
2451
2452         struct yaffs_ext_tags tags;
2453
2454         struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
2455
2456         struct yaffs_obj *object;
2457
2458         is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
2459
2460         yaffs_trace(YAFFS_TRACE_TRACING,
2461                 "Collecting block %d, in use %d, shrink %d, whole_block %d",
2462                 block, bi->pages_in_use, bi->has_shrink_hdr,
2463                 whole_block);
2464
2465         /*yaffs_verify_free_chunks(dev); */
2466
2467         if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2468                 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
2469
2470         bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
2471
2472         dev->gc_disable = 1;
2473
2474         if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
2475                 yaffs_trace(YAFFS_TRACE_TRACING,
2476                         "Collecting block %d that has no chunks in use",
2477                         block);
2478                 yaffs_block_became_dirty(dev, block);
2479         } else {
2480
2481                 u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
2482
2483                 yaffs_verify_blk(dev, bi, block);
2484
2485                 max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
2486                 old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
2487
2488                 for ( /* init already done */ ;
2489                      ret_val == YAFFS_OK &&
2490                      dev->gc_chunk < dev->param.chunks_per_block &&
2491                      (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
2492                      max_copies > 0; dev->gc_chunk++, old_chunk++) {
2493                         if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
2494
2495                                 /* This page is in use and might need to be copied off */
2496
2497                                 max_copies--;
2498
2499                                 mark_flash = 1;
2500
2501                                 yaffs_init_tags(&tags);
2502
2503                                 yaffs_rd_chunk_tags_nand(dev, old_chunk,
2504                                                          buffer, &tags);
2505
2506                                 object = yaffs_find_by_number(dev, tags.obj_id);
2507
2508                                 yaffs_trace(YAFFS_TRACE_GC_DETAIL,
2509                                         "Collecting chunk in block %d, %d %d %d ",
2510                                         dev->gc_chunk, tags.obj_id,
2511                                         tags.chunk_id, tags.n_bytes);
2512
2513                                 if (object && !yaffs_skip_verification(dev)) {
2514                                         if (tags.chunk_id == 0)
2515                                                 matching_chunk =
2516                                                     object->hdr_chunk;
2517                                         else if (object->soft_del)
2518                                                 matching_chunk = old_chunk;     /* Defeat the test */
2519                                         else
2520                                                 matching_chunk =
2521                                                     yaffs_find_chunk_in_file
2522                                                     (object, tags.chunk_id,
2523                                                      NULL);
2524
2525                                         if (old_chunk != matching_chunk)
2526                                                 yaffs_trace(YAFFS_TRACE_ERROR,
2527                                                         "gc: page in gc mismatch: %d %d %d %d",
2528                                                         old_chunk,
2529                                                         matching_chunk,
2530                                                         tags.obj_id,
2531                                                         tags.chunk_id);
2532
2533                                 }
2534
2535                                 if (!object) {
2536                                         yaffs_trace(YAFFS_TRACE_ERROR,
2537                                                 "page %d in gc has no object: %d %d %d ",
2538                                                 old_chunk,
2539                                                 tags.obj_id, tags.chunk_id,
2540                                                 tags.n_bytes);
2541                                 }
2542
2543                                 if (object &&
2544                                     object->deleted &&
2545                                     object->soft_del && tags.chunk_id != 0) {
2546                                         /* Data chunk in a soft deleted file, throw it away
2547                                          * It's a soft deleted data chunk,
2548                                          * No need to copy this, just forget about it and
2549                                          * fix up the object.
2550                                          */
2551
2552                                         /* Free chunks already includes softdeleted chunks.
2553                                          * How ever this chunk is going to soon be really deleted
2554                                          * which will increment free chunks.
2555                                          * We have to decrement free chunks so this works out properly.
2556                                          */
2557                                         dev->n_free_chunks--;
2558                                         bi->soft_del_pages--;
2559
2560                                         object->n_data_chunks--;
2561
2562                                         if (object->n_data_chunks <= 0) {
2563                                                 /* remeber to clean up the object */
2564                                                 dev->gc_cleanup_list[dev->
2565                                                                      n_clean_ups]
2566                                                     = tags.obj_id;
2567                                                 dev->n_clean_ups++;
2568                                         }
2569                                         mark_flash = 0;
2570                                 } else if (0) {
2571                                         /* Todo object && object->deleted && object->n_data_chunks == 0 */
2572                                         /* Deleted object header with no data chunks.
2573                                          * Can be discarded and the file deleted.
2574                                          */
2575                                         object->hdr_chunk = 0;
2576                                         yaffs_free_tnode(object->my_dev,
2577                                                          object->
2578                                                          variant.file_variant.
2579                                                          top);
2580                                         object->variant.file_variant.top = NULL;
2581                                         yaffs_generic_obj_del(object);
2582
2583                                 } else if (object) {
2584                                         /* It's either a data chunk in a live file or
2585                                          * an ObjectHeader, so we're interested in it.
2586                                          * NB Need to keep the ObjectHeaders of deleted files
2587                                          * until the whole file has been deleted off
2588                                          */
2589                                         tags.serial_number++;
2590
2591                                         dev->n_gc_copies++;
2592
2593                                         if (tags.chunk_id == 0) {
2594                                                 /* It is an object Id,
2595                                                  * We need to nuke the shrinkheader flags first
2596                                                  * Also need to clean up shadowing.
2597                                                  * We no longer want the shrink_header flag since its work is done
2598                                                  * and if it is left in place it will mess up scanning.
2599                                                  */
2600
2601                                                 struct yaffs_obj_hdr *oh;
2602                                                 oh = (struct yaffs_obj_hdr *)
2603                                                     buffer;
2604
2605                                                 oh->is_shrink = 0;
2606                                                 tags.extra_is_shrink = 0;
2607
2608                                                 oh->shadows_obj = 0;
2609                                                 oh->inband_shadowed_obj_id = 0;
2610                                                 tags.extra_shadows = 0;
2611
2612                                                 /* Update file size */
2613                                                 if (object->variant_type ==
2614                                                     YAFFS_OBJECT_TYPE_FILE) {
2615                                                         oh->file_size =
2616                                                             object->variant.
2617                                                             file_variant.
2618                                                             file_size;
2619                                                         tags.extra_length =
2620                                                             oh->file_size;
2621                                                 }
2622
2623                                                 yaffs_verify_oh(object, oh,
2624                                                                 &tags, 1);
2625                                                 new_chunk =
2626                                                     yaffs_write_new_chunk(dev,
2627                                                                           (u8 *)
2628                                                                           oh,
2629                                                                           &tags,
2630                                                                           1);
2631                                         } else {
2632                                                 new_chunk =
2633                                                     yaffs_write_new_chunk(dev,
2634                                                                           buffer,
2635                                                                           &tags,
2636                                                                           1);
2637                                         }
2638
2639                                         if (new_chunk < 0) {
2640                                                 ret_val = YAFFS_FAIL;
2641                                         } else {
2642
2643                                                 /* Ok, now fix up the Tnodes etc. */
2644
2645                                                 if (tags.chunk_id == 0) {
2646                                                         /* It's a header */
2647                                                         object->hdr_chunk =
2648                                                             new_chunk;
2649                                                         object->serial =
2650                                                             tags.serial_number;
2651                                                 } else {
2652                                                         /* It's a data chunk */
2653                                                         int ok;
2654                                                         ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
2655                                                 }
2656                                         }
2657                                 }
2658
2659                                 if (ret_val == YAFFS_OK)
2660                                         yaffs_chunk_del(dev, old_chunk,
2661                                                         mark_flash, __LINE__);
2662
2663                         }
2664                 }
2665
2666                 yaffs_release_temp_buffer(dev, buffer, __LINE__);
2667
2668         }
2669
2670         yaffs_verify_collected_blk(dev, bi, block);
2671
2672         if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
2673                 /*
2674                  * The gc did not complete. Set block state back to FULL
2675                  * because checkpointing does not restore gc.
2676                  */
2677                 bi->block_state = YAFFS_BLOCK_STATE_FULL;
2678         } else {
2679                 /* The gc completed. */
2680                 /* Do any required cleanups */
2681                 for (i = 0; i < dev->n_clean_ups; i++) {
2682                         /* Time to delete the file too */
2683                         object =
2684                             yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
2685                         if (object) {
2686                                 yaffs_free_tnode(dev,
2687                                                  object->variant.
2688                                                  file_variant.top);
2689                                 object->variant.file_variant.top = NULL;
2690                                 yaffs_trace(YAFFS_TRACE_GC,
2691                                         "yaffs: About to finally delete object %d",
2692                                         object->obj_id);
2693                                 yaffs_generic_obj_del(object);
2694                                 object->my_dev->n_deleted_files--;
2695                         }
2696
2697                 }
2698
2699                 chunks_after = yaffs_get_erased_chunks(dev);
2700                 if (chunks_before >= chunks_after)
2701                         yaffs_trace(YAFFS_TRACE_GC,
2702                                 "gc did not increase free chunks before %d after %d",
2703                                 chunks_before, chunks_after);
2704                 dev->gc_block = 0;
2705                 dev->gc_chunk = 0;
2706                 dev->n_clean_ups = 0;
2707         }
2708
2709         dev->gc_disable = 0;
2710
2711         return ret_val;
2712 }
2713
2714 /*
2715  * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
2716  * for garbage collection.
2717  */
2718
2719 static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2720                                     int aggressive, int background)
2721 {
2722         int i;
2723         int iterations;
2724         unsigned selected = 0;
2725         int prioritised = 0;
2726         int prioritised_exist = 0;
2727         struct yaffs_block_info *bi;
2728         int threshold;
2729
2730         /* First let's see if we need to grab a prioritised block */
2731         if (dev->has_pending_prioritised_gc && !aggressive) {
2732                 dev->gc_dirtiest = 0;
2733                 bi = dev->block_info;
2734                 for (i = dev->internal_start_block;
2735                      i <= dev->internal_end_block && !selected; i++) {
2736
2737                         if (bi->gc_prioritise) {
2738                                 prioritised_exist = 1;
2739                                 if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2740                                     yaffs_block_ok_for_gc(dev, bi)) {
2741                                         selected = i;
2742                                         prioritised = 1;
2743                                 }
2744                         }
2745                         bi++;
2746                 }
2747
2748                 /*
2749                  * If there is a prioritised block and none was selected then
2750                  * this happened because there is at least one old dirty block gumming
2751                  * up the works. Let's gc the oldest dirty block.
2752                  */
2753
2754                 if (prioritised_exist &&
2755                     !selected && dev->oldest_dirty_block > 0)
2756                         selected = dev->oldest_dirty_block;
2757
2758                 if (!prioritised_exist) /* None found, so we can clear this */
2759                         dev->has_pending_prioritised_gc = 0;
2760         }
2761
2762         /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
2763          * search harder.
2764          * else (we're doing a leasurely gc), then we only bother to do this if the
2765          * block has only a few pages in use.
2766          */
2767
2768         if (!selected) {
2769                 int pages_used;
2770                 int n_blocks =
2771                     dev->internal_end_block - dev->internal_start_block + 1;
2772                 if (aggressive) {
2773                         threshold = dev->param.chunks_per_block;
2774                         iterations = n_blocks;
2775                 } else {
2776                         int max_threshold;
2777
2778                         if (background)
2779                                 max_threshold = dev->param.chunks_per_block / 2;
2780                         else
2781                                 max_threshold = dev->param.chunks_per_block / 8;
2782
2783                         if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2784                                 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2785
2786                         threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
2787                         if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2788                                 threshold = YAFFS_GC_PASSIVE_THRESHOLD;
2789                         if (threshold > max_threshold)
2790                                 threshold = max_threshold;
2791
2792                         iterations = n_blocks / 16 + 1;
2793                         if (iterations > 100)
2794                                 iterations = 100;
2795                 }
2796
2797                 for (i = 0;
2798                      i < iterations &&
2799                      (dev->gc_dirtiest < 1 ||
2800                       dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
2801                         dev->gc_block_finder++;
2802                         if (dev->gc_block_finder < dev->internal_start_block ||
2803                             dev->gc_block_finder > dev->internal_end_block)
2804                                 dev->gc_block_finder =
2805                                     dev->internal_start_block;
2806
2807                         bi = yaffs_get_block_info(dev, dev->gc_block_finder);
2808
2809                         pages_used = bi->pages_in_use - bi->soft_del_pages;
2810
2811                         if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
2812                             pages_used < dev->param.chunks_per_block &&
2813                             (dev->gc_dirtiest < 1
2814                              || pages_used < dev->gc_pages_in_use)
2815                             && yaffs_block_ok_for_gc(dev, bi)) {
2816                                 dev->gc_dirtiest = dev->gc_block_finder;
2817                                 dev->gc_pages_in_use = pages_used;
2818                         }
2819                 }
2820
2821                 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2822                         selected = dev->gc_dirtiest;
2823         }
2824
2825         /*
2826          * If nothing has been selected for a while, try selecting the oldest dirty
2827          * because that's gumming up the works.
2828          */
2829
2830         if (!selected && dev->param.is_yaffs2 &&
2831             dev->gc_not_done >= (background ? 10 : 20)) {
2832                 yaffs2_find_oldest_dirty_seq(dev);
2833                 if (dev->oldest_dirty_block > 0) {
2834                         selected = dev->oldest_dirty_block;
2835                         dev->gc_dirtiest = selected;
2836                         dev->oldest_dirty_gc_count++;
2837                         bi = yaffs_get_block_info(dev, selected);
2838                         dev->gc_pages_in_use =
2839                             bi->pages_in_use - bi->soft_del_pages;
2840                 } else {
2841                         dev->gc_not_done = 0;
2842                 }
2843         }
2844
2845         if (selected) {
2846                 yaffs_trace(YAFFS_TRACE_GC,
2847                         "GC Selected block %d with %d free, prioritised:%d",
2848                         selected,
2849                         dev->param.chunks_per_block - dev->gc_pages_in_use,
2850                         prioritised);
2851
2852                 dev->n_gc_blocks++;
2853                 if (background)
2854                         dev->bg_gcs++;
2855
2856                 dev->gc_dirtiest = 0;
2857                 dev->gc_pages_in_use = 0;
2858                 dev->gc_not_done = 0;
2859                 if (dev->refresh_skip > 0)
2860                         dev->refresh_skip--;
2861         } else {
2862                 dev->gc_not_done++;
2863                 yaffs_trace(YAFFS_TRACE_GC,
2864                         "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
2865                         dev->gc_block_finder, dev->gc_not_done, threshold,
2866                         dev->gc_dirtiest, dev->gc_pages_in_use,
2867                         dev->oldest_dirty_block, background ? " bg" : "");
2868         }
2869
2870         return selected;
2871 }
2872
2873 /* New garbage collector
2874  * If we're very low on erased blocks then we do aggressive garbage collection
2875  * otherwise we do "leasurely" garbage collection.
2876  * Aggressive gc looks further (whole array) and will accept less dirty blocks.
2877  * Passive gc only inspects smaller areas and will only accept more dirty blocks.
2878  *
2879  * The idea is to help clear out space in a more spread-out manner.
2880  * Dunno if it really does anything useful.
2881  */
2882 static int yaffs_check_gc(struct yaffs_dev *dev, int background)
2883 {
2884         int aggressive = 0;
2885         int gc_ok = YAFFS_OK;
2886         int max_tries = 0;
2887         int min_erased;
2888         int erased_chunks;
2889         int checkpt_block_adjust;
2890
2891         if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2892                 return YAFFS_OK;
2893
2894         if (dev->gc_disable) {
2895                 /* Bail out so we don't get recursive gc */
2896                 return YAFFS_OK;
2897         }
2898
2899         /* This loop should pass the first time.
2900          * We'll only see looping here if the collection does not increase space.
2901          */
2902
2903         do {
2904                 max_tries++;
2905
2906                 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
2907
2908                 min_erased =
2909                     dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
2910                 erased_chunks =
2911                     dev->n_erased_blocks * dev->param.chunks_per_block;
2912
2913                 /* If we need a block soon then do aggressive gc. */
2914                 if (dev->n_erased_blocks < min_erased)
2915                         aggressive = 1;
2916                 else {
2917                         if (!background
2918                             && erased_chunks > (dev->n_free_chunks / 4))
2919                                 break;
2920
2921                         if (dev->gc_skip > 20)
2922                                 dev->gc_skip = 20;
2923                         if (erased_chunks < dev->n_free_chunks / 2 ||
2924                             dev->gc_skip < 1 || background)
2925                                 aggressive = 0;
2926                         else {
2927                                 dev->gc_skip--;
2928                                 break;
2929                         }
2930                 }
2931
2932                 dev->gc_skip = 5;
2933
2934                 /* If we don't already have a block being gc'd then see if we should start another */
2935
2936                 if (dev->gc_block < 1 && !aggressive) {
2937                         dev->gc_block = yaffs2_find_refresh_block(dev);
2938                         dev->gc_chunk = 0;
2939                         dev->n_clean_ups = 0;
2940                 }
2941                 if (dev->gc_block < 1) {
2942                         dev->gc_block =
2943                             yaffs_find_gc_block(dev, aggressive, background);
2944                         dev->gc_chunk = 0;
2945                         dev->n_clean_ups = 0;
2946                 }
2947
2948                 if (dev->gc_block > 0) {
2949                         dev->all_gcs++;
2950                         if (!aggressive)
2951                                 dev->passive_gc_count++;
2952
2953                         yaffs_trace(YAFFS_TRACE_GC,
2954                                 "yaffs: GC n_erased_blocks %d aggressive %d",
2955                                 dev->n_erased_blocks, aggressive);
2956
2957                         gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2958                 }
2959
2960                 if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
2961                     && dev->gc_block > 0) {
2962                         yaffs_trace(YAFFS_TRACE_GC,
2963                                 "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
2964                                 dev->n_erased_blocks, max_tries,
2965                                 dev->gc_block);
2966                 }
2967         } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
2968                  (dev->gc_block > 0) && (max_tries < 2));
2969
2970         return aggressive ? gc_ok : YAFFS_OK;
2971 }
2972
2973 /*
2974  * yaffs_bg_gc()
2975  * Garbage collects. Intended to be called from a background thread.
2976  * Returns non-zero if at least half the free chunks are erased.
2977  */
2978 int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2979 {
2980         int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2981
2982         yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
2983
2984         yaffs_check_gc(dev, 1);
2985         return erased_chunks > dev->n_free_chunks / 2;
2986 }
2987
2988 /*-------------------- Data file manipulation -----------------*/
2989
2990 static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
2991 {
2992         int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
2993
2994         if (nand_chunk >= 0)
2995                 return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
2996                                                 buffer, NULL);
2997         else {
2998                 yaffs_trace(YAFFS_TRACE_NANDACCESS,
2999                         "Chunk %d not found zero instead",
3000                         nand_chunk);
3001                 /* get sane (zero) data if you read a hole */
3002                 memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
3003                 return 0;
3004         }
3005
3006 }
3007
3008 void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
3009                      int lyn)
3010 {
3011         int block;
3012         int page;
3013         struct yaffs_ext_tags tags;
3014         struct yaffs_block_info *bi;
3015
3016         if (chunk_id <= 0)
3017                 return;
3018
3019         dev->n_deletions++;
3020         block = chunk_id / dev->param.chunks_per_block;
3021         page = chunk_id % dev->param.chunks_per_block;
3022
3023         if (!yaffs_check_chunk_bit(dev, block, page))
3024                 yaffs_trace(YAFFS_TRACE_VERIFY,
3025                         "Deleting invalid chunk %d", chunk_id);
3026
3027         bi = yaffs_get_block_info(dev, block);
3028
3029         yaffs2_update_oldest_dirty_seq(dev, block, bi);
3030
3031         yaffs_trace(YAFFS_TRACE_DELETION,
3032                 "line %d delete of chunk %d",
3033                 lyn, chunk_id);
3034
3035         if (!dev->param.is_yaffs2 && mark_flash &&
3036             bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
3037
3038                 yaffs_init_tags(&tags);
3039
3040                 tags.is_deleted = 1;
3041
3042                 yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
3043                 yaffs_handle_chunk_update(dev, chunk_id, &tags);
3044         } else {
3045                 dev->n_unmarked_deletions++;
3046         }
3047
3048         /* Pull out of the management area.
3049          * If the whole block became dirty, this will kick off an erasure.
3050          */
3051         if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
3052             bi->block_state == YAFFS_BLOCK_STATE_FULL ||
3053             bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3054             bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
3055                 dev->n_free_chunks++;
3056
3057                 yaffs_clear_chunk_bit(dev, block, page);
3058
3059                 bi->pages_in_use--;
3060
3061                 if (bi->pages_in_use == 0 &&
3062                     !bi->has_shrink_hdr &&
3063                     bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
3064                     bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
3065                         yaffs_block_became_dirty(dev, block);
3066                 }
3067
3068         }
3069
3070 }
3071
3072 static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
3073                              const u8 * buffer, int n_bytes, int use_reserve)
3074 {
3075         /* Find old chunk Need to do this to get serial number
3076          * Write new one and patch into tree.
3077          * Invalidate old tags.
3078          */
3079
3080         int prev_chunk_id;
3081         struct yaffs_ext_tags prev_tags;
3082
3083         int new_chunk_id;
3084         struct yaffs_ext_tags new_tags;
3085
3086         struct yaffs_dev *dev = in->my_dev;
3087
3088         yaffs_check_gc(dev, 0);
3089
3090         /* Get the previous chunk at this location in the file if it exists.
3091          * If it does not exist then put a zero into the tree. This creates
3092          * the tnode now, rather than later when it is harder to clean up.
3093          */
3094         prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
3095         if (prev_chunk_id < 1 &&
3096             !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
3097                 return 0;
3098
3099         /* Set up new tags */
3100         yaffs_init_tags(&new_tags);
3101
3102         new_tags.chunk_id = inode_chunk;
3103         new_tags.obj_id = in->obj_id;
3104         new_tags.serial_number =
3105             (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
3106         new_tags.n_bytes = n_bytes;
3107
3108         if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
3109                 yaffs_trace(YAFFS_TRACE_ERROR,
3110                   "Writing %d bytes to chunk!!!!!!!!!",
3111                    n_bytes);
3112                 YBUG();
3113         }
3114
3115         new_chunk_id =
3116             yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
3117
3118         if (new_chunk_id > 0) {
3119                 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
3120
3121                 if (prev_chunk_id > 0)
3122                         yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3123
3124                 yaffs_verify_file_sane(in);
3125         }
3126         return new_chunk_id;
3127
3128 }
3129
3130
3131
3132 static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
3133                                 const YCHAR * name, const void *value, int size,
3134                                 int flags)
3135 {
3136         struct yaffs_xattr_mod xmod;
3137
3138         int result;
3139
3140         xmod.set = set;
3141         xmod.name = name;
3142         xmod.data = value;
3143         xmod.size = size;
3144         xmod.flags = flags;
3145         xmod.result = -ENOSPC;
3146
3147         result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
3148
3149         if (result > 0)
3150                 return xmod.result;
3151         else
3152                 return -ENOSPC;
3153 }
3154
3155 static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
3156                                    struct yaffs_xattr_mod *xmod)
3157 {
3158         int retval = 0;
3159         int x_offs = sizeof(struct yaffs_obj_hdr);
3160         struct yaffs_dev *dev = obj->my_dev;
3161         int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3162
3163         char *x_buffer = buffer + x_offs;
3164
3165         if (xmod->set)
3166                 retval =
3167                     nval_set(x_buffer, x_size, xmod->name, xmod->data,
3168                              xmod->size, xmod->flags);
3169         else
3170                 retval = nval_del(x_buffer, x_size, xmod->name);
3171
3172         obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3173         obj->xattr_known = 1;
3174
3175         xmod->result = retval;
3176
3177         return retval;
3178 }
3179
3180 static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
3181                                   void *value, int size)
3182 {
3183         char *buffer = NULL;
3184         int result;
3185         struct yaffs_ext_tags tags;
3186         struct yaffs_dev *dev = obj->my_dev;
3187         int x_offs = sizeof(struct yaffs_obj_hdr);
3188         int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
3189
3190         char *x_buffer;
3191
3192         int retval = 0;
3193
3194         if (obj->hdr_chunk < 1)
3195                 return -ENODATA;
3196
3197         /* If we know that the object has no xattribs then don't do all the
3198          * reading and parsing.
3199          */
3200         if (obj->xattr_known && !obj->has_xattr) {
3201                 if (name)
3202                         return -ENODATA;
3203                 else
3204                         return 0;
3205         }
3206
3207         buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
3208         if (!buffer)
3209                 return -ENOMEM;
3210
3211         result =
3212             yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
3213
3214         if (result != YAFFS_OK)
3215                 retval = -ENOENT;
3216         else {
3217                 x_buffer = buffer + x_offs;
3218
3219                 if (!obj->xattr_known) {
3220                         obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3221                         obj->xattr_known = 1;
3222                 }
3223
3224                 if (name)
3225                         retval = nval_get(x_buffer, x_size, name, value, size);
3226                 else
3227                         retval = nval_list(x_buffer, x_size, value, size);
3228         }
3229         yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
3230         return retval;
3231 }
3232
3233 int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
3234                       const void *value, int size, int flags)
3235 {
3236         return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
3237 }
3238
3239 int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
3240 {
3241         return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
3242 }
3243
3244 int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
3245                       int size)
3246 {
3247         return yaffs_do_xattrib_fetch(obj, name, value, size);
3248 }
3249
3250 int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3251 {
3252         return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3253 }
3254
3255 static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
3256 {
3257         u8 *chunk_data;
3258         struct yaffs_obj_hdr *oh;
3259         struct yaffs_dev *dev;
3260         struct yaffs_ext_tags tags;
3261         int result;
3262         int alloc_failed = 0;
3263
3264         if (!in)
3265                 return;
3266
3267         dev = in->my_dev;
3268
3269         if (in->lazy_loaded && in->hdr_chunk > 0) {
3270                 in->lazy_loaded = 0;
3271                 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
3272
3273                 result =
3274                     yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
3275                                              &tags);
3276                 oh = (struct yaffs_obj_hdr *)chunk_data;
3277
3278                 in->yst_mode = oh->yst_mode;
3279                 yaffs_load_attribs(in, oh);
3280                 yaffs_set_obj_name_from_oh(in, oh);
3281
3282                 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3283                         in->variant.symlink_variant.alias =
3284                             yaffs_clone_str(oh->alias);
3285                         if (!in->variant.symlink_variant.alias)
3286                                 alloc_failed = 1;       /* Not returned to caller */
3287                 }
3288
3289                 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
3290         }
3291 }
3292
3293 static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
3294                                     const YCHAR * oh_name, int buff_size)
3295 {
3296 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3297         if (dev->param.auto_unicode) {
3298                 if (*oh_name) {
3299                         /* It is an ASCII name, do an ASCII to
3300                          * unicode conversion */
3301                         const char *ascii_oh_name = (const char *)oh_name;
3302                         int n = buff_size - 1;
3303                         while (n > 0 && *ascii_oh_name) {
3304                                 *name = *ascii_oh_name;
3305                                 name++;
3306                                 ascii_oh_name++;
3307                                 n--;
3308                         }
3309                 } else {
3310                         strncpy(name, oh_name + 1, buff_size - 1);
3311                 }
3312         } else {
3313 #else
3314         {
3315 #endif
3316                 strncpy(name, oh_name, buff_size - 1);
3317         }
3318 }
3319
3320 static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
3321                                     const YCHAR * name)
3322 {
3323 #ifdef CONFIG_YAFFS_AUTO_UNICODE
3324
3325         int is_ascii;
3326         YCHAR *w;
3327
3328         if (dev->param.auto_unicode) {
3329
3330                 is_ascii = 1;
3331                 w = name;
3332
3333                 /* Figure out if the name will fit in ascii character set */
3334                 while (is_ascii && *w) {
3335                         if ((*w) & 0xff00)
3336                                 is_ascii = 0;
3337                         w++;
3338                 }
3339
3340                 if (is_ascii) {
3341                         /* It is an ASCII name, so do a unicode to ascii conversion */
3342                         char *ascii_oh_name = (char *)oh_name;
3343                         int n = YAFFS_MAX_NAME_LENGTH - 1;
3344                         while (n > 0 && *name) {
3345                                 *ascii_oh_name = *name;
3346                                 name++;
3347                                 ascii_oh_name++;
3348                                 n--;
3349                         }
3350                 } else {
3351                         /* It is a unicode name, so save starting at the second YCHAR */
3352                         *oh_name = 0;
3353                         strncpy(oh_name + 1, name,
3354                                       YAFFS_MAX_NAME_LENGTH - 2);
3355                 }
3356         } else {
3357 #else
3358         {
3359 #endif
3360                 strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
3361         }
3362
3363 }
3364
3365 /* UpdateObjectHeader updates the header on NAND for an object.
3366  * If name is not NULL, then that new name is used.
3367  */
3368 int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
3369                     int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
3370 {
3371
3372         struct yaffs_block_info *bi;
3373
3374         struct yaffs_dev *dev = in->my_dev;
3375
3376         int prev_chunk_id;
3377         int ret_val = 0;
3378         int result = 0;
3379
3380         int new_chunk_id;
3381         struct yaffs_ext_tags new_tags;
3382         struct yaffs_ext_tags old_tags;
3383         const YCHAR *alias = NULL;
3384
3385         u8 *buffer = NULL;
3386         YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
3387
3388         struct yaffs_obj_hdr *oh = NULL;
3389
3390         strcpy(old_name, _Y("silly old name"));
3391
3392         if (!in->fake || in == dev->root_dir ||
3393             force || xmod) {
3394
3395                 yaffs_check_gc(dev, 0);
3396                 yaffs_check_obj_details_loaded(in);
3397
3398                 buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
3399                 oh = (struct yaffs_obj_hdr *)buffer;
3400
3401                 prev_chunk_id = in->hdr_chunk;
3402
3403                 if (prev_chunk_id > 0) {
3404                         result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3405                                                           buffer, &old_tags);
3406
3407                         yaffs_verify_oh(in, oh, &old_tags, 0);
3408
3409                         memcpy(old_name, oh->name, sizeof(oh->name));
3410                         memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
3411                 } else {
3412                         memset(buffer, 0xFF, dev->data_bytes_per_chunk);
3413                 }
3414
3415                 oh->type = in->variant_type;
3416                 oh->yst_mode = in->yst_mode;
3417                 oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
3418
3419                 yaffs_load_attribs_oh(oh, in);
3420
3421                 if (in->parent)
3422                         oh->parent_obj_id = in->parent->obj_id;
3423                 else
3424                         oh->parent_obj_id = 0;
3425
3426                 if (name && *name) {
3427                         memset(oh->name, 0, sizeof(oh->name));
3428                         yaffs_load_oh_from_name(dev, oh->name, name);
3429                 } else if (prev_chunk_id > 0) {
3430                         memcpy(oh->name, old_name, sizeof(oh->name));
3431                 } else {
3432                         memset(oh->name, 0, sizeof(oh->name));
3433                 }
3434
3435                 oh->is_shrink = is_shrink;
3436
3437                 switch (in->variant_type) {
3438                 case YAFFS_OBJECT_TYPE_UNKNOWN:
3439                         /* Should not happen */
3440                         break;
3441                 case YAFFS_OBJECT_TYPE_FILE:
3442                         oh->file_size =
3443                             (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
3444                              || oh->parent_obj_id ==
3445                              YAFFS_OBJECTID_UNLINKED) ? 0 : in->
3446                             variant.file_variant.file_size;
3447                         break;
3448                 case YAFFS_OBJECT_TYPE_HARDLINK:
3449                         oh->equiv_id = in->variant.hardlink_variant.equiv_id;
3450                         break;
3451                 case YAFFS_OBJECT_TYPE_SPECIAL:
3452                         /* Do nothing */
3453                         break;
3454                 case YAFFS_OBJECT_TYPE_DIRECTORY:
3455                         /* Do nothing */
3456                         break;
3457                 case YAFFS_OBJECT_TYPE_SYMLINK:
3458                         alias = in->variant.symlink_variant.alias;
3459                         if (!alias)
3460                                 alias = _Y("no alias");
3461                         strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
3462                         oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3463                         break;
3464                 }
3465
3466                 /* process any xattrib modifications */
3467                 if (xmod)
3468                         yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
3469
3470                 /* Tags */
3471                 yaffs_init_tags(&new_tags);
3472                 in->serial++;
3473                 new_tags.chunk_id = 0;
3474                 new_tags.obj_id = in->obj_id;
3475                 new_tags.serial_number = in->serial;
3476
3477                 /* Add extra info for file header */
3478
3479                 new_tags.extra_available = 1;
3480                 new_tags.extra_parent_id = oh->parent_obj_id;
3481                 new_tags.extra_length = oh->file_size;
3482                 new_tags.extra_is_shrink = oh->is_shrink;
3483                 new_tags.extra_equiv_id = oh->equiv_id;
3484                 new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
3485                 new_tags.extra_obj_type = in->variant_type;
3486
3487                 yaffs_verify_oh(in, oh, &new_tags, 1);
3488
3489                 /* Create new chunk in NAND */
3490                 new_chunk_id =
3491                     yaffs_write_new_chunk(dev, buffer, &new_tags,
3492                                           (prev_chunk_id > 0) ? 1 : 0);
3493
3494                 if (new_chunk_id >= 0) {
3495
3496                         in->hdr_chunk = new_chunk_id;
3497
3498                         if (prev_chunk_id > 0) {
3499                                 yaffs_chunk_del(dev, prev_chunk_id, 1,
3500                                                 __LINE__);
3501                         }
3502
3503                         if (!yaffs_obj_cache_dirty(in))
3504                                 in->dirty = 0;
3505
3506                         /* If this was a shrink, then mark the block that the chunk lives on */
3507                         if (is_shrink) {
3508                                 bi = yaffs_get_block_info(in->my_dev,
3509                                                           new_chunk_id /
3510                                                           in->my_dev->param.
3511                                                           chunks_per_block);
3512                                 bi->has_shrink_hdr = 1;
3513                         }
3514
3515                 }
3516
3517                 ret_val = new_chunk_id;
3518
3519         }
3520
3521         if (buffer)
3522                 yaffs_release_temp_buffer(dev, buffer, __LINE__);
3523
3524         return ret_val;
3525 }
3526
3527 /*--------------------- File read/write ------------------------
3528  * Read and write have very similar structures.
3529  * In general the read/write has three parts to it
3530  * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3531  * Some complete chunks
3532  * An incomplete chunk to end off with
3533  *
3534  * Curve-balls: the first chunk might also be the last chunk.
3535  */
3536
3537 int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
3538 {
3539
3540         int chunk;
3541         u32 start;
3542         int n_copy;
3543         int n = n_bytes;
3544         int n_done = 0;
3545         struct yaffs_cache *cache;
3546
3547         struct yaffs_dev *dev;
3548
3549         dev = in->my_dev;
3550
3551         while (n > 0) {
3552                 /* chunk = offset / dev->data_bytes_per_chunk + 1; */
3553                 /* start = offset % dev->data_bytes_per_chunk; */
3554                 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3555                 chunk++;
3556
3557                 /* OK now check for the curveball where the start and end are in
3558                  * the same chunk.
3559                  */
3560                 if ((start + n) < dev->data_bytes_per_chunk)
3561                         n_copy = n;
3562                 else
3563                         n_copy = dev->data_bytes_per_chunk - start;
3564
3565                 cache = yaffs_find_chunk_cache(in, chunk);
3566
3567                 /* If the chunk is already in the cache or it is less than a whole chunk
3568                  * or we're using inband tags then use the cache (if there is caching)
3569                  * else bypass the cache.
3570                  */
3571                 if (cache || n_copy != dev->data_bytes_per_chunk
3572                     || dev->param.inband_tags) {
3573                         if (dev->param.n_caches > 0) {
3574
3575                                 /* If we can't find the data in the cache, then load it up. */
3576
3577                                 if (!cache) {
3578                                         cache =
3579                                             yaffs_grab_chunk_cache(in->my_dev);
3580                                         cache->object = in;
3581                                         cache->chunk_id = chunk;
3582                                         cache->dirty = 0;
3583                                         cache->locked = 0;
3584                                         yaffs_rd_data_obj(in, chunk,
3585                                                           cache->data);
3586                                         cache->n_bytes = 0;
3587                                 }
3588
3589                                 yaffs_use_cache(dev, cache, 0);
3590
3591                                 cache->locked = 1;
3592
3593                                 memcpy(buffer, &cache->data[start], n_copy);
3594
3595                                 cache->locked = 0;
3596                         } else {
3597                                 /* Read into the local buffer then copy.. */
3598
3599                                 u8 *local_buffer =
3600                                     yaffs_get_temp_buffer(dev, __LINE__);
3601                                 yaffs_rd_data_obj(in, chunk, local_buffer);
3602
3603                                 memcpy(buffer, &local_buffer[start], n_copy);
3604
3605                                 yaffs_release_temp_buffer(dev, local_buffer,
3606                                                           __LINE__);
3607                         }
3608
3609                 } else {
3610
3611                         /* A full chunk. Read directly into the supplied buffer. */
3612                         yaffs_rd_data_obj(in, chunk, buffer);
3613
3614                 }
3615
3616                 n -= n_copy;
3617                 offset += n_copy;
3618                 buffer += n_copy;
3619                 n_done += n_copy;
3620
3621         }
3622
3623         return n_done;
3624 }
3625
3626 int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3627                      int n_bytes, int write_trhrough)
3628 {
3629
3630         int chunk;
3631         u32 start;
3632         int n_copy;
3633         int n = n_bytes;
3634         int n_done = 0;
3635         int n_writeback;
3636         int start_write = offset;
3637         int chunk_written = 0;
3638         u32 n_bytes_read;
3639         u32 chunk_start;
3640
3641         struct yaffs_dev *dev;
3642
3643         dev = in->my_dev;
3644
3645         while (n > 0 && chunk_written >= 0) {
3646                 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3647
3648                 if (chunk * dev->data_bytes_per_chunk + start != offset ||
3649                     start >= dev->data_bytes_per_chunk) {
3650                         yaffs_trace(YAFFS_TRACE_ERROR,
3651                                 "AddrToChunk of offset %d gives chunk %d start %d",
3652                                 (int)offset, chunk, start);
3653                 }
3654                 chunk++;        /* File pos to chunk in file offset */
3655
3656                 /* OK now check for the curveball where the start and end are in
3657                  * the same chunk.
3658                  */
3659
3660                 if ((start + n) < dev->data_bytes_per_chunk) {
3661                         n_copy = n;
3662
3663                         /* Now folks, to calculate how many bytes to write back....
3664                          * If we're overwriting and not writing to then end of file then
3665                          * we need to write back as much as was there before.
3666                          */
3667
3668                         chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
3669
3670                         if (chunk_start > in->variant.file_variant.file_size)
3671                                 n_bytes_read = 0;       /* Past end of file */
3672                         else
3673                                 n_bytes_read =
3674                                     in->variant.file_variant.file_size -
3675                                     chunk_start;
3676
3677                         if (n_bytes_read > dev->data_bytes_per_chunk)
3678                                 n_bytes_read = dev->data_bytes_per_chunk;
3679
3680                         n_writeback =
3681                             (n_bytes_read >
3682                              (start + n)) ? n_bytes_read : (start + n);
3683
3684                         if (n_writeback < 0
3685                             || n_writeback > dev->data_bytes_per_chunk)
3686                                 YBUG();
3687
3688                 } else {
3689                         n_copy = dev->data_bytes_per_chunk - start;
3690                         n_writeback = dev->data_bytes_per_chunk;
3691                 }
3692
3693                 if (n_copy != dev->data_bytes_per_chunk
3694                     || dev->param.inband_tags) {
3695                         /* An incomplete start or end chunk (or maybe both start and end chunk),
3696                          * or we're using inband tags, so we want to use the cache buffers.
3697                          */
3698                         if (dev->param.n_caches > 0) {
3699                                 struct yaffs_cache *cache;
3700                                 /* If we can't find the data in the cache, then load the cache */
3701                                 cache = yaffs_find_chunk_cache(in, chunk);
3702
3703                                 if (!cache
3704                                     && yaffs_check_alloc_available(dev, 1)) {
3705                                         cache = yaffs_grab_chunk_cache(dev);
3706                                         cache->object = in;
3707                                         cache->chunk_id = chunk;
3708                                         cache->dirty = 0;
3709                                         cache->locked = 0;
3710                                         yaffs_rd_data_obj(in, chunk,
3711                                                           cache->data);
3712                                 } else if (cache &&
3713                                            !cache->dirty &&
3714                                            !yaffs_check_alloc_available(dev,
3715                                                                         1)) {
3716                                         /* Drop the cache if it was a read cache item and
3717                                          * no space check has been made for it.
3718                                          */
3719                                         cache = NULL;
3720                                 }
3721
3722                                 if (cache) {
3723                                         yaffs_use_cache(dev, cache, 1);
3724                                         cache->locked = 1;
3725
3726                                         memcpy(&cache->data[start], buffer,
3727                                                n_copy);
3728
3729                                         cache->locked = 0;
3730                                         cache->n_bytes = n_writeback;
3731
3732                                         if (write_trhrough) {
3733                                                 chunk_written =
3734                                                     yaffs_wr_data_obj
3735                                                     (cache->object,
3736                                                      cache->chunk_id,
3737                                                      cache->data,
3738                                                      cache->n_bytes, 1);
3739                                                 cache->dirty = 0;
3740                                         }
3741
3742                                 } else {
3743                                         chunk_written = -1;     /* fail the write */
3744                                 }
3745                         } else {
3746                                 /* An incomplete start or end chunk (or maybe both start and end chunk)
3747                                  * Read into the local buffer then copy, then copy over and write back.
3748                                  */
3749
3750                                 u8 *local_buffer =
3751                                     yaffs_get_temp_buffer(dev, __LINE__);
3752
3753                                 yaffs_rd_data_obj(in, chunk, local_buffer);
3754
3755                                 memcpy(&local_buffer[start], buffer, n_copy);
3756
3757                                 chunk_written =
3758                                     yaffs_wr_data_obj(in, chunk,
3759                                                       local_buffer,
3760                                                       n_writeback, 0);
3761
3762                                 yaffs_release_temp_buffer(dev, local_buffer,
3763                                                           __LINE__);
3764
3765                         }
3766
3767                 } else {
3768                         /* A full chunk. Write directly from the supplied buffer. */
3769
3770                         chunk_written =
3771                             yaffs_wr_data_obj(in, chunk, buffer,
3772                                               dev->data_bytes_per_chunk, 0);
3773
3774                         /* Since we've overwritten the cached data, we better invalidate it. */
3775                         yaffs_invalidate_chunk_cache(in, chunk);
3776                 }
3777
3778                 if (chunk_written >= 0) {
3779                         n -= n_copy;
3780                         offset += n_copy;
3781                         buffer += n_copy;
3782                         n_done += n_copy;
3783                 }
3784
3785         }
3786
3787         /* Update file object */
3788
3789         if ((start_write + n_done) > in->variant.file_variant.file_size)
3790                 in->variant.file_variant.file_size = (start_write + n_done);
3791
3792         in->dirty = 1;
3793
3794         return n_done;
3795 }
3796
3797 int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
3798                   int n_bytes, int write_trhrough)
3799 {
3800         yaffs2_handle_hole(in, offset);
3801         return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
3802 }
3803
3804 /* ---------------------- File resizing stuff ------------------ */
3805
3806 static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
3807 {
3808
3809         struct yaffs_dev *dev = in->my_dev;
3810         int old_size = in->variant.file_variant.file_size;
3811
3812         int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
3813
3814         int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
3815             dev->data_bytes_per_chunk;
3816         int i;
3817         int chunk_id;
3818
3819         /* Delete backwards so that we don't end up with holes if
3820          * power is lost part-way through the operation.
3821          */
3822         for (i = last_del; i >= start_del; i--) {
3823                 /* NB this could be optimised somewhat,
3824                  * eg. could retrieve the tags and write them without
3825                  * using yaffs_chunk_del
3826                  */
3827
3828                 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
3829                 if (chunk_id > 0) {
3830                         if (chunk_id <
3831                             (dev->internal_start_block *
3832                              dev->param.chunks_per_block)
3833                             || chunk_id >=
3834                             ((dev->internal_end_block +
3835                               1) * dev->param.chunks_per_block)) {
3836                                 yaffs_trace(YAFFS_TRACE_ALWAYS,
3837                                         "Found daft chunk_id %d for %d",
3838                                         chunk_id, i);
3839                         } else {
3840                                 in->n_data_chunks--;
3841                                 yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
3842                         }
3843                 }
3844         }
3845
3846 }
3847
3848 void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
3849 {
3850         int new_full;
3851         u32 new_partial;
3852         struct yaffs_dev *dev = obj->my_dev;
3853
3854         yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
3855
3856         yaffs_prune_chunks(obj, new_size);
3857
3858         if (new_partial != 0) {
3859                 int last_chunk = 1 + new_full;
3860                 u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
3861
3862                 /* Rewrite the last chunk with its new size and zero pad */
3863                 yaffs_rd_data_obj(obj, last_chunk, local_buffer);
3864                 memset(local_buffer + new_partial, 0,
3865                        dev->data_bytes_per_chunk - new_partial);
3866
3867                 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3868                                   new_partial, 1);
3869
3870                 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
3871         }
3872
3873         obj->variant.file_variant.file_size = new_size;
3874
3875         yaffs_prune_tree(dev, &obj->variant.file_variant);
3876 }
3877
3878 int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
3879 {
3880         struct yaffs_dev *dev = in->my_dev;
3881         int old_size = in->variant.file_variant.file_size;
3882
3883         yaffs_flush_file_cache(in);
3884         yaffs_invalidate_whole_cache(in);
3885
3886         yaffs_check_gc(dev, 0);
3887
3888         if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3889                 return YAFFS_FAIL;
3890
3891         if (new_size == old_size)
3892                 return YAFFS_OK;
3893
3894         if (new_size > old_size) {
3895                 yaffs2_handle_hole(in, new_size);
3896                 in->variant.file_variant.file_size = new_size;
3897         } else {
3898                 /* new_size < old_size */
3899                 yaffs_resize_file_down(in, new_size);
3900         }
3901
3902         /* Write a new object header to reflect the resize.
3903          * show we've shrunk the file, if need be
3904          * Do this only if the file is not in the deleted directories
3905          * and is not shadowed.
3906          */
3907         if (in->parent &&
3908             !in->is_shadowed &&
3909             in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
3910             in->parent->obj_id != YAFFS_OBJECTID_DELETED)
3911                 yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
3912
3913         return YAFFS_OK;
3914 }
3915
3916 int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3917 {
3918         int ret_val;
3919         if (in->dirty) {
3920                 yaffs_flush_file_cache(in);
3921                 if (data_sync)  /* Only sync data */
3922                         ret_val = YAFFS_OK;
3923                 else {
3924                         if (update_time)
3925                                 yaffs_load_current_time(in, 0, 0);
3926
3927                         ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
3928                                    0) ? YAFFS_OK : YAFFS_FAIL;
3929                 }
3930         } else {
3931                 ret_val = YAFFS_OK;
3932         }
3933
3934         return ret_val;
3935
3936 }
3937
3938
3939 /* yaffs_del_file deletes the whole file data
3940  * and the inode associated with the file.
3941  * It does not delete the links associated with the file.
3942  */
3943 static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
3944 {
3945
3946         int ret_val;
3947         int del_now = 0;
3948         struct yaffs_dev *dev = in->my_dev;
3949
3950         if (!in->my_inode)
3951                 del_now = 1;
3952
3953         if (del_now) {
3954                 ret_val =
3955                     yaffs_change_obj_name(in, in->my_dev->del_dir,
3956                                           _Y("deleted"), 0, 0);
3957                 yaffs_trace(YAFFS_TRACE_TRACING,
3958                         "yaffs: immediate deletion of file %d",
3959                         in->obj_id);
3960                 in->deleted = 1;
3961                 in->my_dev->n_deleted_files++;
3962                 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3963                         yaffs_resize_file(in, 0);
3964                 yaffs_soft_del_file(in);
3965         } else {
3966                 ret_val =
3967                     yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
3968                                           _Y("unlinked"), 0, 0);
3969         }
3970
3971         return ret_val;
3972 }
3973
3974 int yaffs_del_file(struct yaffs_obj *in)
3975 {
3976         int ret_val = YAFFS_OK;
3977         int deleted;            /* Need to cache value on stack if in is freed */
3978         struct yaffs_dev *dev = in->my_dev;
3979
3980         if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3981                 yaffs_resize_file(in, 0);
3982
3983         if (in->n_data_chunks > 0) {
3984                 /* Use soft deletion if there is data in the file.
3985                  * That won't be the case if it has been resized to zero.
3986                  */
3987                 if (!in->unlinked)
3988                         ret_val = yaffs_unlink_file_if_needed(in);
3989
3990                 deleted = in->deleted;
3991
3992                 if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
3993                         in->deleted = 1;
3994                         deleted = 1;
3995                         in->my_dev->n_deleted_files++;
3996                         yaffs_soft_del_file(in);
3997                 }
3998                 return deleted ? YAFFS_OK : YAFFS_FAIL;
3999         } else {
4000                 /* The file has no data chunks so we toss it immediately */
4001                 yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
4002                 in->variant.file_variant.top = NULL;
4003                 yaffs_generic_obj_del(in);
4004
4005                 return YAFFS_OK;
4006         }
4007 }
4008
4009 int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
4010 {
4011         return (obj &&
4012                 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
4013                 !(list_empty(&obj->variant.dir_variant.children));
4014 }
4015
4016 static int yaffs_del_dir(struct yaffs_obj *obj)
4017 {
4018         /* First check that the directory is empty. */
4019         if (yaffs_is_non_empty_dir(obj))
4020                 return YAFFS_FAIL;
4021
4022         return yaffs_generic_obj_del(obj);
4023 }
4024
4025 static int yaffs_del_symlink(struct yaffs_obj *in)
4026 {
4027         if (in->variant.symlink_variant.alias)
4028                 kfree(in->variant.symlink_variant.alias);
4029         in->variant.symlink_variant.alias = NULL;
4030
4031         return yaffs_generic_obj_del(in);
4032 }
4033
4034 static int yaffs_del_link(struct yaffs_obj *in)
4035 {
4036         /* remove this hardlink from the list assocaited with the equivalent
4037          * object
4038          */
4039         list_del_init(&in->hard_links);
4040         return yaffs_generic_obj_del(in);
4041 }
4042
4043 int yaffs_del_obj(struct yaffs_obj *obj)
4044 {
4045         int ret_val = -1;
4046         switch (obj->variant_type) {
4047         case YAFFS_OBJECT_TYPE_FILE:
4048                 ret_val = yaffs_del_file(obj);
4049                 break;
4050         case YAFFS_OBJECT_TYPE_DIRECTORY:
4051                 if (!list_empty(&obj->variant.dir_variant.dirty)) {
4052                         yaffs_trace(YAFFS_TRACE_BACKGROUND,
4053                                 "Remove object %d from dirty directories",
4054                                 obj->obj_id);
4055                         list_del_init(&obj->variant.dir_variant.dirty);
4056                 }
4057                 return yaffs_del_dir(obj);
4058                 break;
4059         case YAFFS_OBJECT_TYPE_SYMLINK:
4060                 ret_val = yaffs_del_symlink(obj);
4061                 break;
4062         case YAFFS_OBJECT_TYPE_HARDLINK:
4063                 ret_val = yaffs_del_link(obj);
4064                 break;
4065         case YAFFS_OBJECT_TYPE_SPECIAL:
4066                 ret_val = yaffs_generic_obj_del(obj);
4067                 break;
4068         case YAFFS_OBJECT_TYPE_UNKNOWN:
4069                 ret_val = 0;
4070                 break;          /* should not happen. */
4071         }
4072
4073         return ret_val;
4074 }
4075
4076 static int yaffs_unlink_worker(struct yaffs_obj *obj)
4077 {
4078
4079         int del_now = 0;
4080
4081         if(!obj)
4082                 return YAFFS_FAIL;
4083
4084         if (!obj->my_inode)
4085                 del_now = 1;
4086
4087         yaffs_update_parent(obj->parent);
4088
4089         if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4090                 return yaffs_del_link(obj);
4091         } else if (!list_empty(&obj->hard_links)) {
4092                 /* Curve ball: We're unlinking an object that has a hardlink.
4093                  *
4094                  * This problem arises because we are not strictly following
4095                  * The Linux link/inode model.
4096                  *
4097                  * We can't really delete the object.
4098                  * Instead, we do the following:
4099                  * - Select a hardlink.
4100                  * - Unhook it from the hard links
4101                  * - Move it from its parent directory (so that the rename can work)
4102                  * - Rename the object to the hardlink's name.
4103                  * - Delete the hardlink
4104                  */
4105
4106                 struct yaffs_obj *hl;
4107                 struct yaffs_obj *parent;
4108                 int ret_val;
4109                 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
4110
4111                 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
4112                                 hard_links);
4113
4114                 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
4115                 parent = hl->parent;
4116
4117                 list_del_init(&hl->hard_links);
4118
4119                 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
4120
4121                 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
4122
4123                 if (ret_val == YAFFS_OK)
4124                         ret_val = yaffs_generic_obj_del(hl);
4125
4126                 return ret_val;
4127
4128         } else if (del_now) {
4129                 switch (obj->variant_type) {
4130                 case YAFFS_OBJECT_TYPE_FILE:
4131                         return yaffs_del_file(obj);
4132                         break;
4133                 case YAFFS_OBJECT_TYPE_DIRECTORY:
4134                         list_del_init(&obj->variant.dir_variant.dirty);
4135                         return yaffs_del_dir(obj);
4136                         break;
4137                 case YAFFS_OBJECT_TYPE_SYMLINK:
4138                         return yaffs_del_symlink(obj);
4139                         break;
4140                 case YAFFS_OBJECT_TYPE_SPECIAL:
4141                         return yaffs_generic_obj_del(obj);
4142                         break;
4143                 case YAFFS_OBJECT_TYPE_HARDLINK:
4144                 case YAFFS_OBJECT_TYPE_UNKNOWN:
4145                 default:
4146                         return YAFFS_FAIL;
4147                 }
4148         } else if (yaffs_is_non_empty_dir(obj)) {
4149                 return YAFFS_FAIL;
4150         } else {
4151                 return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
4152                                              _Y("unlinked"), 0, 0);
4153         }
4154 }
4155
4156 static int yaffs_unlink_obj(struct yaffs_obj *obj)
4157 {
4158
4159         if (obj && obj->unlink_allowed)
4160                 return yaffs_unlink_worker(obj);
4161
4162         return YAFFS_FAIL;
4163
4164 }
4165
4166 int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
4167 {
4168         struct yaffs_obj *obj;
4169
4170         obj = yaffs_find_by_name(dir, name);
4171         return yaffs_unlink_obj(obj);
4172 }
4173
4174 /* Note:
4175  * If old_name is NULL then we take old_dir as the object to be renamed.
4176  */
4177 int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
4178                      struct yaffs_obj *new_dir, const YCHAR * new_name)
4179 {
4180         struct yaffs_obj *obj = NULL;
4181         struct yaffs_obj *existing_target = NULL;
4182         int force = 0;
4183         int result;
4184         struct yaffs_dev *dev;
4185
4186         if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4187                 YBUG();
4188         if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4189                 YBUG();
4190
4191         dev = old_dir->my_dev;
4192
4193 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4194         /* Special case for case insemsitive systems.
4195          * While look-up is case insensitive, the name isn't.
4196          * Therefore we might want to change x.txt to X.txt
4197          */
4198         if (old_dir == new_dir && 
4199                 old_name && new_name && 
4200                 strcmp(old_name, new_name) == 0)
4201                 force = 1;
4202 #endif
4203
4204         if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4205             YAFFS_MAX_NAME_LENGTH)
4206                 /* ENAMETOOLONG */
4207                 return YAFFS_FAIL;
4208
4209         if(old_name)
4210                 obj = yaffs_find_by_name(old_dir, old_name);
4211         else{
4212                 obj = old_dir;
4213                 old_dir = obj->parent;
4214         }
4215
4216
4217         if (obj && obj->rename_allowed) {
4218
4219                 /* Now do the handling for an existing target, if there is one */
4220
4221                 existing_target = yaffs_find_by_name(new_dir, new_name);
4222                 if (yaffs_is_non_empty_dir(existing_target)){
4223                         return YAFFS_FAIL;      /* ENOTEMPTY */
4224                 } else if (existing_target && existing_target != obj) {
4225                         /* Nuke the target first, using shadowing,
4226                          * but only if it isn't the same object.
4227                          *
4228                          * Note we must disable gc otherwise it can mess up the shadowing.
4229                          *
4230                          */
4231                         dev->gc_disable = 1;
4232                         yaffs_change_obj_name(obj, new_dir, new_name, force,
4233                                               existing_target->obj_id);
4234                         existing_target->is_shadowed = 1;
4235                         yaffs_unlink_obj(existing_target);
4236                         dev->gc_disable = 0;
4237                 }
4238
4239                 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
4240
4241                 yaffs_update_parent(old_dir);
4242                 if (new_dir != old_dir)
4243                         yaffs_update_parent(new_dir);
4244
4245                 return result;
4246         }
4247         return YAFFS_FAIL;
4248 }
4249
4250 /*----------------------- Initialisation Scanning ---------------------- */
4251
4252 void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4253                                int backward_scanning)
4254 {
4255         struct yaffs_obj *obj;
4256
4257         if (!backward_scanning) {
4258                 /* Handle YAFFS1 forward scanning case
4259                  * For YAFFS1 we always do the deletion
4260                  */
4261
4262         } else {
4263                 /* Handle YAFFS2 case (backward scanning)
4264                  * If the shadowed object exists then ignore.
4265                  */
4266                 obj = yaffs_find_by_number(dev, obj_id);
4267                 if (obj)
4268                         return;
4269         }
4270
4271         /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
4272          * We put it in unlinked dir to be cleaned up after the scanning
4273          */
4274         obj =
4275             yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
4276         if (!obj)
4277                 return;
4278         obj->is_shadowed = 1;
4279         yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
4280         obj->variant.file_variant.shrink_size = 0;
4281         obj->valid = 1;         /* So that we don't read any other info for this file */
4282
4283 }
4284
4285 void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
4286 {
4287         struct yaffs_obj *hl;
4288         struct yaffs_obj *in;
4289
4290         while (hard_list) {
4291                 hl = hard_list;
4292                 hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
4293
4294                 in = yaffs_find_by_number(dev,
4295                                           hl->variant.
4296                                           hardlink_variant.equiv_id);
4297
4298                 if (in) {
4299                         /* Add the hardlink pointers */
4300                         hl->variant.hardlink_variant.equiv_obj = in;
4301                         list_add(&hl->hard_links, &in->hard_links);
4302                 } else {
4303                         /* Todo Need to report/handle this better.
4304                          * Got a problem... hardlink to a non-existant object
4305                          */
4306                         hl->variant.hardlink_variant.equiv_obj = NULL;
4307                         INIT_LIST_HEAD(&hl->hard_links);
4308
4309                 }
4310         }
4311 }
4312
4313 static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
4314 {
4315         /*
4316          *  Sort out state of unlinked and deleted objects after scanning.
4317          */
4318         struct list_head *i;
4319         struct list_head *n;
4320         struct yaffs_obj *l;
4321
4322         if (dev->read_only)
4323                 return;
4324
4325         /* Soft delete all the unlinked files */
4326         list_for_each_safe(i, n,
4327                            &dev->unlinked_dir->variant.dir_variant.children) {
4328                 if (i) {
4329                         l = list_entry(i, struct yaffs_obj, siblings);
4330                         yaffs_del_obj(l);
4331                 }
4332         }
4333
4334         list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
4335                 if (i) {
4336                         l = list_entry(i, struct yaffs_obj, siblings);
4337                         yaffs_del_obj(l);
4338                 }
4339         }
4340
4341 }
4342
4343 /*
4344  * This code iterates through all the objects making sure that they are rooted.
4345  * Any unrooted objects are re-rooted in lost+found.
4346  * An object needs to be in one of:
4347  * - Directly under deleted, unlinked
4348  * - Directly or indirectly under root.
4349  *
4350  * Note:
4351  *  This code assumes that we don't ever change the current relationships between
4352  *  directories:
4353  *   root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
4354  *   lost-n-found->parent == root_dir
4355  *
4356  * This fixes the problem where directories might have inadvertently been deleted
4357  * leaving the object "hanging" without being rooted in the directory tree.
4358  */
4359
4360 static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
4361 {
4362         return (obj == dev->del_dir ||
4363                 obj == dev->unlinked_dir || obj == dev->root_dir);
4364 }
4365
4366 static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
4367 {
4368         struct yaffs_obj *obj;
4369         struct yaffs_obj *parent;
4370         int i;
4371         struct list_head *lh;
4372         struct list_head *n;
4373         int depth_limit;
4374         int hanging;
4375
4376         if (dev->read_only)
4377                 return;
4378
4379         /* Iterate through the objects in each hash entry,
4380          * looking at each object.
4381          * Make sure it is rooted.
4382          */
4383
4384         for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4385                 list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
4386                         if (lh) {
4387                                 obj =
4388                                     list_entry(lh, struct yaffs_obj, hash_link);
4389                                 parent = obj->parent;
4390
4391                                 if (yaffs_has_null_parent(dev, obj)) {
4392                                         /* These directories are not hanging */
4393                                         hanging = 0;
4394                                 } else if (!parent
4395                                            || parent->variant_type !=
4396                                            YAFFS_OBJECT_TYPE_DIRECTORY) {
4397                                         hanging = 1;
4398                                 } else if (yaffs_has_null_parent(dev, parent)) {
4399                                         hanging = 0;
4400                                 } else {
4401                                         /*
4402                                          * Need to follow the parent chain to see if it is hanging.
4403                                          */
4404                                         hanging = 0;
4405                                         depth_limit = 100;
4406
4407                                         while (parent != dev->root_dir &&
4408                                                parent->parent &&
4409                                                parent->parent->variant_type ==
4410                                                YAFFS_OBJECT_TYPE_DIRECTORY
4411                                                && depth_limit > 0) {
4412                                                 parent = parent->parent;
4413                                                 depth_limit--;
4414                                         }
4415                                         if (parent != dev->root_dir)
4416                                                 hanging = 1;
4417                                 }
4418                                 if (hanging) {
4419                                         yaffs_trace(YAFFS_TRACE_SCAN,
4420                                                 "Hanging object %d moved to lost and found",
4421                                                 obj->obj_id);
4422                                         yaffs_add_obj_to_dir(dev->lost_n_found,
4423                                                              obj);
4424                                 }
4425                         }
4426                 }
4427         }
4428 }
4429
4430 /*
4431  * Delete directory contents for cleaning up lost and found.
4432  */
4433 static void yaffs_del_dir_contents(struct yaffs_obj *dir)
4434 {
4435         struct yaffs_obj *obj;
4436         struct list_head *lh;
4437         struct list_head *n;
4438
4439         if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4440                 YBUG();
4441
4442         list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
4443                 if (lh) {
4444                         obj = list_entry(lh, struct yaffs_obj, siblings);
4445                         if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
4446                                 yaffs_del_dir_contents(obj);
4447
4448                         yaffs_trace(YAFFS_TRACE_SCAN,
4449                                 "Deleting lost_found object %d",
4450                                 obj->obj_id);
4451
4452                         /* Need to use UnlinkObject since Delete would not handle
4453                          * hardlinked objects correctly.
4454                          */
4455                         yaffs_unlink_obj(obj);
4456                 }
4457         }
4458
4459 }
4460
4461 static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
4462 {
4463         yaffs_del_dir_contents(dev->lost_n_found);
4464 }
4465
4466
4467 struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4468                                      const YCHAR * name)
4469 {
4470         int sum;
4471
4472         struct list_head *i;
4473         YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
4474
4475         struct yaffs_obj *l;
4476
4477         if (!name)
4478                 return NULL;
4479
4480         if (!directory) {
4481                 yaffs_trace(YAFFS_TRACE_ALWAYS,
4482                         "tragedy: yaffs_find_by_name: null pointer directory"
4483                         );
4484                 YBUG();
4485                 return NULL;
4486         }
4487         if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
4488                 yaffs_trace(YAFFS_TRACE_ALWAYS,
4489                         "tragedy: yaffs_find_by_name: non-directory"
4490                         );
4491                 YBUG();
4492         }
4493
4494         sum = yaffs_calc_name_sum(name);
4495
4496         list_for_each(i, &directory->variant.dir_variant.children) {
4497                 l = list_entry(i, struct yaffs_obj, siblings);
4498
4499                 if (l->parent != directory)
4500                         YBUG();
4501
4502                 yaffs_check_obj_details_loaded(l);
4503
4504                 /* Special case for lost-n-found */
4505                 if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4506                         if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
4507                                 return l;
4508                 } else if (l->sum == sum
4509                            || l->hdr_chunk <= 0) {
4510                         /* LostnFound chunk called Objxxx
4511                          * Do a real check
4512                          */
4513                         yaffs_get_obj_name(l, buffer,
4514                                 YAFFS_MAX_NAME_LENGTH + 1);
4515                         if (strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
4516                                 return l;
4517                 }
4518         }
4519
4520         return NULL;
4521 }
4522
4523 /* GetEquivalentObject dereferences any hard links to get to the
4524  * actual object.
4525  */
4526
4527 struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
4528 {
4529         if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
4530                 /* We want the object id of the equivalent object, not this one */
4531                 obj = obj->variant.hardlink_variant.equiv_obj;
4532                 yaffs_check_obj_details_loaded(obj);
4533         }
4534         return obj;
4535 }
4536
4537 /*
4538  *  A note or two on object names.
4539  *  * If the object name is missing, we then make one up in the form objnnn
4540  *
4541  *  * ASCII names are stored in the object header's name field from byte zero
4542  *  * Unicode names are historically stored starting from byte zero.
4543  *
4544  * Then there are automatic Unicode names...
4545  * The purpose of these is to save names in a way that can be read as
4546  * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
4547  * system to share files.
4548  *
4549  * These automatic unicode are stored slightly differently...
4550  *  - If the name can fit in the ASCII character space then they are saved as 
4551  *    ascii names as per above.
4552  *  - If the name needs Unicode then the name is saved in Unicode
4553  *    starting at oh->name[1].
4554
4555  */
4556 static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
4557                                 int buffer_size)
4558 {
4559         /* Create an object name if we could not find one. */
4560         if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
4561                 YCHAR local_name[20];
4562                 YCHAR num_string[20];
4563                 YCHAR *x = &num_string[19];
4564                 unsigned v = obj->obj_id;
4565                 num_string[19] = 0;
4566                 while (v > 0) {
4567                         x--;
4568                         *x = '0' + (v % 10);
4569                         v /= 10;
4570                 }
4571                 /* make up a name */
4572                 strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4573                 strcat(local_name, x);
4574                 strncpy(name, local_name, buffer_size - 1);
4575         }
4576 }
4577
4578 int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
4579 {
4580         memset(name, 0, buffer_size * sizeof(YCHAR));
4581
4582         yaffs_check_obj_details_loaded(obj);
4583
4584         if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
4585                 strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
4586         }
4587 #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
4588         else if (obj->short_name[0]) {
4589                 strcpy(name, obj->short_name);
4590         }
4591 #endif
4592         else if (obj->hdr_chunk > 0) {
4593                 int result;
4594                 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
4595
4596                 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
4597
4598                 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
4599
4600                 if (obj->hdr_chunk > 0) {
4601                         result = yaffs_rd_chunk_tags_nand(obj->my_dev,
4602                                                           obj->hdr_chunk,
4603                                                           buffer, NULL);
4604                 }
4605                 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4606                                         buffer_size);
4607
4608                 yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
4609         }
4610
4611         yaffs_fix_null_name(obj, name, buffer_size);
4612
4613         return strnlen(name, YAFFS_MAX_NAME_LENGTH);
4614 }
4615
4616 int yaffs_get_obj_length(struct yaffs_obj *obj)
4617 {
4618         /* Dereference any hard linking */
4619         obj = yaffs_get_equivalent_obj(obj);
4620
4621         if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
4622                 return obj->variant.file_variant.file_size;
4623         if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
4624                 if (!obj->variant.symlink_variant.alias)
4625                         return 0;
4626                 return strnlen(obj->variant.symlink_variant.alias,
4627                                      YAFFS_MAX_ALIAS_LENGTH);
4628         } else {
4629                 /* Only a directory should drop through to here */
4630                 return obj->my_dev->data_bytes_per_chunk;
4631         }
4632 }
4633
4634 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
4635 {
4636         int count = 0;
4637         struct list_head *i;
4638
4639         if (!obj->unlinked)
4640                 count++;        /* the object itself */
4641
4642         list_for_each(i, &obj->hard_links)
4643             count++;            /* add the hard links; */
4644
4645         return count;
4646 }
4647
4648 int yaffs_get_obj_inode(struct yaffs_obj *obj)
4649 {
4650         obj = yaffs_get_equivalent_obj(obj);
4651
4652         return obj->obj_id;
4653 }
4654
4655 unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
4656 {
4657         obj = yaffs_get_equivalent_obj(obj);
4658
4659         switch (obj->variant_type) {
4660         case YAFFS_OBJECT_TYPE_FILE:
4661                 return DT_REG;
4662                 break;
4663         case YAFFS_OBJECT_TYPE_DIRECTORY:
4664                 return DT_DIR;
4665                 break;
4666         case YAFFS_OBJECT_TYPE_SYMLINK:
4667                 return DT_LNK;
4668                 break;
4669         case YAFFS_OBJECT_TYPE_HARDLINK:
4670                 return DT_REG;
4671                 break;
4672         case YAFFS_OBJECT_TYPE_SPECIAL:
4673                 if (S_ISFIFO(obj->yst_mode))
4674                         return DT_FIFO;
4675                 if (S_ISCHR(obj->yst_mode))
4676                         return DT_CHR;
4677                 if (S_ISBLK(obj->yst_mode))
4678                         return DT_BLK;
4679                 if (S_ISSOCK(obj->yst_mode))
4680                         return DT_SOCK;
4681         default:
4682                 return DT_REG;
4683                 break;
4684         }
4685 }
4686
4687 YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
4688 {
4689         obj = yaffs_get_equivalent_obj(obj);
4690         if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
4691                 return yaffs_clone_str(obj->variant.symlink_variant.alias);
4692         else
4693                 return yaffs_clone_str(_Y(""));
4694 }
4695
4696 /*--------------------------- Initialisation code -------------------------- */
4697
4698 static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
4699 {
4700
4701         /* Common functions, gotta have */
4702         if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
4703                 return 0;
4704
4705 #ifdef CONFIG_YAFFS_YAFFS2
4706
4707         /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
4708         if (dev->param.write_chunk_tags_fn &&
4709             dev->param.read_chunk_tags_fn &&
4710             !dev->param.write_chunk_fn &&
4711             !dev->param.read_chunk_fn &&
4712             dev->param.bad_block_fn && dev->param.query_block_fn)
4713                 return 1;
4714 #endif
4715
4716         /* Can use the "spare" style interface for yaffs1 */
4717         if (!dev->param.is_yaffs2 &&
4718             !dev->param.write_chunk_tags_fn &&
4719             !dev->param.read_chunk_tags_fn &&
4720             dev->param.write_chunk_fn &&
4721             dev->param.read_chunk_fn &&
4722             !dev->param.bad_block_fn && !dev->param.query_block_fn)
4723                 return 1;
4724
4725         return 0;               /* bad */
4726 }
4727
4728 static int yaffs_create_initial_dir(struct yaffs_dev *dev)
4729 {
4730         /* Initialise the unlinked, deleted, root and lost and found directories */
4731
4732         dev->lost_n_found = dev->root_dir = NULL;
4733         dev->unlinked_dir = dev->del_dir = NULL;
4734
4735         dev->unlinked_dir =
4736             yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4737
4738         dev->del_dir =
4739             yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
4740
4741         dev->root_dir =
4742             yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
4743                                   YAFFS_ROOT_MODE | S_IFDIR);
4744         dev->lost_n_found =
4745             yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
4746                                   YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4747
4748         if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
4749             && dev->del_dir) {
4750                 yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
4751                 return YAFFS_OK;
4752         }
4753
4754         return YAFFS_FAIL;
4755 }
4756
4757 int yaffs_guts_initialise(struct yaffs_dev *dev)
4758 {
4759         int init_failed = 0;
4760         unsigned x;
4761         int bits;
4762
4763         yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()" );
4764
4765         /* Check stuff that must be set */
4766
4767         if (!dev) {
4768                 yaffs_trace(YAFFS_TRACE_ALWAYS,
4769                         "yaffs: Need a device"
4770                         );
4771                 return YAFFS_FAIL;
4772         }
4773
4774         dev->internal_start_block = dev->param.start_block;
4775         dev->internal_end_block = dev->param.end_block;
4776         dev->block_offset = 0;
4777         dev->chunk_offset = 0;
4778         dev->n_free_chunks = 0;
4779
4780         dev->gc_block = 0;
4781
4782         if (dev->param.start_block == 0) {
4783                 dev->internal_start_block = dev->param.start_block + 1;
4784                 dev->internal_end_block = dev->param.end_block + 1;
4785                 dev->block_offset = 1;
4786                 dev->chunk_offset = dev->param.chunks_per_block;
4787         }
4788
4789         /* Check geometry parameters. */
4790
4791         if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
4792                 dev->param.total_bytes_per_chunk < 1024) ||
4793                 (!dev->param.is_yaffs2 &&
4794                         dev->param.total_bytes_per_chunk < 512) ||
4795                 (dev->param.inband_tags && !dev->param.is_yaffs2) ||
4796                  dev->param.chunks_per_block < 2 ||
4797                  dev->param.n_reserved_blocks < 2 ||
4798                 dev->internal_start_block <= 0 || 
4799                 dev->internal_end_block <= 0 || 
4800                 dev->internal_end_block <= 
4801                 (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
4802                 ) {
4803                 /* otherwise it is too small */
4804                 yaffs_trace(YAFFS_TRACE_ALWAYS,
4805                         "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
4806                         dev->param.total_bytes_per_chunk,
4807                         dev->param.is_yaffs2 ? "2" : "",
4808                         dev->param.inband_tags);
4809                 return YAFFS_FAIL;
4810         }
4811
4812         if (yaffs_init_nand(dev) != YAFFS_OK) {
4813                 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
4814                 return YAFFS_FAIL;
4815         }
4816
4817         /* Sort out space for inband tags, if required */
4818         if (dev->param.inband_tags)
4819                 dev->data_bytes_per_chunk =
4820                     dev->param.total_bytes_per_chunk -
4821                     sizeof(struct yaffs_packed_tags2_tags_only);
4822         else
4823                 dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
4824
4825         /* Got the right mix of functions? */
4826         if (!yaffs_check_dev_fns(dev)) {
4827                 /* Function missing */
4828                 yaffs_trace(YAFFS_TRACE_ALWAYS,
4829                         "device function(s) missing or wrong");
4830
4831                 return YAFFS_FAIL;
4832         }
4833
4834         if (dev->is_mounted) {
4835                 yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
4836                 return YAFFS_FAIL;
4837         }
4838
4839         /* Finished with most checks. One or two more checks happen later on too. */
4840
4841         dev->is_mounted = 1;
4842
4843         /* OK now calculate a few things for the device */
4844
4845         /*
4846          *  Calculate all the chunk size manipulation numbers:
4847          */
4848         x = dev->data_bytes_per_chunk;
4849         /* We always use dev->chunk_shift and dev->chunk_div */
4850         dev->chunk_shift = calc_shifts(x);
4851         x >>= dev->chunk_shift;
4852         dev->chunk_div = x;
4853         /* We only use chunk mask if chunk_div is 1 */
4854         dev->chunk_mask = (1 << dev->chunk_shift) - 1;
4855
4856         /*
4857          * Calculate chunk_grp_bits.
4858          * We need to find the next power of 2 > than internal_end_block
4859          */
4860
4861         x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
4862
4863         bits = calc_shifts_ceiling(x);
4864
4865         /* Set up tnode width if wide tnodes are enabled. */
4866         if (!dev->param.wide_tnodes_disabled) {
4867                 /* bits must be even so that we end up with 32-bit words */
4868                 if (bits & 1)
4869                         bits++;
4870                 if (bits < 16)
4871                         dev->tnode_width = 16;
4872                 else
4873                         dev->tnode_width = bits;
4874         } else {
4875                 dev->tnode_width = 16;
4876         }
4877
4878         dev->tnode_mask = (1 << dev->tnode_width) - 1;
4879
4880         /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
4881          * so if the bitwidth of the
4882          * chunk range we're using is greater than 16 we need
4883          * to figure out chunk shift and chunk_grp_size
4884          */
4885
4886         if (bits <= dev->tnode_width)
4887                 dev->chunk_grp_bits = 0;
4888         else
4889                 dev->chunk_grp_bits = bits - dev->tnode_width;
4890
4891         dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
4892         if (dev->tnode_size < sizeof(struct yaffs_tnode))
4893                 dev->tnode_size = sizeof(struct yaffs_tnode);
4894
4895         dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
4896
4897         if (dev->param.chunks_per_block < dev->chunk_grp_size) {
4898                 /* We have a problem because the soft delete won't work if
4899                  * the chunk group size > chunks per block.
4900                  * This can be remedied by using larger "virtual blocks".
4901                  */
4902                 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
4903
4904                 return YAFFS_FAIL;
4905         }
4906
4907         /* OK, we've finished verifying the device, lets continue with initialisation */
4908
4909         /* More device initialisation */
4910         dev->all_gcs = 0;
4911         dev->passive_gc_count = 0;
4912         dev->oldest_dirty_gc_count = 0;
4913         dev->bg_gcs = 0;
4914         dev->gc_block_finder = 0;
4915         dev->buffered_block = -1;
4916         dev->doing_buffered_block_rewrite = 0;
4917         dev->n_deleted_files = 0;
4918         dev->n_bg_deletions = 0;
4919         dev->n_unlinked_files = 0;
4920         dev->n_ecc_fixed = 0;
4921         dev->n_ecc_unfixed = 0;
4922         dev->n_tags_ecc_fixed = 0;
4923         dev->n_tags_ecc_unfixed = 0;
4924         dev->n_erase_failures = 0;
4925         dev->n_erased_blocks = 0;
4926         dev->gc_disable = 0;
4927         dev->has_pending_prioritised_gc = 1;    /* Assume the worst for now, will get fixed on first GC */
4928         INIT_LIST_HEAD(&dev->dirty_dirs);
4929         dev->oldest_dirty_seq = 0;
4930         dev->oldest_dirty_block = 0;
4931
4932         /* Initialise temporary buffers and caches. */
4933         if (!yaffs_init_tmp_buffers(dev))
4934                 init_failed = 1;
4935
4936         dev->cache = NULL;
4937         dev->gc_cleanup_list = NULL;
4938
4939         if (!init_failed && dev->param.n_caches > 0) {
4940                 int i;
4941                 void *buf;
4942                 int cache_bytes =
4943                     dev->param.n_caches * sizeof(struct yaffs_cache);
4944
4945                 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4946                         dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
4947
4948                 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
4949
4950                 buf = (u8 *) dev->cache;
4951
4952                 if (dev->cache)
4953                         memset(dev->cache, 0, cache_bytes);
4954
4955                 for (i = 0; i < dev->param.n_caches && buf; i++) {
4956                         dev->cache[i].object = NULL;
4957                         dev->cache[i].last_use = 0;
4958                         dev->cache[i].dirty = 0;
4959                         dev->cache[i].data = buf =
4960                             kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
4961                 }
4962                 if (!buf)
4963                         init_failed = 1;
4964
4965                 dev->cache_last_use = 0;
4966         }
4967
4968         dev->cache_hits = 0;
4969
4970         if (!init_failed) {
4971                 dev->gc_cleanup_list =
4972                     kmalloc(dev->param.chunks_per_block * sizeof(u32),
4973                                         GFP_NOFS);
4974                 if (!dev->gc_cleanup_list)
4975                         init_failed = 1;
4976         }
4977
4978         if (dev->param.is_yaffs2)
4979                 dev->param.use_header_file_size = 1;
4980
4981         if (!init_failed && !yaffs_init_blocks(dev))
4982                 init_failed = 1;
4983
4984         yaffs_init_tnodes_and_objs(dev);
4985
4986         if (!init_failed && !yaffs_create_initial_dir(dev))
4987                 init_failed = 1;
4988
4989         if (!init_failed) {
4990                 /* Now scan the flash. */
4991                 if (dev->param.is_yaffs2) {
4992                         if (yaffs2_checkpt_restore(dev)) {
4993                                 yaffs_check_obj_details_loaded(dev->root_dir);
4994                                 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
4995                                         "yaffs: restored from checkpoint"
4996                                         );
4997                         } else {
4998
4999                                 /* Clean up the mess caused by an aborted checkpoint load
5000                                  * and scan backwards.
5001                                  */
5002                                 yaffs_deinit_blocks(dev);
5003
5004                                 yaffs_deinit_tnodes_and_objs(dev);
5005
5006                                 dev->n_erased_blocks = 0;
5007                                 dev->n_free_chunks = 0;
5008                                 dev->alloc_block = -1;
5009                                 dev->alloc_page = -1;
5010                                 dev->n_deleted_files = 0;
5011                                 dev->n_unlinked_files = 0;
5012                                 dev->n_bg_deletions = 0;
5013
5014                                 if (!init_failed && !yaffs_init_blocks(dev))
5015                                         init_failed = 1;
5016
5017                                 yaffs_init_tnodes_and_objs(dev);
5018
5019                                 if (!init_failed
5020                                     && !yaffs_create_initial_dir(dev))
5021                                         init_failed = 1;
5022
5023                                 if (!init_failed && !yaffs2_scan_backwards(dev))
5024                                         init_failed = 1;
5025                         }
5026                 } else if (!yaffs1_scan(dev)) {
5027                         init_failed = 1;
5028                 }
5029
5030                 yaffs_strip_deleted_objs(dev);
5031                 yaffs_fix_hanging_objs(dev);
5032                 if (dev->param.empty_lost_n_found)
5033                         yaffs_empty_l_n_f(dev);
5034         }
5035
5036         if (init_failed) {
5037                 /* Clean up the mess */
5038                 yaffs_trace(YAFFS_TRACE_TRACING,
5039                   "yaffs: yaffs_guts_initialise() aborted.");
5040
5041                 yaffs_deinitialise(dev);
5042                 return YAFFS_FAIL;
5043         }
5044
5045         /* Zero out stats */
5046         dev->n_page_reads = 0;
5047         dev->n_page_writes = 0;
5048         dev->n_erasures = 0;
5049         dev->n_gc_copies = 0;
5050         dev->n_retired_writes = 0;
5051
5052         dev->n_retired_blocks = 0;
5053
5054         yaffs_verify_free_chunks(dev);
5055         yaffs_verify_blocks(dev);
5056
5057         /* Clean up any aborted checkpoint data */
5058         if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
5059                 yaffs2_checkpt_invalidate(dev);
5060
5061         yaffs_trace(YAFFS_TRACE_TRACING,
5062           "yaffs: yaffs_guts_initialise() done.");
5063         return YAFFS_OK;
5064
5065 }
5066
5067 void yaffs_deinitialise(struct yaffs_dev *dev)
5068 {
5069         if (dev->is_mounted) {
5070                 int i;
5071
5072                 yaffs_deinit_blocks(dev);
5073                 yaffs_deinit_tnodes_and_objs(dev);
5074                 if (dev->param.n_caches > 0 && dev->cache) {
5075
5076                         for (i = 0; i < dev->param.n_caches; i++) {
5077                                 if (dev->cache[i].data)
5078                                         kfree(dev->cache[i].data);
5079                                 dev->cache[i].data = NULL;
5080                         }
5081
5082                         kfree(dev->cache);
5083                         dev->cache = NULL;
5084                 }
5085
5086                 kfree(dev->gc_cleanup_list);
5087
5088                 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
5089                         kfree(dev->temp_buffer[i].buffer);
5090
5091                 dev->is_mounted = 0;
5092
5093                 if (dev->param.deinitialise_flash_fn)
5094                         dev->param.deinitialise_flash_fn(dev);
5095         }
5096 }
5097
5098 int yaffs_count_free_chunks(struct yaffs_dev *dev)
5099 {
5100         int n_free = 0;
5101         int b;
5102
5103         struct yaffs_block_info *blk;
5104
5105         blk = dev->block_info;
5106         for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
5107                 switch (blk->block_state) {
5108                 case YAFFS_BLOCK_STATE_EMPTY:
5109                 case YAFFS_BLOCK_STATE_ALLOCATING:
5110                 case YAFFS_BLOCK_STATE_COLLECTING:
5111                 case YAFFS_BLOCK_STATE_FULL:
5112                         n_free +=
5113                             (dev->param.chunks_per_block - blk->pages_in_use +
5114                              blk->soft_del_pages);
5115                         break;
5116                 default:
5117                         break;
5118                 }
5119                 blk++;
5120         }
5121
5122         return n_free;
5123 }
5124
5125 int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
5126 {
5127         /* This is what we report to the outside world */
5128
5129         int n_free;
5130         int n_dirty_caches;
5131         int blocks_for_checkpt;
5132         int i;
5133
5134         n_free = dev->n_free_chunks;
5135         n_free += dev->n_deleted_files;
5136
5137         /* Now count the number of dirty chunks in the cache and subtract those */
5138
5139         for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
5140                 if (dev->cache[i].dirty)
5141                         n_dirty_caches++;
5142         }
5143
5144         n_free -= n_dirty_caches;
5145
5146         n_free -=
5147             ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
5148
5149         /* Now we figure out how much to reserve for the checkpoint and report that... */
5150         blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
5151
5152         n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
5153
5154         if (n_free < 0)
5155                 n_free = 0;
5156
5157         return n_free;
5158
5159 }