yaffs testing: Don't want ramdisk created if it is already there
[yaffs2.git] / yaffs_yaffs1.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include "yaffs_yaffs1.h"
15 #include "yportenv.h"
16 #include "yaffs_trace.h"
17 #include "yaffs_bitmap.h"
18 #include "yaffs_getblockinfo.h"
19 #include "yaffs_nand.h"
20 #include "yaffs_attribs.h"
21
22 int yaffs1_scan(struct yaffs_dev *dev)
23 {
24         struct yaffs_ext_tags tags;
25         int blk;
26         int result;
27
28         int chunk;
29         int c;
30         int deleted;
31         enum yaffs_block_state state;
32         struct yaffs_obj *hard_list = NULL;
33         struct yaffs_block_info *bi;
34         u32 seq_number;
35         struct yaffs_obj_hdr *oh;
36         struct yaffs_obj *in;
37         struct yaffs_obj *parent;
38
39         int alloc_failed = 0;
40
41         struct yaffs_shadow_fixer *shadow_fixers = NULL;
42
43         u8 *chunk_data;
44
45         T(YAFFS_TRACE_SCAN,
46           (TSTR("yaffs1_scan starts  intstartblk %d intendblk %d..." TENDSTR),
47            dev->internal_start_block, dev->internal_end_block));
48
49         chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
50
51         dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
52
53         /* Scan all the blocks to determine their state */
54         bi = dev->block_info;
55         for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
56              blk++) {
57                 yaffs_clear_chunk_bits(dev, blk);
58                 bi->pages_in_use = 0;
59                 bi->soft_del_pages = 0;
60
61                 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
62
63                 bi->block_state = state;
64                 bi->seq_number = seq_number;
65
66                 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
67                         bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
68
69                 T(YAFFS_TRACE_SCAN_DEBUG,
70                   (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
71                    state, seq_number));
72
73                 if (state == YAFFS_BLOCK_STATE_DEAD) {
74                         T(YAFFS_TRACE_BAD_BLOCKS,
75                           (TSTR("block %d is bad" TENDSTR), blk));
76                 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
77                         T(YAFFS_TRACE_SCAN_DEBUG,
78                           (TSTR("Block empty " TENDSTR)));
79                         dev->n_erased_blocks++;
80                         dev->n_free_chunks += dev->param.chunks_per_block;
81                 }
82                 bi++;
83         }
84
85         /* For each block.... */
86         for (blk = dev->internal_start_block;
87              !alloc_failed && blk <= dev->internal_end_block; blk++) {
88
89                 YYIELD();
90
91                 bi = yaffs_get_block_info(dev, blk);
92                 state = bi->block_state;
93
94                 deleted = 0;
95
96                 /* For each chunk in each block that needs scanning.... */
97                 for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
98                      state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
99                         /* Read the tags and decide what to do */
100                         chunk = blk * dev->param.chunks_per_block + c;
101
102                         result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
103                                                           &tags);
104
105                         /* Let's have a good look at this chunk... */
106
107                         if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
108                             || tags.is_deleted) {
109                                 /* YAFFS1 only...
110                                  * A deleted chunk
111                                  */
112                                 deleted++;
113                                 dev->n_free_chunks++;
114                                 /*T((" %d %d deleted\n",blk,c)); */
115                         } else if (!tags.chunk_used) {
116                                 /* An unassigned chunk in the block
117                                  * This means that either the block is empty or
118                                  * this is the one being allocated from
119                                  */
120
121                                 if (c == 0) {
122                                         /* We're looking at the first chunk in the block so the block is unused */
123                                         state = YAFFS_BLOCK_STATE_EMPTY;
124                                         dev->n_erased_blocks++;
125                                 } else {
126                                         /* this is the block being allocated from */
127                                         T(YAFFS_TRACE_SCAN,
128                                           (TSTR
129                                            (" Allocating from %d %d" TENDSTR),
130                                            blk, c));
131                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
132                                         dev->alloc_block = blk;
133                                         dev->alloc_page = c;
134                                         dev->alloc_block_finder = blk;
135                                         /* Set block finder here to encourage the allocator to go forth from here. */
136
137                                 }
138
139                                 dev->n_free_chunks +=
140                                     (dev->param.chunks_per_block - c);
141                         } else if (tags.chunk_id > 0) {
142                                 /* chunk_id > 0 so it is a data chunk... */
143                                 unsigned int endpos;
144
145                                 yaffs_set_chunk_bit(dev, blk, c);
146                                 bi->pages_in_use++;
147
148                                 in = yaffs_find_or_create_by_number(dev,
149                                                                     tags.obj_id,
150                                                                     YAFFS_OBJECT_TYPE_FILE);
151                                 /* PutChunkIntoFile checks for a clash (two data chunks with
152                                  * the same chunk_id).
153                                  */
154
155                                 if (!in)
156                                         alloc_failed = 1;
157
158                                 if (in) {
159                                         if (!yaffs_put_chunk_in_file
160                                             (in, tags.chunk_id, chunk, 1))
161                                                 alloc_failed = 1;
162                                 }
163
164                                 endpos =
165                                     (tags.chunk_id -
166                                      1) * dev->data_bytes_per_chunk +
167                                     tags.n_bytes;
168                                 if (in
169                                     && in->variant_type ==
170                                     YAFFS_OBJECT_TYPE_FILE
171                                     && in->variant.file_variant.scanned_size <
172                                     endpos) {
173                                         in->variant.file_variant.scanned_size =
174                                             endpos;
175                                         if (!dev->param.use_header_file_size) {
176                                                 in->variant.
177                                                     file_variant.file_size =
178                                                     in->variant.
179                                                     file_variant.scanned_size;
180                                         }
181
182                                 }
183                                 /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id));   */
184                         } else {
185                                 /* chunk_id == 0, so it is an ObjectHeader.
186                                  * Thus, we read in the object header and make the object
187                                  */
188                                 yaffs_set_chunk_bit(dev, blk, c);
189                                 bi->pages_in_use++;
190
191                                 result = yaffs_rd_chunk_tags_nand(dev, chunk,
192                                                                   chunk_data,
193                                                                   NULL);
194
195                                 oh = (struct yaffs_obj_hdr *)chunk_data;
196
197                                 in = yaffs_find_by_number(dev, tags.obj_id);
198                                 if (in && in->variant_type != oh->type) {
199                                         /* This should not happen, but somehow
200                                          * Wev'e ended up with an obj_id that has been reused but not yet
201                                          * deleted, and worse still it has changed type. Delete the old object.
202                                          */
203
204                                         yaffs_del_obj(in);
205
206                                         in = 0;
207                                 }
208
209                                 in = yaffs_find_or_create_by_number(dev,
210                                                                     tags.obj_id,
211                                                                     oh->type);
212
213                                 if (!in)
214                                         alloc_failed = 1;
215
216                                 if (in && oh->shadows_obj > 0) {
217
218                                         struct yaffs_shadow_fixer *fixer;
219                                         fixer =
220                                             YMALLOC(sizeof
221                                                     (struct
222                                                      yaffs_shadow_fixer));
223                                         if (fixer) {
224                                                 fixer->next = shadow_fixers;
225                                                 shadow_fixers = fixer;
226                                                 fixer->obj_id = tags.obj_id;
227                                                 fixer->shadowed_id =
228                                                     oh->shadows_obj;
229                                                 T(YAFFS_TRACE_SCAN,
230                                                   (TSTR
231                                                    (" Shadow fixer: %d shadows %d"
232                                                     TENDSTR), fixer->obj_id,
233                                                    fixer->shadowed_id));
234
235                                         }
236
237                                 }
238
239                                 if (in && in->valid) {
240                                         /* We have already filled this one. We have a duplicate and need to resolve it. */
241
242                                         unsigned existing_serial = in->serial;
243                                         unsigned new_serial =
244                                             tags.serial_number;
245
246                                         if (((existing_serial + 1) & 3) ==
247                                             new_serial) {
248                                                 /* Use new one - destroy the exisiting one */
249                                                 yaffs_chunk_del(dev,
250                                                                 in->hdr_chunk,
251                                                                 1, __LINE__);
252                                                 in->valid = 0;
253                                         } else {
254                                                 /* Use existing - destroy this one. */
255                                                 yaffs_chunk_del(dev, chunk, 1,
256                                                                 __LINE__);
257                                         }
258                                 }
259
260                                 if (in && !in->valid &&
261                                     (tags.obj_id == YAFFS_OBJECTID_ROOT ||
262                                      tags.obj_id ==
263                                      YAFFS_OBJECTID_LOSTNFOUND)) {
264                                         /* We only load some info, don't fiddle with directory structure */
265                                         in->valid = 1;
266                                         in->variant_type = oh->type;
267
268                                         in->yst_mode = oh->yst_mode;
269                                         yaffs_load_attribs(in, oh);
270                                         in->hdr_chunk = chunk;
271                                         in->serial = tags.serial_number;
272
273                                 } else if (in && !in->valid) {
274                                         /* we need to load this info */
275
276                                         in->valid = 1;
277                                         in->variant_type = oh->type;
278
279                                         in->yst_mode = oh->yst_mode;
280                                         yaffs_load_attribs(in, oh);
281                                         in->hdr_chunk = chunk;
282                                         in->serial = tags.serial_number;
283
284                                         yaffs_set_obj_name_from_oh(in, oh);
285                                         in->dirty = 0;
286
287                                         /* directory stuff...
288                                          * hook up to parent
289                                          */
290
291                                         parent =
292                                             yaffs_find_or_create_by_number
293                                             (dev, oh->parent_obj_id,
294                                              YAFFS_OBJECT_TYPE_DIRECTORY);
295                                         if (!parent)
296                                                 alloc_failed = 1;
297                                         if (parent && parent->variant_type ==
298                                             YAFFS_OBJECT_TYPE_UNKNOWN) {
299                                                 /* Set up as a directory */
300                                                 parent->variant_type =
301                                                     YAFFS_OBJECT_TYPE_DIRECTORY;
302                                                 INIT_LIST_HEAD(&parent->
303                                                                variant.dir_variant.children);
304                                         } else if (!parent
305                                                    || parent->variant_type !=
306                                                    YAFFS_OBJECT_TYPE_DIRECTORY) {
307                                                 /* Hoosterman, another problem....
308                                                  * We're trying to use a non-directory as a directory
309                                                  */
310
311                                                 T(YAFFS_TRACE_ERROR,
312                                                   (TSTR
313                                                    ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
314                                                     TENDSTR)));
315                                                 parent = dev->lost_n_found;
316                                         }
317
318                                         yaffs_add_obj_to_dir(parent, in);
319
320                                         if (0 && (parent == dev->del_dir ||
321                                                   parent ==
322                                                   dev->unlinked_dir)) {
323                                                 in->deleted = 1;        /* If it is unlinked at start up then it wants deleting */
324                                                 dev->n_deleted_files++;
325                                         }
326                                         /* Note re hardlinks.
327                                          * Since we might scan a hardlink before its equivalent object is scanned
328                                          * we put them all in a list.
329                                          * After scanning is complete, we should have all the objects, so we run through this
330                                          * list and fix up all the chains.
331                                          */
332
333                                         switch (in->variant_type) {
334                                         case YAFFS_OBJECT_TYPE_UNKNOWN:
335                                                 /* Todo got a problem */
336                                                 break;
337                                         case YAFFS_OBJECT_TYPE_FILE:
338                                                 if (dev->param.
339                                                     use_header_file_size)
340
341                                                         in->variant.
342                                                             file_variant.file_size
343                                                             = oh->file_size;
344
345                                                 break;
346                                         case YAFFS_OBJECT_TYPE_HARDLINK:
347                                                 in->variant.
348                                                     hardlink_variant.equiv_id =
349                                                     oh->equiv_id;
350                                                 in->hard_links.next =
351                                                     (struct list_head *)
352                                                     hard_list;
353                                                 hard_list = in;
354                                                 break;
355                                         case YAFFS_OBJECT_TYPE_DIRECTORY:
356                                                 /* Do nothing */
357                                                 break;
358                                         case YAFFS_OBJECT_TYPE_SPECIAL:
359                                                 /* Do nothing */
360                                                 break;
361                                         case YAFFS_OBJECT_TYPE_SYMLINK:
362                                                 in->variant.symlink_variant.
363                                                     alias =
364                                                     yaffs_clone_str(oh->alias);
365                                                 if (!in->variant.
366                                                     symlink_variant.alias)
367                                                         alloc_failed = 1;
368                                                 break;
369                                         }
370
371                                 }
372                         }
373                 }
374
375                 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
376                         /* If we got this far while scanning, then the block is fully allocated. */
377                         state = YAFFS_BLOCK_STATE_FULL;
378                 }
379
380                 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
381                         /* If the block was partially allocated then treat it as fully allocated. */
382                         state = YAFFS_BLOCK_STATE_FULL;
383                         dev->alloc_block = -1;
384                 }
385
386                 bi->block_state = state;
387
388                 /* Now let's see if it was dirty */
389                 if (bi->pages_in_use == 0 &&
390                     !bi->has_shrink_hdr &&
391                     bi->block_state == YAFFS_BLOCK_STATE_FULL) {
392                         yaffs_block_became_dirty(dev, blk);
393                 }
394
395         }
396
397         /* Ok, we've done all the scanning.
398          * Fix up the hard link chains.
399          * We should now have scanned all the objects, now it's time to add these
400          * hardlinks.
401          */
402
403         yaffs_link_fixup(dev, hard_list);
404
405         /* Fix up any shadowed objects */
406         {
407                 struct yaffs_shadow_fixer *fixer;
408                 struct yaffs_obj *obj;
409
410                 while (shadow_fixers) {
411                         fixer = shadow_fixers;
412                         shadow_fixers = fixer->next;
413                         /* Complete the rename transaction by deleting the shadowed object
414                          * then setting the object header to unshadowed.
415                          */
416                         obj = yaffs_find_by_number(dev, fixer->shadowed_id);
417                         if (obj)
418                                 yaffs_del_obj(obj);
419
420                         obj = yaffs_find_by_number(dev, fixer->obj_id);
421
422                         if (obj)
423                                 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
424
425                         YFREE(fixer);
426                 }
427         }
428
429         yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
430
431         if (alloc_failed)
432                 return YAFFS_FAIL;
433
434         T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));
435
436         return YAFFS_OK;
437 }