Apply 2.6.14 patches from Andre
[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 = 0;
118                 }
119 #else
120                 {
121                         yaffs_ECCOther ecc;
122                         yaffs_ECCCalculateOther((unsigned char *)&pt->t,
123                                                 sizeof
124                                                 (yaffs_PackedTags2TagsPart),
125                                                 &ecc);
126                         t->eccResult =
127                             yaffs_ECCCorrectOther((unsigned char *)&pt->t,
128                                                   sizeof
129                                                   (yaffs_PackedTags2TagsPart),
130                                                   &pt->ecc, &ecc);
131                 }
132 #endif
133                 t->blockBad = 0;
134                 t->chunkUsed = 1;
135                 t->objectId = pt->t.objectId;
136                 t->chunkId = pt->t.chunkId;
137                 t->byteCount = pt->t.byteCount;
138                 t->chunkDeleted = 0;
139                 t->serialNumber = 0;
140                 t->sequenceNumber = pt->t.sequenceNumber;
141
142                 /* Do extra header info stuff */
143
144                 if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
145                         t->chunkId = 0;
146                         t->byteCount = 0;
147
148                         t->extraHeaderInfoAvailable = 1;
149                         t->extraParentObjectId =
150                             pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
151                         t->extraIsShrinkHeader =
152                             (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
153                         t->extraShadows =
154                             (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
155                         t->extraObjectType =
156                             pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
157                         t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
158
159                         if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
160                                 t->extraEquivalentObjectId = pt->t.byteCount;
161                         } else {
162                                 t->extraFileLength = pt->t.byteCount;
163                         }
164                 }
165         }
166
167         yaffs_DumpPackedTags2(pt);
168         yaffs_DumpTags2(t);
169
170 }