]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/prealloc.c
5805897a4a0a26156b0c3fddf73fe981f93d377d
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 #if defined(HAVE_FALLOCATE)
8 #include <linux/falloc.h>
15 #ifndef FALLOC_FL_PUNCH_HOLE
16 #define FALLOC_FL_PUNCH_HOLE 0x02
19 #ifndef FALLOC_FL_COLLAPSE_RANGE
20 #define FALLOC_FL_COLLAPSE_RANGE 0x08
23 #ifndef FALLOC_FL_ZERO_RANGE
24 #define FALLOC_FL_ZERO_RANGE 0x10
27 #ifndef FALLOC_FL_INSERT_RANGE
28 #define FALLOC_FL_INSERT_RANGE 0x20
31 #ifndef FALLOC_FL_UNSHARE_RANGE
32 #define FALLOC_FL_UNSHARE_RANGE 0x40
35 static cmdinfo_t allocsp_cmd
;
36 static cmdinfo_t freesp_cmd
;
37 static cmdinfo_t resvsp_cmd
;
38 static cmdinfo_t unresvsp_cmd
;
39 static cmdinfo_t zero_cmd
;
40 #if defined(HAVE_FALLOCATE)
41 static cmdinfo_t falloc_cmd
;
42 static cmdinfo_t fpunch_cmd
;
43 static cmdinfo_t fcollapse_cmd
;
44 static cmdinfo_t finsert_cmd
;
45 static cmdinfo_t fzero_cmd
;
46 static cmdinfo_t funshare_cmd
;
53 xfs_flock64_t
*segment
)
55 size_t blocksize
, sectsize
;
57 init_cvtnum(&blocksize
, §size
);
58 memset(segment
, 0, sizeof(*segment
));
59 segment
->l_whence
= SEEK_SET
;
60 segment
->l_start
= cvtnum(blocksize
, sectsize
, offset
);
61 if (segment
->l_start
< 0) {
62 printf(_("non-numeric offset argument -- %s\n"), offset
);
65 segment
->l_len
= cvtnum(blocksize
, sectsize
, length
);
66 if (segment
->l_len
< 0) {
67 printf(_("non-numeric length argument -- %s\n"), length
);
74 * These ioctls were withdrawn in Linux 5.17, but we'll keep them around for
77 #ifndef XFS_IOC_ALLOCSP64
78 # define XFS_IOC_ALLOCSP64 _IOW ('X', 36, struct xfs_flock64)
80 #ifndef XFS_IOC_FREESP64
81 # define XFS_IOC_FREESP64 _IOW ('X', 37, struct xfs_flock64)
89 xfs_flock64_t segment
;
91 if (!offset_length(argv
[1], argv
[2], &segment
)) {
96 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_ALLOCSP64
, &segment
) < 0) {
97 perror("XFS_IOC_ALLOCSP64");
109 xfs_flock64_t segment
;
111 if (!offset_length(argv
[1], argv
[2], &segment
)) {
116 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_FREESP64
, &segment
) < 0) {
117 perror("XFS_IOC_FREESP64");
129 xfs_flock64_t segment
;
131 if (!offset_length(argv
[1], argv
[2], &segment
)) {
136 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_RESVSP64
, &segment
) < 0) {
137 perror("XFS_IOC_RESVSP64");
149 xfs_flock64_t segment
;
151 if (!offset_length(argv
[1], argv
[2], &segment
)) {
156 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_UNRESVSP64
, &segment
) < 0) {
157 perror("XFS_IOC_UNRESVSP64");
169 xfs_flock64_t segment
;
171 if (!offset_length(argv
[1], argv
[2], &segment
)) {
176 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_ZERO_RANGE
, &segment
) < 0) {
177 perror("XFS_IOC_ZERO_RANGE");
185 #if defined (HAVE_FALLOCATE)
191 " modifies space associated with part of a file via fallocate"
194 " 'falloc 0 1m' - fills all holes within the first megabyte\n"
196 " falloc uses the fallocate system call to alter space allocations in the\n"
197 " open file. The following operations are supported:\n"
198 " All the file offsets are in units of bytes.\n"
199 " -c -- collapses the given range.\n"
200 " -i -- inserts a hole into the given range of the file.\n"
201 " -k -- do not change file size.\n"
202 " -p -- unmap the given range from the file.\n"
203 " -u -- unshare shared extents in the given range.\n"
212 xfs_flock64_t segment
;
216 while ((c
= getopt(argc
, argv
, "cikpu")) != EOF
) {
219 mode
= FALLOC_FL_COLLAPSE_RANGE
;
222 mode
= FALLOC_FL_INSERT_RANGE
;
225 mode
= FALLOC_FL_KEEP_SIZE
;
228 mode
= FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
;
231 mode
= FALLOC_FL_UNSHARE_RANGE
;
235 command_usage(&falloc_cmd
);
238 if (optind
!= argc
- 2) {
240 return command_usage(&falloc_cmd
);
243 if (!offset_length(argv
[optind
], argv
[optind
+1], &segment
)) {
248 if (fallocate(file
->fd
, mode
,
249 segment
.l_start
, segment
.l_len
)) {
262 xfs_flock64_t segment
;
263 int mode
= FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
;
265 if (!offset_length(argv
[1], argv
[2], &segment
)) {
270 if (fallocate(file
->fd
, mode
,
271 segment
.l_start
, segment
.l_len
)) {
284 xfs_flock64_t segment
;
285 int mode
= FALLOC_FL_COLLAPSE_RANGE
;
287 if (!offset_length(argv
[1], argv
[2], &segment
)) {
292 if (fallocate(file
->fd
, mode
,
293 segment
.l_start
, segment
.l_len
)) {
306 xfs_flock64_t segment
;
307 int mode
= FALLOC_FL_INSERT_RANGE
;
309 if (!offset_length(argv
[1], argv
[2], &segment
)) {
314 if (fallocate(file
->fd
, mode
,
315 segment
.l_start
, segment
.l_len
)) {
328 xfs_flock64_t segment
;
329 int mode
= FALLOC_FL_ZERO_RANGE
;
332 while ((c
= getopt(argc
, argv
, "k")) != EOF
) {
335 mode
|= FALLOC_FL_KEEP_SIZE
;
338 command_usage(&fzero_cmd
);
341 if (optind
!= argc
- 2)
342 return command_usage(&fzero_cmd
);
344 if (!offset_length(argv
[optind
], argv
[optind
+ 1], &segment
))
347 if (fallocate(file
->fd
, mode
, segment
.l_start
, segment
.l_len
)) {
359 xfs_flock64_t segment
;
361 int mode
= FALLOC_FL_UNSHARE_RANGE
;
363 while ((c
= getopt(argc
, argv
, "")) != EOF
) {
366 command_usage(&funshare_cmd
);
369 if (optind
!= argc
- 2)
370 return command_usage(&funshare_cmd
);
372 if (!offset_length(argv
[optind
], argv
[optind
+ 1], &segment
)) {
377 if (fallocate(file
->fd
, mode
, segment
.l_start
, segment
.l_len
)) {
384 #endif /* HAVE_FALLOCATE */
389 allocsp_cmd
.name
= "allocsp";
390 allocsp_cmd
.cfunc
= allocsp_f
;
391 allocsp_cmd
.argmin
= 2;
392 allocsp_cmd
.argmax
= 2;
393 allocsp_cmd
.flags
= CMD_NOMAP_OK
;
394 allocsp_cmd
.args
= _("off len");
395 allocsp_cmd
.oneline
= _("allocates zeroed space for part of a file");
397 freesp_cmd
.name
= "freesp";
398 freesp_cmd
.cfunc
= freesp_f
;
399 freesp_cmd
.argmin
= 2;
400 freesp_cmd
.argmax
= 2;
401 freesp_cmd
.flags
= CMD_NOMAP_OK
;
402 freesp_cmd
.args
= _("off len");
403 freesp_cmd
.oneline
= _("frees space associated with part of a file");
405 resvsp_cmd
.name
= "resvsp";
406 resvsp_cmd
.cfunc
= resvsp_f
;
407 resvsp_cmd
.argmin
= 2;
408 resvsp_cmd
.argmax
= 2;
409 resvsp_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
410 resvsp_cmd
.args
= _("off len");
412 _("reserves space associated with part of a file");
414 unresvsp_cmd
.name
= "unresvsp";
415 unresvsp_cmd
.cfunc
= unresvsp_f
;
416 unresvsp_cmd
.argmin
= 2;
417 unresvsp_cmd
.argmax
= 2;
418 unresvsp_cmd
.args
= _("off len");
419 unresvsp_cmd
.flags
= CMD_NOMAP_OK
;
420 unresvsp_cmd
.oneline
=
421 _("frees reserved space associated with part of a file");
423 zero_cmd
.name
= "zero";
424 zero_cmd
.cfunc
= zero_f
;
427 zero_cmd
.flags
= CMD_NOMAP_OK
;
428 zero_cmd
.args
= _("off len");
430 _("Converts the given range of a file to allocated zeros");
432 add_command(&allocsp_cmd
);
433 add_command(&freesp_cmd
);
434 add_command(&resvsp_cmd
);
435 add_command(&unresvsp_cmd
);
436 add_command(&zero_cmd
);
438 #if defined (HAVE_FALLOCATE)
439 falloc_cmd
.name
= "falloc";
440 falloc_cmd
.cfunc
= fallocate_f
;
441 falloc_cmd
.argmin
= 2;
442 falloc_cmd
.argmax
= -1;
443 falloc_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
444 falloc_cmd
.args
= _("[-c] [-k] [-p] [-u] off len");
446 _("allocates space associated with part of a file via fallocate");
447 falloc_cmd
.help
= falloc_help
;
448 add_command(&falloc_cmd
);
450 fpunch_cmd
.name
= "fpunch";
451 fpunch_cmd
.cfunc
= fpunch_f
;
452 fpunch_cmd
.argmin
= 2;
453 fpunch_cmd
.argmax
= 2;
454 fpunch_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
455 fpunch_cmd
.args
= _("off len");
457 _("de-allocates space associated with part of a file via fallocate");
458 add_command(&fpunch_cmd
);
460 fcollapse_cmd
.name
= "fcollapse";
461 fcollapse_cmd
.cfunc
= fcollapse_f
;
462 fcollapse_cmd
.argmin
= 2;
463 fcollapse_cmd
.argmax
= 2;
464 fcollapse_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
465 fcollapse_cmd
.args
= _("off len");
466 fcollapse_cmd
.oneline
=
467 _("de-allocates space and eliminates the hole by shifting extents");
468 add_command(&fcollapse_cmd
);
470 finsert_cmd
.name
= "finsert";
471 finsert_cmd
.cfunc
= finsert_f
;
472 finsert_cmd
.argmin
= 2;
473 finsert_cmd
.argmax
= 2;
474 finsert_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
475 finsert_cmd
.args
= _("off len");
476 finsert_cmd
.oneline
=
477 _("creates new space for writing within file by shifting extents");
478 add_command(&finsert_cmd
);
480 fzero_cmd
.name
= "fzero";
481 fzero_cmd
.cfunc
= fzero_f
;
482 fzero_cmd
.argmin
= 2;
483 fzero_cmd
.argmax
= 3;
484 fzero_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
485 fzero_cmd
.args
= _("[-k] off len");
487 _("zeroes space and eliminates holes by preallocating");
488 add_command(&fzero_cmd
);
490 funshare_cmd
.name
= "funshare";
491 funshare_cmd
.cfunc
= funshare_f
;
492 funshare_cmd
.argmin
= 2;
493 funshare_cmd
.argmax
= 2;
494 funshare_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
495 funshare_cmd
.args
= _("off len");
496 funshare_cmd
.oneline
=
497 _("unshares shared blocks within the range");
498 add_command(&funshare_cmd
);
499 #endif /* HAVE_FALLOCATE */