linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andreas Dilger <adilger@whamcloud.com>
To: tytso@mit.edu
Cc: linux-ext4@vger.kernel.org,
	Andreas Dilger <adilger@whamcloud.com>,
	Andreas Dilger <adilger@dilger.ca>
Subject: [PATCH] misc: handle very large files with filefrag
Date: Tue, 11 Feb 2020 17:58:05 -0700	[thread overview]
Message-ID: <1581469085-85472-1-git-send-email-adilger@whamcloud.com> (raw)
In-Reply-To: <1581037786-62789-1-git-send-email-adilger@whamcloud.com>

Avoid overflowing the column-width calc printing files over 4B blocks.

Document the [KMG] suffixes for the "-b <blocksize>" option.

The blocksize is limited to at most 1GiB blocksize to avoid shifting
all extents down to zero GB in size.  Even the use of 1GB blocksize
is unlikely, but non-ext4 filesystems may use multi-GB extents.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Lustre-bug-id: https://jira.whamcloud.com/browse/LU-13197
---
 misc/filefrag.8.in |  4 ++--
 misc/filefrag.c    | 36 ++++++++++++++++++++++++------------
 2 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/misc/filefrag.8.in b/misc/filefrag.8.in
index 5292672..4b89e72 100644
--- a/misc/filefrag.8.in
+++ b/misc/filefrag.8.in
@@ -33,8 +33,8 @@ testing purposes.
 .BI \-b blocksize
 Use
 .I blocksize
-in bytes for output instead of the filesystem blocksize.
-For compatibility with earlier versions of
+in bytes, or with [KMG] suffix, up to 1GB for output instead of the
+filesystem blocksize.  For compatibility with earlier versions of
 .BR filefrag ,
 if
 .I blocksize
diff --git a/misc/filefrag.c b/misc/filefrag.c
index 1eec146..032535f 100644
--- a/misc/filefrag.c
+++ b/misc/filefrag.c
@@ -53,7 +53,7 @@ extern int optind;
 #include <ext2fs/fiemap.h>
 
 int verbose = 0;
-int blocksize;		/* Use specified blocksize (default 1kB) */
+unsigned int blocksize;	/* Use specified blocksize (default 1kB) */
 int sync_file = 0;	/* fsync file before getting the mapping */
 int xattr_map = 0;	/* get xattr mapping */
 int force_bmap;	/* force use of FIBMAP instead of FIEMAP */
@@ -73,7 +73,7 @@ const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
 #define	EXT4_EXTENTS_FL			0x00080000 /* Inode uses extents */
 #define	EXT3_IOC_GETFLAGS		_IOR('f', 1, long)
 
-static int int_log2(int arg)
+static int ulong_log2(unsigned long arg)
 {
 	int     l = 0;
 
@@ -85,7 +85,7 @@ static int int_log2(int arg)
 	return l;
 }
 
-static int int_log10(unsigned long long arg)
+static int ulong_log10(unsigned long long arg)
 {
 	int     l = 0;
 
@@ -452,17 +452,17 @@ static int frag_report(const char *filename)
 	}
 	last_device = st.st_dev;
 
-	width = int_log10(fsinfo.f_blocks);
+	width = ulong_log10(fsinfo.f_blocks);
 	if (width > physical_width)
 		physical_width = width;
 
 	numblocks = (st.st_size + blksize - 1) / blksize;
 	if (blocksize != 0)
-		blk_shift = int_log2(blocksize);
+		blk_shift = ulong_log2(blocksize);
 	else
-		blk_shift = int_log2(blksize);
+		blk_shift = ulong_log2(blksize);
 
-	width = int_log10(numblocks);
+	width = ulong_log10(numblocks);
 	if (width > logical_width)
 		logical_width = width;
 	if (verbose)
@@ -517,7 +517,7 @@ out_close:
 
 static void usage(const char *progname)
 {
-	fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeksvxX] file ...\n",
+	fprintf(stderr, "Usage: %s [-b{blocksize}[KMG]] [-BeksvxX] file ...\n",
 		progname);
 	exit(1);
 }
