Skip to contentSkip to conversation

ovl: improving copy-up efficiency for sparse file

PATCH Superseded Chengguang Xu 2 participants 4 messages Oct 4, 2019 – Oct 16, 2019 Overlay filesystem

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ovl: improving copy-up efficiency for sparse file

Current copy-up is not efficient for sparse file, It's not only slow but also wasting more disk space when the target lower file has huge hole inside. This patch tries to recognize file hole and skip it during copy-up.

In detail, this optimization checks the hole according to copy-up chunk size so it may not recognize all kind of holes in the file. However, it is easy to implement and will be enough for most of the time.

Additionally, this optimization relies on lseek(2) SEEK_DATA implementation, so for some specific filesystems which do not support this feature will behave as before on copy-up.

---
 fs/overlayfs/copy_up.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/overlayfs/copy_up.c +14 -1
index b801c6353100..028033c9f021 100644
@@ -144,10 +144,11 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
 		goto out;
 	/* Couldn't clone, so now we try to copy the data */
 
-	/* FIXME: copy up sparse files efficiently */
 	while (len) {
 		size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
 		long bytes;
+		loff_t old_next_data_pos;
+		loff_t hole_len;
 
 		if (len < this_len)
 			this_len = len;
@@ -157,6 +158,18 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
 			break;
 		}
 
+		old_next_data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
+		if (old_next_data_pos >= old_pos + OVL_COPY_UP_CHUNK_SIZE) {
+			hole_len = (old_next_data_pos - old_pos) /
+				OVL_COPY_UP_CHUNK_SIZE * OVL_COPY_UP_CHUNK_SIZE;
+			old_pos += hole_len;
+			new_pos += hole_len;
+			len -= hole_len;
+			continue;
+		} else if (old_next_data_pos == -ENXIO) {
+			break;
+		}
+
 		bytes = do_splice_direct(old_file, &old_pos,
 					 new_file, &new_pos,
 					 this_len, SPLICE_F_MOVE);
Signature · 1 line
2.21.0
#1 lore raw

---- 在 星期五, 2019-10-04 21:20:30 Chengguang Xu <[email protected]> 撰写 ----

Quoted 11 lines

Current copy-up is not efficient for sparse file, It's not only slow but also wasting more disk space when the target lower file has huge hole inside. This patch tries to recognize file hole and skip it during copy-up.

In detail, this optimization checks the hole according to copy-up chunk size so it may not recognize all kind of holes in the file. However, it is easy to implement and will be enough for most of the time.

Quoted 7 lines

Additionally, this optimization relies on lseek(2) SEEK_DATA implementation, so for some specific filesystems which do not support this feature will behave as before on copy-up.

---

Any better idea or suggestion for this?

Thanks,
Chengguang

Quoted 44 lines
 fs/overlayfs/copy_up.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/overlayfs/copy_up.c +14 -1
index b801c6353100..028033c9f021 100644
@@ -144,10 +144,11 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
         goto out;
     /* Couldn't clone, so now we try to copy the data */
 
-    /* FIXME: copy up sparse files efficiently */
     while (len) {
         size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
         long bytes;
+        loff_t old_next_data_pos;
+        loff_t hole_len;
 
         if (len < this_len)
             this_len = len;
@@ -157,6 +158,18 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
             break;
         }
 
+        old_next_data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
+        if (old_next_data_pos >= old_pos + OVL_COPY_UP_CHUNK_SIZE) {
+            hole_len = (old_next_data_pos - old_pos) /
+                OVL_COPY_UP_CHUNK_SIZE * OVL_COPY_UP_CHUNK_SIZE;
+            old_pos += hole_len;
+            new_pos += hole_len;
+            len -= hole_len;
+            continue;
+        } else if (old_next_data_pos == -ENXIO) {
+            break;
+        }
+
         bytes = do_splice_direct(old_file, &old_pos,
                      new_file, &new_pos,
                      this_len, SPLICE_F_MOVE);
Signature · 1 line
2.21.0
maintainer
#2 lore raw
Quoted 12 lines · Chengguang Xu

---- 在 星期五, 2019-10-04 21:20:30 Chengguang Xu <[email protected]> 撰写 ----

Quoted 10 lines

Current copy-up is not efficient for sparse file, It's not only slow but also wasting more disk space when the target lower file has huge hole inside. This patch tries to recognize file hole and skip it during copy-up.

In detail, this optimization checks the hole according to copy-up chunk size so it may not recognize all kind of holes in the file. However, it is easy to implement and will be enough for most of the time.

I must say I do not see how aligning to copy-up chunk size simplifies the change. Why is that more complicated?

