Skip to contentSkip to conversation

btrfs: do not commit logs and transactions during link and rename operations

PATCH In review [email protected] 2 participants 3 messages Aug 11, 2020 – Aug 12, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 btrfs: do not commit logs and transactions during link and rename operations

From: Filipe Manana <[email protected]>

Since commit d4682ba03ef618 ("Btrfs: sync log after logging new name") we started to commit logs, and fallback to transaction commits when we failed to log the new names or commit the logs, after link and rename operations when the target inodes (or their parents) were previously logged in the current transaction. This was to avoid losing directories despite an explicit fsync on them when they are ancestors of some inode that got a new named logged, due to a link or rename operation. However that adds the cost of starting IO and waiting for it to complete, which can cause higher latencies for applications.

Instead of doing that, just make sure that when we log a new name for an inode we don't mark any of its ancestors as logged, so that if any one does an fsync against any of them, without doing any other change on them, the fsync commits the log. This way we only pay the cost of a log commit (or a transaction commit if something goes wrong or a new block group was created) if the application explicitly asks to fsync any of the parent directories.

Using dbench, which mixes several filesystems operations including renames, revealed some significant latency gains. The following script that uses dbench was used to test this:

#!/bin/bash

DEV=/dev/nvme0n1
MNT=/mnt/btrfs
MOUNT_OPTIONS="-o ssd -o space_cache=v2"
MKFS_OPTIONS="-m single -d single"
THREADS=16

echo "performance" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor mkfs.btrfs -f $MKFS_OPTIONS $DEV
mount $MOUNT_OPTIONS $DEV $MNT

dbench -t 300 -D $MNT $THREADS

umount $MNT

The test was run on bare metal, no virtualization, on a box with 12 cores (Intel i7-8700), 64Gb of RAM and using a NVMe device, with a kernel configuration that is the default of typical distributions (debian in this case), without debug options enabled (kasan, kmemleak, slub debug, debug of page allocations, lock debugging, etc).

Results before this patch:

Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 10750455 0.011 155.088

 Close        7896674     0.001     0.243
 Rename        455222     2.158  1101.947
 Unlink       2171189     0.067   121.638
 Deltree          256     2.425     7.816
 Mkdir            128     0.002     0.003
 Qpathinfo    9744323     0.006    21.370
 Qfileinfo    1707092     0.001     0.146
 Qfsinfo      1786756     0.001    11.228
 Sfileinfo     875612     0.003    21.263
 Find         3767281     0.025     9.617
 WriteX       5356924     0.011   211.390
 ReadX        16852694     0.003    9.442
 LockX          35008     0.002     0.119
 UnlockX        35008     0.001     0.138

Flush 753458 4.252 1102.249

Throughput 1128.35 MB/sec 16 clients 16 procs max_latency=1102.255 ms

Results after this patch:

16 clients, after

Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 11471098 0.012 448.281

 Close        8426396     0.001     0.925
 Rename        485746     0.123   267.183
 Unlink       2316477     0.080    63.433
 Deltree          288     2.830    11.144
 Mkdir            144     0.003     0.010
 Qpathinfo    10397420     0.006    10.288
 Qfileinfo    1822039     0.001     0.169
 Qfsinfo      1906497     0.002    14.039
 Sfileinfo     934433     0.004     2.438
 Find         4019879     0.026    10.200
 WriteX       5718932     0.011   200.985
 ReadX        17981671     0.003    10.036
 LockX          37352     0.002     0.076
 UnlockX        37352     0.001     0.109

Flush 804018 5.015 778.033

Throughput 1201.98 MB/sec 16 clients 16 procs max_latency=778.036 ms (+6.5% throughput, -29.4% max latency, -75.8% rename latency)

Test case generic/498 from fstests tests the scenario that the previously mentioned commit fixed.

---
 fs/btrfs/inode.c    | 115 +++++---------------------------------------
 fs/btrfs/tree-log.c | 100 +++++++++++++++++---------------------
 fs/btrfs/tree-log.h |  14 ++----
 3 files changed, 60 insertions(+), 169 deletions(-)