@@ -535,7 +535,9 @@ int main(int argc, char**argv)
 		case 'b':
 			if (optarg) {
 				char *end;
-				blocksize = strtoul(optarg, &end, 0);
+				unsigned long val;
+
+				val = strtoul(optarg, &end, 0);
 				if (end) {
 #if __GNUC_PREREQ (7, 0)
 #pragma GCC diagnostic push
@@ -544,15 +546,15 @@ int main(int argc, char**argv)
 					switch (end[0]) {
 					case 'g':
 					case 'G':
-						blocksize *= 1024;
+						val *= 1024;
 						/* fall through */
 					case 'm':
 					case 'M':
-						blocksize *= 1024;
+						val *= 1024;
 						/* fall through */
 					case 'k':
 					case 'K':
-						blocksize *= 1024;
+						val *= 1024;
 						break;
 					default:
 						break;
@@ -561,6 +563,16 @@ int main(int argc, char**argv)
 #pragma GCC diagnostic pop
 #endif
 				}
+				/* Specifying too large a blocksize will just
+				 * shift all extents down to zero length. Even
+				 * 1GB is questionable, but caveat emptor. */
+				if (val > 1024 * 1024 * 1024) {
+					fprintf(stderr,
+						"%s: blocksize %lu over 1GB\n",
+						argv[0], val);
+					usage(argv[0]);
+				}
+				blocksize = val;
 			} else { /* Allow -b without argument for compat. Remove
 				  * this eventually so "-b {blocksize}" works */
 				fprintf(stderr, "%s: -b needs a blocksize "
-- 
1.8.0


  parent reply	other threads:[~2020-02-12  1:06 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-07  1:09 [PATCH 1/9] e2fsck: fix e2fsck_allocate_memory() overflow Andreas Dilger
2020-02-07  1:09 ` [PATCH 2/9] e2fsck: use proper types for variables Andreas Dilger
2020-02-29 23:27   ` Theodore Y. Ts'o
2020-02-07  1:09 ` [PATCH 3/9] e2fsck: avoid mallinfo() if over 2GB allocated Andreas Dilger
2020-02-29 23:28   ` Theodore Y. Ts'o
2020-02-07  1:09 ` [PATCH 4/9] e2fsck: reduce memory usage for many directories Andreas Dilger
2020-02-29 23:29   ` Theodore Y. Ts'o
2020-02-07  1:09 ` [PATCH 5/9] debugfs: allow comment lines in command file Andreas Dilger
2020-02-29 23:32   ` Theodore Y. Ts'o
2020-02-07  1:09 ` [PATCH 6/9] debugfs: print inode numbers as unsigned Andreas Dilger
2020-02-29 23:34   ` Theodore Y. Ts'o
2020-02-07  1:09 ` [PATCH 7/9] e2fsck: fix overflow if more than 4B inodes Andreas Dilger
2020-02-29 23:35   ` Theodore Y. Ts'o
2020-02-07  1:09 ` [PATCH 8/9] e2fsck: consistently use ext2fs_get_mem() Andreas Dilger
2020-02-29 23:36   ` Theodore Y. Ts'o
2020-03-04 23:23     ` Theodore Y. Ts'o
2020-02-07  1:09 ` [PATCH 9/9] misc: handle very large files with filefrag Andreas Dilger
2020-03-04 23:27   ` Theodore Y. Ts'o
2020-02-12  0:58 ` Andreas Dilger [this message]
2020-02-12  1:09   ` [PATCH] " Andreas Dilger
2020-02-12  1:07 ` [PATCH] e2fsck: avoid overflow with very large dirs Andreas Dilger
2020-03-04 23:39   ` Theodore Y. Ts'o
2020-02-29 23:25 ` [PATCH 1/9] e2fsck: fix e2fsck_allocate_memory() overflow Theodore Y. Ts'o

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=1581469085-85472-1-git-send-email-adilger@whamcloud.com \
    --to=adilger@whamcloud.com \
    --cc=adilger@dilger.ca \
    --cc=linux-ext4@vger.kernel.org \
    --cc=tytso@mit.edu \
    /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).