(root)/
util-linux-2.39/
sys-utils/
fallocate.c
       1  /*
       2   * fallocate - utility to use the fallocate system call
       3   *
       4   * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
       5   * Written by Eric Sandeen <sandeen@redhat.com>
       6   *            Karel Zak <kzak@redhat.com>
       7   *
       8   * cvtnum routine taken from xfsprogs,
       9   * Copyright (c) 2003-2005 Silicon Graphics, Inc.
      10   *
      11   * This program is free software; you can redistribute it and/or
      12   * modify it under the terms of the GNU General Public License as
      13   * published by the Free Software Foundation.
      14   *
      15   * This program is distributed in the hope that it would be useful,
      16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18   * GNU General Public License for more details.
      19   *
      20   * You should have received a copy of the GNU General Public License along
      21   * with this program; if not, write to the Free Software Foundation, Inc.,
      22   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      23   */
      24  #include <sys/stat.h>
      25  #include <sys/types.h>
      26  #include <sys/mman.h>
      27  #include <ctype.h>
      28  #include <errno.h>
      29  #include <fcntl.h>
      30  #include <stdio.h>
      31  #include <stdlib.h>
      32  #include <unistd.h>
      33  #include <getopt.h>
      34  #include <limits.h>
      35  #include <string.h>
      36  
      37  #ifndef HAVE_FALLOCATE
      38  # include <sys/syscall.h>
      39  #endif
      40  
      41  #if defined(HAVE_LINUX_FALLOC_H) && \
      42      (!defined(FALLOC_FL_KEEP_SIZE) || !defined(FALLOC_FL_PUNCH_HOLE) || \
      43       !defined(FALLOC_FL_COLLAPSE_RANGE) || !defined(FALLOC_FL_ZERO_RANGE) || \
      44       !defined(FALLOC_FL_INSERT_RANGE))
      45  # include <linux/falloc.h>	/* non-libc fallback for FALLOC_FL_* flags */
      46  #endif
      47  
      48  
      49  #ifndef FALLOC_FL_KEEP_SIZE
      50  # define FALLOC_FL_KEEP_SIZE		0x1
      51  #endif
      52  
      53  #ifndef FALLOC_FL_PUNCH_HOLE
      54  # define FALLOC_FL_PUNCH_HOLE		0x2
      55  #endif
      56  
      57  #ifndef FALLOC_FL_COLLAPSE_RANGE
      58  # define FALLOC_FL_COLLAPSE_RANGE	0x8
      59  #endif
      60  
      61  #ifndef FALLOC_FL_ZERO_RANGE
      62  # define FALLOC_FL_ZERO_RANGE		0x10
      63  #endif
      64  
      65  #ifndef FALLOC_FL_INSERT_RANGE
      66  # define FALLOC_FL_INSERT_RANGE		0x20
      67  #endif
      68  
      69  #include "nls.h"
      70  #include "strutils.h"
      71  #include "c.h"
      72  #include "closestream.h"
      73  #include "xalloc.h"
      74  #include "optutils.h"
      75  
      76  static int verbose;
      77  static char *filename;
      78  
      79  static void __attribute__((__noreturn__)) usage(void)
      80  {
      81  	FILE *out = stdout;
      82  	fputs(USAGE_HEADER, out);
      83  	fprintf(out,
      84  	      _(" %s [options] <filename>\n"), program_invocation_short_name);
      85  
      86  	fputs(USAGE_SEPARATOR, out);
      87  	fputs(_("Preallocate space to, or deallocate space from a file.\n"), out);
      88  
      89  	fputs(USAGE_OPTIONS, out);
      90  	fputs(_(" -c, --collapse-range remove a range from the file\n"), out);
      91  	fputs(_(" -d, --dig-holes      detect zeroes and replace with holes\n"), out);
      92  	fputs(_(" -i, --insert-range   insert a hole at range, shifting existing data\n"), out);
      93  	fputs(_(" -l, --length <num>   length for range operations, in bytes\n"), out);
      94  	fputs(_(" -n, --keep-size      maintain the apparent size of the file\n"), out);
      95  	fputs(_(" -o, --offset <num>   offset for range operations, in bytes\n"), out);
      96  	fputs(_(" -p, --punch-hole     replace a range with a hole (implies -n)\n"), out);
      97  	fputs(_(" -z, --zero-range     zero and ensure allocation of a range\n"), out);
      98  #ifdef HAVE_POSIX_FALLOCATE
      99  	fputs(_(" -x, --posix          use posix_fallocate(3) instead of fallocate(2)\n"), out);
     100  #endif
     101  	fputs(_(" -v, --verbose        verbose mode\n"), out);
     102  
     103  	fputs(USAGE_SEPARATOR, out);
     104  	printf(USAGE_HELP_OPTIONS(22));
     105  
     106  	fputs(USAGE_ARGUMENTS, out);
     107  	printf(USAGE_ARG_SIZE(_("<num>")));
     108  
     109  	printf(USAGE_MAN_TAIL("fallocate(1)"));
     110  
     111  	exit(EXIT_SUCCESS);
     112  }
     113  
     114  static loff_t cvtnum(char *s)
     115  {
     116  	uintmax_t x;
     117  
     118  	if (strtosize(s, &x))
     119  		return -1LL;
     120  
     121  	return x;
     122  }
     123  
     124  static void xfallocate(int fd, int mode, off_t offset, off_t length)
     125  {
     126  	int error;
     127  
     128  #ifdef HAVE_FALLOCATE
     129  	error = fallocate(fd, mode, offset, length);
     130  #else
     131  	error = syscall(SYS_fallocate, fd, mode, offset, length);
     132  #endif
     133  	/*
     134  	 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
     135  	 * ENOSYS: The filesystem does not support sys_fallocate
     136  	 */
     137  	if (error < 0) {
     138  		if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP)
     139  			errx(EXIT_FAILURE, _("fallocate failed: keep size mode is unsupported"));
     140  		err(EXIT_FAILURE, _("fallocate failed"));
     141  	}
     142  }
     143  
     144  #ifdef HAVE_POSIX_FALLOCATE
     145  static void xposix_fallocate(int fd, off_t offset, off_t length)
     146  {
     147  	int error = posix_fallocate(fd, offset, length);
     148  	if (error < 0) {
     149  		err(EXIT_FAILURE, _("fallocate failed"));
     150  	}
     151  }
     152  #endif
     153  
     154  /* The real buffer size has to be bufsize + sizeof(uintptr_t) */
     155  static int is_nul(void *buf, size_t bufsize)
     156  {
     157  	typedef uintptr_t word;
     158  	void const *vp;
     159  	char const *cbuf = buf, *cp;
     160  	word const *wp = buf;
     161  
     162  	/* set sentinel */
     163  	memset((char *) buf + bufsize, '\1', sizeof(word));
     164  
     165  	/* Find first nonzero *word*, or the word with the sentinel.  */
     166  	while (*wp++ == 0)
     167  		continue;
     168  
     169  	/* Find the first nonzero *byte*, or the sentinel.  */
     170  	vp = wp - 1;
     171  	cp = vp;
     172  
     173  	while (*cp++ == 0)
     174  		continue;
     175  
     176  	return cbuf + bufsize < cp;
     177  }
     178  
     179  static void dig_holes(int fd, off_t file_off, off_t len)
     180  {
     181  	off_t file_end = len ? file_off + len : 0;
     182  	off_t hole_start = 0, hole_sz = 0;
     183  	uintmax_t ct = 0;
     184  	size_t  bufsz;
     185  	char *buf;
     186  	struct stat st;
     187  #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
     188  	off_t cache_start = file_off;
     189  	/*
     190  	 * We don't want to call POSIX_FADV_DONTNEED to discard cached
     191  	 * data in PAGE_SIZE steps. IMHO it's overkill (too many syscalls).
     192  	 *
     193  	 * Let's assume that 1MiB (on system with 4K page size) is just
     194  	 * a good compromise.
     195  	 *					    -- kzak Feb-2014
     196  	 */
     197  	const size_t cachesz = getpagesize() * 256;
     198  #endif
     199  
     200  	if (fstat(fd, &st) != 0)
     201  		err(EXIT_FAILURE, _("stat of %s failed"), filename);
     202  
     203  	bufsz = st.st_blksize;
     204  
     205  	if (lseek(fd, file_off, SEEK_SET) < 0)
     206  		err(EXIT_FAILURE, _("seek on %s failed"), filename);
     207  
     208  	/* buffer + extra space for is_nul() sentinel */
     209  	buf = xmalloc(bufsz + sizeof(uintptr_t));
     210  	while (file_end == 0 || file_off < file_end) {
     211  		/*
     212  		 * Detect data area (skip holes)
     213  		 */
     214  		off_t end, off;
     215  
     216  		off = lseek(fd, file_off, SEEK_DATA);
     217  		if ((off == -1 && errno == ENXIO) ||
     218  		    (file_end && off >= file_end))
     219  			break;
     220  
     221  		end = lseek(fd, off, SEEK_HOLE);
     222  		if (file_end && end > file_end)
     223  			end = file_end;
     224  
     225  		if (off < 0 || end < 0)
     226  			break;
     227  
     228  #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
     229  		(void) posix_fadvise(fd, off, end, POSIX_FADV_SEQUENTIAL);
     230  #endif
     231  		/*
     232  		 * Dig holes in the area
     233  		 */
     234  		while (off < end) {
     235  			ssize_t rsz = pread(fd, buf, bufsz, off);
     236  			if (rsz < 0 && errno)
     237  				err(EXIT_FAILURE, _("%s: read failed"), filename);
     238  			if (end && rsz > 0 && off > end - rsz)
     239  				rsz = end - off;
     240  			if (rsz <= 0)
     241  				break;
     242  
     243  			if (is_nul(buf, rsz)) {
     244  				if (!hole_sz)				/* new hole detected */
     245  					hole_start = off;
     246  				hole_sz += rsz;
     247  			 } else if (hole_sz) {
     248  				xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,
     249  					   hole_start, hole_sz);
     250  				ct += hole_sz;
     251  				hole_sz = hole_start = 0;
     252  			}
     253  
     254  #if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE)
     255  			/* discard cached data */
     256  			if (off - cache_start > (off_t) cachesz) {
     257  				size_t clen = off - cache_start;
     258  
     259  				clen = (clen / cachesz) * cachesz;
     260  				(void) posix_fadvise(fd, cache_start, clen, POSIX_FADV_DONTNEED);
     261  				cache_start = cache_start + clen;
     262  			}
     263  #endif
     264  			off += rsz;
     265  		}
     266  		if (hole_sz) {
     267  			off_t alloc_sz = hole_sz;
     268  			if (off >= end)
     269  				alloc_sz += st.st_blksize;		/* meet block boundary */
     270  			xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,
     271  					hole_start, alloc_sz);
     272  			ct += hole_sz;
     273  		}
     274  		file_off = off;
     275  	}
     276  
     277  	free(buf);
     278  
     279  	if (verbose) {
     280  		char *str = size_to_human_string(SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, ct);
     281  		fprintf(stdout, _("%s: %s (%ju bytes) converted to sparse holes.\n"),
     282  				filename, str, ct);
     283  		free(str);
     284  	}
     285  }
     286  
     287  int main(int argc, char **argv)
     288  {
     289  	int	c;
     290  	int	fd;
     291  	int	mode = 0;
     292  	int	dig = 0;
     293  	int posix = 0;
     294  	loff_t	length = -2LL;
     295  	loff_t	offset = 0;
     296  
     297  	static const struct option longopts[] = {
     298  	    { "help",           no_argument,       NULL, 'h' },
     299  	    { "version",        no_argument,       NULL, 'V' },
     300  	    { "keep-size",      no_argument,       NULL, 'n' },
     301  	    { "punch-hole",     no_argument,       NULL, 'p' },
     302  	    { "collapse-range", no_argument,       NULL, 'c' },
     303  	    { "dig-holes",      no_argument,       NULL, 'd' },
     304  	    { "insert-range",   no_argument,       NULL, 'i' },
     305  	    { "zero-range",     no_argument,       NULL, 'z' },
     306  	    { "offset",         required_argument, NULL, 'o' },
     307  	    { "length",         required_argument, NULL, 'l' },
     308  	    { "posix",          no_argument,       NULL, 'x' },
     309  	    { "verbose",        no_argument,       NULL, 'v' },
     310  	    { NULL, 0, NULL, 0 }
     311  	};
     312  
     313  	static const ul_excl_t excl[] = {	/* rows and cols in ASCII order */
     314  		{ 'c', 'd', 'p', 'z' },
     315  		{ 'c', 'n' },
     316  		{ 'x', 'c', 'd', 'i', 'n', 'p', 'z'},
     317  		{ 0 }
     318  	};
     319  	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
     320  
     321  	setlocale(LC_ALL, "");
     322  	bindtextdomain(PACKAGE, LOCALEDIR);
     323  	textdomain(PACKAGE);
     324  	close_stdout_atexit();
     325  
     326  	while ((c = getopt_long(argc, argv, "hvVncpdizxl:o:", longopts, NULL))
     327  			!= -1) {
     328  
     329  		err_exclusive_options(c, longopts, excl, excl_st);
     330  
     331  		switch(c) {
     332  		case 'c':
     333  			mode |= FALLOC_FL_COLLAPSE_RANGE;
     334  			break;
     335  		case 'd':
     336  			dig = 1;
     337  			break;
     338  		case 'i':
     339  			mode |= FALLOC_FL_INSERT_RANGE;
     340  			break;
     341  		case 'l':
     342  			length = cvtnum(optarg);
     343  			break;
     344  		case 'n':
     345  			mode |= FALLOC_FL_KEEP_SIZE;
     346  			break;
     347  		case 'o':
     348  			offset = cvtnum(optarg);
     349  			break;
     350  		case 'p':
     351  			mode |= FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
     352  			break;
     353  		case 'z':
     354  			mode |= FALLOC_FL_ZERO_RANGE;
     355  			break;
     356  		case 'x':
     357  #ifdef HAVE_POSIX_FALLOCATE
     358  			posix = 1;
     359  			break;
     360  #else
     361  			errx(EXIT_FAILURE, _("posix_fallocate support is not compiled"));
     362  #endif
     363  		case 'v':
     364  			verbose++;
     365  			break;
     366  
     367  		case 'h':
     368  			usage();
     369  		case 'V':
     370  			print_version(EXIT_SUCCESS);
     371  		default:
     372  			errtryhelp(EXIT_FAILURE);
     373  		}
     374  	}
     375  
     376  	if (optind == argc)
     377  		errx(EXIT_FAILURE, _("no filename specified"));
     378  
     379  	filename = argv[optind++];
     380  
     381  	if (optind != argc)
     382  		errx(EXIT_FAILURE, _("unexpected number of arguments"));
     383  
     384  	if (dig) {
     385  		/* for --dig-holes the default is analyze all file */
     386  		if (length == -2LL)
     387  			length = 0;
     388  		if (length < 0)
     389  			errx(EXIT_FAILURE, _("invalid length value specified"));
     390  	} else {
     391  		/* it's safer to require the range specification (--length --offset) */
     392  		if (length == -2LL)
     393  			errx(EXIT_FAILURE, _("no length argument specified"));
     394  		if (length <= 0)
     395  			errx(EXIT_FAILURE, _("invalid length value specified"));
     396  	}
     397  	if (offset < 0)
     398  		errx(EXIT_FAILURE, _("invalid offset value specified"));
     399  
     400  	/* O_CREAT makes sense only for the default fallocate(2) behavior
     401  	 * when mode is no specified and new space is allocated */
     402  	fd = open(filename, O_RDWR | (!dig && !mode ? O_CREAT : 0),
     403  		  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
     404  	if (fd < 0)
     405  		err(EXIT_FAILURE, _("cannot open %s"), filename);
     406  
     407  	if (dig)
     408  		dig_holes(fd, offset, length);
     409  	else {
     410  #ifdef HAVE_POSIX_FALLOCATE
     411  		if (posix)
     412  			xposix_fallocate(fd, offset, length);
     413  		else
     414  #endif
     415  			xfallocate(fd, mode, offset, length);
     416  
     417  		if (verbose) {
     418  			char *str = size_to_human_string(SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, length);
     419  
     420  			if (mode & FALLOC_FL_PUNCH_HOLE)
     421  				fprintf(stdout, _("%s: %s (%ju bytes) hole created.\n"),
     422  								filename, str, length);
     423  			else if (mode & FALLOC_FL_COLLAPSE_RANGE)
     424  				fprintf(stdout, _("%s: %s (%ju bytes) removed.\n"),
     425  								filename, str, length);
     426  			else if (mode & FALLOC_FL_INSERT_RANGE)
     427  				fprintf(stdout, _("%s: %s (%ju bytes) inserted.\n"),
     428  								filename, str, length);
     429  			else if (mode & FALLOC_FL_ZERO_RANGE)
     430  				fprintf(stdout, _("%s: %s (%ju bytes) zeroed.\n"),
     431  								filename, str, length);
     432  			else
     433  				fprintf(stdout, _("%s: %s (%ju bytes) allocated.\n"),
     434  								filename, str, length);
     435  			free(str);
     436  		}
     437  	}
     438  
     439  	if (close_fd(fd) != 0)
     440  		err(EXIT_FAILURE, _("write failed: %s"), filename);
     441  
     442  	return EXIT_SUCCESS;
     443  }