yaffs direct: Add proper error handler for too many files open
[yaffs2.git] / yaffs_tagscompat.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_guts.h"
15 #include "yaffs_tagscompat.h"
16 #include "yaffs_ecc.h"
17 #include "yaffs_getblockinfo.h"
18 #include "yaffs_trace.h"
19
20 static void yaffs_handle_rd_data_error(yaffs_dev_t *dev, int nand_chunk);
21 #ifdef NOTYET
22 static void yaffs_check_written_block(yaffs_dev_t *dev, int nand_chunk);
23 static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
24                                      const __u8 *data,
25                                      const yaffs_spare *spare);
26 static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
27                                     const yaffs_spare *spare);
28 static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk);
29 #endif
30
31 static const char yaffs_count_bits_table[256] = {
32         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
33         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
34         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
35         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
36         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
37         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
38         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
39         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
40         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
41         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
42         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
43         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
44         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
45         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
46         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
47         4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
48 };
49
50 int yaffs_count_bits(__u8 x)
51 {
52         int ret_val;
53         ret_val = yaffs_count_bits_table[x];
54         return ret_val;
55 }
56
57 /********** Tags ECC calculations  *********/
58
59 void yaffs_calc_ecc(const __u8 *data, yaffs_spare *spare)
60 {
61         yaffs_ecc_cacl(data, spare->ecc1);
62         yaffs_ecc_cacl(&data[256], spare->ecc2);
63 }
64
65 void yaffs_calc_tags_ecc(yaffs_tags_t *tags)
66 {
67         /* Calculate an ecc */
68
69         unsigned char *b = ((yaffs_tags_union_t *) tags)->as_bytes;
70         unsigned i, j;
71         unsigned ecc = 0;
72         unsigned bit = 0;
73
74         tags->ecc = 0;
75
76         for (i = 0; i < 8; i++) {
77                 for (j = 1; j & 0xff; j <<= 1) {
78                         bit++;
79                         if (b[i] & j)
80                                 ecc ^= bit;
81                 }
82         }
83
84         tags->ecc = ecc;
85
86 }
87
88 int yaffs_check_tags_ecc(yaffs_tags_t *tags)
89 {
90         unsigned ecc = tags->ecc;
91
92         yaffs_calc_tags_ecc(tags);
93
94         ecc ^= tags->ecc;
95
96         if (ecc && ecc <= 64) {
97                 /* TODO: Handle the failure better. Retire? */
98                 unsigned char *b = ((yaffs_tags_union_t *) tags)->as_bytes;
99
100                 ecc--;
101
102                 b[ecc / 8] ^= (1 << (ecc & 7));
103
104                 /* Now recvalc the ecc */
105                 yaffs_calc_tags_ecc(tags);
106
107                 return 1;       /* recovered error */
108         } else if (ecc) {
109                 /* Wierd ecc failure value */
110                 /* TODO Need to do somethiong here */
111                 return -1;      /* unrecovered error */
112         }
113
114         return 0;
115 }
116
117 /********** Tags **********/
118
119 static void yaffs_load_tags_to_spare(yaffs_spare *spare_ptr,
120                                 yaffs_tags_t *tags_ptr)
121 {
122         yaffs_tags_union_t *tu = (yaffs_tags_union_t *) tags_ptr;
123
124         yaffs_calc_tags_ecc(tags_ptr);
125
126         spare_ptr->tb0 = tu->as_bytes[0];
127         spare_ptr->tb1 = tu->as_bytes[1];
128         spare_ptr->tb2 = tu->as_bytes[2];
129         spare_ptr->tb3 = tu->as_bytes[3];
130         spare_ptr->tb4 = tu->as_bytes[4];
131         spare_ptr->tb5 = tu->as_bytes[5];
132         spare_ptr->tb6 = tu->as_bytes[6];
133         spare_ptr->tb7 = tu->as_bytes[7];
134 }
135
136 static void yaffs_get_tags_from_spare(yaffs_dev_t *dev, yaffs_spare *spare_ptr,
137                                 yaffs_tags_t *tags_ptr)
138 {
139         yaffs_tags_union_t *tu = (yaffs_tags_union_t *) tags_ptr;
140         int result;
141
142         tu->as_bytes[0] = spare_ptr->tb0;
143         tu->as_bytes[1] = spare_ptr->tb1;
144         tu->as_bytes[2] = spare_ptr->tb2;
145         tu->as_bytes[3] = spare_ptr->tb3;
146         tu->as_bytes[4] = spare_ptr->tb4;
147         tu->as_bytes[5] = spare_ptr->tb5;
148         tu->as_bytes[6] = spare_ptr->tb6;
149         tu->as_bytes[7] = spare_ptr->tb7;
150
151         result = yaffs_check_tags_ecc(tags_ptr);
152         if (result > 0)
153                 dev->n_tags_ecc_fixed++;
154         else if (result < 0)
155                 dev->n_tags_ecc_unfixed++;
156 }
157
158 static void yaffs_spare_init(yaffs_spare *spare)
159 {
160         memset(spare, 0xFF, sizeof(yaffs_spare));
161 }
162
163 static int yaffs_wr_nand(struct yaffs_dev_s *dev,
164                                 int nand_chunk, const __u8 *data,
165                                 yaffs_spare *spare)
166 {
167         if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
168                 T(YAFFS_TRACE_ERROR,
169                   (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
170                    nand_chunk));
171                 return YAFFS_FAIL;
172         }
173
174         return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
175 }
176
177 static int yaffs_rd_chunk_nand(struct yaffs_dev_s *dev,
178                                    int nand_chunk,
179                                    __u8 *data,
180                                    yaffs_spare *spare,
181                                    yaffs_ecc_result *ecc_result,
182                                    int correct_errors)
183 {
184         int ret_val;
185         yaffs_spare local_spare;
186
187         if (!spare && data) {
188                 /* If we don't have a real spare, then we use a local one. */
189                 /* Need this for the calculation of the ecc */
190                 spare = &local_spare;
191         }
192
193         if (!dev->param.use_nand_ecc) {
194                 ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
195                 if (data && correct_errors) {
196                         /* Do ECC correction */
197                         /* Todo handle any errors */
198                         int ecc_result1, ecc_result2;
199                         __u8 calc_ecc[3];
200
201                         yaffs_ecc_cacl(data, calc_ecc);
202                         ecc_result1 =
203                             yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
204                         yaffs_ecc_cacl(&data[256], calc_ecc);
205                         ecc_result2 =
206                             yaffs_ecc_correct(&data[256], spare->ecc2, calc_ecc);
207
208                         if (ecc_result1 > 0) {
209                                 T(YAFFS_TRACE_ERROR,
210                                   (TSTR
211                                    ("**>>yaffs ecc error fix performed on chunk %d:0"
212                                     TENDSTR), nand_chunk));
213                                 dev->n_ecc_fixed++;
214                         } else if (ecc_result1 < 0) {
215                                 T(YAFFS_TRACE_ERROR,
216                                   (TSTR
217                                    ("**>>yaffs ecc error unfixed on chunk %d:0"
218                                     TENDSTR), nand_chunk));
219                                 dev->n_ecc_unfixed++;
220                         }
221
222                         if (ecc_result2 > 0) {
223                                 T(YAFFS_TRACE_ERROR,
224                                   (TSTR
225                                    ("**>>yaffs ecc error fix performed on chunk %d:1"
226                                     TENDSTR), nand_chunk));
227                                 dev->n_ecc_fixed++;
228                         } else if (ecc_result2 < 0) {
229                                 T(YAFFS_TRACE_ERROR,
230                                   (TSTR
231                                    ("**>>yaffs ecc error unfixed on chunk %d:1"
232                                     TENDSTR), nand_chunk));
233                                 dev->n_ecc_unfixed++;
234                         }
235
236                         if (ecc_result1 || ecc_result2) {
237                                 /* We had a data problem on this page */
238                                 yaffs_handle_rd_data_error(dev, nand_chunk);
239                         }
240
241                         if (ecc_result1 < 0 || ecc_result2 < 0)
242                                 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
243                         else if (ecc_result1 > 0 || ecc_result2 > 0)
244                                 *ecc_result = YAFFS_ECC_RESULT_FIXED;
245                         else
246                                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
247                 }
248         } else {
249                 /* Must allocate enough memory for spare+2*sizeof(int) */
250                 /* for ecc results from device. */
251                 struct yaffs_nand_spare nspare;
252
253                 memset(&nspare, 0, sizeof(nspare));
254
255                 ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
256                                         (yaffs_spare *) &nspare);
257                 memcpy(spare, &nspare, sizeof(yaffs_spare));
258                 if (data && correct_errors) {
259                         if (nspare.eccres1 > 0) {
260                                 T(YAFFS_TRACE_ERROR,
261                                   (TSTR
262                                    ("**>>mtd ecc error fix performed on chunk %d:0"
263                                     TENDSTR), nand_chunk));
264                         } else if (nspare.eccres1 < 0) {
265                                 T(YAFFS_TRACE_ERROR,
266                                   (TSTR
267                                    ("**>>mtd ecc error unfixed on chunk %d:0"
268                                     TENDSTR), nand_chunk));
269                         }
270
271                         if (nspare.eccres2 > 0) {
272                                 T(YAFFS_TRACE_ERROR,
273                                   (TSTR
274                                    ("**>>mtd ecc error fix performed on chunk %d:1"
275                                     TENDSTR), nand_chunk));
276                         } else if (nspare.eccres2 < 0) {
277                                 T(YAFFS_TRACE_ERROR,
278                                   (TSTR
279                                    ("**>>mtd ecc error unfixed on chunk %d:1"
280                                     TENDSTR), nand_chunk));
281                         }
282
283                         if (nspare.eccres1 || nspare.eccres2) {
284                                 /* We had a data problem on this page */
285                                 yaffs_handle_rd_data_error(dev, nand_chunk);
286                         }
287
288                         if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
289                                 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
290                         else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
291                                 *ecc_result = YAFFS_ECC_RESULT_FIXED;
292                         else
293                                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
294
295                 }
296         }
297         return ret_val;
298 }
299
300 #ifdef NOTYET
301 static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
302                                   int nand_chunk)
303 {
304         static int init;
305         static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
306         static __u8 data[YAFFS_BYTES_PER_CHUNK];
307         /* Might as well always allocate the larger size for */
308         /* dev->param.use_nand_ecc == true; */
309         static __u8 spare[sizeof(struct yaffs_nand_spare)];
310
311         dev->param.read_chunk_fn(dev, nand_chunk, data, (yaffs_spare *) spare);
312
313         if (!init) {
314                 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
315                 init = 1;
316         }
317
318         if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
319                 return YAFFS_FAIL;
320         if (memcmp(cmpbuf, spare, 16))
321                 return YAFFS_FAIL;
322
323         return YAFFS_OK;
324
325 }
326 #endif
327
328 /*
329  * Functions for robustisizing
330  */
331
332 static void yaffs_handle_rd_data_error(yaffs_dev_t *dev, int nand_chunk)
333 {
334         int flash_block = nand_chunk / dev->param.chunks_per_block;
335
336         /* Mark the block for retirement */
337         yaffs_get_block_info(dev, flash_block + dev->block_offset)->needs_retiring = 1;
338         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
339           (TSTR("**>>Block %d marked for retirement" TENDSTR), flash_block));
340
341         /* TODO:
342          * Just do a garbage collection on the affected block
343          * then retire the block
344          * NB recursion
345          */
346 }
347
348 #ifdef NOTYET
349 static void yaffs_check_written_block(yaffs_dev_t *dev, int nand_chunk)
350 {
351 }
352
353 static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
354                                      const __u8 *data,
355                                      const yaffs_spare *spare)
356 {
357 }
358
359 static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
360                                     const yaffs_spare *spare)
361 {
362 }
363
364 static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk)
365 {
366         int flash_block = nand_chunk / dev->param.chunks_per_block;
367
368         /* Mark the block for retirement */
369         yaffs_get_block_info(dev, flash_block)->needs_retiring = 1;
370         /* Delete the chunk */
371         yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
372 }
373
374 static int yaffs_verify_cmp(const __u8 *d0, const __u8 *d1,
375                                const yaffs_spare *s0, const yaffs_spare *s1)
376 {
377
378         if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
379             s0->tb0 != s1->tb0 ||
380             s0->tb1 != s1->tb1 ||
381             s0->tb2 != s1->tb2 ||
382             s0->tb3 != s1->tb3 ||
383             s0->tb4 != s1->tb4 ||
384             s0->tb5 != s1->tb5 ||
385             s0->tb6 != s1->tb6 ||
386             s0->tb7 != s1->tb7 ||
387             s0->ecc1[0] != s1->ecc1[0] ||
388             s0->ecc1[1] != s1->ecc1[1] ||
389             s0->ecc1[2] != s1->ecc1[2] ||
390             s0->ecc2[0] != s1->ecc2[0] ||
391             s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
392                 return 0;
393         }
394
395         return 1;
396 }
397 #endif                          /* NOTYET */
398
399 int yaffs_tags_compat_wr(yaffs_dev_t *dev,
400                                                 int nand_chunk,
401                                                 const __u8 *data,
402                                                 const yaffs_ext_tags *ext_tags)
403 {
404         yaffs_spare spare;
405         yaffs_tags_t tags;
406
407         yaffs_spare_init(&spare);
408
409         if (ext_tags->is_deleted)
410                 spare.page_status = 0;
411         else {
412                 tags.obj_id = ext_tags->obj_id;
413                 tags.chunk_id = ext_tags->chunk_id;
414
415                 tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff;
416
417                 if (dev->data_bytes_per_chunk >= 1024)
418                         tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
419                 else
420                         tags.n_bytes_msb = 3;
421
422
423                 tags.serial_number = ext_tags->serial_number;
424
425                 if (!dev->param.use_nand_ecc && data)
426                         yaffs_calc_ecc(data, &spare);
427
428                 yaffs_load_tags_to_spare(&spare, &tags);
429
430         }
431
432         return yaffs_wr_nand(dev, nand_chunk, data, &spare);
433 }
434
435 int yaffs_tags_compat_rd(yaffs_dev_t *dev,
436                                                      int nand_chunk,
437                                                      __u8 *data,
438                                                      yaffs_ext_tags *ext_tags)
439 {
440
441         yaffs_spare spare;
442         yaffs_tags_t tags;
443         yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
444
445         static yaffs_spare spare_ff;
446         static int init;
447
448         if (!init) {
449                 memset(&spare_ff, 0xFF, sizeof(spare_ff));
450                 init = 1;
451         }
452
453         if (yaffs_rd_chunk_nand
454             (dev, nand_chunk, data, &spare, &ecc_result, 1)) {
455                 /* ext_tags may be NULL */
456                 if (ext_tags) {
457
458                         int deleted =
459                             (yaffs_count_bits(spare.page_status) < 7) ? 1 : 0;
460
461                         ext_tags->is_deleted = deleted;
462                         ext_tags->ecc_result = ecc_result;
463                         ext_tags->block_bad = 0;        /* We're reading it */
464                         /* therefore it is not a bad block */
465                         ext_tags->chunk_used =
466                             (memcmp(&spare_ff, &spare, sizeof(spare_ff)) !=
467                              0) ? 1 : 0;
468
469                         if (ext_tags->chunk_used) {
470                                 yaffs_get_tags_from_spare(dev, &spare, &tags);
471
472                                 ext_tags->obj_id = tags.obj_id;
473                                 ext_tags->chunk_id = tags.chunk_id;
474                                 ext_tags->n_bytes = tags.n_bytes_lsb;
475
476                                 if (dev->data_bytes_per_chunk >= 1024)
477                                         ext_tags->n_bytes |= (((unsigned) tags.n_bytes_msb) << 10);
478
479                                 ext_tags->serial_number = tags.serial_number;
480                         }
481                 }
482
483                 return YAFFS_OK;
484         } else {
485                 return YAFFS_FAIL;
486         }
487 }
488
489 int yaffs_tags_compat_mark_bad(struct yaffs_dev_s *dev,
490                                             int flash_block)
491 {
492
493         yaffs_spare spare;
494
495         memset(&spare, 0xff, sizeof(yaffs_spare));
496
497         spare.block_status = 'Y';
498
499         yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
500                                &spare);
501         yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
502                                NULL, &spare);
503
504         return YAFFS_OK;
505
506 }
507
508 int yaffs_tags_compat_query_block(struct yaffs_dev_s *dev,
509                                           int block_no,
510                                           yaffs_block_state_t *state,
511                                           __u32 *seq_number)
512 {
513
514         yaffs_spare spare0, spare1;
515         static yaffs_spare spare_ff;
516         static int init;
517         yaffs_ecc_result dummy;
518
519         if (!init) {
520                 memset(&spare_ff, 0xFF, sizeof(spare_ff));
521                 init = 1;
522         }
523
524         *seq_number = 0;
525
526         yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
527                                 &spare0, &dummy, 1);
528         yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1, NULL,
529                                 &spare1, &dummy, 1);
530
531         if (yaffs_count_bits(spare0.block_status & spare1.block_status) < 7)
532                 *state = YAFFS_BLOCK_STATE_DEAD;
533         else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
534                 *state = YAFFS_BLOCK_STATE_EMPTY;
535         else
536                 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
537
538         return YAFFS_OK;
539 }