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