From patchwork Sat Jan 6 01:52:04 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: 10147527 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 608A360155 for ; Sat, 6 Jan 2018 02:02:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC83A289D7 for ; Sat, 6 Jan 2018 02:02:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CFDD3289D8; Sat, 6 Jan 2018 02:02:11 +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 13D87289DC for ; Sat, 6 Jan 2018 02:02:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753536AbeAFCCK (ORCPT ); Fri, 5 Jan 2018 21:02:10 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:42568 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753494AbeAFCCK (ORCPT ); Fri, 5 Jan 2018 21:02:10 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.21/8.16.0.21) with SMTP id w06221mp054683; Sat, 6 Jan 2018 02:02:07 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=aSqYYy+9kn7mznl9ETLb/BzyVbhmllSLOZ896mMQidI=; b=VO1RxhFG3zka+PHZPGAAcADdSwD/mmgNXiN7FN4I+EF9sAenw61ZomuBV1exLIzTU+wM 5gwUY1CauzHrD3K4KAdA0k2f2+WQ7cj/ejPnwdMCfCLA674f0rN3/GWEks2h1AFeGwSQ 7iP2HKCJMINCFwbLDXRt9p7UxcyCQ/NfZtFe9xc1LxqRyPvEBPYnrl/TQTLI2DSG5zHY zmO3WKXOSjuVAwKdGF954oDKFwFwkG7q7O+/tG9LSyfbwoyirfP6o7utJ3G7hwxnKC5e ptr27oF+4D8iBcP42WfkhvgmC6sNEsChLNf+626crqm6oAGM7v6Rk5WcdZ0/8UQsTQGm lg== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp2120.oracle.com with ESMTP id 2fan7900d4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 06 Jan 2018 02:02:07 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w061q6CB008848 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Sat, 6 Jan 2018 01:52:06 GMT Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w061q59u007137; Sat, 6 Jan 2018 01:52:06 GMT Received: from localhost (/65.154.186.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 05 Jan 2018 17:52:05 -0800 Subject: [PATCH 06/27] xfs_scrub: create an abstraction for a block device 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:04 -0800 Message-ID: <151520352472.2027.8375038586822233334.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-1801060022 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 Create an abstraction to handle all of our low level disk operations. We'll eventually use it to bind to a fs mount point and block device. Signed-off-by: Darrick J. Wong --- scrub/Makefile | 2 + scrub/disk.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ scrub/disk.h | 39 +++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 scrub/disk.c create mode 100644 scrub/disk.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 097ec84..c3a9986 100644 --- a/scrub/Makefile +++ b/scrub/Makefile @@ -17,10 +17,12 @@ endif # scrub_prereqs HFILES = \ common.h \ +disk.h \ xfs_scrub.h CFILES = \ common.c \ +disk.c \ xfs_scrub.c LLDLIBS += $(LIBHANDLE) $(LIBFROG) $(LIBPTHREAD) diff --git a/scrub/disk.c b/scrub/disk.c new file mode 100644 index 0000000..d4bf81f --- /dev/null +++ b/scrub/disk.c @@ -0,0 +1,164 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include "platform_defs.h" +#include "libfrog.h" +#include "xfs_scrub.h" +#include "disk.h" + +/* + * Disk Abstraction + * + * These routines help us to discover the geometry of a block device, + * estimate the amount of concurrent IOs that we can send to it, and + * abstract the process of performing read verification of disk blocks. + */ + +/* Figure out how many disk heads are available. */ +static unsigned int +__disk_heads( + struct disk *disk) +{ + int iomin; + int ioopt; + unsigned short rot; + int error; + + /* If it's not a block device, throw all the CPUs at it. */ + if (!S_ISBLK(disk->d_sb.st_mode)) + return nproc; + + /* Non-rotational device? Throw all the CPUs. */ + rot = 1; + error = ioctl(disk->d_fd, BLKROTATIONAL, &rot); + if (error == 0 && rot == 0) + return nproc; + + /* + * Sometimes we can infer the number of devices from the + * min/optimal IO sizes. + */ + iomin = ioopt = 0; + if (ioctl(disk->d_fd, BLKIOMIN, &iomin) == 0 && + ioctl(disk->d_fd, BLKIOOPT, &ioopt) == 0 && + iomin > 0 && ioopt > 0) { + return min(nproc, max(1, ioopt / iomin)); + } + + /* Rotating device? I guess? */ + return 2; +} + +/* Figure out how many disk heads are available. */ +unsigned int +disk_heads( + struct disk *disk) +{ + if (nr_threads) + return nr_threads; + return __disk_heads(disk); +} + +/* Open a disk device and discover its geometry. */ +struct disk * +disk_open( + const char *pathname) +{ + struct disk *disk; + int lba_sz; + int error; + + disk = calloc(1, sizeof(struct disk)); + if (!disk) + return NULL; + + disk->d_fd = open(pathname, O_RDONLY | O_DIRECT | O_NOATIME); + if (disk->d_fd < 0) + goto out_free; + + /* Try to get LBA size. */ + error = ioctl(disk->d_fd, BLKSSZGET, &lba_sz); + if (error) + lba_sz = 512; + disk->d_lbalog = log2_roundup(lba_sz); + + /* Obtain disk's stat info. */ + error = fstat(disk->d_fd, &disk->d_sb); + if (error) + goto out_close; + + /* Determine bdev size, block size, and offset. */ + if (S_ISBLK(disk->d_sb.st_mode)) { + error = ioctl(disk->d_fd, BLKGETSIZE64, &disk->d_size); + if (error) + disk->d_size = 0; + error = ioctl(disk->d_fd, BLKBSZGET, &disk->d_blksize); + if (error) + disk->d_blksize = 0; + disk->d_start = 0; + } else { + disk->d_size = disk->d_sb.st_size; + disk->d_blksize = disk->d_sb.st_blksize; + disk->d_start = 0; + } + + return disk; +out_close: + close(disk->d_fd); +out_free: + free(disk); + return NULL; +} + +/* Close a disk device. */ +int +disk_close( + struct disk *disk) +{ + int error = 0; + + if (disk->d_fd >= 0) + error = close(disk->d_fd); + disk->d_fd = -1; + free(disk); + return error; +} + +/* Read-verify an extent of a disk device. */ +ssize_t +disk_read_verify( + struct disk *disk, + void *buf, + uint64_t start, + uint64_t length) +{ + return pread(disk->d_fd, buf, length, start); +} diff --git a/scrub/disk.h b/scrub/disk.h new file mode 100644 index 0000000..834678e --- /dev/null +++ b/scrub/disk.h @@ -0,0 +1,39 @@ +/* + * 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_DISK_H_ +#define XFS_SCRUB_DISK_H_ + +struct disk { + struct stat d_sb; + int d_fd; + int d_lbalog; + unsigned int d_flags; + unsigned int d_blksize; /* bytes */ + uint64_t d_size; /* bytes */ + uint64_t d_start; /* bytes */ +}; + +unsigned int disk_heads(struct disk *disk); +struct disk *disk_open(const char *pathname); +int disk_close(struct disk *disk); +ssize_t disk_read_verify(struct disk *disk, void *buf, uint64_t startblock, + uint64_t blockcount); + +#endif /* XFS_SCRUB_DISK_H_ */