fs/btrfs/inode.c +13 -102
index 96064eb41d55..a6eaaa039447 100644
@@ -6373,7 +6373,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
 		drop_inode = 1;
 	} else {
 		struct dentry *parent = dentry->d_parent;
-		int ret;
 
 		err = btrfs_update_inode(trans, root, inode);
 		if (err)
@@ -6388,12 +6387,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
 				goto fail;
 		}
 		d_instantiate(dentry, inode);
-		ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
-					 true, NULL);
-		if (ret == BTRFS_NEED_TRANS_COMMIT) {
-			err = btrfs_commit_transaction(trans);
-			trans = NULL;
-		}
+		btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
 	}
 
 fail:
@@ -8775,27 +8769,19 @@ static int btrfs_rename_exchange(struct inode *old_dir,
 	struct inode *new_inode = new_dentry->d_inode;
 	struct inode *old_inode = old_dentry->d_inode;
 	struct timespec64 ctime = current_time(old_inode);
-	struct dentry *parent;
 	u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
 	u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
 	u64 old_idx = 0;
 	u64 new_idx = 0;
 	int ret;
+	int ret2;
 	bool root_log_pinned = false;
 	bool dest_log_pinned = false;
-	struct btrfs_log_ctx ctx_root;
-	struct btrfs_log_ctx ctx_dest;
-	bool sync_log_root = false;
-	bool sync_log_dest = false;
-	bool commit_transaction = false;
 
 	/* we only allow rename subvolume link between subvolumes */
 	if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
 		return -EXDEV;
 
-	btrfs_init_log_ctx(&ctx_root, old_inode);
-	btrfs_init_log_ctx(&ctx_dest, new_inode);
-
 	/* close the race window with snapshot create/destroy ioctl */
 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
 	    new_ino == BTRFS_FIRST_FREE_OBJECTID)
@@ -8937,30 +8923,14 @@ static int btrfs_rename_exchange(struct inode *old_dir,
 		BTRFS_I(new_inode)->dir_index = new_idx;
 
 	if (root_log_pinned) {
-		parent = new_dentry->d_parent;
-		ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
-					 BTRFS_I(old_dir), parent,
-					 false, &ctx_root);
-		if (ret == BTRFS_NEED_LOG_SYNC)
-			sync_log_root = true;
-		else if (ret == BTRFS_NEED_TRANS_COMMIT)
-			commit_transaction = true;
-		ret = 0;
+		btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
+				   new_dentry->d_parent);
 		btrfs_end_log_trans(root);
 		root_log_pinned = false;
 	}
 	if (dest_log_pinned) {
-		if (!commit_transaction) {
-			parent = old_dentry->d_parent;
-			ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
-						 BTRFS_I(new_dir), parent,
-						 false, &ctx_dest);
-			if (ret == BTRFS_NEED_LOG_SYNC)
-				sync_log_dest = true;
-			else if (ret == BTRFS_NEED_TRANS_COMMIT)
-				commit_transaction = true;
-			ret = 0;
-		}
+		btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
+				   old_dentry->d_parent);
 		btrfs_end_log_trans(dest);
 		dest_log_pinned = false;
 	}
@@ -8993,46 +8963,13 @@ static int btrfs_rename_exchange(struct inode *old_dir,
 			dest_log_pinned = false;
 		}
 	}
-	if (!ret && sync_log_root && !commit_transaction) {
-		ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
-				     &ctx_root);
-		if (ret)
-			commit_transaction = true;
-	}
-	if (!ret && sync_log_dest && !commit_transaction) {
-		ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
-				     &ctx_dest);
-		if (ret)
-			commit_transaction = true;
-	}
-	if (commit_transaction) {
-		/*
-		 * We may have set commit_transaction when logging the new name
-		 * in the destination root, in which case we left the source
-		 * root context in the list of log contextes. So make sure we
-		 * remove it to avoid invalid memory accesses, since the context
-		 * was allocated in our stack frame.
-		 */
-		if (sync_log_root) {
-			mutex_lock(&root->log_mutex);
-			list_del_init(&ctx_root.list);
-			mutex_unlock(&root->log_mutex);
-		}
-		ret = btrfs_commit_transaction(trans);
-	} else {
-		int ret2;
-
-		ret2 = btrfs_end_transaction(trans);
-		ret = ret ? ret : ret2;
-	}
+	ret2 = btrfs_end_transaction(trans);
+	ret = ret ? ret : ret2;
 out_notrans:
 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
 	    old_ino == BTRFS_FIRST_FREE_OBJECTID)
 		up_read(&fs_info->subvol_sem);
 
