Whamcloud - gitweb
quota: fix memory leak in quota_compare_and_update()
[tools/e2fsprogs.git] / lib / quota / 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 "mkquota.h"
22 #include "common.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, dq->dq_dqb.dqb_curspace,
55                 dq->dq_dqb.dqb_bsoftlimit, dq->dq_dqb.dqb_bhardlimit,
56                 dq->dq_dqb.dqb_curinodes,
57                 dq->dq_dqb.dqb_isoftlimit, dq->dq_dqb.dqb_ihardlimit);
58 }
59 #else
60 static void print_dquot(const char *desc, struct dquot *dq)
61 {
62 }
63 #endif
64
65 /*
66  * Returns 0 if not able to find the quota file, otherwise returns its
67  * inode number.
68  */
69 int quota_file_exists(ext2_filsys fs, int qtype, int fmt)
70 {
71         char qf_name[256];
72         errcode_t ret;
73         ext2_ino_t ino;
74
75         if (qtype >= MAXQUOTAS)
76                 return -EINVAL;
77
78         quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name);
79
80         ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0,
81                             &ino);
82         if (ret)
83                 return 0;
84
85         return ino;
86 }
87
88 /*
89  * Set the value for reserved quota inode number field in superblock.
90  */
91 void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, int qtype)
92 {
93         ext2_ino_t *inump;
94
95         inump = (qtype == USRQUOTA) ? &fs->super->s_usr_quota_inum :
96                 &fs->super->s_grp_quota_inum;
97
98         log_debug("setting quota ino in superblock: ino=%u, type=%d", ino,
99                  qtype);
100         *inump = ino;
101         ext2fs_mark_super_dirty(fs);
102 }
103
104 errcode_t quota_remove_inode(ext2_filsys fs, int qtype)
105 {
106         ext2_ino_t qf_ino;
107         errcode_t       retval;
108
109         retval = ext2fs_read_bitmaps(fs);
110         if (retval) {
111                 log_err("Couldn't read bitmaps: %s", error_message(retval));
112                 return retval;
113         }
114         qf_ino = (qtype == USRQUOTA) ? fs->super->s_usr_quota_inum :
115                 fs->super->s_grp_quota_inum;
116         quota_set_sb_inum(fs, 0, qtype);
117         /* Truncate the inode only if its a reserved one. */
118         if (qf_ino < EXT2_FIRST_INODE(fs->super))
119                 quota_inode_truncate(fs, qf_ino);
120
121         ext2fs_mark_super_dirty(fs);
122         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
123         retval = ext2fs_write_bitmaps(fs);
124         if (retval) {
125                 log_err("Couldn't write bitmaps: %s", error_message(retval));
126                 return retval;
127         }
128         return 0;
129 }
130
131 static void write_dquots(dict_t *dict, struct quota_handle *qh)
132 {
133         dnode_t         *n;
134         struct dquot    *dq;
135
136         for (n = dict_first(dict); n; n = dict_next(dict, n)) {
137                 dq = dnode_get(n);
138                 if (dq) {
139                         print_dquot("write", dq);
140                         dq->dq_h = qh;
141                         update_grace_times(dq);
142                         qh->qh_ops->commit_dquot(dq);
143                 }
144         }
145 }
146
147 errcode_t quota_write_inode(quota_ctx_t qctx, int qtype)
148 {
149         int             retval = 0, i;
150         dict_t          *dict;
151         ext2_filsys     fs;
152         struct quota_handle *h = NULL;
153         int             fmt = QFMT_VFS_V1;
154
155         if (!qctx)
156                 return 0;
157
158         fs = qctx->fs;
159         retval = ext2fs_get_mem(sizeof(struct quota_handle), &h);
160         if (retval) {
161                 log_err("Unable to allocate quota handle: %s",
162                         error_message(retval));
163                 goto out;
164         }
165
166         retval = ext2fs_read_bitmaps(fs);
167         if (retval) {
168                 log_err("Couldn't read bitmaps: %s", error_message(retval));
169                 goto out;
170         }
171
172         for (i = 0; i < MAXQUOTAS; i++) {
173                 if ((qtype != -1) && (i != qtype))
174                         continue;
175
176                 dict = qctx->quota_dict[i];
177                 if (!dict)
178                         continue;
179
180                 retval = quota_file_create(h, fs, i, fmt);
181                 if (retval < 0) {
182                         log_err("Cannot initialize io on quotafile");
183                         continue;
184                 }
185
186                 write_dquots(dict, h);
187                 retval = quota_file_close(h);
188                 if (retval < 0) {
189                         log_err("Cannot finish IO on new quotafile: %s",
190                                 strerror(errno));
191                         if (h->qh_qf.e2_file)
192                                 ext2fs_file_close(h->qh_qf.e2_file);
193                         quota_inode_truncate(fs, h->qh_qf.ino);
194                         continue;
195                 }
196
197                 /* Set quota inode numbers in superblock. */
198                 quota_set_sb_inum(fs, h->qh_qf.ino, i);
199                 ext2fs_mark_super_dirty(fs);
200                 ext2fs_mark_bb_dirty(fs);
201                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
202         }
203
204         retval = ext2fs_write_bitmaps(fs);
205         if (retval) {
206                 log_err("Couldn't write bitmaps: %s", error_message(retval));
207                 goto out;
208         }
209 out:
210         if (h)
211                 ext2fs_free_mem(&h);
212         return retval;
213 }
214
215 /******************************************************************/
216 /* Helper functions for computing quota in memory.                */
217 /******************************************************************/
218
219 static int dict_uint_cmp(const void *a, const void *b)
220 {
221         unsigned int    c, d;
222
223         c = VOIDPTR_TO_UINT(a);
224         d = VOIDPTR_TO_UINT(b);
225
226         if (c == d)
227                 return 0;
228         else if (c > d)
229                 return 1;
230         else
231                 return -1;
232 }
233
234 static inline qid_t get_qid(struct ext2_inode *inode, int qtype)
235 {
236         if (qtype == USRQUOTA)
237                 return inode_uid(*inode);
238         return inode_gid(*inode);
239 }
240
241 static void quota_dnode_free(dnode_t *node,
242                              void *context EXT2FS_ATTR((unused)))
243 {
244         void *ptr = node ? dnode_get(node) : 0;
245
246         ext2fs_free_mem(&ptr);
247         free(node);
248 }
249
250 /*
251  * Set up the quota tracking data structures.
252  */
253 errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs, int qtype)
254 {
255         int     i, err = 0;
256         dict_t  *dict;
257         quota_ctx_t ctx;
258
259         err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx);
260         if (err) {
261                 log_err("Failed to allocate quota context");
262                 return err;
263         }
264
265         memset(ctx, 0, sizeof(struct quota_ctx));
266         for (i = 0; i < MAXQUOTAS; i++) {
267                 if ((qtype != -1) && (i != qtype))
268                         continue;
269                 err = ext2fs_get_mem(sizeof(dict_t), &dict);
270                 if (err) {
271                         log_err("Failed to allocate dictionary");
272                         quota_release_context(&ctx);
273                         return err;
274                 }
275                 ctx->quota_dict[i] = dict;
276                 dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
277                 dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
278         }
279
280         ctx->fs = fs;
281         *qctx = ctx;
282         return 0;
283 }
284
285 void quota_release_context(quota_ctx_t *qctx)
286 {
287         dict_t  *dict;
288         int     i;
289         quota_ctx_t ctx;
290
291         if (!qctx)
292                 return;
293
294         ctx = *qctx;
295         for (i = 0; i < MAXQUOTAS; i++) {
296                 dict = ctx->quota_dict[i];
297                 ctx->quota_dict[i] = 0;
298                 if (dict) {
299                         dict_free_nodes(dict);
300                         free(dict);
301                 }
302         }
303         *qctx = NULL;
304         free(ctx);
305 }
306
307 static struct dquot *get_dq(dict_t *dict, __u32 key)
308 {
309         struct dquot    *dq;
310         dnode_t         *n;
311
312         n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
313         if (n)
314                 dq = dnode_get(n);
315         else {
316                 if (ext2fs_get_mem(sizeof(struct dquot), &dq)) {
317                         log_err("Unable to allocate dquot");
318                         return NULL;
319                 }
320                 memset(dq, 0, sizeof(struct dquot));
321                 dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
322                 dq->dq_id = key;
323         }
324         return dq;
325 }
326
327
328 /*
329  * Called to update the blocks used by a particular inode
330  */
331 void quota_data_add(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
332                     qsize_t space)
333 {
334         struct dquot    *dq;
335         dict_t          *dict;
336         int             i;
337
338         if (!qctx)
339                 return;
340
341         log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
342                         inode_uid(*inode),
343                         inode_gid(*inode), space);
344         for (i = 0; i < MAXQUOTAS; i++) {
345                 dict = qctx->quota_dict[i];
346                 if (dict) {
347                         dq = get_dq(dict, get_qid(inode, i));
348                         if (dq)
349                                 dq->dq_dqb.dqb_curspace += space;
350                 }
351         }
352 }
353
354 /*
355  * Called to remove some blocks used by a particular inode
356  */
357 void quota_data_sub(quota_ctx_t qctx, struct ext2_inode *inode, ext2_ino_t ino,
358                     qsize_t space)
359 {
360         struct dquot    *dq;
361         dict_t          *dict;
362         int             i;
363
364         if (!qctx)
365                 return;
366
367         log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino,
368                         inode_uid(*inode),
369                         inode_gid(*inode), space);
370         for (i = 0; i < MAXQUOTAS; i++) {
371                 dict = qctx->quota_dict[i];
372                 if (dict) {
373                         dq = get_dq(dict, get_qid(inode, i));
374                         dq->dq_dqb.dqb_curspace -= space;
375                 }
376         }
377 }
378
379 /*
380  * Called to count the files used by an inode's user/group
381  */
382 void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode *inode,
383                        ext2_ino_t ino, int adjust)
384 {
385         struct dquot    *dq;
386         dict_t          *dict;
387         int             i;
388
389         if (!qctx)
390                 return;
391
392         log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino,
393                         inode_uid(*inode),
394                         inode_gid(*inode), adjust);
395         for (i = 0; i < MAXQUOTAS; i++) {
396                 dict = qctx->quota_dict[i];
397                 if (dict) {
398                         dq = get_dq(dict, get_qid(inode, i));
399                         dq->dq_dqb.dqb_curinodes += adjust;
400                 }
401         }
402 }
403
404 errcode_t quota_compute_usage(quota_ctx_t qctx)
405 {
406         ext2_filsys fs;
407         ext2_ino_t ino;
408         errcode_t ret;
409         struct ext2_inode inode;
410         qsize_t space;
411         ext2_inode_scan scan;
412
413         if (!qctx)
414                 return 0;
415
416         fs = qctx->fs;
417         ret = ext2fs_open_inode_scan(fs, 0, &scan);
418         if (ret) {
419                 log_err("while opening inode scan. ret=%ld", ret);
420                 return ret;
421         }
422
423         while (1) {
424                 ret = ext2fs_get_next_inode(scan, &ino, &inode);
425                 if (ret) {
426                         log_err("while getting next inode. ret=%ld", ret);
427                         ext2fs_close_inode_scan(scan);
428                         return ret;
429                 }
430                 if (ino == 0)
431                         break;
432                 if (inode.i_links_count &&
433                     (ino == EXT2_ROOT_INO ||
434                      ino >= EXT2_FIRST_INODE(fs->super))) {
435                         space = ext2fs_inode_i_blocks(fs, &inode) << 9;
436                         quota_data_add(qctx, &inode, ino, space);
437                         quota_data_inodes(qctx, &inode, ino, +1);
438                 }
439         }
440
441         ext2fs_close_inode_scan(scan);
442
443         return 0;
444 }
445
446 struct scan_dquots_data {
447         dict_t          *quota_dict;
448         int             update_limits; /* update limits from disk */
449         int             update_usage;
450         int             usage_is_inconsistent;
451 };
452
453 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
454 {
455         struct scan_dquots_data *scan_data = cb_data;
456         dict_t *quota_dict = scan_data->quota_dict;
457         struct dquot *dq;
458
459         dq = get_dq(quota_dict, dquot->dq_id);
460         dq->dq_id = dquot->dq_id;
461         dq->dq_flags |= DQF_SEEN;
462
463         print_dquot("mem", dq);
464         print_dquot("dsk", dquot);
465
466         /* Check if there is inconsistancy. */
467         if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
468             dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
469                 scan_data->usage_is_inconsistent = 1;
470                 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
471                         "actual (%llu, %llu) != expected (%llu, %llu)\n",
472                         dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
473                         (long long)dq->dq_dqb.dqb_curinodes,
474                         (long long)dquot->dq_dqb.dqb_curspace,
475                         (long long)dquot->dq_dqb.dqb_curinodes);
476         }
477
478         if (scan_data->update_limits) {
479                 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
480                 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
481                 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
482                 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
483         }
484
485         if (scan_data->update_usage) {
486                 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
487                 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
488         }
489
490         return 0;
491 }
492
493 /*
494  * Read all dquots from quota file into memory
495  */
496 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
497                                        quota_ctx_t qctx, int update_limits)
498 {
499         struct scan_dquots_data scan_data;
500
501         scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
502         scan_data.update_limits = update_limits;
503         scan_data.update_usage = 0;
504
505         return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
506 }
507
508 /*
509  * Write all memory dquots into quota file
510  */
511 #if 0 /* currently unused, but may be useful in the future? */
512 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
513                                         quota_ctx_t qctx)
514 {
515         errcode_t err;
516
517         err = ext2fs_read_bitmaps(qctx->fs);
518         if (err)
519                 return err;
520         write_dquots(qctx->quota_dict[qh->qh_type], qh);
521         ext2fs_mark_bb_dirty(qctx->fs);
522         qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
523         ext2fs_write_bitmaps(qctx->fs);
524         return 0;
525 }
526 #endif
527
528 /*
529  * Updates the in-memory quota limits from the given quota inode.
530  */
531 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
532 {
533         struct quota_handle *qh;
534         errcode_t err;
535
536         if (!qctx)
537                 return 0;
538
539         err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
540         if (err) {
541                 log_err("Unable to allocate quota handle");
542                 return err;
543         }
544
545         err = quota_file_open(qh, qctx->fs, qf_ino, type, -1, 0);
546         if (err) {
547                 log_err("Open quota file failed");
548                 goto out;
549         }
550
551         quota_read_all_dquots(qh, qctx, 1);
552
553         err = quota_file_close(qh);
554         if (err) {
555                 log_err("Cannot finish IO on new quotafile: %s",
556                         strerror(errno));
557                 if (qh->qh_qf.e2_file)
558                         ext2fs_file_close(qh->qh_qf.e2_file);
559         }
560 out:
561         ext2fs_free_mem(&qh);
562         return err;
563 }
564
565 /*
566  * Compares the measured quota in qctx->quota_dict with that in the quota inode
567  * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
568  * set to 1 if the supplied and on-disk quota usage values are not identical.
569  */
570 errcode_t quota_compare_and_update(quota_ctx_t qctx, int qtype,
571                                    int *usage_inconsistent)
572 {
573         ext2_filsys fs = qctx->fs;
574         struct quota_handle qh;
575         struct scan_dquots_data scan_data;
576         struct dquot *dq;
577         dnode_t *n;
578         dict_t *dict = qctx->quota_dict[qtype];
579         ext2_ino_t qf_ino;
580         errcode_t err = 0;
581
582         if (!dict)
583                 goto out;
584
585         qf_ino = qtype == USRQUOTA ? fs->super->s_usr_quota_inum :
586                                      fs->super->s_grp_quota_inum;
587         err = quota_file_open(&qh, fs, qf_ino, qtype, -1, 0);
588         if (err) {
589                 log_err("Open quota file failed");
590                 goto out;
591         }
592
593         scan_data.quota_dict = qctx->quota_dict[qtype];
594         scan_data.update_limits = 1;
595         scan_data.update_usage = 0;
596         scan_data.usage_is_inconsistent = 0;
597         err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
598         if (err) {
599                 log_err("Error scanning dquots");
600                 goto out_close_qh;
601         }
602
603         for (n = dict_first(dict); n; n = dict_next(dict, n)) {
604                 dq = dnode_get(n);
605                 if (!dq)
606                         continue;
607                 if ((dq->dq_flags & DQF_SEEN) == 0) {
608                         fprintf(stderr, "[QUOTA WARNING] "
609                                 "Missing quota entry ID %d\n", dq->dq_id);
610                         scan_data.usage_is_inconsistent = 1;
611                 }
612         }
613         *usage_inconsistent = scan_data.usage_is_inconsistent;
614
615 out_close_qh:
616         err = quota_file_close(&qh);
617         if (err) {
618                 log_err("Cannot close quotafile: %s", error_message(errno));
619                 if (qh.qh_qf.e2_file)
620                         ext2fs_file_close(qh.qh_qf.e2_file);
621         }
622 out:
623         return err;
624 }