Skip to content

Commit 93061f3

Browse files
committed
Merge tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
Pull ext4 bugfixes from Ted Ts'o: "These changes contains a fix for overlayfs interacting with some (badly behaved) dentry code in various file systems. These have been reviewed by Al and the respective file system mtinainers and are going through the ext4 tree for convenience. This also has a few ext4 encryption bug fixes that were discovered in Android testing (yes, we will need to get these sync'ed up with the fs/crypto code; I'll take care of that). It also has some bug fixes and a change to ignore the legacy quota options to allow for xfstests regression testing of ext4's internal quota feature and to be more consistent with how xfs handles this case" * tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: ext4: ignore quota mount options if the quota feature is enabled ext4 crypto: fix some error handling ext4: avoid calling dquot_get_next_id() if quota is not enabled ext4: retry block allocation for failed DIO and DAX writes ext4: add lockdep annotations for i_data_sem ext4: allow readdir()'s of large empty directories to be interrupted btrfs: fix crash/invalid memory access on fsync when using overlayfs ext4 crypto: use dget_parent() in ext4_d_revalidate() ext4: use file_dentry() ext4: use dget_parent() in ext4_file_open() nfs: use file_dentry() fs: add file_dentry() ext4 crypto: don't let data integrity writebacks fail with ENOMEM ext4: check if in-inode xattr is corrupted in ext4_expand_extra_isize_ea()
2 parents 1c915b3 + c325a67 commit 93061f3

File tree

19 files changed

+264
-86
lines changed

19 files changed

+264
-86
lines changed

fs/btrfs/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end)
19051905
*/
19061906
int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
19071907
{
1908-
struct dentry *dentry = file->f_path.dentry;
1908+
struct dentry *dentry = file_dentry(file);
19091909
struct inode *inode = d_inode(dentry);
19101910
struct btrfs_root *root = BTRFS_I(inode)->root;
19111911
struct btrfs_trans_handle *trans;

fs/dcache.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,8 @@ void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
16671667
DCACHE_OP_REVALIDATE |
16681668
DCACHE_OP_WEAK_REVALIDATE |
16691669
DCACHE_OP_DELETE |
1670-
DCACHE_OP_SELECT_INODE));
1670+
DCACHE_OP_SELECT_INODE |
1671+
DCACHE_OP_REAL));
16711672
dentry->d_op = op;
16721673
if (!op)
16731674
return;
@@ -1685,6 +1686,8 @@ void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
16851686
dentry->d_flags |= DCACHE_OP_PRUNE;
16861687
if (op->d_select_inode)
16871688
dentry->d_flags |= DCACHE_OP_SELECT_INODE;
1689+
if (op->d_real)
1690+
dentry->d_flags |= DCACHE_OP_REAL;
16881691

16891692
}
16901693
EXPORT_SYMBOL(d_set_d_op);

