1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#include "ubifs.h"
54
55
56
57
58
59
60void ubifs_ro_mode(struct ubifs_info *c, int err)
61{
62 if (!c->ro_media) {
63 c->ro_media = 1;
64 c->no_chk_data_crc = 0;
65 ubifs_warn("switched to read-only mode, error %d", err);
66 dbg_dump_stack();
67 }
68}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
95 int offs, int quiet, int must_chk_crc)
96{
97 int err = -EINVAL, type, node_len;
98 uint32_t crc, node_crc, magic;
99 const struct ubifs_ch *ch = buf;
100
101 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
102 ubifs_assert(!(offs & 7) && offs < c->leb_size);
103
104 magic = le32_to_cpu(ch->magic);
105 if (magic != UBIFS_NODE_MAGIC) {
106 if (!quiet)
107 ubifs_err("bad magic %#08x, expected %#08x",
108 magic, UBIFS_NODE_MAGIC);
109 err = -EUCLEAN;
110 goto out;
111 }
112
113 type = ch->node_type;
114 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
115 if (!quiet)
116 ubifs_err("bad node type %d", type);
117 goto out;
118 }
119
120 node_len = le32_to_cpu(ch->len);
121 if (node_len + offs > c->leb_size)
122 goto out_len;
123
124 if (c->ranges[type].max_len == 0) {
125 if (node_len != c->ranges[type].len)
126 goto out_len;
127 } else if (node_len < c->ranges[type].min_len ||
128 node_len > c->ranges[type].max_len)
129 goto out_len;
130
131 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc &&
132 c->no_chk_data_crc)
133 return 0;
134
135 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
136 node_crc = le32_to_cpu(ch->crc);
137 if (crc != node_crc) {
138 if (!quiet)
139 ubifs_err("bad CRC: calculated %#08x, read %#08x",
140 crc, node_crc);
141 err = -EUCLEAN;
142 goto out;
143 }
144
145 return 0;
146
147out_len:
148 if (!quiet)
149 ubifs_err("bad node length %d", node_len);
150out:
151 if (!quiet) {
152 ubifs_err("bad node at LEB %d:%d", lnum, offs);
153 dbg_dump_node(c, buf);
154 dbg_dump_stack();
155 }
156 return err;
157}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
176{
177 uint32_t crc;
178
179 ubifs_assert(pad >= 0 && !(pad & 7));
180
181 if (pad >= UBIFS_PAD_NODE_SZ) {
182 struct ubifs_ch *ch = buf;
183 struct ubifs_pad_node *pad_node = buf;
184
185 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
186 ch->node_type = UBIFS_PAD_NODE;
187 ch->group_type = UBIFS_NO_NODE_GROUP;
188 ch->padding[0] = ch->padding[1] = 0;
189 ch->sqnum = 0;
190 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
191 pad -= UBIFS_PAD_NODE_SZ;
192 pad_node->pad_len = cpu_to_le32(pad);
193 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
194 ch->crc = cpu_to_le32(crc);
195 memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
196 } else if (pad > 0)
197
198 memset(buf, UBIFS_PADDING_BYTE, pad);
199}
200
201
202
203
204
205static unsigned long long next_sqnum(struct ubifs_info *c)
206{
207 unsigned long long sqnum;
208
209 spin_lock(&c->cnt_lock);
210 sqnum = ++c->max_sqnum;
211 spin_unlock(&c->cnt_lock);
212
213 if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
214 if (sqnum >= SQNUM_WATERMARK) {
215 ubifs_err("sequence number overflow %llu, end of life",
216 sqnum);
217 ubifs_ro_mode(c, -EINVAL);
218 }
219 ubifs_warn("running out of sequence numbers, end of life soon");
220 }
221
222 return sqnum;
223}
224
225
226
227
228
229
230
231
232
233
234
235
236void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
237{
238 uint32_t crc;
239 struct ubifs_ch *ch = node;
240 unsigned long long sqnum = next_sqnum(c);
241
242 ubifs_assert(len >= UBIFS_CH_SZ);
243
244 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
245 ch->len = cpu_to_le32(len);
246 ch->group_type = UBIFS_NO_NODE_GROUP;
247 ch->sqnum = cpu_to_le64(sqnum);
248 ch->padding[0] = ch->padding[1] = 0;
249 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
250 ch->crc = cpu_to_le32(crc);
251
252 if (pad) {
253 len = ALIGN(len, 8);
254 pad = ALIGN(len, c->min_io_size) - len;
255 ubifs_pad(c, node + len, pad);
256 }
257}
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
273 int lnum, int offs)
274{
275 int err, l;
276 struct ubifs_ch *ch = buf;
277
278 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
279 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
280 ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
281 ubifs_assert(!(offs & 7) && offs < c->leb_size);
282 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
283
284 err = ubi_read(c->ubi, lnum, buf, offs, len);
285 if (err && err != -EBADMSG) {
286 ubifs_err("cannot read node %d from LEB %d:%d, error %d",
287 type, lnum, offs, err);
288 return err;
289 }
290
291 if (type != ch->node_type) {
292 ubifs_err("bad node type (%d but expected %d)",
293 ch->node_type, type);
294 goto out;
295 }
296
297 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
298 if (err) {
299 ubifs_err("expected node type %d", type);
300 return err;
301 }
302
303 l = le32_to_cpu(ch->len);
304 if (l != len) {
305 ubifs_err("bad node length %d, expected %d", l, len);
306 goto out;
307 }
308
309 return 0;
310
311out:
312 ubifs_err("bad node at LEB %d:%d", lnum, offs);
313 dbg_dump_node(c, buf);
314 dbg_dump_stack();
315 return -EINVAL;
316}
317