Prevent resizes bigger than 2GBytes
[yaffs2.git] / yaffs_mtdif2.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 /* mtd interface for YAFFS2 */
15
16 const char *yaffs_mtdif2_c_version =
17         "$Id: yaffs_mtdif2.c,v 1.26 2010-01-11 21:43:18 charles Exp $";
18
19 #include "yportenv.h"
20 #include "yaffs_trace.h"
21
22 #include "yaffs_mtdif2.h"
23
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
27
28 #include "yaffs_packedtags2.h"
29
30 /* NB For use with inband tags....
31  * We assume that the data buffer is of size totalBytersPerChunk so that we can also
32  * use it to load the tags.
33  */
34 int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
35                                       const __u8 *data,
36                                       const yaffs_ExtendedTags *tags)
37 {
38         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
39 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
40         struct mtd_oob_ops ops;
41 #else
42         size_t dummy;
43 #endif
44         int retval = 0;
45
46         loff_t addr;
47
48         yaffs_PackedTags2 pt;
49
50         int packed_tags_size = dev->noTagsECC ? sizeof(pt.t) : sizeof(pt);
51         void * packed_tags_ptr = dev->noTagsECC ? (void *) &pt.t : (void *)&pt;
52
53         T(YAFFS_TRACE_MTD,
54           (TSTR
55            ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
56             TENDSTR), chunkInNAND, data, tags));
57
58
59         addr  = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
60
61         /* For yaffs2 writing there must be both data and tags.
62          * If we're using inband tags, then the tags are stuffed into
63          * the end of the data buffer.
64          */
65         if (!data || !tags)
66                 BUG();
67         else if (dev->inbandTags) {
68                 yaffs_PackedTags2TagsPart *pt2tp;
69                 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
70                 yaffs_PackTags2TagsPart(pt2tp, tags);
71         } else
72                 yaffs_PackTags2(&pt, tags, !dev->noTagsECC);
73
74 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
75         ops.mode = MTD_OOB_AUTO;
76         ops.ooblen = (dev->inbandTags) ? 0 : packed_tags_size;
77         ops.len = dev->totalBytesPerChunk;
78         ops.ooboffs = 0;
79         ops.datbuf = (__u8 *)data;
80         ops.oobbuf = (dev->inbandTags) ? NULL : packed_tags_ptr;
81         retval = mtd->write_oob(mtd, addr, &ops);
82
83 #else
84         if (!dev->inbandTags) {
85                 retval =
86                     mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
87                                    &dummy, data, (__u8 *) packed_tags_ptr, NULL);
88         } else {
89                 retval =
90                     mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
91                                data);
92         }
93 #endif
94
95         if (retval == 0)
96                 return YAFFS_OK;
97         else
98                 return YAFFS_FAIL;
99 }
100
101 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
102                                        __u8 *data, yaffs_ExtendedTags *tags)
103 {
104         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
105 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
106         struct mtd_oob_ops ops;
107 #endif
108         size_t dummy;
109         int retval = 0;
110         int localData = 0;
111
112         loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
113
114         yaffs_PackedTags2 pt;
115
116         int packed_tags_size = dev->noTagsECC ? sizeof(pt.t) : sizeof(pt);
117         void * packed_tags_ptr = dev->noTagsECC ? (void *) &pt.t: (void *)&pt;
118
119         T(YAFFS_TRACE_MTD,
120           (TSTR
121            ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
122             TENDSTR), chunkInNAND, data, tags));
123
124         if (dev->inbandTags) {
125
126                 if (!data) {
127                         localData = 1;
128                         data = yaffs_GetTempBuffer(dev, __LINE__);
129                 }
130
131
132         }
133
134
135 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
136         if (dev->inbandTags || (data && !tags))
137                 retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
138                                 &dummy, data);
139         else if (tags) {
140                 ops.mode = MTD_OOB_AUTO;
141                 ops.ooblen = packed_tags_size;
142                 ops.len = data ? dev->nDataBytesPerChunk : packed_tags_size;
143                 ops.ooboffs = 0;
144                 ops.datbuf = data;
145                 ops.oobbuf = dev->spareBuffer;
146                 retval = mtd->read_oob(mtd, addr, &ops);
147         }
148 #else
149         if (!dev->inbandTags && data && tags) {
150
151                 retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
152                                           &dummy, data, dev->spareBuffer,
153                                           NULL);
154         } else {
155                 if (data)
156                         retval =
157                             mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
158                                       data);
159                 if (!dev->inbandTags && tags)
160                         retval =
161                             mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
162                                           dev->spareBuffer);
163         }
164 #endif
165
166
167         if (dev->inbandTags) {
168                 if (tags) {
169                         yaffs_PackedTags2TagsPart *pt2tp;
170                         pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
171                         yaffs_UnpackTags2TagsPart(tags, pt2tp);
172                 }
173         } else {
174                 if (tags) {
175                         memcpy(packed_tags_ptr, dev->spareBuffer, packed_tags_size);
176                         yaffs_UnpackTags2(tags, &pt, !dev->noTagsECC);
177                 }
178         }
179
180         if (localData)
181                 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
182
183         if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
184                 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
185         if (retval == 0)
186                 return YAFFS_OK;
187         else
188                 return YAFFS_FAIL;
189 }
190
191 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
192 {
193         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
194         int retval;
195         T(YAFFS_TRACE_MTD,
196           (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
197
198         retval =
199             mtd->block_markbad(mtd,
200                                blockNo * dev->nChunksPerBlock *
201                                dev->totalBytesPerChunk);
202
203         if (retval == 0)
204                 return YAFFS_OK;
205         else
206                 return YAFFS_FAIL;
207
208 }
209
210 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
211                             yaffs_BlockState *state, __u32 *sequenceNumber)
212 {
213         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
214         int retval;
215
216         T(YAFFS_TRACE_MTD,
217           (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
218         retval =
219             mtd->block_isbad(mtd,
220                              blockNo * dev->nChunksPerBlock *
221                              dev->totalBytesPerChunk);
222
223         if (retval) {
224                 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
225
226                 *state = YAFFS_BLOCK_STATE_DEAD;
227                 *sequenceNumber = 0;
228         } else {
229                 yaffs_ExtendedTags t;
230                 nandmtd2_ReadChunkWithTagsFromNAND(dev,
231                                                    blockNo *
232                                                    dev->nChunksPerBlock, NULL,
233                                                    &t);
234
235                 if (t.chunkUsed) {
236                         *sequenceNumber = t.sequenceNumber;
237                         *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
238                 } else {
239                         *sequenceNumber = 0;
240                         *state = YAFFS_BLOCK_STATE_EMPTY;
241                 }
242         }
243         T(YAFFS_TRACE_MTD,
244           (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
245            *state));
246
247         if (retval == 0)
248                 return YAFFS_OK;
249         else
250                 return YAFFS_FAIL;
251 }
252