Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc
[cris-mirror.git] / fs / ubifs / io.c
blobdfd168b7807e179fd450784a6e3a4c0d4b222ebe
1 /*
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
14 * more details.
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 (Битюцкий Артём)
21 * Adrian Hunter
22 * Zoltan Sogor
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 * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
35 * write size (@c->max_write_size). The latter is the maximum amount of bytes
36 * the underlying flash is able to program at a time, and writing in
37 * @c->max_write_size units should presumably be faster. Obviously,
38 * @c->min_io_size <= @c->max_write_size. Write-buffers are of
39 * @c->max_write_size bytes in size for maximum performance. However, when a
40 * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
41 * boundary) which contains data is written, not the whole write-buffer,
42 * because this is more space-efficient.
44 * This optimization adds few complications to the code. Indeed, on the one
45 * hand, we want to write in optimal @c->max_write_size bytes chunks, which
46 * also means aligning writes at the @c->max_write_size bytes offsets. On the
47 * other hand, we do not want to waste space when synchronizing the write
48 * buffer, so during synchronization we writes in smaller chunks. And this makes
49 * the next write offset to be not aligned to @c->max_write_size bytes. So the
50 * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
51 * to @c->max_write_size bytes again. We do this by temporarily shrinking
52 * write-buffer size (@wbuf->size).
54 * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
55 * mutexes defined inside these objects. Since sometimes upper-level code
56 * has to lock the write-buffer (e.g. journal space reservation code), many
57 * functions related to write-buffers have "nolock" suffix which means that the
58 * caller has to lock the write-buffer before calling this function.
60 * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
61 * aligned, UBIFS starts the next node from the aligned address, and the padded
62 * bytes may contain any rubbish. In other words, UBIFS does not put padding
63 * bytes in those small gaps. Common headers of nodes store real node lengths,
64 * not aligned lengths. Indexing nodes also store real lengths in branches.
66 * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
67 * uses padding nodes or padding bytes, if the padding node does not fit.
69 * All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when
70 * they are read from the flash media.
73 #include <linux/crc32.h>
74 #include <linux/slab.h>
75 #include "ubifs.h"
77 /**
78 * ubifs_ro_mode - switch UBIFS to read read-only mode.
79 * @c: UBIFS file-system description object
80 * @err: error code which is the reason of switching to R/O mode
82 void ubifs_ro_mode(struct ubifs_info *c, int err)
84 if (!c->ro_error) {
85 c->ro_error = 1;
86 c->no_chk_data_crc = 0;
87 c->vfs_sb->s_flags |= MS_RDONLY;
88 ubifs_warn("switched to read-only mode, error %d", err);
89 dbg_dump_stack();
93 /**
94 * ubifs_check_node - check node.
95 * @c: UBIFS file-system description object
96 * @buf: node to check
97 * @lnum: logical eraseblock number
98 * @offs: offset within the logical eraseblock
99 * @quiet: print no messages
100 * @must_chk_crc: indicates whether to always check the CRC
102 * This function checks node magic number and CRC checksum. This function also
103 * validates node length to prevent UBIFS from becoming crazy when an attacker
104 * feeds it a file-system image with incorrect nodes. For example, too large
105 * node length in the common header could cause UBIFS to read memory outside of
106 * allocated buffer when checking the CRC checksum.
108 * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
109 * true, which is controlled by corresponding UBIFS mount option. However, if
110 * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
111 * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
112 * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
113 * is checked. This is because during mounting or re-mounting from R/O mode to
114 * R/W mode we may read journal nodes (when replying the journal or doing the
115 * recovery) and the journal nodes may potentially be corrupted, so checking is
116 * required.
118 * This function returns zero in case of success and %-EUCLEAN in case of bad
119 * CRC or magic.
121 int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
122 int offs, int quiet, int must_chk_crc)
124 int err = -EINVAL, type, node_len;
125 uint32_t crc, node_crc, magic;
126 const struct ubifs_ch *ch = buf;
128 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
129 ubifs_assert(!(offs & 7) && offs < c->leb_size);
131 magic = le32_to_cpu(ch->magic);
132 if (magic != UBIFS_NODE_MAGIC) {
133 if (!quiet)
134 ubifs_err("bad magic %#08x, expected %#08x",
135 magic, UBIFS_NODE_MAGIC);
136 err = -EUCLEAN;
137 goto out;
140 type = ch->node_type;
141 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
142 if (!quiet)
143 ubifs_err("bad node type %d", type);
144 goto out;
147 node_len = le32_to_cpu(ch->len);
148 if (node_len + offs > c->leb_size)
149 goto out_len;
151 if (c->ranges[type].max_len == 0) {
152 if (node_len != c->ranges[type].len)
153 goto out_len;
154 } else if (node_len < c->ranges[type].min_len ||
155 node_len > c->ranges[type].max_len)
156 goto out_len;
158 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
159 !c->remounting_rw && c->no_chk_data_crc)
160 return 0;
162 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
163 node_crc = le32_to_cpu(ch->crc);
164 if (crc != node_crc) {
165 if (!quiet)
166 ubifs_err("bad CRC: calculated %#08x, read %#08x",
167 crc, node_crc);
168 err = -EUCLEAN;
169 goto out;
172 return 0;
174 out_len:
175 if (!quiet)
176 ubifs_err("bad node length %d", node_len);
177 out:
178 if (!quiet) {
179 ubifs_err("bad node at LEB %d:%d", lnum, offs);
180 dbg_dump_node(c, buf);
181 dbg_dump_stack();
183 return err;
187 * ubifs_pad - pad flash space.
188 * @c: UBIFS file-system description object
189 * @buf: buffer to put padding to
190 * @pad: how many bytes to pad
192 * The flash media obliges us to write only in chunks of %c->min_io_size and
193 * when we have to write less data we add padding node to the write-buffer and
194 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
195 * media is being scanned. If the amount of wasted space is not enough to fit a
196 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
197 * pattern (%UBIFS_PADDING_BYTE).
199 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
200 * used.
202 void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
204 uint32_t crc;
206 ubifs_assert(pad >= 0 && !(pad & 7));
208 if (pad >= UBIFS_PAD_NODE_SZ) {
209 struct ubifs_ch *ch = buf;
210 struct ubifs_pad_node *pad_node = buf;
212 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
213 ch->node_type = UBIFS_PAD_NODE;
214 ch->group_type = UBIFS_NO_NODE_GROUP;
215 ch->padding[0] = ch->padding[1] = 0;
216 ch->sqnum = 0;
217 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
218 pad -= UBIFS_PAD_NODE_SZ;
219 pad_node->pad_len = cpu_to_le32(pad);
220 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
221 ch->crc = cpu_to_le32(crc);
222 memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
223 } else if (pad > 0)
224 /* Too little space, padding node won't fit */
225 memset(buf, UBIFS_PADDING_BYTE, pad);
229 * next_sqnum - get next sequence number.
230 * @c: UBIFS file-system description object
232 static unsigned long long next_sqnum(struct ubifs_info *c)
234 unsigned long long sqnum;
236 spin_lock(&c->cnt_lock);
237 sqnum = ++c->max_sqnum;
238 spin_unlock(&c->cnt_lock);
240 if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
241 if (sqnum >= SQNUM_WATERMARK) {
242 ubifs_err("sequence number overflow %llu, end of life",
243 sqnum);
244 ubifs_ro_mode(c, -EINVAL);
246 ubifs_warn("running out of sequence numbers, end of life soon");
249 return sqnum;
253 * ubifs_prepare_node - prepare node to be written to flash.
254 * @c: UBIFS file-system description object
255 * @node: the node to pad
256 * @len: node length
257 * @pad: if the buffer has to be padded
259 * This function prepares node at @node to be written to the media - it
260 * calculates node CRC, fills the common header, and adds proper padding up to
261 * the next minimum I/O unit if @pad is not zero.
263 void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
265 uint32_t crc;
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);
273 ch->group_type = UBIFS_NO_NODE_GROUP;
274 ch->sqnum = cpu_to_le64(sqnum);
275 ch->padding[0] = ch->padding[1] = 0;
276 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
277 ch->crc = cpu_to_le32(crc);
279 if (pad) {
280 len = ALIGN(len, 8);
281 pad = ALIGN(len, c->min_io_size) - len;
282 ubifs_pad(c, node + len, pad);
287 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
288 * @c: UBIFS file-system description object
289 * @node: the node to pad
290 * @len: node length
291 * @last: indicates the last node of the group
293 * This function prepares node at @node to be written to the media - it
294 * calculates node CRC and fills the common header.
296 void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
298 uint32_t crc;
299 struct ubifs_ch *ch = node;
300 unsigned long long sqnum = next_sqnum(c);
302 ubifs_assert(len >= UBIFS_CH_SZ);
304 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
305 ch->len = cpu_to_le32(len);
306 if (last)
307 ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
308 else
309 ch->group_type = UBIFS_IN_NODE_GROUP;
310 ch->sqnum = cpu_to_le64(sqnum);
311 ch->padding[0] = ch->padding[1] = 0;
312 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
313 ch->crc = cpu_to_le32(crc);
317 * wbuf_timer_callback - write-buffer timer callback function.
318 * @data: timer data (write-buffer descriptor)
320 * This function is called when the write-buffer timer expires.
322 static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
324 struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
326 dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
327 wbuf->need_sync = 1;
328 wbuf->c->need_wbuf_sync = 1;
329 ubifs_wake_up_bgt(wbuf->c);
330 return HRTIMER_NORESTART;
334 * new_wbuf_timer - start new write-buffer timer.
335 * @wbuf: write-buffer descriptor
337 static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
339 ubifs_assert(!hrtimer_active(&wbuf->timer));
341 if (wbuf->no_timer)
342 return;
343 dbg_io("set timer for jhead %s, %llu-%llu millisecs",
344 dbg_jhead(wbuf->jhead),
345 div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC),
346 div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta,
347 USEC_PER_SEC));
348 hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta,
349 HRTIMER_MODE_REL);
353 * cancel_wbuf_timer - cancel write-buffer timer.
354 * @wbuf: write-buffer descriptor
356 static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
358 if (wbuf->no_timer)
359 return;
360 wbuf->need_sync = 0;
361 hrtimer_cancel(&wbuf->timer);
365 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
366 * @wbuf: write-buffer to synchronize
368 * This function synchronizes write-buffer @buf and returns zero in case of
369 * success or a negative error code in case of failure.
371 * Note, although write-buffers are of @c->max_write_size, this function does
372 * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
373 * if the write-buffer is only partially filled with data, only the used part
374 * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
375 * This way we waste less space.
377 int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
379 struct ubifs_info *c = wbuf->c;
380 int err, dirt, sync_len;
382 cancel_wbuf_timer_nolock(wbuf);
383 if (!wbuf->used || wbuf->lnum == -1)
384 /* Write-buffer is empty or not seeked */
385 return 0;
387 dbg_io("LEB %d:%d, %d bytes, jhead %s",
388 wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
389 ubifs_assert(!(wbuf->avail & 7));
390 ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size);
391 ubifs_assert(wbuf->size >= c->min_io_size);
392 ubifs_assert(wbuf->size <= c->max_write_size);
393 ubifs_assert(wbuf->size % c->min_io_size == 0);
394 ubifs_assert(!c->ro_media && !c->ro_mount);
395 if (c->leb_size - wbuf->offs >= c->max_write_size)
396 ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size ));
398 if (c->ro_error)
399 return -EROFS;
402 * Do not write whole write buffer but write only the minimum necessary
403 * amount of min. I/O units.
405 sync_len = ALIGN(wbuf->used, c->min_io_size);
406 dirt = sync_len - wbuf->used;
407 if (dirt)
408 ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
409 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
410 sync_len, wbuf->dtype);
411 if (err) {
412 ubifs_err("cannot write %d bytes to LEB %d:%d",
413 sync_len, wbuf->lnum, wbuf->offs);
414 dbg_dump_stack();
415 return err;
418 spin_lock(&wbuf->lock);
419 wbuf->offs += sync_len;
421 * Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
422 * But our goal is to optimize writes and make sure we write in
423 * @c->max_write_size chunks and to @c->max_write_size-aligned offset.
424 * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
425 * sure that @wbuf->offs + @wbuf->size is aligned to
426 * @c->max_write_size. This way we make sure that after next
427 * write-buffer flush we are again at the optimal offset (aligned to
428 * @c->max_write_size).
430 if (c->leb_size - wbuf->offs < c->max_write_size)
431 wbuf->size = c->leb_size - wbuf->offs;
432 else if (wbuf->offs & (c->max_write_size - 1))
433 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
434 else
435 wbuf->size = c->max_write_size;
436 wbuf->avail = wbuf->size;
437 wbuf->used = 0;
438 wbuf->next_ino = 0;
439 spin_unlock(&wbuf->lock);
441 if (wbuf->sync_callback)
442 err = wbuf->sync_callback(c, wbuf->lnum,
443 c->leb_size - wbuf->offs, dirt);
444 return err;
448 * ubifs_wbuf_seek_nolock - seek write-buffer.
449 * @wbuf: write-buffer
450 * @lnum: logical eraseblock number to seek to
451 * @offs: logical eraseblock offset to seek to
452 * @dtype: data type
454 * This function targets the write-buffer to logical eraseblock @lnum:@offs.
455 * The write-buffer is synchronized if it is not empty. Returns zero in case of
456 * success and a negative error code in case of failure.
458 int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
459 int dtype)
461 const struct ubifs_info *c = wbuf->c;
463 dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
464 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
465 ubifs_assert(offs >= 0 && offs <= c->leb_size);
466 ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
467 ubifs_assert(lnum != wbuf->lnum);
469 if (wbuf->used > 0) {
470 int err = ubifs_wbuf_sync_nolock(wbuf);
472 if (err)
473 return err;
476 spin_lock(&wbuf->lock);
477 wbuf->lnum = lnum;
478 wbuf->offs = offs;
479 if (c->leb_size - wbuf->offs < c->max_write_size)
480 wbuf->size = c->leb_size - wbuf->offs;
481 else if (wbuf->offs & (c->max_write_size - 1))
482 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
483 else
484 wbuf->size = c->max_write_size;
485 wbuf->avail = wbuf->size;
486 wbuf->used = 0;
487 spin_unlock(&wbuf->lock);
488 wbuf->dtype = dtype;
490 return 0;
494 * ubifs_bg_wbufs_sync - synchronize write-buffers.
495 * @c: UBIFS file-system description object
497 * This function is called by background thread to synchronize write-buffers.
498 * Returns zero in case of success and a negative error code in case of
499 * failure.
501 int ubifs_bg_wbufs_sync(struct ubifs_info *c)
503 int err, i;
505 ubifs_assert(!c->ro_media && !c->ro_mount);
506 if (!c->need_wbuf_sync)
507 return 0;
508 c->need_wbuf_sync = 0;
510 if (c->ro_error) {
511 err = -EROFS;
512 goto out_timers;
515 dbg_io("synchronize");
516 for (i = 0; i < c->jhead_cnt; i++) {
517 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
519 cond_resched();
522 * If the mutex is locked then wbuf is being changed, so
523 * synchronization is not necessary.
525 if (mutex_is_locked(&wbuf->io_mutex))
526 continue;
528 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
529 if (!wbuf->need_sync) {
530 mutex_unlock(&wbuf->io_mutex);
531 continue;
534 err = ubifs_wbuf_sync_nolock(wbuf);
535 mutex_unlock(&wbuf->io_mutex);
536 if (err) {
537 ubifs_err("cannot sync write-buffer, error %d", err);
538 ubifs_ro_mode(c, err);
539 goto out_timers;
543 return 0;
545 out_timers:
546 /* Cancel all timers to prevent repeated errors */
547 for (i = 0; i < c->jhead_cnt; i++) {
548 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
550 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
551 cancel_wbuf_timer_nolock(wbuf);
552 mutex_unlock(&wbuf->io_mutex);
554 return err;
558 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
559 * @wbuf: write-buffer
560 * @buf: node to write
561 * @len: node length
563 * This function writes data to flash via write-buffer @wbuf. This means that
564 * the last piece of the node won't reach the flash media immediately if it
565 * does not take whole max. write unit (@c->max_write_size). Instead, the node
566 * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
567 * because more data are appended to the write-buffer).
569 * This function returns zero in case of success and a negative error code in
570 * case of failure. If the node cannot be written because there is no more
571 * space in this logical eraseblock, %-ENOSPC is returned.
573 int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
575 struct ubifs_info *c = wbuf->c;
576 int err, written, n, aligned_len = ALIGN(len, 8), offs;
578 dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
579 dbg_ntype(((struct ubifs_ch *)buf)->node_type),
580 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
581 ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
582 ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
583 ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
584 ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size);
585 ubifs_assert(wbuf->size >= c->min_io_size);
586 ubifs_assert(wbuf->size <= c->max_write_size);
587 ubifs_assert(wbuf->size % c->min_io_size == 0);
588 ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
589 ubifs_assert(!c->ro_media && !c->ro_mount);
590 if (c->leb_size - wbuf->offs >= c->max_write_size)
591 ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size ));
593 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
594 err = -ENOSPC;
595 goto out;
598 cancel_wbuf_timer_nolock(wbuf);
600 if (c->ro_error)
601 return -EROFS;
603 if (aligned_len <= wbuf->avail) {
605 * The node is not very large and fits entirely within
606 * write-buffer.
608 memcpy(wbuf->buf + wbuf->used, buf, len);
610 if (aligned_len == wbuf->avail) {
611 dbg_io("flush jhead %s wbuf to LEB %d:%d",
612 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
613 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
614 wbuf->offs, wbuf->size,
615 wbuf->dtype);
616 if (err)
617 goto out;
619 spin_lock(&wbuf->lock);
620 wbuf->offs += wbuf->size;
621 if (c->leb_size - wbuf->offs >= c->max_write_size)
622 wbuf->size = c->max_write_size;
623 else
624 wbuf->size = c->leb_size - wbuf->offs;
625 wbuf->avail = wbuf->size;
626 wbuf->used = 0;
627 wbuf->next_ino = 0;
628 spin_unlock(&wbuf->lock);
629 } else {
630 spin_lock(&wbuf->lock);
631 wbuf->avail -= aligned_len;
632 wbuf->used += aligned_len;
633 spin_unlock(&wbuf->lock);
636 goto exit;
639 offs = wbuf->offs;
640 written = 0;
642 if (wbuf->used) {
644 * The node is large enough and does not fit entirely within
645 * current available space. We have to fill and flush
646 * write-buffer and switch to the next max. write unit.
648 dbg_io("flush jhead %s wbuf to LEB %d:%d",
649 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
650 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
651 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
652 wbuf->size, wbuf->dtype);
653 if (err)
654 goto out;
656 offs += wbuf->size;
657 len -= wbuf->avail;
658 aligned_len -= wbuf->avail;
659 written += wbuf->avail;
660 } else if (wbuf->offs & (c->max_write_size - 1)) {
662 * The write-buffer offset is not aligned to
663 * @c->max_write_size and @wbuf->size is less than
664 * @c->max_write_size. Write @wbuf->size bytes to make sure the
665 * following writes are done in optimal @c->max_write_size
666 * chunks.
668 dbg_io("write %d bytes to LEB %d:%d",
669 wbuf->size, wbuf->lnum, wbuf->offs);
670 err = ubi_leb_write(c->ubi, wbuf->lnum, buf, wbuf->offs,
671 wbuf->size, wbuf->dtype);
672 if (err)
673 goto out;
675 offs += wbuf->size;
676 len -= wbuf->size;
677 aligned_len -= wbuf->size;
678 written += wbuf->size;
682 * The remaining data may take more whole max. write units, so write the
683 * remains multiple to max. write unit size directly to the flash media.
684 * We align node length to 8-byte boundary because we anyway flash wbuf
685 * if the remaining space is less than 8 bytes.
687 n = aligned_len >> c->max_write_shift;
688 if (n) {
689 n <<= c->max_write_shift;
690 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
691 err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
692 wbuf->dtype);
693 if (err)
694 goto out;
695 offs += n;
696 aligned_len -= n;
697 len -= n;
698 written += n;
701 spin_lock(&wbuf->lock);
702 if (aligned_len)
704 * And now we have what's left and what does not take whole
705 * max. write unit, so write it to the write-buffer and we are
706 * done.
708 memcpy(wbuf->buf, buf + written, len);
710 wbuf->offs = offs;
711 if (c->leb_size - wbuf->offs >= c->max_write_size)
712 wbuf->size = c->max_write_size;
713 else
714 wbuf->size = c->leb_size - wbuf->offs;
715 wbuf->avail = wbuf->size - aligned_len;
716 wbuf->used = aligned_len;
717 wbuf->next_ino = 0;
718 spin_unlock(&wbuf->lock);
720 exit:
721 if (wbuf->sync_callback) {
722 int free = c->leb_size - wbuf->offs - wbuf->used;
724 err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
725 if (err)
726 goto out;
729 if (wbuf->used)
730 new_wbuf_timer_nolock(wbuf);
732 return 0;
734 out:
735 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
736 len, wbuf->lnum, wbuf->offs, err);
737 dbg_dump_node(c, buf);
738 dbg_dump_stack();
739 dbg_dump_leb(c, wbuf->lnum);
740 return err;
744 * ubifs_write_node - write node to the media.
745 * @c: UBIFS file-system description object
746 * @buf: the node to write
747 * @len: node length
748 * @lnum: logical eraseblock number
749 * @offs: offset within the logical eraseblock
750 * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
752 * This function automatically fills node magic number, assigns sequence
753 * number, and calculates node CRC checksum. The length of the @buf buffer has
754 * to be aligned to the minimal I/O unit size. This function automatically
755 * appends padding node and padding bytes if needed. Returns zero in case of
756 * success and a negative error code in case of failure.
758 int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
759 int offs, int dtype)
761 int err, buf_len = ALIGN(len, c->min_io_size);
763 dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
764 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
765 buf_len);
766 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
767 ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
768 ubifs_assert(!c->ro_media && !c->ro_mount);
770 if (c->ro_error)
771 return -EROFS;
773 ubifs_prepare_node(c, buf, len, 1);
774 err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
775 if (err) {
776 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
777 buf_len, lnum, offs, err);
778 dbg_dump_node(c, buf);
779 dbg_dump_stack();
782 return err;
786 * ubifs_read_node_wbuf - read node from the media or write-buffer.
787 * @wbuf: wbuf to check for un-written data
788 * @buf: buffer to read to
789 * @type: node type
790 * @len: node length
791 * @lnum: logical eraseblock number
792 * @offs: offset within the logical eraseblock
794 * This function reads a node of known type and length, checks it and stores
795 * in @buf. If the node partially or fully sits in the write-buffer, this
796 * function takes data from the buffer, otherwise it reads the flash media.
797 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
798 * error code in case of failure.
800 int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
801 int lnum, int offs)
803 const struct ubifs_info *c = wbuf->c;
804 int err, rlen, overlap;
805 struct ubifs_ch *ch = buf;
807 dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
808 dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
809 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
810 ubifs_assert(!(offs & 7) && offs < c->leb_size);
811 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
813 spin_lock(&wbuf->lock);
814 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
815 if (!overlap) {
816 /* We may safely unlock the write-buffer and read the data */
817 spin_unlock(&wbuf->lock);
818 return ubifs_read_node(c, buf, type, len, lnum, offs);
821 /* Don't read under wbuf */
822 rlen = wbuf->offs - offs;
823 if (rlen < 0)
824 rlen = 0;
826 /* Copy the rest from the write-buffer */
827 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
828 spin_unlock(&wbuf->lock);
830 if (rlen > 0) {
831 /* Read everything that goes before write-buffer */
832 err = ubi_read(c->ubi, lnum, buf, offs, rlen);
833 if (err && err != -EBADMSG) {
834 ubifs_err("failed to read node %d from LEB %d:%d, "
835 "error %d", type, lnum, offs, err);
836 dbg_dump_stack();
837 return err;
841 if (type != ch->node_type) {
842 ubifs_err("bad node type (%d but expected %d)",
843 ch->node_type, type);
844 goto out;
847 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
848 if (err) {
849 ubifs_err("expected node type %d", type);
850 return err;
853 rlen = le32_to_cpu(ch->len);
854 if (rlen != len) {
855 ubifs_err("bad node length %d, expected %d", rlen, len);
856 goto out;
859 return 0;
861 out:
862 ubifs_err("bad node at LEB %d:%d", lnum, offs);
863 dbg_dump_node(c, buf);
864 dbg_dump_stack();
865 return -EINVAL;
869 * ubifs_read_node - read node.
870 * @c: UBIFS file-system description object
871 * @buf: buffer to read to
872 * @type: node type
873 * @len: node length (not aligned)
874 * @lnum: logical eraseblock number
875 * @offs: offset within the logical eraseblock
877 * This function reads a node of known type and and length, checks it and
878 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
879 * and a negative error code in case of failure.
881 int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
882 int lnum, int offs)
884 int err, l;
885 struct ubifs_ch *ch = buf;
887 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
888 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
889 ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
890 ubifs_assert(!(offs & 7) && offs < c->leb_size);
891 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
893 err = ubi_read(c->ubi, lnum, buf, offs, len);
894 if (err && err != -EBADMSG) {
895 ubifs_err("cannot read node %d from LEB %d:%d, error %d",
896 type, lnum, offs, err);
897 return err;
900 if (type != ch->node_type) {
901 ubifs_err("bad node type (%d but expected %d)",
902 ch->node_type, type);
903 goto out;
906 err = ubifs_check_node(c, buf, lnum, offs, 0, 0);
907 if (err) {
908 ubifs_err("expected node type %d", type);
909 return err;
912 l = le32_to_cpu(ch->len);
913 if (l != len) {
914 ubifs_err("bad node length %d, expected %d", l, len);
915 goto out;
918 return 0;
920 out:
921 ubifs_err("bad node at LEB %d:%d, LEB mapping status %d", lnum, offs,
922 ubi_is_mapped(c->ubi, lnum));
923 dbg_dump_node(c, buf);
924 dbg_dump_stack();
925 return -EINVAL;
929 * ubifs_wbuf_init - initialize write-buffer.
930 * @c: UBIFS file-system description object
931 * @wbuf: write-buffer to initialize
933 * This function initializes write-buffer. Returns zero in case of success
934 * %-ENOMEM in case of failure.
936 int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
938 size_t size;
940 wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
941 if (!wbuf->buf)
942 return -ENOMEM;
944 size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
945 wbuf->inodes = kmalloc(size, GFP_KERNEL);
946 if (!wbuf->inodes) {
947 kfree(wbuf->buf);
948 wbuf->buf = NULL;
949 return -ENOMEM;
952 wbuf->used = 0;
953 wbuf->lnum = wbuf->offs = -1;
955 * If the LEB starts at the max. write size aligned address, then
956 * write-buffer size has to be set to @c->max_write_size. Otherwise,
957 * set it to something smaller so that it ends at the closest max.
958 * write size boundary.
960 size = c->max_write_size - (c->leb_start % c->max_write_size);
961 wbuf->avail = wbuf->size = size;
962 wbuf->dtype = UBI_UNKNOWN;
963 wbuf->sync_callback = NULL;
964 mutex_init(&wbuf->io_mutex);
965 spin_lock_init(&wbuf->lock);
966 wbuf->c = c;
967 wbuf->next_ino = 0;
969 hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
970 wbuf->timer.function = wbuf_timer_callback_nolock;
971 wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0);
972 wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT;
973 wbuf->delta *= 1000000000ULL;
974 ubifs_assert(wbuf->delta <= ULONG_MAX);
975 return 0;
979 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
980 * @wbuf: the write-buffer where to add
981 * @inum: the inode number
983 * This function adds an inode number to the inode array of the write-buffer.
985 void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
987 if (!wbuf->buf)
988 /* NOR flash or something similar */
989 return;
991 spin_lock(&wbuf->lock);
992 if (wbuf->used)
993 wbuf->inodes[wbuf->next_ino++] = inum;
994 spin_unlock(&wbuf->lock);
998 * wbuf_has_ino - returns if the wbuf contains data from the inode.
999 * @wbuf: the write-buffer
1000 * @inum: the inode number
1002 * This function returns with %1 if the write-buffer contains some data from the
1003 * given inode otherwise it returns with %0.
1005 static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
1007 int i, ret = 0;
1009 spin_lock(&wbuf->lock);
1010 for (i = 0; i < wbuf->next_ino; i++)
1011 if (inum == wbuf->inodes[i]) {
1012 ret = 1;
1013 break;
1015 spin_unlock(&wbuf->lock);
1017 return ret;
1021 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
1022 * @c: UBIFS file-system description object
1023 * @inode: inode to synchronize
1025 * This function synchronizes write-buffers which contain nodes belonging to
1026 * @inode. Returns zero in case of success and a negative error code in case of
1027 * failure.
1029 int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
1031 int i, err = 0;
1033 for (i = 0; i < c->jhead_cnt; i++) {
1034 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
1036 if (i == GCHD)
1038 * GC head is special, do not look at it. Even if the
1039 * head contains something related to this inode, it is
1040 * a _copy_ of corresponding on-flash node which sits
1041 * somewhere else.
1043 continue;
1045 if (!wbuf_has_ino(wbuf, inode->i_ino))
1046 continue;
1048 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1049 if (wbuf_has_ino(wbuf, inode->i_ino))
1050 err = ubifs_wbuf_sync_nolock(wbuf);
1051 mutex_unlock(&wbuf->io_mutex);
1053 if (err) {
1054 ubifs_ro_mode(c, err);
1055 return err;
1058 return 0;