d18f3e5cb1aa9e2e63437ade2db67fd30fdf8374
[yaffs2.git] / yaffs_packedtags2.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_packedtags2.h"
15 #include "yportenv.h"
16 #include "yaffs_trace.h"
17 #include "yaffs_tagsvalidity.h"
18
19 /* This code packs a set of extended tags into a binary structure for
20  * NAND storage
21  */
22
23 /* Some of the information is "extra" struff which can be packed in to
24  * speed scanning
25  * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
26  */
27
28 /* Extra flags applied to chunk_id */
29
30 #define EXTRA_HEADER_INFO_FLAG  0x80000000
31 #define EXTRA_SHRINK_FLAG       0x40000000
32 #define EXTRA_SHADOWS_FLAG      0x20000000
33 #define EXTRA_SPARE_FLAGS       0x10000000
34
35 #define ALL_EXTRA_FLAGS         0xf0000000
36
37 /* Also, the top 4 bits of the object Id are set to the object type. */
38 #define EXTRA_OBJECT_TYPE_SHIFT (28)
39 #define EXTRA_OBJECT_TYPE_MASK  ((0x0f) << EXTRA_OBJECT_TYPE_SHIFT)
40
41 static void yaffs_dump_packed_tags2_tags_only(
42                                 const struct yaffs_packed_tags2_tags_only *ptt)
43 {
44         yaffs_trace(YAFFS_TRACE_MTD,
45                 "packed tags obj %d chunk %d byte %d seq %d",
46                 ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number);
47 }
48
49 static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
50 {
51         yaffs_dump_packed_tags2_tags_only(&pt->t);
52 }
53
54 static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
55 {
56         yaffs_trace(YAFFS_TRACE_MTD,
57                 "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d",
58                 t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
59                 t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
60                 t->seq_number);
61
62 }
63
64 void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt,
65                                 const struct yaffs_ext_tags *t)
66 {
67         ptt->chunk_id = t->chunk_id;
68         ptt->seq_number = t->seq_number;
69         ptt->n_bytes = t->n_bytes;
70         ptt->obj_id = t->obj_id;
71
72         if (t->chunk_id == 0 && t->extra_available) {
73                 /* Store the extra header info instead */
74                 /* We save the parent object in the chunk_id */
75                 ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
76                 if (t->extra_is_shrink)
77                         ptt->chunk_id |= EXTRA_SHRINK_FLAG;
78                 if (t->extra_shadows)
79                         ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
80
81                 ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
82                 ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
83
84                 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
85                         ptt->n_bytes = t->extra_equiv_id;
86                 else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
87                         ptt->n_bytes = t->extra_length;
88                 else
89                         ptt->n_bytes = 0;
90         }
91
92         yaffs_dump_packed_tags2_tags_only(ptt);
93         yaffs_dump_tags2(t);
94 }
95
96 void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
97                       const struct yaffs_ext_tags *t, int tags_ecc)
98 {
99         yaffs_pack_tags2_tags_only(&pt->t, t);
100
101         if (tags_ecc)
102                 yaffs_ecc_calc_other((unsigned char *)&pt->t,
103                                     sizeof(struct yaffs_packed_tags2_tags_only),
104                                     &pt->ecc);
105 }
106
107 void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
108                                   struct yaffs_packed_tags2_tags_only *ptt)
109 {
110         memset(t, 0, sizeof(struct yaffs_ext_tags));
111         yaffs_init_tags(t);
112
113         if (ptt->seq_number == 0xffffffff)
114                 return;
115
116         t->block_bad = 0;
117         t->chunk_used = 1;
118         t->obj_id = ptt->obj_id;
119         t->chunk_id = ptt->chunk_id;
120         t->n_bytes = ptt->n_bytes;
121         t->is_deleted = 0;
122         t->serial_number = 0;
123         t->seq_number = ptt->seq_number;
124
125         /* Do extra header info stuff */
126         if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
127                 t->chunk_id = 0;
128                 t->n_bytes = 0;
129
130                 t->extra_available = 1;
131                 t->extra_parent_id = ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
132                 t->extra_is_shrink = ptt->chunk_id & EXTRA_SHRINK_FLAG ? 1 : 0;
133                 t->extra_shadows = ptt->chunk_id & EXTRA_SHADOWS_FLAG ? 1 : 0;
134                 t->extra_obj_type = ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
135                 t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
136
137                 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
138                         t->extra_equiv_id = ptt->n_bytes;
139                 else
140                         t->extra_length = ptt->n_bytes;
141         }
142         yaffs_dump_packed_tags2_tags_only(ptt);
143         yaffs_dump_tags2(t);
144 }
145
146 void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
147                         int tags_ecc)
148 {
149         enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
150
151         if (pt->t.seq_number != 0xffffffff && tags_ecc) {
152                 /* Chunk is in use and we need to do ECC */
153
154                 struct yaffs_ecc_other ecc;
155                 int result;
156                 yaffs_ecc_calc_other((unsigned char *)&pt->t,
157                                 sizeof(struct yaffs_packed_tags2_tags_only),
158                                 &ecc);
159                 result =
160                     yaffs_ecc_correct_other((unsigned char *)&pt->t,
161                                 sizeof(struct yaffs_packed_tags2_tags_only),
162                                 &pt->ecc, &ecc);
163                 switch (result) {
164                 case 0:
165                         ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
166                         break;
167                 case 1:
168                         ecc_result = YAFFS_ECC_RESULT_FIXED;
169                         break;
170                 case -1:
171                         ecc_result = YAFFS_ECC_RESULT_UNFIXED;
172                         break;
173                 default:
174                         ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
175                 }
176         }
177         yaffs_unpack_tags2_tags_only(t, &pt->t);
178
179         t->ecc_result = ecc_result;
180
181         yaffs_dump_packed_tags2(pt);
182         yaffs_dump_tags2(t);
183 }