Skip to contentSkip to conversation

mkfs: get fd for f2fs_trim_device

PATCH New Jaegeuk Kim 1 participant 3 messages Sep 24, 2016 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 mkfs: get fd for f2fs_trim_device

This patch is to get a parameter for fd in f2fs_trim_device.

---
 mkfs/f2fs_format.c       | 10 ++++++----
 mkfs/f2fs_format_utils.c | 28 ++++++++++++----------------
 mkfs/f2fs_format_utils.h |  2 +-
 3 files changed, 19 insertions(+), 21 deletions(-)
mkfs/f2fs_format.c +6 -4
index 6b1318a..466df27 100644
@@ -927,10 +927,12 @@ int f2fs_format_device(void)
 		goto exit;
 	}
 
-	err = f2fs_trim_device();
-	if (err < 0) {
-		MSG(0, "\tError: Failed to trim whole device!!!\n");
-		goto exit;
+	if (config.trim) {
+		err = f2fs_trim_device(config.fd);
+		if (err < 0) {
+			MSG(0, "\tError: Failed to trim whole device!!!\n");
+			goto exit;
+		}
 	}
 
 	err = f2fs_init_sit_area();
mkfs/f2fs_format_utils.c +12 -16
index d469ce5..eb8e3c0 100644
@@ -34,27 +34,24 @@
 #define BLKSECDISCARD	_IO(0x12,125)
 #endif
 
-int f2fs_trim_device()
+int f2fs_trim_device(int fd)
 {
 	unsigned long long range[2];
 	struct stat stat_buf;
 
-	if (!config.trim)
-		return 0;
-
-	range[0] = 0;
-	range[1] = config.total_sectors * config.sector_size;
-
-	if (fstat(config.fd, &stat_buf) < 0 ) {
+	if (fstat(fd, &stat_buf) < 0 ) {
 		MSG(1, "\tError: Failed to get the device stat!!!\n");
 		return -1;
 	}
 
+	range[0] = 0;
+	range[1] = stat_buf.st_size;
+
 #if defined(WITH_BLKDISCARD) && defined(BLKDISCARD)
 	MSG(0, "Info: Discarding device\n");
 	if (S_ISREG(stat_buf.st_mode)) {
 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
-		if (fallocate(config.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+		if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
 				range[0], range[1]) < 0) {
 			MSG(0, "Info: fallocate(PUNCH_HOLE|KEEP_SIZE) is failed\n");
 		}
@@ -62,23 +59,22 @@ int f2fs_trim_device()
 		return 0;
 	} else if (S_ISBLK(stat_buf.st_mode)) {
 #ifdef BLKSECDISCARD
-		if (ioctl(config.fd, BLKSECDISCARD, &range) < 0) {
+		if (ioctl(fd, BLKSECDISCARD, &range) < 0) {
 			MSG(0, "Info: This device doesn't support BLKSECDISCARD\n");
 		} else {
-			MSG(0, "Info: Secure Discarded %lu sectors\n",
-						config.total_sectors);
+			MSG(0, "Info: Secure Discarded %lu MB\n",
+						stat_buf.st_size >> 20);
 			return 0;
 		}
 #endif
-		if (ioctl(config.fd, BLKDISCARD, &range) < 0) {
+		if (ioctl(fd, BLKDISCARD, &range) < 0) {
 			MSG(0, "Info: This device doesn't support BLKDISCARD\n");
 		} else {
-			MSG(0, "Info: Discarded %lu sectors\n",
-						config.total_sectors);
+			MSG(0, "Info: Discarded %lu MB\n",
+						stat_buf.st_size >> 20);
 		}
 	} else
 		return -1;
 #endif
 	return 0;
 }
-
mkfs/f2fs_format_utils.h +1 -1
index 9eb2cea..8bf598e 100644
@@ -12,5 +12,5 @@
 
 extern struct f2fs_configuration config;
 
-int f2fs_trim_device(void);
+int f2fs_trim_device(int);
 int f2fs_format_device(void);
Signature · 4 lines
2.8.3


------------------------------------------------------------------------------
#1 lore raw

PATCH 2/3 libf2fs: use dev_read_block and dev_write_block

This patche tries to use dev_read_block and dev_write_block as much as possible.

---
 include/f2fs_fs.h  |   2 +-
 lib/libf2fs_io.c   |  12 +++----
 mkfs/f2fs_format.c | 101 ++++++++++++++++++++++++++++-------------------------
 3 files changed, 61 insertions(+), 54 deletions(-)
include/f2fs_fs.h +1 -1
index 1345e2d..d44e10e 100644
@@ -964,9 +964,9 @@ extern int dev_write_block(void *, __u64);
 extern int dev_write_dump(void *, __u64, size_t);
 /* All bytes in the buffer must be 0 use dev_fill(). */
 extern int dev_fill(void *, __u64, size_t);
+extern int dev_fill_block(void *, __u64);
 
 extern int dev_read_block(void *, __u64);
-extern int dev_read_blocks(void *, __u64, __u32 );
 extern int dev_reada_block(__u64);
 
 extern int dev_read_version(void *, __u64, size_t);
lib/libf2fs_io.c +6 -6
index afa345f..d2a04b2 100644
@@ -66,7 +66,7 @@ int dev_write(void *buf, __u64 offset, size_t len)
 
 int dev_write_block(void *buf, __u64 blk_addr)
 {
-	return dev_write(buf, blk_addr * F2FS_BLKSIZE, F2FS_BLKSIZE);
+	return dev_write(buf, blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
 }
 
 int dev_write_dump(void *buf, __u64 offset, size_t len)
@@ -90,19 +90,19 @@ int dev_fill(void *buf, __u64 offset, size_t len)
 	return 0;
 }
 
-int dev_read_block(void *buf, __u64 blk_addr)
+int dev_fill_block(void *buf, __u64 blk_addr)
 {
-	return dev_read(buf, blk_addr * F2FS_BLKSIZE, F2FS_BLKSIZE);
+	return dev_fill(buf, blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
 }
 
-int dev_read_blocks(void *buf, __u64 addr, __u32 nr_blks)
+int dev_read_block(void *buf, __u64 blk_addr)
 {
-	return dev_read(buf, addr * F2FS_BLKSIZE, nr_blks * F2FS_BLKSIZE);
+	return dev_read(buf, blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
 }
 
 int dev_reada_block(__u64 blk_addr)
 {
-	return dev_readahead(blk_addr * F2FS_BLKSIZE, F2FS_BLKSIZE);
+	return dev_readahead(blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
 }
 
 void f2fs_finalize_device(struct f2fs_configuration *c)
mkfs/f2fs_format.c +54 -47

212 lines not on this page · raw message

Signature · 4 lines
2.8.3


------------------------------------------------------------------------------
#2 lore raw

PATCH 3/3 f2fs-tools: use shorter config variable name

In reply to Jaegeuk Kim

This patch has no functional change.

---
 fsck/dump.c              |  12 +--
 fsck/fsck.c              |  88 +++++++++---------
 fsck/main.c              | 198 +++++++++++++++++++--------------------
 fsck/mount.c             |  50 +++++-----
 fsck/resize.c            |  30 +++---
 include/f2fs_fs.h        |  26 +++---
 lib/libf2fs.c            | 108 +++++++++++-----------
 lib/libf2fs_io.c         |  32 +++----
 lib/zbc.c                |  46 +++++-----
 mkfs/f2fs_format.c       | 235 +++++++++++++++++++++++++----------------------
 mkfs/f2fs_format_main.c  |  53 ++++++-----
 mkfs/f2fs_format_utils.h |   2 +-
 12 files changed, 446 insertions(+), 434 deletions(-)
fsck/dump.c +6 -6
index 332ff7c..80b18d2 100644
@@ -363,17 +363,17 @@ void dump_file(struct f2fs_sb_info *sbi, struct node_info *ni,
 		name[le32_to_cpu(inode->i_namelen)] = 0;
 		sprintf(path, "./lost_found/%s", name);
 
-		config.dump_fd = open(path, O_TRUNC|O_CREAT|O_RDWR, 0666);
-		ASSERT(config.dump_fd >= 0);
+		c.dump_fd = open(path, O_TRUNC|O_CREAT|O_RDWR, 0666);
+		ASSERT(c.dump_fd >= 0);
 
 		/* dump file's data */
 		dump_inode_blk(sbi, ni->ino, node_blk);
 
 		/* adjust file size */
-		ret = ftruncate(config.dump_fd, le32_to_cpu(inode->i_size));
+		ret = ftruncate(c.dump_fd, le32_to_cpu(inode->i_size));
 		ASSERT(ret >= 0);
 
-		close(config.dump_fd);
+		close(c.dump_fd);
 	}
 }
 
@@ -420,7 +420,7 @@ static void dump_node_from_blkaddr(u32 blk_addr)
 	ret = dev_read_block(node_blk, blk_addr);
 	ASSERT(ret >= 0);
 
-	if (config.dbg_lv > 0)
+	if (c.dbg_lv > 0)
 		print_node_info(node_blk);
 	else
 		print_inode_info(&node_blk->i, 1);
@@ -546,7 +546,7 @@ int dump_info_from_blkaddr(struct f2fs_sb_info *sbi, u32 blk_addr)
 	}
 
 	/* print inode */
-	if (config.dbg_lv > 0)
+	if (c.dbg_lv > 0)
 		dump_node_from_blkaddr(ino_ni.blk_addr);
 
 	if (type == SEG_TYPE_CUR_DATA || type == SEG_TYPE_DATA) {
fsck/fsck.c +44 -44

345 lines not on this page · raw message

fsck/main.c +99 -99

423 lines not on this page · raw message

fsck/mount.c +25 -25

117 lines not on this page · raw message

fsck/resize.c +15 -15
index b53a6fa..565555c 100644
@@ -22,19 +22,19 @@ static int get_new_sb(struct f2fs_sb_info *sbi, struct f2fs_super_block *sb)
 	u_int32_t blks_per_seg = 1 << get_sb(log_blocks_per_seg);
 	u_int32_t segs_per_zone = get_sb(segs_per_sec) * get_sb(secs_per_zone);
 
-	set_sb(block_count, config.target_sectors >>
+	set_sb(block_count, c.target_sectors >>
 				get_sb(log_sectors_per_block));
 
 	zone_size_bytes = segment_size_bytes * segs_per_zone;
 	zone_align_start_offset =
-		(config.start_sector * config.sector_size +
+		(c.start_sector * c.sector_size +
 		2 * F2FS_BLKSIZE + zone_size_bytes - 1) /
 		zone_size_bytes * zone_size_bytes -
-		config.start_sector * config.sector_size;
+		c.start_sector * c.sector_size;
 
-	set_sb(segment_count, (config.target_sectors * config.sector_size -
+	set_sb(segment_count, (c.target_sectors * c.sector_size -
 				zone_align_start_offset) / segment_size_bytes /
-				config.segs_per_sec * config.segs_per_sec);
+				c.segs_per_sec * c.segs_per_sec);
 
 	blocks_for_sit = ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
 	sit_segments = SEG_ALIGN(blocks_for_sit);
@@ -115,17 +115,17 @@ static int get_new_sb(struct f2fs_sb_info *sbi, struct f2fs_super_block *sb)
 						get_sb(segs_per_sec));
 
 	/* Let's determine the best reserved and overprovisioned space */
-	config.new_overprovision = get_best_overprovision(sb);
-	config.new_reserved_segments =
-		(2 * (100 / config.new_overprovision + 1) + 6) *
+	c.new_overprovision = get_best_overprovision(sb);
+	c.new_reserved_segments =
+		(2 * (100 / c.new_overprovision + 1) + 6) *
 						get_sb(segs_per_sec);
 
-	if ((get_sb(segment_count_main) - 2) < config.new_reserved_segments ||
+	if ((get_sb(segment_count_main) - 2) < c.new_reserved_segments ||
 		get_sb(segment_count_main) * blks_per_seg >
 						get_sb(block_count)) {
 		MSG(0, "\tError: Device size is not sufficient for F2FS volume, "
 			"more segment needed =%u",
-			config.new_reserved_segments -
+			c.new_reserved_segments -
 			(get_sb(segment_count_main) - 2));
 		return -1;
 	}
@@ -406,10 +406,10 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi,
 	ASSERT(buf);
 
 	/* ovp / free segments */
-	set_cp(rsvd_segment_count, config.new_reserved_segments);
+	set_cp(rsvd_segment_count, c.new_reserved_segments);
 	set_cp(overprov_segment_count, (get_newsb(segment_count_main) -
 			get_cp(rsvd_segment_count)) *
-			config.new_overprovision / 100);
+			c.new_overprovision / 100);
 	set_cp(overprov_segment_count, get_cp(overprov_segment_count) +
 						get_cp(rsvd_segment_count));
 
@@ -419,7 +419,7 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi,
 
 	set_cp(free_segment_count, free_segment_count + new_segment_count);
 	set_cp(user_block_count, ((get_newsb(segment_count_main) -
-			get_cp(overprov_segment_count)) * config.blks_per_seg));
+			get_cp(overprov_segment_count)) * c.blks_per_seg));
 
 	if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG))
 		orphan_blks = __start_sum_addr(sbi) - 1;
@@ -545,10 +545,10 @@ int f2fs_resize(struct f2fs_sb_info *sbi)
 		}
 	}
 
-	config.dbg_lv = 1;
+	c.dbg_lv = 1;
 	print_raw_sb_info(sb);
 	print_raw_sb_info(new_sb);
-	config.dbg_lv = 0;
+	c.dbg_lv = 0;
 
 	old_main_blkaddr = get_sb(main_blkaddr);
 	new_main_blkaddr = get_newsb(main_blkaddr);
include/f2fs_fs.h +13 -13
index d44e10e..19ce250 100644
@@ -125,7 +125,7 @@ static inline uint64_t bswap_64(uint64_t val)
 	do {								\
 		printf("[ASSERT] (%s:%4d) ", __func__, __LINE__);	\
 		printf(" --> "fmt"\n", ##__VA_ARGS__);			\
-		config.bug_on = 1;					\
+		c.bug_on = 1;						\
 	} while (0)
 
 #define ASSERT(exp)							\
@@ -144,14 +144,14 @@ static inline uint64_t bswap_64(uint64_t val)
 
 #define MSG(n, fmt, ...)						\
 	do {								\
-		if (config.dbg_lv >= n) {				\
+		if (c.dbg_lv >= n) {					\
 			printf(fmt, ##__VA_ARGS__);			\
 		}							\
 	} while (0)
 
 #define DBG(n, fmt, ...)						\
 	do {								\
-		if (config.dbg_lv >= n) {				\
+		if (c.dbg_lv >= n) {					\
 			printf("[%s:%4d] " fmt,				\
 				__func__, __LINE__, ##__VA_ARGS__);	\
 		}							\
@@ -708,7 +708,7 @@ struct f2fs_nat_block {
 #define F2FS_MAX_SEGMENT       ((16 * 1024 * 1024) / 2)
 #define MAX_SIT_BITMAP_SIZE    (SEG_ALIGN(ALIGN(F2FS_MAX_SEGMENT, \
 						SIT_ENTRY_PER_BLOCK)) * \
-						config.blks_per_seg / 8)
+						c.blks_per_seg / 8)
 
 /*
  * Note that f2fs_sit_entry->vblocks has the following bit-field information.
@@ -953,10 +953,10 @@ extern u64 find_next_zero_bit_le(const u8 *, u64, u64);
 extern u_int32_t f2fs_cal_crc32(u_int32_t, void *, int);
 extern int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len);
 
-extern void f2fs_init_configuration(struct f2fs_configuration *);
-extern int f2fs_dev_is_umounted(struct f2fs_configuration *);
-extern int f2fs_get_device_info(struct f2fs_configuration *);
-extern void f2fs_finalize_device(struct f2fs_configuration *);
+extern void f2fs_init_configuration(void);
+extern int f2fs_dev_is_umounted(void);
+extern int f2fs_get_device_info(void);
+extern void f2fs_finalize_device(void);
 
 extern int dev_read(void *, __u64, size_t);
 extern int dev_write(void *, __u64, size_t);
@@ -973,14 +973,14 @@ extern int dev_read_version(void *, __u64, size_t);
 extern void get_kernel_version(__u8 *);
 f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int);
 
-extern int zbc_scsi_report_zones(struct f2fs_configuration *);
+extern int zbc_scsi_report_zones(void);
 
-extern struct f2fs_configuration config;
+extern struct f2fs_configuration c;
 
 #define ALIGN(val, size)	((val) + (size) - 1) / (size)
-#define SEG_ALIGN(blks)		ALIGN(blks, config.blks_per_seg)
-#define ZONE_ALIGN(blks)	ALIGN(blks, config.blks_per_seg * \
-					config.segs_per_zone)
+#define SEG_ALIGN(blks)		ALIGN(blks, c.blks_per_seg)
+#define ZONE_ALIGN(blks)	ALIGN(blks, c.blks_per_seg * \
+					c.segs_per_zone)
 
 static inline double get_best_overprovision(struct f2fs_super_block *sb)
 {
lib/libf2fs.c +54 -54

209 lines not on this page · raw message

lib/libf2fs_io.c +16 -16
index d2a04b2..1817c15 100644
@@ -23,25 +23,25 @@
 
 #include <f2fs_fs.h>
 
-struct f2fs_configuration config;
+struct f2fs_configuration c;
 
 /*
  * IO interfaces
  */
 int dev_read_version(void *buf, __u64 offset, size_t len)
 {
-	if (lseek64(config.kd, (off64_t)offset, SEEK_SET) < 0)
+	if (lseek64(c.kd, (off64_t)offset, SEEK_SET) < 0)
 		return -1;
-	if (read(config.kd, buf, len) < 0)
+	if (read(c.kd, buf, len) < 0)
 		return -1;
 	return 0;
 }
 
 int dev_read(void *buf, __u64 offset, size_t len)
 {
-	if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0)
+	if (lseek64(c.fd, (off64_t)offset, SEEK_SET) < 0)
 		return -1;
-	if (read(config.fd, buf, len) < 0)
+	if (read(c.fd, buf, len) < 0)
 		return -1;
 	return 0;
 }
@@ -49,7 +49,7 @@ int dev_read(void *buf, __u64 offset, size_t len)
 int dev_readahead(__u64 offset, size_t len)
 {
 #ifdef POSIX_FADV_WILLNEED
-	return posix_fadvise(config.fd, offset, len, POSIX_FADV_WILLNEED);
+	return posix_fadvise(c.fd, offset, len, POSIX_FADV_WILLNEED);
 #else
 	return 0;
 #endif
@@ -57,9 +57,9 @@ int dev_readahead(__u64 offset, size_t len)
 
 int dev_write(void *buf, __u64 offset, size_t len)
 {
-	if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0)
+	if (lseek64(c.fd, (off64_t)offset, SEEK_SET) < 0)
 		return -1;
-	if (write(config.fd, buf, len) < 0)
+	if (write(c.fd, buf, len) < 0)
 		return -1;
 	return 0;
 }
@@ -71,9 +71,9 @@ int dev_write_block(void *buf, __u64 blk_addr)
 
 int dev_write_dump(void *buf, __u64 offset, size_t len)
 {
-	if (lseek64(config.dump_fd, (off64_t)offset, SEEK_SET) < 0)
+	if (lseek64(c.dump_fd, (off64_t)offset, SEEK_SET) < 0)
 		return -1;
-	if (write(config.dump_fd, buf, len) < 0)
+	if (write(c.dump_fd, buf, len) < 0)
 		return -1;
 	return 0;
 }
@@ -83,9 +83,9 @@ int dev_fill(void *buf, __u64 offset, size_t len)
 	/* Only allow fill to zero */
 	if (*((__u8*)buf))
 		return -1;
-	if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0)
+	if (lseek64(c.fd, (off64_t)offset, SEEK_SET) < 0)
 		return -1;
-	if (write(config.fd, buf, len) < 0)
+	if (write(c.fd, buf, len) < 0)
 		return -1;
 	return 0;
 }
@@ -105,17 +105,17 @@ int dev_reada_block(__u64 blk_addr)
 	return dev_readahead(blk_addr << F2FS_BLKSIZE_BITS, F2FS_BLKSIZE);
 }
 
-void f2fs_finalize_device(struct f2fs_configuration *c)
+void f2fs_finalize_device(void)
 {
 	/*
 	 * We should call fsync() to flush out all the dirty pages
 	 * in the block device page cache.
 	 */
-	if (fsync(c->fd) < 0)
+	if (fsync(c.fd) < 0)
 		MSG(0, "\tError: Could not conduct fsync!!!\n");
 
-	if (close(c->fd) < 0)
+	if (close(c.fd) < 0)
 		MSG(0, "\tError: Failed to close device file!!!\n");
 
-	close(c->kd);
+	close(c.kd);
 }
lib/zbc.c +23 -23

139 lines not on this page · raw message

mkfs/f2fs_format.c +124 -111

473 lines not on this page · raw message

mkfs/f2fs_format_main.c +26 -27

137 lines not on this page · raw message

mkfs/f2fs_format_utils.h +1 -1
index 8bf598e..bc91aa3 100644
@@ -10,7 +10,7 @@
 
 #include "f2fs_fs.h"
 
-extern struct f2fs_configuration config;
+extern struct f2fs_configuration c;
 
 int f2fs_trim_device(int);
 int f2fs_format_device(void);
Signature · 4 lines
2.8.3


------------------------------------------------------------------------------