]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - quota/edit.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2005 Silicon Graphics, Inc.
15 static cmdinfo_t limit_cmd
;
16 static cmdinfo_t restore_cmd
;
17 static cmdinfo_t timer_cmd
;
18 static cmdinfo_t warn_cmd
;
25 " modify quota limits for the specified user\n"
28 " 'limit bsoft=100m bhard=110m tanya\n"
30 " Changes the soft and/or hard block limits, inode limits and/or realtime\n"
31 " block limits that are currently being used for the specified user, group,\n"
32 " or project. The filesystem identified by the current path is modified.\n"
33 " -d -- set the default values, used the first time a file is created\n"
34 " -g -- modify group quota limits\n"
35 " -p -- modify project quota limits\n"
36 " -u -- modify user quota limits\n"
37 " The block limit values can be specified with a units suffix - accepted\n"
38 " units are: k (kilobytes), m (megabytes), g (gigabytes), and t (terabytes).\n"
39 " The user/group/project can be specified either by name or by number.\n"
48 " modify quota enforcement timeout for the current filesystem\n"
52 " (soft inode limit timer is changed to 3 days)\n"
54 " Changes the timeout value associated with the block limits, inode limits\n"
55 " and/or realtime block limits for all users, groups, or projects on the\n"
56 " current filesystem.\n"
57 " As soon as a user consumes the amount of space or number of inodes set as\n"
58 " the soft limit, a timer is started. If the timer expires and the user is\n"
59 " still over the soft limit, the soft limit is enforced as the hard limit.\n"
60 " The default timeout is 7 days.\n"
61 " -d -- set the default values, used the first time a file is created\n"
62 " -g -- modify group quota timer\n"
63 " -p -- modify project quota timer\n"
64 " -u -- modify user quota timer\n"
65 " -b -- modify the blocks-used timer\n"
66 " -i -- modify the inodes-used timer\n"
67 " -r -- modify the blocks-used timer for the (optional) realtime subvolume\n"
68 " The timeout value is specified as a number of seconds, by default.\n"
69 " However, a suffix may be used to alternatively specify minutes (m),\n"
70 " hours (h), days (d), or weeks (w) - either the full word or the first\n"
71 " letter of the word can be used.\n"
80 " modify the number of quota warnings sent to the specified user\n"
84 " (tell the quota system that two warnings have been sent to user jimmy)\n"
86 " Changes the warning count associated with the block limits, inode limits\n"
87 " and/or realtime block limits for the specified user, group, or project.\n"
88 " When a user has been warned the maximum number of times allowed, the soft\n"
89 " limit is enforced as the hard limit. It is intended as an alternative to\n"
90 " the timeout system, where the system administrator updates a count of the\n"
91 " number of warnings issued to people, and they are penalised if the warnings\n"
93 " -d -- set maximum warning count, which triggers soft limit enforcement\n"
94 " -g -- set group quota warning count\n"
95 " -p -- set project quota warning count\n"
96 " -u -- set user quota warning count\n"
97 " -b -- set the blocks-used warning count\n"
98 " -i -- set the inodes-used warning count\n"
99 " -r -- set the blocks-used warn count for the (optional) realtime subvolume\n"
100 " The user/group/project can be specified either by name or by number.\n"
119 memset(&d
, 0, sizeof(d
));
120 d
.d_version
= FS_DQUOT_VERSION
;
123 d
.d_fieldmask
= mask
;
124 d
.d_blk_hardlimit
= *bhard
;
125 d
.d_blk_softlimit
= *bsoft
;
126 d
.d_ino_hardlimit
= *ihard
;
127 d
.d_ino_softlimit
= *isoft
;
128 d
.d_rtb_hardlimit
= *rtbhard
;
129 d
.d_rtb_softlimit
= *rtbsoft
;
131 if (xfsquotactl(XFS_SETQLIM
, dev
, type
, id
, (void *)&d
) < 0) {
133 fprintf(stderr
, _("%s: cannot set limits: %s\n"),
134 progname
, strerror(errno
));
150 uid_t uid
= uid_from_string(name
);
154 fprintf(stderr
, _("%s: invalid user name: %s\n"),
157 set_limits(uid
, type
, mask
, fs_path
->fs_name
,
158 bsoft
, bhard
, isoft
, ihard
, rtbsoft
, rtbhard
);
173 gid_t gid
= gid_from_string(name
);
177 fprintf(stderr
, _("%s: invalid group name: %s\n"),
180 set_limits(gid
, type
, mask
, fs_path
->fs_name
,
181 bsoft
, bhard
, isoft
, ihard
, rtbsoft
, rtbhard
);
196 prid_t prid
= prid_from_string(name
);
200 fprintf(stderr
, _("%s: invalid project name: %s\n"),
203 set_limits(prid
, type
, mask
, fs_path
->fs_name
,
204 bsoft
, bhard
, isoft
, ihard
, rtbsoft
, rtbhard
);
207 /* extract number of blocks from an ascii string */
220 if (strncmp(string
, prefix
, length
) == 0) {
221 s
= string
+ length
+ 1;
222 v
= cvtnum(blocksize
, sectorsize
, s
);
225 _("%s: Error: could not parse size %s.\n"),
229 *value
= (uint64_t)v
>> 9; /* syscalls use basic blocks */
230 if (v
> 0 && *value
== 0)
231 fprintf(stderr
, _("%s: Warning: `%s' in quota blocks is 0 (unlimited).\n"), progname
, s
);
237 /* extract number of inodes from an ascii string */
245 char *sp
, *s
= string
;
247 if (strncmp(string
, prefix
, length
) == 0) {
248 s
= string
+ length
+ 1;
249 *value
= strtoll(s
, &sp
, 0);
261 uint64_t bsoft
, bhard
, isoft
, ihard
, rtbsoft
, rtbhard
;
262 int c
, type
= 0, mask
= 0, flags
= 0;
263 uint bsize
, ssize
, endoptions
;
265 init_cvtnum(&bsize
, &ssize
);
266 bsoft
= bhard
= isoft
= ihard
= rtbsoft
= rtbhard
= 0;
267 while ((c
= getopt(argc
, argv
, "dgpu")) != EOF
) {
270 flags
|= DEFAULTS_FLAG
;
273 type
|= XFS_GROUP_QUOTA
;
276 type
|= XFS_PROJ_QUOTA
;
279 type
|= XFS_USER_QUOTA
;
282 return command_usage(&limit_cmd
);
287 * In the usual case, we need at least 2 more arguments -
288 * one (or more) limits and a user name/id.
289 * For setting defaults (-d) we don't want a user name/id.
291 if (flags
& DEFAULTS_FLAG
) {
292 if (argc
< optind
+ 1)
293 return command_usage(&limit_cmd
);
295 } else if (argc
< optind
+ 2) {
296 return command_usage(&limit_cmd
);
302 * Extract limit values from remaining optional arguments.
304 while (argc
> optind
+ endoptions
- 1) {
305 char *s
= argv
[optind
++];
306 if (extractb(s
, "bsoft=", 5, bsize
, ssize
, &bsoft
))
308 else if (extractb(s
, "bhard=", 5, bsize
, ssize
, &bhard
))
310 else if (extracti(s
, "isoft=", 5, &isoft
))
312 else if (extracti(s
, "ihard=", 5, &ihard
))
314 else if (extractb(s
, "rtbsoft=", 7, bsize
, ssize
, &rtbsoft
))
315 mask
|= FS_DQ_RTBSOFT
;
316 else if (extractb(s
, "rtbhard=", 7, bsize
, ssize
, &rtbhard
))
317 mask
|= FS_DQ_RTBHARD
;
320 fprintf(stderr
, _("%s: unrecognised argument %s\n"),
327 fprintf(stderr
, _("%s: cannot find any valid arguments\n"),
332 name
= (flags
& DEFAULTS_FLAG
) ? "0" : argv
[optind
++];
335 type
= XFS_USER_QUOTA
;
336 } else if (type
!= XFS_GROUP_QUOTA
&&
337 type
!= XFS_PROJ_QUOTA
&&
338 type
!= XFS_USER_QUOTA
) {
339 return command_usage(&limit_cmd
);
344 set_user_limits(name
, type
, mask
,
345 &bsoft
, &bhard
, &isoft
, &ihard
, &rtbsoft
, &rtbhard
);
347 case XFS_GROUP_QUOTA
:
348 set_group_limits(name
, type
, mask
,
349 &bsoft
, &bhard
, &isoft
, &ihard
, &rtbsoft
, &rtbhard
);
352 set_project_limits(name
, type
, mask
,
353 &bsoft
, &bhard
, &isoft
, &ihard
, &rtbsoft
, &rtbhard
);
360 * Iterate through input file, restoring the limits.
361 * File format is as follows:
363 * <numeric id> bsoft bhard isoft ihard [rtbsoft rtbhard]
375 uint64_t bsoft
, bhard
, isoft
, ihard
, rtbsoft
, rtbhard
;
377 while (fgets(buffer
, sizeof(buffer
), fp
) != NULL
) {
378 if (strncmp("fs = ", buffer
, 5) == 0) {
380 * Copy the device name to dev, strip off the trailing
381 * newline, and move on to the next line.
383 strncpy(dev
, buffer
+ 5, sizeof(dev
) - 1);
384 dev
[strlen(dev
) - 1] = '\0';
387 rtbsoft
= rtbhard
= 0;
388 cnt
= sscanf(buffer
, "%u %llu %llu %llu %llu %llu %llu\n",
390 (unsigned long long *)&bsoft
,
391 (unsigned long long *)&bhard
,
392 (unsigned long long *)&isoft
,
393 (unsigned long long *)&ihard
,
394 (unsigned long long *)&rtbsoft
,
395 (unsigned long long *)&rtbhard
);
396 if (cnt
== 5 || cnt
== 7) {
397 mask
= FS_DQ_ISOFT
|FS_DQ_IHARD
|FS_DQ_BSOFT
|FS_DQ_BHARD
;
399 mask
|= FS_DQ_RTBSOFT
|FS_DQ_RTBHARD
;
400 set_limits(id
, type
, mask
, dev
, &bsoft
, &bhard
,
401 &isoft
, &ihard
, &rtbsoft
, &rtbhard
);
415 while ((c
= getopt(argc
, argv
, "f:gpu")) != EOF
) {
421 type
|= XFS_GROUP_QUOTA
;
424 type
|= XFS_PROJ_QUOTA
;
427 type
|= XFS_USER_QUOTA
;
430 return command_usage(&restore_cmd
);
435 return command_usage(&restore_cmd
);
438 type
= XFS_USER_QUOTA
;
439 } else if (type
!= XFS_GROUP_QUOTA
&&
440 type
!= XFS_PROJ_QUOTA
&&
441 type
!= XFS_USER_QUOTA
) {
442 return command_usage(&restore_cmd
);
446 if ((fp
= fopen(fname
, "r")) == NULL
) {
448 fprintf(stderr
, _("%s: fopen on %s failed: %s\n"),
449 progname
, fname
, strerror(errno
));
454 restore_file(fp
, type
);
470 memset(&d
, 0, sizeof(d
));
471 d
.d_version
= FS_DQUOT_VERSION
;
473 d
.d_fieldmask
= mask
;
476 d
.d_rtbtimer
= value
;
478 if (xfsquotactl(XFS_SETQLIM
, dev
, type
, 0, (void *)&d
) < 0) {
480 fprintf(stderr
, _("%s: cannot set timer: %s\n"),
481 progname
, strerror(errno
));
491 int c
, type
= 0, mask
= 0;
493 while ((c
= getopt(argc
, argv
, "bgipru")) != EOF
) {
496 mask
|= FS_DQ_BTIMER
;
499 mask
|= FS_DQ_ITIMER
;
502 mask
|= FS_DQ_RTBTIMER
;
505 type
|= XFS_GROUP_QUOTA
;
508 type
|= XFS_PROJ_QUOTA
;
511 type
|= XFS_USER_QUOTA
;
514 return command_usage(&timer_cmd
);
518 if (argc
!= optind
+ 1)
519 return command_usage(&timer_cmd
);
521 value
= cvttime(argv
[optind
++]);
524 mask
= FS_DQ_TIMER_MASK
;
527 type
= XFS_USER_QUOTA
;
528 } else if (type
!= XFS_GROUP_QUOTA
&&
529 type
!= XFS_PROJ_QUOTA
&&
530 type
!= XFS_USER_QUOTA
) {
531 return command_usage(&timer_cmd
);
534 set_timer(type
, mask
, fs_path
->fs_name
, value
);
548 memset(&d
, 0, sizeof(d
));
549 d
.d_version
= FS_DQUOT_VERSION
;
552 d
.d_fieldmask
= mask
;
555 d
.d_rtbwarns
= value
;
557 if (xfsquotactl(XFS_SETQLIM
, dev
, type
, id
, (void *)&d
) < 0) {
559 fprintf(stderr
, _("%s: cannot set warnings: %s\n"),
560 progname
, strerror(errno
));
571 uid_t uid
= uid_from_string(name
);
575 fprintf(stderr
, _("%s: invalid user name: %s\n"),
578 set_warnings(uid
, type
, mask
, fs_path
->fs_name
, value
);
588 gid_t gid
= gid_from_string(name
);
592 fprintf(stderr
, _("%s: invalid group name: %s\n"),
595 set_warnings(gid
, type
, mask
, fs_path
->fs_name
, value
);
599 set_project_warnings(
605 prid_t prid
= prid_from_string(name
);
609 fprintf(stderr
, _("%s: invalid project name: %s\n"),
612 set_warnings(prid
, type
, mask
, fs_path
->fs_name
, value
);
622 int c
, flags
= 0, type
= 0, mask
= 0;
624 while ((c
= getopt(argc
, argv
, "bdgipru")) != EOF
) {
627 flags
|= DEFAULTS_FLAG
;
630 mask
|= FS_DQ_BWARNS
;
633 mask
|= FS_DQ_IWARNS
;
636 mask
|= FS_DQ_RTBWARNS
;
639 type
|= XFS_GROUP_QUOTA
;
642 type
|= XFS_PROJ_QUOTA
;
645 type
|= XFS_USER_QUOTA
;
648 return command_usage(&warn_cmd
);
653 * In the usual case, we need at least 2 more arguments -
654 * one (or more) value and a user name/id.
655 * For setting defaults (-d) we don't want a user name/id.
657 if (flags
& DEFAULTS_FLAG
) {
658 if (argc
!= optind
+ 1)
659 return command_usage(&warn_cmd
);
660 } else if (argc
!= optind
+ 2) {
661 return command_usage(&warn_cmd
);
664 value
= atoi(argv
[optind
++]);
665 name
= (flags
& DEFAULTS_FLAG
) ? "0" : argv
[optind
++];
668 mask
= FS_DQ_WARNS_MASK
;
671 type
= XFS_USER_QUOTA
;
672 } else if (type
!= XFS_GROUP_QUOTA
&&
673 type
!= XFS_PROJ_QUOTA
&&
674 type
!= XFS_USER_QUOTA
) {
675 return command_usage(&warn_cmd
);
680 set_user_warnings(name
, type
, mask
, value
);
682 case XFS_GROUP_QUOTA
:
683 set_group_warnings(name
, type
, mask
, value
);
686 set_project_warnings(name
, type
, mask
, value
);
695 limit_cmd
.name
= "limit";
696 limit_cmd
.cfunc
= limit_f
;
697 limit_cmd
.argmin
= 2;
698 limit_cmd
.argmax
= -1;
700 _("[-g|-p|-u] bsoft|bhard|isoft|ihard|rtbsoft|rtbhard=N -d|id|name");
701 limit_cmd
.oneline
= _("modify quota limits");
702 limit_cmd
.help
= limit_help
;
703 limit_cmd
.flags
= CMD_FLAG_FOREIGN_OK
;
705 restore_cmd
.name
= "restore";
706 restore_cmd
.cfunc
= restore_f
;
707 restore_cmd
.argmin
= 0;
708 restore_cmd
.argmax
= -1;
709 restore_cmd
.args
= _("[-g|-p|-u] [-f file]");
710 restore_cmd
.oneline
= _("restore quota limits from a backup file");
711 restore_cmd
.flags
= CMD_FLAG_FOREIGN_OK
;
713 timer_cmd
.name
= "timer";
714 timer_cmd
.cfunc
= timer_f
;
715 timer_cmd
.argmin
= 2;
716 timer_cmd
.argmax
= -1;
717 timer_cmd
.args
= _("[-bir] [-g|-p|-u] value");
718 timer_cmd
.oneline
= _("set quota enforcement timeouts");
719 timer_cmd
.help
= timer_help
;
720 timer_cmd
.flags
= CMD_FLAG_FOREIGN_OK
;
722 warn_cmd
.name
= "warn";
723 warn_cmd
.cfunc
= warn_f
;
725 warn_cmd
.argmax
= -1;
726 warn_cmd
.args
= _("[-bir] [-g|-p|-u] value -d|id|name");
727 warn_cmd
.oneline
= _("get/set enforcement warning counter");
728 warn_cmd
.help
= warn_help
;
731 add_command(&limit_cmd
);
732 add_command(&restore_cmd
);
733 add_command(&timer_cmd
);
734 add_command(&warn_cmd
);