-	ASSERT(list_empty(&ctx_root.list));
-	ASSERT(list_empty(&ctx_dest.list));
-
 	return ret;
 }
 
@@ -9100,11 +9037,9 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 	struct inode *old_inode = d_inode(old_dentry);
 	u64 index = 0;
 	int ret;
+	int ret2;
 	u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
 	bool log_pinned = false;
-	struct btrfs_log_ctx ctx;
-	bool sync_log = false;
-	bool commit_transaction = false;
 
 	if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
 		return -EPERM;
@@ -9254,17 +9189,8 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		BTRFS_I(old_inode)->dir_index = index;
 
 	if (log_pinned) {
-		struct dentry *parent = new_dentry->d_parent;
-
-		btrfs_init_log_ctx(&ctx, old_inode);
-		ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
-					 BTRFS_I(old_dir), parent,
-					 false, &ctx);
-		if (ret == BTRFS_NEED_LOG_SYNC)
-			sync_log = true;
-		else if (ret == BTRFS_NEED_TRANS_COMMIT)
-			commit_transaction = true;
-		ret = 0;
+		btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
+				   new_dentry->d_parent);
 		btrfs_end_log_trans(root);
 		log_pinned = false;
 	}
@@ -9301,23 +9227,8 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		btrfs_end_log_trans(root);
 		log_pinned = false;
 	}
-	if (!ret && sync_log) {
-		ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
-		if (ret)
-			commit_transaction = true;
-	} else if (sync_log) {
-		mutex_lock(&root->log_mutex);
-		list_del(&ctx.list);
-		mutex_unlock(&root->log_mutex);
-	}
-	if (commit_transaction) {
-		ret = btrfs_commit_transaction(trans);
-	} else {
-		int ret2;
-
-		ret2 = btrfs_end_transaction(trans);
-		ret = ret ? ret : ret2;
-	}
+	ret2 = btrfs_end_transaction(trans);
+	ret = ret ? ret : ret2;
 out_notrans:
 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
 		up_read(&fs_info->subvol_sem);
fs/btrfs/tree-log.c +43 -57

129 lines not on this page · raw message

fs/btrfs/tree-log.h +4 -10
index 132e43d29034..ddfc6789d9bf 100644
@@ -16,6 +16,7 @@ struct btrfs_log_ctx {
 	int log_ret;
 	int log_transid;
 	bool log_new_dentries;
+	bool logging_new_name;
 	struct inode *inode;
 	struct list_head list;
 };
@@ -26,6 +27,7 @@ static inline void btrfs_init_log_ctx(struct btrfs_log_ctx *ctx,
 	ctx->log_ret = 0;
 	ctx->log_transid = 0;
 	ctx->log_new_dentries = false;
+	ctx->logging_new_name = false;
 	ctx->inode = inode;
 	INIT_LIST_HEAD(&ctx->list);
 }
@@ -67,16 +69,8 @@ void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
 			     int for_rename);
 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
 				   struct btrfs_inode *dir);
-/* Return values for btrfs_log_new_name() */
-enum {
-	BTRFS_DONT_NEED_TRANS_COMMIT,
-	BTRFS_NEED_TRANS_COMMIT,
-	BTRFS_DONT_NEED_LOG_SYNC,
-	BTRFS_NEED_LOG_SYNC,
-};
-int btrfs_log_new_name(struct btrfs_trans_handle *trans,
+void btrfs_log_new_name(struct btrfs_trans_handle *trans,
 			struct btrfs_inode *inode, struct btrfs_inode *old_dir,
-			struct dentry *parent,
-			bool sync_log, struct btrfs_log_ctx *ctx);
+			struct dentry *parent);
 
 #endif
