From patchwork Sun Jan 20 21:04:07 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gene Czarcinski X-Patchwork-Id: 2008661 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 40F6F40E35 for ; Sun, 20 Jan 2013 21:05:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752581Ab3ATVFE (ORCPT ); Sun, 20 Jan 2013 16:05:04 -0500 Received: from eastrmfepo202.cox.net ([68.230.241.217]:51606 "EHLO eastrmfepo202.cox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752488Ab3ATVEi (ORCPT ); Sun, 20 Jan 2013 16:04:38 -0500 Received: from eastrmimpo209 ([68.230.241.224]) by eastrmfepo202.cox.net (InterMail vM.8.01.04.00 201-2260-137-20101110) with ESMTP id <20130120210437.PZD6475.eastrmfepo202.cox.net@eastrmimpo209> for ; Sun, 20 Jan 2013 16:04:37 -0500 Received: from falcon.lcl ([68.100.144.189]) by eastrmimpo209 with cox id qM4W1k00945PsQc01M4dfi; Sun, 20 Jan 2013 16:04:37 -0500 X-CT-Class: Clean X-CT-Score: 0.00 X-CT-RefID: str=0001.0A020209.50FC5BE5.0068,ss=1,re=0.000,fgs=0 X-CT-Spam: 0 X-Authority-Analysis: v=2.0 cv=E5JPVNhl c=1 sm=1 a=xiXiwr23JuvKlkj6ngz4TA==:17 a=r7CeFmvHzS4A:10 a=103jMkqsgCkA:10 a=BIJj-m-0AAAA:8 a=UVDCeuS066IA:10 a=gvl4HP8tAAAA:8 a=JFMd3qXr8Zvn4Hw7guQA:9 a=OTgC_HKJBnUA:10 a=LUue1KWhiSUA:10 a=xiXiwr23JuvKlkj6ngz4TA==:117 X-CM-Score: 0.00 Authentication-Results: cox.net; auth=pass (CRAM-MD5) smtp.auth=gczarcinski@cox.net From: Gene Czarcinski To: linux-btrfs@vger.kernel.org Cc: Nirbheek Chauhan , Gene Czarcinski Subject: [PATCH 02/13] Btrfs-progs: fix resolving of loop devices Date: Sun, 20 Jan 2013 16:04:07 -0500 Message-Id: <1358715858-4469-3-git-send-email-gene@czarc.net> X-Mailer: git-send-email 1.8.1 In-Reply-To: <1358715858-4469-1-git-send-email-gene@czarc.net> References: <1358715858-4469-1-git-send-email-gene@czarc.net> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Nirbheek Chauhan The LOOP_GET_STATUS ioctl truncates filenames to 64 characters. We should get the backing file for a given loop device from /sys/. This is how losetup does it as well. Signed-off-by: Gene Czarcinski Tested-By: Hector Oron --- utils.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/utils.c b/utils.c index 51b78d5..7953ef9 100644 --- a/utils.c +++ b/utils.c @@ -20,6 +20,7 @@ #define __USE_XOPEN2K #include #include +#include #ifndef __CHECKER__ #include #include @@ -653,21 +654,22 @@ int is_loop_device (const char* device) { * the associated file (e.g. /images/my_btrfs.img) */ int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len) { - int loop_fd; - int ret_ioctl; - struct loop_info loopinfo; + int ret; + FILE *f; + char fmt[20]; + char p[PATH_MAX]; + char real_loop_dev[PATH_MAX]; - if ((loop_fd = open(loop_dev, O_RDONLY)) < 0) + 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"))) return -errno; - ret_ioctl = ioctl(loop_fd, LOOP_GET_STATUS, &loopinfo); - close(loop_fd); - - if (ret_ioctl == 0) { - strncpy(loop_file, loopinfo.lo_name, max_len); - if (max_len > 0) - loop_file[max_len-1] = 0; - } else + snprintf(fmt, 20, "%%%i[^\n]", max_len-1); + ret = fscanf(f, fmt, loop_file); + fclose(f); + if (ret == EOF) return -errno; return 0;