From patchwork Thu Nov 12 09:10:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Margaine X-Patchwork-Id: 7600571 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 42A5FBF90C for ; Thu, 12 Nov 2015 09:11:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 43D272061C for ; Thu, 12 Nov 2015 09:11:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F403520616 for ; Thu, 12 Nov 2015 09:11:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753964AbbKLJLA (ORCPT ); Thu, 12 Nov 2015 04:11:00 -0500 Received: from mail-wm0-f46.google.com ([74.125.82.46]:36235 "EHLO mail-wm0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753945AbbKLJKx (ORCPT ); Thu, 12 Nov 2015 04:10:53 -0500 Received: by wmww144 with SMTP id w144so191417049wmw.1 for ; Thu, 12 Nov 2015 01:10:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=commerceguys.com; s=2012; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-type; bh=GFLWnqz69fLTpS6051y9isD+C7w2NVGE0Uts9e2eCGg=; b=OQ+n9AIiidsCvMO6m5XSpgTINFAvOb6ZTVlawgAY12eNksFfqj4oX+rtWwlptkBCDt ZYaUEIrc5kXZvvyPBN2Kh1PtQKyUdh7EJ7tU9PCS0Bs9G19q+tfAr/hoZ4DVjcMbD5A5 zwqt1v7KY+db5UpjCSpNCQQlb4T9oUACAnkwM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-type; bh=GFLWnqz69fLTpS6051y9isD+C7w2NVGE0Uts9e2eCGg=; b=RbjfeN+d4tUTg4KWPb1xOCQuK08CYfsws1LnI6rFxNDIqj5D+Z5ARhHV2Sppazc+NF xOpI6uiJvOkkg0ZlzrHu6HCJWR5LqHles4JxyPKPqu11gCMpoDcoHkEo6fT4jj4K7TUd s8rrBdKpkz8mff+ZFjanxvtmgdMJYMkq+wK6HV8DYtt40qO5kcIjHkIhwBSLeRs/cKav 75+h0ud0E3Br+D/2cCIViuZsWD0bnhivNmvjRyqSJWT93X6uAzkkuvRRl+HVq6dzMNcc zUftrtArBcvzSGcbJ/zZggjk0goQg86HlGQuyEOUxa9cLAGYBpHNE1+aGIk1bg3nArYw xC6A== X-Gm-Message-State: ALoCoQmk4r5z7hM295RC3OseQ5kqBMXQBvS7b5TwghhygcsbNLmJ8wfhgrTIjyDZll3gTgRaY2M3 X-Received: by 10.194.174.202 with SMTP id bu10mr17827418wjc.74.1447319452263; Thu, 12 Nov 2015 01:10:52 -0800 (PST) Received: from [192.168.6.235] ([92.103.57.251]) by smtp.gmail.com with ESMTPSA id l128sm21573218wmf.10.2015.11.12.01.10.51 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Nov 2015 01:10:51 -0800 (PST) Subject: Re: [PATCH] btrfs-progs: Fix partitioned loop devices resolve. To: Karel Zak References: <56409A52.2000708@commerceguys.com> <20151109141256.GW19508@ws.net.home> <5641AC4A.2090703@commerceguys.com> <20151110115018.GY19508@ws.net.home> Cc: linux-btrfs@vger.kernel.org From: Florian Margaine Message-ID: <5644579B.3000102@commerceguys.com> Date: Thu, 12 Nov 2015 10:10:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.3.0 MIME-Version: 1.0 In-Reply-To: <20151110115018.GY19508@ws.net.home> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,T_TVD_MIME_EPI, 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 New patch is attached. On 11/10/2015 12:50 PM, Karel Zak wrote: > > Yep, first try /sys/... and when unsuccessful then try ioctl. > > losetup example: > https://github.com/karelzak/util-linux/blob/master/lib/loopdev.c#L686 > > (it's probably too complex, but the basic idea is obvious) > > Maybe we need libloop.so to share all these things between various > project :-) That would be lovely indeed. > > Karel > Regards, When using partitions on a loop device, the device's name can be /dev/loop0p1 or similar, and no relevant entry exists in the /sys filesystem, so the current resolve_loop_device function fails. This patch adds a fallback to using loopdev API which will get the correct backing file even for partitioned loop devices, at the expense of using more memory, hence using it as a fallback only. --- utils.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/utils.c b/utils.c index d546bea..5e4813d 100644 --- a/utils.c +++ b/utils.c @@ -1170,2 +1170,2 @@ static int is_loop_device (const char* device) { MAJOR(statbuf.st_rdev) == LOOP_MAJOR); } +/* + * Takes a loop device path (e.g. /dev/loop0) and returns + * the associated file (e.g. /images/my_btrfs.img) using + * loopdev API + */ +static int resolve_loop_device_with_loopdev(const char* loop_dev, char* loop_file) +{ + int fd; + struct loop_info64 lo64; + + if (!(fd = open(loop_dev, O_RDONLY | O_NONBLOCK))) + return -errno; + if (ioctl(fd, LOOP_GET_STATUS64, &lo64) != 0) + return -errno; + + memcpy(loop_file, lo64.lo_file_name, strlen(lo64.lo_file_name) + 1); + if (close(fd) != 0) + return -errno; + + return 0; +} /* Takes a loop device path (e.g. /dev/loop0) and returns * the associated file (e.g. /images/my_btrfs.img) */ @@ -1185,5 +1206,10 @@ static int resolve_loop_device(const char* loop_dev, char* loop_file, if (!realpath(loop_dev, real_loop_dev)) return -errno; snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/')); - if (!(f = fopen(p, "r"))) + if (!(f = fopen(p, "r"))) { + if (errno == ENOENT) + /* It's possibly a partitioned loop device, which + is resolvable with loopdev API. */ + return resolve_loop_device_with_loopdev(loop_dev, loop_file); return -errno; + } snprintf(fmt, 20, "%%%i[^\n]", max_len-1); ret = fscanf(f, fmt, loop_file); -- 2.6.1