657
* fixup_leb - fixup/unmap an LEB containing free space.
658
* @c: UBIFS file-system description object
659
* @lnum: the LEB number to fix up
660
* @len: number of used bytes in LEB (starting at offset 0)
662
* This function reads the contents of the given LEB number @lnum, then fixes
663
* it up, so that empty min. I/O units in the end of LEB are actually erased on
664
* flash (rather than being just all-0xff real data). If the LEB is completely
665
* empty, it is simply unmapped.
667
static int fixup_leb(struct ubifs_info *c, int lnum, int len)
671
ubifs_assert(len >= 0);
672
ubifs_assert(len % c->min_io_size == 0);
673
ubifs_assert(len < c->leb_size);
676
dbg_mnt("unmap empty LEB %d", lnum);
677
return ubi_leb_unmap(c->ubi, lnum);
680
dbg_mnt("fixup LEB %d, data len %d", lnum, len);
681
err = ubi_read(c->ubi, lnum, c->sbuf, 0, len);
685
return ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
689
* fixup_free_space - find & remap all LEBs containing free space.
690
* @c: UBIFS file-system description object
692
* This function walks through all LEBs in the filesystem and fiexes up those
693
* containing free/empty space.
695
static int fixup_free_space(struct ubifs_info *c)
698
struct ubifs_lprops *lprops;
702
/* Fixup LEBs in the master area */
703
for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
704
err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
709
/* Unmap unused log LEBs */
710
lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
711
while (lnum != c->ltail_lnum) {
712
err = fixup_leb(c, lnum, 0);
715
lnum = ubifs_next_log_lnum(c, lnum);
718
/* Fixup the current log head */
719
err = fixup_leb(c, c->lhead_lnum, c->lhead_offs);
723
/* Fixup LEBs in the LPT area */
724
for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
725
int free = c->ltab[lnum - c->lpt_first].free;
728
err = fixup_leb(c, lnum, c->leb_size - free);
734
/* Unmap LEBs in the orphans area */
735
for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
736
err = fixup_leb(c, lnum, 0);
741
/* Fixup LEBs in the main area */
742
for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
743
lprops = ubifs_lpt_lookup(c, lnum);
744
if (IS_ERR(lprops)) {
745
err = PTR_ERR(lprops);
749
if (lprops->free > 0) {
750
err = fixup_leb(c, lnum, c->leb_size - lprops->free);
757
ubifs_release_lprops(c);
762
* ubifs_fixup_free_space - find & fix all LEBs with free space.
763
* @c: UBIFS file-system description object
765
* This function fixes up LEBs containing free space on first mount, if the
766
* appropriate flag was set when the FS was created. Each LEB with one or more
767
* empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
768
* the free space is actually erased. E.g., this is necessary for some NAND
769
* chips, since the free space may have been programmed like real "0xff" data
770
* (generating a non-0xff ECC), causing future writes to the not-really-erased
771
* NAND pages to behave badly. After the space is fixed up, the superblock flag
772
* is cleared, so that this is skipped for all future mounts.
774
int ubifs_fixup_free_space(struct ubifs_info *c)
777
struct ubifs_sb_node *sup;
779
ubifs_assert(c->space_fixup);
780
ubifs_assert(!c->ro_mount);
782
ubifs_msg("start fixing up free space");
784
err = fixup_free_space(c);
788
sup = ubifs_read_sb_node(c);
792
/* Free-space fixup is no longer required */
794
sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
796
err = ubifs_write_sb_node(c, sup);
801
ubifs_msg("free space fixup complete");