linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Goffredo Baroncelli <kreijack@libero.it>
To: linux-btrfs@vger.kernel.org
Cc: Goffredo Baroncelli <kreijack@inwind.it>
Subject: [PATCH 1/3] btrfs-progs: remove use BLKGETSIZE64
Date: Sun, 15 Mar 2020 16:24:28 +0100	[thread overview]
Message-ID: <20200315152430.7532-2-kreijack@libero.it> (raw)
In-Reply-To: <20200315152430.7532-1-kreijack@libero.it>

From: Goffredo Baroncelli <kreijack@inwind.it>

Allow the get_partition_size() to be called without
root privileges.

Signed-off-by: Goffredo baroncelli <kreijack@inwind.it>
---
 common/device-utils.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/common/device-utils.c b/common/device-utils.c
index b03d62fa..9519dbce 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -17,6 +17,8 @@
 #include <sys/ioctl.h>
 #include <sys/mount.h>
 #include <sys/statfs.h>
+#include <sys/types.h>
+#include <sys/sysmacros.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -238,17 +240,28 @@ u64 disk_size(const char *path)
 
 u64 get_partition_size(const char *dev)
 {
-	u64 result;
-	int fd = open(dev, O_RDONLY);
+	struct stat statbuf;
+	int r;
+	int fd;
+	char buf[100];
 
-	if (fd < 0)
+	r = stat(dev, &statbuf);
+	if (r != 0)
 		return 0;
-	if (ioctl(fd, BLKGETSIZE64, &result) < 0) {
-		close(fd);
-		return 0;
-	}
+
+	snprintf(buf, sizeof(buf),
+		"/sys/dev/block/%d:%d/size", gnu_dev_major(statbuf.st_rdev),
+		 gnu_dev_minor(statbuf.st_rdev));
+
+	fd = open(buf, O_RDONLY);
+	BUG_ON(fd < 0);
+
+	r = read(fd, buf, sizeof(buf)-1);
 	close(fd);
 
-	return result;
+	BUG_ON(r < 0);
+	buf[r] = 0;
+
+	return atoll(buf) * 512;
 }
 
-- 
2.25.1


  reply	other threads:[~2020-03-15 15:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-15 15:24 [RFC] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
2020-03-15 15:24 ` Goffredo Baroncelli [this message]
2020-03-15 15:24 ` [PATCH 2/3] btrfs-progs: Add BTRFS_IOC_GET_CHUNK_INFO ioctl Goffredo Baroncelli
2020-03-15 15:24 ` [PATCH 3/3] btrfs-progs: use the new ioctl BTRFS_IOC_GET_CHUNK_INFO Goffredo Baroncelli
2020-03-25 20:12 ` [RFC] " Goffredo Baroncelli
2020-03-31 19:17 ` Goffredo Baroncelli
2020-03-31 19:18 ` Goffredo Baroncelli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200315152430.7532-2-kreijack@libero.it \
    --to=kreijack@libero.it \
    --cc=kreijack@inwind.it \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).