Skip to content

Commit af65207

Browse files
Tahsin Erdogantytso
authored andcommitted
ext4: fix __ext4_new_inode() journal credits calculation
ea_inode feature allows creating extended attributes that are up to 64k in size. Update __ext4_new_inode() to pick increased credit limits. To avoid overallocating too many journal credits, update __ext4_xattr_set_credits() to make a distinction between xattr create vs update. This helps __ext4_new_inode() because all attributes are known to be new, so we can save credits that are normally needed to delete old values. Also, have fscrypt specify its maximum context size so that we don't end up allocating credits for 64k size. Signed-off-by: Tahsin Erdogan <tahsin@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent ad47f95 commit af65207

7 files changed

Lines changed: 89 additions & 34 deletions

File tree

‎fs/crypto/policy.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
260260
memcpy(ctx.master_key_descriptor, ci->ci_master_key,
261261
FS_KEY_DESCRIPTOR_SIZE);
262262
get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
263+
BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
263264
res = parent->i_sb->s_cop->set_context(child, &ctx,
264265
sizeof(ctx), fs_data);
265266
if (res)

‎fs/ext4/acl.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ ext4_get_acl(struct inode *inode, int type)
183183
*/
184184
static int
185185
__ext4_set_acl(handle_t *handle, struct inode *inode, int type,
186-
struct posix_acl *acl)
186+
struct posix_acl *acl, int xattr_flags)
187187
{
188188
int name_index;
189189
void *value = NULL;
@@ -218,7 +218,7 @@ __ext4_set_acl(handle_t *handle, struct inode *inode, int type,
218218
}
219219

220220
error = ext4_xattr_set_handle(handle, inode, name_index, "",
221-
value, size, 0);
221+
value, size, xattr_flags);
222222

223223
kfree(value);
224224
if (!error)
@@ -238,15 +238,16 @@ ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
238238
if (error)
239239
return error;
240240
retry:
241-
error = ext4_xattr_set_credits(inode, acl_size, &credits);
241+
error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
242+
&credits);
242243
if (error)
243244
return error;
244245

245246
handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
246247
if (IS_ERR(handle))
247248
return PTR_ERR(handle);
248249

249-
error = __ext4_set_acl(handle, inode, type, acl);
250+
error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
250251
ext4_journal_stop(handle);
251252
if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
252253
goto retry;
@@ -271,13 +272,13 @@ ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
271272

272273
if (default_acl) {
273274
error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
274-
default_acl);
275+
default_acl, XATTR_CREATE);
275276
posix_acl_release(default_acl);
276277
}
277278
if (acl) {
278279
if (!error)
279280
error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
280-
acl);
281+
acl, XATTR_CREATE);
281282
posix_acl_release(acl);
282283
}
283284
return error;

‎fs/ext4/ialloc.c‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -766,31 +766,69 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
766766
if (!dir || !dir->i_nlink)
767767
return ERR_PTR(-EPERM);
768768

769-
if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
769+
sb = dir->i_sb;
770+
sbi = EXT4_SB(sb);
771+
772+
if (unlikely(ext4_forced_shutdown(sbi)))
770773
return ERR_PTR(-EIO);
771774

