Whamcloud - gitweb
e2fsprogs: add quota library to e2fsprogs
[tools/e2fsprogs.git] / lib / quota / quotaio_v2.c
1 /*
2  * Implementation of new quotafile format
3  *
4  * Jan Kara <jack@suse.cz> - sponsored by SuSE CR
5  */
6
7 #include <sys/types.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <asm/byteorder.h>
14
15 #include "common.h"
16 #include "quotaio_v2.h"
17 #include "dqblk_v2.h"
18 #include "quotaio.h"
19 #include "quotaio_tree.h"
20
21 typedef char *dqbuf_t;
22
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,
31                                                 char *dqname));
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 #define getdqbuf() smalloc(V2_DQBLKSIZE)
46 #define freedqbuf(buf) free(buf)
47
48 /*
49  * Copy dquot from disk to memory
50  */
51 static void v2r1_disk2memdqblk(struct dquot *dquot, void *dp)
52 {
53         struct util_dqblk *m = &dquot->dq_dqb;
54         struct v2r1_disk_dqblk *d = dp, empty;
55
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);
65
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)))
69                 m->dqb_itime = 0;
70 }
71
72 /*
73  * Copy dquot from memory to disk
74  */
75 static void v2r1_mem2diskdqblk(void *dp, struct dquot *dquot)
76 {
77         struct util_dqblk *m = &dquot->dq_dqb;
78         struct v2r1_disk_dqblk *d = dp;
79
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);
91 }
92
93 static int v2r1_is_id(void *dp, struct dquot *dquot)
94 {
95         struct v2r1_disk_dqblk *d = dp;
96         struct qtree_mem_dqinfo *info =
97                         &dquot->dq_h->qh_info.u.v2_mdqi.dqi_qtree;
98
99         if (qtree_entry_unused(info, dp))
100                 return 0;
101         return __le32_to_cpu(d->dqb_id) == dquot->dq_id;
102 }
103
104 static struct qtree_fmt_operations v2r1_fmt_ops = {
105         .mem2disk_dqblk = v2r1_mem2diskdqblk,
106         .disk2mem_dqblk = v2r1_disk2memdqblk,
107         .is_id = v2r1_is_id,
108 };
109
110 /*
111  * Copy dqinfo from disk to memory
112  */
113 static inline void v2_disk2memdqinfo(struct util_dqinfo *m,
114                                      struct v2_disk_dqinfo *d)
115 {
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);
123 }
124
125 /*
126  * Copy dqinfo from memory to disk
127  */
128 static inline void v2_mem2diskdqinfo(struct v2_disk_dqinfo *d,
129                                      struct util_dqinfo *m)
130 {
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);
136         d->dqi_free_entry =
137                         __cpu_to_le32(m->u.v2_mdqi.dqi_qtree.dqi_free_entry);
138 }
139
140 /* Convert kernel quotablock format to utility one */
141 static inline void v2_kern2utildqblk(struct util_dqblk *u,
142                                      struct v2_kern_dqblk *k)
143 {
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;
152 }
153
154 /* Convert utility quotablock format to kernel one */
155 static inline void v2_util2kerndqblk(struct v2_kern_dqblk *k,
156                                      struct util_dqblk *u)
157 {
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;
166 }
167
168 static int v2_read_header(struct quota_handle *h, struct v2_disk_dqheader *dqh)
169 {
170         if (h->e2fs_read(&h->qh_qf, 0, dqh, sizeof(struct v2_disk_dqheader)) !=
171                         sizeof(struct v2_disk_dqheader))
172                 return 0;
173
174         return 1;
175 }
176
177 /*
178  * Check whether given quota file is in our format
179  */
180 static int v2_check_file(struct quota_handle *h, int type, int fmt)
181 {
182         struct v2_disk_dqheader dqh;
183         int file_magics[] = INITQMAGICS;
184         int known_versions[] = INIT_V2_VERSIONS;
185         int version;
186
187         if (!v2_read_header(h, &dqh))
188                 return 0;
189         if (fmt == QFMT_VFS_V0)
190                 version = 0;
191         else if (fmt == QFMT_VFS_V1)
192                 version = 1;
193         else
194                 return 0;
195
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 "
199                                   "endianity.", "");
200                 return 0;
201         }
202         if (__le32_to_cpu(dqh.dqh_version) > known_versions[type])
203                 return 0;
204         if (version != __le32_to_cpu(dqh.dqh_version))
205                 return 0;
206         return 1;
207 }
208
209 /*
210  * Open quotafile
211  */
212 static int v2_init_io(struct quota_handle *h)
213 {
214         log_err("Not Implemented.", "");
215         BUG_ON(1);
216         return 0;
217 }
218
219 /*
220  * Initialize new quotafile
221  */
222 static int v2_new_io(struct quota_handle *h)
223 {
224         int file_magics[] = INITQMAGICS;
225         struct v2_disk_dqheader ddqheader;
226         struct v2_disk_dqinfo ddqinfo;
227         int version = 1;
228
229         BUG_ON(h->qh_fmt != QFMT_VFS_V1);
230
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)) !=
235                         sizeof(ddqheader))
236                 return -1;
237
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,
250                           sizeof(ddqinfo)) !=
251             sizeof(ddqinfo))
252                 return -1;
253
254         return 0;
255 }
256
257 /*
258  * Write information (grace times to file)
259  */
260 static int v2_write_info(struct quota_handle *h)
261 {
262         struct v2_disk_dqinfo ddqinfo;
263
264         v2_mem2diskdqinfo(&ddqinfo, &h->qh_info);
265         if (h->e2fs_write(&h->qh_qf, V2_DQINFOOFF, &ddqinfo, sizeof(ddqinfo)) !=
266                         sizeof(ddqinfo))
267                 return -1;
268
269         return 0;
270 }
271
272 /*
273  * Read dquot (either from disk or from kernel)
274  * User can use errno to detect errstr when NULL is returned
275  */
276 static struct dquot *v2_read_dquot(struct quota_handle *h, qid_t id)
277 {
278         return qtree_read_dquot(h, id);
279 }
280
281 /*
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.
285  */
286 static int v2_commit_dquot(struct dquot *dquot, int flags)
287 {
288         struct util_dqblk *b = &dquot->dq_dqb;
289
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);
293         else
294                 qtree_write_dquot(dquot);
295         return 0;
296 }
297
298 static int v2_scan_dquots(struct quota_handle *h,
299                           int (*process_dquot) (struct dquot *, char *))
300 {
301         return qtree_scan_dquots(h, process_dquot);
302 }
303
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.
307  */
308 static int v2_report(struct quota_handle *h, int verbose)
309 {
310         log_err("Not Implemented.", "");
311         BUG_ON(1);
312         return 0;
313 }