]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - quota/free.c
2 * Copyright (c) 2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <xfs/command.h>
23 static cmdinfo_t free_cmd
;
30 " reports the number of free disk blocks and inodes\n"
32 " This command reports the number of total, used, and available disk blocks.\n"
33 " It can optionally report the same set of numbers for inodes and realtime\n"
34 " disk blocks, and will report on all known XFS filesystem mount points and\n"
35 " project quota paths by default (see 'print' command for a list).\n"
36 " -b -- report the block count values\n"
37 " -i -- report the inode count values\n"
38 " -r -- report the realtime block count values\n"
39 " -h -- report in a human-readable format\n"
40 " -N -- suppress the header from the output\n"
45 * The data and realtime block counts returned (count, used, and
46 * free) are all in basic block units.
49 mount_free_space_data(
50 struct fs_path
*mount
,
61 struct xfs_fsop_counts fscounts
;
62 struct xfs_fsop_geom fsgeo
;
64 __uint64_t logsize
, count
, free
;
67 if ((fd
= open(mount
->fs_dir
, O_RDONLY
)) < 0) {
69 fprintf(stderr
, "%s: cannot open %s: %s\n",
70 progname
, mount
->fs_dir
, strerror(errno
));
74 if (platform_fstatfs(fd
, &st
) < 0) {
79 if ((xfsctl(mount
->fs_dir
, fd
, XFS_IOC_FSGEOMETRY_V1
, &fsgeo
)) < 0) {
80 perror("XFS_IOC_FSGEOMETRY_V1");
84 if ((xfsctl(mount
->fs_dir
, fd
, XFS_IOC_FSCOUNTS
, &fscounts
)) < 0) {
85 perror("XFS_IOC_FSCOUNTS");
90 logsize
= fsgeo
.logstart
? fsgeo
.logblocks
: 0;
91 count
= (fsgeo
.datablocks
- logsize
) * fsgeo
.blocksize
;
92 free
= fscounts
.freedata
* fsgeo
.blocksize
;
93 *bcount
= BTOBB(count
);
95 *bused
= BTOBB(count
- free
);
99 *iused
= st
.f_files
- st
.f_ffree
;
101 count
= fsgeo
.rtextents
* fsgeo
.rtextsize
* fsgeo
.blocksize
;
102 free
= fscounts
.freertx
* fsgeo
.rtextsize
* fsgeo
.blocksize
;
103 *rcount
= BTOBB(count
);
104 *rfree
= BTOBB(free
);
105 *rused
= BTOBB(count
- free
);
112 * The data and realtime block counts returned (count, used, and
113 * free) are all in basic block units.
116 projects_free_space_data(
117 struct fs_path
*path
,
131 uint type
= XFS_PROJ_QUOTA
;
132 char *dev
= path
->fs_name
;
135 if (xfsquotactl(XFS_GETQSTAT
, dev
, type
, 0, &qfs
) < 0 ||
136 !(qfs
.qs_flags
& XFS_QUOTA_PDQ_ACCT
))
139 if ((fd
= open(path
->fs_dir
, O_RDONLY
)) < 0) {
141 fprintf(stderr
, "%s: cannot open %s: %s\n",
142 progname
, path
->fs_dir
, strerror(errno
));
146 if ((xfsctl(path
->fs_dir
, fd
, XFS_IOC_FSGETXATTR
, &fsx
)) < 0) {
148 perror("XFS_IOC_FSGETXATTR");
152 if (!(fsx
.fsx_xflags
& XFS_XFLAG_PROJINHERIT
)) {
154 fprintf(stderr
, _("%s: project quota flag not set on %s\n"),
155 progname
, path
->fs_dir
);
160 if (path
->fs_prid
!= fsx
.fsx_projid
) {
163 _("%s: project ID %u (%s) doesn't match ID %u (%s)\n"),
164 progname
, path
->fs_prid
, projects_file
,
165 fsx
.fsx_projid
, path
->fs_dir
);
170 xfsquotactl(XFS_QSYNC
, dev
, type
, fsx
.fsx_projid
, NULL
);
171 if (xfsquotactl(XFS_GETQUOTA
, dev
, type
, fsx
.fsx_projid
, &d
) < 0) {
172 perror("XFS_GETQUOTA");
177 /* If no softlimit is set for any of blk/ino/rt, get actual usage */
178 if (!d
.d_blk_softlimit
|| !d
.d_ino_softlimit
|| !d
.d_rtb_softlimit
) {
179 mount_free_space_data(path
, bcount
, bused
, bfree
,
180 icount
, iused
, ifree
, rcount
, rused
, rfree
);
183 if (d
.d_blk_softlimit
) {
184 *bcount
= d
.d_blk_softlimit
;
185 *bfree
= (d
.d_blk_softlimit
- d
.d_bcount
);
189 if (d
.d_ino_softlimit
) {
190 *icount
= d
.d_ino_softlimit
;
191 *ifree
= (d
.d_ino_softlimit
- d
.d_icount
);
195 if (d
.d_rtb_softlimit
) {
196 *rcount
= d
.d_rtb_softlimit
;
197 *rfree
= (d
.d_rtb_softlimit
- d
.d_rtbcount
);
199 *rused
= d
.d_rtbcount
;
212 __uint64_t bcount
, bused
, bfree
;
213 __uint64_t icount
, iused
, ifree
;
214 __uint64_t rcount
, rused
, rfree
;
215 char a
[8], s
[8], u
[8], p
[8];
218 count
= (path
->fs_flags
& FS_PROJECT_PATH
) ?
219 projects_free_space_data(path
, &bcount
, &bused
, &bfree
,
220 &icount
, &iused
, &ifree
,
221 &rcount
, &rused
, &rfree
) :
222 mount_free_space_data(path
, &bcount
, &bused
, &bfree
,
223 &icount
, &iused
, &ifree
,
224 &rcount
, &rused
, &rfree
);
228 if (!(flags
& NO_HEADER_FLAG
)) {
229 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
230 _("Filesystem ") : _("Filesystem "));
231 if (form
& (XFS_BLOCK_QUOTA
|XFS_RTBLOCK_QUOTA
))
232 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
233 _(" Size Used Avail Use%%") :
234 _(" 1K-blocks Used Available Use%%"));
235 else if (form
& XFS_INODE_QUOTA
)
236 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
237 _(" Inodes Used Free Use%%") :
238 _(" Inodes IUsed IFree IUse%%"));
239 fprintf(fp
, _(" Pathname\n"));
242 if (flags
& HUMAN_FLAG
) {
243 count
= fprintf(fp
, "%-12s", path
->fs_name
);
245 fprintf(fp
, "\n%12s", " ");
247 count
= fprintf(fp
, "%-19s", path
->fs_name
);
249 fprintf(fp
, "\n%19s", " ");
252 if (form
& XFS_BLOCK_QUOTA
) {
253 if (flags
& HUMAN_FLAG
)
254 fprintf(fp
, " %6s %6s %6s %3s%%",
255 bbs_to_string(bcount
, s
, sizeof(s
)),
256 bbs_to_string(bused
, u
, sizeof(u
)),
257 bbs_to_string(bfree
, a
, sizeof(a
)),
258 pct_to_string(bused
, bcount
, p
, sizeof(p
)));
260 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
261 (unsigned long long)bcount
>> 1,
262 (unsigned long long)bused
>> 1,
263 (unsigned long long)bfree
>> 1,
264 pct_to_string(bused
, bcount
, p
, sizeof(p
)));
265 } else if (form
& XFS_INODE_QUOTA
) {
266 if (flags
& HUMAN_FLAG
)
267 fprintf(fp
, " %6s %6s %6s %3s%%",
268 num_to_string(icount
, s
, sizeof(s
)),
269 num_to_string(iused
, u
, sizeof(u
)),
270 num_to_string(ifree
, a
, sizeof(a
)),
271 pct_to_string(iused
, icount
, p
, sizeof(p
)));
273 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
274 (unsigned long long)icount
,
275 (unsigned long long)iused
,
276 (unsigned long long)ifree
,
277 pct_to_string(iused
, icount
, p
, sizeof(p
)));
278 } else if (form
& XFS_RTBLOCK_QUOTA
) {
279 if (flags
& HUMAN_FLAG
)
280 fprintf(fp
, " %6s %6s %6s %3s%%",
281 bbs_to_string(rcount
, s
, sizeof(s
)),
282 bbs_to_string(rused
, u
, sizeof(u
)),
283 bbs_to_string(rfree
, a
, sizeof(a
)),
284 pct_to_string(rused
, rcount
, p
, sizeof(p
)));
286 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
287 (unsigned long long)rcount
>> 1,
288 (unsigned long long)rused
>> 1,
289 (unsigned long long)rfree
>> 1,
290 pct_to_string(rused
, rcount
, p
, sizeof(p
)));
292 fprintf(fp
, " %s\n", path
->fs_dir
);
307 fs_cursor_initialise(dir
, type
, &cursor
);
308 while ((path
= fs_cursor_next_entry(&cursor
))) {
309 if (free_space(fp
, form
, path
, flags
))
310 flags
|= NO_HEADER_FLAG
;
321 int c
, flags
= 0, form
= 0, type
= 0;
323 while ((c
= getopt(argc
, argv
, "bf:hNir")) != EOF
) {
329 form
|= XFS_BLOCK_QUOTA
;
332 form
|= XFS_INODE_QUOTA
;
335 form
|= XFS_RTBLOCK_QUOTA
;
341 flags
|= NO_HEADER_FLAG
;
344 return command_usage(&free_cmd
);
349 form
= XFS_BLOCK_QUOTA
;
352 type
= FS_MOUNT_POINT
|FS_PROJECT_PATH
;
354 if ((fp
= fopen_write_secure(fname
)) == NULL
)
358 free_space_list(fp
, form
, type
, NULL
, flags
);
359 else while (argc
> optind
)
360 free_space_list(fp
, form
, type
, argv
[optind
++], flags
);
370 free_cmd
.name
= "df";
371 free_cmd
.altname
= "free";
372 free_cmd
.cfunc
= free_f
;
374 free_cmd
.argmax
= -1;
375 free_cmd
.args
= _("[-bir] [-hn] [-f file]");
376 free_cmd
.oneline
= _("show free and used counts for blocks and inodes");
377 free_cmd
.help
= free_help
;
379 add_command(&free_cmd
);