]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/prealloc.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 #include <linux/falloc.h>
13 #ifndef FALLOC_FL_PUNCH_HOLE
14 #define FALLOC_FL_PUNCH_HOLE 0x02
17 #ifndef FALLOC_FL_COLLAPSE_RANGE
18 #define FALLOC_FL_COLLAPSE_RANGE 0x08
21 #ifndef FALLOC_FL_ZERO_RANGE
22 #define FALLOC_FL_ZERO_RANGE 0x10
25 #ifndef FALLOC_FL_INSERT_RANGE
26 #define FALLOC_FL_INSERT_RANGE 0x20
29 #ifndef FALLOC_FL_UNSHARE_RANGE
30 #define FALLOC_FL_UNSHARE_RANGE 0x40
33 static cmdinfo_t allocsp_cmd
;
34 static cmdinfo_t freesp_cmd
;
35 static cmdinfo_t resvsp_cmd
;
36 static cmdinfo_t unresvsp_cmd
;
37 static cmdinfo_t zero_cmd
;
38 static cmdinfo_t falloc_cmd
;
39 static cmdinfo_t fpunch_cmd
;
40 static cmdinfo_t fcollapse_cmd
;
41 static cmdinfo_t finsert_cmd
;
42 static cmdinfo_t fzero_cmd
;
43 static cmdinfo_t funshare_cmd
;
49 xfs_flock64_t
*segment
)
51 size_t blocksize
, sectsize
;
53 init_cvtnum(&blocksize
, §size
);
54 memset(segment
, 0, sizeof(*segment
));
55 segment
->l_whence
= SEEK_SET
;
56 segment
->l_start
= cvtnum(blocksize
, sectsize
, offset
);
57 if (segment
->l_start
< 0) {
58 printf(_("non-numeric offset argument -- %s\n"), offset
);
61 segment
->l_len
= cvtnum(blocksize
, sectsize
, length
);
62 if (segment
->l_len
< 0) {
63 printf(_("non-numeric length argument -- %s\n"), length
);
70 * These ioctls were withdrawn in Linux 5.17, but we'll keep them around for
73 #ifndef XFS_IOC_ALLOCSP64
74 # define XFS_IOC_ALLOCSP64 _IOW ('X', 36, struct xfs_flock64)
76 #ifndef XFS_IOC_FREESP64
77 # define XFS_IOC_FREESP64 _IOW ('X', 37, struct xfs_flock64)
85 xfs_flock64_t segment
;
87 if (!offset_length(argv
[1], argv
[2], &segment
)) {
92 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_ALLOCSP64
, &segment
) < 0) {
93 perror("XFS_IOC_ALLOCSP64");
105 xfs_flock64_t segment
;
107 if (!offset_length(argv
[1], argv
[2], &segment
)) {
112 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_FREESP64
, &segment
) < 0) {
113 perror("XFS_IOC_FREESP64");
125 xfs_flock64_t segment
;
127 if (!offset_length(argv
[1], argv
[2], &segment
)) {
132 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_RESVSP64
, &segment
) < 0) {
133 perror("XFS_IOC_RESVSP64");
145 xfs_flock64_t segment
;
147 if (!offset_length(argv
[1], argv
[2], &segment
)) {
152 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_UNRESVSP64
, &segment
) < 0) {
153 perror("XFS_IOC_UNRESVSP64");
165 xfs_flock64_t segment
;
167 if (!offset_length(argv
[1], argv
[2], &segment
)) {
172 if (xfsctl(file
->name
, file
->fd
, XFS_IOC_ZERO_RANGE
, &segment
) < 0) {
173 perror("XFS_IOC_ZERO_RANGE");
186 " modifies space associated with part of a file via fallocate"
189 " 'falloc 0 1m' - fills all holes within the first megabyte\n"
191 " falloc uses the fallocate system call to alter space allocations in the\n"
192 " open file. The following operations are supported:\n"
193 " All the file offsets are in units of bytes.\n"
194 " -c -- collapses the given range.\n"
195 " -i -- inserts a hole into the given range of the file.\n"
196 " -k -- do not change file size.\n"
197 " -p -- unmap the given range from the file.\n"
198 " -u -- unshare shared extents in the given range.\n"
207 xfs_flock64_t segment
;
211 while ((c
= getopt(argc
, argv
, "cikpu")) != EOF
) {
214 mode
= FALLOC_FL_COLLAPSE_RANGE
;
217 mode
= FALLOC_FL_INSERT_RANGE
;
220 mode
= FALLOC_FL_KEEP_SIZE
;
223 mode
= FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
;
226 mode
= FALLOC_FL_UNSHARE_RANGE
;
230 command_usage(&falloc_cmd
);
233 if (optind
!= argc
- 2) {
235 return command_usage(&falloc_cmd
);
238 if (!offset_length(argv
[optind
], argv
[optind
+1], &segment
)) {
243 if (fallocate(file
->fd
, mode
,
244 segment
.l_start
, segment
.l_len
)) {
257 xfs_flock64_t segment
;
258 int mode
= FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
;
260 if (!offset_length(argv
[1], argv
[2], &segment
)) {
265 if (fallocate(file
->fd
, mode
,
266 segment
.l_start
, segment
.l_len
)) {
279 xfs_flock64_t segment
;
280 int mode
= FALLOC_FL_COLLAPSE_RANGE
;
282 if (!offset_length(argv
[1], argv
[2], &segment
)) {
287 if (fallocate(file
->fd
, mode
,
288 segment
.l_start
, segment
.l_len
)) {
301 xfs_flock64_t segment
;
302 int mode
= FALLOC_FL_INSERT_RANGE
;
304 if (!offset_length(argv
[1], argv
[2], &segment
)) {
309 if (fallocate(file
->fd
, mode
,
310 segment
.l_start
, segment
.l_len
)) {
323 xfs_flock64_t segment
;
324 int mode
= FALLOC_FL_ZERO_RANGE
;
327 while ((c
= getopt(argc
, argv
, "k")) != EOF
) {
330 mode
|= FALLOC_FL_KEEP_SIZE
;
333 command_usage(&fzero_cmd
);
336 if (optind
!= argc
- 2)
337 return command_usage(&fzero_cmd
);
339 if (!offset_length(argv
[optind
], argv
[optind
+ 1], &segment
))
342 if (fallocate(file
->fd
, mode
, segment
.l_start
, segment
.l_len
)) {
354 xfs_flock64_t segment
;
356 int mode
= FALLOC_FL_UNSHARE_RANGE
;
358 while ((c
= getopt(argc
, argv
, "")) != EOF
) {
361 command_usage(&funshare_cmd
);
364 if (optind
!= argc
- 2)
365 return command_usage(&funshare_cmd
);
367 if (!offset_length(argv
[optind
], argv
[optind
+ 1], &segment
)) {
372 if (fallocate(file
->fd
, mode
, segment
.l_start
, segment
.l_len
)) {
383 allocsp_cmd
.name
= "allocsp";
384 allocsp_cmd
.cfunc
= allocsp_f
;
385 allocsp_cmd
.argmin
= 2;
386 allocsp_cmd
.argmax
= 2;
387 allocsp_cmd
.flags
= CMD_NOMAP_OK
;
388 allocsp_cmd
.args
= _("off len");
389 allocsp_cmd
.oneline
= _("allocates zeroed space for part of a file");
391 freesp_cmd
.name
= "freesp";
392 freesp_cmd
.cfunc
= freesp_f
;
393 freesp_cmd
.argmin
= 2;
394 freesp_cmd
.argmax
= 2;
395 freesp_cmd
.flags
= CMD_NOMAP_OK
;
396 freesp_cmd
.args
= _("off len");
397 freesp_cmd
.oneline
= _("frees space associated with part of a file");
399 resvsp_cmd
.name
= "resvsp";
400 resvsp_cmd
.cfunc
= resvsp_f
;
401 resvsp_cmd
.argmin
= 2;
402 resvsp_cmd
.argmax
= 2;
403 resvsp_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
404 resvsp_cmd
.args
= _("off len");
406 _("reserves space associated with part of a file");
408 unresvsp_cmd
.name
= "unresvsp";
409 unresvsp_cmd
.cfunc
= unresvsp_f
;
410 unresvsp_cmd
.argmin
= 2;
411 unresvsp_cmd
.argmax
= 2;
412 unresvsp_cmd
.args
= _("off len");
413 unresvsp_cmd
.flags
= CMD_NOMAP_OK
;
414 unresvsp_cmd
.oneline
=
415 _("frees reserved space associated with part of a file");
417 zero_cmd
.name
= "zero";
418 zero_cmd
.cfunc
= zero_f
;
421 zero_cmd
.flags
= CMD_NOMAP_OK
;
422 zero_cmd
.args
= _("off len");
424 _("Converts the given range of a file to allocated zeros");
426 add_command(&allocsp_cmd
);
427 add_command(&freesp_cmd
);
428 add_command(&resvsp_cmd
);
429 add_command(&unresvsp_cmd
);
430 add_command(&zero_cmd
);
432 falloc_cmd
.name
= "falloc";
433 falloc_cmd
.cfunc
= fallocate_f
;
434 falloc_cmd
.argmin
= 2;
435 falloc_cmd
.argmax
= -1;
436 falloc_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
437 falloc_cmd
.args
= _("[-c] [-k] [-p] [-u] off len");
439 _("allocates space associated with part of a file via fallocate");
440 falloc_cmd
.help
= falloc_help
;
441 add_command(&falloc_cmd
);
443 fpunch_cmd
.name
= "fpunch";
444 fpunch_cmd
.cfunc
= fpunch_f
;
445 fpunch_cmd
.argmin
= 2;
446 fpunch_cmd
.argmax
= 2;
447 fpunch_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
448 fpunch_cmd
.args
= _("off len");
450 _("de-allocates space associated with part of a file via fallocate");
451 add_command(&fpunch_cmd
);
453 fcollapse_cmd
.name
= "fcollapse";
454 fcollapse_cmd
.cfunc
= fcollapse_f
;
455 fcollapse_cmd
.argmin
= 2;
456 fcollapse_cmd
.argmax
= 2;
457 fcollapse_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
458 fcollapse_cmd
.args
= _("off len");
459 fcollapse_cmd
.oneline
=
460 _("de-allocates space and eliminates the hole by shifting extents");
461 add_command(&fcollapse_cmd
);
463 finsert_cmd
.name
= "finsert";
464 finsert_cmd
.cfunc
= finsert_f
;
465 finsert_cmd
.argmin
= 2;
466 finsert_cmd
.argmax
= 2;
467 finsert_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
468 finsert_cmd
.args
= _("off len");
469 finsert_cmd
.oneline
=
470 _("creates new space for writing within file by shifting extents");
471 add_command(&finsert_cmd
);
473 fzero_cmd
.name
= "fzero";
474 fzero_cmd
.cfunc
= fzero_f
;
475 fzero_cmd
.argmin
= 2;
476 fzero_cmd
.argmax
= 3;
477 fzero_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
478 fzero_cmd
.args
= _("[-k] off len");
480 _("zeroes space and eliminates holes by preallocating");
481 add_command(&fzero_cmd
);
483 funshare_cmd
.name
= "funshare";
484 funshare_cmd
.cfunc
= funshare_f
;
485 funshare_cmd
.argmin
= 2;
486 funshare_cmd
.argmax
= 2;
487 funshare_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
488 funshare_cmd
.args
= _("off len");
489 funshare_cmd
.oneline
=
490 _("unshares shared blocks within the range");
491 add_command(&funshare_cmd
);