From patchwork Sat Feb 3 13:16:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Benatto X-Patchwork-Id: 10198505 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 5FA3760388 for ; Sat, 3 Feb 2018 13:16:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 467B628FC4 for ; Sat, 3 Feb 2018 13:16:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A83C28FD0; Sat, 3 Feb 2018 13:16:52 +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_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 9972F28FC4 for ; Sat, 3 Feb 2018 13:16:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751644AbeBCNQr (ORCPT ); Sat, 3 Feb 2018 08:16:47 -0500 Received: from mail-qt0-f193.google.com ([209.85.216.193]:42485 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751530AbeBCNQq (ORCPT ); Sat, 3 Feb 2018 08:16:46 -0500 Received: by mail-qt0-f193.google.com with SMTP id c2so34336625qtn.9 for ; Sat, 03 Feb 2018 05:16:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=Drz8R7RYIT8a3W/5Xiq3vJTPOBYNuEL5LFpvOAc9ePo=; b=BPDufdwvi+6lSbIaSSkT+xXbO4CuKmTyMWrfR3WZf1dMXIsAr7apmtxR/vhx0al/kD 0+FOUNS+JU7K6qS+S2sSgHWRC8KmKwNo5CKL3H8e7RHXMTJmi9bFxbS2jT7vdB9Uv2cd cq4sp6Nllm5Hjycejh87zBBEKfwFC+Kw2/WYMCdJlZLB86XxLZuP+jb1itkJWvLvtBSg FpslYOZysNDCNsvjwEwQWF1pGfmUEjl/HvD+YDvxSpKn2b12/SHYhkY72UMHYOE9DL4o ty/IMSgWNIraMfSwNqsnIKCa8IKc326lR/+WHscWMtmWslW664Fe0uO3sCTD/sbiAp4s 4ESQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=Drz8R7RYIT8a3W/5Xiq3vJTPOBYNuEL5LFpvOAc9ePo=; b=TtMdzdbXkMvrdpg5e9HfwCvNQ27r3nMIZ6Y8wGJ5IblqFsSFCMV/EEj0IP4Lev8TY1 4ICMaKp4qDhhDA7gBrYso/cD5OB/fd0oOrivo5NQt3G5KrUX6oQv/d+e+Rdl+KywF/n3 ZGIn33UAp3PJ7h60wUAGqi8DSEWs7RWcoxH3gdzMhfPJmQSlbkxYPnnmVW5B42Ehqjwh ivh3CyBNdAjiie8HG2jFdP0RkR2s6nf8NFZnwgp6OijjuydvsFgMJXgExvN9B/qiJsMt aYpKZuR8x3KQZ8XGQyvPnJyPz/f9YI0CLGT/+x9PSpbZ10GgA2GYW4lhbAobCD0XRjv1 34Yw== X-Gm-Message-State: APf1xPA1bfmlpIi3UKZ6agO2oisM5WVwBAsXjKjE0LxV/1/VtWdE5Ywc ofHFcf1Z/1soL3gU8qotqYfxCg== X-Google-Smtp-Source: AH8x227ngATkFKpPSpZrZnr4S73CvGSYl475+8zlBAvA1p9ui00gce3DSXkOO4jI0SSxbWQmDb3mPA== X-Received: by 10.200.43.174 with SMTP id m43mr9773106qtm.6.1517663804998; Sat, 03 Feb 2018 05:16:44 -0800 (PST) Received: from testarossa.localdomain ([191.177.187.140]) by smtp.googlemail.com with ESMTPSA id o62sm2882583qke.20.2018.02.03.05.16.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 03 Feb 2018 05:16:43 -0800 (PST) From: Marco A Benatto To: linux-xfs@vger.kernel.org, darrick.wong@oracle.com Subject: [PATCH] xfs_mdrestore: Don't rewind source file stream Date: Sat, 3 Feb 2018 11:16:36 -0200 Message-Id: <1517663796-89208-1-git-send-email-marco.antonio.780@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <20180202202729.GC4849@magnolia> References: <20180202202729.GC4849@magnolia> 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 Today, xfs_mdrestore from stdin will fail if the -i flag is specified, because it attempts to rewind the stream after the initial read of the metablock. This fails, and results in an abort with "specified file is not a metadata dump." Read the metablock exactly once in main(), validate the magic, print informational flags if requested, and then pass it to perform_restore() which will then continue the restore process. Reported-by: Darrick J. Wong Signed-off-by: Marco A Benatto --- mdrestore/xfs_mdrestore.c | 55 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/mdrestore/xfs_mdrestore.c b/mdrestore/xfs_mdrestore.c index 0bb4ac8..3db740d 100644 --- a/mdrestore/xfs_mdrestore.c +++ b/mdrestore/xfs_mdrestore.c @@ -51,35 +51,34 @@ print_progress(const char *fmt, ...) progress_since_warning = 1; } +/* + * perform_restore() -- do the actual work to restore the metadump + * + * @src_f: A FILE pointer to the source metadump + * @dst_fd: the file descriptor for the target file + * @is_target_file: designates whether the target is a regular file + * @mbp: pointer to metadump's first xfs_metablock, read and verified by the caller + * + * src_f should be positioned just past a read the previously validated metablock + */ static void perform_restore( FILE *src_f, int dst_fd, - int is_target_file) + int is_target_file, + const struct xfs_metablock *mbp) { - xfs_metablock_t *metablock; /* header + index + blocks */ + struct xfs_metablock *metablock, tmb; /* header + index + blocks */ __be64 *block_index; char *block_buffer; int block_size; int max_indices; int cur_index; int mb_count; - xfs_metablock_t tmb; xfs_sb_t sb; int64_t bytes_read; - /* - * read in first blocks (superblock 0), set "inprogress" flag for it, - * read in the rest of the file, and if complete, clear SB 0's - * "inprogress flag" - */ - - if (fread(&tmb, sizeof(tmb), 1, src_f) != 1) - fatal("error reading from file: %s\n", strerror(errno)); - - if (be32_to_cpu(tmb.mb_magic) != XFS_MD_MAGIC) - fatal("specified file is not a metadata dump\n"); - + tmb = *mbp; block_size = 1 << tmb.mb_blocklog; max_indices = (block_size - sizeof(xfs_metablock_t)) / sizeof(__be64); @@ -211,6 +210,7 @@ main( int open_flags; struct stat statbuf; int is_target_file; + xfs_metablock_t mb; progname = basename(argv[0]); @@ -237,7 +237,12 @@ main( if (!show_info && argc - optind != 2) usage(); - /* open source */ + /* + * open source and test if this really is a dump. The first metadump block + * will be passed to perform_restore() which will continue to read the + * file from this point. This avoids rewind the stream, which causes + * restore to fail when source was being read from stdin. + */ if (strcmp(argv[optind], "-") == 0) { src_f = stdin; if (isatty(fileno(stdin))) @@ -248,15 +253,12 @@ main( fatal("cannot open source dump file\n"); } - if (show_info) { - xfs_metablock_t mb; - - if (fread(&mb, sizeof(mb), 1, src_f) != 1) - fatal("error reading from file: %s\n", strerror(errno)); - - if (be32_to_cpu(mb.mb_magic) != XFS_MD_MAGIC) - fatal("specified file is not a metadata dump\n"); + if (fread(&mb, sizeof(mb), 1, src_f) != 1) + fatal("error reading from file: %s\n", strerror(errno)); + if (mb.mb_magic != cpu_to_be32(XFS_MD_MAGIC)) + fatal("specified file is not a metadata dump\n"); + if (show_info) { if (mb.mb_info & XFS_METADUMP_INFO_FLAGS) { printf("%s: %sobfuscated, %s log, %s metadata blocks\n", argv[optind], @@ -270,9 +272,6 @@ main( if (argc - optind == 1) exit(0); - - /* Go back to the beginning for the restore function */ - fseek(src_f, 0L, SEEK_SET); } optind++; @@ -301,7 +300,7 @@ main( if (dst_fd < 0) fatal("couldn't open target \"%s\"\n", argv[optind]); - perform_restore(src_f, dst_fd, is_target_file); + perform_restore(src_f, dst_fd, is_target_file, &mb); close(dst_fd); if (src_f != stdin)