fs/ntfs3: Fix fiemap + fix shrink file size (to remove preallocated space)
authorKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
Mon, 25 Oct 2021 15:31:28 +0000 (18:31 +0300)
committerKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
Wed, 24 Nov 2021 12:13:26 +0000 (15:13 +0300)
Two problems:
1. ntfs3_setattr can't truncate preallocated space;
2. if allocated fragment "cross" valid size, then fragment splits into two parts:
- normal part;
- unwritten part (here we must return FIEMAP_EXTENT_LAST).
Before this commit we returned FIEMAP_EXTENT_LAST for whole fragment.
Fixes xfstest generic/092
Fixes: 4342306f0f0d ("fs/ntfs3: Add file operations and implementation")
Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
fs/ntfs3/file.c
fs/ntfs3/frecord.c

index 3ac0482c688042ed48f21730df9b24a689407e22..6242708980d0a6f14d4142b688891e2c334c9bd8 100644 (file)
@@ -761,7 +761,7 @@ int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
                }
                inode_dio_wait(inode);
 
-               if (attr->ia_size < oldsize)
+               if (attr->ia_size <= oldsize)
                        err = ntfs_truncate(inode, attr->ia_size);
                else if (attr->ia_size > oldsize)
                        err = ntfs_extend(inode, attr->ia_size, 0, NULL);
index 6f47a9c17f896c62e355db7a6948075a88247378..18842998c8fa3bdfc370fd01da437774e8fd6414 100644 (file)
@@ -1964,10 +1964,8 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
 
                vcn += clen;
 
-               if (vbo + bytes >= end) {
+               if (vbo + bytes >= end)
                        bytes = end - vbo;
-                       flags |= FIEMAP_EXTENT_LAST;
-               }
 
                if (vbo + bytes <= valid) {
                        ;
@@ -1977,6 +1975,9 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
                        /* vbo < valid && valid < vbo + bytes */
                        u64 dlen = valid - vbo;
 
+                       if (vbo + dlen >= end)
+                               flags |= FIEMAP_EXTENT_LAST;
+
                        err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen,
                                                      flags);
                        if (err < 0)
@@ -1995,6 +1996,9 @@ int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
                        flags |= FIEMAP_EXTENT_UNWRITTEN;
                }
 
+               if (vbo + bytes >= end)
+                       flags |= FIEMAP_EXTENT_LAST;
+
                err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags);
                if (err < 0)
                        break;