2 * Implementation of new quotafile format
4 * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
13 #include <asm/byteorder.h>
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, int flags);
29 static int v2_scan_dquots(struct quota_handle *h,
30 int (*process_dquot) (struct dquot *dquot,
32 static int v2_report(struct quota_handle *h, int verbose);
34 struct quotafile_ops quotafile_ops_2 = {
35 check_file: v2_check_file,
38 write_info: v2_write_info,
39 read_dquot: v2_read_dquot,
40 commit_dquot: v2_commit_dquot,
41 scan_dquots: v2_scan_dquots,
45 #define getdqbuf() smalloc(V2_DQBLKSIZE)
46 #define freedqbuf(buf) free(buf)
49 * Copy dquot from disk to memory
51 static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
53 struct util_dqblk *m = &dquot->dq_dqb;
54 struct v2r1_disk_dqblk *d = dp, empty;
56 dquot->dq_id = __le32_to_cpu(d->dqb_id);
57 m->dqb_ihardlimit = __le64_to_cpu(d->dqb_ihardlimit);
58 m->dqb_isoftlimit = __le64_to_cpu(d->dqb_isoftlimit);
59 m->dqb_bhardlimit = __le64_to_cpu(d->dqb_bhardlimit);
60 m->dqb_bsoftlimit = __le64_to_cpu(d->dqb_bsoftlimit);
61 m->dqb_curinodes = __le64_to_cpu(d->dqb_curinodes);
62 m->dqb_curspace = __le64_to_cpu(d->dqb_curspace);
63 m->dqb_itime = __le64_to_cpu(d->dqb_itime);
64 m->dqb_btime = __le64_to_cpu(d->dqb_btime);
66 memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
67 empty.dqb_itime = __cpu_to_le64(1);
68 if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
73 * Copy dquot from memory to disk
75 static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
77 struct util_dqblk *m = &dquot->dq_dqb;
78 struct v2r1_disk_dqblk *d = dp;
80 d->dqb_ihardlimit = __cpu_to_le64(m->dqb_ihardlimit);
81 d->dqb_isoftlimit = __cpu_to_le64(m->dqb_isoftlimit);
82 d->dqb_bhardlimit = __cpu_to_le64(m->dqb_bhardlimit);
83 d->dqb_bsoftlimit = __cpu_to_le64(m->dqb_bsoftlimit);
84 d->dqb_curinodes = __cpu_to_le64(m->dqb_curinodes);
85 d->dqb_curspace = __cpu_to_le64(m->dqb_curspace);
86 d->dqb_itime = __cpu_to_le64(m->dqb_itime);
87 d->dqb_btime = __cpu_to_le64(m->dqb_btime);
88 d->dqb_id = __cpu_to_le32(dquot->dq_id);
89 if (qtree_entry_unused(&dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree, dp))
90 d->dqb_itime = __cpu_to_le64(1);
93 static int v2r1_is_id(void *dp, struct dquot *dquot)
95 struct v2r1_disk_dqblk *d = dp;
96 struct qtree_mem_dqinfo *info =
97 &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
99 if (qtree_entry_unused(info, dp))
101 return __le32_to_cpu(d->dqb_id) == dquot->dq_id;
104 static struct qtree_fmt_operations v2r1_fmt_ops = {
105 .mem2disk_dqblk = v2r1_mem2diskdqblk,
106 .disk2mem_dqblk = v2r1_disk2memdqblk,
111 * Copy dqinfo from disk to memory
113 static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
114 struct v2_disk_dqinfo *d)
116 m->dqi_bgrace = __le32_to_cpu(d->dqi_bgrace);
117 m->dqi_igrace = __le32_to_cpu(d->dqi_igrace);
118 m->u.v2_mdqi.dqi_flags = __le32_to_cpu(d->dqi_flags) & V2_DQF_MASK;
119 m->u.v2_mdqi.dqi_qtree.dqi_blocks = __le32_to_cpu(d->dqi_blocks);
120 m->u.v2_mdqi.dqi_qtree.dqi_free_blk = __le32_to_cpu(d->dqi_free_blk);
121 m->u.v2_mdqi.dqi_qtree.dqi_free_entry =
122 __le32_to_cpu(d->dqi_free_entry);
126 * Copy dqinfo from memory to disk
128 static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
129 struct util_dqinfo *m)
131 d->dqi_bgrace = __cpu_to_le32(m->dqi_bgrace);
132 d->dqi_igrace = __cpu_to_le32(m->dqi_igrace);
133 d->dqi_flags = __cpu_to_le32(m->u.v2_mdqi.dqi_flags & V2_DQF_MASK);
134 d->dqi_blocks = __cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_blocks);
135 d->dqi_free_blk = __cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_blk);
137 __cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
140 /* Convert kernel quotablock format to utility one */
141 static inline void v2_kern2utildqblk(struct util_dqblk *u,
142 struct v2_kern_dqblk *k)
144 u->dqb_ihardlimit = k->dqb_ihardlimit;
145 u->dqb_isoftlimit = k->dqb_isoftlimit;
146 u->dqb_bhardlimit = k->dqb_bhardlimit;
147 u->dqb_bsoftlimit = k->dqb_bsoftlimit;
148 u->dqb_curinodes = k->dqb_curinodes;
149 u->dqb_curspace = k->dqb_curspace;
150 u->dqb_itime = k->dqb_itime;
151 u->dqb_btime = k->dqb_btime;
154 /* Convert utility quotablock format to kernel one */
155 static inline void v2_util2kerndqblk(struct v2_kern_dqblk *k,
156 struct util_dqblk *u)
158 k->dqb_ihardlimit = u->dqb_ihardlimit;
159 k->dqb_isoftlimit = u->dqb_isoftlimit;
160 k->dqb_bhardlimit = u->dqb_bhardlimit;
161 k->dqb_bsoftlimit = u->dqb_bsoftlimit;
162 k->dqb_curinodes = u->dqb_curinodes;
163 k->dqb_curspace = u->dqb_curspace;
164 k->dqb_itime = u->dqb_itime;
165 k->dqb_btime = u->dqb_btime;
168 static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
170 if (h->e2fs_read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
171 sizeof(struct v2_disk_dqheader))
178 * Check whether given quota file is in our format
180 static int v2_check_file(struct quota_handle *h, int type, int fmt)
182 struct v2_disk_dqheader dqh;
183 int file_magics[] = INITQMAGICS;
184 int known_versions[] = INIT_V2_VERSIONS;
187 if (!v2_read_header(h, &dqh))
189 if (fmt == QFMT_VFS_V0)
191 else if (fmt == QFMT_VFS_V1)
196 if (__le32_to_cpu(dqh.dqh_magic) != file_magics[type]) {
197 if (__be32_to_cpu(dqh.dqh_magic) == file_magics[type])
198 log_fatal(3, "Your quota file is stored in wrong "
202 if (__le32_to_cpu(dqh.dqh_version) > known_versions[type])
204 if (version != __le32_to_cpu(dqh.dqh_version))
212 static int v2_init_io(struct quota_handle *h)
214 log_err("Not Implemented.", "");
220 * Initialize new quotafile
222 static int v2_new_io(struct quota_handle *h)
224 int file_magics[] = INITQMAGICS;
225 struct v2_disk_dqheader ddqheader;
226 struct v2_disk_dqinfo ddqinfo;
229 BUG_ON(h->qh_fmt != QFMT_VFS_V1);
231 /* Write basic quota header */
232 ddqheader.dqh_magic = __cpu_to_le32(file_magics[h->qh_type]);
233 ddqheader.dqh_version = __cpu_to_le32(version);
234 if (h->e2fs_write(&h->qh_qf, 0, &ddqheader, sizeof(ddqheader)) !=
238 /* Write information about quotafile */
239 h->qh_info.dqi_bgrace = MAX_DQ_TIME;
240 h->qh_info.dqi_igrace = MAX_IQ_TIME;
241 h->qh_info.u.v2_mdqi.dqi_flags = 0;
242 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_blocks = QT_TREEOFF + 1;
243 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_blk = 0;
244 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_free_entry = 0;
245 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_entry_size =
246 sizeof(struct v2r1_disk_dqblk);
247 h->qh_info.u.v2_mdqi.dqi_qtree.dqi_ops = &v2r1_fmt_ops;
248 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
249 if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo,
258 * Write information (grace times to file)
260 static int v2_write_info(struct quota_handle *h)
262 struct v2_disk_dqinfo ddqinfo;
264 v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
265 if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
273 * Read dquot (either from disk or from kernel)
274 * User can use errno to detect errstr when NULL is returned
276 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
278 return qtree_read_dquot(h, id);
282 * Commit changes of dquot to disk - it might also mean deleting it when quota
283 * became fake one and user has no blocks.
284 * User can process use 'errno' to detect errstr.
286 static int v2_commit_dquot(struct dquot *dquot, int flags)
288 struct util_dqblk *b = &dquot->dq_dqb;
290 if (!b->dqb_curspace && !b->dqb_curinodes && !b->dqb_bsoftlimit &&
291 !b->dqb_isoftlimit && !b->dqb_bhardlimit && !b->dqb_ihardlimit)
292 qtree_delete_dquot(dquot);
294 qtree_write_dquot(dquot);
298 static int v2_scan_dquots(struct quota_handle *h,
299 int (*process_dquot) (struct dquot *, char *))
301 return qtree_scan_dquots(h, process_dquot);
304 /* Report information about quotafile.
305 * TODO: Not used right now, but we should be able to use this when we add
306 * support to debugfs to read quota files.
308 static int v2_report(struct quota_handle *h, int verbose)
310 log_err("Not Implemented.", "");