From patchwork Wed Jun 21 21:21:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tahsin Erdogan X-Patchwork-Id: 779153 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3wtHpy3jdpz9s2s for ; Thu, 22 Jun 2017 07:27:06 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="n3HKdA89"; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752792AbdFUV07 (ORCPT ); Wed, 21 Jun 2017 17:26:59 -0400 Received: from mail-pf0-f175.google.com ([209.85.192.175]:34007 "EHLO mail-pf0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752661AbdFUVWR (ORCPT ); Wed, 21 Jun 2017 17:22:17 -0400 Received: by mail-pf0-f175.google.com with SMTP id s66so90499356pfs.1 for ; Wed, 21 Jun 2017 14:22:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YAeMQ1YxUGW9OMGO7KBfIrx8v9aNXoZCjFgAnd72jcE=; b=n3HKdA89ZwlPb2szD+JjfIYaNGJwElseiMYI77ULTJ6wf7jrFtr3N3o+4w7mIgffXH vPozsDld1jY1g4a5JmKCL/q4bskxrbl4m4EPrBmkkGgpcDsRGOKRZ2P5RJslC064cAtk iDbqNxxfHIHsKidDaV2goufEtEbgSnM2tZIUxHMD/DhoDtGJAdHA2d9AKhOFqcNjYCJa 10vgCaJyBctxMTNr7lv1xQoC4MGH+Rc2fHB9EL+IdkPOtddYeah41fV/h63x+xJarIYz OuNTn5NTFhz7NzdVHfRn6EXvLLF3q94gnHbNquC5HSRU6ATnhDctF78I2UXqPlp03peB rRAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YAeMQ1YxUGW9OMGO7KBfIrx8v9aNXoZCjFgAnd72jcE=; b=XrTAwCjq3+NXdJre4PwCvEmLBpRjqyxgw+RPAy47KGcJXE1ilMibN4RKWaq4vowrqg 7blXq2ASAJuT0rFQDJ5AIINEOwxlf6mrISEdABz0yRrp7nsGSXQje02/N1XI43PrBh1y wS2wxVVRKC9cs0ZRZ7VAhmRn5VtmOCIcGSERTaQqxQPCSbzzuQox5GR7DJBN03YQ+ksW SFJsJokbfNXUY89/D5vC0LzicvQUCW4ysvB19dQKVPXRwY8CBt/V4YsoT51DvGZ0P+fW YkkFXRVnq+WfGZDXKizQdbwOrCtKeECxHSJzjtCE3gSQx5aULoSvJ2e5ZIIGuEmfbQ/P 1Dzg== X-Gm-Message-State: AKS2vOyWZT3ZU02dWRdDuMViPKcF4iDZOt2zasPzUfrg6M9GyrX7HMPP o+b61kU9pgk4KRdx X-Received: by 10.99.113.11 with SMTP id m11mr38913093pgc.45.1498080136525; Wed, 21 Jun 2017 14:22:16 -0700 (PDT) Received: from tahsin1.svl.corp.google.com ([100.123.230.167]) by smtp.gmail.com with ESMTPSA id s64sm1829980pfd.77.2017.06.21.14.22.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 21 Jun 2017 14:22:16 -0700 (PDT) From: Tahsin Erdogan To: Andreas Dilger , "Darrick J . Wong" , Jan Kara , Theodore Ts'o , linux-ext4@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Tahsin Erdogan Subject: [PATCH 20/32] ext4: improve journal credit handling in set xattr paths Date: Wed, 21 Jun 2017 14:21:30 -0700 Message-Id: <20170621212142.16581-20-tahsin@google.com> X-Mailer: git-send-email 2.13.1.611.g7e3b11ae1-goog In-Reply-To: <20170621212142.16581-1-tahsin@google.com> References: <20170621212142.16581-1-tahsin@google.com> Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Both ext4_set_acl() and ext4_set_context() need to be made aware of ea_inode feature when it comes to credits calculation. Also add a sufficient credits check in ext4_xattr_set_handle() right after xattr write lock is grabbed. Original credits calculation is done outside the lock so there is a possiblity that the initially calculated credits are not sufficient anymore. Signed-off-by: Tahsin Erdogan --- v2: fixed checkpatch.pl warning about replacing spaces with tab fs/ext4/acl.c | 7 ++++--- fs/ext4/ext4_jbd2.h | 14 -------------- fs/ext4/super.c | 6 +++--- fs/ext4/xattr.c | 55 +++++++++++++++++++++++++++++++++++++++++------------ fs/ext4/xattr.h | 1 + 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c index 3ec0e46de95f..74f7ac539e00 100644 --- a/fs/ext4/acl.c +++ b/fs/ext4/acl.c @@ -231,14 +231,15 @@ int ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type) { handle_t *handle; - int error, retries = 0; + int error, credits, retries = 0; + size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0; error = dquot_initialize(inode); if (error) return error; retry: - handle = ext4_journal_start(inode, EXT4_HT_XATTR, - ext4_jbd2_credits_xattr(inode)); + credits = ext4_xattr_set_credits(inode, acl_size); + handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); if (IS_ERR(handle)) return PTR_ERR(handle); diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h index f97611171023..a5bda70feed5 100644 --- a/fs/ext4/ext4_jbd2.h +++ b/fs/ext4/ext4_jbd2.h @@ -104,20 +104,6 @@ #define EXT4_MAXQUOTAS_INIT_BLOCKS(sb) (EXT4_MAXQUOTAS*EXT4_QUOTA_INIT_BLOCKS(sb)) #define EXT4_MAXQUOTAS_DEL_BLOCKS(sb) (EXT4_MAXQUOTAS*EXT4_QUOTA_DEL_BLOCKS(sb)) -static inline int ext4_jbd2_credits_xattr(struct inode *inode) -{ - int credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb); - - /* - * In case of inline data, we may push out the data to a block, - * so we need to reserve credits for this eventuality - */ - if (ext4_has_inline_data(inode)) - credits += ext4_writepage_trans_blocks(inode) + 1; - return credits; -} - - /* * Ext4 handle operation types -- for logging purposes */ diff --git a/fs/ext4/super.c b/fs/ext4/super.c index d37c81f327e7..b02a23ec92ca 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1143,7 +1143,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, void *fs_data) { handle_t *handle = fs_data; - int res, res2, retries = 0; + int res, res2, credits, retries = 0; res = ext4_convert_inline_data(inode); if (res) @@ -1178,8 +1178,8 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, if (res) return res; retry: - handle = ext4_journal_start(inode, EXT4_HT_MISC, - ext4_jbd2_credits_xattr(inode)); + credits = ext4_xattr_set_credits(inode, len); + handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); if (IS_ERR(handle)) return PTR_ERR(handle); diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c index 97d33ecf0818..fd017faaf221 100644 --- a/fs/ext4/xattr.c +++ b/fs/ext4/xattr.c @@ -1473,6 +1473,17 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, ext4_write_lock_xattr(inode, &no_expand); + /* Check journal credits under write lock. */ + if (ext4_handle_valid(handle)) { + int credits; + + credits = ext4_xattr_set_credits(inode, value_len); + if (!ext4_handle_has_enough_credits(handle, credits)) { + error = -ENOSPC; + goto cleanup; + } + } + error = ext4_reserve_inode_write(handle, inode, &is.iloc); if (error) goto cleanup; @@ -1570,6 +1581,36 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, return error; } +int ext4_xattr_set_credits(struct inode *inode, size_t value_len) +{ + struct super_block *sb = inode->i_sb; + int credits; + + if (!EXT4_SB(sb)->s_journal) + return 0; + + credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb); + + /* + * In case of inline data, we may push out the data to a block, + * so we need to reserve credits for this eventuality + */ + if (ext4_has_inline_data(inode)) + credits += ext4_writepage_trans_blocks(inode) + 1; + + if (ext4_has_feature_ea_inode(sb)) { + int nrblocks = (value_len + sb->s_blocksize - 1) >> + sb->s_blocksize_bits; + + /* For new inode */ + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3; + + /* For data blocks of EA inode */ + credits += ext4_meta_trans_blocks(inode, nrblocks, 0); + } + return credits; +} + /* * ext4_xattr_set() * @@ -1585,24 +1626,14 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name, handle_t *handle; struct super_block *sb = inode->i_sb; int error, retries = 0; - int credits = ext4_jbd2_credits_xattr(inode); + int credits; error = dquot_initialize(inode); if (error) return error; - if (ext4_has_feature_ea_inode(sb)) { - int nrblocks = (value_len + sb->s_blocksize - 1) >> - sb->s_blocksize_bits; - - /* For new inode */ - credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3; - - /* For data blocks of EA inode */ - credits += ext4_meta_trans_blocks(inode, nrblocks, 0); - } - retry: + credits = ext4_xattr_set_credits(inode, value_len); handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); if (IS_ERR(handle)) { error = PTR_ERR(handle); diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h index b6ef99d1a061..e82c5fe36a26 100644 --- a/fs/ext4/xattr.h +++ b/fs/ext4/xattr.h @@ -160,6 +160,7 @@ extern ssize_t ext4_listxattr(struct dentry *, char *, size_t); extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t); extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int); extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int); +extern int ext4_xattr_set_credits(struct inode *inode, size_t value_len); extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino); extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,