Add one-shot feature for yaffs auto checkpointing.
[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_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 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 /* 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
42 static void yaffs_DumpPackedTags2TagsPart(const yaffs_PackedTags2TagsPart *ptt)
43 {
44         T(YAFFS_TRACE_MTD,
45           (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
46            ptt->objectId, ptt->chunkId, ptt->byteCount,
47            ptt->sequenceNumber));
48 }
49 static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 *pt)
50 {
51         yaffs_DumpPackedTags2TagsPart(&pt->t);
52 }
53
54 static void yaffs_DumpTags2(const yaffs_ExtendedTags *t)
55 {
56         T(YAFFS_TRACE_MTD,
57           (TSTR
58            ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
59             TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
60            t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
61            t->sequenceNumber));
62
63 }
64
65 void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *ptt,
66                 const yaffs_ExtendedTags *t)
67 {
68         ptt->chunkId = t->chunkId;
69         ptt->sequenceNumber = t->sequenceNumber;
70         ptt->byteCount = t->byteCount;
71         ptt->objectId = t->objectId;
72
73         if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
74                 /* Store the extra header info instead */
75                 /* We save the parent object in the chunkId */
76                 ptt->chunkId = EXTRA_HEADER_INFO_FLAG
77                         | t->extraParentObjectId;
78                 if (t->extraIsShrinkHeader)
79                         ptt->chunkId |= EXTRA_SHRINK_FLAG;
80                 if (t->extraShadows)
81                         ptt->chunkId |= EXTRA_SHADOWS_FLAG;
82
83                 ptt->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
84                 ptt->objectId |=
85                     (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
86
87                 if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
88                         ptt->byteCount = t->extraEquivalentObjectId;
89                 else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE)
90                         ptt->byteCount = t->extraFileLength;
91                 else
92                         ptt->byteCount = 0;
93         }
94
95         yaffs_DumpPackedTags2TagsPart(ptt);
96         yaffs_DumpTags2(t);
97 }
98
99
100 void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t, int tagsECC)
101 {
102         yaffs_PackTags2TagsPart(&pt->t, t);
103
104         if(tagsECC)
105                 yaffs_ECCCalculateOther((unsigned char *)&pt->t,
106                                         sizeof(yaffs_PackedTags2TagsPart),
107                                         &pt->ecc);
108 }
109
110
111 void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t,
112                 yaffs_PackedTags2TagsPart *ptt)
113 {
114
115         memset(t, 0, sizeof(yaffs_ExtendedTags));
116
117         yaffs_InitialiseTags(t);
118
119         if (ptt->sequenceNumber != 0xFFFFFFFF) {
120                 t->blockBad = 0;
121                 t->chunkUsed = 1;
122                 t->objectId = ptt->objectId;
123                 t->chunkId = ptt->chunkId;
124                 t->byteCount = ptt->byteCount;
125                 t->chunkDeleted = 0;
126                 t->serialNumber = 0;
127                 t->sequenceNumber = ptt->sequenceNumber;
128
129                 /* Do extra header info stuff */
130
131                 if (ptt->chunkId & EXTRA_HEADER_INFO_FLAG) {
132                         t->chunkId = 0;
133                         t->byteCount = 0;
134
135                         t->extraHeaderInfoAvailable = 1;
136                         t->extraParentObjectId =
137                             ptt->chunkId & (~(ALL_EXTRA_FLAGS));
138                         t->extraIsShrinkHeader =
139                             (ptt->chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
140                         t->extraShadows =
141                             (ptt->chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
142                         t->extraObjectType =
143                             ptt->objectId >> EXTRA_OBJECT_TYPE_SHIFT;
144                         t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
145
146                         if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
147                                 t->extraEquivalentObjectId = ptt->byteCount;
148                         else
149                                 t->extraFileLength = ptt->byteCount;
150                 }
151         }
152
153         yaffs_DumpPackedTags2TagsPart(ptt);
154         yaffs_DumpTags2(t);
155
156 }
157
158
159 void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt, int tagsECC)
160 {
161
162         yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_NO_ERROR;
163
164         if (pt->t.sequenceNumber != 0xFFFFFFFF &&
165             tagsECC){
166                 /* Chunk is in use and we need to do ECC */
167                 
168                 yaffs_ECCOther ecc;
169                 int result;
170                 yaffs_ECCCalculateOther((unsigned char *)&pt->t,
171                                         sizeof(yaffs_PackedTags2TagsPart),
172                                         &ecc);
173                 result = yaffs_ECCCorrectOther((unsigned char *)&pt->t,
174                                                 sizeof(yaffs_PackedTags2TagsPart),
175                                                 &pt->ecc, &ecc);
176                 switch (result) {
177                         case 0:
178                                 eccResult = YAFFS_ECC_RESULT_NO_ERROR;
179                                 break;
180                         case 1:
181                                 eccResult = YAFFS_ECC_RESULT_FIXED;
182                                 break;
183                         case -1:
184                                 eccResult = YAFFS_ECC_RESULT_UNFIXED;
185                                 break;
186                         default:
187                                 eccResult = YAFFS_ECC_RESULT_UNKNOWN;
188                 }
189         }
190
191         yaffs_UnpackTags2TagsPart(t, &pt->t);
192
193         t->eccResult = eccResult;
194
195         yaffs_DumpPackedTags2(pt);
196         yaffs_DumpTags2(t);
197 }
198