Make buffers larger to stop compiler complaining
[yaffs2.git] / yaffs_tagsmarshall.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This file handles the marshalling (ie internal<-->external structure
13  * translation between the internal tags and the stored tags in Yaffs2-style
14  * tags storage.
15  */
16
17 #include "yaffs_guts.h"
18 #include "yaffs_trace.h"
19 #include "yaffs_packedtags2.h"
20
21 static int yaffs_tags_marshall_write(struct yaffs_dev *dev,
22                                     int nand_chunk, const u8 *data,
23                                     const struct yaffs_ext_tags *tags)
24 {
25         struct yaffs_packed_tags2 pt;
26         int retval;
27
28         int packed_tags_size =
29             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
30         void *packed_tags_ptr =
31             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
32
33         yaffs_trace(YAFFS_TRACE_MTD,
34                 "yaffs_tags_marshall_write chunk %d data %p tags %p",
35                 nand_chunk, data, tags);
36
37         /* For yaffs2 writing there must be both data and tags.
38          * If we're using inband tags, then the tags are stuffed into
39          * the end of the data buffer.
40          */
41         if (!data || !tags)
42                 BUG();
43         else if (dev->param.inband_tags) {
44                 struct yaffs_packed_tags2_tags_only *pt2tp;
45                 pt2tp =
46                     (struct yaffs_packed_tags2_tags_only *)(data +
47                                                         dev->
48                                                         data_bytes_per_chunk);
49                 yaffs_pack_tags2_tags_only(dev, pt2tp, tags);
50         } else {
51                 yaffs_pack_tags2(dev, &pt, tags, !dev->param.no_tags_ecc);
52         }
53
54         retval = dev->drv.drv_write_chunk_fn(dev, nand_chunk,
55                         data, dev->param.total_bytes_per_chunk,
56                         (dev->param.inband_tags) ? NULL : packed_tags_ptr,
57                         (dev->param.inband_tags) ? 0 : packed_tags_size);
58
59         return retval;
60 }
61
62 static int yaffs_tags_marshall_read(struct yaffs_dev *dev,
63                                    int nand_chunk, u8 *data,
64                                    struct yaffs_ext_tags *tags)
65 {
66         int retval = 0;
67         int local_data = 0;
68         u8 spare_buffer[100];
69         enum yaffs_ecc_result ecc_result;
70
71         struct yaffs_packed_tags2 pt;
72
73         int packed_tags_size =
74             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
75         void *packed_tags_ptr =
76             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
77
78         yaffs_trace(YAFFS_TRACE_MTD,
79                 "yaffs_tags_marshall_read chunk %d data %p tags %p",
80                 nand_chunk, data, tags);
81
82         if (dev->param.inband_tags) {
83                 if (!data) {
84                         local_data = 1;
85                         data = yaffs_get_temp_buffer(dev);
86                 }
87         }
88
89         if (dev->param.inband_tags || (data && !tags))
90                 retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
91                                         data, dev->param.total_bytes_per_chunk,
92                                         NULL, 0,
93                                         &ecc_result);
94         else if (tags)
95                 retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
96                                         data, dev->param.total_bytes_per_chunk,
97                                         spare_buffer, packed_tags_size,
98                                         &ecc_result);
99         else
100                 BUG();
101
102
103         if (retval == YAFFS_FAIL)
104                 return YAFFS_FAIL;
105
106         if (dev->param.inband_tags) {
107                 if (tags) {
108                         struct yaffs_packed_tags2_tags_only *pt2tp;
109                         pt2tp =
110                                 (struct yaffs_packed_tags2_tags_only *)
111                                 &data[dev->data_bytes_per_chunk];
112                         yaffs_unpack_tags2_tags_only(dev, tags, pt2tp);
113                 }
114         } else if (tags) {
115                 memcpy(packed_tags_ptr, spare_buffer, packed_tags_size);
116                 yaffs_unpack_tags2(dev, tags, &pt, !dev->param.no_tags_ecc);
117         }
118
119         if (local_data)
120                 yaffs_release_temp_buffer(dev, data);
121
122         if (tags && ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
123                 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
124                 dev->n_ecc_unfixed++;
125         }
126
127         if (tags && ecc_result == YAFFS_ECC_RESULT_FIXED) {
128                 if (tags->ecc_result <= YAFFS_ECC_RESULT_NO_ERROR)
129                         tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
130                 dev->n_ecc_fixed++;
131         }
132
133         if (ecc_result < YAFFS_ECC_RESULT_UNFIXED)
134                 return YAFFS_OK;
135         else
136                 return YAFFS_FAIL;
137 }
138
139 static int yaffs_tags_marshall_query_block(struct yaffs_dev *dev, int block_no,
140                                enum yaffs_block_state *state,
141                                u32 *seq_number)
142 {
143         int retval;
144
145         yaffs_trace(YAFFS_TRACE_MTD, "yaffs_tags_marshall_query_block %d",
146                         block_no);
147
148         retval = dev->drv.drv_check_bad_fn(dev, block_no);
149
150         if (retval== YAFFS_FAIL) {
151                 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
152
153                 *state = YAFFS_BLOCK_STATE_DEAD;
154                 *seq_number = 0;
155         } else {
156                 struct yaffs_ext_tags t;
157
158                 yaffs_tags_marshall_read(dev,
159                                     block_no * dev->param.chunks_per_block,
160                                     NULL, &t);
161
162                 if (t.chunk_used) {
163                         *seq_number = t.seq_number;
164                         *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
165                 } else {
166                         *seq_number = 0;
167                         *state = YAFFS_BLOCK_STATE_EMPTY;
168                 }
169         }
170
171         yaffs_trace(YAFFS_TRACE_MTD,
172                 "block query returns  seq %d state %d",
173                 *seq_number, *state);
174
175         if (retval == 0)
176                 return YAFFS_OK;
177         else
178                 return YAFFS_FAIL;
179 }
180
181 static int yaffs_tags_marshall_mark_bad(struct yaffs_dev *dev, int block_no)
182 {
183         return dev->drv.drv_mark_bad_fn(dev, block_no);
184
185 }
186
187
188 void yaffs_tags_marshall_install(struct yaffs_dev *dev)
189 {
190         if (!dev->param.is_yaffs2)
191                 return;
192
193         if (!dev->tagger.write_chunk_tags_fn)
194                 dev->tagger.write_chunk_tags_fn = yaffs_tags_marshall_write;
195
196         if (!dev->tagger.read_chunk_tags_fn)
197                 dev->tagger.read_chunk_tags_fn = yaffs_tags_marshall_read;
198
199         if (!dev->tagger.query_block_fn)
200                 dev->tagger.query_block_fn = yaffs_tags_marshall_query_block;
201
202         if (!dev->tagger.mark_bad_fn)
203                 dev->tagger.mark_bad_fn = yaffs_tags_marshall_mark_bad;
204
205 }