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