• Home
  • Raw
  • Download

Lines Matching +full:write +full:- +full:to +full:- +full:write

1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2006-2008 Nokia Corporation.
14 * This file implements UBIFS I/O subsystem which provides various I/O-related
16 * write-buffering support. Write buffers help to save space which otherwise
17 * would have been wasted for padding to the nearest minimal I/O unit boundary.
18 * Instead, data first goes to the write-buffer and is flushed when the
20 * similar to the mechanism is used by JFFS2.
22 * UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
23 * write size (@c->max_write_size). The latter is the maximum amount of bytes
24 * the underlying flash is able to program at a time, and writing in
25 * @c->max_write_size units should presumably be faster. Obviously,
26 * @c->min_io_size <= @c->max_write_size. Write-buffers are of
27 * @c->max_write_size bytes in size for maximum performance. However, when a
28 * write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
29 * boundary) which contains data is written, not the whole write-buffer,
30 * because this is more space-efficient.
32 * This optimization adds few complications to the code. Indeed, on the one
33 * hand, we want to write in optimal @c->max_write_size bytes chunks, which
34 * also means aligning writes at the @c->max_write_size bytes offsets. On the
35 * other hand, we do not want to waste space when synchronizing the write
37 * the next write offset to be not aligned to @c->max_write_size bytes. So the
38 * have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
39 * to @c->max_write_size bytes again. We do this by temporarily shrinking
40 * write-buffer size (@wbuf->size).
42 * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
43 * mutexes defined inside these objects. Since sometimes upper-level code
44 * has to lock the write-buffer (e.g. journal space reservation code), many
45 * functions related to write-buffers have "nolock" suffix which means that the
46 * caller has to lock the write-buffer before calling this function.
48 * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
54 * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
66 * ubifs_ro_mode - switch UBIFS to read read-only mode.
67 * @c: UBIFS file-system description object
68 * @err: error code which is the reason of switching to R/O mode
72 if (!c->ro_error) { in ubifs_ro_mode()
73 c->ro_error = 1; in ubifs_ro_mode()
74 c->no_chk_data_crc = 0; in ubifs_ro_mode()
75 c->vfs_sb->s_flags |= SB_RDONLY; in ubifs_ro_mode()
76 ubifs_warn(c, "switched to read-only mode, error %d", err); in ubifs_ro_mode()
92 err = ubi_read(c->ubi, lnum, buf, offs, len); in ubifs_leb_read()
94 * In case of %-EBADMSG print the error message only if the in ubifs_leb_read()
97 if (err && (err != -EBADMSG || even_ebadmsg)) { in ubifs_leb_read()
110 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_leb_write()
111 if (c->ro_error) in ubifs_leb_write()
112 return -EROFS; in ubifs_leb_write()
114 err = ubi_leb_write(c->ubi, lnum, buf, offs, len); in ubifs_leb_write()
118 ubifs_err(c, "writing %d bytes to LEB %d:%d failed, error %d", in ubifs_leb_write()
130 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_leb_change()
131 if (c->ro_error) in ubifs_leb_change()
132 return -EROFS; in ubifs_leb_change()
134 err = ubi_leb_change(c->ubi, lnum, buf, len); in ubifs_leb_change()
150 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_leb_unmap()
151 if (c->ro_error) in ubifs_leb_unmap()
152 return -EROFS; in ubifs_leb_unmap()
154 err = ubi_leb_unmap(c->ubi, lnum); in ubifs_leb_unmap()
169 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_leb_map()
170 if (c->ro_error) in ubifs_leb_map()
171 return -EROFS; in ubifs_leb_map()
173 err = ubi_leb_map(c->ubi, lnum); in ubifs_leb_map()
188 err = ubi_is_mapped(c->ubi, lnum); in ubifs_is_mapped()
198 * ubifs_check_node - check node.
199 * @c: UBIFS file-system description object
200 * @buf: node to check
205 * @must_chk_crc: indicates whether to always check the CRC
208 * validates node length to prevent UBIFS from becoming crazy when an attacker
209 * feeds it a file-system image with incorrect nodes. For example, too large
210 * node length in the common header could cause UBIFS to read memory outside of
213 * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
215 * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
216 * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
217 * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
218 * is checked. This is because during mounting or re-mounting from R/O mode to
223 * This function returns zero in case of success and %-EUCLEAN in case of bad
229 int err = -EINVAL, type, node_len; in ubifs_check_node()
233 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_check_node()
234 ubifs_assert(c, !(offs & 7) && offs < c->leb_size); in ubifs_check_node()
236 magic = le32_to_cpu(ch->magic); in ubifs_check_node()
241 err = -EUCLEAN; in ubifs_check_node()
245 type = ch->node_type; in ubifs_check_node()
252 node_len = le32_to_cpu(ch->len); in ubifs_check_node()
253 if (node_len + offs > c->leb_size) in ubifs_check_node()
256 if (c->ranges[type].max_len == 0) { in ubifs_check_node()
257 if (node_len != c->ranges[type].len) in ubifs_check_node()
259 } else if (node_len < c->ranges[type].min_len || in ubifs_check_node()
260 node_len > c->ranges[type].max_len) in ubifs_check_node()
263 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting && in ubifs_check_node()
264 !c->remounting_rw && c->no_chk_data_crc) in ubifs_check_node()
267 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); in ubifs_check_node()
268 node_crc = le32_to_cpu(ch->crc); in ubifs_check_node()
273 err = -EUCLEAN; in ubifs_check_node()
292 * ubifs_pad - pad flash space.
293 * @c: UBIFS file-system description object
294 * @buf: buffer to put padding to
295 * @pad: how many bytes to pad
297 * The flash media obliges us to write only in chunks of %c->min_io_size and
298 * when we have to write less data we add padding node to the write-buffer and
299 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
300 * media is being scanned. If the amount of wasted space is not enough to fit a
301 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
304 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
317 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_pad()
318 ch->node_type = UBIFS_PAD_NODE; in ubifs_pad()
319 ch->group_type = UBIFS_NO_NODE_GROUP; in ubifs_pad()
320 ch->padding[0] = ch->padding[1] = 0; in ubifs_pad()
321 ch->sqnum = 0; in ubifs_pad()
322 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ); in ubifs_pad()
323 pad -= UBIFS_PAD_NODE_SZ; in ubifs_pad()
324 pad_node->pad_len = cpu_to_le32(pad); in ubifs_pad()
325 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8); in ubifs_pad()
326 ch->crc = cpu_to_le32(crc); in ubifs_pad()
334 * next_sqnum - get next sequence number.
335 * @c: UBIFS file-system description object
341 spin_lock(&c->cnt_lock); in next_sqnum()
342 sqnum = ++c->max_sqnum; in next_sqnum()
343 spin_unlock(&c->cnt_lock); in next_sqnum()
349 ubifs_ro_mode(c, -EINVAL); in next_sqnum()
364 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_init_node()
365 ch->len = cpu_to_le32(len); in ubifs_init_node()
366 ch->group_type = UBIFS_NO_NODE_GROUP; in ubifs_init_node()
367 ch->sqnum = cpu_to_le64(sqnum); in ubifs_init_node()
368 ch->padding[0] = ch->padding[1] = 0; in ubifs_init_node()
372 pad = ALIGN(len, c->min_io_size) - len; in ubifs_init_node()
382 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); in ubifs_crc_node()
383 ch->crc = cpu_to_le32(crc); in ubifs_crc_node()
387 * ubifs_prepare_node_hmac - prepare node to be written to flash.
388 * @c: UBIFS file-system description object
389 * @node: the node to pad
392 * @pad: if the buffer has to be padded
394 * This function prepares node at @node to be written to the media - it
395 * calculates node CRC, fills the common header, and adds proper padding up to
420 * ubifs_prepare_node - prepare node to be written to flash.
421 * @c: UBIFS file-system description object
422 * @node: the node to pad
424 * @pad: if the buffer has to be padded
426 * This function prepares node at @node to be written to the media - it
427 * calculates node CRC, fills the common header, and adds proper padding up to
440 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
441 * @c: UBIFS file-system description object
442 * @node: the node to pad
446 * This function prepares node at @node to be written to the media - it
457 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_prep_grp_node()
458 ch->len = cpu_to_le32(len); in ubifs_prep_grp_node()
460 ch->group_type = UBIFS_LAST_OF_NODE_GROUP; in ubifs_prep_grp_node()
462 ch->group_type = UBIFS_IN_NODE_GROUP; in ubifs_prep_grp_node()
463 ch->sqnum = cpu_to_le64(sqnum); in ubifs_prep_grp_node()
464 ch->padding[0] = ch->padding[1] = 0; in ubifs_prep_grp_node()
465 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); in ubifs_prep_grp_node()
466 ch->crc = cpu_to_le32(crc); in ubifs_prep_grp_node()
470 * wbuf_timer_callback - write-buffer timer callback function.
471 * @timer: timer data (write-buffer descriptor)
473 * This function is called when the write-buffer timer expires.
479 dbg_io("jhead %s", dbg_jhead(wbuf->jhead)); in wbuf_timer_callback_nolock()
480 wbuf->need_sync = 1; in wbuf_timer_callback_nolock()
481 wbuf->c->need_wbuf_sync = 1; in wbuf_timer_callback_nolock()
482 ubifs_wake_up_bgt(wbuf->c); in wbuf_timer_callback_nolock()
487 * new_wbuf_timer - start new write-buffer timer.
488 * @c: UBIFS file-system description object
489 * @wbuf: write-buffer descriptor
496 /* centi to milli, milli to nano, then 10% */ in new_wbuf_timer_nolock()
499 ubifs_assert(c, !hrtimer_active(&wbuf->timer)); in new_wbuf_timer_nolock()
502 if (wbuf->no_timer) in new_wbuf_timer_nolock()
504 dbg_io("set timer for jhead %s, %llu-%llu millisecs", in new_wbuf_timer_nolock()
505 dbg_jhead(wbuf->jhead), in new_wbuf_timer_nolock()
508 hrtimer_start_range_ns(&wbuf->timer, softlimit, delta, in new_wbuf_timer_nolock()
513 * cancel_wbuf_timer - cancel write-buffer timer.
514 * @wbuf: write-buffer descriptor
518 if (wbuf->no_timer) in cancel_wbuf_timer_nolock()
520 wbuf->need_sync = 0; in cancel_wbuf_timer_nolock()
521 hrtimer_cancel(&wbuf->timer); in cancel_wbuf_timer_nolock()
525 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
526 * @wbuf: write-buffer to synchronize
528 * This function synchronizes write-buffer @buf and returns zero in case of
531 * Note, although write-buffers are of @c->max_write_size, this function does
532 * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
533 * if the write-buffer is only partially filled with data, only the used part
534 * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
539 struct ubifs_info *c = wbuf->c; in ubifs_wbuf_sync_nolock()
543 if (!wbuf->used || wbuf->lnum == -1) in ubifs_wbuf_sync_nolock()
544 /* Write-buffer is empty or not seeked */ in ubifs_wbuf_sync_nolock()
548 wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead)); in ubifs_wbuf_sync_nolock()
549 ubifs_assert(c, !(wbuf->avail & 7)); in ubifs_wbuf_sync_nolock()
550 ubifs_assert(c, wbuf->offs + wbuf->size <= c->leb_size); in ubifs_wbuf_sync_nolock()
551 ubifs_assert(c, wbuf->size >= c->min_io_size); in ubifs_wbuf_sync_nolock()
552 ubifs_assert(c, wbuf->size <= c->max_write_size); in ubifs_wbuf_sync_nolock()
553 ubifs_assert(c, wbuf->size % c->min_io_size == 0); in ubifs_wbuf_sync_nolock()
554 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_wbuf_sync_nolock()
555 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_sync_nolock()
556 ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size)); in ubifs_wbuf_sync_nolock()
558 if (c->ro_error) in ubifs_wbuf_sync_nolock()
559 return -EROFS; in ubifs_wbuf_sync_nolock()
562 * Do not write whole write buffer but write only the minimum necessary in ubifs_wbuf_sync_nolock()
565 sync_len = ALIGN(wbuf->used, c->min_io_size); in ubifs_wbuf_sync_nolock()
566 dirt = sync_len - wbuf->used; in ubifs_wbuf_sync_nolock()
568 ubifs_pad(c, wbuf->buf + wbuf->used, dirt); in ubifs_wbuf_sync_nolock()
569 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len); in ubifs_wbuf_sync_nolock()
573 spin_lock(&wbuf->lock); in ubifs_wbuf_sync_nolock()
574 wbuf->offs += sync_len; in ubifs_wbuf_sync_nolock()
576 * Now @wbuf->offs is not necessarily aligned to @c->max_write_size. in ubifs_wbuf_sync_nolock()
577 * But our goal is to optimize writes and make sure we write in in ubifs_wbuf_sync_nolock()
578 * @c->max_write_size chunks and to @c->max_write_size-aligned offset. in ubifs_wbuf_sync_nolock()
579 * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make in ubifs_wbuf_sync_nolock()
580 * sure that @wbuf->offs + @wbuf->size is aligned to in ubifs_wbuf_sync_nolock()
581 * @c->max_write_size. This way we make sure that after next in ubifs_wbuf_sync_nolock()
582 * write-buffer flush we are again at the optimal offset (aligned to in ubifs_wbuf_sync_nolock()
583 * @c->max_write_size). in ubifs_wbuf_sync_nolock()
585 if (c->leb_size - wbuf->offs < c->max_write_size) in ubifs_wbuf_sync_nolock()
586 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_sync_nolock()
587 else if (wbuf->offs & (c->max_write_size - 1)) in ubifs_wbuf_sync_nolock()
588 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs; in ubifs_wbuf_sync_nolock()
590 wbuf->size = c->max_write_size; in ubifs_wbuf_sync_nolock()
591 wbuf->avail = wbuf->size; in ubifs_wbuf_sync_nolock()
592 wbuf->used = 0; in ubifs_wbuf_sync_nolock()
593 wbuf->next_ino = 0; in ubifs_wbuf_sync_nolock()
594 spin_unlock(&wbuf->lock); in ubifs_wbuf_sync_nolock()
596 if (wbuf->sync_callback) in ubifs_wbuf_sync_nolock()
597 err = wbuf->sync_callback(c, wbuf->lnum, in ubifs_wbuf_sync_nolock()
598 c->leb_size - wbuf->offs, dirt); in ubifs_wbuf_sync_nolock()
603 * ubifs_wbuf_seek_nolock - seek write-buffer.
604 * @wbuf: write-buffer
605 * @lnum: logical eraseblock number to seek to
606 * @offs: logical eraseblock offset to seek to
608 * This function targets the write-buffer to logical eraseblock @lnum:@offs.
609 * The write-buffer has to be empty. Returns zero in case of success and a
614 const struct ubifs_info *c = wbuf->c; in ubifs_wbuf_seek_nolock()
616 dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead)); in ubifs_wbuf_seek_nolock()
617 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt); in ubifs_wbuf_seek_nolock()
618 ubifs_assert(c, offs >= 0 && offs <= c->leb_size); in ubifs_wbuf_seek_nolock()
619 ubifs_assert(c, offs % c->min_io_size == 0 && !(offs & 7)); in ubifs_wbuf_seek_nolock()
620 ubifs_assert(c, lnum != wbuf->lnum); in ubifs_wbuf_seek_nolock()
621 ubifs_assert(c, wbuf->used == 0); in ubifs_wbuf_seek_nolock()
623 spin_lock(&wbuf->lock); in ubifs_wbuf_seek_nolock()
624 wbuf->lnum = lnum; in ubifs_wbuf_seek_nolock()
625 wbuf->offs = offs; in ubifs_wbuf_seek_nolock()
626 if (c->leb_size - wbuf->offs < c->max_write_size) in ubifs_wbuf_seek_nolock()
627 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_seek_nolock()
628 else if (wbuf->offs & (c->max_write_size - 1)) in ubifs_wbuf_seek_nolock()
629 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs; in ubifs_wbuf_seek_nolock()
631 wbuf->size = c->max_write_size; in ubifs_wbuf_seek_nolock()
632 wbuf->avail = wbuf->size; in ubifs_wbuf_seek_nolock()
633 wbuf->used = 0; in ubifs_wbuf_seek_nolock()
634 spin_unlock(&wbuf->lock); in ubifs_wbuf_seek_nolock()
640 * ubifs_bg_wbufs_sync - synchronize write-buffers.
641 * @c: UBIFS file-system description object
643 * This function is called by background thread to synchronize write-buffers.
651 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_bg_wbufs_sync()
652 if (!c->need_wbuf_sync) in ubifs_bg_wbufs_sync()
654 c->need_wbuf_sync = 0; in ubifs_bg_wbufs_sync()
656 if (c->ro_error) { in ubifs_bg_wbufs_sync()
657 err = -EROFS; in ubifs_bg_wbufs_sync()
662 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_bg_wbufs_sync()
663 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_bg_wbufs_sync()
671 if (mutex_is_locked(&wbuf->io_mutex)) in ubifs_bg_wbufs_sync()
674 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_bg_wbufs_sync()
675 if (!wbuf->need_sync) { in ubifs_bg_wbufs_sync()
676 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
681 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
683 ubifs_err(c, "cannot sync write-buffer, error %d", err); in ubifs_bg_wbufs_sync()
692 /* Cancel all timers to prevent repeated errors */ in ubifs_bg_wbufs_sync()
693 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_bg_wbufs_sync()
694 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_bg_wbufs_sync()
696 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_bg_wbufs_sync()
698 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
704 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
705 * @wbuf: write-buffer
706 * @buf: node to write
709 * This function writes data to flash via write-buffer @wbuf. This means that
711 * does not take whole max. write unit (@c->max_write_size). Instead, the node
712 * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
713 * because more data are appended to the write-buffer).
717 * space in this logical eraseblock, %-ENOSPC is returned.
721 struct ubifs_info *c = wbuf->c; in ubifs_wbuf_write_nolock()
724 dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len, in ubifs_wbuf_write_nolock()
725 dbg_ntype(((struct ubifs_ch *)buf)->node_type), in ubifs_wbuf_write_nolock()
726 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used); in ubifs_wbuf_write_nolock()
727 ubifs_assert(c, len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt); in ubifs_wbuf_write_nolock()
728 ubifs_assert(c, wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0); in ubifs_wbuf_write_nolock()
729 ubifs_assert(c, !(wbuf->offs & 7) && wbuf->offs <= c->leb_size); in ubifs_wbuf_write_nolock()
730 ubifs_assert(c, wbuf->avail > 0 && wbuf->avail <= wbuf->size); in ubifs_wbuf_write_nolock()
731 ubifs_assert(c, wbuf->size >= c->min_io_size); in ubifs_wbuf_write_nolock()
732 ubifs_assert(c, wbuf->size <= c->max_write_size); in ubifs_wbuf_write_nolock()
733 ubifs_assert(c, wbuf->size % c->min_io_size == 0); in ubifs_wbuf_write_nolock()
734 ubifs_assert(c, mutex_is_locked(&wbuf->io_mutex)); in ubifs_wbuf_write_nolock()
735 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_wbuf_write_nolock()
736 ubifs_assert(c, !c->space_fixup); in ubifs_wbuf_write_nolock()
737 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
738 ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size)); in ubifs_wbuf_write_nolock()
740 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) { in ubifs_wbuf_write_nolock()
741 err = -ENOSPC; in ubifs_wbuf_write_nolock()
747 if (c->ro_error) in ubifs_wbuf_write_nolock()
748 return -EROFS; in ubifs_wbuf_write_nolock()
750 if (aligned_len <= wbuf->avail) { in ubifs_wbuf_write_nolock()
753 * write-buffer. in ubifs_wbuf_write_nolock()
755 memcpy(wbuf->buf + wbuf->used, buf, len); in ubifs_wbuf_write_nolock()
757 ubifs_assert(c, aligned_len - len < 8); in ubifs_wbuf_write_nolock()
758 ubifs_pad(c, wbuf->buf + wbuf->used + len, aligned_len - len); in ubifs_wbuf_write_nolock()
761 if (aligned_len == wbuf->avail) { in ubifs_wbuf_write_nolock()
762 dbg_io("flush jhead %s wbuf to LEB %d:%d", in ubifs_wbuf_write_nolock()
763 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
764 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, in ubifs_wbuf_write_nolock()
765 wbuf->offs, wbuf->size); in ubifs_wbuf_write_nolock()
769 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
770 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
771 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
772 wbuf->size = c->max_write_size; in ubifs_wbuf_write_nolock()
774 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_write_nolock()
775 wbuf->avail = wbuf->size; in ubifs_wbuf_write_nolock()
776 wbuf->used = 0; in ubifs_wbuf_write_nolock()
777 wbuf->next_ino = 0; in ubifs_wbuf_write_nolock()
778 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
780 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
781 wbuf->avail -= aligned_len; in ubifs_wbuf_write_nolock()
782 wbuf->used += aligned_len; in ubifs_wbuf_write_nolock()
783 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
789 if (wbuf->used) { in ubifs_wbuf_write_nolock()
792 * current available space. We have to fill and flush in ubifs_wbuf_write_nolock()
793 * write-buffer and switch to the next max. write unit. in ubifs_wbuf_write_nolock()
795 dbg_io("flush jhead %s wbuf to LEB %d:%d", in ubifs_wbuf_write_nolock()
796 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
797 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail); in ubifs_wbuf_write_nolock()
798 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, in ubifs_wbuf_write_nolock()
799 wbuf->size); in ubifs_wbuf_write_nolock()
803 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
804 len -= wbuf->avail; in ubifs_wbuf_write_nolock()
805 aligned_len -= wbuf->avail; in ubifs_wbuf_write_nolock()
806 written += wbuf->avail; in ubifs_wbuf_write_nolock()
807 } else if (wbuf->offs & (c->max_write_size - 1)) { in ubifs_wbuf_write_nolock()
809 * The write-buffer offset is not aligned to in ubifs_wbuf_write_nolock()
810 * @c->max_write_size and @wbuf->size is less than in ubifs_wbuf_write_nolock()
811 * @c->max_write_size. Write @wbuf->size bytes to make sure the in ubifs_wbuf_write_nolock()
812 * following writes are done in optimal @c->max_write_size in ubifs_wbuf_write_nolock()
815 dbg_io("write %d bytes to LEB %d:%d", in ubifs_wbuf_write_nolock()
816 wbuf->size, wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
817 err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs, in ubifs_wbuf_write_nolock()
818 wbuf->size); in ubifs_wbuf_write_nolock()
822 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
823 len -= wbuf->size; in ubifs_wbuf_write_nolock()
824 aligned_len -= wbuf->size; in ubifs_wbuf_write_nolock()
825 written += wbuf->size; in ubifs_wbuf_write_nolock()
829 * The remaining data may take more whole max. write units, so write the in ubifs_wbuf_write_nolock()
830 * remains multiple to max. write unit size directly to the flash media. in ubifs_wbuf_write_nolock()
831 * We align node length to 8-byte boundary because we anyway flash wbuf in ubifs_wbuf_write_nolock()
834 n = aligned_len >> c->max_write_shift; in ubifs_wbuf_write_nolock()
836 int m = n - 1; in ubifs_wbuf_write_nolock()
838 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, in ubifs_wbuf_write_nolock()
839 wbuf->offs); in ubifs_wbuf_write_nolock()
842 /* '(n-1)<<c->max_write_shift < len' is always true. */ in ubifs_wbuf_write_nolock()
843 m <<= c->max_write_shift; in ubifs_wbuf_write_nolock()
844 err = ubifs_leb_write(c, wbuf->lnum, buf + written, in ubifs_wbuf_write_nolock()
845 wbuf->offs, m); in ubifs_wbuf_write_nolock()
848 wbuf->offs += m; in ubifs_wbuf_write_nolock()
849 aligned_len -= m; in ubifs_wbuf_write_nolock()
850 len -= m; in ubifs_wbuf_write_nolock()
855 * The non-written len of buf may be less than 'n' because in ubifs_wbuf_write_nolock()
859 n = 1 << c->max_write_shift; in ubifs_wbuf_write_nolock()
860 memcpy(wbuf->buf, buf + written, min(len, n)); in ubifs_wbuf_write_nolock()
862 ubifs_assert(c, n - len < 8); in ubifs_wbuf_write_nolock()
863 ubifs_pad(c, wbuf->buf + len, n - len); in ubifs_wbuf_write_nolock()
866 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, n); in ubifs_wbuf_write_nolock()
869 wbuf->offs += n; in ubifs_wbuf_write_nolock()
870 aligned_len -= n; in ubifs_wbuf_write_nolock()
871 len -= min(len, n); in ubifs_wbuf_write_nolock()
875 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
879 * max. write unit, so write it to the write-buffer and we are in ubifs_wbuf_write_nolock()
882 memcpy(wbuf->buf, buf + written, len); in ubifs_wbuf_write_nolock()
884 ubifs_assert(c, aligned_len - len < 8); in ubifs_wbuf_write_nolock()
885 ubifs_pad(c, wbuf->buf + len, aligned_len - len); in ubifs_wbuf_write_nolock()
889 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
890 wbuf->size = c->max_write_size; in ubifs_wbuf_write_nolock()
892 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_write_nolock()
893 wbuf->avail = wbuf->size - aligned_len; in ubifs_wbuf_write_nolock()
894 wbuf->used = aligned_len; in ubifs_wbuf_write_nolock()
895 wbuf->next_ino = 0; in ubifs_wbuf_write_nolock()
896 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
899 if (wbuf->sync_callback) { in ubifs_wbuf_write_nolock()
900 int free = c->leb_size - wbuf->offs - wbuf->used; in ubifs_wbuf_write_nolock()
902 err = wbuf->sync_callback(c, wbuf->lnum, free, 0); in ubifs_wbuf_write_nolock()
907 if (wbuf->used) in ubifs_wbuf_write_nolock()
913 ubifs_err(c, "cannot write %d bytes to LEB %d:%d, error %d", in ubifs_wbuf_write_nolock()
914 len, wbuf->lnum, wbuf->offs, err); in ubifs_wbuf_write_nolock()
917 ubifs_dump_leb(c, wbuf->lnum); in ubifs_wbuf_write_nolock()
922 * ubifs_write_node_hmac - write node to the media.
923 * @c: UBIFS file-system description object
924 * @buf: the node to write
932 * to be aligned to the minimal I/O unit size. This function automatically
939 int err, buf_len = ALIGN(len, c->min_io_size); in ubifs_write_node_hmac()
942 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len, in ubifs_write_node_hmac()
944 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_write_node_hmac()
945 ubifs_assert(c, offs % c->min_io_size == 0 && offs < c->leb_size); in ubifs_write_node_hmac()
946 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_write_node_hmac()
947 ubifs_assert(c, !c->space_fixup); in ubifs_write_node_hmac()
949 if (c->ro_error) in ubifs_write_node_hmac()
950 return -EROFS; in ubifs_write_node_hmac()
964 * ubifs_write_node - write node to the media.
965 * @c: UBIFS file-system description object
966 * @buf: the node to write
973 * to be aligned to the minimal I/O unit size. This function automatically
980 return ubifs_write_node_hmac(c, buf, len, lnum, offs, -1); in ubifs_write_node()
984 * ubifs_read_node_wbuf - read node from the media or write-buffer.
985 * @wbuf: wbuf to check for un-written data
986 * @buf: buffer to read to
993 * in @buf. If the node partially or fully sits in the write-buffer, this
995 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
1001 const struct ubifs_info *c = wbuf->c; in ubifs_read_node_wbuf()
1006 dbg_ntype(type), len, dbg_jhead(wbuf->jhead)); in ubifs_read_node_wbuf()
1007 ubifs_assert(c, wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_read_node_wbuf()
1008 ubifs_assert(c, !(offs & 7) && offs < c->leb_size); in ubifs_read_node_wbuf()
1011 spin_lock(&wbuf->lock); in ubifs_read_node_wbuf()
1012 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); in ubifs_read_node_wbuf()
1014 /* We may safely unlock the write-buffer and read the data */ in ubifs_read_node_wbuf()
1015 spin_unlock(&wbuf->lock); in ubifs_read_node_wbuf()
1020 rlen = wbuf->offs - offs; in ubifs_read_node_wbuf()
1024 /* Copy the rest from the write-buffer */ in ubifs_read_node_wbuf()
1025 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen); in ubifs_read_node_wbuf()
1026 spin_unlock(&wbuf->lock); in ubifs_read_node_wbuf()
1029 /* Read everything that goes before write-buffer */ in ubifs_read_node_wbuf()
1031 if (err && err != -EBADMSG) in ubifs_read_node_wbuf()
1035 if (type != ch->node_type) { in ubifs_read_node_wbuf()
1037 ch->node_type, type); in ubifs_read_node_wbuf()
1047 rlen = le32_to_cpu(ch->len); in ubifs_read_node_wbuf()
1059 return -EINVAL; in ubifs_read_node_wbuf()
1063 * ubifs_read_node - read node.
1064 * @c: UBIFS file-system description object
1065 * @buf: buffer to read to
1072 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
1082 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_read_node()
1083 ubifs_assert(c, len >= UBIFS_CH_SZ && offs + len <= c->leb_size); in ubifs_read_node()
1084 ubifs_assert(c, !(offs & 7) && offs < c->leb_size); in ubifs_read_node()
1088 if (err && err != -EBADMSG) in ubifs_read_node()
1091 if (type != ch->node_type) { in ubifs_read_node()
1093 ch->node_type, type); in ubifs_read_node()
1103 l = le32_to_cpu(ch->len); in ubifs_read_node()
1113 offs, ubi_is_mapped(c->ubi, lnum)); in ubifs_read_node()
1114 if (!c->probing) { in ubifs_read_node()
1118 return -EINVAL; in ubifs_read_node()
1122 * ubifs_wbuf_init - initialize write-buffer.
1123 * @c: UBIFS file-system description object
1124 * @wbuf: write-buffer to initialize
1126 * This function initializes write-buffer. Returns zero in case of success
1127 * %-ENOMEM in case of failure.
1133 wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL); in ubifs_wbuf_init()
1134 if (!wbuf->buf) in ubifs_wbuf_init()
1135 return -ENOMEM; in ubifs_wbuf_init()
1137 size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t); in ubifs_wbuf_init()
1138 wbuf->inodes = kmalloc(size, GFP_KERNEL); in ubifs_wbuf_init()
1139 if (!wbuf->inodes) { in ubifs_wbuf_init()
1140 kfree(wbuf->buf); in ubifs_wbuf_init()
1141 wbuf->buf = NULL; in ubifs_wbuf_init()
1142 return -ENOMEM; in ubifs_wbuf_init()
1145 wbuf->used = 0; in ubifs_wbuf_init()
1146 wbuf->lnum = wbuf->offs = -1; in ubifs_wbuf_init()
1148 * If the LEB starts at the max. write size aligned address, then in ubifs_wbuf_init()
1149 * write-buffer size has to be set to @c->max_write_size. Otherwise, in ubifs_wbuf_init()
1150 * set it to something smaller so that it ends at the closest max. in ubifs_wbuf_init()
1151 * write size boundary. in ubifs_wbuf_init()
1153 size = c->max_write_size - (c->leb_start % c->max_write_size); in ubifs_wbuf_init()
1154 wbuf->avail = wbuf->size = size; in ubifs_wbuf_init()
1155 wbuf->sync_callback = NULL; in ubifs_wbuf_init()
1156 mutex_init(&wbuf->io_mutex); in ubifs_wbuf_init()
1157 spin_lock_init(&wbuf->lock); in ubifs_wbuf_init()
1158 wbuf->c = c; in ubifs_wbuf_init()
1159 wbuf->next_ino = 0; in ubifs_wbuf_init()
1161 hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in ubifs_wbuf_init()
1162 wbuf->timer.function = wbuf_timer_callback_nolock; in ubifs_wbuf_init()
1167 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
1168 * @wbuf: the write-buffer where to add
1171 * This function adds an inode number to the inode array of the write-buffer.
1175 if (!wbuf->buf) in ubifs_wbuf_add_ino_nolock()
1179 spin_lock(&wbuf->lock); in ubifs_wbuf_add_ino_nolock()
1180 if (wbuf->used) in ubifs_wbuf_add_ino_nolock()
1181 wbuf->inodes[wbuf->next_ino++] = inum; in ubifs_wbuf_add_ino_nolock()
1182 spin_unlock(&wbuf->lock); in ubifs_wbuf_add_ino_nolock()
1186 * wbuf_has_ino - returns if the wbuf contains data from the inode.
1187 * @wbuf: the write-buffer
1190 * This function returns with %1 if the write-buffer contains some data from the
1197 spin_lock(&wbuf->lock); in wbuf_has_ino()
1198 for (i = 0; i < wbuf->next_ino; i++) in wbuf_has_ino()
1199 if (inum == wbuf->inodes[i]) { in wbuf_has_ino()
1203 spin_unlock(&wbuf->lock); in wbuf_has_ino()
1209 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
1210 * @c: UBIFS file-system description object
1211 * @inode: inode to synchronize
1213 * This function synchronizes write-buffers which contain nodes belonging to
1221 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_sync_wbufs_by_inode()
1222 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_sync_wbufs_by_inode()
1227 * head contains something related to this inode, it is in ubifs_sync_wbufs_by_inode()
1228 * a _copy_ of corresponding on-flash node which sits in ubifs_sync_wbufs_by_inode()
1233 if (!wbuf_has_ino(wbuf, inode->i_ino)) in ubifs_sync_wbufs_by_inode()
1236 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_sync_wbufs_by_inode()
1237 if (wbuf_has_ino(wbuf, inode->i_ino)) in ubifs_sync_wbufs_by_inode()
1239 mutex_unlock(&wbuf->io_mutex); in ubifs_sync_wbufs_by_inode()