Fix unmatched temporary buffer allocations
[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_tagsmarshall.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                 if (local_data)
105                         yaffs_release_temp_buffer(dev, data);
106
107                 return YAFFS_FAIL;
108         }
109
110         if (dev->param.inband_tags) {
111                 if (tags) {
112                         struct yaffs_packed_tags2_tags_only *pt2tp;
113                         pt2tp =
114                                 (struct yaffs_packed_tags2_tags_only *)
115                                 &data[dev->data_bytes_per_chunk];
116                         yaffs_unpack_tags2_tags_only(dev, tags, pt2tp);
117                 }
118         } else if (tags) {
119                 memcpy(packed_tags_ptr, spare_buffer, packed_tags_size);
120                 yaffs_unpack_tags2(dev, tags, &pt, !dev->param.no_tags_ecc);
121         }
122
123         if (local_data)
124                 yaffs_release_temp_buffer(dev, data);
125
126         if (tags && ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
127                 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
128                 dev->n_ecc_unfixed++;
129         }
130
131         if (tags && ecc_result == YAFFS_ECC_RESULT_FIXED) {
132                 if (tags->ecc_result <= YAFFS_ECC_RESULT_NO_ERROR)
133                         tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
134                 dev->n_ecc_fixed++;
135         }
136
137         if (ecc_result < YAFFS_ECC_RESULT_UNFIXED)
138                 return YAFFS_OK;
139         else
140                 return YAFFS_FAIL;
141 }
142
143 static int yaffs_tags_marshall_query_block(struct yaffs_dev *dev, int block_no,
144                                enum yaffs_block_state *state,
145                                u32 *seq_number)
146 {
147         int retval;
148
149         yaffs_trace(YAFFS_TRACE_MTD, "yaffs_tags_marshall_query_block %d",
150                         block_no);
151
152         retval = dev->drv.drv_check_bad_fn(dev, block_no);
153
154         if (retval== YAFFS_FAIL) {
155                 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
156
157                 *state = YAFFS_BLOCK_STATE_DEAD;
158                 *seq_number = 0;
159         } else {
160                 struct yaffs_ext_tags t;
161
162                 yaffs_tags_marshall_read(dev,
163                                     block_no * dev->param.chunks_per_block,
164                                     NULL, &t);
165
166                 if (t.chunk_used) {
167                         *seq_number = t.seq_number;
168                         *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
169                 } else {
170                         *seq_number = 0;
171                         *state = YAFFS_BLOCK_STATE_EMPTY;
172                 }
173         }
174
175         yaffs_trace(YAFFS_TRACE_MTD,
176                 "block query returns  seq %d state %d",
177                 *seq_number, *state);
178
179         return retval;
180 }
181
182 static int yaffs_tags_marshall_mark_bad(struct yaffs_dev *dev, int block_no)
183 {
184         return dev->drv.drv_mark_bad_fn(dev, block_no);
185
186 }
187
188
189 void yaffs_tags_marshall_install(struct yaffs_dev *dev)
190 {
191         if (!dev->param.is_yaffs2)
192                 return;
193
194         if (!dev->tagger.write_chunk_tags_fn)
195                 dev->tagger.write_chunk_tags_fn = yaffs_tags_marshall_write;
196
197         if (!dev->tagger.read_chunk_tags_fn)
198                 dev->tagger.read_chunk_tags_fn = yaffs_tags_marshall_read;
199
200         if (!dev->tagger.query_block_fn)
201                 dev->tagger.query_block_fn = yaffs_tags_marshall_query_block;
202
203         if (!dev->tagger.mark_bad_fn)
204                 dev->tagger.mark_bad_fn = yaffs_tags_marshall_mark_bad;
205
206 }