2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
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.
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
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
20 * Authors: Artem Bityutskiy (Битюцкий Артём)
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.
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.
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.
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.
49 * All UBIFS nodes are protected by CRC checksums and UBIFS checks all nodes
50 * every time they are read from the flash media.
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
60 void ubifs_ro_mode(struct ubifs_info *c, int err)
64 c->no_chk_data_crc = 0;
65 ubifs_warn("switched to read-only mode, error %d", err);
71 * ubifs_check_node - check node.
72 * @c: UBIFS file-system description object
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
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.
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.
91 * This function returns zero in case of success and %-EUCLEAN in case of bad
94 int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
95 int offs, int quiet, int must_chk_crc)
97 int err = -EINVAL, type, node_len;
98 uint32_t crc, node_crc, magic;
99 const struct ubifs_ch *ch = buf;
101 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
102 ubifs_assert(!(offs & 7) && offs < c->leb_size);
104 magic = le32_to_cpu(ch->magic);
105 if (magic != UBIFS_NODE_MAGIC) {
107 ubifs_err("bad magic %#08x, expected %#08x",
108 magic, UBIFS_NODE_MAGIC);
113 type = ch->node_type;
114 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
116 ubifs_err("bad node type %d", type);
120 node_len = le32_to_cpu(ch->len);
121 if (node_len + offs > c->leb_size)
124 if (c->ranges[type].max_len == 0) {
125 if (node_len != c->ranges[type].len)
127 } else if (node_len < c->ranges[type].min_len ||
128 node_len > c->ranges[type].max_len)
131 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc &&
135 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
136 node_crc = le32_to_cpu(ch->crc);
137 if (crc != node_crc) {
139 ubifs_err("bad CRC: calculated %#08x, read %#08x",
149 ubifs_err("bad node length %d", node_len);
152 ubifs_err("bad node at LEB %d:%d", lnum, offs);
153 dbg_dump_node(c, buf);
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
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).
172 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
175 void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
179 ubifs_assert(pad >= 0 && !(pad & 7));
181 if (pad >= UBIFS_PAD_NODE_SZ) {
182 struct ubifs_ch *ch = buf;
183 struct ubifs_pad_node *pad_node = buf;
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;
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);
197 /* Too little space, padding node won't fit */
198 memset(buf, UBIFS_PADDING_BYTE, pad);
202 * next_sqnum - get next sequence number.
203 * @c: UBIFS file-system description object
205 static unsigned long long next_sqnum(struct ubifs_info *c)
207 unsigned long long sqnum;
209 spin_lock(&c->cnt_lock);
210 sqnum = ++c->max_sqnum;
211 spin_unlock(&c->cnt_lock);
213 if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
214 if (sqnum >= SQNUM_WATERMARK) {
215 ubifs_err("sequence number overflow %llu, end of life",
217 ubifs_ro_mode(c, -EINVAL);
219 ubifs_warn("running out of sequence numbers, end of life soon");
226 * ubifs_prepare_node - prepare node to be written to flash.
227 * @c: UBIFS file-system description object
228 * @node: the node to pad
230 * @pad: if the buffer has to be padded
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.
236 void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
239 struct ubifs_ch *ch = node;
240 unsigned long long sqnum = next_sqnum(c);
242 ubifs_assert(len >= UBIFS_CH_SZ);
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);
254 pad = ALIGN(len, c->min_io_size) - len;
255 ubifs_pad(c, node + len, pad);
260 * ubifs_read_node - read node.
261 * @c: UBIFS file-system description object
262 * @buf: buffer to read to
264 * @len: node length (not aligned)
265 * @lnum: logical eraseblock number
266 * @offs: offset within the logical eraseblock
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.
272 int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
276 struct ubifs_ch *ch = buf;
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);
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);
291 if (type != ch->node_type) {
292 ubifs_err("bad node type (%d but expected %d)",
293 ch->node_type, type);
297 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
299 ubifs_err("expected node type %d", type);
303 l = le32_to_cpu(ch->len);
305 ubifs_err("bad node length %d, expected %d", l, len);
312 ubifs_err("bad node at LEB %d:%d", lnum, offs);
313 dbg_dump_node(c, buf);