add patch return-EFSBADCRC-on-csum-error-in-ext4_find_entry
[ext4-patch-queue.git] / improve-journal-credit-handling-in-set-xattr-paths
blob2b06bcfa22db5a8e016774c322d0887f97624cc5
1 ext4: improve journal credit handling in set xattr paths
3 From: Tahsin Erdogan <tahsin@google.com>
5 Both ext4_set_acl() and ext4_set_context() need to be made aware of
6 ea_inode feature when it comes to credits calculation.
8 Also add a sufficient credits check in ext4_xattr_set_handle() right
9 after xattr write lock is grabbed. Original credits calculation is done
10 outside the lock so there is a possiblity that the initially calculated
11 credits are not sufficient anymore.
13 Signed-off-by: Tahsin Erdogan <tahsin@google.com>
14 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
15 ---
16 v2: fixed checkpatch.pl warning about replacing spaces with tab
18  fs/ext4/acl.c       |  7 ++++---
19  fs/ext4/ext4_jbd2.h | 14 --------------
20  fs/ext4/super.c     |  6 +++---
21  fs/ext4/xattr.c     | 55 +++++++++++++++++++++++++++++++++++++++++------------
22  fs/ext4/xattr.h     |  1 +
23  5 files changed, 51 insertions(+), 32 deletions(-)
25 diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
26 index 3ec0e46de95f..74f7ac539e00 100644
27 --- a/fs/ext4/acl.c
28 +++ b/fs/ext4/acl.c
29 @@ -231,14 +231,15 @@ int
30  ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
31  {
32         handle_t *handle;
33 -       int error, retries = 0;
34 +       int error, credits, retries = 0;
35 +       size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
37         error = dquot_initialize(inode);
38         if (error)
39                 return error;
40  retry:
41 -       handle = ext4_journal_start(inode, EXT4_HT_XATTR,
42 -                                   ext4_jbd2_credits_xattr(inode));
43 +       credits = ext4_xattr_set_credits(inode, acl_size);
44 +       handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
45         if (IS_ERR(handle))
46                 return PTR_ERR(handle);
48 diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
49 index f97611171023..a5bda70feed5 100644
50 --- a/fs/ext4/ext4_jbd2.h
51 +++ b/fs/ext4/ext4_jbd2.h
52 @@ -104,20 +104,6 @@
53  #define EXT4_MAXQUOTAS_INIT_BLOCKS(sb) (EXT4_MAXQUOTAS*EXT4_QUOTA_INIT_BLOCKS(sb))
54  #define EXT4_MAXQUOTAS_DEL_BLOCKS(sb) (EXT4_MAXQUOTAS*EXT4_QUOTA_DEL_BLOCKS(sb))
56 -static inline int ext4_jbd2_credits_xattr(struct inode *inode)
58 -       int credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb);
60 -       /*
61 -        * In case of inline data, we may push out the data to a block,
62 -        * so we need to reserve credits for this eventuality
63 -        */
64 -       if (ext4_has_inline_data(inode))
65 -               credits += ext4_writepage_trans_blocks(inode) + 1;
66 -       return credits;
70  /*
71   * Ext4 handle operation types -- for logging purposes
72   */
73 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
74 index d37c81f327e7..b02a23ec92ca 100644
75 --- a/fs/ext4/super.c
76 +++ b/fs/ext4/super.c
77 @@ -1143,7 +1143,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
78                                                         void *fs_data)
79  {
80         handle_t *handle = fs_data;
81 -       int res, res2, retries = 0;
82 +       int res, res2, credits, retries = 0;
84         res = ext4_convert_inline_data(inode);
85         if (res)
86 @@ -1178,8 +1178,8 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
87         if (res)
88                 return res;
89  retry:
90 -       handle = ext4_journal_start(inode, EXT4_HT_MISC,
91 -                       ext4_jbd2_credits_xattr(inode));
92 +       credits = ext4_xattr_set_credits(inode, len);
93 +       handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
94         if (IS_ERR(handle))
95                 return PTR_ERR(handle);
97 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
98 index 97d33ecf0818..fd017faaf221 100644
99 --- a/fs/ext4/xattr.c
100 +++ b/fs/ext4/xattr.c
101 @@ -1473,6 +1473,17 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
103         ext4_write_lock_xattr(inode, &no_expand);
105 +       /* Check journal credits under write lock. */
106 +       if (ext4_handle_valid(handle)) {
107 +               int credits;
109 +               credits = ext4_xattr_set_credits(inode, value_len);
110 +               if (!ext4_handle_has_enough_credits(handle, credits)) {
111 +                       error = -ENOSPC;
112 +                       goto cleanup;
113 +               }
114 +       }
116         error = ext4_reserve_inode_write(handle, inode, &is.iloc);
117         if (error)
118                 goto cleanup;
119 @@ -1570,6 +1581,36 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
120         return error;
123 +int ext4_xattr_set_credits(struct inode *inode, size_t value_len)
125 +       struct super_block *sb = inode->i_sb;
126 +       int credits;
128 +       if (!EXT4_SB(sb)->s_journal)
129 +               return 0;
131 +       credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb);
133 +       /*
134 +        * In case of inline data, we may push out the data to a block,
135 +        * so we need to reserve credits for this eventuality
136 +        */
137 +       if (ext4_has_inline_data(inode))
138 +               credits += ext4_writepage_trans_blocks(inode) + 1;
140 +       if (ext4_has_feature_ea_inode(sb)) {
141 +               int nrblocks = (value_len + sb->s_blocksize - 1) >>
142 +                                       sb->s_blocksize_bits;
144 +               /* For new inode */
145 +               credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
147 +               /* For data blocks of EA inode */
148 +               credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
149 +       }
150 +       return credits;
153  /*
154   * ext4_xattr_set()
155   *
156 @@ -1585,24 +1626,14 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
157         handle_t *handle;
158         struct super_block *sb = inode->i_sb;
159         int error, retries = 0;
160 -       int credits = ext4_jbd2_credits_xattr(inode);
161 +       int credits;
163         error = dquot_initialize(inode);
164         if (error)
165                 return error;
167 -       if (ext4_has_feature_ea_inode(sb)) {
168 -               int nrblocks = (value_len + sb->s_blocksize - 1) >>
169 -                                       sb->s_blocksize_bits;
171 -               /* For new inode */
172 -               credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
174 -               /* For data blocks of EA inode */
175 -               credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
176 -       }
178  retry:
179 +       credits = ext4_xattr_set_credits(inode, value_len);
180         handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
181         if (IS_ERR(handle)) {
182                 error = PTR_ERR(handle);
183 diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
184 index b6ef99d1a061..e82c5fe36a26 100644
185 --- a/fs/ext4/xattr.h
186 +++ b/fs/ext4/xattr.h
187 @@ -160,6 +160,7 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t);
188  extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
189  extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
190  extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
191 +extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len);
193  extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
194  extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
195 -- 
196 2.13.1.611.g7e3b11ae1-goog