1 /*
2 * Implementation of new quotafile format
3 *
4 * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
5 * Hyojun Kim <hyojun@google.com> - Ported to f2fs-tools
6 */
7
8 #include "config.h"
9 #include <sys/types.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "common.h"
17
18 #include "quotaio_v2.h"
19 #include "dqblk_v2.h"
20 #include "quotaio_tree.h"
21
22 static int v2_check_file(struct quota_handle *h, int type);
23 static int v2_init_io(struct quota_handle *h, enum quota_type qtype);
24 static int v2_new_io(struct quota_handle *h);
25 static int v2_write_info(struct quota_handle *h);
26 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id);
27 static int v2_commit_dquot(struct dquot *dquot);
28 static int v2_scan_dquots(struct quota_handle *h,
29 int (*process_dquot) (struct dquot *dquot,
30 void *data),
31 void *data);
32 static int v2_report(struct quota_handle *h, int verbose);
33
34 struct quotafile_ops quotafile_ops_2 = {
35 .check_file = v2_check_file,
36 .init_io = v2_init_io,
37 .new_io = v2_new_io,
38 .write_info = v2_write_info,
39 .read_dquot = v2_read_dquot,
40 .commit_dquot = v2_commit_dquot,
41 .scan_dquots = v2_scan_dquots,
42 .report = v2_report,
43 };
44
45 /*
46 * Copy dquot from disk to memory
47 */
v2r1_disk2memdqblk(struct dquot * dquot,void * dp)48 static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
49 {
50 struct util_dqblk *m = &dquot->dq_dqb;
51 struct v2r1_disk_dqblk *d = dp, empty;
52
53 dquot->dq_id = le32_to_cpu(d->dqb_id);
54 m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
55 m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
56 m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
57 m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
58 m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
59 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
60 m->dqb_itime = le64_to_cpu(d->dqb_itime);
61 m->dqb_btime = le64_to_cpu(d->dqb_btime);
62
63 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
64 empty.dqb_itime = cpu_to_le64(1);
65 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
66 m->dqb_itime = 0;
67 }
68
69 /*
70 * Copy dquot from memory to disk
71 */
v2r1_mem2diskdqblk(void * dp,struct dquot * dquot)72 static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
73 {
74 struct util_dqblk *m = &dquot->dq_dqb;
75 struct v2r1_disk_dqblk *d = dp;
76
77 d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
78 d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
79 d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
80 d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
81 d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
82 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
83 d->dqb_itime = cpu_to_le64(m->dqb_itime);
84 d->dqb_btime = cpu_to_le64(m->dqb_btime);
85 d->dqb_id = cpu_to_le32(dquot->dq_id);
86 if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
87 d->dqb_itime = cpu_to_le64(1);
88 }
89
v2r1_is_id(void * dp,struct dquot * dquot)90 static int v2r1_is_id(void *dp, struct dquot *dquot)
91 {
92 struct v2r1_disk_dqblk *d = dp;
93 struct qtree_mem_dqinfo *info =
94 &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
95
96 if (qtree_entry_unused(info, dp))
97 return 0;
98 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
99 }
100
101 static struct qtree_fmt_operations v2r1_fmt_ops = {
102 .mem2disk_dqblk = v2r1_mem2diskdqblk,
103 .disk2mem_dqblk = v2r1_disk2memdqblk,
104 .is_id = v2r1_is_id,
105 };
106
107 /*
108 * Copy dqinfo from disk to memory
109 */
v2_disk2memdqinfo(struct util_dqinfo * m,struct v2_disk_dqinfo * d)110 static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
111 struct v2_disk_dqinfo *d)
112 {
113 m->dqi_bgrace = le32_to_cpu(d->dqi_bgrace);
114 m->dqi_igrace = le32_to_cpu(d->dqi_igrace);
115 m->u.v2_mdqi.dqi_flags = le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
116 m->u.v2_mdqi.dqi_qtree.dqi_blocks = le32_to_cpu(d->dqi_blocks);
117 m->u.v2_mdqi.dqi_qtree.dqi_free_blk =
118 le32_to_cpu(d->dqi_free_blk);
119 m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
120 le32_to_cpu(d->dqi_free_entry);
121 }
122
123 /*
124 * Copy dqinfo from memory to disk
125 */
v2_mem2diskdqinfo(struct v2_disk_dqinfo * d,struct util_dqinfo * m)126 static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
127 struct util_dqinfo *m)
128 {
129 d->dqi_bgrace = cpu_to_le32(m->dqi_bgrace);
130 d->dqi_igrace = cpu_to_le32(m->dqi_igrace);
131 d->dqi_flags = cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
132 d->dqi_blocks = cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
133 d->dqi_free_blk =
134 cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
135 d->dqi_free_entry =
136 cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
137 }
138
v2_read_header(struct quota_handle * h,struct v2_disk_dqheader * dqh)139 static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
140 {
141 if (h->read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
142 sizeof(struct v2_disk_dqheader))
143 return 0;
144
145 return 1;
146 }
147
148 /*
149 * Check whether given quota file is in our format
150 */
v2_check_file(struct quota_handle * h,int type)151 static int v2_check_file(struct quota_handle *h, int type)
152 {
153 struct v2_disk_dqheader dqh;
154 int file_magics[] = INITQMAGICS;
155 int be_magic;
156
157 if (!v2_read_header(h, &dqh))
158 return 0;
159
160 be_magic = be32_to_cpu((__force __be32)dqh.dqh_magic);
161 if (be_magic == file_magics[type]) {
162 log_err("Your quota file is stored in wrong endianity");
163 return 0;
164 }
165 if (V2_VERSION != le32_to_cpu(dqh.dqh_version))
166 return 0;
167 return 1;
168 }
169
170 /*
171 * Open quotafile
172 */
v2_init_io(struct quota_handle * h,enum quota_type qtype)173 static int v2_init_io(struct quota_handle *h, enum quota_type qtype)
174 {
175 struct v2_disk_dqinfo ddqinfo;
176 struct v2_mem_dqinfo *info;
177 u64 filesize;
178 struct quota_file *qf = &h->qh_qf;
179 u32 last_blkofs = qf_last_blkofs[qtype];
180
181 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
182 sizeof(struct v2r1_disk_dqblk);
183 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
184
185 /* Read information about quotafile */
186 if (h->read(qf, V2_DQINFOOFF, &ddqinfo,
187 sizeof(ddqinfo)) != sizeof(ddqinfo))
188 return -1;
189 v2_disk2memdqinfo(&h->qh_info, &ddqinfo);
190
191 /* Check to make sure quota file info is sane */
192 info = &h->qh_info.u.v2_mdqi;
193 filesize = qf->filesize = f2fs_quota_size(qf);
194 if (qf_szchk_type[qtype] == QF_SZCHK_REGFILE &&
195 ((filesize + F2FS_BLKSIZE - 1) >> F2FS_BLKSIZE_BITS <
196 last_blkofs + 1 || filesize > qf_maxsize[qtype])) {
197 /*
198 * reqular: qf_szchk is now the last block index,
199 * including the hole's index
200 */
201 log_err("Quota inode %u corrupted: file size %" PRIu64
202 " does not match page offset %" PRIu32,
203 h->qh_qf.ino,
204 filesize,
205 last_blkofs);
206 filesize = (last_blkofs + 1) << F2FS_BLKSIZE_BITS;
207 f2fs_filesize_update(qf->sbi, qf->ino, filesize);
208 }
209
210 if ((info->dqi_qtree.dqi_blocks >
211 (filesize + QT_BLKSIZE - 1) >> QT_BLKSIZE_BITS)) {
212 log_err("Quota inode %u corrupted: file size %" PRId64 "; "
213 "dqi_blocks %u", h->qh_qf.ino,
214 filesize, info->dqi_qtree.dqi_blocks);
215 return -1;
216 }
217 if (info->dqi_qtree.dqi_free_blk >= info->dqi_qtree.dqi_blocks) {
218 log_err("Quota inode %u corrupted: free_blk %u;"
219 " dqi_blocks %u",
220 h->qh_qf.ino, info->dqi_qtree.dqi_free_blk,
221 info->dqi_qtree.dqi_blocks);
222 return -1;
223 }
224 if (info->dqi_qtree.dqi_free_entry >= info->dqi_qtree.dqi_blocks) {
225 log_err("Quota inode %u corrupted: free_entry %u; "
226 "dqi_blocks %u", h->qh_qf.ino,
227 info->dqi_qtree.dqi_free_entry,
228 info->dqi_qtree.dqi_blocks);
229 return -1;
230 }
231 return 0;
232 }
233
234 /*
235 * Initialize new quotafile
236 */
v2_new_io(struct quota_handle * h)237 static int v2_new_io(struct quota_handle *h)
238 {
239 int file_magics[] = INITQMAGICS;
240 struct v2_disk_dqheader ddqheader;
241 struct v2_disk_dqinfo ddqinfo;
242
243 if (h->qh_fmt != QFMT_VFS_V1)
244 return -1;
245
246 /* Write basic quota header */
247 ddqheader.dqh_magic = cpu_to_le32(file_magics[h->qh_type]);
248 ddqheader.dqh_version = cpu_to_le32(V2_VERSION);
249 if (h->write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
250 sizeof(ddqheader))
251 return -1;
252
253 /* Write information about quotafile */
254 h->qh_info.dqi_bgrace = MAX_DQ_TIME;
255 h->qh_info.dqi_igrace = MAX_IQ_TIME;
256 h->qh_info.u.v2_mdqi.dqi_flags = 0;
257 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
258 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
259 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
260 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
261 sizeof(struct v2r1_disk_dqblk);
262 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
263 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
264 if (h->write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
265 sizeof(ddqinfo)) !=
266 sizeof(ddqinfo))
267 return -1;
268
269 return 0;
270 }
271
272 /*
273 * Write information (grace times to file)
274 */
v2_write_info(struct quota_handle * h)275 static int v2_write_info(struct quota_handle *h)
276 {
277 struct v2_disk_dqinfo ddqinfo;
278
279 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
280 if (h->write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
281 sizeof(ddqinfo))
282 return -1;
283
284 return 0;
285 }
286
287 /*
288 * Read dquot from disk
289 */
v2_read_dquot(struct quota_handle * h,qid_t id)290 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
291 {
292 return qtree_read_dquot(h, id);
293 }
294
295 /*
296 * Commit changes of dquot to disk - it might also mean deleting it when quota
297 * became fake one and user has no blocks.
298 * User can process use 'errno' to detect errstr.
299 */
v2_commit_dquot(struct dquot * dquot)300 static int v2_commit_dquot(struct dquot *dquot)
301 {
302 struct util_dqblk *b = &dquot->dq_dqb;
303
304 if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
305 !b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
306 {
307 qtree_delete_dquot(dquot);
308 } else {
309 return qtree_write_dquot(dquot);
310 }
311 return 0;
312 }
313
v2_scan_dquots(struct quota_handle * h,int (* process_dquot)(struct dquot *,void *),void * data)314 static int v2_scan_dquots(struct quota_handle *h,
315 int (*process_dquot) (struct dquot *, void *),
316 void *data)
317 {
318 return qtree_scan_dquots(h, process_dquot, data);
319 }
320
321 /* Report information about quotafile.
322 * TODO: Not used right now, but we should be able to use this when we add
323 * support to debugfs to read quota files.
324 */
v2_report(struct quota_handle * UNUSED (h),int UNUSED (verbose))325 static int v2_report(struct quota_handle *UNUSED(h), int UNUSED(verbose))
326 {
327 log_err("Not Implemented.");
328 return -1;
329 }
330