]>
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
24 static cmdinfo_t free_cmd
;
31 " reports the number of free disk blocks and inodes\n"
33 " This command reports the number of total, used, and available disk blocks.\n"
34 " It can optionally report the same set of numbers for inodes and realtime\n"
35 " disk blocks, and will report on all known XFS filesystem mount points and\n"
36 " project quota paths by default (see 'print' command for a list).\n"
37 " -b -- report the block count values\n"
38 " -i -- report the inode count values\n"
39 " -r -- report the realtime block count values\n"
40 " -h -- report in a human-readable format\n"
41 " -N -- suppress the header from the output\n"
46 * The data and realtime block counts returned (count, used, and
47 * free) are all in basic block units.
50 mount_free_space_data(
51 struct fs_path
*mount
,
62 struct xfs_fsop_counts fscounts
;
63 struct xfs_fsop_geom fsgeo
;
65 uint64_t logsize
, count
, free
;
68 if ((fd
= open(mount
->fs_dir
, O_RDONLY
)) < 0) {
70 fprintf(stderr
, "%s: cannot open %s: %s\n",
71 progname
, mount
->fs_dir
, strerror(errno
));
75 if (platform_fstatfs(fd
, &st
) < 0) {
81 if (!(mount
->fs_flags
& FS_FOREIGN
)) {
82 if ((xfsctl(mount
->fs_dir
, fd
, XFS_IOC_FSGEOMETRY_V1
,
84 perror("XFS_IOC_FSGEOMETRY_V1");
88 if ((xfsctl(mount
->fs_dir
, fd
, XFS_IOC_FSCOUNTS
,
90 perror("XFS_IOC_FSCOUNTS");
95 logsize
= fsgeo
.logstart
? fsgeo
.logblocks
: 0;
96 count
= (fsgeo
.datablocks
- logsize
) * fsgeo
.blocksize
;
97 free
= fscounts
.freedata
* fsgeo
.blocksize
;
98 *bcount
= BTOBB(count
);
100 *bused
= BTOBB(count
- free
);
102 count
= fsgeo
.rtextents
* fsgeo
.rtextsize
* fsgeo
.blocksize
;
103 free
= fscounts
.freertx
* fsgeo
.rtextsize
* fsgeo
.blocksize
;
104 *rcount
= BTOBB(count
);
105 *rfree
= BTOBB(free
);
106 *rused
= BTOBB(count
- free
);
108 count
= st
.f_blocks
* st
.f_bsize
;
109 free
= st
.f_bfree
* st
.f_bsize
;
110 *bcount
= BTOBB(count
);
111 *bfree
= BTOBB(free
);
112 *bused
= BTOBB(count
- free
);
120 *icount
= st
.f_files
;
122 *iused
= st
.f_files
- st
.f_ffree
;
129 * The data and realtime block counts returned (count, used, and
130 * free) are all in basic block units.
133 projects_free_space_data(
134 struct fs_path
*path
,
148 uint type
= XFS_PROJ_QUOTA
;
149 char *dev
= path
->fs_name
;
152 if (xfsquotactl(XFS_GETQSTAT
, dev
, type
, 0, &qfs
) < 0 ||
153 !(qfs
.qs_flags
& XFS_QUOTA_PDQ_ACCT
))
156 if ((fd
= open(path
->fs_dir
, O_RDONLY
)) < 0) {
158 fprintf(stderr
, "%s: cannot open %s: %s\n",
159 progname
, path
->fs_dir
, strerror(errno
));
163 if ((xfsctl(path
->fs_dir
, fd
, FS_IOC_FSGETXATTR
, &fsx
)) < 0) {
165 perror("FS_IOC_FSGETXATTR");
169 if (!(fsx
.fsx_xflags
& FS_XFLAG_PROJINHERIT
)) {
171 fprintf(stderr
, _("%s: project quota flag not set on %s\n"),
172 progname
, path
->fs_dir
);
177 if (path
->fs_prid
!= fsx
.fsx_projid
) {
180 _("%s: project ID %u (%s) doesn't match ID %u (%s)\n"),
181 progname
, path
->fs_prid
, projects_file
,
182 fsx
.fsx_projid
, path
->fs_dir
);
187 xfsquotactl(XFS_QSYNC
, dev
, type
, fsx
.fsx_projid
, NULL
);
188 if (xfsquotactl(XFS_GETQUOTA
, dev
, type
, fsx
.fsx_projid
, &d
) < 0) {
189 perror("XFS_GETQUOTA");
194 /* If no softlimit is set for any of blk/ino/rt, get actual usage */
195 if (!d
.d_blk_softlimit
|| !d
.d_ino_softlimit
|| !d
.d_rtb_softlimit
) {
196 mount_free_space_data(path
, bcount
, bused
, bfree
,
197 icount
, iused
, ifree
, rcount
, rused
, rfree
);
200 if (d
.d_blk_softlimit
) {
201 *bcount
= d
.d_blk_softlimit
;
202 *bfree
= (d
.d_blk_softlimit
- d
.d_bcount
);
206 if (d
.d_ino_softlimit
) {
207 *icount
= d
.d_ino_softlimit
;
208 *ifree
= (d
.d_ino_softlimit
- d
.d_icount
);
212 if (d
.d_rtb_softlimit
) {
213 *rcount
= d
.d_rtb_softlimit
;
214 *rfree
= (d
.d_rtb_softlimit
- d
.d_rtbcount
);
216 *rused
= d
.d_rtbcount
;
229 uint64_t bcount
, bused
, bfree
;
230 uint64_t icount
, iused
, ifree
;
231 uint64_t rcount
, rused
, rfree
;
232 char a
[8], s
[8], u
[8], p
[8];
235 count
= (path
->fs_flags
& FS_PROJECT_PATH
) ?
236 projects_free_space_data(path
, &bcount
, &bused
, &bfree
,
237 &icount
, &iused
, &ifree
,
238 &rcount
, &rused
, &rfree
) :
239 mount_free_space_data(path
, &bcount
, &bused
, &bfree
,
240 &icount
, &iused
, &ifree
,
241 &rcount
, &rused
, &rfree
);
245 if (!(flags
& NO_HEADER_FLAG
)) {
246 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
247 _("Filesystem ") : _("Filesystem "));
248 if (form
& (XFS_BLOCK_QUOTA
|XFS_RTBLOCK_QUOTA
))
249 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
250 _(" Size Used Avail Use%%") :
251 _(" 1K-blocks Used Available Use%%"));
252 else if (form
& XFS_INODE_QUOTA
)
253 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
254 _(" Inodes Used Free Use%%") :
255 _(" Inodes IUsed IFree IUse%%"));
256 fprintf(fp
, _(" Pathname\n"));
259 if (flags
& HUMAN_FLAG
) {
260 count
= fprintf(fp
, "%-12s", path
->fs_name
);
262 fprintf(fp
, "\n%12s", " ");
264 count
= fprintf(fp
, "%-19s", path
->fs_name
);
266 fprintf(fp
, "\n%19s", " ");
269 if (form
& XFS_BLOCK_QUOTA
) {
270 if (flags
& HUMAN_FLAG
)
271 fprintf(fp
, " %6s %6s %6s %3s%%",
272 bbs_to_string(bcount
, s
, sizeof(s
)),
273 bbs_to_string(bused
, u
, sizeof(u
)),
274 bbs_to_string(bfree
, a
, sizeof(a
)),
275 pct_to_string(bused
, bcount
, p
, sizeof(p
)));
277 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
278 (unsigned long long)bcount
>> 1,
279 (unsigned long long)bused
>> 1,
280 (unsigned long long)bfree
>> 1,
281 pct_to_string(bused
, bcount
, p
, sizeof(p
)));
282 } else if (form
& XFS_INODE_QUOTA
) {
283 if (flags
& HUMAN_FLAG
)
284 fprintf(fp
, " %6s %6s %6s %3s%%",
285 num_to_string(icount
, s
, sizeof(s
)),
286 num_to_string(iused
, u
, sizeof(u
)),
287 num_to_string(ifree
, a
, sizeof(a
)),
288 pct_to_string(iused
, icount
, p
, sizeof(p
)));
290 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
291 (unsigned long long)icount
,
292 (unsigned long long)iused
,
293 (unsigned long long)ifree
,
294 pct_to_string(iused
, icount
, p
, sizeof(p
)));
295 } else if (form
& XFS_RTBLOCK_QUOTA
) {
296 if (flags
& HUMAN_FLAG
)
297 fprintf(fp
, " %6s %6s %6s %3s%%",
298 bbs_to_string(rcount
, s
, sizeof(s
)),
299 bbs_to_string(rused
, u
, sizeof(u
)),
300 bbs_to_string(rfree
, a
, sizeof(a
)),
301 pct_to_string(rused
, rcount
, p
, sizeof(p
)));
303 fprintf(fp
, " %10llu %10llu %10llu %3s%%",
304 (unsigned long long)rcount
>> 1,
305 (unsigned long long)rused
>> 1,
306 (unsigned long long)rfree
>> 1,
307 pct_to_string(rused
, rcount
, p
, sizeof(p
)));
309 fprintf(fp
, " %s\n", path
->fs_dir
);
323 fs_cursor_initialise(dir
, 0, &cursor
);
324 while ((path
= fs_cursor_next_entry(&cursor
))) {
325 if (free_space(fp
, form
, path
, flags
))
326 flags
|= NO_HEADER_FLAG
;
337 int c
, flags
= 0, form
= 0;
339 while ((c
= getopt(argc
, argv
, "bf:hNir")) != EOF
) {
345 form
|= XFS_BLOCK_QUOTA
;
348 form
|= XFS_INODE_QUOTA
;
351 form
|= XFS_RTBLOCK_QUOTA
;
357 flags
|= NO_HEADER_FLAG
;
360 return command_usage(&free_cmd
);
365 form
= XFS_BLOCK_QUOTA
;
367 if ((fp
= fopen_write_secure(fname
)) == NULL
)
371 free_space_list(fp
, form
, NULL
, flags
);
372 else while (argc
> optind
)
373 free_space_list(fp
, form
, argv
[optind
++], flags
);
383 free_cmd
.name
= "df";
384 free_cmd
.altname
= "free";
385 free_cmd
.cfunc
= free_f
;
387 free_cmd
.argmax
= -1;
388 free_cmd
.args
= _("[-bir] [-hN] [-f file]");
389 free_cmd
.oneline
= _("show free and used counts for blocks and inodes");
390 free_cmd
.help
= free_help
;
391 free_cmd
.flags
= CMD_FLAG_FOREIGN_OK
;
393 add_command(&free_cmd
);