2 * quota.c --- debugfs quota commands
4 * Copyright (C) 2014 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
18 #include <sys/types.h>
28 const char *quota_type[] = { "user", "group", NULL };
30 static int load_quota_ctx(char *progname)
34 if (check_fs_open(progname))
37 if (!EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
38 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
39 com_err(progname, 0, "quota feature not eanbled");
46 retval = quota_init_context(¤t_qctx, current_fs, -1);
48 com_err(current_fs->device_name, retval,
49 "while trying to load quota information");
55 static int parse_quota_type(const char *cmdname, const char *str)
62 for (i = 0; i < MAXQUOTAS; i++) {
63 if (strcasecmp(str, quota_type[i]) == 0)
67 i = strtol(str, &t, 0);
71 if (i < 0 || i >= MAXQUOTAS) {
72 com_err(0, 0, "Invalid quota type: %s", str);
73 printf("Valid quota types are: ");
74 for (i = 0; i < MAXQUOTAS; i++)
75 printf("%s ", quota_type[i]);
80 if (current_fs->flags & EXT2_FLAG_RW)
81 flags |= EXT2_FILE_WRITE;
83 retval = quota_file_open(current_qctx, NULL, 0, i, -1, flags);
85 com_err(cmdname, retval,
86 "while opening quota inode (type %d)", i);
93 static int list_quota_callback(struct dquot *dq, void *cb_data)
95 printf("%8u %8lld %8lld %8lld %8lld %8lld %8lld\n",
96 dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
97 (long long)dq->dq_dqb.dqb_bsoftlimit,
98 (long long)dq->dq_dqb.dqb_bhardlimit,
99 (long long)dq->dq_dqb.dqb_curinodes,
100 (long long)dq->dq_dqb.dqb_isoftlimit,
101 (long long)dq->dq_dqb.dqb_ihardlimit);
105 void do_list_quota(int argc, char *argv[])
109 struct quota_handle *qh;
111 if (load_quota_ctx(argv[0]))
115 com_err(0, 0, "Usage: list_quota <quota_type>\n");
119 type = parse_quota_type(argv[0], argv[1]);
123 printf("%8s %8s %8s %8s %8s %8s %8s\n",
124 (type == 0) ? "user id" : "group id",
125 "blocks", "quota", "limit", "inodes", "quota", "limit");
126 qh = current_qctx->quota_file[type];
127 retval = qh->qh_ops->scan_dquots(qh, list_quota_callback, NULL);
129 com_err(argv[0], retval, "while scanning dquots");
134 void do_get_quota(int argc, char *argv[])
137 struct quota_handle *qh;
141 if (load_quota_ctx(argv[0]))
145 com_err(0, 0, "Usage: get_quota <quota_type> <id>\n");
149 type = parse_quota_type(argv[0], argv[1]);
153 id = parse_ulong(argv[2], argv[0], "id", &err);
157 printf("%8s %8s %8s %8s %8s %8s %8s\n",
158 (type == 0) ? "user id" : "group id",
159 "blocks", "quota", "limit", "inodes", "quota", "limit");
161 qh = current_qctx->quota_file[type];
163 dq = qh->qh_ops->read_dquot(qh, id);
165 list_quota_callback(dq, NULL);
166 ext2fs_free_mem(&dq);
168 com_err(argv[0], 0, "couldn't read quota record");