From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1YaK4O-00046l-5v for mharc-grub-devel@gnu.org; Tue, 24 Mar 2015 04:20:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36942) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaK4I-00046a-FW for grub-devel@gnu.org; Tue, 24 Mar 2015 04:20:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YaK4A-00057h-ET for grub-devel@gnu.org; Tue, 24 Mar 2015 04:20:10 -0400 Received: from mail.kernel.org ([198.145.29.136]:49046) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaK4A-00054G-3p for grub-devel@gnu.org; Tue, 24 Mar 2015 04:20:02 -0400 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C1F6E201F4; Tue, 24 Mar 2015 08:19:57 +0000 (UTC) Received: from localhost (unknown [73.189.178.17]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C50C7201B9; Tue, 24 Mar 2015 08:19:54 +0000 (UTC) From: Jaegeuk Kim To: grub-devel@gnu.org, linux-f2fs-devel@lists.sourceforge.net Subject: [PATCH] F2FS support Date: Tue, 24 Mar 2015 01:19:00 -0700 Message-Id: <1427185140-41120-1-git-send-email-jaegeuk@kernel.org> X-Mailer: git-send-email 2.1.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Virus-Scanned: ClamAV using ClamSMTP Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 198.145.29.136 Cc: Jaegeuk Kim X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Mar 2015 08:20:15 -0000 * Makefile.util.def: Add f2fs.c. * doc/grub.texi: Add f2fs description. * grub-core/Makefile.core.def: Add f2fs module. * grub-core/fs/f2fs.c: New file. * tests/f2fs_test.in: New file. * tests/util/grub-fs-tester.in: Add f2fs requirements. Signed-off-by: Jaegeuk Kim --- ChangeLog-2015 | 11 + Makefile.util.def | 7 + docs/grub.texi | 5 +- grub-core/Makefile.core.def | 5 + grub-core/fs/f2fs.c | 1321 ++++++++++++++++++++++++++++++++++++= ++++++ po/exclude.pot | 1 + tests/f2fs_test.in | 19 + tests/util/grub-fs-tester.in | 14 +- 8 files changed, 1378 insertions(+), 5 deletions(-) create mode 100644 grub-core/fs/f2fs.c create mode 100644 tests/f2fs_test.in diff --git a/ChangeLog-2015 b/ChangeLog-2015 index 869f6bf..59351de 100644 --- a/ChangeLog-2015 +++ b/ChangeLog-2015 @@ -1,3 +1,14 @@ +2015-03-12 Jaegeuk Kim + + F2FS support. + + * Makefile.util.def: Add f2fs.c. + * doc/grub.texi: Add f2fs description. + * grub-core/Makefile.core.def: Add f2fs module. + * grub-core/fs/f2fs.c: New file. + * tests/f2fs_test.in: New file. + * tests/util/grub-fs-tester.in: Add f2fs requirements. + 2015-01-23 Vladimir Serbinenko =20 * tests/file_filter/file: Really add missing file. diff --git a/Makefile.util.def b/Makefile.util.def index 67dfb29..0b17907 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -98,6 +98,7 @@ library =3D { common =3D grub-core/fs/newc.c; common =3D grub-core/fs/ext2.c; common =3D grub-core/fs/fat.c; + common =3D grub-core/fs/f2fs.c; common =3D grub-core/fs/exfat.c; common =3D grub-core/fs/fshelp.c; common =3D grub-core/fs/hfs.c; @@ -767,6 +768,12 @@ script =3D { =20 script =3D { testcase; + name =3D f2fs_test; + common =3D tests/f2fs_test.in; +}; + +script =3D { + testcase; name =3D nilfs2_test; common =3D tests/nilfs2_test.in; }; diff --git a/docs/grub.texi b/docs/grub.texi index 46b9e7f..edf4eea 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesyste= m types are @dfn{Amiga Fast FileSystem (AFFS)}, @dfn{AtheOS fs}, @dfn{BeFS}, @dfn{BtrFS} (including raid0, raid1, raid10, gzip and lzo), @dfn{cpio} (little- and big-endian bin, odc and newc variants), -@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT}, @d= fn{HFS}, +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{f2fs}, +@dfn{exFAT}, @dfn{HFS}, @dfn{HFS+}, @dfn{ISO9660} (including Joliet, Rock-ridge and multi-chunk = files), @dfn{JFS}, @dfn{Minix fs} (versions 1, 2 and 3), @dfn{nilfs2}, @dfn{NTFS} (including compression), @dfn{ReiserFS}, @dfn{ROMFS}, @@ -5289,7 +5290,7 @@ NTFS, JFS, UDF, HFS+, exFAT, long filenames in FAT,= Joliet part of ISO9660 are treated as UTF-16 as per specification. AFS and BFS are read as UTF-8, again according to specification. BtrFS, cpio, tar, squash4, m= inix, minix2, minix3, ROMFS, ReiserFS, XFS, ext2, ext3, ext4, FAT (short names= ), -RockRidge part of ISO9660, nilfs2, UFS1, UFS2 and ZFS are assumed +f2fs, RockRidge part of ISO9660, nilfs2, UFS1, UFS2 and ZFS are assumed to be UTF-8. This might be false on systems configured with legacy chars= et but as long as the charset used is superset of ASCII you should be able = to access ASCII-named files. And it's recommended to configure your system = to use diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 8eaae45..3fcd07e 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1281,6 +1281,11 @@ module =3D { }; =20 module =3D { + name =3D f2fs; + common =3D fs/f2fs.c; +}; + +module =3D { name =3D exfat; common =3D fs/exfat.c; }; diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c new file mode 100644 index 0000000..40360d5 --- /dev/null +++ b/grub-core/fs/f2fs.c @@ -0,0 +1,1321 @@ +/* + * f2fs.c - Flash-Friendly File System + * + * Written by Jaegeuk Kim + * + * Copyright (C) 2015 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +/* F2FS Magic Number */ +#define F2FS_SUPER_MAGIC 0xF2F52010 + +/* byte-size offset */ +#define F2FS_SUPER_OFFSET 1024 + +/* 12 bits for 4096 bytes */ +#define F2FS_MAX_LOG_SECTOR_SIZE 12 + +/* 9 bits for 512 bytes */ +#define F2FS_MIN_LOG_SECTOR_SIZE 9 + +/* support only 4KB block */ +#define F2FS_BLKSIZE 4096 +#define F2FS_BLK_BITS 12 +#define F2FS_BLK_SEC_BITS (3) + +#define VERSION_LEN 256 +#define F2FS_MAX_EXTENSION 64 + +#define CP_COMPACT_SUM_FLAG 0x00000004 +#define CP_UMOUNT_FLAG 0x00000001 + +#define MAX_ACTIVE_LOGS 16 +#define MAX_ACTIVE_NODE_LOGS 8 +#define MAX_ACTIVE_DATA_LOGS 8 +#define NR_CURSEG_DATA_TYPE (3) +#define NR_CURSEG_NODE_TYPE (3) +#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE) + +#define ENTRIES_IN_SUM 512 +#define SUMMARY_SIZE (7) +#define SUM_FOOTER_SIZE (5) +#define JENTRY_SIZE (13) +#define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM) +#define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\ + SUM_ENTRIES_SIZE) +#define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) / JENTRY_SIZE) +#define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) % JENTRY_SIZE) + +#define NAT_ENTRY_SIZE (9) +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE) + +#define ver_after (a, b) (typecheck (unsigned long long, a) && = \ + typecheck (unsigned long long, b) && \ + ((long long)((a) - (b)) > 0)) + +#define F2FS_NAME_LEN 255 +#define F2FS_SLOT_LEN 8 +#define NR_DENTRY_IN_BLOCK 214 +#define SIZE_OF_DIR_ENTRY 11 /* by byte */ +#define BITS_PER_BYTE 8 +#define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) = / \ + BITS_PER_BYTE) +#define SIZE_OF_RESERVED (F2FS_BLKSIZE - ((SIZE_OF_DIR_ENTRY + \ + F2FS_SLOT_LEN) * \ + NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP)) + +#define F2FS_INLINE_XATTR_ADDRS 50 /* 200 bytes for inline xattrs */ +#define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */ + +#define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */ +#define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */ +#define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1) +#define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2) +#define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3) +#define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4) +#define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5) + +#define MAX_INLINE_DATA (4 * (DEF_ADDRS_PER_INODE - \ + F2FS_INLINE_XATTR_ADDRS - 1)) +#define NR_INLINE_DENTRY (MAX_INLINE_DATA * BITS_PER_BYTE / \ + ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ + BITS_PER_BYTE + 1)) +#define INLINE_DENTRY_BITMAP_SIZE ((NR_INLINE_DENTRY + \ + BITS_PER_BYTE - 1) / BITS_PER_BYTE) +#define INLINE_RESERVED_SIZE (MAX_INLINE_DATA - \ + ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ + NR_INLINE_DENTRY + INLINE_DENTRY_BITMAP_SIZE)) +#define CURSEG_HOT_DATA 0 + +enum + { + FI_INLINE_XATTR =3D 9, + FI_INLINE_DATA =3D 10, + FI_INLINE_DENTRY =3D 11, + FI_DATA_EXIST =3D 18, + }; + +enum FILE_TYPE + { + F2FS_FT_UNKNOWN, + F2FS_FT_REG_FILE =3D 1, + F2FS_FT_DIR =3D 2, + F2FS_FT_SYMLINK =3D 7, + }; + +struct grub_f2fs_superblock +{ + grub_uint32_t magic; + grub_uint16_t dummy1[2]; + grub_uint32_t log_sectorsize; + grub_uint32_t log_sectors_per_block; + grub_uint32_t log_blocksize; + grub_uint32_t log_blocks_per_seg; + grub_uint32_t segs_per_sec; + grub_uint32_t secs_per_zone; + grub_uint32_t checksum_offset; + grub_uint8_t dummy2[40]; + grub_uint32_t cp_blkaddr; + grub_uint32_t sit_blkaddr; + grub_uint32_t nat_blkaddr; + grub_uint32_t ssa_blkaddr; + grub_uint32_t main_blkaddr; + grub_uint32_t root_ino; + grub_uint32_t node_ino; + grub_uint32_t meta_ino; + grub_uint8_t uuid[16]; + grub_uint16_t volume_name[512]; + grub_uint32_t extension_count; + grub_uint8_t extension_list[F2FS_MAX_EXTENSION][8]; + grub_uint32_t cp_payload; + grub_uint8_t version[VERSION_LEN]; + grub_uint8_t init_version[VERSION_LEN]; +} GRUB_PACKED; + +struct grub_f2fs_checkpoint +{ + grub_uint64_t checkpoint_ver; + grub_uint64_t user_block_count; + grub_uint64_t valid_block_count; + grub_uint32_t rsvd_segment_count; + grub_uint32_t overprov_segment_count; + grub_uint32_t free_segment_count; + grub_uint32_t cur_node_segno[MAX_ACTIVE_NODE_LOGS]; + grub_uint16_t cur_node_blkoff[MAX_ACTIVE_NODE_LOGS]; + grub_uint32_t cur_data_segno[MAX_ACTIVE_DATA_LOGS]; + grub_uint16_t cur_data_blkoff[MAX_ACTIVE_DATA_LOGS]; + grub_uint32_t ckpt_flags; + grub_uint32_t cp_pack_total_block_count; + grub_uint32_t cp_pack_start_sum; + grub_uint32_t valid_node_count; + grub_uint32_t valid_inode_count; + grub_uint32_t next_free_nid; + grub_uint32_t sit_ver_bitmap_bytesize; + grub_uint32_t nat_ver_bitmap_bytesize; + grub_uint32_t checksum_offset; + grub_uint64_t elapsed_time; + grub_uint8_t alloc_type[MAX_ACTIVE_LOGS]; + grub_uint8_t sit_nat_version_bitmap[3900]; + grub_uint32_t checksum; +} GRUB_PACKED; + +struct grub_f2fs_nat_entry { + grub_uint8_t version; + grub_uint32_t ino; + grub_uint32_t block_addr; +} GRUB_PACKED; + +struct grub_f2fs_nat_jent +{ + grub_uint32_t nid; + struct grub_f2fs_nat_entry ne; +} GRUB_PACKED; + +struct grub_f2fs_nat_journal { + grub_uint16_t n_nats; + struct grub_f2fs_nat_jent entries[NAT_JOURNAL_ENTRIES]; + grub_uint8_t reserved[NAT_JOURNAL_RESERVED]; +} GRUB_PACKED; + +struct grub_f2fs_nat_block { + struct grub_f2fs_nat_entry ne[NAT_ENTRY_PER_BLOCK]; +} GRUB_PACKED; + +struct grub_f2fs_dir_entry +{ + grub_uint32_t hash_code; + grub_uint32_t ino; + grub_uint16_t name_len; + grub_uint8_t file_type; +} GRUB_PACKED; + +struct grub_f2fs_inline_dentry +{ + grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE]; + grub_uint8_t reserved[INLINE_RESERVED_SIZE]; + struct grub_f2fs_dir_entry dentry[NR_INLINE_DENTRY]; + grub_uint8_t filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN]; +} GRUB_PACKED; + +struct grub_f2fs_dentry_block { + grub_uint8_t dentry_bitmap[SIZE_OF_DENTRY_BITMAP]; + grub_uint8_t reserved[SIZE_OF_RESERVED]; + struct grub_f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK]; + grub_uint8_t filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN]; +} GRUB_PACKED; + +struct grub_f2fs_inode +{ + grub_uint16_t i_mode; + grub_uint8_t i_advise; + grub_uint8_t i_inline; + grub_uint32_t i_uid; + grub_uint32_t i_gid; + grub_uint32_t i_links; + grub_uint64_t i_size; + grub_uint64_t i_blocks; + grub_uint64_t i_atime; + grub_uint64_t i_ctime; + grub_uint64_t i_mtime; + grub_uint32_t i_atime_nsec; + grub_uint32_t i_ctime_nsec; + grub_uint32_t i_mtime_nsec; + grub_uint32_t i_generation; + grub_uint32_t i_current_depth; + grub_uint32_t i_xattr_nid; + grub_uint32_t i_flags; + grub_uint32_t i_pino; + grub_uint32_t i_namelen; + grub_uint8_t i_name[F2FS_NAME_LEN]; + grub_uint8_t i_dir_level; + grub_uint8_t i_ext[12]; + grub_uint32_t i_addr[DEF_ADDRS_PER_INODE]; + grub_uint32_t i_nid[5]; +} GRUB_PACKED; + +struct grub_direct_node { + grub_uint32_t addr[ADDRS_PER_BLOCK]; +} GRUB_PACKED; + +struct grub_indirect_node { + grub_uint32_t nid[NIDS_PER_BLOCK]; +} GRUB_PACKED; + +struct grub_f2fs_node +{ + union + { + struct grub_f2fs_inode i; + struct grub_direct_node dn; + struct grub_indirect_node in; + }; + grub_uint8_t dummy[40]; +} GRUB_PACKED; + +struct grub_fshelp_node +{ + struct grub_f2fs_data *data; + struct grub_f2fs_node inode; + grub_uint32_t ino; + int inode_read; +}; + +struct grub_f2fs_data +{ + struct grub_f2fs_superblock sblock; + struct grub_f2fs_checkpoint ckpt; + + grub_uint32_t root_ino; + grub_uint32_t blocks_per_seg; + grub_uint32_t cp_blkaddr; + grub_uint32_t nat_blkaddr; + + struct grub_f2fs_nat_journal nat_j; + char *nat_bitmap; + + grub_disk_t disk; + struct grub_f2fs_node *inode; + struct grub_fshelp_node diropen; +}; + +struct grub_f2fs_dir_iter_ctx +{ + struct grub_f2fs_data *data; + grub_fshelp_iterate_dir_hook_t hook; + void *hook_data; + grub_uint32_t *bitmap; + grub_uint8_t (*filename)[F2FS_SLOT_LEN]; + struct grub_f2fs_dir_entry *dentry; + int max; +}; + +struct grub_f2fs_dir_ctx +{ + grub_fs_dir_hook_t hook; + void *hook_data; + struct grub_f2fs_data *data; +}; + +static grub_dl_t my_mod; + +static inline int +__test_bit (int nr, grub_uint32_t *addr) +{ + return 1UL & (addr[nr / 32] >> (nr & (31))); +} + +static inline char * +__inline_addr (struct grub_f2fs_inode *inode) +{ + return (char *)&(inode->i_addr[1]); +} + +static inline grub_uint64_t +__i_size (struct grub_f2fs_inode *inode) +{ + return grub_le_to_cpu64 (inode->i_size); +} + +static inline grub_uint32_t +__start_cp_addr (struct grub_f2fs_data *data) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + grub_uint64_t ckpt_version =3D grub_le_to_cpu64 (ckpt->checkpoint_ver)= ; + grub_uint32_t start_addr =3D data->cp_blkaddr; + + if (!(ckpt_version & 1)) + return start_addr + data->blocks_per_seg; + return start_addr; +} + +static inline grub_uint32_t +__start_sum_block (struct grub_f2fs_data *data) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + + return __start_cp_addr (data) + grub_le_to_cpu32 (ckpt->cp_pack_start_= sum); +} + +static inline grub_uint32_t +__sum_blk_addr (struct grub_f2fs_data *data, int base, int type) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + + return __start_cp_addr (data) + + grub_le_to_cpu32 (ckpt->cp_pack_total_block_count) + - (base + 1) + type; +} + +static inline int +__ckpt_flag_set (struct grub_f2fs_checkpoint *ckpt, unsigned int f) +{ + grub_uint32_t ckpt_flags =3D grub_le_to_cpu32 (ckpt->ckpt_flags); + return ckpt_flags & f; +} + +static inline int +__inode_flag_set (struct grub_f2fs_inode *inode, int flag) +{ + grub_uint32_t i_flags =3D grub_le_to_cpu32 (inode->i_flags); + return __test_bit (flag, &i_flags); +} + +static inline grub_uint32_t +__nat_bitmap_size (struct grub_f2fs_data *data) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + + return grub_le_to_cpu32 (ckpt->nat_ver_bitmap_bytesize); +} + +static inline void * +__nat_bitmap_ptr (struct grub_f2fs_data *data) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + grub_uint32_t offset; + + if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0) + return ckpt->sit_nat_version_bitmap; + + offset =3D grub_le_to_cpu32 (ckpt->sit_ver_bitmap_bytesize); + return ckpt->sit_nat_version_bitmap + offset; +} + +static inline grub_uint32_t +__get_node_id (struct grub_f2fs_node *rn, int off, int i) +{ + if (i) + return grub_le_to_cpu32 (rn->i.i_nid[off - NODE_DIR1_BLOCK]); + return grub_le_to_cpu32 (rn->in.nid[off]); +} + +static inline grub_err_t +grub_f2fs_block_read (struct grub_f2fs_data *data, grub_uint32_t blkaddr= , void *buf) +{ + return grub_disk_read (data->disk, blkaddr << F2FS_BLK_SEC_BITS, + 0, F2FS_BLKSIZE, buf); +} + +/* + * CRC32 + */ +#define CRCPOLY_LE 0xedb88320 + +static inline grub_uint32_t +grub_f2fs_cal_crc32 (grub_uint32_t crc, void *buf, int len) +{ + int i; + unsigned char *p =3D (unsigned char *)buf; + + while (len--) + { + crc ^=3D *p++; + for (i =3D 0; i < 8; i++) + crc =3D (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); + } + return crc; +} + +static inline int +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, int len) +{ + grub_uint32_t cal_crc =3D 0; + + cal_crc =3D grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len); + + return (cal_crc =3D=3D blk_crc) ? 1 : 0; +} + +static inline int +grub_f2fs_test_bit (grub_uint32_t nr, const char *p) +{ + int mask; + char *addr =3D (char *)p; + + addr +=3D (nr >> 3); + mask =3D 1 << (7 - (nr & 0x07)); + return (mask & *addr) !=3D 0; +} + +static int +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb) +{ + unsigned int blocksize; + + if (F2FS_SUPER_MAGIC !=3D grub_le_to_cpu32 (sb->magic)) + return -1; + + blocksize =3D 1 << grub_le_to_cpu32 (sb->log_blocksize); + if (blocksize !=3D F2FS_BLKSIZE) + return -1; + + if (grub_le_to_cpu32 (sb->log_sectorsize) > F2FS_MAX_LOG_SECTOR_SIZE) + return -1; + + if (grub_le_to_cpu32 (sb->log_sectorsize) < F2FS_MIN_LOG_SECTOR_SIZE) + return -1; + + if (grub_le_to_cpu32 (sb->log_sectors_per_block) + + grub_le_to_cpu32 (sb->log_sectorsize) !=3D F2FS_MAX_LOG_SECTOR_SIZ= E) + return -1; + + return 0; +} + +static grub_err_t +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block) +{ + grub_disk_t disk =3D data->disk; + grub_uint64_t offset; + grub_err_t err; + + if (block =3D=3D 0) + offset =3D F2FS_SUPER_OFFSET; + else + offset =3D F2FS_BLKSIZE + F2FS_SUPER_OFFSET; + + /* Read first super block. */ + err =3D grub_disk_read (disk, offset >> GRUB_DISK_SECTOR_BITS, 0, + sizeof (data->sblock), &data->sblock); + if (err) + return err; + + if (grub_f2fs_sanity_check_sb (&data->sblock)) + err =3D GRUB_ERR_BAD_FS; + + return err; +} + +static void * +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr, + grub_uint64_t *version) +{ + void *cp_page_1, *cp_page_2; + struct grub_f2fs_checkpoint *cp_block; + grub_uint64_t cur_version =3D 0, pre_version =3D 0; + grub_uint32_t crc =3D 0; + grub_uint32_t crc_offset; + grub_err_t err; + + /* Read the 1st cp block in this CP pack */ + cp_page_1 =3D grub_malloc (F2FS_BLKSIZE); + if (!cp_page_1) + return NULL; + + err =3D grub_f2fs_block_read (data, cp_addr, cp_page_1); + if (err) + goto invalid_cp1; + + cp_block =3D (struct grub_f2fs_checkpoint *)cp_page_1; + crc_offset =3D grub_le_to_cpu32 (cp_block->checksum_offset); + if (crc_offset >=3D F2FS_BLKSIZE) + goto invalid_cp1; + + crc =3D *(grub_uint32_t *)((char *)cp_block + crc_offset); + if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset)) + goto invalid_cp1; + + pre_version =3D grub_le_to_cpu64 (cp_block->checkpoint_ver); + + /* Read the 2nd cp block in this CP pack */ + cp_page_2 =3D grub_malloc (F2FS_BLKSIZE); + if (!cp_page_2) + goto invalid_cp1; + + cp_addr +=3D grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - = 1; + + err =3D grub_f2fs_block_read (data, cp_addr, cp_page_2); + if (err) + goto invalid_cp2; + + cp_block =3D (struct grub_f2fs_checkpoint *)cp_page_2; + crc_offset =3D grub_le_to_cpu32 (cp_block->checksum_offset); + if (crc_offset >=3D F2FS_BLKSIZE) + goto invalid_cp2; + + crc =3D *(grub_uint32_t *)((char *)cp_block + crc_offset); + if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset)) + goto invalid_cp2; + + cur_version =3D grub_le_to_cpu64 (cp_block->checkpoint_ver); + if (cur_version =3D=3D pre_version) + { + *version =3D cur_version; + grub_free (cp_page_2); + return cp_page_1; + } + +invalid_cp2: + grub_free (cp_page_2); +invalid_cp1: + grub_free (cp_page_1); + return NULL; +} + +static grub_err_t +grub_f2fs_read_cp (struct grub_f2fs_data *data) +{ + void *cp1, *cp2, *cur_page; + grub_uint64_t cp1_version =3D 0, cp2_version =3D 0; + grub_uint64_t cp_start_blk_no; + + /* + * Finding out valid cp block involves read both + * sets (cp pack1 and cp pack 2) + */ + cp_start_blk_no =3D data->cp_blkaddr; + cp1 =3D validate_checkpoint (data, cp_start_blk_no, &cp1_version); + if (!cp1 && grub_errno) + return grub_errno; + + /* The second checkpoint pack should start at the next segment */ + cp_start_blk_no +=3D data->blocks_per_seg; + cp2 =3D validate_checkpoint (data, cp_start_blk_no, &cp2_version); + if (!cp2 && grub_errno) + { + grub_free (cp1); + return grub_errno; + } + + if (cp1 && cp2) + cur_page =3D (cp2_version > cp1_version) ? cp2 : cp1; + else if (cp1) + cur_page =3D cp1; + else if (cp2) + cur_page =3D cp2; + else + return grub_error (GRUB_ERR_BAD_FS, "no checkpoints\n"); + + grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE); + + grub_free (cp1); + grub_free (cp2); + return 0; +} + +static int +get_nat_journal (struct grub_f2fs_data *data) +{ + grub_uint32_t block; + char *buf; + grub_err_t err; + + buf =3D grub_malloc (F2FS_BLKSIZE); + if (!buf) + return grub_errno; + + if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG)) + block =3D __start_sum_block (data); + else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG)) + block =3D __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA); + else + block =3D __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA= ); + + err =3D grub_f2fs_block_read (data, block, buf); + if (err) + goto fail; + + if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG)) + grub_memcpy (&data->nat_j, buf, SUM_JOURNAL_SIZE); + else + grub_memcpy (&data->nat_j, buf + SUM_ENTRIES_SIZE, SUM_JOURNAL_SIZE)= ; + +fail: + grub_free (buf); + return err; +} + +static grub_uint32_t +get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t= nid) +{ + grub_uint16_t n =3D grub_le_to_cpu16 (data->nat_j.n_nats); + grub_uint32_t blkaddr =3D 0; + grub_uint16_t i; + + for (i =3D 0; i < n; i++) + { + if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) =3D=3D nid) + { + blkaddr =3D grub_le_to_cpu32 (data->nat_j.entries[i].ne.block_= addr); + break; + } + } + return blkaddr; +} + +static grub_uint32_t +get_node_blkaddr (struct grub_f2fs_data *data, grub_uint32_t nid) +{ + struct grub_f2fs_nat_block *nat_block; + grub_uint32_t seg_off, block_off, entry_off, block_addr; + grub_uint32_t blkaddr; + grub_err_t err; + + blkaddr =3D get_blkaddr_from_nat_journal (data, nid); + if (blkaddr) + return blkaddr; + + nat_block =3D grub_malloc (F2FS_BLKSIZE); + if (!nat_block) + return 0; + + block_off =3D nid / NAT_ENTRY_PER_BLOCK; + entry_off =3D nid % NAT_ENTRY_PER_BLOCK; + + seg_off =3D block_off / data->blocks_per_seg; + block_addr =3D data->nat_blkaddr + + ((seg_off * data->blocks_per_seg) << 1) + + (block_off & (data->blocks_per_seg - 1)); + + if (grub_f2fs_test_bit (block_off, data->nat_bitmap)) + block_addr +=3D data->blocks_per_seg; + + err =3D grub_f2fs_block_read (data, block_addr, nat_block); + if (err) + { + grub_free (nat_block); + return 0; + } + + blkaddr =3D grub_le_to_cpu32 (nat_block->ne[entry_off].block_addr); + + grub_free (nat_block); + + return blkaddr; +} + +static int +grub_get_node_path (struct grub_f2fs_inode *inode, grub_uint32_t block, + grub_uint32_t offset[4], grub_uint32_t noffset[4]) +{ + grub_uint32_t direct_blks =3D ADDRS_PER_BLOCK; + grub_uint32_t dptrs_per_blk =3D NIDS_PER_BLOCK; + grub_uint32_t indirect_blks =3D ADDRS_PER_BLOCK * NIDS_PER_BLOCK; + grub_uint32_t dindirect_blks =3D indirect_blks * NIDS_PER_BLOCK; + grub_uint32_t direct_index =3D DEF_ADDRS_PER_INODE; + int n =3D 0; + int level =3D 0; + + if (__inode_flag_set (inode, FI_INLINE_XATTR)) + direct_index -=3D F2FS_INLINE_XATTR_ADDRS; + + noffset[0] =3D 0; + + if (block < direct_index) + { + offset[n] =3D block; + goto got; + } + + block -=3D direct_index; + if (block < direct_blks) + { + offset[n++] =3D NODE_DIR1_BLOCK; + noffset[n] =3D 1; + offset[n] =3D block; + level =3D 1; + goto got; + } + + block -=3D direct_blks; + if (block < direct_blks) + { + offset[n++] =3D NODE_DIR2_BLOCK; + noffset[n] =3D 2; + offset[n] =3D block; + level =3D 1; + goto got; + } + + block -=3D direct_blks; + if (block < indirect_blks) + { + offset[n++] =3D NODE_IND1_BLOCK; + noffset[n] =3D 3; + offset[n++] =3D block / direct_blks; + noffset[n] =3D 4 + offset[n - 1]; + offset[n] =3D block % direct_blks; + level =3D 2; + goto got; + } + + block -=3D indirect_blks; + if (block < indirect_blks) + { + offset[n++] =3D NODE_IND2_BLOCK; + noffset[n] =3D 4 + dptrs_per_blk; + offset[n++] =3D block / direct_blks; + noffset[n] =3D 5 + dptrs_per_blk + offset[n - 1]; + offset[n] =3D block % direct_blks; + level =3D 2; + goto got; + } + + block -=3D indirect_blks; + if (block < dindirect_blks) + { + offset[n++] =3D NODE_DIND_BLOCK; + noffset[n] =3D 5 + (dptrs_per_blk * 2); + offset[n++] =3D block / indirect_blks; + noffset[n] =3D 6 + (dptrs_per_blk * 2) + + offset[n - 1] * (dptrs_per_blk + 1); + offset[n++] =3D (block / direct_blks) % dptrs_per_blk; + noffset[n] =3D 7 + (dptrs_per_blk * 2) + + offset[n - 2] * (dptrs_per_blk + 1) + + offset[n - 1]; + offset[n] =3D block % direct_blks; + level =3D 3; + goto got; + } +got: + return level; +} + + +static grub_err_t +load_nat_info (struct grub_f2fs_data *data) +{ + void *version_bitmap; + grub_err_t err; + + data->nat_bitmap =3D grub_malloc (__nat_bitmap_size (data)); + if (!data->nat_bitmap) + return grub_errno; + + version_bitmap =3D __nat_bitmap_ptr (data); + + /* copy version bitmap */ + grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data= )); + + err =3D get_nat_journal (data); + if (err) + grub_free (data->nat_bitmap); + + return err; +} + +static grub_err_t +grub_f2fs_read_node (struct grub_f2fs_data *data, + grub_uint32_t nid, struct grub_f2fs_node *np) +{ + grub_uint32_t blkaddr; + + blkaddr =3D get_node_blkaddr (data, nid); + if (!blkaddr) + return grub_errno; + + return grub_f2fs_block_read (data, blkaddr, np); +} + +static struct grub_f2fs_data * +grub_f2fs_mount (grub_disk_t disk) +{ + struct grub_f2fs_data *data; + grub_err_t err; + + data =3D grub_zalloc (sizeof (*data)); + if (!data) + return NULL; + + data->disk =3D disk; + + err =3D grub_f2fs_read_sb (data, 0); + if (err) + { + err =3D grub_f2fs_read_sb (data, 1); + if (err) + { + grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem"); + goto fail; + } + } + + data->root_ino =3D grub_le_to_cpu32 (data->sblock.root_ino); + data->cp_blkaddr =3D grub_le_to_cpu32 (data->sblock.cp_blkaddr); + data->nat_blkaddr =3D grub_le_to_cpu32 (data->sblock.nat_blkaddr); + data->blocks_per_seg =3D 1 << + grub_le_to_cpu32 (data->sblock.log_blocks_per_seg); + + err =3D grub_f2fs_read_cp (data); + if (err) + goto fail; + + err =3D load_nat_info (data); + if (err) + goto fail; + + data->diropen.data =3D data; + data->diropen.ino =3D data->root_ino; + data->diropen.inode_read =3D 1; + data->inode =3D &data->diropen.inode; + + err =3D grub_f2fs_read_node (data, data->root_ino, data->inode); + if (err) + goto fail; + + return data; + +fail: + if (data) + grub_free (data->nat_bitmap); + grub_free (data); + return NULL; +} + +/* guarantee inline_data was handled by caller */ +static grub_disk_addr_t +grub_f2fs_read_block (grub_fshelp_node_t node, grub_disk_addr_t block_of= s) +{ + struct grub_f2fs_data *data =3D node->data; + struct grub_f2fs_inode *inode =3D &node->inode.i; + grub_uint32_t offset[4], noffset[4], nids[4]; + struct grub_f2fs_node *node_block; + grub_uint32_t block_addr =3D -1; + int level, i; + + level =3D grub_get_node_path (inode, block_ofs, offset, noffset); + if (level =3D=3D 0) + return grub_le_to_cpu32 (inode->i_addr[offset[0]]); + + node_block =3D grub_malloc (F2FS_BLKSIZE); + if (!node_block) + return -1; + + nids[1] =3D __get_node_id (&node->inode, offset[0], 1); + + /* get indirect or direct nodes */ + for (i =3D 1; i <=3D level; i++) + { + grub_f2fs_read_node (data, nids[i], node_block); + if (grub_errno) + goto fail; + + if (i < level) + nids[i + 1] =3D __get_node_id (node_block, offset[i], 0); + } + + block_addr =3D grub_le_to_cpu32 (node_block->dn.addr[offset[level]]); +fail: + grub_free (node_block); + return block_addr; +} + +static grub_ssize_t +grub_f2fs_read_file (grub_fshelp_node_t node, + grub_disk_read_hook_t read_hook, void *read_hook_data, + grub_off_t pos, grub_size_t len, char *buf) +{ + struct grub_f2fs_inode *inode =3D &(node->inode.i); + grub_off_t filesize =3D __i_size (inode); + char *inline_addr =3D __inline_addr (inode); + + if (__inode_flag_set (&node->inode.i, FI_INLINE_DATA)) + { + if (pos > filesize || filesize > MAX_INLINE_DATA) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, + N_("attempt to read past the end of file")); + return -1; + } + if (pos + len > filesize) + len =3D filesize - pos; + + grub_memcpy (buf + pos, inline_addr + pos, len); + return len; + } + + return grub_fshelp_read_file (node->data->disk, node, + read_hook, read_hook_data, + pos, len, buf, grub_f2fs_read_block, + filesize, + F2FS_BLK_SEC_BITS, 0); +} + +static char * +grub_f2fs_read_symlink (grub_fshelp_node_t node) +{ + char *symlink; + struct grub_fshelp_node *diro =3D node; + + if (!diro->inode_read) + { + grub_f2fs_read_node (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + symlink =3D grub_malloc (__i_size (&diro->inode.i) + 1); + if (!symlink) + return 0; + + grub_f2fs_read_file (diro, 0, 0, 0, __i_size (&diro->inode.i), symlink= ); + if (grub_errno) + { + grub_free (symlink); + return 0; + } + + symlink[__i_size (&diro->inode.i)] =3D '\0'; + return symlink; +} + +static int +grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx) +{ + struct grub_fshelp_node *fdiro; + int i; + + for (i =3D 0; i < ctx->max;) + { + char filename[F2FS_NAME_LEN + 1]; + enum grub_fshelp_filetype type =3D GRUB_FSHELP_UNKNOWN; + enum FILE_TYPE ftype; + int name_len; + + if (__test_bit (i, ctx->bitmap) =3D=3D 0) + { + i++; + continue; + } + + ftype =3D ctx->dentry[i].file_type; + name_len =3D grub_le_to_cpu16 (ctx->dentry[i].name_len); + grub_memcpy (filename, ctx->filename[i], name_len); + filename[name_len] =3D '\0'; + + fdiro =3D grub_malloc (sizeof (struct grub_fshelp_node)); + if (!fdiro) + return 0; + + if (ftype =3D=3D F2FS_FT_DIR) + type =3D GRUB_FSHELP_DIR; + else if (ftype =3D=3D F2FS_FT_SYMLINK) + type =3D GRUB_FSHELP_SYMLINK; + else if (ftype =3D=3D F2FS_FT_REG_FILE) + type =3D GRUB_FSHELP_REG; + + fdiro->data =3D ctx->data; + fdiro->ino =3D grub_le_to_cpu32 (ctx->dentry[i].ino); + fdiro->inode_read =3D 0; + + if (ctx->hook (filename, type, fdiro, ctx->hook_data)) + return 1; + + i +=3D (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN; + } + return 0; +} + +static int +grub_f2fs_iterate_inline_dir (struct grub_f2fs_inode *dir, + struct grub_f2fs_dir_iter_ctx *ctx) +{ + struct grub_f2fs_inline_dentry *de_blk; + + de_blk =3D (struct grub_f2fs_inline_dentry *) __inline_addr (dir); + + ctx->bitmap =3D (grub_uint32_t *) de_blk->dentry_bitmap; + ctx->dentry =3D de_blk->dentry; + ctx->filename =3D de_blk->filename; + ctx->max =3D NR_INLINE_DENTRY; + + return grub_f2fs_check_dentries (ctx); +} + +static int +grub_f2fs_iterate_dir (grub_fshelp_node_t dir, + grub_fshelp_iterate_dir_hook_t hook, void *hook_data) +{ + struct grub_fshelp_node *diro =3D (struct grub_fshelp_node *) dir; + struct grub_f2fs_inode *inode; + struct grub_f2fs_dir_iter_ctx ctx =3D { + .data =3D diro->data, + .hook =3D hook, + .hook_data =3D hook_data + }; + grub_off_t fpos =3D 0; + + if (!diro->inode_read) + { + grub_f2fs_read_node (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + inode =3D &diro->inode.i; + + if (__inode_flag_set (inode, FI_INLINE_DENTRY)) + return grub_f2fs_iterate_inline_dir (inode, &ctx); + + while (fpos < __i_size (inode)) + { + struct grub_f2fs_dentry_block *de_blk; + char *buf; + + buf =3D grub_zalloc (F2FS_BLKSIZE); + if (!buf) + return 0; + + grub_f2fs_read_file (diro, 0, 0, fpos, F2FS_BLKSIZE, buf); + if (grub_errno) + { + grub_free (buf); + return 0; + } + + de_blk =3D (struct grub_f2fs_dentry_block *) buf; + + ctx.bitmap =3D (grub_uint32_t *) de_blk->dentry_bitmap; + ctx.dentry =3D de_blk->dentry; + ctx.filename =3D de_blk->filename; + ctx.max =3D NR_DENTRY_IN_BLOCK; + + if (grub_f2fs_check_dentries (&ctx)) + return 1; + + grub_free (buf); + + fpos +=3D F2FS_BLKSIZE; + } + return 0; +} + +static int +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype file= type, + grub_fshelp_node_t node, void *data) +{ + struct grub_f2fs_dir_ctx *ctx =3D data; + struct grub_dirhook_info info; + + grub_memset (&info, 0, sizeof (info)); + if (!node->inode_read) + { + grub_f2fs_read_node (ctx->data, node->ino, &node->inode); + if (!grub_errno) + node->inode_read =3D 1; + grub_errno =3D GRUB_ERR_NONE; + } + if (node->inode_read) + { + info.mtimeset =3D 1; + info.mtime =3D grub_le_to_cpu64 (node->inode.i.i_mtime); + } + + info.dir =3D ((filetype & GRUB_FSHELP_TYPE_MASK) =3D=3D GRUB_FSHELP_DI= R); + grub_free (node); + return ctx->hook (filename, &info, ctx->hook_data); +} + +static grub_err_t +grub_f2fs_dir (grub_device_t device, const char *path, + grub_fs_dir_hook_t hook, void *hook_data) +{ + struct grub_f2fs_dir_ctx ctx =3D { + .hook =3D hook, + .hook_data =3D hook_data + }; + struct grub_fshelp_node *fdiro =3D 0; + + grub_dl_ref (my_mod); + + ctx.data =3D grub_f2fs_mount (device->disk); + if (!ctx.data) + goto fail; + + grub_fshelp_find_file (path, &ctx.data->diropen, &fdiro, + grub_f2fs_iterate_dir, grub_f2fs_read_symlink, + GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_f2fs_iterate_dir (fdiro, grub_f2fs_dir_iter, &ctx); + +fail: + if (fdiro !=3D &ctx.data->diropen) + grub_free (fdiro); + if (ctx.data) + grub_free (ctx.data->nat_bitmap); + grub_free (ctx.data); + grub_dl_unref (my_mod); + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_f2fs_open (struct grub_file *file, const char *name) +{ + struct grub_f2fs_data *data =3D NULL; + struct grub_fshelp_node *fdiro =3D 0; + + grub_dl_ref (my_mod); + + data =3D grub_f2fs_mount (file->device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (name, &data->diropen, &fdiro, + grub_f2fs_iterate_dir, grub_f2fs_read_symlink, + GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + if (!fdiro->inode_read) + { + grub_f2fs_read_node (data, fdiro->ino, &fdiro->inode); + if (grub_errno) + goto fail; + } + + grub_memcpy (data->inode, &fdiro->inode, F2FS_BLKSIZE); + grub_free (fdiro); + + file->size =3D __i_size (&(data->inode->i)); + file->data =3D data; + file->offset =3D 0; + + return 0; + +fail: + if (fdiro !=3D &data->diropen) + grub_free (fdiro); + if (data) + grub_free (data->nat_bitmap); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_ssize_t +grub_f2fs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_f2fs_data *data =3D (struct grub_f2fs_data *) file->data; + + return grub_f2fs_read_file (&data->diropen, + file->read_hook, file->read_hook_data, + file->offset, len, buf); +} + +static grub_err_t +grub_f2fs_close (grub_file_t file) +{ + struct grub_f2fs_data *data =3D (struct grub_f2fs_data *) file->data; + + if (data) + grub_free (data->nat_bitmap); + grub_free (data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_f2fs_label (grub_device_t device, char **label) +{ + struct grub_f2fs_data *data; + grub_disk_t disk =3D device->disk; + + grub_dl_ref (my_mod); + + data =3D grub_f2fs_mount (disk); + if (data) + { + *label =3D grub_zalloc (sizeof (data->sblock.volume_name)); + grub_utf16_to_utf8 ((grub_uint8_t *) (*label), + data->sblock.volume_name, 512); + } + else + *label =3D NULL; + + if (data) + grub_free (data->nat_bitmap); + grub_free (data); + grub_dl_unref (my_mod); + return grub_errno; +} + +static grub_err_t +grub_f2fs_uuid (grub_device_t device, char **uuid) +{ + struct grub_f2fs_data *data; + grub_disk_t disk =3D device->disk; + + grub_dl_ref (my_mod); + + data =3D grub_f2fs_mount (disk); + if (data) + { + *uuid =3D + grub_xasprintf + ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"= , + data->sblock.uuid[0], data->sblock.uuid[1], + data->sblock.uuid[2], data->sblock.uuid[3], + data->sblock.uuid[4], data->sblock.uuid[5], + data->sblock.uuid[6], data->sblock.uuid[7], + data->sblock.uuid[8], data->sblock.uuid[9], + data->sblock.uuid[10], data->sblock.uuid[11], + data->sblock.uuid[12], data->sblock.uuid[13], + data->sblock.uuid[14], data->sblock.uuid[15]); + } + else + *uuid =3D NULL; + + if (data) + grub_free (data->nat_bitmap); + grub_free (data); + grub_dl_unref (my_mod); + return grub_errno; +} + +static struct grub_fs grub_f2fs_fs =3D { + .name =3D "f2fs", + .dir =3D grub_f2fs_dir, + .open =3D grub_f2fs_open, + .read =3D grub_f2fs_read, + .close =3D grub_f2fs_close, + .label =3D grub_f2fs_label, + .uuid =3D grub_f2fs_uuid, +#ifdef GRUB_UTIL + .reserved_first_sector =3D 1, + .blocklist_install =3D 0, +#endif + .next =3D 0 +}; + +GRUB_MOD_INIT (f2fs) +{ + grub_fs_register (&grub_f2fs_fs); + my_mod =3D mod; +} + +GRUB_MOD_FINI (f2fs) +{ + grub_fs_unregister (&grub_f2fs_fs); +} diff --git a/po/exclude.pot b/po/exclude.pot index ab52e99..a517fdf 100644 --- a/po/exclude.pot +++ b/po/exclude.pot @@ -1214,6 +1214,7 @@ msgstr "" =20 #: grub-core/commands/xnu_uuid.c:75 grub-core/fs/jfs.c:924 #: grub-core/fs/nilfs2.c:1135 +#: grub-core/fs/f2fs.c:1259 #, c-format msgid "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%= 02x" msgstr "" diff --git a/tests/f2fs_test.in b/tests/f2fs_test.in new file mode 100644 index 0000000..98efda6 --- /dev/null +++ b/tests/f2fs_test.in @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e + +if [ "x$EUID" =3D "x" ] ; then + EUID=3D`id -u` +fi + +if [ "$EUID" !=3D 0 ] ; then + exit 77 +fi + +if ! which mkfs.f2fs >/dev/null 2>&1; then + echo "mkfs.f2fs not installed; cannot test f2fs." + exit 77 +fi + + +"@builddir@/grub-fs-tester" f2fs diff --git a/tests/util/grub-fs-tester.in b/tests/util/grub-fs-tester.in index e9e85c2..acc35cc 100644 --- a/tests/util/grub-fs-tester.in +++ b/tests/util/grub-fs-tester.in @@ -36,7 +36,7 @@ case x"$fs" in MINLOGSECSIZE=3D8 # OS LIMITATION: It could go up to 32768 but Linux rejects sector = sizes > 4096 MAXLOGSECSIZE=3D12;; - xxfs) + xxfs|xf2fs) MINLOGSECSIZE=3D9 # OS LIMITATION: GNU/Linux doesn't accept > 4096 MAXLOGSECSIZE=3D12;; @@ -135,6 +135,10 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLO= GSECSIZE;LOGSECSIZE=3DLOGSECSIZE + fi MAXBLKSIZE=3D4096 ;; + xf2fs) + MINBLKSIZE=3D$SECSIZE + # OS Limitation: GNU/Linux doesn't accept > 4096 + MAXBLKSIZE=3D4096;; xsquash*) MINBLKSIZE=3D4096 MAXBLKSIZE=3D1048576;; @@ -256,7 +260,9 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLOG= SECSIZE;LOGSECSIZE=3DLOGSECSIZE + # FS LIMITATION: btrfs label is at most 255 UTF-8 chars x"btrfs"*) FSLABEL=3D"grub_;/test=C3=A9=F0=AF=A6=9B=F0=AF=A6=9D=F0=9F=98=81=D0= =BA=D0=B8=D1=80=D0=B8=D1=82i urewfceniuewruevrewnuuireurevueurnievrewfner= fcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvew= nvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoir= oireoireoifrefoieroifoireoif";; - + # FS LIMITATION: btrfs label is at most 512 UTF-16 chars + x"f2fs") + FSLABEL=3D"grub_;/testjaegeuk kim f2fsaskdfjkasdlfajskdfjaksdjfkja= skjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkh= akjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkx= chvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjk= dhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";; # FS LIMITATION: exfat is at most 15 UTF-16 chars x"exfat") FSLABEL=3D"g=C3=A9=D1=82 ;/=F0=AF=A6=9B=F0=AF=A6=9D=F0=9F=98=81=D0= =BA=D0=B8=D1=80";; @@ -466,7 +472,7 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLOG= SECSIZE;LOGSECSIZE=3DLOGSECSIZE + # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check i= t. # FS LIMITATION: as far as I know those FS don't store their last modif= ication date. x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reis= erfs" \ - | x"bfs" | x"afs" \ + | x"bfs" | x"afs" | x"f2fs" \ | x"tarfs" | x"cpio_"* | x"minix" | x"minix2" \ | x"minix3" | x"ntfs"* | x"udf" | x"sfs"*) NOFSTIME=3Dy;; @@ -745,6 +751,8 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLOG= SECSIZE;LOGSECSIZE=3DLOGSECSIZE + MOUNTDEVICE=3D"/dev/mapper/grub_test-testvol" MOUNTFS=3Dext2 "mkfs.ext2" -L "$FSLABEL" -q "${MOUNTDEVICE}" ;; + xf2fs) + "mkfs.f2fs" -l "$FSLABEL" -q "${LODEVICES[0]}" ;; xnilfs2) "mkfs.nilfs2" -L "$FSLABEL" -b $BLKSIZE -q "${LODEVICES[0]}" ;; xext2_old) --=20 2.1.1