From patchwork Fri Feb 7 07:20:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 11369881 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 33AC592A for ; Fri, 7 Feb 2020 07:21:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0788F214AF for ; Fri, 7 Feb 2020 07:21:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=wdc.com header.i=@wdc.com header.b="jEbZXlXX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726915AbgBGHVD (ORCPT ); Fri, 7 Feb 2020 02:21:03 -0500 Received: from esa2.hgst.iphmx.com ([68.232.143.124]:26076 "EHLO esa2.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726843AbgBGHVC (ORCPT ); Fri, 7 Feb 2020 02:21:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1581060069; x=1612596069; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+lKSLtZ0YtuZeXXrgYLxje8qJhwixKLcMw/52PwUvUc=; b=jEbZXlXXVFwI8PiO8xyoUdXiIp/q2GH8Gd7lUspaeH12wb6uSWpkMLcQ nBRlqC0ysKg2bgcxYiG/Rt5faw5IN3/TpDq3LVMoEyJ7metGcuch31DCp NHwZj8OeodIUgtx/pDcF4EvH7/d4mHdgeWhhRUfBuB3IfE/Mh/3VSx567 2Zp1dkGFnLEufAV8ha6MLSXErMl41cRjHxB04Po6nPzPIYIEU1iiLuyOh dmPtmQJbw5F98pGwnKMjKo7MtoIo3g4eR2wbcWHSHKerTd/4tILYuguMI rhuz1mj1UdvGzMZGI96cwWC82L0VUVlmSgyQSfbfldFkhuo//pVFV572c w==; IronPort-SDR: EWx9rWuNEhpcc7y7xjoIMulYdqKcRzMvrXjDOjiCWZmCU951ZPedqq6nlnlvxYrx+gLCY7HZrh OfbpAUWu/z2gQaK9tiu5UgfmYP8DW2TddpvcSho4u4Kvffi5G5G3iwbRh1t3sn1i0XCLlhonQv K5icn+21gFbtnQ308zK4mMJwRXZgayplM/d0p8myuSUXww6kKqTqMz/KvvJG8xrPwoPGBLOi5g edKeImBvCbi3YXOvomVJ2MAM01S/SZsBAvcxWXNcx48lvChBL+sjNvh2n2PKuPtbYSlx2Z9kM5 ecM= X-IronPort-AV: E=Sophos;i="5.70,412,1574092800"; d="scan'208";a="231092230" Received: from h199-255-45-14.hgst.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 07 Feb 2020 15:20:23 +0800 IronPort-SDR: 9cbYsviE6PtMEgarZM573CpTFubEBLRCuQ+gj8mgQypkQpeWTDI5LS5hdX2ZfJ2EzLKb8XLIWm aHgEfAGq4Yykf1WNH7OpSX6Up03qg1Je65Cdr0CUABgp9+/kKaFysXwu3Z6XaPk5x15pKg8ULq MEugffdEepEIj93LUYijptQ43LGMvlrtspN2zpKScMcOL65JjcVnbz2oSu/DfVGqp3n9340UGb /5XUCtlBuDmc0fsBKRDAW92fJnVaVTQdDqbyy2gblHEnWNThbKCMhBj0rvjXeqezzseCGU0TvM BMoo3H7FIdcKSJ8Kq+BZ27XV Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2020 23:13:13 -0800 IronPort-SDR: pArY00xOjDqRbVArIdpA7y4M4iAN4i5Tn03ylCoHBHyD9V9d6GI17NUluF977dPXAQHOhQ7UvG ojq5uHwoBLBSX5KuCLS1MiGGFJqZA1zW74SBauUXQ2bD9BVh0fViOA+shkjm3qPP31F9FPm6Wn gdNrNA2g4HRsmDjMsQY1NJvl6d7w7Wy+qewnITJimfa7+TdAbCcfCmnQAQWh3nv4LPq6n2lwEr AoWYtX2KR7GqmiQtuCKVBX2MA25FqOKF1TmesIfIe65XxDfp1m+FQJdxVxT9NPD59mAM6vJJTb wOQ= WDCIronportException: Internal Received: from unknown (HELO redsun60.ssa.fujisawa.hgst.com) ([10.149.66.36]) by uls-op-cesaip01.wdc.com with ESMTP; 06 Feb 2020 23:20:15 -0800 From: Johannes Thumshirn To: David Sterba Cc: Nikolay Borisov , Josef Bacik , "linux-btrfs @ vger . kernel . org" , Johannes Thumshirn Subject: [PATCH v5 4/7] btrfs: use BIOs instead of buffer_heads from superblock writeout Date: Fri, 7 Feb 2020 16:20:02 +0900 Message-Id: <20200207072005.22867-5-johannes.thumshirn@wdc.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200207072005.22867-1-johannes.thumshirn@wdc.com> References: <20200207072005.22867-1-johannes.thumshirn@wdc.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Similar to the superblock read path, change the write path to using BIOs and pages instead of buffer_heads. This allows us to skip over the buffer_head code, for writing the superblock to disk. This is based on a patch originally authored by Nikolay Borisov. Co-developed-by: Nikolay Borisov Signed-off-by: Nikolay Borisov Signed-off-by: Johannes Thumshirn Reviewed-by: Nikolay Borisov Reviewed-by: Josef Bacik --- Changes to v4: - get rid of op_flags (hch) - don't use mapping_gfp_constraint() (hch) - print errno on error (hch) - use typed pointer (hch) Changes to v2: - Don't use bi_set_op_attrs() (David) Changes to v1: - Remove left-over buffer_head.h include (David) --- fs/btrfs/disk-io.c | 118 ++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 50 deletions(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 988df20baefd..cf1c5ed5a5cb 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -3333,25 +3332,34 @@ int __cold open_ctree(struct super_block *sb, } ALLOW_ERROR_INJECTION(open_ctree, ERRNO); -static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate) +static void btrfs_end_super_write(struct bio *bio) { - if (uptodate) { - set_buffer_uptodate(bh); - } else { - struct btrfs_device *device = (struct btrfs_device *) - bh->b_private; - - btrfs_warn_rl_in_rcu(device->fs_info, - "lost page write due to IO error on %s", - rcu_str_deref(device->name)); - /* note, we don't set_buffer_write_io_error because we have - * our own ways of dealing with the IO errors - */ - clear_buffer_uptodate(bh); - btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS); + struct btrfs_device *device = bio->bi_private; + struct bio_vec *bvec; + struct bvec_iter_all iter_all; + struct page *page; + + bio_for_each_segment_all(bvec, bio, iter_all) { + page = bvec->bv_page; + + if (bio->bi_status) { + btrfs_warn_rl_in_rcu(device->fs_info, + "lost page write due to IO error on %s (%d)", + rcu_str_deref(device->name), + blk_status_to_errno(bio->bi_status)); + ClearPageUptodate(page); + SetPageError(page); + btrfs_dev_stat_inc_and_print(device, + BTRFS_DEV_STAT_WRITE_ERRS); + } else { + SetPageUptodate(page); + } + + put_page(page); + unlock_page(page); } - unlock_buffer(bh); - put_bh(bh); + + bio_put(bio); } struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, @@ -3417,19 +3425,17 @@ struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev) * the expected device size at commit time. Note that max_mirrors must be * same for write and wait phases. * - * Return number of errors when buffer head is not found or submission fails. + * Return number of errors when page is not found or submission fails. */ static int write_dev_supers(struct btrfs_device *device, struct btrfs_super_block *sb, int max_mirrors) { struct btrfs_fs_info *fs_info = device->fs_info; + struct address_space *mapping = device->bdev->bd_inode->i_mapping; SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); - struct buffer_head *bh; int i; - int ret; int errors = 0; u64 bytenr; - int op_flags; if (max_mirrors == 0) max_mirrors = BTRFS_SUPER_MIRROR_MAX; @@ -3437,6 +3443,10 @@ static int write_dev_supers(struct btrfs_device *device, shash->tfm = fs_info->csum_shash; for (i = 0; i < max_mirrors; i++) { + struct page *page; + struct bio *bio; + struct btrfs_super_block *disk_super; + bytenr = btrfs_sb_offset(i); if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->commit_total_bytes) @@ -3449,37 +3459,45 @@ static int write_dev_supers(struct btrfs_device *device, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); crypto_shash_final(shash, sb->csum); - /* One reference for us, and we leave it for the caller */ - bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, - BTRFS_SUPER_INFO_SIZE); - if (!bh) { + page = find_or_create_page(mapping, bytenr >> PAGE_SHIFT, + GFP_NOFS); + if (!page) { btrfs_err(device->fs_info, - "couldn't get super buffer head for bytenr %llu", + "couldn't get superblock page for bytenr %llu", bytenr); errors++; continue; } - memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE); + /* Bump the refcount for wait_dev_supers() */ + get_page(page); - /* one reference for submit_bh */ - get_bh(bh); + disk_super = kmap(page) + offset_in_page(bytenr); + memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE); + kunmap(page); - set_buffer_uptodate(bh); - lock_buffer(bh); - bh->b_end_io = btrfs_end_buffer_write_sync; - bh->b_private = device; + /* + * Directly use BIOs here instead of relying on the page-cache + * to do I/O, so we don't loose the ability to do integrity + * checking. + */ + bio = bio_alloc(GFP_NOFS, 1); + bio_set_dev(bio, device->bdev); + bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT; + bio->bi_private = device; + bio->bi_end_io = btrfs_end_super_write; + __bio_add_page(bio, page, BTRFS_SUPER_INFO_SIZE, + offset_in_page(bytenr)); /* - * we fua the first super. The others we allow + * We fua the first super. The others we allow * to go down lazy. */ - op_flags = REQ_SYNC | REQ_META | REQ_PRIO; + bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO; if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER)) - op_flags |= REQ_FUA; - ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh); - if (ret) - errors++; + bio->bi_opf |= REQ_FUA; + + btrfsic_submit_bio(bio); } return errors < i ? 0 : -1; } @@ -3488,12 +3506,11 @@ static int write_dev_supers(struct btrfs_device *device, * Wait for write completion of superblocks done by write_dev_supers, * @max_mirrors same for write and wait phases. * - * Return number of errors when buffer head is not found or not marked up to + * Return number of errors when page is not found or not marked up to * date. */ static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) { - struct buffer_head *bh; int i; int errors = 0; bool primary_failed = false; @@ -3503,32 +3520,33 @@ static int wait_dev_supers(struct btrfs_device *device, int max_mirrors) max_mirrors = BTRFS_SUPER_MIRROR_MAX; for (i = 0; i < max_mirrors; i++) { + struct page *page; + bytenr = btrfs_sb_offset(i); if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->commit_total_bytes) break; - bh = __find_get_block(device->bdev, - bytenr / BTRFS_BDEV_BLOCKSIZE, - BTRFS_SUPER_INFO_SIZE); - if (!bh) { + page = find_get_page(device->bdev->bd_inode->i_mapping, + bytenr >> PAGE_SHIFT); + if (!page) { errors++; if (i == 0) primary_failed = true; continue; } - wait_on_buffer(bh); - if (!buffer_uptodate(bh)) { + wait_on_page_locked(page); + if (PageError(page)) { errors++; if (i == 0) primary_failed = true; } /* drop our reference */ - brelse(bh); + put_page(page); /* drop the reference from the writing run */ - brelse(bh); + put_page(page); } /* log error, force error return */