Signature · 1 line
2.26.2
#1 lore raw
Quoted 92 lines · On 8/11/20 7:43 AM, [email protected] wrote:

From: Filipe Manana <[email protected]>

Since commit d4682ba03ef618 ("Btrfs: sync log after logging new name") we started to commit logs, and fallback to transaction commits when we failed to log the new names or commit the logs, after link and rename operations when the target inodes (or their parents) were previously logged in the current transaction. This was to avoid losing directories despite an explicit fsync on them when they are ancestors of some inode that got a new named logged, due to a link or rename operation. However that adds the cost of starting IO and waiting for it to complete, which can cause higher latencies for applications.

Instead of doing that, just make sure that when we log a new name for an inode we don't mark any of its ancestors as logged, so that if any one does an fsync against any of them, without doing any other change on them, the fsync commits the log. This way we only pay the cost of a log commit (or a transaction commit if something goes wrong or a new block group was created) if the application explicitly asks to fsync any of the parent directories.

Using dbench, which mixes several filesystems operations including renames, revealed some significant latency gains. The following script that uses dbench was used to test this:

#!/bin/bash

DEV=/dev/nvme0n1
MNT=/mnt/btrfs
MOUNT_OPTIONS="-o ssd -o space_cache=v2"
MKFS_OPTIONS="-m single -d single"
THREADS=16

echo "performance" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor mkfs.btrfs -f $MKFS_OPTIONS $DEV
mount $MOUNT_OPTIONS $DEV $MNT

dbench -t 300 -D $MNT $THREADS

umount $MNT

The test was run on bare metal, no virtualization, on a box with 12 cores (Intel i7-8700), 64Gb of RAM and using a NVMe device, with a kernel configuration that is the default of typical distributions (debian in this case), without debug options enabled (kasan, kmemleak, slub debug, debug of page allocations, lock debugging, etc).

Results before this patch:

Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 10750455 0.011 155.088

  Close        7896674     0.001     0.243
  Rename        455222     2.158  1101.947
  Unlink       2171189     0.067   121.638
  Deltree          256     2.425     7.816
  Mkdir            128     0.002     0.003
  Qpathinfo    9744323     0.006    21.370
  Qfileinfo    1707092     0.001     0.146
  Qfsinfo      1786756     0.001    11.228
  Sfileinfo     875612     0.003    21.263
  Find         3767281     0.025     9.617
  WriteX       5356924     0.011   211.390
  ReadX        16852694     0.003    9.442
  LockX          35008     0.002     0.119
  UnlockX        35008     0.001     0.138

Flush 753458 4.252 1102.249

Throughput 1128.35 MB/sec 16 clients 16 procs max_latency=1102.255 ms

Results after this patch:

16 clients, after

Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 11471098 0.012 448.281

  Close        8426396     0.001     0.925
  Rename        485746     0.123   267.183
  Unlink       2316477     0.080    63.433
  Deltree          288     2.830    11.144
  Mkdir            144     0.003     0.010
  Qpathinfo    10397420     0.006    10.288
  Qfileinfo    1822039     0.001     0.169
  Qfsinfo      1906497     0.002    14.039
  Sfileinfo     934433     0.004     2.438
  Find         4019879     0.026    10.200
  WriteX       5718932     0.011   200.985
  ReadX        17981671     0.003    10.036
  LockX          37352     0.002     0.076
  UnlockX        37352     0.001     0.109

Flush 804018 5.015 778.033

Quoted 7 lines

Throughput 1201.98 MB/sec 16 clients 16 procs max_latency=778.036 ms (+6.5% throughput, -29.4% max latency, -75.8% rename latency)

Test case generic/498 from fstests tests the scenario that the previously mentioned commit fixed.

Man this took me a while to grok, but I think I've got it. The only thing is this patch doesn't apply to current (as of today) misc-next. Thanks,

Josef

#2 lore raw
Quoted 97 lines · Josef Bacik
Quoted 95 lines · On 8/11/20 7:43 AM, [email protected] wrote:

