blob: 18a4b8d7c84480f43de67b26009a9937e495364b [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
91 * checked. Similarly, if @c->always_chk_crc is true, @c->no_chk_data_crc is
92 * ignored and CRC is checked.
93 *
94 * This function returns zero in case of success and %-EUCLEAN in case of bad
95 * CRC or magic.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030096 */
97int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +020098 int offs, int quiet, int must_chk_crc)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030099{
100 int err = -EINVAL, type, node_len;
101 uint32_t crc, node_crc, magic;
102 const struct ubifs_ch *ch = buf;
103
104 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
105 ubifs_assert(!(offs & 7) && offs < c->leb_size);
106
107 magic = le32_to_cpu(ch->magic);
108 if (magic != UBIFS_NODE_MAGIC) {
109 if (!quiet)
110 ubifs_err("bad magic %#08x, expected %#08x",
111 magic, UBIFS_NODE_MAGIC);
112 err = -EUCLEAN;
113 goto out;
114 }
115
116 type = ch->node_type;
117 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
118 if (!quiet)
119 ubifs_err("bad node type %d", type);
120 goto out;
121 }
122
123 node_len = le32_to_cpu(ch->len);
124 if (node_len + offs > c->leb_size)
125 goto out_len;
126
127 if (c->ranges[type].max_len == 0) {
128 if (node_len != c->ranges[type].len)
129 goto out_len;
130 } else if (node_len < c->ranges[type].min_len ||
131 node_len > c->ranges[type].max_len)
132 goto out_len;
133
Artem Bityutskiy6f7ab6d2009-01-27 16:12:31 +0200134 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc &&
135 c->no_chk_data_crc)
136 return 0;
Adrian Hunter2953e732008-09-04 16:26:00 +0300137
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300138 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
139 node_crc = le32_to_cpu(ch->crc);
140 if (crc != node_crc) {
141 if (!quiet)
142 ubifs_err("bad CRC: calculated %#08x, read %#08x",
143 crc, node_crc);
144 err = -EUCLEAN;
145 goto out;
146 }
147
148 return 0;
149
150out_len:
151 if (!quiet)
152 ubifs_err("bad node length %d", node_len);
153out:
154 if (!quiet) {
155 ubifs_err("bad node at LEB %d:%d", lnum, offs);
156 dbg_dump_node(c, buf);
157 dbg_dump_stack();
158 }
159 return err;
160}
161
162/**
163 * ubifs_pad - pad flash space.
164 * @c: UBIFS file-system description object
165 * @buf: buffer to put padding to
166 * @pad: how many bytes to pad
167 *
168 * The flash media obliges us to write only in chunks of %c->min_io_size and
169 * when we have to write less data we add padding node to the write-buffer and
170 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
171 * media is being scanned. If the amount of wasted space is not enough to fit a
172 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
173 * pattern (%UBIFS_PADDING_BYTE).
174 *
175 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
176 * used.
177 */
178void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
179{
180 uint32_t crc;
181
182 ubifs_assert(pad >= 0 && !(pad & 7));
183
184 if (pad >= UBIFS_PAD_NODE_SZ) {
185 struct ubifs_ch *ch = buf;
186 struct ubifs_pad_node *pad_node = buf;
187
188 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
189 ch->node_type = UBIFS_PAD_NODE;
190 ch->group_type = UBIFS_NO_NODE_GROUP;
191 ch->padding[0] = ch->padding[1] = 0;
192 ch->sqnum = 0;
193 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
194 pad -= UBIFS_PAD_NODE_SZ;
195 pad_node->pad_len = cpu_to_le32(pad);
196 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
197 ch->crc = cpu_to_le32(crc);
198 memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
199 } else if (pad > 0)
200 /* Too little space, padding node won't fit */
201 memset(buf, UBIFS_PADDING_BYTE, pad);
202}
203
204/**
205 * next_sqnum - get next sequence number.
206 * @c: UBIFS file-system description object
207 */
208static unsigned long long next_sqnum(struct ubifs_info *c)
209{
210 unsigned long long sqnum;
211
212 spin_lock(&c->cnt_lock);
213 sqnum = ++c->max_sqnum;
214 spin_unlock(&c->cnt_lock);
215
216 if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
217 if (sqnum >= SQNUM_WATERMARK) {
218 ubifs_err("sequence number overflow %llu, end of life",
219 sqnum);
220 ubifs_ro_mode(c, -EINVAL);
221 }
222 ubifs_warn("running out of sequence numbers, end of life soon");
223 }
224
225 return sqnum;
226}
227
228/**
229 * ubifs_prepare_node - prepare node to be written to flash.
230 * @c: UBIFS file-system description object
231 * @node: the node to pad
232 * @len: node length
233 * @pad: if the buffer has to be padded
234 *
235 * This function prepares node at @node to be written to the media - it
236 * calculates node CRC, fills the common header, and adds proper padding up to
237 * the next minimum I/O unit if @pad is not zero.
238 */
239void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
240{
241 uint32_t crc;
242 struct ubifs_ch *ch = node;
243 unsigned long long sqnum = next_sqnum(c);
244
245 ubifs_assert(len >= UBIFS_CH_SZ);
246
247 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
248 ch->len = cpu_to_le32(len);
249 ch->group_type = UBIFS_NO_NODE_GROUP;
250 ch->sqnum = cpu_to_le64(sqnum);
251 ch->padding[0] = ch->padding[1] = 0;
252 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
253 ch->crc = cpu_to_le32(crc);
254
255 if (pad) {
256 len = ALIGN(len, 8);
257 pad = ALIGN(len, c->min_io_size) - len;
258 ubifs_pad(c, node + len, pad);
259 }
260}
261
262/**
263 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
264 * @c: UBIFS file-system description object
265 * @node: the node to pad
266 * @len: node length
267 * @last: indicates the last node of the group
268 *
269 * This function prepares node at @node to be written to the media - it
270 * calculates node CRC and fills the common header.
271 */
272void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
273{
274 uint32_t crc;
275 struct ubifs_ch *ch = node;
276 unsigned long long sqnum = next_sqnum(c);
277
278 ubifs_assert(len >= UBIFS_CH_SZ);
279
280 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
281 ch->len = cpu_to_le32(len);
282 if (last)
283 ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
284 else
285 ch->group_type = UBIFS_IN_NODE_GROUP;
286 ch->sqnum = cpu_to_le64(sqnum);
287 ch->padding[0] = ch->padding[1] = 0;
288 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
289 ch->crc = cpu_to_le32(crc);
290}
291
292/**
293 * wbuf_timer_callback - write-buffer timer callback function.
294 * @data: timer data (write-buffer descriptor)
295 *
296 * This function is called when the write-buffer timer expires.
297 */
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300298static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300299{
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300300 struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300301
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300302 dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300303 wbuf->need_sync = 1;
304 wbuf->c->need_wbuf_sync = 1;
305 ubifs_wake_up_bgt(wbuf->c);
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300306 return HRTIMER_NORESTART;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307}
308
309/**
310 * new_wbuf_timer - start new write-buffer timer.
311 * @wbuf: write-buffer descriptor
312 */
313static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
314{
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300315 ubifs_assert(!hrtimer_active(&wbuf->timer));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300316
Artem Bityutskiy0b335b92009-06-23 12:30:43 +0300317 if (wbuf->no_timer)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300318 return;
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300319 dbg_io("set timer for jhead %s, %llu-%llu millisecs",
320 dbg_jhead(wbuf->jhead),
Adrian Hunter44737582009-06-24 10:15:12 +0300321 div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC),
322 div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta,
323 USEC_PER_SEC));
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300324 hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
325 HRTIMER_MODE_REL);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300326}
327
328/**
329 * cancel_wbuf_timer - cancel write-buffer timer.
330 * @wbuf: write-buffer descriptor
331 */
332static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
333{
Artem Bityutskiy0b335b92009-06-23 12:30:43 +0300334 if (wbuf->no_timer)
335 return;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300336 wbuf->need_sync = 0;
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300337 hrtimer_cancel(&wbuf->timer);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300338}
339
340/**
341 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
342 * @wbuf: write-buffer to synchronize
343 *
344 * This function synchronizes write-buffer @buf and returns zero in case of
345 * success or a negative error code in case of failure.
346 */
347int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
348{
349 struct ubifs_info *c = wbuf->c;
350 int err, dirt;
351
352 cancel_wbuf_timer_nolock(wbuf);
353 if (!wbuf->used || wbuf->lnum == -1)
354 /* Write-buffer is empty or not seeked */
355 return 0;
356
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300357 dbg_io("LEB %d:%d, %d bytes, jhead %s",
358 wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300359 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY));
360 ubifs_assert(!(wbuf->avail & 7));
361 ubifs_assert(wbuf->offs + c->min_io_size <= c->leb_size);
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300362 ubifs_assert(!c->ro_media);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300363
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300364 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300365 return -EROFS;
366
367 ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail);
368 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
369 c->min_io_size, wbuf->dtype);
370 if (err) {
371 ubifs_err("cannot write %d bytes to LEB %d:%d",
372 c->min_io_size, wbuf->lnum, wbuf->offs);
373 dbg_dump_stack();
374 return err;
375 }
376
377 dirt = wbuf->avail;
378
379 spin_lock(&wbuf->lock);
380 wbuf->offs += c->min_io_size;
381 wbuf->avail = c->min_io_size;
382 wbuf->used = 0;
383 wbuf->next_ino = 0;
384 spin_unlock(&wbuf->lock);
385
386 if (wbuf->sync_callback)
387 err = wbuf->sync_callback(c, wbuf->lnum,
388 c->leb_size - wbuf->offs, dirt);
389 return err;
390}
391
392/**
393 * ubifs_wbuf_seek_nolock - seek write-buffer.
394 * @wbuf: write-buffer
395 * @lnum: logical eraseblock number to seek to
396 * @offs: logical eraseblock offset to seek to
397 * @dtype: data type
398 *
Artem Bityutskiycb54ef82009-06-23 20:30:32 +0300399 * This function targets the write-buffer to logical eraseblock @lnum:@offs.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300400 * The write-buffer is synchronized if it is not empty. Returns zero in case of
401 * success and a negative error code in case of failure.
402 */
403int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
404 int dtype)
405{
406 const struct ubifs_info *c = wbuf->c;
407
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300408 dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300409 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
410 ubifs_assert(offs >= 0 && offs <= c->leb_size);
411 ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
412 ubifs_assert(lnum != wbuf->lnum);
413
414 if (wbuf->used > 0) {
415 int err = ubifs_wbuf_sync_nolock(wbuf);
416
417 if (err)
418 return err;
419 }
420
421 spin_lock(&wbuf->lock);
422 wbuf->lnum = lnum;
423 wbuf->offs = offs;
424 wbuf->avail = c->min_io_size;
425 wbuf->used = 0;
426 spin_unlock(&wbuf->lock);
427 wbuf->dtype = dtype;
428
429 return 0;
430}
431
432/**
433 * ubifs_bg_wbufs_sync - synchronize write-buffers.
434 * @c: UBIFS file-system description object
435 *
436 * This function is called by background thread to synchronize write-buffers.
437 * Returns zero in case of success and a negative error code in case of
438 * failure.
439 */
440int ubifs_bg_wbufs_sync(struct ubifs_info *c)
441{
442 int err, i;
443
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300444 ubifs_assert(!c->ro_media);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300445 if (!c->need_wbuf_sync)
446 return 0;
447 c->need_wbuf_sync = 0;
448
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300449 if (c->ro_error) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300450 err = -EROFS;
451 goto out_timers;
452 }
453
454 dbg_io("synchronize");
455 for (i = 0; i < c->jhead_cnt; i++) {
456 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
457
458 cond_resched();
459
460 /*
461 * If the mutex is locked then wbuf is being changed, so
462 * synchronization is not necessary.
463 */
464 if (mutex_is_locked(&wbuf->io_mutex))
465 continue;
466
467 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
468 if (!wbuf->need_sync) {
469 mutex_unlock(&wbuf->io_mutex);
470 continue;
471 }
472
473 err = ubifs_wbuf_sync_nolock(wbuf);
474 mutex_unlock(&wbuf->io_mutex);
475 if (err) {
476 ubifs_err("cannot sync write-buffer, error %d", err);
477 ubifs_ro_mode(c, err);
478 goto out_timers;
479 }
480 }
481
482 return 0;
483
484out_timers:
485 /* Cancel all timers to prevent repeated errors */
486 for (i = 0; i < c->jhead_cnt; i++) {
487 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
488
489 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
490 cancel_wbuf_timer_nolock(wbuf);
491 mutex_unlock(&wbuf->io_mutex);
492 }
493 return err;
494}
495
496/**
497 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
498 * @wbuf: write-buffer
499 * @buf: node to write
500 * @len: node length
501 *
502 * This function writes data to flash via write-buffer @wbuf. This means that
503 * the last piece of the node won't reach the flash media immediately if it
504 * does not take whole minimal I/O unit. Instead, the node will sit in RAM
505 * until the write-buffer is synchronized (e.g., by timer).
506 *
507 * This function returns zero in case of success and a negative error code in
508 * case of failure. If the node cannot be written because there is no more
509 * space in this logical eraseblock, %-ENOSPC is returned.
510 */
511int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
512{
513 struct ubifs_info *c = wbuf->c;
514 int err, written, n, aligned_len = ALIGN(len, 8), offs;
515
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300516 dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
517 dbg_ntype(((struct ubifs_ch *)buf)->node_type),
518 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300519 ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
520 ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
521 ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
522 ubifs_assert(wbuf->avail > 0 && wbuf->avail <= c->min_io_size);
523 ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300524 ubifs_assert(!c->ro_media);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300525
526 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
527 err = -ENOSPC;
528 goto out;
529 }
530
531 cancel_wbuf_timer_nolock(wbuf);
532
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300533 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300534 return -EROFS;
535
536 if (aligned_len <= wbuf->avail) {
537 /*
538 * The node is not very large and fits entirely within
539 * write-buffer.
540 */
541 memcpy(wbuf->buf + wbuf->used, buf, len);
542
543 if (aligned_len == wbuf->avail) {
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300544 dbg_io("flush jhead %s wbuf to LEB %d:%d",
545 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300546 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
547 wbuf->offs, c->min_io_size,
548 wbuf->dtype);
549 if (err)
550 goto out;
551
552 spin_lock(&wbuf->lock);
553 wbuf->offs += c->min_io_size;
554 wbuf->avail = c->min_io_size;
555 wbuf->used = 0;
556 wbuf->next_ino = 0;
557 spin_unlock(&wbuf->lock);
558 } else {
559 spin_lock(&wbuf->lock);
560 wbuf->avail -= aligned_len;
561 wbuf->used += aligned_len;
562 spin_unlock(&wbuf->lock);
563 }
564
565 goto exit;
566 }
567
568 /*
569 * The node is large enough and does not fit entirely within current
570 * minimal I/O unit. We have to fill and flush write-buffer and switch
571 * to the next min. I/O unit.
572 */
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300573 dbg_io("flush jhead %s wbuf to LEB %d:%d",
574 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300575 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
576 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
577 c->min_io_size, wbuf->dtype);
578 if (err)
579 goto out;
580
581 offs = wbuf->offs + c->min_io_size;
582 len -= wbuf->avail;
583 aligned_len -= wbuf->avail;
584 written = wbuf->avail;
585
586 /*
587 * The remaining data may take more whole min. I/O units, so write the
588 * remains multiple to min. I/O unit size directly to the flash media.
589 * We align node length to 8-byte boundary because we anyway flash wbuf
590 * if the remaining space is less than 8 bytes.
591 */
592 n = aligned_len >> c->min_io_shift;
593 if (n) {
594 n <<= c->min_io_shift;
595 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
596 err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
597 wbuf->dtype);
598 if (err)
599 goto out;
600 offs += n;
601 aligned_len -= n;
602 len -= n;
603 written += n;
604 }
605
606 spin_lock(&wbuf->lock);
607 if (aligned_len)
608 /*
609 * And now we have what's left and what does not take whole
610 * min. I/O unit, so write it to the write-buffer and we are
611 * done.
612 */
613 memcpy(wbuf->buf, buf + written, len);
614
615 wbuf->offs = offs;
616 wbuf->used = aligned_len;
617 wbuf->avail = c->min_io_size - aligned_len;
618 wbuf->next_ino = 0;
619 spin_unlock(&wbuf->lock);
620
621exit:
622 if (wbuf->sync_callback) {
623 int free = c->leb_size - wbuf->offs - wbuf->used;
624
625 err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
626 if (err)
627 goto out;
628 }
629
630 if (wbuf->used)
631 new_wbuf_timer_nolock(wbuf);
632
633 return 0;
634
635out:
636 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
637 len, wbuf->lnum, wbuf->offs, err);
638 dbg_dump_node(c, buf);
639 dbg_dump_stack();
640 dbg_dump_leb(c, wbuf->lnum);
641 return err;
642}
643
644/**
645 * ubifs_write_node - write node to the media.
646 * @c: UBIFS file-system description object
647 * @buf: the node to write
648 * @len: node length
649 * @lnum: logical eraseblock number
650 * @offs: offset within the logical eraseblock
651 * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
652 *
653 * This function automatically fills node magic number, assigns sequence
654 * number, and calculates node CRC checksum. The length of the @buf buffer has
655 * to be aligned to the minimal I/O unit size. This function automatically
656 * appends padding node and padding bytes if needed. Returns zero in case of
657 * success and a negative error code in case of failure.
658 */
659int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
660 int offs, int dtype)
661{
662 int err, buf_len = ALIGN(len, c->min_io_size);
663
664 dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
665 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
666 buf_len);
667 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
668 ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300669 ubifs_assert(!c->ro_media);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300670
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300671 if (c->ro_error)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300672 return -EROFS;
673
674 ubifs_prepare_node(c, buf, len, 1);
675 err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
676 if (err) {
677 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
678 buf_len, lnum, offs, err);
679 dbg_dump_node(c, buf);
680 dbg_dump_stack();
681 }
682
683 return err;
684}
685
686/**
687 * ubifs_read_node_wbuf - read node from the media or write-buffer.
688 * @wbuf: wbuf to check for un-written data
689 * @buf: buffer to read to
690 * @type: node type
691 * @len: node length
692 * @lnum: logical eraseblock number
693 * @offs: offset within the logical eraseblock
694 *
695 * This function reads a node of known type and length, checks it and stores
696 * in @buf. If the node partially or fully sits in the write-buffer, this
697 * function takes data from the buffer, otherwise it reads the flash media.
698 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
699 * error code in case of failure.
700 */
701int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
702 int lnum, int offs)
703{
704 const struct ubifs_info *c = wbuf->c;
705 int err, rlen, overlap;
706 struct ubifs_ch *ch = buf;
707
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300708 dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
709 dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300710 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
711 ubifs_assert(!(offs & 7) && offs < c->leb_size);
712 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
713
714 spin_lock(&wbuf->lock);
715 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
716 if (!overlap) {
717 /* We may safely unlock the write-buffer and read the data */
718 spin_unlock(&wbuf->lock);
719 return ubifs_read_node(c, buf, type, len, lnum, offs);
720 }
721
722 /* Don't read under wbuf */
723 rlen = wbuf->offs - offs;
724 if (rlen < 0)
725 rlen = 0;
726
727 /* Copy the rest from the write-buffer */
728 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
729 spin_unlock(&wbuf->lock);
730
731 if (rlen > 0) {
732 /* Read everything that goes before write-buffer */
733 err = ubi_read(c->ubi, lnum, buf, offs, rlen);
734 if (err && err != -EBADMSG) {
735 ubifs_err("failed to read node %d from LEB %d:%d, "
736 "error %d", type, lnum, offs, err);
737 dbg_dump_stack();
738 return err;
739 }
740 }
741
742 if (type != ch->node_type) {
743 ubifs_err("bad node type (%d but expected %d)",
744 ch->node_type, type);
745 goto out;
746 }
747
Adrian Hunter2953e732008-09-04 16:26:00 +0300748 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300749 if (err) {
750 ubifs_err("expected node type %d", type);
751 return err;
752 }
753
754 rlen = le32_to_cpu(ch->len);
755 if (rlen != len) {
756 ubifs_err("bad node length %d, expected %d", rlen, len);
757 goto out;
758 }
759
760 return 0;
761
762out:
763 ubifs_err("bad node at LEB %d:%d", lnum, offs);
764 dbg_dump_node(c, buf);
765 dbg_dump_stack();
766 return -EINVAL;
767}
768
769/**
770 * ubifs_read_node - read node.
771 * @c: UBIFS file-system description object
772 * @buf: buffer to read to
773 * @type: node type
774 * @len: node length (not aligned)
775 * @lnum: logical eraseblock number
776 * @offs: offset within the logical eraseblock
777 *
778 * This function reads a node of known type and and length, checks it and
779 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
780 * and a negative error code in case of failure.
781 */
782int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
783 int lnum, int offs)
784{
785 int err, l;
786 struct ubifs_ch *ch = buf;
787
788 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
789 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
790 ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
791 ubifs_assert(!(offs & 7) && offs < c->leb_size);
792 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
793
794 err = ubi_read(c->ubi, lnum, buf, offs, len);
795 if (err && err != -EBADMSG) {
796 ubifs_err("cannot read node %d from LEB %d:%d, error %d",
797 type, lnum, offs, err);
798 return err;
799 }
800
801 if (type != ch->node_type) {
802 ubifs_err("bad node type (%d but expected %d)",
803 ch->node_type, type);
804 goto out;
805 }
806
Adrian Hunter2953e732008-09-04 16:26:00 +0300807 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300808 if (err) {
809 ubifs_err("expected node type %d", type);
810 return err;
811 }
812
813 l = le32_to_cpu(ch->len);
814 if (l != len) {
815 ubifs_err("bad node length %d, expected %d", l, len);
816 goto out;
817 }
818
819 return 0;
820
821out:
Artem Bityutskiy3a8fa0e2010-08-22 21:27:30 +0300822 ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs,
823 ubi_is_mapped(c->ubi, lnum));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300824 dbg_dump_node(c, buf);
825 dbg_dump_stack();
826 return -EINVAL;
827}
828
829/**
830 * ubifs_wbuf_init - initialize write-buffer.
831 * @c: UBIFS file-system description object
832 * @wbuf: write-buffer to initialize
833 *
Artem Bityutskiycb54ef82009-06-23 20:30:32 +0300834 * This function initializes write-buffer. Returns zero in case of success
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300835 * %-ENOMEM in case of failure.
836 */
837int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
838{
839 size_t size;
840
841 wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL);
842 if (!wbuf->buf)
843 return -ENOMEM;
844
845 size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
846 wbuf->inodes = kmalloc(size, GFP_KERNEL);
847 if (!wbuf->inodes) {
848 kfree(wbuf->buf);
849 wbuf->buf = NULL;
850 return -ENOMEM;
851 }
852
853 wbuf->used = 0;
854 wbuf->lnum = wbuf->offs = -1;
855 wbuf->avail = c->min_io_size;
856 wbuf->dtype = UBI_UNKNOWN;
857 wbuf->sync_callback = NULL;
858 mutex_init(&wbuf->io_mutex);
859 spin_lock_init(&wbuf->lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300860 wbuf->c = c;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300861 wbuf->next_ino = 0;
862
Artem Bityutskiyf2c5dbd2009-05-28 16:24:15 +0300863 hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
864 wbuf->timer.function = wbuf_timer_callback_nolock;
Artem Bityutskiy2a35a3a82009-06-23 20:26:33 +0300865 wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
866 wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
867 wbuf->delta *= 1000000000ULL;
868 ubifs_assert(wbuf->delta <= ULONG_MAX);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300869 return 0;
870}
871
872/**
873 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
Artem Bityutskiycb54ef82009-06-23 20:30:32 +0300874 * @wbuf: the write-buffer where to add
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300875 * @inum: the inode number
876 *
877 * This function adds an inode number to the inode array of the write-buffer.
878 */
879void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
880{
881 if (!wbuf->buf)
882 /* NOR flash or something similar */
883 return;
884
885 spin_lock(&wbuf->lock);
886 if (wbuf->used)
887 wbuf->inodes[wbuf->next_ino++] = inum;
888 spin_unlock(&wbuf->lock);
889}
890
891/**
892 * wbuf_has_ino - returns if the wbuf contains data from the inode.
893 * @wbuf: the write-buffer
894 * @inum: the inode number
895 *
896 * This function returns with %1 if the write-buffer contains some data from the
897 * given inode otherwise it returns with %0.
898 */
899static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
900{
901 int i, ret = 0;
902
903 spin_lock(&wbuf->lock);
904 for (i = 0; i < wbuf->next_ino; i++)
905 if (inum == wbuf->inodes[i]) {
906 ret = 1;
907 break;
908 }
909 spin_unlock(&wbuf->lock);
910
911 return ret;
912}
913
914/**
915 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
916 * @c: UBIFS file-system description object
917 * @inode: inode to synchronize
918 *
919 * This function synchronizes write-buffers which contain nodes belonging to
920 * @inode. Returns zero in case of success and a negative error code in case of
921 * failure.
922 */
923int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
924{
925 int i, err = 0;
926
927 for (i = 0; i < c->jhead_cnt; i++) {
928 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
929
930 if (i == GCHD)
931 /*
932 * GC head is special, do not look at it. Even if the
933 * head contains something related to this inode, it is
934 * a _copy_ of corresponding on-flash node which sits
935 * somewhere else.
936 */
937 continue;
938
939 if (!wbuf_has_ino(wbuf, inode->i_ino))
940 continue;
941
942 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
943 if (wbuf_has_ino(wbuf, inode->i_ino))
944 err = ubifs_wbuf_sync_nolock(wbuf);
945 mutex_unlock(&wbuf->io_mutex);
946
947 if (err) {
948 ubifs_ro_mode(c, err);
949 return err;
950 }
951 }
952 return 0;
953}