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