From patchwork Wed Jul 5 03:33:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goldwyn Rodrigues X-Patchwork-Id: 9825811 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0057160353 for ; Wed, 5 Jul 2017 03:33:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D6024262FF for ; Wed, 5 Jul 2017 03:33:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CA6F5274D1; Wed, 5 Jul 2017 03:33:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3EBD7262FF for ; Wed, 5 Jul 2017 03:33:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752317AbdGEDd1 (ORCPT ); Tue, 4 Jul 2017 23:33:27 -0400 Received: from mx2.suse.de ([195.135.220.15]:50339 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752286AbdGEDd1 (ORCPT ); Tue, 4 Jul 2017 23:33:27 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 20AFCAAB6; Wed, 5 Jul 2017 03:33:25 +0000 (UTC) From: Goldwyn Rodrigues To: linux-btrfs@vger.kernel.org Cc: linux-block@vger.kernel.org, markus@trippelsdorf.de, linux-kernel@vger.kernel.org, axboe@kernel.dk, dsterba@suse.com, marc.c.dionne@gmail.com, Goldwyn Rodrigues Subject: [PATCH v2] btrfs: Correct assignment of pos Date: Tue, 4 Jul 2017 22:33:07 -0500 Message-Id: <20170705033307.8213-1-rgoldwyn@suse.de> X-Mailer: git-send-email 2.12.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Goldwyn Rodrigues Assigning pos for usage early messes up in append mode, where the pos is re-assigned in generic_write_checks(). Assign pos later to get the correct position to write from iocb->ki_pos. Since check_can_nocow also uses the value of pos, we shift generic_write_checks() before check_can_nocow(). Checks with IOCB_DIRECT are present in generic_write_checks(), so checking for IOCB_NOWAIT is enough. Also, put locking sequence in the fast path. Changes since v1: - Moved pos higher up to encompass check_can_nocow() call. Fixes: edf064e7c6fe ("btrfs: nowait aio support") Signed-off-by: Goldwyn Rodrigues Acked-by: David Sterba Reviewed-by: Liu Bo --- fs/btrfs/file.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index 59e2dccdf75b..ad53832838b5 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1875,16 +1875,25 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb, ssize_t num_written = 0; bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host); ssize_t err; - loff_t pos = iocb->ki_pos; + loff_t pos; size_t count = iov_iter_count(from); loff_t oldsize; int clean_page = 0; - if ((iocb->ki_flags & IOCB_NOWAIT) && - (iocb->ki_flags & IOCB_DIRECT)) { - /* Don't sleep on inode rwsem */ - if (!inode_trylock(inode)) + if (!inode_trylock(inode)) { + if (iocb->ki_flags & IOCB_NOWAIT) return -EAGAIN; + inode_lock(inode); + } + + err = generic_write_checks(iocb, from); + if (err <= 0) { + inode_unlock(inode); + return err; + } + + pos = iocb->ki_pos; + if (iocb->ki_flags & IOCB_NOWAIT) { /* * We will allocate space in case nodatacow is not set, * so bail @@ -1895,13 +1904,6 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb, inode_unlock(inode); return -EAGAIN; } - } else - inode_lock(inode); - - err = generic_write_checks(iocb, from); - if (err <= 0) { - inode_unlock(inode); - return err; } current->backing_dev_info = inode_to_bdi(inode);