blob: aae5c65eaeeedec16264b2b3e37b18914eba6969 [file] [log] [blame]
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors: Artem Bityutskiy (Битюцкий Артём)
21 * Adrian Hunter
22 * Zoltan Sogor
23 */
24
25/*
26 * This file implements UBIFS I/O subsystem which provides various I/O-related
27 * helper functions (reading/writing/checking/validating nodes) and implements
28 * write-buffering support. Write buffers help to save space which otherwise
29 * would have been wasted for padding to the nearest minimal I/O unit boundary.
30 * Instead, data first goes to the write-buffer and is flushed when the
31 * buffer is full or when it is not used for some time (by timer). This is
32 * similar to the mechanism is used by JFFS2.
33 *
34 * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
35 * mutexes defined inside these objects. Since sometimes upper-level code
36 * has to lock the write-buffer (e.g. journal space reservation code), many
37 * functions related to write-buffers have "nolock" suffix which means that the
38 * caller has to lock the write-buffer before calling this function.
39 *
40 * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
41 * aligned, UBIFS starts the next node from the aligned address, and the padded
42 * bytes may contain any rubbish. In other words, UBIFS does not put padding
43 * bytes in those small gaps. Common headers of nodes store real node lengths,
44 * not aligned lengths. Indexing nodes also store real lengths in branches.
45 *
46 * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
47 * uses padding nodes or padding bytes, if the padding node does not fit.
48 *
49 * All UBIFS nodes are protected by CRC checksums and UBIFS checks all nodes
50 * every time they are read from the flash media.
51 */
52
53#include "ubifs.h"
54
55/**
56 * ubifs_ro_mode - switch UBIFS to read read-only mode.
57 * @c: UBIFS file-system description object
58 * @err: error code which is the reason of switching to R/O mode
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 * ubifs_check_node - check node.
72 * @c: UBIFS file-system description object
73 * @buf: node to check
74 * @lnum: logical eraseblock number
75 * @offs: offset within the logical eraseblock
76 * @quiet: print no messages
77 * @must_chk_crc: indicates whether to always check the CRC
78 *
79 * This function checks node magic number and CRC checksum. This function also
80 * validates node length to prevent UBIFS from becoming crazy when an attacker
81 * feeds it a file-system image with incorrect nodes. For example, too large
82 * node length in the common header could cause UBIFS to read memory outside of
83 * allocated buffer when checking the CRC checksum.
84 *
85 * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
86 * true, which is controlled by corresponding UBIFS mount option. However, if
87 * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
88 * checked. Similarly, if @c->always_chk_crc is true, @c->no_chk_data_crc is
89 * ignored and CRC is checked.
90 *
91 * This function returns zero in case of success and %-EUCLEAN in case of bad
92 * CRC or magic.
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 * ubifs_pad - pad flash space.
161 * @c: UBIFS file-system description object
162 * @buf: buffer to put padding to
163 * @pad: how many bytes to pad
164 *
165 * The flash media obliges us to write only in chunks of %c->min_io_size and
166 * when we have to write less data we add padding node to the write-buffer and
167 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
168 * media is being scanned. If the amount of wasted space is not enough to fit a
169 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
170 * pattern (%UBIFS_PADDING_BYTE).
171 *
172 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
173 * used.
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 /* Too little space, padding node won't fit */
198 memset(buf, UBIFS_PADDING_BYTE, pad);
199}
200
201/**
202 * next_sqnum - get next sequence number.
203 * @c: UBIFS file-system description object
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 * ubifs_prepare_node - prepare node to be written to flash.
227 * @c: UBIFS file-system description object
228 * @node: the node to pad
229 * @len: node length
230 * @pad: if the buffer has to be padded
231 *
232 * This function prepares node at @node to be written to the media - it
233 * calculates node CRC, fills the common header, and adds proper padding up to
234 * the next minimum I/O unit if @pad is not zero.
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 * ubifs_read_node - read node.
261 * @c: UBIFS file-system description object
262 * @buf: buffer to read to
263 * @type: node type
264 * @len: node length (not aligned)
265 * @lnum: logical eraseblock number
266 * @offs: offset within the logical eraseblock
267 *
268 * This function reads a node of known type and and length, checks it and
269 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
270 * and a negative error code in case of failure.
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}