fs/ext4/crypto.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
9191
* Return: An allocated and initialized encryption context on success; error
9292
* value or NULL otherwise.
9393
*/
94-
struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
94+
struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode,
95+
gfp_t gfp_flags)
9596
{
9697
struct ext4_crypto_ctx *ctx = NULL;
9798
int res = 0;
@@ -118,7 +119,7 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
118119
list_del(&ctx->free_list);
119120
spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
120121
if (!ctx) {
121-
ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
122+
ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, gfp_flags);
122123
if (!ctx) {
123124
res = -ENOMEM;
124125
goto out;
@@ -255,7 +256,8 @@ static int ext4_page_crypto(struct inode *inode,
255256
ext4_direction_t rw,
256257
pgoff_t index,
257258
struct page *src_page,
258-
struct page *dest_page)
259+
struct page *dest_page,
260+
gfp_t gfp_flags)
259261

260262
{
261263
u8 xts_tweak[EXT4_XTS_TWEAK_SIZE];
@@ -266,7 +268,7 @@ static int ext4_page_crypto(struct inode *inode,
266268
struct crypto_skcipher *tfm = ci->ci_ctfm;
267269
int res = 0;
268270

269-
req = skcipher_request_alloc(tfm, GFP_NOFS);
271+
req = skcipher_request_alloc(tfm, gfp_flags);
270272
if (!req) {
271273
printk_ratelimited(KERN_ERR
272274
"%s: crypto_request_alloc() failed\n",
@@ -307,9 +309,10 @@ static int ext4_page_crypto(struct inode *inode,
307309
return 0;
308310
}
309311

310-
static struct page *alloc_bounce_page(struct ext4_crypto_ctx *ctx)
312+
static struct page *alloc_bounce_page(struct ext4_crypto_ctx *ctx,
313+
gfp_t gfp_flags)
311314
{
312-
ctx->w.bounce_page = mempool_alloc(ext4_bounce_page_pool, GFP_NOWAIT);
315+
ctx->w.bounce_page = mempool_alloc(ext4_bounce_page_pool, gfp_flags);
313316
if (ctx->w.bounce_page == NULL)
314317
return ERR_PTR(-ENOMEM);
315318
ctx->flags |= EXT4_WRITE_PATH_FL;
@@ -332,25 +335,26 @@ static struct page *alloc_bounce_page(struct ext4_crypto_ctx *ctx)
332335
* error value or NULL.
333336
*/
334337
struct page *ext4_encrypt(struct inode *inode,
335-
struct page *plaintext_page)
338+
struct page *plaintext_page,
339+
gfp_t gfp_flags)
336340
{
337341
struct ext4_crypto_ctx *ctx;
338342
struct page *ciphertext_page = NULL;
339343
int err;
340344

341345
BUG_ON(!PageLocked(plaintext_page));
342346

343-
ctx = ext4_get_crypto_ctx(inode);
347+
ctx = ext4_get_crypto_ctx(inode, gfp_flags);
344348
if (IS_ERR(ctx))
345349
return (struct page *) ctx;
346350

347351
/* The encryption operation will require a bounce page. */
348-
ciphertext_page = alloc_bounce_page(ctx);
352+
ciphertext_page = alloc_bounce_page(ctx, gfp_flags);
349353
if (IS_ERR(ciphertext_page))
350354
goto errout;
351355
ctx->w.control_page = plaintext_page;
352356
err = ext4_page_crypto(inode, EXT4_ENCRYPT, plaintext_page->index,
353-
plaintext_page, ciphertext_page);
357+
plaintext_page, ciphertext_page, gfp_flags);
354358
if (err) {
355359
ciphertext_page = ERR_PTR(err);
356360
errout:
@@ -378,8 +382,8 @@ int ext4_decrypt(struct page *page)
378382
{
379383
BUG_ON(!PageLocked(page));
380384

381-
return ext4_page_crypto(page->mapping->host,
382-
EXT4_DECRYPT, page->index, page, page);
385+
return ext4_page_crypto(page->mapping->host, EXT4_DECRYPT,
386+
page->index, page, page, GFP_NOFS);
383387
}
384388

385389
int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
@@ -398,23 +402,24 @@ int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
398402

399403
BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
400404

401-
ctx = ext4_get_crypto_ctx(inode);
405+
ctx = ext4_get_crypto_ctx(inode, GFP_NOFS);
402406
if (IS_ERR(ctx))
403407
return PTR_ERR(ctx);
404408

405-
ciphertext_page = alloc_bounce_page(ctx);
409+
ciphertext_page = alloc_bounce_page(ctx, GFP_NOWAIT);
406410
if (IS_ERR(ciphertext_page)) {
407411
err = PTR_ERR(ciphertext_page);
408412
goto errout;
409413
}
410414

411415
while (len--) {
412416
err = ext4_page_crypto(inode, EXT4_ENCRYPT, lblk,
413-
ZERO_PAGE(0), ciphertext_page);
417+
ZERO_PAGE(0), ciphertext_page,
418+
GFP_NOFS);
414419
if (err)
415420
goto errout;
416421

417-
bio = bio_alloc(GFP_KERNEL, 1);
422+
bio = bio_alloc(GFP_NOWAIT, 1);
418423
if (!bio) {
419424
err = -ENOMEM;
420425
goto errout;
@@ -473,13 +478,16 @@ uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
473478
*/
474479
static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
475480
{
476-
struct inode *dir = d_inode(dentry->d_parent);
477-
struct ext4_crypt_info *ci = EXT4_I(dir)->i_crypt_info;
481+
struct dentry *dir;
482+
struct ext4_crypt_info *ci;
478483
int dir_has_key, cached_with_key;
479484

480-
if (!ext4_encrypted_inode(dir))
485+
dir = dget_parent(dentry);
486+
if (!ext4_encrypted_inode(d_inode(dir))) {
487+
dput(dir);
481488
return 0;
482-
489+
}
490+
ci = EXT4_I(d_inode(dir))->i_crypt_info;
483491
if (ci && ci->ci_keyring_key &&
484492
(ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
485493
(1 << KEY_FLAG_REVOKED) |
@@ -489,6 +497,7 @@ static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
489497
/* this should eventually be an flag in d_flags */
490498
cached_with_key = dentry->d_fsdata != NULL;
491499
dir_has_key = (ci != NULL);
500+
dput(dir);
492501

493502
/*
494503
* If the dentry was cached without the key, and it is a

fs/ext4/dir.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
150150
while (ctx->pos < inode->i_size) {
151151
struct ext4_map_blocks map;
152152

153+
if (fatal_signal_pending(current)) {
154+
err = -ERESTARTSYS;
155+
goto errout;
156+
}
157+
cond_resched();
153158
map.m_lblk = ctx->pos >> EXT4_BLOCK_SIZE_BITS(sb);
154159
map.m_len = 1;
155160
err = ext4_map_blocks(NULL, inode, &map, 0);

fs/ext4/ext4.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,29 @@ do { \
911911

912912
#include "extents_status.h"
913913

914+
/*
915+
* Lock subclasses for i_data_sem in the ext4_inode_info structure.
916+
*
917+
* These are needed to avoid lockdep false positives when we need to
918+
* allocate blocks to the quota inode during ext4_map_blocks(), while
919+
* holding i_data_sem for a normal (non-quota) inode. Since we don't
920+
* do quota tracking for the quota inode, this avoids deadlock (as
921+
* well as infinite recursion, since it isn't turtles all the way
922+
* down...)
923+
*
924+
* I_DATA_SEM_NORMAL - Used for most inodes
925+
* I_DATA_SEM_OTHER - Used by move_inode.c for the second normal inode
926+
* where the second inode has larger inode number
927+
* than the first
928+
* I_DATA_SEM_QUOTA - Used for quota inodes only
929+
*/
930+
enum {
931+
I_DATA_SEM_NORMAL = 0,
932+
I_DATA_SEM_OTHER,
933+
I_DATA_SEM_QUOTA,
934+
};
935+
936+
914937
/*
915938
* fourth extended file system inode data in memory
916939
*/
@@ -2282,11 +2305,13 @@ extern struct kmem_cache *ext4_crypt_info_cachep;
22822305
bool ext4_valid_contents_enc_mode(uint32_t mode);
22832306
uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size);
22842307
extern struct workqueue_struct *ext4_read_workqueue;
2285-
struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode);
2308+
struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode,
2309+
gfp_t gfp_flags);
22862310
void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx);
22872311
void ext4_restore_control_page(struct page *data_page);
22882312
struct page *ext4_encrypt(struct inode *inode,
2289-
struct page *plaintext_page);
2313+
struct page *plaintext_page,
2314+
gfp_t gfp_flags);
22902315
int ext4_decrypt(struct page *page);
22912316
int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
22922317
ext4_fsblk_t pblk, ext4_lblk_t len);

fs/ext4/file.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
329329
struct super_block *sb = inode->i_sb;
330330
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
331331
struct vfsmount *mnt = filp->f_path.mnt;
332-
struct inode *dir = filp->f_path.dentry->d_parent->d_inode;
332+
struct dentry *dir;
333333
struct path path;
334334
char buf[64], *cp;
335335
int ret;
@@ -373,14 +373,18 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
373373
if (ext4_encryption_info(inode) == NULL)
374374
return -ENOKEY;
375375
}
376-
if (ext4_encrypted_inode(dir) &&
377-
!ext4_is_child_context_consistent_with_parent(dir, inode)) {
376+
377+
dir = dget_parent(file_dentry(filp));
378+
if (ext4_encrypted_inode(d_inode(dir)) &&
379+
!ext4_is_child_context_consistent_with_parent(d_inode(dir), inode)) {
378380
ext4_warning(inode->i_sb,
379381
"Inconsistent encryption contexts: %lu/%lu\n",
380-
(unsigned long) dir->i_ino,
382+
(unsigned long) d_inode(dir)->i_ino,
381383
(unsigned long) inode->i_ino);
384+
dput(dir);
382385
return -EPERM;
383386
}
387+
dput(dir);
384388
/*
385389
* Set up the jbd2_inode if we are opening the inode for
386390
* writing and the journal is present

fs/ext4/inode.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -763,39 +763,47 @@ int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
763763
/* Maximum number of blocks we map for direct IO at once. */
764764
#define DIO_MAX_BLOCKS 4096
765765

766-
static handle_t *start_dio_trans(struct inode *inode,
767-
struct buffer_head *bh_result)
766+
/*
767+
* Get blocks function for the cases that need to start a transaction -
768+
* generally difference cases of direct IO and DAX IO. It also handles retries
769+
* in case of ENOSPC.
770+
*/
771+
static int ext4_get_block_trans(struct inode *inode, sector_t iblock,
772+
struct buffer_head *bh_result, int flags)
768773
{
769774
int dio_credits;
775+
handle_t *handle;
776+
int retries = 0;
777+
int ret;
770778

771779
/* Trim mapping request to maximum we can map at once for DIO */
772780
if (bh_result->b_size >> inode->i_blkbits > DIO_MAX_BLOCKS)
773781
bh_result->b_size = DIO_MAX_BLOCKS << inode->i_blkbits;
774782
dio_credits = ext4_chunk_trans_blocks(inode,
775783
bh_result->b_size >> inode->i_blkbits);
776-
return ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
784+
retry:
785+
handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
786+
if (IS_ERR(handle))
787+
return PTR_ERR(handle);
788+
789+
ret = _ext4_get_block(inode, iblock, bh_result, flags);
790+
ext4_journal_stop(handle);
791+
792+
if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
793+
goto retry;
794+
return ret;
777795
}
778796

779797
/* Get block function for DIO reads and writes to inodes without extents */
780798
int ext4_dio_get_block(struct inode *inode, sector_t iblock,
781799
struct buffer_head *bh, int create)
782800
{
783-
handle_t *handle;
784-
int ret;
785-
786801
/* We don't expect handle for direct IO */
787802
WARN_ON_ONCE(ext4_journal_current_handle());
788803

789-
if (create) {
790-
handle = start_dio_trans(inode, bh);
791-
if (IS_ERR(handle))
792-
return PTR_ERR(handle);
793-
}
794-
ret = _ext4_get_block(inode, iblock, bh,
795-
create ? EXT4_GET_BLOCKS_CREATE : 0);
796-
if (create)
797-
ext4_journal_stop(handle);
798-
return ret;
804+
if (!create)
805+
return _ext4_get_block(inode, iblock, bh, 0);
806+
return ext4_get_block_trans(inode, iblock, bh, EXT4_GET_BLOCKS_CREATE);
799807
}
800808

801809
/*
@@ -806,18 +814,13 @@ int ext4_dio_get_block(struct inode *inode, sector_t iblock,
806814
static int ext4_dio_get_block_unwritten_async(struct inode *inode,
807815
sector_t iblock, struct buffer_head *bh_result, int create)
808816
{
809-
handle_t *handle;
810817
int ret;
811818

812819
/* We don't expect handle for direct IO */
813820
WARN_ON_ONCE(ext4_journal_current_handle());
814821

815-
handle = start_dio_trans(inode, bh_result);
816-
if (IS_ERR(handle))
817-
return PTR_ERR(handle);
818-
ret = _ext4_get_block(inode, iblock, bh_result,
819-
EXT4_GET_BLOCKS_IO_CREATE_EXT);
820-
ext4_journal_stop(handle);
822+
ret = ext4_get_block_trans(inode, iblock, bh_result,
823+
EXT4_GET_BLOCKS_IO_CREATE_EXT);
821824

822825
/*
823826
* When doing DIO using unwritten extents, we need io_end to convert
@@ -850,18 +853,13 @@ static int ext4_dio_get_block_unwritten_async(struct inode *inode,
850853
static int ext4_dio_get_block_unwritten_sync(struct inode *inode,
851854
sector_t iblock, struct buffer_head *bh_result, int create)
852855
{
853-
handle_t *handle;
854856
int ret;
855857

856858
/* We don't expect handle for direct IO */
857859
WARN_ON_ONCE(ext4_journal_current_handle());
858860

859-
handle = start_dio_trans(inode, bh_result);
860-
if (IS_ERR(handle))
861-
return PTR_ERR(handle);
862-
ret = _ext4_get_block(inode, iblock, bh_result,
863-
EXT4_GET_BLOCKS_IO_CREATE_EXT);
864-
ext4_journal_stop(handle);
861+
ret = ext4_get_block_trans(inode, iblock, bh_result,
862+
EXT4_GET_BLOCKS_IO_CREATE_EXT);
865863

866864
/*
867865
* Mark inode as having pending DIO writes to unwritten extents.

0 commit comments

Comments
 (0)