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