From patchwork Wed Oct 15 00:50:38 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 5084651 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 8587E9F349 for ; Wed, 15 Oct 2014 08:44:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6F74C20123 for ; Wed, 15 Oct 2014 08:44:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5841E20122 for ; Wed, 15 Oct 2014 08:44:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751287AbaJOIoJ (ORCPT ); Wed, 15 Oct 2014 04:44:09 -0400 Received: from userp1040.oracle.com ([156.151.31.81]:27135 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751232AbaJOIoG (ORCPT ); Wed, 15 Oct 2014 04:44:06 -0400 Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id s9F8i1TC025376 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 15 Oct 2014 08:44:02 GMT Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s9F8i0n3018858 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 15 Oct 2014 08:44:01 GMT Received: from abhmp0008.oracle.com (abhmp0008.oracle.com [141.146.116.14]) by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s9F8i08i020847; Wed, 15 Oct 2014 08:44:00 GMT Received: from OL.sg.oracle.com (/10.186.101.34) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 15 Oct 2014 01:44:00 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, clm@fb.com Subject: [PATCH 1/1] btrfs-progs: code optimize cmd_scan_dev() use btrfs_register_one_device() Date: Wed, 15 Oct 2014 08:50:38 +0800 Message-Id: <1413334238-25729-1-git-send-email-anand.jain@oracle.com> X-Mailer: git-send-email 2.0.0.153.g79dcccc X-Source-IP: acsinet21.oracle.com [141.146.126.237] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, DATE_IN_PAST_06_12, RCVD_IN_DNSWL_HI,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP cmd_scan_dev() has it own code to register device (calling ioctl BTRFS_IOC_SCAN_DEV), apparently it could use btrfs_register_one_device(). Signed-off-by: Anand Jain --- cmds-device.c | 33 ++++++--------------------------- utils.c | 8 +++++--- utils.h | 2 +- 3 files changed, 12 insertions(+), 31 deletions(-) diff --git a/cmds-device.c b/cmds-device.c index a728f21..57ad0b2 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -206,7 +206,7 @@ static const char * const cmd_scan_dev_usage[] = { static int cmd_scan_dev(int argc, char **argv) { - int i, fd, e; + int i; int devstart = 1; int all = 0; int ret = 0; @@ -242,22 +242,14 @@ static int cmd_scan_dev(int argc, char **argv) goto out; } - fd = open("/dev/btrfs-control", O_RDWR); - if (fd < 0) { - perror("failed to open /dev/btrfs-control"); - ret = 1; - goto out; - } - for( i = devstart ; i < argc ; i++ ){ - struct btrfs_ioctl_vol_args args; char *path; if (!is_block_device(argv[i])) { fprintf(stderr, "ERROR: %s is not a block device\n", argv[i]); ret = 1; - goto close_out; + goto out; } path = canonicalize_path(argv[i]); if (!path) { @@ -265,30 +257,17 @@ static int cmd_scan_dev(int argc, char **argv) "ERROR: Could not canonicalize path '%s': %s\n", argv[i], strerror(errno)); ret = 1; - goto close_out; + goto out; } printf("Scanning for Btrfs filesystems in '%s'\n", path); - - strncpy_null(args.name, path); - /* - * FIXME: which are the error code returned by this ioctl ? - * it seems that is impossible to understand if there no is - * a btrfs filesystem from an I/O error !!! - */ - ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args); - e = errno; - - if( ret < 0 ){ - fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", - path, strerror(e)); + if (btrfs_register_one_device(path) != 0) { + ret = 1; free(path); - goto close_out; + goto out; } free(path); } -close_out: - close(fd); out: return !!ret; } diff --git a/utils.c b/utils.c index 78cb93f..017c513 100644 --- a/utils.c +++ b/utils.c @@ -1236,7 +1236,7 @@ struct pending_dir { char name[PATH_MAX]; }; -void btrfs_register_one_device(char *fname) +int btrfs_register_one_device(char *fname) { struct btrfs_ioctl_vol_args args; int fd; @@ -1248,17 +1248,19 @@ void btrfs_register_one_device(char *fname) fprintf(stderr, "failed to open /dev/btrfs-control " "skipping device registration: %s\n", strerror(errno)); - return; + return -errno; } strncpy(args.name, fname, BTRFS_PATH_NAME_MAX); args.name[BTRFS_PATH_NAME_MAX-1] = 0; ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args); e = errno; - if(ret<0){ + if (ret < 0) { fprintf(stderr, "ERROR: device scan failed '%s' - %s\n", fname, strerror(e)); + ret = -e; } close(fd); + return ret; } int btrfs_device_already_in_root(struct btrfs_root *root, int fd, diff --git a/utils.h b/utils.h index 1e13b9d..3f7b388 100644 --- a/utils.h +++ b/utils.h @@ -81,7 +81,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, u64 block_count, u32 io_width, u32 io_align, u32 sectorsize); int btrfs_scan_for_fsid(int run_ioctls); -void btrfs_register_one_device(char *fname); +int btrfs_register_one_device(char *fname); char *canonicalize_dm_name(const char *ptname); char *canonicalize_path(const char *path); int check_mounted(const char *devicename);