]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - quota/quota.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2005 Silicon Graphics, Inc.
15 static cmdinfo_t quota_cmd
;
22 " display usage and quota information\n"
24 " -g -- display group quota information\n"
25 " -p -- display project quota information\n"
26 " -u -- display user quota information\n"
27 " -b -- display number of blocks used\n"
28 " -i -- display number of inodes used\n"
29 " -r -- display number of realtime blocks used\n"
30 " -h -- report in a human-readable format\n"
31 " -n -- skip identifier-to-name translations, just report IDs\n"
32 " -N -- suppress the initial header\n"
33 " -v -- increase verbosity in reporting (also dumps zero values)\n"
34 " -f -- send output to a file\n"
35 " The (optional) user/group/project can be specified either by name or by\n"
36 " number (i.e. uid/gid/projid).\n"
51 char *dev
= mount
->fs_name
;
52 char c
[8], h
[8], s
[8];
56 xfsquotactl(XFS_QSYNC
, dev
, type
, 0, NULL
);
57 if (xfsquotactl(XFS_GETQUOTA
, dev
, type
, id
, (void *)&d
) < 0)
60 if (!(flags
& VERBOSE_FLAG
)) {
62 if ((form
& XFS_BLOCK_QUOTA
) && d
.d_bcount
)
64 if ((form
& XFS_INODE_QUOTA
) && d
.d_icount
)
66 if ((form
& XFS_RTBLOCK_QUOTA
) && d
.d_rtbcount
)
72 if (!(flags
& NO_HEADER_FLAG
)) {
74 _("Disk quotas for %s %s (%u)\nFilesystem%s"),
75 type_to_string(type
), name
, id
,
76 (flags
& HUMAN_FLAG
) ? " " : " ");
77 if (form
& XFS_BLOCK_QUOTA
)
78 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
79 _(" Blocks Quota Limit Warn/Time ") :
80 _(" Blocks Quota Limit Warn/Time "));
81 if (form
& XFS_INODE_QUOTA
)
82 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
83 _(" Files Quota Limit Warn/Time ") :
84 _(" Files Quota Limit Warn/Time "));
85 if (form
& XFS_RTBLOCK_QUOTA
)
86 fprintf(fp
, (flags
& HUMAN_FLAG
) ?
87 _("Realtime Quota Limit Warn/Time ") :
88 _(" Realtime Quota Limit Warn/Time "));
89 fputs("Mounted on\n", fp
);
92 if (flags
& HUMAN_FLAG
) {
93 count
= fprintf(fp
, "%-12s", dev
);
95 fprintf(fp
, "\n%12s", " ");
97 count
= fprintf(fp
, "%-19s", dev
);
99 fprintf(fp
, "\n%19s", " ");
102 if (form
& XFS_BLOCK_QUOTA
) {
103 qflags
= (flags
& HUMAN_FLAG
);
104 if (d
.d_blk_hardlimit
&& d
.d_bcount
> d
.d_blk_hardlimit
)
105 qflags
|= LIMIT_FLAG
;
106 if (d
.d_blk_softlimit
&& d
.d_bcount
> d
.d_blk_softlimit
)
107 qflags
|= QUOTA_FLAG
;
108 if (flags
& HUMAN_FLAG
)
109 fprintf(fp
, " %6s %6s %6s %02d %8s ",
110 bbs_to_string(d
.d_bcount
, c
, sizeof(c
)),
111 bbs_to_string(d
.d_blk_softlimit
, s
, sizeof(s
)),
112 bbs_to_string(d
.d_blk_hardlimit
, h
, sizeof(h
)),
114 time_to_string(d
.d_btimer
, qflags
));
116 fprintf(fp
, " %10llu %10llu %10llu %02d %9s ",
117 (unsigned long long)d
.d_bcount
>> 1,
118 (unsigned long long)d
.d_blk_softlimit
>> 1,
119 (unsigned long long)d
.d_blk_hardlimit
>> 1,
121 time_to_string(d
.d_btimer
, qflags
));
123 if (form
& XFS_INODE_QUOTA
) {
124 qflags
= (flags
& HUMAN_FLAG
);
125 if (d
.d_ino_hardlimit
&& d
.d_icount
> d
.d_ino_hardlimit
)
126 qflags
|= LIMIT_FLAG
;
127 if (d
.d_ino_softlimit
&& d
.d_icount
> d
.d_ino_softlimit
)
128 qflags
|= QUOTA_FLAG
;
129 if (flags
& HUMAN_FLAG
)
130 fprintf(fp
, " %6s %6s %6s %02d %8s ",
131 num_to_string(d
.d_icount
, c
, sizeof(c
)),
132 num_to_string(d
.d_ino_softlimit
, s
, sizeof(s
)),
133 num_to_string(d
.d_ino_hardlimit
, h
, sizeof(h
)),
135 time_to_string(d
.d_itimer
, qflags
));
137 fprintf(fp
, " %10llu %10llu %10llu %02d %9s ",
138 (unsigned long long)d
.d_icount
,
139 (unsigned long long)d
.d_ino_softlimit
,
140 (unsigned long long)d
.d_ino_hardlimit
,
142 time_to_string(d
.d_itimer
, qflags
));
144 if (form
& XFS_RTBLOCK_QUOTA
) {
145 qflags
= (flags
& HUMAN_FLAG
);
146 if (d
.d_rtb_hardlimit
&& d
.d_rtbcount
> d
.d_rtb_hardlimit
)
147 qflags
|= LIMIT_FLAG
;
148 if (d
.d_rtb_softlimit
&& d
.d_rtbcount
> d
.d_rtb_softlimit
)
149 qflags
|= QUOTA_FLAG
;
150 if (flags
& HUMAN_FLAG
)
151 fprintf(fp
, " %6s %6s %6s %02d %8s ",
152 bbs_to_string(d
.d_rtbcount
, c
, sizeof(c
)),
153 bbs_to_string(d
.d_rtb_softlimit
, s
, sizeof(s
)),
154 bbs_to_string(d
.d_rtb_hardlimit
, h
, sizeof(h
)),
156 time_to_string(d
.d_rtbtimer
, qflags
));
158 fprintf(fp
, " %10llu %10llu %10llu %02d %9s ",
159 (unsigned long long)d
.d_rtbcount
>> 1,
160 (unsigned long long)d
.d_rtb_softlimit
>> 1,
161 (unsigned long long)d
.d_rtb_hardlimit
>> 1,
163 time_to_string(d
.d_rtbtimer
, qflags
));
165 fprintf(fp
, "%s\n", mount
->fs_dir
);
181 fs_cursor_initialise(NULL
, FS_MOUNT_POINT
, &cursor
);
182 while ((path
= fs_cursor_next_entry(&cursor
))) {
183 if (quota_mount(fp
, id
, name
, form
, type
, path
, flags
))
184 flags
|= NO_HEADER_FLAG
;
193 static char buffer
[32];
196 struct passwd
*u
= getpwuid(uid
);
200 snprintf(buffer
, sizeof(buffer
), "#%u", uid
);
216 if (isdigits_only(name
)) {
218 name
= getusername(id
, flags
& NO_LOOKUP_FLAG
);
219 } else if ((u
= getpwnam(name
))) {
224 fprintf(stderr
, _("%s: cannot find user %s\n"),
230 name
= getusername(id
, flags
& NO_LOOKUP_FLAG
);
233 quota(fp
, id
, name
, form
, type
, flags
);
241 static char buffer
[32];
244 struct group
*g
= getgrgid(gid
);
248 snprintf(buffer
, sizeof(buffer
), "#%u", gid
);
261 gid_t gid
, *gids
= NULL
;
262 int i
, ngroups
, dofree
= 0;
265 if (isdigits_only(name
)) {
267 name
= getgroupname(gid
, flags
& NO_LOOKUP_FLAG
);
269 if ((g
= getgrnam(name
))) {
274 fprintf(stderr
, _("%s: cannot find group %s\n"),
282 if ( ((ngroups
= sysconf(_SC_NGROUPS_MAX
)) < 0) ||
283 ((gids
= malloc(ngroups
* sizeof(gid_t
))) == NULL
) ||
284 ((ngroups
= getgroups(ngroups
, gids
)) < 0)) {
285 /* something failed. Fall back to 1 group */
291 /* It all worked, and we allocated memory */
296 for (i
= 0; i
< ngroups
; i
++, name
= NULL
) {
298 name
= getgroupname(gids
[i
], flags
& NO_LOOKUP_FLAG
);
299 quota(fp
, gids
[i
], name
, form
, type
, flags
);
311 static char buffer
[32];
314 fs_project_t
*p
= getprprid(prid
);
318 snprintf(buffer
, sizeof(buffer
), "#%u", (unsigned int)prid
);
335 fprintf(stderr
, _("%s: must specify a project name/ID\n"),
340 if (isdigits_only(name
)) {
342 name
= getprojectname(id
, flags
& NO_LOOKUP_FLAG
);
343 } else if ((p
= getprnam(name
))) {
348 fprintf(stderr
, _("%s: cannot find project %s\n"),
353 quota(fp
, id
, name
, form
, type
, flags
);
366 quota_user_type(fp
, name
, form
, type
, flags
);
368 case XFS_GROUP_QUOTA
:
369 quota_group_type(fp
, name
, form
, type
, flags
);
372 quota_proj_type(fp
, name
, form
, type
, flags
);
384 int c
, flags
= 0, type
= 0, form
= 0;
386 while ((c
= getopt(argc
, argv
, "bf:ghnNipruv")) != EOF
) {
392 form
|= XFS_BLOCK_QUOTA
;
395 form
|= XFS_INODE_QUOTA
;
398 form
|= XFS_RTBLOCK_QUOTA
;
401 type
|= XFS_GROUP_QUOTA
;
404 type
|= XFS_PROJ_QUOTA
;
407 type
|= XFS_USER_QUOTA
;
413 flags
|= NO_LOOKUP_FLAG
;
416 flags
|= NO_HEADER_FLAG
;
419 flags
|= VERBOSE_FLAG
;
422 return command_usage("a_cmd
);
427 form
= XFS_BLOCK_QUOTA
;
430 type
= XFS_USER_QUOTA
;
431 } else if (type
!= XFS_GROUP_QUOTA
&&
432 type
!= XFS_PROJ_QUOTA
&&
433 type
!= XFS_USER_QUOTA
) {
434 return command_usage("a_cmd
);
437 if ((fp
= fopen_write_secure(fname
)) == NULL
)
441 quota_any_type(fp
, NULL
, form
, type
, flags
);
442 else while (argc
> optind
)
443 quota_any_type(fp
, argv
[optind
++], form
, type
, flags
);
453 quota_cmd
.name
= "quota";
454 quota_cmd
.altname
= "l";
455 quota_cmd
.cfunc
= quota_f
;
456 quota_cmd
.argmin
= 0;
457 quota_cmd
.argmax
= -1;
458 quota_cmd
.args
= _("[-bir] [-g|-p|-u] [-hnNv] [-f file] [id|name]...");
459 quota_cmd
.oneline
= _("show usage and limits");
460 quota_cmd
.help
= quota_help
;
461 quota_cmd
.flags
= CMD_FLAG_FOREIGN_OK
;
463 add_command("a_cmd
);