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