From patchwork Fri Sep 24 13:09:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiyang Ruan X-Patchwork-Id: 12515369 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2210CC4332F for ; Fri, 24 Sep 2021 13:10:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B15F460F12 for ; Fri, 24 Sep 2021 13:10:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B15F460F12 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=fujitsu.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 4A3D06B007E; Fri, 24 Sep 2021 09:10:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 405FB900002; Fri, 24 Sep 2021 09:10:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20AC36B0081; Fri, 24 Sep 2021 09:10:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0133.hostedemail.com [216.40.44.133]) by kanga.kvack.org (Postfix) with ESMTP id 0CCE16B007E for ; Fri, 24 Sep 2021 09:10:40 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id B15798249980 for ; Fri, 24 Sep 2021 13:10:39 +0000 (UTC) X-FDA: 78622501398.04.481FB8B Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by imf30.hostedemail.com (Postfix) with ESMTP id 80845E001980 for ; Fri, 24 Sep 2021 13:10:38 +0000 (UTC) IronPort-Data: A9a23:6ZgjnaiTPqpuNoy33CxjH/1OX161CxEKZh0ujC45NGQNrF6WrkUOzmQWXTiBbv2LZDD0L9xyO9+/9E0G78TXx9Y1GQpornw8FHgiRejtX4rAdhiqV8+xwmwvdGo+toNGLICowPkcFhcwnT/wdOi8xZVA/fvQHOOkWbSYYnkZqTJME0/NtzoywobVvaY42bBVMyvV0T/Di5W31G2NglaYAUpIg063ky6Didyp0N8uUvPSUtgQ1LPWvyF94JvyvshdJVOgKmVfNrbSq+ouUNiEEm3lExcFUrtJk57wdAsEX7zTIROTzHFRXsBOgDAb/mprjPl9b6FaNC+7iB3Q9zx14M9QvJqrWEEnOLbQsOoAURhECDw4NqpDkFPCCSHm7Z3KkBGYKhMAxN0rVinaJ7Yw9u9pAG1m++YfLTcXZBGfwemxxdqTSuJsrsUlItPiMI4Wtjdn1z6xJfovR9bBBbrL4dtZ1TIrrsFIAfvaIcEebFJHYBbfZBtAElQaEpQzmKGvnHaXWzlZrk+F4K8yy2vNxQdylr/3P7L9fMKGRMBQtkKZvX7duWD4BAwKctCS11Kt8Huqi6nEnT7TX5gbH7m1sPVthTW7wm0VFQ1TW0C3rOe0jmagVN9FbU8Z4Cwjqe417kPDZt38WQCo5X2JpBgRX/JOHOAgrgKA0KzZ50CeHGdsZjpAbsE28d84XhQ02VKT2dDkHzpitPuSU331y1s+hVteIgBMdSlbO3BCFlBDvrHeTEgIpkqnZr5e/GSd1LUZwQ3N/g0= IronPort-HdrOrdr: A9a23:3g0GUK+B7SX+fB9Pg89uk+A8I+orL9Y04lQ7vn2YSXRuHPBw8Pre+sjztCWE8Qr5N0tBpTntAsW9qDbnhPtICOoqTNCftWvdyQiVxehZhOOIqVDd8m/Fh4pgPMxbEpSWZueeMbEDt7eZ3OCnKadc/PC3tLCvmfzFz2pgCSVja6Rb5Q9/DQqBe3cGPzVuNN4oEoaG/Mpbq36FcXQTVM6yAX4IRKztvNvO/aiWGyIuNlo27hWUlzO05PrfGxic5B0XVDRC2vMD3AH+4nTE2pk= X-IronPort-AV: E=Sophos;i="5.85,319,1624291200"; d="scan'208";a="114917461" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 24 Sep 2021 21:10:36 +0800 Received: from G08CNEXMBPEKD05.g08.fujitsu.local (unknown [10.167.33.204]) by cn.fujitsu.com (Postfix) with ESMTP id 5E28D4D0DC77; Fri, 24 Sep 2021 21:10:30 +0800 (CST) Received: from G08CNEXCHPEKD09.g08.fujitsu.local (10.167.33.85) by G08CNEXMBPEKD05.g08.fujitsu.local (10.167.33.204) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Fri, 24 Sep 2021 21:10:26 +0800 Received: from irides.mr.mr.mr (10.167.225.141) by G08CNEXCHPEKD09.g08.fujitsu.local (10.167.33.209) with Microsoft SMTP Server id 15.0.1497.23 via Frontend Transport; Fri, 24 Sep 2021 21:10:23 +0800 From: Shiyang Ruan To: , , , , CC: , , , , Subject: [PATCH v7 7/8] xfs: Implement ->notify_failure() for XFS Date: Fri, 24 Sep 2021 21:09:58 +0800 Message-ID: <20210924130959.2695749-8-ruansy.fnst@fujitsu.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210924130959.2695749-1-ruansy.fnst@fujitsu.com> References: <20210924130959.2695749-1-ruansy.fnst@fujitsu.com> MIME-Version: 1.0 X-yoursite-MailScanner-ID: 5E28D4D0DC77.A27F1 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: ruansy.fnst@fujitsu.com X-Rspamd-Queue-Id: 80845E001980 X-Stat-Signature: ihkijs5sag6ehu96fzsgg1z1kbk5ya5z Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=fujitsu.com (policy=none); spf=none (imf30.hostedemail.com: domain of ruansy.fnst@fujitsu.com has no SPF policy when checking 183.91.158.132) smtp.mailfrom=ruansy.fnst@fujitsu.com X-Rspamd-Server: rspam06 X-HE-Tag: 1632489038-749418 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This function is used to handle errors which may cause data lost in filesystem. Such as memory failure in fsdax mode. If the rmap feature of XFS enabled, we can query it to find files and metadata which are associated with the corrupt data. For now all we do is kill processes with that file mapped into their address spaces, but future patches could actually do something about corrupt metadata. After that, the memory failure needs to notify the processes who are using those files. Signed-off-by: Shiyang Ruan --- drivers/dax/super.c | 19 +++++ fs/xfs/xfs_fsops.c | 3 + fs/xfs/xfs_mount.h | 1 + fs/xfs/xfs_super.c | 188 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/dax.h | 18 +++++ 5 files changed, 229 insertions(+) diff --git a/drivers/dax/super.c b/drivers/dax/super.c index 7d4a11dcba90..22091e7fb0ef 100644 --- a/drivers/dax/super.c +++ b/drivers/dax/super.c @@ -135,6 +135,25 @@ struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev) } EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev); +void fs_dax_register_holder(struct dax_device *dax_dev, void *holder, + const struct dax_holder_operations *ops) +{ + dax_set_holder(dax_dev, holder, ops); +} +EXPORT_SYMBOL_GPL(fs_dax_register_holder); + +void fs_dax_unregister_holder(struct dax_device *dax_dev) +{ + dax_set_holder(dax_dev, NULL, NULL); +} +EXPORT_SYMBOL_GPL(fs_dax_unregister_holder); + +void *fs_dax_get_holder(struct dax_device *dax_dev) +{ + return dax_get_holder(dax_dev); +} +EXPORT_SYMBOL_GPL(fs_dax_get_holder); + bool generic_fsdax_supported(struct dax_device *dax_dev, struct block_device *bdev, int blocksize, sector_t start, sector_t sectors) diff --git a/fs/xfs/xfs_fsops.c b/fs/xfs/xfs_fsops.c index 33e26690a8c4..4c2d3d4ca5a5 100644 --- a/fs/xfs/xfs_fsops.c +++ b/fs/xfs/xfs_fsops.c @@ -542,6 +542,9 @@ xfs_do_force_shutdown( } else if (flags & SHUTDOWN_CORRUPT_INCORE) { tag = XFS_PTAG_SHUTDOWN_CORRUPT; why = "Corruption of in-memory data"; + } else if (flags & SHUTDOWN_CORRUPT_META) { + tag = XFS_PTAG_SHUTDOWN_CORRUPT; + why = "Corruption of on-disk metadata"; } else { tag = XFS_PTAG_SHUTDOWN_IOERROR; why = "Metadata I/O Error"; diff --git a/fs/xfs/xfs_mount.h b/fs/xfs/xfs_mount.h index e091f3b3fa15..d0f6da23e3df 100644 --- a/fs/xfs/xfs_mount.h +++ b/fs/xfs/xfs_mount.h @@ -434,6 +434,7 @@ void xfs_do_force_shutdown(struct xfs_mount *mp, int flags, char *fname, #define SHUTDOWN_LOG_IO_ERROR 0x0002 /* write attempt to the log failed */ #define SHUTDOWN_FORCE_UMOUNT 0x0004 /* shutdown from a forced unmount */ #define SHUTDOWN_CORRUPT_INCORE 0x0008 /* corrupt in-memory data structures */ +#define SHUTDOWN_CORRUPT_META 0x0010 /* corrupt metadata on device */ #define XFS_SHUTDOWN_STRINGS \ { SHUTDOWN_META_IO_ERROR, "metadata_io" }, \ diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c index c4e0cd1c1c8c..46fdf44b5ec2 100644 --- a/fs/xfs/xfs_super.c +++ b/fs/xfs/xfs_super.c @@ -37,11 +37,19 @@ #include "xfs_reflink.h" #include "xfs_pwork.h" #include "xfs_ag.h" +#include "xfs_alloc.h" +#include "xfs_rmap.h" +#include "xfs_rmap_btree.h" +#include "xfs_rtalloc.h" +#include "xfs_bit.h" #include #include #include +#include +#include +static const struct dax_holder_operations xfs_dax_holder_operations; static const struct super_operations xfs_super_operations; static struct kset *xfs_kset; /* top-level xfs sysfs dir */ @@ -377,6 +385,8 @@ xfs_close_devices( xfs_free_buftarg(mp->m_logdev_targp); xfs_blkdev_put(logdev); + if (dax_logdev) + fs_dax_unregister_holder(dax_logdev); fs_put_dax(dax_logdev); } if (mp->m_rtdev_targp) { @@ -385,9 +395,13 @@ xfs_close_devices( xfs_free_buftarg(mp->m_rtdev_targp); xfs_blkdev_put(rtdev); + if (dax_rtdev) + fs_dax_unregister_holder(dax_rtdev); fs_put_dax(dax_rtdev); } xfs_free_buftarg(mp->m_ddev_targp); + if (dax_ddev) + fs_dax_unregister_holder(dax_ddev); fs_put_dax(dax_ddev); } @@ -411,6 +425,9 @@ xfs_open_devices( struct block_device *logdev = NULL, *rtdev = NULL; int error; + if (dax_ddev) + fs_dax_register_holder(dax_ddev, mp, + &xfs_dax_holder_operations); /* * Open real time and log devices - order is important. */ @@ -419,6 +436,9 @@ xfs_open_devices( if (error) goto out; dax_logdev = fs_dax_get_by_bdev(logdev); + if (dax_logdev != dax_ddev && dax_logdev) + fs_dax_register_holder(dax_logdev, mp, + &xfs_dax_holder_operations); } if (mp->m_rtname) { @@ -433,6 +453,9 @@ xfs_open_devices( goto out_close_rtdev; } dax_rtdev = fs_dax_get_by_bdev(rtdev); + if (dax_rtdev) + fs_dax_register_holder(dax_rtdev, mp, + &xfs_dax_holder_operations); } /* @@ -1110,6 +1133,171 @@ xfs_fs_free_cached_objects( return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan); } +struct notify_failure_info { + xfs_agblock_t startblock; + xfs_filblks_t blockcount; + int flags; +}; + +static loff_t +xfs_notify_failure_start( + struct xfs_mount *mp, + const struct xfs_rmap_irec *rec, + const struct notify_failure_info *notify) +{ + loff_t start = rec->rm_offset; + + if (notify->startblock > rec->rm_startblock) + start += XFS_FSB_TO_B(mp, + notify->startblock - rec->rm_startblock); + return start; +} + +static size_t +xfs_notify_failure_size( + struct xfs_mount *mp, + const struct xfs_rmap_irec *rec, + const struct notify_failure_info *notify) +{ + xfs_agblock_t rec_start = rec->rm_startblock; + xfs_agblock_t rec_end = rec->rm_startblock + rec->rm_blockcount; + xfs_agblock_t notify_start = notify->startblock; + xfs_agblock_t notify_end = notify->startblock + notify->blockcount; + xfs_agblock_t cross_start = max(rec_start, notify_start); + xfs_agblock_t cross_end = min(rec_end, notify_end); + + return XFS_FSB_TO_B(mp, cross_end - cross_start); +} + +static int +xfs_dax_notify_failure_fn( + struct xfs_btree_cur *cur, + const struct xfs_rmap_irec *rec, + void *data) +{ + struct xfs_mount *mp = cur->bc_mp; + struct xfs_inode *ip; + struct address_space *mapping; + struct notify_failure_info *notify = data; + int error = 0; + + if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || + (rec->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))) { + // TODO check and try to fix metadata + xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_META); + return -EFSCORRUPTED; + } + + /* Get files that incore, filter out others that are not in use. */ + error = xfs_iget(mp, cur->bc_tp, rec->rm_owner, XFS_IGET_INCORE, + 0, &ip); + if (error) + return error; + + mapping = VFS_I(ip)->i_mapping; + if (IS_ENABLED(CONFIG_MEMORY_FAILURE)) { + loff_t offset = xfs_notify_failure_start(mp, rec, notify); + size_t size = xfs_notify_failure_size(mp, rec, notify); + + error = mf_dax_kill_procs(mapping, offset >> PAGE_SHIFT, size, + notify->flags); + } + // TODO try to fix data + xfs_irele(ip); + + return error; +} + +static loff_t +xfs_dax_bdev_offset( + struct xfs_mount *mp, + struct dax_device *dax_dev, + loff_t disk_offset) +{ + struct block_device *bdev; + + if (mp->m_ddev_targp->bt_daxdev == dax_dev) + bdev = mp->m_ddev_targp->bt_bdev; + else if (mp->m_logdev_targp->bt_daxdev == dax_dev) + bdev = mp->m_logdev_targp->bt_bdev; + else + bdev = mp->m_rtdev_targp->bt_bdev; + + return disk_offset - (get_start_sect(bdev) << SECTOR_SHIFT); +} + +static int +xfs_dax_notify_failure( + struct dax_device *dax_dev, + loff_t offset, + size_t len, + int flags) +{ + struct xfs_mount *mp = fs_dax_get_holder(dax_dev); + struct xfs_trans *tp = NULL; + struct xfs_btree_cur *cur = NULL; + struct xfs_buf *agf_bp = NULL; + struct xfs_rmap_irec rmap_low, rmap_high; + loff_t bdev_offset = xfs_dax_bdev_offset(mp, dax_dev, + offset); + xfs_fsblock_t fsbno = XFS_B_TO_FSB(mp, bdev_offset); + xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, fsbno); + int error = 0; + struct notify_failure_info notify = { + .startblock = XFS_FSB_TO_AGBNO(mp, fsbno), + .blockcount = XFS_B_TO_FSB(mp, len), + .flags = flags, + }; + + if (mp->m_rtdev_targp && mp->m_rtdev_targp->bt_daxdev == dax_dev) { + xfs_warn(mp, + "notify_failure() not supported on realtime device!"); + return -EOPNOTSUPP; + } + + if (mp->m_logdev_targp && mp->m_logdev_targp->bt_daxdev == dax_dev && + mp->m_logdev_targp != mp->m_ddev_targp) { + xfs_err(mp, "ondisk log corrupt, shutting down fs!"); + xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_META); + return -EFSCORRUPTED; + } + + if (!xfs_has_rmapbt(mp)) { + xfs_warn(mp, "notify_failure() needs rmapbt enabled!"); + return -EOPNOTSUPP; + } + + error = xfs_trans_alloc_empty(mp, &tp); + if (error) + return error; + + error = xfs_alloc_read_agf(mp, tp, agno, 0, &agf_bp); + if (error) + goto out_cancel_tp; + + cur = xfs_rmapbt_init_cursor(mp, tp, agf_bp, agf_bp->b_pag); + + /* Construct a range for rmap query */ + memset(&rmap_low, 0, sizeof(rmap_low)); + memset(&rmap_high, 0xFF, sizeof(rmap_high)); + rmap_low.rm_startblock = rmap_high.rm_startblock = notify.startblock; + rmap_low.rm_blockcount = rmap_high.rm_blockcount = notify.blockcount; + + error = xfs_rmap_query_range(cur, &rmap_low, &rmap_high, + xfs_dax_notify_failure_fn, ¬ify); + + xfs_btree_del_cursor(cur, error); + xfs_trans_brelse(tp, agf_bp); + +out_cancel_tp: + xfs_trans_cancel(tp); + return error; +} + +static const struct dax_holder_operations xfs_dax_holder_operations = { + .notify_failure = xfs_dax_notify_failure, +}; + static const struct super_operations xfs_super_operations = { .alloc_inode = xfs_fs_alloc_inode, .destroy_inode = xfs_fs_destroy_inode, diff --git a/include/linux/dax.h b/include/linux/dax.h index 3d90becbd160..0f1fa7a4a616 100644 --- a/include/linux/dax.h +++ b/include/linux/dax.h @@ -149,6 +149,10 @@ static inline void fs_put_dax(struct dax_device *dax_dev) } struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev); +void fs_dax_register_holder(struct dax_device *dax_dev, void *holder, + const struct dax_holder_operations *ops); +void fs_dax_unregister_holder(struct dax_device *dax_dev); +void *fs_dax_get_holder(struct dax_device *dax_dev); int dax_writeback_mapping_range(struct address_space *mapping, struct dax_device *dax_dev, struct writeback_control *wbc); @@ -179,6 +183,20 @@ static inline struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev) return NULL; } +static inline void fs_dax_register_holder(struct dax_device *dax_dev, + void *holder, const struct dax_holder_operations *ops) +{ +} + +static inline void fs_dax_unregister_holder(struct dax_device *dax_dev) +{ +} + +static inline void *fs_dax_get_holder(struct dax_device *dax_dev) +{ + return NULL; +} + static inline struct page *dax_layout_busy_page(struct address_space *mapping) { return NULL;