vquota(8): finish implementing the check command
[dragonfly.git] / sbin / vquota / vquota.c
1 /*
2  * Copyright (c) 2011 François Tigeot <ftigeot@wolfpond.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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
14  *    distribution.
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.
18  *
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
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/stat.h>
34 #include <sys/mount.h>
35 #include <sys/vfs_quota.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <err.h>
40 #include <string.h>
41 #include <fts.h>
42 #include <libprop/proplib.h>
43 #include <unistd.h>
44 #include <sys/tree.h>
45 #include <errno.h>
46 #include <inttypes.h>
47
48 static bool flag_debug = 0;
49
50 static void usage(int);
51 static int get_dirsize(char *);
52 static int get_fslist(void);
53
54 static void
55 usage(int retcode)
56 {
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");
60         exit(retcode);
61 }
62
63 /*
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.
68  */
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)
75
76 struct hl_node {
77         RB_ENTRY(hl_node)       rb_entry;
78         ino_t                   ino_left_bits;
79         uint64_t                hl_chunk[HL_CHUNK_ENTRIES/64];
80 };
81
82 RB_HEAD(hl_tree,hl_node)        hl_root;
83
84 RB_PROTOTYPE(hl_tree, hl_node, rb_entry, rb_hl_node_cmp);
85
86 static int
87 rb_hl_node_cmp(struct hl_node *a, struct hl_node *b);
88
89 RB_GENERATE(hl_tree, hl_node, rb_entry, rb_hl_node_cmp);
90
91 struct hl_node* hl_node_insert(ino_t);
92
93
94 static int
95 rb_hl_node_cmp(struct hl_node *a, struct hl_node *b)
96 {
97         if (a->ino_left_bits < b->ino_left_bits)
98                 return(-1);
99         else if (a->ino_left_bits > b->ino_left_bits)
100                 return(1);
101         return(0);
102 }
103
104 struct hl_node* hl_node_insert(ino_t inode)
105 {
106         struct hl_node *hlp, *res;
107
108         hlp = malloc(sizeof(struct hl_node));
109         if (hlp == NULL) {
110                 /* shouldn't happen */
111                 printf("hl_node_insert(): malloc failed\n");
112                 exit(ENOMEM);
113         }
114         bzero(hlp, sizeof(struct hl_node));
115
116         hlp->ino_left_bits = (inode >> HL_CHUNK_BITS);
117         res = RB_INSERT(hl_tree, &hl_root, hlp);
118
119         if (res != NULL)        /* shouldn't happen */
120                 printf("hl_node_insert(): RB_INSERT didn't return NULL\n");
121
122         return hlp;
123 }
124
125 /*
126  * hl_register: register an inode number in a rb-tree of bit arrays
127  * returns:
128  * - true if the inode was already processed
129  * - false otherwise
130  */
131 static bool
132 hl_register(ino_t inode)
133 {
134         struct hl_node hl_find, *hlp;
135         uint64_t ino_right_bits, ba_index, ba_offset;
136         uint64_t bitmask, bitval;
137         bool retval = false;
138
139         /* calculate the different addresses of the wanted bit */
140         hl_find.ino_left_bits = (inode >> HL_CHUNK_BITS);
141
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;
145
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);
149         }
150
151         /* node was found, check the bit value */
152         bitmask = 1 << ba_offset;
153         bitval = hlp->hl_chunk[ba_index] & bitmask;
154         if (bitval != 0) {
155                 retval = true;
156         }
157
158         /* set the bit */
159         hlp->hl_chunk[ba_index] |= bitmask;
160
161         return retval;
162 }
163
164 /* storage for collected uid numbers */
165 /* FIXME: same data structures used in kernel, should find a way to
166  * deduplicate this code */
167
168 static int
169 rb_ac_unode_cmp(struct ac_unode*, struct ac_unode*);
170 static int
171 rb_ac_gnode_cmp(struct ac_gnode*, struct ac_gnode*);
172
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);
179
180 static int
181 rb_ac_unode_cmp(struct ac_unode *a, struct ac_unode *b)
182 {
183         if (a->left_bits < b->left_bits)
184                 return(-1);
185         else if (a->left_bits > b->left_bits)
186                 return(1);
187         return(0);
188 }
189
190 static int
191 rb_ac_gnode_cmp(struct ac_gnode *a, struct ac_gnode *b)
192 {
193         if (a->left_bits < b->left_bits)
194                 return(-1);
195         else if (a->left_bits > b->left_bits)
196                 return(1);
197         return(0);
198 }
199
200 static struct ac_unode*
201 unode_insert(uid_t uid)
202 {
203         struct ac_unode *unp, *res;
204
205         unp = malloc(sizeof(struct ac_unode));
206         if (unp == NULL) {
207                 printf("unode_insert(): malloc failed\n");
208                 exit (ENOMEM);
209         }
210         bzero(unp, sizeof(struct ac_unode));
211
212         unp->left_bits = (uid >> ACCT_CHUNK_BITS);
213         res = RB_INSERT(ac_utree, &ac_uroot, unp);
214
215         if (res != NULL)        /* shouldn't happen */
216                 printf("unode_insert(): RB_INSERT didn't return NULL\n");
217
218         return unp;
219 }
220
221 static struct ac_gnode*
222 gnode_insert(gid_t gid)
223 {
224         struct ac_gnode *gnp, *res;
225
226         gnp = malloc(sizeof(struct ac_gnode));
227         if (gnp == NULL) {
228                 printf("gnode_insert(): malloc failed\n");
229                 exit (ENOMEM);
230         }
231         bzero(gnp, sizeof(struct ac_gnode));
232
233         gnp->left_bits = (gid >> ACCT_CHUNK_BITS);
234         res = RB_INSERT(ac_gtree, &ac_groot, gnp);
235
236         if (res != NULL)        /* shouldn't happen */
237                 printf("gnode_insert(): RB_INSERT didn't return NULL\n");
238
239         return gnp;
240 }
241
242 static int
243 get_dirsize(char* dirname)
244 {
245         FTS             *fts;
246         FTSENT          *p;
247         char*           fts_args[2];
248         uint64_t        global_size = 0;
249         int             retval = 0;
250
251         /* what we need */
252         ino_t           file_inode;
253         off_t           file_size;
254         uid_t           file_uid;
255         gid_t           file_gid;
256
257         struct ac_unode *unp, ufind;
258         struct ac_gnode *gnp, gfind;
259         int             i;
260
261         /* TODO: check directory name sanity */
262         fts_args[0] = dirname;
263         fts_args[1] = NULL;
264
265         if ((fts = fts_open(fts_args, FTS_PHYSICAL|FTS_XDEV, NULL)) == NULL)
266                 err(1, "fts_open() failed");
267
268         while ((p = fts_read(fts)) != NULL) {
269                 switch (p->fts_info) {
270                 /* directories, ignore them */
271                 case FTS_D:
272                 case FTS_DC:
273                 case FTS_DP:
274                         break;
275                 /* read errors, warn, continue and flag */
276                 case FTS_DNR:
277                 case FTS_ERR:
278                 case FTS_NS:
279                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
280                         retval = 1;
281                         break;
282                 default:
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;
287
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)
292                                         break;
293
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;
303                 }
304         }
305         fts_close(fts);
306
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]);
313                         }
314
315                 }
316         }
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]);
322                         }
323                 }
324         }
325
326         return retval;
327 }
328
329 /* print a list of filesystems with accounting enabled */
330 static int get_fslist(void) {
331         struct statfs *mntbufp;
332         int nloc, i;
333
334         /* read mount table from kernel */
335         nloc = getmntinfo(&mntbufp, MNT_NOWAIT|MNT_LOCAL);
336         if (nloc <= 0) {
337                 perror("getmntinfo");
338                 exit(1);
339         }
340
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);
347         }
348
349         return 0;
350 }
351
352 static bool
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;
357
358         bool rv;
359         int error;
360
361         dict = prop_dictionary_create();
362
363         if (dict == NULL) {
364                 printf("send_command(): couldn't create dictionary\n");
365                 return false;
366         }
367
368         rv = prop_dictionary_set_cstring(dict, "command", cmd);
369         if (rv== false) {
370                 printf("send_command(): couldn't initialize dictionary\n");
371                 return false;
372         }
373
374         rv = prop_dictionary_set(dict, "arguments", args);
375         if (rv == false) {
376                 printf("prop_dictionary_set() failed\n");
377                 return false;
378         }
379
380         error = prop_dictionary_send_syscall(dict, &pref);
381         if (error != 0) {
382                 printf("prop_dictionary_send_syscall() failed\n");
383                 prop_object_release(dict);
384                 return false;
385         }
386
387         if (flag_debug)
388                 printf("Message to kernel:\n%s\n", prop_dictionary_externalize(dict));
389
390         error = vquotactl(path, &pref);
391         if (error != 0) {
392                 printf("send_command: vquotactl = %d\n", error);
393                 return false;
394         }
395
396         error = prop_dictionary_recv_syscall(&pref, res);
397         if (error != 0) {
398                 printf("prop_dictionary_recv_syscall() failed\n");
399         }
400
401         if (flag_debug)
402                 printf("Message from kernel:\n%s\n", prop_dictionary_externalize(*res));
403
404         return true;
405 }
406
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;
411         bool rv;
412         prop_object_iterator_t  iter;
413         prop_dictionary_t item;
414         uint32_t id;
415         uint64_t space;
416
417         args = prop_dictionary_create();
418         res  = prop_dictionary_create();
419         if (args == NULL)
420                 printf("couldn't create args dictionary\n");
421
422         rv = send_command(path, "get usage all", args, &res);
423         if (rv == false) {
424                 printf("show-mp(): failed to send message to kernel\n");
425                 goto end;
426         }
427
428         reslist = prop_dictionary_get(res, "get usage all");
429         if (reslist == NULL) {
430                 printf("show_mp(): failed to get array of results");
431                 rv = false;
432                 goto end;
433         }
434
435         iter = prop_array_iterator(reslist);
436         if (iter == NULL) {
437                 printf("show_mp(): failed to create iterator\n");
438                 rv = false;
439                 goto end;
440         }
441
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);
448                 else
449                         printf("total space used");
450                 printf(" %" PRIu64 "\n", space);
451         }
452         prop_object_iterator_release(iter);
453
454 end:
455         prop_object_release(args);
456         prop_object_release(res);
457         return (rv == true);
458 }
459
460 int
461 main(int argc, char **argv) {
462         int ch;
463
464         while ((ch = getopt(argc, argv, "D")) != -1) {
465                 switch(ch) {
466                 case 'D':
467                         flag_debug = 1;
468                         break;
469                 }
470         }
471         argc -= optind;
472         argv += optind;
473         if (argc < 1)
474                 usage(1);
475         
476         if (strcmp(argv[0], "check") == 0) {
477                 if (argc != 2)
478                         usage(1);
479                 return get_dirsize(argv[1]);
480         }
481         if (strcmp(argv[0], "lsfs") == 0) {
482                 return get_fslist();
483         }
484         if (strcmp(argv[0], "show") == 0) {
485                 if (argc != 2)
486                         usage(1);
487                 return show_mp(argv[1]);
488         }
489
490         usage(0);
491 }