From: Filipe Manana <[email protected]>

Since commit d4682ba03ef618 ("Btrfs: sync log after logging new name") we started to commit logs, and fallback to transaction commits when we failed to log the new names or commit the logs, after link and rename operations when the target inodes (or their parents) were previously logged in the current transaction. This was to avoid losing directories despite an explicit fsync on them when they are ancestors of some inode that got a new named logged, due to a link or rename operation. However that adds the cost of starting IO and waiting for it to complete, which can cause higher latencies for applications.

Instead of doing that, just make sure that when we log a new name for an inode we don't mark any of its ancestors as logged, so that if any one does an fsync against any of them, without doing any other change on them, the fsync commits the log. This way we only pay the cost of a log commit (or a transaction commit if something goes wrong or a new block group was created) if the application explicitly asks to fsync any of the parent directories.

Using dbench, which mixes several filesystems operations including renames, revealed some significant latency gains. The following script that uses dbench was used to test this:

#!/bin/bash

DEV=/dev/nvme0n1
MNT=/mnt/btrfs
MOUNT_OPTIONS="-o ssd -o space_cache=v2"
MKFS_OPTIONS="-m single -d single"
THREADS=16

echo "performance" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor mkfs.btrfs -f $MKFS_OPTIONS $DEV
mount $MOUNT_OPTIONS $DEV $MNT

dbench -t 300 -D $MNT $THREADS

umount $MNT

The test was run on bare metal, no virtualization, on a box with 12 cores (Intel i7-8700), 64Gb of RAM and using a NVMe device, with a kernel configuration that is the default of typical distributions (debian in this case), without debug options enabled (kasan, kmemleak, slub debug, debug of page allocations, lock debugging, etc).

Results before this patch:

Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 10750455 0.011 155.088

  Close        7896674     0.001     0.243
  Rename        455222     2.158  1101.947
  Unlink       2171189     0.067   121.638
  Deltree          256     2.425     7.816
  Mkdir            128     0.002     0.003
  Qpathinfo    9744323     0.006    21.370
  Qfileinfo    1707092     0.001     0.146
  Qfsinfo      1786756     0.001    11.228
  Sfileinfo     875612     0.003    21.263
  Find         3767281     0.025     9.617
  WriteX       5356924     0.011   211.390
  ReadX        16852694     0.003    9.442
  LockX          35008     0.002     0.119
  UnlockX        35008     0.001     0.138

Flush 753458 4.252 1102.249

Throughput 1128.35 MB/sec 16 clients 16 procs max_latency=1102.255 ms

Results after this patch:

16 clients, after

Operation Count AvgLat MaxLat
----------------------------------------
NTCreateX 11471098 0.012 448.281

  Close        8426396     0.001     0.925
  Rename        485746     0.123   267.183
  Unlink       2316477     0.080    63.433
  Deltree          288     2.830    11.144
  Mkdir            144     0.003     0.010
  Qpathinfo    10397420     0.006    10.288
  Qfileinfo    1822039     0.001     0.169
  Qfsinfo      1906497     0.002    14.039
  Sfileinfo     934433     0.004     2.438
  Find         4019879     0.026    10.200
  WriteX       5718932     0.011   200.985
  ReadX        17981671     0.003    10.036
  LockX          37352     0.002     0.076
  UnlockX        37352     0.001     0.109

Flush 804018 5.015 778.033

Throughput 1201.98 MB/sec 16 clients 16 procs max_latency=778.036 ms (+6.5% throughput, -29.4% max latency, -75.8% rename latency)

Quoted 7 lines
Quoted 4 lines

Test case generic/498 from fstests tests the scenario that the previously mentioned commit fixed.

Man this took me a while to grok, but I think I've got it. The only thing is this patch doesn't apply to current (as of today) misc-next. Thanks,

It was based on misc-next from last week.
It conflicts with current misc-next due to a trivial patch that fixes typos and grammar on comments:

https://github.com/kdave/btrfs-devel/commit/f993ba65484d364e30d7aee0293afafadf565f25

I can resend if needed.

Thanks.

Quoted 2 lines

Josef