UBIFS檔案系統分析3 – 超級塊管理

來源:互聯網
上載者:User

UBIFS superblock儲存在volume的第一個LEB,在UBIFS運行期間superblock幾乎不會改變,只有一種情況會導致superblock node被重寫,就是自動resize時。之所以需要自動resize,是因為建立ubifs檔案系統鏡像時,並不知道將要mount的UBI bolume的大小,所以當我們將UBIFS鏡像安裝到UBI上時,UBI的尺寸可能大於UBIFS鏡像所需要的最大空間,此時就需要把UBIFS resize以適合UBI volume,以更好的利用UBI空間。

superblock 磁碟結構儲存著檔案系統大部分尺寸參數
struct ubifs_sb_node {
    struct ubifs_ch ch;
    __u8 padding[2];
    __u8 key_hash;
    __u8 key_fmt;
    __le32 flags;
    __le32 min_io_size;
    __le32 leb_size;
    __le32 leb_cnt;
    __le32 max_leb_cnt;
    __le64 max_bud_bytes;
    __le32 log_lebs;
    __le32 lpt_lebs;
    __le32 orph_lebs;
    __le32 jhead_cnt;
    __le32 fanout;
    __le32 lsave_cnt;
    __le32 fmt_version;
    __le16 default_compr;
    __u8 padding1[2];
    __le32 rp_uid;
    __le32 rp_gid;
    __le64 rp_size;
    __le32 time_gran;
    __u8 uuid[16];
    __le32 ro_compat_version;
    __u8 padding2[3968];
} __attribute__ ((packed));

@key_hash:檔案系統產生key的hash函數類型
@key_fmt: 檔案系統key的格式,ubifs當前只有一種64bits key格式:32bit ino + 3bits key type + 29bits hash
@min_io_size:檔案系統最小讀寫單元
@leb_size:Logical erased block尺寸
@leb_cnt:檔案系統實際的leb count
@max_leb_cnt:檔案系統允許使用的最大leb count
@log_lebs:log area 佔用的邏輯LEB數目
@lpt_lebs: lprops table佔用的邏輯LEB數目
@orph_lebs: orphan area佔用的邏輯LEB數目
@jhead_cnt:journal head記錄下一個節點寫到flash上的位置,UBIFS採用多線程journal來寫入兩種主要的heads:base head和data head.jhead_cnt記錄這個數目.
@fanout:ubifs檔案系統樹的最大扇出數.
@lsave_cnt:LPT save table中LEB Numbers的數目,
@fmt_version:UBIFS on flash format version.

fs/ubifs/sb.c

499 /**
500  * ubifs_write_sb_node - write superblock node.
501  * @c: UBIFS file-system description object
502  * @sup: superblock node read with 'ubifs_read_sb_node()'
503  *  
504  * This function returns %0 on success and a negative error code on failure.
505  */
506 int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
507 {
508     int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
509     
510     ubifs_prepare_node(c, sup, UBIFS_SB_NODE_SZ, 1);
511     return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len, UBI_LONGTERM);
512 }
superblock只使用擦除塊第一個page,並不會使用其他的page,這是因為superblock修改很少,不需要考慮利用其他的page

514 /**
515  * ubifs_read_superblock - read superblock.
516  * @c: UBIFS file-system description object
517  *
518  * This function finds, reads and checks the superblock. If an empty UBI volume
519  * is being mounted, this function creates default superblock. Returns zero in
520  * case of success, and a negative error code in case of failure.
521  */
522 int ubifs_read_superblock(struct ubifs_info *c)
523 {
524     int err, sup_flags;
525     struct ubifs_sb_node *sup;
526
527     if (c->empty) {
528         err = create_default_filesystem(c);
529         if (err)
530             return err;
531     }
532
533     sup = ubifs_read_sb_node(c);
534     if (IS_ERR(sup))
535         return PTR_ERR(sup);
536
       ...........
622
623     /* Automatically increase file system size to the maximum size */
624     c->old_leb_cnt = c->leb_cnt;
625     if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
626         c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
627         if (c->vfs_sb->s_flags & MS_RDONLY)
628             dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
629                 c->old_leb_cnt, c->leb_cnt);
630         else {
631             dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
632                 c->old_leb_cnt, c->leb_cnt);
633             sup->leb_cnt = cpu_to_le32(c->leb_cnt);
634             err = ubifs_write_sb_node(c, sup);
635             if (err)
636                 goto out;
637             c->old_leb_cnt = c->leb_cnt;
638         }
639     }
640
641     c->log_bytes = (long long)c->log_lebs * c->leb_size;
642     c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
643     c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
644     c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
645     c->orph_first = c->lpt_last + 1;
646     c->orph_last = c->orph_first + c->orph_lebs - 1;
647     c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
648     c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
649     c->main_first = c->leb_cnt - c->main_lebs;
650
651     err = validate_sb(c, sup);
652 out:
653     kfree(sup);
654     return err;
655 }

527 ~ 531 c->empty表示要mount的UBI volume 是empty,那麼create_default_filesystem建立一個預設的檔案系統
533 讀取superblock on-flash結構
623 ~ 639 如果鏡像的leb_cnt小於volume的可用尺寸,那麼resize檔案系統,使其利用volume的所有可用空間,更新on-flash superblock,這也是UBIFS檔案系統唯一修改superblock的地方。
641 ~ 649 可以看到ubifs各個area的起始位置和大小
651 最後對superblock中的參數進行驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.