772-
if ((ext4_encrypted_inode(dir) ||
773-
DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) &&
775+
if ((ext4_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
774776
(S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
775777
!(i_flags & EXT4_EA_INODE_FL)) {
776778
err = fscrypt_get_encryption_info(dir);
777779
if (err)
778780
return ERR_PTR(err);
779781
if (!fscrypt_has_encryption_key(dir))
780782
return ERR_PTR(-ENOKEY);
781-
if (!handle)
782-
nblocks += EXT4_DATA_TRANS_BLOCKS(dir->i_sb);
783783
encrypt = 1;
784784
}
785785

786-
sb = dir->i_sb;
786+
if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
787+
#ifdef CONFIG_EXT4_FS_POSIX_ACL
788+
struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
789+
790+
if (p) {
791+
int acl_size = p->a_count * sizeof(ext4_acl_entry);
792+
793+
nblocks += (S_ISDIR(mode) ? 2 : 1) *
794+
__ext4_xattr_set_credits(sb, NULL /* inode */,
795+
NULL /* block_bh */, acl_size,
796+
true /* is_create */);
797+
posix_acl_release(p);
798+
}
799+
#endif
800+
801+
#ifdef CONFIG_SECURITY
802+
{
803+
int num_security_xattrs = 1;
804+
805+
#ifdef CONFIG_INTEGRITY
806+
num_security_xattrs++;
807+
#endif
808+
/*
809+
* We assume that security xattrs are never
810+
* more than 1k. In practice they are under
811+
* 128 bytes.
812+
*/
813+
nblocks += num_security_xattrs *
814+
__ext4_xattr_set_credits(sb, NULL /* inode */,
815+
NULL /* block_bh */, 1024,
816+
true /* is_create */);
817+
}
818+
#endif
819+
if (encrypt)
820+
nblocks += __ext4_xattr_set_credits(sb,
821+
NULL /* inode */, NULL /* block_bh */,
822+
FSCRYPT_SET_CONTEXT_MAX_SIZE,
823+
true /* is_create */);
824+
}
825+
787826
ngroups = ext4_get_groups_count(sb);
788827
trace_ext4_request_inode(dir, mode);
789828
inode = new_inode(sb);
790829
if (!inode)
791830
return ERR_PTR(-ENOMEM);
792831
ei = EXT4_I(inode);
793-
sbi = EXT4_SB(sb);
794832

795833
/*
796834
* Initialize owners and quota early so that we don't have to account

‎fs/ext4/super.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,8 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
11941194
if (res)
11951195
return res;
11961196
retry:
1197-
res = ext4_xattr_set_credits(inode, len, &credits);
1197+
res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1198+
&credits);
11981199
if (res)
11991200
return res;
12001201

‎fs/ext4/xattr.c‎

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -830,11 +830,10 @@ static void ext4_xattr_inode_free_quota(struct inode *inode, size_t len)
830830
dquot_free_inode(inode);
831831
}
832832

833-
static int __ext4_xattr_set_credits(struct inode *inode,
834-
struct buffer_head *block_bh,
835-
size_t value_len)
833+
int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
834+
struct buffer_head *block_bh, size_t value_len,
835+
bool is_create)
836836
{
837-
struct super_block *sb = inode->i_sb;
838837
int credits;
839838
int blocks;
840839

@@ -860,7 +859,7 @@ static int __ext4_xattr_set_credits(struct inode *inode,
860859
* In case of inline data, we may push out the data to a block,
861860
* so we need to reserve credits for this eventuality
862861
*/
863-
if (ext4_has_inline_data(inode))
862+
if (inode && ext4_has_inline_data(inode))
864863
credits += ext4_writepage_trans_blocks(inode) + 1;
865864

866865
/* We are done if ea_inode feature is not enabled. */
@@ -882,19 +881,23 @@ static int __ext4_xattr_set_credits(struct inode *inode,
882881
/* Blocks themselves. */
883882
credits += blocks;
884883

885-
/* Dereference ea_inode holding old xattr value.
886-
* Old ea_inode, inode map, block bitmap, group descriptor.
887-
*/
888-
credits += 4;
884+
if (!is_create) {
885+
/* Dereference ea_inode holding old xattr value.
886+
* Old ea_inode, inode map, block bitmap, group descriptor.
887+
*/
888+
credits += 4;
889889

890-
/* Data blocks for old ea_inode. */
891-
blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
890+
/* Data blocks for old ea_inode. */
891+
blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
892892

893-
/* Indirection block or one level of extent tree for old ea_inode. */
894-
blocks += 1;
893+
/* Indirection block or one level of extent tree for old
894+
* ea_inode.
895+
*/
896+
blocks += 1;
895897

896-
/* Block bitmap and group descriptor updates for each block. */
897-
credits += blocks * 2;
898+
/* Block bitmap and group descriptor updates for each block. */
899+
credits += blocks * 2;
900+
}
898901

899902
/* We may need to clone the existing xattr block in which case we need
900903
* to increment ref counts for existing ea_inodes referenced by it.
@@ -2263,7 +2266,9 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
22632266
goto cleanup;
22642267
}
22652268

2266-
credits = __ext4_xattr_set_credits(inode, bh, value_len);
2269+
credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2270+
value_len,
2271+
flags & XATTR_CREATE);
22672272
brelse(bh);
22682273

22692274
if (!ext4_handle_has_enough_credits(handle, credits)) {
@@ -2370,7 +2375,8 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
23702375
return error;
23712376
}
23722377

2373-
int ext4_xattr_set_credits(struct inode *inode, size_t value_len, int *credits)
2378+
int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2379+
bool is_create, int *credits)
23742380
{
23752381
struct buffer_head *bh;
23762382
int err;
@@ -2386,7 +2392,8 @@ int ext4_xattr_set_credits(struct inode *inode, size_t value_len, int *credits)
23862392
if (IS_ERR(bh)) {
23872393
err = PTR_ERR(bh);
23882394
} else {
2389-
*credits = __ext4_xattr_set_credits(inode, bh, value_len);
2395+
*credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2396+
value_len, is_create);
23902397
brelse(bh);
23912398
err = 0;
23922399
}
@@ -2417,7 +2424,8 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
24172424
return error;
24182425

24192426
retry:
2420-
error = ext4_xattr_set_credits(inode, value_len, &credits);
2427+
error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2428+
&credits);
24212429
if (error)
24222430
return error;
24232431

‎fs/ext4/xattr.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
153153
extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
154154
extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
155155
extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
156-
int *credits);
156+
bool is_create, int *credits);
157+
extern int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
158+
struct buffer_head *block_bh, size_t value_len,
159+
bool is_create);
157160

158161
extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
159162
struct ext4_xattr_inode_array **array,

‎include/linux/fscrypt_common.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ struct fscrypt_operations {
8383
unsigned (*max_namelen)(struct inode *);
8484
};
8585

86+
/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
87+
#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
88+
8689
static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
8790
{
8891
if (inode->i_sb->s_cop->dummy_context &&

0 commit comments

Comments
 (0)