blob: 7c2a014b59f91efc12122415b2c0151712ce5139 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +020032 * similar to the mechanism is used by JFFS2.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030033 *
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 <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Artem Bityutskiy1e517642008-07-14 19:08:37 +030055#include "ubifs.h"
56
57/**
Adrian Hunterff46d7b2008-07-21 15:39:05 +030058 * ubifs_ro_mode - switch UBIFS to read read-only mode.
59 * @c: UBIFS file-system description object
60 * @err: error code which is the reason of switching to R/O mode
61 */
62void ubifs_ro_mode(struct ubifs_info *c, int err)
63{
Artem Bityutskiy2680d722010-09-17 16:44:28 +030064 if (!c->ro_error) {
65 c->ro_error = 1;
Artem Bityutskiyccb3eba2008-09-08 16:07:01 +030066 c->no_chk_data_crc = 0;
ZhangJieJing2fde99c2010-04-16 11:36:50 +080067 c->vfs_sb->s_flags |= MS_RDONLY;
Adrian Hunterff46d7b2008-07-21 15:39:05 +030068 ubifs_warn("switched to read-only mode, error %d", err);
69 dbg_dump_stack();
70 }
71}
72
73/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +030074 * ubifs_check_node - check node.
75 * @c: UBIFS file-system description object
76 * @buf: node to check
77 * @lnum: logical eraseblock number
78 * @offs: offset within the logical eraseblock
79 * @quiet: print no messages
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +020080 * @must_chk_crc: indicates whether to always check the CRC
Artem Bityutskiy1e517642008-07-14 19:08:37 +030081 *
82 * This function checks node magic number and CRC checksum. This function also
83 * validates node length to prevent UBIFS from becoming crazy when an attacker
84 * feeds it a file-system image with incorrect nodes. For example, too large
85 * node length in the common header could cause UBIFS to read memory outside of
86 * allocated buffer when checking the CRC checksum.
87 *
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +020088 * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
89 * true, which is controlled by corresponding UBIFS mount option. However, if
90 * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
Artem Bityutskiy18d1d7f2011-01-17 22:27:56 +020091 * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
92 * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
93 * is checked. This is because during mounting or re-mounting from R/O mode to
94 * R/W mode we may read journal nodes (when replying the journal or doing the
95 * recovery) and the journal nodes may potentially be corrupted, so checking is
96 * required.
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +020097 *
98 * This function returns zero in case of success and %-EUCLEAN in case of bad
99 * CRC or magic.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300100 */
101int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +0200102 int offs, int quiet, int must_chk_crc)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300103{
104 int err = -EINVAL, type, node_len;
105 uint32_t crc, node_crc, magic;
106 const struct ubifs_ch *ch = buf;
107
108 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
109 ubifs_assert(!(offs & 7) && offs < c->leb_size);
110
111 magic = le32_to_cpu(ch->magic);
112 if (magic != UBIFS_NODE_MAGIC) {
113 if (!quiet)
114 ubifs_err("bad magic %#08x, expected %#08x",
115 magic, UBIFS_NODE_MAGIC);
116 err = -EUCLEAN;
117 goto out;
118 }
119
120 type = ch->node_type;
121 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
122 if (!quiet)
123 ubifs_err("bad node type %d", type);
124 goto out;
125 }
126
127 node_len = le32_to_cpu(ch->len);
128 if (node_len + offs > c->leb_size)
129 goto out_len;
130
131 if (c->ranges[type].max_len == 0) {
132 if (node_len != c->ranges[type].len)
133 goto out_len;
134 } else if (node_len < c->ranges[type].min_len ||
135 node_len > c->ranges[type].max_len)
136 goto out_len;
137
Artem Bityutskiy18d1d7f2011-01-17 22:27:56 +0200138 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
139 !c->remounting_rw && c->no_chk_data_crc)
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +0200140 return 0;
Adrian Hunter2953e732008-09-04 16:26:00 +0300141
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300142 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
143 node_crc = le32_to_cpu(ch->crc);
144 if (crc != node_crc) {
145 if (!quiet)
146 ubifs_err("bad CRC: calculated %#08x, read %#08x",
147 crc, node_crc);
148 err = -EUCLEAN;
149 goto out;
150 }
151
152 return 0;
153
154out_len:
155 if (!quiet)
156 ubifs_err("bad node length %d", node_len);
157out:
158 if (!quiet) {
159 ubifs_err("bad node at LEB %d:%d", lnum, offs);
160 dbg_dump_node(c, buf);
161 dbg_dump_stack();
162 }
163 return err;
164}
165
166/**
167 * ubifs_pad - pad flash space.
168 * @c: UBIFS file-system description object
169 * @buf: buffer to put padding to
170 * @pad: how many bytes to pad
171 *
172 * The flash media obliges us to write only in chunks of %c->min_io_size and
173 * when we have to write less data we add padding node to the write-buffer and
174 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
175 * media is being scanned. If the amount of wasted space is not enough to fit a
176 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
177 * pattern (%UBIFS_PADDING_BYTE).
178 *
179 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
180 * used.
181 */
182void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
183{
184 uint32_t crc;
185
186 ubifs_assert(pad >= 0 && !(pad & 7));
187
188 if (pad >= UBIFS_PAD_NODE_SZ) {
189 struct ubifs_ch *ch = buf;
190 struct ubifs_pad_node *pad_node = buf;
191
192 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
193 ch->node_type = UBIFS_PAD_NODE;
194 ch->group_type = UBIFS_NO_NODE_GROUP;
195 ch->padding[0] = ch->padding[1] = 0;
196 ch->sqnum = 0;
197 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
198 pad -= UBIFS_PAD_NODE_SZ;
199 pad_node->pad_len = cpu_to_le32(pad);
200 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
201 ch->crc = cpu_to_le32(crc);
202 memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
203 } else if (pad > 0)
204 /* Too little space, padding node won't fit */
205 memset(buf, UBIFS_PADDING_BYTE, pad);
206}
207
208/**
209 * next_sqnum - get next sequence number.
210 * @c: UBIFS file-system description object
211 */
212static unsigned long long next_sqnum(struct ubifs_info *c)
213{
214 unsigned long long sqnum;
215
216 spin_lock(&c->cnt_lock);
217 sqnum = ++c->max_sqnum;
218 spin_unlock(&c->cnt_lock);
219
220 if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
221 if (sqnum >= SQNUM_WATERMARK) {
222 ubifs_err("sequence number overflow %llu, end of life",
223 sqnum);
224 ubifs_ro_mode(c, -EINVAL);
225 }
226 ubifs_warn("running out of sequence numbers, end of life soon");
227 }
228
229 return sqnum;
230}
231
232/**
233 * ubifs_prepare_node - prepare node to be written to flash.
234 * @c: UBIFS file-system description object
235 * @node: the node to pad
236 * @len: node length
237 * @pad: if the buffer has to be padded
238 *
239 * This function prepares node at @node to be written to the media - it
240 * calculates node CRC, fills the common header, and adds proper padding up to
241 * the next minimum I/O unit if @pad is not zero.
242 */
243void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
244{
245 uint32_t crc;
246 struct ubifs_ch *ch = node;
247 unsigned long long sqnum = next_sqnum(c);
248
249 ubifs_assert(len >= UBIFS_CH_SZ);
250
251 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
252 ch->len = cpu_to_le32(len);
253 ch->group_type = UBIFS_NO_NODE_GROUP;
254 ch->sqnum = cpu_to_le64(sqnum);
255 ch->padding[0] = ch->padding[1] = 0;
256 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
257 ch->crc = cpu_to_le32(crc);
258
259 if (pad) {
260 len = ALIGN(len, 8);
261 pad = ALIGN(len, c->min_io_size) - len;
262 ubifs_pad(c, node + len, pad);
263 }
264}
265
266/**
267 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
268 * @c: UBIFS file-system description object
269 * @node: the node to pad
270 * @len: node length
271 * @last: indicates the last node of the group
272 *
273 * This function prepares node at @node to be written to the media - it
274 * calculates node CRC and fills the common header.
275 */
276void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
277{
278 uint32_t crc;
279 struct ubifs_ch *ch = node;
280 unsigned long long sqnum = next_sqnum(c);
281
282 ubifs_assert(len >= UBIFS_CH_SZ);
283
284 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
285 ch->len = cpu_to_le32(len);
286 if (last)
287 ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
288 else
289 ch->group_type = UBIFS_IN_NODE_GROUP;
290 ch->sqnum = cpu_to_le64(sqnum);
291 ch->padding[0] = ch->padding[1] = 0;
292 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
293 ch->crc = cpu_to_le32(crc);
294}
295
296/**
297 * wbuf_timer_callback - write-buffer timer callback function.
298 * @data: timer data (write-buffer descriptor)
299 *
300 * This function is called when the write-buffer timer expires.
301 */
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300302static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300303{
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300304 struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300305
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300306 dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307 wbuf->need_sync = 1;
308 wbuf->c->need_wbuf_sync = 1;
309 ubifs_wake_up_bgt(wbuf->c);
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300310 return HRTIMER_NORESTART;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300311}
312
313/**
314 * new_wbuf_timer - start new write-buffer timer.
315 * @wbuf: write-buffer descriptor
316 */
317static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
318{
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300319 ubifs_assert(!hrtimer_active(&wbuf->timer));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300320
Artem Bityutskiy0b335b92009-06-23 12:30:43 +0300321 if (wbuf->no_timer)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300322 return;
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300323 dbg_io("set timer for jhead %s, %llu-%llu millisecs",
324 dbg_jhead(wbuf->jhead),
Adrian Hunter44737582009-06-24 10:15:12 +0300325 div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC),
326 div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta,
327 USEC_PER_SEC));
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300328 hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
329 HRTIMER_MODE_REL);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300330}
331
332/**
333 * cancel_wbuf_timer - cancel write-buffer timer.
334 * @wbuf: write-buffer descriptor
335 */
336static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
337{
Artem Bityutskiy0b335b92009-06-23 12:30:43 +0300338 if (wbuf->no_timer)
339 return;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300340 wbuf->need_sync = 0;
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300341 hrtimer_cancel(&wbuf->timer);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300342}
343
344/**
345 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
346 * @wbuf: write-buffer to synchronize
347 *
348 * This function synchronizes write-buffer @buf and returns zero in case of
349 * success or a negative error code in case of failure.
350 */
351int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
352{
353 struct ubifs_info *c = wbuf->c;
354 int err, dirt;
355
356 cancel_wbuf_timer_nolock(wbuf);
357 if (!wbuf->used || wbuf->lnum == -1)
358 /* Write-buffer is empty or not seeked */
359 return 0;
360
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300361 dbg_io("LEB %d:%d, %d bytes, jhead %s",
362 wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300363 ubifs_assert(!(wbuf->avail & 7));
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200364 ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size);
365 ubifs_assert(wbuf->size >= c->min_io_size);
366 ubifs_assert(wbuf->size <= c->max_write_size);
367 ubifs_assert(wbuf->size % c->min_io_size == 0);
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300368 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300369
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300370 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300371 return -EROFS;
372
373 ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail);
374 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200375 wbuf->size, wbuf->dtype);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300376 if (err) {
377 ubifs_err("cannot write %d bytes to LEB %d:%d",
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200378 wbuf->size, wbuf->lnum, wbuf->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300379 dbg_dump_stack();
380 return err;
381 }
382
383 dirt = wbuf->avail;
384
385 spin_lock(&wbuf->lock);
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200386 wbuf->offs += wbuf->size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300387 wbuf->avail = c->min_io_size;
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200388 wbuf->size = c->min_io_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300389 wbuf->used = 0;
390 wbuf->next_ino = 0;
391 spin_unlock(&wbuf->lock);
392
393 if (wbuf->sync_callback)
394 err = wbuf->sync_callback(c, wbuf->lnum,
395 c->leb_size - wbuf->offs, dirt);
396 return err;
397}
398
399/**
400 * ubifs_wbuf_seek_nolock - seek write-buffer.
401 * @wbuf: write-buffer
402 * @lnum: logical eraseblock number to seek to
403 * @offs: logical eraseblock offset to seek to
404 * @dtype: data type
405 *
Artem Bityutskiycb54ef82009-06-23 20:30:32 +0300406 * This function targets the write-buffer to logical eraseblock @lnum:@offs.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300407 * The write-buffer is synchronized if it is not empty. Returns zero in case of
408 * success and a negative error code in case of failure.
409 */
410int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
411 int dtype)
412{
413 const struct ubifs_info *c = wbuf->c;
414
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300415 dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300416 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
417 ubifs_assert(offs >= 0 && offs <= c->leb_size);
418 ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
419 ubifs_assert(lnum != wbuf->lnum);
420
421 if (wbuf->used > 0) {
422 int err = ubifs_wbuf_sync_nolock(wbuf);
423
424 if (err)
425 return err;
426 }
427
428 spin_lock(&wbuf->lock);
429 wbuf->lnum = lnum;
430 wbuf->offs = offs;
431 wbuf->avail = c->min_io_size;
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200432 wbuf->size = c->min_io_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300433 wbuf->used = 0;
434 spin_unlock(&wbuf->lock);
435 wbuf->dtype = dtype;
436
437 return 0;
438}
439
440/**
441 * ubifs_bg_wbufs_sync - synchronize write-buffers.
442 * @c: UBIFS file-system description object
443 *
444 * This function is called by background thread to synchronize write-buffers.
445 * Returns zero in case of success and a negative error code in case of
446 * failure.
447 */
448int ubifs_bg_wbufs_sync(struct ubifs_info *c)
449{
450 int err, i;
451
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300452 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300453 if (!c->need_wbuf_sync)
454 return 0;
455 c->need_wbuf_sync = 0;
456
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300457 if (c->ro_error) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300458 err = -EROFS;
459 goto out_timers;
460 }
461
462 dbg_io("synchronize");
463 for (i = 0; i < c->jhead_cnt; i++) {
464 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
465
466 cond_resched();
467
468 /*
469 * If the mutex is locked then wbuf is being changed, so
470 * synchronization is not necessary.
471 */
472 if (mutex_is_locked(&wbuf->io_mutex))
473 continue;
474
475 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
476 if (!wbuf->need_sync) {
477 mutex_unlock(&wbuf->io_mutex);
478 continue;
479 }
480
481 err = ubifs_wbuf_sync_nolock(wbuf);
482 mutex_unlock(&wbuf->io_mutex);
483 if (err) {
484 ubifs_err("cannot sync write-buffer, error %d", err);
485 ubifs_ro_mode(c, err);
486 goto out_timers;
487 }
488 }
489
490 return 0;
491
492out_timers:
493 /* Cancel all timers to prevent repeated errors */
494 for (i = 0; i < c->jhead_cnt; i++) {
495 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
496
497 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
498 cancel_wbuf_timer_nolock(wbuf);
499 mutex_unlock(&wbuf->io_mutex);
500 }
501 return err;
502}
503
504/**
505 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
506 * @wbuf: write-buffer
507 * @buf: node to write
508 * @len: node length
509 *
510 * This function writes data to flash via write-buffer @wbuf. This means that
511 * the last piece of the node won't reach the flash media immediately if it
512 * does not take whole minimal I/O unit. Instead, the node will sit in RAM
513 * until the write-buffer is synchronized (e.g., by timer).
514 *
515 * This function returns zero in case of success and a negative error code in
516 * case of failure. If the node cannot be written because there is no more
517 * space in this logical eraseblock, %-ENOSPC is returned.
518 */
519int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
520{
521 struct ubifs_info *c = wbuf->c;
522 int err, written, n, aligned_len = ALIGN(len, 8), offs;
523
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300524 dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
525 dbg_ntype(((struct ubifs_ch *)buf)->node_type),
526 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300527 ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
528 ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
529 ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200530 ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size);
531 ubifs_assert(wbuf->size >= c->min_io_size);
532 ubifs_assert(wbuf->size <= c->max_write_size);
533 ubifs_assert(wbuf->size % c->min_io_size == 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300534 ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300535 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300536
537 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
538 err = -ENOSPC;
539 goto out;
540 }
541
542 cancel_wbuf_timer_nolock(wbuf);
543
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300544 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300545 return -EROFS;
546
547 if (aligned_len <= wbuf->avail) {
548 /*
549 * The node is not very large and fits entirely within
550 * write-buffer.
551 */
552 memcpy(wbuf->buf + wbuf->used, buf, len);
553
554 if (aligned_len == wbuf->avail) {
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300555 dbg_io("flush jhead %s wbuf to LEB %d:%d",
556 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300557 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200558 wbuf->offs, wbuf->size,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300559 wbuf->dtype);
560 if (err)
561 goto out;
562
563 spin_lock(&wbuf->lock);
564 wbuf->offs += c->min_io_size;
565 wbuf->avail = c->min_io_size;
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200566 wbuf->size = c->min_io_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300567 wbuf->used = 0;
568 wbuf->next_ino = 0;
569 spin_unlock(&wbuf->lock);
570 } else {
571 spin_lock(&wbuf->lock);
572 wbuf->avail -= aligned_len;
573 wbuf->used += aligned_len;
574 spin_unlock(&wbuf->lock);
575 }
576
577 goto exit;
578 }
579
580 /*
581 * The node is large enough and does not fit entirely within current
582 * minimal I/O unit. We have to fill and flush write-buffer and switch
583 * to the next min. I/O unit.
584 */
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300585 dbg_io("flush jhead %s wbuf to LEB %d:%d",
586 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300587 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
588 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200589 wbuf->size, wbuf->dtype);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300590 if (err)
591 goto out;
592
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200593 offs = wbuf->offs + wbuf->size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300594 len -= wbuf->avail;
595 aligned_len -= wbuf->avail;
596 written = wbuf->avail;
597
598 /*
599 * The remaining data may take more whole min. I/O units, so write the
600 * remains multiple to min. I/O unit size directly to the flash media.
601 * We align node length to 8-byte boundary because we anyway flash wbuf
602 * if the remaining space is less than 8 bytes.
603 */
604 n = aligned_len >> c->min_io_shift;
605 if (n) {
606 n <<= c->min_io_shift;
607 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
608 err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
609 wbuf->dtype);
610 if (err)
611 goto out;
612 offs += n;
613 aligned_len -= n;
614 len -= n;
615 written += n;
616 }
617
618 spin_lock(&wbuf->lock);
619 if (aligned_len)
620 /*
621 * And now we have what's left and what does not take whole
622 * min. I/O unit, so write it to the write-buffer and we are
623 * done.
624 */
625 memcpy(wbuf->buf, buf + written, len);
626
627 wbuf->offs = offs;
628 wbuf->used = aligned_len;
629 wbuf->avail = c->min_io_size - aligned_len;
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200630 wbuf->size = c->min_io_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300631 wbuf->next_ino = 0;
632 spin_unlock(&wbuf->lock);
633
634exit:
635 if (wbuf->sync_callback) {
636 int free = c->leb_size - wbuf->offs - wbuf->used;
637
638 err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
639 if (err)
640 goto out;
641 }
642
643 if (wbuf->used)
644 new_wbuf_timer_nolock(wbuf);
645
646 return 0;
647
648out:
649 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
650 len, wbuf->lnum, wbuf->offs, err);
651 dbg_dump_node(c, buf);
652 dbg_dump_stack();
653 dbg_dump_leb(c, wbuf->lnum);
654 return err;
655}
656
657/**
658 * ubifs_write_node - write node to the media.
659 * @c: UBIFS file-system description object
660 * @buf: the node to write
661 * @len: node length
662 * @lnum: logical eraseblock number
663 * @offs: offset within the logical eraseblock
664 * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
665 *
666 * This function automatically fills node magic number, assigns sequence
667 * number, and calculates node CRC checksum. The length of the @buf buffer has
668 * to be aligned to the minimal I/O unit size. This function automatically
669 * appends padding node and padding bytes if needed. Returns zero in case of
670 * success and a negative error code in case of failure.
671 */
672int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
673 int offs, int dtype)
674{
675 int err, buf_len = ALIGN(len, c->min_io_size);
676
677 dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
678 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
679 buf_len);
680 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
681 ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300682 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300683
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300684 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300685 return -EROFS;
686
687 ubifs_prepare_node(c, buf, len, 1);
688 err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
689 if (err) {
690 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
691 buf_len, lnum, offs, err);
692 dbg_dump_node(c, buf);
693 dbg_dump_stack();
694 }
695
696 return err;
697}
698
699/**
700 * ubifs_read_node_wbuf - read node from the media or write-buffer.
701 * @wbuf: wbuf to check for un-written data
702 * @buf: buffer to read to
703 * @type: node type
704 * @len: node length
705 * @lnum: logical eraseblock number
706 * @offs: offset within the logical eraseblock
707 *
708 * This function reads a node of known type and length, checks it and stores
709 * in @buf. If the node partially or fully sits in the write-buffer, this
710 * function takes data from the buffer, otherwise it reads the flash media.
711 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
712 * error code in case of failure.
713 */
714int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
715 int lnum, int offs)
716{
717 const struct ubifs_info *c = wbuf->c;
718 int err, rlen, overlap;
719 struct ubifs_ch *ch = buf;
720
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300721 dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
722 dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300723 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
724 ubifs_assert(!(offs & 7) && offs < c->leb_size);
725 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
726
727 spin_lock(&wbuf->lock);
728 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
729 if (!overlap) {
730 /* We may safely unlock the write-buffer and read the data */
731 spin_unlock(&wbuf->lock);
732 return ubifs_read_node(c, buf, type, len, lnum, offs);
733 }
734
735 /* Don't read under wbuf */
736 rlen = wbuf->offs - offs;
737 if (rlen < 0)
738 rlen = 0;
739
740 /* Copy the rest from the write-buffer */
741 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
742 spin_unlock(&wbuf->lock);
743
744 if (rlen > 0) {
745 /* Read everything that goes before write-buffer */
746 err = ubi_read(c->ubi, lnum, buf, offs, rlen);
747 if (err && err != -EBADMSG) {
748 ubifs_err("failed to read node %d from LEB %d:%d, "
749 "error %d", type, lnum, offs, err);
750 dbg_dump_stack();
751 return err;
752 }
753 }
754
755 if (type != ch->node_type) {
756 ubifs_err("bad node type (%d but expected %d)",
757 ch->node_type, type);
758 goto out;
759 }
760
Adrian Hunter2953e732008-09-04 16:26:00 +0300761 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300762 if (err) {
763 ubifs_err("expected node type %d", type);
764 return err;
765 }
766
767 rlen = le32_to_cpu(ch->len);
768 if (rlen != len) {
769 ubifs_err("bad node length %d, expected %d", rlen, len);
770 goto out;
771 }
772
773 return 0;
774
775out:
776 ubifs_err("bad node at LEB %d:%d", lnum, offs);
777 dbg_dump_node(c, buf);
778 dbg_dump_stack();
779 return -EINVAL;
780}
781
782/**
783 * ubifs_read_node - read node.
784 * @c: UBIFS file-system description object
785 * @buf: buffer to read to
786 * @type: node type
787 * @len: node length (not aligned)
788 * @lnum: logical eraseblock number
789 * @offs: offset within the logical eraseblock
790 *
791 * This function reads a node of known type and and length, checks it and
792 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
793 * and a negative error code in case of failure.
794 */
795int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
796 int lnum, int offs)
797{
798 int err, l;
799 struct ubifs_ch *ch = buf;
800
801 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
802 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
803 ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
804 ubifs_assert(!(offs & 7) && offs < c->leb_size);
805 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
806
807 err = ubi_read(c->ubi, lnum, buf, offs, len);
808 if (err && err != -EBADMSG) {
809 ubifs_err("cannot read node %d from LEB %d:%d, error %d",
810 type, lnum, offs, err);
811 return err;
812 }
813
814 if (type != ch->node_type) {
815 ubifs_err("bad node type (%d but expected %d)",
816 ch->node_type, type);
817 goto out;
818 }
819
Adrian Hunter2953e732008-09-04 16:26:00 +0300820 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300821 if (err) {
822 ubifs_err("expected node type %d", type);
823 return err;
824 }
825
826 l = le32_to_cpu(ch->len);
827 if (l != len) {
828 ubifs_err("bad node length %d, expected %d", l, len);
829 goto out;
830 }
831
832 return 0;
833
834out:
Artem Bityutskiy3a8fa0e2010-08-22 21:27:30 +0300835 ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs,
836 ubi_is_mapped(c->ubi, lnum));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300837 dbg_dump_node(c, buf);
838 dbg_dump_stack();
839 return -EINVAL;
840}
841
842/**
843 * ubifs_wbuf_init - initialize write-buffer.
844 * @c: UBIFS file-system description object
845 * @wbuf: write-buffer to initialize
846 *
Artem Bityutskiycb54ef82009-06-23 20:30:32 +0300847 * This function initializes write-buffer. Returns zero in case of success
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300848 * %-ENOMEM in case of failure.
849 */
850int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
851{
852 size_t size;
853
854 wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL);
855 if (!wbuf->buf)
856 return -ENOMEM;
857
858 size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
859 wbuf->inodes = kmalloc(size, GFP_KERNEL);
860 if (!wbuf->inodes) {
861 kfree(wbuf->buf);
862 wbuf->buf = NULL;
863 return -ENOMEM;
864 }
865
866 wbuf->used = 0;
867 wbuf->lnum = wbuf->offs = -1;
Artem Bityutskiy3c89f392011-02-01 19:02:49 +0200868 wbuf->avail = wbuf->size = c->min_io_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300869 wbuf->dtype = UBI_UNKNOWN;
870 wbuf->sync_callback = NULL;
871 mutex_init(&wbuf->io_mutex);
872 spin_lock_init(&wbuf->lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300873 wbuf->c = c;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300874 wbuf->next_ino = 0;
875
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300876 hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
877 wbuf->timer.function = wbuf_timer_callback_nolock;
Artem Bityutskiy2a35a3a82009-06-23 20:26:33 +0300878 wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
879 wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
880 wbuf->delta *= 1000000000ULL;
881 ubifs_assert(wbuf->delta <= ULONG_MAX);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300882 return 0;
883}
884
885/**
886 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
Artem Bityutskiycb54ef82009-06-23 20:30:32 +0300887 * @wbuf: the write-buffer where to add
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300888 * @inum: the inode number
889 *
890 * This function adds an inode number to the inode array of the write-buffer.
891 */
892void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
893{
894 if (!wbuf->buf)
895 /* NOR flash or something similar */
896 return;
897
898 spin_lock(&wbuf->lock);
899 if (wbuf->used)
900 wbuf->inodes[wbuf->next_ino++] = inum;
901 spin_unlock(&wbuf->lock);
902}
903
904/**
905 * wbuf_has_ino - returns if the wbuf contains data from the inode.
906 * @wbuf: the write-buffer
907 * @inum: the inode number
908 *
909 * This function returns with %1 if the write-buffer contains some data from the
910 * given inode otherwise it returns with %0.
911 */
912static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
913{
914 int i, ret = 0;
915
916 spin_lock(&wbuf->lock);
917 for (i = 0; i < wbuf->next_ino; i++)
918 if (inum == wbuf->inodes[i]) {
919 ret = 1;
920 break;
921 }
922 spin_unlock(&wbuf->lock);
923
924 return ret;
925}
926
927/**
928 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
929 * @c: UBIFS file-system description object
930 * @inode: inode to synchronize
931 *
932 * This function synchronizes write-buffers which contain nodes belonging to
933 * @inode. Returns zero in case of success and a negative error code in case of
934 * failure.
935 */
936int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
937{
938 int i, err = 0;
939
940 for (i = 0; i < c->jhead_cnt; i++) {
941 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
942
943 if (i == GCHD)
944 /*
945 * GC head is special, do not look at it. Even if the
946 * head contains something related to this inode, it is
947 * a _copy_ of corresponding on-flash node which sits
948 * somewhere else.
949 */
950 continue;
951
952 if (!wbuf_has_ino(wbuf, inode->i_ino))
953 continue;
954
955 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
956 if (wbuf_has_ino(wbuf, inode->i_ino))
957 err = ubifs_wbuf_sync_nolock(wbuf);
958 mutex_unlock(&wbuf->io_mutex);
959
960 if (err) {
961 ubifs_ro_mode(c, err);
962 return err;
963 }
964 }
965 return 0;
966}