Change number of handles to 100
[yaffs2.git] / yaffs_packedtags2.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 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_tagsvalidity.h"
17
18 /* This code packs a set of extended tags into a binary structure for
19  * NAND storage
20  */
21
22 /* Some of the information is "extra" struff which can be packed in to
23  * speed scanning
24  * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
25  */
26
27 /* Extra flags applied to chunkId */
28
29 #define EXTRA_HEADER_INFO_FLAG  0x80000000
30 #define EXTRA_SHRINK_FLAG       0x40000000
31 #define EXTRA_SHADOWS_FLAG      0x20000000
32 #define EXTRA_SPARE_FLAGS       0x10000000
33
34 #define ALL_EXTRA_FLAGS         0xF0000000
35
36 /* Also, the top 4 bits of the object Id are set to the object type. */
37 #define EXTRA_OBJECT_TYPE_SHIFT (28)
38 #define EXTRA_OBJECT_TYPE_MASK  ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
39
40
41 static void yaffs_DumpPackedTags2TagsPart(const yaffs_PackedTags2TagsPart *ptt)
42 {
43         T(YAFFS_TRACE_MTD,
44           (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
45            ptt->objectId, ptt->chunkId, ptt->byteCount,
46            ptt->sequenceNumber));
47 }
48 static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 *pt)
49 {
50         yaffs_DumpPackedTags2TagsPart(&pt->t);
51 }
52
53 static void yaffs_DumpTags2(const yaffs_ExtendedTags *t)
54 {
55         T(YAFFS_TRACE_MTD,
56           (TSTR
57            ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
58             TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
59            t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
60            t->sequenceNumber));
61
62 }
63
64 void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *ptt,
65                 const yaffs_ExtendedTags *t)
66 {
67         ptt->chunkId = t->chunkId;
68         ptt->sequenceNumber = t->sequenceNumber;
69         ptt->byteCount = t->byteCount;
70         ptt->objectId = t->objectId;
71
72         if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
73                 /* Store the extra header info instead */
74                 /* We save the parent object in the chunkId */
75                 ptt->chunkId = EXTRA_HEADER_INFO_FLAG
76                         | t->extraParentObjectId;
77                 if (t->extraIsShrinkHeader)
78                         ptt->chunkId |= EXTRA_SHRINK_FLAG;
79                 if (t->extraShadows)
80                         ptt->chunkId |= EXTRA_SHADOWS_FLAG;
81
82                 ptt->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
83                 ptt->objectId |=
84                     (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
85
86                 if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
87                         ptt->byteCount = t->extraEquivalentObjectId;
88                 else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE)
89                         ptt->byteCount = t->extraFileLength;
90                 else
91                         ptt->byteCount = 0;
92         }
93
94         yaffs_DumpPackedTags2TagsPart(ptt);
95         yaffs_DumpTags2(t);
96 }
97
98
99 void yaffs_PackTags2(yaffs_Device *dev, yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t)
100 {
101         yaffs_PackTags2TagsPart(&pt->t, t);
102
103         if(!dev->noTagsECC)
104                 yaffs_ECCCalculateOther((unsigned char *)&pt->t,
105                                         sizeof(yaffs_PackedTags2TagsPart),
106                                         &pt->ecc);
107 }
108
109
110 void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t,
111                 yaffs_PackedTags2TagsPart *ptt)
112 {
113
114         memset(t, 0, sizeof(yaffs_ExtendedTags));
115
116         yaffs_InitialiseTags(t);
117
118         if (ptt->sequenceNumber != 0xFFFFFFFF) {
119                 t->blockBad = 0;
120                 t->chunkUsed = 1;
121                 t->objectId = ptt->objectId;
122                 t->chunkId = ptt->chunkId;
123                 t->byteCount = ptt->byteCount;
124                 t->chunkDeleted = 0;
125                 t->serialNumber = 0;
126                 t->sequenceNumber = ptt->sequenceNumber;
127
128                 /* Do extra header info stuff */
129
130                 if (ptt->chunkId & EXTRA_HEADER_INFO_FLAG) {
131                         t->chunkId = 0;
132                         t->byteCount = 0;
133
134                         t->extraHeaderInfoAvailable = 1;
135                         t->extraParentObjectId =
136                             ptt->chunkId & (~(ALL_EXTRA_FLAGS));
137                         t->extraIsShrinkHeader =
138                             (ptt->chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
139                         t->extraShadows =
140                             (ptt->chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
141                         t->extraObjectType =
142                             ptt->objectId >> EXTRA_OBJECT_TYPE_SHIFT;
143                         t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
144
145                         if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
146                                 t->extraEquivalentObjectId = ptt->byteCount;
147                         else
148                                 t->extraFileLength = ptt->byteCount;
149                 }
150         }
151
152         yaffs_DumpPackedTags2TagsPart(ptt);
153         yaffs_DumpTags2(t);
154
155 }
156
157
158 void yaffs_UnpackTags2(yaffs_Device *dev, yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt)
159 {
160
161         yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_NO_ERROR;
162
163         if (pt->t.sequenceNumber != 0xFFFFFFFF &&
164             !dev->noTagsECC){
165                 /* Chunk is in use and we need to do ECC */
166                 
167                 yaffs_ECCOther ecc;
168                 int result;
169                 yaffs_ECCCalculateOther((unsigned char *)&pt->t,
170                                         sizeof(yaffs_PackedTags2TagsPart),
171                                         &ecc);
172                 result = yaffs_ECCCorrectOther((unsigned char *)&pt->t,
173                                                 sizeof(yaffs_PackedTags2TagsPart),
174                                                 &pt->ecc, &ecc);
175                 switch (result) {
176                         case 0:
177                                 eccResult = YAFFS_ECC_RESULT_NO_ERROR;
178                                 break;
179                         case 1:
180                                 eccResult = YAFFS_ECC_RESULT_FIXED;
181                                 break;
182                         case -1:
183                                 eccResult = YAFFS_ECC_RESULT_UNFIXED;
184                                 break;
185                         default:
186                                 eccResult = YAFFS_ECC_RESULT_UNKNOWN;
187                 }
188         }
189
190         yaffs_UnpackTags2TagsPart(t, &pt->t);
191
192         t->eccResult = eccResult;
193
194         yaffs_DumpPackedTags2(pt);
195         yaffs_DumpTags2(t);
196 }
197