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