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