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