Ooops. missed this one: Needed to change MTD_ defines to match changes
[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
21
22 // This code packs a set of extended tags into a binary structure for NAND storage
23
24 // Some of the information is "extra" struff which can be packed in to speed scanning
25 // This is defined by having the EXTRA_HEADER_INFO_FLAG set.
26
27
28 // Extra flags applied to chunkId
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
38
39 // Also, the top 4 bits of the object Id are set to the object type.
40 #define EXTRA_OBJECT_TYPE_SHIFT (28)
41 #define EXTRA_OBJECT_TYPE_MASK  ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
42
43
44
45 static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 *pt)
46 {
47         T(YAFFS_TRACE_MTD,(TSTR("packed tags obj %d chunk %d byte %d seq %d"TENDSTR),pt->t.objectId,pt->t.chunkId,pt->t.byteCount,pt->t.sequenceNumber));
48 }
49
50 static void yaffs_DumpTags2(const yaffs_ExtendedTags *t)
51 {
52         T(YAFFS_TRACE_MTD,(TSTR("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"TENDSTR),
53              t->eccResult, t->blockBad, t->chunkUsed, t->objectId, t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber, t->sequenceNumber));
54           
55 }
56
57 void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t)
58 {
59         pt->t.chunkId = t->chunkId;
60         pt->t.sequenceNumber = t->sequenceNumber;
61         pt->t.byteCount = t->byteCount;
62         pt->t.objectId = t->objectId;
63         
64         if(t->chunkId == 0 && t->extraHeaderInfoAvailable)
65         {
66                 // Store the extra header info instead
67                 pt->t.chunkId = EXTRA_HEADER_INFO_FLAG | t->extraParentObjectId; // We save the parent object in the chunkId
68                 if(t->extraIsShrinkHeader) 
69                 {
70                     pt->t.chunkId |= EXTRA_SHRINK_FLAG;
71                 }
72                 if(t->extraShadows) 
73                 {
74                     pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
75                 }
76                 
77                 pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
78                 pt->t.objectId |= (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
79                  
80                 if(t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
81                 {
82                    pt->t.byteCount = t->extraEquivalentObjectId;
83                 }
84                 else if(t->extraObjectType == YAFFS_OBJECT_TYPE_FILE)
85                 {
86                    pt->t.byteCount = t->extraFileLength;
87                 }
88                 else
89                 {
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,sizeof(yaffs_PackedTags2TagsPart),&pt->ecc);
100         }
101 #endif
102 }
103
104 void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt)
105 {
106
107         
108         memset(t,0,sizeof(yaffs_ExtendedTags));
109         
110         yaffs_InitialiseTags(t);
111         
112         if(pt->t.sequenceNumber != 0xFFFFFFFF)
113         {
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,sizeof(yaffs_PackedTags2TagsPart),&ecc);
123                         t->eccResult = yaffs_ECCCorrectOther((unsigned char *)&pt->t,sizeof(yaffs_PackedTags2TagsPart),&pt->ecc,&ecc);
124                 }
125 #endif
126                 t->blockBad = 0;
127                 t->chunkUsed = 1;
128                 t->objectId = pt->t.objectId;
129                 t->chunkId =  pt->t.chunkId;
130                 t->byteCount = pt->t.byteCount;
131                 t->chunkDeleted = 0;
132                 t->serialNumber = 0;
133                 t->sequenceNumber = pt->t.sequenceNumber;
134                 
135                 // Do extra header info stuff
136                 
137                 if(pt->t.chunkId & EXTRA_HEADER_INFO_FLAG)
138                 {
139                         t->chunkId = 0;
140                         t->byteCount = 0;
141                         
142                         t->extraHeaderInfoAvailable = 1;
143                         t->extraParentObjectId = pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
144                         t->extraIsShrinkHeader =  (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
145                         t->extraShadows =  (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
146                         t->extraObjectType = pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
147                         t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
148                         
149                         if(t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
150                         {
151                             t->extraEquivalentObjectId = pt->t.byteCount;
152                         }
153                         else 
154                         {
155                             t->extraFileLength = pt->t.byteCount;
156                         }
157                 }
158         }
159
160         yaffs_DumpPackedTags2(pt);
161         yaffs_DumpTags2(t);
162
163 }
164