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