Skip to content

Commit

Permalink
fallocate: introduce an option -c to support COLLAPSE_RANGE
Browse files Browse the repository at this point in the history
Introduce a new option -c (or --collapse-range) to support a new flag
FALLOC_FL_COLLAPSE_RANGE for fallocate(2). It will nullify a particular
range [offset, offset+len] by shifting extents beyond the range to the
beginning of the hole.

Cc: Lukas Czerner <lczerner@redhat.com>
Cc: Namjae Jeon <namjae.jeon@samsung.com>
Cc: Ashish Sangwan <a.sangwan@samsung.com>
Signed-off-by: Dongsu Park <dongsu.park@profitbricks.com>
  • Loading branch information
Dongsu Park authored and karelzak committed Apr 18, 2014
1 parent 2dc0c62 commit 83cc932
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
7 changes: 7 additions & 0 deletions sys-utils/fallocate.1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fallocate \- preallocate or deallocate space to a file
.B fallocate
.RB [ \-n ]
.RB [ \-p ]
.RB [ \-c ]
.RB [ \-o
.IR offset ]
.B \-l
Expand Down Expand Up @@ -54,6 +55,12 @@ implied.
.IP
You can think of this as doing a "\fBcp --sparse\fP" and
renaming the dest file as the original, without the need for extra disk space.
.IP "\fB\-c, \-\-collapse-range\fP"
Collapse a particular file range to nullify the hole. Extents beyond the range
[offset, offset+length] will be shifted to the beginning of hole. Hence this
command does not leave a hole, while \fI\-\-punch-hole\fP leaves a hole
instead of shifting extents. Both offset and length should be aligned to
the block size of filesystem.
.IP "\fB\-o, \-\-offset\fP \fIoffset\fP
Specifies the beginning offset of the allocation, in bytes.
.IP "\fB\-l, \-\-length\fP \fIlength\fP
Expand Down
45 changes: 28 additions & 17 deletions sys-utils/fallocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
#endif

#if defined(HAVE_LINUX_FALLOC_H) && \
(!defined(FALLOC_FL_KEEP_SIZE) || !defined(FALLOC_FL_PUNCH_HOLE))
(!defined(FALLOC_FL_KEEP_SIZE) || !defined(FALLOC_FL_PUNCH_HOLE) || \
!defined(FALLOC_FL_COLLAPSE_RANGE))
# include <linux/falloc.h> /* non-libc fallback for FALLOC_FL_* flags */
#endif

Expand All @@ -51,6 +52,10 @@
# define FALLOC_FL_PUNCH_HOLE 2
#endif

#ifndef FALLOC_FL_COLLAPSE_RANGE
# define FALLOC_FL_COLLAPSE_RANGE 8
#endif

#include "nls.h"
#include "strutils.h"
#include "c.h"
Expand All @@ -66,12 +71,13 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
fprintf(out,
_(" %s [options] <filename>\n"), program_invocation_short_name);
fputs(USAGE_OPTIONS, out);
fputs(_(" -d, --dig-holes detect and dig holes\n"), out);
fputs(_(" -l, --length <num> length of the (de)allocation, in bytes\n"), out);
fputs(_(" -n, --keep-size don't modify the length of the file\n"), out);
fputs(_(" -o, --offset <num> offset of the (de)allocation, in bytes\n"), out);
fputs(_(" -p, --punch-hole punch holes in the file\n"), out);
fputs(_(" -v, --verbose verbose mode\n"), out);
fputs(_(" -c, --collapse-range collapse space in the file\n"), out);
fputs(_(" -d, --dig-holes detect and dig holes\n"), out);
fputs(_(" -l, --length <num> length of the (de)allocation, in bytes\n"), out);
fputs(_(" -n, --keep-size don't modify the length of the file\n"), out);
fputs(_(" -o, --offset <num> offset of the (de)allocation, in bytes\n"), out);
fputs(_(" -p, --punch-hole punch holes in the file\n"), out);
fputs(_(" -v, --verbose verbose mode\n"), out);

fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
Expand Down Expand Up @@ -258,30 +264,35 @@ int main(int argc, char **argv)
loff_t offset = 0;

static const struct option longopts[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
{ "keep-size", 0, 0, 'n' },
{ "punch-hole", 0, 0, 'p' },
{ "dig-holes", 0, 0, 'd' },
{ "offset", 1, 0, 'o' },
{ "length", 1, 0, 'l' },
{ "verbose", 0, 0, 'v' },
{ NULL, 0, 0, 0 }
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
{ "keep-size", 0, 0, 'n' },
{ "punch-hole", 0, 0, 'p' },
{ "collapse-range", 0, 0, 'c' },
{ "dig-holes", 0, 0, 'd' },
{ "offset", 1, 0, 'o' },
{ "length", 1, 0, 'l' },
{ "verbose", 0, 0, 'v' },
{ NULL, 0, 0, 0 }
};

setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
atexit(close_stdout);

while ((c = getopt_long(argc, argv, "hvVnpdl:o:", longopts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "hvVncpdl:o:", longopts, NULL))
!= -1) {
switch(c) {
case 'h':
usage(stdout);
break;
case 'V':
printf(UTIL_LINUX_VERSION);
return EXIT_SUCCESS;
case 'c':
mode |= FALLOC_FL_COLLAPSE_RANGE;
break;
case 'p':
mode |= FALLOC_FL_PUNCH_HOLE;
/* fall through */
Expand Down

0 comments on commit 83cc932

Please sign in to comment.