Skip to contentSkip to conversation

create_inode: fix copying large files

PATCH Superseded Ross Burton 2 participants 3 messages Feb 1, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 create_inode: fix copying large files

When copying large files into a ext filesystem at mkfs time the copy fails at 2^31 bytes in. There are two problems:

copy_file_chunk() passes an offset (off_t, 64-bit typically) to ext2fs_file_lseek() which expects a ext2_off_t (typedef to __u32) so the value is truncated. Solve by calling ext2fs_file_llseek() which takes a u64 offset instead.

try_lseek_copy() rounds the data and hole offsets as found by lseek() to block boundaries, but the calculation gets truncated to 32-bits. Solve by casting the 32-bit blocksize to off_t to ensure this doesn't happen.

---
 misc/create_inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
misc/create_inode.c +2 -2
index 05aa6363..f106dcda 100644
@@ -438,7 +438,7 @@ static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
 				ptr += blen;
 				continue;
 			}
-			err = ext2fs_file_lseek(e2_file, off + bpos,
+			err = ext2fs_file_llseek(e2_file, off + bpos,
 						EXT2_SEEK_SET, NULL);
 			if (err)
 				goto fail;
@@ -481,7 +481,7 @@ static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
 			return EXT2_ET_UNIMPLEMENTED;
 
 		data_blk = data & ~(fs->blocksize - 1);
-		hole_blk = (hole + (fs->blocksize - 1)) & ~(fs->blocksize - 1);
+		hole_blk = (hole + (off_t)(fs->blocksize - 1)) & ~(off_t)(fs->blocksize - 1);
 		err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
 				      zerobuf);
 		if (err)
Signature · 1 line
2.11.0
#1 lore raw
Quoted 31 lines · Ross Burton

When copying large files into a ext filesystem at mkfs time the copy fails at 2^31 bytes in. There are two problems:

copy_file_chunk() passes an offset (off_t, 64-bit typically) to ext2fs_file_lseek() which expects a ext2_off_t (typedef to __u32) so the value is truncated. Solve by calling ext2fs_file_llseek() which takes a u64 offset instead.

try_lseek_copy() rounds the data and hole offsets as found by lseek() to block boundaries, but the calculation gets truncated to 32-bits. Solve by casting the 32-bit blocksize to off_t to ensure this doesn't happen.

---
 misc/create_inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
misc/create_inode.c +1 -1
index 05aa6363..f106dcda 100644
@@ -438,7 +438,7 @@ static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
 				ptr += blen;
 				continue;
 			}
-			err = ext2fs_file_lseek(e2_file, off + bpos,
+			err = ext2fs_file_llseek(e2_file, off + bpos,
 						EXT2_SEEK_SET, NULL);
 			if (err)
 				goto fail;

misc/create_inode.c static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf, · line 483

Quoted 3 lines
misc/create_inode.c +0 -0
@@ -481,7 +481,7 @@ static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
 			return EXT2_ET_UNIMPLEMENTED;
 
 		data_blk = data & ~(fs->blocksize - 1);

Do we need to cast this one too? ^^^^^^^

(Looks reasonable otherwise...)

--D

Quoted 8 lines
misc/create_inode.c +1 -1
-		hole_blk = (hole + (fs->blocksize - 1)) & ~(fs->blocksize - 1);
+		hole_blk = (hole + (off_t)(fs->blocksize - 1)) & ~(off_t)(fs->blocksize - 1);
 		err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
 				      zerobuf);
 		if (err)
Signature · 1 line
2.11.0
#2 lore raw

Yeah, you're right, thanks for the review.

Ross

Quoted 51 lines · Darrick J. Wong
Quoted 31 lines · Ross Burton

When copying large files into a ext filesystem at mkfs time the copy fails at 2^31 bytes in. There are two problems:

copy_file_chunk() passes an offset (off_t, 64-bit typically) to ext2fs_file_lseek() which expects a ext2_off_t (typedef to __u32) so the value is truncated. Solve by calling ext2fs_file_llseek() which takes a u64 offset instead.

try_lseek_copy() rounds the data and hole offsets as found by lseek() to block boundaries, but the calculation gets truncated to 32-bits. Solve by casting the 32-bit blocksize to off_t to ensure this doesn't happen.

---
 misc/create_inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
misc/create_inode.c +1 -1
index 05aa6363..f106dcda 100644
@@ -438,7 +438,7 @@ static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
                              ptr += blen;
                              continue;
                      }
-                     err = ext2fs_file_lseek(e2_file, off + bpos,
+                     err = ext2fs_file_llseek(e2_file, off + bpos,
                                              EXT2_SEEK_SET, NULL);
                      if (err)
                              goto fail;

misc/create_inode.c static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf, · line 483

Quoted 3 lines
misc/create_inode.c +0 -0
@@ -481,7 +481,7 @@ static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
                      return EXT2_ET_UNIMPLEMENTED;
 
              data_blk = data & ~(fs->blocksize - 1);

Do we need to cast this one too? ^^^^^^^

(Looks reasonable otherwise...)

--D

Quoted 8 lines
misc/create_inode.c +1 -2
-             hole_blk = (hole + (fs->blocksize - 1)) & ~(fs->blocksize - 1);
+             hole_blk = (hole + (off_t)(fs->blocksize - 1)) & ~(off_t)(fs->blocksize - 1);
              err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
                                    zerobuf);
              if (err)
--

2.11.0