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 * similarto 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>
57 * ubifs_ro_mode - switch UBIFS to read read-only mode.
58 * @c: UBIFS file-system description object
59 * @err: error code which is the reason of switching to R/O mode
61 void ubifs_ro_mode(struct ubifs_info *c, int err)
65 ubifs_warn("switched to read-only mode, error %d", err);
71 * ubifs_check_node - check node.
72 * @c: UBIFS file-system description object
74 * @lnum: logical eraseblock number
75 * @offs: offset within the logical eraseblock
76 * @quiet: print no messages
77 * @chk_crc: indicates whether to always check the CRC
79 * This function checks node magic number and CRC checksum. This function also
80 * validates node length to prevent UBIFS from becoming crazy when an attacker
81 * feeds it a file-system image with incorrect nodes. For example, too large
82 * node length in the common header could cause UBIFS to read memory outside of
83 * allocated buffer when checking the CRC checksum.
85 * This function returns zero in case of success %-EUCLEAN in case of bad CRC
88 int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
89 int offs, int quiet, int chk_crc)
91 int err = -EINVAL, type, node_len;
92 uint32_t crc, node_crc, magic;
93 const struct ubifs_ch *ch = buf;
95 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
96 ubifs_assert(!(offs & 7) && offs < c->leb_size);
98 magic = le32_to_cpu(ch->magic);
99 if (magic != UBIFS_NODE_MAGIC) {
101 ubifs_err("bad magic %#08x, expected %#08x",
102 magic, UBIFS_NODE_MAGIC);
107 type = ch->node_type;
108 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
110 ubifs_err("bad node type %d", type);
114 node_len = le32_to_cpu(ch->len);
115 if (node_len + offs > c->leb_size)
118 if (c->ranges[type].max_len == 0) {
119 if (node_len != c->ranges[type].len)
121 } else if (node_len < c->ranges[type].min_len ||
122 node_len > c->ranges[type].max_len)
125 if (!chk_crc && type == UBIFS_DATA_NODE && !c->always_chk_crc)
126 if (c->no_chk_data_crc)
129 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
130 node_crc = le32_to_cpu(ch->crc);
131 if (crc != node_crc) {
133 ubifs_err("bad CRC: calculated %#08x, read %#08x",
143 ubifs_err("bad node length %d", node_len);
146 ubifs_err("bad node at LEB %d:%d", lnum, offs);
147 dbg_dump_node(c, buf);
154 * ubifs_pad - pad flash space.
155 * @c: UBIFS file-system description object
156 * @buf: buffer to put padding to
157 * @pad: how many bytes to pad
159 * The flash media obliges us to write only in chunks of %c->min_io_size and
160 * when we have to write less data we add padding node to the write-buffer and
161 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
162 * media is being scanned. If the amount of wasted space is not enough to fit a
163 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
164 * pattern (%UBIFS_PADDING_BYTE).
166 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
169 void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
173 ubifs_assert(pad >= 0 && !(pad & 7));
175 if (pad >= UBIFS_PAD_NODE_SZ) {
176 struct ubifs_ch *ch = buf;
177 struct ubifs_pad_node *pad_node = buf;
179 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
180 ch->node_type = UBIFS_PAD_NODE;
181 ch->group_type = UBIFS_NO_NODE_GROUP;
182 ch->padding[0] = ch->padding[1] = 0;
184 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
185 pad -= UBIFS_PAD_NODE_SZ;
186 pad_node->pad_len = cpu_to_le32(pad);
187 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
188 ch->crc = cpu_to_le32(crc);
189 memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
191 /* Too little space, padding node won't fit */
192 memset(buf, UBIFS_PADDING_BYTE, pad);
196 * next_sqnum - get next sequence number.
197 * @c: UBIFS file-system description object
199 static unsigned long long next_sqnum(struct ubifs_info *c)
201 unsigned long long sqnum;
203 spin_lock(&c->cnt_lock);
204 sqnum = ++c->max_sqnum;
205 spin_unlock(&c->cnt_lock);
207 if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
208 if (sqnum >= SQNUM_WATERMARK) {
209 ubifs_err("sequence number overflow %llu, end of life",
211 ubifs_ro_mode(c, -EINVAL);
213 ubifs_warn("running out of sequence numbers, end of life soon");
220 * ubifs_prepare_node - prepare node to be written to flash.
221 * @c: UBIFS file-system description object
222 * @node: the node to pad
224 * @pad: if the buffer has to be padded
226 * This function prepares node at @node to be written to the media - it
227 * calculates node CRC, fills the common header, and adds proper padding up to
228 * the next minimum I/O unit if @pad is not zero.
230 void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
233 struct ubifs_ch *ch = node;
234 unsigned long long sqnum = next_sqnum(c);
236 ubifs_assert(len >= UBIFS_CH_SZ);
238 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
239 ch->len = cpu_to_le32(len);
240 ch->group_type = UBIFS_NO_NODE_GROUP;
241 ch->sqnum = cpu_to_le64(sqnum);
242 ch->padding[0] = ch->padding[1] = 0;
243 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
244 ch->crc = cpu_to_le32(crc);
248 pad = ALIGN(len, c->min_io_size) - len;
249 ubifs_pad(c, node + len, pad);
254 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
255 * @c: UBIFS file-system description object
256 * @node: the node to pad
258 * @last: indicates the last node of the group
260 * This function prepares node at @node to be written to the media - it
261 * calculates node CRC and fills the common header.
263 void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
266 struct ubifs_ch *ch = node;
267 unsigned long long sqnum = next_sqnum(c);
269 ubifs_assert(len >= UBIFS_CH_SZ);
271 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
272 ch->len = cpu_to_le32(len);
274 ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
276 ch->group_type = UBIFS_IN_NODE_GROUP;
277 ch->sqnum = cpu_to_le64(sqnum);
278 ch->padding[0] = ch->padding[1] = 0;
279 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
280 ch->crc = cpu_to_le32(crc);
284 * wbuf_timer_callback - write-buffer timer callback function.
285 * @data: timer data (write-buffer descriptor)
287 * This function is called when the write-buffer timer expires.
289 static void wbuf_timer_callback_nolock(unsigned long data)
291 struct ubifs_wbuf *wbuf = (struct ubifs_wbuf *)data;
294 wbuf->c->need_wbuf_sync = 1;
295 ubifs_wake_up_bgt(wbuf->c);
299 * new_wbuf_timer - start new write-buffer timer.
300 * @wbuf: write-buffer descriptor
302 static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
304 ubifs_assert(!timer_pending(&wbuf->timer));
309 wbuf->timer.expires = jiffies + wbuf->timeout;
310 add_timer(&wbuf->timer);
314 * cancel_wbuf_timer - cancel write-buffer timer.
315 * @wbuf: write-buffer descriptor
317 static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
320 * If the syncer is waiting for the lock (from the background thread's
321 * context) and another task is changing write-buffer then the syncing
322 * should be canceled.
325 del_timer(&wbuf->timer);
329 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
330 * @wbuf: write-buffer to synchronize
332 * This function synchronizes write-buffer @buf and returns zero in case of
333 * success or a negative error code in case of failure.
335 int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
337 struct ubifs_info *c = wbuf->c;
340 cancel_wbuf_timer_nolock(wbuf);
341 if (!wbuf->used || wbuf->lnum == -1)
342 /* Write-buffer is empty or not seeked */
345 dbg_io("LEB %d:%d, %d bytes",
346 wbuf->lnum, wbuf->offs, wbuf->used);
347 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY));
348 ubifs_assert(!(wbuf->avail & 7));
349 ubifs_assert(wbuf->offs + c->min_io_size <= c->leb_size);
354 ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail);
355 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
356 c->min_io_size, wbuf->dtype);
358 ubifs_err("cannot write %d bytes to LEB %d:%d",
359 c->min_io_size, wbuf->lnum, wbuf->offs);
366 spin_lock(&wbuf->lock);
367 wbuf->offs += c->min_io_size;
368 wbuf->avail = c->min_io_size;
371 spin_unlock(&wbuf->lock);
373 if (wbuf->sync_callback)
374 err = wbuf->sync_callback(c, wbuf->lnum,
375 c->leb_size - wbuf->offs, dirt);
380 * ubifs_wbuf_seek_nolock - seek write-buffer.
381 * @wbuf: write-buffer
382 * @lnum: logical eraseblock number to seek to
383 * @offs: logical eraseblock offset to seek to
386 * This function targets the write buffer to logical eraseblock @lnum:@offs.
387 * The write-buffer is synchronized if it is not empty. Returns zero in case of
388 * success and a negative error code in case of failure.
390 int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
393 const struct ubifs_info *c = wbuf->c;
395 dbg_io("LEB %d:%d", lnum, offs);
396 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
397 ubifs_assert(offs >= 0 && offs <= c->leb_size);
398 ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
399 ubifs_assert(lnum != wbuf->lnum);
401 if (wbuf->used > 0) {
402 int err = ubifs_wbuf_sync_nolock(wbuf);
408 spin_lock(&wbuf->lock);
411 wbuf->avail = c->min_io_size;
413 spin_unlock(&wbuf->lock);
420 * ubifs_bg_wbufs_sync - synchronize write-buffers.
421 * @c: UBIFS file-system description object
423 * This function is called by background thread to synchronize write-buffers.
424 * Returns zero in case of success and a negative error code in case of
427 int ubifs_bg_wbufs_sync(struct ubifs_info *c)
431 if (!c->need_wbuf_sync)
433 c->need_wbuf_sync = 0;
440 dbg_io("synchronize");
441 for (i = 0; i < c->jhead_cnt; i++) {
442 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
447 * If the mutex is locked then wbuf is being changed, so
448 * synchronization is not necessary.
450 if (mutex_is_locked(&wbuf->io_mutex))
453 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
454 if (!wbuf->need_sync) {
455 mutex_unlock(&wbuf->io_mutex);
459 err = ubifs_wbuf_sync_nolock(wbuf);
460 mutex_unlock(&wbuf->io_mutex);
462 ubifs_err("cannot sync write-buffer, error %d", err);
463 ubifs_ro_mode(c, err);
471 /* Cancel all timers to prevent repeated errors */
472 for (i = 0; i < c->jhead_cnt; i++) {
473 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
475 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
476 cancel_wbuf_timer_nolock(wbuf);
477 mutex_unlock(&wbuf->io_mutex);
483 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
484 * @wbuf: write-buffer
485 * @buf: node to write
488 * This function writes data to flash via write-buffer @wbuf. This means that
489 * the last piece of the node won't reach the flash media immediately if it
490 * does not take whole minimal I/O unit. Instead, the node will sit in RAM
491 * until the write-buffer is synchronized (e.g., by timer).
493 * This function returns zero in case of success and a negative error code in
494 * case of failure. If the node cannot be written because there is no more
495 * space in this logical eraseblock, %-ENOSPC is returned.
497 int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
499 struct ubifs_info *c = wbuf->c;
500 int err, written, n, aligned_len = ALIGN(len, 8), offs;
502 dbg_io("%d bytes (%s) to wbuf at LEB %d:%d", len,
503 dbg_ntype(((struct ubifs_ch *)buf)->node_type), wbuf->lnum,
504 wbuf->offs + wbuf->used);
505 ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
506 ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
507 ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
508 ubifs_assert(wbuf->avail > 0 && wbuf->avail <= c->min_io_size);
509 ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
511 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
516 cancel_wbuf_timer_nolock(wbuf);
521 if (aligned_len <= wbuf->avail) {
523 * The node is not very large and fits entirely within
526 memcpy(wbuf->buf + wbuf->used, buf, len);
528 if (aligned_len == wbuf->avail) {
529 dbg_io("flush wbuf to LEB %d:%d", wbuf->lnum,
531 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
532 wbuf->offs, c->min_io_size,
537 spin_lock(&wbuf->lock);
538 wbuf->offs += c->min_io_size;
539 wbuf->avail = c->min_io_size;
542 spin_unlock(&wbuf->lock);
544 spin_lock(&wbuf->lock);
545 wbuf->avail -= aligned_len;
546 wbuf->used += aligned_len;
547 spin_unlock(&wbuf->lock);
554 * The node is large enough and does not fit entirely within current
555 * minimal I/O unit. We have to fill and flush write-buffer and switch
556 * to the next min. I/O unit.
558 dbg_io("flush wbuf to LEB %d:%d", wbuf->lnum, wbuf->offs);
559 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
560 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
561 c->min_io_size, wbuf->dtype);
565 offs = wbuf->offs + c->min_io_size;
567 aligned_len -= wbuf->avail;
568 written = wbuf->avail;
571 * The remaining data may take more whole min. I/O units, so write the
572 * remains multiple to min. I/O unit size directly to the flash media.
573 * We align node length to 8-byte boundary because we anyway flash wbuf
574 * if the remaining space is less than 8 bytes.
576 n = aligned_len >> c->min_io_shift;
578 n <<= c->min_io_shift;
579 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
580 err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
590 spin_lock(&wbuf->lock);
593 * And now we have what's left and what does not take whole
594 * min. I/O unit, so write it to the write-buffer and we are
597 memcpy(wbuf->buf, buf + written, len);
600 wbuf->used = aligned_len;
601 wbuf->avail = c->min_io_size - aligned_len;
603 spin_unlock(&wbuf->lock);
606 if (wbuf->sync_callback) {
607 int free = c->leb_size - wbuf->offs - wbuf->used;
609 err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
615 new_wbuf_timer_nolock(wbuf);
620 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
621 len, wbuf->lnum, wbuf->offs, err);
622 dbg_dump_node(c, buf);
624 dbg_dump_leb(c, wbuf->lnum);
629 * ubifs_write_node - write node to the media.
630 * @c: UBIFS file-system description object
631 * @buf: the node to write
633 * @lnum: logical eraseblock number
634 * @offs: offset within the logical eraseblock
635 * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
637 * This function automatically fills node magic number, assigns sequence
638 * number, and calculates node CRC checksum. The length of the @buf buffer has
639 * to be aligned to the minimal I/O unit size. This function automatically
640 * appends padding node and padding bytes if needed. Returns zero in case of
641 * success and a negative error code in case of failure.
643 int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
646 int err, buf_len = ALIGN(len, c->min_io_size);
648 dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
649 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
651 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
652 ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
657 ubifs_prepare_node(c, buf, len, 1);
658 err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
660 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
661 buf_len, lnum, offs, err);
662 dbg_dump_node(c, buf);
670 * ubifs_read_node_wbuf - read node from the media or write-buffer.
671 * @wbuf: wbuf to check for un-written data
672 * @buf: buffer to read to
675 * @lnum: logical eraseblock number
676 * @offs: offset within the logical eraseblock
678 * This function reads a node of known type and length, checks it and stores
679 * in @buf. If the node partially or fully sits in the write-buffer, this
680 * function takes data from the buffer, otherwise it reads the flash media.
681 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
682 * error code in case of failure.
684 int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
687 const struct ubifs_info *c = wbuf->c;
688 int err, rlen, overlap;
689 struct ubifs_ch *ch = buf;
691 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
692 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
693 ubifs_assert(!(offs & 7) && offs < c->leb_size);
694 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
696 spin_lock(&wbuf->lock);
697 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
699 /* We may safely unlock the write-buffer and read the data */
700 spin_unlock(&wbuf->lock);
701 return ubifs_read_node(c, buf, type, len, lnum, offs);
704 /* Don't read under wbuf */
705 rlen = wbuf->offs - offs;
709 /* Copy the rest from the write-buffer */
710 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
711 spin_unlock(&wbuf->lock);
714 /* Read everything that goes before write-buffer */
715 err = ubi_read(c->ubi, lnum, buf, offs, rlen);
716 if (err && err != -EBADMSG) {
717 ubifs_err("failed to read node %d from LEB %d:%d, "
718 "error %d", type, lnum, offs, err);
724 if (type != ch->node_type) {
725 ubifs_err("bad node type (%d but expected %d)",
726 ch->node_type, type);
730 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
732 ubifs_err("expected node type %d", type);
736 rlen = le32_to_cpu(ch->len);
738 ubifs_err("bad node length %d, expected %d", rlen, len);
745 ubifs_err("bad node at LEB %d:%d", lnum, offs);
746 dbg_dump_node(c, buf);
752 * ubifs_read_node - read node.
753 * @c: UBIFS file-system description object
754 * @buf: buffer to read to
756 * @len: node length (not aligned)
757 * @lnum: logical eraseblock number
758 * @offs: offset within the logical eraseblock
760 * This function reads a node of known type and and length, checks it and
761 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
762 * and a negative error code in case of failure.
764 int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
768 struct ubifs_ch *ch = buf;
770 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
771 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
772 ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
773 ubifs_assert(!(offs & 7) && offs < c->leb_size);
774 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
776 err = ubi_read(c->ubi, lnum, buf, offs, len);
777 if (err && err != -EBADMSG) {
778 ubifs_err("cannot read node %d from LEB %d:%d, error %d",
779 type, lnum, offs, err);
783 if (type != ch->node_type) {
784 ubifs_err("bad node type (%d but expected %d)",
785 ch->node_type, type);
789 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
791 ubifs_err("expected node type %d", type);
795 l = le32_to_cpu(ch->len);
797 ubifs_err("bad node length %d, expected %d", l, len);
804 ubifs_err("bad node at LEB %d:%d", lnum, offs);
805 dbg_dump_node(c, buf);
811 * ubifs_wbuf_init - initialize write-buffer.
812 * @c: UBIFS file-system description object
813 * @wbuf: write-buffer to initialize
815 * This function initializes write buffer. Returns zero in case of success
816 * %-ENOMEM in case of failure.
818 int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
822 wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL);
826 size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
827 wbuf->inodes = kmalloc(size, GFP_KERNEL);
835 wbuf->lnum = wbuf->offs = -1;
836 wbuf->avail = c->min_io_size;
837 wbuf->dtype = UBI_UNKNOWN;
838 wbuf->sync_callback = NULL;
839 mutex_init(&wbuf->io_mutex);
840 spin_lock_init(&wbuf->lock);
843 init_timer(&wbuf->timer);
844 wbuf->timer.function = wbuf_timer_callback_nolock;
845 wbuf->timer.data = (unsigned long)wbuf;
846 wbuf->timeout = DEFAULT_WBUF_TIMEOUT;
853 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
854 * @wbuf: the write-buffer whereto add
855 * @inum: the inode number
857 * This function adds an inode number to the inode array of the write-buffer.
859 void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
862 /* NOR flash or something similar */
865 spin_lock(&wbuf->lock);
867 wbuf->inodes[wbuf->next_ino++] = inum;
868 spin_unlock(&wbuf->lock);
872 * wbuf_has_ino - returns if the wbuf contains data from the inode.
873 * @wbuf: the write-buffer
874 * @inum: the inode number
876 * This function returns with %1 if the write-buffer contains some data from the
877 * given inode otherwise it returns with %0.
879 static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
883 spin_lock(&wbuf->lock);
884 for (i = 0; i < wbuf->next_ino; i++)
885 if (inum == wbuf->inodes[i]) {
889 spin_unlock(&wbuf->lock);
895 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
896 * @c: UBIFS file-system description object
897 * @inode: inode to synchronize
899 * This function synchronizes write-buffers which contain nodes belonging to
900 * @inode. Returns zero in case of success and a negative error code in case of
903 int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
907 for (i = 0; i < c->jhead_cnt; i++) {
908 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
912 * GC head is special, do not look at it. Even if the
913 * head contains something related to this inode, it is
914 * a _copy_ of corresponding on-flash node which sits
919 if (!wbuf_has_ino(wbuf, inode->i_ino))
922 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
923 if (wbuf_has_ino(wbuf, inode->i_ino))
924 err = ubifs_wbuf_sync_nolock(wbuf);
925 mutex_unlock(&wbuf->io_mutex);
928 ubifs_ro_mode(c, err);