]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - spaceman/prealloc.c
e5d857bdd3345d54ee5efa7f23ba778935e43f0c
[thirdparty/xfsprogs-dev.git] / spaceman / prealloc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012 Red Hat, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "libxfs.h"
8 #include "libfrog/fsgeom.h"
9 #include "command.h"
10 #include "input.h"
11 #include "init.h"
12 #include "libfrog/paths.h"
13 #include "space.h"
14
15 static cmdinfo_t prealloc_cmd;
16
17 /*
18 * Control preallocation amounts.
19 */
20 static int
21 prealloc_f(
22 int argc,
23 char **argv)
24 {
25 struct xfs_fs_eofblocks eofb = {0};
26 struct xfs_fsop_geom *fsgeom = &file->xfd.fsgeom;
27 int c;
28
29 eofb.eof_version = XFS_EOFBLOCKS_VERSION;
30
31 while ((c = getopt(argc, argv, "g:m:p:su:")) != EOF) {
32 switch (c) {
33 case 'g':
34 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
35 eofb.eof_gid = cvt_u32(optarg, 10);
36 if (errno)
37 return command_usage(&prealloc_cmd);
38 break;
39 case 'u':
40 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
41 eofb.eof_uid = cvt_u32(optarg, 10);
42 if (errno)
43 return command_usage(&prealloc_cmd);
44 break;
45 case 'p':
46 eofb.eof_flags |= XFS_EOF_FLAGS_PRID;
47 eofb.eof_prid = cvt_u32(optarg, 10);
48 if (errno)
49 return command_usage(&prealloc_cmd);
50 break;
51 case 's':
52 eofb.eof_flags |= XFS_EOF_FLAGS_SYNC;
53 break;
54 case 'm':
55 eofb.eof_flags |= XFS_EOF_FLAGS_MINFILESIZE;
56 eofb.eof_min_file_size = cvtnum(fsgeom->blocksize,
57 fsgeom->sectsize, optarg);
58 break;
59 case '?':
60 default:
61 return command_usage(&prealloc_cmd);
62 }
63 }
64 if (optind != argc)
65 return command_usage(&prealloc_cmd);
66
67 if (ioctl(file->xfd.fd, XFS_IOC_FREE_EOFBLOCKS, &eofb) < 0) {
68 fprintf(stderr, _("%s: XFS_IOC_FREE_EOFBLOCKS on %s: %s\n"),
69 progname, file->name, strerror(errno));
70 }
71 return 0;
72 }
73
74 static void
75 prealloc_help(void)
76 {
77 printf(_(
78 "\n"
79 "Remove speculative preallocation\n"
80 "\n"
81 " -g gid -- remove prealloc on files matching group <gid>\n"
82 " -m minlen -- only consider files larger than <minlen>\n"
83 " -p prid -- remove prealloc on files matching project <prid>\n"
84 " -s -- wait for removal to complete\n"
85 " -u uid -- remove prealloc on files matching user <uid>\n"
86 "\n"
87 "If none of -u, -g, or -p are specified, this command acts on all files.\n"
88 "minlen can take units.\n"
89 "\n"));
90
91 }
92
93 void
94 prealloc_init(void)
95 {
96 prealloc_cmd.name = "prealloc";
97 prealloc_cmd.altname = "prealloc";
98 prealloc_cmd.cfunc = prealloc_f;
99 prealloc_cmd.argmin = 1;
100 prealloc_cmd.argmax = -1;
101 prealloc_cmd.args = "[-s] [-u id] [-g id] [-p id] [-m minlen]";
102 prealloc_cmd.flags = CMD_FLAG_ONESHOT;
103 prealloc_cmd.oneline = _("Remove speculative preallocation");
104 prealloc_cmd.help = prealloc_help;
105
106 add_command(&prealloc_cmd);
107 }
108