Whamcloud - gitweb
LU-4017 e2fsprogs: always read full inode structure
[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         int inode_size;
421         qsize_t space;
422         ext2_inode_scan scan;
423
424         if (!qctx)
425                 return 0;
426
427         inode_size = EXT2_INODE_SIZE(qctx->fs->super);
428         ret = ext2fs_get_mem(inode_size, &inode);
429         if (ret)
430                 return ret;
431
432         fs = qctx->fs;
433         ret = ext2fs_open_inode_scan(fs, 0, &scan);
434         if (ret) {
435                 ext2fs_free_mem(&inode);
436                 log_err("while opening inode scan. ret=%ld", ret);
437                 return ret;
438         }
439
440         while (1) {
441                 ret = ext2fs_get_next_inode_full(scan, &ino, inode,
442                                                  inode_size);
443                 if (ret) {
444                         log_err("while getting next inode. ret=%ld", ret);
445                         goto err;
446                 }
447                 if (ino == 0)
448                         break;
449                 if (inode->i_links_count &&
450                     (ino == EXT2_ROOT_INO ||
451                      ino >= EXT2_FIRST_INODE(fs->super))) {
452                         space = ext2fs_inode_i_blocks(fs, inode) << 9;
453                         quota_data_add(qctx, inode, ino, space);
454                         quota_data_inodes(qctx, inode, ino, +1);
455                 }
456         }
457
458 err:
459         ext2fs_close_inode_scan(scan);
460         ext2fs_free_mem(&inode);
461
462         return ret;
463 }
464
465 struct scan_dquots_data {
466         dict_t          *quota_dict;
467         int             update_limits; /* update limits from disk */
468         int             update_usage;
469         int             usage_is_inconsistent;
470 };
471
472 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
473 {
474         struct scan_dquots_data *scan_data = cb_data;
475         dict_t *quota_dict = scan_data->quota_dict;
476         struct dquot *dq;
477
478         dq = get_dq(quota_dict, dquot->dq_id);
479         dq->dq_id = dquot->dq_id;
480         dq->dq_flags |= DQF_SEEN;
481
482         print_dquot("mem", dq);
483         print_dquot("dsk", dquot);
484
485         /* Check if there is inconsistancy. */
486         if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
487             dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
488                 scan_data->usage_is_inconsistent = 1;
489                 fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %d:"
490                         "actual (%llu, %llu) != expected (%llu, %llu)\n",
491                         dq->dq_id, (long long)dq->dq_dqb.dqb_curspace,
492                         (long long)dq->dq_dqb.dqb_curinodes,
493                         (long long)dquot->dq_dqb.dqb_curspace,
494                         (long long)dquot->dq_dqb.dqb_curinodes);
495         }
496
497         if (scan_data->update_limits) {
498                 dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
499                 dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
500                 dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
501                 dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
502         }
503
504         if (scan_data->update_usage) {
505                 dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
506                 dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
507         }
508
509         return 0;
510 }
511
512 /*
513  * Read all dquots from quota file into memory
514  */
515 static errcode_t quota_read_all_dquots(struct quota_handle *qh,
516                                        quota_ctx_t qctx, int update_limits)
517 {
518         struct scan_dquots_data scan_data;
519
520         scan_data.quota_dict = qctx->quota_dict[qh->qh_type];
521         scan_data.update_limits = update_limits;
522         scan_data.update_usage = 0;
523
524         return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data);
525 }
526
527 /*
528  * Write all memory dquots into quota file
529  */
530 #if 0 /* currently unused, but may be useful in the future? */
531 static errcode_t quota_write_all_dquots(struct quota_handle *qh,
532                                         quota_ctx_t qctx)
533 {
534         errcode_t err;
535
536         err = ext2fs_read_bitmaps(qctx->fs);
537         if (err)
538                 return err;
539         write_dquots(qctx->quota_dict[qh->qh_type], qh);
540         ext2fs_mark_bb_dirty(qctx->fs);
541         qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
542         ext2fs_write_bitmaps(qctx->fs);
543         return 0;
544 }
545 #endif
546
547 /*
548  * Updates the in-memory quota limits from the given quota inode.
549  */
550 errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, int type)
551 {
552         struct quota_handle *qh;
553         errcode_t err;
554
555         if (!qctx)
556                 return 0;
557
558         err = ext2fs_get_mem(sizeof(struct quota_handle), &qh);
559         if (err) {
560                 log_err("Unable to allocate quota handle");
561                 return err;
562         }
563
564         err = quota_file_open(qctx, qh, qf_ino, type, -1, 0);
565         if (err) {
566                 log_err("Open quota file failed");
567                 goto out;
568         }
569
570         quota_read_all_dquots(qh, qctx, 1);
571
572         err = quota_file_close(qctx, qh);
573         if (err) {
574                 log_err("Cannot finish IO on new quotafile: %s",
575                         strerror(errno));
576                 if (qh->qh_qf.e2_file)
577                         ext2fs_file_close(qh->qh_qf.e2_file);
578         }
579 out:
580         ext2fs_free_mem(&qh);
581         return err;
582 }
583
584 /*
585  * Compares the measured quota in qctx->quota_dict with that in the quota inode
586  * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
587  * set to 1 if the supplied and on-disk quota usage values are not identical.
588  */
589 errcode_t quota_compare_and_update(quota_ctx_t qctx, int qtype,
590                                    int *usage_inconsistent)
591 {
592         struct quota_handle qh;
593         struct scan_dquots_data scan_data;
594         struct dquot *dq;
595         dnode_t *n;
596         dict_t *dict = qctx->quota_dict[qtype];
597         errcode_t err = 0;
598
599         if (!dict)
600                 goto out;
601
602         err = quota_file_open(qctx, &qh, 0, qtype, -1, 0);
603         if (err) {
604                 log_err("Open quota file failed");
605                 goto out;
606         }
607
608         scan_data.quota_dict = qctx->quota_dict[qtype];
609         scan_data.update_limits = 1;
610         scan_data.update_usage = 0;
611         scan_data.usage_is_inconsistent = 0;
612         err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
613         if (err) {
614                 log_err("Error scanning dquots");
615                 goto out_close_qh;
616         }
617
618         for (n = dict_first(dict); n; n = dict_next(dict, n)) {
619                 dq = dnode_get(n);
620                 if (!dq)
621                         continue;
622                 if ((dq->dq_flags & DQF_SEEN) == 0) {
623                         fprintf(stderr, "[QUOTA WARNING] "
624                                 "Missing quota entry ID %d\n", dq->dq_id);
625                         scan_data.usage_is_inconsistent = 1;
626                 }
627         }
628         *usage_inconsistent = scan_data.usage_is_inconsistent;
629
630 out_close_qh:
631         err = quota_file_close(qctx, &qh);
632         if (err) {
633                 log_err("Cannot close quotafile: %s", error_message(errno));
634                 if (qh.qh_qf.e2_file)
635                         ext2fs_file_close(qh.qh_qf.e2_file);
636         }
637 out:
638         return err;
639 }