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