Whamcloud - gitweb
e2fsck: merge quota context after threads finish
[tools/e2fsprogs.git] / lib / support / mkquota.c
1 /*
2  * mkquota.c --- create quota files for a filesystem
3  *
4  * Aditya Kali <adityakali@google.com>
5  */
6 #include "config.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <fcntl.h>
13
14 #include "ext2fs/ext2_fs.h"
15 #include "ext2fs/ext2fs.h"
16 #include "e2p/e2p.h"
17
18 #include "quotaio.h"
19 #include "quotaio_v2.h"
20 #include "quotaio_tree.h"
21 #include "common.h"
22 #include "dict.h"
23
24 /* Needed for architectures where sizeof(int) != sizeof(void *) */
25 #define UINT_TO_VOIDPTR(val)  ((void *)(intptr_t)(val))
26 #define VOIDPTR_TO_UINT(ptr)  ((unsigned int)(intptr_t)(ptr))
27
28 #if DEBUG_QUOTA
29 static void print_inode(struct ext2_inode *inode)
30 {
31         if (!inode)
32                 return;
33
34         fprintf(stderr, "  i_mode = %d\n", inode->i_mode);
35         fprintf(stderr, "  i_uid = %d\n", inode->i_uid);
36         fprintf(stderr, "  i_size = %d\n", inode->i_size);
37         fprintf(stderr, "  i_atime = %d\n", inode->i_atime);
38         fprintf(stderr, "  i_ctime = %d\n", inode->i_ctime);
39         fprintf(stderr, "  i_mtime = %d\n", inode->i_mtime);
40         fprintf(stderr, "  i_dtime = %d\n", inode->i_dtime);
41         fprintf(stderr, "  i_gid = %d\n", inode->i_gid);
42         fprintf(stderr, "  i_links_count = %d\n", inode->i_links_count);
43         fprintf(stderr, "  i_blocks = %d\n", inode->i_blocks);
44         fprintf(stderr, "  i_flags = %d\n", inode->i_flags);
45
46         return;
47 }
48
49 static void print_dquot(const char *desc, struct dquot *dq)
50 {
51         if (desc)
52                 fprintf(stderr, "%s: ", desc);
53         fprintf(stderr, "%u %lld:%lld:%lld %lld:%lld:%lld\n",
54                 dq->dq_id, (long long) dq->dq_dqb.dqb_curspace,
55                 (long long) dq->dq_dqb.dqb_bsoftlimit,
56                 (long long) dq->dq_dqb.dqb_bhardlimit,
57                 (long long) dq->dq_dqb.dqb_curinodes,
58                 (long long) dq->dq_dqb.dqb_isoftlimit,
59                 (long long) dq->dq_dqb.dqb_ihardlimit);
60 }
61 #else
62 static void print_dquot(const char *desc EXT2FS_ATTR((unused)),
63                         struct dquot *dq EXT2FS_ATTR((unused)))
64 {
65 }
66 #endif
67
68 /*
69  * Returns 0 if not able to find the quota file, otherwise returns its
70  * inode number.
71  */
72 int quota_file_exists(ext2_filsys fs, enum quota_type qtype)
73 {
74         char qf_name[256];
75         errcode_t ret;
76         ext2_ino_t ino;
77
78         if (qtype >= MAXQUOTAS)
79                 return -EINVAL;
80
81         quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
82
83         ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
84                             &ino);
85         if (ret)
86                 return 0;
87
88         return ino;
89 }
90
91 /*
92  * Set the value for reserved quota inode number field in superblock.
93  */
94 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype)
95 {
96         ext2_ino_t *inump;
97
98         inump = quota_sb_inump(fs->super, qtype);
99
100         log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
101                  qtype);
102         if (inump == NULL)
103                 return;
104         *inump = ino;
105         ext2fs_mark_super_dirty(fs);
106 }
107
108 errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype)
109 {
110         ext2_ino_t qf_ino;
111         errcode_t       retval;
112
113         retval = ext2fs_read_bitmaps(fs);
114         if (retval) {
115                 log_debug("Couldn't read bitmaps: %s", error_message(retval));
116                 return retval;
117         }
118
119         qf_ino = *quota_sb_inump(fs->super, qtype);
120         if (qf_ino == 0)
121                 return 0;
122         retval = quota_inode_truncate(fs, qf_ino);
123         if (retval)
124                 return retval;
125         if (qf_ino >= EXT2_FIRST_INODE(fs->super)) {
126                 struct ext2_inode inode;
127
128                 retval = ext2fs_read_inode(fs, qf_ino, &inode);
129                 if (!retval) {
130                         memset(&inode, 0, sizeof(struct ext2_inode));
131                         ext2fs_write_inode(fs, qf_ino, &inode);
132                 }
133                 ext2fs_inode_alloc_stats2(fs, qf_ino, -1, 0);
134                 ext2fs_mark_ib_dirty(fs);
135
136         }
137         quota_set_sb_inum(fs, 0, qtype);
138
139         ext2fs_mark_super_dirty(fs);
140         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
141         retval = ext2fs_write_bitmaps(fs);
142         if (retval) {
143                 log_debug("Couldn't write bitmaps: %s", error_message(retval));
144                 return retval;
145         }
146         return 0;
147 }
148
149 static void write_dquots(dict_t *dict, struct quota_handle *qh)
150 {
151         dnode_t         *n;
152         struct dquot    *dq;
153
154         for (n = dict_first(dict); n; n = dict_next(dict, n)) {
155                 dq = dnode_get(n);
156                 if (dq) {
157                         print_dquot("write", dq);
158                         dq->dq_h = qh;
159                         update_grace_times(dq);
160                         qh->qh_ops->commit_dquot(dq);
161                 }
162         }
163 }
164
165 errcode_t quota_write_inode(quota_ctx_t qctx, unsigned int qtype_bits)
166 {
167         int             retval = 0;
168         enum quota_type qtype;
169         dict_t          *dict;
170         ext2_filsys     fs;
171         struct quota_handle *h = NULL;
172         int             fmt = QFMT_VFS_V1;
173
174         if (!qctx)
175                 return 0;
176
177         fs = qctx->fs;
178         retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
179         if (retval) {
180                 log_debug("Unable to allocate quota handle: %s",
181                         error_message(retval));
182                 goto out;
183         }
184
185         retval = ext2fs_read_bitmaps(fs);
186         if (retval) {
187                 log_debug("Couldn't read bitmaps: %s", error_message(retval));
188                 goto out;
189         }
190
191         for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
192                 if (((1 << qtype) & qtype_bits) == 0)
193                         continue;
194
195                 dict = qctx->quota_dict[qtype];
196                 if (!dict)
197                         continue;
198
199                 retval = quota_file_create(h, fs, qtype, fmt);
200                 if (retval) {
201                         log_debug("Cannot initialize io on quotafile: %s",
202                                   error_message(retval));
203                         goto out;
204                 }
205
206                 write_dquots(dict, h);
207                 retval = quota_file_close(qctx, h);
208                 if (retval) {
209                         log_debug("Cannot finish IO on new quotafile: %s",
210                                   strerror(errno));
211                         if (h->qh_qf.e2_file)
212                                 ext2fs_file_close(h->qh_qf.e2_file);
213                         (void) quota_inode_truncate(fs, h->qh_qf.ino);
214                         goto out;
215                 }
216
217                 /* Set quota inode numbers in superblock. */
218                 quota_set_sb_inum(fs, h->qh_qf.ino, qtype);
219                 ext2fs_mark_super_dirty(fs);
220                 ext2fs_mark_bb_dirty(fs);
221                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
222         }
223
224         retval = ext2fs_write_bitmaps(fs);
225         if (retval) {
226                 log_debug("Couldn't write bitmaps: %s", error_message(retval));
227                 goto out;
228         }
229 out:
230         if (h)
231                 ext2fs_free_mem(&h);
232         return retval;
233 }
234
235 /******************************************************************/
236 /* Helper functions for computing quota in memory.                */
237 /******************************************************************/
238
239 static int dict_uint_cmp(const void *cmp_ctx EXT2FS_ATTR((unused)),
240                          const void *a, const void *b)
241 {
242         unsigned int    c, d;
243
244         c = VOIDPTR_TO_UINT(a);
245         d = VOIDPTR_TO_UINT(b);
246
247         if (c == d)
248                 return 0;
249         else if (c > d)
250                 return 1;
251         else
252                 return -1;
253 }
254
255 static inline int project_quota_valid(quota_ctx_t qctx)
256 {
257         return (EXT2_INODE_SIZE(qctx->fs->super) > EXT2_GOOD_OLD_INODE_SIZE);
258 }
259
260 static inline qid_t get_qid(struct ext2_inode_large *inode, enum quota_type qtype)
261 {
262         unsigned int inode_size;
263
264         switch (qtype) {
265         case USRQUOTA:
266                 return inode_uid(*inode);
267         case GRPQUOTA:
268                 return inode_gid(*inode);
269         case PRJQUOTA:
270                 inode_size = EXT2_GOOD_OLD_INODE_SIZE +
271                         inode->i_extra_isize;
272                 if (inode_includes(inode_size, i_projid))
273                         return inode_projid(*inode);
274                 return 0;
275         default:
276                 return 0;
277         }
278
279         return 0;
280 }
281
282 static void quota_dnode_free(dnode_t *node,
283                              void *context EXT2FS_ATTR((unused)))
284 {
285         void *ptr = node ? dnode_get(node) : 0;
286
287         ext2fs_free_mem(&ptr);
288         free(node);
289 }
290
291 /*
292  * Set up the quota tracking data structures.
293  */
294 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs,
295                              unsigned int qtype_bits)
296 {
297         errcode_t err;
298         dict_t  *dict;
299         quota_ctx_t ctx;
300         enum quota_type qtype;
301
302         err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
303         if (err) {
304                 log_debug("Failed to allocate quota context");
305                 return err;
306         }
307
308         memset(ctx, 0, sizeof(struct quota_ctx));
309         for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
310                 ctx->quota_file[qtype] = NULL;
311                 if (qtype_bits) {
312                         if (((1 << qtype) & qtype_bits) == 0)
313                                 continue;
314                 } else {
315                         if (*quota_sb_inump(fs->super, qtype) == 0)
316                                 continue;
317                 }
318                 err = ext2fs_get_mem(sizeof(dict_t), &dict);
319                 if (err) {
320                         log_debug("Failed to allocate dictionary");
321                         quota_release_context(&ctx);
322                         return err;
323                 }
324                 ctx->quota_dict[qtype] = dict;
325                 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
326                 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
327         }
328
329         ctx->fs = fs;
330         *qctx = ctx;
331         return 0;
332 }
333
334 void quota_release_context(quota_ctx_t *qctx)
335 {
336         errcode_t err;
337         dict_t  *dict;
338         enum quota_type qtype;
339         quota_ctx_t ctx;
340
341         if (!qctx)
342                 return;
343
344         ctx = *qctx;
345         for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
346                 dict = ctx->quota_dict[qtype];
347                 ctx->quota_dict[qtype] = 0;
348                 if (dict) {
349                         dict_free_nodes(dict);
350                         free(dict);
351                 }
352                 if (ctx->quota_file[qtype]) {
353                         err = quota_file_close(ctx, ctx->quota_file[qtype]);
354                         if (err) {
355                                 log_err("Cannot close quotafile: %s",
356                                         strerror(errno));
357                                 ext2fs_free_mem(&ctx->quota_file[qtype]);
358                         }
359                 }
360         }
361         *qctx = NULL;
362         free(ctx);
363 }
364
365 static struct dquot *get_dq(dict_t *dict, __u32 key)
366 {
367         struct dquot    *dq;
368         dnode_t         *n;
369
370         n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
371         if (n)
372                 dq = dnode_get(n);
373         else {
374                 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
375                         log_err("Unable to allocate dquot");
376                         return NULL;
377                 }
378                 memset(dq, 0, sizeof(struct dquot));
379                 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
380                 dq->dq_id = key;
381         }
382         return dq;
383 }
384
385
386 /*
387  * Called to update the blocks used by a particular inode
388  */
389 void quota_data_add(quota_ctx_t qctx, struct ext2_inode_large *inode,
390                     ext2_ino_t ino EXT2FS_ATTR((unused)),
391                     qsize_t space)
392 {
393         struct dquot    *dq;
394         dict_t          *dict;
395         enum quota_type qtype;
396
397         if (!qctx)
398                 return;
399
400         log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
401                         inode_uid(*inode),
402                         inode_gid(*inode), space);
403         for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
404                 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
405                         continue;
406                 dict = qctx->quota_dict[qtype];
407                 if (dict) {
408                         dq = get_dq(dict, get_qid(inode, qtype));
409                         if (dq)
410                                 dq->dq_dqb.dqb_curspace += space;
411                 }
412         }
413 }
414
415 /*
416  * Called to remove some blocks used by a particular inode
417  */
418 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode_large *inode,
419                     ext2_ino_t ino EXT2FS_ATTR((unused)),
420                     qsize_t space)
421 {
422         struct dquot    *dq;
423         dict_t          *dict;
424         enum quota_type qtype;
425
426         if (!qctx)
427                 return;
428
429         log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
430                         inode_uid(*inode),
431                         inode_gid(*inode), space);
432         for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
433                 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
434                         continue;
435                 dict = qctx->quota_dict[qtype];
436                 if (dict) {
437                         dq = get_dq(dict, get_qid(inode, qtype));
438                         if (dq)
439                                 dq->dq_dqb.dqb_curspace -= space;
440                 }
441         }
442 }
443
444 /*
445  * Called to count the files used by an inode's user/group
446  */
447 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode_large *inode,
448                        ext2_ino_t ino EXT2FS_ATTR((unused)), int adjust)
449 {
450         struct dquot    *dq;
451         dict_t          *dict;
452         enum quota_type qtype;
453
454         if (!qctx)
455                 return;
456
457         log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
458                         inode_uid(*inode),
459                         inode_gid(*inode), adjust);
460         for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
461                 if (qtype == PRJQUOTA && !project_quota_valid(qctx))
462                         continue;
463                 dict = qctx->quota_dict[qtype];
464                 if (dict) {
465                         dq = get_dq(dict, get_qid(inode, qtype));
466                         if (dq)
467                                 dq->dq_dqb.dqb_curinodes += adjust;
468                 }
469         }
470 }
471
472 errcode_t quota_compute_usage(quota_ctx_t qctx)
473 {
474         ext2_filsys fs;
475         ext2_ino_t ino;
476         errcode_t ret;
477         struct ext2_inode_large *inode;
478         int inode_size;
479         qsize_t space;
480         ext2_inode_scan scan;
481
482         if (!qctx)
483                 return 0;
484
485         fs = qctx->fs;
486         ret = ext2fs_open_inode_scan(fs, 0, &scan);
487         if (ret) {
488                 log_err("while opening inode scan. ret=%ld", ret);
489                 return ret;
490         }
491         inode_size = fs->super->s_inode_size;
492         inode = malloc(inode_size);
493         if (!inode) {
494                 ext2fs_close_inode_scan(scan);
495                 return ENOMEM;
496         }
497         while (1) {
498                 ret = ext2fs_get_next_inode_full(scan, &ino,
499                                                  EXT2_INODE(inode), inode_size);
500                 if (ret) {
501                         log_err("while getting next inode. ret=%ld", ret);
502                         ext2fs_close_inode_scan(scan);
503                         free(inode);
504                         return ret;
505                 }
506                 if (ino == 0)
507                         break;
508                 if (!inode->i_links_count)
509                         continue;
510                 if (ino == EXT2_ROOT_INO ||
511                     (ino >= EXT2_FIRST_INODE(fs->super) &&
512                      ino != quota_type2inum(PRJQUOTA, fs->super) &&
513                      ino != fs->super->s_orphan_file_inum)) {
514                         space = ext2fs_get_stat_i_blocks(fs,
515                                                 EXT2_INODE(inode)) << 9;
516                         quota_data_add(qctx, inode, ino, space);
517                         quota_data_inodes(qctx, inode, ino, +1);
518                 }
519         }
520
521         ext2fs_close_inode_scan(scan);
522         free(inode);
523         return 0;
524 }
525
526 struct scan_dquots_data {
527         dict_t          *quota_dict;
528         int             update_limits; /* update limits from disk */
529         int             update_usage;
530         int             check_consistency;
531         int             usage_is_inconsistent;
532 };
533
534 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
535 {
536         struct scan_dquots_data *scan_data = cb_data;
537         dict_t *quota_dict = scan_data->quota_dict;
538         struct dquot *dq;
539
540         dq = get_dq(quota_dict, dquot->dq_id);
541         if (!dq)
542                 return -1;
543         dq->dq_id = dquot->dq_id;
544         dq->dq_flags |= DQF_SEEN;
545
546         print_dquot("mem", dq);
547         print_dquot("dsk", dquot);
548
549         /* Check if there is inconsistency */
550         if (scan_data->check_consistency &&
551             (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
552              dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes)) {
553                 scan_data->usage_is_inconsistent = 1;
554                 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %u:"
555                         "actual (%lld, %lld) != expected (%lld, %lld)\n",
556                         dq->dq_id, (long long) dq->dq_dqb.dqb_curspace,
557                         (long long) dq->dq_dqb.dqb_curinodes,
558                         (long long) dquot->dq_dqb.dqb_curspace,
559                         (long long) dquot->dq_dqb.dqb_curinodes);
560         }
561
562         if (scan_data->update_limits) {
563                 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
564                 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
565                 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
566                 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
567         }
568
569         if (scan_data->update_usage) {
570                 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
571                 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
572         }
573
574         return 0;
575 }
576
577 /*
578  * Read quotas from disk and updates the in-memory information determined by
579  * 'flags' from the on-disk data.
580  */
581 errcode_t quota_read_all_dquots(quota_ctx_t qctx, ext2_ino_t qf_ino,
582                                 enum quota_type qtype, unsigned int flags)
583 {
584         struct scan_dquots_data scan_data;
585         struct quota_handle *qh;
586         errcode_t err;
587
588         if (!qctx)
589                 return 0;
590
591         err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
592         if (err) {
593                 log_debug("Unable to allocate quota handle");
594                 return err;
595         }
596
597         err = quota_file_open(qctx, qh, qf_ino, qtype, -1, 0);
598         if (err) {
599                 log_debug("Open quota file failed");
600                 goto out;
601         }
602
603         scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
604         scan_data.check_consistency = 0;
605         scan_data.update_limits = !!(flags & QREAD_LIMITS);
606         scan_data.update_usage = !!(flags & QREAD_USAGE);
607         qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
608
609         err = quota_file_close(qctx, qh);
610         if (err) {
611                 log_debug("Cannot finish IO on new quotafile: %s",
612                         strerror(errno));
613                 if (qh->qh_qf.e2_file)
614                         ext2fs_file_close(qh->qh_qf.e2_file);
615         }
616 out:
617         ext2fs_free_mem(&qh);
618         return err;
619 }
620
621 static errcode_t merge_usage(dict_t *dest, dict_t *src)
622 {
623         dnode_t *n;
624         struct dquot *src_dq, *dest_dq;
625
626         for (n = dict_first(src); n; n = dict_next(src, n)) {
627                 src_dq = dnode_get(n);
628                 if (!src_dq)
629                         continue;
630                 dest_dq = get_dq(dest, src_dq->dq_id);
631                 if (dest_dq == NULL)
632                         return -ENOMEM;
633                 dest_dq->dq_dqb.dqb_curspace += src_dq->dq_dqb.dqb_curspace;
634                 dest_dq->dq_dqb.dqb_curinodes += src_dq->dq_dqb.dqb_curinodes;
635         }
636
637         return 0;
638 }
639
640
641 errcode_t quota_merge_and_update_usage(quota_ctx_t dest_qctx,
642                                         quota_ctx_t src_qctx)
643 {
644         dict_t *dict;
645         enum quota_type qtype;
646         errcode_t retval = 0;
647
648         for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
649                 dict = src_qctx->quota_dict[qtype];
650                 if (!dict)
651                         continue;
652                 retval = merge_usage(dest_qctx->quota_dict[qtype], dict);
653                 if (retval)
654                         break;
655         }
656
657         return retval;
658 }
659
660 /*
661  * Compares the measured quota in qctx->quota_dict with that in the quota inode
662  * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
663  * set to 1 if the supplied and on-disk quota usage values are not identical.
664  */
665 errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype,
666                                    int *usage_inconsistent)
667 {
668         struct quota_handle qh;
669         struct scan_dquots_data scan_data;
670         struct dquot *dq;
671         dnode_t *n;
672         dict_t *dict = qctx->quota_dict[qtype];
673         errcode_t err = 0;
674
675         if (!dict)
676                 goto out;
677
678         err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
679         if (err) {
680                 log_debug("Open quota file failed");
681                 goto out;
682         }
683
684         scan_data.quota_dict = qctx->quota_dict[qtype];
685         scan_data.update_limits = 1;
686         scan_data.update_usage = 0;
687         scan_data.check_consistency = 1;
688         scan_data.usage_is_inconsistent = 0;
689         err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
690         if (err) {
691                 log_debug("Error scanning dquots");
692                 *usage_inconsistent = 1;
693                 goto out_close_qh;
694         }
695
696         for (n = dict_first(dict); n; n = dict_next(dict, n)) {
697                 dq = dnode_get(n);
698                 if (!dq)
699                         continue;
700                 if ((dq->dq_flags & DQF_SEEN) == 0) {
701                         fprintf(stderr, "[QUOTA WARNING] "
702                                 "Missing quota entry ID %d\n", dq->dq_id);
703                         scan_data.usage_is_inconsistent = 1;
704                 }
705         }
706         *usage_inconsistent = scan_data.usage_is_inconsistent;
707
708 out_close_qh:
709         err = quota_file_close(qctx, &qh);
710         if (err) {
711                 log_debug("Cannot close quotafile: %s", error_message(errno));
712                 if (qh.qh_qf.e2_file)
713                         ext2fs_file_close(qh.qh_qf.e2_file);
714         }
715 out:
716         return err;
717 }
718
719 int parse_quota_opts(const char *opts, int (*func)(char *))
720 {
721         char    *buf, *token, *next, *p;
722         int     len;
723         int     ret = 0;
724
725         len = strlen(opts);
726         buf = malloc(len + 1);
727         if (!buf) {
728                 fprintf(stderr,
729                         "Couldn't allocate memory to parse quota options!\n");
730                 return -ENOMEM;
731         }
732         strcpy(buf, opts);
733         for (token = buf; token && *token; token = next) {
734                 p = strchr(token, ',');
735                 next = 0;
736                 if (p) {
737                         *p = 0;
738                         next = p + 1;
739                 }
740                 ret = func(token);
741                 if (ret)
742                         break;
743         }
744         free(buf);
745         return ret;
746 }