2 * Copyright (c) 2011 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>
48 static bool flag_debug = 0;
50 static void usage(int);
51 static int get_dirsize(char *);
52 static int get_fslist(void);
57 fprintf(stderr, "usage: vquota [-D] check directory\n");
58 fprintf(stderr, " vquota [-D] lsfs\n");
59 fprintf(stderr, " vquota [-D] show mount_point\n");
64 * Inode numbers with more than one hard link often come in groups;
65 * use linear arrays of 1024 ones as the basic unit of allocation.
66 * We only need to check if the inodes have been previously processed,
67 * bit arrays are perfect for that purpose.
69 #define HL_CHUNK_BITS 10
70 #define HL_CHUNK_ENTRIES (1<<HL_CHUNK_BITS)
71 #define HL_CHUNK_MASK (HL_CHUNK_ENTRIES - 1)
72 #define BA_UINT64_BITS 6
73 #define BA_UINT64_ENTRIES (1<<BA_UINT64_BITS)
74 #define BA_UINT64_MASK (BA_UINT64_ENTRIES - 1)
77 RB_ENTRY(hl_node) rb_entry;
79 uint64_t hl_chunk[HL_CHUNK_ENTRIES/64];
82 RB_HEAD(hl_tree,hl_node) hl_root;
84 RB_PROTOTYPE(hl_tree, hl_node, rb_entry, rb_hl_node_cmp);
87 rb_hl_node_cmp(struct hl_node *a, struct hl_node *b);
89 RB_GENERATE(hl_tree, hl_node, rb_entry, rb_hl_node_cmp);
91 struct hl_node* hl_node_insert(ino_t);
95 rb_hl_node_cmp(struct hl_node *a, struct hl_node *b)
97 if (a->ino_left_bits < b->ino_left_bits)
99 else if (a->ino_left_bits > b->ino_left_bits)
104 struct hl_node* hl_node_insert(ino_t inode)
106 struct hl_node *hlp, *res;
108 hlp = malloc(sizeof(struct hl_node));
110 /* shouldn't happen */
111 printf("hl_node_insert(): malloc failed\n");
114 bzero(hlp, sizeof(struct hl_node));
116 hlp->ino_left_bits = (inode >> HL_CHUNK_BITS);
117 res = RB_INSERT(hl_tree, &hl_root, hlp);
119 if (res != NULL) /* shouldn't happen */
120 printf("hl_node_insert(): RB_INSERT didn't return NULL\n");
126 * hl_register: register an inode number in a rb-tree of bit arrays
128 * - true if the inode was already processed
132 hl_register(ino_t inode)
134 struct hl_node hl_find, *hlp;
135 uint64_t ino_right_bits, ba_index, ba_offset;
136 uint64_t bitmask, bitval;
139 /* calculate the different addresses of the wanted bit */
140 hl_find.ino_left_bits = (inode >> HL_CHUNK_BITS);
142 ino_right_bits = inode & HL_CHUNK_MASK;
143 ba_index = ino_right_bits >> BA_UINT64_BITS;
144 ba_offset = ino_right_bits & BA_UINT64_MASK;
146 /* no existing node? create and initialize it */
147 if ((hlp = RB_FIND(hl_tree, &hl_root, &hl_find)) == NULL) {
148 hlp = hl_node_insert(inode);
151 /* node was found, check the bit value */
152 bitmask = 1 << ba_offset;
153 bitval = hlp->hl_chunk[ba_index] & bitmask;
159 hlp->hl_chunk[ba_index] |= bitmask;
164 /* storage for collected uid numbers */
165 /* FIXME: same data structures used in kernel, should find a way to
166 * deduplicate this code */
169 rb_ac_unode_cmp(struct ac_unode*, struct ac_unode*);
171 rb_ac_gnode_cmp(struct ac_gnode*, struct ac_gnode*);
173 RB_HEAD(ac_utree,ac_unode) ac_uroot;
174 RB_HEAD(ac_gtree,ac_gnode) ac_groot;
175 RB_PROTOTYPE(ac_utree, ac_unode, rb_entry, rb_ac_unode_cmp);
176 RB_PROTOTYPE(ac_gtree, ac_gnode, rb_entry, rb_ac_gnode_cmp);
177 RB_GENERATE(ac_utree, ac_unode, rb_entry, rb_ac_unode_cmp);
178 RB_GENERATE(ac_gtree, ac_gnode, rb_entry, rb_ac_gnode_cmp);
181 rb_ac_unode_cmp(struct ac_unode *a, struct ac_unode *b)
183 if (a->left_bits < b->left_bits)
185 else if (a->left_bits > b->left_bits)
191 rb_ac_gnode_cmp(struct ac_gnode *a, struct ac_gnode *b)
193 if (a->left_bits < b->left_bits)
195 else if (a->left_bits > b->left_bits)
200 static struct ac_unode*
201 unode_insert(uid_t uid)
203 struct ac_unode *unp, *res;
205 unp = malloc(sizeof(struct ac_unode));
207 printf("unode_insert(): malloc failed\n");
210 bzero(unp, sizeof(struct ac_unode));
212 unp->left_bits = (uid >> ACCT_CHUNK_BITS);
213 res = RB_INSERT(ac_utree, &ac_uroot, unp);
215 if (res != NULL) /* shouldn't happen */
216 printf("unode_insert(): RB_INSERT didn't return NULL\n");
221 static struct ac_gnode*
222 gnode_insert(gid_t gid)
224 struct ac_gnode *gnp, *res;
226 gnp = malloc(sizeof(struct ac_gnode));
228 printf("gnode_insert(): malloc failed\n");
231 bzero(gnp, sizeof(struct ac_gnode));
233 gnp->left_bits = (gid >> ACCT_CHUNK_BITS);
234 res = RB_INSERT(ac_gtree, &ac_groot, gnp);
236 if (res != NULL) /* shouldn't happen */
237 printf("gnode_insert(): RB_INSERT didn't return NULL\n");
243 get_dirsize(char* dirname)
248 uint64_t global_size = 0;
257 struct ac_unode *unp, ufind;
258 struct ac_gnode *gnp, gfind;
261 /* TODO: check directory name sanity */
262 fts_args[0] = dirname;
265 if ((fts = fts_open(fts_args, FTS_PHYSICAL|FTS_XDEV, NULL)) == NULL)
266 err(1, "fts_open() failed");
268 while ((p = fts_read(fts)) != NULL) {
269 switch (p->fts_info) {
270 /* directories, ignore them */
275 /* read errors, warn, continue and flag */
279 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
283 file_inode = p->fts_statp->st_ino;
284 file_size = p->fts_statp->st_size;
285 file_uid = p->fts_statp->st_uid;
286 file_gid = p->fts_statp->st_gid;
288 /* files with more than one hard link: */
289 /* process them only once */
290 if (p->fts_statp->st_nlink > 1)
291 if (hl_register(file_inode) == false)
294 global_size += file_size;
295 ufind.left_bits = (file_uid >> ACCT_CHUNK_BITS);
296 gfind.left_bits = (file_gid >> ACCT_CHUNK_BITS);
297 if ((unp = RB_FIND(ac_utree, &ac_uroot, &ufind)) == NULL)
298 unp = unode_insert(file_uid);
299 if ((gnp = RB_FIND(ac_gtree, &ac_groot, &gfind)) == NULL)
300 gnp = gnode_insert(file_gid);
301 unp->uid_chunk[(file_uid & ACCT_CHUNK_MASK)] += file_size;
302 gnp->gid_chunk[(file_gid & ACCT_CHUNK_MASK)] += file_size;
307 printf("total: %"PRIu64"\n", global_size);
308 RB_FOREACH(unp, ac_utree, &ac_uroot) {
309 for (i=0; i<ACCT_CHUNK_NIDS; i++) {
310 if (unp->uid_chunk[i] != 0) {
311 printf("uid %"PRIu32": %"PRIu64"\n",
312 (unp->left_bits << ACCT_CHUNK_BITS) + i, unp->uid_chunk[i]);
317 RB_FOREACH(gnp, ac_gtree, &ac_groot) {
318 for (i=0; i<ACCT_CHUNK_NIDS; i++) {
319 if (gnp->gid_chunk[i] != 0) {
320 printf("gid %"PRIu32": %"PRIu64"\n",
321 (gnp->left_bits << ACCT_CHUNK_BITS) + i, gnp->gid_chunk[i]);
329 /* print a list of filesystems with accounting enabled */
330 static int get_fslist(void) {
331 struct statfs *mntbufp;
334 /* read mount table from kernel */
335 nloc = getmntinfo(&mntbufp, MNT_NOWAIT|MNT_LOCAL);
337 perror("getmntinfo");
341 /* iterate mounted filesystems */
342 for (i=0; i<nloc; i++) {
343 /* vfs accounting enabled on this one ? */
344 if (mntbufp[i].f_flags & MNT_ACCOUNTING)
345 printf("%s on %s\n", mntbufp[i].f_mntfromname,
346 mntbufp[i].f_mntonname);
353 send_command(const char *path, const char *cmd,
354 prop_dictionary_t args, prop_dictionary_t *res) {
355 prop_dictionary_t dict;
356 struct plistref pref;
361 dict = prop_dictionary_create();
364 printf("send_command(): couldn't create dictionary\n");
368 rv = prop_dictionary_set_cstring(dict, "command", cmd);
370 printf("send_command(): couldn't initialize dictionary\n");
374 rv = prop_dictionary_set(dict, "arguments", args);
376 printf("prop_dictionary_set() failed\n");
380 error = prop_dictionary_send_syscall(dict, &pref);
382 printf("prop_dictionary_send_syscall() failed\n");
383 prop_object_release(dict);
388 printf("Message to kernel:\n%s\n", prop_dictionary_externalize(dict));
390 error = vquotactl(path, &pref);
392 printf("send_command: vquotactl = %d\n", error);
396 error = prop_dictionary_recv_syscall(&pref, res);
398 printf("prop_dictionary_recv_syscall() failed\n");
402 printf("Message from kernel:\n%s\n", prop_dictionary_externalize(*res));
407 /* show collected statistics on mount point */
408 static int show_mp(char *path) {
409 prop_dictionary_t args, res;
410 prop_array_t reslist;
412 prop_object_iterator_t iter;
413 prop_dictionary_t item;
417 args = prop_dictionary_create();
418 res = prop_dictionary_create();
420 printf("couldn't create args dictionary\n");
422 rv = send_command(path, "get usage all", args, &res);
424 printf("show-mp(): failed to send message to kernel\n");
428 reslist = prop_dictionary_get(res, "get usage all");
429 if (reslist == NULL) {
430 printf("show_mp(): failed to get array of results");
435 iter = prop_array_iterator(reslist);
437 printf("show_mp(): failed to create iterator\n");
442 while ((item = prop_object_iterator_next(iter)) != NULL) {
443 rv = prop_dictionary_get_uint64(item, "space used", &space);
444 if (prop_dictionary_get_uint32(item, "uid", &id))
445 printf("uid %u:", id);
446 else if (prop_dictionary_get_uint32(item, "gid", &id))
447 printf("gid %u:", id);
449 printf("total space used");
450 printf(" %" PRIu64 "\n", space);
452 prop_object_iterator_release(iter);
455 prop_object_release(args);
456 prop_object_release(res);
461 main(int argc, char **argv) {
464 while ((ch = getopt(argc, argv, "D")) != -1) {
476 if (strcmp(argv[0], "check") == 0) {
479 return get_dirsize(argv[1]);
481 if (strcmp(argv[0], "lsfs") == 0) {
484 if (strcmp(argv[0], "show") == 0) {
487 return show_mp(argv[1]);