2 * Copyright (c) 2011, 2012 François Tigeot <ftigeot@wolfpond.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/mount.h>
35 #include <sys/vfs_quota.h>
42 #include <libprop/proplib.h>
47 #include <sys/types.h>
52 static bool flag_debug = 0;
53 static bool flag_humanize = 0;
54 static bool flag_resolve_ids = 1;
56 static void usage(int);
57 static int get_dirsize(char *);
58 static int get_fslist(void);
63 fprintf(stderr, "usage: vquota [-Dhn] check directory\n");
64 fprintf(stderr, " vquota [-Dhn] lsfs\n");
65 fprintf(stderr, " vquota [-Dhn] limit mount_point size\n");
66 fprintf(stderr, " vquota [-Dhn] ulim mount_point user size\n");
67 fprintf(stderr, " vquota [-Dhn] show mount_point\n");
68 fprintf(stderr, " vquota [-Dhn] sync mount_point\n");
73 * Inode numbers with more than one hard link often come in groups;
74 * use linear arrays of 1024 ones as the basic unit of allocation.
75 * We only need to check if the inodes have been previously processed,
76 * bit arrays are perfect for that purpose.
78 #define HL_CHUNK_BITS 10
79 #define HL_CHUNK_ENTRIES (1<<HL_CHUNK_BITS)
80 #define HL_CHUNK_MASK (HL_CHUNK_ENTRIES - 1)
81 #define BA_UINT64_BITS 6
82 #define BA_UINT64_ENTRIES (1<<BA_UINT64_BITS)
83 #define BA_UINT64_MASK (BA_UINT64_ENTRIES - 1)
86 RB_ENTRY(hl_node) rb_entry;
88 uint64_t hl_chunk[HL_CHUNK_ENTRIES/64];
91 RB_HEAD(hl_tree,hl_node) hl_root;
93 RB_PROTOTYPE(hl_tree, hl_node, rb_entry, rb_hl_node_cmp);
96 rb_hl_node_cmp(struct hl_node *a, struct hl_node *b);
98 RB_GENERATE(hl_tree, hl_node, rb_entry, rb_hl_node_cmp);
100 struct hl_node* hl_node_insert(ino_t);
104 rb_hl_node_cmp(struct hl_node *a, struct hl_node *b)
106 if (a->ino_left_bits < b->ino_left_bits)
108 else if (a->ino_left_bits > b->ino_left_bits)
113 struct hl_node* hl_node_insert(ino_t inode)
115 struct hl_node *hlp, *res;
117 hlp = malloc(sizeof(struct hl_node));
119 /* shouldn't happen */
120 printf("hl_node_insert(): malloc failed\n");
123 bzero(hlp, sizeof(struct hl_node));
125 hlp->ino_left_bits = (inode >> HL_CHUNK_BITS);
126 res = RB_INSERT(hl_tree, &hl_root, hlp);
128 if (res != NULL) /* shouldn't happen */
129 printf("hl_node_insert(): RB_INSERT didn't return NULL\n");
135 * hl_register: register an inode number in a rb-tree of bit arrays
137 * - true if the inode was already processed
141 hl_register(ino_t inode)
143 struct hl_node hl_find, *hlp;
144 uint64_t ino_right_bits, ba_index, ba_offset;
145 uint64_t bitmask, bitval;
148 /* calculate the different addresses of the wanted bit */
149 hl_find.ino_left_bits = (inode >> HL_CHUNK_BITS);
151 ino_right_bits = inode & HL_CHUNK_MASK;
152 ba_index = ino_right_bits >> BA_UINT64_BITS;
153 ba_offset = ino_right_bits & BA_UINT64_MASK;
155 /* no existing node? create and initialize it */
156 if ((hlp = RB_FIND(hl_tree, &hl_root, &hl_find)) == NULL) {
157 hlp = hl_node_insert(inode);
160 /* node was found, check the bit value */
161 bitmask = 1 << ba_offset;
162 bitval = hlp->hl_chunk[ba_index] & bitmask;
168 hlp->hl_chunk[ba_index] |= bitmask;
173 /* global variable used by get_dir_size() */
174 uint64_t global_size;
176 /* storage for collected id numbers */
177 /* FIXME: same data structures used in kernel, should find a way to
178 * deduplicate this code */
181 rb_ac_unode_cmp(struct ac_unode*, struct ac_unode*);
183 rb_ac_gnode_cmp(struct ac_gnode*, struct ac_gnode*);
185 RB_HEAD(ac_utree,ac_unode) ac_uroot;
186 RB_HEAD(ac_gtree,ac_gnode) ac_groot;
187 RB_PROTOTYPE(ac_utree, ac_unode, rb_entry, rb_ac_unode_cmp);
188 RB_PROTOTYPE(ac_gtree, ac_gnode, rb_entry, rb_ac_gnode_cmp);
189 RB_GENERATE(ac_utree, ac_unode, rb_entry, rb_ac_unode_cmp);
190 RB_GENERATE(ac_gtree, ac_gnode, rb_entry, rb_ac_gnode_cmp);
193 rb_ac_unode_cmp(struct ac_unode *a, struct ac_unode *b)
195 if (a->left_bits < b->left_bits)
197 else if (a->left_bits > b->left_bits)
203 rb_ac_gnode_cmp(struct ac_gnode *a, struct ac_gnode *b)
205 if (a->left_bits < b->left_bits)
207 else if (a->left_bits > b->left_bits)
212 static struct ac_unode*
213 unode_insert(uid_t uid)
215 struct ac_unode *unp, *res;
217 unp = malloc(sizeof(struct ac_unode));
219 printf("unode_insert(): malloc failed\n");
222 bzero(unp, sizeof(struct ac_unode));
224 unp->left_bits = (uid >> ACCT_CHUNK_BITS);
225 res = RB_INSERT(ac_utree, &ac_uroot, unp);
227 if (res != NULL) /* shouldn't happen */
228 printf("unode_insert(): RB_INSERT didn't return NULL\n");
233 static struct ac_gnode*
234 gnode_insert(gid_t gid)
236 struct ac_gnode *gnp, *res;
238 gnp = malloc(sizeof(struct ac_gnode));
240 printf("gnode_insert(): malloc failed\n");
243 bzero(gnp, sizeof(struct ac_gnode));
245 gnp->left_bits = (gid >> ACCT_CHUNK_BITS);
246 res = RB_INSERT(ac_gtree, &ac_groot, gnp);
248 if (res != NULL) /* shouldn't happen */
249 printf("gnode_insert(): RB_INSERT didn't return NULL\n");
255 * get_dirsize(): walks a directory tree in the same filesystem
257 * - global rb-trees ac_uroot and ac_groot
258 * - global variable global_size
261 get_dirsize(char* dirname)
274 struct ac_unode *unp, ufind;
275 struct ac_gnode *gnp, gfind;
277 /* TODO: check directory name sanity */
278 fts_args[0] = dirname;
281 if ((fts = fts_open(fts_args, FTS_PHYSICAL|FTS_XDEV, NULL)) == NULL)
282 err(1, "fts_open() failed");
284 while ((p = fts_read(fts)) != NULL) {
285 switch (p->fts_info) {
286 /* directories, ignore them */
291 /* read errors, warn, continue and flag */
295 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
299 file_inode = p->fts_statp->st_ino;
300 file_size = p->fts_statp->st_size;
301 file_uid = p->fts_statp->st_uid;
302 file_gid = p->fts_statp->st_gid;
304 /* files with more than one hard link: */
305 /* process them only once */
306 if (p->fts_statp->st_nlink > 1)
307 if (hl_register(file_inode))
310 global_size += file_size;
311 ufind.left_bits = (file_uid >> ACCT_CHUNK_BITS);
312 gfind.left_bits = (file_gid >> ACCT_CHUNK_BITS);
313 if ((unp = RB_FIND(ac_utree, &ac_uroot, &ufind)) == NULL)
314 unp = unode_insert(file_uid);
315 if ((gnp = RB_FIND(ac_gtree, &ac_groot, &gfind)) == NULL)
316 gnp = gnode_insert(file_gid);
317 unp->uid_chunk[(file_uid & ACCT_CHUNK_MASK)].space += file_size;
318 gnp->gid_chunk[(file_gid & ACCT_CHUNK_MASK)].space += file_size;
327 print_user(uid_t uid)
331 if (flag_resolve_ids && ((pw = getpwuid(uid)) != NULL)) {
332 printf("user %s:", pw->pw_name);
334 printf("uid %u:", uid);
339 print_group(gid_t gid)
343 if (flag_resolve_ids && ((gr = getgrgid(gid)) != NULL)) {
344 printf("group %s:", gr->gr_name);
346 printf("gid %u:", gid);
351 cmd_check(char* dirname)
355 struct ac_unode *unp;
356 struct ac_gnode *gnp;
359 rv = get_dirsize(dirname);
362 humanize_number(hbuf, sizeof(hbuf), global_size, "",
363 HN_AUTOSCALE, HN_NOSPACE);
364 printf("total: %s\n", hbuf);
366 printf("total: %"PRIu64"\n", global_size);
368 RB_FOREACH(unp, ac_utree, &ac_uroot) {
369 for (i=0; i<ACCT_CHUNK_NIDS; i++) {
370 if (unp->uid_chunk[i].space != 0) {
371 uid = (unp->left_bits << ACCT_CHUNK_BITS) + i;
374 humanize_number(hbuf, sizeof(hbuf),
375 unp->uid_chunk[i].space, "", HN_AUTOSCALE, HN_NOSPACE);
376 printf(" %s\n", hbuf);
378 printf(" %" PRIu64 "\n", unp->uid_chunk[i].space);
383 RB_FOREACH(gnp, ac_gtree, &ac_groot) {
384 for (i=0; i<ACCT_CHUNK_NIDS; i++) {
385 if (gnp->gid_chunk[i].space != 0) {
386 gid = (gnp->left_bits << ACCT_CHUNK_BITS) + i;
389 humanize_number(hbuf, sizeof(hbuf),
390 gnp->gid_chunk[i].space, "", HN_AUTOSCALE, HN_NOSPACE);
391 printf(" %s\n", hbuf);
393 printf(" %" PRIu64 "\n", gnp->gid_chunk[i].space);
402 /* print a list of filesystems with accounting enabled */
406 struct statfs *mntbufp;
409 /* read mount table from kernel */
410 nloc = getmntinfo(&mntbufp, MNT_NOWAIT|MNT_LOCAL);
412 perror("getmntinfo");
416 /* iterate mounted filesystems */
417 for (i=0; i<nloc; i++) {
418 /* vfs accounting enabled on this one ? */
419 if (mntbufp[i].f_flags & MNT_ACCOUNTING)
420 printf("%s on %s\n", mntbufp[i].f_mntfromname,
421 mntbufp[i].f_mntonname);
428 send_command(const char *path, const char *cmd,
429 prop_object_t args, prop_dictionary_t *res)
431 prop_dictionary_t dict;
432 struct plistref pref;
437 dict = prop_dictionary_create();
440 printf("send_command(): couldn't create dictionary\n");
444 rv = prop_dictionary_set_cstring(dict, "command", cmd);
446 printf("send_command(): couldn't initialize dictionary\n");
450 rv = prop_dictionary_set(dict, "arguments", args);
452 printf("prop_dictionary_set() failed\n");
456 error = prop_dictionary_send_syscall(dict, &pref);
458 printf("prop_dictionary_send_syscall() failed\n");
459 prop_object_release(dict);
464 printf("Message to kernel:\n%s\n",
465 prop_dictionary_externalize(dict));
467 error = vquotactl(path, &pref);
469 printf("send_command: vquotactl = %d\n", error);
473 error = prop_dictionary_recv_syscall(&pref, res);
475 printf("prop_dictionary_recv_syscall() failed\n");
479 printf("Message from kernel:\n%s\n",
480 prop_dictionary_externalize(*res));
485 /* show collected statistics on mount point */
489 prop_dictionary_t args, res;
490 prop_array_t reslist;
492 prop_object_iterator_t iter;
493 prop_dictionary_t item;
495 uint64_t space, limit=0;
498 args = prop_dictionary_create();
499 res = prop_dictionary_create();
501 printf("show_mp(): couldn't create args dictionary\n");
502 res = prop_dictionary_create();
504 printf("show_mp(): couldn't create res dictionary\n");
506 rv = send_command(path, "get usage all", args, &res);
508 printf("show-mp(): failed to send message to kernel\n");
512 reslist = prop_dictionary_get(res, "returned data");
513 if (reslist == NULL) {
514 printf("show_mp(): failed to get array of results");
519 iter = prop_array_iterator(reslist);
521 printf("show_mp(): failed to create iterator\n");
526 while ((item = prop_object_iterator_next(iter)) != NULL) {
527 rv = prop_dictionary_get_uint64(item, "space used", &space);
528 rv = prop_dictionary_get_uint64(item, "limit", &limit);
529 if (prop_dictionary_get_uint32(item, "uid", &id))
531 else if (prop_dictionary_get_uint32(item, "gid", &id))
536 humanize_number(hbuf, sizeof(hbuf), space, "", HN_AUTOSCALE, HN_NOSPACE);
539 printf(" %"PRIu64, space);
546 humanize_number(hbuf, sizeof(hbuf), limit, "", HN_AUTOSCALE, HN_NOSPACE);
547 printf(", limit = %s\n", hbuf);
549 printf(", limit = %"PRIu64"\n", limit);
552 prop_object_iterator_release(iter);
555 prop_object_release(args);
556 prop_object_release(res);
560 /* sync the in-kernel counters to the actual file system usage */
561 static int cmd_sync(char *dirname)
563 prop_dictionary_t res, item;
565 struct ac_unode *unp;
566 struct ac_gnode *gnp;
569 args = prop_array_create();
571 printf("cmd_sync(): couldn't create args dictionary\n");
572 res = prop_dictionary_create();
574 printf("cmd_sync(): couldn't create res dictionary\n");
576 rv = get_dirsize(dirname);
578 item = prop_dictionary_create();
580 printf("cmd_sync(): couldn't create item dictionary\n");
581 (void) prop_dictionary_set_uint64(item, "space used", global_size);
582 prop_array_add_and_rel(args, item);
584 RB_FOREACH(unp, ac_utree, &ac_uroot) {
585 for (i=0; i<ACCT_CHUNK_NIDS; i++) {
586 if (unp->uid_chunk[i].space != 0) {
587 item = prop_dictionary_create();
588 (void) prop_dictionary_set_uint32(item, "uid",
589 (unp->left_bits << ACCT_CHUNK_BITS) + i);
590 (void) prop_dictionary_set_uint64(item, "space used",
591 unp->uid_chunk[i].space);
592 prop_array_add_and_rel(args, item);
596 RB_FOREACH(gnp, ac_gtree, &ac_groot) {
597 for (i=0; i<ACCT_CHUNK_NIDS; i++) {
598 if (gnp->gid_chunk[i].space != 0) {
599 item = prop_dictionary_create();
600 (void) prop_dictionary_set_uint32(item, "gid",
601 (gnp->left_bits << ACCT_CHUNK_BITS) + i);
602 (void) prop_dictionary_set_uint64(item, "space used",
603 gnp->gid_chunk[i].space);
604 prop_array_add_and_rel(args, item);
609 if (send_command(dirname, "set usage all", args, &res) == false) {
610 printf("Failed to send message to kernel\n");
614 prop_object_release(args);
615 prop_object_release(res);
621 cmd_limit(char *dirname, uint64_t limit)
623 prop_dictionary_t res, args;
626 args = prop_dictionary_create();
628 printf("cmd_limit(): couldn't create args dictionary\n");
629 res = prop_dictionary_create();
631 printf("cmd_limit(): couldn't create res dictionary\n");
633 (void) prop_dictionary_set_uint64(args, "limit", limit);
635 if (send_command(dirname, "set limit", args, &res) == false) {
636 printf("Failed to send message to kernel\n");
640 prop_object_release(args);
641 prop_object_release(res);
647 cmd_limit_uid(char *dirname, uid_t uid, uint64_t limit)
649 prop_dictionary_t res, args;
652 args = prop_dictionary_create();
654 printf("cmd_limit_uid(): couldn't create args dictionary\n");
655 res = prop_dictionary_create();
657 printf("cmd_limit_uid(): couldn't create res dictionary\n");
659 (void) prop_dictionary_set_uint32(args, "uid", uid);
660 (void) prop_dictionary_set_uint64(args, "limit", limit);
662 if (send_command(dirname, "set limit uid", args, &res) == false) {
663 printf("Failed to send message to kernel\n");
667 prop_object_release(args);
668 prop_object_release(res);
674 main(int argc, char **argv)
679 while ((ch = getopt(argc, argv, "Dhn")) != -1) {
688 flag_resolve_ids = 0;
697 if (strcmp(argv[0], "check") == 0) {
700 return cmd_check(argv[1]);
702 if (strcmp(argv[0], "lsfs") == 0) {
705 if (strcmp(argv[0], "limit") == 0) {
708 if (dehumanize_number(argv[2], &limit) < 0)
709 err(1, "bad number for option: %s", argv[2]);
711 return cmd_limit(argv[1], limit);
713 if (strcmp(argv[0], "show") == 0) {
716 return show_mp(argv[1]);
718 if (strcmp(argv[0], "sync") == 0) {
721 return cmd_sync(argv[1]);
723 if (strcmp(argv[0], "ulim") == 0) {
727 if ((pwd = getpwnam(argv[2])) == NULL)
728 errx(1, "%s: no such user", argv[2]);
729 if (dehumanize_number(argv[3], &limit) < 0)
730 err(1, "bad number for option: %s", argv[2]);
732 return cmd_limit_uid(argv[1], pwd->pw_uid, limit);