if (old_next_data_pos >= old_pos) {
      hole_len = old_next_data_pos - old_pos;

...

It can still copy hole up to this_len, because there is no SEEK_HOLE, so you can document that.

Quoted 6 lines
Quoted 6 lines

Additionally, this optimization relies on lseek(2) SEEK_DATA implementation, so for some specific filesystems which do not support this feature will behave as before on copy-up.

I am not sure if we support any lower fs with no f_op->llseek in that case, copy up will not behave as before - it will return -ESPIPE and will be a regression.

Quoted 4 lines
Quoted 2 lines

---

Any better idea or suggestion for this?

This change should be accompanied with proper xfstests examining all sorts of sparse files.
See overlay/001 and _run_seek_sanity_test for inspiration.

Perhaps you can run all _run_seek_sanity_test tests on lower fs, then mount overlay. copy up all the sanity test files and then check something???

Thanks,
Amir.

Quoted 42 lines
Quoted 42 lines

 fs/overlayfs/copy_up.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/overlayfs/copy_up.c +14 -2
index b801c6353100..028033c9f021 100644
@@ -144,10 +144,11 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
         goto out;
     /* Couldn't clone, so now we try to copy the data */
 
-    /* FIXME: copy up sparse files efficiently */
     while (len) {
         size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
         long bytes;
+        loff_t old_next_data_pos;
+        loff_t hole_len;
 
         if (len < this_len)
             this_len = len;
@@ -157,6 +158,18 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
             break;
         }
 
+        old_next_data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
+        if (old_next_data_pos >= old_pos + OVL_COPY_UP_CHUNK_SIZE) {
+            hole_len = (old_next_data_pos - old_pos) /
+                OVL_COPY_UP_CHUNK_SIZE * OVL_COPY_UP_CHUNK_SIZE;
+            old_pos += hole_len;
+            new_pos += hole_len;
+            len -= hole_len;
+            continue;
+        } else if (old_next_data_pos == -ENXIO) {
+            break;
+        }
+
         bytes = do_splice_direct(old_file, &old_pos,
                      new_file, &new_pos,
                      this_len, SPLICE_F_MOVE);
--
#3 lore raw

---- 在 星期二, 2019-10-15 22:26:35 Amir Goldstein <[email protected]> 撰写 ----

Quoted 9 lines · Chengguang Xu
Quoted 8 lines

---- 在 星期五, 2019-10-04 21:20:30 Chengguang Xu <[email protected]> 撰写 ----

Quoted 6 lines

Current copy-up is not efficient for sparse file, It's not only slow but also wasting more disk space when the target lower file has huge hole inside. This patch tries to recognize file hole and skip it during copy-up.

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

In detail, this optimization checks the hole according to copy-up chunk size so it may not recognize all kind of holes in the file. However, it is easy to implement and will be enough for most of the time.

I must say I do not see how aligning to copy-up chunk size simplifies the change. Why is that more complicated?

Hi Amir,

Thanks for your feedback.
I would like to say if we do not align to copy-up chunk size then we have to recognize exact size of every hole in the lower file and that may need relying on both SEEK_HOLE and SEEK_DATA methods. However, currently SEEK_HOLE implementation seems not so reliable. Look deep into the code, generic_file_llseek() could not work correctly for SEEK_HOLE in our check, so it means for filesystems which don't have their own version of f_op->llseek function will get problem.

Quoted 7 lines
if (old_next_data_pos >= old_pos) {
      hole_len = old_next_data_pos - old_pos;

...

It can still copy hole up to this_len, because there is no SEEK_HOLE, so you can document that.

I'll do.

Quoted 11 lines
Quoted 7 lines
Quoted 7 lines

Additionally, this optimization relies on lseek(2) SEEK_DATA implementation, so for some specific filesystems which do not support this feature will behave as before on copy-up.

I am not sure if we support any lower fs with no f_op->llseek in that case, copy up will not behave as before - it will return -ESPIPE and will be a regression.

We need adding a check for lower fs llseek support and catching llseek error for safety. If we notice the optimization could not work then we back to original copy-up behavior.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

---

Any better idea or suggestion for this?

Quoted 8 lines

This change should be accompanied with proper xfstests examining all sorts of sparse files.
See overlay/001 and _run_seek_sanity_test for inspiration.

Perhaps you can run all _run_seek_sanity_test tests on lower fs, then mount overlay. copy up all the sanity test files and then check something???

Yeah, thanks for your suggestion, I'll do more work on testing.

Thanks,
Chengguang

Quoted 45 lines
Quoted 44 lines
Quoted 44 lines



 fs/overlayfs/copy_up.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/overlayfs/copy_up.c +14 -2
index b801c6353100..028033c9f021 100644
@@ -144,10 +144,11 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
         goto out;
     /* Couldn't clone, so now we try to copy the data */
 
-    /* FIXME: copy up sparse files efficiently */
     while (len) {
         size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
         long bytes;
+        loff_t old_next_data_pos;
+        loff_t hole_len;
 
         if (len < this_len)
             this_len = len;
@@ -157,6 +158,18 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
             break;
         }
 
+        old_next_data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
+        if (old_next_data_pos >= old_pos + OVL_COPY_UP_CHUNK_SIZE) {
+            hole_len = (old_next_data_pos - old_pos) /
+                OVL_COPY_UP_CHUNK_SIZE * OVL_COPY_UP_CHUNK_SIZE;
+            old_pos += hole_len;
+            new_pos += hole_len;
+            len -= hole_len;
+            continue;
+        } else if (old_next_data_pos == -ENXIO) {
+            break;
+        }
+
         bytes = do_splice_direct(old_file, &old_pos,
                      new_file, &new_pos,
                      this_len, SPLICE_F_MOVE);
--