2 * Implementation of new quotafile format
4 * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
16 #include "quotaio_v2.h"
19 #include "quotaio_tree.h"
21 typedef char *dqbuf_t;
23 static int v2_check_file(struct quota_handle *h, int type, int fmt);
24 static int v2_init_io(struct quota_handle *h);
25 static int v2_new_io(struct quota_handle *h);
26 static int v2_write_info(struct quota_handle *h);
27 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id);
28 static int v2_commit_dquot(struct dquot *dquot);
29 static int v2_scan_dquots(struct quota_handle *h,
30 int (*process_dquot) (struct dquot *dquot,
33 static int v2_report(struct quota_handle *h, int verbose);
35 struct quotafile_ops quotafile_ops_2 = {
36 .check_file = v2_check_file,
37 .init_io = v2_init_io,
39 .write_info = v2_write_info,
40 .read_dquot = v2_read_dquot,
41 .commit_dquot = v2_commit_dquot,
42 .scan_dquots = v2_scan_dquots,
46 #define getdqbuf() smalloc(V2_DQBLKSIZE)
47 #define freedqbuf(buf) free(buf)
50 * Copy dquot from disk to memory
52 static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
54 struct util_dqblk *m = &dquot->dq_dqb;
55 struct v2r1_disk_dqblk *d = dp, empty;
57 dquot->dq_id = ext2fs_le32_to_cpu(d->dqb_id);
58 m->dqb_ihardlimit = ext2fs_le64_to_cpu(d->dqb_ihardlimit);
59 m->dqb_isoftlimit = ext2fs_le64_to_cpu(d->dqb_isoftlimit);
60 m->dqb_bhardlimit = ext2fs_le64_to_cpu(d->dqb_bhardlimit);
61 m->dqb_bsoftlimit = ext2fs_le64_to_cpu(d->dqb_bsoftlimit);
62 m->dqb_curinodes = ext2fs_le64_to_cpu(d->dqb_curinodes);
63 m->dqb_curspace = ext2fs_le64_to_cpu(d->dqb_curspace);
64 m->dqb_itime = ext2fs_le64_to_cpu(d->dqb_itime);
65 m->dqb_btime = ext2fs_le64_to_cpu(d->dqb_btime);
67 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
68 empty.dqb_itime = ext2fs_cpu_to_le64(1);
69 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
74 * Copy dquot from memory to disk
76 static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
78 struct util_dqblk *m = &dquot->dq_dqb;
79 struct v2r1_disk_dqblk *d = dp;
81 d->dqb_ihardlimit = ext2fs_cpu_to_le64(m->dqb_ihardlimit);
82 d->dqb_isoftlimit = ext2fs_cpu_to_le64(m->dqb_isoftlimit);
83 d->dqb_bhardlimit = ext2fs_cpu_to_le64(m->dqb_bhardlimit);
84 d->dqb_bsoftlimit = ext2fs_cpu_to_le64(m->dqb_bsoftlimit);
85 d->dqb_curinodes = ext2fs_cpu_to_le64(m->dqb_curinodes);
86 d->dqb_curspace = ext2fs_cpu_to_le64(m->dqb_curspace);
87 d->dqb_itime = ext2fs_cpu_to_le64(m->dqb_itime);
88 d->dqb_btime = ext2fs_cpu_to_le64(m->dqb_btime);
89 d->dqb_id = ext2fs_cpu_to_le32(dquot->dq_id);
90 if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
91 d->dqb_itime = ext2fs_cpu_to_le64(1);
94 static int v2r1_is_id(void *dp, struct dquot *dquot)
96 struct v2r1_disk_dqblk *d = dp;
97 struct qtree_mem_dqinfo *info =
98 &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
100 if (qtree_entry_unused(info, dp))
102 return ext2fs_le32_to_cpu(d->dqb_id) == dquot->dq_id;
105 static struct qtree_fmt_operations v2r1_fmt_ops = {
106 .mem2disk_dqblk = v2r1_mem2diskdqblk,
107 .disk2mem_dqblk = v2r1_disk2memdqblk,
112 * Copy dqinfo from disk to memory
114 static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
115 struct v2_disk_dqinfo *d)
117 m->dqi_bgrace = ext2fs_le32_to_cpu(d->dqi_bgrace);
118 m->dqi_igrace = ext2fs_le32_to_cpu(d->dqi_igrace);
119 m->u.v2_mdqi.dqi_flags = ext2fs_le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
120 m->u.v2_mdqi.dqi_qtree.dqi_blocks = ext2fs_le32_to_cpu(d->dqi_blocks);
121 m->u.v2_mdqi.dqi_qtree.dqi_free_blk =
122 ext2fs_le32_to_cpu(d->dqi_free_blk);
123 m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
124 ext2fs_le32_to_cpu(d->dqi_free_entry);
128 * Copy dqinfo from memory to disk
130 static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
131 struct util_dqinfo *m)
133 d->dqi_bgrace = ext2fs_cpu_to_le32(m->dqi_bgrace);
134 d->dqi_igrace = ext2fs_cpu_to_le32(m->dqi_igrace);
135 d->dqi_flags = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
136 d->dqi_blocks = ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
138 ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
140 ext2fs_cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
143 /* Convert kernel quotablock format to utility one */
144 static inline void v2_kern2utildqblk(struct util_dqblk *u,
145 struct v2_kern_dqblk *k)
147 u->dqb_ihardlimit = k->dqb_ihardlimit;
148 u->dqb_isoftlimit = k->dqb_isoftlimit;
149 u->dqb_bhardlimit = k->dqb_bhardlimit;
150 u->dqb_bsoftlimit = k->dqb_bsoftlimit;
151 u->dqb_curinodes = k->dqb_curinodes;
152 u->dqb_curspace = k->dqb_curspace;
153 u->dqb_itime = k->dqb_itime;
154 u->dqb_btime = k->dqb_btime;
157 /* Convert utility quotablock format to kernel one */
158 static inline void v2_util2kerndqblk(struct v2_kern_dqblk *k,
159 struct util_dqblk *u)
161 k->dqb_ihardlimit = u->dqb_ihardlimit;
162 k->dqb_isoftlimit = u->dqb_isoftlimit;
163 k->dqb_bhardlimit = u->dqb_bhardlimit;
164 k->dqb_bsoftlimit = u->dqb_bsoftlimit;
165 k->dqb_curinodes = u->dqb_curinodes;
166 k->dqb_curspace = u->dqb_curspace;
167 k->dqb_itime = u->dqb_itime;
168 k->dqb_btime = u->dqb_btime;
171 static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
173 if (h->e2fs_read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
174 sizeof(struct v2_disk_dqheader))
181 * Check whether given quota file is in our format
183 static int v2_check_file(struct quota_handle *h, int type, int fmt)
185 struct v2_disk_dqheader dqh;
186 int file_magics[] = INITQMAGICS;
187 int known_versions[] = INIT_V2_VERSIONS;
190 if (!v2_read_header(h, &dqh))
192 if (fmt == QFMT_VFS_V0)
194 else if (fmt == QFMT_VFS_V1)
199 if (ext2fs_le32_to_cpu(dqh.dqh_magic) != file_magics[type]) {
200 if (ext2fs_be32_to_cpu(dqh.dqh_magic) == file_magics[type])
201 log_err("Your quota file is stored in wrong "
205 if (ext2fs_le32_to_cpu(dqh.dqh_version) > known_versions[type])
207 if (version != ext2fs_le32_to_cpu(dqh.dqh_version))
215 static int v2_init_io(struct quota_handle *h)
217 struct v2_disk_dqinfo ddqinfo;
219 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
220 sizeof(struct v2r1_disk_dqblk);
221 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
223 /* Read information about quotafile */
224 if (h->e2fs_read(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
225 sizeof(ddqinfo)) != sizeof(ddqinfo))
227 v2_disk2memdqinfo(&h->qh_info, &ddqinfo);
232 * Initialize new quotafile
234 static int v2_new_io(struct quota_handle *h)
236 int file_magics[] = INITQMAGICS;
237 struct v2_disk_dqheader ddqheader;
238 struct v2_disk_dqinfo ddqinfo;
241 if (h->qh_fmt != QFMT_VFS_V1)
244 /* Write basic quota header */
245 ddqheader.dqh_magic = ext2fs_cpu_to_le32(file_magics[h->qh_type]);
246 ddqheader.dqh_version = ext2fs_cpu_to_le32(version);
247 if (h->e2fs_write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
251 /* Write information about quotafile */
252 h->qh_info.dqi_bgrace = MAX_DQ_TIME;
253 h->qh_info.dqi_igrace = MAX_IQ_TIME;
254 h->qh_info.u.v2_mdqi.dqi_flags = 0;
255 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
256 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
257 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
258 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
259 sizeof(struct v2r1_disk_dqblk);
260 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
261 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
262 if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
271 * Write information (grace times to file)
273 static int v2_write_info(struct quota_handle *h)
275 struct v2_disk_dqinfo ddqinfo;
277 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
278 if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
286 * Read dquot from disk
288 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
290 return qtree_read_dquot(h, id);
294 * Commit changes of dquot to disk - it might also mean deleting it when quota
295 * became fake one and user has no blocks.
296 * User can process use 'errno' to detect errstr.
298 static int v2_commit_dquot(struct dquot *dquot)
300 struct util_dqblk *b = &dquot->dq_dqb;
302 if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
303 !b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
304 qtree_delete_dquot(dquot);
306 qtree_write_dquot(dquot);
310 static int v2_scan_dquots(struct quota_handle *h,
311 int (*process_dquot) (struct dquot *, void *),
314 return qtree_scan_dquots(h, process_dquot, data);
317 /* Report information about quotafile.
318 * TODO: Not used right now, but we should be able to use this when we add
319 * support to debugfs to read quota files.
321 static int v2_report(struct quota_handle *h, int verbose)
323 log_err("Not Implemented.", "");