From patchwork Sat Jan 6 01:52:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 10147515 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 5C84860134 for ; Sat, 6 Jan 2018 01:54:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D5B4928997 for ; Sat, 6 Jan 2018 01:54:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CA971289B9; Sat, 6 Jan 2018 01:54:23 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY 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 AC70328997 for ; Sat, 6 Jan 2018 01:54:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753703AbeAFByU (ORCPT ); Fri, 5 Jan 2018 20:54:20 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:43491 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753698AbeAFByQ (ORCPT ); Fri, 5 Jan 2018 20:54:16 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.21/8.16.0.21) with SMTP id w061qnJ3016481; Sat, 6 Jan 2018 01:54:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2017-10-26; bh=F4cY/9is2Tn6//isSkhFtv8+DpDB+rwXWs47JFu42xY=; b=CZyGfguFvYQ2QyqBStM4eRPY9BlVx9JBBFSd/E6YfrK92YUxr9MPT/ZjnxNMSMc2FwSh L2U2BvkZ3aQNvOcK9B4FSIgY1Y3bW5as+0DS2KYlu/zNjtCKiixPOxgggTsmgYek1iAn URo3RB08UsEdiTtnQ2XF7d5V/u6H7SE+NxVJydtn6Iyi2CcVbRi1nrYoTUelA5fmbGfn 4mWuPEd5BAl45PC4PefKyHVnqQ+gewB13UQURD1oxPbTaENs42mNGmdLdVXE3OvGpNvi atDpXhoP0vdUqV3GCjA7R8qt3SEGdz6XftA/YEcHKRdzb6Ukw875nF8v+UlBzZpgmMmB Hg== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp2130.oracle.com with ESMTP id 2fam6ar2xf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 06 Jan 2018 01:54:14 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w061qbQr023156 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Sat, 6 Jan 2018 01:52:37 GMT Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w061qaTM017590; Sat, 6 Jan 2018 01:52:36 GMT Received: from localhost (/65.154.186.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 05 Jan 2018 17:52:36 -0800 Subject: [PATCH 11/27] xfs_scrub: filesystem counter collection functions From: "Darrick J. Wong" To: sandeen@redhat.com, darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Fri, 05 Jan 2018 17:52:35 -0800 Message-ID: <151520355552.2027.1869080885589442013.stgit@magnolia> In-Reply-To: <151520348769.2027.9860697266310422360.stgit@magnolia> References: <151520348769.2027.9860697266310422360.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8765 signatures=668651 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1801060020 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Darrick J. Wong Add a couple of helper functions to estimate the inode and block counters on the filesystem. Signed-off-by: Darrick J. Wong --- scrub/Makefile | 2 scrub/fscounters.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++ scrub/fscounters.h | 29 +++++++ 3 files changed, 243 insertions(+) create mode 100644 scrub/fscounters.c create mode 100644 scrub/fscounters.h -- To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/scrub/Makefile b/scrub/Makefile index a3534e6..5397339 100644 --- a/scrub/Makefile +++ b/scrub/Makefile @@ -19,6 +19,7 @@ HFILES = \ common.h \ disk.h \ filemap.h \ +fscounters.h \ inodes.h \ spacemap.h \ xfs_scrub.h @@ -27,6 +28,7 @@ CFILES = \ common.c \ disk.c \ filemap.c \ +fscounters.c \ inodes.c \ phase1.c \ spacemap.c \ diff --git a/scrub/fscounters.c b/scrub/fscounters.c new file mode 100644 index 0000000..4294bf3 --- /dev/null +++ b/scrub/fscounters.c @@ -0,0 +1,212 @@ +/* + * Copyright (C) 2018 Oracle. All Rights Reserved. + * + * Author: Darrick J. Wong + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ +#include +#include +#include +#include +#include +#include "platform_defs.h" +#include "xfs.h" +#include "xfs_arch.h" +#include "xfs_fs.h" +#include "xfs_format.h" +#include "path.h" +#include "workqueue.h" +#include "xfs_scrub.h" +#include "common.h" +#include "fscounters.h" + +/* + * Filesystem counter collection routines. We can count the number of + * inodes in the filesystem, and we can estimate the block counters. + */ + +/* Count the number of inodes in the filesystem. */ + +/* INUMBERS wrapper routines. */ +struct xfs_count_inodes { + bool moveon; + uint64_t counters[0]; +}; + +/* + * Count the number of inodes. Use INUMBERS to figure out how many inodes + * exist in the filesystem, assuming we've already scrubbed that. + */ +static bool +xfs_count_inodes_range( + struct scrub_ctx *ctx, + const char *descr, + uint64_t first_ino, + uint64_t last_ino, + uint64_t *count) +{ + struct xfs_fsop_bulkreq igrpreq = {0}; + struct xfs_inogrp inogrp; + __u64 igrp_ino; + uint64_t nr = 0; + __s32 igrplen = 0; + int error; + + ASSERT(!(first_ino & (XFS_INODES_PER_CHUNK - 1))); + ASSERT((last_ino & (XFS_INODES_PER_CHUNK - 1))); + + igrpreq.lastip = &igrp_ino; + igrpreq.icount = 1; + igrpreq.ubuffer = &inogrp; + igrpreq.ocount = &igrplen; + + igrp_ino = first_ino; + error = ioctl(ctx->mnt_fd, XFS_IOC_FSINUMBERS, &igrpreq); + while (!error && igrplen && inogrp.xi_startino < last_ino) { + nr += inogrp.xi_alloccount; + error = ioctl(ctx->mnt_fd, XFS_IOC_FSINUMBERS, &igrpreq); + } + + if (error) { + str_errno(ctx, descr); + return false; + } + + *count = nr; + return true; +} + +/* Scan all the inodes in an AG. */ +static void +xfs_count_ag_inodes( + struct workqueue *wq, + xfs_agnumber_t agno, + void *arg) +{ + struct xfs_count_inodes *ci = arg; + struct scrub_ctx *ctx = (struct scrub_ctx *)wq->wq_ctx; + char descr[DESCR_BUFSZ]; + uint64_t ag_ino; + uint64_t next_ag_ino; + bool moveon; + + snprintf(descr, DESCR_BUFSZ, _("dev %d:%d AG %u inodes"), + major(ctx->fsinfo.fs_datadev), + minor(ctx->fsinfo.fs_datadev), + agno); + + ag_ino = (__u64)agno << (ctx->inopblog + ctx->agblklog); + next_ag_ino = (__u64)(agno + 1) << (ctx->inopblog + ctx->agblklog); + + moveon = xfs_count_inodes_range(ctx, descr, ag_ino, next_ag_ino - 1, + &ci->counters[agno]); + if (!moveon) + ci->moveon = false; +} + +/* Count all the inodes in a filesystem. */ +bool +xfs_count_all_inodes( + struct scrub_ctx *ctx, + uint64_t *count) +{ + struct xfs_count_inodes *ci; + xfs_agnumber_t agno; + struct workqueue wq; + bool moveon; + int ret; + + ci = calloc(1, sizeof(struct xfs_count_inodes) + + (ctx->geo.agcount * sizeof(uint64_t))); + if (!ci) + return false; + ci->moveon = true; + + ret = workqueue_create(&wq, (struct xfs_mount *)ctx, + scrub_nproc_workqueue(ctx)); + if (ret) { + moveon = false; + str_error(ctx, ctx->mntpoint, _("Could not create workqueue.")); + goto out_free; + } + for (agno = 0; agno < ctx->geo.agcount; agno++) { + ret = workqueue_add(&wq, xfs_count_ag_inodes, agno, ci); + if (ret) { + moveon = false; + str_error(ctx, ctx->mntpoint, +_("Could not queue AG %u icount work."), agno); + break; + } + } + workqueue_destroy(&wq); + + for (agno = 0; agno < ctx->geo.agcount; agno++) + *count += ci->counters[agno]; + moveon = ci->moveon; + +out_free: + free(ci); + return moveon; +} + +/* Estimate the number of blocks and inodes in the filesystem. */ +bool +xfs_scan_estimate_blocks( + struct scrub_ctx *ctx, + unsigned long long *d_blocks, + unsigned long long *d_bfree, + unsigned long long *r_blocks, + unsigned long long *r_bfree, + unsigned long long *f_files, + unsigned long long *f_free) +{ + struct xfs_fsop_counts fc; + struct xfs_fsop_resblks rb; + struct statvfs sfs; + int error; + + /* Grab the fstatvfs counters, since it has to report accurately. */ + error = fstatvfs(ctx->mnt_fd, &sfs); + if (error) { + str_errno(ctx, ctx->mntpoint); + return false; + } + + /* Fetch the filesystem counters. */ + error = ioctl(ctx->mnt_fd, XFS_IOC_FSCOUNTS, &fc); + if (error) { + str_errno(ctx, ctx->mntpoint); + return false; + } + + /* + * XFS reserves some blocks to prevent hard ENOSPC, so add those + * blocks back to the free data counts. + */ + error = ioctl(ctx->mnt_fd, XFS_IOC_GET_RESBLKS, &rb); + if (error) + str_errno(ctx, ctx->mntpoint); + sfs.f_bfree += rb.resblks_avail; + + *d_blocks = sfs.f_blocks + (ctx->geo.logstart ? ctx->geo.logblocks : 0); + *d_bfree = sfs.f_bfree; + *r_blocks = ctx->geo.rtblocks; + *r_bfree = fc.freertx; + *f_files = sfs.f_files; + *f_free = sfs.f_ffree; + + return true; +} diff --git a/scrub/fscounters.h b/scrub/fscounters.h new file mode 100644 index 0000000..40a4c05 --- /dev/null +++ b/scrub/fscounters.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2018 Oracle. All Rights Reserved. + * + * Author: Darrick J. Wong + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ +#ifndef XFS_SCRUB_FSCOUNTERS_H_ +#define XFS_SCRUB_FSCOUNTERS_H_ + +bool xfs_scan_estimate_blocks(struct scrub_ctx *ctx, + unsigned long long *d_blocks, unsigned long long *d_bfree, + unsigned long long *r_blocks, unsigned long long *r_bfree, + unsigned long long *f_files, unsigned long long *f_free); +bool xfs_count_all_inodes(struct scrub_ctx *ctx, uint64_t *count); + +#endif /* XFS_SCRUB_FSCOUNTERS_H_ */