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.
53 #include <linux/crc32.h>
54 #include <linux/slab.h>
58 * 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
62 void ubifs_ro_mode(struct ubifs_info
*c
, int err
)
66 c
->no_chk_data_crc
= 0;
67 c
->vfs_sb
->s_flags
|= MS_RDONLY
;
68 ubifs_warn("switched to read-only mode, error %d", err
);
74 * ubifs_check_node - check node.
75 * @c: UBIFS file-system description object
77 * @lnum: logical eraseblock number
78 * @offs: offset within the logical eraseblock
79 * @quiet: print no messages
80 * @must_chk_crc: indicates whether to always check the CRC
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.
88 * 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.
94 * This function returns zero in case of success and %-EUCLEAN in case of bad
97 int ubifs_check_node(const struct ubifs_info
*c
, const void *buf
, int lnum
,
98 int offs
, int quiet
, int must_chk_crc
)
100 int err
= -EINVAL
, type
, node_len
;
101 uint32_t crc
, node_crc
, magic
;
102 const struct ubifs_ch
*ch
= buf
;
104 ubifs_assert(lnum
>= 0 && lnum
< c
->leb_cnt
&& offs
>= 0);
105 ubifs_assert(!(offs
& 7) && offs
< c
->leb_size
);
107 magic
= le32_to_cpu(ch
->magic
);
108 if (magic
!= UBIFS_NODE_MAGIC
) {
110 ubifs_err("bad magic %#08x, expected %#08x",
111 magic
, UBIFS_NODE_MAGIC
);
116 type
= ch
->node_type
;
117 if (type
< 0 || type
>= UBIFS_NODE_TYPES_CNT
) {
119 ubifs_err("bad node type %d", type
);
123 node_len
= le32_to_cpu(ch
->len
);
124 if (node_len
+ offs
> c
->leb_size
)
127 if (c
->ranges
[type
].max_len
== 0) {
128 if (node_len
!= c
->ranges
[type
].len
)
130 } else if (node_len
< c
->ranges
[type
].min_len
||
131 node_len
> c
->ranges
[type
].max_len
)
134 if (!must_chk_crc
&& type
== UBIFS_DATA_NODE
&& !c
->always_chk_crc
&&
138 crc
= crc32(UBIFS_CRC32_INIT
, buf
+ 8, node_len
- 8);
139 node_crc
= le32_to_cpu(ch
->crc
);
140 if (crc
!= node_crc
) {
142 ubifs_err("bad CRC: calculated %#08x, read %#08x",
152 ubifs_err("bad node length %d", node_len
);
155 ubifs_err("bad node at LEB %d:%d", lnum
, offs
);
156 dbg_dump_node(c
, buf
);
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
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).
175 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
178 void ubifs_pad(const struct ubifs_info
*c
, void *buf
, int pad
)
182 ubifs_assert(pad
>= 0 && !(pad
& 7));
184 if (pad
>= UBIFS_PAD_NODE_SZ
) {
185 struct ubifs_ch
*ch
= buf
;
186 struct ubifs_pad_node
*pad_node
= buf
;
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;
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
);
200 /* Too little space, padding node won't fit */
201 memset(buf
, UBIFS_PADDING_BYTE
, pad
);
205 * next_sqnum - get next sequence number.
206 * @c: UBIFS file-system description object
208 static unsigned long long next_sqnum(struct ubifs_info
*c
)
210 unsigned long long sqnum
;
212 spin_lock(&c
->cnt_lock
);
213 sqnum
= ++c
->max_sqnum
;
214 spin_unlock(&c
->cnt_lock
);
216 if (unlikely(sqnum
>= SQNUM_WARN_WATERMARK
)) {
217 if (sqnum
>= SQNUM_WATERMARK
) {
218 ubifs_err("sequence number overflow %llu, end of life",
220 ubifs_ro_mode(c
, -EINVAL
);
222 ubifs_warn("running out of sequence numbers, end of life soon");
229 * ubifs_prepare_node - prepare node to be written to flash.
230 * @c: UBIFS file-system description object
231 * @node: the node to pad
233 * @pad: if the buffer has to be padded
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.
239 void ubifs_prepare_node(struct ubifs_info
*c
, void *node
, int len
, int pad
)
242 struct ubifs_ch
*ch
= node
;
243 unsigned long long sqnum
= next_sqnum(c
);
245 ubifs_assert(len
>= UBIFS_CH_SZ
);
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
);
257 pad
= ALIGN(len
, c
->min_io_size
) - len
;
258 ubifs_pad(c
, node
+ len
, pad
);
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
267 * @last: indicates the last node of the group
269 * This function prepares node at @node to be written to the media - it
270 * calculates node CRC and fills the common header.
272 void ubifs_prep_grp_node(struct ubifs_info
*c
, void *node
, int len
, int last
)
275 struct ubifs_ch
*ch
= node
;
276 unsigned long long sqnum
= next_sqnum(c
);
278 ubifs_assert(len
>= UBIFS_CH_SZ
);
280 ch
->magic
= cpu_to_le32(UBIFS_NODE_MAGIC
);
281 ch
->len
= cpu_to_le32(len
);
283 ch
->group_type
= UBIFS_LAST_OF_NODE_GROUP
;
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
);
293 * wbuf_timer_callback - write-buffer timer callback function.
294 * @data: timer data (write-buffer descriptor)
296 * This function is called when the write-buffer timer expires.
298 static enum hrtimer_restart
wbuf_timer_callback_nolock(struct hrtimer
*timer
)
300 struct ubifs_wbuf
*wbuf
= container_of(timer
, struct ubifs_wbuf
, timer
);
302 dbg_io("jhead %s", dbg_jhead(wbuf
->jhead
));
304 wbuf
->c
->need_wbuf_sync
= 1;
305 ubifs_wake_up_bgt(wbuf
->c
);
306 return HRTIMER_NORESTART
;
310 * new_wbuf_timer - start new write-buffer timer.
311 * @wbuf: write-buffer descriptor
313 static void new_wbuf_timer_nolock(struct ubifs_wbuf
*wbuf
)
315 ubifs_assert(!hrtimer_active(&wbuf
->timer
));
319 dbg_io("set timer for jhead %s, %llu-%llu millisecs",
320 dbg_jhead(wbuf
->jhead
),
321 div_u64(ktime_to_ns(wbuf
->softlimit
), USEC_PER_SEC
),
322 div_u64(ktime_to_ns(wbuf
->softlimit
) + wbuf
->delta
,
324 hrtimer_start_range_ns(&wbuf
->timer
, wbuf
->softlimit
, wbuf
->delta
,
329 * cancel_wbuf_timer - cancel write-buffer timer.
330 * @wbuf: write-buffer descriptor
332 static void cancel_wbuf_timer_nolock(struct ubifs_wbuf
*wbuf
)
337 hrtimer_cancel(&wbuf
->timer
);
341 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
342 * @wbuf: write-buffer to synchronize
344 * This function synchronizes write-buffer @buf and returns zero in case of
345 * success or a negative error code in case of failure.
347 int ubifs_wbuf_sync_nolock(struct ubifs_wbuf
*wbuf
)
349 struct ubifs_info
*c
= wbuf
->c
;
352 cancel_wbuf_timer_nolock(wbuf
);
353 if (!wbuf
->used
|| wbuf
->lnum
== -1)
354 /* Write-buffer is empty or not seeked */
357 dbg_io("LEB %d:%d, %d bytes, jhead %s",
358 wbuf
->lnum
, wbuf
->offs
, wbuf
->used
, dbg_jhead(wbuf
->jhead
));
359 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
);
366 ubifs_pad(c
, wbuf
->buf
+ wbuf
->used
, wbuf
->avail
);
367 err
= ubi_leb_write(c
->ubi
, wbuf
->lnum
, wbuf
->buf
, wbuf
->offs
,
368 c
->min_io_size
, wbuf
->dtype
);
370 ubifs_err("cannot write %d bytes to LEB %d:%d",
371 c
->min_io_size
, wbuf
->lnum
, wbuf
->offs
);
378 spin_lock(&wbuf
->lock
);
379 wbuf
->offs
+= c
->min_io_size
;
380 wbuf
->avail
= c
->min_io_size
;
383 spin_unlock(&wbuf
->lock
);
385 if (wbuf
->sync_callback
)
386 err
= wbuf
->sync_callback(c
, wbuf
->lnum
,
387 c
->leb_size
- wbuf
->offs
, dirt
);
392 * ubifs_wbuf_seek_nolock - seek write-buffer.
393 * @wbuf: write-buffer
394 * @lnum: logical eraseblock number to seek to
395 * @offs: logical eraseblock offset to seek to
398 * This function targets the write-buffer to logical eraseblock @lnum:@offs.
399 * The write-buffer is synchronized if it is not empty. Returns zero in case of
400 * success and a negative error code in case of failure.
402 int ubifs_wbuf_seek_nolock(struct ubifs_wbuf
*wbuf
, int lnum
, int offs
,
405 const struct ubifs_info
*c
= wbuf
->c
;
407 dbg_io("LEB %d:%d, jhead %s", lnum
, offs
, dbg_jhead(wbuf
->jhead
));
408 ubifs_assert(lnum
>= 0 && lnum
< c
->leb_cnt
);
409 ubifs_assert(offs
>= 0 && offs
<= c
->leb_size
);
410 ubifs_assert(offs
% c
->min_io_size
== 0 && !(offs
& 7));
411 ubifs_assert(lnum
!= wbuf
->lnum
);
413 if (wbuf
->used
> 0) {
414 int err
= ubifs_wbuf_sync_nolock(wbuf
);
420 spin_lock(&wbuf
->lock
);
423 wbuf
->avail
= c
->min_io_size
;
425 spin_unlock(&wbuf
->lock
);
432 * ubifs_bg_wbufs_sync - synchronize write-buffers.
433 * @c: UBIFS file-system description object
435 * This function is called by background thread to synchronize write-buffers.
436 * Returns zero in case of success and a negative error code in case of
439 int ubifs_bg_wbufs_sync(struct ubifs_info
*c
)
443 if (!c
->need_wbuf_sync
)
445 c
->need_wbuf_sync
= 0;
452 dbg_io("synchronize");
453 for (i
= 0; i
< c
->jhead_cnt
; i
++) {
454 struct ubifs_wbuf
*wbuf
= &c
->jheads
[i
].wbuf
;
459 * If the mutex is locked then wbuf is being changed, so
460 * synchronization is not necessary.
462 if (mutex_is_locked(&wbuf
->io_mutex
))
465 mutex_lock_nested(&wbuf
->io_mutex
, wbuf
->jhead
);
466 if (!wbuf
->need_sync
) {
467 mutex_unlock(&wbuf
->io_mutex
);
471 err
= ubifs_wbuf_sync_nolock(wbuf
);
472 mutex_unlock(&wbuf
->io_mutex
);
474 ubifs_err("cannot sync write-buffer, error %d", err
);
475 ubifs_ro_mode(c
, err
);
483 /* Cancel all timers to prevent repeated errors */
484 for (i
= 0; i
< c
->jhead_cnt
; i
++) {
485 struct ubifs_wbuf
*wbuf
= &c
->jheads
[i
].wbuf
;
487 mutex_lock_nested(&wbuf
->io_mutex
, wbuf
->jhead
);
488 cancel_wbuf_timer_nolock(wbuf
);
489 mutex_unlock(&wbuf
->io_mutex
);
495 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
496 * @wbuf: write-buffer
497 * @buf: node to write
500 * This function writes data to flash via write-buffer @wbuf. This means that
501 * the last piece of the node won't reach the flash media immediately if it
502 * does not take whole minimal I/O unit. Instead, the node will sit in RAM
503 * until the write-buffer is synchronized (e.g., by timer).
505 * This function returns zero in case of success and a negative error code in
506 * case of failure. If the node cannot be written because there is no more
507 * space in this logical eraseblock, %-ENOSPC is returned.
509 int ubifs_wbuf_write_nolock(struct ubifs_wbuf
*wbuf
, void *buf
, int len
)
511 struct ubifs_info
*c
= wbuf
->c
;
512 int err
, written
, n
, aligned_len
= ALIGN(len
, 8), offs
;
514 dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len
,
515 dbg_ntype(((struct ubifs_ch
*)buf
)->node_type
),
516 dbg_jhead(wbuf
->jhead
), wbuf
->lnum
, wbuf
->offs
+ wbuf
->used
);
517 ubifs_assert(len
> 0 && wbuf
->lnum
>= 0 && wbuf
->lnum
< c
->leb_cnt
);
518 ubifs_assert(wbuf
->offs
>= 0 && wbuf
->offs
% c
->min_io_size
== 0);
519 ubifs_assert(!(wbuf
->offs
& 7) && wbuf
->offs
<= c
->leb_size
);
520 ubifs_assert(wbuf
->avail
> 0 && wbuf
->avail
<= c
->min_io_size
);
521 ubifs_assert(mutex_is_locked(&wbuf
->io_mutex
));
523 if (c
->leb_size
- wbuf
->offs
- wbuf
->used
< aligned_len
) {
528 cancel_wbuf_timer_nolock(wbuf
);
533 if (aligned_len
<= wbuf
->avail
) {
535 * The node is not very large and fits entirely within
538 memcpy(wbuf
->buf
+ wbuf
->used
, buf
, len
);
540 if (aligned_len
== wbuf
->avail
) {
541 dbg_io("flush jhead %s wbuf to LEB %d:%d",
542 dbg_jhead(wbuf
->jhead
), wbuf
->lnum
, wbuf
->offs
);
543 err
= ubi_leb_write(c
->ubi
, wbuf
->lnum
, wbuf
->buf
,
544 wbuf
->offs
, c
->min_io_size
,
549 spin_lock(&wbuf
->lock
);
550 wbuf
->offs
+= c
->min_io_size
;
551 wbuf
->avail
= c
->min_io_size
;
554 spin_unlock(&wbuf
->lock
);
556 spin_lock(&wbuf
->lock
);
557 wbuf
->avail
-= aligned_len
;
558 wbuf
->used
+= aligned_len
;
559 spin_unlock(&wbuf
->lock
);
566 * The node is large enough and does not fit entirely within current
567 * minimal I/O unit. We have to fill and flush write-buffer and switch
568 * to the next min. I/O unit.
570 dbg_io("flush jhead %s wbuf to LEB %d:%d",
571 dbg_jhead(wbuf
->jhead
), wbuf
->lnum
, wbuf
->offs
);
572 memcpy(wbuf
->buf
+ wbuf
->used
, buf
, wbuf
->avail
);
573 err
= ubi_leb_write(c
->ubi
, wbuf
->lnum
, wbuf
->buf
, wbuf
->offs
,
574 c
->min_io_size
, wbuf
->dtype
);
578 offs
= wbuf
->offs
+ c
->min_io_size
;
580 aligned_len
-= wbuf
->avail
;
581 written
= wbuf
->avail
;
584 * The remaining data may take more whole min. I/O units, so write the
585 * remains multiple to min. I/O unit size directly to the flash media.
586 * We align node length to 8-byte boundary because we anyway flash wbuf
587 * if the remaining space is less than 8 bytes.
589 n
= aligned_len
>> c
->min_io_shift
;
591 n
<<= c
->min_io_shift
;
592 dbg_io("write %d bytes to LEB %d:%d", n
, wbuf
->lnum
, offs
);
593 err
= ubi_leb_write(c
->ubi
, wbuf
->lnum
, buf
+ written
, offs
, n
,
603 spin_lock(&wbuf
->lock
);
606 * And now we have what's left and what does not take whole
607 * min. I/O unit, so write it to the write-buffer and we are
610 memcpy(wbuf
->buf
, buf
+ written
, len
);
613 wbuf
->used
= aligned_len
;
614 wbuf
->avail
= c
->min_io_size
- aligned_len
;
616 spin_unlock(&wbuf
->lock
);
619 if (wbuf
->sync_callback
) {
620 int free
= c
->leb_size
- wbuf
->offs
- wbuf
->used
;
622 err
= wbuf
->sync_callback(c
, wbuf
->lnum
, free
, 0);
628 new_wbuf_timer_nolock(wbuf
);
633 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
634 len
, wbuf
->lnum
, wbuf
->offs
, err
);
635 dbg_dump_node(c
, buf
);
637 dbg_dump_leb(c
, wbuf
->lnum
);
642 * ubifs_write_node - write node to the media.
643 * @c: UBIFS file-system description object
644 * @buf: the node to write
646 * @lnum: logical eraseblock number
647 * @offs: offset within the logical eraseblock
648 * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
650 * This function automatically fills node magic number, assigns sequence
651 * number, and calculates node CRC checksum. The length of the @buf buffer has
652 * to be aligned to the minimal I/O unit size. This function automatically
653 * appends padding node and padding bytes if needed. Returns zero in case of
654 * success and a negative error code in case of failure.
656 int ubifs_write_node(struct ubifs_info
*c
, void *buf
, int len
, int lnum
,
659 int err
, buf_len
= ALIGN(len
, c
->min_io_size
);
661 dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
662 lnum
, offs
, dbg_ntype(((struct ubifs_ch
*)buf
)->node_type
), len
,
664 ubifs_assert(lnum
>= 0 && lnum
< c
->leb_cnt
&& offs
>= 0);
665 ubifs_assert(offs
% c
->min_io_size
== 0 && offs
< c
->leb_size
);
670 ubifs_prepare_node(c
, buf
, len
, 1);
671 err
= ubi_leb_write(c
->ubi
, lnum
, buf
, offs
, buf_len
, dtype
);
673 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
674 buf_len
, lnum
, offs
, err
);
675 dbg_dump_node(c
, buf
);
683 * ubifs_read_node_wbuf - read node from the media or write-buffer.
684 * @wbuf: wbuf to check for un-written data
685 * @buf: buffer to read to
688 * @lnum: logical eraseblock number
689 * @offs: offset within the logical eraseblock
691 * This function reads a node of known type and length, checks it and stores
692 * in @buf. If the node partially or fully sits in the write-buffer, this
693 * function takes data from the buffer, otherwise it reads the flash media.
694 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
695 * error code in case of failure.
697 int ubifs_read_node_wbuf(struct ubifs_wbuf
*wbuf
, void *buf
, int type
, int len
,
700 const struct ubifs_info
*c
= wbuf
->c
;
701 int err
, rlen
, overlap
;
702 struct ubifs_ch
*ch
= buf
;
704 dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum
, offs
,
705 dbg_ntype(type
), len
, dbg_jhead(wbuf
->jhead
));
706 ubifs_assert(wbuf
&& lnum
>= 0 && lnum
< c
->leb_cnt
&& offs
>= 0);
707 ubifs_assert(!(offs
& 7) && offs
< c
->leb_size
);
708 ubifs_assert(type
>= 0 && type
< UBIFS_NODE_TYPES_CNT
);
710 spin_lock(&wbuf
->lock
);
711 overlap
= (lnum
== wbuf
->lnum
&& offs
+ len
> wbuf
->offs
);
713 /* We may safely unlock the write-buffer and read the data */
714 spin_unlock(&wbuf
->lock
);
715 return ubifs_read_node(c
, buf
, type
, len
, lnum
, offs
);
718 /* Don't read under wbuf */
719 rlen
= wbuf
->offs
- offs
;
723 /* Copy the rest from the write-buffer */
724 memcpy(buf
+ rlen
, wbuf
->buf
+ offs
+ rlen
- wbuf
->offs
, len
- rlen
);
725 spin_unlock(&wbuf
->lock
);
728 /* Read everything that goes before write-buffer */
729 err
= ubi_read(c
->ubi
, lnum
, buf
, offs
, rlen
);
730 if (err
&& err
!= -EBADMSG
) {
731 ubifs_err("failed to read node %d from LEB %d:%d, "
732 "error %d", type
, lnum
, offs
, err
);
738 if (type
!= ch
->node_type
) {
739 ubifs_err("bad node type (%d but expected %d)",
740 ch
->node_type
, type
);
744 err
= ubifs_check_node(c
, buf
, lnum
, offs
, 0, 0);
746 ubifs_err("expected node type %d", type
);
750 rlen
= le32_to_cpu(ch
->len
);
752 ubifs_err("bad node length %d, expected %d", rlen
, len
);
759 ubifs_err("bad node at LEB %d:%d", lnum
, offs
);
760 dbg_dump_node(c
, buf
);
766 * ubifs_read_node - read node.
767 * @c: UBIFS file-system description object
768 * @buf: buffer to read to
770 * @len: node length (not aligned)
771 * @lnum: logical eraseblock number
772 * @offs: offset within the logical eraseblock
774 * This function reads a node of known type and and length, checks it and
775 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
776 * and a negative error code in case of failure.
778 int ubifs_read_node(const struct ubifs_info
*c
, void *buf
, int type
, int len
,
782 struct ubifs_ch
*ch
= buf
;
784 dbg_io("LEB %d:%d, %s, length %d", lnum
, offs
, dbg_ntype(type
), len
);
785 ubifs_assert(lnum
>= 0 && lnum
< c
->leb_cnt
&& offs
>= 0);
786 ubifs_assert(len
>= UBIFS_CH_SZ
&& offs
+ len
<= c
->leb_size
);
787 ubifs_assert(!(offs
& 7) && offs
< c
->leb_size
);
788 ubifs_assert(type
>= 0 && type
< UBIFS_NODE_TYPES_CNT
);
790 err
= ubi_read(c
->ubi
, lnum
, buf
, offs
, len
);
791 if (err
&& err
!= -EBADMSG
) {
792 ubifs_err("cannot read node %d from LEB %d:%d, error %d",
793 type
, lnum
, offs
, err
);
797 if (type
!= ch
->node_type
) {
798 ubifs_err("bad node type (%d but expected %d)",
799 ch
->node_type
, type
);
803 err
= ubifs_check_node(c
, buf
, lnum
, offs
, 0, 0);
805 ubifs_err("expected node type %d", type
);
809 l
= le32_to_cpu(ch
->len
);
811 ubifs_err("bad node length %d, expected %d", l
, len
);
818 ubifs_err("bad node at LEB %d:%d", lnum
, offs
);
819 dbg_dump_node(c
, buf
);
825 * ubifs_wbuf_init - initialize write-buffer.
826 * @c: UBIFS file-system description object
827 * @wbuf: write-buffer to initialize
829 * This function initializes write-buffer. Returns zero in case of success
830 * %-ENOMEM in case of failure.
832 int ubifs_wbuf_init(struct ubifs_info
*c
, struct ubifs_wbuf
*wbuf
)
836 wbuf
->buf
= kmalloc(c
->min_io_size
, GFP_KERNEL
);
840 size
= (c
->min_io_size
/ UBIFS_CH_SZ
+ 1) * sizeof(ino_t
);
841 wbuf
->inodes
= kmalloc(size
, GFP_KERNEL
);
849 wbuf
->lnum
= wbuf
->offs
= -1;
850 wbuf
->avail
= c
->min_io_size
;
851 wbuf
->dtype
= UBI_UNKNOWN
;
852 wbuf
->sync_callback
= NULL
;
853 mutex_init(&wbuf
->io_mutex
);
854 spin_lock_init(&wbuf
->lock
);
858 hrtimer_init(&wbuf
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
859 wbuf
->timer
.function
= wbuf_timer_callback_nolock
;
860 wbuf
->softlimit
= ktime_set(WBUF_TIMEOUT_SOFTLIMIT
, 0);
861 wbuf
->delta
= WBUF_TIMEOUT_HARDLIMIT
- WBUF_TIMEOUT_SOFTLIMIT
;
862 wbuf
->delta
*= 1000000000ULL;
863 ubifs_assert(wbuf
->delta
<= ULONG_MAX
);
868 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
869 * @wbuf: the write-buffer where to add
870 * @inum: the inode number
872 * This function adds an inode number to the inode array of the write-buffer.
874 void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf
*wbuf
, ino_t inum
)
877 /* NOR flash or something similar */
880 spin_lock(&wbuf
->lock
);
882 wbuf
->inodes
[wbuf
->next_ino
++] = inum
;
883 spin_unlock(&wbuf
->lock
);
887 * wbuf_has_ino - returns if the wbuf contains data from the inode.
888 * @wbuf: the write-buffer
889 * @inum: the inode number
891 * This function returns with %1 if the write-buffer contains some data from the
892 * given inode otherwise it returns with %0.
894 static int wbuf_has_ino(struct ubifs_wbuf
*wbuf
, ino_t inum
)
898 spin_lock(&wbuf
->lock
);
899 for (i
= 0; i
< wbuf
->next_ino
; i
++)
900 if (inum
== wbuf
->inodes
[i
]) {
904 spin_unlock(&wbuf
->lock
);
910 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
911 * @c: UBIFS file-system description object
912 * @inode: inode to synchronize
914 * This function synchronizes write-buffers which contain nodes belonging to
915 * @inode. Returns zero in case of success and a negative error code in case of
918 int ubifs_sync_wbufs_by_inode(struct ubifs_info
*c
, struct inode
*inode
)
922 for (i
= 0; i
< c
->jhead_cnt
; i
++) {
923 struct ubifs_wbuf
*wbuf
= &c
->jheads
[i
].wbuf
;
927 * GC head is special, do not look at it. Even if the
928 * head contains something related to this inode, it is
929 * a _copy_ of corresponding on-flash node which sits
934 if (!wbuf_has_ino(wbuf
, inode
->i_ino
))
937 mutex_lock_nested(&wbuf
->io_mutex
, wbuf
->jhead
);
938 if (wbuf_has_ino(wbuf
, inode
->i_ino
))
939 err
= ubifs_wbuf_sync_nolock(wbuf
);
940 mutex_unlock(&wbuf
->io_mutex
);
943 ubifs_ro_mode(c
, err
);