Lines Matching +full:write +full:- +full:0
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
18 * Instead, data first goes to the write-buffer and is flushed when the
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
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.
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
66 * ubifs_ro_mode - switch UBIFS to read read-only mode.
67 * @c: UBIFS file-system description object
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()
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()
189 if (err < 0) { in ubifs_is_mapped()
200 stats->magic_errors++; in record_magic_error()
206 stats->node_errors++; in record_node_error()
212 stats->crc_errors++; in record_crc_error()
216 * ubifs_check_node - check node.
217 * @c: UBIFS file-system description object
227 * feeds it a file-system image with incorrect nodes. For example, too large
231 * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
233 * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
234 * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
235 * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
236 * is checked. This is because during mounting or re-mounting from R/O mode to
241 * This function returns zero in case of success and %-EUCLEAN in case of bad
247 int err = -EINVAL, type, node_len; in ubifs_check_node()
251 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_check_node()
252 ubifs_assert(c, !(offs & 7) && offs < c->leb_size); in ubifs_check_node()
254 magic = le32_to_cpu(ch->magic); in ubifs_check_node()
259 record_magic_error(c->stats); in ubifs_check_node()
260 err = -EUCLEAN; in ubifs_check_node()
264 type = ch->node_type; in ubifs_check_node()
265 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) { in ubifs_check_node()
268 record_node_error(c->stats); in ubifs_check_node()
272 node_len = le32_to_cpu(ch->len); in ubifs_check_node()
273 if (node_len + offs > c->leb_size) in ubifs_check_node()
276 if (c->ranges[type].max_len == 0) { in ubifs_check_node()
277 if (node_len != c->ranges[type].len) in ubifs_check_node()
279 } else if (node_len < c->ranges[type].min_len || in ubifs_check_node()
280 node_len > c->ranges[type].max_len) in ubifs_check_node()
283 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting && in ubifs_check_node()
284 !c->remounting_rw && c->no_chk_data_crc) in ubifs_check_node()
285 return 0; in ubifs_check_node()
287 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); in ubifs_check_node()
288 node_crc = le32_to_cpu(ch->crc); in ubifs_check_node()
293 record_crc_error(c->stats); in ubifs_check_node()
294 err = -EUCLEAN; in ubifs_check_node()
298 return 0; in ubifs_check_node()
313 * ubifs_pad - pad flash space.
314 * @c: UBIFS file-system description object
318 * The flash media obliges us to write only in chunks of %c->min_io_size and
319 * when we have to write less data we add padding node to the write-buffer and
322 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
325 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
332 ubifs_assert(c, pad >= 0); in ubifs_pad()
338 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_pad()
339 ch->node_type = UBIFS_PAD_NODE; in ubifs_pad()
340 ch->group_type = UBIFS_NO_NODE_GROUP; in ubifs_pad()
341 ch->padding[0] = ch->padding[1] = 0; in ubifs_pad()
342 ch->sqnum = 0; in ubifs_pad()
343 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ); in ubifs_pad()
344 pad -= UBIFS_PAD_NODE_SZ; in ubifs_pad()
345 pad_node->pad_len = cpu_to_le32(pad); in ubifs_pad()
346 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8); in ubifs_pad()
347 ch->crc = cpu_to_le32(crc); in ubifs_pad()
348 memset(buf + UBIFS_PAD_NODE_SZ, 0, pad); in ubifs_pad()
349 } else if (pad > 0) in ubifs_pad()
355 * next_sqnum - get next sequence number.
356 * @c: UBIFS file-system description object
362 spin_lock(&c->cnt_lock); in next_sqnum()
363 sqnum = ++c->max_sqnum; in next_sqnum()
364 spin_unlock(&c->cnt_lock); in next_sqnum()
370 ubifs_ro_mode(c, -EINVAL); in next_sqnum()
385 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_init_node()
386 ch->len = cpu_to_le32(len); in ubifs_init_node()
387 ch->group_type = UBIFS_NO_NODE_GROUP; in ubifs_init_node()
388 ch->sqnum = cpu_to_le64(sqnum); in ubifs_init_node()
389 ch->padding[0] = ch->padding[1] = 0; in ubifs_init_node()
393 pad = ALIGN(len, c->min_io_size) - len; in ubifs_init_node()
403 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); in ubifs_crc_node()
404 ch->crc = cpu_to_le32(crc); in ubifs_crc_node()
408 * ubifs_prepare_node_hmac - prepare node to be written to flash.
409 * @c: UBIFS file-system description object
415 * This function prepares node at @node to be written to the media - it
420 * This function returns 0 for success or a negative error code otherwise.
429 if (hmac_offs > 0) { in ubifs_prepare_node_hmac()
437 return 0; in ubifs_prepare_node_hmac()
441 * ubifs_prepare_node - prepare node to be written to flash.
442 * @c: UBIFS file-system description object
447 * This function prepares node at @node to be written to the media - it
457 ubifs_prepare_node_hmac(c, node, len, 0, pad); in ubifs_prepare_node()
461 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
462 * @c: UBIFS file-system description object
467 * This function prepares node at @node to be written to the media - it
478 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_prep_grp_node()
479 ch->len = cpu_to_le32(len); in ubifs_prep_grp_node()
481 ch->group_type = UBIFS_LAST_OF_NODE_GROUP; in ubifs_prep_grp_node()
483 ch->group_type = UBIFS_IN_NODE_GROUP; in ubifs_prep_grp_node()
484 ch->sqnum = cpu_to_le64(sqnum); in ubifs_prep_grp_node()
485 ch->padding[0] = ch->padding[1] = 0; in ubifs_prep_grp_node()
486 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); in ubifs_prep_grp_node()
487 ch->crc = cpu_to_le32(crc); in ubifs_prep_grp_node()
491 * wbuf_timer_callback_nolock - write-buffer timer callback function.
492 * @timer: timer data (write-buffer descriptor)
494 * This function is called when the write-buffer timer expires.
500 dbg_io("jhead %s", dbg_jhead(wbuf->jhead)); in wbuf_timer_callback_nolock()
501 wbuf->need_sync = 1; in wbuf_timer_callback_nolock()
502 wbuf->c->need_wbuf_sync = 1; in wbuf_timer_callback_nolock()
503 ubifs_wake_up_bgt(wbuf->c); in wbuf_timer_callback_nolock()
508 * new_wbuf_timer_nolock - start new write-buffer timer.
509 * @c: UBIFS file-system description object
510 * @wbuf: write-buffer descriptor
520 ubifs_assert(c, !hrtimer_active(&wbuf->timer)); in new_wbuf_timer_nolock()
523 if (wbuf->no_timer) in new_wbuf_timer_nolock()
525 dbg_io("set timer for jhead %s, %llu-%llu millisecs", in new_wbuf_timer_nolock()
526 dbg_jhead(wbuf->jhead), in new_wbuf_timer_nolock()
529 hrtimer_start_range_ns(&wbuf->timer, softlimit, delta, in new_wbuf_timer_nolock()
534 * cancel_wbuf_timer_nolock - cancel write-buffer timer.
535 * @wbuf: write-buffer descriptor
539 if (wbuf->no_timer) in cancel_wbuf_timer_nolock()
541 wbuf->need_sync = 0; in cancel_wbuf_timer_nolock()
542 hrtimer_cancel(&wbuf->timer); in cancel_wbuf_timer_nolock()
546 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
547 * @wbuf: write-buffer to synchronize
549 * This function synchronizes write-buffer @buf and returns zero in case of
552 * Note, although write-buffers are of @c->max_write_size, this function does
553 * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
554 * if the write-buffer is only partially filled with data, only the used part
555 * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
560 struct ubifs_info *c = wbuf->c; in ubifs_wbuf_sync_nolock()
564 if (!wbuf->used || wbuf->lnum == -1) in ubifs_wbuf_sync_nolock()
565 /* Write-buffer is empty or not seeked */ in ubifs_wbuf_sync_nolock()
566 return 0; in ubifs_wbuf_sync_nolock()
569 wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead)); in ubifs_wbuf_sync_nolock()
570 ubifs_assert(c, !(wbuf->avail & 7)); in ubifs_wbuf_sync_nolock()
571 ubifs_assert(c, wbuf->offs + wbuf->size <= c->leb_size); in ubifs_wbuf_sync_nolock()
572 ubifs_assert(c, wbuf->size >= c->min_io_size); in ubifs_wbuf_sync_nolock()
573 ubifs_assert(c, wbuf->size <= c->max_write_size); in ubifs_wbuf_sync_nolock()
574 ubifs_assert(c, wbuf->size % c->min_io_size == 0); in ubifs_wbuf_sync_nolock()
575 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_wbuf_sync_nolock()
576 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_sync_nolock()
577 ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size)); in ubifs_wbuf_sync_nolock()
579 if (c->ro_error) in ubifs_wbuf_sync_nolock()
580 return -EROFS; in ubifs_wbuf_sync_nolock()
583 * Do not write whole write buffer but write only the minimum necessary in ubifs_wbuf_sync_nolock()
586 sync_len = ALIGN(wbuf->used, c->min_io_size); in ubifs_wbuf_sync_nolock()
587 dirt = sync_len - wbuf->used; in ubifs_wbuf_sync_nolock()
589 ubifs_pad(c, wbuf->buf + wbuf->used, dirt); in ubifs_wbuf_sync_nolock()
590 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len); in ubifs_wbuf_sync_nolock()
594 spin_lock(&wbuf->lock); in ubifs_wbuf_sync_nolock()
595 wbuf->offs += sync_len; in ubifs_wbuf_sync_nolock()
597 * Now @wbuf->offs is not necessarily aligned to @c->max_write_size. in ubifs_wbuf_sync_nolock()
598 * But our goal is to optimize writes and make sure we write in in ubifs_wbuf_sync_nolock()
599 * @c->max_write_size chunks and to @c->max_write_size-aligned offset. in ubifs_wbuf_sync_nolock()
600 * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make in ubifs_wbuf_sync_nolock()
601 * sure that @wbuf->offs + @wbuf->size is aligned to in ubifs_wbuf_sync_nolock()
602 * @c->max_write_size. This way we make sure that after next in ubifs_wbuf_sync_nolock()
603 * write-buffer flush we are again at the optimal offset (aligned to in ubifs_wbuf_sync_nolock()
604 * @c->max_write_size). in ubifs_wbuf_sync_nolock()
606 if (c->leb_size - wbuf->offs < c->max_write_size) in ubifs_wbuf_sync_nolock()
607 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_sync_nolock()
608 else if (wbuf->offs & (c->max_write_size - 1)) in ubifs_wbuf_sync_nolock()
609 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs; in ubifs_wbuf_sync_nolock()
611 wbuf->size = c->max_write_size; in ubifs_wbuf_sync_nolock()
612 wbuf->avail = wbuf->size; in ubifs_wbuf_sync_nolock()
613 wbuf->used = 0; in ubifs_wbuf_sync_nolock()
614 wbuf->next_ino = 0; in ubifs_wbuf_sync_nolock()
615 spin_unlock(&wbuf->lock); in ubifs_wbuf_sync_nolock()
617 if (wbuf->sync_callback) in ubifs_wbuf_sync_nolock()
618 err = wbuf->sync_callback(c, wbuf->lnum, in ubifs_wbuf_sync_nolock()
619 c->leb_size - wbuf->offs, dirt); in ubifs_wbuf_sync_nolock()
624 * ubifs_wbuf_seek_nolock - seek write-buffer.
625 * @wbuf: write-buffer
629 * This function targets the write-buffer to logical eraseblock @lnum:@offs.
630 * The write-buffer has to be empty. Returns zero in case of success and a
635 const struct ubifs_info *c = wbuf->c; in ubifs_wbuf_seek_nolock()
637 dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead)); in ubifs_wbuf_seek_nolock()
638 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt); in ubifs_wbuf_seek_nolock()
639 ubifs_assert(c, offs >= 0 && offs <= c->leb_size); in ubifs_wbuf_seek_nolock()
640 ubifs_assert(c, offs % c->min_io_size == 0 && !(offs & 7)); in ubifs_wbuf_seek_nolock()
641 ubifs_assert(c, lnum != wbuf->lnum); in ubifs_wbuf_seek_nolock()
642 ubifs_assert(c, wbuf->used == 0); in ubifs_wbuf_seek_nolock()
644 spin_lock(&wbuf->lock); in ubifs_wbuf_seek_nolock()
645 wbuf->lnum = lnum; in ubifs_wbuf_seek_nolock()
646 wbuf->offs = offs; in ubifs_wbuf_seek_nolock()
647 if (c->leb_size - wbuf->offs < c->max_write_size) in ubifs_wbuf_seek_nolock()
648 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_seek_nolock()
649 else if (wbuf->offs & (c->max_write_size - 1)) in ubifs_wbuf_seek_nolock()
650 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs; in ubifs_wbuf_seek_nolock()
652 wbuf->size = c->max_write_size; in ubifs_wbuf_seek_nolock()
653 wbuf->avail = wbuf->size; in ubifs_wbuf_seek_nolock()
654 wbuf->used = 0; in ubifs_wbuf_seek_nolock()
655 spin_unlock(&wbuf->lock); in ubifs_wbuf_seek_nolock()
657 return 0; in ubifs_wbuf_seek_nolock()
661 * ubifs_bg_wbufs_sync - synchronize write-buffers.
662 * @c: UBIFS file-system description object
664 * This function is called by background thread to synchronize write-buffers.
672 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_bg_wbufs_sync()
673 if (!c->need_wbuf_sync) in ubifs_bg_wbufs_sync()
674 return 0; in ubifs_bg_wbufs_sync()
675 c->need_wbuf_sync = 0; in ubifs_bg_wbufs_sync()
677 if (c->ro_error) { in ubifs_bg_wbufs_sync()
678 err = -EROFS; in ubifs_bg_wbufs_sync()
683 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_bg_wbufs_sync()
684 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_bg_wbufs_sync()
692 if (mutex_is_locked(&wbuf->io_mutex)) in ubifs_bg_wbufs_sync()
695 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_bg_wbufs_sync()
696 if (!wbuf->need_sync) { in ubifs_bg_wbufs_sync()
697 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
702 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
704 ubifs_err(c, "cannot sync write-buffer, error %d", err); in ubifs_bg_wbufs_sync()
710 return 0; in ubifs_bg_wbufs_sync()
714 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_bg_wbufs_sync()
715 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_bg_wbufs_sync()
717 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_bg_wbufs_sync()
719 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
725 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
726 * @wbuf: write-buffer
727 * @buf: node to write
730 * This function writes data to flash via write-buffer @wbuf. This means that
732 * does not take whole max. write unit (@c->max_write_size). Instead, the node
733 * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
734 * because more data are appended to the write-buffer).
738 * space in this logical eraseblock, %-ENOSPC is returned.
742 struct ubifs_info *c = wbuf->c; in ubifs_wbuf_write_nolock()
743 int err, n, written = 0, aligned_len = ALIGN(len, 8); in ubifs_wbuf_write_nolock()
746 dbg_ntype(((struct ubifs_ch *)buf)->node_type), in ubifs_wbuf_write_nolock()
747 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used); in ubifs_wbuf_write_nolock()
748 ubifs_assert(c, len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt); in ubifs_wbuf_write_nolock()
749 ubifs_assert(c, wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0); in ubifs_wbuf_write_nolock()
750 ubifs_assert(c, !(wbuf->offs & 7) && wbuf->offs <= c->leb_size); in ubifs_wbuf_write_nolock()
751 ubifs_assert(c, wbuf->avail > 0 && wbuf->avail <= wbuf->size); in ubifs_wbuf_write_nolock()
752 ubifs_assert(c, wbuf->size >= c->min_io_size); in ubifs_wbuf_write_nolock()
753 ubifs_assert(c, wbuf->size <= c->max_write_size); in ubifs_wbuf_write_nolock()
754 ubifs_assert(c, wbuf->size % c->min_io_size == 0); in ubifs_wbuf_write_nolock()
755 ubifs_assert(c, mutex_is_locked(&wbuf->io_mutex)); in ubifs_wbuf_write_nolock()
756 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_wbuf_write_nolock()
757 ubifs_assert(c, !c->space_fixup); in ubifs_wbuf_write_nolock()
758 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
759 ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size)); in ubifs_wbuf_write_nolock()
761 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) { in ubifs_wbuf_write_nolock()
762 err = -ENOSPC; in ubifs_wbuf_write_nolock()
768 if (c->ro_error) in ubifs_wbuf_write_nolock()
769 return -EROFS; in ubifs_wbuf_write_nolock()
771 if (aligned_len <= wbuf->avail) { in ubifs_wbuf_write_nolock()
774 * write-buffer. in ubifs_wbuf_write_nolock()
776 memcpy(wbuf->buf + wbuf->used, buf, len); in ubifs_wbuf_write_nolock()
778 ubifs_assert(c, aligned_len - len < 8); in ubifs_wbuf_write_nolock()
779 ubifs_pad(c, wbuf->buf + wbuf->used + len, aligned_len - len); in ubifs_wbuf_write_nolock()
782 if (aligned_len == wbuf->avail) { in ubifs_wbuf_write_nolock()
784 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
785 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, in ubifs_wbuf_write_nolock()
786 wbuf->offs, wbuf->size); in ubifs_wbuf_write_nolock()
790 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
791 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
792 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
793 wbuf->size = c->max_write_size; in ubifs_wbuf_write_nolock()
795 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_write_nolock()
796 wbuf->avail = wbuf->size; in ubifs_wbuf_write_nolock()
797 wbuf->used = 0; in ubifs_wbuf_write_nolock()
798 wbuf->next_ino = 0; in ubifs_wbuf_write_nolock()
799 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
801 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
802 wbuf->avail -= aligned_len; in ubifs_wbuf_write_nolock()
803 wbuf->used += aligned_len; in ubifs_wbuf_write_nolock()
804 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
810 if (wbuf->used) { in ubifs_wbuf_write_nolock()
814 * write-buffer and switch to the next max. write unit. in ubifs_wbuf_write_nolock()
817 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
818 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail); in ubifs_wbuf_write_nolock()
819 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, in ubifs_wbuf_write_nolock()
820 wbuf->size); in ubifs_wbuf_write_nolock()
824 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
825 len -= wbuf->avail; in ubifs_wbuf_write_nolock()
826 aligned_len -= wbuf->avail; in ubifs_wbuf_write_nolock()
827 written += wbuf->avail; in ubifs_wbuf_write_nolock()
828 } else if (wbuf->offs & (c->max_write_size - 1)) { in ubifs_wbuf_write_nolock()
830 * The write-buffer offset is not aligned to in ubifs_wbuf_write_nolock()
831 * @c->max_write_size and @wbuf->size is less than in ubifs_wbuf_write_nolock()
832 * @c->max_write_size. Write @wbuf->size bytes to make sure the in ubifs_wbuf_write_nolock()
833 * following writes are done in optimal @c->max_write_size in ubifs_wbuf_write_nolock()
836 dbg_io("write %d bytes to LEB %d:%d", in ubifs_wbuf_write_nolock()
837 wbuf->size, wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
838 err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs, in ubifs_wbuf_write_nolock()
839 wbuf->size); in ubifs_wbuf_write_nolock()
843 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
844 len -= wbuf->size; in ubifs_wbuf_write_nolock()
845 aligned_len -= wbuf->size; in ubifs_wbuf_write_nolock()
846 written += wbuf->size; in ubifs_wbuf_write_nolock()
850 * The remaining data may take more whole max. write units, so write the in ubifs_wbuf_write_nolock()
851 * remains multiple to max. write unit size directly to the flash media. in ubifs_wbuf_write_nolock()
852 * We align node length to 8-byte boundary because we anyway flash wbuf in ubifs_wbuf_write_nolock()
855 n = aligned_len >> c->max_write_shift; in ubifs_wbuf_write_nolock()
857 int m = n - 1; in ubifs_wbuf_write_nolock()
859 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, in ubifs_wbuf_write_nolock()
860 wbuf->offs); in ubifs_wbuf_write_nolock()
863 /* '(n-1)<<c->max_write_shift < len' is always true. */ in ubifs_wbuf_write_nolock()
864 m <<= c->max_write_shift; in ubifs_wbuf_write_nolock()
865 err = ubifs_leb_write(c, wbuf->lnum, buf + written, in ubifs_wbuf_write_nolock()
866 wbuf->offs, m); in ubifs_wbuf_write_nolock()
869 wbuf->offs += m; in ubifs_wbuf_write_nolock()
870 aligned_len -= m; in ubifs_wbuf_write_nolock()
871 len -= m; in ubifs_wbuf_write_nolock()
876 * The non-written len of buf may be less than 'n' because in ubifs_wbuf_write_nolock()
880 n = 1 << c->max_write_shift; in ubifs_wbuf_write_nolock()
881 memcpy(wbuf->buf, buf + written, min(len, n)); in ubifs_wbuf_write_nolock()
883 ubifs_assert(c, n - len < 8); in ubifs_wbuf_write_nolock()
884 ubifs_pad(c, wbuf->buf + len, n - len); in ubifs_wbuf_write_nolock()
887 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, n); in ubifs_wbuf_write_nolock()
890 wbuf->offs += n; in ubifs_wbuf_write_nolock()
891 aligned_len -= n; in ubifs_wbuf_write_nolock()
892 len -= min(len, n); in ubifs_wbuf_write_nolock()
896 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
900 * max. write unit, so write it to the write-buffer and we are in ubifs_wbuf_write_nolock()
903 memcpy(wbuf->buf, buf + written, len); in ubifs_wbuf_write_nolock()
905 ubifs_assert(c, aligned_len - len < 8); in ubifs_wbuf_write_nolock()
906 ubifs_pad(c, wbuf->buf + len, aligned_len - len); in ubifs_wbuf_write_nolock()
910 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
911 wbuf->size = c->max_write_size; in ubifs_wbuf_write_nolock()
913 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_write_nolock()
914 wbuf->avail = wbuf->size - aligned_len; in ubifs_wbuf_write_nolock()
915 wbuf->used = aligned_len; in ubifs_wbuf_write_nolock()
916 wbuf->next_ino = 0; in ubifs_wbuf_write_nolock()
917 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
920 if (wbuf->sync_callback) { in ubifs_wbuf_write_nolock()
921 int free = c->leb_size - wbuf->offs - wbuf->used; in ubifs_wbuf_write_nolock()
923 err = wbuf->sync_callback(c, wbuf->lnum, free, 0); in ubifs_wbuf_write_nolock()
928 if (wbuf->used) in ubifs_wbuf_write_nolock()
931 return 0; in ubifs_wbuf_write_nolock()
934 ubifs_err(c, "cannot write %d bytes to LEB %d:%d, error %d", in ubifs_wbuf_write_nolock()
935 len, wbuf->lnum, wbuf->offs, err); in ubifs_wbuf_write_nolock()
938 ubifs_dump_leb(c, wbuf->lnum); in ubifs_wbuf_write_nolock()
943 * ubifs_write_node_hmac - write node to the media.
944 * @c: UBIFS file-system description object
945 * @buf: the node to write
960 int err, buf_len = ALIGN(len, c->min_io_size); in ubifs_write_node_hmac()
963 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len, in ubifs_write_node_hmac()
965 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_write_node_hmac()
966 ubifs_assert(c, offs % c->min_io_size == 0 && offs < c->leb_size); in ubifs_write_node_hmac()
967 ubifs_assert(c, !c->ro_media && !c->ro_mount); in ubifs_write_node_hmac()
968 ubifs_assert(c, !c->space_fixup); in ubifs_write_node_hmac()
970 if (c->ro_error) in ubifs_write_node_hmac()
971 return -EROFS; in ubifs_write_node_hmac()
985 * ubifs_write_node - write node to the media.
986 * @c: UBIFS file-system description object
987 * @buf: the node to write
1001 return ubifs_write_node_hmac(c, buf, len, lnum, offs, -1); in ubifs_write_node()
1005 * ubifs_read_node_wbuf - read node from the media or write-buffer.
1006 * @wbuf: wbuf to check for un-written data
1014 * in @buf. If the node partially or fully sits in the write-buffer, this
1016 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
1022 const struct ubifs_info *c = wbuf->c; in ubifs_read_node_wbuf()
1027 dbg_ntype(type), len, dbg_jhead(wbuf->jhead)); in ubifs_read_node_wbuf()
1028 ubifs_assert(c, wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_read_node_wbuf()
1029 ubifs_assert(c, !(offs & 7) && offs < c->leb_size); in ubifs_read_node_wbuf()
1030 ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT); in ubifs_read_node_wbuf()
1032 spin_lock(&wbuf->lock); in ubifs_read_node_wbuf()
1033 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); in ubifs_read_node_wbuf()
1035 /* We may safely unlock the write-buffer and read the data */ in ubifs_read_node_wbuf()
1036 spin_unlock(&wbuf->lock); in ubifs_read_node_wbuf()
1041 rlen = wbuf->offs - offs; in ubifs_read_node_wbuf()
1042 if (rlen < 0) in ubifs_read_node_wbuf()
1043 rlen = 0; in ubifs_read_node_wbuf()
1045 /* Copy the rest from the write-buffer */ in ubifs_read_node_wbuf()
1046 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen); in ubifs_read_node_wbuf()
1047 spin_unlock(&wbuf->lock); in ubifs_read_node_wbuf()
1049 if (rlen > 0) { in ubifs_read_node_wbuf()
1050 /* Read everything that goes before write-buffer */ in ubifs_read_node_wbuf()
1051 err = ubifs_leb_read(c, lnum, buf, offs, rlen, 0); in ubifs_read_node_wbuf()
1052 if (err && err != -EBADMSG) in ubifs_read_node_wbuf()
1056 if (type != ch->node_type) { in ubifs_read_node_wbuf()
1058 ch->node_type, type); in ubifs_read_node_wbuf()
1062 err = ubifs_check_node(c, buf, len, lnum, offs, 0, 0); in ubifs_read_node_wbuf()
1068 rlen = le32_to_cpu(ch->len); in ubifs_read_node_wbuf()
1074 return 0; in ubifs_read_node_wbuf()
1080 return -EINVAL; in ubifs_read_node_wbuf()
1084 * ubifs_read_node - read node.
1085 * @c: UBIFS file-system description object
1093 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
1103 ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_read_node()
1104 ubifs_assert(c, len >= UBIFS_CH_SZ && offs + len <= c->leb_size); in ubifs_read_node()
1105 ubifs_assert(c, !(offs & 7) && offs < c->leb_size); in ubifs_read_node()
1106 ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT); in ubifs_read_node()
1108 err = ubifs_leb_read(c, lnum, buf, offs, len, 0); in ubifs_read_node()
1109 if (err && err != -EBADMSG) in ubifs_read_node()
1112 if (type != ch->node_type) { in ubifs_read_node()
1114 ch->node_type, type); in ubifs_read_node()
1118 err = ubifs_check_node(c, buf, len, lnum, offs, 0, 0); in ubifs_read_node()
1124 l = le32_to_cpu(ch->len); in ubifs_read_node()
1130 return 0; in ubifs_read_node()
1134 offs, ubi_is_mapped(c->ubi, lnum)); in ubifs_read_node()
1135 if (!c->probing) { in ubifs_read_node()
1139 return -EINVAL; in ubifs_read_node()
1143 * ubifs_wbuf_init - initialize write-buffer.
1144 * @c: UBIFS file-system description object
1145 * @wbuf: write-buffer to initialize
1147 * This function initializes write-buffer. Returns zero in case of success
1148 * %-ENOMEM in case of failure.
1154 wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL); in ubifs_wbuf_init()
1155 if (!wbuf->buf) in ubifs_wbuf_init()
1156 return -ENOMEM; in ubifs_wbuf_init()
1158 size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t); in ubifs_wbuf_init()
1159 wbuf->inodes = kmalloc(size, GFP_KERNEL); in ubifs_wbuf_init()
1160 if (!wbuf->inodes) { in ubifs_wbuf_init()
1161 kfree(wbuf->buf); in ubifs_wbuf_init()
1162 wbuf->buf = NULL; in ubifs_wbuf_init()
1163 return -ENOMEM; in ubifs_wbuf_init()
1166 wbuf->used = 0; in ubifs_wbuf_init()
1167 wbuf->lnum = wbuf->offs = -1; in ubifs_wbuf_init()
1169 * If the LEB starts at the max. write size aligned address, then in ubifs_wbuf_init()
1170 * write-buffer size has to be set to @c->max_write_size. Otherwise, in ubifs_wbuf_init()
1172 * write size boundary. in ubifs_wbuf_init()
1174 size = c->max_write_size - (c->leb_start % c->max_write_size); in ubifs_wbuf_init()
1175 wbuf->avail = wbuf->size = size; in ubifs_wbuf_init()
1176 wbuf->sync_callback = NULL; in ubifs_wbuf_init()
1177 mutex_init(&wbuf->io_mutex); in ubifs_wbuf_init()
1178 spin_lock_init(&wbuf->lock); in ubifs_wbuf_init()
1179 wbuf->c = c; in ubifs_wbuf_init()
1180 wbuf->next_ino = 0; in ubifs_wbuf_init()
1182 hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in ubifs_wbuf_init()
1183 wbuf->timer.function = wbuf_timer_callback_nolock; in ubifs_wbuf_init()
1184 return 0; in ubifs_wbuf_init()
1188 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
1189 * @wbuf: the write-buffer where to add
1192 * This function adds an inode number to the inode array of the write-buffer.
1196 if (!wbuf->buf) in ubifs_wbuf_add_ino_nolock()
1200 spin_lock(&wbuf->lock); in ubifs_wbuf_add_ino_nolock()
1201 if (wbuf->used) in ubifs_wbuf_add_ino_nolock()
1202 wbuf->inodes[wbuf->next_ino++] = inum; in ubifs_wbuf_add_ino_nolock()
1203 spin_unlock(&wbuf->lock); in ubifs_wbuf_add_ino_nolock()
1207 * wbuf_has_ino - returns if the wbuf contains data from the inode.
1208 * @wbuf: the write-buffer
1211 * This function returns with %1 if the write-buffer contains some data from the
1212 * given inode otherwise it returns with %0.
1216 int i, ret = 0; in wbuf_has_ino()
1218 spin_lock(&wbuf->lock); in wbuf_has_ino()
1219 for (i = 0; i < wbuf->next_ino; i++) in wbuf_has_ino()
1220 if (inum == wbuf->inodes[i]) { in wbuf_has_ino()
1224 spin_unlock(&wbuf->lock); in wbuf_has_ino()
1230 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
1231 * @c: UBIFS file-system description object
1234 * This function synchronizes write-buffers which contain nodes belonging to
1240 int i, err = 0; in ubifs_sync_wbufs_by_inode()
1242 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_sync_wbufs_by_inode()
1243 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_sync_wbufs_by_inode()
1249 * a _copy_ of corresponding on-flash node which sits in ubifs_sync_wbufs_by_inode()
1254 if (!wbuf_has_ino(wbuf, inode->i_ino)) in ubifs_sync_wbufs_by_inode()
1257 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_sync_wbufs_by_inode()
1258 if (wbuf_has_ino(wbuf, inode->i_ino)) in ubifs_sync_wbufs_by_inode()
1260 mutex_unlock(&wbuf->io_mutex); in ubifs_sync_wbufs_by_inode()
1267 return 0; in ubifs_sync_wbufs_by_inode()