Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[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
21
22 int yaffs1_scan(yaffs_dev_t *dev)
23 {
24         yaffs_ext_tags tags;
25         int blk;
26         int result;
27
28         int chunk;
29         int c;
30         int deleted;
31         yaffs_block_state_t state;
32         yaffs_obj_t *hard_list = NULL;
33         yaffs_block_info_t *bi;
34         __u32 seq_number;
35         yaffs_obj_header *oh;
36         yaffs_obj_t *in;
37         yaffs_obj_t *parent;
38
39         int alloc_failed = 0;
40
41         struct yaffs_shadow_fixer_s *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 = (yaffs_obj_header *) 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_s *fixer;
220                                         fixer = YMALLOC(sizeof(struct yaffs_shadow_fixer_s));
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 #ifdef CONFIG_YAFFS_WINCE
263                                         in->win_atime[0] = oh->win_atime[0];
264                                         in->win_ctime[0] = oh->win_ctime[0];
265                                         in->win_mtime[0] = oh->win_mtime[0];
266                                         in->win_atime[1] = oh->win_atime[1];
267                                         in->win_ctime[1] = oh->win_ctime[1];
268                                         in->win_mtime[1] = oh->win_mtime[1];
269 #else
270                                         in->yst_uid = oh->yst_uid;
271                                         in->yst_gid = oh->yst_gid;
272                                         in->yst_atime = oh->yst_atime;
273                                         in->yst_mtime = oh->yst_mtime;
274                                         in->yst_ctime = oh->yst_ctime;
275                                         in->yst_rdev = oh->yst_rdev;
276 #endif
277                                         in->hdr_chunk = chunk;
278                                         in->serial = tags.serial_number;
279
280                                 } else if (in && !in->valid) {
281                                         /* we need to load this info */
282
283                                         in->valid = 1;
284                                         in->variant_type = oh->type;
285
286                                         in->yst_mode = oh->yst_mode;
287 #ifdef CONFIG_YAFFS_WINCE
288                                         in->win_atime[0] = oh->win_atime[0];
289                                         in->win_ctime[0] = oh->win_ctime[0];
290                                         in->win_mtime[0] = oh->win_mtime[0];
291                                         in->win_atime[1] = oh->win_atime[1];
292                                         in->win_ctime[1] = oh->win_ctime[1];
293                                         in->win_mtime[1] = oh->win_mtime[1];
294 #else
295                                         in->yst_uid = oh->yst_uid;
296                                         in->yst_gid = oh->yst_gid;
297                                         in->yst_atime = oh->yst_atime;
298                                         in->yst_mtime = oh->yst_mtime;
299                                         in->yst_ctime = oh->yst_ctime;
300                                         in->yst_rdev = oh->yst_rdev;
301 #endif
302                                         in->hdr_chunk = chunk;
303                                         in->serial = tags.serial_number;
304
305                                         yaffs_set_obj_name_from_oh(in, oh);
306                                         in->dirty = 0;
307
308                                         /* directory stuff...
309                                          * hook up to parent
310                                          */
311
312                                         parent =
313                                             yaffs_find_or_create_by_number
314                                             (dev, oh->parent_obj_id,
315                                              YAFFS_OBJECT_TYPE_DIRECTORY);
316                                         if (!parent)
317                                                 alloc_failed = 1;
318                                         if (parent && parent->variant_type ==
319                                             YAFFS_OBJECT_TYPE_UNKNOWN) {
320                                                 /* Set up as a directory */
321                                                 parent->variant_type =
322                                                         YAFFS_OBJECT_TYPE_DIRECTORY;
323                                                 YINIT_LIST_HEAD(&parent->variant.
324                                                                 dir_variant.
325                                                                 children);
326                                         } else if (!parent || parent->variant_type !=
327                                                    YAFFS_OBJECT_TYPE_DIRECTORY) {
328                                                 /* Hoosterman, another problem....
329                                                  * We're trying to use a non-directory as a directory
330                                                  */
331
332                                                 T(YAFFS_TRACE_ERROR,
333                                                   (TSTR
334                                                    ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
335                                                     TENDSTR)));
336                                                 parent = dev->lost_n_found;
337                                         }
338
339                                         yaffs_add_obj_to_dir(parent, in);
340
341                                         if (0 && (parent == dev->del_dir ||
342                                                   parent == dev->unlinked_dir)) {
343                                                 in->deleted = 1;        /* If it is unlinked at start up then it wants deleting */
344                                                 dev->n_deleted_files++;
345                                         }
346                                         /* Note re hardlinks.
347                                          * Since we might scan a hardlink before its equivalent object is scanned
348                                          * we put them all in a list.
349                                          * After scanning is complete, we should have all the objects, so we run through this
350                                          * list and fix up all the chains.
351                                          */
352
353                                         switch (in->variant_type) {
354                                         case YAFFS_OBJECT_TYPE_UNKNOWN:
355                                                 /* Todo got a problem */
356                                                 break;
357                                         case YAFFS_OBJECT_TYPE_FILE:
358                                                 if (dev->param.use_header_file_size)
359
360                                                         in->variant.file_variant.
361                                                             file_size =
362                                                             oh->file_size;
363
364                                                 break;
365                                         case YAFFS_OBJECT_TYPE_HARDLINK:
366                                                 in->variant.hardlink_variant.
367                                                         equiv_id =
368                                                         oh->equiv_id;
369                                                 in->hard_links.next =
370                                                         (struct ylist_head *)
371                                                         hard_list;
372                                                 hard_list = in;
373                                                 break;
374                                         case YAFFS_OBJECT_TYPE_DIRECTORY:
375                                                 /* Do nothing */
376                                                 break;
377                                         case YAFFS_OBJECT_TYPE_SPECIAL:
378                                                 /* Do nothing */
379                                                 break;
380                                         case YAFFS_OBJECT_TYPE_SYMLINK:
381                                                 in->variant.symlink_variant.alias =
382                                                     yaffs_clone_str(oh->alias);
383                                                 if (!in->variant.symlink_variant.alias)
384                                                         alloc_failed = 1;
385                                                 break;
386                                         }
387
388                                 }
389                         }
390                 }
391
392                 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
393                         /* If we got this far while scanning, then the block is fully allocated.*/
394                         state = YAFFS_BLOCK_STATE_FULL;
395                 }
396
397                 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
398                         /* If the block was partially allocated then treat it as fully allocated.*/
399                         state = YAFFS_BLOCK_STATE_FULL;
400                         dev->alloc_block = -1;
401                 }
402
403                 bi->block_state = state;
404
405                 /* Now let's see if it was dirty */
406                 if (bi->pages_in_use == 0 &&
407                     !bi->has_shrink_hdr &&
408                     bi->block_state == YAFFS_BLOCK_STATE_FULL) {
409                         yaffs_block_became_dirty(dev, blk);
410                 }
411
412         }
413
414
415         /* Ok, we've done all the scanning.
416          * Fix up the hard link chains.
417          * We should now have scanned all the objects, now it's time to add these
418          * hardlinks.
419          */
420
421         yaffs_link_fixup(dev, hard_list);
422
423         /* Fix up any shadowed objects */
424         {
425                 struct yaffs_shadow_fixer_s *fixer;
426                 yaffs_obj_t *obj;
427
428                 while (shadow_fixers) {
429                         fixer = shadow_fixers;
430                         shadow_fixers = fixer->next;
431                         /* Complete the rename transaction by deleting the shadowed object
432                          * then setting the object header to unshadowed.
433                          */
434                         obj = yaffs_find_by_number(dev, fixer->shadowed_id);
435                         if (obj)
436                                 yaffs_del_obj(obj);
437
438                         obj = yaffs_find_by_number(dev, fixer->obj_id);
439
440                         if (obj)
441                                 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
442
443                         YFREE(fixer);
444                 }
445         }
446
447         yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
448
449         if (alloc_failed)
450                 return YAFFS_FAIL;
451
452         T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));
453
454
455         return YAFFS_OK;
456 }
457