add patch fix-sleep-in-atomic-context-in-grab_mapping_entry
[ext4-patch-queue.git] / factor-out-checks-from-ext4_file_write_iter
blobf17aaa10bbb1da86dbe2898f156ac4f8a4110cc3
1 ext4: factor out checks from ext4_file_write_iter()
3 From: Jan Kara <jack@suse.cz>
5 Factor out checks of 'from' and whether we are overwriting out of
6 ext4_file_write_iter() so that the function is easier to follow.
8 Reviewed-by: Ross Zwisler <ross.zwisler@linux.intel.com>
9 Signed-off-by: Jan Kara <jack@suse.cz>
10 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
11 ---
12  fs/ext4/file.c | 97 ++++++++++++++++++++++++++++++----------------------------
13  1 file changed, 50 insertions(+), 47 deletions(-)
15 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
16 index 2a822d30e73f..9facb4dc5c70 100644
17 --- a/fs/ext4/file.c
18 +++ b/fs/ext4/file.c
19 @@ -88,6 +88,51 @@ ext4_unaligned_aio(struct inode *inode, struct iov_iter *from, loff_t pos)
20         return 0;
21  }
23 +/* Is IO overwriting allocated and initialized blocks? */
24 +static bool ext4_overwrite_io(struct inode *inode, loff_t pos, loff_t len)
26 +       struct ext4_map_blocks map;
27 +       unsigned int blkbits = inode->i_blkbits;
28 +       int err, blklen;
30 +       if (pos + len > i_size_read(inode))
31 +               return false;
33 +       map.m_lblk = pos >> blkbits;
34 +       map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
35 +       blklen = map.m_len;
37 +       err = ext4_map_blocks(NULL, inode, &map, 0);
38 +       /*
39 +        * 'err==len' means that all of the blocks have been preallocated,
40 +        * regardless of whether they have been initialized or not. To exclude
41 +        * unwritten extents, we need to check m_flags.
42 +        */
43 +       return err == blklen && (map.m_flags & EXT4_MAP_MAPPED);
46 +static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
48 +       struct inode *inode = file_inode(iocb->ki_filp);
49 +       ssize_t ret;
51 +       ret = generic_write_checks(iocb, from);
52 +       if (ret <= 0)
53 +               return ret;
54 +       /*
55 +        * If we have encountered a bitmap-format file, the size limit
56 +        * is smaller than s_maxbytes, which is for extent-mapped files.
57 +        */
58 +       if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
59 +               struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
61 +               if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
62 +                       return -EFBIG;
63 +               iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
64 +       }
65 +       return iov_iter_count(from);
68  static ssize_t
69  ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
70  {
71 @@ -98,7 +143,7 @@ ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
72         ssize_t ret;
74         inode_lock(inode);
75 -       ret = generic_write_checks(iocb, from);
76 +       ret = ext4_write_checks(iocb, from);
77         if (ret <= 0)
78                 goto out;
80 @@ -114,53 +159,11 @@ ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
81                 ext4_unwritten_wait(inode);
82         }
84 -       /*
85 -        * If we have encountered a bitmap-format file, the size limit
86 -        * is smaller than s_maxbytes, which is for extent-mapped files.
87 -        */
88 -       if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
89 -               struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
91 -               if (iocb->ki_pos >= sbi->s_bitmap_maxbytes) {
92 -                       ret = -EFBIG;
93 -                       goto out;
94 -               }
95 -               iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
96 -       }
98         iocb->private = &overwrite;
99 -       if (o_direct) {
100 -               size_t length = iov_iter_count(from);
101 -               loff_t pos = iocb->ki_pos;
103 -               /* check whether we do a DIO overwrite or not */
104 -               if (ext4_should_dioread_nolock(inode) && !unaligned_aio &&
105 -                   pos + length <= i_size_read(inode)) {
106 -                       struct ext4_map_blocks map;
107 -                       unsigned int blkbits = inode->i_blkbits;
108 -                       int err, len;
110 -                       map.m_lblk = pos >> blkbits;
111 -                       map.m_len = EXT4_MAX_BLOCKS(length, pos, blkbits);
112 -                       len = map.m_len;
114 -                       err = ext4_map_blocks(NULL, inode, &map, 0);
115 -                       /*
116 -                        * 'err==len' means that all of blocks has
117 -                        * been preallocated no matter they are
118 -                        * initialized or not.  For excluding
119 -                        * unwritten extents, we need to check
120 -                        * m_flags.  There are two conditions that
121 -                        * indicate for initialized extents.  1) If we
122 -                        * hit extent cache, EXT4_MAP_MAPPED flag is
123 -                        * returned; 2) If we do a real lookup,
124 -                        * non-flags are returned.  So we should check
125 -                        * these two conditions.
126 -                        */
127 -                       if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
128 -                               overwrite = 1;
129 -               }
130 -       }
131 +       /* Check whether we do a DIO overwrite or not */
132 +       if (o_direct && ext4_should_dioread_nolock(inode) && !unaligned_aio &&
133 +           ext4_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from)))
134 +               overwrite = 1;
136         ret = __generic_file_write_iter(iocb, from);
137         inode_unlock(inode);
138 -- 
139 2.6.6