1 /*
2  * mkquota.c --- create quota files for a filesystem
3  *
4  * Aditya Kali <adityakali@google.com>
5  * Hyojun Kim <hyojun@google.com> - Ported to f2fs-tools
6  */
7 #include "config.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 
17 #include "quotaio.h"
18 #include "quotaio_v2.h"
19 #include "quotaio_tree.h"
20 #include "common.h"
21 #include "dict.h"
22 
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_dquot(const char * desc,struct dquot * dq)29 static void print_dquot(const char *desc, struct dquot *dq)
30 {
31 	if (desc)
32 		fprintf(stderr, "%s: ", desc);
33 	fprintf(stderr, "%u %lld:%lld:%lld %lld:%lld:%lld\n",
34 		dq->dq_id, (long long) dq->dq_dqb.dqb_curspace,
35 		(long long) dq->dq_dqb.dqb_bsoftlimit,
36 		(long long) dq->dq_dqb.dqb_bhardlimit,
37 		(long long) dq->dq_dqb.dqb_curinodes,
38 		(long long) dq->dq_dqb.dqb_isoftlimit,
39 		(long long) dq->dq_dqb.dqb_ihardlimit);
40 }
41 #else
42 #define print_dquot(...)
43 #endif
44 
write_dquots(dict_t * dict,struct quota_handle * qh)45 static void write_dquots(dict_t *dict, struct quota_handle *qh)
46 {
47 	dnode_t		*n;
48 	struct dquot	*dq;
49 
50 	for (n = dict_first(dict); n; n = dict_next(dict, n)) {
51 		dq = dnode_get(n);
52 		if (dq) {
53 			print_dquot("write", dq);
54 			dq->dq_h = qh;
55 			update_grace_times(dq);
56 			if (qh->qh_ops->commit_dquot(dq))
57 				break;
58 		}
59 	}
60 }
61 
quota_write_inode(struct f2fs_sb_info * sbi,enum quota_type qtype)62 errcode_t quota_write_inode(struct f2fs_sb_info *sbi, enum quota_type qtype)
63 {
64 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
65 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
66 	quota_ctx_t qctx = fsck->qctx;
67 	struct quota_handle *h = NULL;
68 	int retval = 0;
69 	dict_t *dict;
70 
71 	if ((!qctx) || (!sb->qf_ino[qtype]))
72 		return 0;
73 
74 	retval = quota_get_mem(sizeof(struct quota_handle), &h);
75 	if (retval) {
76 		log_debug("Unable to allocate quota handle");
77 		goto out;
78 	}
79 
80 	dict = qctx->quota_dict[qtype];
81 	if (dict) {
82 		retval = quota_file_create(sbi, h, qtype);
83 		if (retval) {
84 			log_debug("Cannot initialize io on quotafile");
85 		} else {
86 			write_dquots(dict, h);
87 			quota_file_close(sbi, h, 1);
88 		}
89 	}
90 out:
91 	if (h)
92 		quota_free_mem(&h);
93 	return retval;
94 }
95 
96 /******************************************************************/
97 /* Helper functions for computing quota in memory.                */
98 /******************************************************************/
99 
dict_uint_cmp(const void * a,const void * b)100 static int dict_uint_cmp(const void *a, const void *b)
101 {
102 	unsigned int	c, d;
103 
104 	c = VOIDPTR_TO_UINT(a);
105 	d = VOIDPTR_TO_UINT(b);
106 
107 	if (c == d)
108 		return 0;
109 	else if (c > d)
110 		return 1;
111 	else
112 		return -1;
113 }
114 
get_qid(struct f2fs_inode * inode,enum quota_type qtype)115 static inline qid_t get_qid(struct f2fs_inode *inode, enum quota_type qtype)
116 {
117 	switch (qtype) {
118 	case USRQUOTA:
119 		return inode->i_uid;
120 	case GRPQUOTA:
121 		return inode->i_gid;
122 	case PRJQUOTA:
123 		return inode->i_projid;
124 	default:
125 		return 0;
126 	}
127 
128 	return 0;
129 }
130 
quota_dnode_free(dnode_t * node,void * UNUSED (context))131 static void quota_dnode_free(dnode_t *node, void *UNUSED(context))
132 {
133 	void *ptr = node ? dnode_get(node) : 0;
134 
135 	quota_free_mem(&ptr);
136 	free(node);
137 }
138 
139 /*
140  * Set up the quota tracking data structures.
141  */
quota_init_context(struct f2fs_sb_info * sbi)142 errcode_t quota_init_context(struct f2fs_sb_info *sbi)
143 {
144 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
145 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
146 	errcode_t err;
147 	dict_t	*dict;
148 	quota_ctx_t ctx;
149 	enum quota_type	qtype;
150 
151 	err = quota_get_mem(sizeof(struct quota_ctx), &ctx);
152 	if (err) {
153 		log_debug("Failed to allocate quota context");
154 		return err;
155 	}
156 
157 	memset(ctx, 0, sizeof(struct quota_ctx));
158 	dict_init(&ctx->linked_inode_dict, DICTCOUNT_T_MAX, dict_uint_cmp);
159 	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
160 		ctx->quota_file[qtype] = NULL;
161 		if (!sb->qf_ino[qtype])
162 			continue;
163 		err = quota_get_mem(sizeof(dict_t), &dict);
164 		if (err) {
165 			log_debug("Failed to allocate dictionary");
166 			quota_release_context(&ctx);
167 			return err;
168 		}
169 		ctx->quota_dict[qtype] = dict;
170 		dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp);
171 		dict_set_allocator(dict, NULL, quota_dnode_free, NULL);
172 	}
173 	ctx->sbi = sbi;
174 	fsck->qctx = ctx;
175 	return 0;
176 }
177 
quota_release_context(quota_ctx_t * qctx)178 void quota_release_context(quota_ctx_t *qctx)
179 {
180 	dict_t	*dict;
181 	enum quota_type	qtype;
182 	quota_ctx_t ctx;
183 
184 	if (!qctx)
185 		return;
186 
187 	ctx = *qctx;
188 	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
189 		dict = ctx->quota_dict[qtype];
190 		ctx->quota_dict[qtype] = 0;
191 		if (dict) {
192 			dict_free_nodes(dict);
193 			free(dict);
194 		}
195 	}
196 	dict_free_nodes(&ctx->linked_inode_dict);
197 	*qctx = NULL;
198 	free(ctx);
199 }
200 
get_dq(dict_t * dict,__u32 key)201 static struct dquot *get_dq(dict_t *dict, __u32 key)
202 {
203 	struct dquot	*dq;
204 	dnode_t		*n;
205 
206 	n = dict_lookup(dict, UINT_TO_VOIDPTR(key));
207 	if (n)
208 		dq = dnode_get(n);
209 	else {
210 		if (quota_get_mem(sizeof(struct dquot), &dq)) {
211 			log_err("Unable to allocate dquot");
212 			return NULL;
213 		}
214 		memset(dq, 0, sizeof(struct dquot));
215 		dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq);
216 		dq->dq_id = key;
217 	}
218 	return dq;
219 }
220 
221 /*
222  * Called to update the blocks used by a particular inode
223  */
quota_data_add(quota_ctx_t qctx,struct f2fs_inode * inode,qsize_t space)224 void quota_data_add(quota_ctx_t qctx, struct f2fs_inode *inode, qsize_t space)
225 {
226 	struct dquot	*dq;
227 	dict_t		*dict;
228 	enum quota_type	qtype;
229 
230 	if (!qctx)
231 		return;
232 
233 	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
234 		dict = qctx->quota_dict[qtype];
235 		if (dict) {
236 			dq = get_dq(dict, get_qid(inode, qtype));
237 			if (dq)
238 				dq->dq_dqb.dqb_curspace += space;
239 		}
240 	}
241 }
242 
243 /*
244  * Called to remove some blocks used by a particular inode
245  */
quota_data_sub(quota_ctx_t qctx,struct f2fs_inode * inode,qsize_t space)246 void quota_data_sub(quota_ctx_t qctx, struct f2fs_inode *inode, qsize_t space)
247 {
248 	struct dquot	*dq;
249 	dict_t		*dict;
250 	enum quota_type	qtype;
251 
252 	if (!qctx)
253 		return;
254 
255 	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
256 		dict = qctx->quota_dict[qtype];
257 		if (dict) {
258 			dq = get_dq(dict, get_qid(inode, qtype));
259 			dq->dq_dqb.dqb_curspace -= space;
260 		}
261 	}
262 }
263 
264 /*
265  * Called to count the files used by an inode's user/group
266  */
quota_data_inodes(quota_ctx_t qctx,struct f2fs_inode * inode,int adjust)267 void quota_data_inodes(quota_ctx_t qctx, struct f2fs_inode *inode, int adjust)
268 {
269 	struct dquot	*dq;
270 	dict_t		*dict; enum quota_type	qtype;
271 
272 	if (!qctx)
273 		return;
274 
275 	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
276 		dict = qctx->quota_dict[qtype];
277 		if (dict) {
278 			dq = get_dq(dict, get_qid(inode, qtype));
279 			dq->dq_dqb.dqb_curinodes += adjust;
280 		}
281 	}
282 }
283 
284 /*
285  * Called from fsck to count quota.
286  */
quota_add_inode_usage(quota_ctx_t qctx,f2fs_ino_t ino,struct f2fs_inode * inode)287 void quota_add_inode_usage(quota_ctx_t qctx, f2fs_ino_t ino,
288 		struct f2fs_inode* inode)
289 {
290 	if (qctx) {
291 		/* Handle hard linked inodes */
292 		if (inode->i_links > 1) {
293 			if (dict_lookup(&qctx->linked_inode_dict,
294 				UINT_TO_VOIDPTR(ino))) {
295 				return;
296 			}
297 			dict_alloc_insert(&qctx->linked_inode_dict,
298 					UINT_TO_VOIDPTR(ino), NULL);
299 		}
300 
301 		qsize_t space = (inode->i_blocks - 1) * BLOCK_SZ;
302 		quota_data_add(qctx, inode, space);
303 		quota_data_inodes(qctx, inode, +1);
304 	}
305 }
306 
307 struct scan_dquots_data {
308 	dict_t		*quota_dict;
309 	int             update_limits; /* update limits from disk */
310 	int		update_usage;
311 	int		usage_is_inconsistent;
312 };
313 
scan_dquots_callback(struct dquot * dquot,void * cb_data)314 static int scan_dquots_callback(struct dquot *dquot, void *cb_data)
315 {
316 	struct scan_dquots_data *scan_data = cb_data;
317 	dict_t *quota_dict = scan_data->quota_dict;
318 	struct dquot *dq;
319 
320 	dq = get_dq(quota_dict, dquot->dq_id);
321 	dq->dq_id = dquot->dq_id;
322 	dq->dq_flags |= DQF_SEEN;
323 
324 	print_dquot("mem", dq);
325 	print_dquot("dsk", dquot);
326 	/* Check if there is inconsistency */
327 	if (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace ||
328 	    dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes) {
329 		scan_data->usage_is_inconsistent = 1;
330 		log_debug("[QUOTA WARNING] Usage inconsistent for ID %u:"
331 			"actual (%lld, %lld) != expected (%lld, %lld)\n",
332 				dq->dq_id, (long long) dq->dq_dqb.dqb_curspace,
333 				(long long) dq->dq_dqb.dqb_curinodes,
334 				(long long) dquot->dq_dqb.dqb_curspace,
335 				(long long) dquot->dq_dqb.dqb_curinodes);
336 	}
337 
338 	if (scan_data->update_limits) {
339 		dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
340 		dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
341 		dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
342 		dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
343 	}
344 
345 	if (scan_data->update_usage) {
346 		dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace;
347 		dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
348 	}
349 
350 	return 0;
351 }
352 
353 /*
354  * Compares the measured quota in qctx->quota_dict with that in the quota inode
355  * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is
356  * set to 1 if the supplied and on-disk quota usage values are not identical.
357  */
quota_compare_and_update(struct f2fs_sb_info * sbi,enum quota_type qtype,int * usage_inconsistent,int preserve_limits)358 errcode_t quota_compare_and_update(struct f2fs_sb_info *sbi,
359 		enum quota_type qtype, int *usage_inconsistent,
360 		int preserve_limits)
361 {
362 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
363 	quota_ctx_t qctx = fsck->qctx;
364 	struct quota_handle qh;
365 	struct scan_dquots_data scan_data;
366 	struct dquot *dq;
367 	dnode_t *n;
368 	dict_t *dict = qctx->quota_dict[qtype];
369 	errcode_t err = 0;
370 
371 	if (!dict)
372 		goto out;
373 
374 	err = quota_file_open(sbi, &qh, qtype, 0);
375 	if (err) {
376 		log_debug("Open quota file failed");
377 		goto out;
378 	}
379 
380 	scan_data.quota_dict = qctx->quota_dict[qtype];
381 	scan_data.update_limits = preserve_limits;
382 	scan_data.update_usage = 0;
383 	scan_data.usage_is_inconsistent = 0;
384 	err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data);
385 	if (err) {
386 		log_debug("Error scanning dquots");
387 		goto out;
388 	}
389 
390 	for (n = dict_first(dict); n; n = dict_next(dict, n)) {
391 		dq = dnode_get(n);
392 		if (!dq)
393 			continue;
394 		if ((dq->dq_flags & DQF_SEEN) == 0) {
395 			log_debug("[QUOTA WARNING] "
396 				"Missing quota entry ID %d\n", dq->dq_id);
397 			scan_data.usage_is_inconsistent = 1;
398 		}
399 	}
400 	*usage_inconsistent = scan_data.usage_is_inconsistent;
401 
402 out:
403 	return err;
404 }
405 
406