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