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