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

4  * Copyright (C) 2006-2008 Nokia Corporation.
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 * This file implements UBIFS I/O subsystem which provides various I/O-related
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
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
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
66 * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
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
84 if (!c->ro_error) { in ubifs_ro_mode()
85 c->ro_error = 1; in ubifs_ro_mode()
86 c->no_chk_data_crc = 0; in ubifs_ro_mode()
87 c->vfs_sb->s_flags |= MS_RDONLY; in ubifs_ro_mode()
88 ubifs_warn("switched to read-only mode, error %d", err); in ubifs_ro_mode()
104 err = ubi_read(c->ubi, lnum, buf, offs, len); in ubifs_leb_read()
106 * In case of %-EBADMSG print the error message only if the in ubifs_leb_read()
109 if (err && (err != -EBADMSG || even_ebadmsg)) { in ubifs_leb_read()
122 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_leb_write()
123 if (c->ro_error) in ubifs_leb_write()
124 return -EROFS; in ubifs_leb_write()
126 err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype); in ubifs_leb_write()
130 ubifs_err("writing %d bytes to LEB %d:%d failed, error %d", in ubifs_leb_write()
143 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_leb_change()
144 if (c->ro_error) in ubifs_leb_change()
145 return -EROFS; in ubifs_leb_change()
147 err = ubi_leb_change(c->ubi, lnum, buf, len, dtype); in ubifs_leb_change()
163 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_leb_unmap()
164 if (c->ro_error) in ubifs_leb_unmap()
165 return -EROFS; in ubifs_leb_unmap()
167 err = ubi_leb_unmap(c->ubi, lnum); in ubifs_leb_unmap()
182 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_leb_map()
183 if (c->ro_error) in ubifs_leb_map()
184 return -EROFS; in ubifs_leb_map()
186 err = ubi_leb_map(c->ubi, lnum, dtype); in ubifs_leb_map()
201 err = ubi_is_mapped(c->ubi, lnum); in ubifs_is_mapped()
211 * ubifs_check_node - check node.
212 * @c: UBIFS file-system description object
213 * @buf: node to check
217 * @must_chk_crc: indicates whether to always check the CRC
220 * validates node length to prevent UBIFS from becoming crazy when an attacker
221 * feeds it a file-system image with incorrect nodes. For example, too large
222 * node length in the common header could cause UBIFS to read memory outside of
225 * This function may skip data nodes CRC checking if @c->no_chk_data_crc is
227 * @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
228 * checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
229 * mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
230 * is checked. This is because during mounting or re-mounting from R/O mode to
235 * This function returns zero in case of success and %-EUCLEAN in case of bad
241 int err = -EINVAL, type, node_len; in ubifs_check_node()
245 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_check_node()
246 ubifs_assert(!(offs & 7) && offs < c->leb_size); in ubifs_check_node()
248 magic = le32_to_cpu(ch->magic); in ubifs_check_node()
253 err = -EUCLEAN; in ubifs_check_node()
257 type = ch->node_type; in ubifs_check_node()
264 node_len = le32_to_cpu(ch->len); in ubifs_check_node()
265 if (node_len + offs > c->leb_size) in ubifs_check_node()
268 if (c->ranges[type].max_len == 0) { in ubifs_check_node()
269 if (node_len != c->ranges[type].len) in ubifs_check_node()
271 } else if (node_len < c->ranges[type].min_len || in ubifs_check_node()
272 node_len > c->ranges[type].max_len) in ubifs_check_node()
275 if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting && in ubifs_check_node()
276 !c->remounting_rw && c->no_chk_data_crc) in ubifs_check_node()
279 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8); in ubifs_check_node()
280 node_crc = le32_to_cpu(ch->crc); in ubifs_check_node()
285 err = -EUCLEAN; in ubifs_check_node()
304 * ubifs_pad - pad flash space.
305 * @c: UBIFS file-system description object
306 * @buf: buffer to put padding to
307 * @pad: how many bytes to pad
309 * The flash media obliges us to write only in chunks of %c->min_io_size and
310 * when we have to write less data we add padding node to the write-buffer and
311 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
312 * media is being scanned. If the amount of wasted space is not enough to fit a
313 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
316 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
329 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_pad()
330 ch->node_type = UBIFS_PAD_NODE; in ubifs_pad()
331 ch->group_type = UBIFS_NO_NODE_GROUP; in ubifs_pad()
332 ch->padding[0] = ch->padding[1] = 0; in ubifs_pad()
333 ch->sqnum = 0; in ubifs_pad()
334 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ); in ubifs_pad()
335 pad -= UBIFS_PAD_NODE_SZ; in ubifs_pad()
336 pad_node->pad_len = cpu_to_le32(pad); in ubifs_pad()
337 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8); in ubifs_pad()
338 ch->crc = cpu_to_le32(crc); in ubifs_pad()
346 * next_sqnum - get next sequence number.
347 * @c: UBIFS file-system description object
353 spin_lock(&c->cnt_lock); in next_sqnum()
354 sqnum = ++c->max_sqnum; in next_sqnum()
355 spin_unlock(&c->cnt_lock); in next_sqnum()
361 ubifs_ro_mode(c, -EINVAL); in next_sqnum()
370 * ubifs_prepare_node - prepare node to be written to flash.
371 * @c: UBIFS file-system description object
372 * @node: the node to pad
374 * @pad: if the buffer has to be padded
376 * This function prepares node at @node to be written to the media - it
377 * calculates node CRC, fills the common header, and adds proper padding up to
388 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_prepare_node()
389 ch->len = cpu_to_le32(len); in ubifs_prepare_node()
390 ch->group_type = UBIFS_NO_NODE_GROUP; in ubifs_prepare_node()
391 ch->sqnum = cpu_to_le64(sqnum); in ubifs_prepare_node()
392 ch->padding[0] = ch->padding[1] = 0; in ubifs_prepare_node()
393 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); in ubifs_prepare_node()
394 ch->crc = cpu_to_le32(crc); in ubifs_prepare_node()
398 pad = ALIGN(len, c->min_io_size) - len; in ubifs_prepare_node()
404 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
405 * @c: UBIFS file-system description object
406 * @node: the node to pad
410 * This function prepares node at @node to be written to the media - it
421 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC); in ubifs_prep_grp_node()
422 ch->len = cpu_to_le32(len); in ubifs_prep_grp_node()
424 ch->group_type = UBIFS_LAST_OF_NODE_GROUP; in ubifs_prep_grp_node()
426 ch->group_type = UBIFS_IN_NODE_GROUP; in ubifs_prep_grp_node()
427 ch->sqnum = cpu_to_le64(sqnum); in ubifs_prep_grp_node()
428 ch->padding[0] = ch->padding[1] = 0; in ubifs_prep_grp_node()
429 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8); in ubifs_prep_grp_node()
430 ch->crc = cpu_to_le32(crc); in ubifs_prep_grp_node()
434 * wbuf_timer_callback - write-buffer timer callback function.
435 * @data: timer data (write-buffer descriptor)
437 * This function is called when the write-buffer timer expires.
443 dbg_io("jhead %s", dbg_jhead(wbuf->jhead)); in wbuf_timer_callback_nolock()
444 wbuf->need_sync = 1; in wbuf_timer_callback_nolock()
445 wbuf->c->need_wbuf_sync = 1; in wbuf_timer_callback_nolock()
446 ubifs_wake_up_bgt(wbuf->c); in wbuf_timer_callback_nolock()
451 * new_wbuf_timer - start new write-buffer timer.
452 * @wbuf: write-buffer descriptor
456 ubifs_assert(!hrtimer_active(&wbuf->timer)); in new_wbuf_timer_nolock()
458 if (wbuf->no_timer) in new_wbuf_timer_nolock()
460 dbg_io("set timer for jhead %s, %llu-%llu millisecs", in new_wbuf_timer_nolock()
461 dbg_jhead(wbuf->jhead), in new_wbuf_timer_nolock()
462 div_u64(ktime_to_ns(wbuf->softlimit), USEC_PER_SEC), in new_wbuf_timer_nolock()
463 div_u64(ktime_to_ns(wbuf->softlimit) + wbuf->delta, in new_wbuf_timer_nolock()
465 hrtimer_start_range_ns(&wbuf->timer, wbuf->softlimit, wbuf->delta, in new_wbuf_timer_nolock()
470 * cancel_wbuf_timer - cancel write-buffer timer.
471 * @wbuf: write-buffer descriptor
475 if (wbuf->no_timer) in cancel_wbuf_timer_nolock()
477 wbuf->need_sync = 0; in cancel_wbuf_timer_nolock()
478 hrtimer_cancel(&wbuf->timer); in cancel_wbuf_timer_nolock()
482 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
483 * @wbuf: write-buffer to synchronize
485 * This function synchronizes write-buffer @buf and returns zero in case of
488 * Note, although write-buffers are of @c->max_write_size, this function does
489 * not necessarily writes all @c->max_write_size bytes to the flash. Instead,
490 * if the write-buffer is only partially filled with data, only the used part
491 * of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
496 struct ubifs_info *c = wbuf->c; in ubifs_wbuf_sync_nolock()
500 if (!wbuf->used || wbuf->lnum == -1) in ubifs_wbuf_sync_nolock()
501 /* Write-buffer is empty or not seeked */ in ubifs_wbuf_sync_nolock()
505 wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead)); in ubifs_wbuf_sync_nolock()
506 ubifs_assert(!(wbuf->avail & 7)); in ubifs_wbuf_sync_nolock()
507 ubifs_assert(wbuf->offs + wbuf->size <= c->leb_size); in ubifs_wbuf_sync_nolock()
508 ubifs_assert(wbuf->size >= c->min_io_size); in ubifs_wbuf_sync_nolock()
509 ubifs_assert(wbuf->size <= c->max_write_size); in ubifs_wbuf_sync_nolock()
510 ubifs_assert(wbuf->size % c->min_io_size == 0); in ubifs_wbuf_sync_nolock()
511 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_wbuf_sync_nolock()
512 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_sync_nolock()
513 ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size)); in ubifs_wbuf_sync_nolock()
515 if (c->ro_error) in ubifs_wbuf_sync_nolock()
516 return -EROFS; in ubifs_wbuf_sync_nolock()
519 * Do not write whole write buffer but write only the minimum necessary in ubifs_wbuf_sync_nolock()
522 sync_len = ALIGN(wbuf->used, c->min_io_size); in ubifs_wbuf_sync_nolock()
523 dirt = sync_len - wbuf->used; in ubifs_wbuf_sync_nolock()
525 ubifs_pad(c, wbuf->buf + wbuf->used, dirt); in ubifs_wbuf_sync_nolock()
526 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len, in ubifs_wbuf_sync_nolock()
527 wbuf->dtype); in ubifs_wbuf_sync_nolock()
531 spin_lock(&wbuf->lock); in ubifs_wbuf_sync_nolock()
532 wbuf->offs += sync_len; in ubifs_wbuf_sync_nolock()
534 * Now @wbuf->offs is not necessarily aligned to @c->max_write_size. in ubifs_wbuf_sync_nolock()
535 * But our goal is to optimize writes and make sure we write in in ubifs_wbuf_sync_nolock()
536 * @c->max_write_size chunks and to @c->max_write_size-aligned offset. in ubifs_wbuf_sync_nolock()
537 * Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make in ubifs_wbuf_sync_nolock()
538 * sure that @wbuf->offs + @wbuf->size is aligned to in ubifs_wbuf_sync_nolock()
539 * @c->max_write_size. This way we make sure that after next in ubifs_wbuf_sync_nolock()
540 * write-buffer flush we are again at the optimal offset (aligned to in ubifs_wbuf_sync_nolock()
541 * @c->max_write_size). in ubifs_wbuf_sync_nolock()
543 if (c->leb_size - wbuf->offs < c->max_write_size) in ubifs_wbuf_sync_nolock()
544 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_sync_nolock()
545 else if (wbuf->offs & (c->max_write_size - 1)) in ubifs_wbuf_sync_nolock()
546 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs; in ubifs_wbuf_sync_nolock()
548 wbuf->size = c->max_write_size; in ubifs_wbuf_sync_nolock()
549 wbuf->avail = wbuf->size; in ubifs_wbuf_sync_nolock()
550 wbuf->used = 0; in ubifs_wbuf_sync_nolock()
551 wbuf->next_ino = 0; in ubifs_wbuf_sync_nolock()
552 spin_unlock(&wbuf->lock); in ubifs_wbuf_sync_nolock()
554 if (wbuf->sync_callback) in ubifs_wbuf_sync_nolock()
555 err = wbuf->sync_callback(c, wbuf->lnum, in ubifs_wbuf_sync_nolock()
556 c->leb_size - wbuf->offs, dirt); in ubifs_wbuf_sync_nolock()
561 * ubifs_wbuf_seek_nolock - seek write-buffer.
562 * @wbuf: write-buffer
563 * @lnum: logical eraseblock number to seek to
564 * @offs: logical eraseblock offset to seek to
567 * This function targets the write-buffer to logical eraseblock @lnum:@offs.
568 * The write-buffer has to be empty. Returns zero in case of success and a
574 const struct ubifs_info *c = wbuf->c; in ubifs_wbuf_seek_nolock()
576 dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead)); in ubifs_wbuf_seek_nolock()
577 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt); in ubifs_wbuf_seek_nolock()
578 ubifs_assert(offs >= 0 && offs <= c->leb_size); in ubifs_wbuf_seek_nolock()
579 ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7)); in ubifs_wbuf_seek_nolock()
580 ubifs_assert(lnum != wbuf->lnum); in ubifs_wbuf_seek_nolock()
581 ubifs_assert(wbuf->used == 0); in ubifs_wbuf_seek_nolock()
583 spin_lock(&wbuf->lock); in ubifs_wbuf_seek_nolock()
584 wbuf->lnum = lnum; in ubifs_wbuf_seek_nolock()
585 wbuf->offs = offs; in ubifs_wbuf_seek_nolock()
586 if (c->leb_size - wbuf->offs < c->max_write_size) in ubifs_wbuf_seek_nolock()
587 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_seek_nolock()
588 else if (wbuf->offs & (c->max_write_size - 1)) in ubifs_wbuf_seek_nolock()
589 wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs; in ubifs_wbuf_seek_nolock()
591 wbuf->size = c->max_write_size; in ubifs_wbuf_seek_nolock()
592 wbuf->avail = wbuf->size; in ubifs_wbuf_seek_nolock()
593 wbuf->used = 0; in ubifs_wbuf_seek_nolock()
594 spin_unlock(&wbuf->lock); in ubifs_wbuf_seek_nolock()
595 wbuf->dtype = dtype; in ubifs_wbuf_seek_nolock()
601 * ubifs_bg_wbufs_sync - synchronize write-buffers.
602 * @c: UBIFS file-system description object
604 * This function is called by background thread to synchronize write-buffers.
612 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_bg_wbufs_sync()
613 if (!c->need_wbuf_sync) in ubifs_bg_wbufs_sync()
615 c->need_wbuf_sync = 0; in ubifs_bg_wbufs_sync()
617 if (c->ro_error) { in ubifs_bg_wbufs_sync()
618 err = -EROFS; in ubifs_bg_wbufs_sync()
623 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_bg_wbufs_sync()
624 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_bg_wbufs_sync()
632 if (mutex_is_locked(&wbuf->io_mutex)) in ubifs_bg_wbufs_sync()
635 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_bg_wbufs_sync()
636 if (!wbuf->need_sync) { in ubifs_bg_wbufs_sync()
637 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
642 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
644 ubifs_err("cannot sync write-buffer, error %d", err); in ubifs_bg_wbufs_sync()
653 /* Cancel all timers to prevent repeated errors */ in ubifs_bg_wbufs_sync()
654 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_bg_wbufs_sync()
655 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_bg_wbufs_sync()
657 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_bg_wbufs_sync()
659 mutex_unlock(&wbuf->io_mutex); in ubifs_bg_wbufs_sync()
665 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
666 * @wbuf: write-buffer
667 * @buf: node to write
670 * This function writes data to flash via write-buffer @wbuf. This means that
672 * does not take whole max. write unit (@c->max_write_size). Instead, the node
673 * will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
674 * because more data are appended to the write-buffer).
678 * space in this logical eraseblock, %-ENOSPC is returned.
682 struct ubifs_info *c = wbuf->c; in ubifs_wbuf_write_nolock()
685 dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len, in ubifs_wbuf_write_nolock()
686 dbg_ntype(((struct ubifs_ch *)buf)->node_type), in ubifs_wbuf_write_nolock()
687 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used); in ubifs_wbuf_write_nolock()
688 ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt); in ubifs_wbuf_write_nolock()
689 ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0); in ubifs_wbuf_write_nolock()
690 ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size); in ubifs_wbuf_write_nolock()
691 ubifs_assert(wbuf->avail > 0 && wbuf->avail <= wbuf->size); in ubifs_wbuf_write_nolock()
692 ubifs_assert(wbuf->size >= c->min_io_size); in ubifs_wbuf_write_nolock()
693 ubifs_assert(wbuf->size <= c->max_write_size); in ubifs_wbuf_write_nolock()
694 ubifs_assert(wbuf->size % c->min_io_size == 0); in ubifs_wbuf_write_nolock()
695 ubifs_assert(mutex_is_locked(&wbuf->io_mutex)); in ubifs_wbuf_write_nolock()
696 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_wbuf_write_nolock()
697 ubifs_assert(!c->space_fixup); in ubifs_wbuf_write_nolock()
698 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
699 ubifs_assert(!((wbuf->offs + wbuf->size) % c->max_write_size)); in ubifs_wbuf_write_nolock()
701 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) { in ubifs_wbuf_write_nolock()
702 err = -ENOSPC; in ubifs_wbuf_write_nolock()
708 if (c->ro_error) in ubifs_wbuf_write_nolock()
709 return -EROFS; in ubifs_wbuf_write_nolock()
711 if (aligned_len <= wbuf->avail) { in ubifs_wbuf_write_nolock()
714 * write-buffer. in ubifs_wbuf_write_nolock()
716 memcpy(wbuf->buf + wbuf->used, buf, len); in ubifs_wbuf_write_nolock()
718 if (aligned_len == wbuf->avail) { in ubifs_wbuf_write_nolock()
719 dbg_io("flush jhead %s wbuf to LEB %d:%d", in ubifs_wbuf_write_nolock()
720 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
721 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, in ubifs_wbuf_write_nolock()
722 wbuf->offs, wbuf->size, in ubifs_wbuf_write_nolock()
723 wbuf->dtype); in ubifs_wbuf_write_nolock()
727 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
728 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
729 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
730 wbuf->size = c->max_write_size; in ubifs_wbuf_write_nolock()
732 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_write_nolock()
733 wbuf->avail = wbuf->size; in ubifs_wbuf_write_nolock()
734 wbuf->used = 0; in ubifs_wbuf_write_nolock()
735 wbuf->next_ino = 0; in ubifs_wbuf_write_nolock()
736 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
738 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
739 wbuf->avail -= aligned_len; in ubifs_wbuf_write_nolock()
740 wbuf->used += aligned_len; in ubifs_wbuf_write_nolock()
741 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
749 if (wbuf->used) { in ubifs_wbuf_write_nolock()
752 * current available space. We have to fill and flush in ubifs_wbuf_write_nolock()
753 * write-buffer and switch to the next max. write unit. in ubifs_wbuf_write_nolock()
755 dbg_io("flush jhead %s wbuf to LEB %d:%d", in ubifs_wbuf_write_nolock()
756 dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
757 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail); in ubifs_wbuf_write_nolock()
758 err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, in ubifs_wbuf_write_nolock()
759 wbuf->size, wbuf->dtype); in ubifs_wbuf_write_nolock()
763 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
764 len -= wbuf->avail; in ubifs_wbuf_write_nolock()
765 aligned_len -= wbuf->avail; in ubifs_wbuf_write_nolock()
766 written += wbuf->avail; in ubifs_wbuf_write_nolock()
767 } else if (wbuf->offs & (c->max_write_size - 1)) { in ubifs_wbuf_write_nolock()
769 * The write-buffer offset is not aligned to in ubifs_wbuf_write_nolock()
770 * @c->max_write_size and @wbuf->size is less than in ubifs_wbuf_write_nolock()
771 * @c->max_write_size. Write @wbuf->size bytes to make sure the in ubifs_wbuf_write_nolock()
772 * following writes are done in optimal @c->max_write_size in ubifs_wbuf_write_nolock()
775 dbg_io("write %d bytes to LEB %d:%d", in ubifs_wbuf_write_nolock()
776 wbuf->size, wbuf->lnum, wbuf->offs); in ubifs_wbuf_write_nolock()
777 err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs, in ubifs_wbuf_write_nolock()
778 wbuf->size, wbuf->dtype); in ubifs_wbuf_write_nolock()
782 wbuf->offs += wbuf->size; in ubifs_wbuf_write_nolock()
783 len -= wbuf->size; in ubifs_wbuf_write_nolock()
784 aligned_len -= wbuf->size; in ubifs_wbuf_write_nolock()
785 written += wbuf->size; in ubifs_wbuf_write_nolock()
789 * The remaining data may take more whole max. write units, so write the in ubifs_wbuf_write_nolock()
790 * remains multiple to max. write unit size directly to the flash media. in ubifs_wbuf_write_nolock()
791 * We align node length to 8-byte boundary because we anyway flash wbuf in ubifs_wbuf_write_nolock()
794 n = aligned_len >> c->max_write_shift; in ubifs_wbuf_write_nolock()
796 n <<= c->max_write_shift; in ubifs_wbuf_write_nolock()
797 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, in ubifs_wbuf_write_nolock()
798 wbuf->offs); in ubifs_wbuf_write_nolock()
799 err = ubifs_leb_write(c, wbuf->lnum, buf + written, in ubifs_wbuf_write_nolock()
800 wbuf->offs, n, wbuf->dtype); in ubifs_wbuf_write_nolock()
803 wbuf->offs += n; in ubifs_wbuf_write_nolock()
804 aligned_len -= n; in ubifs_wbuf_write_nolock()
805 len -= n; in ubifs_wbuf_write_nolock()
809 spin_lock(&wbuf->lock); in ubifs_wbuf_write_nolock()
813 * max. write unit, so write it to the write-buffer and we are in ubifs_wbuf_write_nolock()
816 memcpy(wbuf->buf, buf + written, len); in ubifs_wbuf_write_nolock()
818 if (c->leb_size - wbuf->offs >= c->max_write_size) in ubifs_wbuf_write_nolock()
819 wbuf->size = c->max_write_size; in ubifs_wbuf_write_nolock()
821 wbuf->size = c->leb_size - wbuf->offs; in ubifs_wbuf_write_nolock()
822 wbuf->avail = wbuf->size - aligned_len; in ubifs_wbuf_write_nolock()
823 wbuf->used = aligned_len; in ubifs_wbuf_write_nolock()
824 wbuf->next_ino = 0; in ubifs_wbuf_write_nolock()
825 spin_unlock(&wbuf->lock); in ubifs_wbuf_write_nolock()
828 if (wbuf->sync_callback) { in ubifs_wbuf_write_nolock()
829 int free = c->leb_size - wbuf->offs - wbuf->used; in ubifs_wbuf_write_nolock()
831 err = wbuf->sync_callback(c, wbuf->lnum, free, 0); in ubifs_wbuf_write_nolock()
836 if (wbuf->used) in ubifs_wbuf_write_nolock()
842 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d", in ubifs_wbuf_write_nolock()
843 len, wbuf->lnum, wbuf->offs, err); in ubifs_wbuf_write_nolock()
846 dbg_dump_leb(c, wbuf->lnum); in ubifs_wbuf_write_nolock()
851 * ubifs_write_node - write node to the media.
852 * @c: UBIFS file-system description object
853 * @buf: the node to write
857 * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
861 * to be aligned to the minimal I/O unit size. This function automatically
868 int err, buf_len = ALIGN(len, c->min_io_size); in ubifs_write_node()
871 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len, in ubifs_write_node()
873 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_write_node()
874 ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size); in ubifs_write_node()
875 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_write_node()
876 ubifs_assert(!c->space_fixup); in ubifs_write_node()
878 if (c->ro_error) in ubifs_write_node()
879 return -EROFS; in ubifs_write_node()
890 * ubifs_read_node_wbuf - read node from the media or write-buffer.
891 * @wbuf: wbuf to check for un-written data
892 * @buf: buffer to read to
899 * in @buf. If the node partially or fully sits in the write-buffer, this
901 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
907 const struct ubifs_info *c = wbuf->c; in ubifs_read_node_wbuf()
912 dbg_ntype(type), len, dbg_jhead(wbuf->jhead)); in ubifs_read_node_wbuf()
913 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_read_node_wbuf()
914 ubifs_assert(!(offs & 7) && offs < c->leb_size); in ubifs_read_node_wbuf()
917 spin_lock(&wbuf->lock); in ubifs_read_node_wbuf()
918 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs); in ubifs_read_node_wbuf()
920 /* We may safely unlock the write-buffer and read the data */ in ubifs_read_node_wbuf()
921 spin_unlock(&wbuf->lock); in ubifs_read_node_wbuf()
926 rlen = wbuf->offs - offs; in ubifs_read_node_wbuf()
930 /* Copy the rest from the write-buffer */ in ubifs_read_node_wbuf()
931 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen); in ubifs_read_node_wbuf()
932 spin_unlock(&wbuf->lock); in ubifs_read_node_wbuf()
935 /* Read everything that goes before write-buffer */ in ubifs_read_node_wbuf()
937 if (err && err != -EBADMSG) in ubifs_read_node_wbuf()
941 if (type != ch->node_type) { in ubifs_read_node_wbuf()
943 ch->node_type, type); in ubifs_read_node_wbuf()
953 rlen = le32_to_cpu(ch->len); in ubifs_read_node_wbuf()
965 return -EINVAL; in ubifs_read_node_wbuf()
969 * ubifs_read_node - read node.
970 * @c: UBIFS file-system description object
971 * @buf: buffer to read to
978 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
988 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0); in ubifs_read_node()
989 ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size); in ubifs_read_node()
990 ubifs_assert(!(offs & 7) && offs < c->leb_size); in ubifs_read_node()
994 if (err && err != -EBADMSG) in ubifs_read_node()
997 if (type != ch->node_type) { in ubifs_read_node()
999 ch->node_type, type); in ubifs_read_node()
1009 l = le32_to_cpu(ch->len); in ubifs_read_node()
1019 ubi_is_mapped(c->ubi, lnum)); in ubifs_read_node()
1022 return -EINVAL; in ubifs_read_node()
1026 * ubifs_wbuf_init - initialize write-buffer.
1027 * @c: UBIFS file-system description object
1028 * @wbuf: write-buffer to initialize
1030 * This function initializes write-buffer. Returns zero in case of success
1031 * %-ENOMEM in case of failure.
1037 wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL); in ubifs_wbuf_init()
1038 if (!wbuf->buf) in ubifs_wbuf_init()
1039 return -ENOMEM; in ubifs_wbuf_init()
1041 size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t); in ubifs_wbuf_init()
1042 wbuf->inodes = kmalloc(size, GFP_KERNEL); in ubifs_wbuf_init()
1043 if (!wbuf->inodes) { in ubifs_wbuf_init()
1044 kfree(wbuf->buf); in ubifs_wbuf_init()
1045 wbuf->buf = NULL; in ubifs_wbuf_init()
1046 return -ENOMEM; in ubifs_wbuf_init()
1049 wbuf->used = 0; in ubifs_wbuf_init()
1050 wbuf->lnum = wbuf->offs = -1; in ubifs_wbuf_init()
1052 * If the LEB starts at the max. write size aligned address, then in ubifs_wbuf_init()
1053 * write-buffer size has to be set to @c->max_write_size. Otherwise, in ubifs_wbuf_init()
1054 * set it to something smaller so that it ends at the closest max. in ubifs_wbuf_init()
1055 * write size boundary. in ubifs_wbuf_init()
1057 size = c->max_write_size - (c->leb_start % c->max_write_size); in ubifs_wbuf_init()
1058 wbuf->avail = wbuf->size = size; in ubifs_wbuf_init()
1059 wbuf->dtype = UBI_UNKNOWN; in ubifs_wbuf_init()
1060 wbuf->sync_callback = NULL; in ubifs_wbuf_init()
1061 mutex_init(&wbuf->io_mutex); in ubifs_wbuf_init()
1062 spin_lock_init(&wbuf->lock); in ubifs_wbuf_init()
1063 wbuf->c = c; in ubifs_wbuf_init()
1064 wbuf->next_ino = 0; in ubifs_wbuf_init()
1066 hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in ubifs_wbuf_init()
1067 wbuf->timer.function = wbuf_timer_callback_nolock; in ubifs_wbuf_init()
1068 wbuf->softlimit = ktime_set(WBUF_TIMEOUT_SOFTLIMIT, 0); in ubifs_wbuf_init()
1069 wbuf->delta = WBUF_TIMEOUT_HARDLIMIT - WBUF_TIMEOUT_SOFTLIMIT; in ubifs_wbuf_init()
1070 wbuf->delta *= 1000000000ULL; in ubifs_wbuf_init()
1071 ubifs_assert(wbuf->delta <= ULONG_MAX); in ubifs_wbuf_init()
1076 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
1077 * @wbuf: the write-buffer where to add
1080 * This function adds an inode number to the inode array of the write-buffer.
1084 if (!wbuf->buf) in ubifs_wbuf_add_ino_nolock()
1088 spin_lock(&wbuf->lock); in ubifs_wbuf_add_ino_nolock()
1089 if (wbuf->used) in ubifs_wbuf_add_ino_nolock()
1090 wbuf->inodes[wbuf->next_ino++] = inum; in ubifs_wbuf_add_ino_nolock()
1091 spin_unlock(&wbuf->lock); in ubifs_wbuf_add_ino_nolock()
1095 * wbuf_has_ino - returns if the wbuf contains data from the inode.
1096 * @wbuf: the write-buffer
1099 * This function returns with %1 if the write-buffer contains some data from the
1106 spin_lock(&wbuf->lock); in wbuf_has_ino()
1107 for (i = 0; i < wbuf->next_ino; i++) in wbuf_has_ino()
1108 if (inum == wbuf->inodes[i]) { in wbuf_has_ino()
1112 spin_unlock(&wbuf->lock); in wbuf_has_ino()
1118 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
1119 * @c: UBIFS file-system description object
1120 * @inode: inode to synchronize
1122 * This function synchronizes write-buffers which contain nodes belonging to
1130 for (i = 0; i < c->jhead_cnt; i++) { in ubifs_sync_wbufs_by_inode()
1131 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf; in ubifs_sync_wbufs_by_inode()
1136 * head contains something related to this inode, it is in ubifs_sync_wbufs_by_inode()
1137 * a _copy_ of corresponding on-flash node which sits in ubifs_sync_wbufs_by_inode()
1142 if (!wbuf_has_ino(wbuf, inode->i_ino)) in ubifs_sync_wbufs_by_inode()
1145 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); in ubifs_sync_wbufs_by_inode()
1146 if (wbuf_has_ino(wbuf, inode->i_ino)) in ubifs_sync_wbufs_by_inode()
1148 mutex_unlock(&wbuf->io_mutex); in ubifs_sync_wbufs_by_inode()