Clean up some version dependencies
[yaffs2.git] / yaffs_yaffs1.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 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         u32 blk;
26         int result;
27         int chunk;
28         u32 c;
29         int deleted;
30         enum yaffs_block_state state;
31         LIST_HEAD(hard_list);
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);
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;
93                         !alloc_failed && c < dev->param.chunks_per_block &&
94                         state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
95                         /* Read the tags and decide what to do */
96                         chunk = blk * dev->param.chunks_per_block + c;
97
98                         result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
99                                                           &tags);
100
101                         if (result != YAFFS_OK)
102                                 continue;
103                         /* Let's have a good look at this chunk... */
104
105                         if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
106                             tags.is_deleted) {
107                                 /* YAFFS1 only...
108                                  * A deleted chunk
109                                  */
110                                 deleted++;
111                                 dev->n_free_chunks++;
112                         } else if (!tags.chunk_used) {
113                                 /* An unassigned chunk in the block
114                                  * This means that either the block is empty or
115                                  * this is the one being allocated from
116                                  */
117
118                                 if (c == 0) {
119                                         /* We're looking at the first chunk in
120                                          *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 */
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
133                                 }
134
135                                 dev->n_free_chunks +=
136                                     (dev->param.chunks_per_block - c);
137                         } else if (tags.chunk_id > 0) {
138                                 /* chunk_id > 0 so it is a data chunk... */
139                                 unsigned int endpos;
140
141                                 yaffs_set_chunk_bit(dev, blk, c);
142                                 bi->pages_in_use++;
143
144                                 in = yaffs_find_or_create_by_number(dev,
145                                                         tags.obj_id,
146                                                         YAFFS_OBJECT_TYPE_FILE);
147                                 /* PutChunkIntoFile checks for a clash
148                                  * (two data chunks with the same chunk_id).
149                                  */
150
151                                 if (!in)
152                                         alloc_failed = 1;
153
154                                 if (in) {
155                                         if (!yaffs_put_chunk_in_file
156                                             (in, tags.chunk_id, chunk, 1))
157                                                 alloc_failed = 1;
158                                 }
159
160                                 endpos =
161                                     (tags.chunk_id - 1) *
162                                     dev->data_bytes_per_chunk +
163                                     tags.n_bytes;
164                                 if (in &&
165                                     in->variant_type ==
166                                      YAFFS_OBJECT_TYPE_FILE &&
167                                     in->variant.file_variant.stored_size <
168                                       endpos) {
169                                         in->variant.file_variant.stored_size =
170                                             endpos;
171                                         if (!dev->param.use_header_file_size) {
172                                                 in->variant.
173                                                     file_variant.file_size =
174                                                     in->variant.
175                                                     file_variant.stored_size;
176                                         }
177
178                                 }
179                         } else {
180                                 /* chunk_id == 0, so it is an ObjectHeader.
181                                  * Make the object
182                                  */
183                                 yaffs_set_chunk_bit(dev, blk, c);
184                                 bi->pages_in_use++;
185
186                                 result = yaffs_rd_chunk_tags_nand(dev, chunk,
187                                                                   chunk_data,
188                                                                   NULL);
189
190                                 oh = (struct yaffs_obj_hdr *)chunk_data;
191
192                                 in = yaffs_find_by_number(dev, tags.obj_id);
193                                 if (in && in->variant_type != oh->type) {
194                                         /* This should not happen, but somehow
195                                          * Wev'e ended up with an obj_id that
196                                          * has been reused but not yet deleted,
197                                          * and worse still it has changed type.
198                                          * Delete the old object.
199                                          */
200
201                                         yaffs_del_obj(in);
202                                         in = NULL;
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.
236                                          * We have a duplicate and need to
237                                          * resolve it. */
238
239                                         unsigned existing_serial = in->serial;
240                                         unsigned new_serial =
241                                             tags.serial_number;
242
243                                         if (((existing_serial + 1) & 3) ==
244                                             new_serial) {
245                                                 /* Use new one - destroy the
246                                                  * exisiting one */
247                                                 yaffs_chunk_del(dev,
248                                                                 in->hdr_chunk,
249                                                                 1, __LINE__);
250                                                 in->valid = 0;
251                                         } else {
252                                                 /* Use existing - destroy
253                                                  * this one. */
254                                                 yaffs_chunk_del(dev, chunk, 1,
255                                                                 __LINE__);
256                                         }
257                                 }
258
259                                 if (in && !in->valid &&
260                                     (tags.obj_id == YAFFS_OBJECTID_ROOT ||
261                                      tags.obj_id ==
262                                      YAFFS_OBJECTID_LOSTNFOUND)) {
263                                         /* We only load some info, don't fiddle
264                                          * with directory structure */
265                                         in->valid = 1;
266                                         in->variant_type = oh->type;
267
268                                         in->yst_mode = oh->yst_mode;
269                                         yaffs_load_attribs(in, oh);
270                                         in->hdr_chunk = chunk;
271                                         in->serial = tags.serial_number;
272
273                                 } else if (in && !in->valid) {
274                                         /* we need to load this info */
275
276                                         in->valid = 1;
277                                         in->variant_type = oh->type;
278
279                                         in->yst_mode = oh->yst_mode;
280                                         yaffs_load_attribs(in, oh);
281                                         in->hdr_chunk = chunk;
282                                         in->serial = tags.serial_number;
283
284                                         yaffs_set_obj_name_from_oh(in, oh);
285                                         in->dirty = 0;
286
287                                         /* directory stuff...
288                                          * hook up to parent
289                                          */
290
291                                         parent =
292                                             yaffs_find_or_create_by_number
293                                             (dev, oh->parent_obj_id,
294                                              YAFFS_OBJECT_TYPE_DIRECTORY);
295                                         if (!parent)
296                                                 alloc_failed = 1;
297                                         if (parent && parent->variant_type ==
298                                             YAFFS_OBJECT_TYPE_UNKNOWN) {
299                                                 /* Set up as a directory */
300                                                 parent->variant_type =
301                                                     YAFFS_OBJECT_TYPE_DIRECTORY;
302                                                 INIT_LIST_HEAD(&parent->
303                                                         variant.dir_variant.
304                                                         children);
305                                         } else if (!parent ||
306                                                 parent->variant_type !=
307                                                 YAFFS_OBJECT_TYPE_DIRECTORY) {
308                                                 /* Hoosterman, a problem....
309                                                  * We're trying to use a
310                                                  * non-directory as a directory
311                                                  */
312
313                                                 yaffs_trace(YAFFS_TRACE_ERROR,
314                                                         "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
315                                                         );
316                                                 parent = dev->lost_n_found;
317                                         }
318
319                                         yaffs_add_obj_to_dir(parent, in);
320
321                                         switch (in->variant_type) {
322                                         case YAFFS_OBJECT_TYPE_UNKNOWN:
323                                                 /* Todo got a problem */
324                                                 break;
325                                         case YAFFS_OBJECT_TYPE_FILE:
326                                                 if (dev->param.
327                                                     use_header_file_size)
328                                                         in->variant.
329                                                         file_variant.file_size
330                                                         = yaffs_oh_to_size(dev, oh, 0);
331                                                 break;
332                                         case YAFFS_OBJECT_TYPE_HARDLINK:
333                                                 in->variant.
334                                                     hardlink_variant.equiv_id =
335                                                     oh->equiv_id;
336                                                 list_add(&in->hard_links,
337                                                                 &hard_list);
338                                                 break;
339                                         case YAFFS_OBJECT_TYPE_DIRECTORY:
340                                                 /* Do nothing */
341                                                 break;
342                                         case YAFFS_OBJECT_TYPE_SPECIAL:
343                                                 /* Do nothing */
344                                                 break;
345                                         case YAFFS_OBJECT_TYPE_SYMLINK:
346                                                 in->variant.symlink_variant.
347                                                     alias =
348                                                     yaffs_clone_str(oh->alias);
349                                                 if (!in->variant.
350                                                     symlink_variant.alias)
351                                                         alloc_failed = 1;
352                                                 break;
353                                         }
354                                 }
355                         }
356                 }
357
358                 if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
359                         /* If we got this far while scanning,
360                          * then the block is fully allocated. */
361                         state = YAFFS_BLOCK_STATE_FULL;
362                 }
363
364                 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
365                         /* If the block was partially allocated then
366                          * treat it as fully allocated. */
367                         state = YAFFS_BLOCK_STATE_FULL;
368                         dev->alloc_block = -1;
369                 }
370
371                 bi->block_state = state;
372
373                 /* Now let's see if it was dirty */
374                 if (bi->pages_in_use == 0 &&
375                     !bi->has_shrink_hdr &&
376                     bi->block_state == YAFFS_BLOCK_STATE_FULL)
377                         yaffs_block_became_dirty(dev, blk);
378         }
379
380         /* Ok, we've done all the scanning.
381          * Fix up the hard link chains.
382          * We should now have scanned all the objects, now it's time to add
383          * these hardlinks.
384          */
385
386         yaffs_link_fixup(dev, &hard_list);
387
388         /*
389          * Fix up any shadowed objects.
390          * There should not be more than one of these.
391          */
392         {
393                 struct yaffs_shadow_fixer *fixer;
394                 struct yaffs_obj *obj;
395
396                 while (shadow_fixers) {
397                         fixer = shadow_fixers;
398                         shadow_fixers = fixer->next;
399                         /* Complete the rename transaction by deleting the
400                          * shadowed object then setting the object header
401                          to unshadowed.
402                          */
403                         obj = yaffs_find_by_number(dev, fixer->shadowed_id);
404                         if (obj)
405                                 yaffs_del_obj(obj);
406
407                         obj = yaffs_find_by_number(dev, fixer->obj_id);
408
409                         if (obj)
410                                 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
411
412                         kfree(fixer);
413                 }
414         }
415
416         yaffs_release_temp_buffer(dev, chunk_data);
417
418         if (alloc_failed)
419                 return YAFFS_FAIL;
420
421         yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
422
423         return YAFFS_OK;
424 }