]>
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 mount_free_space_data(
46 struct fs_path
*mount
,
57 struct xfs_fsop_counts fscounts
;
58 struct xfs_fsop_geom fsgeo
;
60 __uint64_t logsize
, count
, free
;
63 if ((fd
= open(mount
->fs_dir
, O_RDONLY
)) < 0) {
64 fprintf(stderr
, "%s: cannot open %s: %s\n",
65 progname
, mount
->fs_dir
, strerror(errno
));
69 if (platform_fstatfs(fd
, &st
) < 0) {
74 if ((xfsctl(mount
->fs_dir
, fd
, XFS_IOC_FSGEOMETRY_V1
, &fsgeo
)) < 0) {
75 perror("XFS_IOC_FSGEOMETRY_V1");
79 if ((xfsctl(mount
->fs_dir
, fd
, XFS_IOC_FSCOUNTS
, &fscounts
)) < 0) {
80 perror("XFS_IOC_FSCOUNTS");
85 logsize
= fsgeo
.logstart
? fsgeo
.logblocks
: 0;
86 count
= (fsgeo
.datablocks
- logsize
) * fsgeo
.blocksize
;
87 free
= fscounts
.freedata
* fsgeo
.blocksize
;
88 *bcount
= BTOBB(count
);
90 *bused
= BTOBB(count
- free
);
94 *iused
= st
.f_files
- st
.f_ffree
;
96 count
= fsgeo
.rtextents
* fsgeo
.rtextsize
;
97 free
= fscounts
.freertx
* fsgeo
.rtextsize
;
98 *rcount
= BTOBB(count
);
100 *rused
= BTOBB(count
- free
);
107 projects_free_space_data(
108 struct fs_path
*path
,
121 uint type
= XFS_PROJ_QUOTA
;
122 char *dev
= path
->fs_name
;
125 if ((fd
= open(path
->fs_dir
, O_RDONLY
)) < 0) {
126 fprintf(stderr
, "%s: cannot open %s: %s\n",
127 progname
, path
->fs_dir
, strerror(errno
));
131 if ((xfsctl(path
->fs_dir
, fd
, XFS_IOC_FSGETXATTR
, &fsx
)) < 0) {
132 perror("XFS_IOC_FSGETXATTR");
136 if (!(fsx
.fsx_xflags
& XFS_XFLAG_PROJINHERIT
)) {
137 fprintf(stderr
, _("%s: project quota flag not set on %s\n"),
138 progname
, path
->fs_dir
);
143 if (path
->fs_prid
!= fsx
.fsx_projid
) {
145 _("%s: project ID %u (%s) doesn't match ID %u (%s)\n"),
146 progname
, path
->fs_prid
, projects_file
,
147 fsx
.fsx_projid
, path
->fs_dir
);
152 xfsquotactl(XFS_QSYNC
, dev
, type
, fsx
.fsx_projid
, NULL
);
153 if (xfsquotactl(XFS_GETQUOTA
, dev
, type
, fsx
.fsx_projid
, &d
) < 0) {
154 perror("XFS_GETQUOTA");
159 /* If no softlimit is set for any of blk/ino/rt, get actual usage */
160 if (!d
.d_blk_softlimit
|| !d
.d_ino_softlimit
|| !d
.d_rtb_softlimit
) {
161 mount_free_space_data(path
, bcount
, bused
, bfree
,
162 icount
, iused
, ifree
, rcount
, rused
, rfree
);
165 if (d
.d_blk_softlimit
) {
166 *bcount
= d
.d_blk_softlimit
<< 1;
167 *bfree
= (d
.d_blk_softlimit
- d
.d_bcount
) << 1;
169 *bused
= d
.d_bcount
<< 1;
170 if (d
.d_ino_softlimit
) {
171 *icount
= d
.d_ino_softlimit
;
172 *ifree
= (d
.d_ino_softlimit
- d
.d_icount
);
175 if (d
.d_rtb_softlimit
) {
176 *rcount
= d
.d_rtb_softlimit
<< 1;
177 *rfree
= (d
.d_rtb_softlimit
- d
.d_rtbcount
) << 1;
179 *rcount
= d
.d_rtbcount
<< 1;
192 __uint64_t bcount
, bused
, bfree
;
193 __uint64_t icount
, iused
, ifree
;
194 __uint64_t rcount
, rused
, rfree
;
195 char a
[8], s
[8], u
[8], p
[8];
198 count
= (path
->fs_flags
& FS_PROJECT_PATH
) ?
199 projects_free_space_data(path
, &bcount
, &bused
, &bfree
,
200 &icount
, &iused
, &ifree
,
201 &rcount
, &rused
, &rfree
) :
202 mount_free_space_data(path
, &bcount
, &bused
, &bfree
,
203 &icount
, &iused
, &ifree
,
204 &rcount
, &rused
, &rfree
);
208 if (!(flags
& NO_HEADER_FLAG
)) {
209 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
210 _("Filesystem ") : _("Filesystem "));
211 if (form
& (XFS_BLOCK_QUOTA
|XFS_RTBLOCK_QUOTA
))
212 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
213 _(" Size Used Avail Use%%") :
214 _(" 1K-blocks Used Available Use%%"));
215 else if (form
& XFS_INODE_QUOTA
)
216 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
217 _(" Inodes Used Free Use%%") :
218 _(" Inodes IUsed IFree IUse%%"));
219 fprintf(fp
, _(" Pathname\n"));
222 if (flags
& HUMAN_FLAG
) {
223 count
= fprintf(fp
, "%-12s", path
->fs_name
);
225 fprintf(fp
, "\n%12s", " ");
227 count
= fprintf(fp
, "%-19s", path
->fs_name
);
229 fprintf(fp
, "\n%19s", " ");
232 if (form
& XFS_BLOCK_QUOTA
) {
233 if (flags
& HUMAN_FLAG
)
234 fprintf(fp
, " %6s %6s %6s %3s%%",
235 bbs_to_string(bcount
, s
, sizeof(s
)),
236 bbs_to_string(bused
, u
, sizeof(u
)),
237 bbs_to_string(bfree
, a
, sizeof(a
)),
238 pct_to_string(bused
, bcount
, p
, sizeof(p
)));
240 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
241 (unsigned long long)bcount
>> 1,
242 (unsigned long long)bused
>> 1,
243 (unsigned long long)bfree
>> 1,
244 pct_to_string(bused
, bcount
, p
, sizeof(p
)));
245 } else if (form
& XFS_INODE_QUOTA
) {
246 if (flags
& HUMAN_FLAG
)
247 fprintf(fp
, " %6s %6s %6s %3s%%",
248 num_to_string(icount
, s
, sizeof(s
)),
249 num_to_string(iused
, u
, sizeof(u
)),
250 num_to_string(ifree
, a
, sizeof(a
)),
251 pct_to_string(iused
, icount
, p
, sizeof(p
)));
253 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
254 (unsigned long long)icount
,
255 (unsigned long long)iused
,
256 (unsigned long long)ifree
,
257 pct_to_string(iused
, icount
, p
, sizeof(p
)));
258 } else if (form
& XFS_RTBLOCK_QUOTA
) {
259 if (flags
& HUMAN_FLAG
)
260 fprintf(fp
, " %6s %6s %6s %3s%%",
261 bbs_to_string(rcount
, s
, sizeof(s
)),
262 bbs_to_string(rused
, u
, sizeof(u
)),
263 bbs_to_string(rfree
, a
, sizeof(a
)),
264 pct_to_string(rused
, rcount
, p
, sizeof(p
)));
266 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
267 (unsigned long long)rcount
>> 1,
268 (unsigned long long)rused
>> 1,
269 (unsigned long long)rfree
>> 1,
270 pct_to_string(rused
, rcount
, p
, sizeof(p
)));
272 fprintf(fp
, " %s\n", path
->fs_dir
);
287 fs_cursor_initialise(dir
, type
, &cursor
);
288 while ((path
= fs_cursor_next_entry(&cursor
))) {
289 if (free_space(fp
, form
, path
, flags
))
290 flags
|= NO_HEADER_FLAG
;
301 int c
, flags
= 0, form
= 0, type
= 0;
303 while ((c
= getopt(argc
, argv
, "bf:hNir")) != EOF
) {
309 form
|= XFS_BLOCK_QUOTA
;
312 form
|= XFS_INODE_QUOTA
;
315 form
|= XFS_RTBLOCK_QUOTA
;
321 flags
|= NO_HEADER_FLAG
;
324 return command_usage(&free_cmd
);
329 form
= XFS_BLOCK_QUOTA
;
332 type
= FS_MOUNT_POINT
|FS_PROJECT_PATH
;
334 if ((fp
= fopen_write_secure(fname
)) == NULL
)
338 free_space_list(fp
, form
, type
, NULL
, flags
);
339 else while (argc
> optind
)
340 free_space_list(fp
, form
, type
, argv
[optind
++], flags
);
350 free_cmd
.name
= _("df");
351 free_cmd
.altname
= _("free");
352 free_cmd
.cfunc
= free_f
;
354 free_cmd
.argmax
= -1;
355 free_cmd
.args
= _("[-bir] [-hn] [-f file]");
356 free_cmd
.oneline
= _("show free and used counts for blocks and inodes");
357 free_cmd
.help
= free_help
;
359 add_command(&free_cmd
);