Skip to contentSkip to conversation

xfsprogs: add fpunch command for hole punching via fallocate

PATCH In review Josef Bacik 3 participants 6 messages Jan 14, 2011 – Jan 19, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfsprogs: add fpunch command for hole punching via fallocate

This adds a fpunch command that Dave Chinner recommended. It simply uses fallocate to punch a hole for the given offset and length. It is necessary to run the xfstest I have for hole punching. Thanks,

---
 io/prealloc.c |   45 +++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 41 insertions(+), 4 deletions(-)
io/prealloc.c +41 -4
index c8b7df6..9dcfc1c 100644
@@ -32,6 +32,9 @@ static cmdinfo_t unresvsp_cmd;
 static cmdinfo_t zero_cmd;
 #if defined(HAVE_FALLOCATE)
 static cmdinfo_t falloc_cmd;
+#if defined (FALLOC_FL_PUNCH_HOLE)
+static cmdinfo_t fpunch_cmd;
+#endif
 #endif
 
 static int
@@ -153,8 +156,10 @@ fallocate_f(
 	xfs_flock64_t	segment;
 	int		mode = 0;
 	int		c;
+	const char	*opts;
 
-	while ((c = getopt(argc, argv, "k")) != EOF) {
+	opts = "k";
+	while ((c = getopt(argc, argv, opts)) != EOF) {
 		switch (c) {
 		case 'k':
 			mode = FALLOC_FL_KEEP_SIZE;
@@ -176,7 +181,28 @@ fallocate_f(
 	}
 	return 0;
 }
-#endif
+
+#if defined (FALLOC_FL_PUNCH_HOLE)
+static int
+fpunch_f(
+	 int		argc,
+	 char		**argv)
+{
+	xfs_flock64_t	segment;
+	int		mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
+
+	if (!offset_length(argv[1], argv[2], &segment))
+		return 0;
+
+	if (fallocate(file->fd, mode,
+			segment.l_start, segment.l_len)) {
+		perror("fallocate");
+		return 0;
+	}
+	return 0;
+}
+#endif	/* FALLOC_FL_PUNCH_HOLE */
+#endif	/* HAVE_FALLOCATE */
 
 void
 prealloc_init(void)
@@ -239,7 +265,18 @@ prealloc_init(void)
 	falloc_cmd.args = _("[-k] off len");
 	falloc_cmd.oneline =
 		_("allocates space associated with part of a file via fallocate");
-
 	add_command(&falloc_cmd);
-#endif
+
+#if defined (FALLOC_FL_PUNCH_HOLE)
+	fpunch_cmd.name = _("fpunch");
+	fpunch_cmd.cfunc = fpunch_f;
+	fpunch_cmd.argmin = 2;
+	fpunch_cmd.argmax = -1;
+	fpunch_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
+	fpunch_cmd.args = _("off len");
+	fpunch_cmd.oneline =
+		_("de-allocates space assocated with part of a file via fallocate");
+	add_command(&fpunch_cmd);
+#endif	/* FALLOC_FL_PUNCH_HOLE */
+#endif	/* HAVE_FALLOCATE */
 }
Signature · 6 lines
1.6.6.1

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw

Looks mostly good, but I wonder what the point of a new command for this is. It's just one new flag to fallocate, so I'd also implement it as a flag to the fallocate command.

io/prealloc.c fallocate_f( · line 165

Quoted 12 lines
io/prealloc.c +3 -1
@@ -153,8 +156,10 @@ fallocate_f(
 	xfs_flock64_t	segment;
 	int		mode = 0;
 	int		c;
+	const char	*opts;
 
-	while ((c = getopt(argc, argv, "k")) != EOF) {
+	opts = "k";
+	while ((c = getopt(argc, argv, opts)) != EOF) {
 		switch (c) {
 		case 'k':
 			mode = FALLOC_FL_KEEP_SIZE;

Why do you change unrelated code?

Quoted 1 line

+#if defined (FALLOC_FL_PUNCH_HOLE)

I'd rather have a

#ifndef FALLOC_FL_PUNCH_HOLE
#define FALLOC_FL_PUNCH_HOLE 0x02
#endif

to avoid requiring newest kernel headers.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw
Quoted 4 lines · Christoph Hellwig

Looks mostly good, but I wonder what the point of a new command for this is. It's just one new flag to fallocate, so I'd also implement it as a flag to the fallocate command.

:( thats what I did to begin with, and Dave said he'd rather have a seperate command.

Quoted 6 lines
Quoted 6 lines
io/prealloc.c +1 -0
@@ -153,8 +156,10 @@ fallocate_f(
 	xfs_flock64_t	segment;
 	int		mode = 0;
 	int		c;
+	const char	*opts;
 
Quoted 9 lines

io/prealloc.c fallocate_f( · line 165

Quoted 6 lines
io/prealloc.c +2 -1
-	while ((c = getopt(argc, argv, "k")) != EOF) {
+	opts = "k";
+	while ((c = getopt(argc, argv, opts)) != EOF) {
 		switch (c) {
 		case 'k':
 			mode = FALLOC_FL_KEEP_SIZE;

Why do you change unrelated code?

Crap sorry this is left over from my original patch.

Quoted 10 lines
Quoted 1 line

+#if defined (FALLOC_FL_PUNCH_HOLE)

I'd rather have a

#ifndef FALLOC_FL_PUNCH_HOLE
#define FALLOC_FL_PUNCH_HOLE 0x02
#endif

to avoid requiring newest kernel headers.

Sounds good. So which do we want, a new command or a new flag? Thanks,

Josef

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw
Quoted 1 line · Josef Bacik

Sounds good. So which do we want, a new command or a new flag? Thanks,

I'll wait for dave to chime in. I think we should absolutely expose it as a fallocate flag, but if there's a good reason we can also expose it as a separate command.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#4 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 1 line · Josef Bacik

Sounds good. So which do we want, a new command or a new flag? Thanks,

I'll wait for dave to chime in. I think we should absolutely expose it as a fallocate flag, but if there's a good reason we can also expose it as a separate command.

My reasoning was that:

a) it is consistent with other xfs_io allocation manipulation command structures such as resvsp/unresvsp
b) "punch" is less to type than "fallocate -p"
c) self documenting in scripts e.g. -c "punch 4k 4k" is much more obvious than -c "fallocate -p 4k 4k" and saves a man page lookup when reading the script.
d) punch as a top level command will show up in the "xfs_io -c help", not require you to know it is a suboption of the "falloc" command to find out how to use it.
e) the xfs_io command does not have to have the same name and structure as the underlying API that implements the functionality the commands execute.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#5 lore raw
Quoted 2 lines · Dave Chinner

a) it is consistent with other xfs_io allocation manipulation command structures such as resvsp/unresvsp

These are all different ioctls.

Quoted 10 lines

b) "punch" is less to type than "fallocate -p"
c) self documenting in scripts e.g. -c "punch 4k 4k" is much more obvious than -c "fallocate -p 4k 4k" and saves a man page lookup when reading the script.
d) punch as a top level command will show up in the "xfs_io -c help", not require you to know it is a suboption of the "falloc" command to find out how to use it.
e) the xfs_io command does not have to have the same name and structure as the underlying API that implements the functionality the commands execute.

I still don't like this as a reason to duplicate the code, and not having the different arguments for fallocate exposed similar to the syscall level.

What do you think about introducing a concept of aliases in xfs_io so that we can have a toplevel punch command that just gets aliased to fallocate -p without having to reimplement it?

I'd take Josef's older falocate -p implementation and will add the alias support myself.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs