fs/ntfs3: Fixing work with sparse clusters
authorKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
Fri, 7 Oct 2022 10:57:28 +0000 (13:57 +0300)
committerKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
Mon, 14 Nov 2022 16:50:43 +0000 (19:50 +0300)
Simplify logic in ntfs_extend_initialized_size, ntfs_sparse_cluster
and ntfs_fallocate.

Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
fs/ntfs3/file.c
fs/ntfs3/inode.c

index 4f2ffc7ef296f1f7a8bb22a330b4321a0e6fbcc9..96ba3f5a8470ee7d0789119f227833303cd2a549 100644 (file)
@@ -128,25 +128,9 @@ static int ntfs_extend_initialized_size(struct file *file,
                                goto out;
 
                        if (lcn == SPARSE_LCN) {
-                               loff_t vbo = (loff_t)vcn << bits;
-                               loff_t to = vbo + ((loff_t)clen << bits);
-
-                               if (to <= new_valid) {
-                                       ni->i_valid = to;
-                                       pos = to;
-                                       goto next;
-                               }
-
-                               if (vbo < pos) {
-                                       pos = vbo;
-                               } else {
-                                       to = (new_valid >> bits) << bits;
-                                       if (pos < to) {
-                                               ni->i_valid = to;
-                                               pos = to;
-                                               goto next;
-                                       }
-                               }
+                               pos = ((loff_t)clen + vcn) << bits;
+                               ni->i_valid = pos;
+                               goto next;
                        }
                }
 
@@ -279,8 +263,9 @@ void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
 {
        struct address_space *mapping = inode->i_mapping;
        struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
-       u64 vbo = (u64)vcn << sbi->cluster_bits;
-       u64 bytes = (u64)len << sbi->cluster_bits;
+       u8 cluster_bits = sbi->cluster_bits;
+       u64 vbo = (u64)vcn << cluster_bits;
+       u64 bytes = (u64)len << cluster_bits;
        u32 blocksize = 1 << inode->i_blkbits;
        pgoff_t idx0 = page0 ? page0->index : -1;
        loff_t vbo_clst = vbo & sbi->cluster_mask_inv;
@@ -329,11 +314,10 @@ void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
 
                zero_user_segment(page, from, to);
 
-               if (!partial) {
-                       if (!PageUptodate(page))
-                               SetPageUptodate(page);
-                       set_page_dirty(page);
-               }
+               if (!partial)
+                       SetPageUptodate(page);
+               flush_dcache_page(page);
+               set_page_dirty(page);
 
                if (idx != idx0) {
                        unlock_page(page);
@@ -341,7 +325,6 @@ void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
                }
                cond_resched();
        }
-       mark_inode_dirty(inode);
 }
 
 /*
@@ -588,11 +571,7 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
                u32 frame_size;
                loff_t mask, vbo_a, end_a, tmp;
 
-               err = filemap_write_and_wait_range(mapping, vbo, end - 1);
-               if (err)
-                       goto out;
-
-               err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
+               err = filemap_write_and_wait_range(mapping, vbo, LLONG_MAX);
                if (err)
                        goto out;
 
@@ -693,7 +672,7 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
                        goto out;
 
                if (is_supported_holes) {
-                       CLST vcn_v = ni->i_valid >> sbi->cluster_bits;
+                       CLST vcn_v = bytes_to_cluster(sbi, ni->i_valid);
                        CLST vcn = vbo >> sbi->cluster_bits;
                        CLST cend = bytes_to_cluster(sbi, end);
                        CLST lcn, clen;
index 763dd982a43a71005cb65797eb288ec4964076a0..d8d00ffe7a1f72dc9dc44a6c8a453ffe57dc1332 100644 (file)
@@ -646,7 +646,12 @@ static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
                        bh->b_size = block_size;
                        off = vbo & (PAGE_SIZE - 1);
                        set_bh_page(bh, page, off);
-                       ll_rw_block(REQ_OP_READ, 1, &bh);
+
+                       lock_buffer(bh);
+                       bh->b_end_io = end_buffer_read_sync;
+                       get_bh(bh);
+                       submit_bh(REQ_OP_READ, bh);
+
                        wait_on_buffer(bh);
                        if (!buffer_uptodate(bh)) {
                                err = -EIO;