Fix offset problem in checkpt code
[yaffs2.git] / yaffs_packedtags2.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  *
4  * yaffs_packedtags2.c: Tags packing for YAFFS2
5  *
6  * Copyright (C) 2002 Aleph One Ltd.
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * version 2.1 as published by the Free Software Foundation.
14  */
15
16 #include "yaffs_packedtags2.h"
17 #include "yportenv.h"
18 #include "yaffs_tagsvalidity.h"
19
20 /* This code packs a set of extended tags into a binary structure for
21  * NAND storage
22  */
23
24 /* Some of the information is "extra" struff which can be packed in to
25  * speed scanning
26  * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
27  */
28
29 /* Extra flags applied to chunkId */
30
31 #define EXTRA_HEADER_INFO_FLAG  0x80000000
32 #define EXTRA_SHRINK_FLAG       0x40000000
33 #define EXTRA_SHADOWS_FLAG      0x20000000
34 #define EXTRA_SPARE_FLAGS       0x10000000
35
36 #define ALL_EXTRA_FLAGS         0xF0000000
37
38 /* Also, the top 4 bits of the object Id are set to the object type. */
39 #define EXTRA_OBJECT_TYPE_SHIFT (28)
40 #define EXTRA_OBJECT_TYPE_MASK  ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
41
42 static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
43 {
44         T(YAFFS_TRACE_MTD,
45           (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
46            pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
47            pt->t.sequenceNumber));
48 }
49
50 static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
51 {
52         T(YAFFS_TRACE_MTD,
53           (TSTR
54            ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
55             "%d del %d ser %d seq %d"
56             TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
57            t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
58            t->sequenceNumber));
59
60 }
61
62 void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
63 {
64         pt->t.chunkId = t->chunkId;
65         pt->t.sequenceNumber = t->sequenceNumber;
66         pt->t.byteCount = t->byteCount;
67         pt->t.objectId = t->objectId;
68
69         if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
70                 /* Store the extra header info instead */
71                 /* We save the parent object in the chunkId */
72                 pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
73                         | t->extraParentObjectId;
74                 if (t->extraIsShrinkHeader) {
75                         pt->t.chunkId |= EXTRA_SHRINK_FLAG;
76                 }
77                 if (t->extraShadows) {
78                         pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
79                 }
80
81                 pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
82                 pt->t.objectId |=
83                     (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
84
85                 if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
86                         pt->t.byteCount = t->extraEquivalentObjectId;
87                 } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
88                         pt->t.byteCount = t->extraFileLength;
89                 } else {
90                         pt->t.byteCount = 0;
91                 }
92         }
93
94         yaffs_DumpPackedTags2(pt);
95         yaffs_DumpTags2(t);
96
97 #ifndef YAFFS_IGNORE_TAGS_ECC
98         {
99                 yaffs_ECCCalculateOther((unsigned char *)&pt->t,
100                                         sizeof(yaffs_PackedTags2TagsPart),
101                                         &pt->ecc);
102         }
103 #endif
104 }
105
106 void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
107 {
108
109         memset(t, 0, sizeof(yaffs_ExtendedTags));
110
111         yaffs_InitialiseTags(t);
112
113         if (pt->t.sequenceNumber != 0xFFFFFFFF) {
114                 /* Page is in use */
115 #ifdef YAFFS_IGNORE_TAGS_ECC
116                 {
117                         t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
118                 }
119 #else
120                 {
121                         yaffs_ECCOther ecc;
122                         int result;
123                         yaffs_ECCCalculateOther((unsigned char *)&pt->t,
124                                                 sizeof
125                                                 (yaffs_PackedTags2TagsPart),
126                                                 &ecc);
127                         result =
128                             yaffs_ECCCorrectOther((unsigned char *)&pt->t,
129                                                   sizeof
130                                                   (yaffs_PackedTags2TagsPart),
131                                                   &pt->ecc, &ecc);
132                         switch(result){
133                                 case 0: 
134                                         t->eccResult = YAFFS_ECC_RESULT_NO_ERROR; 
135                                         break;
136                                 case 1: 
137                                         t->eccResult = YAFFS_ECC_RESULT_FIXED;
138                                         break;
139                                 case -1:
140                                         t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
141                                         break;
142                                 default:
143                                         t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
144                         }
145                 }
146 #endif
147                 t->blockBad = 0;
148                 t->chunkUsed = 1;
149                 t->objectId = pt->t.objectId;
150                 t->chunkId = pt->t.chunkId;
151                 t->byteCount = pt->t.byteCount;
152                 t->chunkDeleted = 0;
153                 t->serialNumber = 0;
154                 t->sequenceNumber = pt->t.sequenceNumber;
155
156                 /* Do extra header info stuff */
157
158                 if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
159                         t->chunkId = 0;
160                         t->byteCount = 0;
161
162                         t->extraHeaderInfoAvailable = 1;
163                         t->extraParentObjectId =
164                             pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
165                         t->extraIsShrinkHeader =
166                             (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
167                         t->extraShadows =
168                             (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
169                         t->extraObjectType =
170                             pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
171                         t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
172
173                         if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
174                                 t->extraEquivalentObjectId = pt->t.byteCount;
175                         } else {
176                                 t->extraFileLength = pt->t.byteCount;
177                         }
178                 }
179         }
180
181         yaffs_DumpPackedTags2(pt);
182         yaffs_DumpTags2(t);
183
184 }