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