All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] F2FS support
@ 2015-03-24  8:19 ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-03-24  8:19 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel; +Cc: Jaegeuk Kim

 * 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 <jaegeuk@kernel.org>
---
 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 <jaegeuk@kernel.org>
+
+	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  <phcoder@gmail.com>
 
 	* 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 = {
   common = grub-core/fs/newc.c;
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/exfat.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
@@ -767,6 +768,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = 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 filesystem 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}, @dfn{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, minix,
 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 charset
 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 = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = exfat;
   common = 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 <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+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 = 9,
+    FI_INLINE_DATA = 10,
+    FI_INLINE_DENTRY = 11,
+    FI_DATA_EXIST = 18,
+  };
+
+enum FILE_TYPE
+  {
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 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 = &data->ckpt;
+  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
+  grub_uint32_t start_addr = 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 = &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 = &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 = 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 = 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 = &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 = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 = (unsigned char *)buf;
+
+  while (len--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (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 = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+  char *addr = (char *)p;
+
+  addr += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return (mask & *addr) != 0;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  unsigned int blocksize;
+
+  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))
+    return -1;
+
+  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
+  if (blocksize != 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) != F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static grub_err_t
+grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
+{
+  grub_disk_t disk = data->disk;
+  grub_uint64_t offset;
+  grub_err_t err;
+
+  if (block == 0)
+    offset = F2FS_SUPER_OFFSET;
+  else
+    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
+
+  /* Read first super block. */
+  err = 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 = 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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp1;
+
+  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp2;
+
+  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = 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 = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (__inode_flag_set (inode, FI_INLINE_XATTR))
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+		offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) +
+		offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 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 = grub_malloc (__nat_bitmap_size (data));
+  if (!data->nat_bitmap)
+    return grub_errno;
+
+  version_bitmap = __nat_bitmap_ptr (data);
+
+  /* copy version bitmap */
+  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
+
+  err = 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 = 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 = grub_zalloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  err = grub_f2fs_read_sb (data, 0);
+  if (err)
+    {
+      err = grub_f2fs_read_sb (data, 1);
+      if (err)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");
+          goto fail;
+	}
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  err = load_nat_info (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = 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_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &(node->inode.i);
+  grub_off_t filesize = __i_size (inode);
+  char *inline_addr = __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 = 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 = node;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  symlink = 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)] = '\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 = 0; i < ctx->max;)
+    {
+      char filename[F2FS_NAME_LEN + 1];
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+
+      if (__test_bit (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = '\0';
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        return 0;
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      if (grub_f2fs_check_dentries (&ctx))
+        return 1;
+
+      grub_free (buf);
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+		      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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 = __i_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &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 = (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 = (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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *label = grub_zalloc (sizeof (data->sblock.volume_name));
+      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),
+				data->sblock.volume_name, 512);
+    }
+  else
+    *label = 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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = 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 = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = 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 ""
 
 #: 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" = "x" ] ; then
+  EUID=`id -u`
+fi
+
+if [ "$EUID" != 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=8
 	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
 	MAXLOGSECSIZE=12;;
-    xxfs)
+    xxfs|xf2fs)
 	MINLOGSECSIZE=9
   	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
 	MAXLOGSECSIZE=12;;
@@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    fi
 	    MAXBLKSIZE=4096
 	    ;;
+	xf2fs)
+	    MINBLKSIZE=$SECSIZE
+		# OS Limitation: GNU/Linux doesn't accept > 4096
+	    MAXBLKSIZE=4096;;
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
@@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
-
+	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testjaegeuk kim f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.1.1


------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* [PATCH] F2FS support
@ 2015-03-24  8:19 ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-03-24  8:19 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel; +Cc: Jaegeuk Kim

 * 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 <jaegeuk@kernel.org>
---
 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 <jaegeuk@kernel.org>
+
+	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  <phcoder@gmail.com>
 
 	* 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 = {
   common = grub-core/fs/newc.c;
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/exfat.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
@@ -767,6 +768,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = 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 filesystem 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}, @dfn{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, minix,
 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 charset
 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 = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = exfat;
   common = 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 <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+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 = 9,
+    FI_INLINE_DATA = 10,
+    FI_INLINE_DENTRY = 11,
+    FI_DATA_EXIST = 18,
+  };
+
+enum FILE_TYPE
+  {
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 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 = &data->ckpt;
+  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
+  grub_uint32_t start_addr = 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 = &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 = &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 = 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 = 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 = &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 = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 = (unsigned char *)buf;
+
+  while (len--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (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 = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+  char *addr = (char *)p;
+
+  addr += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return (mask & *addr) != 0;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  unsigned int blocksize;
+
+  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))
+    return -1;
+
+  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
+  if (blocksize != 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) != F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static grub_err_t
+grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
+{
+  grub_disk_t disk = data->disk;
+  grub_uint64_t offset;
+  grub_err_t err;
+
+  if (block == 0)
+    offset = F2FS_SUPER_OFFSET;
+  else
+    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
+
+  /* Read first super block. */
+  err = 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 = 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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp1;
+
+  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp2;
+
+  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = 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 = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (__inode_flag_set (inode, FI_INLINE_XATTR))
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+		offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) +
+		offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 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 = grub_malloc (__nat_bitmap_size (data));
+  if (!data->nat_bitmap)
+    return grub_errno;
+
+  version_bitmap = __nat_bitmap_ptr (data);
+
+  /* copy version bitmap */
+  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
+
+  err = 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 = 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 = grub_zalloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  err = grub_f2fs_read_sb (data, 0);
+  if (err)
+    {
+      err = grub_f2fs_read_sb (data, 1);
+      if (err)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");
+          goto fail;
+	}
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  err = load_nat_info (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = 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_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &(node->inode.i);
+  grub_off_t filesize = __i_size (inode);
+  char *inline_addr = __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 = 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 = node;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  symlink = 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)] = '\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 = 0; i < ctx->max;)
+    {
+      char filename[F2FS_NAME_LEN + 1];
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+
+      if (__test_bit (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = '\0';
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        return 0;
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      if (grub_f2fs_check_dentries (&ctx))
+        return 1;
+
+      grub_free (buf);
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+		      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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 = __i_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &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 = (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 = (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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *label = grub_zalloc (sizeof (data->sblock.volume_name));
+      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),
+				data->sblock.volume_name, 512);
+    }
+  else
+    *label = 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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = 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 = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = 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 ""
 
 #: 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" = "x" ] ; then
+  EUID=`id -u`
+fi
+
+if [ "$EUID" != 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=8
 	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
 	MAXLOGSECSIZE=12;;
-    xxfs)
+    xxfs|xf2fs)
 	MINLOGSECSIZE=9
   	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
 	MAXLOGSECSIZE=12;;
@@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    fi
 	    MAXBLKSIZE=4096
 	    ;;
+	xf2fs)
+	    MINBLKSIZE=$SECSIZE
+		# OS Limitation: GNU/Linux doesn't accept > 4096
+	    MAXBLKSIZE=4096;;
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
@@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
-
+	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testjaegeuk kim f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.1.1



^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2015-03-24  8:19 ` Jaegeuk Kim
@ 2015-03-28  7:31   ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-03-28  7:31 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Tue, 24 Mar 2015 01:19:00 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

>  * 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.
> 

It's not the most useful commit message. Better would be short
explanation of use cases and intended platforms. I'm curious here -
F2FS is intended for raw flash access, on which platform(s) grub has
access to such devices? 

> 
> diff --git a/ChangeLog-2015 b/ChangeLog-2015

We do not use ChangeLog any more, it is autogenerated from commits.
This file is legacy before this change, do not change it.

> 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 <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +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

(2 << F2FS_BLK_BITS)?

> +#define F2FS_BLK_BITS			12
> +#define F2FS_BLK_SEC_BITS		(3)


It is confusing to have some defines parenthesized and some not. Could
it be unified somehow?

Also this can be computed from F2FS_BLK_BITS and GRUB_DISK_SECTOR_BITS
- one magic number less.

...
> +struct grub_f2fs_inline_dentry
> +{
> +  grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];

This is cast to grub_uint32_t everywhere. Can it be non-multiple of 4
bytes? If not, may be just define as such? 

> +  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];

ditto

> +  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;
> +


...
> +
> +#define ver_after (a, b) (typecheck (unsigned long long, a) &&          \
> +		typecheck (unsigned long long, b) &&                    \
> +		((long long)((a) - (b)) > 0))
> +

Where typecheck definition comes from?

...
> +
> +static inline int
> +__test_bit (int nr, grub_uint32_t *addr)
> +{
> +  return 1UL & (addr[nr / 32] >> (nr & (31)));
Extra parenthesis (31)

> +}
> +

It is used for dentry_bitmap which is kept in LE format and not
converted as far as I can tell. This needs fixing for BE systems. Linux
kernel is explicitly using test_bit_le here. This will also work for
inode flags (just skip explicit conversion).

There are two functions with more or less identical names. May be make
them

grub_f2fs_test_bit_le32
grub_f2fs_test_bit

As a general comment - marking non-modified arguments as const
everywhere would be good.

> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&(inode->i_addr[1]);
Redundant parens around inode->

> +}
> +
> +static inline grub_uint64_t
> +__i_size (struct grub_f2fs_inode *inode)

Could we make it grub_f2fs_file_size or similar? i_size really does not
tell much outside of linux kernel.

> +{
> +  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 = &data->ckpt;
> +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> +  grub_uint32_t start_addr = 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 = &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 = &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 = grub_le_to_cpu32 (ckpt->ckpt_flags);
> +  return ckpt_flags & f;

All flags are constant so you can simply do 

ckpt->ckpt_flags & grub_cpu_to_le32_compile_time (FLAG)

in place to avoid extra calls. This makes function redundant.

> +}
> +
> +static inline int
> +__inode_flag_set (struct grub_f2fs_inode *inode, int flag)
> +{
> +  grub_uint32_t i_flags = grub_le_to_cpu32 (inode->i_flags);
> +  return __test_bit (flag, &i_flags);
> +}

grub_f2fs_test_bit_le32?

> +
> +/*
> + * CRC32
> + */
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (grub_uint32_t crc, void *buf, int len)

Why crc is parameter here? This function is used exactly once with
fixed value for initial crc.

> +{
> +  int i;
> +  unsigned char *p = (unsigned char *)buf;
> +
> +  while (len--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (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 = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +  char *addr = (char *)p;

Why cast? We are not going to modify it, right?

> +
> +  addr += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return (mask & *addr) != 0;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  unsigned int blocksize;
> +
> +  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))

sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC)

> +    return -1;
> +
> +  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
> +  if (blocksize != F2FS_BLKSIZE)

sb->log_blksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS)

> +    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) != F2FS_MAX_LOG_SECTOR_SIZE)

Should not it be F2FS_BLKSIZE? At least it sounds logical. Also please
convert log_sectorsize just once.

> +    return -1;
> +
> +  return 0;
> +}
> +
> +static grub_err_t
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_uint64_t offset;
> +  grub_err_t err;
> +
> +  if (block == 0)
> +    offset = F2FS_SUPER_OFFSET;
> +  else
> +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> +

Please name it "secondary" or similar instead of "block" to avoid
confusion. You do not really want to read arbitrary block, right?

offset = F2FS_SUPER_OFFEST;
if (secondary)
  offset += F2FS_BLKSIZE;

> +  /* Read first super block. */
> +  err = 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 = 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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp1;
> +
> +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);

Is unaligned access possible here? If yes, it probably should be
grub_get_unaligned32.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +

Should not CRC be converted from LE?

> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp2;
> +
> +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);

Ditto alignment.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
Ditto endianness.

> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = 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

static grub_error_t

> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))

As mentioned, use grub_cpu_to_leXX_compile_time to avoid run time
conversion.

> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (__inode_flag_set (inode, FI_INLINE_XATTR))
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];

That does not fit. You declared offset and noffset as arrays of four
elements and pass arrays of four elements; here is out of bound
access already.

> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +		offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) +
> +		offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 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 = grub_malloc (__nat_bitmap_size (data));
> +  if (!data->nat_bitmap)
> +    return grub_errno;
> +
> +  version_bitmap = __nat_bitmap_ptr (data);
> +
> +  /* copy version bitmap */
> +  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
> +

Any reason to actually copy it? Why is it not possible to just set
pointer to source, which is available all the time anyway?

> +  err = 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 = get_node_blkaddr (data, nid);
> +  if (!blkaddr)
> +    return grub_errno;
> +
> +  return grub_f2fs_block_read (data, blkaddr, np);

Is struct grub_f2fs_node guaranteed to always have the same size as F2FS
block? Then adding char [F2FS_BLKSIZE] to union to make it obvious is
better and ensures that it will always be at least this size.

> +}
> +
> +static struct grub_f2fs_data *
> +grub_f2fs_mount (grub_disk_t disk)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_err_t err;
> +
> +  data = grub_zalloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  err = grub_f2fs_read_sb (data, 0);
> +  if (err)
> +    {
> +      err = grub_f2fs_read_sb (data, 1);
> +      if (err)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");

May be mentioning that superblock could not be read? In another place
you already tell that checkpoints could not be found. It helps to
troubleshoot issues.

> +          goto fail;
> +	}
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  err = load_nat_info (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  if (data)
> +    grub_free (data->nat_bitmap);

Double free after load_nat_info failure. Assuming that we do need to
allocate anything at all (see above).

> +  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_ofs)

You have grub_f2fs_read_block and grub_f2fs_block_read. Could we make
them more different and self-explaining? In particular, this one does
not read anything, it returns disk address. grub_f2fs_map_file_block?

> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];

See above about overflow in grub_get_inode_path.

> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = grub_le_to_cpu32 (node_block->dn.addr[offset[level]]);
> +fail:
> +  grub_free (node_block);
> +  return block_addr;
> +}
> +
...
> +
> +static char *
> +grub_f2fs_read_symlink (grub_fshelp_node_t node)
> +{
> +  char *symlink;
> +  struct grub_fshelp_node *diro = node;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  symlink = 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;
> +    }
> +

What about short read? Is this an error or not?

> +  symlink[__i_size (&diro->inode.i)] = '\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 = 0; i < ctx->max;)
> +    {
> +      char filename[F2FS_NAME_LEN + 1];

Could we avoid large stack allocations?

> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +
> +      if (__test_bit (i, ctx->bitmap) == 0)

grub_f2fs_test_bit_le32?

> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = '\0';
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        return 0;
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
> +        return 1;
> +
> +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> +    }
> +    return 0;
> +}
> +
...
> +
> +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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      if (grub_f2fs_check_dentries (&ctx))
> +        return 1;

memory leak

> +
> +      grub_free (buf);
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
...
> +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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> +  if (ctx.data)
> +    grub_free (ctx.data->nat_bitmap);

Triple free :)

> +  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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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);
sizeof (*data->inode)? Or they can be different?

> +  grub_free (fdiro);
> +
> +  file->size = __i_size (&(data->inode->i));
> +  file->data = data;
> +  file->offset = 0;
> +
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  if (data)
> +    grub_free (data->nat_bitmap);

Again.

> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  if (data)
> +    grub_free (data->nat_bitmap);

Again.

> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> +      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),

malloc failure check?

> +				data->sblock.volume_name, 512);

Where 512 comes from? Should it not be sizeof
(data->sblock.volume_name) as well?

> +    }
> +  else
> +    *label = NULL;
> +
> +  if (data)
> +    grub_free (data->nat_bitmap);

Again.

> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
...
> 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=8
>  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
>  	MAXLOGSECSIZE=12;;
> -    xxfs)
> +    xxfs|xf2fs)
>  	MINLOGSECSIZE=9
>    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
>  	MAXLOGSECSIZE=12;;
> @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    fi
>  	    MAXBLKSIZE=4096
>  	    ;;
> +	xf2fs)
> +	    MINBLKSIZE=$SECSIZE
> +		# OS Limitation: GNU/Linux doesn't accept > 4096
> +	    MAXBLKSIZE=4096;;
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> @@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> -
> +	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars

F2FS, not btrfs

> +		x"f2fs")
> +		    FSLABEL="grub_;/testjaegeuk kim 

Could you leave initial part in place? This includes some funny UNICODE
characters for a reason, actually. Unless this is not possible with
f2fs?

f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
@ 2015-03-28  7:31   ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-03-28  7:31 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Tue, 24 Mar 2015 01:19:00 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

>  * 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.
> 

It's not the most useful commit message. Better would be short
explanation of use cases and intended platforms. I'm curious here -
F2FS is intended for raw flash access, on which platform(s) grub has
access to such devices? 

> 
> diff --git a/ChangeLog-2015 b/ChangeLog-2015

We do not use ChangeLog any more, it is autogenerated from commits.
This file is legacy before this change, do not change it.

> 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 <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +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

(2 << F2FS_BLK_BITS)?

> +#define F2FS_BLK_BITS			12
> +#define F2FS_BLK_SEC_BITS		(3)


It is confusing to have some defines parenthesized and some not. Could
it be unified somehow?

Also this can be computed from F2FS_BLK_BITS and GRUB_DISK_SECTOR_BITS
- one magic number less.

...
> +struct grub_f2fs_inline_dentry
> +{
> +  grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];

This is cast to grub_uint32_t everywhere. Can it be non-multiple of 4
bytes? If not, may be just define as such? 

> +  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];

ditto

> +  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;
> +


...
> +
> +#define ver_after (a, b) (typecheck (unsigned long long, a) &&          \
> +		typecheck (unsigned long long, b) &&                    \
> +		((long long)((a) - (b)) > 0))
> +

Where typecheck definition comes from?

...
> +
> +static inline int
> +__test_bit (int nr, grub_uint32_t *addr)
> +{
> +  return 1UL & (addr[nr / 32] >> (nr & (31)));
Extra parenthesis (31)

> +}
> +

It is used for dentry_bitmap which is kept in LE format and not
converted as far as I can tell. This needs fixing for BE systems. Linux
kernel is explicitly using test_bit_le here. This will also work for
inode flags (just skip explicit conversion).

There are two functions with more or less identical names. May be make
them

grub_f2fs_test_bit_le32
grub_f2fs_test_bit

As a general comment - marking non-modified arguments as const
everywhere would be good.

> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&(inode->i_addr[1]);
Redundant parens around inode->

> +}
> +
> +static inline grub_uint64_t
> +__i_size (struct grub_f2fs_inode *inode)

Could we make it grub_f2fs_file_size or similar? i_size really does not
tell much outside of linux kernel.

> +{
> +  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 = &data->ckpt;
> +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> +  grub_uint32_t start_addr = 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 = &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 = &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 = grub_le_to_cpu32 (ckpt->ckpt_flags);
> +  return ckpt_flags & f;

All flags are constant so you can simply do 

ckpt->ckpt_flags & grub_cpu_to_le32_compile_time (FLAG)

in place to avoid extra calls. This makes function redundant.

> +}
> +
> +static inline int
> +__inode_flag_set (struct grub_f2fs_inode *inode, int flag)
> +{
> +  grub_uint32_t i_flags = grub_le_to_cpu32 (inode->i_flags);
> +  return __test_bit (flag, &i_flags);
> +}

grub_f2fs_test_bit_le32?

> +
> +/*
> + * CRC32
> + */
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (grub_uint32_t crc, void *buf, int len)

Why crc is parameter here? This function is used exactly once with
fixed value for initial crc.

> +{
> +  int i;
> +  unsigned char *p = (unsigned char *)buf;
> +
> +  while (len--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (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 = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +  char *addr = (char *)p;

Why cast? We are not going to modify it, right?

> +
> +  addr += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return (mask & *addr) != 0;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  unsigned int blocksize;
> +
> +  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))

sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC)

> +    return -1;
> +
> +  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
> +  if (blocksize != F2FS_BLKSIZE)

sb->log_blksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS)

> +    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) != F2FS_MAX_LOG_SECTOR_SIZE)

Should not it be F2FS_BLKSIZE? At least it sounds logical. Also please
convert log_sectorsize just once.

> +    return -1;
> +
> +  return 0;
> +}
> +
> +static grub_err_t
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_uint64_t offset;
> +  grub_err_t err;
> +
> +  if (block == 0)
> +    offset = F2FS_SUPER_OFFSET;
> +  else
> +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> +

Please name it "secondary" or similar instead of "block" to avoid
confusion. You do not really want to read arbitrary block, right?

offset = F2FS_SUPER_OFFEST;
if (secondary)
  offset += F2FS_BLKSIZE;

> +  /* Read first super block. */
> +  err = 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 = 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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp1;
> +
> +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);

Is unaligned access possible here? If yes, it probably should be
grub_get_unaligned32.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +

Should not CRC be converted from LE?

> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp2;
> +
> +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);

Ditto alignment.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
Ditto endianness.

> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = 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

static grub_error_t

> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))

As mentioned, use grub_cpu_to_leXX_compile_time to avoid run time
conversion.

> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (__inode_flag_set (inode, FI_INLINE_XATTR))
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];

That does not fit. You declared offset and noffset as arrays of four
elements and pass arrays of four elements; here is out of bound
access already.

> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +		offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) +
> +		offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 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 = grub_malloc (__nat_bitmap_size (data));
> +  if (!data->nat_bitmap)
> +    return grub_errno;
> +
> +  version_bitmap = __nat_bitmap_ptr (data);
> +
> +  /* copy version bitmap */
> +  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
> +

Any reason to actually copy it? Why is it not possible to just set
pointer to source, which is available all the time anyway?

> +  err = 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 = get_node_blkaddr (data, nid);
> +  if (!blkaddr)
> +    return grub_errno;
> +
> +  return grub_f2fs_block_read (data, blkaddr, np);

Is struct grub_f2fs_node guaranteed to always have the same size as F2FS
block? Then adding char [F2FS_BLKSIZE] to union to make it obvious is
better and ensures that it will always be at least this size.

> +}
> +
> +static struct grub_f2fs_data *
> +grub_f2fs_mount (grub_disk_t disk)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_err_t err;
> +
> +  data = grub_zalloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  err = grub_f2fs_read_sb (data, 0);
> +  if (err)
> +    {
> +      err = grub_f2fs_read_sb (data, 1);
> +      if (err)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");

May be mentioning that superblock could not be read? In another place
you already tell that checkpoints could not be found. It helps to
troubleshoot issues.

> +          goto fail;
> +	}
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  err = load_nat_info (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  if (data)
> +    grub_free (data->nat_bitmap);

Double free after load_nat_info failure. Assuming that we do need to
allocate anything at all (see above).

> +  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_ofs)

You have grub_f2fs_read_block and grub_f2fs_block_read. Could we make
them more different and self-explaining? In particular, this one does
not read anything, it returns disk address. grub_f2fs_map_file_block?

> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];

See above about overflow in grub_get_inode_path.

> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = grub_le_to_cpu32 (node_block->dn.addr[offset[level]]);
> +fail:
> +  grub_free (node_block);
> +  return block_addr;
> +}
> +
...
> +
> +static char *
> +grub_f2fs_read_symlink (grub_fshelp_node_t node)
> +{
> +  char *symlink;
> +  struct grub_fshelp_node *diro = node;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  symlink = 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;
> +    }
> +

What about short read? Is this an error or not?

> +  symlink[__i_size (&diro->inode.i)] = '\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 = 0; i < ctx->max;)
> +    {
> +      char filename[F2FS_NAME_LEN + 1];

Could we avoid large stack allocations?

> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +
> +      if (__test_bit (i, ctx->bitmap) == 0)

grub_f2fs_test_bit_le32?

> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = '\0';
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        return 0;
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
> +        return 1;
> +
> +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> +    }
> +    return 0;
> +}
> +
...
> +
> +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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      if (grub_f2fs_check_dentries (&ctx))
> +        return 1;

memory leak

> +
> +      grub_free (buf);
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
...
> +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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> +  if (ctx.data)
> +    grub_free (ctx.data->nat_bitmap);

Triple free :)

> +  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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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);
sizeof (*data->inode)? Or they can be different?

> +  grub_free (fdiro);
> +
> +  file->size = __i_size (&(data->inode->i));
> +  file->data = data;
> +  file->offset = 0;
> +
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  if (data)
> +    grub_free (data->nat_bitmap);

Again.

> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  if (data)
> +    grub_free (data->nat_bitmap);

Again.

> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> +      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),

malloc failure check?

> +				data->sblock.volume_name, 512);

Where 512 comes from? Should it not be sizeof
(data->sblock.volume_name) as well?

> +    }
> +  else
> +    *label = NULL;
> +
> +  if (data)
> +    grub_free (data->nat_bitmap);

Again.

> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
...
> 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=8
>  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
>  	MAXLOGSECSIZE=12;;
> -    xxfs)
> +    xxfs|xf2fs)
>  	MINLOGSECSIZE=9
>    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
>  	MAXLOGSECSIZE=12;;
> @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    fi
>  	    MAXBLKSIZE=4096
>  	    ;;
> +	xf2fs)
> +	    MINBLKSIZE=$SECSIZE
> +		# OS Limitation: GNU/Linux doesn't accept > 4096
> +	    MAXBLKSIZE=4096;;
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> @@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> -
> +	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars

F2FS, not btrfs

> +		x"f2fs")
> +		    FSLABEL="grub_;/testjaegeuk kim 

Could you leave initial part in place? This includes some funny UNICODE
characters for a reason, actually. Unless this is not possible with
f2fs?

f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)



^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2015-03-28  7:31   ` Andrei Borzenkov
@ 2015-03-28 20:43     ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-03-28 20:43 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: grub-devel, linux-f2fs-devel

Hi Andrei,

On Sat, Mar 28, 2015 at 10:31:55AM +0300, Andrei Borzenkov wrote:
> В Tue, 24 Mar 2015 01:19:00 -0700
> Jaegeuk Kim <jaegeuk@kernel.org> пишет:
> 
> >  * 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.
> > 
> 
> It's not the most useful commit message. Better would be short
> explanation of use cases and intended platforms. I'm curious here -
> F2FS is intended for raw flash access, on which platform(s) grub has
> access to such devices? 

I just followed the commit convention in grub.git.
Anyway I'll add some description in v2.

F2FS is *not* intended for raw flash, for general block device such as SSD,
eMMC, and SD cards.
Please refer the following documents.

http://en.wikipedia.org/wiki/F2FS

Thank you for the detailed review.
I'll fix them and send v2.

Sincerely yours,

> 
> > 
> > diff --git a/ChangeLog-2015 b/ChangeLog-2015
> 
> We do not use ChangeLog any more, it is autogenerated from commits.
> This file is legacy before this change, do not change it.
> 
> > 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 <jaegeuk@kernel.org>
> > + *
> > + *  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 <http://www.gnu.org/licenses/>.
> > + */
> > +#include <grub/err.h>
> > +#include <grub/file.h>
> > +#include <grub/mm.h>
> > +#include <grub/misc.h>
> > +#include <grub/disk.h>
> > +#include <grub/dl.h>
> > +#include <grub/types.h>
> > +#include <grub/charset.h>
> > +#include <grub/fshelp.h>
> > +
> > +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
> 
> (2 << F2FS_BLK_BITS)?
> 
> > +#define F2FS_BLK_BITS			12
> > +#define F2FS_BLK_SEC_BITS		(3)
> 
> 
> It is confusing to have some defines parenthesized and some not. Could
> it be unified somehow?
> 
> Also this can be computed from F2FS_BLK_BITS and GRUB_DISK_SECTOR_BITS
> - one magic number less.
> 
> ...
> > +struct grub_f2fs_inline_dentry
> > +{
> > +  grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
> 
> This is cast to grub_uint32_t everywhere. Can it be non-multiple of 4
> bytes? If not, may be just define as such? 
> 
> > +  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];
> 
> ditto
> 
> > +  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;
> > +
> 
> 
> ...
> > +
> > +#define ver_after (a, b) (typecheck (unsigned long long, a) &&          \
> > +		typecheck (unsigned long long, b) &&                    \
> > +		((long long)((a) - (b)) > 0))
> > +
> 
> Where typecheck definition comes from?
> 
> ...
> > +
> > +static inline int
> > +__test_bit (int nr, grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & (31)));
> Extra parenthesis (31)
> 
> > +}
> > +
> 
> It is used for dentry_bitmap which is kept in LE format and not
> converted as far as I can tell. This needs fixing for BE systems. Linux
> kernel is explicitly using test_bit_le here. This will also work for
> inode flags (just skip explicit conversion).
> 
> There are two functions with more or less identical names. May be make
> them
> 
> grub_f2fs_test_bit_le32
> grub_f2fs_test_bit
> 
> As a general comment - marking non-modified arguments as const
> everywhere would be good.
> 
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&(inode->i_addr[1]);
> Redundant parens around inode->
> 
> > +}
> > +
> > +static inline grub_uint64_t
> > +__i_size (struct grub_f2fs_inode *inode)
> 
> Could we make it grub_f2fs_file_size or similar? i_size really does not
> tell much outside of linux kernel.
> 
> > +{
> > +  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 = &data->ckpt;
> > +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> > +  grub_uint32_t start_addr = 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 = &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 = &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 = grub_le_to_cpu32 (ckpt->ckpt_flags);
> > +  return ckpt_flags & f;
> 
> All flags are constant so you can simply do 
> 
> ckpt->ckpt_flags & grub_cpu_to_le32_compile_time (FLAG)
> 
> in place to avoid extra calls. This makes function redundant.
> 
> > +}
> > +
> > +static inline int
> > +__inode_flag_set (struct grub_f2fs_inode *inode, int flag)
> > +{
> > +  grub_uint32_t i_flags = grub_le_to_cpu32 (inode->i_flags);
> > +  return __test_bit (flag, &i_flags);
> > +}
> 
> grub_f2fs_test_bit_le32?
> 
> > +
> > +/*
> > + * CRC32
> > + */
> > +#define CRCPOLY_LE 0xedb88320
> > +
> > +static inline grub_uint32_t
> > +grub_f2fs_cal_crc32 (grub_uint32_t crc, void *buf, int len)
> 
> Why crc is parameter here? This function is used exactly once with
> fixed value for initial crc.
> 
> > +{
> > +  int i;
> > +  unsigned char *p = (unsigned char *)buf;
> > +
> > +  while (len--)
> > +    {
> > +      crc ^= *p++;
> > +      for (i = 0; i < 8; i++)
> > +        crc = (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 = 0;
> > +
> > +  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
> > +
> > +  return (cal_crc == blk_crc) ? 1 : 0;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +  char *addr = (char *)p;
> 
> Why cast? We are not going to modify it, right?
> 
> > +
> > +  addr += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return (mask & *addr) != 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  unsigned int blocksize;
> > +
> > +  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))
> 
> sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC)
> 
> > +    return -1;
> > +
> > +  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
> > +  if (blocksize != F2FS_BLKSIZE)
> 
> sb->log_blksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS)
> 
> > +    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) != F2FS_MAX_LOG_SECTOR_SIZE)
> 
> Should not it be F2FS_BLKSIZE? At least it sounds logical. Also please
> convert log_sectorsize just once.
> 
> > +    return -1;
> > +
> > +  return 0;
> > +}
> > +
> > +static grub_err_t
> > +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> > +{
> > +  grub_disk_t disk = data->disk;
> > +  grub_uint64_t offset;
> > +  grub_err_t err;
> > +
> > +  if (block == 0)
> > +    offset = F2FS_SUPER_OFFSET;
> > +  else
> > +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> > +
> 
> Please name it "secondary" or similar instead of "block" to avoid
> confusion. You do not really want to read arbitrary block, right?
> 
> offset = F2FS_SUPER_OFFEST;
> if (secondary)
>   offset += F2FS_BLKSIZE;
> 
> > +  /* Read first super block. */
> > +  err = 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 = 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 = 0, pre_version = 0;
> > +  grub_uint32_t crc = 0;
> > +  grub_uint32_t crc_offset;
> > +  grub_err_t err;
> > +
> > +  /* Read the 1st cp block in this CP pack */
> > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_1)
> > +    return NULL;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > +  if (err)
> > +    goto invalid_cp1;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp1;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Is unaligned access possible here? If yes, it probably should be
> grub_get_unaligned32.
> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp1;
> > +
> 
> Should not CRC be converted from LE?
> 
> > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +
> > +  /* Read the 2nd cp block in this CP pack */
> > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_2)
> > +    goto invalid_cp1;
> > +
> > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > +  if (err)
> > +    goto invalid_cp2;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp2;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Ditto alignment.
> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> Ditto endianness.
> 
> > +    goto invalid_cp2;
> > +
> > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +  if (cur_version == pre_version)
> > +    {
> > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > +  cp1 = 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 += data->blocks_per_seg;
> > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > +  if (!cp2 && grub_errno)
> > +    {
> > +      grub_free (cp1);
> > +      return grub_errno;
> > +    }
> > +
> > +  if (cp1 && cp2)
> > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > +  else if (cp1)
> > +    cur_page = cp1;
> > +  else if (cp2)
> > +    cur_page = 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
> 
> static grub_error_t
> 
> > +get_nat_journal (struct grub_f2fs_data *data)
> > +{
> > +  grub_uint32_t block;
> > +  char *buf;
> > +  grub_err_t err;
> > +
> > +  buf = grub_malloc (F2FS_BLKSIZE);
> > +  if (!buf)
> > +    return grub_errno;
> > +
> > +  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
> > +    block = __start_sum_block (data);
> > +  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))
> 
> As mentioned, use grub_cpu_to_leXX_compile_time to avoid run time
> conversion.
> 
> > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > +  else
> > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > +
> > +  err = 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 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 = ADDRS_PER_BLOCK;
> > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > +  int n = 0;
> > +  int level = 0;
> > +
> > +  if (__inode_flag_set (inode, FI_INLINE_XATTR))
> > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > +
> > +  noffset[0] = 0;
> > +
> > +  if (block < direct_index)
> > +    {
> > +      offset[n] = block;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_index;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR1_BLOCK;
> > +      noffset[n] = 1;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR2_BLOCK;
> > +      noffset[n] = 2;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND1_BLOCK;
> > +      noffset[n] = 3;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 4 + offset[n - 1];
> 
> That does not fit. You declared offset and noffset as arrays of four
> elements and pass arrays of four elements; here is out of bound
> access already.
> 
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND2_BLOCK;
> > +      noffset[n] = 4 + dptrs_per_blk;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < dindirect_blks)
> > +    {
> > +      offset[n++] = NODE_DIND_BLOCK;
> > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > +      offset[n++] = block / indirect_blks;
> > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > +		offset[n - 1] * (dptrs_per_blk + 1);
> > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > +		offset[n - 2] * (dptrs_per_blk + 1) +
> > +		offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 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 = grub_malloc (__nat_bitmap_size (data));
> > +  if (!data->nat_bitmap)
> > +    return grub_errno;
> > +
> > +  version_bitmap = __nat_bitmap_ptr (data);
> > +
> > +  /* copy version bitmap */
> > +  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
> > +
> 
> Any reason to actually copy it? Why is it not possible to just set
> pointer to source, which is available all the time anyway?
> 
> > +  err = 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 = get_node_blkaddr (data, nid);
> > +  if (!blkaddr)
> > +    return grub_errno;
> > +
> > +  return grub_f2fs_block_read (data, blkaddr, np);
> 
> Is struct grub_f2fs_node guaranteed to always have the same size as F2FS
> block? Then adding char [F2FS_BLKSIZE] to union to make it obvious is
> better and ensures that it will always be at least this size.
> 
> > +}
> > +
> > +static struct grub_f2fs_data *
> > +grub_f2fs_mount (grub_disk_t disk)
> > +{
> > +  struct grub_f2fs_data *data;
> > +  grub_err_t err;
> > +
> > +  data = grub_zalloc (sizeof (*data));
> > +  if (!data)
> > +    return NULL;
> > +
> > +  data->disk = disk;
> > +
> > +  err = grub_f2fs_read_sb (data, 0);
> > +  if (err)
> > +    {
> > +      err = grub_f2fs_read_sb (data, 1);
> > +      if (err)
> > +        {
> > +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");
> 
> May be mentioning that superblock could not be read? In another place
> you already tell that checkpoints could not be found. It helps to
> troubleshoot issues.
> 
> > +          goto fail;
> > +	}
> > +    }
> > +
> > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > +  data->blocks_per_seg = 1 <<
> > +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > +
> > +  err = grub_f2fs_read_cp (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  err = load_nat_info (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->diropen.data = data;
> > +  data->diropen.ino = data->root_ino;
> > +  data->diropen.inode_read = 1;
> > +  data->inode = &data->diropen.inode;
> > +
> > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > +  if (err)
> > +    goto fail;
> > +
> > +  return data;
> > +
> > +fail:
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Double free after load_nat_info failure. Assuming that we do need to
> allocate anything at all (see above).
> 
> > +  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_ofs)
> 
> You have grub_f2fs_read_block and grub_f2fs_block_read. Could we make
> them more different and self-explaining? In particular, this one does
> not read anything, it returns disk address. grub_f2fs_map_file_block?
> 
> > +{
> > +  struct grub_f2fs_data *data = node->data;
> > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > +  grub_uint32_t offset[4], noffset[4], nids[4];
> 
> See above about overflow in grub_get_inode_path.
> 
> > +  struct grub_f2fs_node *node_block;
> > +  grub_uint32_t block_addr = -1;
> > +  int level, i;
> > +
> > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > +  if (level == 0)
> > +      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > +
> > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!node_block)
> > +    return -1;
> > +
> > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > +
> > +  /* get indirect or direct nodes */
> > +  for (i = 1; i <= level; i++)
> > +    {
> > +      grub_f2fs_read_node (data, nids[i], node_block);
> > +      if (grub_errno)
> > +        goto fail;
> > +
> > +      if (i < level)
> > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > +    }
> > +
> > +  block_addr = grub_le_to_cpu32 (node_block->dn.addr[offset[level]]);
> > +fail:
> > +  grub_free (node_block);
> > +  return block_addr;
> > +}
> > +
> ...
> > +
> > +static char *
> > +grub_f2fs_read_symlink (grub_fshelp_node_t node)
> > +{
> > +  char *symlink;
> > +  struct grub_fshelp_node *diro = node;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  symlink = 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;
> > +    }
> > +
> 
> What about short read? Is this an error or not?
> 
> > +  symlink[__i_size (&diro->inode.i)] = '\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 = 0; i < ctx->max;)
> > +    {
> > +      char filename[F2FS_NAME_LEN + 1];
> 
> Could we avoid large stack allocations?
> 
> > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > +      enum FILE_TYPE ftype;
> > +      int name_len;
> > +
> > +      if (__test_bit (i, ctx->bitmap) == 0)
> 
> grub_f2fs_test_bit_le32?
> 
> > +        {
> > +          i++;
> > +          continue;
> > +        }
> > +
> > +      ftype = ctx->dentry[i].file_type;
> > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > +      filename[name_len] = '\0';
> > +
> > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > +      if (!fdiro)
> > +        return 0;
> > +
> > +      if (ftype == F2FS_FT_DIR)
> > +        type = GRUB_FSHELP_DIR;
> > +      else if (ftype == F2FS_FT_SYMLINK)
> > +        type = GRUB_FSHELP_SYMLINK;
> > +      else if (ftype == F2FS_FT_REG_FILE)
> > +        type = GRUB_FSHELP_REG;
> > +
> > +      fdiro->data = ctx->data;
> > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > +      fdiro->inode_read = 0;
> > +
> > +      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
> > +        return 1;
> > +
> > +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> > +    }
> > +    return 0;
> > +}
> > +
> ...
> > +
> > +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 = (struct grub_fshelp_node *) dir;
> > +  struct grub_f2fs_inode *inode;
> > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > +    .data = diro->data,
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  grub_off_t fpos = 0;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
> > +
> > +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> > +      ctx.dentry = de_blk->dentry;
> > +      ctx.filename = de_blk->filename;
> > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > +
> > +      if (grub_f2fs_check_dentries (&ctx))
> > +        return 1;
> 
> memory leak
> 
> > +
> > +      grub_free (buf);
> > +
> > +      fpos += F2FS_BLKSIZE;
> > +    }
> > +  return 0;
> > +}
> > +
> ...
> > +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 = {
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  ctx.data = 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 != &ctx.data->diropen)
> > +    grub_free (fdiro);
> > +  if (ctx.data)
> > +    grub_free (ctx.data->nat_bitmap);
> 
> Triple free :)
> 
> > +  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 = NULL;
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = 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);
> sizeof (*data->inode)? Or they can be different?
> 
> > +  grub_free (fdiro);
> > +
> > +  file->size = __i_size (&(data->inode->i));
> > +  file->data = data;
> > +  file->offset = 0;
> > +
> > +  return 0;
> > +
> > +fail:
> > +  if (fdiro != &data->diropen)
> > +    grub_free (fdiro);
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    {
> > +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> > +      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),
> 
> malloc failure check?
> 
> > +				data->sblock.volume_name, 512);
> 
> Where 512 comes from? Should it not be sizeof
> (data->sblock.volume_name) as well?
> 
> > +    }
> > +  else
> > +    *label = NULL;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  grub_free (data);
> > +  grub_dl_unref (my_mod);
> > +  return grub_errno;
> > +}
> > +
> ...
> > 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=8
> >  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
> >  	MAXLOGSECSIZE=12;;
> > -    xxfs)
> > +    xxfs|xf2fs)
> >  	MINLOGSECSIZE=9
> >    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
> >  	MAXLOGSECSIZE=12;;
> > @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    fi
> >  	    MAXBLKSIZE=4096
> >  	    ;;
> > +	xf2fs)
> > +	    MINBLKSIZE=$SECSIZE
> > +		# OS Limitation: GNU/Linux doesn't accept > 4096
> > +	    MAXBLKSIZE=4096;;
> >  	xsquash*)
> >  	    MINBLKSIZE=4096
> >  	    MAXBLKSIZE=1048576;;
> > @@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
> >  		x"btrfs"*)
> >  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> > -
> > +	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars
> 
> F2FS, not btrfs
> 
> > +		x"f2fs")
> > +		    FSLABEL="grub_;/testjaegeuk kim 
> 
> Could you leave initial part in place? This includes some funny UNICODE
> characters for a reason, actually. Unless this is not possible with
> f2fs?
> 
> f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
> >  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >  		x"exfat")
> >  		    FSLABEL="géт ;/莭莽😁кир";;
> > @@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
> >  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
> >  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> > -		    | 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=y;;
> > @@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >  		    MOUNTFS=ext2
> >  		    "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)

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
@ 2015-03-28 20:43     ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-03-28 20:43 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: grub-devel, linux-f2fs-devel

Hi Andrei,

On Sat, Mar 28, 2015 at 10:31:55AM +0300, Andrei Borzenkov wrote:
> В Tue, 24 Mar 2015 01:19:00 -0700
> Jaegeuk Kim <jaegeuk@kernel.org> пишет:
> 
> >  * 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.
> > 
> 
> It's not the most useful commit message. Better would be short
> explanation of use cases and intended platforms. I'm curious here -
> F2FS is intended for raw flash access, on which platform(s) grub has
> access to such devices? 

I just followed the commit convention in grub.git.
Anyway I'll add some description in v2.

F2FS is *not* intended for raw flash, for general block device such as SSD,
eMMC, and SD cards.
Please refer the following documents.

http://en.wikipedia.org/wiki/F2FS

Thank you for the detailed review.
I'll fix them and send v2.

Sincerely yours,

> 
> > 
> > diff --git a/ChangeLog-2015 b/ChangeLog-2015
> 
> We do not use ChangeLog any more, it is autogenerated from commits.
> This file is legacy before this change, do not change it.
> 
> > 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 <jaegeuk@kernel.org>
> > + *
> > + *  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 <http://www.gnu.org/licenses/>.
> > + */
> > +#include <grub/err.h>
> > +#include <grub/file.h>
> > +#include <grub/mm.h>
> > +#include <grub/misc.h>
> > +#include <grub/disk.h>
> > +#include <grub/dl.h>
> > +#include <grub/types.h>
> > +#include <grub/charset.h>
> > +#include <grub/fshelp.h>
> > +
> > +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
> 
> (2 << F2FS_BLK_BITS)?
> 
> > +#define F2FS_BLK_BITS			12
> > +#define F2FS_BLK_SEC_BITS		(3)
> 
> 
> It is confusing to have some defines parenthesized and some not. Could
> it be unified somehow?
> 
> Also this can be computed from F2FS_BLK_BITS and GRUB_DISK_SECTOR_BITS
> - one magic number less.
> 
> ...
> > +struct grub_f2fs_inline_dentry
> > +{
> > +  grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
> 
> This is cast to grub_uint32_t everywhere. Can it be non-multiple of 4
> bytes? If not, may be just define as such? 
> 
> > +  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];
> 
> ditto
> 
> > +  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;
> > +
> 
> 
> ...
> > +
> > +#define ver_after (a, b) (typecheck (unsigned long long, a) &&          \
> > +		typecheck (unsigned long long, b) &&                    \
> > +		((long long)((a) - (b)) > 0))
> > +
> 
> Where typecheck definition comes from?
> 
> ...
> > +
> > +static inline int
> > +__test_bit (int nr, grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & (31)));
> Extra parenthesis (31)
> 
> > +}
> > +
> 
> It is used for dentry_bitmap which is kept in LE format and not
> converted as far as I can tell. This needs fixing for BE systems. Linux
> kernel is explicitly using test_bit_le here. This will also work for
> inode flags (just skip explicit conversion).
> 
> There are two functions with more or less identical names. May be make
> them
> 
> grub_f2fs_test_bit_le32
> grub_f2fs_test_bit
> 
> As a general comment - marking non-modified arguments as const
> everywhere would be good.
> 
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&(inode->i_addr[1]);
> Redundant parens around inode->
> 
> > +}
> > +
> > +static inline grub_uint64_t
> > +__i_size (struct grub_f2fs_inode *inode)
> 
> Could we make it grub_f2fs_file_size or similar? i_size really does not
> tell much outside of linux kernel.
> 
> > +{
> > +  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 = &data->ckpt;
> > +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> > +  grub_uint32_t start_addr = 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 = &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 = &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 = grub_le_to_cpu32 (ckpt->ckpt_flags);
> > +  return ckpt_flags & f;
> 
> All flags are constant so you can simply do 
> 
> ckpt->ckpt_flags & grub_cpu_to_le32_compile_time (FLAG)
> 
> in place to avoid extra calls. This makes function redundant.
> 
> > +}
> > +
> > +static inline int
> > +__inode_flag_set (struct grub_f2fs_inode *inode, int flag)
> > +{
> > +  grub_uint32_t i_flags = grub_le_to_cpu32 (inode->i_flags);
> > +  return __test_bit (flag, &i_flags);
> > +}
> 
> grub_f2fs_test_bit_le32?
> 
> > +
> > +/*
> > + * CRC32
> > + */
> > +#define CRCPOLY_LE 0xedb88320
> > +
> > +static inline grub_uint32_t
> > +grub_f2fs_cal_crc32 (grub_uint32_t crc, void *buf, int len)
> 
> Why crc is parameter here? This function is used exactly once with
> fixed value for initial crc.
> 
> > +{
> > +  int i;
> > +  unsigned char *p = (unsigned char *)buf;
> > +
> > +  while (len--)
> > +    {
> > +      crc ^= *p++;
> > +      for (i = 0; i < 8; i++)
> > +        crc = (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 = 0;
> > +
> > +  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
> > +
> > +  return (cal_crc == blk_crc) ? 1 : 0;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +  char *addr = (char *)p;
> 
> Why cast? We are not going to modify it, right?
> 
> > +
> > +  addr += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return (mask & *addr) != 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  unsigned int blocksize;
> > +
> > +  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))
> 
> sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC)
> 
> > +    return -1;
> > +
> > +  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
> > +  if (blocksize != F2FS_BLKSIZE)
> 
> sb->log_blksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS)
> 
> > +    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) != F2FS_MAX_LOG_SECTOR_SIZE)
> 
> Should not it be F2FS_BLKSIZE? At least it sounds logical. Also please
> convert log_sectorsize just once.
> 
> > +    return -1;
> > +
> > +  return 0;
> > +}
> > +
> > +static grub_err_t
> > +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> > +{
> > +  grub_disk_t disk = data->disk;
> > +  grub_uint64_t offset;
> > +  grub_err_t err;
> > +
> > +  if (block == 0)
> > +    offset = F2FS_SUPER_OFFSET;
> > +  else
> > +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> > +
> 
> Please name it "secondary" or similar instead of "block" to avoid
> confusion. You do not really want to read arbitrary block, right?
> 
> offset = F2FS_SUPER_OFFEST;
> if (secondary)
>   offset += F2FS_BLKSIZE;
> 
> > +  /* Read first super block. */
> > +  err = 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 = 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 = 0, pre_version = 0;
> > +  grub_uint32_t crc = 0;
> > +  grub_uint32_t crc_offset;
> > +  grub_err_t err;
> > +
> > +  /* Read the 1st cp block in this CP pack */
> > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_1)
> > +    return NULL;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > +  if (err)
> > +    goto invalid_cp1;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp1;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Is unaligned access possible here? If yes, it probably should be
> grub_get_unaligned32.
> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp1;
> > +
> 
> Should not CRC be converted from LE?
> 
> > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +
> > +  /* Read the 2nd cp block in this CP pack */
> > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_2)
> > +    goto invalid_cp1;
> > +
> > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > +  if (err)
> > +    goto invalid_cp2;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp2;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Ditto alignment.
> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> Ditto endianness.
> 
> > +    goto invalid_cp2;
> > +
> > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +  if (cur_version == pre_version)
> > +    {
> > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > +  cp1 = 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 += data->blocks_per_seg;
> > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > +  if (!cp2 && grub_errno)
> > +    {
> > +      grub_free (cp1);
> > +      return grub_errno;
> > +    }
> > +
> > +  if (cp1 && cp2)
> > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > +  else if (cp1)
> > +    cur_page = cp1;
> > +  else if (cp2)
> > +    cur_page = 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
> 
> static grub_error_t
> 
> > +get_nat_journal (struct grub_f2fs_data *data)
> > +{
> > +  grub_uint32_t block;
> > +  char *buf;
> > +  grub_err_t err;
> > +
> > +  buf = grub_malloc (F2FS_BLKSIZE);
> > +  if (!buf)
> > +    return grub_errno;
> > +
> > +  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
> > +    block = __start_sum_block (data);
> > +  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))
> 
> As mentioned, use grub_cpu_to_leXX_compile_time to avoid run time
> conversion.
> 
> > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > +  else
> > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > +
> > +  err = 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 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 = ADDRS_PER_BLOCK;
> > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > +  int n = 0;
> > +  int level = 0;
> > +
> > +  if (__inode_flag_set (inode, FI_INLINE_XATTR))
> > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > +
> > +  noffset[0] = 0;
> > +
> > +  if (block < direct_index)
> > +    {
> > +      offset[n] = block;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_index;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR1_BLOCK;
> > +      noffset[n] = 1;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR2_BLOCK;
> > +      noffset[n] = 2;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND1_BLOCK;
> > +      noffset[n] = 3;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 4 + offset[n - 1];
> 
> That does not fit. You declared offset and noffset as arrays of four
> elements and pass arrays of four elements; here is out of bound
> access already.
> 
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND2_BLOCK;
> > +      noffset[n] = 4 + dptrs_per_blk;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < dindirect_blks)
> > +    {
> > +      offset[n++] = NODE_DIND_BLOCK;
> > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > +      offset[n++] = block / indirect_blks;
> > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > +		offset[n - 1] * (dptrs_per_blk + 1);
> > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > +		offset[n - 2] * (dptrs_per_blk + 1) +
> > +		offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 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 = grub_malloc (__nat_bitmap_size (data));
> > +  if (!data->nat_bitmap)
> > +    return grub_errno;
> > +
> > +  version_bitmap = __nat_bitmap_ptr (data);
> > +
> > +  /* copy version bitmap */
> > +  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
> > +
> 
> Any reason to actually copy it? Why is it not possible to just set
> pointer to source, which is available all the time anyway?
> 
> > +  err = 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 = get_node_blkaddr (data, nid);
> > +  if (!blkaddr)
> > +    return grub_errno;
> > +
> > +  return grub_f2fs_block_read (data, blkaddr, np);
> 
> Is struct grub_f2fs_node guaranteed to always have the same size as F2FS
> block? Then adding char [F2FS_BLKSIZE] to union to make it obvious is
> better and ensures that it will always be at least this size.
> 
> > +}
> > +
> > +static struct grub_f2fs_data *
> > +grub_f2fs_mount (grub_disk_t disk)
> > +{
> > +  struct grub_f2fs_data *data;
> > +  grub_err_t err;
> > +
> > +  data = grub_zalloc (sizeof (*data));
> > +  if (!data)
> > +    return NULL;
> > +
> > +  data->disk = disk;
> > +
> > +  err = grub_f2fs_read_sb (data, 0);
> > +  if (err)
> > +    {
> > +      err = grub_f2fs_read_sb (data, 1);
> > +      if (err)
> > +        {
> > +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");
> 
> May be mentioning that superblock could not be read? In another place
> you already tell that checkpoints could not be found. It helps to
> troubleshoot issues.
> 
> > +          goto fail;
> > +	}
> > +    }
> > +
> > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > +  data->blocks_per_seg = 1 <<
> > +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > +
> > +  err = grub_f2fs_read_cp (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  err = load_nat_info (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->diropen.data = data;
> > +  data->diropen.ino = data->root_ino;
> > +  data->diropen.inode_read = 1;
> > +  data->inode = &data->diropen.inode;
> > +
> > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > +  if (err)
> > +    goto fail;
> > +
> > +  return data;
> > +
> > +fail:
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Double free after load_nat_info failure. Assuming that we do need to
> allocate anything at all (see above).
> 
> > +  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_ofs)
> 
> You have grub_f2fs_read_block and grub_f2fs_block_read. Could we make
> them more different and self-explaining? In particular, this one does
> not read anything, it returns disk address. grub_f2fs_map_file_block?
> 
> > +{
> > +  struct grub_f2fs_data *data = node->data;
> > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > +  grub_uint32_t offset[4], noffset[4], nids[4];
> 
> See above about overflow in grub_get_inode_path.
> 
> > +  struct grub_f2fs_node *node_block;
> > +  grub_uint32_t block_addr = -1;
> > +  int level, i;
> > +
> > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > +  if (level == 0)
> > +      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > +
> > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!node_block)
> > +    return -1;
> > +
> > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > +
> > +  /* get indirect or direct nodes */
> > +  for (i = 1; i <= level; i++)
> > +    {
> > +      grub_f2fs_read_node (data, nids[i], node_block);
> > +      if (grub_errno)
> > +        goto fail;
> > +
> > +      if (i < level)
> > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > +    }
> > +
> > +  block_addr = grub_le_to_cpu32 (node_block->dn.addr[offset[level]]);
> > +fail:
> > +  grub_free (node_block);
> > +  return block_addr;
> > +}
> > +
> ...
> > +
> > +static char *
> > +grub_f2fs_read_symlink (grub_fshelp_node_t node)
> > +{
> > +  char *symlink;
> > +  struct grub_fshelp_node *diro = node;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  symlink = 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;
> > +    }
> > +
> 
> What about short read? Is this an error or not?
> 
> > +  symlink[__i_size (&diro->inode.i)] = '\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 = 0; i < ctx->max;)
> > +    {
> > +      char filename[F2FS_NAME_LEN + 1];
> 
> Could we avoid large stack allocations?
> 
> > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > +      enum FILE_TYPE ftype;
> > +      int name_len;
> > +
> > +      if (__test_bit (i, ctx->bitmap) == 0)
> 
> grub_f2fs_test_bit_le32?
> 
> > +        {
> > +          i++;
> > +          continue;
> > +        }
> > +
> > +      ftype = ctx->dentry[i].file_type;
> > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > +      filename[name_len] = '\0';
> > +
> > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > +      if (!fdiro)
> > +        return 0;
> > +
> > +      if (ftype == F2FS_FT_DIR)
> > +        type = GRUB_FSHELP_DIR;
> > +      else if (ftype == F2FS_FT_SYMLINK)
> > +        type = GRUB_FSHELP_SYMLINK;
> > +      else if (ftype == F2FS_FT_REG_FILE)
> > +        type = GRUB_FSHELP_REG;
> > +
> > +      fdiro->data = ctx->data;
> > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > +      fdiro->inode_read = 0;
> > +
> > +      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
> > +        return 1;
> > +
> > +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> > +    }
> > +    return 0;
> > +}
> > +
> ...
> > +
> > +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 = (struct grub_fshelp_node *) dir;
> > +  struct grub_f2fs_inode *inode;
> > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > +    .data = diro->data,
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  grub_off_t fpos = 0;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
> > +
> > +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> > +      ctx.dentry = de_blk->dentry;
> > +      ctx.filename = de_blk->filename;
> > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > +
> > +      if (grub_f2fs_check_dentries (&ctx))
> > +        return 1;
> 
> memory leak
> 
> > +
> > +      grub_free (buf);
> > +
> > +      fpos += F2FS_BLKSIZE;
> > +    }
> > +  return 0;
> > +}
> > +
> ...
> > +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 = {
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  ctx.data = 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 != &ctx.data->diropen)
> > +    grub_free (fdiro);
> > +  if (ctx.data)
> > +    grub_free (ctx.data->nat_bitmap);
> 
> Triple free :)
> 
> > +  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 = NULL;
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = 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);
> sizeof (*data->inode)? Or they can be different?
> 
> > +  grub_free (fdiro);
> > +
> > +  file->size = __i_size (&(data->inode->i));
> > +  file->data = data;
> > +  file->offset = 0;
> > +
> > +  return 0;
> > +
> > +fail:
> > +  if (fdiro != &data->diropen)
> > +    grub_free (fdiro);
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    {
> > +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> > +      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),
> 
> malloc failure check?
> 
> > +				data->sblock.volume_name, 512);
> 
> Where 512 comes from? Should it not be sizeof
> (data->sblock.volume_name) as well?
> 
> > +    }
> > +  else
> > +    *label = NULL;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  grub_free (data);
> > +  grub_dl_unref (my_mod);
> > +  return grub_errno;
> > +}
> > +
> ...
> > 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=8
> >  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
> >  	MAXLOGSECSIZE=12;;
> > -    xxfs)
> > +    xxfs|xf2fs)
> >  	MINLOGSECSIZE=9
> >    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
> >  	MAXLOGSECSIZE=12;;
> > @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    fi
> >  	    MAXBLKSIZE=4096
> >  	    ;;
> > +	xf2fs)
> > +	    MINBLKSIZE=$SECSIZE
> > +		# OS Limitation: GNU/Linux doesn't accept > 4096
> > +	    MAXBLKSIZE=4096;;
> >  	xsquash*)
> >  	    MINBLKSIZE=4096
> >  	    MAXBLKSIZE=1048576;;
> > @@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
> >  		x"btrfs"*)
> >  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> > -
> > +	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars
> 
> F2FS, not btrfs
> 
> > +		x"f2fs")
> > +		    FSLABEL="grub_;/testjaegeuk kim 
> 
> Could you leave initial part in place? This includes some funny UNICODE
> characters for a reason, actually. Unless this is not possible with
> f2fs?
> 
> f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
> >  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >  		x"exfat")
> >  		    FSLABEL="géт ;/莭莽😁кир";;
> > @@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
> >  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
> >  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> > -		    | 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=y;;
> > @@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >  		    MOUNTFS=ext2
> >  		    "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)


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2015-03-28 20:43     ` Jaegeuk Kim
@ 2015-03-28 21:00       ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-03-28 21:00 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Sat, 28 Mar 2015 13:43:18 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

> Hi Andrei,
> 
> On Sat, Mar 28, 2015 at 10:31:55AM +0300, Andrei Borzenkov wrote:
> > В Tue, 24 Mar 2015 01:19:00 -0700
> > Jaegeuk Kim <jaegeuk@kernel.org> пишет:
> > 
> > >  * 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.
> > > 
> > 
> > It's not the most useful commit message. Better would be short
> > explanation of use cases and intended platforms. I'm curious here -
> > F2FS is intended for raw flash access, on which platform(s) grub has
> > access to such devices? 
> 
> I just followed the commit convention in grub.git.

It has changed meanwhile. We are using normal git conventions now.

> > > +static grub_err_t
> > > +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> > > +{
> > > +  grub_disk_t disk = data->disk;
> > > +  grub_uint64_t offset;
> > > +  grub_err_t err;
> > > +
> > > +  if (block == 0)
> > > +    offset = F2FS_SUPER_OFFSET;
> > > +  else
> > > +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> > > +
> > 
> > Please name it "secondary" or similar instead of "block" to avoid
> > confusion. You do not really want to read arbitrary block, right?
> >

Actually it makes more sense just to pass offset directly to eliminate
useless computation. 

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
@ 2015-03-28 21:00       ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-03-28 21:00 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Sat, 28 Mar 2015 13:43:18 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

> Hi Andrei,
> 
> On Sat, Mar 28, 2015 at 10:31:55AM +0300, Andrei Borzenkov wrote:
> > В Tue, 24 Mar 2015 01:19:00 -0700
> > Jaegeuk Kim <jaegeuk@kernel.org> пишет:
> > 
> > >  * 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.
> > > 
> > 
> > It's not the most useful commit message. Better would be short
> > explanation of use cases and intended platforms. I'm curious here -
> > F2FS is intended for raw flash access, on which platform(s) grub has
> > access to such devices? 
> 
> I just followed the commit convention in grub.git.

It has changed meanwhile. We are using normal git conventions now.

> > > +static grub_err_t
> > > +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> > > +{
> > > +  grub_disk_t disk = data->disk;
> > > +  grub_uint64_t offset;
> > > +  grub_err_t err;
> > > +
> > > +  if (block == 0)
> > > +    offset = F2FS_SUPER_OFFSET;
> > > +  else
> > > +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> > > +
> > 
> > Please name it "secondary" or similar instead of "block" to avoid
> > confusion. You do not really want to read arbitrary block, right?
> >

Actually it makes more sense just to pass offset directly to eliminate
useless computation. 


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2015-03-28  7:31   ` Andrei Borzenkov
@ 2015-04-03 22:48     ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-04-03 22:48 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: grub-devel, linux-f2fs-devel

Hi Andrei,

On Sat, Mar 28, 2015 at 10:31:55AM +0300, Andrei Borzenkov wrote:
> В Tue, 24 Mar 2015 01:19:00 -0700
> Jaegeuk Kim <jaegeuk@kernel.org> пишет:
> 

...

> > +/* 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
> 
> (2 << F2FS_BLK_BITS)?
> 
> > +#define F2FS_BLK_BITS			12
> > +#define F2FS_BLK_SEC_BITS		(3)
> 
> 
> It is confusing to have some defines parenthesized and some not. Could
> it be unified somehow?
> 
> Also this can be computed from F2FS_BLK_BITS and GRUB_DISK_SECTOR_BITS
> - one magic number less.

Fixed.

> 
> ...
> > +struct grub_f2fs_inline_dentry
> > +{
> > +  grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
> 
> This is cast to grub_uint32_t everywhere. Can it be non-multiple of 4
> bytes? If not, may be just define as such? 

I remained this, since this can be non-multiple of 4 bytes.

> 
> > +  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;
> > +

...

> > +
> > +#define ver_after (a, b) (typecheck (unsigned long long, a) &&          \
> > +		typecheck (unsigned long long, b) &&                    \
> > +		((long long)((a) - (b)) > 0))
> > +
> 
> Where typecheck definition comes from?

Removed this.

> 
> ...
> > +
> > +static inline int
> > +__test_bit (int nr, grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & (31)));
> Extra parenthesis (31)

Fixed.

> 
> > +}
> > +
> 
> It is used for dentry_bitmap which is kept in LE format and not
> converted as far as I can tell. This needs fixing for BE systems. Linux
> kernel is explicitly using test_bit_le here. This will also work for
> inode flags (just skip explicit conversion).
> 
> There are two functions with more or less identical names. May be make
> them
> 
> grub_f2fs_test_bit_le32
> grub_f2fs_test_bit
> 
> As a general comment - marking non-modified arguments as const
> everywhere would be good.

I added:
grub_f2fs_general_test_bit
grub_f2fs_test_bit

Both of them keep bit streams without endian conversion, and the difference is
beyond handling the order of bit stream indices.

> 
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&(inode->i_addr[1]);
> Redundant parens around inode->

Fixed.

> 
> > +}
> > +
> > +static inline grub_uint64_t
> > +__i_size (struct grub_f2fs_inode *inode)
> 
> Could we make it grub_f2fs_file_size or similar? i_size really does not
> tell much outside of linux kernel.

Changed to grub_f2fs_file_size. 

> 
> > +{
> > +  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 = &data->ckpt;
> > +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> > +  grub_uint32_t start_addr = 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 = &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 = &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 = grub_le_to_cpu32 (ckpt->ckpt_flags);
> > +  return ckpt_flags & f;
> 
> All flags are constant so you can simply do 
> 
> ckpt->ckpt_flags & grub_cpu_to_le32_compile_time (FLAG)
> 
> in place to avoid extra calls. This makes function redundant.

Fixed.

> 
> > +}
> > +
> > +static inline int
> > +__inode_flag_set (struct grub_f2fs_inode *inode, int flag)

Fixed to refer i_inline. This was a bug.

...

> > + * CRC32
> > + */
> > +#define CRCPOLY_LE 0xedb88320
> > +
> > +static inline grub_uint32_t
> > +grub_f2fs_cal_crc32 (grub_uint32_t crc, void *buf, int len)
> 
> Why crc is parameter here? This function is used exactly once with
> fixed value for initial crc.

Fixed.

> 
> > +{
> > +  int i;
> > +  unsigned char *p = (unsigned char *)buf;
> > +
> > +  while (len--)
> > +    {
> > +      crc ^= *p++;
> > +      for (i = 0; i < 8; i++)
> > +        crc = (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 = 0;
> > +
> > +  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
> > +
> > +  return (cal_crc == blk_crc) ? 1 : 0;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +  char *addr = (char *)p;
> 
> Why cast? We are not going to modify it, right?

Right.

> 
> > +
> > +  addr += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return (mask & *addr) != 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  unsigned int blocksize;
> > +
> > +  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))
> 
> sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC)

Fixed.

> 
> > +    return -1;
> > +
> > +  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
> > +  if (blocksize != F2FS_BLKSIZE)
> 
> sb->log_blksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS)

Fixed.

> 
> > +    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) != F2FS_MAX_LOG_SECTOR_SIZE)
> 
> Should not it be F2FS_BLKSIZE? At least it sounds logical. Also please
> convert log_sectorsize just once.

Fixed.

> 
> > +    return -1;
> > +
> > +  return 0;
> > +}
> > +
> > +static grub_err_t
> > +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> > +{
> > +  grub_disk_t disk = data->disk;
> > +  grub_uint64_t offset;
> > +  grub_err_t err;
> > +
> > +  if (block == 0)
> > +    offset = F2FS_SUPER_OFFSET;
> > +  else
> > +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> > +
> 
> Please name it "secondary" or similar instead of "block" to avoid
> confusion. You do not really want to read arbitrary block, right?
> 
> offset = F2FS_SUPER_OFFEST;
> if (secondary)
>   offset += F2FS_BLKSIZE;

Fixed as your latest comment.

> 
> > +  /* Read first super block. */
> > +  err = 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 = 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 = 0, pre_version = 0;
> > +  grub_uint32_t crc = 0;
> > +  grub_uint32_t crc_offset;
> > +  grub_err_t err;
> > +
> > +  /* Read the 1st cp block in this CP pack */
> > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_1)
> > +    return NULL;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > +  if (err)
> > +    goto invalid_cp1;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp1;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Is unaligned access possible here? If yes, it probably should be
> grub_get_unaligned32.

No. It was hard-coded as 4092 from mkfs.

> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp1;
> > +
> 
> Should not CRC be converted from LE?

Fixed.

> 
> > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +
> > +  /* Read the 2nd cp block in this CP pack */
> > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_2)
> > +    goto invalid_cp1;
> > +
> > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > +  if (err)
> > +    goto invalid_cp2;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp2;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Ditto alignment.
> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> Ditto endianness.
> 
> > +    goto invalid_cp2;
> > +
> > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +  if (cur_version == pre_version)
> > +    {
> > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > +  cp1 = 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 += data->blocks_per_seg;
> > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > +  if (!cp2 && grub_errno)
> > +    {
> > +      grub_free (cp1);
> > +      return grub_errno;
> > +    }
> > +
> > +  if (cp1 && cp2)
> > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > +  else if (cp1)
> > +    cur_page = cp1;
> > +  else if (cp2)
> > +    cur_page = 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
> 
> static grub_error_t

Fixed.

> 
> > +get_nat_journal (struct grub_f2fs_data *data)
> > +{
> > +  grub_uint32_t block;
> > +  char *buf;
> > +  grub_err_t err;
> > +
> > +  buf = grub_malloc (F2FS_BLKSIZE);
> > +  if (!buf)
> > +    return grub_errno;
> > +
> > +  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
> > +    block = __start_sum_block (data);
> > +  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))
> 
> As mentioned, use grub_cpu_to_leXX_compile_time to avoid run time
> conversion.

Fixed.

> 
> > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > +  else
> > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > +
> > +  err = 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 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 = ADDRS_PER_BLOCK;
> > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > +  int n = 0;
> > +  int level = 0;
> > +
> > +  if (__inode_flag_set (inode, FI_INLINE_XATTR))
> > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > +
> > +  noffset[0] = 0;
> > +
> > +  if (block < direct_index)
> > +    {
> > +      offset[n] = block;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_index;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR1_BLOCK;
> > +      noffset[n] = 1;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR2_BLOCK;
> > +      noffset[n] = 2;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND1_BLOCK;
> > +      noffset[n] = 3;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 4 + offset[n - 1];
> 
> That does not fit. You declared offset and noffset as arrays of four
> elements and pass arrays of four elements; here is out of bound
> access already.

It is not out of bound access, since it decreases *block* at every condition
checks.
This function should hit only one of if {} conditions.

> 
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND2_BLOCK;
> > +      noffset[n] = 4 + dptrs_per_blk;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < dindirect_blks)
> > +    {
> > +      offset[n++] = NODE_DIND_BLOCK;
> > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > +      offset[n++] = block / indirect_blks;
> > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > +		offset[n - 1] * (dptrs_per_blk + 1);
> > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > +		offset[n - 2] * (dptrs_per_blk + 1) +
> > +		offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 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 = grub_malloc (__nat_bitmap_size (data));
> > +  if (!data->nat_bitmap)
> > +    return grub_errno;
> > +
> > +  version_bitmap = __nat_bitmap_ptr (data);
> > +
> > +  /* copy version bitmap */
> > +  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
> > +
> 
> Any reason to actually copy it? Why is it not possible to just set
> pointer to source, which is available all the time anyway?

Fixed not to allocate and copying this.

> 
> > +  err = 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 = get_node_blkaddr (data, nid);
> > +  if (!blkaddr)
> > +    return grub_errno;
> > +
> > +  return grub_f2fs_block_read (data, blkaddr, np);
> 
> Is struct grub_f2fs_node guaranteed to always have the same size as F2FS
> block? Then adding char [F2FS_BLKSIZE] to union to make it obvious is
> better and ensures that it will always be at least this size.

Fixed.

> 
> > +}
> > +
> > +static struct grub_f2fs_data *
> > +grub_f2fs_mount (grub_disk_t disk)
> > +{
> > +  struct grub_f2fs_data *data;
> > +  grub_err_t err;
> > +
> > +  data = grub_zalloc (sizeof (*data));
> > +  if (!data)
> > +    return NULL;
> > +
> > +  data->disk = disk;
> > +
> > +  err = grub_f2fs_read_sb (data, 0);
> > +  if (err)
> > +    {
> > +      err = grub_f2fs_read_sb (data, 1);
> > +      if (err)
> > +        {
> > +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");
> 
> May be mentioning that superblock could not be read? In another place
> you already tell that checkpoints could not be found. It helps to
> troubleshoot issues.

Fixed.

> 
> > +          goto fail;
> > +	}
> > +    }
> > +
> > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > +  data->blocks_per_seg = 1 <<
> > +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > +
> > +  err = grub_f2fs_read_cp (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  err = load_nat_info (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->diropen.data = data;
> > +  data->diropen.ino = data->root_ino;
> > +  data->diropen.inode_read = 1;
> > +  data->inode = &data->diropen.inode;
> > +
> > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > +  if (err)
> > +    goto fail;
> > +
> > +  return data;
> > +
> > +fail:
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Double free after load_nat_info failure. Assuming that we do need to
> allocate anything at all (see above).

Removed all grub_frees.

> 
> > +  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_ofs)
> 
> You have grub_f2fs_read_block and grub_f2fs_block_read. Could we make
> them more different and self-explaining? In particular, this one does
> not read anything, it returns disk address. grub_f2fs_map_file_block?

Good suggestion.
Changed to grub_f2fs_get_block.

> 
> > +{
> > +  struct grub_f2fs_data *data = node->data;
> > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > +  grub_uint32_t offset[4], noffset[4], nids[4];
> 
> See above about overflow in grub_get_inode_path.

No error.

> 
> > +  struct grub_f2fs_node *node_block;
> > +  grub_uint32_t block_addr = -1;
> > +  int level, i;
> > +
> > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > +  if (level == 0)
> > +      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > +
> > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!node_block)
> > +    return -1;
> > +
> > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > +
> > +  /* get indirect or direct nodes */
> > +  for (i = 1; i <= level; i++)
> > +    {
> > +      grub_f2fs_read_node (data, nids[i], node_block);
> > +      if (grub_errno)
> > +        goto fail;
> > +
> > +      if (i < level)
> > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > +    }
> > +
> > +  block_addr = grub_le_to_cpu32 (node_block->dn.addr[offset[level]]);
> > +fail:
> > +  grub_free (node_block);
> > +  return block_addr;
> > +}
> > +
> ...
> > +
> > +static char *
> > +grub_f2fs_read_symlink (grub_fshelp_node_t node)
> > +{
> > +  char *symlink;
> > +  struct grub_fshelp_node *diro = node;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  symlink = 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;
> > +    }
> > +
> 
> What about short read? Is this an error or not?

What is short read?
When I refer the other filesystem, it seems that it doesn't need to return
errors.

> 
> > +  symlink[__i_size (&diro->inode.i)] = '\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 = 0; i < ctx->max;)
> > +    {
> > +      char filename[F2FS_NAME_LEN + 1];
> 
> Could we avoid large stack allocations?

Fixed to allocate dynamically.

> 
> > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > +      enum FILE_TYPE ftype;
> > +      int name_len;
> > +
> > +      if (__test_bit (i, ctx->bitmap) == 0)
> 
> grub_f2fs_test_bit_le32?
> 
> > +        {
> > +          i++;
> > +          continue;
> > +        }
> > +
> > +      ftype = ctx->dentry[i].file_type;
> > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > +      filename[name_len] = '\0';
> > +
> > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > +      if (!fdiro)
> > +        return 0;
> > +
> > +      if (ftype == F2FS_FT_DIR)
> > +        type = GRUB_FSHELP_DIR;
> > +      else if (ftype == F2FS_FT_SYMLINK)
> > +        type = GRUB_FSHELP_SYMLINK;
> > +      else if (ftype == F2FS_FT_REG_FILE)
> > +        type = GRUB_FSHELP_REG;
> > +
> > +      fdiro->data = ctx->data;
> > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > +      fdiro->inode_read = 0;
> > +
> > +      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
> > +        return 1;
> > +
> > +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> > +    }
> > +    return 0;
> > +}
> > +
> ...
> > +
> > +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 = (struct grub_fshelp_node *) dir;
> > +  struct grub_f2fs_inode *inode;
> > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > +    .data = diro->data,
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  grub_off_t fpos = 0;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
> > +
> > +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> > +      ctx.dentry = de_blk->dentry;
> > +      ctx.filename = de_blk->filename;
> > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > +
> > +      if (grub_f2fs_check_dentries (&ctx))
> > +        return 1;
> 
> memory leak

Fixed.

> 
> > +
> > +      grub_free (buf);
> > +
> > +      fpos += F2FS_BLKSIZE;
> > +    }
> > +  return 0;
> > +}
> > +
> ...
> > +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 = {
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  ctx.data = 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 != &ctx.data->diropen)
> > +    grub_free (fdiro);
> > +  if (ctx.data)
> > +    grub_free (ctx.data->nat_bitmap);
> 
> Triple free :)

Removed nat_bitmap entirely. :)

> 
> > +  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 = NULL;
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = 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);
> sizeof (*data->inode)? Or they can be different?

Not a big deal. Fixed.

> 
> > +  grub_free (fdiro);
> > +
> > +  file->size = __i_size (&(data->inode->i));
> > +  file->data = data;
> > +  file->offset = 0;
> > +
> > +  return 0;
> > +
> > +fail:
> > +  if (fdiro != &data->diropen)
> > +    grub_free (fdiro);
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    {
> > +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> > +      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),
> 
> malloc failure check?

Fxied.

> 
> > +				data->sblock.volume_name, 512);
> 
> Where 512 comes from? Should it not be sizeof
> (data->sblock.volume_name) as well?

Fixed regarding to mkfs.f2fs handling.

> 
> > +    }
> > +  else
> > +    *label = NULL;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  grub_free (data);
> > +  grub_dl_unref (my_mod);
> > +  return grub_errno;
> > +}
> > +
> ...
> > 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=8
> >  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
> >  	MAXLOGSECSIZE=12;;
> > -    xxfs)
> > +    xxfs|xf2fs)
> >  	MINLOGSECSIZE=9
> >    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
> >  	MAXLOGSECSIZE=12;;
> > @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    fi
> >  	    MAXBLKSIZE=4096
> >  	    ;;
> > +	xf2fs)
> > +	    MINBLKSIZE=$SECSIZE
> > +		# OS Limitation: GNU/Linux doesn't accept > 4096
> > +	    MAXBLKSIZE=4096;;
> >  	xsquash*)
> >  	    MINBLKSIZE=4096
> >  	    MAXBLKSIZE=1048576;;
> > @@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
> >  		x"btrfs"*)
> >  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> > -
> > +	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars
> 
> F2FS, not btrfs
> 
> > +		x"f2fs")
> > +		    FSLABEL="grub_;/testjaegeuk kim 
> 
> Could you leave initial part in place? This includes some funny UNICODE
> characters for a reason, actually. Unless this is not possible with
> f2fs?

I found that mkfs.f2fs doesn't handle utf conversion correctly.
So, for now, I'd like to add just few characters.

Please, v2 patch.

Thanks,

> 
> f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
> >  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >  		x"exfat")
> >  		    FSLABEL="géт ;/莭莽😁кир";;
> > @@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
> >  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
> >  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> > -		    | 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=y;;
> > @@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >  		    MOUNTFS=ext2
> >  		    "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)

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
@ 2015-04-03 22:48     ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-04-03 22:48 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: grub-devel, linux-f2fs-devel

Hi Andrei,

On Sat, Mar 28, 2015 at 10:31:55AM +0300, Andrei Borzenkov wrote:
> В Tue, 24 Mar 2015 01:19:00 -0700
> Jaegeuk Kim <jaegeuk@kernel.org> пишет:
> 

...

> > +/* 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
> 
> (2 << F2FS_BLK_BITS)?
> 
> > +#define F2FS_BLK_BITS			12
> > +#define F2FS_BLK_SEC_BITS		(3)
> 
> 
> It is confusing to have some defines parenthesized and some not. Could
> it be unified somehow?
> 
> Also this can be computed from F2FS_BLK_BITS and GRUB_DISK_SECTOR_BITS
> - one magic number less.

Fixed.

> 
> ...
> > +struct grub_f2fs_inline_dentry
> > +{
> > +  grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
> 
> This is cast to grub_uint32_t everywhere. Can it be non-multiple of 4
> bytes? If not, may be just define as such? 

I remained this, since this can be non-multiple of 4 bytes.

> 
> > +  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;
> > +

...

> > +
> > +#define ver_after (a, b) (typecheck (unsigned long long, a) &&          \
> > +		typecheck (unsigned long long, b) &&                    \
> > +		((long long)((a) - (b)) > 0))
> > +
> 
> Where typecheck definition comes from?

Removed this.

> 
> ...
> > +
> > +static inline int
> > +__test_bit (int nr, grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & (31)));
> Extra parenthesis (31)

Fixed.

> 
> > +}
> > +
> 
> It is used for dentry_bitmap which is kept in LE format and not
> converted as far as I can tell. This needs fixing for BE systems. Linux
> kernel is explicitly using test_bit_le here. This will also work for
> inode flags (just skip explicit conversion).
> 
> There are two functions with more or less identical names. May be make
> them
> 
> grub_f2fs_test_bit_le32
> grub_f2fs_test_bit
> 
> As a general comment - marking non-modified arguments as const
> everywhere would be good.

I added:
grub_f2fs_general_test_bit
grub_f2fs_test_bit

Both of them keep bit streams without endian conversion, and the difference is
beyond handling the order of bit stream indices.

> 
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&(inode->i_addr[1]);
> Redundant parens around inode->

Fixed.

> 
> > +}
> > +
> > +static inline grub_uint64_t
> > +__i_size (struct grub_f2fs_inode *inode)
> 
> Could we make it grub_f2fs_file_size or similar? i_size really does not
> tell much outside of linux kernel.

Changed to grub_f2fs_file_size. 

> 
> > +{
> > +  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 = &data->ckpt;
> > +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> > +  grub_uint32_t start_addr = 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 = &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 = &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 = grub_le_to_cpu32 (ckpt->ckpt_flags);
> > +  return ckpt_flags & f;
> 
> All flags are constant so you can simply do 
> 
> ckpt->ckpt_flags & grub_cpu_to_le32_compile_time (FLAG)
> 
> in place to avoid extra calls. This makes function redundant.

Fixed.

> 
> > +}
> > +
> > +static inline int
> > +__inode_flag_set (struct grub_f2fs_inode *inode, int flag)

Fixed to refer i_inline. This was a bug.

...

> > + * CRC32
> > + */
> > +#define CRCPOLY_LE 0xedb88320
> > +
> > +static inline grub_uint32_t
> > +grub_f2fs_cal_crc32 (grub_uint32_t crc, void *buf, int len)
> 
> Why crc is parameter here? This function is used exactly once with
> fixed value for initial crc.

Fixed.

> 
> > +{
> > +  int i;
> > +  unsigned char *p = (unsigned char *)buf;
> > +
> > +  while (len--)
> > +    {
> > +      crc ^= *p++;
> > +      for (i = 0; i < 8; i++)
> > +        crc = (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 = 0;
> > +
> > +  cal_crc = grub_f2fs_cal_crc32 (F2FS_SUPER_MAGIC, buf, len);
> > +
> > +  return (cal_crc == blk_crc) ? 1 : 0;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +  char *addr = (char *)p;
> 
> Why cast? We are not going to modify it, right?

Right.

> 
> > +
> > +  addr += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return (mask & *addr) != 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  unsigned int blocksize;
> > +
> > +  if (F2FS_SUPER_MAGIC != grub_le_to_cpu32 (sb->magic))
> 
> sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC)

Fixed.

> 
> > +    return -1;
> > +
> > +  blocksize = 1 << grub_le_to_cpu32 (sb->log_blocksize);
> > +  if (blocksize != F2FS_BLKSIZE)
> 
> sb->log_blksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS)

Fixed.

> 
> > +    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) != F2FS_MAX_LOG_SECTOR_SIZE)
> 
> Should not it be F2FS_BLKSIZE? At least it sounds logical. Also please
> convert log_sectorsize just once.

Fixed.

> 
> > +    return -1;
> > +
> > +  return 0;
> > +}
> > +
> > +static grub_err_t
> > +grub_f2fs_read_sb (struct grub_f2fs_data *data, int block)
> > +{
> > +  grub_disk_t disk = data->disk;
> > +  grub_uint64_t offset;
> > +  grub_err_t err;
> > +
> > +  if (block == 0)
> > +    offset = F2FS_SUPER_OFFSET;
> > +  else
> > +    offset = F2FS_BLKSIZE + F2FS_SUPER_OFFSET;
> > +
> 
> Please name it "secondary" or similar instead of "block" to avoid
> confusion. You do not really want to read arbitrary block, right?
> 
> offset = F2FS_SUPER_OFFEST;
> if (secondary)
>   offset += F2FS_BLKSIZE;

Fixed as your latest comment.

> 
> > +  /* Read first super block. */
> > +  err = 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 = 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 = 0, pre_version = 0;
> > +  grub_uint32_t crc = 0;
> > +  grub_uint32_t crc_offset;
> > +  grub_err_t err;
> > +
> > +  /* Read the 1st cp block in this CP pack */
> > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_1)
> > +    return NULL;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > +  if (err)
> > +    goto invalid_cp1;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp1;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Is unaligned access possible here? If yes, it probably should be
> grub_get_unaligned32.

No. It was hard-coded as 4092 from mkfs.

> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp1;
> > +
> 
> Should not CRC be converted from LE?

Fixed.

> 
> > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +
> > +  /* Read the 2nd cp block in this CP pack */
> > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_2)
> > +    goto invalid_cp1;
> > +
> > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > +  if (err)
> > +    goto invalid_cp2;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset >= F2FS_BLKSIZE)
> > +    goto invalid_cp2;
> > +
> > +  crc = *(grub_uint32_t *)((char *)cp_block + crc_offset);
> 
> Ditto alignment.
> 
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> Ditto endianness.
> 
> > +    goto invalid_cp2;
> > +
> > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +  if (cur_version == pre_version)
> > +    {
> > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > +  cp1 = 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 += data->blocks_per_seg;
> > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > +  if (!cp2 && grub_errno)
> > +    {
> > +      grub_free (cp1);
> > +      return grub_errno;
> > +    }
> > +
> > +  if (cp1 && cp2)
> > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > +  else if (cp1)
> > +    cur_page = cp1;
> > +  else if (cp2)
> > +    cur_page = 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
> 
> static grub_error_t

Fixed.

> 
> > +get_nat_journal (struct grub_f2fs_data *data)
> > +{
> > +  grub_uint32_t block;
> > +  char *buf;
> > +  grub_err_t err;
> > +
> > +  buf = grub_malloc (F2FS_BLKSIZE);
> > +  if (!buf)
> > +    return grub_errno;
> > +
> > +  if (__ckpt_flag_set (&data->ckpt, CP_COMPACT_SUM_FLAG))
> > +    block = __start_sum_block (data);
> > +  else if (__ckpt_flag_set (&data->ckpt, CP_UMOUNT_FLAG))
> 
> As mentioned, use grub_cpu_to_leXX_compile_time to avoid run time
> conversion.

Fixed.

> 
> > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > +  else
> > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > +
> > +  err = 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 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 = ADDRS_PER_BLOCK;
> > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > +  int n = 0;
> > +  int level = 0;
> > +
> > +  if (__inode_flag_set (inode, FI_INLINE_XATTR))
> > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > +
> > +  noffset[0] = 0;
> > +
> > +  if (block < direct_index)
> > +    {
> > +      offset[n] = block;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_index;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR1_BLOCK;
> > +      noffset[n] = 1;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR2_BLOCK;
> > +      noffset[n] = 2;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND1_BLOCK;
> > +      noffset[n] = 3;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 4 + offset[n - 1];
> 
> That does not fit. You declared offset and noffset as arrays of four
> elements and pass arrays of four elements; here is out of bound
> access already.

It is not out of bound access, since it decreases *block* at every condition
checks.
This function should hit only one of if {} conditions.

> 
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND2_BLOCK;
> > +      noffset[n] = 4 + dptrs_per_blk;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < dindirect_blks)
> > +    {
> > +      offset[n++] = NODE_DIND_BLOCK;
> > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > +      offset[n++] = block / indirect_blks;
> > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > +		offset[n - 1] * (dptrs_per_blk + 1);
> > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > +		offset[n - 2] * (dptrs_per_blk + 1) +
> > +		offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 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 = grub_malloc (__nat_bitmap_size (data));
> > +  if (!data->nat_bitmap)
> > +    return grub_errno;
> > +
> > +  version_bitmap = __nat_bitmap_ptr (data);
> > +
> > +  /* copy version bitmap */
> > +  grub_memcpy (data->nat_bitmap, version_bitmap, __nat_bitmap_size (data));
> > +
> 
> Any reason to actually copy it? Why is it not possible to just set
> pointer to source, which is available all the time anyway?

Fixed not to allocate and copying this.

> 
> > +  err = 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 = get_node_blkaddr (data, nid);
> > +  if (!blkaddr)
> > +    return grub_errno;
> > +
> > +  return grub_f2fs_block_read (data, blkaddr, np);
> 
> Is struct grub_f2fs_node guaranteed to always have the same size as F2FS
> block? Then adding char [F2FS_BLKSIZE] to union to make it obvious is
> better and ensures that it will always be at least this size.

Fixed.

> 
> > +}
> > +
> > +static struct grub_f2fs_data *
> > +grub_f2fs_mount (grub_disk_t disk)
> > +{
> > +  struct grub_f2fs_data *data;
> > +  grub_err_t err;
> > +
> > +  data = grub_zalloc (sizeof (*data));
> > +  if (!data)
> > +    return NULL;
> > +
> > +  data->disk = disk;
> > +
> > +  err = grub_f2fs_read_sb (data, 0);
> > +  if (err)
> > +    {
> > +      err = grub_f2fs_read_sb (data, 1);
> > +      if (err)
> > +        {
> > +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem");
> 
> May be mentioning that superblock could not be read? In another place
> you already tell that checkpoints could not be found. It helps to
> troubleshoot issues.

Fixed.

> 
> > +          goto fail;
> > +	}
> > +    }
> > +
> > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > +  data->blocks_per_seg = 1 <<
> > +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > +
> > +  err = grub_f2fs_read_cp (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  err = load_nat_info (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->diropen.data = data;
> > +  data->diropen.ino = data->root_ino;
> > +  data->diropen.inode_read = 1;
> > +  data->inode = &data->diropen.inode;
> > +
> > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > +  if (err)
> > +    goto fail;
> > +
> > +  return data;
> > +
> > +fail:
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Double free after load_nat_info failure. Assuming that we do need to
> allocate anything at all (see above).

Removed all grub_frees.

> 
> > +  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_ofs)
> 
> You have grub_f2fs_read_block and grub_f2fs_block_read. Could we make
> them more different and self-explaining? In particular, this one does
> not read anything, it returns disk address. grub_f2fs_map_file_block?

Good suggestion.
Changed to grub_f2fs_get_block.

> 
> > +{
> > +  struct grub_f2fs_data *data = node->data;
> > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > +  grub_uint32_t offset[4], noffset[4], nids[4];
> 
> See above about overflow in grub_get_inode_path.

No error.

> 
> > +  struct grub_f2fs_node *node_block;
> > +  grub_uint32_t block_addr = -1;
> > +  int level, i;
> > +
> > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > +  if (level == 0)
> > +      return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > +
> > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!node_block)
> > +    return -1;
> > +
> > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > +
> > +  /* get indirect or direct nodes */
> > +  for (i = 1; i <= level; i++)
> > +    {
> > +      grub_f2fs_read_node (data, nids[i], node_block);
> > +      if (grub_errno)
> > +        goto fail;
> > +
> > +      if (i < level)
> > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > +    }
> > +
> > +  block_addr = grub_le_to_cpu32 (node_block->dn.addr[offset[level]]);
> > +fail:
> > +  grub_free (node_block);
> > +  return block_addr;
> > +}
> > +
> ...
> > +
> > +static char *
> > +grub_f2fs_read_symlink (grub_fshelp_node_t node)
> > +{
> > +  char *symlink;
> > +  struct grub_fshelp_node *diro = node;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  symlink = 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;
> > +    }
> > +
> 
> What about short read? Is this an error or not?

What is short read?
When I refer the other filesystem, it seems that it doesn't need to return
errors.

> 
> > +  symlink[__i_size (&diro->inode.i)] = '\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 = 0; i < ctx->max;)
> > +    {
> > +      char filename[F2FS_NAME_LEN + 1];
> 
> Could we avoid large stack allocations?

Fixed to allocate dynamically.

> 
> > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > +      enum FILE_TYPE ftype;
> > +      int name_len;
> > +
> > +      if (__test_bit (i, ctx->bitmap) == 0)
> 
> grub_f2fs_test_bit_le32?
> 
> > +        {
> > +          i++;
> > +          continue;
> > +        }
> > +
> > +      ftype = ctx->dentry[i].file_type;
> > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > +      filename[name_len] = '\0';
> > +
> > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > +      if (!fdiro)
> > +        return 0;
> > +
> > +      if (ftype == F2FS_FT_DIR)
> > +        type = GRUB_FSHELP_DIR;
> > +      else if (ftype == F2FS_FT_SYMLINK)
> > +        type = GRUB_FSHELP_SYMLINK;
> > +      else if (ftype == F2FS_FT_REG_FILE)
> > +        type = GRUB_FSHELP_REG;
> > +
> > +      fdiro->data = ctx->data;
> > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > +      fdiro->inode_read = 0;
> > +
> > +      if (ctx->hook (filename, type, fdiro, ctx->hook_data))
> > +        return 1;
> > +
> > +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> > +    }
> > +    return 0;
> > +}
> > +
> ...
> > +
> > +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 = (struct grub_fshelp_node *) dir;
> > +  struct grub_f2fs_inode *inode;
> > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > +    .data = diro->data,
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  grub_off_t fpos = 0;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +	return 0;
> > +    }
> > +
> > +  inode = &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 = 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 = (struct grub_f2fs_dentry_block *) buf;
> > +
> > +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> > +      ctx.dentry = de_blk->dentry;
> > +      ctx.filename = de_blk->filename;
> > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > +
> > +      if (grub_f2fs_check_dentries (&ctx))
> > +        return 1;
> 
> memory leak

Fixed.

> 
> > +
> > +      grub_free (buf);
> > +
> > +      fpos += F2FS_BLKSIZE;
> > +    }
> > +  return 0;
> > +}
> > +
> ...
> > +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 = {
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  ctx.data = 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 != &ctx.data->diropen)
> > +    grub_free (fdiro);
> > +  if (ctx.data)
> > +    grub_free (ctx.data->nat_bitmap);
> 
> Triple free :)

Removed nat_bitmap entirely. :)

> 
> > +  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 = NULL;
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = 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);
> sizeof (*data->inode)? Or they can be different?

Not a big deal. Fixed.

> 
> > +  grub_free (fdiro);
> > +
> > +  file->size = __i_size (&(data->inode->i));
> > +  file->data = data;
> > +  file->offset = 0;
> > +
> > +  return 0;
> > +
> > +fail:
> > +  if (fdiro != &data->diropen)
> > +    grub_free (fdiro);
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  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 = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    {
> > +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> > +      grub_utf16_to_utf8 ((grub_uint8_t *) (*label),
> 
> malloc failure check?

Fxied.

> 
> > +				data->sblock.volume_name, 512);
> 
> Where 512 comes from? Should it not be sizeof
> (data->sblock.volume_name) as well?

Fixed regarding to mkfs.f2fs handling.

> 
> > +    }
> > +  else
> > +    *label = NULL;
> > +
> > +  if (data)
> > +    grub_free (data->nat_bitmap);
> 
> Again.
> 
> > +  grub_free (data);
> > +  grub_dl_unref (my_mod);
> > +  return grub_errno;
> > +}
> > +
> ...
> > 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=8
> >  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
> >  	MAXLOGSECSIZE=12;;
> > -    xxfs)
> > +    xxfs|xf2fs)
> >  	MINLOGSECSIZE=9
> >    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
> >  	MAXLOGSECSIZE=12;;
> > @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    fi
> >  	    MAXBLKSIZE=4096
> >  	    ;;
> > +	xf2fs)
> > +	    MINBLKSIZE=$SECSIZE
> > +		# OS Limitation: GNU/Linux doesn't accept > 4096
> > +	    MAXBLKSIZE=4096;;
> >  	xsquash*)
> >  	    MINBLKSIZE=4096
> >  	    MAXBLKSIZE=1048576;;
> > @@ -256,7 +260,9 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
> >  		x"btrfs"*)
> >  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> > -
> > +	    # FS LIMITATION: btrfs label is at most 512 UTF-16 chars
> 
> F2FS, not btrfs
> 
> > +		x"f2fs")
> > +		    FSLABEL="grub_;/testjaegeuk kim 
> 
> Could you leave initial part in place? This includes some funny UNICODE
> characters for a reason, actually. Unless this is not possible with
> f2fs?

I found that mkfs.f2fs doesn't handle utf conversion correctly.
So, for now, I'd like to add just few characters.

Please, v2 patch.

Thanks,

> 
> f2fsaskdfjkasdlfajskdfjaksdjfkjaskjkjkzjkjckzjvkcjkjkjekqjkwejkqwrlkasdfjksadjflaskdhzxhvjzxchvjzkxchvjkhakjsdhfjkhqjkwehrjkhasjkdfhjkashdfjkhjzkxhcjkvzhxcjkvhzxjchvkzhxckjvhjzkxchvjkhzjkxchvjkzhxckjvhzkxjchvkjzxhckjvzxcjkvhjzxkchkvjhzxkjcvhjkhjkahsjkdhkjqhwekrjhakjsdfhkjashdkjzhxcvjkhzxcvzxcvggggggggggf";;
> >  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >  		x"exfat")
> >  		    FSLABEL="géт ;/莭莽😁кир";;
> > @@ -466,7 +472,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
> >  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
> >  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> > -		    | 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=y;;
> > @@ -745,6 +751,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >  		    MOUNTFS=ext2
> >  		    "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)


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
  2015-03-24  8:19 ` Jaegeuk Kim
@ 2015-04-03 22:49   ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-04-03 22:49 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

 http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
 http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

This patch changes:

 * 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 <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1308 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   15 +-
 7 files changed, 1355 insertions(+), 5 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

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 = {
   common = grub-core/fs/newc.c;
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/exfat.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
@@ -767,6 +768,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = 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 filesystem 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}, @dfn{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, minix,
 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 charset
 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 = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = exfat;
   common = fs/exfat.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..e6b8386
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1308 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+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_BLK_BITS			12
+#define F2FS_BLKSIZE			(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS		(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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)	((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
+
+#define CKPT_FLAG_SET(ckpt, f)			\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+enum
+  {
+    FI_INLINE_XATTR = 9,
+    FI_INLINE_DATA = 10,
+    FI_INLINE_DENTRY = 11,
+    FI_DATA_EXIST = 18,
+  };
+
+enum FILE_TYPE
+  {
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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
+grub_generic_test_bit (int nr, const 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
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
+  grub_uint32_t start_addr = 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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+		grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+		- (base + 1) + type;
+}
+
+static inline int
+__inode_inline_set (struct grub_f2fs_inode *inode, int flag)
+{
+  return inode->i_inline & flag;
+}
+
+static inline grub_uint32_t
+__nat_bitmap_size (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &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 = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return (mask & *p) != 0;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static grub_err_t
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_uint64_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = 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 = 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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = 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 grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (__inode_inline_set (inode, FI_INLINE_XATTR))
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+		offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) +
+		offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_zalloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  err = grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET);
+  if (err)
+    {
+      err = grub_f2fs_read_sb (data, F2FS_BLKSIZE + F2FS_SUPER_OFFSET);
+      if (err)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem (no superblock)");
+          goto fail;
+	}
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &(node->inode.i);
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (__inode_inline_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 = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_zalloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (__inode_inline_set (inode, FI_INLINE_DENTRY))
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+		      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+  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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static void
+grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
+{
+  grub_uint16_t *pchTempPtr = in_buf;
+  grub_uint8_t *pwTempPtr = out_buf;
+
+  while (*pchTempPtr != '\0')
+  {
+    *pwTempPtr = (grub_uint8_t) *pchTempPtr;
+    pchTempPtr++;
+    pwTempPtr++;
+  }
+  *pwTempPtr = '\0';
+  return;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *label = grub_zalloc (sizeof (data->sblock.volume_name));
+      if (*label)
+        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
+				data->sblock.volume_name);
+    }
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = 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 ""
 
 #: 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" = "x" ] ; then
+  EUID=`id -u`
+fi
+
+if [ "$EUID" != 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..7d2d512 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -36,7 +36,7 @@ case x"$fs" in
 	MINLOGSECSIZE=8
 	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
 	MAXLOGSECSIZE=12;;
-    xxfs)
+    xxfs|xf2fs)
 	MINLOGSECSIZE=9
   	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
 	MAXLOGSECSIZE=12;;
@@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    fi
 	    MAXBLKSIZE=4096
 	    ;;
+	xf2fs)
+	    MINBLKSIZE=$SECSIZE
+		# OS Limitation: GNU/Linux doesn't accept > 4096
+	    MAXBLKSIZE=4096;;
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
@@ -256,7 +260,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
-
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+	    # TODO: seems that mkfs.f2fs doesn't handle encoding stuffs correctly
+		x"f2fs")
+		    FSLABEL="grub_test:no utf16";;
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -466,7 +473,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -745,6 +752,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.1.1


------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
@ 2015-04-03 22:49   ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-04-03 22:49 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

 http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
 http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

This patch changes:

 * 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 <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1308 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   15 +-
 7 files changed, 1355 insertions(+), 5 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

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 = {
   common = grub-core/fs/newc.c;
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/exfat.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
@@ -767,6 +768,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = 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 filesystem 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}, @dfn{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, minix,
 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 charset
 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 = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = exfat;
   common = fs/exfat.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..e6b8386
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1308 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+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_BLK_BITS			12
+#define F2FS_BLKSIZE			(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS		(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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)	((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
+
+#define CKPT_FLAG_SET(ckpt, f)			\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+enum
+  {
+    FI_INLINE_XATTR = 9,
+    FI_INLINE_DATA = 10,
+    FI_INLINE_DENTRY = 11,
+    FI_DATA_EXIST = 18,
+  };
+
+enum FILE_TYPE
+  {
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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
+grub_generic_test_bit (int nr, const 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
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
+  grub_uint32_t start_addr = 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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+		grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+		- (base + 1) + type;
+}
+
+static inline int
+__inode_inline_set (struct grub_f2fs_inode *inode, int flag)
+{
+  return inode->i_inline & flag;
+}
+
+static inline grub_uint32_t
+__nat_bitmap_size (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &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 = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return (mask & *p) != 0;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static grub_err_t
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_uint64_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = 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 = 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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset >= F2FS_BLKSIZE)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = 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 grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (__inode_inline_set (inode, FI_INLINE_XATTR))
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+		offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) +
+		offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_zalloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  err = grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET);
+  if (err)
+    {
+      err = grub_f2fs_read_sb (data, F2FS_BLKSIZE + F2FS_SUPER_OFFSET);
+      if (err)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem (no superblock)");
+          goto fail;
+	}
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &(node->inode.i);
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (__inode_inline_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 = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_zalloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (__inode_inline_set (inode, FI_INLINE_DENTRY))
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+		      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+  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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static void
+grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
+{
+  grub_uint16_t *pchTempPtr = in_buf;
+  grub_uint8_t *pwTempPtr = out_buf;
+
+  while (*pchTempPtr != '\0')
+  {
+    *pwTempPtr = (grub_uint8_t) *pchTempPtr;
+    pchTempPtr++;
+    pwTempPtr++;
+  }
+  *pwTempPtr = '\0';
+  return;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *label = grub_zalloc (sizeof (data->sblock.volume_name));
+      if (*label)
+        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
+				data->sblock.volume_name);
+    }
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = 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 ""
 
 #: 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" = "x" ] ; then
+  EUID=`id -u`
+fi
+
+if [ "$EUID" != 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..7d2d512 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -36,7 +36,7 @@ case x"$fs" in
 	MINLOGSECSIZE=8
 	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
 	MAXLOGSECSIZE=12;;
-    xxfs)
+    xxfs|xf2fs)
 	MINLOGSECSIZE=9
   	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
 	MAXLOGSECSIZE=12;;
@@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    fi
 	    MAXBLKSIZE=4096
 	    ;;
+	xf2fs)
+	    MINBLKSIZE=$SECSIZE
+		# OS Limitation: GNU/Linux doesn't accept > 4096
+	    MAXBLKSIZE=4096;;
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
@@ -256,7 +260,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
-
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+	    # TODO: seems that mkfs.f2fs doesn't handle encoding stuffs correctly
+		x"f2fs")
+		    FSLABEL="grub_test:no utf16";;
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -466,7 +473,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -745,6 +752,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.1.1



^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v2] F2FS support
  2015-04-03 22:49   ` Jaegeuk Kim
@ 2015-04-29 20:48     ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-04-29 20:48 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel

Hi folks,

Any comments on this patch?

Thanks,

On Fri, Apr 03, 2015 at 03:49:08PM -0700, Jaegeuk Kim wrote:
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
>  http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
>  http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 
> This patch changes:
> 
>  * 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 <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1308 ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   15 +-
>  7 files changed, 1355 insertions(+), 5 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
> 
> 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 = {
>    common = grub-core/fs/newc.c;
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/exfat.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
> @@ -767,6 +768,12 @@ script = {
>  
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = 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 filesystem 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}, @dfn{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, minix,
>  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 charset
>  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 = {
>  };
>  
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = exfat;
>    common = fs/exfat.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..e6b8386
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1308 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +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_BLK_BITS			12
> +#define F2FS_BLKSIZE			(1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS		(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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)	((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
> +
> +#define CKPT_FLAG_SET(ckpt, f)			\
> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +enum
> +  {
> +    FI_INLINE_XATTR = 9,
> +    FI_INLINE_DATA = 10,
> +    FI_INLINE_DENTRY = 11,
> +    FI_DATA_EXIST = 18,
> +  };
> +
> +enum FILE_TYPE
> +  {
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 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;
> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
> +  };
> +  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
> +grub_generic_test_bit (int nr, const 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
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> +  grub_uint32_t start_addr = 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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +		grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +		- (base + 1) + type;
> +}
> +
> +static inline int
> +__inode_inline_set (struct grub_f2fs_inode *inode, int flag)
> +{
> +  return inode->i_inline & flag;
> +}
> +
> +static inline grub_uint32_t
> +__nat_bitmap_size (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &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 = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return (mask & *p) != 0;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static grub_err_t
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_uint64_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = 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 = 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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = 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 grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (__inode_inline_set (inode, FI_INLINE_XATTR))
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +		offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) +
> +		offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_zalloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  err = grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET);
> +  if (err)
> +    {
> +      err = grub_f2fs_read_sb (data, F2FS_BLKSIZE + F2FS_SUPER_OFFSET);
> +      if (err)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem (no superblock)");
> +          goto fail;
> +	}
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &(node->inode.i);
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (__inode_inline_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 = 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_zalloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (__inode_inline_set (inode, FI_INLINE_DENTRY))
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> +		      grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> +  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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  file->size = grub_f2fs_file_size (&(data->inode->i));
> +  file->data = data;
> +  file->offset = 0;
> +
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static void
> +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> +{
> +  grub_uint16_t *pchTempPtr = in_buf;
> +  grub_uint8_t *pwTempPtr = out_buf;
> +
> +  while (*pchTempPtr != '\0')
> +  {
> +    *pwTempPtr = (grub_uint8_t) *pchTempPtr;
> +    pchTempPtr++;
> +    pwTempPtr++;
> +  }
> +  *pwTempPtr = '\0';
> +  return;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> +      if (*label)
> +        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
> +				data->sblock.volume_name);
> +    }
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +	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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = 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 ""
>  
>  #: 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" = "x" ] ; then
> +  EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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..7d2d512 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -36,7 +36,7 @@ case x"$fs" in
>  	MINLOGSECSIZE=8
>  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
>  	MAXLOGSECSIZE=12;;
> -    xxfs)
> +    xxfs|xf2fs)
>  	MINLOGSECSIZE=9
>    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
>  	MAXLOGSECSIZE=12;;
> @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    fi
>  	    MAXBLKSIZE=4096
>  	    ;;
> +	xf2fs)
> +	    MINBLKSIZE=$SECSIZE
> +		# OS Limitation: GNU/Linux doesn't accept > 4096
> +	    MAXBLKSIZE=4096;;
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> @@ -256,7 +260,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> -
> +	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +	    # TODO: seems that mkfs.f2fs doesn't handle encoding stuffs correctly
> +		x"f2fs")
> +		    FSLABEL="grub_test:no utf16";;
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -466,7 +473,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -745,6 +752,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)
> -- 
> 2.1.1
> 
> 
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website, sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for all
> things parallel software development, from weekly thought leadership blogs to
> news, videos, case studies, tutorials and more. Take a look and join the 
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v2] F2FS support
@ 2015-04-29 20:48     ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-04-29 20:48 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel

Hi folks,

Any comments on this patch?

Thanks,

On Fri, Apr 03, 2015 at 03:49:08PM -0700, Jaegeuk Kim wrote:
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
>  http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
>  http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 
> This patch changes:
> 
>  * 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 <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1308 ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   15 +-
>  7 files changed, 1355 insertions(+), 5 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
> 
> 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 = {
>    common = grub-core/fs/newc.c;
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/exfat.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
> @@ -767,6 +768,12 @@ script = {
>  
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = 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 filesystem 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}, @dfn{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, minix,
>  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 charset
>  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 = {
>  };
>  
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = exfat;
>    common = fs/exfat.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..e6b8386
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1308 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +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_BLK_BITS			12
> +#define F2FS_BLKSIZE			(1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS		(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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)	((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
> +
> +#define CKPT_FLAG_SET(ckpt, f)			\
> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +enum
> +  {
> +    FI_INLINE_XATTR = 9,
> +    FI_INLINE_DATA = 10,
> +    FI_INLINE_DENTRY = 11,
> +    FI_DATA_EXIST = 18,
> +  };
> +
> +enum FILE_TYPE
> +  {
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 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;
> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
> +  };
> +  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
> +grub_generic_test_bit (int nr, const 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
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> +  grub_uint32_t start_addr = 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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +		grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +		- (base + 1) + type;
> +}
> +
> +static inline int
> +__inode_inline_set (struct grub_f2fs_inode *inode, int flag)
> +{
> +  return inode->i_inline & flag;
> +}
> +
> +static inline grub_uint32_t
> +__nat_bitmap_size (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &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 = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return (mask & *p) != 0;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static grub_err_t
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_uint64_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = 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 = 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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = 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 grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (__inode_inline_set (inode, FI_INLINE_XATTR))
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +		offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) +
> +		offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_zalloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  err = grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET);
> +  if (err)
> +    {
> +      err = grub_f2fs_read_sb (data, F2FS_BLKSIZE + F2FS_SUPER_OFFSET);
> +      if (err)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem (no superblock)");
> +          goto fail;
> +	}
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &(node->inode.i);
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (__inode_inline_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 = 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_zalloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (__inode_inline_set (inode, FI_INLINE_DENTRY))
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> +		      grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> +  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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  file->size = grub_f2fs_file_size (&(data->inode->i));
> +  file->data = data;
> +  file->offset = 0;
> +
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static void
> +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> +{
> +  grub_uint16_t *pchTempPtr = in_buf;
> +  grub_uint8_t *pwTempPtr = out_buf;
> +
> +  while (*pchTempPtr != '\0')
> +  {
> +    *pwTempPtr = (grub_uint8_t) *pchTempPtr;
> +    pchTempPtr++;
> +    pwTempPtr++;
> +  }
> +  *pwTempPtr = '\0';
> +  return;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> +      if (*label)
> +        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
> +				data->sblock.volume_name);
> +    }
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +	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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = 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 ""
>  
>  #: 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" = "x" ] ; then
> +  EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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..7d2d512 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -36,7 +36,7 @@ case x"$fs" in
>  	MINLOGSECSIZE=8
>  	    #  OS LIMITATION: It could go up to 32768 but Linux rejects sector sizes > 4096
>  	MAXLOGSECSIZE=12;;
> -    xxfs)
> +    xxfs|xf2fs)
>  	MINLOGSECSIZE=9
>    	    # OS LIMITATION: GNU/Linux doesn't accept > 4096
>  	MAXLOGSECSIZE=12;;
> @@ -135,6 +135,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    fi
>  	    MAXBLKSIZE=4096
>  	    ;;
> +	xf2fs)
> +	    MINBLKSIZE=$SECSIZE
> +		# OS Limitation: GNU/Linux doesn't accept > 4096
> +	    MAXBLKSIZE=4096;;
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> @@ -256,7 +260,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FS LIMITATION: btrfs label is at most 255 UTF-8 chars
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> -
> +	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +	    # TODO: seems that mkfs.f2fs doesn't handle encoding stuffs correctly
> +		x"f2fs")
> +		    FSLABEL="grub_test:no utf16";;
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -466,7 +473,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs"| x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -745,6 +752,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)
> -- 
> 2.1.1
> 
> 
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website, sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for all
> things parallel software development, from weekly thought leadership blogs to
> news, videos, case studies, tutorials and more. Take a look and join the 
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v2] F2FS support
  2015-04-29 20:48     ` Jaegeuk Kim
@ 2015-04-30  3:32       ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-04-30  3:32 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Wed, 29 Apr 2015 13:48:21 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

> Hi folks,
> 
> Any comments on this patch?
> 

Sorry, still did not get around, hopefully someone beats me :)

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v2] F2FS support
@ 2015-04-30  3:32       ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-04-30  3:32 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Wed, 29 Apr 2015 13:48:21 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

> Hi folks,
> 
> Any comments on this patch?
> 

Sorry, still did not get around, hopefully someone beats me :)


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
  2015-04-03 22:49   ` Jaegeuk Kim
@ 2015-05-02 17:15     ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-05-02 17:15 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

Sorry for delay.


В Fri, 3 Apr 2015 15:49:08 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

> 
> This patch changes:
> 
>  * 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.
> 

Drop file list, it is available from git.

...
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..e6b8386
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> +
...
> +#define JENTRY_SIZE		13

sizeof (struct grub_f2fs_nat_jent), right?

...
> +
> +#define NAT_ENTRY_SIZE 		9

That's sizeof (struct grub_f2fs_nat_entry)?

...
> +#define ver_after (a, b)	((long long)((a) - (b)) > 0)

This macro is not used anywhere

> +#define F2FS_NAME_LEN		255
> +#define F2FS_SLOT_LEN		8
> +#define NR_DENTRY_IN_BLOCK	214
> +#define SIZE_OF_DIR_ENTRY	11	/* by byte */

sizeof (struct grub_f2fs_dir_entry)?

...
> +enum
> +  {
> +    FI_INLINE_XATTR = 9,
> +    FI_INLINE_DATA = 10,
> +    FI_INLINE_DENTRY = 11,
> +    FI_DATA_EXIST = 18,
> +  };
> +

This does not match subsequent usage; you use them with i_inline, not
i_flags.

...
> +
> +static inline int
> +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> +{
> +  return 1UL & (addr[nr / 32] >> (nr & 31));
> +}
> +

This is used only in grub_f2fs_check_dentries() with on-disk bitmap.
On-disk bitmap is little-endian; code is wrong on big-endian system.

Also dentry_bitmap is not multiple of 4 bytes as you replied earlier.
You should rather compute correct byte address instead and make all
parameters grub_uint8_t *. This will also avoid all those casts later.

That's really just

  grub_uint8_t *addr;

  byte = nr >> 3;
#ifdef WORDS_BIGENDIAN
  byte ^= 3;
#endif
  return addr[byte] & (1 << nr & 7);

...
> +
> +static inline int
> +__inode_inline_set (struct grub_f2fs_inode *inode, int flag)
> +{
> +  return inode->i_inline & flag;
> +}
> +

Function is completely redundant if you really want to check i_inline;
just use it directly. Then definition of flags is wrong.

If you mean inode->i_flags (as implied by passed flag values) this
should obviously be (1 << flag) as adjusted by system byte order.

Out of curiosity - it appears those flags are present in both i_inline
and i_flags; which one is authoritative?

> +static inline grub_uint32_t
> +__nat_bitmap_size (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +
> +  return grub_le_to_cpu32 (ckpt->nat_ver_bitmap_bytesize);
> +}

This function is not used anywhere.

...
> +
> +static inline grub_uint32_t
> +__get_node_id (struct grub_f2fs_node *rn, int off, int i)
> +{
> +  if (i)

Judging by usage something like "first" would probably be more
appropriate.

> +    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,

I suspect coverity will complain about overflow here; cast of blkaddr
to grub_disk_addr_t should silence it.

> +					0, F2FS_BLKSIZE, buf);
> +}
> +
...
> +
> +static grub_err_t

Not sure if it is needed here; see comment below at caller.

> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_uint64_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset >> GRUB_DISK_SECTOR_BITS, 0,

Make parameter grub_disk_addr_t and compute it in caller. It is
constant, there is no need to compute it at run time.

> +			sizeof (data->sblock), &data->sblock);
> +  if (err)
> +    return err;
> +
> +  if (grub_f2fs_sanity_check_sb (&data->sblock))
> +    err = 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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));

I understand that it /should/ be hardcoded to 4092, but then please
either check that crc_offset *is* 4092 before or use
grub_get_unaligned. Otherwise it crashes on archs that do not support
unaligned access.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));

Ditto.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints\n");

Trailing "\n" is not needed.

> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
...
> +
> +static struct grub_f2fs_data *
> +grub_f2fs_mount (grub_disk_t disk)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_err_t err;
> +
> +  data = grub_zalloc (sizeof (*data));

Is it needed to be zalloc? Structure is large and it runs every time
file is accessed. Most of it is immediately overwritten, may be
explicitly initialize what remains?

> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  err = grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET);
> +  if (err)
> +    {
> +      err = grub_f2fs_read_sb (data, F2FS_BLKSIZE + F2FS_SUPER_OFFSET);

As mentioned just compute disk address here, it is static.

> +      if (err)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem (no superblock)");
> +          goto fail;
> +	}
> +    }

You should check for err != GRUB_ERR_BAD_FS here, otherwise error from
grub_disk_read is lost. Alternatively just return 0/1 from read_sb, so
that

if (grub_f2fs_read_sb)
  if (grub_f2fs_read_sb)
    if (grub_errno == GRUB_ERR_NONE)
      grub_error (GRUB_ERR_BAD_FS, ...)
    goto fail

> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
...
> +
> +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 = &(node->inode.i);

Why extra parens?

> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (__inode_inline_set (&node->inode.i, FI_INLINE_DATA))
Just inode, you already have it.

> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> +        {
> +          grub_error (GRUB_ERR_OUT_OF_RANGE,
> +		  N_("attempt to read past the end of file"));

If filesize > MAX_INLINE_DATA at this point we really deal with
corrupted filesystem so this should be GRUB_ERR_BAD_FS.

> +          return -1;
> +        }
> +      if (pos + len > filesize)

len > filesize - pos is probably more coverity-friendly.

> +        len = 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_get_block,
> +				filesize,
> +				F2FS_BLK_SEC_BITS, 0);
> +}
> +
...
> +
> +static int
> +grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx)
> +{
> +  struct grub_fshelp_node *fdiro;
> +  int i;
> +
> +  for (i = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_zalloc (name_len + 1);
It is overwritten on next line, do you really need grub_zalloc only for
the trailing zero?

> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> +    }
> +    return 0;
> +}
> +
...
> +
> +static void
> +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> +{
> +  grub_uint16_t *pchTempPtr = in_buf;
> +  grub_uint8_t *pwTempPtr = out_buf;
> +
> +  while (*pchTempPtr != '\0')
> +  {
> +    *pwTempPtr = (grub_uint8_t) *pchTempPtr;

This is not byte order safe and it does not convert to ASCII as name
suggests. If you are going to convert to 8 bit encoding why not simply
use grub_utf16_to_utf8?

> +    pchTempPtr++;
> +    pwTempPtr++;
> +  }
> +  *pwTempPtr = '\0';
> +  return;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> +      if (*label)
> +        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
> +				data->sblock.volume_name);

See above.

> +    }
> +  else
> +    *label = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
...


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
@ 2015-05-02 17:15     ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-05-02 17:15 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

Sorry for delay.


В Fri, 3 Apr 2015 15:49:08 -0700
Jaegeuk Kim <jaegeuk@kernel.org> пишет:

> 
> This patch changes:
> 
>  * 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.
> 

Drop file list, it is available from git.

...
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..e6b8386
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> +
...
> +#define JENTRY_SIZE		13

sizeof (struct grub_f2fs_nat_jent), right?

...
> +
> +#define NAT_ENTRY_SIZE 		9

That's sizeof (struct grub_f2fs_nat_entry)?

...
> +#define ver_after (a, b)	((long long)((a) - (b)) > 0)

This macro is not used anywhere

> +#define F2FS_NAME_LEN		255
> +#define F2FS_SLOT_LEN		8
> +#define NR_DENTRY_IN_BLOCK	214
> +#define SIZE_OF_DIR_ENTRY	11	/* by byte */

sizeof (struct grub_f2fs_dir_entry)?

...
> +enum
> +  {
> +    FI_INLINE_XATTR = 9,
> +    FI_INLINE_DATA = 10,
> +    FI_INLINE_DENTRY = 11,
> +    FI_DATA_EXIST = 18,
> +  };
> +

This does not match subsequent usage; you use them with i_inline, not
i_flags.

...
> +
> +static inline int
> +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> +{
> +  return 1UL & (addr[nr / 32] >> (nr & 31));
> +}
> +

This is used only in grub_f2fs_check_dentries() with on-disk bitmap.
On-disk bitmap is little-endian; code is wrong on big-endian system.

Also dentry_bitmap is not multiple of 4 bytes as you replied earlier.
You should rather compute correct byte address instead and make all
parameters grub_uint8_t *. This will also avoid all those casts later.

That's really just

  grub_uint8_t *addr;

  byte = nr >> 3;
#ifdef WORDS_BIGENDIAN
  byte ^= 3;
#endif
  return addr[byte] & (1 << nr & 7);

...
> +
> +static inline int
> +__inode_inline_set (struct grub_f2fs_inode *inode, int flag)
> +{
> +  return inode->i_inline & flag;
> +}
> +

Function is completely redundant if you really want to check i_inline;
just use it directly. Then definition of flags is wrong.

If you mean inode->i_flags (as implied by passed flag values) this
should obviously be (1 << flag) as adjusted by system byte order.

Out of curiosity - it appears those flags are present in both i_inline
and i_flags; which one is authoritative?

> +static inline grub_uint32_t
> +__nat_bitmap_size (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +
> +  return grub_le_to_cpu32 (ckpt->nat_ver_bitmap_bytesize);
> +}

This function is not used anywhere.

...
> +
> +static inline grub_uint32_t
> +__get_node_id (struct grub_f2fs_node *rn, int off, int i)
> +{
> +  if (i)

Judging by usage something like "first" would probably be more
appropriate.

> +    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,

I suspect coverity will complain about overflow here; cast of blkaddr
to grub_disk_addr_t should silence it.

> +					0, F2FS_BLKSIZE, buf);
> +}
> +
...
> +
> +static grub_err_t

Not sure if it is needed here; see comment below at caller.

> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_uint64_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset >> GRUB_DISK_SECTOR_BITS, 0,

Make parameter grub_disk_addr_t and compute it in caller. It is
constant, there is no need to compute it at run time.

> +			sizeof (data->sblock), &data->sblock);
> +  if (err)
> +    return err;
> +
> +  if (grub_f2fs_sanity_check_sb (&data->sblock))
> +    err = 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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));

I understand that it /should/ be hardcoded to 4092, but then please
either check that crc_offset *is* 4092 before or use
grub_get_unaligned. Otherwise it crashes on archs that do not support
unaligned access.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset >= F2FS_BLKSIZE)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));

Ditto.

> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints\n");

Trailing "\n" is not needed.

> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
...
> +
> +static struct grub_f2fs_data *
> +grub_f2fs_mount (grub_disk_t disk)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_err_t err;
> +
> +  data = grub_zalloc (sizeof (*data));

Is it needed to be zalloc? Structure is large and it runs every time
file is accessed. Most of it is immediately overwritten, may be
explicitly initialize what remains?

> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  err = grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET);
> +  if (err)
> +    {
> +      err = grub_f2fs_read_sb (data, F2FS_BLKSIZE + F2FS_SUPER_OFFSET);

As mentioned just compute disk address here, it is static.

> +      if (err)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem (no superblock)");
> +          goto fail;
> +	}
> +    }

You should check for err != GRUB_ERR_BAD_FS here, otherwise error from
grub_disk_read is lost. Alternatively just return 0/1 from read_sb, so
that

if (grub_f2fs_read_sb)
  if (grub_f2fs_read_sb)
    if (grub_errno == GRUB_ERR_NONE)
      grub_error (GRUB_ERR_BAD_FS, ...)
    goto fail

> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +		grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
...
> +
> +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 = &(node->inode.i);

Why extra parens?

> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (__inode_inline_set (&node->inode.i, FI_INLINE_DATA))
Just inode, you already have it.

> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> +        {
> +          grub_error (GRUB_ERR_OUT_OF_RANGE,
> +		  N_("attempt to read past the end of file"));

If filesize > MAX_INLINE_DATA at this point we really deal with
corrupted filesystem so this should be GRUB_ERR_BAD_FS.

> +          return -1;
> +        }
> +      if (pos + len > filesize)

len > filesize - pos is probably more coverity-friendly.

> +        len = 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_get_block,
> +				filesize,
> +				F2FS_BLK_SEC_BITS, 0);
> +}
> +
...
> +
> +static int
> +grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx)
> +{
> +  struct grub_fshelp_node *fdiro;
> +  int i;
> +
> +  for (i = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_zalloc (name_len + 1);
It is overwritten on next line, do you really need grub_zalloc only for
the trailing zero?

> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
> +    }
> +    return 0;
> +}
> +
...
> +
> +static void
> +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> +{
> +  grub_uint16_t *pchTempPtr = in_buf;
> +  grub_uint8_t *pwTempPtr = out_buf;
> +
> +  while (*pchTempPtr != '\0')
> +  {
> +    *pwTempPtr = (grub_uint8_t) *pchTempPtr;

This is not byte order safe and it does not convert to ASCII as name
suggests. If you are going to convert to 8 bit encoding why not simply
use grub_utf16_to_utf8?

> +    pchTempPtr++;
> +    pwTempPtr++;
> +  }
> +  *pwTempPtr = '\0';
> +  return;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *label = grub_zalloc (sizeof (data->sblock.volume_name));
> +      if (*label)
> +        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
> +				data->sblock.volume_name);

See above.

> +    }
> +  else
> +    *label = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
...



^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
  2015-05-02 17:15     ` Andrei Borzenkov
@ 2015-05-03  6:28       ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-05-03  6:28 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Sat, 2 May 2015 20:15:45 +0300
Andrei Borzenkov <arvidjaar@gmail.com> пишет:

> > +
> > +static inline int
> > +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & 31));
> > +}
> > +
> 
> This is used only in grub_f2fs_check_dentries() with on-disk bitmap.
> On-disk bitmap is little-endian; code is wrong on big-endian system.
> 
> Also dentry_bitmap is not multiple of 4 bytes as you replied earlier.
> You should rather compute correct byte address instead and make all
> parameters grub_uint8_t *. This will also avoid all those casts later.
> 
> That's really just
> 
>   grub_uint8_t *addr;
> 
>   byte = nr >> 3;
> #ifdef WORDS_BIGENDIAN
>   byte ^= 3;
> #endif

Scratch it. We do know that byte order is LE so it is fixed. Sorry.

>   return addr[byte] & (1 << nr & 7);
> 

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
@ 2015-05-03  6:28       ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-05-03  6:28 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: grub-devel, linux-f2fs-devel

В Sat, 2 May 2015 20:15:45 +0300
Andrei Borzenkov <arvidjaar@gmail.com> пишет:

> > +
> > +static inline int
> > +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & 31));
> > +}
> > +
> 
> This is used only in grub_f2fs_check_dentries() with on-disk bitmap.
> On-disk bitmap is little-endian; code is wrong on big-endian system.
> 
> Also dentry_bitmap is not multiple of 4 bytes as you replied earlier.
> You should rather compute correct byte address instead and make all
> parameters grub_uint8_t *. This will also avoid all those casts later.
> 
> That's really just
> 
>   grub_uint8_t *addr;
> 
>   byte = nr >> 3;
> #ifdef WORDS_BIGENDIAN
>   byte ^= 3;
> #endif

Scratch it. We do know that byte order is LE so it is fixed. Sorry.

>   return addr[byte] & (1 << nr & 7);
> 


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
  2015-05-02 17:15     ` Andrei Borzenkov
  (?)
  (?)
@ 2015-05-07 14:51     ` Vladimir 'φ-coder/phcoder' Serbinenko
  2015-05-07 14:57       ` Andrei Borzenkov
  -1 siblings, 1 reply; 77+ messages in thread
From: Vladimir 'φ-coder/phcoder' Serbinenko @ 2015-05-07 14:51 UTC (permalink / raw)
  To: The development of GNU GRUB

[-- Attachment #1: Type: text/plain, Size: 239 bytes --]


>   grub_uint8_t *addr;
> 
>   byte = nr >> 3;
> #ifdef WORDS_BIGENDIAN
#ifdef GRUB_CPU_WORDS_BIGENDIAN actually
Also in general it's very good idea to check on big-endian machines.

We don't add __ prefix to function names.



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 213 bytes --]

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v2] F2FS support
  2015-05-07 14:51     ` Vladimir 'φ-coder/phcoder' Serbinenko
@ 2015-05-07 14:57       ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-05-07 14:57 UTC (permalink / raw)
  To: The development of GNU GRUB

On Thu, May 7, 2015 at 5:51 PM, Vladimir 'φ-coder/phcoder' Serbinenko
<phcoder@gmail.com> wrote:
>
>>   grub_uint8_t *addr;
>>
>>   byte = nr >> 3;
>> #ifdef WORDS_BIGENDIAN
> #ifdef GRUB_CPU_WORDS_BIGENDIAN actually

It is not needed at all. on-disk format is fixed and little endian.
Sorry for confusion.


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v3] F2FS support
  2015-04-03 22:49   ` Jaegeuk Kim
@ 2015-11-19 21:28     ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-11-19 21:28 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel

Hello,

Change log from v2:
 o Enhance the code quality suggested by Andrei

Sorry for the long delay.
Could you please check this patch?

Thank you so much,

--- >& ---
From 2c0e1d33533fc9fb2686f0e2c17efca11ce57e2f Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1291 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   11 +-
 7 files changed, 1335 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..df0add8
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1291 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 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_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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
+grub_generic_test_bit (int nr, const 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
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
+  grub_uint32_t start_addr = 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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return (mask & *p) != 0;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+/* TODO: mkfs.f2fs stores label in a wrong way. Should be fixed. */
+static void
+grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
+{
+  grub_uint16_t *pchTempPtr = in_buf;
+  grub_uint8_t *pwTempPtr = out_buf;
+
+  while (*pchTempPtr != '\0')
+    {
+      *pwTempPtr = (grub_uint8_t) *pchTempPtr;
+      pchTempPtr++;
+      pwTempPtr++;
+    }
+  *pwTempPtr = '\0';
+  return;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *label = grub_zalloc (sizeof (data->sblock.volume_name));
+      if (*label)
+        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
+			data->sblock.volume_name);
+    }
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..7245cb8 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,11 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+	    # TODO: seems that mkfs.f2fs doesn't handle encoding stuffs correctly
+		x"f2fs")
+		  FSLABEL="grub_test:no utf16";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +479,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +758,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.4.9 (Apple Git-60)


------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH v3] F2FS support
@ 2015-11-19 21:28     ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-11-19 21:28 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel

Hello,

Change log from v2:
 o Enhance the code quality suggested by Andrei

Sorry for the long delay.
Could you please check this patch?

Thank you so much,

--- >& ---
From 2c0e1d33533fc9fb2686f0e2c17efca11ce57e2f Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1291 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   11 +-
 7 files changed, 1335 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..df0add8
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1291 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 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_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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
+grub_generic_test_bit (int nr, const 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
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
+  grub_uint32_t start_addr = 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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return (mask & *p) != 0;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_generic_test_bit (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = (grub_uint32_t *) de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+/* TODO: mkfs.f2fs stores label in a wrong way. Should be fixed. */
+static void
+grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
+{
+  grub_uint16_t *pchTempPtr = in_buf;
+  grub_uint8_t *pwTempPtr = out_buf;
+
+  while (*pchTempPtr != '\0')
+    {
+      *pwTempPtr = (grub_uint8_t) *pchTempPtr;
+      pchTempPtr++;
+      pwTempPtr++;
+    }
+  *pwTempPtr = '\0';
+  return;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *label = grub_zalloc (sizeof (data->sblock.volume_name));
+      if (*label)
+        grub_f2fs_unicode_to_ascii ((grub_uint8_t *) (*label),
+			data->sblock.volume_name);
+    }
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..7245cb8 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,11 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+	    # TODO: seems that mkfs.f2fs doesn't handle encoding stuffs correctly
+		x"f2fs")
+		  FSLABEL="grub_test:no utf16";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +479,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +758,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.4.9 (Apple Git-60)



^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH v3] F2FS support
  2015-11-19 21:28     ` Jaegeuk Kim
@ 2015-12-14  8:28       ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-12-14  8:28 UTC (permalink / raw)
  To: The development of GNU GRUB, linux-f2fs-devel

20.11.2015 00:28, Jaegeuk Kim пишет:
> Hello,
> 
> Change log from v2:
>  o Enhance the code quality suggested by Andrei
> 
> Sorry for the long delay.
> Could you please check this patch?
> 
> Thank you so much,
> 

Thank you for continuing to work on it!

...

> +
> +static inline int
> +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> +{
> +  return 1UL & (addr[nr / 32] >> (nr & 31));
> +}
> +

As already discussed this code is wrong on big-endian platform. On-disk
bitmap is little-endian and kernel explicitly uses test_bit_le() here.

> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt_version & 1))

This can use grub_cpu_to_le64_compile_time (1)

...

> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return (mask & *p) != 0;

This is really just "return mask & *p".

> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)

This sounds like it should actually be F2FS_BLK_BITS; at least assuming
that F2FS_MAX_LOG_SECTOR_SIZE may differ from F2FS_BLK_BITS.

...

> +
> +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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");


Sorry for confusion, my fault. pos > filesize was OK, but filesize >
MAX_INLINE_DATA not.

...

> +
> +/* TODO: mkfs.f2fs stores label in a wrong way. Should be fixed. */
> +static void
> +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> +{
> +  grub_uint16_t *pchTempPtr = in_buf;
> +  grub_uint8_t *pwTempPtr = out_buf;
> +
> +  while (*pchTempPtr != '\0')
> +    {
> +      *pwTempPtr = (grub_uint8_t) *pchTempPtr;
> +      pchTempPtr++;
> +      pwTempPtr++;
> +    }
> +  *pwTempPtr = '\0';
> +  return;
> +}

Sorry, I do not see how it can work on both big and little endian
platforms. What byte order is used for on-disk label? Why cannot you use
grub_utf16_to_utf8 as I asked last time?

Also please add bundary check, do not rely on correct content.



_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v3] F2FS support
@ 2015-12-14  8:28       ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-12-14  8:28 UTC (permalink / raw)
  To: The development of GNU GRUB, linux-f2fs-devel

20.11.2015 00:28, Jaegeuk Kim пишет:
> Hello,
> 
> Change log from v2:
>  o Enhance the code quality suggested by Andrei
> 
> Sorry for the long delay.
> Could you please check this patch?
> 
> Thank you so much,
> 

Thank you for continuing to work on it!

...

> +
> +static inline int
> +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> +{
> +  return 1UL & (addr[nr / 32] >> (nr & 31));
> +}
> +

As already discussed this code is wrong on big-endian platform. On-disk
bitmap is little-endian and kernel explicitly uses test_bit_le() here.

> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt_version & 1))

This can use grub_cpu_to_le64_compile_time (1)

...

> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return (mask & *p) != 0;

This is really just "return mask & *p".

> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)

This sounds like it should actually be F2FS_BLK_BITS; at least assuming
that F2FS_MAX_LOG_SECTOR_SIZE may differ from F2FS_BLK_BITS.

...

> +
> +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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");


Sorry for confusion, my fault. pos > filesize was OK, but filesize >
MAX_INLINE_DATA not.

...

> +
> +/* TODO: mkfs.f2fs stores label in a wrong way. Should be fixed. */
> +static void
> +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> +{
> +  grub_uint16_t *pchTempPtr = in_buf;
> +  grub_uint8_t *pwTempPtr = out_buf;
> +
> +  while (*pchTempPtr != '\0')
> +    {
> +      *pwTempPtr = (grub_uint8_t) *pchTempPtr;
> +      pchTempPtr++;
> +      pwTempPtr++;
> +    }
> +  *pwTempPtr = '\0';
> +  return;
> +}

Sorry, I do not see how it can work on both big and little endian
platforms. What byte order is used for on-disk label? Why cannot you use
grub_utf16_to_utf8 as I asked last time?

Also please add bundary check, do not rely on correct content.




^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v3] F2FS support
  2015-12-14  8:28       ` Andrei Borzenkov
@ 2015-12-15  0:30         ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15  0:30 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Thank you for the review.

On Mon, Dec 14, 2015 at 11:28:26AM +0300, Andrei Borzenkov wrote:
> 20.11.2015 00:28, Jaegeuk Kim пишет:
> > Hello,
> > 
> > Change log from v2:
> >  o Enhance the code quality suggested by Andrei
> > 
> > Sorry for the long delay.
> > Could you please check this patch?
> > 
> > Thank you so much,
> > 
> 
> Thank you for continuing to work on it!
> 
> ...
> 
> > +
> > +static inline int
> > +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & 31));
> > +}
> > +
> 
> As already discussed this code is wrong on big-endian platform. On-disk
> bitmap is little-endian and kernel explicitly uses test_bit_le() here.

Got it.

> 
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&inode->i_addr[1];
> > +}
> > +
> > +static inline grub_uint64_t
> > +grub_f2fs_file_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 = &data->ckpt;
> > +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> > +  grub_uint32_t start_addr = data->cp_blkaddr;
> > +
> > +  if (!(ckpt_version & 1))
> 
> This can use grub_cpu_to_le64_compile_time (1)

Done.

> 
> ...
> 
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +
> > +  p += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return (mask & *p) != 0;
> 
> This is really just "return mask & *p".

Okay.

> 
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > +
> > +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > +    return -1;
> > +
> > +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> > +    return -1;
> > +
> > +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> > +
> > +  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
> > +    return -1;
> > +
> > +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > +    return -1;
> > +
> > +  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
> 
> This sounds like it should actually be F2FS_BLK_BITS; at least assuming
> that F2FS_MAX_LOG_SECTOR_SIZE may differ from F2FS_BLK_BITS.
> 

Yup. In order to avoid confusion, I removed F2FS_MAX_LOG_SECTOR_SIZE.

> ...
> 
> > +
> > +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 = &node->inode.i;
> > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > +  char *inline_addr = __inline_addr (inode);
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > +    {
> > +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> > +        {
> > +          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> 
> 
> Sorry for confusion, my fault. pos > filesize was OK, but filesize >
> MAX_INLINE_DATA not.

If filesize is over MAX_INLINE_DATA, this file is corrupted as well.
It will cause boundary violation during memcpy below.

> 
> ...
> 
> > +
> > +/* TODO: mkfs.f2fs stores label in a wrong way. Should be fixed. */
> > +static void
> > +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> > +{
> > +  grub_uint16_t *pchTempPtr = in_buf;
> > +  grub_uint8_t *pwTempPtr = out_buf;
> > +
> > +  while (*pchTempPtr != '\0')
> > +    {
> > +      *pwTempPtr = (grub_uint8_t) *pchTempPtr;
> > +      pchTempPtr++;
> > +      pwTempPtr++;
> > +    }
> > +  *pwTempPtr = '\0';
> > +  return;
> > +}
> 
> Sorry, I do not see how it can work on both big and little endian
> platforms. What byte order is used for on-disk label? Why cannot you use
> grub_utf16_to_utf8 as I asked last time?
> 
> Also please add bundary check, do not rely on correct content.

Got it. I refactored this here and in f2fs-tools as well.
Could you check v4?

Thanks,

> 
> 
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v3] F2FS support
@ 2015-12-15  0:30         ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15  0:30 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Thank you for the review.

On Mon, Dec 14, 2015 at 11:28:26AM +0300, Andrei Borzenkov wrote:
> 20.11.2015 00:28, Jaegeuk Kim пишет:
> > Hello,
> > 
> > Change log from v2:
> >  o Enhance the code quality suggested by Andrei
> > 
> > Sorry for the long delay.
> > Could you please check this patch?
> > 
> > Thank you so much,
> > 
> 
> Thank you for continuing to work on it!
> 
> ...
> 
> > +
> > +static inline int
> > +grub_generic_test_bit (int nr, const grub_uint32_t *addr)
> > +{
> > +  return 1UL & (addr[nr / 32] >> (nr & 31));
> > +}
> > +
> 
> As already discussed this code is wrong on big-endian platform. On-disk
> bitmap is little-endian and kernel explicitly uses test_bit_le() here.

Got it.

> 
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&inode->i_addr[1];
> > +}
> > +
> > +static inline grub_uint64_t
> > +grub_f2fs_file_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 = &data->ckpt;
> > +  grub_uint64_t ckpt_version = grub_le_to_cpu64 (ckpt->checkpoint_ver);
> > +  grub_uint32_t start_addr = data->cp_blkaddr;
> > +
> > +  if (!(ckpt_version & 1))
> 
> This can use grub_cpu_to_le64_compile_time (1)

Done.

> 
> ...
> 
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +
> > +  p += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return (mask & *p) != 0;
> 
> This is really just "return mask & *p".

Okay.

> 
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > +
> > +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > +    return -1;
> > +
> > +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> > +    return -1;
> > +
> > +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> > +
> > +  if (log_sectorsize > F2FS_MAX_LOG_SECTOR_SIZE)
> > +    return -1;
> > +
> > +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > +    return -1;
> > +
> > +  if (log_sectors_per_block + log_sectorsize != F2FS_MAX_LOG_SECTOR_SIZE)
> 
> This sounds like it should actually be F2FS_BLK_BITS; at least assuming
> that F2FS_MAX_LOG_SECTOR_SIZE may differ from F2FS_BLK_BITS.
> 

Yup. In order to avoid confusion, I removed F2FS_MAX_LOG_SECTOR_SIZE.

> ...
> 
> > +
> > +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 = &node->inode.i;
> > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > +  char *inline_addr = __inline_addr (inode);
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > +    {
> > +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> > +        {
> > +          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> 
> 
> Sorry for confusion, my fault. pos > filesize was OK, but filesize >
> MAX_INLINE_DATA not.

If filesize is over MAX_INLINE_DATA, this file is corrupted as well.
It will cause boundary violation during memcpy below.

> 
> ...
> 
> > +
> > +/* TODO: mkfs.f2fs stores label in a wrong way. Should be fixed. */
> > +static void
> > +grub_f2fs_unicode_to_ascii (grub_uint8_t *out_buf, grub_uint16_t *in_buf)
> > +{
> > +  grub_uint16_t *pchTempPtr = in_buf;
> > +  grub_uint8_t *pwTempPtr = out_buf;
> > +
> > +  while (*pchTempPtr != '\0')
> > +    {
> > +      *pwTempPtr = (grub_uint8_t) *pchTempPtr;
> > +      pchTempPtr++;
> > +      pwTempPtr++;
> > +    }
> > +  *pwTempPtr = '\0';
> > +  return;
> > +}
> 
> Sorry, I do not see how it can work on both big and little endian
> platforms. What byte order is used for on-disk label? Why cannot you use
> grub_utf16_to_utf8 as I asked last time?
> 
> Also please add bundary check, do not rely on correct content.

Got it. I refactored this here and in f2fs-tools as well.
Could you check v4?

Thanks,

> 
> 
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v4] F2FS support
  2015-12-14  8:28       ` Andrei Borzenkov
@ 2015-12-15  0:34         ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15  0:34 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Change log from v3:
 o add grub_test_bit_le()
 o fix label handling with utf16
 o change some minor concerns

 -- >8 --
From 4ab813c29851113da04d9ca84670219434ee570e Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1291 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1334 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..6e47227
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1291 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  const grub_int32_t *p = (const grub_int32_t *)addr;
+
+  nr = nr ^ 0;
+
+  return p[nr >> 5] & (1 << (nr & 31));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.5.4 (Apple Git-61)

------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v4] F2FS support
@ 2015-12-15  0:34         ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15  0:34 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Change log from v3:
 o add grub_test_bit_le()
 o fix label handling with utf16
 o change some minor concerns

 -- >8 --
From 4ab813c29851113da04d9ca84670219434ee570e Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1291 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1334 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..6e47227
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1291 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  const grub_int32_t *p = (const grub_int32_t *)addr;
+
+  nr = nr ^ 0;
+
+  return p[nr >> 5] & (1 << (nr & 31));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.5.4 (Apple Git-61)


^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH v4] F2FS support
  2015-12-15  0:34         ` [f2fs-dev] " Jaegeuk Kim
@ 2015-12-15  8:34           ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-12-15  8:34 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

15.12.2015 03:34, Jaegeuk Kim пишет:
> Change log from v3:
>  o add grub_test_bit_le()
...

> +
> +static inline int
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  const grub_int32_t *p = (const grub_int32_t *)addr;
> +
> +  nr = nr ^ 0;

It does nothing.

> +
> +  return p[nr >> 5] & (1 << (nr & 31));
> +}

Well, you still miss the point - if you are working with integers you
must shift differently depending on whether we are running big or little
endian.

But as I mentioned before, we know that bitmap is little endian so we
can work with bytes and be independent of byte order. Could you test if
this works for you:

static inline int
grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
{
  return addr[nr >> 3] & (1 << (nr & 7));
}



------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v4] F2FS support
@ 2015-12-15  8:34           ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2015-12-15  8:34 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

15.12.2015 03:34, Jaegeuk Kim пишет:
> Change log from v3:
>  o add grub_test_bit_le()
...

> +
> +static inline int
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  const grub_int32_t *p = (const grub_int32_t *)addr;
> +
> +  nr = nr ^ 0;

It does nothing.

> +
> +  return p[nr >> 5] & (1 << (nr & 31));
> +}

Well, you still miss the point - if you are working with integers you
must shift differently depending on whether we are running big or little
endian.

But as I mentioned before, we know that bitmap is little endian so we
can work with bytes and be independent of byte order. Could you test if
this works for you:

static inline int
grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
{
  return addr[nr >> 3] & (1 << (nr & 7));
}




^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v4] F2FS support
  2015-12-15  8:34           ` [f2fs-dev] " Andrei Borzenkov
@ 2015-12-15 18:08             ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15 18:08 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

On Tue, Dec 15, 2015 at 11:34:24AM +0300, Andrei Borzenkov wrote:
> 15.12.2015 03:34, Jaegeuk Kim пишет:
> > Change log from v3:
> >  o add grub_test_bit_le()
> ...
> 
> > +
> > +static inline int
> > +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > +{
> > +  const grub_int32_t *p = (const grub_int32_t *)addr;
> > +
> > +  nr = nr ^ 0;
> 
> It does nothing.

Ah, right.

> 
> > +
> > +  return p[nr >> 5] & (1 << (nr & 31));
> > +}
> 
> Well, you still miss the point - if you are working with integers you
> must shift differently depending on whether we are running big or little
> endian.

I just thought that it would be okay to bypass 32bits and then check its target
location, since we know it is LE.
Indeed, I was wrong that your concern was this line will be converted to BE when
CPU loads this 32-bit value.

> But as I mentioned before, we know that bitmap is little endian so we
> can work with bytes and be independent of byte order. Could you test if
> this works for you:
> 
> static inline int
> grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> {
>   return addr[nr >> 3] & (1 << (nr & 7));
> }

Actually, I tested this before, but I got somehow failure, so tried this one.
Once I've checked bitmap contents in more details, I realized that this should
be a correct way; I confirmed that this has no problem.

I'll send another patch.
Thank you so much,


------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v4] F2FS support
@ 2015-12-15 18:08             ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15 18:08 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

On Tue, Dec 15, 2015 at 11:34:24AM +0300, Andrei Borzenkov wrote:
> 15.12.2015 03:34, Jaegeuk Kim пишет:
> > Change log from v3:
> >  o add grub_test_bit_le()
> ...
> 
> > +
> > +static inline int
> > +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > +{
> > +  const grub_int32_t *p = (const grub_int32_t *)addr;
> > +
> > +  nr = nr ^ 0;
> 
> It does nothing.

Ah, right.

> 
> > +
> > +  return p[nr >> 5] & (1 << (nr & 31));
> > +}
> 
> Well, you still miss the point - if you are working with integers you
> must shift differently depending on whether we are running big or little
> endian.

I just thought that it would be okay to bypass 32bits and then check its target
location, since we know it is LE.
Indeed, I was wrong that your concern was this line will be converted to BE when
CPU loads this 32-bit value.

> But as I mentioned before, we know that bitmap is little endian so we
> can work with bytes and be independent of byte order. Could you test if
> this works for you:
> 
> static inline int
> grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> {
>   return addr[nr >> 3] & (1 << (nr & 7));
> }

Actually, I tested this before, but I got somehow failure, so tried this one.
Once I've checked bitmap contents in more details, I realized that this should
be a correct way; I confirmed that this has no problem.

I'll send another patch.
Thank you so much,



^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v5] F2FS support
  2015-12-15  0:34         ` [f2fs-dev] " Jaegeuk Kim
@ 2015-12-15 18:14           ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15 18:14 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

 Change log from v4:
  o fix grub_test_bit_le()

-- >8 --
From c507937c6e1f74ea0f5b75bab4bbb983334d9581 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1287 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1330 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..be302e3
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1287 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.5.4 (Apple Git-61)


------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v5] F2FS support
@ 2015-12-15 18:14           ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2015-12-15 18:14 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

 Change log from v4:
  o fix grub_test_bit_le()

-- >8 --
From c507937c6e1f74ea0f5b75bab4bbb983334d9581 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1287 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1330 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..be302e3
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1287 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+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 = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block + crc_offset));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.5.4 (Apple Git-61)



^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v5] F2FS support
  2015-12-15 18:14           ` [f2fs-dev] " Jaegeuk Kim
@ 2016-01-07 19:37             ` Michael Zimmermann
  -1 siblings, 0 replies; 77+ messages in thread
From: Michael Zimmermann @ 2016-01-07 19:37 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Andrei Borzenkov, linux-f2fs-devel


[-- Attachment #1.1: Type: text/plain, Size: 48378 bytes --]

I've tried to compile this for ARM(using gcc 5.2.0) and there are a few
warnings:
In file included from grub/include/grub/file.h:22:0,
                 from grub/grub-core/fs/f2fs.c:22:
grub/grub-core/fs/f2fs.c: In function 'validate_checkpoint':
grub/grub-core/fs/f2fs.c:509:28: error: cast increases required alignment
of target type [-Werror=cast-align]
   crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
crc_offset));
                            ^
grub/include/grub/types.h:239:48: note: in definition of macro
'grub_le_to_cpu32'
 # define grub_le_to_cpu32(x) ((grub_uint32_t) (x))
                                                ^
grub/grub-core/fs/f2fs.c:531:28: error: cast increases required alignment
of target type [-Werror=cast-align]
   crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
crc_offset));
                            ^
grub/include/grub/types.h:239:48: note: in definition of macro
'grub_le_to_cpu32'
 # define grub_le_to_cpu32(x) ((grub_uint32_t) (x))

On Tue, Dec 15, 2015 at 7:14 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:

>  Change log from v4:
>   o fix grub_test_bit_le()
>
> -- >8 --
> From c507937c6e1f74ea0f5b75bab4bbb983334d9581 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
>
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was
> merged
> into Linux kernel v3.8 in 2013.
>
> The motive for F2FS was to build a file system that from the start, takes
> into
> account the characteristics of NAND flash memory-based storage devices
> (such as
> solid-state disks, eMMC, and SD cards).
>
> F2FS was designed on a basis of a log-structured file system approach,
> which
> remedies some known issues of the older log structured file systems, such
> as
> the snowball effect of wandering trees and high cleaning overhead. In
> addition,
> since a NAND-based storage device shows different characteristics
> according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
>
> The source codes for F2FS are available from:
>
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
>
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1287
> ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   10 +-
>  7 files changed, 1330 insertions(+), 4 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
>
> diff --git a/Makefile.util.def b/Makefile.util.def
> index db7e8ec..84627bb 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -99,6 +99,7 @@ library = {
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
>    common = grub-core/fs/exfat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
>    common = grub-core/fs/hfsplus.c;
> @@ -766,6 +767,12 @@ script = {
>
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = tests/nilfs2_test.in;
>  };
> diff --git a/docs/grub.texi b/docs/grub.texi
> index 1df3db2..e5a80f3 100644
> --- a/docs/grub.texi
> +++ b/docs/grub.texi
> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem
> 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},
> @dfn{HFS},
> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> +@dfn{f2fs}, @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},
> @@ -5347,7 +5348,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,
> minix,
>  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 charset
>  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 d9fa0e3..b585ade 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1278,6 +1278,11 @@ module = {
>  };
>
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = fshelp;
>    common = fs/fshelp.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..be302e3
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1287 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +/* F2FS Magic Number */
> +#define F2FS_SUPER_MAGIC       0xF2F52010
> +#define CHECKSUM_OFFSET                4092
> +
> +/* byte-size offset */
> +#define F2FS_SUPER_OFFSET      ((grub_disk_addr_t)1024)
> +#define F2FS_SUPER_OFFSET0     (F2FS_SUPER_OFFSET >>
> GRUB_DISK_SECTOR_BITS)
> +#define F2FS_SUPER_OFFSET1     ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> +                                               GRUB_DISK_SECTOR_BITS)
> +
> +/* 9 bits for 512 bytes */
> +#define F2FS_MIN_LOG_SECTOR_SIZE       9
> +
> +/* support only 4KB block */
> +#define F2FS_BLK_BITS  12
> +#define F2FS_BLKSIZE   (1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS      (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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    (sizeof(struct grub_f2fs_nat_jent))
> +#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 (sizeof(struct grub_f2fs_nat_entry))
> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> +
> +#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
> +
> +#define CKPT_FLAG_SET(ckpt, f) \
> +               (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +#define F2FS_INLINE_XATTR      0x01    /* file inline xattr flag */
> +#define F2FS_INLINE_DATA       0x02    /* file inline data flag */
> +#define F2FS_INLINE_DENTRY     0x04    /* file inline dentry flag */
> +#define F2FS_DATA_EXIST                0x08    /* file inline data exist
> flag */
> +#define F2FS_INLINE_DOTS       0x10    /* file having implicit dot
> dentries */
> +
> +enum FILE_TYPE
> +{
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 7,
> +};
> +
> +#define MAX_VOLUME_NAME                512
> +
> +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[MAX_VOLUME_NAME];
> +  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;
> +    char buf[F2FS_BLKSIZE - 40];       /* Should occupy F2FS_BLKSIZE
> totally */
> +  };
> +  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_uint8_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
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  return addr[nr >> 3] & (1 << (nr & 7));
> +}
> +
> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +       grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +       - (base + 1) + type;
> +}
> +
> +static inline void *
> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 inode_block)
> +{
> +  if (inode_block)
> +    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,
> +               ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> +               0, F2FS_BLKSIZE, buf);
> +}
> +
> +/*
> + * CRC32
> +*/
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return mask & *p;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_BLK_BITS)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> &data->sblock);
> +  if (err)
> +    return -1;
> +
> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> +}
> +
> +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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
> crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
> crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
> +static grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +      offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +               offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_malloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> +    {
> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> +        {
> +          if (grub_errno == GRUB_ERR_NONE)
> +            grub_error (GRUB_ERR_BAD_FS,
> +                       "not a F2FS filesystem (no superblock)");
> +          goto fail;
> +        }
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +       grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need
> fsck");
> +          return -1;
> +        }
> +      if (len > filesize - pos)
> +        len = 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +        return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_malloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = 0;
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +       return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> filetype,
> +             grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> + 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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  file->size = grub_f2fs_file_size (&(data->inode->i));
> +  file->data = data;
> +  file->offset = 0;
> +
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_uint8_t *
> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> +{
> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> +  grub_uint8_t *out_buf;
> +  int len = 0;
> +
> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> +  if (!out_buf)
> +    return NULL;
> +
> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> +    len++;
> +  }
> +
> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> +  return out_buf;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +       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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = mod;
> +}
> +
> +GRUB_MOD_FINI (f2fs)
> +{
> +  grub_fs_unregister (&grub_f2fs_fs);
> +}
> diff --git a/po/exclude.pot b/po/exclude.pot
> index 0a9b215..816089c 100644
> --- a/po/exclude.pot
> +++ b/po/exclude.pot
> @@ -1214,6 +1214,7 @@ msgstr ""
>
>  #: 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..1ea77c8
> --- /dev/null
> +++ b/tests/f2fs_test.in
> @@ -0,0 +1,19 @@
> +#!/bin/sh
> +
> +set -e
> +
> +if [ "x$EUID" = "x" ] ; then
> + EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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 424de22..e3e4109 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -142,7 +142,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>         xsquash*)
>             MINBLKSIZE=4096
>             MAXBLKSIZE=1048576;;
> -       xxfs)
> +       xxfs|xf2fs)
>             MINBLKSIZE=$SECSIZE
>                 # OS Limitation: GNU/Linux doesn't accept > 4096
>             MAXBLKSIZE=4096;;
> @@ -265,6 +265,10 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>                 x"btrfs"*)
>                     FSLABEL="grub_;/testé莭莽😁киритi
> urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>
> +           # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +               x"f2fs")
> +
>  FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> +
>             # FS LIMITATION: exfat is at most 15 UTF-16 chars
>                 x"exfat")
>                     FSLABEL="géт ;/莭莽😁кир";;
> @@ -474,7 +478,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>             # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> Check it.
>         # FS LIMITATION: as far as I know those FS don't store their last
> modification date.
>                 x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"*
> | x"reiserfs_old" | x"reiserfs" \
> -                   | 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=y;;
> @@ -753,6 +757,8 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>                     MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>                     MOUNTFS=ext2
>                     "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)
> --
> 2.5.4 (Apple Git-61)
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #1.2: Type: text/html, Size: 57084 bytes --]

[-- Attachment #2: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v5] F2FS support
@ 2016-01-07 19:37             ` Michael Zimmermann
  0 siblings, 0 replies; 77+ messages in thread
From: Michael Zimmermann @ 2016-01-07 19:37 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Andrei Borzenkov, linux-f2fs-devel

[-- Attachment #1: Type: text/plain, Size: 48378 bytes --]

I've tried to compile this for ARM(using gcc 5.2.0) and there are a few
warnings:
In file included from grub/include/grub/file.h:22:0,
                 from grub/grub-core/fs/f2fs.c:22:
grub/grub-core/fs/f2fs.c: In function 'validate_checkpoint':
grub/grub-core/fs/f2fs.c:509:28: error: cast increases required alignment
of target type [-Werror=cast-align]
   crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
crc_offset));
                            ^
grub/include/grub/types.h:239:48: note: in definition of macro
'grub_le_to_cpu32'
 # define grub_le_to_cpu32(x) ((grub_uint32_t) (x))
                                                ^
grub/grub-core/fs/f2fs.c:531:28: error: cast increases required alignment
of target type [-Werror=cast-align]
   crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
crc_offset));
                            ^
grub/include/grub/types.h:239:48: note: in definition of macro
'grub_le_to_cpu32'
 # define grub_le_to_cpu32(x) ((grub_uint32_t) (x))

On Tue, Dec 15, 2015 at 7:14 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:

>  Change log from v4:
>   o fix grub_test_bit_le()
>
> -- >8 --
> From c507937c6e1f74ea0f5b75bab4bbb983334d9581 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
>
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was
> merged
> into Linux kernel v3.8 in 2013.
>
> The motive for F2FS was to build a file system that from the start, takes
> into
> account the characteristics of NAND flash memory-based storage devices
> (such as
> solid-state disks, eMMC, and SD cards).
>
> F2FS was designed on a basis of a log-structured file system approach,
> which
> remedies some known issues of the older log structured file systems, such
> as
> the snowball effect of wandering trees and high cleaning overhead. In
> addition,
> since a NAND-based storage device shows different characteristics
> according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
>
> The source codes for F2FS are available from:
>
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
>
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1287
> ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   10 +-
>  7 files changed, 1330 insertions(+), 4 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
>
> diff --git a/Makefile.util.def b/Makefile.util.def
> index db7e8ec..84627bb 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -99,6 +99,7 @@ library = {
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
>    common = grub-core/fs/exfat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
>    common = grub-core/fs/hfsplus.c;
> @@ -766,6 +767,12 @@ script = {
>
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = tests/nilfs2_test.in;
>  };
> diff --git a/docs/grub.texi b/docs/grub.texi
> index 1df3db2..e5a80f3 100644
> --- a/docs/grub.texi
> +++ b/docs/grub.texi
> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem
> 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},
> @dfn{HFS},
> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> +@dfn{f2fs}, @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},
> @@ -5347,7 +5348,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,
> minix,
>  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 charset
>  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 d9fa0e3..b585ade 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1278,6 +1278,11 @@ module = {
>  };
>
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = fshelp;
>    common = fs/fshelp.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..be302e3
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1287 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +/* F2FS Magic Number */
> +#define F2FS_SUPER_MAGIC       0xF2F52010
> +#define CHECKSUM_OFFSET                4092
> +
> +/* byte-size offset */
> +#define F2FS_SUPER_OFFSET      ((grub_disk_addr_t)1024)
> +#define F2FS_SUPER_OFFSET0     (F2FS_SUPER_OFFSET >>
> GRUB_DISK_SECTOR_BITS)
> +#define F2FS_SUPER_OFFSET1     ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> +                                               GRUB_DISK_SECTOR_BITS)
> +
> +/* 9 bits for 512 bytes */
> +#define F2FS_MIN_LOG_SECTOR_SIZE       9
> +
> +/* support only 4KB block */
> +#define F2FS_BLK_BITS  12
> +#define F2FS_BLKSIZE   (1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS      (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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    (sizeof(struct grub_f2fs_nat_jent))
> +#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 (sizeof(struct grub_f2fs_nat_entry))
> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> +
> +#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
> +
> +#define CKPT_FLAG_SET(ckpt, f) \
> +               (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +#define F2FS_INLINE_XATTR      0x01    /* file inline xattr flag */
> +#define F2FS_INLINE_DATA       0x02    /* file inline data flag */
> +#define F2FS_INLINE_DENTRY     0x04    /* file inline dentry flag */
> +#define F2FS_DATA_EXIST                0x08    /* file inline data exist
> flag */
> +#define F2FS_INLINE_DOTS       0x10    /* file having implicit dot
> dentries */
> +
> +enum FILE_TYPE
> +{
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 7,
> +};
> +
> +#define MAX_VOLUME_NAME                512
> +
> +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[MAX_VOLUME_NAME];
> +  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;
> +    char buf[F2FS_BLKSIZE - 40];       /* Should occupy F2FS_BLKSIZE
> totally */
> +  };
> +  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_uint8_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
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  return addr[nr >> 3] & (1 << (nr & 7));
> +}
> +
> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +       grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +       - (base + 1) + type;
> +}
> +
> +static inline void *
> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 inode_block)
> +{
> +  if (inode_block)
> +    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,
> +               ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> +               0, F2FS_BLKSIZE, buf);
> +}
> +
> +/*
> + * CRC32
> +*/
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return mask & *p;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_BLK_BITS)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> &data->sblock);
> +  if (err)
> +    return -1;
> +
> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> +}
> +
> +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 = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
> crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(grub_uint32_t *)((char *)cp_block +
> crc_offset));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
> +static grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +      offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +               offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_malloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> +    {
> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> +        {
> +          if (grub_errno == GRUB_ERR_NONE)
> +            grub_error (GRUB_ERR_BAD_FS,
> +                       "not a F2FS filesystem (no superblock)");
> +          goto fail;
> +        }
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +       grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> +        {
> +          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need
> fsck");
> +          return -1;
> +        }
> +      if (len > filesize - pos)
> +        len = 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +        return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_malloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = 0;
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +       return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> filetype,
> +             grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> + 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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  file->size = grub_f2fs_file_size (&(data->inode->i));
> +  file->data = data;
> +  file->offset = 0;
> +
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_uint8_t *
> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> +{
> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> +  grub_uint8_t *out_buf;
> +  int len = 0;
> +
> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> +  if (!out_buf)
> +    return NULL;
> +
> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> +    len++;
> +  }
> +
> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> +  return out_buf;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +       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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = mod;
> +}
> +
> +GRUB_MOD_FINI (f2fs)
> +{
> +  grub_fs_unregister (&grub_f2fs_fs);
> +}
> diff --git a/po/exclude.pot b/po/exclude.pot
> index 0a9b215..816089c 100644
> --- a/po/exclude.pot
> +++ b/po/exclude.pot
> @@ -1214,6 +1214,7 @@ msgstr ""
>
>  #: 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..1ea77c8
> --- /dev/null
> +++ b/tests/f2fs_test.in
> @@ -0,0 +1,19 @@
> +#!/bin/sh
> +
> +set -e
> +
> +if [ "x$EUID" = "x" ] ; then
> + EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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 424de22..e3e4109 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -142,7 +142,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>         xsquash*)
>             MINBLKSIZE=4096
>             MAXBLKSIZE=1048576;;
> -       xxfs)
> +       xxfs|xf2fs)
>             MINBLKSIZE=$SECSIZE
>                 # OS Limitation: GNU/Linux doesn't accept > 4096
>             MAXBLKSIZE=4096;;
> @@ -265,6 +265,10 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>                 x"btrfs"*)
>                     FSLABEL="grub_;/testé莭莽😁киритi
> urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>
> +           # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +               x"f2fs")
> +
>  FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> +
>             # FS LIMITATION: exfat is at most 15 UTF-16 chars
>                 x"exfat")
>                     FSLABEL="géт ;/莭莽😁кир";;
> @@ -474,7 +478,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>             # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> Check it.
>         # FS LIMITATION: as far as I know those FS don't store their last
> modification date.
>                 x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"*
> | x"reiserfs_old" | x"reiserfs" \
> -                   | 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=y;;
> @@ -753,6 +757,8 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>                     MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>                     MOUNTFS=ext2
>                     "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)
> --
> 2.5.4 (Apple Git-61)
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 57083 bytes --]

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v6] F2FS support
  2015-12-15 18:14           ` [f2fs-dev] " Jaegeuk Kim
@ 2016-01-08 19:41             ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-01-08 19:41 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Change log from v5:
 o fix build warning for ARM, reported by Michael Zimmermann

Thanks to Michael for testing this patch.

-- >8 --
From 284fd66662a6d5e07770cc021c8f59b538471c35 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1288 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1331 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..8d1cabf
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1288 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.6.3


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v6] F2FS support
@ 2016-01-08 19:41             ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-01-08 19:41 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Change log from v5:
 o fix build warning for ARM, reported by Michael Zimmermann

Thanks to Michael for testing this patch.

-- >8 --
From 284fd66662a6d5e07770cc021c8f59b538471c35 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1288 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1331 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..8d1cabf
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1288 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (pos > filesize || filesize > MAX_INLINE_DATA)
+        {
+          grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+          return -1;
+        }
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  file->size = grub_f2fs_file_size (&(data->inode->i));
+  file->data = data;
+  file->offset = 0;
+
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.6.3



^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH v6] F2FS support
  2016-01-08 19:41             ` [f2fs-dev] " Jaegeuk Kim
@ 2016-02-22  9:25               ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2016-02-22  9:25 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

08.01.2016 22:41, Jaegeuk Kim пишет:
> Change log from v5:
>  o fix build warning for ARM, reported by Michael Zimmermann
> 
> Thanks to Michael for testing this patch.
> 
> -- >8 --
> From 284fd66662a6d5e07770cc021c8f59b538471c35 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
> 
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 

Sorry for delay. I'm fine with it (with single nitpick, see below);
Vladimir has final word whether this can be committed.
...

> +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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)

GRUB checks that pos < filesize before calling into fs so this is
redundant. And filesize check is static so can be done in ->open.


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v6] F2FS support
@ 2016-02-22  9:25               ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2016-02-22  9:25 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

08.01.2016 22:41, Jaegeuk Kim пишет:
> Change log from v5:
>  o fix build warning for ARM, reported by Michael Zimmermann
> 
> Thanks to Michael for testing this patch.
> 
> -- >8 --
> From 284fd66662a6d5e07770cc021c8f59b538471c35 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
> 
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 

Sorry for delay. I'm fine with it (with single nitpick, see below);
Vladimir has final word whether this can be committed.
...

> +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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (pos > filesize || filesize > MAX_INLINE_DATA)

GRUB checks that pos < filesize before calling into fs so this is
redundant. And filesize check is static so can be done in ->open.



^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v6] F2FS support
  2016-02-22  9:25               ` [f2fs-dev] " Andrei Borzenkov
@ 2016-02-22 18:21                 ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-02-22 18:21 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Hi Andrei,

I'll send v7 which addresses your comment.

Thanks,

On Mon, Feb 22, 2016 at 12:25:26PM +0300, Andrei Borzenkov wrote:
> 08.01.2016 22:41, Jaegeuk Kim пишет:
> > Change log from v5:
> >  o fix build warning for ARM, reported by Michael Zimmermann
> > 
> > Thanks to Michael for testing this patch.
> > 
> > -- >8 --
> > From 284fd66662a6d5e07770cc021c8f59b538471c35 Mon Sep 17 00:00:00 2001
> > From: Jaegeuk Kim <jaegeuk@kernel.org>
> > Date: Tue, 17 Nov 2015 12:45:13 -0800
> > Subject: [PATCH] F2FS support
> > 
> > "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> > into Linux kernel v3.8 in 2013.
> > 
> > The motive for F2FS was to build a file system that from the start, takes into
> > account the characteristics of NAND flash memory-based storage devices (such as
> > solid-state disks, eMMC, and SD cards).
> > 
> > F2FS was designed on a basis of a log-structured file system approach, which
> > remedies some known issues of the older log structured file systems, such as
> > the snowball effect of wandering trees and high cleaning overhead. In addition,
> > since a NAND-based storage device shows different characteristics according to
> > its internal geometry or flash memory management scheme (such as the Flash
> > Translation Layer or FTL), it supports various parameters not only for
> > configuring on-disk layout, but also for selecting allocation and cleaning
> > algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> > 
> > The source codes for F2FS are available from:
> > 
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> > 
> 
> Sorry for delay. I'm fine with it (with single nitpick, see below);
> Vladimir has final word whether this can be committed.
> ...
> 
> > +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 = &node->inode.i;
> > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > +  char *inline_addr = __inline_addr (inode);
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > +    {
> > +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> 
> GRUB checks that pos < filesize before calling into fs so this is
> redundant. And filesize check is static so can be done in ->open.

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v6] F2FS support
@ 2016-02-22 18:21                 ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-02-22 18:21 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Hi Andrei,

I'll send v7 which addresses your comment.

Thanks,

On Mon, Feb 22, 2016 at 12:25:26PM +0300, Andrei Borzenkov wrote:
> 08.01.2016 22:41, Jaegeuk Kim пишет:
> > Change log from v5:
> >  o fix build warning for ARM, reported by Michael Zimmermann
> > 
> > Thanks to Michael for testing this patch.
> > 
> > -- >8 --
> > From 284fd66662a6d5e07770cc021c8f59b538471c35 Mon Sep 17 00:00:00 2001
> > From: Jaegeuk Kim <jaegeuk@kernel.org>
> > Date: Tue, 17 Nov 2015 12:45:13 -0800
> > Subject: [PATCH] F2FS support
> > 
> > "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> > into Linux kernel v3.8 in 2013.
> > 
> > The motive for F2FS was to build a file system that from the start, takes into
> > account the characteristics of NAND flash memory-based storage devices (such as
> > solid-state disks, eMMC, and SD cards).
> > 
> > F2FS was designed on a basis of a log-structured file system approach, which
> > remedies some known issues of the older log structured file systems, such as
> > the snowball effect of wandering trees and high cleaning overhead. In addition,
> > since a NAND-based storage device shows different characteristics according to
> > its internal geometry or flash memory management scheme (such as the Flash
> > Translation Layer or FTL), it supports various parameters not only for
> > configuring on-disk layout, but also for selecting allocation and cleaning
> > algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> > 
> > The source codes for F2FS are available from:
> > 
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> > 
> 
> Sorry for delay. I'm fine with it (with single nitpick, see below);
> Vladimir has final word whether this can be committed.
> ...
> 
> > +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 = &node->inode.i;
> > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > +  char *inline_addr = __inline_addr (inode);
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > +    {
> > +      if (pos > filesize || filesize > MAX_INLINE_DATA)
> 
> GRUB checks that pos < filesize before calling into fs so this is
> redundant. And filesize check is static so can be done in ->open.


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH v7] F2FS support
  2016-01-08 19:41             ` [f2fs-dev] " Jaegeuk Kim
@ 2016-02-22 18:25               ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-02-22 18:25 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Chang log from v6:
 o remove redundant filesize check in ->read
 o check consistency in ->open regarding to inline_data

-- >8 --
From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1332 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..cb312b3
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1289 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.6.3


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [PATCH v7] F2FS support
@ 2016-02-22 18:25               ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-02-22 18:25 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Chang log from v6:
 o remove redundant filesize check in ->read
 o check consistency in ->open regarding to inline_data

-- >8 --
From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1332 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..cb312b3
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1289 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.6.3



^ permalink raw reply related	[flat|nested] 77+ messages in thread

* [2.02] Re: [f2fs-dev] [PATCH v7] F2FS support
  2016-02-22 18:25               ` [f2fs-dev] " Jaegeuk Kim
@ 2016-03-01 19:52                 ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2016-03-01 19:52 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

ACK from my side. It is leaf code that does not affect anything so
should not cause any regression.

22.02.2016 21:25, Jaegeuk Kim пишет:
> Chang log from v6:
>  o remove redundant filesize check in ->read
>  o check consistency in ->open regarding to inline_data
> 
> -- >8 --
> From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
> 
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   10 +-
>  7 files changed, 1332 insertions(+), 4 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
> 
> diff --git a/Makefile.util.def b/Makefile.util.def
> index db7e8ec..84627bb 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -99,6 +99,7 @@ library = {
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
>    common = grub-core/fs/exfat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
>    common = grub-core/fs/hfsplus.c;
> @@ -766,6 +767,12 @@ script = {
>  
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = tests/nilfs2_test.in;
>  };
> diff --git a/docs/grub.texi b/docs/grub.texi
> index 1df3db2..e5a80f3 100644
> --- a/docs/grub.texi
> +++ b/docs/grub.texi
> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> +@dfn{f2fs}, @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},
> @@ -5347,7 +5348,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, minix,
>  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 charset
>  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 d9fa0e3..b585ade 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1278,6 +1278,11 @@ module = {
>  };
>  
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = fshelp;
>    common = fs/fshelp.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..cb312b3
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1289 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +/* F2FS Magic Number */
> +#define F2FS_SUPER_MAGIC	0xF2F52010
> +#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
> +#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
> +
> +/* byte-size offset */
> +#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
> +#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
> +#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
> +						GRUB_DISK_SECTOR_BITS)
> +
> +/* 9 bits for 512 bytes */
> +#define F2FS_MIN_LOG_SECTOR_SIZE	9
> +
> +/* support only 4KB block */
> +#define F2FS_BLK_BITS	12
> +#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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	(sizeof(struct grub_f2fs_nat_jent))
> +#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 (sizeof(struct grub_f2fs_nat_entry))
> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> +
> +#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
> +
> +#define CKPT_FLAG_SET(ckpt, f)	\
> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
> +#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
> +#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
> +#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
> +#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
> +
> +enum FILE_TYPE
> +{
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 7,
> +};
> +
> +#define MAX_VOLUME_NAME		512
> +
> +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[MAX_VOLUME_NAME];
> +  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;
> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
> +  };
> +  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_uint8_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
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  return addr[nr >> 3] & (1 << (nr & 7));
> +}
> +
> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +	- (base + 1) + type;
> +}
> +
> +static inline void *
> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 inode_block)
> +{
> +  if (inode_block)
> +    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,
> +		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> +		0, F2FS_BLKSIZE, buf);
> +}
> +
> +/*
> + * CRC32
> +*/
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return mask & *p;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_BLK_BITS)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
> +  if (err)
> +    return -1;
> +
> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> +}
> +
> +static void *
> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> +	grub_uint64_t *version)
> +{
> +  grub_uint32_t *cp_page_1, *cp_page_2;
> +  struct grub_f2fs_checkpoint *cp_block;
> +  grub_uint64_t cur_version = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
> +static grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +      offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_malloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> +    {
> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> +        {
> +          if (grub_errno == GRUB_ERR_NONE)
> +            grub_error (GRUB_ERR_BAD_FS,
> +			"not a F2FS filesystem (no superblock)");
> +          goto fail;
> +        }
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (filesize > MAX_INLINE_DATA)
> +        return -1;
> +      if (len > filesize - pos)
> +        len = 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +        return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_malloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = 0;
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> +	      grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> + 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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +  struct grub_f2fs_inode *inode;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  inode = &(data->inode->i);
> +  file->size = grub_f2fs_file_size (inode);
> +  file->data = data;
> +  file->offset = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_uint8_t *
> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> +{
> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> +  grub_uint8_t *out_buf;
> +  int len = 0;
> +
> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> +  if (!out_buf)
> +    return NULL;
> +
> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> +    len++;
> +  }
> +
> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> +  return out_buf;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +	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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = mod;
> +}
> +
> +GRUB_MOD_FINI (f2fs)
> +{
> +  grub_fs_unregister (&grub_f2fs_fs);
> +}
> diff --git a/po/exclude.pot b/po/exclude.pot
> index 0a9b215..816089c 100644
> --- a/po/exclude.pot
> +++ b/po/exclude.pot
> @@ -1214,6 +1214,7 @@ msgstr ""
>  
>  #: 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..1ea77c8
> --- /dev/null
> +++ b/tests/f2fs_test.in
> @@ -0,0 +1,19 @@
> +#!/bin/sh
> +
> +set -e
> +
> +if [ "x$EUID" = "x" ] ; then
> + EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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 424de22..e3e4109 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> -	xxfs)
> +	xxfs|xf2fs)
>  	    MINBLKSIZE=$SECSIZE
>  		# OS Limitation: GNU/Linux doesn't accept > 4096
>  	    MAXBLKSIZE=4096;;
> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>  
> +	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +		x"f2fs")
> +		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> +
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)
> 


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* [2.02] Re: [f2fs-dev] [PATCH v7] F2FS support
@ 2016-03-01 19:52                 ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2016-03-01 19:52 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

ACK from my side. It is leaf code that does not affect anything so
should not cause any regression.

22.02.2016 21:25, Jaegeuk Kim пишет:
> Chang log from v6:
>  o remove redundant filesize check in ->read
>  o check consistency in ->open regarding to inline_data
> 
> -- >8 --
> From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
> 
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   10 +-
>  7 files changed, 1332 insertions(+), 4 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
> 
> diff --git a/Makefile.util.def b/Makefile.util.def
> index db7e8ec..84627bb 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -99,6 +99,7 @@ library = {
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
>    common = grub-core/fs/exfat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
>    common = grub-core/fs/hfsplus.c;
> @@ -766,6 +767,12 @@ script = {
>  
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = tests/nilfs2_test.in;
>  };
> diff --git a/docs/grub.texi b/docs/grub.texi
> index 1df3db2..e5a80f3 100644
> --- a/docs/grub.texi
> +++ b/docs/grub.texi
> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> +@dfn{f2fs}, @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},
> @@ -5347,7 +5348,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, minix,
>  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 charset
>  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 d9fa0e3..b585ade 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1278,6 +1278,11 @@ module = {
>  };
>  
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = fshelp;
>    common = fs/fshelp.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..cb312b3
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1289 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +/* F2FS Magic Number */
> +#define F2FS_SUPER_MAGIC	0xF2F52010
> +#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
> +#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
> +
> +/* byte-size offset */
> +#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
> +#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
> +#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
> +						GRUB_DISK_SECTOR_BITS)
> +
> +/* 9 bits for 512 bytes */
> +#define F2FS_MIN_LOG_SECTOR_SIZE	9
> +
> +/* support only 4KB block */
> +#define F2FS_BLK_BITS	12
> +#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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	(sizeof(struct grub_f2fs_nat_jent))
> +#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 (sizeof(struct grub_f2fs_nat_entry))
> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> +
> +#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
> +
> +#define CKPT_FLAG_SET(ckpt, f)	\
> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
> +#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
> +#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
> +#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
> +#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
> +
> +enum FILE_TYPE
> +{
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 7,
> +};
> +
> +#define MAX_VOLUME_NAME		512
> +
> +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[MAX_VOLUME_NAME];
> +  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;
> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
> +  };
> +  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_uint8_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
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  return addr[nr >> 3] & (1 << (nr & 7));
> +}
> +
> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +	- (base + 1) + type;
> +}
> +
> +static inline void *
> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 inode_block)
> +{
> +  if (inode_block)
> +    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,
> +		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> +		0, F2FS_BLKSIZE, buf);
> +}
> +
> +/*
> + * CRC32
> +*/
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return mask & *p;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_BLK_BITS)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
> +  if (err)
> +    return -1;
> +
> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> +}
> +
> +static void *
> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> +	grub_uint64_t *version)
> +{
> +  grub_uint32_t *cp_page_1, *cp_page_2;
> +  struct grub_f2fs_checkpoint *cp_block;
> +  grub_uint64_t cur_version = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
> +static grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +      offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_malloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> +    {
> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> +        {
> +          if (grub_errno == GRUB_ERR_NONE)
> +            grub_error (GRUB_ERR_BAD_FS,
> +			"not a F2FS filesystem (no superblock)");
> +          goto fail;
> +        }
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (filesize > MAX_INLINE_DATA)
> +        return -1;
> +      if (len > filesize - pos)
> +        len = 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +        return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_malloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = 0;
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> +	      grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> + 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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +  struct grub_f2fs_inode *inode;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  inode = &(data->inode->i);
> +  file->size = grub_f2fs_file_size (inode);
> +  file->data = data;
> +  file->offset = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_uint8_t *
> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> +{
> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> +  grub_uint8_t *out_buf;
> +  int len = 0;
> +
> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> +  if (!out_buf)
> +    return NULL;
> +
> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> +    len++;
> +  }
> +
> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> +  return out_buf;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +	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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = mod;
> +}
> +
> +GRUB_MOD_FINI (f2fs)
> +{
> +  grub_fs_unregister (&grub_f2fs_fs);
> +}
> diff --git a/po/exclude.pot b/po/exclude.pot
> index 0a9b215..816089c 100644
> --- a/po/exclude.pot
> +++ b/po/exclude.pot
> @@ -1214,6 +1214,7 @@ msgstr ""
>  
>  #: 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..1ea77c8
> --- /dev/null
> +++ b/tests/f2fs_test.in
> @@ -0,0 +1,19 @@
> +#!/bin/sh
> +
> +set -e
> +
> +if [ "x$EUID" = "x" ] ; then
> + EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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 424de22..e3e4109 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> -	xxfs)
> +	xxfs|xf2fs)
>  	    MINBLKSIZE=$SECSIZE
>  		# OS Limitation: GNU/Linux doesn't accept > 4096
>  	    MAXBLKSIZE=4096;;
> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>  
> +	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +		x"f2fs")
> +		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> +
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)
> 



^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [2.02] Re: [f2fs-dev] [PATCH v7] F2FS support
  2016-03-01 19:52                 ` Andrei Borzenkov
@ 2016-03-02 23:20                   ` Michael Zimmermann
  -1 siblings, 0 replies; 77+ messages in thread
From: Michael Zimmermann @ 2016-03-02 23:20 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Jaegeuk Kim, linux-f2fs-devel


[-- Attachment #1.1: Type: text/plain, Size: 50742 bytes --]

I found a bug in the 'grub_f2fs_read_file' function.
It fails when the file position is not 0. to fix it:
-      grub_memcpy (buf + pos, inline_addr + pos, len);
+      grub_memcpy (buf, inline_addr + pos, len);


Michael

On Tue, Mar 1, 2016 at 8:52 PM, Andrei Borzenkov <arvidjaar@gmail.com>
wrote:

> ACK from my side. It is leaf code that does not affect anything so
> should not cause any regression.
>
> 22.02.2016 21:25, Jaegeuk Kim пишет:
> > Chang log from v6:
> >  o remove redundant filesize check in ->read
> >  o check consistency in ->open regarding to inline_data
> >
> > -- >8 --
> > From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
> > From: Jaegeuk Kim <jaegeuk@kernel.org>
> > Date: Tue, 17 Nov 2015 12:45:13 -0800
> > Subject: [PATCH] F2FS support
> >
> > "F2FS (Flash-Friendly File System) is flash-friendly file system which
> was merged
> > into Linux kernel v3.8 in 2013.
> >
> > The motive for F2FS was to build a file system that from the start,
> takes into
> > account the characteristics of NAND flash memory-based storage devices
> (such as
> > solid-state disks, eMMC, and SD cards).
> >
> > F2FS was designed on a basis of a log-structured file system approach,
> which
> > remedies some known issues of the older log structured file systems,
> such as
> > the snowball effect of wandering trees and high cleaning overhead. In
> addition,
> > since a NAND-based storage device shows different characteristics
> according to
> > its internal geometry or flash memory management scheme (such as the
> Flash
> > Translation Layer or FTL), it supports various parameters not only for
> > configuring on-disk layout, but also for selecting allocation and
> cleaning
> > algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> >
> > The source codes for F2FS are available from:
> >
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> >
> > Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> > ---
> >  Makefile.util.def            |    7 +
> >  docs/grub.texi               |    5 +-
> >  grub-core/Makefile.core.def  |    5 +
> >  grub-core/fs/f2fs.c          | 1289
> ++++++++++++++++++++++++++++++++++++++++++
> >  po/exclude.pot               |    1 +
> >  tests/f2fs_test.in           |   19 +
> >  tests/util/grub-fs-tester.in |   10 +-
> >  7 files changed, 1332 insertions(+), 4 deletions(-)
> >  create mode 100644 grub-core/fs/f2fs.c
> >  create mode 100644 tests/f2fs_test.in
> >
> > diff --git a/Makefile.util.def b/Makefile.util.def
> > index db7e8ec..84627bb 100644
> > --- a/Makefile.util.def
> > +++ b/Makefile.util.def
> > @@ -99,6 +99,7 @@ library = {
> >    common = grub-core/fs/ext2.c;
> >    common = grub-core/fs/fat.c;
> >    common = grub-core/fs/exfat.c;
> > +  common = grub-core/fs/f2fs.c;
> >    common = grub-core/fs/fshelp.c;
> >    common = grub-core/fs/hfs.c;
> >    common = grub-core/fs/hfsplus.c;
> > @@ -766,6 +767,12 @@ script = {
> >
> >  script = {
> >    testcase;
> > +  name = f2fs_test;
> > +  common = tests/f2fs_test.in;
> > +};
> > +
> > +script = {
> > +  testcase;
> >    name = nilfs2_test;
> >    common = tests/nilfs2_test.in;
> >  };
> > diff --git a/docs/grub.texi b/docs/grub.texi
> > index 1df3db2..e5a80f3 100644
> > --- a/docs/grub.texi
> > +++ b/docs/grub.texi
> > @@ -360,7 +360,8 @@ blocklist notation. The currently supported
> filesystem 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},
> @dfn{HFS},
> > +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> > +@dfn{f2fs}, @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},
> > @@ -5347,7 +5348,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,
> minix,
> >  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
> charset
> >  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 d9fa0e3..b585ade 100644
> > --- a/grub-core/Makefile.core.def
> > +++ b/grub-core/Makefile.core.def
> > @@ -1278,6 +1278,11 @@ module = {
> >  };
> >
> >  module = {
> > +  name = f2fs;
> > +  common = fs/f2fs.c;
> > +};
> > +
> > +module = {
> >    name = fshelp;
> >    common = fs/fshelp.c;
> >  };
> > diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> > new file mode 100644
> > index 0000000..cb312b3
> > --- /dev/null
> > +++ b/grub-core/fs/f2fs.c
> > @@ -0,0 +1,1289 @@
> > +/*
> > + *  f2fs.c - Flash-Friendly File System
> > + *
> > + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> > + *
> > + *  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 <http://www.gnu.org/licenses/>.
> > + */
> > +#include <grub/err.h>
> > +#include <grub/file.h>
> > +#include <grub/mm.h>
> > +#include <grub/misc.h>
> > +#include <grub/disk.h>
> > +#include <grub/dl.h>
> > +#include <grub/types.h>
> > +#include <grub/charset.h>
> > +#include <grub/fshelp.h>
> > +
> > +GRUB_MOD_LICENSE ("GPLv3+");
> > +
> > +/* F2FS Magic Number */
> > +#define F2FS_SUPER_MAGIC     0xF2F52010
> > +#define CHECKSUM_OFFSET              4092            /* must be aligned
> 4 bytes */
> > +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> > +
> > +/* byte-size offset */
> > +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> > +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >>
> GRUB_DISK_SECTOR_BITS)
> > +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> > +                                             GRUB_DISK_SECTOR_BITS)
> > +
> > +/* 9 bits for 512 bytes */
> > +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> > +
> > +/* support only 4KB block */
> > +#define F2FS_BLK_BITS        12
> > +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> > +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> > +
> > +#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  (sizeof(struct grub_f2fs_nat_jent))
> > +#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 (sizeof(struct grub_f2fs_nat_entry))
> > +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> > +
> > +#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
> > +
> > +#define CKPT_FLAG_SET(ckpt, f)       \
> > +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> > +
> > +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> > +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> > +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> > +#define F2FS_DATA_EXIST              0x08    /* file inline data exist
> flag */
> > +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot
> dentries */
> > +
> > +enum FILE_TYPE
> > +{
> > +  F2FS_FT_UNKNOWN,
> > +  F2FS_FT_REG_FILE = 1,
> > +  F2FS_FT_DIR = 2,
> > +  F2FS_FT_SYMLINK = 7,
> > +};
> > +
> > +#define MAX_VOLUME_NAME              512
> > +
> > +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[MAX_VOLUME_NAME];
> > +  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;
> > +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE
> totally */
> > +  };
> > +  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_uint8_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
> > +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > +{
> > +  return addr[nr >> 3] & (1 << (nr & 7));
> > +}
> > +
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&inode->i_addr[1];
> > +}
> > +
> > +static inline grub_uint64_t
> > +grub_f2fs_file_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 = &data->ckpt;
> > +  grub_uint32_t start_addr = data->cp_blkaddr;
> > +
> > +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> > +
> > +  return __start_cp_addr (data) +
> > +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> > +     - (base + 1) + type;
> > +}
> > +
> > +static inline void *
> > +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> > +{
> > +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> > +  grub_uint32_t offset;
> > +
> > +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> > +    return ckpt->sit_nat_version_bitmap;
> > +
> > +  offset = 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 inode_block)
> > +{
> > +  if (inode_block)
> > +    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,
> > +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> > +             0, F2FS_BLKSIZE, buf);
> > +}
> > +
> > +/*
> > + * CRC32
> > +*/
> > +#define CRCPOLY_LE 0xedb88320
> > +
> > +static inline grub_uint32_t
> > +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> > +{
> > +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> > +  unsigned char *p = (unsigned char *)buf;
> > +  grub_uint32_t tmp = len;
> > +  int i;
> > +
> > +  while (tmp--)
> > +    {
> > +      crc ^= *p++;
> > +      for (i = 0; i < 8; i++)
> > +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> > +    }
> > +  return crc;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> grub_uint32_t len)
> > +{
> > +  grub_uint32_t cal_crc = 0;
> > +
> > +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> > +
> > +  return (cal_crc == blk_crc) ? 1 : 0;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +
> > +  p += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return mask & *p;
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > +
> > +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > +    return -1;
> > +
> > +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time
> (F2FS_BLK_BITS))
> > +    return -1;
> > +
> > +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> > +
> > +  if (log_sectorsize > F2FS_BLK_BITS)
> > +    return -1;
> > +
> > +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > +    return -1;
> > +
> > +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> > +    return -1;
> > +
> > +  return 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> > +{
> > +  grub_disk_t disk = data->disk;
> > +  grub_err_t err;
> > +
> > +  /* Read first super block. */
> > +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> &data->sblock);
> > +  if (err)
> > +    return -1;
> > +
> > +  return grub_f2fs_sanity_check_sb (&data->sblock);
> > +}
> > +
> > +static void *
> > +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> > +     grub_uint64_t *version)
> > +{
> > +  grub_uint32_t *cp_page_1, *cp_page_2;
> > +  struct grub_f2fs_checkpoint *cp_block;
> > +  grub_uint64_t cur_version = 0, pre_version = 0;
> > +  grub_uint32_t crc = 0;
> > +  grub_uint32_t crc_offset;
> > +  grub_err_t err;
> > +
> > +  /* Read the 1st cp block in this CP pack */
> > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_1)
> > +    return NULL;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > +  if (err)
> > +    goto invalid_cp1;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset != CHECKSUM_OFFSET)
> > +    goto invalid_cp1;
> > +
> > +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp1;
> > +
> > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +
> > +  /* Read the 2nd cp block in this CP pack */
> > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_2)
> > +    goto invalid_cp1;
> > +
> > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > +  if (err)
> > +    goto invalid_cp2;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset != CHECKSUM_OFFSET)
> > +    goto invalid_cp2;
> > +
> > +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp2;
> > +
> > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +  if (cur_version == pre_version)
> > +    {
> > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > +  cp1 = 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 += data->blocks_per_seg;
> > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > +  if (!cp2 && grub_errno)
> > +    {
> > +      grub_free (cp1);
> > +      return grub_errno;
> > +    }
> > +
> > +  if (cp1 && cp2)
> > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > +  else if (cp1)
> > +    cur_page = cp1;
> > +  else if (cp2)
> > +    cur_page = cp2;
> > +  else
> > +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> > +
> > +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> > +
> > +  grub_free (cp1);
> > +  grub_free (cp2);
> > +  return 0;
> > +}
> > +
> > +static grub_err_t
> > +get_nat_journal (struct grub_f2fs_data *data)
> > +{
> > +  grub_uint32_t block;
> > +  char *buf;
> > +  grub_err_t err;
> > +
> > +  buf = grub_malloc (F2FS_BLKSIZE);
> > +  if (!buf)
> > +    return grub_errno;
> > +
> > +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> > +    block = __start_sum_block (data);
> > +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > +  else
> > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > +
> > +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> > +  grub_uint32_t blkaddr = 0;
> > +  grub_uint16_t i;
> > +
> > +  for (i = 0; i < n; i++)
> > +    {
> > +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> > +        {
> > +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> > +  if (blkaddr)
> > +    return blkaddr;
> > +
> > +  nat_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!nat_block)
> > +    return 0;
> > +
> > +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> > +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> > +
> > +  seg_off = block_off / data->blocks_per_seg;
> > +  block_addr = 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 += data->blocks_per_seg;
> > +
> > +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> > +  if (err)
> > +    {
> > +      grub_free (nat_block);
> > +      return 0;
> > +    }
> > +
> > +  blkaddr = 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 = ADDRS_PER_BLOCK;
> > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > +  int n = 0;
> > +  int level = 0;
> > +
> > +  if (inode->i_inline & F2FS_INLINE_XATTR)
> > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > +
> > +  noffset[0] = 0;
> > +
> > +  if (block < direct_index)
> > +    {
> > +      offset[n] = block;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_index;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR1_BLOCK;
> > +      noffset[n] = 1;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR2_BLOCK;
> > +      noffset[n] = 2;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND1_BLOCK;
> > +      noffset[n] = 3;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 4 + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND2_BLOCK;
> > +      noffset[n] = 4 + dptrs_per_blk;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < dindirect_blks)
> > +    {
> > +      offset[n++] = NODE_DIND_BLOCK;
> > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > +      offset[n++] = block / indirect_blks;
> > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > +      offset[n - 1] * (dptrs_per_blk + 1);
> > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 3;
> > +      goto got;
> > +    }
> > +got:
> > +  return level;
> > +}
> > +
> > +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 = 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 = grub_malloc (sizeof (*data));
> > +  if (!data)
> > +    return NULL;
> > +
> > +  data->disk = disk;
> > +
> > +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> > +    {
> > +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> > +        {
> > +          if (grub_errno == GRUB_ERR_NONE)
> > +            grub_error (GRUB_ERR_BAD_FS,
> > +                     "not a F2FS filesystem (no superblock)");
> > +          goto fail;
> > +        }
> > +    }
> > +
> > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > +  data->blocks_per_seg = 1 <<
> > +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > +
> > +  err = grub_f2fs_read_cp (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->nat_bitmap = __nat_bitmap_ptr (data);
> > +
> > +  err = get_nat_journal (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->diropen.data = data;
> > +  data->diropen.ino = data->root_ino;
> > +  data->diropen.inode_read = 1;
> > +  data->inode = &data->diropen.inode;
> > +
> > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > +  if (err)
> > +    goto fail;
> > +
> > +  return data;
> > +
> > +fail:
> > +  grub_free (data);
> > +  return NULL;
> > +}
> > +
> > +/* guarantee inline_data was handled by caller */
> > +static grub_disk_addr_t
> > +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t
> block_ofs)
> > +{
> > +  struct grub_f2fs_data *data = node->data;
> > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > +  grub_uint32_t offset[4], noffset[4], nids[4];
> > +  struct grub_f2fs_node *node_block;
> > +  grub_uint32_t block_addr = -1;
> > +  int level, i;
> > +
> > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > +  if (level == 0)
> > +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > +
> > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!node_block)
> > +    return -1;
> > +
> > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > +
> > +  /* get indirect or direct nodes */
> > +  for (i = 1; i <= level; i++)
> > +    {
> > +      grub_f2fs_read_node (data, nids[i], node_block);
> > +      if (grub_errno)
> > +        goto fail;
> > +
> > +      if (i < level)
> > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > +    }
> > +
> > +  block_addr = 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 = &node->inode.i;
> > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > +  char *inline_addr = __inline_addr (inode);
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > +    {
> > +      if (filesize > MAX_INLINE_DATA)
> > +        return -1;
> > +      if (len > filesize - pos)
> > +        len = 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_get_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 = node;
> > +  grub_uint64_t filesize;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +        return 0;
> > +    }
> > +
> > +  filesize = grub_f2fs_file_size(&diro->inode.i);
> > +
> > +  symlink = grub_malloc (filesize + 1);
> > +  if (!symlink)
> > +    return 0;
> > +
> > +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> > +  if (grub_errno)
> > +    {
> > +      grub_free (symlink);
> > +      return 0;
> > +    }
> > +
> > +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> > +    {
> > +      char *filename;
> > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > +      enum FILE_TYPE ftype;
> > +      int name_len;
> > +      int ret;
> > +
> > +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> > +        {
> > +          i++;
> > +          continue;
> > +        }
> > +
> > +      ftype = ctx->dentry[i].file_type;
> > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > +      filename = grub_malloc (name_len + 1);
> > +      if (!filename)
> > +        return 0;
> > +
> > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > +      filename[name_len] = 0;
> > +
> > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > +      if (!fdiro)
> > +        {
> > +          grub_free(filename);
> > +          return 0;
> > +        }
> > +
> > +      if (ftype == F2FS_FT_DIR)
> > +        type = GRUB_FSHELP_DIR;
> > +      else if (ftype == F2FS_FT_SYMLINK)
> > +        type = GRUB_FSHELP_SYMLINK;
> > +      else if (ftype == F2FS_FT_REG_FILE)
> > +        type = GRUB_FSHELP_REG;
> > +
> > +      fdiro->data = ctx->data;
> > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > +      fdiro->inode_read = 0;
> > +
> > +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> > +      grub_free(filename);
> > +      if (ret)
> > +        return 1;
> > +
> > +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> > +
> > +  ctx->bitmap = de_blk->dentry_bitmap;
> > +  ctx->dentry = de_blk->dentry;
> > +  ctx->filename = de_blk->filename;
> > +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> > +  struct grub_f2fs_inode *inode;
> > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > +    .data = diro->data,
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  grub_off_t fpos = 0;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +     return 0;
> > +    }
> > +
> > +  inode = &diro->inode.i;
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> > +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> > +
> > +  while (fpos < grub_f2fs_file_size (inode))
> > +    {
> > +      struct grub_f2fs_dentry_block *de_blk;
> > +      char *buf;
> > +      int ret;
> > +
> > +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> > +
> > +      ctx.bitmap = de_blk->dentry_bitmap;
> > +      ctx.dentry = de_blk->dentry;
> > +      ctx.filename = de_blk->filename;
> > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > +
> > +      ret = grub_f2fs_check_dentries (&ctx);
> > +      grub_free (buf);
> > +      if (ret)
> > +        return 1;
> > +
> > +      fpos += F2FS_BLKSIZE;
> > +    }
> > +  return 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> filetype,
> > +           grub_fshelp_node_t node, void *data)
> > +{
> > +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> > +      grub_errno = GRUB_ERR_NONE;
> > +    }
> > +  if (node->inode_read)
> > +    {
> > +      info.mtimeset = 1;
> > +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> > +    }
> > +
> > +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> > +  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 = {
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  ctx.data = 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 != &ctx.data->diropen)
> > +    grub_free (fdiro);
> > + 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 = NULL;
> > +  struct grub_fshelp_node *fdiro = 0;
> > +  struct grub_f2fs_inode *inode;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = 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, sizeof (*data->inode));
> > +  grub_free (fdiro);
> > +
> > +  inode = &(data->inode->i);
> > +  file->size = grub_f2fs_file_size (inode);
> > +  file->data = data;
> > +  file->offset = 0;
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA && file->size >
> MAX_INLINE_DATA)
> > +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> > +  return 0;
> > +
> > +fail:
> > +  if (fdiro != &data->diropen)
> > +    grub_free (fdiro);
> > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > +
> > +  grub_free (data);
> > +
> > +  grub_dl_unref (my_mod);
> > +
> > +  return GRUB_ERR_NONE;
> > +}
> > +
> > +static grub_uint8_t *
> > +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> > +{
> > +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> > +  grub_uint8_t *out_buf;
> > +  int len = 0;
> > +
> > +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> > +  if (!out_buf)
> > +    return NULL;
> > +
> > +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> > +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> > +    len++;
> > +  }
> > +
> > +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> > +  return out_buf;
> > +}
> > +
> > +static grub_err_t
> > +grub_f2fs_label (grub_device_t device, char **label)
> > +{
> > +  struct grub_f2fs_data *data;
> > +  grub_disk_t disk = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    *label = (char *) grub_f2fs_utf16_to_utf8
> (data->sblock.volume_name);
> > +  else
> > +    *label = NULL;
> > +
> > +  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 = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    {
> > +      *uuid =
> > +     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 = NULL;
> > +
> > +  grub_free (data);
> > +  grub_dl_unref (my_mod);
> > +  return grub_errno;
> > +}
> > +
> > +static struct grub_fs grub_f2fs_fs = {
> > +  .name = "f2fs",
> > +  .dir = grub_f2fs_dir,
> > +  .open = grub_f2fs_open,
> > +  .read = grub_f2fs_read,
> > +  .close = grub_f2fs_close,
> > +  .label = grub_f2fs_label,
> > +  .uuid = grub_f2fs_uuid,
> > +#ifdef GRUB_UTIL
> > +  .reserved_first_sector = 1,
> > +  .blocklist_install = 0,
> > +#endif
> > +  .next = 0
> > +};
> > +
> > +GRUB_MOD_INIT (f2fs)
> > +{
> > +  grub_fs_register (&grub_f2fs_fs);
> > +  my_mod = mod;
> > +}
> > +
> > +GRUB_MOD_FINI (f2fs)
> > +{
> > +  grub_fs_unregister (&grub_f2fs_fs);
> > +}
> > diff --git a/po/exclude.pot b/po/exclude.pot
> > index 0a9b215..816089c 100644
> > --- a/po/exclude.pot
> > +++ b/po/exclude.pot
> > @@ -1214,6 +1214,7 @@ msgstr ""
> >
> >  #: 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..1ea77c8
> > --- /dev/null
> > +++ b/tests/f2fs_test.in
> > @@ -0,0 +1,19 @@
> > +#!/bin/sh
> > +
> > +set -e
> > +
> > +if [ "x$EUID" = "x" ] ; then
> > + EUID=`id -u`
> > +fi
> > +
> > +if [ "$EUID" != 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 424de22..e3e4109 100644
> > --- a/tests/util/grub-fs-tester.in
> > +++ b/tests/util/grub-fs-tester.in
> > @@ -142,7 +142,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >       xsquash*)
> >           MINBLKSIZE=4096
> >           MAXBLKSIZE=1048576;;
> > -     xxfs)
> > +     xxfs|xf2fs)
> >           MINBLKSIZE=$SECSIZE
> >               # OS Limitation: GNU/Linux doesn't accept > 4096
> >           MAXBLKSIZE=4096;;
> > @@ -265,6 +265,10 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >               x"btrfs"*)
> >                   FSLABEL="grub_;/testé莭莽😁киритi
> urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> >
> > +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> > +             x"f2fs")
> > +
>  FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> > +
> >           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >               x"exfat")
> >                   FSLABEL="géт ;/莭莽😁кир";;
> > @@ -474,7 +478,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> Check it.
> >       # FS LIMITATION: as far as I know those FS don't store their last
> modification date.
> >               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"*
> | x"reiserfs_old" | x"reiserfs" \
> > -                 | 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=y;;
> > @@ -753,6 +757,8 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >                   MOUNTFS=ext2
> >                   "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)
> >
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #1.2: Type: text/html, Size: 63675 bytes --]

[-- Attachment #2: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [2.02] Re: [f2fs-dev] [PATCH v7] F2FS support
@ 2016-03-02 23:20                   ` Michael Zimmermann
  0 siblings, 0 replies; 77+ messages in thread
From: Michael Zimmermann @ 2016-03-02 23:20 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Jaegeuk Kim, linux-f2fs-devel

[-- Attachment #1: Type: text/plain, Size: 50742 bytes --]

I found a bug in the 'grub_f2fs_read_file' function.
It fails when the file position is not 0. to fix it:
-      grub_memcpy (buf + pos, inline_addr + pos, len);
+      grub_memcpy (buf, inline_addr + pos, len);


Michael

On Tue, Mar 1, 2016 at 8:52 PM, Andrei Borzenkov <arvidjaar@gmail.com>
wrote:

> ACK from my side. It is leaf code that does not affect anything so
> should not cause any regression.
>
> 22.02.2016 21:25, Jaegeuk Kim пишет:
> > Chang log from v6:
> >  o remove redundant filesize check in ->read
> >  o check consistency in ->open regarding to inline_data
> >
> > -- >8 --
> > From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
> > From: Jaegeuk Kim <jaegeuk@kernel.org>
> > Date: Tue, 17 Nov 2015 12:45:13 -0800
> > Subject: [PATCH] F2FS support
> >
> > "F2FS (Flash-Friendly File System) is flash-friendly file system which
> was merged
> > into Linux kernel v3.8 in 2013.
> >
> > The motive for F2FS was to build a file system that from the start,
> takes into
> > account the characteristics of NAND flash memory-based storage devices
> (such as
> > solid-state disks, eMMC, and SD cards).
> >
> > F2FS was designed on a basis of a log-structured file system approach,
> which
> > remedies some known issues of the older log structured file systems,
> such as
> > the snowball effect of wandering trees and high cleaning overhead. In
> addition,
> > since a NAND-based storage device shows different characteristics
> according to
> > its internal geometry or flash memory management scheme (such as the
> Flash
> > Translation Layer or FTL), it supports various parameters not only for
> > configuring on-disk layout, but also for selecting allocation and
> cleaning
> > algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> >
> > The source codes for F2FS are available from:
> >
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> >
> > Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> > ---
> >  Makefile.util.def            |    7 +
> >  docs/grub.texi               |    5 +-
> >  grub-core/Makefile.core.def  |    5 +
> >  grub-core/fs/f2fs.c          | 1289
> ++++++++++++++++++++++++++++++++++++++++++
> >  po/exclude.pot               |    1 +
> >  tests/f2fs_test.in           |   19 +
> >  tests/util/grub-fs-tester.in |   10 +-
> >  7 files changed, 1332 insertions(+), 4 deletions(-)
> >  create mode 100644 grub-core/fs/f2fs.c
> >  create mode 100644 tests/f2fs_test.in
> >
> > diff --git a/Makefile.util.def b/Makefile.util.def
> > index db7e8ec..84627bb 100644
> > --- a/Makefile.util.def
> > +++ b/Makefile.util.def
> > @@ -99,6 +99,7 @@ library = {
> >    common = grub-core/fs/ext2.c;
> >    common = grub-core/fs/fat.c;
> >    common = grub-core/fs/exfat.c;
> > +  common = grub-core/fs/f2fs.c;
> >    common = grub-core/fs/fshelp.c;
> >    common = grub-core/fs/hfs.c;
> >    common = grub-core/fs/hfsplus.c;
> > @@ -766,6 +767,12 @@ script = {
> >
> >  script = {
> >    testcase;
> > +  name = f2fs_test;
> > +  common = tests/f2fs_test.in;
> > +};
> > +
> > +script = {
> > +  testcase;
> >    name = nilfs2_test;
> >    common = tests/nilfs2_test.in;
> >  };
> > diff --git a/docs/grub.texi b/docs/grub.texi
> > index 1df3db2..e5a80f3 100644
> > --- a/docs/grub.texi
> > +++ b/docs/grub.texi
> > @@ -360,7 +360,8 @@ blocklist notation. The currently supported
> filesystem 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},
> @dfn{HFS},
> > +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> > +@dfn{f2fs}, @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},
> > @@ -5347,7 +5348,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,
> minix,
> >  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
> charset
> >  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 d9fa0e3..b585ade 100644
> > --- a/grub-core/Makefile.core.def
> > +++ b/grub-core/Makefile.core.def
> > @@ -1278,6 +1278,11 @@ module = {
> >  };
> >
> >  module = {
> > +  name = f2fs;
> > +  common = fs/f2fs.c;
> > +};
> > +
> > +module = {
> >    name = fshelp;
> >    common = fs/fshelp.c;
> >  };
> > diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> > new file mode 100644
> > index 0000000..cb312b3
> > --- /dev/null
> > +++ b/grub-core/fs/f2fs.c
> > @@ -0,0 +1,1289 @@
> > +/*
> > + *  f2fs.c - Flash-Friendly File System
> > + *
> > + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> > + *
> > + *  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 <http://www.gnu.org/licenses/>.
> > + */
> > +#include <grub/err.h>
> > +#include <grub/file.h>
> > +#include <grub/mm.h>
> > +#include <grub/misc.h>
> > +#include <grub/disk.h>
> > +#include <grub/dl.h>
> > +#include <grub/types.h>
> > +#include <grub/charset.h>
> > +#include <grub/fshelp.h>
> > +
> > +GRUB_MOD_LICENSE ("GPLv3+");
> > +
> > +/* F2FS Magic Number */
> > +#define F2FS_SUPER_MAGIC     0xF2F52010
> > +#define CHECKSUM_OFFSET              4092            /* must be aligned
> 4 bytes */
> > +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> > +
> > +/* byte-size offset */
> > +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> > +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >>
> GRUB_DISK_SECTOR_BITS)
> > +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> > +                                             GRUB_DISK_SECTOR_BITS)
> > +
> > +/* 9 bits for 512 bytes */
> > +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> > +
> > +/* support only 4KB block */
> > +#define F2FS_BLK_BITS        12
> > +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> > +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> > +
> > +#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  (sizeof(struct grub_f2fs_nat_jent))
> > +#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 (sizeof(struct grub_f2fs_nat_entry))
> > +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> > +
> > +#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
> > +
> > +#define CKPT_FLAG_SET(ckpt, f)       \
> > +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> > +
> > +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> > +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> > +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> > +#define F2FS_DATA_EXIST              0x08    /* file inline data exist
> flag */
> > +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot
> dentries */
> > +
> > +enum FILE_TYPE
> > +{
> > +  F2FS_FT_UNKNOWN,
> > +  F2FS_FT_REG_FILE = 1,
> > +  F2FS_FT_DIR = 2,
> > +  F2FS_FT_SYMLINK = 7,
> > +};
> > +
> > +#define MAX_VOLUME_NAME              512
> > +
> > +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[MAX_VOLUME_NAME];
> > +  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;
> > +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE
> totally */
> > +  };
> > +  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_uint8_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
> > +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > +{
> > +  return addr[nr >> 3] & (1 << (nr & 7));
> > +}
> > +
> > +static inline char *
> > +__inline_addr (struct grub_f2fs_inode *inode)
> > +{
> > +  return (char *)&inode->i_addr[1];
> > +}
> > +
> > +static inline grub_uint64_t
> > +grub_f2fs_file_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 = &data->ckpt;
> > +  grub_uint32_t start_addr = data->cp_blkaddr;
> > +
> > +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> > +
> > +  return __start_cp_addr (data) +
> > +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> > +     - (base + 1) + type;
> > +}
> > +
> > +static inline void *
> > +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> > +{
> > +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> > +  grub_uint32_t offset;
> > +
> > +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> > +    return ckpt->sit_nat_version_bitmap;
> > +
> > +  offset = 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 inode_block)
> > +{
> > +  if (inode_block)
> > +    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,
> > +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> > +             0, F2FS_BLKSIZE, buf);
> > +}
> > +
> > +/*
> > + * CRC32
> > +*/
> > +#define CRCPOLY_LE 0xedb88320
> > +
> > +static inline grub_uint32_t
> > +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> > +{
> > +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> > +  unsigned char *p = (unsigned char *)buf;
> > +  grub_uint32_t tmp = len;
> > +  int i;
> > +
> > +  while (tmp--)
> > +    {
> > +      crc ^= *p++;
> > +      for (i = 0; i < 8; i++)
> > +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> > +    }
> > +  return crc;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> grub_uint32_t len)
> > +{
> > +  grub_uint32_t cal_crc = 0;
> > +
> > +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> > +
> > +  return (cal_crc == blk_crc) ? 1 : 0;
> > +}
> > +
> > +static inline int
> > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > +{
> > +  int mask;
> > +
> > +  p += (nr >> 3);
> > +  mask = 1 << (7 - (nr & 0x07));
> > +  return mask & *p;
> > +}
> > +
> > +static int
> > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > +{
> > +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > +
> > +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > +    return -1;
> > +
> > +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time
> (F2FS_BLK_BITS))
> > +    return -1;
> > +
> > +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> > +
> > +  if (log_sectorsize > F2FS_BLK_BITS)
> > +    return -1;
> > +
> > +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > +    return -1;
> > +
> > +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> > +    return -1;
> > +
> > +  return 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> > +{
> > +  grub_disk_t disk = data->disk;
> > +  grub_err_t err;
> > +
> > +  /* Read first super block. */
> > +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> &data->sblock);
> > +  if (err)
> > +    return -1;
> > +
> > +  return grub_f2fs_sanity_check_sb (&data->sblock);
> > +}
> > +
> > +static void *
> > +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> > +     grub_uint64_t *version)
> > +{
> > +  grub_uint32_t *cp_page_1, *cp_page_2;
> > +  struct grub_f2fs_checkpoint *cp_block;
> > +  grub_uint64_t cur_version = 0, pre_version = 0;
> > +  grub_uint32_t crc = 0;
> > +  grub_uint32_t crc_offset;
> > +  grub_err_t err;
> > +
> > +  /* Read the 1st cp block in this CP pack */
> > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_1)
> > +    return NULL;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > +  if (err)
> > +    goto invalid_cp1;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset != CHECKSUM_OFFSET)
> > +    goto invalid_cp1;
> > +
> > +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp1;
> > +
> > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +
> > +  /* Read the 2nd cp block in this CP pack */
> > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > +  if (!cp_page_2)
> > +    goto invalid_cp1;
> > +
> > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > +
> > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > +  if (err)
> > +    goto invalid_cp2;
> > +
> > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > +  if (crc_offset != CHECKSUM_OFFSET)
> > +    goto invalid_cp2;
> > +
> > +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > +    goto invalid_cp2;
> > +
> > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > +  if (cur_version == pre_version)
> > +    {
> > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > +  cp1 = 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 += data->blocks_per_seg;
> > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > +  if (!cp2 && grub_errno)
> > +    {
> > +      grub_free (cp1);
> > +      return grub_errno;
> > +    }
> > +
> > +  if (cp1 && cp2)
> > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > +  else if (cp1)
> > +    cur_page = cp1;
> > +  else if (cp2)
> > +    cur_page = cp2;
> > +  else
> > +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> > +
> > +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> > +
> > +  grub_free (cp1);
> > +  grub_free (cp2);
> > +  return 0;
> > +}
> > +
> > +static grub_err_t
> > +get_nat_journal (struct grub_f2fs_data *data)
> > +{
> > +  grub_uint32_t block;
> > +  char *buf;
> > +  grub_err_t err;
> > +
> > +  buf = grub_malloc (F2FS_BLKSIZE);
> > +  if (!buf)
> > +    return grub_errno;
> > +
> > +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> > +    block = __start_sum_block (data);
> > +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > +  else
> > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > +
> > +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> > +  grub_uint32_t blkaddr = 0;
> > +  grub_uint16_t i;
> > +
> > +  for (i = 0; i < n; i++)
> > +    {
> > +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> > +        {
> > +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> > +  if (blkaddr)
> > +    return blkaddr;
> > +
> > +  nat_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!nat_block)
> > +    return 0;
> > +
> > +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> > +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> > +
> > +  seg_off = block_off / data->blocks_per_seg;
> > +  block_addr = 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 += data->blocks_per_seg;
> > +
> > +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> > +  if (err)
> > +    {
> > +      grub_free (nat_block);
> > +      return 0;
> > +    }
> > +
> > +  blkaddr = 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 = ADDRS_PER_BLOCK;
> > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > +  int n = 0;
> > +  int level = 0;
> > +
> > +  if (inode->i_inline & F2FS_INLINE_XATTR)
> > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > +
> > +  noffset[0] = 0;
> > +
> > +  if (block < direct_index)
> > +    {
> > +      offset[n] = block;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_index;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR1_BLOCK;
> > +      noffset[n] = 1;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < direct_blks)
> > +    {
> > +      offset[n++] = NODE_DIR2_BLOCK;
> > +      noffset[n] = 2;
> > +      offset[n] = block;
> > +      level = 1;
> > +      goto got;
> > +    }
> > +
> > +  block -= direct_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND1_BLOCK;
> > +      noffset[n] = 3;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 4 + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < indirect_blks)
> > +    {
> > +      offset[n++] = NODE_IND2_BLOCK;
> > +      noffset[n] = 4 + dptrs_per_blk;
> > +      offset[n++] = block / direct_blks;
> > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 2;
> > +      goto got;
> > +    }
> > +
> > +  block -= indirect_blks;
> > +  if (block < dindirect_blks)
> > +    {
> > +      offset[n++] = NODE_DIND_BLOCK;
> > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > +      offset[n++] = block / indirect_blks;
> > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > +      offset[n - 1] * (dptrs_per_blk + 1);
> > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> > +      offset[n] = block % direct_blks;
> > +      level = 3;
> > +      goto got;
> > +    }
> > +got:
> > +  return level;
> > +}
> > +
> > +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 = 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 = grub_malloc (sizeof (*data));
> > +  if (!data)
> > +    return NULL;
> > +
> > +  data->disk = disk;
> > +
> > +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> > +    {
> > +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> > +        {
> > +          if (grub_errno == GRUB_ERR_NONE)
> > +            grub_error (GRUB_ERR_BAD_FS,
> > +                     "not a F2FS filesystem (no superblock)");
> > +          goto fail;
> > +        }
> > +    }
> > +
> > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > +  data->blocks_per_seg = 1 <<
> > +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > +
> > +  err = grub_f2fs_read_cp (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->nat_bitmap = __nat_bitmap_ptr (data);
> > +
> > +  err = get_nat_journal (data);
> > +  if (err)
> > +    goto fail;
> > +
> > +  data->diropen.data = data;
> > +  data->diropen.ino = data->root_ino;
> > +  data->diropen.inode_read = 1;
> > +  data->inode = &data->diropen.inode;
> > +
> > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > +  if (err)
> > +    goto fail;
> > +
> > +  return data;
> > +
> > +fail:
> > +  grub_free (data);
> > +  return NULL;
> > +}
> > +
> > +/* guarantee inline_data was handled by caller */
> > +static grub_disk_addr_t
> > +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t
> block_ofs)
> > +{
> > +  struct grub_f2fs_data *data = node->data;
> > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > +  grub_uint32_t offset[4], noffset[4], nids[4];
> > +  struct grub_f2fs_node *node_block;
> > +  grub_uint32_t block_addr = -1;
> > +  int level, i;
> > +
> > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > +  if (level == 0)
> > +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > +
> > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > +  if (!node_block)
> > +    return -1;
> > +
> > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > +
> > +  /* get indirect or direct nodes */
> > +  for (i = 1; i <= level; i++)
> > +    {
> > +      grub_f2fs_read_node (data, nids[i], node_block);
> > +      if (grub_errno)
> > +        goto fail;
> > +
> > +      if (i < level)
> > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > +    }
> > +
> > +  block_addr = 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 = &node->inode.i;
> > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > +  char *inline_addr = __inline_addr (inode);
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > +    {
> > +      if (filesize > MAX_INLINE_DATA)
> > +        return -1;
> > +      if (len > filesize - pos)
> > +        len = 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_get_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 = node;
> > +  grub_uint64_t filesize;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +        return 0;
> > +    }
> > +
> > +  filesize = grub_f2fs_file_size(&diro->inode.i);
> > +
> > +  symlink = grub_malloc (filesize + 1);
> > +  if (!symlink)
> > +    return 0;
> > +
> > +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> > +  if (grub_errno)
> > +    {
> > +      grub_free (symlink);
> > +      return 0;
> > +    }
> > +
> > +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> > +    {
> > +      char *filename;
> > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > +      enum FILE_TYPE ftype;
> > +      int name_len;
> > +      int ret;
> > +
> > +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> > +        {
> > +          i++;
> > +          continue;
> > +        }
> > +
> > +      ftype = ctx->dentry[i].file_type;
> > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > +      filename = grub_malloc (name_len + 1);
> > +      if (!filename)
> > +        return 0;
> > +
> > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > +      filename[name_len] = 0;
> > +
> > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > +      if (!fdiro)
> > +        {
> > +          grub_free(filename);
> > +          return 0;
> > +        }
> > +
> > +      if (ftype == F2FS_FT_DIR)
> > +        type = GRUB_FSHELP_DIR;
> > +      else if (ftype == F2FS_FT_SYMLINK)
> > +        type = GRUB_FSHELP_SYMLINK;
> > +      else if (ftype == F2FS_FT_REG_FILE)
> > +        type = GRUB_FSHELP_REG;
> > +
> > +      fdiro->data = ctx->data;
> > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > +      fdiro->inode_read = 0;
> > +
> > +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> > +      grub_free(filename);
> > +      if (ret)
> > +        return 1;
> > +
> > +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> > +
> > +  ctx->bitmap = de_blk->dentry_bitmap;
> > +  ctx->dentry = de_blk->dentry;
> > +  ctx->filename = de_blk->filename;
> > +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> > +  struct grub_f2fs_inode *inode;
> > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > +    .data = diro->data,
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  grub_off_t fpos = 0;
> > +
> > +  if (!diro->inode_read)
> > +    {
> > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > +      if (grub_errno)
> > +     return 0;
> > +    }
> > +
> > +  inode = &diro->inode.i;
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> > +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> > +
> > +  while (fpos < grub_f2fs_file_size (inode))
> > +    {
> > +      struct grub_f2fs_dentry_block *de_blk;
> > +      char *buf;
> > +      int ret;
> > +
> > +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> > +
> > +      ctx.bitmap = de_blk->dentry_bitmap;
> > +      ctx.dentry = de_blk->dentry;
> > +      ctx.filename = de_blk->filename;
> > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > +
> > +      ret = grub_f2fs_check_dentries (&ctx);
> > +      grub_free (buf);
> > +      if (ret)
> > +        return 1;
> > +
> > +      fpos += F2FS_BLKSIZE;
> > +    }
> > +  return 0;
> > +}
> > +
> > +static int
> > +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> filetype,
> > +           grub_fshelp_node_t node, void *data)
> > +{
> > +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> > +      grub_errno = GRUB_ERR_NONE;
> > +    }
> > +  if (node->inode_read)
> > +    {
> > +      info.mtimeset = 1;
> > +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> > +    }
> > +
> > +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> > +  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 = {
> > +    .hook = hook,
> > +    .hook_data = hook_data
> > +  };
> > +  struct grub_fshelp_node *fdiro = 0;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  ctx.data = 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 != &ctx.data->diropen)
> > +    grub_free (fdiro);
> > + 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 = NULL;
> > +  struct grub_fshelp_node *fdiro = 0;
> > +  struct grub_f2fs_inode *inode;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = 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, sizeof (*data->inode));
> > +  grub_free (fdiro);
> > +
> > +  inode = &(data->inode->i);
> > +  file->size = grub_f2fs_file_size (inode);
> > +  file->data = data;
> > +  file->offset = 0;
> > +
> > +  if (inode->i_inline & F2FS_INLINE_DATA && file->size >
> MAX_INLINE_DATA)
> > +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> > +  return 0;
> > +
> > +fail:
> > +  if (fdiro != &data->diropen)
> > +    grub_free (fdiro);
> > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > +
> > +  grub_free (data);
> > +
> > +  grub_dl_unref (my_mod);
> > +
> > +  return GRUB_ERR_NONE;
> > +}
> > +
> > +static grub_uint8_t *
> > +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> > +{
> > +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> > +  grub_uint8_t *out_buf;
> > +  int len = 0;
> > +
> > +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> > +  if (!out_buf)
> > +    return NULL;
> > +
> > +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> > +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> > +    len++;
> > +  }
> > +
> > +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> > +  return out_buf;
> > +}
> > +
> > +static grub_err_t
> > +grub_f2fs_label (grub_device_t device, char **label)
> > +{
> > +  struct grub_f2fs_data *data;
> > +  grub_disk_t disk = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    *label = (char *) grub_f2fs_utf16_to_utf8
> (data->sblock.volume_name);
> > +  else
> > +    *label = NULL;
> > +
> > +  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 = device->disk;
> > +
> > +  grub_dl_ref (my_mod);
> > +
> > +  data = grub_f2fs_mount (disk);
> > +  if (data)
> > +    {
> > +      *uuid =
> > +     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 = NULL;
> > +
> > +  grub_free (data);
> > +  grub_dl_unref (my_mod);
> > +  return grub_errno;
> > +}
> > +
> > +static struct grub_fs grub_f2fs_fs = {
> > +  .name = "f2fs",
> > +  .dir = grub_f2fs_dir,
> > +  .open = grub_f2fs_open,
> > +  .read = grub_f2fs_read,
> > +  .close = grub_f2fs_close,
> > +  .label = grub_f2fs_label,
> > +  .uuid = grub_f2fs_uuid,
> > +#ifdef GRUB_UTIL
> > +  .reserved_first_sector = 1,
> > +  .blocklist_install = 0,
> > +#endif
> > +  .next = 0
> > +};
> > +
> > +GRUB_MOD_INIT (f2fs)
> > +{
> > +  grub_fs_register (&grub_f2fs_fs);
> > +  my_mod = mod;
> > +}
> > +
> > +GRUB_MOD_FINI (f2fs)
> > +{
> > +  grub_fs_unregister (&grub_f2fs_fs);
> > +}
> > diff --git a/po/exclude.pot b/po/exclude.pot
> > index 0a9b215..816089c 100644
> > --- a/po/exclude.pot
> > +++ b/po/exclude.pot
> > @@ -1214,6 +1214,7 @@ msgstr ""
> >
> >  #: 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..1ea77c8
> > --- /dev/null
> > +++ b/tests/f2fs_test.in
> > @@ -0,0 +1,19 @@
> > +#!/bin/sh
> > +
> > +set -e
> > +
> > +if [ "x$EUID" = "x" ] ; then
> > + EUID=`id -u`
> > +fi
> > +
> > +if [ "$EUID" != 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 424de22..e3e4109 100644
> > --- a/tests/util/grub-fs-tester.in
> > +++ b/tests/util/grub-fs-tester.in
> > @@ -142,7 +142,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >       xsquash*)
> >           MINBLKSIZE=4096
> >           MAXBLKSIZE=1048576;;
> > -     xxfs)
> > +     xxfs|xf2fs)
> >           MINBLKSIZE=$SECSIZE
> >               # OS Limitation: GNU/Linux doesn't accept > 4096
> >           MAXBLKSIZE=4096;;
> > @@ -265,6 +265,10 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >               x"btrfs"*)
> >                   FSLABEL="grub_;/testé莭莽😁киритi
> urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> >
> > +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> > +             x"f2fs")
> > +
>  FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> > +
> >           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >               x"exfat")
> >                   FSLABEL="géт ;/莭莽😁кир";;
> > @@ -474,7 +478,7 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> Check it.
> >       # FS LIMITATION: as far as I know those FS don't store their last
> modification date.
> >               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"*
> | x"reiserfs_old" | x"reiserfs" \
> > -                 | 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=y;;
> > @@ -753,6 +757,8 @@ for
> ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >                   MOUNTFS=ext2
> >                   "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)
> >
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 63675 bytes --]

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [2.02] Re:  [PATCH v7] F2FS support
  2016-03-02 23:20                   ` Michael Zimmermann
@ 2016-03-03 21:35                     ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-03-03 21:35 UTC (permalink / raw)
  To: Michael Zimmermann; +Cc: The development of GNU GRUB, linux-f2fs-devel

On Thu, Mar 03, 2016 at 12:20:30AM +0100, Michael Zimmermann wrote:
> I found a bug in the 'grub_f2fs_read_file' function.
> It fails when the file position is not 0. to fix it:
> -      grub_memcpy (buf + pos, inline_addr + pos, len);
> +      grub_memcpy (buf, inline_addr + pos, len);

Oops.
Thank you for taking a look at this.

Thanks,

> 
> 
> Michael
> 
> On Tue, Mar 1, 2016 at 8:52 PM, Andrei Borzenkov <arvidjaar@gmail.com>
> wrote:
> 
> > ACK from my side. It is leaf code that does not affect anything so
> > should not cause any regression.
> >
> > 22.02.2016 21:25, Jaegeuk Kim пишет:
> > > Chang log from v6:
> > >  o remove redundant filesize check in ->read
> > >  o check consistency in ->open regarding to inline_data
> > >
> > > -- >8 --
> > > From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
> > > From: Jaegeuk Kim <jaegeuk@kernel.org>
> > > Date: Tue, 17 Nov 2015 12:45:13 -0800
> > > Subject: [PATCH] F2FS support
> > >
> > > "F2FS (Flash-Friendly File System) is flash-friendly file system which
> > was merged
> > > into Linux kernel v3.8 in 2013.
> > >
> > > The motive for F2FS was to build a file system that from the start,
> > takes into
> > > account the characteristics of NAND flash memory-based storage devices
> > (such as
> > > solid-state disks, eMMC, and SD cards).
> > >
> > > F2FS was designed on a basis of a log-structured file system approach,
> > which
> > > remedies some known issues of the older log structured file systems,
> > such as
> > > the snowball effect of wandering trees and high cleaning overhead. In
> > addition,
> > > since a NAND-based storage device shows different characteristics
> > according to
> > > its internal geometry or flash memory management scheme (such as the
> > Flash
> > > Translation Layer or FTL), it supports various parameters not only for
> > > configuring on-disk layout, but also for selecting allocation and
> > cleaning
> > > algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> > >
> > > The source codes for F2FS are available from:
> > >
> > > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> > >
> > > Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> > > ---
> > >  Makefile.util.def            |    7 +
> > >  docs/grub.texi               |    5 +-
> > >  grub-core/Makefile.core.def  |    5 +
> > >  grub-core/fs/f2fs.c          | 1289
> > ++++++++++++++++++++++++++++++++++++++++++
> > >  po/exclude.pot               |    1 +
> > >  tests/f2fs_test.in           |   19 +
> > >  tests/util/grub-fs-tester.in |   10 +-
> > >  7 files changed, 1332 insertions(+), 4 deletions(-)
> > >  create mode 100644 grub-core/fs/f2fs.c
> > >  create mode 100644 tests/f2fs_test.in
> > >
> > > diff --git a/Makefile.util.def b/Makefile.util.def
> > > index db7e8ec..84627bb 100644
> > > --- a/Makefile.util.def
> > > +++ b/Makefile.util.def
> > > @@ -99,6 +99,7 @@ library = {
> > >    common = grub-core/fs/ext2.c;
> > >    common = grub-core/fs/fat.c;
> > >    common = grub-core/fs/exfat.c;
> > > +  common = grub-core/fs/f2fs.c;
> > >    common = grub-core/fs/fshelp.c;
> > >    common = grub-core/fs/hfs.c;
> > >    common = grub-core/fs/hfsplus.c;
> > > @@ -766,6 +767,12 @@ script = {
> > >
> > >  script = {
> > >    testcase;
> > > +  name = f2fs_test;
> > > +  common = tests/f2fs_test.in;
> > > +};
> > > +
> > > +script = {
> > > +  testcase;
> > >    name = nilfs2_test;
> > >    common = tests/nilfs2_test.in;
> > >  };
> > > diff --git a/docs/grub.texi b/docs/grub.texi
> > > index 1df3db2..e5a80f3 100644
> > > --- a/docs/grub.texi
> > > +++ b/docs/grub.texi
> > > @@ -360,7 +360,8 @@ blocklist notation. The currently supported
> > filesystem 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},
> > @dfn{HFS},
> > > +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> > > +@dfn{f2fs}, @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},
> > > @@ -5347,7 +5348,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,
> > minix,
> > >  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
> > charset
> > >  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 d9fa0e3..b585ade 100644
> > > --- a/grub-core/Makefile.core.def
> > > +++ b/grub-core/Makefile.core.def
> > > @@ -1278,6 +1278,11 @@ module = {
> > >  };
> > >
> > >  module = {
> > > +  name = f2fs;
> > > +  common = fs/f2fs.c;
> > > +};
> > > +
> > > +module = {
> > >    name = fshelp;
> > >    common = fs/fshelp.c;
> > >  };
> > > diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> > > new file mode 100644
> > > index 0000000..cb312b3
> > > --- /dev/null
> > > +++ b/grub-core/fs/f2fs.c
> > > @@ -0,0 +1,1289 @@
> > > +/*
> > > + *  f2fs.c - Flash-Friendly File System
> > > + *
> > > + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> > > + *
> > > + *  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 <http://www.gnu.org/licenses/>.
> > > + */
> > > +#include <grub/err.h>
> > > +#include <grub/file.h>
> > > +#include <grub/mm.h>
> > > +#include <grub/misc.h>
> > > +#include <grub/disk.h>
> > > +#include <grub/dl.h>
> > > +#include <grub/types.h>
> > > +#include <grub/charset.h>
> > > +#include <grub/fshelp.h>
> > > +
> > > +GRUB_MOD_LICENSE ("GPLv3+");
> > > +
> > > +/* F2FS Magic Number */
> > > +#define F2FS_SUPER_MAGIC     0xF2F52010
> > > +#define CHECKSUM_OFFSET              4092            /* must be aligned
> > 4 bytes */
> > > +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> > > +
> > > +/* byte-size offset */
> > > +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> > > +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >>
> > GRUB_DISK_SECTOR_BITS)
> > > +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> > > +                                             GRUB_DISK_SECTOR_BITS)
> > > +
> > > +/* 9 bits for 512 bytes */
> > > +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> > > +
> > > +/* support only 4KB block */
> > > +#define F2FS_BLK_BITS        12
> > > +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> > > +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> > > +
> > > +#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  (sizeof(struct grub_f2fs_nat_jent))
> > > +#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 (sizeof(struct grub_f2fs_nat_entry))
> > > +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> > > +
> > > +#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
> > > +
> > > +#define CKPT_FLAG_SET(ckpt, f)       \
> > > +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> > > +
> > > +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> > > +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> > > +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> > > +#define F2FS_DATA_EXIST              0x08    /* file inline data exist
> > flag */
> > > +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot
> > dentries */
> > > +
> > > +enum FILE_TYPE
> > > +{
> > > +  F2FS_FT_UNKNOWN,
> > > +  F2FS_FT_REG_FILE = 1,
> > > +  F2FS_FT_DIR = 2,
> > > +  F2FS_FT_SYMLINK = 7,
> > > +};
> > > +
> > > +#define MAX_VOLUME_NAME              512
> > > +
> > > +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[MAX_VOLUME_NAME];
> > > +  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;
> > > +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE
> > totally */
> > > +  };
> > > +  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_uint8_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
> > > +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > > +{
> > > +  return addr[nr >> 3] & (1 << (nr & 7));
> > > +}
> > > +
> > > +static inline char *
> > > +__inline_addr (struct grub_f2fs_inode *inode)
> > > +{
> > > +  return (char *)&inode->i_addr[1];
> > > +}
> > > +
> > > +static inline grub_uint64_t
> > > +grub_f2fs_file_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 = &data->ckpt;
> > > +  grub_uint32_t start_addr = data->cp_blkaddr;
> > > +
> > > +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> > > +
> > > +  return __start_cp_addr (data) +
> > > +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> > > +     - (base + 1) + type;
> > > +}
> > > +
> > > +static inline void *
> > > +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> > > +{
> > > +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> > > +  grub_uint32_t offset;
> > > +
> > > +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> > > +    return ckpt->sit_nat_version_bitmap;
> > > +
> > > +  offset = 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 inode_block)
> > > +{
> > > +  if (inode_block)
> > > +    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,
> > > +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> > > +             0, F2FS_BLKSIZE, buf);
> > > +}
> > > +
> > > +/*
> > > + * CRC32
> > > +*/
> > > +#define CRCPOLY_LE 0xedb88320
> > > +
> > > +static inline grub_uint32_t
> > > +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> > > +{
> > > +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> > > +  unsigned char *p = (unsigned char *)buf;
> > > +  grub_uint32_t tmp = len;
> > > +  int i;
> > > +
> > > +  while (tmp--)
> > > +    {
> > > +      crc ^= *p++;
> > > +      for (i = 0; i < 8; i++)
> > > +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> > > +    }
> > > +  return crc;
> > > +}
> > > +
> > > +static inline int
> > > +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> > grub_uint32_t len)
> > > +{
> > > +  grub_uint32_t cal_crc = 0;
> > > +
> > > +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> > > +
> > > +  return (cal_crc == blk_crc) ? 1 : 0;
> > > +}
> > > +
> > > +static inline int
> > > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > > +{
> > > +  int mask;
> > > +
> > > +  p += (nr >> 3);
> > > +  mask = 1 << (7 - (nr & 0x07));
> > > +  return mask & *p;
> > > +}
> > > +
> > > +static int
> > > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > > +{
> > > +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > > +
> > > +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > > +    return -1;
> > > +
> > > +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time
> > (F2FS_BLK_BITS))
> > > +    return -1;
> > > +
> > > +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > > +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> > > +
> > > +  if (log_sectorsize > F2FS_BLK_BITS)
> > > +    return -1;
> > > +
> > > +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > > +    return -1;
> > > +
> > > +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> > > +    return -1;
> > > +
> > > +  return 0;
> > > +}
> > > +
> > > +static int
> > > +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> > > +{
> > > +  grub_disk_t disk = data->disk;
> > > +  grub_err_t err;
> > > +
> > > +  /* Read first super block. */
> > > +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> > &data->sblock);
> > > +  if (err)
> > > +    return -1;
> > > +
> > > +  return grub_f2fs_sanity_check_sb (&data->sblock);
> > > +}
> > > +
> > > +static void *
> > > +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> > > +     grub_uint64_t *version)
> > > +{
> > > +  grub_uint32_t *cp_page_1, *cp_page_2;
> > > +  struct grub_f2fs_checkpoint *cp_block;
> > > +  grub_uint64_t cur_version = 0, pre_version = 0;
> > > +  grub_uint32_t crc = 0;
> > > +  grub_uint32_t crc_offset;
> > > +  grub_err_t err;
> > > +
> > > +  /* Read the 1st cp block in this CP pack */
> > > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!cp_page_1)
> > > +    return NULL;
> > > +
> > > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > > +  if (err)
> > > +    goto invalid_cp1;
> > > +
> > > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > > +  if (crc_offset != CHECKSUM_OFFSET)
> > > +    goto invalid_cp1;
> > > +
> > > +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> > > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > > +    goto invalid_cp1;
> > > +
> > > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > > +
> > > +  /* Read the 2nd cp block in this CP pack */
> > > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!cp_page_2)
> > > +    goto invalid_cp1;
> > > +
> > > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > > +
> > > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > > +  if (err)
> > > +    goto invalid_cp2;
> > > +
> > > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > > +  if (crc_offset != CHECKSUM_OFFSET)
> > > +    goto invalid_cp2;
> > > +
> > > +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> > > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > > +    goto invalid_cp2;
> > > +
> > > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > > +  if (cur_version == pre_version)
> > > +    {
> > > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > > +  cp1 = 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 += data->blocks_per_seg;
> > > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > > +  if (!cp2 && grub_errno)
> > > +    {
> > > +      grub_free (cp1);
> > > +      return grub_errno;
> > > +    }
> > > +
> > > +  if (cp1 && cp2)
> > > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > > +  else if (cp1)
> > > +    cur_page = cp1;
> > > +  else if (cp2)
> > > +    cur_page = cp2;
> > > +  else
> > > +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> > > +
> > > +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> > > +
> > > +  grub_free (cp1);
> > > +  grub_free (cp2);
> > > +  return 0;
> > > +}
> > > +
> > > +static grub_err_t
> > > +get_nat_journal (struct grub_f2fs_data *data)
> > > +{
> > > +  grub_uint32_t block;
> > > +  char *buf;
> > > +  grub_err_t err;
> > > +
> > > +  buf = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!buf)
> > > +    return grub_errno;
> > > +
> > > +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> > > +    block = __start_sum_block (data);
> > > +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> > > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > > +  else
> > > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > > +
> > > +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> > > +  grub_uint32_t blkaddr = 0;
> > > +  grub_uint16_t i;
> > > +
> > > +  for (i = 0; i < n; i++)
> > > +    {
> > > +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> > > +        {
> > > +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> > > +  if (blkaddr)
> > > +    return blkaddr;
> > > +
> > > +  nat_block = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!nat_block)
> > > +    return 0;
> > > +
> > > +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> > > +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> > > +
> > > +  seg_off = block_off / data->blocks_per_seg;
> > > +  block_addr = 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 += data->blocks_per_seg;
> > > +
> > > +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> > > +  if (err)
> > > +    {
> > > +      grub_free (nat_block);
> > > +      return 0;
> > > +    }
> > > +
> > > +  blkaddr = 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 = ADDRS_PER_BLOCK;
> > > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > > +  int n = 0;
> > > +  int level = 0;
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_XATTR)
> > > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > > +
> > > +  noffset[0] = 0;
> > > +
> > > +  if (block < direct_index)
> > > +    {
> > > +      offset[n] = block;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= direct_index;
> > > +  if (block < direct_blks)
> > > +    {
> > > +      offset[n++] = NODE_DIR1_BLOCK;
> > > +      noffset[n] = 1;
> > > +      offset[n] = block;
> > > +      level = 1;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= direct_blks;
> > > +  if (block < direct_blks)
> > > +    {
> > > +      offset[n++] = NODE_DIR2_BLOCK;
> > > +      noffset[n] = 2;
> > > +      offset[n] = block;
> > > +      level = 1;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= direct_blks;
> > > +  if (block < indirect_blks)
> > > +    {
> > > +      offset[n++] = NODE_IND1_BLOCK;
> > > +      noffset[n] = 3;
> > > +      offset[n++] = block / direct_blks;
> > > +      noffset[n] = 4 + offset[n - 1];
> > > +      offset[n] = block % direct_blks;
> > > +      level = 2;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= indirect_blks;
> > > +  if (block < indirect_blks)
> > > +    {
> > > +      offset[n++] = NODE_IND2_BLOCK;
> > > +      noffset[n] = 4 + dptrs_per_blk;
> > > +      offset[n++] = block / direct_blks;
> > > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > > +      offset[n] = block % direct_blks;
> > > +      level = 2;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= indirect_blks;
> > > +  if (block < dindirect_blks)
> > > +    {
> > > +      offset[n++] = NODE_DIND_BLOCK;
> > > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > > +      offset[n++] = block / indirect_blks;
> > > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > > +      offset[n - 1] * (dptrs_per_blk + 1);
> > > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > > +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> > > +      offset[n] = block % direct_blks;
> > > +      level = 3;
> > > +      goto got;
> > > +    }
> > > +got:
> > > +  return level;
> > > +}
> > > +
> > > +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 = 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 = grub_malloc (sizeof (*data));
> > > +  if (!data)
> > > +    return NULL;
> > > +
> > > +  data->disk = disk;
> > > +
> > > +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> > > +    {
> > > +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> > > +        {
> > > +          if (grub_errno == GRUB_ERR_NONE)
> > > +            grub_error (GRUB_ERR_BAD_FS,
> > > +                     "not a F2FS filesystem (no superblock)");
> > > +          goto fail;
> > > +        }
> > > +    }
> > > +
> > > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > > +  data->blocks_per_seg = 1 <<
> > > +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > > +
> > > +  err = grub_f2fs_read_cp (data);
> > > +  if (err)
> > > +    goto fail;
> > > +
> > > +  data->nat_bitmap = __nat_bitmap_ptr (data);
> > > +
> > > +  err = get_nat_journal (data);
> > > +  if (err)
> > > +    goto fail;
> > > +
> > > +  data->diropen.data = data;
> > > +  data->diropen.ino = data->root_ino;
> > > +  data->diropen.inode_read = 1;
> > > +  data->inode = &data->diropen.inode;
> > > +
> > > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > > +  if (err)
> > > +    goto fail;
> > > +
> > > +  return data;
> > > +
> > > +fail:
> > > +  grub_free (data);
> > > +  return NULL;
> > > +}
> > > +
> > > +/* guarantee inline_data was handled by caller */
> > > +static grub_disk_addr_t
> > > +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t
> > block_ofs)
> > > +{
> > > +  struct grub_f2fs_data *data = node->data;
> > > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > > +  grub_uint32_t offset[4], noffset[4], nids[4];
> > > +  struct grub_f2fs_node *node_block;
> > > +  grub_uint32_t block_addr = -1;
> > > +  int level, i;
> > > +
> > > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > > +  if (level == 0)
> > > +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > > +
> > > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!node_block)
> > > +    return -1;
> > > +
> > > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > > +
> > > +  /* get indirect or direct nodes */
> > > +  for (i = 1; i <= level; i++)
> > > +    {
> > > +      grub_f2fs_read_node (data, nids[i], node_block);
> > > +      if (grub_errno)
> > > +        goto fail;
> > > +
> > > +      if (i < level)
> > > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > > +    }
> > > +
> > > +  block_addr = 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 = &node->inode.i;
> > > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > > +  char *inline_addr = __inline_addr (inode);
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > > +    {
> > > +      if (filesize > MAX_INLINE_DATA)
> > > +        return -1;
> > > +      if (len > filesize - pos)
> > > +        len = 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_get_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 = node;
> > > +  grub_uint64_t filesize;
> > > +
> > > +  if (!diro->inode_read)
> > > +    {
> > > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > > +      if (grub_errno)
> > > +        return 0;
> > > +    }
> > > +
> > > +  filesize = grub_f2fs_file_size(&diro->inode.i);
> > > +
> > > +  symlink = grub_malloc (filesize + 1);
> > > +  if (!symlink)
> > > +    return 0;
> > > +
> > > +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> > > +  if (grub_errno)
> > > +    {
> > > +      grub_free (symlink);
> > > +      return 0;
> > > +    }
> > > +
> > > +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> > > +    {
> > > +      char *filename;
> > > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > > +      enum FILE_TYPE ftype;
> > > +      int name_len;
> > > +      int ret;
> > > +
> > > +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> > > +        {
> > > +          i++;
> > > +          continue;
> > > +        }
> > > +
> > > +      ftype = ctx->dentry[i].file_type;
> > > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > > +      filename = grub_malloc (name_len + 1);
> > > +      if (!filename)
> > > +        return 0;
> > > +
> > > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > > +      filename[name_len] = 0;
> > > +
> > > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > > +      if (!fdiro)
> > > +        {
> > > +          grub_free(filename);
> > > +          return 0;
> > > +        }
> > > +
> > > +      if (ftype == F2FS_FT_DIR)
> > > +        type = GRUB_FSHELP_DIR;
> > > +      else if (ftype == F2FS_FT_SYMLINK)
> > > +        type = GRUB_FSHELP_SYMLINK;
> > > +      else if (ftype == F2FS_FT_REG_FILE)
> > > +        type = GRUB_FSHELP_REG;
> > > +
> > > +      fdiro->data = ctx->data;
> > > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > > +      fdiro->inode_read = 0;
> > > +
> > > +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> > > +      grub_free(filename);
> > > +      if (ret)
> > > +        return 1;
> > > +
> > > +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> > > +
> > > +  ctx->bitmap = de_blk->dentry_bitmap;
> > > +  ctx->dentry = de_blk->dentry;
> > > +  ctx->filename = de_blk->filename;
> > > +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> > > +  struct grub_f2fs_inode *inode;
> > > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > > +    .data = diro->data,
> > > +    .hook = hook,
> > > +    .hook_data = hook_data
> > > +  };
> > > +  grub_off_t fpos = 0;
> > > +
> > > +  if (!diro->inode_read)
> > > +    {
> > > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > > +      if (grub_errno)
> > > +     return 0;
> > > +    }
> > > +
> > > +  inode = &diro->inode.i;
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> > > +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> > > +
> > > +  while (fpos < grub_f2fs_file_size (inode))
> > > +    {
> > > +      struct grub_f2fs_dentry_block *de_blk;
> > > +      char *buf;
> > > +      int ret;
> > > +
> > > +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> > > +
> > > +      ctx.bitmap = de_blk->dentry_bitmap;
> > > +      ctx.dentry = de_blk->dentry;
> > > +      ctx.filename = de_blk->filename;
> > > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > > +
> > > +      ret = grub_f2fs_check_dentries (&ctx);
> > > +      grub_free (buf);
> > > +      if (ret)
> > > +        return 1;
> > > +
> > > +      fpos += F2FS_BLKSIZE;
> > > +    }
> > > +  return 0;
> > > +}
> > > +
> > > +static int
> > > +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> > filetype,
> > > +           grub_fshelp_node_t node, void *data)
> > > +{
> > > +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> > > +      grub_errno = GRUB_ERR_NONE;
> > > +    }
> > > +  if (node->inode_read)
> > > +    {
> > > +      info.mtimeset = 1;
> > > +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> > > +    }
> > > +
> > > +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> > > +  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 = {
> > > +    .hook = hook,
> > > +    .hook_data = hook_data
> > > +  };
> > > +  struct grub_fshelp_node *fdiro = 0;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  ctx.data = 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 != &ctx.data->diropen)
> > > +    grub_free (fdiro);
> > > + 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 = NULL;
> > > +  struct grub_fshelp_node *fdiro = 0;
> > > +  struct grub_f2fs_inode *inode;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  data = 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, sizeof (*data->inode));
> > > +  grub_free (fdiro);
> > > +
> > > +  inode = &(data->inode->i);
> > > +  file->size = grub_f2fs_file_size (inode);
> > > +  file->data = data;
> > > +  file->offset = 0;
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_DATA && file->size >
> > MAX_INLINE_DATA)
> > > +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> > > +  return 0;
> > > +
> > > +fail:
> > > +  if (fdiro != &data->diropen)
> > > +    grub_free (fdiro);
> > > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > > +
> > > +  grub_free (data);
> > > +
> > > +  grub_dl_unref (my_mod);
> > > +
> > > +  return GRUB_ERR_NONE;
> > > +}
> > > +
> > > +static grub_uint8_t *
> > > +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> > > +{
> > > +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> > > +  grub_uint8_t *out_buf;
> > > +  int len = 0;
> > > +
> > > +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> > > +  if (!out_buf)
> > > +    return NULL;
> > > +
> > > +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> > > +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> > > +    len++;
> > > +  }
> > > +
> > > +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> > > +  return out_buf;
> > > +}
> > > +
> > > +static grub_err_t
> > > +grub_f2fs_label (grub_device_t device, char **label)
> > > +{
> > > +  struct grub_f2fs_data *data;
> > > +  grub_disk_t disk = device->disk;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  data = grub_f2fs_mount (disk);
> > > +  if (data)
> > > +    *label = (char *) grub_f2fs_utf16_to_utf8
> > (data->sblock.volume_name);
> > > +  else
> > > +    *label = NULL;
> > > +
> > > +  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 = device->disk;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  data = grub_f2fs_mount (disk);
> > > +  if (data)
> > > +    {
> > > +      *uuid =
> > > +     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 = NULL;
> > > +
> > > +  grub_free (data);
> > > +  grub_dl_unref (my_mod);
> > > +  return grub_errno;
> > > +}
> > > +
> > > +static struct grub_fs grub_f2fs_fs = {
> > > +  .name = "f2fs",
> > > +  .dir = grub_f2fs_dir,
> > > +  .open = grub_f2fs_open,
> > > +  .read = grub_f2fs_read,
> > > +  .close = grub_f2fs_close,
> > > +  .label = grub_f2fs_label,
> > > +  .uuid = grub_f2fs_uuid,
> > > +#ifdef GRUB_UTIL
> > > +  .reserved_first_sector = 1,
> > > +  .blocklist_install = 0,
> > > +#endif
> > > +  .next = 0
> > > +};
> > > +
> > > +GRUB_MOD_INIT (f2fs)
> > > +{
> > > +  grub_fs_register (&grub_f2fs_fs);
> > > +  my_mod = mod;
> > > +}
> > > +
> > > +GRUB_MOD_FINI (f2fs)
> > > +{
> > > +  grub_fs_unregister (&grub_f2fs_fs);
> > > +}
> > > diff --git a/po/exclude.pot b/po/exclude.pot
> > > index 0a9b215..816089c 100644
> > > --- a/po/exclude.pot
> > > +++ b/po/exclude.pot
> > > @@ -1214,6 +1214,7 @@ msgstr ""
> > >
> > >  #: 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..1ea77c8
> > > --- /dev/null
> > > +++ b/tests/f2fs_test.in
> > > @@ -0,0 +1,19 @@
> > > +#!/bin/sh
> > > +
> > > +set -e
> > > +
> > > +if [ "x$EUID" = "x" ] ; then
> > > + EUID=`id -u`
> > > +fi
> > > +
> > > +if [ "$EUID" != 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 424de22..e3e4109 100644
> > > --- a/tests/util/grub-fs-tester.in
> > > +++ b/tests/util/grub-fs-tester.in
> > > @@ -142,7 +142,7 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >       xsquash*)
> > >           MINBLKSIZE=4096
> > >           MAXBLKSIZE=1048576;;
> > > -     xxfs)
> > > +     xxfs|xf2fs)
> > >           MINBLKSIZE=$SECSIZE
> > >               # OS Limitation: GNU/Linux doesn't accept > 4096
> > >           MAXBLKSIZE=4096;;
> > > @@ -265,6 +265,10 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >               x"btrfs"*)
> > >                   FSLABEL="grub_;/testé莭莽😁киритi
> > urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> > >
> > > +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> > > +             x"f2fs")
> > > +
> >  FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> > > +
> > >           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> > >               x"exfat")
> > >                   FSLABEL="géт ;/莭莽😁кир";;
> > > @@ -474,7 +478,7 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> > Check it.
> > >       # FS LIMITATION: as far as I know those FS don't store their last
> > modification date.
> > >               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"*
> > | x"reiserfs_old" | x"reiserfs" \
> > > -                 | 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=y;;
> > > @@ -753,6 +757,8 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> > >                   MOUNTFS=ext2
> > >                   "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)
> > >
> >
> >
> > _______________________________________________
> > Grub-devel mailing list
> > Grub-devel@gnu.org
> > https://lists.gnu.org/mailman/listinfo/grub-devel
> >

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [2.02] Re: [f2fs-dev] [PATCH v7] F2FS support
@ 2016-03-03 21:35                     ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-03-03 21:35 UTC (permalink / raw)
  To: Michael Zimmermann; +Cc: The development of GNU GRUB, linux-f2fs-devel

On Thu, Mar 03, 2016 at 12:20:30AM +0100, Michael Zimmermann wrote:
> I found a bug in the 'grub_f2fs_read_file' function.
> It fails when the file position is not 0. to fix it:
> -      grub_memcpy (buf + pos, inline_addr + pos, len);
> +      grub_memcpy (buf, inline_addr + pos, len);

Oops.
Thank you for taking a look at this.

Thanks,

> 
> 
> Michael
> 
> On Tue, Mar 1, 2016 at 8:52 PM, Andrei Borzenkov <arvidjaar@gmail.com>
> wrote:
> 
> > ACK from my side. It is leaf code that does not affect anything so
> > should not cause any regression.
> >
> > 22.02.2016 21:25, Jaegeuk Kim пишет:
> > > Chang log from v6:
> > >  o remove redundant filesize check in ->read
> > >  o check consistency in ->open regarding to inline_data
> > >
> > > -- >8 --
> > > From 51c120612fdbc4bc315e9c4c6e91ca888c3f3596 Mon Sep 17 00:00:00 2001
> > > From: Jaegeuk Kim <jaegeuk@kernel.org>
> > > Date: Tue, 17 Nov 2015 12:45:13 -0800
> > > Subject: [PATCH] F2FS support
> > >
> > > "F2FS (Flash-Friendly File System) is flash-friendly file system which
> > was merged
> > > into Linux kernel v3.8 in 2013.
> > >
> > > The motive for F2FS was to build a file system that from the start,
> > takes into
> > > account the characteristics of NAND flash memory-based storage devices
> > (such as
> > > solid-state disks, eMMC, and SD cards).
> > >
> > > F2FS was designed on a basis of a log-structured file system approach,
> > which
> > > remedies some known issues of the older log structured file systems,
> > such as
> > > the snowball effect of wandering trees and high cleaning overhead. In
> > addition,
> > > since a NAND-based storage device shows different characteristics
> > according to
> > > its internal geometry or flash memory management scheme (such as the
> > Flash
> > > Translation Layer or FTL), it supports various parameters not only for
> > > configuring on-disk layout, but also for selecting allocation and
> > cleaning
> > > algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> > >
> > > The source codes for F2FS are available from:
> > >
> > > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > > http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> > >
> > > Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> > > ---
> > >  Makefile.util.def            |    7 +
> > >  docs/grub.texi               |    5 +-
> > >  grub-core/Makefile.core.def  |    5 +
> > >  grub-core/fs/f2fs.c          | 1289
> > ++++++++++++++++++++++++++++++++++++++++++
> > >  po/exclude.pot               |    1 +
> > >  tests/f2fs_test.in           |   19 +
> > >  tests/util/grub-fs-tester.in |   10 +-
> > >  7 files changed, 1332 insertions(+), 4 deletions(-)
> > >  create mode 100644 grub-core/fs/f2fs.c
> > >  create mode 100644 tests/f2fs_test.in
> > >
> > > diff --git a/Makefile.util.def b/Makefile.util.def
> > > index db7e8ec..84627bb 100644
> > > --- a/Makefile.util.def
> > > +++ b/Makefile.util.def
> > > @@ -99,6 +99,7 @@ library = {
> > >    common = grub-core/fs/ext2.c;
> > >    common = grub-core/fs/fat.c;
> > >    common = grub-core/fs/exfat.c;
> > > +  common = grub-core/fs/f2fs.c;
> > >    common = grub-core/fs/fshelp.c;
> > >    common = grub-core/fs/hfs.c;
> > >    common = grub-core/fs/hfsplus.c;
> > > @@ -766,6 +767,12 @@ script = {
> > >
> > >  script = {
> > >    testcase;
> > > +  name = f2fs_test;
> > > +  common = tests/f2fs_test.in;
> > > +};
> > > +
> > > +script = {
> > > +  testcase;
> > >    name = nilfs2_test;
> > >    common = tests/nilfs2_test.in;
> > >  };
> > > diff --git a/docs/grub.texi b/docs/grub.texi
> > > index 1df3db2..e5a80f3 100644
> > > --- a/docs/grub.texi
> > > +++ b/docs/grub.texi
> > > @@ -360,7 +360,8 @@ blocklist notation. The currently supported
> > filesystem 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},
> > @dfn{HFS},
> > > +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> > > +@dfn{f2fs}, @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},
> > > @@ -5347,7 +5348,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,
> > minix,
> > >  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
> > charset
> > >  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 d9fa0e3..b585ade 100644
> > > --- a/grub-core/Makefile.core.def
> > > +++ b/grub-core/Makefile.core.def
> > > @@ -1278,6 +1278,11 @@ module = {
> > >  };
> > >
> > >  module = {
> > > +  name = f2fs;
> > > +  common = fs/f2fs.c;
> > > +};
> > > +
> > > +module = {
> > >    name = fshelp;
> > >    common = fs/fshelp.c;
> > >  };
> > > diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> > > new file mode 100644
> > > index 0000000..cb312b3
> > > --- /dev/null
> > > +++ b/grub-core/fs/f2fs.c
> > > @@ -0,0 +1,1289 @@
> > > +/*
> > > + *  f2fs.c - Flash-Friendly File System
> > > + *
> > > + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> > > + *
> > > + *  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 <http://www.gnu.org/licenses/>.
> > > + */
> > > +#include <grub/err.h>
> > > +#include <grub/file.h>
> > > +#include <grub/mm.h>
> > > +#include <grub/misc.h>
> > > +#include <grub/disk.h>
> > > +#include <grub/dl.h>
> > > +#include <grub/types.h>
> > > +#include <grub/charset.h>
> > > +#include <grub/fshelp.h>
> > > +
> > > +GRUB_MOD_LICENSE ("GPLv3+");
> > > +
> > > +/* F2FS Magic Number */
> > > +#define F2FS_SUPER_MAGIC     0xF2F52010
> > > +#define CHECKSUM_OFFSET              4092            /* must be aligned
> > 4 bytes */
> > > +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> > > +
> > > +/* byte-size offset */
> > > +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> > > +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >>
> > GRUB_DISK_SECTOR_BITS)
> > > +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> > > +                                             GRUB_DISK_SECTOR_BITS)
> > > +
> > > +/* 9 bits for 512 bytes */
> > > +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> > > +
> > > +/* support only 4KB block */
> > > +#define F2FS_BLK_BITS        12
> > > +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> > > +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> > > +
> > > +#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  (sizeof(struct grub_f2fs_nat_jent))
> > > +#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 (sizeof(struct grub_f2fs_nat_entry))
> > > +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> > > +
> > > +#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
> > > +
> > > +#define CKPT_FLAG_SET(ckpt, f)       \
> > > +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> > > +
> > > +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> > > +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> > > +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> > > +#define F2FS_DATA_EXIST              0x08    /* file inline data exist
> > flag */
> > > +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot
> > dentries */
> > > +
> > > +enum FILE_TYPE
> > > +{
> > > +  F2FS_FT_UNKNOWN,
> > > +  F2FS_FT_REG_FILE = 1,
> > > +  F2FS_FT_DIR = 2,
> > > +  F2FS_FT_SYMLINK = 7,
> > > +};
> > > +
> > > +#define MAX_VOLUME_NAME              512
> > > +
> > > +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[MAX_VOLUME_NAME];
> > > +  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;
> > > +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE
> > totally */
> > > +  };
> > > +  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_uint8_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
> > > +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > > +{
> > > +  return addr[nr >> 3] & (1 << (nr & 7));
> > > +}
> > > +
> > > +static inline char *
> > > +__inline_addr (struct grub_f2fs_inode *inode)
> > > +{
> > > +  return (char *)&inode->i_addr[1];
> > > +}
> > > +
> > > +static inline grub_uint64_t
> > > +grub_f2fs_file_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 = &data->ckpt;
> > > +  grub_uint32_t start_addr = data->cp_blkaddr;
> > > +
> > > +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> > > +
> > > +  return __start_cp_addr (data) +
> > > +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> > > +     - (base + 1) + type;
> > > +}
> > > +
> > > +static inline void *
> > > +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> > > +{
> > > +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> > > +  grub_uint32_t offset;
> > > +
> > > +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> > > +    return ckpt->sit_nat_version_bitmap;
> > > +
> > > +  offset = 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 inode_block)
> > > +{
> > > +  if (inode_block)
> > > +    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,
> > > +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> > > +             0, F2FS_BLKSIZE, buf);
> > > +}
> > > +
> > > +/*
> > > + * CRC32
> > > +*/
> > > +#define CRCPOLY_LE 0xedb88320
> > > +
> > > +static inline grub_uint32_t
> > > +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> > > +{
> > > +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> > > +  unsigned char *p = (unsigned char *)buf;
> > > +  grub_uint32_t tmp = len;
> > > +  int i;
> > > +
> > > +  while (tmp--)
> > > +    {
> > > +      crc ^= *p++;
> > > +      for (i = 0; i < 8; i++)
> > > +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> > > +    }
> > > +  return crc;
> > > +}
> > > +
> > > +static inline int
> > > +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> > grub_uint32_t len)
> > > +{
> > > +  grub_uint32_t cal_crc = 0;
> > > +
> > > +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> > > +
> > > +  return (cal_crc == blk_crc) ? 1 : 0;
> > > +}
> > > +
> > > +static inline int
> > > +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > > +{
> > > +  int mask;
> > > +
> > > +  p += (nr >> 3);
> > > +  mask = 1 << (7 - (nr & 0x07));
> > > +  return mask & *p;
> > > +}
> > > +
> > > +static int
> > > +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > > +{
> > > +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > > +
> > > +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > > +    return -1;
> > > +
> > > +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time
> > (F2FS_BLK_BITS))
> > > +    return -1;
> > > +
> > > +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > > +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> > > +
> > > +  if (log_sectorsize > F2FS_BLK_BITS)
> > > +    return -1;
> > > +
> > > +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > > +    return -1;
> > > +
> > > +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> > > +    return -1;
> > > +
> > > +  return 0;
> > > +}
> > > +
> > > +static int
> > > +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> > > +{
> > > +  grub_disk_t disk = data->disk;
> > > +  grub_err_t err;
> > > +
> > > +  /* Read first super block. */
> > > +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> > &data->sblock);
> > > +  if (err)
> > > +    return -1;
> > > +
> > > +  return grub_f2fs_sanity_check_sb (&data->sblock);
> > > +}
> > > +
> > > +static void *
> > > +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> > > +     grub_uint64_t *version)
> > > +{
> > > +  grub_uint32_t *cp_page_1, *cp_page_2;
> > > +  struct grub_f2fs_checkpoint *cp_block;
> > > +  grub_uint64_t cur_version = 0, pre_version = 0;
> > > +  grub_uint32_t crc = 0;
> > > +  grub_uint32_t crc_offset;
> > > +  grub_err_t err;
> > > +
> > > +  /* Read the 1st cp block in this CP pack */
> > > +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!cp_page_1)
> > > +    return NULL;
> > > +
> > > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > > +  if (err)
> > > +    goto invalid_cp1;
> > > +
> > > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > > +  if (crc_offset != CHECKSUM_OFFSET)
> > > +    goto invalid_cp1;
> > > +
> > > +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> > > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > > +    goto invalid_cp1;
> > > +
> > > +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > > +
> > > +  /* Read the 2nd cp block in this CP pack */
> > > +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!cp_page_2)
> > > +    goto invalid_cp1;
> > > +
> > > +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> > > +
> > > +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > > +  if (err)
> > > +    goto invalid_cp2;
> > > +
> > > +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > > +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > > +  if (crc_offset != CHECKSUM_OFFSET)
> > > +    goto invalid_cp2;
> > > +
> > > +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> > > +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > > +    goto invalid_cp2;
> > > +
> > > +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > > +  if (cur_version == pre_version)
> > > +    {
> > > +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > > +  cp1 = 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 += data->blocks_per_seg;
> > > +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > > +  if (!cp2 && grub_errno)
> > > +    {
> > > +      grub_free (cp1);
> > > +      return grub_errno;
> > > +    }
> > > +
> > > +  if (cp1 && cp2)
> > > +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > > +  else if (cp1)
> > > +    cur_page = cp1;
> > > +  else if (cp2)
> > > +    cur_page = cp2;
> > > +  else
> > > +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> > > +
> > > +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> > > +
> > > +  grub_free (cp1);
> > > +  grub_free (cp2);
> > > +  return 0;
> > > +}
> > > +
> > > +static grub_err_t
> > > +get_nat_journal (struct grub_f2fs_data *data)
> > > +{
> > > +  grub_uint32_t block;
> > > +  char *buf;
> > > +  grub_err_t err;
> > > +
> > > +  buf = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!buf)
> > > +    return grub_errno;
> > > +
> > > +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> > > +    block = __start_sum_block (data);
> > > +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> > > +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > > +  else
> > > +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> > > +
> > > +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> > > +  grub_uint32_t blkaddr = 0;
> > > +  grub_uint16_t i;
> > > +
> > > +  for (i = 0; i < n; i++)
> > > +    {
> > > +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> > > +        {
> > > +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> > > +  if (blkaddr)
> > > +    return blkaddr;
> > > +
> > > +  nat_block = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!nat_block)
> > > +    return 0;
> > > +
> > > +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> > > +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> > > +
> > > +  seg_off = block_off / data->blocks_per_seg;
> > > +  block_addr = 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 += data->blocks_per_seg;
> > > +
> > > +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> > > +  if (err)
> > > +    {
> > > +      grub_free (nat_block);
> > > +      return 0;
> > > +    }
> > > +
> > > +  blkaddr = 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 = ADDRS_PER_BLOCK;
> > > +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > > +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > > +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > > +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > > +  int n = 0;
> > > +  int level = 0;
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_XATTR)
> > > +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > > +
> > > +  noffset[0] = 0;
> > > +
> > > +  if (block < direct_index)
> > > +    {
> > > +      offset[n] = block;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= direct_index;
> > > +  if (block < direct_blks)
> > > +    {
> > > +      offset[n++] = NODE_DIR1_BLOCK;
> > > +      noffset[n] = 1;
> > > +      offset[n] = block;
> > > +      level = 1;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= direct_blks;
> > > +  if (block < direct_blks)
> > > +    {
> > > +      offset[n++] = NODE_DIR2_BLOCK;
> > > +      noffset[n] = 2;
> > > +      offset[n] = block;
> > > +      level = 1;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= direct_blks;
> > > +  if (block < indirect_blks)
> > > +    {
> > > +      offset[n++] = NODE_IND1_BLOCK;
> > > +      noffset[n] = 3;
> > > +      offset[n++] = block / direct_blks;
> > > +      noffset[n] = 4 + offset[n - 1];
> > > +      offset[n] = block % direct_blks;
> > > +      level = 2;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= indirect_blks;
> > > +  if (block < indirect_blks)
> > > +    {
> > > +      offset[n++] = NODE_IND2_BLOCK;
> > > +      noffset[n] = 4 + dptrs_per_blk;
> > > +      offset[n++] = block / direct_blks;
> > > +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > > +      offset[n] = block % direct_blks;
> > > +      level = 2;
> > > +      goto got;
> > > +    }
> > > +
> > > +  block -= indirect_blks;
> > > +  if (block < dindirect_blks)
> > > +    {
> > > +      offset[n++] = NODE_DIND_BLOCK;
> > > +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > > +      offset[n++] = block / indirect_blks;
> > > +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > > +      offset[n - 1] * (dptrs_per_blk + 1);
> > > +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > > +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > > +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> > > +      offset[n] = block % direct_blks;
> > > +      level = 3;
> > > +      goto got;
> > > +    }
> > > +got:
> > > +  return level;
> > > +}
> > > +
> > > +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 = 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 = grub_malloc (sizeof (*data));
> > > +  if (!data)
> > > +    return NULL;
> > > +
> > > +  data->disk = disk;
> > > +
> > > +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> > > +    {
> > > +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> > > +        {
> > > +          if (grub_errno == GRUB_ERR_NONE)
> > > +            grub_error (GRUB_ERR_BAD_FS,
> > > +                     "not a F2FS filesystem (no superblock)");
> > > +          goto fail;
> > > +        }
> > > +    }
> > > +
> > > +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > > +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > > +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > > +  data->blocks_per_seg = 1 <<
> > > +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > > +
> > > +  err = grub_f2fs_read_cp (data);
> > > +  if (err)
> > > +    goto fail;
> > > +
> > > +  data->nat_bitmap = __nat_bitmap_ptr (data);
> > > +
> > > +  err = get_nat_journal (data);
> > > +  if (err)
> > > +    goto fail;
> > > +
> > > +  data->diropen.data = data;
> > > +  data->diropen.ino = data->root_ino;
> > > +  data->diropen.inode_read = 1;
> > > +  data->inode = &data->diropen.inode;
> > > +
> > > +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > > +  if (err)
> > > +    goto fail;
> > > +
> > > +  return data;
> > > +
> > > +fail:
> > > +  grub_free (data);
> > > +  return NULL;
> > > +}
> > > +
> > > +/* guarantee inline_data was handled by caller */
> > > +static grub_disk_addr_t
> > > +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t
> > block_ofs)
> > > +{
> > > +  struct grub_f2fs_data *data = node->data;
> > > +  struct grub_f2fs_inode *inode = &node->inode.i;
> > > +  grub_uint32_t offset[4], noffset[4], nids[4];
> > > +  struct grub_f2fs_node *node_block;
> > > +  grub_uint32_t block_addr = -1;
> > > +  int level, i;
> > > +
> > > +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > > +  if (level == 0)
> > > +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > > +
> > > +  node_block = grub_malloc (F2FS_BLKSIZE);
> > > +  if (!node_block)
> > > +    return -1;
> > > +
> > > +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > > +
> > > +  /* get indirect or direct nodes */
> > > +  for (i = 1; i <= level; i++)
> > > +    {
> > > +      grub_f2fs_read_node (data, nids[i], node_block);
> > > +      if (grub_errno)
> > > +        goto fail;
> > > +
> > > +      if (i < level)
> > > +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > > +    }
> > > +
> > > +  block_addr = 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 = &node->inode.i;
> > > +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > > +  char *inline_addr = __inline_addr (inode);
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_DATA)
> > > +    {
> > > +      if (filesize > MAX_INLINE_DATA)
> > > +        return -1;
> > > +      if (len > filesize - pos)
> > > +        len = 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_get_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 = node;
> > > +  grub_uint64_t filesize;
> > > +
> > > +  if (!diro->inode_read)
> > > +    {
> > > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > > +      if (grub_errno)
> > > +        return 0;
> > > +    }
> > > +
> > > +  filesize = grub_f2fs_file_size(&diro->inode.i);
> > > +
> > > +  symlink = grub_malloc (filesize + 1);
> > > +  if (!symlink)
> > > +    return 0;
> > > +
> > > +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> > > +  if (grub_errno)
> > > +    {
> > > +      grub_free (symlink);
> > > +      return 0;
> > > +    }
> > > +
> > > +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> > > +    {
> > > +      char *filename;
> > > +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > > +      enum FILE_TYPE ftype;
> > > +      int name_len;
> > > +      int ret;
> > > +
> > > +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> > > +        {
> > > +          i++;
> > > +          continue;
> > > +        }
> > > +
> > > +      ftype = ctx->dentry[i].file_type;
> > > +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > > +      filename = grub_malloc (name_len + 1);
> > > +      if (!filename)
> > > +        return 0;
> > > +
> > > +      grub_memcpy (filename, ctx->filename[i], name_len);
> > > +      filename[name_len] = 0;
> > > +
> > > +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > > +      if (!fdiro)
> > > +        {
> > > +          grub_free(filename);
> > > +          return 0;
> > > +        }
> > > +
> > > +      if (ftype == F2FS_FT_DIR)
> > > +        type = GRUB_FSHELP_DIR;
> > > +      else if (ftype == F2FS_FT_SYMLINK)
> > > +        type = GRUB_FSHELP_SYMLINK;
> > > +      else if (ftype == F2FS_FT_REG_FILE)
> > > +        type = GRUB_FSHELP_REG;
> > > +
> > > +      fdiro->data = ctx->data;
> > > +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > > +      fdiro->inode_read = 0;
> > > +
> > > +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> > > +      grub_free(filename);
> > > +      if (ret)
> > > +        return 1;
> > > +
> > > +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> > > +
> > > +  ctx->bitmap = de_blk->dentry_bitmap;
> > > +  ctx->dentry = de_blk->dentry;
> > > +  ctx->filename = de_blk->filename;
> > > +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> > > +  struct grub_f2fs_inode *inode;
> > > +  struct grub_f2fs_dir_iter_ctx ctx = {
> > > +    .data = diro->data,
> > > +    .hook = hook,
> > > +    .hook_data = hook_data
> > > +  };
> > > +  grub_off_t fpos = 0;
> > > +
> > > +  if (!diro->inode_read)
> > > +    {
> > > +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > > +      if (grub_errno)
> > > +     return 0;
> > > +    }
> > > +
> > > +  inode = &diro->inode.i;
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> > > +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> > > +
> > > +  while (fpos < grub_f2fs_file_size (inode))
> > > +    {
> > > +      struct grub_f2fs_dentry_block *de_blk;
> > > +      char *buf;
> > > +      int ret;
> > > +
> > > +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> > > +
> > > +      ctx.bitmap = de_blk->dentry_bitmap;
> > > +      ctx.dentry = de_blk->dentry;
> > > +      ctx.filename = de_blk->filename;
> > > +      ctx.max = NR_DENTRY_IN_BLOCK;
> > > +
> > > +      ret = grub_f2fs_check_dentries (&ctx);
> > > +      grub_free (buf);
> > > +      if (ret)
> > > +        return 1;
> > > +
> > > +      fpos += F2FS_BLKSIZE;
> > > +    }
> > > +  return 0;
> > > +}
> > > +
> > > +static int
> > > +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> > filetype,
> > > +           grub_fshelp_node_t node, void *data)
> > > +{
> > > +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> > > +      grub_errno = GRUB_ERR_NONE;
> > > +    }
> > > +  if (node->inode_read)
> > > +    {
> > > +      info.mtimeset = 1;
> > > +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> > > +    }
> > > +
> > > +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> > > +  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 = {
> > > +    .hook = hook,
> > > +    .hook_data = hook_data
> > > +  };
> > > +  struct grub_fshelp_node *fdiro = 0;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  ctx.data = 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 != &ctx.data->diropen)
> > > +    grub_free (fdiro);
> > > + 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 = NULL;
> > > +  struct grub_fshelp_node *fdiro = 0;
> > > +  struct grub_f2fs_inode *inode;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  data = 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, sizeof (*data->inode));
> > > +  grub_free (fdiro);
> > > +
> > > +  inode = &(data->inode->i);
> > > +  file->size = grub_f2fs_file_size (inode);
> > > +  file->data = data;
> > > +  file->offset = 0;
> > > +
> > > +  if (inode->i_inline & F2FS_INLINE_DATA && file->size >
> > MAX_INLINE_DATA)
> > > +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> > > +  return 0;
> > > +
> > > +fail:
> > > +  if (fdiro != &data->diropen)
> > > +    grub_free (fdiro);
> > > +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > > +
> > > +  grub_free (data);
> > > +
> > > +  grub_dl_unref (my_mod);
> > > +
> > > +  return GRUB_ERR_NONE;
> > > +}
> > > +
> > > +static grub_uint8_t *
> > > +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> > > +{
> > > +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> > > +  grub_uint8_t *out_buf;
> > > +  int len = 0;
> > > +
> > > +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> > > +  if (!out_buf)
> > > +    return NULL;
> > > +
> > > +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> > > +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> > > +    len++;
> > > +  }
> > > +
> > > +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> > > +  return out_buf;
> > > +}
> > > +
> > > +static grub_err_t
> > > +grub_f2fs_label (grub_device_t device, char **label)
> > > +{
> > > +  struct grub_f2fs_data *data;
> > > +  grub_disk_t disk = device->disk;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  data = grub_f2fs_mount (disk);
> > > +  if (data)
> > > +    *label = (char *) grub_f2fs_utf16_to_utf8
> > (data->sblock.volume_name);
> > > +  else
> > > +    *label = NULL;
> > > +
> > > +  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 = device->disk;
> > > +
> > > +  grub_dl_ref (my_mod);
> > > +
> > > +  data = grub_f2fs_mount (disk);
> > > +  if (data)
> > > +    {
> > > +      *uuid =
> > > +     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 = NULL;
> > > +
> > > +  grub_free (data);
> > > +  grub_dl_unref (my_mod);
> > > +  return grub_errno;
> > > +}
> > > +
> > > +static struct grub_fs grub_f2fs_fs = {
> > > +  .name = "f2fs",
> > > +  .dir = grub_f2fs_dir,
> > > +  .open = grub_f2fs_open,
> > > +  .read = grub_f2fs_read,
> > > +  .close = grub_f2fs_close,
> > > +  .label = grub_f2fs_label,
> > > +  .uuid = grub_f2fs_uuid,
> > > +#ifdef GRUB_UTIL
> > > +  .reserved_first_sector = 1,
> > > +  .blocklist_install = 0,
> > > +#endif
> > > +  .next = 0
> > > +};
> > > +
> > > +GRUB_MOD_INIT (f2fs)
> > > +{
> > > +  grub_fs_register (&grub_f2fs_fs);
> > > +  my_mod = mod;
> > > +}
> > > +
> > > +GRUB_MOD_FINI (f2fs)
> > > +{
> > > +  grub_fs_unregister (&grub_f2fs_fs);
> > > +}
> > > diff --git a/po/exclude.pot b/po/exclude.pot
> > > index 0a9b215..816089c 100644
> > > --- a/po/exclude.pot
> > > +++ b/po/exclude.pot
> > > @@ -1214,6 +1214,7 @@ msgstr ""
> > >
> > >  #: 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..1ea77c8
> > > --- /dev/null
> > > +++ b/tests/f2fs_test.in
> > > @@ -0,0 +1,19 @@
> > > +#!/bin/sh
> > > +
> > > +set -e
> > > +
> > > +if [ "x$EUID" = "x" ] ; then
> > > + EUID=`id -u`
> > > +fi
> > > +
> > > +if [ "$EUID" != 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 424de22..e3e4109 100644
> > > --- a/tests/util/grub-fs-tester.in
> > > +++ b/tests/util/grub-fs-tester.in
> > > @@ -142,7 +142,7 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >       xsquash*)
> > >           MINBLKSIZE=4096
> > >           MAXBLKSIZE=1048576;;
> > > -     xxfs)
> > > +     xxfs|xf2fs)
> > >           MINBLKSIZE=$SECSIZE
> > >               # OS Limitation: GNU/Linux doesn't accept > 4096
> > >           MAXBLKSIZE=4096;;
> > > @@ -265,6 +265,10 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >               x"btrfs"*)
> > >                   FSLABEL="grub_;/testé莭莽😁киритi
> > urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> > >
> > > +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> > > +             x"f2fs")
> > > +
> >  FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> > nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> > > +
> > >           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> > >               x"exfat")
> > >                   FSLABEL="géт ;/莭莽😁кир";;
> > > @@ -474,7 +478,7 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> > Check it.
> > >       # FS LIMITATION: as far as I know those FS don't store their last
> > modification date.
> > >               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"*
> > | x"reiserfs_old" | x"reiserfs" \
> > > -                 | 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=y;;
> > > @@ -753,6 +757,8 @@ for
> > ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> > >                   MOUNTFS=ext2
> > >                   "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)
> > >
> >
> >
> > _______________________________________________
> > Grub-devel mailing list
> > Grub-devel@gnu.org
> > https://lists.gnu.org/mailman/listinfo/grub-devel
> >


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [2.02] Re: [f2fs-dev] [PATCH v8] F2FS support
  2016-03-01 19:52                 ` Andrei Borzenkov
@ 2016-03-03 21:36                   ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-03-03 21:36 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Change log from v7:
 - fix an offset bug in read_file

-- >8 --
From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1332 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..7fb256f
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1289 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = filesize - pos;
+
+      grub_memcpy (buf, 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.6.3


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [2.02] Re: [f2fs-dev] [PATCH v8] F2FS support
@ 2016-03-03 21:36                   ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-03-03 21:36 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Change log from v7:
 - fix an offset bug in read_file

-- >8 --
From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Tue, 17 Nov 2015 12:45:13 -0800
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1332 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index db7e8ec..84627bb 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -766,6 +767,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 1df3db2..e5a80f3 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5347,7 +5348,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, minix,
 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 charset
 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 d9fa0e3..b585ade 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1278,6 +1278,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..7fb256f
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1289 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = filesize - pos;
+
+      grub_memcpy (buf, 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 424de22..e3e4109 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.6.3



^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [2.02] Re:  [PATCH v8] F2FS support
  2016-03-03 21:36                   ` Jaegeuk Kim
@ 2016-08-04 17:06                     ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-08-04 17:06 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Hi Andrei,

It's been sitting for a long long time.
May I ask whether or not there is any merging plan of this?

Thanks,

On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
> Change log from v7:
>  - fix an offset bug in read_file
> 
> -- >8 --
> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
> 
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   10 +-
>  7 files changed, 1332 insertions(+), 4 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
> 
> diff --git a/Makefile.util.def b/Makefile.util.def
> index db7e8ec..84627bb 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -99,6 +99,7 @@ library = {
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
>    common = grub-core/fs/exfat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
>    common = grub-core/fs/hfsplus.c;
> @@ -766,6 +767,12 @@ script = {
>  
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = tests/nilfs2_test.in;
>  };
> diff --git a/docs/grub.texi b/docs/grub.texi
> index 1df3db2..e5a80f3 100644
> --- a/docs/grub.texi
> +++ b/docs/grub.texi
> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> +@dfn{f2fs}, @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},
> @@ -5347,7 +5348,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, minix,
>  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 charset
>  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 d9fa0e3..b585ade 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1278,6 +1278,11 @@ module = {
>  };
>  
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = fshelp;
>    common = fs/fshelp.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..7fb256f
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1289 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +/* F2FS Magic Number */
> +#define F2FS_SUPER_MAGIC	0xF2F52010
> +#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
> +#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
> +
> +/* byte-size offset */
> +#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
> +#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
> +#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
> +						GRUB_DISK_SECTOR_BITS)
> +
> +/* 9 bits for 512 bytes */
> +#define F2FS_MIN_LOG_SECTOR_SIZE	9
> +
> +/* support only 4KB block */
> +#define F2FS_BLK_BITS	12
> +#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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	(sizeof(struct grub_f2fs_nat_jent))
> +#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 (sizeof(struct grub_f2fs_nat_entry))
> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> +
> +#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
> +
> +#define CKPT_FLAG_SET(ckpt, f)	\
> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
> +#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
> +#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
> +#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
> +#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
> +
> +enum FILE_TYPE
> +{
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 7,
> +};
> +
> +#define MAX_VOLUME_NAME		512
> +
> +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[MAX_VOLUME_NAME];
> +  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;
> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
> +  };
> +  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_uint8_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
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  return addr[nr >> 3] & (1 << (nr & 7));
> +}
> +
> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +	- (base + 1) + type;
> +}
> +
> +static inline void *
> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 inode_block)
> +{
> +  if (inode_block)
> +    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,
> +		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> +		0, F2FS_BLKSIZE, buf);
> +}
> +
> +/*
> + * CRC32
> +*/
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return mask & *p;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_BLK_BITS)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
> +  if (err)
> +    return -1;
> +
> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> +}
> +
> +static void *
> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> +	grub_uint64_t *version)
> +{
> +  grub_uint32_t *cp_page_1, *cp_page_2;
> +  struct grub_f2fs_checkpoint *cp_block;
> +  grub_uint64_t cur_version = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
> +static grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +      offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_malloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> +    {
> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> +        {
> +          if (grub_errno == GRUB_ERR_NONE)
> +            grub_error (GRUB_ERR_BAD_FS,
> +			"not a F2FS filesystem (no superblock)");
> +          goto fail;
> +        }
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (filesize > MAX_INLINE_DATA)
> +        return -1;
> +      if (len > filesize - pos)
> +        len = filesize - pos;
> +
> +      grub_memcpy (buf, 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +        return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_malloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = 0;
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> +	      grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> + 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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +  struct grub_f2fs_inode *inode;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  inode = &(data->inode->i);
> +  file->size = grub_f2fs_file_size (inode);
> +  file->data = data;
> +  file->offset = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_uint8_t *
> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> +{
> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> +  grub_uint8_t *out_buf;
> +  int len = 0;
> +
> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> +  if (!out_buf)
> +    return NULL;
> +
> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> +    len++;
> +  }
> +
> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> +  return out_buf;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +	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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = mod;
> +}
> +
> +GRUB_MOD_FINI (f2fs)
> +{
> +  grub_fs_unregister (&grub_f2fs_fs);
> +}
> diff --git a/po/exclude.pot b/po/exclude.pot
> index 0a9b215..816089c 100644
> --- a/po/exclude.pot
> +++ b/po/exclude.pot
> @@ -1214,6 +1214,7 @@ msgstr ""
>  
>  #: 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..1ea77c8
> --- /dev/null
> +++ b/tests/f2fs_test.in
> @@ -0,0 +1,19 @@
> +#!/bin/sh
> +
> +set -e
> +
> +if [ "x$EUID" = "x" ] ; then
> + EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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 424de22..e3e4109 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> -	xxfs)
> +	xxfs|xf2fs)
>  	    MINBLKSIZE=$SECSIZE
>  		# OS Limitation: GNU/Linux doesn't accept > 4096
>  	    MAXBLKSIZE=4096;;
> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>  
> +	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +		x"f2fs")
> +		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> +
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)
> -- 
> 2.6.3
> 
> 
> ------------------------------------------------------------------------------
> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> Monitor end-to-end web transactions and take corrective actions now
> Troubleshoot faster and improve end-user experience. Signup Now!
> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [2.02] Re:  [PATCH v8] F2FS support
@ 2016-08-04 17:06                     ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-08-04 17:06 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

Hi Andrei,

It's been sitting for a long long time.
May I ask whether or not there is any merging plan of this?

Thanks,

On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
> Change log from v7:
>  - fix an offset bug in read_file
> 
> -- >8 --
> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Tue, 17 Nov 2015 12:45:13 -0800
> Subject: [PATCH] F2FS support
> 
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
> 
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> 
> The source codes for F2FS are available from:
> 
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> 
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---
>  Makefile.util.def            |    7 +
>  docs/grub.texi               |    5 +-
>  grub-core/Makefile.core.def  |    5 +
>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
>  po/exclude.pot               |    1 +
>  tests/f2fs_test.in           |   19 +
>  tests/util/grub-fs-tester.in |   10 +-
>  7 files changed, 1332 insertions(+), 4 deletions(-)
>  create mode 100644 grub-core/fs/f2fs.c
>  create mode 100644 tests/f2fs_test.in
> 
> diff --git a/Makefile.util.def b/Makefile.util.def
> index db7e8ec..84627bb 100644
> --- a/Makefile.util.def
> +++ b/Makefile.util.def
> @@ -99,6 +99,7 @@ library = {
>    common = grub-core/fs/ext2.c;
>    common = grub-core/fs/fat.c;
>    common = grub-core/fs/exfat.c;
> +  common = grub-core/fs/f2fs.c;
>    common = grub-core/fs/fshelp.c;
>    common = grub-core/fs/hfs.c;
>    common = grub-core/fs/hfsplus.c;
> @@ -766,6 +767,12 @@ script = {
>  
>  script = {
>    testcase;
> +  name = f2fs_test;
> +  common = tests/f2fs_test.in;
> +};
> +
> +script = {
> +  testcase;
>    name = nilfs2_test;
>    common = tests/nilfs2_test.in;
>  };
> diff --git a/docs/grub.texi b/docs/grub.texi
> index 1df3db2..e5a80f3 100644
> --- a/docs/grub.texi
> +++ b/docs/grub.texi
> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> +@dfn{f2fs}, @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},
> @@ -5347,7 +5348,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, minix,
>  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 charset
>  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 d9fa0e3..b585ade 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1278,6 +1278,11 @@ module = {
>  };
>  
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = fshelp;
>    common = fs/fshelp.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..7fb256f
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1289 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */
> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +/* F2FS Magic Number */
> +#define F2FS_SUPER_MAGIC	0xF2F52010
> +#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
> +#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
> +
> +/* byte-size offset */
> +#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
> +#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
> +#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
> +						GRUB_DISK_SECTOR_BITS)
> +
> +/* 9 bits for 512 bytes */
> +#define F2FS_MIN_LOG_SECTOR_SIZE	9
> +
> +/* support only 4KB block */
> +#define F2FS_BLK_BITS	12
> +#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
> +#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#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	(sizeof(struct grub_f2fs_nat_jent))
> +#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 (sizeof(struct grub_f2fs_nat_entry))
> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> +
> +#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
> +
> +#define CKPT_FLAG_SET(ckpt, f)	\
> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
> +#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
> +#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
> +#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
> +#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
> +
> +enum FILE_TYPE
> +{
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 7,
> +};
> +
> +#define MAX_VOLUME_NAME		512
> +
> +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[MAX_VOLUME_NAME];
> +  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;
> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
> +  };
> +  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_uint8_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
> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  return addr[nr >> 3] & (1 << (nr & 7));
> +}
> +
> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_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 = &data->ckpt;
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +	- (base + 1) + type;
> +}
> +
> +static inline void *
> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 inode_block)
> +{
> +  if (inode_block)
> +    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,
> +		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> +		0, F2FS_BLKSIZE, buf);
> +}
> +
> +/*
> + * CRC32
> +*/
> +#define CRCPOLY_LE 0xedb88320
> +
> +static inline grub_uint32_t
> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> +  unsigned char *p = (unsigned char *)buf;
> +  grub_uint32_t tmp = len;
> +  int i;
> +
> +  while (tmp--)
> +    {
> +      crc ^= *p++;
> +      for (i = 0; i < 8; i++)
> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> +    }
> +  return crc;
> +}
> +
> +static inline int
> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> +{
> +  grub_uint32_t cal_crc = 0;
> +
> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> +
> +  return (cal_crc == blk_crc) ? 1 : 0;
> +}
> +
> +static inline int
> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> +{
> +  int mask;
> +
> +  p += (nr >> 3);
> +  mask = 1 << (7 - (nr & 0x07));
> +  return mask & *p;
> +}
> +
> +static int
> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> +{
> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> +
> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> +    return -1;
> +
> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> +    return -1;
> +
> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> +
> +  if (log_sectorsize > F2FS_BLK_BITS)
> +    return -1;
> +
> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> +    return -1;
> +
> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> +{
> +  grub_disk_t disk = data->disk;
> +  grub_err_t err;
> +
> +  /* Read first super block. */
> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
> +  if (err)
> +    return -1;
> +
> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> +}
> +
> +static void *
> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> +	grub_uint64_t *version)
> +{
> +  grub_uint32_t *cp_page_1, *cp_page_2;
> +  struct grub_f2fs_checkpoint *cp_block;
> +  grub_uint64_t cur_version = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
> +static grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> +  grub_uint32_t blkaddr = 0;
> +  grub_uint16_t i;
> +
> +  for (i = 0; i < n; i++)
> +    {
> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> +        {
> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> +  if (blkaddr)
> +    return blkaddr;
> +
> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!nat_block)
> +    return 0;
> +
> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> +
> +  seg_off = block_off / data->blocks_per_seg;
> +  block_addr = 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 += data->blocks_per_seg;
> +
> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> +  if (err)
> +    {
> +      grub_free (nat_block);
> +      return 0;
> +    }
> +
> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> +  int n = 0;
> +  int level = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> +
> +  noffset[0] = 0;
> +
> +  if (block < direct_index)
> +    {
> +      offset[n] = block;
> +      goto got;
> +    }
> +
> +  block -= direct_index;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR1_BLOCK;
> +      noffset[n] = 1;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < direct_blks)
> +    {
> +      offset[n++] = NODE_DIR2_BLOCK;
> +      noffset[n] = 2;
> +      offset[n] = block;
> +      level = 1;
> +      goto got;
> +    }
> +
> +  block -= direct_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND1_BLOCK;
> +      noffset[n] = 3;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 4 + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < indirect_blks)
> +    {
> +      offset[n++] = NODE_IND2_BLOCK;
> +      noffset[n] = 4 + dptrs_per_blk;
> +      offset[n++] = block / direct_blks;
> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 2;
> +      goto got;
> +    }
> +
> +  block -= indirect_blks;
> +  if (block < dindirect_blks)
> +    {
> +      offset[n++] = NODE_DIND_BLOCK;
> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> +      offset[n++] = block / indirect_blks;
> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> +      offset[n - 1] * (dptrs_per_blk + 1);
> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> +		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> +      offset[n] = block % direct_blks;
> +      level = 3;
> +      goto got;
> +    }
> +got:
> +  return level;
> +}
> +
> +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 = 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 = grub_malloc (sizeof (*data));
> +  if (!data)
> +    return NULL;
> +
> +  data->disk = disk;
> +
> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> +    {
> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> +        {
> +          if (grub_errno == GRUB_ERR_NONE)
> +            grub_error (GRUB_ERR_BAD_FS,
> +			"not a F2FS filesystem (no superblock)");
> +          goto fail;
> +        }
> +    }
> +
> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> +  data->blocks_per_seg = 1 <<
> +	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> +
> +  err = grub_f2fs_read_cp (data);
> +  if (err)
> +    goto fail;
> +
> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> +
> +  err = get_nat_journal (data);
> +  if (err)
> +    goto fail;
> +
> +  data->diropen.data = data;
> +  data->diropen.ino = data->root_ino;
> +  data->diropen.inode_read = 1;
> +  data->inode = &data->diropen.inode;
> +
> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> +  if (err)
> +    goto fail;
> +
> +  return data;
> +
> +fail:
> +  grub_free (data);
> +  return NULL;
> +}
> +
> +/* guarantee inline_data was handled by caller */
> +static grub_disk_addr_t
> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> +{
> +  struct grub_f2fs_data *data = node->data;
> +  struct grub_f2fs_inode *inode = &node->inode.i;
> +  grub_uint32_t offset[4], noffset[4], nids[4];
> +  struct grub_f2fs_node *node_block;
> +  grub_uint32_t block_addr = -1;
> +  int level, i;
> +
> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> +  if (level == 0)
> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> +
> +  node_block = grub_malloc (F2FS_BLKSIZE);
> +  if (!node_block)
> +    return -1;
> +
> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> +
> +  /* get indirect or direct nodes */
> +  for (i = 1; i <= level; i++)
> +    {
> +      grub_f2fs_read_node (data, nids[i], node_block);
> +      if (grub_errno)
> +        goto fail;
> +
> +      if (i < level)
> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> +    }
> +
> +  block_addr = 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 = &node->inode.i;
> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> +  char *inline_addr = __inline_addr (inode);
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA)
> +    {
> +      if (filesize > MAX_INLINE_DATA)
> +        return -1;
> +      if (len > filesize - pos)
> +        len = filesize - pos;
> +
> +      grub_memcpy (buf, 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_get_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 = node;
> +  grub_uint64_t filesize;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +        return 0;
> +    }
> +
> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> +
> +  symlink = grub_malloc (filesize + 1);
> +  if (!symlink)
> +    return 0;
> +
> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> +  if (grub_errno)
> +    {
> +      grub_free (symlink);
> +      return 0;
> +    }
> +
> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> +    {
> +      char *filename;
> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> +      enum FILE_TYPE ftype;
> +      int name_len;
> +      int ret;
> +
> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> +        {
> +          i++;
> +          continue;
> +        }
> +
> +      ftype = ctx->dentry[i].file_type;
> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> +      filename = grub_malloc (name_len + 1);
> +      if (!filename)
> +        return 0;
> +
> +      grub_memcpy (filename, ctx->filename[i], name_len);
> +      filename[name_len] = 0;
> +
> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> +      if (!fdiro)
> +        {
> +          grub_free(filename);
> +          return 0;
> +        }
> +
> +      if (ftype == F2FS_FT_DIR)
> +        type = GRUB_FSHELP_DIR;
> +      else if (ftype == F2FS_FT_SYMLINK)
> +        type = GRUB_FSHELP_SYMLINK;
> +      else if (ftype == F2FS_FT_REG_FILE)
> +        type = GRUB_FSHELP_REG;
> +
> +      fdiro->data = ctx->data;
> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> +      fdiro->inode_read = 0;
> +
> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> +      grub_free(filename);
> +      if (ret)
> +        return 1;
> +
> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> +
> +  ctx->bitmap = de_blk->dentry_bitmap;
> +  ctx->dentry = de_blk->dentry;
> +  ctx->filename = de_blk->filename;
> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> +  struct grub_f2fs_inode *inode;
> +  struct grub_f2fs_dir_iter_ctx ctx = {
> +    .data = diro->data,
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  grub_off_t fpos = 0;
> +
> +  if (!diro->inode_read)
> +    {
> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> +      if (grub_errno)
> +	return 0;
> +    }
> +
> +  inode = &diro->inode.i;
> +
> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> +
> +  while (fpos < grub_f2fs_file_size (inode))
> +    {
> +      struct grub_f2fs_dentry_block *de_blk;
> +      char *buf;
> +      int ret;
> +
> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> +
> +      ctx.bitmap = de_blk->dentry_bitmap;
> +      ctx.dentry = de_blk->dentry;
> +      ctx.filename = de_blk->filename;
> +      ctx.max = NR_DENTRY_IN_BLOCK;
> +
> +      ret = grub_f2fs_check_dentries (&ctx);
> +      grub_free (buf);
> +      if (ret)
> +        return 1;
> +
> +      fpos += F2FS_BLKSIZE;
> +    }
> +  return 0;
> +}
> +
> +static int
> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> +	      grub_fshelp_node_t node, void *data)
> +{
> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> +      grub_errno = GRUB_ERR_NONE;
> +    }
> +  if (node->inode_read)
> +    {
> +      info.mtimeset = 1;
> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> +    }
> +
> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> +  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 = {
> +    .hook = hook,
> +    .hook_data = hook_data
> +  };
> +  struct grub_fshelp_node *fdiro = 0;
> +
> +  grub_dl_ref (my_mod);
> +
> +  ctx.data = 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 != &ctx.data->diropen)
> +    grub_free (fdiro);
> + 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 = NULL;
> +  struct grub_fshelp_node *fdiro = 0;
> +  struct grub_f2fs_inode *inode;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = 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, sizeof (*data->inode));
> +  grub_free (fdiro);
> +
> +  inode = &(data->inode->i);
> +  file->size = grub_f2fs_file_size (inode);
> +  file->data = data;
> +  file->offset = 0;
> +
> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> +  return 0;
> +
> +fail:
> +  if (fdiro != &data->diropen)
> +    grub_free (fdiro);
> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> +
> +  grub_free (data);
> +
> +  grub_dl_unref (my_mod);
> +
> +  return GRUB_ERR_NONE;
> +}
> +
> +static grub_uint8_t *
> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> +{
> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> +  grub_uint8_t *out_buf;
> +  int len = 0;
> +
> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> +  if (!out_buf)
> +    return NULL;
> +
> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> +    len++;
> +  }
> +
> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> +  return out_buf;
> +}
> +
> +static grub_err_t
> +grub_f2fs_label (grub_device_t device, char **label)
> +{
> +  struct grub_f2fs_data *data;
> +  grub_disk_t disk = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> +  else
> +    *label = NULL;
> +
> +  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 = device->disk;
> +
> +  grub_dl_ref (my_mod);
> +
> +  data = grub_f2fs_mount (disk);
> +  if (data)
> +    {
> +      *uuid =
> +	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 = NULL;
> +
> +  grub_free (data);
> +  grub_dl_unref (my_mod);
> +  return grub_errno;
> +}
> +
> +static struct grub_fs grub_f2fs_fs = {
> +  .name = "f2fs",
> +  .dir = grub_f2fs_dir,
> +  .open = grub_f2fs_open,
> +  .read = grub_f2fs_read,
> +  .close = grub_f2fs_close,
> +  .label = grub_f2fs_label,
> +  .uuid = grub_f2fs_uuid,
> +#ifdef GRUB_UTIL
> +  .reserved_first_sector = 1,
> +  .blocklist_install = 0,
> +#endif
> +  .next = 0
> +};
> +
> +GRUB_MOD_INIT (f2fs)
> +{
> +  grub_fs_register (&grub_f2fs_fs);
> +  my_mod = mod;
> +}
> +
> +GRUB_MOD_FINI (f2fs)
> +{
> +  grub_fs_unregister (&grub_f2fs_fs);
> +}
> diff --git a/po/exclude.pot b/po/exclude.pot
> index 0a9b215..816089c 100644
> --- a/po/exclude.pot
> +++ b/po/exclude.pot
> @@ -1214,6 +1214,7 @@ msgstr ""
>  
>  #: 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..1ea77c8
> --- /dev/null
> +++ b/tests/f2fs_test.in
> @@ -0,0 +1,19 @@
> +#!/bin/sh
> +
> +set -e
> +
> +if [ "x$EUID" = "x" ] ; then
> + EUID=`id -u`
> +fi
> +
> +if [ "$EUID" != 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 424de22..e3e4109 100644
> --- a/tests/util/grub-fs-tester.in
> +++ b/tests/util/grub-fs-tester.in
> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	xsquash*)
>  	    MINBLKSIZE=4096
>  	    MAXBLKSIZE=1048576;;
> -	xxfs)
> +	xxfs|xf2fs)
>  	    MINBLKSIZE=$SECSIZE
>  		# OS Limitation: GNU/Linux doesn't accept > 4096
>  	    MAXBLKSIZE=4096;;
> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		x"btrfs"*)
>  		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>  
> +	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> +		x"f2fs")
> +		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> +
>  	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
>  		x"exfat")
>  		    FSLABEL="géт ;/莭莽😁кир";;
> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>  	# FS LIMITATION: as far as I know those FS don't store their last modification date.
>  		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> -		    | 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=y;;
> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>  		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>  		    MOUNTFS=ext2
>  		    "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)
> -- 
> 2.6.3
> 
> 
> ------------------------------------------------------------------------------
> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> Monitor end-to-end web transactions and take corrective actions now
> Troubleshoot faster and improve end-user experience. Signup Now!
> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [2.02] Re: [PATCH v8] F2FS support
  2016-08-04 17:06                     ` [f2fs-dev] " Jaegeuk Kim
@ 2016-08-05 10:57                       ` Andrei Borzenkov
  -1 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2016-08-05 10:57 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

Vladimir prefers to postpone new features until 2.02 is out. From my
side I think it is isolated enough and new code so low regression
risk. Vladimir has the final word as maintainer :)

On Thu, Aug 4, 2016 at 8:06 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:
> Hi Andrei,
>
> It's been sitting for a long long time.
> May I ask whether or not there is any merging plan of this?
>
> Thanks,
>
> On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
>> Change log from v7:
>>  - fix an offset bug in read_file
>>
>> -- >8 --
>> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
>> From: Jaegeuk Kim <jaegeuk@kernel.org>
>> Date: Tue, 17 Nov 2015 12:45:13 -0800
>> Subject: [PATCH] F2FS support
>>
>> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
>> into Linux kernel v3.8 in 2013.
>>
>> The motive for F2FS was to build a file system that from the start, takes into
>> account the characteristics of NAND flash memory-based storage devices (such as
>> solid-state disks, eMMC, and SD cards).
>>
>> F2FS was designed on a basis of a log-structured file system approach, which
>> remedies some known issues of the older log structured file systems, such as
>> the snowball effect of wandering trees and high cleaning overhead. In addition,
>> since a NAND-based storage device shows different characteristics according to
>> its internal geometry or flash memory management scheme (such as the Flash
>> Translation Layer or FTL), it supports various parameters not only for
>> configuring on-disk layout, but also for selecting allocation and cleaning
>> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
>>
>> The source codes for F2FS are available from:
>>
>> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
>> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
>>
>> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
>> ---
>>  Makefile.util.def            |    7 +
>>  docs/grub.texi               |    5 +-
>>  grub-core/Makefile.core.def  |    5 +
>>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
>>  po/exclude.pot               |    1 +
>>  tests/f2fs_test.in           |   19 +
>>  tests/util/grub-fs-tester.in |   10 +-
>>  7 files changed, 1332 insertions(+), 4 deletions(-)
>>  create mode 100644 grub-core/fs/f2fs.c
>>  create mode 100644 tests/f2fs_test.in
>>
>> diff --git a/Makefile.util.def b/Makefile.util.def
>> index db7e8ec..84627bb 100644
>> --- a/Makefile.util.def
>> +++ b/Makefile.util.def
>> @@ -99,6 +99,7 @@ library = {
>>    common = grub-core/fs/ext2.c;
>>    common = grub-core/fs/fat.c;
>>    common = grub-core/fs/exfat.c;
>> +  common = grub-core/fs/f2fs.c;
>>    common = grub-core/fs/fshelp.c;
>>    common = grub-core/fs/hfs.c;
>>    common = grub-core/fs/hfsplus.c;
>> @@ -766,6 +767,12 @@ script = {
>>
>>  script = {
>>    testcase;
>> +  name = f2fs_test;
>> +  common = tests/f2fs_test.in;
>> +};
>> +
>> +script = {
>> +  testcase;
>>    name = nilfs2_test;
>>    common = tests/nilfs2_test.in;
>>  };
>> diff --git a/docs/grub.texi b/docs/grub.texi
>> index 1df3db2..e5a80f3 100644
>> --- a/docs/grub.texi
>> +++ b/docs/grub.texi
>> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
>> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
>> +@dfn{f2fs}, @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},
>> @@ -5347,7 +5348,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, minix,
>>  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 charset
>>  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 d9fa0e3..b585ade 100644
>> --- a/grub-core/Makefile.core.def
>> +++ b/grub-core/Makefile.core.def
>> @@ -1278,6 +1278,11 @@ module = {
>>  };
>>
>>  module = {
>> +  name = f2fs;
>> +  common = fs/f2fs.c;
>> +};
>> +
>> +module = {
>>    name = fshelp;
>>    common = fs/fshelp.c;
>>  };
>> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
>> new file mode 100644
>> index 0000000..7fb256f
>> --- /dev/null
>> +++ b/grub-core/fs/f2fs.c
>> @@ -0,0 +1,1289 @@
>> +/*
>> + *  f2fs.c - Flash-Friendly File System
>> + *
>> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
>> + *
>> + *  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 <http://www.gnu.org/licenses/>.
>> + */
>> +#include <grub/err.h>
>> +#include <grub/file.h>
>> +#include <grub/mm.h>
>> +#include <grub/misc.h>
>> +#include <grub/disk.h>
>> +#include <grub/dl.h>
>> +#include <grub/types.h>
>> +#include <grub/charset.h>
>> +#include <grub/fshelp.h>
>> +
>> +GRUB_MOD_LICENSE ("GPLv3+");
>> +
>> +/* F2FS Magic Number */
>> +#define F2FS_SUPER_MAGIC     0xF2F52010
>> +#define CHECKSUM_OFFSET              4092            /* must be aligned 4 bytes */
>> +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
>> +
>> +/* byte-size offset */
>> +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
>> +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
>> +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
>> +                                             GRUB_DISK_SECTOR_BITS)
>> +
>> +/* 9 bits for 512 bytes */
>> +#define F2FS_MIN_LOG_SECTOR_SIZE     9
>> +
>> +/* support only 4KB block */
>> +#define F2FS_BLK_BITS        12
>> +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
>> +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
>> +
>> +#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  (sizeof(struct grub_f2fs_nat_jent))
>> +#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 (sizeof(struct grub_f2fs_nat_entry))
>> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
>> +
>> +#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
>> +
>> +#define CKPT_FLAG_SET(ckpt, f)       \
>> +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
>> +
>> +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
>> +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
>> +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
>> +#define F2FS_DATA_EXIST              0x08    /* file inline data exist flag */
>> +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot dentries */
>> +
>> +enum FILE_TYPE
>> +{
>> +  F2FS_FT_UNKNOWN,
>> +  F2FS_FT_REG_FILE = 1,
>> +  F2FS_FT_DIR = 2,
>> +  F2FS_FT_SYMLINK = 7,
>> +};
>> +
>> +#define MAX_VOLUME_NAME              512
>> +
>> +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[MAX_VOLUME_NAME];
>> +  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;
>> +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE totally */
>> +  };
>> +  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_uint8_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
>> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
>> +{
>> +  return addr[nr >> 3] & (1 << (nr & 7));
>> +}
>> +
>> +static inline char *
>> +__inline_addr (struct grub_f2fs_inode *inode)
>> +{
>> +  return (char *)&inode->i_addr[1];
>> +}
>> +
>> +static inline grub_uint64_t
>> +grub_f2fs_file_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 = &data->ckpt;
>> +  grub_uint32_t start_addr = data->cp_blkaddr;
>> +
>> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
>> +
>> +  return __start_cp_addr (data) +
>> +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
>> +     - (base + 1) + type;
>> +}
>> +
>> +static inline void *
>> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
>> +{
>> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
>> +  grub_uint32_t offset;
>> +
>> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
>> +    return ckpt->sit_nat_version_bitmap;
>> +
>> +  offset = 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 inode_block)
>> +{
>> +  if (inode_block)
>> +    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,
>> +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
>> +             0, F2FS_BLKSIZE, buf);
>> +}
>> +
>> +/*
>> + * CRC32
>> +*/
>> +#define CRCPOLY_LE 0xedb88320
>> +
>> +static inline grub_uint32_t
>> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
>> +{
>> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
>> +  unsigned char *p = (unsigned char *)buf;
>> +  grub_uint32_t tmp = len;
>> +  int i;
>> +
>> +  while (tmp--)
>> +    {
>> +      crc ^= *p++;
>> +      for (i = 0; i < 8; i++)
>> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
>> +    }
>> +  return crc;
>> +}
>> +
>> +static inline int
>> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
>> +{
>> +  grub_uint32_t cal_crc = 0;
>> +
>> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
>> +
>> +  return (cal_crc == blk_crc) ? 1 : 0;
>> +}
>> +
>> +static inline int
>> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
>> +{
>> +  int mask;
>> +
>> +  p += (nr >> 3);
>> +  mask = 1 << (7 - (nr & 0x07));
>> +  return mask & *p;
>> +}
>> +
>> +static int
>> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
>> +{
>> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
>> +
>> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
>> +    return -1;
>> +
>> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
>> +    return -1;
>> +
>> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
>> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
>> +
>> +  if (log_sectorsize > F2FS_BLK_BITS)
>> +    return -1;
>> +
>> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
>> +    return -1;
>> +
>> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
>> +    return -1;
>> +
>> +  return 0;
>> +}
>> +
>> +static int
>> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
>> +{
>> +  grub_disk_t disk = data->disk;
>> +  grub_err_t err;
>> +
>> +  /* Read first super block. */
>> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
>> +  if (err)
>> +    return -1;
>> +
>> +  return grub_f2fs_sanity_check_sb (&data->sblock);
>> +}
>> +
>> +static void *
>> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
>> +     grub_uint64_t *version)
>> +{
>> +  grub_uint32_t *cp_page_1, *cp_page_2;
>> +  struct grub_f2fs_checkpoint *cp_block;
>> +  grub_uint64_t cur_version = 0, pre_version = 0;
>> +  grub_uint32_t crc = 0;
>> +  grub_uint32_t crc_offset;
>> +  grub_err_t err;
>> +
>> +  /* Read the 1st cp block in this CP pack */
>> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
>> +  if (!cp_page_1)
>> +    return NULL;
>> +
>> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
>> +  if (err)
>> +    goto invalid_cp1;
>> +
>> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
>> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
>> +  if (crc_offset != CHECKSUM_OFFSET)
>> +    goto invalid_cp1;
>> +
>> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
>> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
>> +    goto invalid_cp1;
>> +
>> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
>> +
>> +  /* Read the 2nd cp block in this CP pack */
>> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
>> +  if (!cp_page_2)
>> +    goto invalid_cp1;
>> +
>> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
>> +
>> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
>> +  if (err)
>> +    goto invalid_cp2;
>> +
>> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
>> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
>> +  if (crc_offset != CHECKSUM_OFFSET)
>> +    goto invalid_cp2;
>> +
>> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
>> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
>> +    goto invalid_cp2;
>> +
>> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
>> +  if (cur_version == pre_version)
>> +    {
>> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
>> +  cp1 = 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 += data->blocks_per_seg;
>> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
>> +  if (!cp2 && grub_errno)
>> +    {
>> +      grub_free (cp1);
>> +      return grub_errno;
>> +    }
>> +
>> +  if (cp1 && cp2)
>> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
>> +  else if (cp1)
>> +    cur_page = cp1;
>> +  else if (cp2)
>> +    cur_page = cp2;
>> +  else
>> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
>> +
>> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
>> +
>> +  grub_free (cp1);
>> +  grub_free (cp2);
>> +  return 0;
>> +}
>> +
>> +static grub_err_t
>> +get_nat_journal (struct grub_f2fs_data *data)
>> +{
>> +  grub_uint32_t block;
>> +  char *buf;
>> +  grub_err_t err;
>> +
>> +  buf = grub_malloc (F2FS_BLKSIZE);
>> +  if (!buf)
>> +    return grub_errno;
>> +
>> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
>> +    block = __start_sum_block (data);
>> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
>> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
>> +  else
>> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
>> +
>> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
>> +  grub_uint32_t blkaddr = 0;
>> +  grub_uint16_t i;
>> +
>> +  for (i = 0; i < n; i++)
>> +    {
>> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
>> +        {
>> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
>> +  if (blkaddr)
>> +    return blkaddr;
>> +
>> +  nat_block = grub_malloc (F2FS_BLKSIZE);
>> +  if (!nat_block)
>> +    return 0;
>> +
>> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
>> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
>> +
>> +  seg_off = block_off / data->blocks_per_seg;
>> +  block_addr = 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 += data->blocks_per_seg;
>> +
>> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
>> +  if (err)
>> +    {
>> +      grub_free (nat_block);
>> +      return 0;
>> +    }
>> +
>> +  blkaddr = 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 = ADDRS_PER_BLOCK;
>> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
>> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
>> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
>> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
>> +  int n = 0;
>> +  int level = 0;
>> +
>> +  if (inode->i_inline & F2FS_INLINE_XATTR)
>> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
>> +
>> +  noffset[0] = 0;
>> +
>> +  if (block < direct_index)
>> +    {
>> +      offset[n] = block;
>> +      goto got;
>> +    }
>> +
>> +  block -= direct_index;
>> +  if (block < direct_blks)
>> +    {
>> +      offset[n++] = NODE_DIR1_BLOCK;
>> +      noffset[n] = 1;
>> +      offset[n] = block;
>> +      level = 1;
>> +      goto got;
>> +    }
>> +
>> +  block -= direct_blks;
>> +  if (block < direct_blks)
>> +    {
>> +      offset[n++] = NODE_DIR2_BLOCK;
>> +      noffset[n] = 2;
>> +      offset[n] = block;
>> +      level = 1;
>> +      goto got;
>> +    }
>> +
>> +  block -= direct_blks;
>> +  if (block < indirect_blks)
>> +    {
>> +      offset[n++] = NODE_IND1_BLOCK;
>> +      noffset[n] = 3;
>> +      offset[n++] = block / direct_blks;
>> +      noffset[n] = 4 + offset[n - 1];
>> +      offset[n] = block % direct_blks;
>> +      level = 2;
>> +      goto got;
>> +    }
>> +
>> +  block -= indirect_blks;
>> +  if (block < indirect_blks)
>> +    {
>> +      offset[n++] = NODE_IND2_BLOCK;
>> +      noffset[n] = 4 + dptrs_per_blk;
>> +      offset[n++] = block / direct_blks;
>> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
>> +      offset[n] = block % direct_blks;
>> +      level = 2;
>> +      goto got;
>> +    }
>> +
>> +  block -= indirect_blks;
>> +  if (block < dindirect_blks)
>> +    {
>> +      offset[n++] = NODE_DIND_BLOCK;
>> +      noffset[n] = 5 + (dptrs_per_blk * 2);
>> +      offset[n++] = block / indirect_blks;
>> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
>> +      offset[n - 1] * (dptrs_per_blk + 1);
>> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
>> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
>> +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
>> +      offset[n] = block % direct_blks;
>> +      level = 3;
>> +      goto got;
>> +    }
>> +got:
>> +  return level;
>> +}
>> +
>> +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 = 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 = grub_malloc (sizeof (*data));
>> +  if (!data)
>> +    return NULL;
>> +
>> +  data->disk = disk;
>> +
>> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
>> +    {
>> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
>> +        {
>> +          if (grub_errno == GRUB_ERR_NONE)
>> +            grub_error (GRUB_ERR_BAD_FS,
>> +                     "not a F2FS filesystem (no superblock)");
>> +          goto fail;
>> +        }
>> +    }
>> +
>> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
>> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
>> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
>> +  data->blocks_per_seg = 1 <<
>> +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
>> +
>> +  err = grub_f2fs_read_cp (data);
>> +  if (err)
>> +    goto fail;
>> +
>> +  data->nat_bitmap = __nat_bitmap_ptr (data);
>> +
>> +  err = get_nat_journal (data);
>> +  if (err)
>> +    goto fail;
>> +
>> +  data->diropen.data = data;
>> +  data->diropen.ino = data->root_ino;
>> +  data->diropen.inode_read = 1;
>> +  data->inode = &data->diropen.inode;
>> +
>> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
>> +  if (err)
>> +    goto fail;
>> +
>> +  return data;
>> +
>> +fail:
>> +  grub_free (data);
>> +  return NULL;
>> +}
>> +
>> +/* guarantee inline_data was handled by caller */
>> +static grub_disk_addr_t
>> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
>> +{
>> +  struct grub_f2fs_data *data = node->data;
>> +  struct grub_f2fs_inode *inode = &node->inode.i;
>> +  grub_uint32_t offset[4], noffset[4], nids[4];
>> +  struct grub_f2fs_node *node_block;
>> +  grub_uint32_t block_addr = -1;
>> +  int level, i;
>> +
>> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
>> +  if (level == 0)
>> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
>> +
>> +  node_block = grub_malloc (F2FS_BLKSIZE);
>> +  if (!node_block)
>> +    return -1;
>> +
>> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
>> +
>> +  /* get indirect or direct nodes */
>> +  for (i = 1; i <= level; i++)
>> +    {
>> +      grub_f2fs_read_node (data, nids[i], node_block);
>> +      if (grub_errno)
>> +        goto fail;
>> +
>> +      if (i < level)
>> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
>> +    }
>> +
>> +  block_addr = 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 = &node->inode.i;
>> +  grub_off_t filesize = grub_f2fs_file_size (inode);
>> +  char *inline_addr = __inline_addr (inode);
>> +
>> +  if (inode->i_inline & F2FS_INLINE_DATA)
>> +    {
>> +      if (filesize > MAX_INLINE_DATA)
>> +        return -1;
>> +      if (len > filesize - pos)
>> +        len = filesize - pos;
>> +
>> +      grub_memcpy (buf, 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_get_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 = node;
>> +  grub_uint64_t filesize;
>> +
>> +  if (!diro->inode_read)
>> +    {
>> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
>> +      if (grub_errno)
>> +        return 0;
>> +    }
>> +
>> +  filesize = grub_f2fs_file_size(&diro->inode.i);
>> +
>> +  symlink = grub_malloc (filesize + 1);
>> +  if (!symlink)
>> +    return 0;
>> +
>> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
>> +  if (grub_errno)
>> +    {
>> +      grub_free (symlink);
>> +      return 0;
>> +    }
>> +
>> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
>> +    {
>> +      char *filename;
>> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
>> +      enum FILE_TYPE ftype;
>> +      int name_len;
>> +      int ret;
>> +
>> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
>> +        {
>> +          i++;
>> +          continue;
>> +        }
>> +
>> +      ftype = ctx->dentry[i].file_type;
>> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
>> +      filename = grub_malloc (name_len + 1);
>> +      if (!filename)
>> +        return 0;
>> +
>> +      grub_memcpy (filename, ctx->filename[i], name_len);
>> +      filename[name_len] = 0;
>> +
>> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
>> +      if (!fdiro)
>> +        {
>> +          grub_free(filename);
>> +          return 0;
>> +        }
>> +
>> +      if (ftype == F2FS_FT_DIR)
>> +        type = GRUB_FSHELP_DIR;
>> +      else if (ftype == F2FS_FT_SYMLINK)
>> +        type = GRUB_FSHELP_SYMLINK;
>> +      else if (ftype == F2FS_FT_REG_FILE)
>> +        type = GRUB_FSHELP_REG;
>> +
>> +      fdiro->data = ctx->data;
>> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
>> +      fdiro->inode_read = 0;
>> +
>> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
>> +      grub_free(filename);
>> +      if (ret)
>> +        return 1;
>> +
>> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
>> +
>> +  ctx->bitmap = de_blk->dentry_bitmap;
>> +  ctx->dentry = de_blk->dentry;
>> +  ctx->filename = de_blk->filename;
>> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
>> +  struct grub_f2fs_inode *inode;
>> +  struct grub_f2fs_dir_iter_ctx ctx = {
>> +    .data = diro->data,
>> +    .hook = hook,
>> +    .hook_data = hook_data
>> +  };
>> +  grub_off_t fpos = 0;
>> +
>> +  if (!diro->inode_read)
>> +    {
>> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
>> +      if (grub_errno)
>> +     return 0;
>> +    }
>> +
>> +  inode = &diro->inode.i;
>> +
>> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
>> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
>> +
>> +  while (fpos < grub_f2fs_file_size (inode))
>> +    {
>> +      struct grub_f2fs_dentry_block *de_blk;
>> +      char *buf;
>> +      int ret;
>> +
>> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
>> +
>> +      ctx.bitmap = de_blk->dentry_bitmap;
>> +      ctx.dentry = de_blk->dentry;
>> +      ctx.filename = de_blk->filename;
>> +      ctx.max = NR_DENTRY_IN_BLOCK;
>> +
>> +      ret = grub_f2fs_check_dentries (&ctx);
>> +      grub_free (buf);
>> +      if (ret)
>> +        return 1;
>> +
>> +      fpos += F2FS_BLKSIZE;
>> +    }
>> +  return 0;
>> +}
>> +
>> +static int
>> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
>> +           grub_fshelp_node_t node, void *data)
>> +{
>> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
>> +      grub_errno = GRUB_ERR_NONE;
>> +    }
>> +  if (node->inode_read)
>> +    {
>> +      info.mtimeset = 1;
>> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
>> +    }
>> +
>> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
>> +  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 = {
>> +    .hook = hook,
>> +    .hook_data = hook_data
>> +  };
>> +  struct grub_fshelp_node *fdiro = 0;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  ctx.data = 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 != &ctx.data->diropen)
>> +    grub_free (fdiro);
>> + 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 = NULL;
>> +  struct grub_fshelp_node *fdiro = 0;
>> +  struct grub_f2fs_inode *inode;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  data = 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, sizeof (*data->inode));
>> +  grub_free (fdiro);
>> +
>> +  inode = &(data->inode->i);
>> +  file->size = grub_f2fs_file_size (inode);
>> +  file->data = data;
>> +  file->offset = 0;
>> +
>> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
>> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
>> +  return 0;
>> +
>> +fail:
>> +  if (fdiro != &data->diropen)
>> +    grub_free (fdiro);
>> +  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 = (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 = (struct grub_f2fs_data *) file->data;
>> +
>> +  grub_free (data);
>> +
>> +  grub_dl_unref (my_mod);
>> +
>> +  return GRUB_ERR_NONE;
>> +}
>> +
>> +static grub_uint8_t *
>> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
>> +{
>> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
>> +  grub_uint8_t *out_buf;
>> +  int len = 0;
>> +
>> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
>> +  if (!out_buf)
>> +    return NULL;
>> +
>> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
>> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
>> +    len++;
>> +  }
>> +
>> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
>> +  return out_buf;
>> +}
>> +
>> +static grub_err_t
>> +grub_f2fs_label (grub_device_t device, char **label)
>> +{
>> +  struct grub_f2fs_data *data;
>> +  grub_disk_t disk = device->disk;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  data = grub_f2fs_mount (disk);
>> +  if (data)
>> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
>> +  else
>> +    *label = NULL;
>> +
>> +  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 = device->disk;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  data = grub_f2fs_mount (disk);
>> +  if (data)
>> +    {
>> +      *uuid =
>> +     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 = NULL;
>> +
>> +  grub_free (data);
>> +  grub_dl_unref (my_mod);
>> +  return grub_errno;
>> +}
>> +
>> +static struct grub_fs grub_f2fs_fs = {
>> +  .name = "f2fs",
>> +  .dir = grub_f2fs_dir,
>> +  .open = grub_f2fs_open,
>> +  .read = grub_f2fs_read,
>> +  .close = grub_f2fs_close,
>> +  .label = grub_f2fs_label,
>> +  .uuid = grub_f2fs_uuid,
>> +#ifdef GRUB_UTIL
>> +  .reserved_first_sector = 1,
>> +  .blocklist_install = 0,
>> +#endif
>> +  .next = 0
>> +};
>> +
>> +GRUB_MOD_INIT (f2fs)
>> +{
>> +  grub_fs_register (&grub_f2fs_fs);
>> +  my_mod = mod;
>> +}
>> +
>> +GRUB_MOD_FINI (f2fs)
>> +{
>> +  grub_fs_unregister (&grub_f2fs_fs);
>> +}
>> diff --git a/po/exclude.pot b/po/exclude.pot
>> index 0a9b215..816089c 100644
>> --- a/po/exclude.pot
>> +++ b/po/exclude.pot
>> @@ -1214,6 +1214,7 @@ msgstr ""
>>
>>  #: 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..1ea77c8
>> --- /dev/null
>> +++ b/tests/f2fs_test.in
>> @@ -0,0 +1,19 @@
>> +#!/bin/sh
>> +
>> +set -e
>> +
>> +if [ "x$EUID" = "x" ] ; then
>> + EUID=`id -u`
>> +fi
>> +
>> +if [ "$EUID" != 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 424de22..e3e4109 100644
>> --- a/tests/util/grub-fs-tester.in
>> +++ b/tests/util/grub-fs-tester.in
>> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>       xsquash*)
>>           MINBLKSIZE=4096
>>           MAXBLKSIZE=1048576;;
>> -     xxfs)
>> +     xxfs|xf2fs)
>>           MINBLKSIZE=$SECSIZE
>>               # OS Limitation: GNU/Linux doesn't accept > 4096
>>           MAXBLKSIZE=4096;;
>> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>               x"btrfs"*)
>>                   FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>>
>> +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
>> +             x"f2fs")
>> +                 FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
>> +
>>           # FS LIMITATION: exfat is at most 15 UTF-16 chars
>>               x"exfat")
>>                   FSLABEL="géт ;/莭莽😁кир";;
>> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>>       # FS LIMITATION: as far as I know those FS don't store their last modification date.
>>               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
>> -                 | 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=y;;
>> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>>                   MOUNTFS=ext2
>>                   "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)
>> --
>> 2.6.3
>>
>>
>> ------------------------------------------------------------------------------
>> Site24x7 APM Insight: Get Deep Visibility into Application Performance
>> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
>> Monitor end-to-end web transactions and take corrective actions now
>> Troubleshoot faster and improve end-user experience. Signup Now!
>> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
>> _______________________________________________
>> Linux-f2fs-devel mailing list
>> Linux-f2fs-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [2.02] Re: [PATCH v8] F2FS support
@ 2016-08-05 10:57                       ` Andrei Borzenkov
  0 siblings, 0 replies; 77+ messages in thread
From: Andrei Borzenkov @ 2016-08-05 10:57 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: The development of GNU GRUB, linux-f2fs-devel

Vladimir prefers to postpone new features until 2.02 is out. From my
side I think it is isolated enough and new code so low regression
risk. Vladimir has the final word as maintainer :)

On Thu, Aug 4, 2016 at 8:06 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:
> Hi Andrei,
>
> It's been sitting for a long long time.
> May I ask whether or not there is any merging plan of this?
>
> Thanks,
>
> On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
>> Change log from v7:
>>  - fix an offset bug in read_file
>>
>> -- >8 --
>> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
>> From: Jaegeuk Kim <jaegeuk@kernel.org>
>> Date: Tue, 17 Nov 2015 12:45:13 -0800
>> Subject: [PATCH] F2FS support
>>
>> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
>> into Linux kernel v3.8 in 2013.
>>
>> The motive for F2FS was to build a file system that from the start, takes into
>> account the characteristics of NAND flash memory-based storage devices (such as
>> solid-state disks, eMMC, and SD cards).
>>
>> F2FS was designed on a basis of a log-structured file system approach, which
>> remedies some known issues of the older log structured file systems, such as
>> the snowball effect of wandering trees and high cleaning overhead. In addition,
>> since a NAND-based storage device shows different characteristics according to
>> its internal geometry or flash memory management scheme (such as the Flash
>> Translation Layer or FTL), it supports various parameters not only for
>> configuring on-disk layout, but also for selecting allocation and cleaning
>> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
>>
>> The source codes for F2FS are available from:
>>
>> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
>> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
>>
>> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
>> ---
>>  Makefile.util.def            |    7 +
>>  docs/grub.texi               |    5 +-
>>  grub-core/Makefile.core.def  |    5 +
>>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
>>  po/exclude.pot               |    1 +
>>  tests/f2fs_test.in           |   19 +
>>  tests/util/grub-fs-tester.in |   10 +-
>>  7 files changed, 1332 insertions(+), 4 deletions(-)
>>  create mode 100644 grub-core/fs/f2fs.c
>>  create mode 100644 tests/f2fs_test.in
>>
>> diff --git a/Makefile.util.def b/Makefile.util.def
>> index db7e8ec..84627bb 100644
>> --- a/Makefile.util.def
>> +++ b/Makefile.util.def
>> @@ -99,6 +99,7 @@ library = {
>>    common = grub-core/fs/ext2.c;
>>    common = grub-core/fs/fat.c;
>>    common = grub-core/fs/exfat.c;
>> +  common = grub-core/fs/f2fs.c;
>>    common = grub-core/fs/fshelp.c;
>>    common = grub-core/fs/hfs.c;
>>    common = grub-core/fs/hfsplus.c;
>> @@ -766,6 +767,12 @@ script = {
>>
>>  script = {
>>    testcase;
>> +  name = f2fs_test;
>> +  common = tests/f2fs_test.in;
>> +};
>> +
>> +script = {
>> +  testcase;
>>    name = nilfs2_test;
>>    common = tests/nilfs2_test.in;
>>  };
>> diff --git a/docs/grub.texi b/docs/grub.texi
>> index 1df3db2..e5a80f3 100644
>> --- a/docs/grub.texi
>> +++ b/docs/grub.texi
>> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
>> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
>> +@dfn{f2fs}, @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},
>> @@ -5347,7 +5348,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, minix,
>>  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 charset
>>  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 d9fa0e3..b585ade 100644
>> --- a/grub-core/Makefile.core.def
>> +++ b/grub-core/Makefile.core.def
>> @@ -1278,6 +1278,11 @@ module = {
>>  };
>>
>>  module = {
>> +  name = f2fs;
>> +  common = fs/f2fs.c;
>> +};
>> +
>> +module = {
>>    name = fshelp;
>>    common = fs/fshelp.c;
>>  };
>> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
>> new file mode 100644
>> index 0000000..7fb256f
>> --- /dev/null
>> +++ b/grub-core/fs/f2fs.c
>> @@ -0,0 +1,1289 @@
>> +/*
>> + *  f2fs.c - Flash-Friendly File System
>> + *
>> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
>> + *
>> + *  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 <http://www.gnu.org/licenses/>.
>> + */
>> +#include <grub/err.h>
>> +#include <grub/file.h>
>> +#include <grub/mm.h>
>> +#include <grub/misc.h>
>> +#include <grub/disk.h>
>> +#include <grub/dl.h>
>> +#include <grub/types.h>
>> +#include <grub/charset.h>
>> +#include <grub/fshelp.h>
>> +
>> +GRUB_MOD_LICENSE ("GPLv3+");
>> +
>> +/* F2FS Magic Number */
>> +#define F2FS_SUPER_MAGIC     0xF2F52010
>> +#define CHECKSUM_OFFSET              4092            /* must be aligned 4 bytes */
>> +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
>> +
>> +/* byte-size offset */
>> +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
>> +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
>> +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
>> +                                             GRUB_DISK_SECTOR_BITS)
>> +
>> +/* 9 bits for 512 bytes */
>> +#define F2FS_MIN_LOG_SECTOR_SIZE     9
>> +
>> +/* support only 4KB block */
>> +#define F2FS_BLK_BITS        12
>> +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
>> +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
>> +
>> +#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  (sizeof(struct grub_f2fs_nat_jent))
>> +#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 (sizeof(struct grub_f2fs_nat_entry))
>> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
>> +
>> +#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
>> +
>> +#define CKPT_FLAG_SET(ckpt, f)       \
>> +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
>> +
>> +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
>> +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
>> +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
>> +#define F2FS_DATA_EXIST              0x08    /* file inline data exist flag */
>> +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot dentries */
>> +
>> +enum FILE_TYPE
>> +{
>> +  F2FS_FT_UNKNOWN,
>> +  F2FS_FT_REG_FILE = 1,
>> +  F2FS_FT_DIR = 2,
>> +  F2FS_FT_SYMLINK = 7,
>> +};
>> +
>> +#define MAX_VOLUME_NAME              512
>> +
>> +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[MAX_VOLUME_NAME];
>> +  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;
>> +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE totally */
>> +  };
>> +  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_uint8_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
>> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
>> +{
>> +  return addr[nr >> 3] & (1 << (nr & 7));
>> +}
>> +
>> +static inline char *
>> +__inline_addr (struct grub_f2fs_inode *inode)
>> +{
>> +  return (char *)&inode->i_addr[1];
>> +}
>> +
>> +static inline grub_uint64_t
>> +grub_f2fs_file_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 = &data->ckpt;
>> +  grub_uint32_t start_addr = data->cp_blkaddr;
>> +
>> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
>> +
>> +  return __start_cp_addr (data) +
>> +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
>> +     - (base + 1) + type;
>> +}
>> +
>> +static inline void *
>> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
>> +{
>> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
>> +  grub_uint32_t offset;
>> +
>> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
>> +    return ckpt->sit_nat_version_bitmap;
>> +
>> +  offset = 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 inode_block)
>> +{
>> +  if (inode_block)
>> +    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,
>> +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
>> +             0, F2FS_BLKSIZE, buf);
>> +}
>> +
>> +/*
>> + * CRC32
>> +*/
>> +#define CRCPOLY_LE 0xedb88320
>> +
>> +static inline grub_uint32_t
>> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
>> +{
>> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
>> +  unsigned char *p = (unsigned char *)buf;
>> +  grub_uint32_t tmp = len;
>> +  int i;
>> +
>> +  while (tmp--)
>> +    {
>> +      crc ^= *p++;
>> +      for (i = 0; i < 8; i++)
>> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
>> +    }
>> +  return crc;
>> +}
>> +
>> +static inline int
>> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
>> +{
>> +  grub_uint32_t cal_crc = 0;
>> +
>> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
>> +
>> +  return (cal_crc == blk_crc) ? 1 : 0;
>> +}
>> +
>> +static inline int
>> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
>> +{
>> +  int mask;
>> +
>> +  p += (nr >> 3);
>> +  mask = 1 << (7 - (nr & 0x07));
>> +  return mask & *p;
>> +}
>> +
>> +static int
>> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
>> +{
>> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
>> +
>> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
>> +    return -1;
>> +
>> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
>> +    return -1;
>> +
>> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
>> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
>> +
>> +  if (log_sectorsize > F2FS_BLK_BITS)
>> +    return -1;
>> +
>> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
>> +    return -1;
>> +
>> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
>> +    return -1;
>> +
>> +  return 0;
>> +}
>> +
>> +static int
>> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
>> +{
>> +  grub_disk_t disk = data->disk;
>> +  grub_err_t err;
>> +
>> +  /* Read first super block. */
>> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
>> +  if (err)
>> +    return -1;
>> +
>> +  return grub_f2fs_sanity_check_sb (&data->sblock);
>> +}
>> +
>> +static void *
>> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
>> +     grub_uint64_t *version)
>> +{
>> +  grub_uint32_t *cp_page_1, *cp_page_2;
>> +  struct grub_f2fs_checkpoint *cp_block;
>> +  grub_uint64_t cur_version = 0, pre_version = 0;
>> +  grub_uint32_t crc = 0;
>> +  grub_uint32_t crc_offset;
>> +  grub_err_t err;
>> +
>> +  /* Read the 1st cp block in this CP pack */
>> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
>> +  if (!cp_page_1)
>> +    return NULL;
>> +
>> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
>> +  if (err)
>> +    goto invalid_cp1;
>> +
>> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
>> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
>> +  if (crc_offset != CHECKSUM_OFFSET)
>> +    goto invalid_cp1;
>> +
>> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
>> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
>> +    goto invalid_cp1;
>> +
>> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
>> +
>> +  /* Read the 2nd cp block in this CP pack */
>> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
>> +  if (!cp_page_2)
>> +    goto invalid_cp1;
>> +
>> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
>> +
>> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
>> +  if (err)
>> +    goto invalid_cp2;
>> +
>> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
>> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
>> +  if (crc_offset != CHECKSUM_OFFSET)
>> +    goto invalid_cp2;
>> +
>> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
>> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
>> +    goto invalid_cp2;
>> +
>> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
>> +  if (cur_version == pre_version)
>> +    {
>> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
>> +  cp1 = 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 += data->blocks_per_seg;
>> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
>> +  if (!cp2 && grub_errno)
>> +    {
>> +      grub_free (cp1);
>> +      return grub_errno;
>> +    }
>> +
>> +  if (cp1 && cp2)
>> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
>> +  else if (cp1)
>> +    cur_page = cp1;
>> +  else if (cp2)
>> +    cur_page = cp2;
>> +  else
>> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
>> +
>> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
>> +
>> +  grub_free (cp1);
>> +  grub_free (cp2);
>> +  return 0;
>> +}
>> +
>> +static grub_err_t
>> +get_nat_journal (struct grub_f2fs_data *data)
>> +{
>> +  grub_uint32_t block;
>> +  char *buf;
>> +  grub_err_t err;
>> +
>> +  buf = grub_malloc (F2FS_BLKSIZE);
>> +  if (!buf)
>> +    return grub_errno;
>> +
>> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
>> +    block = __start_sum_block (data);
>> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
>> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
>> +  else
>> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
>> +
>> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
>> +  grub_uint32_t blkaddr = 0;
>> +  grub_uint16_t i;
>> +
>> +  for (i = 0; i < n; i++)
>> +    {
>> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
>> +        {
>> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
>> +  if (blkaddr)
>> +    return blkaddr;
>> +
>> +  nat_block = grub_malloc (F2FS_BLKSIZE);
>> +  if (!nat_block)
>> +    return 0;
>> +
>> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
>> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
>> +
>> +  seg_off = block_off / data->blocks_per_seg;
>> +  block_addr = 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 += data->blocks_per_seg;
>> +
>> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
>> +  if (err)
>> +    {
>> +      grub_free (nat_block);
>> +      return 0;
>> +    }
>> +
>> +  blkaddr = 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 = ADDRS_PER_BLOCK;
>> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
>> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
>> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
>> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
>> +  int n = 0;
>> +  int level = 0;
>> +
>> +  if (inode->i_inline & F2FS_INLINE_XATTR)
>> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
>> +
>> +  noffset[0] = 0;
>> +
>> +  if (block < direct_index)
>> +    {
>> +      offset[n] = block;
>> +      goto got;
>> +    }
>> +
>> +  block -= direct_index;
>> +  if (block < direct_blks)
>> +    {
>> +      offset[n++] = NODE_DIR1_BLOCK;
>> +      noffset[n] = 1;
>> +      offset[n] = block;
>> +      level = 1;
>> +      goto got;
>> +    }
>> +
>> +  block -= direct_blks;
>> +  if (block < direct_blks)
>> +    {
>> +      offset[n++] = NODE_DIR2_BLOCK;
>> +      noffset[n] = 2;
>> +      offset[n] = block;
>> +      level = 1;
>> +      goto got;
>> +    }
>> +
>> +  block -= direct_blks;
>> +  if (block < indirect_blks)
>> +    {
>> +      offset[n++] = NODE_IND1_BLOCK;
>> +      noffset[n] = 3;
>> +      offset[n++] = block / direct_blks;
>> +      noffset[n] = 4 + offset[n - 1];
>> +      offset[n] = block % direct_blks;
>> +      level = 2;
>> +      goto got;
>> +    }
>> +
>> +  block -= indirect_blks;
>> +  if (block < indirect_blks)
>> +    {
>> +      offset[n++] = NODE_IND2_BLOCK;
>> +      noffset[n] = 4 + dptrs_per_blk;
>> +      offset[n++] = block / direct_blks;
>> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
>> +      offset[n] = block % direct_blks;
>> +      level = 2;
>> +      goto got;
>> +    }
>> +
>> +  block -= indirect_blks;
>> +  if (block < dindirect_blks)
>> +    {
>> +      offset[n++] = NODE_DIND_BLOCK;
>> +      noffset[n] = 5 + (dptrs_per_blk * 2);
>> +      offset[n++] = block / indirect_blks;
>> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
>> +      offset[n - 1] * (dptrs_per_blk + 1);
>> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
>> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
>> +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
>> +      offset[n] = block % direct_blks;
>> +      level = 3;
>> +      goto got;
>> +    }
>> +got:
>> +  return level;
>> +}
>> +
>> +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 = 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 = grub_malloc (sizeof (*data));
>> +  if (!data)
>> +    return NULL;
>> +
>> +  data->disk = disk;
>> +
>> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
>> +    {
>> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
>> +        {
>> +          if (grub_errno == GRUB_ERR_NONE)
>> +            grub_error (GRUB_ERR_BAD_FS,
>> +                     "not a F2FS filesystem (no superblock)");
>> +          goto fail;
>> +        }
>> +    }
>> +
>> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
>> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
>> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
>> +  data->blocks_per_seg = 1 <<
>> +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
>> +
>> +  err = grub_f2fs_read_cp (data);
>> +  if (err)
>> +    goto fail;
>> +
>> +  data->nat_bitmap = __nat_bitmap_ptr (data);
>> +
>> +  err = get_nat_journal (data);
>> +  if (err)
>> +    goto fail;
>> +
>> +  data->diropen.data = data;
>> +  data->diropen.ino = data->root_ino;
>> +  data->diropen.inode_read = 1;
>> +  data->inode = &data->diropen.inode;
>> +
>> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
>> +  if (err)
>> +    goto fail;
>> +
>> +  return data;
>> +
>> +fail:
>> +  grub_free (data);
>> +  return NULL;
>> +}
>> +
>> +/* guarantee inline_data was handled by caller */
>> +static grub_disk_addr_t
>> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
>> +{
>> +  struct grub_f2fs_data *data = node->data;
>> +  struct grub_f2fs_inode *inode = &node->inode.i;
>> +  grub_uint32_t offset[4], noffset[4], nids[4];
>> +  struct grub_f2fs_node *node_block;
>> +  grub_uint32_t block_addr = -1;
>> +  int level, i;
>> +
>> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
>> +  if (level == 0)
>> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
>> +
>> +  node_block = grub_malloc (F2FS_BLKSIZE);
>> +  if (!node_block)
>> +    return -1;
>> +
>> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
>> +
>> +  /* get indirect or direct nodes */
>> +  for (i = 1; i <= level; i++)
>> +    {
>> +      grub_f2fs_read_node (data, nids[i], node_block);
>> +      if (grub_errno)
>> +        goto fail;
>> +
>> +      if (i < level)
>> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
>> +    }
>> +
>> +  block_addr = 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 = &node->inode.i;
>> +  grub_off_t filesize = grub_f2fs_file_size (inode);
>> +  char *inline_addr = __inline_addr (inode);
>> +
>> +  if (inode->i_inline & F2FS_INLINE_DATA)
>> +    {
>> +      if (filesize > MAX_INLINE_DATA)
>> +        return -1;
>> +      if (len > filesize - pos)
>> +        len = filesize - pos;
>> +
>> +      grub_memcpy (buf, 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_get_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 = node;
>> +  grub_uint64_t filesize;
>> +
>> +  if (!diro->inode_read)
>> +    {
>> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
>> +      if (grub_errno)
>> +        return 0;
>> +    }
>> +
>> +  filesize = grub_f2fs_file_size(&diro->inode.i);
>> +
>> +  symlink = grub_malloc (filesize + 1);
>> +  if (!symlink)
>> +    return 0;
>> +
>> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
>> +  if (grub_errno)
>> +    {
>> +      grub_free (symlink);
>> +      return 0;
>> +    }
>> +
>> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
>> +    {
>> +      char *filename;
>> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
>> +      enum FILE_TYPE ftype;
>> +      int name_len;
>> +      int ret;
>> +
>> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
>> +        {
>> +          i++;
>> +          continue;
>> +        }
>> +
>> +      ftype = ctx->dentry[i].file_type;
>> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
>> +      filename = grub_malloc (name_len + 1);
>> +      if (!filename)
>> +        return 0;
>> +
>> +      grub_memcpy (filename, ctx->filename[i], name_len);
>> +      filename[name_len] = 0;
>> +
>> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
>> +      if (!fdiro)
>> +        {
>> +          grub_free(filename);
>> +          return 0;
>> +        }
>> +
>> +      if (ftype == F2FS_FT_DIR)
>> +        type = GRUB_FSHELP_DIR;
>> +      else if (ftype == F2FS_FT_SYMLINK)
>> +        type = GRUB_FSHELP_SYMLINK;
>> +      else if (ftype == F2FS_FT_REG_FILE)
>> +        type = GRUB_FSHELP_REG;
>> +
>> +      fdiro->data = ctx->data;
>> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
>> +      fdiro->inode_read = 0;
>> +
>> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
>> +      grub_free(filename);
>> +      if (ret)
>> +        return 1;
>> +
>> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
>> +
>> +  ctx->bitmap = de_blk->dentry_bitmap;
>> +  ctx->dentry = de_blk->dentry;
>> +  ctx->filename = de_blk->filename;
>> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
>> +  struct grub_f2fs_inode *inode;
>> +  struct grub_f2fs_dir_iter_ctx ctx = {
>> +    .data = diro->data,
>> +    .hook = hook,
>> +    .hook_data = hook_data
>> +  };
>> +  grub_off_t fpos = 0;
>> +
>> +  if (!diro->inode_read)
>> +    {
>> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
>> +      if (grub_errno)
>> +     return 0;
>> +    }
>> +
>> +  inode = &diro->inode.i;
>> +
>> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
>> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
>> +
>> +  while (fpos < grub_f2fs_file_size (inode))
>> +    {
>> +      struct grub_f2fs_dentry_block *de_blk;
>> +      char *buf;
>> +      int ret;
>> +
>> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
>> +
>> +      ctx.bitmap = de_blk->dentry_bitmap;
>> +      ctx.dentry = de_blk->dentry;
>> +      ctx.filename = de_blk->filename;
>> +      ctx.max = NR_DENTRY_IN_BLOCK;
>> +
>> +      ret = grub_f2fs_check_dentries (&ctx);
>> +      grub_free (buf);
>> +      if (ret)
>> +        return 1;
>> +
>> +      fpos += F2FS_BLKSIZE;
>> +    }
>> +  return 0;
>> +}
>> +
>> +static int
>> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
>> +           grub_fshelp_node_t node, void *data)
>> +{
>> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
>> +      grub_errno = GRUB_ERR_NONE;
>> +    }
>> +  if (node->inode_read)
>> +    {
>> +      info.mtimeset = 1;
>> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
>> +    }
>> +
>> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
>> +  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 = {
>> +    .hook = hook,
>> +    .hook_data = hook_data
>> +  };
>> +  struct grub_fshelp_node *fdiro = 0;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  ctx.data = 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 != &ctx.data->diropen)
>> +    grub_free (fdiro);
>> + 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 = NULL;
>> +  struct grub_fshelp_node *fdiro = 0;
>> +  struct grub_f2fs_inode *inode;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  data = 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, sizeof (*data->inode));
>> +  grub_free (fdiro);
>> +
>> +  inode = &(data->inode->i);
>> +  file->size = grub_f2fs_file_size (inode);
>> +  file->data = data;
>> +  file->offset = 0;
>> +
>> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
>> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
>> +  return 0;
>> +
>> +fail:
>> +  if (fdiro != &data->diropen)
>> +    grub_free (fdiro);
>> +  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 = (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 = (struct grub_f2fs_data *) file->data;
>> +
>> +  grub_free (data);
>> +
>> +  grub_dl_unref (my_mod);
>> +
>> +  return GRUB_ERR_NONE;
>> +}
>> +
>> +static grub_uint8_t *
>> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
>> +{
>> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
>> +  grub_uint8_t *out_buf;
>> +  int len = 0;
>> +
>> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
>> +  if (!out_buf)
>> +    return NULL;
>> +
>> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
>> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
>> +    len++;
>> +  }
>> +
>> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
>> +  return out_buf;
>> +}
>> +
>> +static grub_err_t
>> +grub_f2fs_label (grub_device_t device, char **label)
>> +{
>> +  struct grub_f2fs_data *data;
>> +  grub_disk_t disk = device->disk;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  data = grub_f2fs_mount (disk);
>> +  if (data)
>> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
>> +  else
>> +    *label = NULL;
>> +
>> +  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 = device->disk;
>> +
>> +  grub_dl_ref (my_mod);
>> +
>> +  data = grub_f2fs_mount (disk);
>> +  if (data)
>> +    {
>> +      *uuid =
>> +     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 = NULL;
>> +
>> +  grub_free (data);
>> +  grub_dl_unref (my_mod);
>> +  return grub_errno;
>> +}
>> +
>> +static struct grub_fs grub_f2fs_fs = {
>> +  .name = "f2fs",
>> +  .dir = grub_f2fs_dir,
>> +  .open = grub_f2fs_open,
>> +  .read = grub_f2fs_read,
>> +  .close = grub_f2fs_close,
>> +  .label = grub_f2fs_label,
>> +  .uuid = grub_f2fs_uuid,
>> +#ifdef GRUB_UTIL
>> +  .reserved_first_sector = 1,
>> +  .blocklist_install = 0,
>> +#endif
>> +  .next = 0
>> +};
>> +
>> +GRUB_MOD_INIT (f2fs)
>> +{
>> +  grub_fs_register (&grub_f2fs_fs);
>> +  my_mod = mod;
>> +}
>> +
>> +GRUB_MOD_FINI (f2fs)
>> +{
>> +  grub_fs_unregister (&grub_f2fs_fs);
>> +}
>> diff --git a/po/exclude.pot b/po/exclude.pot
>> index 0a9b215..816089c 100644
>> --- a/po/exclude.pot
>> +++ b/po/exclude.pot
>> @@ -1214,6 +1214,7 @@ msgstr ""
>>
>>  #: 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..1ea77c8
>> --- /dev/null
>> +++ b/tests/f2fs_test.in
>> @@ -0,0 +1,19 @@
>> +#!/bin/sh
>> +
>> +set -e
>> +
>> +if [ "x$EUID" = "x" ] ; then
>> + EUID=`id -u`
>> +fi
>> +
>> +if [ "$EUID" != 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 424de22..e3e4109 100644
>> --- a/tests/util/grub-fs-tester.in
>> +++ b/tests/util/grub-fs-tester.in
>> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>       xsquash*)
>>           MINBLKSIZE=4096
>>           MAXBLKSIZE=1048576;;
>> -     xxfs)
>> +     xxfs|xf2fs)
>>           MINBLKSIZE=$SECSIZE
>>               # OS Limitation: GNU/Linux doesn't accept > 4096
>>           MAXBLKSIZE=4096;;
>> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>               x"btrfs"*)
>>                   FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
>>
>> +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
>> +             x"f2fs")
>> +                 FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
>> +
>>           # FS LIMITATION: exfat is at most 15 UTF-16 chars
>>               x"exfat")
>>                   FSLABEL="géт ;/莭莽😁кир";;
>> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
>>       # FS LIMITATION: as far as I know those FS don't store their last modification date.
>>               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
>> -                 | 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=y;;
>> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
>>                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
>>                   MOUNTFS=ext2
>>                   "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)
>> --
>> 2.6.3
>>
>>
>> ------------------------------------------------------------------------------
>> Site24x7 APM Insight: Get Deep Visibility into Application Performance
>> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
>> Monitor end-to-end web transactions and take corrective actions now
>> Troubleshoot faster and improve end-user experience. Signup Now!
>> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
>> _______________________________________________
>> Linux-f2fs-devel mailing list
>> Linux-f2fs-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [2.02] Re: [PATCH v8] F2FS support
  2016-08-05 10:57                       ` [f2fs-dev] " Andrei Borzenkov
@ 2016-08-05 18:07                         ` Jaegeuk Kim
  -1 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-08-05 18:07 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

On Fri, Aug 05, 2016 at 01:57:37PM +0300, Andrei Borzenkov wrote:
> Vladimir prefers to postpone new features until 2.02 is out. From my
> side I think it is isolated enough and new code so low regression
> risk. Vladimir has the final word as maintainer :)

Okay, I can wait for 2.02. ;)
BTW, I've heard that openmandriva adopted this patch to support F2FS in their
installer.

Thanks,

> 
> On Thu, Aug 4, 2016 at 8:06 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:
> > Hi Andrei,
> >
> > It's been sitting for a long long time.
> > May I ask whether or not there is any merging plan of this?
> >
> > Thanks,
> >
> > On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
> >> Change log from v7:
> >>  - fix an offset bug in read_file
> >>
> >> -- >8 --
> >> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
> >> From: Jaegeuk Kim <jaegeuk@kernel.org>
> >> Date: Tue, 17 Nov 2015 12:45:13 -0800
> >> Subject: [PATCH] F2FS support
> >>
> >> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> >> into Linux kernel v3.8 in 2013.
> >>
> >> The motive for F2FS was to build a file system that from the start, takes into
> >> account the characteristics of NAND flash memory-based storage devices (such as
> >> solid-state disks, eMMC, and SD cards).
> >>
> >> F2FS was designed on a basis of a log-structured file system approach, which
> >> remedies some known issues of the older log structured file systems, such as
> >> the snowball effect of wandering trees and high cleaning overhead. In addition,
> >> since a NAND-based storage device shows different characteristics according to
> >> its internal geometry or flash memory management scheme (such as the Flash
> >> Translation Layer or FTL), it supports various parameters not only for
> >> configuring on-disk layout, but also for selecting allocation and cleaning
> >> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> >>
> >> The source codes for F2FS are available from:
> >>
> >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> >>
> >> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> >> ---
> >>  Makefile.util.def            |    7 +
> >>  docs/grub.texi               |    5 +-
> >>  grub-core/Makefile.core.def  |    5 +
> >>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
> >>  po/exclude.pot               |    1 +
> >>  tests/f2fs_test.in           |   19 +
> >>  tests/util/grub-fs-tester.in |   10 +-
> >>  7 files changed, 1332 insertions(+), 4 deletions(-)
> >>  create mode 100644 grub-core/fs/f2fs.c
> >>  create mode 100644 tests/f2fs_test.in
> >>
> >> diff --git a/Makefile.util.def b/Makefile.util.def
> >> index db7e8ec..84627bb 100644
> >> --- a/Makefile.util.def
> >> +++ b/Makefile.util.def
> >> @@ -99,6 +99,7 @@ library = {
> >>    common = grub-core/fs/ext2.c;
> >>    common = grub-core/fs/fat.c;
> >>    common = grub-core/fs/exfat.c;
> >> +  common = grub-core/fs/f2fs.c;
> >>    common = grub-core/fs/fshelp.c;
> >>    common = grub-core/fs/hfs.c;
> >>    common = grub-core/fs/hfsplus.c;
> >> @@ -766,6 +767,12 @@ script = {
> >>
> >>  script = {
> >>    testcase;
> >> +  name = f2fs_test;
> >> +  common = tests/f2fs_test.in;
> >> +};
> >> +
> >> +script = {
> >> +  testcase;
> >>    name = nilfs2_test;
> >>    common = tests/nilfs2_test.in;
> >>  };
> >> diff --git a/docs/grub.texi b/docs/grub.texi
> >> index 1df3db2..e5a80f3 100644
> >> --- a/docs/grub.texi
> >> +++ b/docs/grub.texi
> >> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
> >> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> >> +@dfn{f2fs}, @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},
> >> @@ -5347,7 +5348,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, minix,
> >>  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 charset
> >>  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 d9fa0e3..b585ade 100644
> >> --- a/grub-core/Makefile.core.def
> >> +++ b/grub-core/Makefile.core.def
> >> @@ -1278,6 +1278,11 @@ module = {
> >>  };
> >>
> >>  module = {
> >> +  name = f2fs;
> >> +  common = fs/f2fs.c;
> >> +};
> >> +
> >> +module = {
> >>    name = fshelp;
> >>    common = fs/fshelp.c;
> >>  };
> >> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> >> new file mode 100644
> >> index 0000000..7fb256f
> >> --- /dev/null
> >> +++ b/grub-core/fs/f2fs.c
> >> @@ -0,0 +1,1289 @@
> >> +/*
> >> + *  f2fs.c - Flash-Friendly File System
> >> + *
> >> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> >> + *
> >> + *  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 <http://www.gnu.org/licenses/>.
> >> + */
> >> +#include <grub/err.h>
> >> +#include <grub/file.h>
> >> +#include <grub/mm.h>
> >> +#include <grub/misc.h>
> >> +#include <grub/disk.h>
> >> +#include <grub/dl.h>
> >> +#include <grub/types.h>
> >> +#include <grub/charset.h>
> >> +#include <grub/fshelp.h>
> >> +
> >> +GRUB_MOD_LICENSE ("GPLv3+");
> >> +
> >> +/* F2FS Magic Number */
> >> +#define F2FS_SUPER_MAGIC     0xF2F52010
> >> +#define CHECKSUM_OFFSET              4092            /* must be aligned 4 bytes */
> >> +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> >> +
> >> +/* byte-size offset */
> >> +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> >> +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
> >> +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> >> +                                             GRUB_DISK_SECTOR_BITS)
> >> +
> >> +/* 9 bits for 512 bytes */
> >> +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> >> +
> >> +/* support only 4KB block */
> >> +#define F2FS_BLK_BITS        12
> >> +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> >> +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> >> +
> >> +#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  (sizeof(struct grub_f2fs_nat_jent))
> >> +#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 (sizeof(struct grub_f2fs_nat_entry))
> >> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> >> +
> >> +#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
> >> +
> >> +#define CKPT_FLAG_SET(ckpt, f)       \
> >> +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> >> +
> >> +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> >> +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> >> +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> >> +#define F2FS_DATA_EXIST              0x08    /* file inline data exist flag */
> >> +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot dentries */
> >> +
> >> +enum FILE_TYPE
> >> +{
> >> +  F2FS_FT_UNKNOWN,
> >> +  F2FS_FT_REG_FILE = 1,
> >> +  F2FS_FT_DIR = 2,
> >> +  F2FS_FT_SYMLINK = 7,
> >> +};
> >> +
> >> +#define MAX_VOLUME_NAME              512
> >> +
> >> +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[MAX_VOLUME_NAME];
> >> +  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;
> >> +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE totally */
> >> +  };
> >> +  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_uint8_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
> >> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> >> +{
> >> +  return addr[nr >> 3] & (1 << (nr & 7));
> >> +}
> >> +
> >> +static inline char *
> >> +__inline_addr (struct grub_f2fs_inode *inode)
> >> +{
> >> +  return (char *)&inode->i_addr[1];
> >> +}
> >> +
> >> +static inline grub_uint64_t
> >> +grub_f2fs_file_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 = &data->ckpt;
> >> +  grub_uint32_t start_addr = data->cp_blkaddr;
> >> +
> >> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> >> +
> >> +  return __start_cp_addr (data) +
> >> +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> >> +     - (base + 1) + type;
> >> +}
> >> +
> >> +static inline void *
> >> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> >> +{
> >> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> >> +  grub_uint32_t offset;
> >> +
> >> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> >> +    return ckpt->sit_nat_version_bitmap;
> >> +
> >> +  offset = 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 inode_block)
> >> +{
> >> +  if (inode_block)
> >> +    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,
> >> +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> >> +             0, F2FS_BLKSIZE, buf);
> >> +}
> >> +
> >> +/*
> >> + * CRC32
> >> +*/
> >> +#define CRCPOLY_LE 0xedb88320
> >> +
> >> +static inline grub_uint32_t
> >> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> >> +{
> >> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> >> +  unsigned char *p = (unsigned char *)buf;
> >> +  grub_uint32_t tmp = len;
> >> +  int i;
> >> +
> >> +  while (tmp--)
> >> +    {
> >> +      crc ^= *p++;
> >> +      for (i = 0; i < 8; i++)
> >> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> >> +    }
> >> +  return crc;
> >> +}
> >> +
> >> +static inline int
> >> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> >> +{
> >> +  grub_uint32_t cal_crc = 0;
> >> +
> >> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> >> +
> >> +  return (cal_crc == blk_crc) ? 1 : 0;
> >> +}
> >> +
> >> +static inline int
> >> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> >> +{
> >> +  int mask;
> >> +
> >> +  p += (nr >> 3);
> >> +  mask = 1 << (7 - (nr & 0x07));
> >> +  return mask & *p;
> >> +}
> >> +
> >> +static int
> >> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> >> +{
> >> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> >> +
> >> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> >> +    return -1;
> >> +
> >> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> >> +    return -1;
> >> +
> >> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> >> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> >> +
> >> +  if (log_sectorsize > F2FS_BLK_BITS)
> >> +    return -1;
> >> +
> >> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> >> +    return -1;
> >> +
> >> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> >> +    return -1;
> >> +
> >> +  return 0;
> >> +}
> >> +
> >> +static int
> >> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> >> +{
> >> +  grub_disk_t disk = data->disk;
> >> +  grub_err_t err;
> >> +
> >> +  /* Read first super block. */
> >> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
> >> +  if (err)
> >> +    return -1;
> >> +
> >> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> >> +}
> >> +
> >> +static void *
> >> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> >> +     grub_uint64_t *version)
> >> +{
> >> +  grub_uint32_t *cp_page_1, *cp_page_2;
> >> +  struct grub_f2fs_checkpoint *cp_block;
> >> +  grub_uint64_t cur_version = 0, pre_version = 0;
> >> +  grub_uint32_t crc = 0;
> >> +  grub_uint32_t crc_offset;
> >> +  grub_err_t err;
> >> +
> >> +  /* Read the 1st cp block in this CP pack */
> >> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!cp_page_1)
> >> +    return NULL;
> >> +
> >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> >> +  if (err)
> >> +    goto invalid_cp1;
> >> +
> >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> >> +  if (crc_offset != CHECKSUM_OFFSET)
> >> +    goto invalid_cp1;
> >> +
> >> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> >> +    goto invalid_cp1;
> >> +
> >> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> >> +
> >> +  /* Read the 2nd cp block in this CP pack */
> >> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!cp_page_2)
> >> +    goto invalid_cp1;
> >> +
> >> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> >> +
> >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> >> +  if (err)
> >> +    goto invalid_cp2;
> >> +
> >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> >> +  if (crc_offset != CHECKSUM_OFFSET)
> >> +    goto invalid_cp2;
> >> +
> >> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> >> +    goto invalid_cp2;
> >> +
> >> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> >> +  if (cur_version == pre_version)
> >> +    {
> >> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> >> +  cp1 = 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 += data->blocks_per_seg;
> >> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> >> +  if (!cp2 && grub_errno)
> >> +    {
> >> +      grub_free (cp1);
> >> +      return grub_errno;
> >> +    }
> >> +
> >> +  if (cp1 && cp2)
> >> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> >> +  else if (cp1)
> >> +    cur_page = cp1;
> >> +  else if (cp2)
> >> +    cur_page = cp2;
> >> +  else
> >> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> >> +
> >> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> >> +
> >> +  grub_free (cp1);
> >> +  grub_free (cp2);
> >> +  return 0;
> >> +}
> >> +
> >> +static grub_err_t
> >> +get_nat_journal (struct grub_f2fs_data *data)
> >> +{
> >> +  grub_uint32_t block;
> >> +  char *buf;
> >> +  grub_err_t err;
> >> +
> >> +  buf = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!buf)
> >> +    return grub_errno;
> >> +
> >> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> >> +    block = __start_sum_block (data);
> >> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> >> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> >> +  else
> >> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> >> +
> >> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> >> +  grub_uint32_t blkaddr = 0;
> >> +  grub_uint16_t i;
> >> +
> >> +  for (i = 0; i < n; i++)
> >> +    {
> >> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> >> +        {
> >> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> >> +  if (blkaddr)
> >> +    return blkaddr;
> >> +
> >> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!nat_block)
> >> +    return 0;
> >> +
> >> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> >> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> >> +
> >> +  seg_off = block_off / data->blocks_per_seg;
> >> +  block_addr = 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 += data->blocks_per_seg;
> >> +
> >> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> >> +  if (err)
> >> +    {
> >> +      grub_free (nat_block);
> >> +      return 0;
> >> +    }
> >> +
> >> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> >> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> >> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> >> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> >> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> >> +  int n = 0;
> >> +  int level = 0;
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> >> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> >> +
> >> +  noffset[0] = 0;
> >> +
> >> +  if (block < direct_index)
> >> +    {
> >> +      offset[n] = block;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= direct_index;
> >> +  if (block < direct_blks)
> >> +    {
> >> +      offset[n++] = NODE_DIR1_BLOCK;
> >> +      noffset[n] = 1;
> >> +      offset[n] = block;
> >> +      level = 1;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= direct_blks;
> >> +  if (block < direct_blks)
> >> +    {
> >> +      offset[n++] = NODE_DIR2_BLOCK;
> >> +      noffset[n] = 2;
> >> +      offset[n] = block;
> >> +      level = 1;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= direct_blks;
> >> +  if (block < indirect_blks)
> >> +    {
> >> +      offset[n++] = NODE_IND1_BLOCK;
> >> +      noffset[n] = 3;
> >> +      offset[n++] = block / direct_blks;
> >> +      noffset[n] = 4 + offset[n - 1];
> >> +      offset[n] = block % direct_blks;
> >> +      level = 2;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= indirect_blks;
> >> +  if (block < indirect_blks)
> >> +    {
> >> +      offset[n++] = NODE_IND2_BLOCK;
> >> +      noffset[n] = 4 + dptrs_per_blk;
> >> +      offset[n++] = block / direct_blks;
> >> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> >> +      offset[n] = block % direct_blks;
> >> +      level = 2;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= indirect_blks;
> >> +  if (block < dindirect_blks)
> >> +    {
> >> +      offset[n++] = NODE_DIND_BLOCK;
> >> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> >> +      offset[n++] = block / indirect_blks;
> >> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> >> +      offset[n - 1] * (dptrs_per_blk + 1);
> >> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> >> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> >> +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> >> +      offset[n] = block % direct_blks;
> >> +      level = 3;
> >> +      goto got;
> >> +    }
> >> +got:
> >> +  return level;
> >> +}
> >> +
> >> +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 = 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 = grub_malloc (sizeof (*data));
> >> +  if (!data)
> >> +    return NULL;
> >> +
> >> +  data->disk = disk;
> >> +
> >> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> >> +    {
> >> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> >> +        {
> >> +          if (grub_errno == GRUB_ERR_NONE)
> >> +            grub_error (GRUB_ERR_BAD_FS,
> >> +                     "not a F2FS filesystem (no superblock)");
> >> +          goto fail;
> >> +        }
> >> +    }
> >> +
> >> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> >> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> >> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> >> +  data->blocks_per_seg = 1 <<
> >> +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> >> +
> >> +  err = grub_f2fs_read_cp (data);
> >> +  if (err)
> >> +    goto fail;
> >> +
> >> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> >> +
> >> +  err = get_nat_journal (data);
> >> +  if (err)
> >> +    goto fail;
> >> +
> >> +  data->diropen.data = data;
> >> +  data->diropen.ino = data->root_ino;
> >> +  data->diropen.inode_read = 1;
> >> +  data->inode = &data->diropen.inode;
> >> +
> >> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> >> +  if (err)
> >> +    goto fail;
> >> +
> >> +  return data;
> >> +
> >> +fail:
> >> +  grub_free (data);
> >> +  return NULL;
> >> +}
> >> +
> >> +/* guarantee inline_data was handled by caller */
> >> +static grub_disk_addr_t
> >> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> >> +{
> >> +  struct grub_f2fs_data *data = node->data;
> >> +  struct grub_f2fs_inode *inode = &node->inode.i;
> >> +  grub_uint32_t offset[4], noffset[4], nids[4];
> >> +  struct grub_f2fs_node *node_block;
> >> +  grub_uint32_t block_addr = -1;
> >> +  int level, i;
> >> +
> >> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> >> +  if (level == 0)
> >> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> >> +
> >> +  node_block = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!node_block)
> >> +    return -1;
> >> +
> >> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> >> +
> >> +  /* get indirect or direct nodes */
> >> +  for (i = 1; i <= level; i++)
> >> +    {
> >> +      grub_f2fs_read_node (data, nids[i], node_block);
> >> +      if (grub_errno)
> >> +        goto fail;
> >> +
> >> +      if (i < level)
> >> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> >> +    }
> >> +
> >> +  block_addr = 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 = &node->inode.i;
> >> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> >> +  char *inline_addr = __inline_addr (inode);
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_DATA)
> >> +    {
> >> +      if (filesize > MAX_INLINE_DATA)
> >> +        return -1;
> >> +      if (len > filesize - pos)
> >> +        len = filesize - pos;
> >> +
> >> +      grub_memcpy (buf, 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_get_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 = node;
> >> +  grub_uint64_t filesize;
> >> +
> >> +  if (!diro->inode_read)
> >> +    {
> >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> >> +      if (grub_errno)
> >> +        return 0;
> >> +    }
> >> +
> >> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> >> +
> >> +  symlink = grub_malloc (filesize + 1);
> >> +  if (!symlink)
> >> +    return 0;
> >> +
> >> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> >> +  if (grub_errno)
> >> +    {
> >> +      grub_free (symlink);
> >> +      return 0;
> >> +    }
> >> +
> >> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> >> +    {
> >> +      char *filename;
> >> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> >> +      enum FILE_TYPE ftype;
> >> +      int name_len;
> >> +      int ret;
> >> +
> >> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> >> +        {
> >> +          i++;
> >> +          continue;
> >> +        }
> >> +
> >> +      ftype = ctx->dentry[i].file_type;
> >> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> >> +      filename = grub_malloc (name_len + 1);
> >> +      if (!filename)
> >> +        return 0;
> >> +
> >> +      grub_memcpy (filename, ctx->filename[i], name_len);
> >> +      filename[name_len] = 0;
> >> +
> >> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> >> +      if (!fdiro)
> >> +        {
> >> +          grub_free(filename);
> >> +          return 0;
> >> +        }
> >> +
> >> +      if (ftype == F2FS_FT_DIR)
> >> +        type = GRUB_FSHELP_DIR;
> >> +      else if (ftype == F2FS_FT_SYMLINK)
> >> +        type = GRUB_FSHELP_SYMLINK;
> >> +      else if (ftype == F2FS_FT_REG_FILE)
> >> +        type = GRUB_FSHELP_REG;
> >> +
> >> +      fdiro->data = ctx->data;
> >> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> >> +      fdiro->inode_read = 0;
> >> +
> >> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> >> +      grub_free(filename);
> >> +      if (ret)
> >> +        return 1;
> >> +
> >> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> >> +
> >> +  ctx->bitmap = de_blk->dentry_bitmap;
> >> +  ctx->dentry = de_blk->dentry;
> >> +  ctx->filename = de_blk->filename;
> >> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> >> +  struct grub_f2fs_inode *inode;
> >> +  struct grub_f2fs_dir_iter_ctx ctx = {
> >> +    .data = diro->data,
> >> +    .hook = hook,
> >> +    .hook_data = hook_data
> >> +  };
> >> +  grub_off_t fpos = 0;
> >> +
> >> +  if (!diro->inode_read)
> >> +    {
> >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> >> +      if (grub_errno)
> >> +     return 0;
> >> +    }
> >> +
> >> +  inode = &diro->inode.i;
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> >> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> >> +
> >> +  while (fpos < grub_f2fs_file_size (inode))
> >> +    {
> >> +      struct grub_f2fs_dentry_block *de_blk;
> >> +      char *buf;
> >> +      int ret;
> >> +
> >> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> >> +
> >> +      ctx.bitmap = de_blk->dentry_bitmap;
> >> +      ctx.dentry = de_blk->dentry;
> >> +      ctx.filename = de_blk->filename;
> >> +      ctx.max = NR_DENTRY_IN_BLOCK;
> >> +
> >> +      ret = grub_f2fs_check_dentries (&ctx);
> >> +      grub_free (buf);
> >> +      if (ret)
> >> +        return 1;
> >> +
> >> +      fpos += F2FS_BLKSIZE;
> >> +    }
> >> +  return 0;
> >> +}
> >> +
> >> +static int
> >> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> >> +           grub_fshelp_node_t node, void *data)
> >> +{
> >> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> >> +      grub_errno = GRUB_ERR_NONE;
> >> +    }
> >> +  if (node->inode_read)
> >> +    {
> >> +      info.mtimeset = 1;
> >> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> >> +    }
> >> +
> >> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> >> +  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 = {
> >> +    .hook = hook,
> >> +    .hook_data = hook_data
> >> +  };
> >> +  struct grub_fshelp_node *fdiro = 0;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  ctx.data = 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 != &ctx.data->diropen)
> >> +    grub_free (fdiro);
> >> + 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 = NULL;
> >> +  struct grub_fshelp_node *fdiro = 0;
> >> +  struct grub_f2fs_inode *inode;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  data = 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, sizeof (*data->inode));
> >> +  grub_free (fdiro);
> >> +
> >> +  inode = &(data->inode->i);
> >> +  file->size = grub_f2fs_file_size (inode);
> >> +  file->data = data;
> >> +  file->offset = 0;
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
> >> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> >> +  return 0;
> >> +
> >> +fail:
> >> +  if (fdiro != &data->diropen)
> >> +    grub_free (fdiro);
> >> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> >> +
> >> +  grub_free (data);
> >> +
> >> +  grub_dl_unref (my_mod);
> >> +
> >> +  return GRUB_ERR_NONE;
> >> +}
> >> +
> >> +static grub_uint8_t *
> >> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> >> +{
> >> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> >> +  grub_uint8_t *out_buf;
> >> +  int len = 0;
> >> +
> >> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> >> +  if (!out_buf)
> >> +    return NULL;
> >> +
> >> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> >> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> >> +    len++;
> >> +  }
> >> +
> >> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> >> +  return out_buf;
> >> +}
> >> +
> >> +static grub_err_t
> >> +grub_f2fs_label (grub_device_t device, char **label)
> >> +{
> >> +  struct grub_f2fs_data *data;
> >> +  grub_disk_t disk = device->disk;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  data = grub_f2fs_mount (disk);
> >> +  if (data)
> >> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> >> +  else
> >> +    *label = NULL;
> >> +
> >> +  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 = device->disk;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  data = grub_f2fs_mount (disk);
> >> +  if (data)
> >> +    {
> >> +      *uuid =
> >> +     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 = NULL;
> >> +
> >> +  grub_free (data);
> >> +  grub_dl_unref (my_mod);
> >> +  return grub_errno;
> >> +}
> >> +
> >> +static struct grub_fs grub_f2fs_fs = {
> >> +  .name = "f2fs",
> >> +  .dir = grub_f2fs_dir,
> >> +  .open = grub_f2fs_open,
> >> +  .read = grub_f2fs_read,
> >> +  .close = grub_f2fs_close,
> >> +  .label = grub_f2fs_label,
> >> +  .uuid = grub_f2fs_uuid,
> >> +#ifdef GRUB_UTIL
> >> +  .reserved_first_sector = 1,
> >> +  .blocklist_install = 0,
> >> +#endif
> >> +  .next = 0
> >> +};
> >> +
> >> +GRUB_MOD_INIT (f2fs)
> >> +{
> >> +  grub_fs_register (&grub_f2fs_fs);
> >> +  my_mod = mod;
> >> +}
> >> +
> >> +GRUB_MOD_FINI (f2fs)
> >> +{
> >> +  grub_fs_unregister (&grub_f2fs_fs);
> >> +}
> >> diff --git a/po/exclude.pot b/po/exclude.pot
> >> index 0a9b215..816089c 100644
> >> --- a/po/exclude.pot
> >> +++ b/po/exclude.pot
> >> @@ -1214,6 +1214,7 @@ msgstr ""
> >>
> >>  #: 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..1ea77c8
> >> --- /dev/null
> >> +++ b/tests/f2fs_test.in
> >> @@ -0,0 +1,19 @@
> >> +#!/bin/sh
> >> +
> >> +set -e
> >> +
> >> +if [ "x$EUID" = "x" ] ; then
> >> + EUID=`id -u`
> >> +fi
> >> +
> >> +if [ "$EUID" != 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 424de22..e3e4109 100644
> >> --- a/tests/util/grub-fs-tester.in
> >> +++ b/tests/util/grub-fs-tester.in
> >> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>       xsquash*)
> >>           MINBLKSIZE=4096
> >>           MAXBLKSIZE=1048576;;
> >> -     xxfs)
> >> +     xxfs|xf2fs)
> >>           MINBLKSIZE=$SECSIZE
> >>               # OS Limitation: GNU/Linux doesn't accept > 4096
> >>           MAXBLKSIZE=4096;;
> >> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>               x"btrfs"*)
> >>                   FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> >>
> >> +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> >> +             x"f2fs")
> >> +                 FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> >> +
> >>           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >>               x"exfat")
> >>                   FSLABEL="géт ;/莭莽😁кир";;
> >> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
> >>       # FS LIMITATION: as far as I know those FS don't store their last modification date.
> >>               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> >> -                 | 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=y;;
> >> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >>                   MOUNTFS=ext2
> >>                   "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)
> >> --
> >> 2.6.3
> >>
> >>
> >> ------------------------------------------------------------------------------
> >> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> >> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> >> Monitor end-to-end web transactions and take corrective actions now
> >> Troubleshoot faster and improve end-user experience. Signup Now!
> >> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> >> _______________________________________________
> >> Linux-f2fs-devel mailing list
> >> Linux-f2fs-devel@lists.sourceforge.net
> >> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

------------------------------------------------------------------------------
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [2.02] Re: [PATCH v8] F2FS support
@ 2016-08-05 18:07                         ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2016-08-05 18:07 UTC (permalink / raw)
  To: Andrei Borzenkov; +Cc: The development of GNU GRUB, linux-f2fs-devel

On Fri, Aug 05, 2016 at 01:57:37PM +0300, Andrei Borzenkov wrote:
> Vladimir prefers to postpone new features until 2.02 is out. From my
> side I think it is isolated enough and new code so low regression
> risk. Vladimir has the final word as maintainer :)

Okay, I can wait for 2.02. ;)
BTW, I've heard that openmandriva adopted this patch to support F2FS in their
installer.

Thanks,

> 
> On Thu, Aug 4, 2016 at 8:06 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:
> > Hi Andrei,
> >
> > It's been sitting for a long long time.
> > May I ask whether or not there is any merging plan of this?
> >
> > Thanks,
> >
> > On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
> >> Change log from v7:
> >>  - fix an offset bug in read_file
> >>
> >> -- >8 --
> >> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00 2001
> >> From: Jaegeuk Kim <jaegeuk@kernel.org>
> >> Date: Tue, 17 Nov 2015 12:45:13 -0800
> >> Subject: [PATCH] F2FS support
> >>
> >> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> >> into Linux kernel v3.8 in 2013.
> >>
> >> The motive for F2FS was to build a file system that from the start, takes into
> >> account the characteristics of NAND flash memory-based storage devices (such as
> >> solid-state disks, eMMC, and SD cards).
> >>
> >> F2FS was designed on a basis of a log-structured file system approach, which
> >> remedies some known issues of the older log structured file systems, such as
> >> the snowball effect of wandering trees and high cleaning overhead. In addition,
> >> since a NAND-based storage device shows different characteristics according to
> >> its internal geometry or flash memory management scheme (such as the Flash
> >> Translation Layer or FTL), it supports various parameters not only for
> >> configuring on-disk layout, but also for selecting allocation and cleaning
> >> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> >>
> >> The source codes for F2FS are available from:
> >>
> >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> >>
> >> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> >> ---
> >>  Makefile.util.def            |    7 +
> >>  docs/grub.texi               |    5 +-
> >>  grub-core/Makefile.core.def  |    5 +
> >>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
> >>  po/exclude.pot               |    1 +
> >>  tests/f2fs_test.in           |   19 +
> >>  tests/util/grub-fs-tester.in |   10 +-
> >>  7 files changed, 1332 insertions(+), 4 deletions(-)
> >>  create mode 100644 grub-core/fs/f2fs.c
> >>  create mode 100644 tests/f2fs_test.in
> >>
> >> diff --git a/Makefile.util.def b/Makefile.util.def
> >> index db7e8ec..84627bb 100644
> >> --- a/Makefile.util.def
> >> +++ b/Makefile.util.def
> >> @@ -99,6 +99,7 @@ library = {
> >>    common = grub-core/fs/ext2.c;
> >>    common = grub-core/fs/fat.c;
> >>    common = grub-core/fs/exfat.c;
> >> +  common = grub-core/fs/f2fs.c;
> >>    common = grub-core/fs/fshelp.c;
> >>    common = grub-core/fs/hfs.c;
> >>    common = grub-core/fs/hfsplus.c;
> >> @@ -766,6 +767,12 @@ script = {
> >>
> >>  script = {
> >>    testcase;
> >> +  name = f2fs_test;
> >> +  common = tests/f2fs_test.in;
> >> +};
> >> +
> >> +script = {
> >> +  testcase;
> >>    name = nilfs2_test;
> >>    common = tests/nilfs2_test.in;
> >>  };
> >> diff --git a/docs/grub.texi b/docs/grub.texi
> >> index 1df3db2..e5a80f3 100644
> >> --- a/docs/grub.texi
> >> +++ b/docs/grub.texi
> >> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
> >> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> >> +@dfn{f2fs}, @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},
> >> @@ -5347,7 +5348,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, minix,
> >>  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 charset
> >>  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 d9fa0e3..b585ade 100644
> >> --- a/grub-core/Makefile.core.def
> >> +++ b/grub-core/Makefile.core.def
> >> @@ -1278,6 +1278,11 @@ module = {
> >>  };
> >>
> >>  module = {
> >> +  name = f2fs;
> >> +  common = fs/f2fs.c;
> >> +};
> >> +
> >> +module = {
> >>    name = fshelp;
> >>    common = fs/fshelp.c;
> >>  };
> >> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> >> new file mode 100644
> >> index 0000000..7fb256f
> >> --- /dev/null
> >> +++ b/grub-core/fs/f2fs.c
> >> @@ -0,0 +1,1289 @@
> >> +/*
> >> + *  f2fs.c - Flash-Friendly File System
> >> + *
> >> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> >> + *
> >> + *  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 <http://www.gnu.org/licenses/>.
> >> + */
> >> +#include <grub/err.h>
> >> +#include <grub/file.h>
> >> +#include <grub/mm.h>
> >> +#include <grub/misc.h>
> >> +#include <grub/disk.h>
> >> +#include <grub/dl.h>
> >> +#include <grub/types.h>
> >> +#include <grub/charset.h>
> >> +#include <grub/fshelp.h>
> >> +
> >> +GRUB_MOD_LICENSE ("GPLv3+");
> >> +
> >> +/* F2FS Magic Number */
> >> +#define F2FS_SUPER_MAGIC     0xF2F52010
> >> +#define CHECKSUM_OFFSET              4092            /* must be aligned 4 bytes */
> >> +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> >> +
> >> +/* byte-size offset */
> >> +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> >> +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
> >> +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>  \
> >> +                                             GRUB_DISK_SECTOR_BITS)
> >> +
> >> +/* 9 bits for 512 bytes */
> >> +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> >> +
> >> +/* support only 4KB block */
> >> +#define F2FS_BLK_BITS        12
> >> +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> >> +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> >> +
> >> +#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  (sizeof(struct grub_f2fs_nat_jent))
> >> +#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 (sizeof(struct grub_f2fs_nat_entry))
> >> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> >> +
> >> +#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
> >> +
> >> +#define CKPT_FLAG_SET(ckpt, f)       \
> >> +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> >> +
> >> +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> >> +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> >> +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> >> +#define F2FS_DATA_EXIST              0x08    /* file inline data exist flag */
> >> +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot dentries */
> >> +
> >> +enum FILE_TYPE
> >> +{
> >> +  F2FS_FT_UNKNOWN,
> >> +  F2FS_FT_REG_FILE = 1,
> >> +  F2FS_FT_DIR = 2,
> >> +  F2FS_FT_SYMLINK = 7,
> >> +};
> >> +
> >> +#define MAX_VOLUME_NAME              512
> >> +
> >> +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[MAX_VOLUME_NAME];
> >> +  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;
> >> +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE totally */
> >> +  };
> >> +  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_uint8_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
> >> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> >> +{
> >> +  return addr[nr >> 3] & (1 << (nr & 7));
> >> +}
> >> +
> >> +static inline char *
> >> +__inline_addr (struct grub_f2fs_inode *inode)
> >> +{
> >> +  return (char *)&inode->i_addr[1];
> >> +}
> >> +
> >> +static inline grub_uint64_t
> >> +grub_f2fs_file_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 = &data->ckpt;
> >> +  grub_uint32_t start_addr = data->cp_blkaddr;
> >> +
> >> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> >> +
> >> +  return __start_cp_addr (data) +
> >> +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> >> +     - (base + 1) + type;
> >> +}
> >> +
> >> +static inline void *
> >> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> >> +{
> >> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> >> +  grub_uint32_t offset;
> >> +
> >> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> >> +    return ckpt->sit_nat_version_bitmap;
> >> +
> >> +  offset = 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 inode_block)
> >> +{
> >> +  if (inode_block)
> >> +    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,
> >> +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> >> +             0, F2FS_BLKSIZE, buf);
> >> +}
> >> +
> >> +/*
> >> + * CRC32
> >> +*/
> >> +#define CRCPOLY_LE 0xedb88320
> >> +
> >> +static inline grub_uint32_t
> >> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> >> +{
> >> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> >> +  unsigned char *p = (unsigned char *)buf;
> >> +  grub_uint32_t tmp = len;
> >> +  int i;
> >> +
> >> +  while (tmp--)
> >> +    {
> >> +      crc ^= *p++;
> >> +      for (i = 0; i < 8; i++)
> >> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> >> +    }
> >> +  return crc;
> >> +}
> >> +
> >> +static inline int
> >> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
> >> +{
> >> +  grub_uint32_t cal_crc = 0;
> >> +
> >> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> >> +
> >> +  return (cal_crc == blk_crc) ? 1 : 0;
> >> +}
> >> +
> >> +static inline int
> >> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> >> +{
> >> +  int mask;
> >> +
> >> +  p += (nr >> 3);
> >> +  mask = 1 << (7 - (nr & 0x07));
> >> +  return mask & *p;
> >> +}
> >> +
> >> +static int
> >> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> >> +{
> >> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> >> +
> >> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> >> +    return -1;
> >> +
> >> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
> >> +    return -1;
> >> +
> >> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> >> +  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
> >> +
> >> +  if (log_sectorsize > F2FS_BLK_BITS)
> >> +    return -1;
> >> +
> >> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> >> +    return -1;
> >> +
> >> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> >> +    return -1;
> >> +
> >> +  return 0;
> >> +}
> >> +
> >> +static int
> >> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
> >> +{
> >> +  grub_disk_t disk = data->disk;
> >> +  grub_err_t err;
> >> +
> >> +  /* Read first super block. */
> >> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
> >> +  if (err)
> >> +    return -1;
> >> +
> >> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> >> +}
> >> +
> >> +static void *
> >> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> >> +     grub_uint64_t *version)
> >> +{
> >> +  grub_uint32_t *cp_page_1, *cp_page_2;
> >> +  struct grub_f2fs_checkpoint *cp_block;
> >> +  grub_uint64_t cur_version = 0, pre_version = 0;
> >> +  grub_uint32_t crc = 0;
> >> +  grub_uint32_t crc_offset;
> >> +  grub_err_t err;
> >> +
> >> +  /* Read the 1st cp block in this CP pack */
> >> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!cp_page_1)
> >> +    return NULL;
> >> +
> >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> >> +  if (err)
> >> +    goto invalid_cp1;
> >> +
> >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> >> +  if (crc_offset != CHECKSUM_OFFSET)
> >> +    goto invalid_cp1;
> >> +
> >> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> >> +    goto invalid_cp1;
> >> +
> >> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> >> +
> >> +  /* Read the 2nd cp block in this CP pack */
> >> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!cp_page_2)
> >> +    goto invalid_cp1;
> >> +
> >> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> >> +
> >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> >> +  if (err)
> >> +    goto invalid_cp2;
> >> +
> >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> >> +  if (crc_offset != CHECKSUM_OFFSET)
> >> +    goto invalid_cp2;
> >> +
> >> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> >> +    goto invalid_cp2;
> >> +
> >> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> >> +  if (cur_version == pre_version)
> >> +    {
> >> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> >> +  cp1 = 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 += data->blocks_per_seg;
> >> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> >> +  if (!cp2 && grub_errno)
> >> +    {
> >> +      grub_free (cp1);
> >> +      return grub_errno;
> >> +    }
> >> +
> >> +  if (cp1 && cp2)
> >> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> >> +  else if (cp1)
> >> +    cur_page = cp1;
> >> +  else if (cp2)
> >> +    cur_page = cp2;
> >> +  else
> >> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> >> +
> >> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> >> +
> >> +  grub_free (cp1);
> >> +  grub_free (cp2);
> >> +  return 0;
> >> +}
> >> +
> >> +static grub_err_t
> >> +get_nat_journal (struct grub_f2fs_data *data)
> >> +{
> >> +  grub_uint32_t block;
> >> +  char *buf;
> >> +  grub_err_t err;
> >> +
> >> +  buf = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!buf)
> >> +    return grub_errno;
> >> +
> >> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> >> +    block = __start_sum_block (data);
> >> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> >> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> >> +  else
> >> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> >> +
> >> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> >> +  grub_uint32_t blkaddr = 0;
> >> +  grub_uint16_t i;
> >> +
> >> +  for (i = 0; i < n; i++)
> >> +    {
> >> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> >> +        {
> >> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> >> +  if (blkaddr)
> >> +    return blkaddr;
> >> +
> >> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!nat_block)
> >> +    return 0;
> >> +
> >> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> >> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> >> +
> >> +  seg_off = block_off / data->blocks_per_seg;
> >> +  block_addr = 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 += data->blocks_per_seg;
> >> +
> >> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> >> +  if (err)
> >> +    {
> >> +      grub_free (nat_block);
> >> +      return 0;
> >> +    }
> >> +
> >> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> >> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> >> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> >> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> >> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> >> +  int n = 0;
> >> +  int level = 0;
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> >> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> >> +
> >> +  noffset[0] = 0;
> >> +
> >> +  if (block < direct_index)
> >> +    {
> >> +      offset[n] = block;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= direct_index;
> >> +  if (block < direct_blks)
> >> +    {
> >> +      offset[n++] = NODE_DIR1_BLOCK;
> >> +      noffset[n] = 1;
> >> +      offset[n] = block;
> >> +      level = 1;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= direct_blks;
> >> +  if (block < direct_blks)
> >> +    {
> >> +      offset[n++] = NODE_DIR2_BLOCK;
> >> +      noffset[n] = 2;
> >> +      offset[n] = block;
> >> +      level = 1;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= direct_blks;
> >> +  if (block < indirect_blks)
> >> +    {
> >> +      offset[n++] = NODE_IND1_BLOCK;
> >> +      noffset[n] = 3;
> >> +      offset[n++] = block / direct_blks;
> >> +      noffset[n] = 4 + offset[n - 1];
> >> +      offset[n] = block % direct_blks;
> >> +      level = 2;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= indirect_blks;
> >> +  if (block < indirect_blks)
> >> +    {
> >> +      offset[n++] = NODE_IND2_BLOCK;
> >> +      noffset[n] = 4 + dptrs_per_blk;
> >> +      offset[n++] = block / direct_blks;
> >> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> >> +      offset[n] = block % direct_blks;
> >> +      level = 2;
> >> +      goto got;
> >> +    }
> >> +
> >> +  block -= indirect_blks;
> >> +  if (block < dindirect_blks)
> >> +    {
> >> +      offset[n++] = NODE_DIND_BLOCK;
> >> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> >> +      offset[n++] = block / indirect_blks;
> >> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> >> +      offset[n - 1] * (dptrs_per_blk + 1);
> >> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> >> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> >> +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> >> +      offset[n] = block % direct_blks;
> >> +      level = 3;
> >> +      goto got;
> >> +    }
> >> +got:
> >> +  return level;
> >> +}
> >> +
> >> +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 = 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 = grub_malloc (sizeof (*data));
> >> +  if (!data)
> >> +    return NULL;
> >> +
> >> +  data->disk = disk;
> >> +
> >> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> >> +    {
> >> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> >> +        {
> >> +          if (grub_errno == GRUB_ERR_NONE)
> >> +            grub_error (GRUB_ERR_BAD_FS,
> >> +                     "not a F2FS filesystem (no superblock)");
> >> +          goto fail;
> >> +        }
> >> +    }
> >> +
> >> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> >> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> >> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> >> +  data->blocks_per_seg = 1 <<
> >> +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> >> +
> >> +  err = grub_f2fs_read_cp (data);
> >> +  if (err)
> >> +    goto fail;
> >> +
> >> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> >> +
> >> +  err = get_nat_journal (data);
> >> +  if (err)
> >> +    goto fail;
> >> +
> >> +  data->diropen.data = data;
> >> +  data->diropen.ino = data->root_ino;
> >> +  data->diropen.inode_read = 1;
> >> +  data->inode = &data->diropen.inode;
> >> +
> >> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> >> +  if (err)
> >> +    goto fail;
> >> +
> >> +  return data;
> >> +
> >> +fail:
> >> +  grub_free (data);
> >> +  return NULL;
> >> +}
> >> +
> >> +/* guarantee inline_data was handled by caller */
> >> +static grub_disk_addr_t
> >> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
> >> +{
> >> +  struct grub_f2fs_data *data = node->data;
> >> +  struct grub_f2fs_inode *inode = &node->inode.i;
> >> +  grub_uint32_t offset[4], noffset[4], nids[4];
> >> +  struct grub_f2fs_node *node_block;
> >> +  grub_uint32_t block_addr = -1;
> >> +  int level, i;
> >> +
> >> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> >> +  if (level == 0)
> >> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> >> +
> >> +  node_block = grub_malloc (F2FS_BLKSIZE);
> >> +  if (!node_block)
> >> +    return -1;
> >> +
> >> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> >> +
> >> +  /* get indirect or direct nodes */
> >> +  for (i = 1; i <= level; i++)
> >> +    {
> >> +      grub_f2fs_read_node (data, nids[i], node_block);
> >> +      if (grub_errno)
> >> +        goto fail;
> >> +
> >> +      if (i < level)
> >> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> >> +    }
> >> +
> >> +  block_addr = 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 = &node->inode.i;
> >> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> >> +  char *inline_addr = __inline_addr (inode);
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_DATA)
> >> +    {
> >> +      if (filesize > MAX_INLINE_DATA)
> >> +        return -1;
> >> +      if (len > filesize - pos)
> >> +        len = filesize - pos;
> >> +
> >> +      grub_memcpy (buf, 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_get_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 = node;
> >> +  grub_uint64_t filesize;
> >> +
> >> +  if (!diro->inode_read)
> >> +    {
> >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> >> +      if (grub_errno)
> >> +        return 0;
> >> +    }
> >> +
> >> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> >> +
> >> +  symlink = grub_malloc (filesize + 1);
> >> +  if (!symlink)
> >> +    return 0;
> >> +
> >> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> >> +  if (grub_errno)
> >> +    {
> >> +      grub_free (symlink);
> >> +      return 0;
> >> +    }
> >> +
> >> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> >> +    {
> >> +      char *filename;
> >> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> >> +      enum FILE_TYPE ftype;
> >> +      int name_len;
> >> +      int ret;
> >> +
> >> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> >> +        {
> >> +          i++;
> >> +          continue;
> >> +        }
> >> +
> >> +      ftype = ctx->dentry[i].file_type;
> >> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> >> +      filename = grub_malloc (name_len + 1);
> >> +      if (!filename)
> >> +        return 0;
> >> +
> >> +      grub_memcpy (filename, ctx->filename[i], name_len);
> >> +      filename[name_len] = 0;
> >> +
> >> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> >> +      if (!fdiro)
> >> +        {
> >> +          grub_free(filename);
> >> +          return 0;
> >> +        }
> >> +
> >> +      if (ftype == F2FS_FT_DIR)
> >> +        type = GRUB_FSHELP_DIR;
> >> +      else if (ftype == F2FS_FT_SYMLINK)
> >> +        type = GRUB_FSHELP_SYMLINK;
> >> +      else if (ftype == F2FS_FT_REG_FILE)
> >> +        type = GRUB_FSHELP_REG;
> >> +
> >> +      fdiro->data = ctx->data;
> >> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> >> +      fdiro->inode_read = 0;
> >> +
> >> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> >> +      grub_free(filename);
> >> +      if (ret)
> >> +        return 1;
> >> +
> >> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> >> +
> >> +  ctx->bitmap = de_blk->dentry_bitmap;
> >> +  ctx->dentry = de_blk->dentry;
> >> +  ctx->filename = de_blk->filename;
> >> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> >> +  struct grub_f2fs_inode *inode;
> >> +  struct grub_f2fs_dir_iter_ctx ctx = {
> >> +    .data = diro->data,
> >> +    .hook = hook,
> >> +    .hook_data = hook_data
> >> +  };
> >> +  grub_off_t fpos = 0;
> >> +
> >> +  if (!diro->inode_read)
> >> +    {
> >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> >> +      if (grub_errno)
> >> +     return 0;
> >> +    }
> >> +
> >> +  inode = &diro->inode.i;
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> >> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> >> +
> >> +  while (fpos < grub_f2fs_file_size (inode))
> >> +    {
> >> +      struct grub_f2fs_dentry_block *de_blk;
> >> +      char *buf;
> >> +      int ret;
> >> +
> >> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> >> +
> >> +      ctx.bitmap = de_blk->dentry_bitmap;
> >> +      ctx.dentry = de_blk->dentry;
> >> +      ctx.filename = de_blk->filename;
> >> +      ctx.max = NR_DENTRY_IN_BLOCK;
> >> +
> >> +      ret = grub_f2fs_check_dentries (&ctx);
> >> +      grub_free (buf);
> >> +      if (ret)
> >> +        return 1;
> >> +
> >> +      fpos += F2FS_BLKSIZE;
> >> +    }
> >> +  return 0;
> >> +}
> >> +
> >> +static int
> >> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
> >> +           grub_fshelp_node_t node, void *data)
> >> +{
> >> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> >> +      grub_errno = GRUB_ERR_NONE;
> >> +    }
> >> +  if (node->inode_read)
> >> +    {
> >> +      info.mtimeset = 1;
> >> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> >> +    }
> >> +
> >> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> >> +  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 = {
> >> +    .hook = hook,
> >> +    .hook_data = hook_data
> >> +  };
> >> +  struct grub_fshelp_node *fdiro = 0;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  ctx.data = 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 != &ctx.data->diropen)
> >> +    grub_free (fdiro);
> >> + 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 = NULL;
> >> +  struct grub_fshelp_node *fdiro = 0;
> >> +  struct grub_f2fs_inode *inode;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  data = 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, sizeof (*data->inode));
> >> +  grub_free (fdiro);
> >> +
> >> +  inode = &(data->inode->i);
> >> +  file->size = grub_f2fs_file_size (inode);
> >> +  file->data = data;
> >> +  file->offset = 0;
> >> +
> >> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
> >> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> >> +  return 0;
> >> +
> >> +fail:
> >> +  if (fdiro != &data->diropen)
> >> +    grub_free (fdiro);
> >> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> >> +
> >> +  grub_free (data);
> >> +
> >> +  grub_dl_unref (my_mod);
> >> +
> >> +  return GRUB_ERR_NONE;
> >> +}
> >> +
> >> +static grub_uint8_t *
> >> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> >> +{
> >> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> >> +  grub_uint8_t *out_buf;
> >> +  int len = 0;
> >> +
> >> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
> >> +  if (!out_buf)
> >> +    return NULL;
> >> +
> >> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> >> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> >> +    len++;
> >> +  }
> >> +
> >> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> >> +  return out_buf;
> >> +}
> >> +
> >> +static grub_err_t
> >> +grub_f2fs_label (grub_device_t device, char **label)
> >> +{
> >> +  struct grub_f2fs_data *data;
> >> +  grub_disk_t disk = device->disk;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  data = grub_f2fs_mount (disk);
> >> +  if (data)
> >> +    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
> >> +  else
> >> +    *label = NULL;
> >> +
> >> +  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 = device->disk;
> >> +
> >> +  grub_dl_ref (my_mod);
> >> +
> >> +  data = grub_f2fs_mount (disk);
> >> +  if (data)
> >> +    {
> >> +      *uuid =
> >> +     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 = NULL;
> >> +
> >> +  grub_free (data);
> >> +  grub_dl_unref (my_mod);
> >> +  return grub_errno;
> >> +}
> >> +
> >> +static struct grub_fs grub_f2fs_fs = {
> >> +  .name = "f2fs",
> >> +  .dir = grub_f2fs_dir,
> >> +  .open = grub_f2fs_open,
> >> +  .read = grub_f2fs_read,
> >> +  .close = grub_f2fs_close,
> >> +  .label = grub_f2fs_label,
> >> +  .uuid = grub_f2fs_uuid,
> >> +#ifdef GRUB_UTIL
> >> +  .reserved_first_sector = 1,
> >> +  .blocklist_install = 0,
> >> +#endif
> >> +  .next = 0
> >> +};
> >> +
> >> +GRUB_MOD_INIT (f2fs)
> >> +{
> >> +  grub_fs_register (&grub_f2fs_fs);
> >> +  my_mod = mod;
> >> +}
> >> +
> >> +GRUB_MOD_FINI (f2fs)
> >> +{
> >> +  grub_fs_unregister (&grub_f2fs_fs);
> >> +}
> >> diff --git a/po/exclude.pot b/po/exclude.pot
> >> index 0a9b215..816089c 100644
> >> --- a/po/exclude.pot
> >> +++ b/po/exclude.pot
> >> @@ -1214,6 +1214,7 @@ msgstr ""
> >>
> >>  #: 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..1ea77c8
> >> --- /dev/null
> >> +++ b/tests/f2fs_test.in
> >> @@ -0,0 +1,19 @@
> >> +#!/bin/sh
> >> +
> >> +set -e
> >> +
> >> +if [ "x$EUID" = "x" ] ; then
> >> + EUID=`id -u`
> >> +fi
> >> +
> >> +if [ "$EUID" != 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 424de22..e3e4109 100644
> >> --- a/tests/util/grub-fs-tester.in
> >> +++ b/tests/util/grub-fs-tester.in
> >> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>       xsquash*)
> >>           MINBLKSIZE=4096
> >>           MAXBLKSIZE=1048576;;
> >> -     xxfs)
> >> +     xxfs|xf2fs)
> >>           MINBLKSIZE=$SECSIZE
> >>               # OS Limitation: GNU/Linux doesn't accept > 4096
> >>           MAXBLKSIZE=4096;;
> >> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>               x"btrfs"*)
> >>                   FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoif";;
> >>
> >> +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> >> +             x"f2fs")
> >> +                 FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> >> +
> >>           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> >>               x"exfat")
> >>                   FSLABEL="géт ;/莭莽😁кир";;
> >> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
> >>       # FS LIMITATION: as far as I know those FS don't store their last modification date.
> >>               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> >> -                 | 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=y;;
> >> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> >>                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> >>                   MOUNTFS=ext2
> >>                   "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)
> >> --
> >> 2.6.3
> >>
> >>
> >> ------------------------------------------------------------------------------
> >> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> >> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> >> Monitor end-to-end web transactions and take corrective actions now
> >> Troubleshoot faster and improve end-user experience. Signup Now!
> >> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> >> _______________________________________________
> >> Linux-f2fs-devel mailing list
> >> Linux-f2fs-devel@lists.sourceforge.net
> >> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [2.02] Re: [PATCH v8] F2FS support
  2016-08-05 18:07                         ` [f2fs-dev] " Jaegeuk Kim
@ 2016-08-05 19:17                           ` Michael Zimmermann
  -1 siblings, 0 replies; 77+ messages in thread
From: Michael Zimmermann @ 2016-08-05 19:17 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Andrei Borzenkov, linux-f2fs-devel


[-- Attachment #1.1: Type: text/plain, Size: 56645 bytes --]

> BTW, I've heard that openmandriva adopted this patch to support F2FS in
their
installer.

And I've ported it to clover's GrubFSPkg which I'm using for EFIDroid :)
Your timing was really perfect. The day I needed f2fs support I found your
V1 in the mailing list :D
https://github.com/efidroid/uefi_edk2packages_GrubFSPkg/blob/master/grub/grub-core/fs/f2fs.c

Thanks
Michael

On Fri, Aug 5, 2016 at 8:07 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:

> On Fri, Aug 05, 2016 at 01:57:37PM +0300, Andrei Borzenkov wrote:
> > Vladimir prefers to postpone new features until 2.02 is out. From my
> > side I think it is isolated enough and new code so low regression
> > risk. Vladimir has the final word as maintainer :)
>
> Okay, I can wait for 2.02. ;)
> BTW, I've heard that openmandriva adopted this patch to support F2FS in
> their
> installer.
>
> Thanks,
>
> >
> > On Thu, Aug 4, 2016 at 8:06 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:
> > > Hi Andrei,
> > >
> > > It's been sitting for a long long time.
> > > May I ask whether or not there is any merging plan of this?
> > >
> > > Thanks,
> > >
> > > On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
> > >> Change log from v7:
> > >>  - fix an offset bug in read_file
> > >>
> > >> -- >8 --
> > >> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00
> 2001
> > >> From: Jaegeuk Kim <jaegeuk@kernel.org>
> > >> Date: Tue, 17 Nov 2015 12:45:13 -0800
> > >> Subject: [PATCH] F2FS support
> > >>
> > >> "F2FS (Flash-Friendly File System) is flash-friendly file system
> which was merged
> > >> into Linux kernel v3.8 in 2013.
> > >>
> > >> The motive for F2FS was to build a file system that from the start,
> takes into
> > >> account the characteristics of NAND flash memory-based storage
> devices (such as
> > >> solid-state disks, eMMC, and SD cards).
> > >>
> > >> F2FS was designed on a basis of a log-structured file system
> approach, which
> > >> remedies some known issues of the older log structured file systems,
> such as
> > >> the snowball effect of wandering trees and high cleaning overhead. In
> addition,
> > >> since a NAND-based storage device shows different characteristics
> according to
> > >> its internal geometry or flash memory management scheme (such as the
> Flash
> > >> Translation Layer or FTL), it supports various parameters not only for
> > >> configuring on-disk layout, but also for selecting allocation and
> cleaning
> > >> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> > >>
> > >> The source codes for F2FS are available from:
> > >>
> > >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> > >>
> > >> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> > >> ---
> > >>  Makefile.util.def            |    7 +
> > >>  docs/grub.texi               |    5 +-
> > >>  grub-core/Makefile.core.def  |    5 +
> > >>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++
> ++++++++++++
> > >>  po/exclude.pot               |    1 +
> > >>  tests/f2fs_test.in           |   19 +
> > >>  tests/util/grub-fs-tester.in |   10 +-
> > >>  7 files changed, 1332 insertions(+), 4 deletions(-)
> > >>  create mode 100644 grub-core/fs/f2fs.c
> > >>  create mode 100644 tests/f2fs_test.in
> > >>
> > >> diff --git a/Makefile.util.def b/Makefile.util.def
> > >> index db7e8ec..84627bb 100644
> > >> --- a/Makefile.util.def
> > >> +++ b/Makefile.util.def
> > >> @@ -99,6 +99,7 @@ library = {
> > >>    common = grub-core/fs/ext2.c;
> > >>    common = grub-core/fs/fat.c;
> > >>    common = grub-core/fs/exfat.c;
> > >> +  common = grub-core/fs/f2fs.c;
> > >>    common = grub-core/fs/fshelp.c;
> > >>    common = grub-core/fs/hfs.c;
> > >>    common = grub-core/fs/hfsplus.c;
> > >> @@ -766,6 +767,12 @@ script = {
> > >>
> > >>  script = {
> > >>    testcase;
> > >> +  name = f2fs_test;
> > >> +  common = tests/f2fs_test.in;
> > >> +};
> > >> +
> > >> +script = {
> > >> +  testcase;
> > >>    name = nilfs2_test;
> > >>    common = tests/nilfs2_test.in;
> > >>  };
> > >> diff --git a/docs/grub.texi b/docs/grub.texi
> > >> index 1df3db2..e5a80f3 100644
> > >> --- a/docs/grub.texi
> > >> +++ b/docs/grub.texi
> > >> @@ -360,7 +360,8 @@ blocklist notation. The currently supported
> filesystem 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}, @dfn{HFS},
> > >> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> > >> +@dfn{f2fs}, @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},
> > >> @@ -5347,7 +5348,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, minix,
> > >>  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
> charset
> > >>  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 d9fa0e3..b585ade 100644
> > >> --- a/grub-core/Makefile.core.def
> > >> +++ b/grub-core/Makefile.core.def
> > >> @@ -1278,6 +1278,11 @@ module = {
> > >>  };
> > >>
> > >>  module = {
> > >> +  name = f2fs;
> > >> +  common = fs/f2fs.c;
> > >> +};
> > >> +
> > >> +module = {
> > >>    name = fshelp;
> > >>    common = fs/fshelp.c;
> > >>  };
> > >> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> > >> new file mode 100644
> > >> index 0000000..7fb256f
> > >> --- /dev/null
> > >> +++ b/grub-core/fs/f2fs.c
> > >> @@ -0,0 +1,1289 @@
> > >> +/*
> > >> + *  f2fs.c - Flash-Friendly File System
> > >> + *
> > >> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> > >> + *
> > >> + *  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 <http://www.gnu.org/licenses/>.
> > >> + */
> > >> +#include <grub/err.h>
> > >> +#include <grub/file.h>
> > >> +#include <grub/mm.h>
> > >> +#include <grub/misc.h>
> > >> +#include <grub/disk.h>
> > >> +#include <grub/dl.h>
> > >> +#include <grub/types.h>
> > >> +#include <grub/charset.h>
> > >> +#include <grub/fshelp.h>
> > >> +
> > >> +GRUB_MOD_LICENSE ("GPLv3+");
> > >> +
> > >> +/* F2FS Magic Number */
> > >> +#define F2FS_SUPER_MAGIC     0xF2F52010
> > >> +#define CHECKSUM_OFFSET              4092            /* must be
> aligned 4 bytes */
> > >> +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> > >> +
> > >> +/* byte-size offset */
> > >> +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> > >> +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >>
> GRUB_DISK_SECTOR_BITS)
> > >> +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>
> \
> > >> +                                             GRUB_DISK_SECTOR_BITS)
> > >> +
> > >> +/* 9 bits for 512 bytes */
> > >> +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> > >> +
> > >> +/* support only 4KB block */
> > >> +#define F2FS_BLK_BITS        12
> > >> +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> > >> +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> > >> +
> > >> +#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  (sizeof(struct grub_f2fs_nat_jent))
> > >> +#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 (sizeof(struct grub_f2fs_nat_entry))
> > >> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> > >> +
> > >> +#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
> > >> +
> > >> +#define CKPT_FLAG_SET(ckpt, f)       \
> > >> +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> > >> +
> > >> +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> > >> +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> > >> +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> > >> +#define F2FS_DATA_EXIST              0x08    /* file inline data
> exist flag */
> > >> +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot
> dentries */
> > >> +
> > >> +enum FILE_TYPE
> > >> +{
> > >> +  F2FS_FT_UNKNOWN,
> > >> +  F2FS_FT_REG_FILE = 1,
> > >> +  F2FS_FT_DIR = 2,
> > >> +  F2FS_FT_SYMLINK = 7,
> > >> +};
> > >> +
> > >> +#define MAX_VOLUME_NAME              512
> > >> +
> > >> +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[MAX_VOLUME_NAME];
> > >> +  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;
> > >> +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE
> totally */
> > >> +  };
> > >> +  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_uint8_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
> > >> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > >> +{
> > >> +  return addr[nr >> 3] & (1 << (nr & 7));
> > >> +}
> > >> +
> > >> +static inline char *
> > >> +__inline_addr (struct grub_f2fs_inode *inode)
> > >> +{
> > >> +  return (char *)&inode->i_addr[1];
> > >> +}
> > >> +
> > >> +static inline grub_uint64_t
> > >> +grub_f2fs_file_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 = &data->ckpt;
> > >> +  grub_uint32_t start_addr = data->cp_blkaddr;
> > >> +
> > >> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> > >> +
> > >> +  return __start_cp_addr (data) +
> > >> +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> > >> +     - (base + 1) + type;
> > >> +}
> > >> +
> > >> +static inline void *
> > >> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> > >> +{
> > >> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> > >> +  grub_uint32_t offset;
> > >> +
> > >> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> > >> +    return ckpt->sit_nat_version_bitmap;
> > >> +
> > >> +  offset = 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 inode_block)
> > >> +{
> > >> +  if (inode_block)
> > >> +    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,
> > >> +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> > >> +             0, F2FS_BLKSIZE, buf);
> > >> +}
> > >> +
> > >> +/*
> > >> + * CRC32
> > >> +*/
> > >> +#define CRCPOLY_LE 0xedb88320
> > >> +
> > >> +static inline grub_uint32_t
> > >> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> > >> +{
> > >> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> > >> +  unsigned char *p = (unsigned char *)buf;
> > >> +  grub_uint32_t tmp = len;
> > >> +  int i;
> > >> +
> > >> +  while (tmp--)
> > >> +    {
> > >> +      crc ^= *p++;
> > >> +      for (i = 0; i < 8; i++)
> > >> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> > >> +    }
> > >> +  return crc;
> > >> +}
> > >> +
> > >> +static inline int
> > >> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> grub_uint32_t len)
> > >> +{
> > >> +  grub_uint32_t cal_crc = 0;
> > >> +
> > >> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> > >> +
> > >> +  return (cal_crc == blk_crc) ? 1 : 0;
> > >> +}
> > >> +
> > >> +static inline int
> > >> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > >> +{
> > >> +  int mask;
> > >> +
> > >> +  p += (nr >> 3);
> > >> +  mask = 1 << (7 - (nr & 0x07));
> > >> +  return mask & *p;
> > >> +}
> > >> +
> > >> +static int
> > >> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > >> +{
> > >> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > >> +
> > >> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > >> +    return -1;
> > >> +
> > >> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time
> (F2FS_BLK_BITS))
> > >> +    return -1;
> > >> +
> > >> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > >> +  log_sectors_per_block = grub_le_to_cpu32
> (sb->log_sectors_per_block);
> > >> +
> > >> +  if (log_sectorsize > F2FS_BLK_BITS)
> > >> +    return -1;
> > >> +
> > >> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > >> +    return -1;
> > >> +
> > >> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> > >> +    return -1;
> > >> +
> > >> +  return 0;
> > >> +}
> > >> +
> > >> +static int
> > >> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t
> offset)
> > >> +{
> > >> +  grub_disk_t disk = data->disk;
> > >> +  grub_err_t err;
> > >> +
> > >> +  /* Read first super block. */
> > >> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> &data->sblock);
> > >> +  if (err)
> > >> +    return -1;
> > >> +
> > >> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> > >> +}
> > >> +
> > >> +static void *
> > >> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t
> cp_addr,
> > >> +     grub_uint64_t *version)
> > >> +{
> > >> +  grub_uint32_t *cp_page_1, *cp_page_2;
> > >> +  struct grub_f2fs_checkpoint *cp_block;
> > >> +  grub_uint64_t cur_version = 0, pre_version = 0;
> > >> +  grub_uint32_t crc = 0;
> > >> +  grub_uint32_t crc_offset;
> > >> +  grub_err_t err;
> > >> +
> > >> +  /* Read the 1st cp block in this CP pack */
> > >> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!cp_page_1)
> > >> +    return NULL;
> > >> +
> > >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > >> +  if (err)
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > >> +  if (crc_offset != CHECKSUM_OFFSET)
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> > >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > >> +
> > >> +  /* Read the 2nd cp block in this CP pack */
> > >> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!cp_page_2)
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count)
> - 1;
> > >> +
> > >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > >> +  if (err)
> > >> +    goto invalid_cp2;
> > >> +
> > >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > >> +  if (crc_offset != CHECKSUM_OFFSET)
> > >> +    goto invalid_cp2;
> > >> +
> > >> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> > >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > >> +    goto invalid_cp2;
> > >> +
> > >> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > >> +  if (cur_version == pre_version)
> > >> +    {
> > >> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > >> +  cp1 = 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 += data->blocks_per_seg;
> > >> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > >> +  if (!cp2 && grub_errno)
> > >> +    {
> > >> +      grub_free (cp1);
> > >> +      return grub_errno;
> > >> +    }
> > >> +
> > >> +  if (cp1 && cp2)
> > >> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > >> +  else if (cp1)
> > >> +    cur_page = cp1;
> > >> +  else if (cp2)
> > >> +    cur_page = cp2;
> > >> +  else
> > >> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> > >> +
> > >> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> > >> +
> > >> +  grub_free (cp1);
> > >> +  grub_free (cp2);
> > >> +  return 0;
> > >> +}
> > >> +
> > >> +static grub_err_t
> > >> +get_nat_journal (struct grub_f2fs_data *data)
> > >> +{
> > >> +  grub_uint32_t block;
> > >> +  char *buf;
> > >> +  grub_err_t err;
> > >> +
> > >> +  buf = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!buf)
> > >> +    return grub_errno;
> > >> +
> > >> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> > >> +    block = __start_sum_block (data);
> > >> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> > >> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > >> +  else
> > >> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE,
> CURSEG_HOT_DATA);
> > >> +
> > >> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> > >> +  grub_uint32_t blkaddr = 0;
> > >> +  grub_uint16_t i;
> > >> +
> > >> +  for (i = 0; i < n; i++)
> > >> +    {
> > >> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> > >> +        {
> > >> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> > >> +  if (blkaddr)
> > >> +    return blkaddr;
> > >> +
> > >> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!nat_block)
> > >> +    return 0;
> > >> +
> > >> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> > >> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> > >> +
> > >> +  seg_off = block_off / data->blocks_per_seg;
> > >> +  block_addr = 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 += data->blocks_per_seg;
> > >> +
> > >> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> > >> +  if (err)
> > >> +    {
> > >> +      grub_free (nat_block);
> > >> +      return 0;
> > >> +    }
> > >> +
> > >> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> > >> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > >> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > >> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > >> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > >> +  int n = 0;
> > >> +  int level = 0;
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> > >> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > >> +
> > >> +  noffset[0] = 0;
> > >> +
> > >> +  if (block < direct_index)
> > >> +    {
> > >> +      offset[n] = block;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= direct_index;
> > >> +  if (block < direct_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_DIR1_BLOCK;
> > >> +      noffset[n] = 1;
> > >> +      offset[n] = block;
> > >> +      level = 1;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= direct_blks;
> > >> +  if (block < direct_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_DIR2_BLOCK;
> > >> +      noffset[n] = 2;
> > >> +      offset[n] = block;
> > >> +      level = 1;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= direct_blks;
> > >> +  if (block < indirect_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_IND1_BLOCK;
> > >> +      noffset[n] = 3;
> > >> +      offset[n++] = block / direct_blks;
> > >> +      noffset[n] = 4 + offset[n - 1];
> > >> +      offset[n] = block % direct_blks;
> > >> +      level = 2;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= indirect_blks;
> > >> +  if (block < indirect_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_IND2_BLOCK;
> > >> +      noffset[n] = 4 + dptrs_per_blk;
> > >> +      offset[n++] = block / direct_blks;
> > >> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > >> +      offset[n] = block % direct_blks;
> > >> +      level = 2;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= indirect_blks;
> > >> +  if (block < dindirect_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_DIND_BLOCK;
> > >> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > >> +      offset[n++] = block / indirect_blks;
> > >> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > >> +      offset[n - 1] * (dptrs_per_blk + 1);
> > >> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > >> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > >> +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> > >> +      offset[n] = block % direct_blks;
> > >> +      level = 3;
> > >> +      goto got;
> > >> +    }
> > >> +got:
> > >> +  return level;
> > >> +}
> > >> +
> > >> +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 = 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 = grub_malloc (sizeof (*data));
> > >> +  if (!data)
> > >> +    return NULL;
> > >> +
> > >> +  data->disk = disk;
> > >> +
> > >> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> > >> +    {
> > >> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> > >> +        {
> > >> +          if (grub_errno == GRUB_ERR_NONE)
> > >> +            grub_error (GRUB_ERR_BAD_FS,
> > >> +                     "not a F2FS filesystem (no superblock)");
> > >> +          goto fail;
> > >> +        }
> > >> +    }
> > >> +
> > >> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > >> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > >> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > >> +  data->blocks_per_seg = 1 <<
> > >> +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > >> +
> > >> +  err = grub_f2fs_read_cp (data);
> > >> +  if (err)
> > >> +    goto fail;
> > >> +
> > >> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> > >> +
> > >> +  err = get_nat_journal (data);
> > >> +  if (err)
> > >> +    goto fail;
> > >> +
> > >> +  data->diropen.data = data;
> > >> +  data->diropen.ino = data->root_ino;
> > >> +  data->diropen.inode_read = 1;
> > >> +  data->inode = &data->diropen.inode;
> > >> +
> > >> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > >> +  if (err)
> > >> +    goto fail;
> > >> +
> > >> +  return data;
> > >> +
> > >> +fail:
> > >> +  grub_free (data);
> > >> +  return NULL;
> > >> +}
> > >> +
> > >> +/* guarantee inline_data was handled by caller */
> > >> +static grub_disk_addr_t
> > >> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t
> block_ofs)
> > >> +{
> > >> +  struct grub_f2fs_data *data = node->data;
> > >> +  struct grub_f2fs_inode *inode = &node->inode.i;
> > >> +  grub_uint32_t offset[4], noffset[4], nids[4];
> > >> +  struct grub_f2fs_node *node_block;
> > >> +  grub_uint32_t block_addr = -1;
> > >> +  int level, i;
> > >> +
> > >> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > >> +  if (level == 0)
> > >> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > >> +
> > >> +  node_block = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!node_block)
> > >> +    return -1;
> > >> +
> > >> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > >> +
> > >> +  /* get indirect or direct nodes */
> > >> +  for (i = 1; i <= level; i++)
> > >> +    {
> > >> +      grub_f2fs_read_node (data, nids[i], node_block);
> > >> +      if (grub_errno)
> > >> +        goto fail;
> > >> +
> > >> +      if (i < level)
> > >> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > >> +    }
> > >> +
> > >> +  block_addr = 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 = &node->inode.i;
> > >> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > >> +  char *inline_addr = __inline_addr (inode);
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_DATA)
> > >> +    {
> > >> +      if (filesize > MAX_INLINE_DATA)
> > >> +        return -1;
> > >> +      if (len > filesize - pos)
> > >> +        len = filesize - pos;
> > >> +
> > >> +      grub_memcpy (buf, 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_get_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 = node;
> > >> +  grub_uint64_t filesize;
> > >> +
> > >> +  if (!diro->inode_read)
> > >> +    {
> > >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > >> +      if (grub_errno)
> > >> +        return 0;
> > >> +    }
> > >> +
> > >> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> > >> +
> > >> +  symlink = grub_malloc (filesize + 1);
> > >> +  if (!symlink)
> > >> +    return 0;
> > >> +
> > >> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> > >> +  if (grub_errno)
> > >> +    {
> > >> +      grub_free (symlink);
> > >> +      return 0;
> > >> +    }
> > >> +
> > >> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> > >> +    {
> > >> +      char *filename;
> > >> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > >> +      enum FILE_TYPE ftype;
> > >> +      int name_len;
> > >> +      int ret;
> > >> +
> > >> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> > >> +        {
> > >> +          i++;
> > >> +          continue;
> > >> +        }
> > >> +
> > >> +      ftype = ctx->dentry[i].file_type;
> > >> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > >> +      filename = grub_malloc (name_len + 1);
> > >> +      if (!filename)
> > >> +        return 0;
> > >> +
> > >> +      grub_memcpy (filename, ctx->filename[i], name_len);
> > >> +      filename[name_len] = 0;
> > >> +
> > >> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > >> +      if (!fdiro)
> > >> +        {
> > >> +          grub_free(filename);
> > >> +          return 0;
> > >> +        }
> > >> +
> > >> +      if (ftype == F2FS_FT_DIR)
> > >> +        type = GRUB_FSHELP_DIR;
> > >> +      else if (ftype == F2FS_FT_SYMLINK)
> > >> +        type = GRUB_FSHELP_SYMLINK;
> > >> +      else if (ftype == F2FS_FT_REG_FILE)
> > >> +        type = GRUB_FSHELP_REG;
> > >> +
> > >> +      fdiro->data = ctx->data;
> > >> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > >> +      fdiro->inode_read = 0;
> > >> +
> > >> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> > >> +      grub_free(filename);
> > >> +      if (ret)
> > >> +        return 1;
> > >> +
> > >> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> > >> +
> > >> +  ctx->bitmap = de_blk->dentry_bitmap;
> > >> +  ctx->dentry = de_blk->dentry;
> > >> +  ctx->filename = de_blk->filename;
> > >> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> > >> +  struct grub_f2fs_inode *inode;
> > >> +  struct grub_f2fs_dir_iter_ctx ctx = {
> > >> +    .data = diro->data,
> > >> +    .hook = hook,
> > >> +    .hook_data = hook_data
> > >> +  };
> > >> +  grub_off_t fpos = 0;
> > >> +
> > >> +  if (!diro->inode_read)
> > >> +    {
> > >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > >> +      if (grub_errno)
> > >> +     return 0;
> > >> +    }
> > >> +
> > >> +  inode = &diro->inode.i;
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> > >> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> > >> +
> > >> +  while (fpos < grub_f2fs_file_size (inode))
> > >> +    {
> > >> +      struct grub_f2fs_dentry_block *de_blk;
> > >> +      char *buf;
> > >> +      int ret;
> > >> +
> > >> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> > >> +
> > >> +      ctx.bitmap = de_blk->dentry_bitmap;
> > >> +      ctx.dentry = de_blk->dentry;
> > >> +      ctx.filename = de_blk->filename;
> > >> +      ctx.max = NR_DENTRY_IN_BLOCK;
> > >> +
> > >> +      ret = grub_f2fs_check_dentries (&ctx);
> > >> +      grub_free (buf);
> > >> +      if (ret)
> > >> +        return 1;
> > >> +
> > >> +      fpos += F2FS_BLKSIZE;
> > >> +    }
> > >> +  return 0;
> > >> +}
> > >> +
> > >> +static int
> > >> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> filetype,
> > >> +           grub_fshelp_node_t node, void *data)
> > >> +{
> > >> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> > >> +      grub_errno = GRUB_ERR_NONE;
> > >> +    }
> > >> +  if (node->inode_read)
> > >> +    {
> > >> +      info.mtimeset = 1;
> > >> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> > >> +    }
> > >> +
> > >> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> > >> +  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 = {
> > >> +    .hook = hook,
> > >> +    .hook_data = hook_data
> > >> +  };
> > >> +  struct grub_fshelp_node *fdiro = 0;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  ctx.data = 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 != &ctx.data->diropen)
> > >> +    grub_free (fdiro);
> > >> + 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 = NULL;
> > >> +  struct grub_fshelp_node *fdiro = 0;
> > >> +  struct grub_f2fs_inode *inode;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  data = 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, sizeof (*data->inode));
> > >> +  grub_free (fdiro);
> > >> +
> > >> +  inode = &(data->inode->i);
> > >> +  file->size = grub_f2fs_file_size (inode);
> > >> +  file->data = data;
> > >> +  file->offset = 0;
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size >
> MAX_INLINE_DATA)
> > >> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> > >> +  return 0;
> > >> +
> > >> +fail:
> > >> +  if (fdiro != &data->diropen)
> > >> +    grub_free (fdiro);
> > >> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > >> +
> > >> +  grub_free (data);
> > >> +
> > >> +  grub_dl_unref (my_mod);
> > >> +
> > >> +  return GRUB_ERR_NONE;
> > >> +}
> > >> +
> > >> +static grub_uint8_t *
> > >> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> > >> +{
> > >> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> > >> +  grub_uint8_t *out_buf;
> > >> +  int len = 0;
> > >> +
> > >> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 +
> 1);
> > >> +  if (!out_buf)
> > >> +    return NULL;
> > >> +
> > >> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> > >> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> > >> +    len++;
> > >> +  }
> > >> +
> > >> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> > >> +  return out_buf;
> > >> +}
> > >> +
> > >> +static grub_err_t
> > >> +grub_f2fs_label (grub_device_t device, char **label)
> > >> +{
> > >> +  struct grub_f2fs_data *data;
> > >> +  grub_disk_t disk = device->disk;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  data = grub_f2fs_mount (disk);
> > >> +  if (data)
> > >> +    *label = (char *) grub_f2fs_utf16_to_utf8
> (data->sblock.volume_name);
> > >> +  else
> > >> +    *label = NULL;
> > >> +
> > >> +  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 = device->disk;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  data = grub_f2fs_mount (disk);
> > >> +  if (data)
> > >> +    {
> > >> +      *uuid =
> > >> +     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 = NULL;
> > >> +
> > >> +  grub_free (data);
> > >> +  grub_dl_unref (my_mod);
> > >> +  return grub_errno;
> > >> +}
> > >> +
> > >> +static struct grub_fs grub_f2fs_fs = {
> > >> +  .name = "f2fs",
> > >> +  .dir = grub_f2fs_dir,
> > >> +  .open = grub_f2fs_open,
> > >> +  .read = grub_f2fs_read,
> > >> +  .close = grub_f2fs_close,
> > >> +  .label = grub_f2fs_label,
> > >> +  .uuid = grub_f2fs_uuid,
> > >> +#ifdef GRUB_UTIL
> > >> +  .reserved_first_sector = 1,
> > >> +  .blocklist_install = 0,
> > >> +#endif
> > >> +  .next = 0
> > >> +};
> > >> +
> > >> +GRUB_MOD_INIT (f2fs)
> > >> +{
> > >> +  grub_fs_register (&grub_f2fs_fs);
> > >> +  my_mod = mod;
> > >> +}
> > >> +
> > >> +GRUB_MOD_FINI (f2fs)
> > >> +{
> > >> +  grub_fs_unregister (&grub_f2fs_fs);
> > >> +}
> > >> diff --git a/po/exclude.pot b/po/exclude.pot
> > >> index 0a9b215..816089c 100644
> > >> --- a/po/exclude.pot
> > >> +++ b/po/exclude.pot
> > >> @@ -1214,6 +1214,7 @@ msgstr ""
> > >>
> > >>  #: 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..1ea77c8
> > >> --- /dev/null
> > >> +++ b/tests/f2fs_test.in
> > >> @@ -0,0 +1,19 @@
> > >> +#!/bin/sh
> > >> +
> > >> +set -e
> > >> +
> > >> +if [ "x$EUID" = "x" ] ; then
> > >> + EUID=`id -u`
> > >> +fi
> > >> +
> > >> +if [ "$EUID" != 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 424de22..e3e4109 100644
> > >> --- a/tests/util/grub-fs-tester.in
> > >> +++ b/tests/util/grub-fs-tester.in
> > >> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>       xsquash*)
> > >>           MINBLKSIZE=4096
> > >>           MAXBLKSIZE=1048576;;
> > >> -     xxfs)
> > >> +     xxfs|xf2fs)
> > >>           MINBLKSIZE=$SECSIZE
> > >>               # OS Limitation: GNU/Linux doesn't accept > 4096
> > >>           MAXBLKSIZE=4096;;
> > >> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>               x"btrfs"*)
> > >>                   FSLABEL="grub_;/testé莭莽😁киритi
> urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvn
> irewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvr
> enurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoi
> roireoireoifrefoieroifoireoif";;
> > >>
> > >> +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> > >> +             x"f2fs")
> > >> +                 FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruew
> nuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvn
> uewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> > >> +
> > >>           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> > >>               x"exfat")
> > >>                   FSLABEL="géт ;/莭莽😁кир";;
> > >> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> Check it.
> > >>       # FS LIMITATION: as far as I know those FS don't store their
> last modification date.
> > >>               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" |
> x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> > >> -                 | 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=y;;
> > >> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> > >>                   MOUNTFS=ext2
> > >>                   "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)
> > >> --
> > >> 2.6.3
> > >>
> > >>
> > >> ------------------------------------------------------------
> ------------------
> > >> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> > >> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> > >> Monitor end-to-end web transactions and take corrective actions now
> > >> Troubleshoot faster and improve end-user experience. Signup Now!
> > >> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> > >> _______________________________________________
> > >> Linux-f2fs-devel mailing list
> > >> Linux-f2fs-devel@lists.sourceforge.net
> > >> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #1.2: Type: text/html, Size: 79702 bytes --]

[-- Attachment #2: Type: text/plain, Size: 141 bytes --]

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [f2fs-dev] [2.02] Re: [PATCH v8] F2FS support
@ 2016-08-05 19:17                           ` Michael Zimmermann
  0 siblings, 0 replies; 77+ messages in thread
From: Michael Zimmermann @ 2016-08-05 19:17 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Andrei Borzenkov, linux-f2fs-devel

[-- Attachment #1: Type: text/plain, Size: 56645 bytes --]

> BTW, I've heard that openmandriva adopted this patch to support F2FS in
their
installer.

And I've ported it to clover's GrubFSPkg which I'm using for EFIDroid :)
Your timing was really perfect. The day I needed f2fs support I found your
V1 in the mailing list :D
https://github.com/efidroid/uefi_edk2packages_GrubFSPkg/blob/master/grub/grub-core/fs/f2fs.c

Thanks
Michael

On Fri, Aug 5, 2016 at 8:07 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:

> On Fri, Aug 05, 2016 at 01:57:37PM +0300, Andrei Borzenkov wrote:
> > Vladimir prefers to postpone new features until 2.02 is out. From my
> > side I think it is isolated enough and new code so low regression
> > risk. Vladimir has the final word as maintainer :)
>
> Okay, I can wait for 2.02. ;)
> BTW, I've heard that openmandriva adopted this patch to support F2FS in
> their
> installer.
>
> Thanks,
>
> >
> > On Thu, Aug 4, 2016 at 8:06 PM, Jaegeuk Kim <jaegeuk@kernel.org> wrote:
> > > Hi Andrei,
> > >
> > > It's been sitting for a long long time.
> > > May I ask whether or not there is any merging plan of this?
> > >
> > > Thanks,
> > >
> > > On Thu, Mar 03, 2016 at 01:36:53PM -0800, Jaegeuk Kim wrote:
> > >> Change log from v7:
> > >>  - fix an offset bug in read_file
> > >>
> > >> -- >8 --
> > >> From d774cdecc2fee18ed44098c88d2a54d358bc04d3 Mon Sep 17 00:00:00
> 2001
> > >> From: Jaegeuk Kim <jaegeuk@kernel.org>
> > >> Date: Tue, 17 Nov 2015 12:45:13 -0800
> > >> Subject: [PATCH] F2FS support
> > >>
> > >> "F2FS (Flash-Friendly File System) is flash-friendly file system
> which was merged
> > >> into Linux kernel v3.8 in 2013.
> > >>
> > >> The motive for F2FS was to build a file system that from the start,
> takes into
> > >> account the characteristics of NAND flash memory-based storage
> devices (such as
> > >> solid-state disks, eMMC, and SD cards).
> > >>
> > >> F2FS was designed on a basis of a log-structured file system
> approach, which
> > >> remedies some known issues of the older log structured file systems,
> such as
> > >> the snowball effect of wandering trees and high cleaning overhead. In
> addition,
> > >> since a NAND-based storage device shows different characteristics
> according to
> > >> its internal geometry or flash memory management scheme (such as the
> Flash
> > >> Translation Layer or FTL), it supports various parameters not only for
> > >> configuring on-disk layout, but also for selecting allocation and
> cleaning
> > >> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
> > >>
> > >> The source codes for F2FS are available from:
> > >>
> > >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> > >> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
> > >>
> > >> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> > >> ---
> > >>  Makefile.util.def            |    7 +
> > >>  docs/grub.texi               |    5 +-
> > >>  grub-core/Makefile.core.def  |    5 +
> > >>  grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++
> ++++++++++++
> > >>  po/exclude.pot               |    1 +
> > >>  tests/f2fs_test.in           |   19 +
> > >>  tests/util/grub-fs-tester.in |   10 +-
> > >>  7 files changed, 1332 insertions(+), 4 deletions(-)
> > >>  create mode 100644 grub-core/fs/f2fs.c
> > >>  create mode 100644 tests/f2fs_test.in
> > >>
> > >> diff --git a/Makefile.util.def b/Makefile.util.def
> > >> index db7e8ec..84627bb 100644
> > >> --- a/Makefile.util.def
> > >> +++ b/Makefile.util.def
> > >> @@ -99,6 +99,7 @@ library = {
> > >>    common = grub-core/fs/ext2.c;
> > >>    common = grub-core/fs/fat.c;
> > >>    common = grub-core/fs/exfat.c;
> > >> +  common = grub-core/fs/f2fs.c;
> > >>    common = grub-core/fs/fshelp.c;
> > >>    common = grub-core/fs/hfs.c;
> > >>    common = grub-core/fs/hfsplus.c;
> > >> @@ -766,6 +767,12 @@ script = {
> > >>
> > >>  script = {
> > >>    testcase;
> > >> +  name = f2fs_test;
> > >> +  common = tests/f2fs_test.in;
> > >> +};
> > >> +
> > >> +script = {
> > >> +  testcase;
> > >>    name = nilfs2_test;
> > >>    common = tests/nilfs2_test.in;
> > >>  };
> > >> diff --git a/docs/grub.texi b/docs/grub.texi
> > >> index 1df3db2..e5a80f3 100644
> > >> --- a/docs/grub.texi
> > >> +++ b/docs/grub.texi
> > >> @@ -360,7 +360,8 @@ blocklist notation. The currently supported
> filesystem 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}, @dfn{HFS},
> > >> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> > >> +@dfn{f2fs}, @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},
> > >> @@ -5347,7 +5348,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, minix,
> > >>  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
> charset
> > >>  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 d9fa0e3..b585ade 100644
> > >> --- a/grub-core/Makefile.core.def
> > >> +++ b/grub-core/Makefile.core.def
> > >> @@ -1278,6 +1278,11 @@ module = {
> > >>  };
> > >>
> > >>  module = {
> > >> +  name = f2fs;
> > >> +  common = fs/f2fs.c;
> > >> +};
> > >> +
> > >> +module = {
> > >>    name = fshelp;
> > >>    common = fs/fshelp.c;
> > >>  };
> > >> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> > >> new file mode 100644
> > >> index 0000000..7fb256f
> > >> --- /dev/null
> > >> +++ b/grub-core/fs/f2fs.c
> > >> @@ -0,0 +1,1289 @@
> > >> +/*
> > >> + *  f2fs.c - Flash-Friendly File System
> > >> + *
> > >> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> > >> + *
> > >> + *  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 <http://www.gnu.org/licenses/>.
> > >> + */
> > >> +#include <grub/err.h>
> > >> +#include <grub/file.h>
> > >> +#include <grub/mm.h>
> > >> +#include <grub/misc.h>
> > >> +#include <grub/disk.h>
> > >> +#include <grub/dl.h>
> > >> +#include <grub/types.h>
> > >> +#include <grub/charset.h>
> > >> +#include <grub/fshelp.h>
> > >> +
> > >> +GRUB_MOD_LICENSE ("GPLv3+");
> > >> +
> > >> +/* F2FS Magic Number */
> > >> +#define F2FS_SUPER_MAGIC     0xF2F52010
> > >> +#define CHECKSUM_OFFSET              4092            /* must be
> aligned 4 bytes */
> > >> +#define U32_CHECKSUM_OFFSET  (CHECKSUM_OFFSET >> 2)
> > >> +
> > >> +/* byte-size offset */
> > >> +#define F2FS_SUPER_OFFSET    ((grub_disk_addr_t)1024)
> > >> +#define F2FS_SUPER_OFFSET0   (F2FS_SUPER_OFFSET >>
> GRUB_DISK_SECTOR_BITS)
> > >> +#define F2FS_SUPER_OFFSET1   ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>
> \
> > >> +                                             GRUB_DISK_SECTOR_BITS)
> > >> +
> > >> +/* 9 bits for 512 bytes */
> > >> +#define F2FS_MIN_LOG_SECTOR_SIZE     9
> > >> +
> > >> +/* support only 4KB block */
> > >> +#define F2FS_BLK_BITS        12
> > >> +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
> > >> +#define F2FS_BLK_SEC_BITS    (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> > >> +
> > >> +#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  (sizeof(struct grub_f2fs_nat_jent))
> > >> +#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 (sizeof(struct grub_f2fs_nat_entry))
> > >> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> > >> +
> > >> +#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
> > >> +
> > >> +#define CKPT_FLAG_SET(ckpt, f)       \
> > >> +             (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> > >> +
> > >> +#define F2FS_INLINE_XATTR    0x01    /* file inline xattr flag */
> > >> +#define F2FS_INLINE_DATA     0x02    /* file inline data flag */
> > >> +#define F2FS_INLINE_DENTRY   0x04    /* file inline dentry flag */
> > >> +#define F2FS_DATA_EXIST              0x08    /* file inline data
> exist flag */
> > >> +#define F2FS_INLINE_DOTS     0x10    /* file having implicit dot
> dentries */
> > >> +
> > >> +enum FILE_TYPE
> > >> +{
> > >> +  F2FS_FT_UNKNOWN,
> > >> +  F2FS_FT_REG_FILE = 1,
> > >> +  F2FS_FT_DIR = 2,
> > >> +  F2FS_FT_SYMLINK = 7,
> > >> +};
> > >> +
> > >> +#define MAX_VOLUME_NAME              512
> > >> +
> > >> +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[MAX_VOLUME_NAME];
> > >> +  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;
> > >> +    char buf[F2FS_BLKSIZE - 40];     /* Should occupy F2FS_BLKSIZE
> totally */
> > >> +  };
> > >> +  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_uint8_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
> > >> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> > >> +{
> > >> +  return addr[nr >> 3] & (1 << (nr & 7));
> > >> +}
> > >> +
> > >> +static inline char *
> > >> +__inline_addr (struct grub_f2fs_inode *inode)
> > >> +{
> > >> +  return (char *)&inode->i_addr[1];
> > >> +}
> > >> +
> > >> +static inline grub_uint64_t
> > >> +grub_f2fs_file_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 = &data->ckpt;
> > >> +  grub_uint32_t start_addr = data->cp_blkaddr;
> > >> +
> > >> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> > >> +
> > >> +  return __start_cp_addr (data) +
> > >> +     grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> > >> +     - (base + 1) + type;
> > >> +}
> > >> +
> > >> +static inline void *
> > >> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> > >> +{
> > >> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> > >> +  grub_uint32_t offset;
> > >> +
> > >> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> > >> +    return ckpt->sit_nat_version_bitmap;
> > >> +
> > >> +  offset = 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 inode_block)
> > >> +{
> > >> +  if (inode_block)
> > >> +    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,
> > >> +             ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> > >> +             0, F2FS_BLKSIZE, buf);
> > >> +}
> > >> +
> > >> +/*
> > >> + * CRC32
> > >> +*/
> > >> +#define CRCPOLY_LE 0xedb88320
> > >> +
> > >> +static inline grub_uint32_t
> > >> +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
> > >> +{
> > >> +  grub_uint32_t crc = F2FS_SUPER_MAGIC;
> > >> +  unsigned char *p = (unsigned char *)buf;
> > >> +  grub_uint32_t tmp = len;
> > >> +  int i;
> > >> +
> > >> +  while (tmp--)
> > >> +    {
> > >> +      crc ^= *p++;
> > >> +      for (i = 0; i < 8; i++)
> > >> +        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
> > >> +    }
> > >> +  return crc;
> > >> +}
> > >> +
> > >> +static inline int
> > >> +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const
> grub_uint32_t len)
> > >> +{
> > >> +  grub_uint32_t cal_crc = 0;
> > >> +
> > >> +  cal_crc = grub_f2fs_cal_crc32 (buf, len);
> > >> +
> > >> +  return (cal_crc == blk_crc) ? 1 : 0;
> > >> +}
> > >> +
> > >> +static inline int
> > >> +grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
> > >> +{
> > >> +  int mask;
> > >> +
> > >> +  p += (nr >> 3);
> > >> +  mask = 1 << (7 - (nr & 0x07));
> > >> +  return mask & *p;
> > >> +}
> > >> +
> > >> +static int
> > >> +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
> > >> +{
> > >> +  grub_uint32_t log_sectorsize, log_sectors_per_block;
> > >> +
> > >> +  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
> > >> +    return -1;
> > >> +
> > >> +  if (sb->log_blocksize != grub_cpu_to_le32_compile_time
> (F2FS_BLK_BITS))
> > >> +    return -1;
> > >> +
> > >> +  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
> > >> +  log_sectors_per_block = grub_le_to_cpu32
> (sb->log_sectors_per_block);
> > >> +
> > >> +  if (log_sectorsize > F2FS_BLK_BITS)
> > >> +    return -1;
> > >> +
> > >> +  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
> > >> +    return -1;
> > >> +
> > >> +  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
> > >> +    return -1;
> > >> +
> > >> +  return 0;
> > >> +}
> > >> +
> > >> +static int
> > >> +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t
> offset)
> > >> +{
> > >> +  grub_disk_t disk = data->disk;
> > >> +  grub_err_t err;
> > >> +
> > >> +  /* Read first super block. */
> > >> +  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock),
> &data->sblock);
> > >> +  if (err)
> > >> +    return -1;
> > >> +
> > >> +  return grub_f2fs_sanity_check_sb (&data->sblock);
> > >> +}
> > >> +
> > >> +static void *
> > >> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t
> cp_addr,
> > >> +     grub_uint64_t *version)
> > >> +{
> > >> +  grub_uint32_t *cp_page_1, *cp_page_2;
> > >> +  struct grub_f2fs_checkpoint *cp_block;
> > >> +  grub_uint64_t cur_version = 0, pre_version = 0;
> > >> +  grub_uint32_t crc = 0;
> > >> +  grub_uint32_t crc_offset;
> > >> +  grub_err_t err;
> > >> +
> > >> +  /* Read the 1st cp block in this CP pack */
> > >> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!cp_page_1)
> > >> +    return NULL;
> > >> +
> > >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> > >> +  if (err)
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> > >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > >> +  if (crc_offset != CHECKSUM_OFFSET)
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> > >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > >> +
> > >> +  /* Read the 2nd cp block in this CP pack */
> > >> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!cp_page_2)
> > >> +    goto invalid_cp1;
> > >> +
> > >> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count)
> - 1;
> > >> +
> > >> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> > >> +  if (err)
> > >> +    goto invalid_cp2;
> > >> +
> > >> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> > >> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> > >> +  if (crc_offset != CHECKSUM_OFFSET)
> > >> +    goto invalid_cp2;
> > >> +
> > >> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> > >> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> > >> +    goto invalid_cp2;
> > >> +
> > >> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> > >> +  if (cur_version == pre_version)
> > >> +    {
> > >> +      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
> > >> +  cp1 = 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 += data->blocks_per_seg;
> > >> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> > >> +  if (!cp2 && grub_errno)
> > >> +    {
> > >> +      grub_free (cp1);
> > >> +      return grub_errno;
> > >> +    }
> > >> +
> > >> +  if (cp1 && cp2)
> > >> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> > >> +  else if (cp1)
> > >> +    cur_page = cp1;
> > >> +  else if (cp2)
> > >> +    cur_page = cp2;
> > >> +  else
> > >> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> > >> +
> > >> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> > >> +
> > >> +  grub_free (cp1);
> > >> +  grub_free (cp2);
> > >> +  return 0;
> > >> +}
> > >> +
> > >> +static grub_err_t
> > >> +get_nat_journal (struct grub_f2fs_data *data)
> > >> +{
> > >> +  grub_uint32_t block;
> > >> +  char *buf;
> > >> +  grub_err_t err;
> > >> +
> > >> +  buf = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!buf)
> > >> +    return grub_errno;
> > >> +
> > >> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> > >> +    block = __start_sum_block (data);
> > >> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> > >> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> > >> +  else
> > >> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE,
> CURSEG_HOT_DATA);
> > >> +
> > >> +  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
> > >> +  grub_uint32_t blkaddr = 0;
> > >> +  grub_uint16_t i;
> > >> +
> > >> +  for (i = 0; i < n; i++)
> > >> +    {
> > >> +      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
> > >> +        {
> > >> +          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
> > >> +  if (blkaddr)
> > >> +    return blkaddr;
> > >> +
> > >> +  nat_block = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!nat_block)
> > >> +    return 0;
> > >> +
> > >> +  block_off = nid / NAT_ENTRY_PER_BLOCK;
> > >> +  entry_off = nid % NAT_ENTRY_PER_BLOCK;
> > >> +
> > >> +  seg_off = block_off / data->blocks_per_seg;
> > >> +  block_addr = 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 += data->blocks_per_seg;
> > >> +
> > >> +  err = grub_f2fs_block_read (data, block_addr, nat_block);
> > >> +  if (err)
> > >> +    {
> > >> +      grub_free (nat_block);
> > >> +      return 0;
> > >> +    }
> > >> +
> > >> +  blkaddr = 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 = ADDRS_PER_BLOCK;
> > >> +  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
> > >> +  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
> > >> +  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
> > >> +  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
> > >> +  int n = 0;
> > >> +  int level = 0;
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_XATTR)
> > >> +    direct_index -= F2FS_INLINE_XATTR_ADDRS;
> > >> +
> > >> +  noffset[0] = 0;
> > >> +
> > >> +  if (block < direct_index)
> > >> +    {
> > >> +      offset[n] = block;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= direct_index;
> > >> +  if (block < direct_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_DIR1_BLOCK;
> > >> +      noffset[n] = 1;
> > >> +      offset[n] = block;
> > >> +      level = 1;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= direct_blks;
> > >> +  if (block < direct_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_DIR2_BLOCK;
> > >> +      noffset[n] = 2;
> > >> +      offset[n] = block;
> > >> +      level = 1;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= direct_blks;
> > >> +  if (block < indirect_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_IND1_BLOCK;
> > >> +      noffset[n] = 3;
> > >> +      offset[n++] = block / direct_blks;
> > >> +      noffset[n] = 4 + offset[n - 1];
> > >> +      offset[n] = block % direct_blks;
> > >> +      level = 2;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= indirect_blks;
> > >> +  if (block < indirect_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_IND2_BLOCK;
> > >> +      noffset[n] = 4 + dptrs_per_blk;
> > >> +      offset[n++] = block / direct_blks;
> > >> +      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
> > >> +      offset[n] = block % direct_blks;
> > >> +      level = 2;
> > >> +      goto got;
> > >> +    }
> > >> +
> > >> +  block -= indirect_blks;
> > >> +  if (block < dindirect_blks)
> > >> +    {
> > >> +      offset[n++] = NODE_DIND_BLOCK;
> > >> +      noffset[n] = 5 + (dptrs_per_blk * 2);
> > >> +      offset[n++] = block / indirect_blks;
> > >> +      noffset[n] = 6 + (dptrs_per_blk * 2) +
> > >> +      offset[n - 1] * (dptrs_per_blk + 1);
> > >> +      offset[n++] = (block / direct_blks) % dptrs_per_blk;
> > >> +      noffset[n] = 7 + (dptrs_per_blk * 2) +
> > >> +             offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
> > >> +      offset[n] = block % direct_blks;
> > >> +      level = 3;
> > >> +      goto got;
> > >> +    }
> > >> +got:
> > >> +  return level;
> > >> +}
> > >> +
> > >> +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 = 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 = grub_malloc (sizeof (*data));
> > >> +  if (!data)
> > >> +    return NULL;
> > >> +
> > >> +  data->disk = disk;
> > >> +
> > >> +  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
> > >> +    {
> > >> +      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
> > >> +        {
> > >> +          if (grub_errno == GRUB_ERR_NONE)
> > >> +            grub_error (GRUB_ERR_BAD_FS,
> > >> +                     "not a F2FS filesystem (no superblock)");
> > >> +          goto fail;
> > >> +        }
> > >> +    }
> > >> +
> > >> +  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
> > >> +  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
> > >> +  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
> > >> +  data->blocks_per_seg = 1 <<
> > >> +     grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
> > >> +
> > >> +  err = grub_f2fs_read_cp (data);
> > >> +  if (err)
> > >> +    goto fail;
> > >> +
> > >> +  data->nat_bitmap = __nat_bitmap_ptr (data);
> > >> +
> > >> +  err = get_nat_journal (data);
> > >> +  if (err)
> > >> +    goto fail;
> > >> +
> > >> +  data->diropen.data = data;
> > >> +  data->diropen.ino = data->root_ino;
> > >> +  data->diropen.inode_read = 1;
> > >> +  data->inode = &data->diropen.inode;
> > >> +
> > >> +  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
> > >> +  if (err)
> > >> +    goto fail;
> > >> +
> > >> +  return data;
> > >> +
> > >> +fail:
> > >> +  grub_free (data);
> > >> +  return NULL;
> > >> +}
> > >> +
> > >> +/* guarantee inline_data was handled by caller */
> > >> +static grub_disk_addr_t
> > >> +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t
> block_ofs)
> > >> +{
> > >> +  struct grub_f2fs_data *data = node->data;
> > >> +  struct grub_f2fs_inode *inode = &node->inode.i;
> > >> +  grub_uint32_t offset[4], noffset[4], nids[4];
> > >> +  struct grub_f2fs_node *node_block;
> > >> +  grub_uint32_t block_addr = -1;
> > >> +  int level, i;
> > >> +
> > >> +  level = grub_get_node_path (inode, block_ofs, offset, noffset);
> > >> +  if (level == 0)
> > >> +    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
> > >> +
> > >> +  node_block = grub_malloc (F2FS_BLKSIZE);
> > >> +  if (!node_block)
> > >> +    return -1;
> > >> +
> > >> +  nids[1] = __get_node_id (&node->inode, offset[0], 1);
> > >> +
> > >> +  /* get indirect or direct nodes */
> > >> +  for (i = 1; i <= level; i++)
> > >> +    {
> > >> +      grub_f2fs_read_node (data, nids[i], node_block);
> > >> +      if (grub_errno)
> > >> +        goto fail;
> > >> +
> > >> +      if (i < level)
> > >> +        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
> > >> +    }
> > >> +
> > >> +  block_addr = 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 = &node->inode.i;
> > >> +  grub_off_t filesize = grub_f2fs_file_size (inode);
> > >> +  char *inline_addr = __inline_addr (inode);
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_DATA)
> > >> +    {
> > >> +      if (filesize > MAX_INLINE_DATA)
> > >> +        return -1;
> > >> +      if (len > filesize - pos)
> > >> +        len = filesize - pos;
> > >> +
> > >> +      grub_memcpy (buf, 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_get_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 = node;
> > >> +  grub_uint64_t filesize;
> > >> +
> > >> +  if (!diro->inode_read)
> > >> +    {
> > >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > >> +      if (grub_errno)
> > >> +        return 0;
> > >> +    }
> > >> +
> > >> +  filesize = grub_f2fs_file_size(&diro->inode.i);
> > >> +
> > >> +  symlink = grub_malloc (filesize + 1);
> > >> +  if (!symlink)
> > >> +    return 0;
> > >> +
> > >> +  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
> > >> +  if (grub_errno)
> > >> +    {
> > >> +      grub_free (symlink);
> > >> +      return 0;
> > >> +    }
> > >> +
> > >> +  symlink[filesize] = '\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 = 0; i < ctx->max;)
> > >> +    {
> > >> +      char *filename;
> > >> +      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
> > >> +      enum FILE_TYPE ftype;
> > >> +      int name_len;
> > >> +      int ret;
> > >> +
> > >> +      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
> > >> +        {
> > >> +          i++;
> > >> +          continue;
> > >> +        }
> > >> +
> > >> +      ftype = ctx->dentry[i].file_type;
> > >> +      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
> > >> +      filename = grub_malloc (name_len + 1);
> > >> +      if (!filename)
> > >> +        return 0;
> > >> +
> > >> +      grub_memcpy (filename, ctx->filename[i], name_len);
> > >> +      filename[name_len] = 0;
> > >> +
> > >> +      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
> > >> +      if (!fdiro)
> > >> +        {
> > >> +          grub_free(filename);
> > >> +          return 0;
> > >> +        }
> > >> +
> > >> +      if (ftype == F2FS_FT_DIR)
> > >> +        type = GRUB_FSHELP_DIR;
> > >> +      else if (ftype == F2FS_FT_SYMLINK)
> > >> +        type = GRUB_FSHELP_SYMLINK;
> > >> +      else if (ftype == F2FS_FT_REG_FILE)
> > >> +        type = GRUB_FSHELP_REG;
> > >> +
> > >> +      fdiro->data = ctx->data;
> > >> +      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
> > >> +      fdiro->inode_read = 0;
> > >> +
> > >> +      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
> > >> +      grub_free(filename);
> > >> +      if (ret)
> > >> +        return 1;
> > >> +
> > >> +      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
> > >> +
> > >> +  ctx->bitmap = de_blk->dentry_bitmap;
> > >> +  ctx->dentry = de_blk->dentry;
> > >> +  ctx->filename = de_blk->filename;
> > >> +  ctx->max = 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 = (struct grub_fshelp_node *) dir;
> > >> +  struct grub_f2fs_inode *inode;
> > >> +  struct grub_f2fs_dir_iter_ctx ctx = {
> > >> +    .data = diro->data,
> > >> +    .hook = hook,
> > >> +    .hook_data = hook_data
> > >> +  };
> > >> +  grub_off_t fpos = 0;
> > >> +
> > >> +  if (!diro->inode_read)
> > >> +    {
> > >> +      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
> > >> +      if (grub_errno)
> > >> +     return 0;
> > >> +    }
> > >> +
> > >> +  inode = &diro->inode.i;
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_DENTRY)
> > >> +    return grub_f2fs_iterate_inline_dir (inode, &ctx);
> > >> +
> > >> +  while (fpos < grub_f2fs_file_size (inode))
> > >> +    {
> > >> +      struct grub_f2fs_dentry_block *de_blk;
> > >> +      char *buf;
> > >> +      int ret;
> > >> +
> > >> +      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
> > >> +
> > >> +      ctx.bitmap = de_blk->dentry_bitmap;
> > >> +      ctx.dentry = de_blk->dentry;
> > >> +      ctx.filename = de_blk->filename;
> > >> +      ctx.max = NR_DENTRY_IN_BLOCK;
> > >> +
> > >> +      ret = grub_f2fs_check_dentries (&ctx);
> > >> +      grub_free (buf);
> > >> +      if (ret)
> > >> +        return 1;
> > >> +
> > >> +      fpos += F2FS_BLKSIZE;
> > >> +    }
> > >> +  return 0;
> > >> +}
> > >> +
> > >> +static int
> > >> +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype
> filetype,
> > >> +           grub_fshelp_node_t node, void *data)
> > >> +{
> > >> +  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
> > >> +      grub_errno = GRUB_ERR_NONE;
> > >> +    }
> > >> +  if (node->inode_read)
> > >> +    {
> > >> +      info.mtimeset = 1;
> > >> +      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
> > >> +    }
> > >> +
> > >> +  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
> > >> +  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 = {
> > >> +    .hook = hook,
> > >> +    .hook_data = hook_data
> > >> +  };
> > >> +  struct grub_fshelp_node *fdiro = 0;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  ctx.data = 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 != &ctx.data->diropen)
> > >> +    grub_free (fdiro);
> > >> + 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 = NULL;
> > >> +  struct grub_fshelp_node *fdiro = 0;
> > >> +  struct grub_f2fs_inode *inode;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  data = 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, sizeof (*data->inode));
> > >> +  grub_free (fdiro);
> > >> +
> > >> +  inode = &(data->inode->i);
> > >> +  file->size = grub_f2fs_file_size (inode);
> > >> +  file->data = data;
> > >> +  file->offset = 0;
> > >> +
> > >> +  if (inode->i_inline & F2FS_INLINE_DATA && file->size >
> MAX_INLINE_DATA)
> > >> +    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
> > >> +  return 0;
> > >> +
> > >> +fail:
> > >> +  if (fdiro != &data->diropen)
> > >> +    grub_free (fdiro);
> > >> +  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 = (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 = (struct grub_f2fs_data *) file->data;
> > >> +
> > >> +  grub_free (data);
> > >> +
> > >> +  grub_dl_unref (my_mod);
> > >> +
> > >> +  return GRUB_ERR_NONE;
> > >> +}
> > >> +
> > >> +static grub_uint8_t *
> > >> +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
> > >> +{
> > >> +  grub_uint16_t in_buf[MAX_VOLUME_NAME];
> > >> +  grub_uint8_t *out_buf;
> > >> +  int len = 0;
> > >> +
> > >> +  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 +
> 1);
> > >> +  if (!out_buf)
> > >> +    return NULL;
> > >> +
> > >> +  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
> > >> +    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
> > >> +    len++;
> > >> +  }
> > >> +
> > >> +  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
> > >> +  return out_buf;
> > >> +}
> > >> +
> > >> +static grub_err_t
> > >> +grub_f2fs_label (grub_device_t device, char **label)
> > >> +{
> > >> +  struct grub_f2fs_data *data;
> > >> +  grub_disk_t disk = device->disk;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  data = grub_f2fs_mount (disk);
> > >> +  if (data)
> > >> +    *label = (char *) grub_f2fs_utf16_to_utf8
> (data->sblock.volume_name);
> > >> +  else
> > >> +    *label = NULL;
> > >> +
> > >> +  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 = device->disk;
> > >> +
> > >> +  grub_dl_ref (my_mod);
> > >> +
> > >> +  data = grub_f2fs_mount (disk);
> > >> +  if (data)
> > >> +    {
> > >> +      *uuid =
> > >> +     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 = NULL;
> > >> +
> > >> +  grub_free (data);
> > >> +  grub_dl_unref (my_mod);
> > >> +  return grub_errno;
> > >> +}
> > >> +
> > >> +static struct grub_fs grub_f2fs_fs = {
> > >> +  .name = "f2fs",
> > >> +  .dir = grub_f2fs_dir,
> > >> +  .open = grub_f2fs_open,
> > >> +  .read = grub_f2fs_read,
> > >> +  .close = grub_f2fs_close,
> > >> +  .label = grub_f2fs_label,
> > >> +  .uuid = grub_f2fs_uuid,
> > >> +#ifdef GRUB_UTIL
> > >> +  .reserved_first_sector = 1,
> > >> +  .blocklist_install = 0,
> > >> +#endif
> > >> +  .next = 0
> > >> +};
> > >> +
> > >> +GRUB_MOD_INIT (f2fs)
> > >> +{
> > >> +  grub_fs_register (&grub_f2fs_fs);
> > >> +  my_mod = mod;
> > >> +}
> > >> +
> > >> +GRUB_MOD_FINI (f2fs)
> > >> +{
> > >> +  grub_fs_unregister (&grub_f2fs_fs);
> > >> +}
> > >> diff --git a/po/exclude.pot b/po/exclude.pot
> > >> index 0a9b215..816089c 100644
> > >> --- a/po/exclude.pot
> > >> +++ b/po/exclude.pot
> > >> @@ -1214,6 +1214,7 @@ msgstr ""
> > >>
> > >>  #: 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..1ea77c8
> > >> --- /dev/null
> > >> +++ b/tests/f2fs_test.in
> > >> @@ -0,0 +1,19 @@
> > >> +#!/bin/sh
> > >> +
> > >> +set -e
> > >> +
> > >> +if [ "x$EUID" = "x" ] ; then
> > >> + EUID=`id -u`
> > >> +fi
> > >> +
> > >> +if [ "$EUID" != 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 424de22..e3e4109 100644
> > >> --- a/tests/util/grub-fs-tester.in
> > >> +++ b/tests/util/grub-fs-tester.in
> > >> @@ -142,7 +142,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>       xsquash*)
> > >>           MINBLKSIZE=4096
> > >>           MAXBLKSIZE=1048576;;
> > >> -     xxfs)
> > >> +     xxfs|xf2fs)
> > >>           MINBLKSIZE=$SECSIZE
> > >>               # OS Limitation: GNU/Linux doesn't accept > 4096
> > >>           MAXBLKSIZE=4096;;
> > >> @@ -265,6 +265,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>               x"btrfs"*)
> > >>                   FSLABEL="grub_;/testé莭莽😁киритi
> urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvn
> irewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvr
> enurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoi
> roireoireoifrefoieroifoireoif";;
> > >>
> > >> +         # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
> > >> +             x"f2fs")
> > >> +                 FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruew
> nuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvn
> uewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire
> nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
> > >> +
> > >>           # FS LIMITATION: exfat is at most 15 UTF-16 chars
> > >>               x"exfat")
> > >>                   FSLABEL="géт ;/莭莽😁кир";;
> > >> @@ -474,7 +478,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>           # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS.
> Check it.
> > >>       # FS LIMITATION: as far as I know those FS don't store their
> last modification date.
> > >>               x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" |
> x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
> > >> -                 | 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=y;;
> > >> @@ -753,6 +757,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;
> LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
> > >>                   MOUNTDEVICE="/dev/mapper/grub_test-testvol"
> > >>                   MOUNTFS=ext2
> > >>                   "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)
> > >> --
> > >> 2.6.3
> > >>
> > >>
> > >> ------------------------------------------------------------
> ------------------
> > >> Site24x7 APM Insight: Get Deep Visibility into Application Performance
> > >> APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
> > >> Monitor end-to-end web transactions and take corrective actions now
> > >> Troubleshoot faster and improve end-user experience. Signup Now!
> > >> http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
> > >> _______________________________________________
> > >> Linux-f2fs-devel mailing list
> > >> Linux-f2fs-devel@lists.sourceforge.net
> > >> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 79702 bytes --]

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-04-04 20:37     ` Daniel Kiper
@ 2018-04-04 21:11       ` Pete Batard
  0 siblings, 0 replies; 77+ messages in thread
From: Pete Batard @ 2018-04-04 21:11 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: arvidjaar, jaegeuk, phcoder, grub-devel

On 2018.04.04 21:37, Daniel Kiper wrote:
> Thanks, LGTM +/- some nitpicks which I fix before committing.
> If there are no significant objections I will apply this patch
> in a week or so.

Thanks Daniel. Much appreciated.

Regards,

/Pete


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-31 20:47     ` Paul Menzel
  2018-04-01 19:16       ` Pete Batard
@ 2018-04-04 21:03       ` Daniel Kiper
  1 sibling, 0 replies; 77+ messages in thread
From: Daniel Kiper @ 2018-04-04 21:03 UTC (permalink / raw)
  To: paulepanter; +Cc: grub-devel

On Sat, Mar 31, 2018 at 10:47:29PM +0200, Paul Menzel wrote:
> Dear Pete,
>
> Am Donnerstag, den 29.03.2018, 17:08 +0100 schrieb Pete Batard:
> > Thanks for reviewing the patch.
> > Here's a v2 that takes your comments into account.
>
> Could you please make the commit message title a statement by adding a
> verb?
>
> > Add F2FS support

I will take that into account.

> Could you additionally add an example to the commit message, how to
> test the support with QEMU? That would help me, test your patch.

I am not sure what do you mean by that. Anyway, IMO this request
does not block patch itself. However, if you wish to provide an
example I am happy to take it.

Daniel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-29 16:08   ` Pete Batard
  2018-03-31 20:47     ` Paul Menzel
@ 2018-04-04 20:37     ` Daniel Kiper
  2018-04-04 21:11       ` Pete Batard
  1 sibling, 1 reply; 77+ messages in thread
From: Daniel Kiper @ 2018-04-04 20:37 UTC (permalink / raw)
  To: pete; +Cc: arvidjaar, jaegeuk, phcoder, grub-devel

On Thu, Mar 29, 2018 at 05:08:42PM +0100, Pete Batard wrote:
> Hi Daniel,
>
> Thanks for reviewing the patch.
> Here's a v2 that takes your comments into account.

Thanks, LGTM +/- some nitpicks which I fix before committing.
If there are no significant objections I will apply this patch
in a week or so.

Daniel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-31 20:47     ` Paul Menzel
@ 2018-04-01 19:16       ` Pete Batard
  2018-04-04 21:03       ` Daniel Kiper
  1 sibling, 0 replies; 77+ messages in thread
From: Pete Batard @ 2018-04-01 19:16 UTC (permalink / raw)
  To: grub-devel

Sorry Paul, but I'm going to have to decline both requests.

There's only so much time I am willing to sink in an effort to remove 
one .patch file from my downstream repository (which is literally the 
only reason I have been pushing for F2FS GRUB integration), and these 
extra requirements are a deal breaker for me.

If this result in this patch being dropped, so be it.

Regards,

/Pete

On 2018.03.31 21:47, Paul Menzel wrote:
> Dear Pete,
> 
> 
> Am Donnerstag, den 29.03.2018, 17:08 +0100 schrieb Pete Batard:
>> Thanks for reviewing the patch.
>> Here's a v2 that takes your comments into account.
> 
> Could you please make the commit message title a statement by adding a
> verb?
> 
>> Add F2FS support
> 
> Could you additionally add an example to the commit message, how to
> test the support with QEMU? That would help me, test your patch.
> 
> 
> Thanks,
> 
> Paul
> 
> 
> 
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
> 



^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-29 16:08   ` Pete Batard
@ 2018-03-31 20:47     ` Paul Menzel
  2018-04-01 19:16       ` Pete Batard
  2018-04-04 21:03       ` Daniel Kiper
  2018-04-04 20:37     ` Daniel Kiper
  1 sibling, 2 replies; 77+ messages in thread
From: Paul Menzel @ 2018-03-31 20:47 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 429 bytes --]

Dear Pete,


Am Donnerstag, den 29.03.2018, 17:08 +0100 schrieb Pete Batard:
> Thanks for reviewing the patch.
> Here's a v2 that takes your comments into account.

Could you please make the commit message title a statement by adding a
verb?

> Add F2FS support

Could you additionally add an example to the commit message, how to
test the support with QEMU? That would help me, test your patch.


Thanks,

Paul

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-28 12:04 ` Daniel Kiper
@ 2018-03-29 16:08   ` Pete Batard
  2018-03-31 20:47     ` Paul Menzel
  2018-04-04 20:37     ` Daniel Kiper
  0 siblings, 2 replies; 77+ messages in thread
From: Pete Batard @ 2018-03-29 16:08 UTC (permalink / raw)
  To: grub-devel; +Cc: arvidjaar, jaegeuk, phcoder

[-- Attachment #1: Type: text/plain, Size: 21702 bytes --]

Hi Daniel,

Thanks for reviewing the patch.
Here's a v2 that takes your comments into account.

Regards,

/Pete

On 2018.03.28 13:04, Daniel Kiper wrote:
> On Thu, Mar 22, 2018 at 04:47:47PM +0000, Pete Batard wrote:
>>  From 40030514e682191281e8ddba8d1e0835e6b685dc Mon Sep 17 00:00:00 2001
>> From: Jaegeuk Kim <jaegeuk@kernel.org>
>> Date: Thu, 4 May 2017 19:12:00 +0100
>> Subject: [PATCH] F2FS support
>>
>> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
>> into Linux kernel v3.8 in 2013.
>>
>> The motive for F2FS was to build a file system that from the start, takes into
>> account the characteristics of NAND flash memory-based storage devices (such as
>> solid-state disks, eMMC, and SD cards).
>>
>> F2FS was designed on a basis of a log-structured file system approach, which
>> remedies some known issues of the older log structured file systems, such as
>> the snowball effect of wandering trees and high cleaning overhead. In addition,
>> since a NAND-based storage device shows different characteristics according to
>> its internal geometry or flash memory management scheme (such as the Flash
>> Translation Layer or FTL), it supports various parameters not only for
>> configuring on-disk layout, but also for selecting allocation and cleaning
>> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
>>
>> The source codes for F2FS are available from:
>>
>> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
>> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
>>
>> Update:
>>   - This patch has been integrated in OpenMandriva Lx 3.
>>     https://www.openmandriva.org/
>>
>> Acked-by: Andrei Borzenkov <arvidjaar@gmail.com>
> 
> Please drop this Acked-by. I will ask you to do some changes, mostly
> nitpicks, and this means that it is no longer valid.
> 
>> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> 
> Lack of your SOB.
> 
> [...]
> 
>> diff --git a/docs/grub.texi b/docs/grub.texi
>> index 65b4bbe..5afdc5a 100644
>> --- a/docs/grub.texi
>> +++ b/docs/grub.texi
>> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
>> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
>> +@dfn{f2fs}, @dfn{HFS},
>>   @dfn{HFS+}, @dfn{ISO9660} (including Joliet, Rock-ridge and multi-chunk files),
> 
> I would like to see this in one line:
> 
> @dfn{exFAT}, @dfn{f2fs}, @dfn{HFS}, @dfn{HFS+},
> 
> Hmmm... s/f2fs/F2FS/?
> 
>>   @dfn{JFS}, @dfn{Minix fs} (versions 1, 2 and 3), @dfn{nilfs2},
>>   @dfn{NTFS} (including compression), @dfn{ReiserFS}, @dfn{ROMFS},
>> @@ -5375,7 +5376,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, minix,
>>   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
> 
> s/f2fs/F2FS/?
> 
>>   to be UTF-8. This might be false on systems configured with legacy charset
>>   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 2c1d62c..fc4767f 100644
>> --- a/grub-core/Makefile.core.def
>> +++ b/grub-core/Makefile.core.def
>> @@ -1315,6 +1315,11 @@ module = {
>>   };
>>
>>   module = {
>> +  name = f2fs;
>> +  common = fs/f2fs.c;
>> +};
>> +
>> +module = {
>>     name = fshelp;
>>     common = fs/fshelp.c;
>>   };
>> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
>> new file mode 100644
>> index 0000000..7fb256f
>> --- /dev/null
>> +++ b/grub-core/fs/f2fs.c
>> @@ -0,0 +1,1289 @@
>> +/*
>> + *  f2fs.c - Flash-Friendly File System
>> + *
>> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
>> + *
>> + *  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 <http://www.gnu.org/licenses/>.
>> + */
> 
> Lack of empty line.
> 
>> +#include <grub/err.h>
>> +#include <grub/file.h>
>> +#include <grub/mm.h>
>> +#include <grub/misc.h>
>> +#include <grub/disk.h>
>> +#include <grub/dl.h>
>> +#include <grub/types.h>
>> +#include <grub/charset.h>
>> +#include <grub/fshelp.h>
>> +
>> +GRUB_MOD_LICENSE ("GPLv3+");
>> +
>> +/* F2FS Magic Number */
>> +#define F2FS_SUPER_MAGIC	0xF2F52010
>> +#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
>> +#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
>> +
>> +/* byte-size offset */
>> +#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
>> +#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
>> +#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
> 
> Redundant space before "\".
> 
>> +						GRUB_DISK_SECTOR_BITS)
>> +
>> +/* 9 bits for 512 bytes */
>> +#define F2FS_MIN_LOG_SECTOR_SIZE	9
>> +
>> +/* support only 4KB block */
>> +#define F2FS_BLK_BITS	12
>> +#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
> 
> Could you align the values above to the values at line below?
> 
>> +#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
>> +
>> +#define VERSION_LEN	256
> 
> Ditto.
> 
>> +#define F2FS_MAX_EXTENSION	64
>> +
>> +#define CP_COMPACT_SUM_FLAG	0x00000004
>> +#define CP_UMOUNT_FLAG	0x00000001
> 
> Ditto.
> 
>> +
>> +#define MAX_ACTIVE_LOGS	16
> 
> Ditto.
> 
>> +#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)
> 
> Ditto.
> 
>> +
>> +#define ENTRIES_IN_SUM	512
>> +#define	SUMMARY_SIZE	7
>> +#define	SUM_FOOTER_SIZE	5
>> +#define JENTRY_SIZE	(sizeof(struct grub_f2fs_nat_jent))
> 
> Same for 4 lines above.
> 
>> +#define SUM_ENTRIES_SIZE	(SUMMARY_SIZE * ENTRIES_IN_SUM)
>> +#define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
> 
> Please add space after "-" before "\".
> 
>> +				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 (sizeof(struct grub_f2fs_nat_entry))
>> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
> 
> Lack of alignment for two lines above.
> 
>> +#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
> 
> More unaligned lines.
> 
>> +#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)
> 
> Same as above...
> 
>> +#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
> 
> Unreadable mess, please fix this...
> 
>> +#define CKPT_FLAG_SET(ckpt, f)	\
>> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
>> +
>> +#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
>> +#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
>> +#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
>> +#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
>> +#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
>> +
>> +enum FILE_TYPE
>> +{
>> +  F2FS_FT_UNKNOWN,
>> +  F2FS_FT_REG_FILE = 1,
>> +  F2FS_FT_DIR = 2,
>> +  F2FS_FT_SYMLINK = 7,
> 
> Could you do this?
> 
>    F2FS_FT_REG_FILE = 1,
>    F2FS_FT_DIR      = 2,
>    F2FS_FT_SYMLINK  = 7,
> 
>> +};
>> +
>> +#define MAX_VOLUME_NAME		512
> 
> Please put this together with constants definitions.
> 
>> +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[MAX_VOLUME_NAME];
>> +  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];
> 
> Could you align all member names in one column?
> Please use spaces here, e.g. dummy2[] requires 2 spaces.
> 
>> +} 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];
> 
> Ditto.
> 
>> +  grub_uint32_t checksum;
>> +} GRUB_PACKED;
>> +
>> +struct grub_f2fs_nat_entry {
>> +  grub_uint8_t version;
> 
> Same as above.
> 
>> +  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;
> 
> Ditto. Hmmm... I am not sure about this one...
> If there are structs in struct then leave them as is.
> 
>> +} 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;
> 
> However, align this please.
> 
>> +} 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];
> 
> Ditto.
> 
>> +} 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;
>> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
> 
> Could you move the comment above this line?
> 
>> +  };
>> +  grub_uint8_t dummy[40];
>> +} GRUB_PACKED;
> 
> [...]
> 
>> +static inline int
> 
> Do we really need to enforce inlining here and below? I think that
> compiler should do work for us.
> 
>> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
>> +{
>> +  return addr[nr >> 3] & (1 << (nr & 7));
>> +}
>> +
>> +static inline char *
>> +__inline_addr (struct grub_f2fs_inode *inode)
>> +{
>> +  return (char *)&inode->i_addr[1];
>> +}
>> +
>> +static inline grub_uint64_t
>> +grub_f2fs_file_size (struct grub_f2fs_inode *inode)
>> +{
>> +  return grub_le_to_cpu64 (inode->i_size);
>> +}
>> +
>> +static inline grub_uint32_t
> 
> I am not sure about this one... Could you double check it?
> 
>> +__start_cp_addr (struct grub_f2fs_data *data)
>> +{
>> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
>> +  grub_uint32_t start_addr = data->cp_blkaddr;
>> +
>> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
>> +
>> +  return __start_cp_addr (data) +
>> +	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
>> +	- (base + 1) + type;
>> +}
>> +
>> +static inline void *
>> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
>> +{
>> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
>> +  grub_uint32_t offset;
>> +
>> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
>> +    return ckpt->sit_nat_version_bitmap;
>> +
>> +  offset = 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 inode_block)
>> +{
>> +  if (inode_block)
>> +    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,
>> +		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
>> +		0, F2FS_BLKSIZE, buf);
>> +}
>> +
>> +/*
>> + * CRC32
>> +*/
>> +#define CRCPOLY_LE 0xedb88320
> 
> Please move this to constants definitions.
> 
> [...]
> 
>> +static void *
>> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
>> +	grub_uint64_t *version)
>> +{
>> +  grub_uint32_t *cp_page_1, *cp_page_2;
>> +  struct grub_f2fs_checkpoint *cp_block;
>> +  grub_uint64_t cur_version = 0, pre_version = 0;
>> +  grub_uint32_t crc = 0;
>> +  grub_uint32_t crc_offset;
>> +  grub_err_t err;
>> +
>> +  /* Read the 1st cp block in this CP pack */
>> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
>> +  if (!cp_page_1)
>> +    return NULL;
>> +
>> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
>> +  if (err)
>> +    goto invalid_cp1;
>> +
>> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
>> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
>> +  if (crc_offset != CHECKSUM_OFFSET)
>> +    goto invalid_cp1;
>> +
>> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
>> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
>> +    goto invalid_cp1;
>> +
>> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
>> +
>> +  /* Read the 2nd cp block in this CP pack */
>> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
>> +  if (!cp_page_2)
>> +    goto invalid_cp1;
>> +
>> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
>> +
>> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
>> +  if (err)
>> +    goto invalid_cp2;
>> +
>> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
>> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
>> +  if (crc_offset != CHECKSUM_OFFSET)
>> +    goto invalid_cp2;
>> +
>> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
>> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
>> +    goto invalid_cp2;
>> +
>> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
>> +  if (cur_version == pre_version)
>> +    {
>> +      *version = cur_version;
>> +      grub_free (cp_page_2);
>> +      return cp_page_1;
>> +    }
>> +
>> +invalid_cp2:
> 
> Please put one space before each label...
> 
>> +  grub_free (cp_page_2);
> 
> ...and empty line before each label.
> 
>> +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 = 0, cp2_version = 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 = data->cp_blkaddr;
>> +  cp1 = 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 += data->blocks_per_seg;
>> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
>> +  if (!cp2 && grub_errno)
>> +    {
>> +      grub_free (cp1);
>> +      return grub_errno;
>> +    }
>> +
>> +  if (cp1 && cp2)
>> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
>> +  else if (cp1)
>> +    cur_page = cp1;
>> +  else if (cp2)
>> +    cur_page = cp2;
>> +  else
>> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
>> +
>> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
>> +
>> +  grub_free (cp1);
>> +  grub_free (cp2);
>> +  return 0;
>> +}
>> +
>> +static grub_err_t
>> +get_nat_journal (struct grub_f2fs_data *data)
>> +{
>> +  grub_uint32_t block;
>> +  char *buf;
>> +  grub_err_t err;
>> +
>> +  buf = grub_malloc (F2FS_BLKSIZE);
>> +  if (!buf)
>> +    return grub_errno;
>> +
>> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
>> +    block = __start_sum_block (data);
>> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
>> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
>> +  else
>> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
>> +
>> +  err = 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:
> 
> Ditto and below...
> 
>> +  grub_free (buf);
>> +  return err;
>> +}
> 
> Daniel
> 


[-- Attachment #2: [PATCH v2] F2FS support.patch --]
[-- Type: text/plain, Size: 45138 bytes --]

From c345dae6232b06f87dae599df313fa867aad3256 Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Thu, 29 Mar 2018 16:37:39 +0100
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Update:
 - This patch has been integrated in OpenMandriva Lx 3.
   https://www.openmandriva.org/

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Pete Batard <pete@akeo.ie>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    7 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1297 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1341 insertions(+), 5 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index f9caccb..3180ac8 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -776,6 +777,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 65b4bbe..0f2ab91 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,8 +360,9 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
-@dfn{HFS+}, @dfn{ISO9660} (including Joliet, Rock-ridge and multi-chunk files),
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32},
+@dfn{exFAT}, @dfn{F2FS}, @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},
 @dfn{Amiga Smart FileSystem (SFS)}, @dfn{Squash4}, @dfn{tar}, @dfn{UDF},
@@ -5375,7 +5376,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, minix,
 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 charset
 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 2c1d62c..fc4767f 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1315,6 +1315,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..4ee0257
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1297 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC          0xF2F52010
+#define CHECKSUM_OFFSET           4092  /* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET       (CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET         ((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0        (F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1        ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >> \
+                                  GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE  9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS             12
+#define F2FS_BLKSIZE              (1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS         (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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               (sizeof(struct grub_f2fs_nat_jent))
+#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            (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK       (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)    (ckpt)->ckpt_flags & \
+                                  grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR         0x01  /* file inline xattr flag */
+#define F2FS_INLINE_DATA          0x02  /* file inline data flag */
+#define F2FS_INLINE_DENTRY        0x04  /* file inline dentry flag */
+#define F2FS_DATA_EXIST           0x08  /* file inline data exist flag */
+#define F2FS_INLINE_DOTS          0x10  /* file having implicit dot dentries */
+
+#define MAX_VOLUME_NAME           512
+
+#define CRCPOLY_LE                0xedb88320
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE                = 1,
+  F2FS_FT_DIR                     = 2,
+  F2FS_FT_SYMLINK                 = 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[MAX_VOLUME_NAME];
+  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;
+    /* Should occupy F2FS_BLKSIZE totally */
+    char                          buf[F2FS_BLKSIZE - 40];
+  };
+  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_uint8_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 int
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static grub_uint64_t
+grub_f2fs_file_size (struct grub_f2fs_inode *inode)
+{
+  return grub_le_to_cpu64 (inode->i_size);
+}
+
+static grub_uint32_t
+__start_cp_addr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(1)))
+    return start_addr + data->blocks_per_seg;
+  return start_addr;
+}
+
+static grub_uint32_t
+__start_sum_block (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+
+  return __start_cp_addr (data) + grub_le_to_cpu32 (ckpt->cp_pack_start_sum);
+}
+
+static grub_uint32_t
+__sum_blk_addr (struct grub_f2fs_data *data, int base, int type)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+
+  return __start_cp_addr (data) +
+           grub_le_to_cpu32 (ckpt->cp_pack_total_block_count) -
+           (base + 1) + type;
+}
+
+static void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = grub_le_to_cpu32 (ckpt->sit_ver_bitmap_bytesize);
+  return ckpt->sit_nat_version_bitmap + offset;
+}
+
+static grub_uint32_t
+__get_node_id (struct grub_f2fs_node *rn, int off, int inode_block)
+{
+  if (inode_block)
+    return grub_le_to_cpu32 (rn->i.i_nid[off - NODE_DIR1_BLOCK]);
+  return grub_le_to_cpu32 (rn->in.nid[off]);
+}
+
+static grub_err_t
+grub_f2fs_block_read (struct grub_f2fs_data *data, grub_uint32_t blkaddr,
+                      void *buf)
+{
+  return grub_disk_read (data->disk,
+                         ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+                         0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+ */
+static grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+                     grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+      offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+
+ got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+                        "not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+    grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+ fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = filesize - pos;
+
+      grub_memcpy (buf, 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+                    grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+ fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+        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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 15969d7..26bd57a 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -172,7 +172,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -299,6 +299,10 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoi";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -508,7 +512,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -792,6 +796,8 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "mkfs.ext2" -L "$FSLABEL" -q "${MOUNTDEVICE}"  ;;
+		xf2fs)
+		    "mkfs.f2fs" -l "$FSLABEL" -q "${LODEVICES[0]}" ;;
 		xnilfs2)
 		    "mkfs.nilfs2" -L "$FSLABEL" -b $BLKSIZE  -q "${MOUNTDEVICE}" ;;
 		xext2_old)
-- 
2.9.3.windows.2


^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-22 16:47 Pete Batard
@ 2018-03-28 12:04 ` Daniel Kiper
  2018-03-29 16:08   ` Pete Batard
  0 siblings, 1 reply; 77+ messages in thread
From: Daniel Kiper @ 2018-03-28 12:04 UTC (permalink / raw)
  To: pete; +Cc: arvidjaar, jaegeuk, phcoder, grub-devel

On Thu, Mar 22, 2018 at 04:47:47PM +0000, Pete Batard wrote:
> From 40030514e682191281e8ddba8d1e0835e6b685dc Mon Sep 17 00:00:00 2001
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> Date: Thu, 4 May 2017 19:12:00 +0100
> Subject: [PATCH] F2FS support
>
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
>
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).
>
> F2FS was designed on a basis of a log-structured file system approach, which
> remedies some known issues of the older log structured file systems, such as
> the snowball effect of wandering trees and high cleaning overhead. In addition,
> since a NAND-based storage device shows different characteristics according to
> its internal geometry or flash memory management scheme (such as the Flash
> Translation Layer or FTL), it supports various parameters not only for
> configuring on-disk layout, but also for selecting allocation and cleaning
> algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.
>
> The source codes for F2FS are available from:
>
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
> http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git
>
> Update:
>  - This patch has been integrated in OpenMandriva Lx 3.
>    https://www.openmandriva.org/
>
> Acked-by: Andrei Borzenkov <arvidjaar@gmail.com>

Please drop this Acked-by. I will ask you to do some changes, mostly
nitpicks, and this means that it is no longer valid.

> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>

Lack of your SOB.

[...]

> diff --git a/docs/grub.texi b/docs/grub.texi
> index 65b4bbe..5afdc5a 100644
> --- a/docs/grub.texi
> +++ b/docs/grub.texi
> @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
> +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
> +@dfn{f2fs}, @dfn{HFS},
>  @dfn{HFS+}, @dfn{ISO9660} (including Joliet, Rock-ridge and multi-chunk files),

I would like to see this in one line:

@dfn{exFAT}, @dfn{f2fs}, @dfn{HFS}, @dfn{HFS+},

Hmmm... s/f2fs/F2FS/?

>  @dfn{JFS}, @dfn{Minix fs} (versions 1, 2 and 3), @dfn{nilfs2},
>  @dfn{NTFS} (including compression), @dfn{ReiserFS}, @dfn{ROMFS},
> @@ -5375,7 +5376,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, minix,
>  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

s/f2fs/F2FS/?

>  to be UTF-8. This might be false on systems configured with legacy charset
>  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 2c1d62c..fc4767f 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -1315,6 +1315,11 @@ module = {
>  };
>
>  module = {
> +  name = f2fs;
> +  common = fs/f2fs.c;
> +};
> +
> +module = {
>    name = fshelp;
>    common = fs/fshelp.c;
>  };
> diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
> new file mode 100644
> index 0000000..7fb256f
> --- /dev/null
> +++ b/grub-core/fs/f2fs.c
> @@ -0,0 +1,1289 @@
> +/*
> + *  f2fs.c - Flash-Friendly File System
> + *
> + *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
> + *
> + *  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 <http://www.gnu.org/licenses/>.
> + */

Lack of empty line.

> +#include <grub/err.h>
> +#include <grub/file.h>
> +#include <grub/mm.h>
> +#include <grub/misc.h>
> +#include <grub/disk.h>
> +#include <grub/dl.h>
> +#include <grub/types.h>
> +#include <grub/charset.h>
> +#include <grub/fshelp.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +/* F2FS Magic Number */
> +#define F2FS_SUPER_MAGIC	0xF2F52010
> +#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
> +#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
> +
> +/* byte-size offset */
> +#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
> +#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
> +#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\

Redundant space before "\".

> +						GRUB_DISK_SECTOR_BITS)
> +
> +/* 9 bits for 512 bytes */
> +#define F2FS_MIN_LOG_SECTOR_SIZE	9
> +
> +/* support only 4KB block */
> +#define F2FS_BLK_BITS	12
> +#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)

Could you align the values above to the values at line below?

> +#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
> +
> +#define VERSION_LEN	256

Ditto.

> +#define F2FS_MAX_EXTENSION	64
> +
> +#define CP_COMPACT_SUM_FLAG	0x00000004
> +#define CP_UMOUNT_FLAG	0x00000001

Ditto.

> +
> +#define MAX_ACTIVE_LOGS	16

Ditto.

> +#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)

Ditto.

> +
> +#define ENTRIES_IN_SUM	512
> +#define	SUMMARY_SIZE	7
> +#define	SUM_FOOTER_SIZE	5
> +#define JENTRY_SIZE	(sizeof(struct grub_f2fs_nat_jent))

Same for 4 lines above.

> +#define SUM_ENTRIES_SIZE	(SUMMARY_SIZE * ENTRIES_IN_SUM)
> +#define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\

Please add space after "-" before "\".

> +				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 (sizeof(struct grub_f2fs_nat_entry))
> +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)

Lack of alignment for two lines above.

> +#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

More unaligned lines.

> +#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)

Same as above...

> +#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

Unreadable mess, please fix this...

> +#define CKPT_FLAG_SET(ckpt, f)	\
> +		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
> +
> +#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
> +#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
> +#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
> +#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
> +#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
> +
> +enum FILE_TYPE
> +{
> +  F2FS_FT_UNKNOWN,
> +  F2FS_FT_REG_FILE = 1,
> +  F2FS_FT_DIR = 2,
> +  F2FS_FT_SYMLINK = 7,

Could you do this?

  F2FS_FT_REG_FILE = 1,
  F2FS_FT_DIR      = 2,
  F2FS_FT_SYMLINK  = 7,

> +};
> +
> +#define MAX_VOLUME_NAME		512

Please put this together with constants definitions.

> +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[MAX_VOLUME_NAME];
> +  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];

Could you align all member names in one column?
Please use spaces here, e.g. dummy2[] requires 2 spaces.

> +} 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];

Ditto.

> +  grub_uint32_t checksum;
> +} GRUB_PACKED;
> +
> +struct grub_f2fs_nat_entry {
> +  grub_uint8_t version;

Same as above.

> +  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;

Ditto. Hmmm... I am not sure about this one...
If there are structs in struct then leave them as is.

> +} 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;

However, align this please.

> +} 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];

Ditto.

> +} 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;
> +    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */

Could you move the comment above this line?

> +  };
> +  grub_uint8_t dummy[40];
> +} GRUB_PACKED;

[...]

> +static inline int

Do we really need to enforce inlining here and below? I think that
compiler should do work for us.

> +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
> +{
> +  return addr[nr >> 3] & (1 << (nr & 7));
> +}
> +
> +static inline char *
> +__inline_addr (struct grub_f2fs_inode *inode)
> +{
> +  return (char *)&inode->i_addr[1];
> +}
> +
> +static inline grub_uint64_t
> +grub_f2fs_file_size (struct grub_f2fs_inode *inode)
> +{
> +  return grub_le_to_cpu64 (inode->i_size);
> +}
> +
> +static inline grub_uint32_t

I am not sure about this one... Could you double check it?

> +__start_cp_addr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t start_addr = data->cp_blkaddr;
> +
> +  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
> +
> +  return __start_cp_addr (data) +
> +	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
> +	- (base + 1) + type;
> +}
> +
> +static inline void *
> +__nat_bitmap_ptr (struct grub_f2fs_data *data)
> +{
> +  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
> +  grub_uint32_t offset;
> +
> +  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
> +    return ckpt->sit_nat_version_bitmap;
> +
> +  offset = 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 inode_block)
> +{
> +  if (inode_block)
> +    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,
> +		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
> +		0, F2FS_BLKSIZE, buf);
> +}
> +
> +/*
> + * CRC32
> +*/
> +#define CRCPOLY_LE 0xedb88320

Please move this to constants definitions.

[...]

> +static void *
> +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
> +	grub_uint64_t *version)
> +{
> +  grub_uint32_t *cp_page_1, *cp_page_2;
> +  struct grub_f2fs_checkpoint *cp_block;
> +  grub_uint64_t cur_version = 0, pre_version = 0;
> +  grub_uint32_t crc = 0;
> +  grub_uint32_t crc_offset;
> +  grub_err_t err;
> +
> +  /* Read the 1st cp block in this CP pack */
> +  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_1)
> +    return NULL;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
> +  if (err)
> +    goto invalid_cp1;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp1;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp1;
> +
> +  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +
> +  /* Read the 2nd cp block in this CP pack */
> +  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
> +  if (!cp_page_2)
> +    goto invalid_cp1;
> +
> +  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
> +
> +  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
> +  if (err)
> +    goto invalid_cp2;
> +
> +  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
> +  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
> +  if (crc_offset != CHECKSUM_OFFSET)
> +    goto invalid_cp2;
> +
> +  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
> +  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
> +    goto invalid_cp2;
> +
> +  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
> +  if (cur_version == pre_version)
> +    {
> +      *version = cur_version;
> +      grub_free (cp_page_2);
> +      return cp_page_1;
> +    }
> +
> +invalid_cp2:

Please put one space before each label...

> +  grub_free (cp_page_2);

...and empty line before each label.

> +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 = 0, cp2_version = 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 = data->cp_blkaddr;
> +  cp1 = 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 += data->blocks_per_seg;
> +  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
> +  if (!cp2 && grub_errno)
> +    {
> +      grub_free (cp1);
> +      return grub_errno;
> +    }
> +
> +  if (cp1 && cp2)
> +    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
> +  else if (cp1)
> +    cur_page = cp1;
> +  else if (cp2)
> +    cur_page = cp2;
> +  else
> +    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
> +
> +  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
> +
> +  grub_free (cp1);
> +  grub_free (cp2);
> +  return 0;
> +}
> +
> +static grub_err_t
> +get_nat_journal (struct grub_f2fs_data *data)
> +{
> +  grub_uint32_t block;
> +  char *buf;
> +  grub_err_t err;
> +
> +  buf = grub_malloc (F2FS_BLKSIZE);
> +  if (!buf)
> +    return grub_errno;
> +
> +  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
> +    block = __start_sum_block (data);
> +  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
> +    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
> +  else
> +    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
> +
> +  err = 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:

Ditto and below...

> +  grub_free (buf);
> +  return err;
> +}

Daniel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* [PATCH] F2FS support
@ 2018-03-22 16:47 Pete Batard
  2018-03-28 12:04 ` Daniel Kiper
  0 siblings, 1 reply; 77+ messages in thread
From: Pete Batard @ 2018-03-22 16:47 UTC (permalink / raw)
  To: grub-devel; +Cc: jaegeuk, arvidjaar

[-- Attachment #1: Type: text/plain, Size: 1758 bytes --]

Hi,

Please find attached a rebased version of the F2FS patch sent by Jaegeuk 
Kim to this mailing list on 2017.05.04.

The original patch was rebased against the latest mainline and we also 
validated that there were no compilation issues on a Debian 9 x64 platform.

Here are the notes from the original:
----------------------------------------------------------------------
"F2FS (Flash-Friendly File System) is flash-friendly file system which 
was merged into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, 
takes into account the characteristics of NAND flash memory-based 
storage devices (such as solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, 
which remedies some known issues of the older log structured file 
systems, such as the snowball effect of wandering trees and high 
cleaning overhead. In addition, since a NAND-based storage device shows 
different characteristics according to its internal geometry or flash 
memory management scheme (such as the Flash Translation Layer or FTL), 
it supports various parameters not only for configuring on-disk layout, 
but also for selecting allocation and cleaning algorithm.", quote by 
https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Update:
  - This patch has been integrated in OpenMandriva Lx 3.
    https://www.openmandriva.org/

Acked-by: Andrei Borzenkov <arvidjaar@gmail.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
----------------------------------------------------------------------

Regards,

/Pete

[-- Attachment #2: F2FS-support.patch --]
[-- Type: text/plain, Size: 41340 bytes --]

From 40030514e682191281e8ddba8d1e0835e6b685dc Mon Sep 17 00:00:00 2001
From: Jaegeuk Kim <jaegeuk@kernel.org>
Date: Thu, 4 May 2017 19:12:00 +0100
Subject: [PATCH] F2FS support

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Update:
 - This patch has been integrated in OpenMandriva Lx 3.
   https://www.openmandriva.org/

Acked-by: Andrei Borzenkov <arvidjaar@gmail.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1332 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index f9caccb..3180ac8 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -776,6 +777,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index 65b4bbe..5afdc5a 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5375,7 +5376,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, minix,
 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 charset
 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 2c1d62c..fc4767f 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1315,6 +1315,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 0000000..7fb256f
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1289 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = filesize - pos;
+
+      grub_memcpy (buf, 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215..816089c 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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..1ea77c8
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 15969d7..26bd57a 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -172,7 +172,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -299,6 +299,10 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoi";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -508,7 +512,7 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -792,6 +796,8 @@ for LOGSECSIZE in $(range "$MINLOGSECSIZE" "$MAXLOGSECSIZE" 1); do
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "mkfs.ext2" -L "$FSLABEL" -q "${MOUNTDEVICE}"  ;;
+		xf2fs)
+		    "mkfs.f2fs" -l "$FSLABEL" -q "${LODEVICES[0]}" ;;
 		xnilfs2)
 		    "mkfs.nilfs2" -L "$FSLABEL" -b $BLKSIZE  -q "${MOUNTDEVICE}" ;;
 		xext2_old)
-- 
2.9.3.windows.2


^ permalink raw reply related	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-18 20:39 ` Pete Batard
@ 2018-03-22 14:26   ` Daniel Kiper
  0 siblings, 0 replies; 77+ messages in thread
From: Daniel Kiper @ 2018-03-22 14:26 UTC (permalink / raw)
  To: pete, buo.ren.lin; +Cc: grub-devel

On Sun, Mar 18, 2018 at 08:39:45PM +0000, Pete Batard wrote:
> +1 for F2FS integration, which I also requested a few months back and
> which I know has also been requested by other people before that.
>
> While I understand that the GRUB project's priorities might be
> elsewhere, I'm just going to point out that I am growing a bit tired of
> having had to keep and maintain an additional patch for F2FS in my GRUB
> derived UEFI file system driver project
> (https://github.com/pbatard/efifs), for the past few years now, so I
> really hope F2FS support can be integrated soon.

If somebody is willing to rebase and repost the patches then I am
happy to review them.

Daniel


^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2018-03-17  9:08 林博仁
@ 2018-03-18 20:39 ` Pete Batard
  2018-03-22 14:26   ` Daniel Kiper
  0 siblings, 1 reply; 77+ messages in thread
From: Pete Batard @ 2018-03-18 20:39 UTC (permalink / raw)
  To: grub-devel

+1 for F2FS integration, which I also requested a few months back and 
which I know has also been requested by other people before that.

While I understand that the GRUB project's priorities might be 
elsewhere, I'm just going to point out that I am growing a bit tired of 
having had to keep and maintain an additional patch for F2FS in my GRUB 
derived UEFI file system driver project 
(https://github.com/pbatard/efifs), for the past few years now, so I 
really hope F2FS support can be integrated soon.

Thank you,

/Pete

On 2018.03.17 09:08, 林博仁 wrote:
>> "F2FS (Flash-Friendly File System) is flash-friendly file system which was
>> merged into Linux kernel v3.8 in 2013.
>> <stripped>
> 
> Any update on this patch?  F2FS really help much on my system's performance on
> a dirt-cheap flash storage key.
> 
> 林博仁(Buo-ren, Lin)
> Buo.Ren.Lin@gmail.com
> 
> 
> 
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
> 



^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
@ 2018-03-17  9:08 林博仁
  2018-03-18 20:39 ` Pete Batard
  0 siblings, 1 reply; 77+ messages in thread
From: 林博仁 @ 2018-03-17  9:08 UTC (permalink / raw)
  To: grub-devel

> "F2FS (Flash-Friendly File System) is flash-friendly file system which was 
> merged into Linux kernel v3.8 in 2013.
> <stripped>

Any update on this patch?  F2FS really help much on my system's performance on 
a dirt-cheap flash storage key.

林博仁(Buo-ren, Lin)
Buo.Ren.Lin@gmail.com




^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
  2017-05-04 18:12 ` Jaegeuk Kim
@ 2017-05-04 20:52   ` Adam Borowski
  -1 siblings, 0 replies; 77+ messages in thread
From: Adam Borowski @ 2017-05-04 20:52 UTC (permalink / raw)
  To: grub-devel; +Cc: linux-f2fs-devel

On Thu, May 04, 2017 at 11:12:40AM -0700, Jaegeuk Kim wrote:
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).

Some reason "why?" would be nice.

Here's a rough comparison, class 4 SD card on a Pine64, median of 5:
* "git reset --hard" in a big empty tree:
  btrfs 3m45s, f2fs 4m, ext4 12m, xfs 16-18m (huge variance)
* "./configure && make -j4 && make test" (highly CPU-bound)
  f2fs 95s, btrfs 97s, xfs 120s, ext4 122s
* linear write of a single big file
  no meaningful differences

Ie, there's a drastic gain for using f2fs or btrfs.  But btrfs is... well,
btrfs.  It has both significant data safety features and "WTF" level caveats
that can result in abysmal performance, unexpected lack of space or even
data loss when handled by a naive user in ways that are perfectly safe for
most other filesystems.  Thus, it'd be irresponsible to unleash btrfs onto
an unprepared user.  Which leaves f2fs which works well with "install and
forget".

And there's a bunch of new machines that boot from SD/eMMC even on x86.


Meow!
-- 
Don't be racist.  White, amber or black, all beers should be judged based
solely on their merits.  Heck, even if occasionally a cider applies for a
beer's job, why not?
On the other hand, corpo lager is not a race.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply	[flat|nested] 77+ messages in thread

* Re: [PATCH] F2FS support
@ 2017-05-04 20:52   ` Adam Borowski
  0 siblings, 0 replies; 77+ messages in thread
From: Adam Borowski @ 2017-05-04 20:52 UTC (permalink / raw)
  To: grub-devel; +Cc: linux-f2fs-devel

On Thu, May 04, 2017 at 11:12:40AM -0700, Jaegeuk Kim wrote:
> "F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
> into Linux kernel v3.8 in 2013.
> 
> The motive for F2FS was to build a file system that from the start, takes into
> account the characteristics of NAND flash memory-based storage devices (such as
> solid-state disks, eMMC, and SD cards).

Some reason "why?" would be nice.

Here's a rough comparison, class 4 SD card on a Pine64, median of 5:
* "git reset --hard" in a big empty tree:
  btrfs 3m45s, f2fs 4m, ext4 12m, xfs 16-18m (huge variance)
* "./configure && make -j4 && make test" (highly CPU-bound)
  f2fs 95s, btrfs 97s, xfs 120s, ext4 122s
* linear write of a single big file
  no meaningful differences

Ie, there's a drastic gain for using f2fs or btrfs.  But btrfs is... well,
btrfs.  It has both significant data safety features and "WTF" level caveats
that can result in abysmal performance, unexpected lack of space or even
data loss when handled by a naive user in ways that are perfectly safe for
most other filesystems.  Thus, it'd be irresponsible to unleash btrfs onto
an unprepared user.  Which leaves f2fs which works well with "install and
forget".

And there's a bunch of new machines that boot from SD/eMMC even on x86.


Meow!
-- 
Don't be racist.  White, amber or black, all beers should be judged based
solely on their merits.  Heck, even if occasionally a cider applies for a
beer's job, why not?
On the other hand, corpo lager is not a race.


^ permalink raw reply	[flat|nested] 77+ messages in thread

* [PATCH] F2FS support
@ 2017-05-04 18:12 ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2017-05-04 18:12 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel; +Cc: Jaegeuk Kim

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Update:
 - This patch has been integrated in OpenMandriva Lx 3.
   https://www.openmandriva.org/

Acked-by: Andrei Borzenkov <arvidjaar@gmail.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1332 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index f9caccb97..3180ac880 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -776,6 +777,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index e935af33e..7137d6d0d 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5368,7 +5369,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, minix,
 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 charset
 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 2dfa22a92..7f084d466 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1291,6 +1291,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 000000000..7fb256f20
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1289 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = filesize - pos;
+
+      grub_memcpy (buf, 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215ea..816089c30 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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 000000000..1ea77c826
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 2337771a1..333c45035 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -145,7 +145,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -268,6 +268,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoi";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -477,7 +481,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -756,6 +760,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 77+ messages in thread

* [PATCH] F2FS support
@ 2017-05-04 18:12 ` Jaegeuk Kim
  0 siblings, 0 replies; 77+ messages in thread
From: Jaegeuk Kim @ 2017-05-04 18:12 UTC (permalink / raw)
  To: grub-devel, linux-f2fs-devel; +Cc: Jaegeuk Kim

"F2FS (Flash-Friendly File System) is flash-friendly file system which was merged
into Linux kernel v3.8 in 2013.

The motive for F2FS was to build a file system that from the start, takes into
account the characteristics of NAND flash memory-based storage devices (such as
solid-state disks, eMMC, and SD cards).

F2FS was designed on a basis of a log-structured file system approach, which
remedies some known issues of the older log structured file systems, such as
the snowball effect of wandering trees and high cleaning overhead. In addition,
since a NAND-based storage device shows different characteristics according to
its internal geometry or flash memory management scheme (such as the Flash
Translation Layer or FTL), it supports various parameters not only for
configuring on-disk layout, but also for selecting allocation and cleaning
algorithm.", quote by https://en.wikipedia.org/wiki/F2FS.

The source codes for F2FS are available from:

http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git
http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git

Update:
 - This patch has been integrated in OpenMandriva Lx 3.
   https://www.openmandriva.org/

Acked-by: Andrei Borzenkov <arvidjaar@gmail.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
---
 Makefile.util.def            |    7 +
 docs/grub.texi               |    5 +-
 grub-core/Makefile.core.def  |    5 +
 grub-core/fs/f2fs.c          | 1289 ++++++++++++++++++++++++++++++++++++++++++
 po/exclude.pot               |    1 +
 tests/f2fs_test.in           |   19 +
 tests/util/grub-fs-tester.in |   10 +-
 7 files changed, 1332 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/fs/f2fs.c
 create mode 100644 tests/f2fs_test.in

diff --git a/Makefile.util.def b/Makefile.util.def
index f9caccb97..3180ac880 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -99,6 +99,7 @@ library = {
   common = grub-core/fs/ext2.c;
   common = grub-core/fs/fat.c;
   common = grub-core/fs/exfat.c;
+  common = grub-core/fs/f2fs.c;
   common = grub-core/fs/fshelp.c;
   common = grub-core/fs/hfs.c;
   common = grub-core/fs/hfsplus.c;
@@ -776,6 +777,12 @@ script = {
 
 script = {
   testcase;
+  name = f2fs_test;
+  common = tests/f2fs_test.in;
+};
+
+script = {
+  testcase;
   name = nilfs2_test;
   common = tests/nilfs2_test.in;
 };
diff --git a/docs/grub.texi b/docs/grub.texi
index e935af33e..7137d6d0d 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -360,7 +360,8 @@ blocklist notation. The currently supported filesystem 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}, @dfn{HFS},
+@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT},
+@dfn{f2fs}, @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},
@@ -5368,7 +5369,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, minix,
 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 charset
 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 2dfa22a92..7f084d466 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1291,6 +1291,11 @@ module = {
 };
 
 module = {
+  name = f2fs;
+  common = fs/f2fs.c;
+};
+
+module = {
   name = fshelp;
   common = fs/fshelp.c;
 };
diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c
new file mode 100644
index 000000000..7fb256f20
--- /dev/null
+++ b/grub-core/fs/f2fs.c
@@ -0,0 +1,1289 @@
+/*
+ *  f2fs.c - Flash-Friendly File System
+ *
+ *  Written by Jaegeuk Kim <jaegeuk@kernel.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <grub/err.h>
+#include <grub/file.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/disk.h>
+#include <grub/dl.h>
+#include <grub/types.h>
+#include <grub/charset.h>
+#include <grub/fshelp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+/* F2FS Magic Number */
+#define F2FS_SUPER_MAGIC	0xF2F52010
+#define CHECKSUM_OFFSET		4092		/* must be aligned 4 bytes */
+#define U32_CHECKSUM_OFFSET	(CHECKSUM_OFFSET >> 2)
+
+/* byte-size offset */
+#define F2FS_SUPER_OFFSET	((grub_disk_addr_t)1024)
+#define F2FS_SUPER_OFFSET0	(F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
+#define F2FS_SUPER_OFFSET1	((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >>	\
+						GRUB_DISK_SECTOR_BITS)
+
+/* 9 bits for 512 bytes */
+#define F2FS_MIN_LOG_SECTOR_SIZE	9
+
+/* support only 4KB block */
+#define F2FS_BLK_BITS	12
+#define F2FS_BLKSIZE	(1 << F2FS_BLK_BITS)
+#define F2FS_BLK_SEC_BITS	(F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
+
+#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	(sizeof(struct grub_f2fs_nat_jent))
+#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 (sizeof(struct grub_f2fs_nat_entry))
+#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
+
+#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
+
+#define CKPT_FLAG_SET(ckpt, f)	\
+		(ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f)
+
+#define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
+#define F2FS_INLINE_DATA	0x02	/* file inline data flag */
+#define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
+#define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
+#define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
+
+enum FILE_TYPE
+{
+  F2FS_FT_UNKNOWN,
+  F2FS_FT_REG_FILE = 1,
+  F2FS_FT_DIR = 2,
+  F2FS_FT_SYMLINK = 7,
+};
+
+#define MAX_VOLUME_NAME		512
+
+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[MAX_VOLUME_NAME];
+  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;
+    char buf[F2FS_BLKSIZE - 40];	/* Should occupy F2FS_BLKSIZE totally */
+  };
+  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_uint8_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
+grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
+{
+  return addr[nr >> 3] & (1 << (nr & 7));
+}
+
+static inline char *
+__inline_addr (struct grub_f2fs_inode *inode)
+{
+  return (char *)&inode->i_addr[1];
+}
+
+static inline grub_uint64_t
+grub_f2fs_file_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 = &data->ckpt;
+  grub_uint32_t start_addr = data->cp_blkaddr;
+
+  if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(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 = &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 = &data->ckpt;
+
+  return __start_cp_addr (data) +
+	grub_le_to_cpu32 (ckpt->cp_pack_total_block_count)
+	- (base + 1) + type;
+}
+
+static inline void *
+__nat_bitmap_ptr (struct grub_f2fs_data *data)
+{
+  struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
+  grub_uint32_t offset;
+
+  if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
+    return ckpt->sit_nat_version_bitmap;
+
+  offset = 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 inode_block)
+{
+  if (inode_block)
+    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,
+		((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
+		0, F2FS_BLKSIZE, buf);
+}
+
+/*
+ * CRC32
+*/
+#define CRCPOLY_LE 0xedb88320
+
+static inline grub_uint32_t
+grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t crc = F2FS_SUPER_MAGIC;
+  unsigned char *p = (unsigned char *)buf;
+  grub_uint32_t tmp = len;
+  int i;
+
+  while (tmp--)
+    {
+      crc ^= *p++;
+      for (i = 0; i < 8; i++)
+        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
+    }
+  return crc;
+}
+
+static inline int
+grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
+{
+  grub_uint32_t cal_crc = 0;
+
+  cal_crc = grub_f2fs_cal_crc32 (buf, len);
+
+  return (cal_crc == blk_crc) ? 1 : 0;
+}
+
+static inline int
+grub_f2fs_test_bit (grub_uint32_t nr, const char *p)
+{
+  int mask;
+
+  p += (nr >> 3);
+  mask = 1 << (7 - (nr & 0x07));
+  return mask & *p;
+}
+
+static int
+grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
+{
+  grub_uint32_t log_sectorsize, log_sectors_per_block;
+
+  if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
+    return -1;
+
+  if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
+    return -1;
+
+  log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
+  log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
+
+  if (log_sectorsize > F2FS_BLK_BITS)
+    return -1;
+
+  if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
+    return -1;
+
+  if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
+    return -1;
+
+  return 0;
+}
+
+static int
+grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
+{
+  grub_disk_t disk = data->disk;
+  grub_err_t err;
+
+  /* Read first super block. */
+  err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
+  if (err)
+    return -1;
+
+  return grub_f2fs_sanity_check_sb (&data->sblock);
+}
+
+static void *
+validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
+	grub_uint64_t *version)
+{
+  grub_uint32_t *cp_page_1, *cp_page_2;
+  struct grub_f2fs_checkpoint *cp_block;
+  grub_uint64_t cur_version = 0, pre_version = 0;
+  grub_uint32_t crc = 0;
+  grub_uint32_t crc_offset;
+  grub_err_t err;
+
+  /* Read the 1st cp block in this CP pack */
+  cp_page_1 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_1)
+    return NULL;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
+  if (err)
+    goto invalid_cp1;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp1;
+
+  crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp1;
+
+  pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+
+  /* Read the 2nd cp block in this CP pack */
+  cp_page_2 = grub_malloc (F2FS_BLKSIZE);
+  if (!cp_page_2)
+    goto invalid_cp1;
+
+  cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
+
+  err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
+  if (err)
+    goto invalid_cp2;
+
+  cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
+  crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
+  if (crc_offset != CHECKSUM_OFFSET)
+    goto invalid_cp2;
+
+  crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
+  if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
+    goto invalid_cp2;
+
+  cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
+  if (cur_version == pre_version)
+    {
+      *version = 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 = 0, cp2_version = 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 = data->cp_blkaddr;
+  cp1 = 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 += data->blocks_per_seg;
+  cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
+  if (!cp2 && grub_errno)
+    {
+      grub_free (cp1);
+      return grub_errno;
+    }
+
+  if (cp1 && cp2)
+    cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
+  else if (cp1)
+    cur_page = cp1;
+  else if (cp2)
+    cur_page = cp2;
+  else
+    return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
+
+  grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
+
+  grub_free (cp1);
+  grub_free (cp2);
+  return 0;
+}
+
+static grub_err_t
+get_nat_journal (struct grub_f2fs_data *data)
+{
+  grub_uint32_t block;
+  char *buf;
+  grub_err_t err;
+
+  buf = grub_malloc (F2FS_BLKSIZE);
+  if (!buf)
+    return grub_errno;
+
+  if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
+    block = __start_sum_block (data);
+  else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
+    block = __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
+  else
+    block = __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
+
+  err = 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 = grub_le_to_cpu16 (data->nat_j.n_nats);
+  grub_uint32_t blkaddr = 0;
+  grub_uint16_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
+        {
+          blkaddr = 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 = get_blkaddr_from_nat_journal (data, nid);
+  if (blkaddr)
+    return blkaddr;
+
+  nat_block = grub_malloc (F2FS_BLKSIZE);
+  if (!nat_block)
+    return 0;
+
+  block_off = nid / NAT_ENTRY_PER_BLOCK;
+  entry_off = nid % NAT_ENTRY_PER_BLOCK;
+
+  seg_off = block_off / data->blocks_per_seg;
+  block_addr = 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 += data->blocks_per_seg;
+
+  err = grub_f2fs_block_read (data, block_addr, nat_block);
+  if (err)
+    {
+      grub_free (nat_block);
+      return 0;
+    }
+
+  blkaddr = 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 = ADDRS_PER_BLOCK;
+  grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
+  grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
+  grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
+  grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
+  int n = 0;
+  int level = 0;
+
+  if (inode->i_inline & F2FS_INLINE_XATTR)
+    direct_index -= F2FS_INLINE_XATTR_ADDRS;
+
+  noffset[0] = 0;
+
+  if (block < direct_index)
+    {
+      offset[n] = block;
+      goto got;
+    }
+
+  block -= direct_index;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR1_BLOCK;
+      noffset[n] = 1;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < direct_blks)
+    {
+      offset[n++] = NODE_DIR2_BLOCK;
+      noffset[n] = 2;
+      offset[n] = block;
+      level = 1;
+      goto got;
+    }
+
+  block -= direct_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND1_BLOCK;
+      noffset[n] = 3;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 4 + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < indirect_blks)
+    {
+      offset[n++] = NODE_IND2_BLOCK;
+      noffset[n] = 4 + dptrs_per_blk;
+      offset[n++] = block / direct_blks;
+      noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 2;
+      goto got;
+    }
+
+  block -= indirect_blks;
+  if (block < dindirect_blks)
+    {
+      offset[n++] = NODE_DIND_BLOCK;
+      noffset[n] = 5 + (dptrs_per_blk * 2);
+      offset[n++] = block / indirect_blks;
+      noffset[n] = 6 + (dptrs_per_blk * 2) +
+      offset[n - 1] * (dptrs_per_blk + 1);
+      offset[n++] = (block / direct_blks) % dptrs_per_blk;
+      noffset[n] = 7 + (dptrs_per_blk * 2) +
+		offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
+      offset[n] = block % direct_blks;
+      level = 3;
+      goto got;
+    }
+got:
+  return level;
+}
+
+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 = 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 = grub_malloc (sizeof (*data));
+  if (!data)
+    return NULL;
+
+  data->disk = disk;
+
+  if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
+    {
+      if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
+        {
+          if (grub_errno == GRUB_ERR_NONE)
+            grub_error (GRUB_ERR_BAD_FS,
+			"not a F2FS filesystem (no superblock)");
+          goto fail;
+        }
+    }
+
+  data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
+  data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
+  data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
+  data->blocks_per_seg = 1 <<
+	grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
+
+  err = grub_f2fs_read_cp (data);
+  if (err)
+    goto fail;
+
+  data->nat_bitmap = __nat_bitmap_ptr (data);
+
+  err = get_nat_journal (data);
+  if (err)
+    goto fail;
+
+  data->diropen.data = data;
+  data->diropen.ino = data->root_ino;
+  data->diropen.inode_read = 1;
+  data->inode = &data->diropen.inode;
+
+  err = grub_f2fs_read_node (data, data->root_ino, data->inode);
+  if (err)
+    goto fail;
+
+  return data;
+
+fail:
+  grub_free (data);
+  return NULL;
+}
+
+/* guarantee inline_data was handled by caller */
+static grub_disk_addr_t
+grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
+{
+  struct grub_f2fs_data *data = node->data;
+  struct grub_f2fs_inode *inode = &node->inode.i;
+  grub_uint32_t offset[4], noffset[4], nids[4];
+  struct grub_f2fs_node *node_block;
+  grub_uint32_t block_addr = -1;
+  int level, i;
+
+  level = grub_get_node_path (inode, block_ofs, offset, noffset);
+  if (level == 0)
+    return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
+
+  node_block = grub_malloc (F2FS_BLKSIZE);
+  if (!node_block)
+    return -1;
+
+  nids[1] = __get_node_id (&node->inode, offset[0], 1);
+
+  /* get indirect or direct nodes */
+  for (i = 1; i <= level; i++)
+    {
+      grub_f2fs_read_node (data, nids[i], node_block);
+      if (grub_errno)
+        goto fail;
+
+      if (i < level)
+        nids[i + 1] = __get_node_id (node_block, offset[i], 0);
+    }
+
+  block_addr = 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 = &node->inode.i;
+  grub_off_t filesize = grub_f2fs_file_size (inode);
+  char *inline_addr = __inline_addr (inode);
+
+  if (inode->i_inline & F2FS_INLINE_DATA)
+    {
+      if (filesize > MAX_INLINE_DATA)
+        return -1;
+      if (len > filesize - pos)
+        len = filesize - pos;
+
+      grub_memcpy (buf, 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_get_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 = node;
+  grub_uint64_t filesize;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+        return 0;
+    }
+
+  filesize = grub_f2fs_file_size(&diro->inode.i);
+
+  symlink = grub_malloc (filesize + 1);
+  if (!symlink)
+    return 0;
+
+  grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
+  if (grub_errno)
+    {
+      grub_free (symlink);
+      return 0;
+    }
+
+  symlink[filesize] = '\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 = 0; i < ctx->max;)
+    {
+      char *filename;
+      enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
+      enum FILE_TYPE ftype;
+      int name_len;
+      int ret;
+
+      if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
+        {
+          i++;
+          continue;
+        }
+
+      ftype = ctx->dentry[i].file_type;
+      name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
+      filename = grub_malloc (name_len + 1);
+      if (!filename)
+        return 0;
+
+      grub_memcpy (filename, ctx->filename[i], name_len);
+      filename[name_len] = 0;
+
+      fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
+      if (!fdiro)
+        {
+          grub_free(filename);
+          return 0;
+        }
+
+      if (ftype == F2FS_FT_DIR)
+        type = GRUB_FSHELP_DIR;
+      else if (ftype == F2FS_FT_SYMLINK)
+        type = GRUB_FSHELP_SYMLINK;
+      else if (ftype == F2FS_FT_REG_FILE)
+        type = GRUB_FSHELP_REG;
+
+      fdiro->data = ctx->data;
+      fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
+      fdiro->inode_read = 0;
+
+      ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
+      grub_free(filename);
+      if (ret)
+        return 1;
+
+      i += (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 = (struct grub_f2fs_inline_dentry *) __inline_addr (dir);
+
+  ctx->bitmap = de_blk->dentry_bitmap;
+  ctx->dentry = de_blk->dentry;
+  ctx->filename = de_blk->filename;
+  ctx->max = 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 = (struct grub_fshelp_node *) dir;
+  struct grub_f2fs_inode *inode;
+  struct grub_f2fs_dir_iter_ctx ctx = {
+    .data = diro->data,
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  grub_off_t fpos = 0;
+
+  if (!diro->inode_read)
+    {
+      grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
+      if (grub_errno)
+	return 0;
+    }
+
+  inode = &diro->inode.i;
+
+  if (inode->i_inline & F2FS_INLINE_DENTRY)
+    return grub_f2fs_iterate_inline_dir (inode, &ctx);
+
+  while (fpos < grub_f2fs_file_size (inode))
+    {
+      struct grub_f2fs_dentry_block *de_blk;
+      char *buf;
+      int ret;
+
+      buf = 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 = (struct grub_f2fs_dentry_block *) buf;
+
+      ctx.bitmap = de_blk->dentry_bitmap;
+      ctx.dentry = de_blk->dentry;
+      ctx.filename = de_blk->filename;
+      ctx.max = NR_DENTRY_IN_BLOCK;
+
+      ret = grub_f2fs_check_dentries (&ctx);
+      grub_free (buf);
+      if (ret)
+        return 1;
+
+      fpos += F2FS_BLKSIZE;
+    }
+  return 0;
+}
+
+static int
+grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
+	      grub_fshelp_node_t node, void *data)
+{
+  struct grub_f2fs_dir_ctx *ctx = 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 = 1;
+      grub_errno = GRUB_ERR_NONE;
+    }
+  if (node->inode_read)
+    {
+      info.mtimeset = 1;
+      info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
+    }
+
+  info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
+  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 = {
+    .hook = hook,
+    .hook_data = hook_data
+  };
+  struct grub_fshelp_node *fdiro = 0;
+
+  grub_dl_ref (my_mod);
+
+  ctx.data = 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 != &ctx.data->diropen)
+    grub_free (fdiro);
+ 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 = NULL;
+  struct grub_fshelp_node *fdiro = 0;
+  struct grub_f2fs_inode *inode;
+
+  grub_dl_ref (my_mod);
+
+  data = 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, sizeof (*data->inode));
+  grub_free (fdiro);
+
+  inode = &(data->inode->i);
+  file->size = grub_f2fs_file_size (inode);
+  file->data = data;
+  file->offset = 0;
+
+  if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
+    grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
+  return 0;
+
+fail:
+  if (fdiro != &data->diropen)
+    grub_free (fdiro);
+  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 = (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 = (struct grub_f2fs_data *) file->data;
+
+  grub_free (data);
+
+  grub_dl_unref (my_mod);
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_uint8_t *
+grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
+{
+  grub_uint16_t in_buf[MAX_VOLUME_NAME];
+  grub_uint8_t *out_buf;
+  int len = 0;
+
+  out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
+  if (!out_buf)
+    return NULL;
+
+  while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
+    in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
+    len++;
+  }
+
+  *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
+  return out_buf;
+}
+
+static grub_err_t
+grub_f2fs_label (grub_device_t device, char **label)
+{
+  struct grub_f2fs_data *data;
+  grub_disk_t disk = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
+  else
+    *label = NULL;
+
+  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 = device->disk;
+
+  grub_dl_ref (my_mod);
+
+  data = grub_f2fs_mount (disk);
+  if (data)
+    {
+      *uuid =
+	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 = NULL;
+
+  grub_free (data);
+  grub_dl_unref (my_mod);
+  return grub_errno;
+}
+
+static struct grub_fs grub_f2fs_fs = {
+  .name = "f2fs",
+  .dir = grub_f2fs_dir,
+  .open = grub_f2fs_open,
+  .read = grub_f2fs_read,
+  .close = grub_f2fs_close,
+  .label = grub_f2fs_label,
+  .uuid = grub_f2fs_uuid,
+#ifdef GRUB_UTIL
+  .reserved_first_sector = 1,
+  .blocklist_install = 0,
+#endif
+  .next = 0
+};
+
+GRUB_MOD_INIT (f2fs)
+{
+  grub_fs_register (&grub_f2fs_fs);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (f2fs)
+{
+  grub_fs_unregister (&grub_f2fs_fs);
+}
diff --git a/po/exclude.pot b/po/exclude.pot
index 0a9b215ea..816089c30 100644
--- a/po/exclude.pot
+++ b/po/exclude.pot
@@ -1214,6 +1214,7 @@ msgstr ""
 
 #: 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 000000000..1ea77c826
--- /dev/null
+++ b/tests/f2fs_test.in
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ "x$EUID" = "x" ] ; then
+ EUID=`id -u`
+fi
+
+if [ "$EUID" != 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 2337771a1..333c45035 100644
--- a/tests/util/grub-fs-tester.in
+++ b/tests/util/grub-fs-tester.in
@@ -145,7 +145,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	xsquash*)
 	    MINBLKSIZE=4096
 	    MAXBLKSIZE=1048576;;
-	xxfs)
+	xxfs|xf2fs)
 	    MINBLKSIZE=$SECSIZE
 		# OS Limitation: GNU/Linux doesn't accept > 4096
 	    MAXBLKSIZE=4096;;
@@ -268,6 +268,10 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		x"btrfs"*)
 		    FSLABEL="grub_;/testé莭莽😁киритi urewfceniuewruevrewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoi";;
 
+	    # FS LIMITATION: f2fs label is at most 512 UTF-16 chars
+		x"f2fs")
+		    FSLABEL="grub_;/testé䏌䐓䏕киритiurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvrevuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifoiq";;
+
 	    # FS LIMITATION: exfat is at most 15 UTF-16 chars
 		x"exfat")
 		    FSLABEL="géт ;/莭莽😁кир";;
@@ -477,7 +481,7 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 	    # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check it.
 	# FS LIMITATION: as far as I know those FS don't store their last modification date.
 		x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs_old" | x"reiserfs" \
-		    | 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=y;;
@@ -756,6 +760,8 @@ for ((LOGSECSIZE=MINLOGSECSIZE;LOGSECSIZE<=MAXLOGSECSIZE;LOGSECSIZE=LOGSECSIZE +
 		    MOUNTDEVICE="/dev/mapper/grub_test-testvol"
 		    MOUNTFS=ext2
 		    "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)
-- 
2.11.0



^ permalink raw reply related	[flat|nested] 77+ messages in thread

end of thread, other threads:[~2018-04-04 21:11 UTC | newest]

Thread overview: 77+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-24  8:19 [PATCH] F2FS support Jaegeuk Kim
2015-03-24  8:19 ` Jaegeuk Kim
2015-03-28  7:31 ` Andrei Borzenkov
2015-03-28  7:31   ` Andrei Borzenkov
2015-03-28 20:43   ` Jaegeuk Kim
2015-03-28 20:43     ` Jaegeuk Kim
2015-03-28 21:00     ` Andrei Borzenkov
2015-03-28 21:00       ` Andrei Borzenkov
2015-04-03 22:48   ` Jaegeuk Kim
2015-04-03 22:48     ` Jaegeuk Kim
2015-04-03 22:49 ` [PATCH v2] " Jaegeuk Kim
2015-04-03 22:49   ` Jaegeuk Kim
2015-04-29 20:48   ` [f2fs-dev] " Jaegeuk Kim
2015-04-29 20:48     ` Jaegeuk Kim
2015-04-30  3:32     ` Andrei Borzenkov
2015-04-30  3:32       ` Andrei Borzenkov
2015-05-02 17:15   ` Andrei Borzenkov
2015-05-02 17:15     ` Andrei Borzenkov
2015-05-03  6:28     ` Andrei Borzenkov
2015-05-03  6:28       ` Andrei Borzenkov
2015-05-07 14:51     ` Vladimir 'φ-coder/phcoder' Serbinenko
2015-05-07 14:57       ` Andrei Borzenkov
2015-11-19 21:28   ` [PATCH v3] " Jaegeuk Kim
2015-11-19 21:28     ` Jaegeuk Kim
2015-12-14  8:28     ` Andrei Borzenkov
2015-12-14  8:28       ` Andrei Borzenkov
2015-12-15  0:30       ` Jaegeuk Kim
2015-12-15  0:30         ` [f2fs-dev] " Jaegeuk Kim
2015-12-15  0:34       ` [PATCH v4] " Jaegeuk Kim
2015-12-15  0:34         ` [f2fs-dev] " Jaegeuk Kim
2015-12-15  8:34         ` Andrei Borzenkov
2015-12-15  8:34           ` [f2fs-dev] " Andrei Borzenkov
2015-12-15 18:08           ` Jaegeuk Kim
2015-12-15 18:08             ` [f2fs-dev] " Jaegeuk Kim
2015-12-15 18:14         ` [PATCH v5] " Jaegeuk Kim
2015-12-15 18:14           ` [f2fs-dev] " Jaegeuk Kim
2016-01-07 19:37           ` Michael Zimmermann
2016-01-07 19:37             ` Michael Zimmermann
2016-01-08 19:41           ` [PATCH v6] " Jaegeuk Kim
2016-01-08 19:41             ` [f2fs-dev] " Jaegeuk Kim
2016-02-22  9:25             ` Andrei Borzenkov
2016-02-22  9:25               ` [f2fs-dev] " Andrei Borzenkov
2016-02-22 18:21               ` Jaegeuk Kim
2016-02-22 18:21                 ` Jaegeuk Kim
2016-02-22 18:25             ` [PATCH v7] " Jaegeuk Kim
2016-02-22 18:25               ` [f2fs-dev] " Jaegeuk Kim
2016-03-01 19:52               ` [2.02] " Andrei Borzenkov
2016-03-01 19:52                 ` Andrei Borzenkov
2016-03-02 23:20                 ` Michael Zimmermann
2016-03-02 23:20                   ` Michael Zimmermann
2016-03-03 21:35                   ` [2.02] " Jaegeuk Kim
2016-03-03 21:35                     ` [2.02] Re: [f2fs-dev] " Jaegeuk Kim
2016-03-03 21:36                 ` [2.02] Re: [f2fs-dev] [PATCH v8] " Jaegeuk Kim
2016-03-03 21:36                   ` Jaegeuk Kim
2016-08-04 17:06                   ` [2.02] " Jaegeuk Kim
2016-08-04 17:06                     ` [f2fs-dev] " Jaegeuk Kim
2016-08-05 10:57                     ` Andrei Borzenkov
2016-08-05 10:57                       ` [f2fs-dev] " Andrei Borzenkov
2016-08-05 18:07                       ` Jaegeuk Kim
2016-08-05 18:07                         ` [f2fs-dev] " Jaegeuk Kim
2016-08-05 19:17                         ` Michael Zimmermann
2016-08-05 19:17                           ` Michael Zimmermann
2017-05-04 18:12 [PATCH] " Jaegeuk Kim
2017-05-04 18:12 ` Jaegeuk Kim
2017-05-04 20:52 ` Adam Borowski
2017-05-04 20:52   ` Adam Borowski
2018-03-17  9:08 林博仁
2018-03-18 20:39 ` Pete Batard
2018-03-22 14:26   ` Daniel Kiper
2018-03-22 16:47 Pete Batard
2018-03-28 12:04 ` Daniel Kiper
2018-03-29 16:08   ` Pete Batard
2018-03-31 20:47     ` Paul Menzel
2018-04-01 19:16       ` Pete Batard
2018-04-04 21:03       ` Daniel Kiper
2018-04-04 20:37     ` Daniel Kiper
2018-04-04 21:11       ` Pete Batard

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.