1 /*
2 * vfsv0 quota IO operations on file
3 */
4
5 #include <linux/errno.h>
6 #include <linux/fs.h>
7 #include <linux/dqblk_v2.h>
8 #include <linux/quotaio_v2.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13
14 #include <asm/byteorder.h>
15 #include <asm/uaccess.h>
16
17 MODULE_AUTHOR("Jan Kara");
18 MODULE_DESCRIPTION("Quota format v2 support");
19 MODULE_LICENSE("GPL");
20
21 #define __QUOTA_V2_PARANOIA
22
23 typedef char *dqbuf_t;
24
25 #define GETIDINDEX(id, depth) (((id) >> ((V2_DQTREEDEPTH-(depth)-1)*8)) & 0xff)
26 #define GETENTRIES(buf) ((struct v2_disk_dqblk *)(((char *)buf)+sizeof(struct v2_disk_dqdbheader)))
27
28 /* Check whether given file is really vfsv0 quotafile */
v2_check_quota_file(struct super_block * sb,int type)29 static int v2_check_quota_file(struct super_block *sb, int type)
30 {
31 struct v2_disk_dqheader dqhead;
32 struct file *f = sb_dqopt(sb)->files[type];
33 mm_segment_t fs;
34 ssize_t size;
35 loff_t offset = 0;
36 static const uint quota_magics[] = V2_INITQMAGICS;
37 static const uint quota_versions[] = V2_INITQVERSIONS;
38
39 fs = get_fs();
40 set_fs(KERNEL_DS);
41 size = f->f_op->read(f, (char *)&dqhead, sizeof(struct v2_disk_dqheader), &offset);
42 set_fs(fs);
43 if (size != sizeof(struct v2_disk_dqheader))
44 return 0;
45 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
46 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
47 return 0;
48 return 1;
49 }
50
51 /* Read information header from quota file */
v2_read_file_info(struct super_block * sb,int type)52 static int v2_read_file_info(struct super_block *sb, int type)
53 {
54 mm_segment_t fs;
55 struct v2_disk_dqinfo dinfo;
56 struct mem_dqinfo *info = sb_dqopt(sb)->info+type;
57 struct file *f = sb_dqopt(sb)->files[type];
58 ssize_t size;
59 loff_t offset = V2_DQINFOOFF;
60
61 fs = get_fs();
62 set_fs(KERNEL_DS);
63 size = f->f_op->read(f, (char *)&dinfo, sizeof(struct v2_disk_dqinfo), &offset);
64 set_fs(fs);
65 if (size != sizeof(struct v2_disk_dqinfo)) {
66 printk(KERN_WARNING "Can't read info structure on device %s.\n",
67 kdevname(f->f_dentry->d_sb->s_dev));
68 return -1;
69 }
70 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
71 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
72 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
73 info->u.v2_i.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
74 info->u.v2_i.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
75 info->u.v2_i.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
76 return 0;
77 }
78
79 /* Write information header to quota file */
v2_write_file_info(struct super_block * sb,int type)80 static int v2_write_file_info(struct super_block *sb, int type)
81 {
82 mm_segment_t fs;
83 struct v2_disk_dqinfo dinfo;
84 struct mem_dqinfo *info = sb_dqopt(sb)->info+type;
85 struct file *f = sb_dqopt(sb)->files[type];
86 ssize_t size;
87 loff_t offset = V2_DQINFOOFF;
88
89 info->dqi_flags &= ~DQF_INFO_DIRTY;
90 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
91 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
92 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
93 dinfo.dqi_blocks = cpu_to_le32(info->u.v2_i.dqi_blocks);
94 dinfo.dqi_free_blk = cpu_to_le32(info->u.v2_i.dqi_free_blk);
95 dinfo.dqi_free_entry = cpu_to_le32(info->u.v2_i.dqi_free_entry);
96 fs = get_fs();
97 set_fs(KERNEL_DS);
98 size = f->f_op->write(f, (char *)&dinfo, sizeof(struct v2_disk_dqinfo), &offset);
99 set_fs(fs);
100 if (size != sizeof(struct v2_disk_dqinfo)) {
101 printk(KERN_WARNING "Can't write info structure on device %s.\n",
102 kdevname(f->f_dentry->d_sb->s_dev));
103 return -1;
104 }
105 return 0;
106 }
107
disk2memdqb(struct mem_dqblk * m,struct v2_disk_dqblk * d)108 static void disk2memdqb(struct mem_dqblk *m, struct v2_disk_dqblk *d)
109 {
110 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
111 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
112 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
113 m->dqb_itime = le64_to_cpu(d->dqb_itime);
114 m->dqb_bhardlimit = le32_to_cpu(d->dqb_bhardlimit);
115 m->dqb_bsoftlimit = le32_to_cpu(d->dqb_bsoftlimit);
116 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
117 m->dqb_btime = le64_to_cpu(d->dqb_btime);
118 }
119
mem2diskdqb(struct v2_disk_dqblk * d,struct mem_dqblk * m,qid_t id)120 static void mem2diskdqb(struct v2_disk_dqblk *d, struct mem_dqblk *m, qid_t id)
121 {
122 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
123 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
124 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
125 d->dqb_itime = cpu_to_le64(m->dqb_itime);
126 d->dqb_bhardlimit = cpu_to_le32(m->dqb_bhardlimit);
127 d->dqb_bsoftlimit = cpu_to_le32(m->dqb_bsoftlimit);
128 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
129 d->dqb_btime = cpu_to_le64(m->dqb_btime);
130 d->dqb_id = cpu_to_le32(id);
131 }
132
getdqbuf(void)133 static dqbuf_t getdqbuf(void)
134 {
135 dqbuf_t buf = kmalloc(V2_DQBLKSIZE, GFP_KERNEL);
136 if (!buf)
137 printk(KERN_WARNING "VFS: Not enough memory for quota buffers.\n");
138 return buf;
139 }
140
freedqbuf(dqbuf_t buf)141 static inline void freedqbuf(dqbuf_t buf)
142 {
143 kfree(buf);
144 }
145
read_blk(struct file * filp,uint blk,dqbuf_t buf)146 static ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
147 {
148 mm_segment_t fs;
149 ssize_t ret;
150 loff_t offset = blk<<V2_DQBLKSIZE_BITS;
151
152 memset(buf, 0, V2_DQBLKSIZE);
153 fs = get_fs();
154 set_fs(KERNEL_DS);
155 ret = filp->f_op->read(filp, (char *)buf, V2_DQBLKSIZE, &offset);
156 set_fs(fs);
157 return ret;
158 }
159
write_blk(struct file * filp,uint blk,dqbuf_t buf)160 static ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
161 {
162 mm_segment_t fs;
163 ssize_t ret;
164 loff_t offset = blk<<V2_DQBLKSIZE_BITS;
165
166 fs = get_fs();
167 set_fs(KERNEL_DS);
168 ret = filp->f_op->write(filp, (char *)buf, V2_DQBLKSIZE, &offset);
169 set_fs(fs);
170 return ret;
171
172 }
173
174 /* Remove empty block from list and return it */
get_free_dqblk(struct file * filp,struct mem_dqinfo * info)175 static int get_free_dqblk(struct file *filp, struct mem_dqinfo *info)
176 {
177 dqbuf_t buf = getdqbuf();
178 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
179 int ret, blk;
180
181 if (!buf)
182 return -ENOMEM;
183 if (info->u.v2_i.dqi_free_blk) {
184 blk = info->u.v2_i.dqi_free_blk;
185 if ((ret = read_blk(filp, blk, buf)) < 0)
186 goto out_buf;
187 info->u.v2_i.dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
188 }
189 else {
190 memset(buf, 0, V2_DQBLKSIZE);
191 if ((ret = write_blk(filp, info->u.v2_i.dqi_blocks, buf)) < 0) /* Assure block allocation... */
192 goto out_buf;
193 blk = info->u.v2_i.dqi_blocks++;
194 }
195 mark_info_dirty(info);
196 ret = blk;
197 out_buf:
198 freedqbuf(buf);
199 return ret;
200 }
201
202 /* Insert empty block to the list */
put_free_dqblk(struct file * filp,struct mem_dqinfo * info,dqbuf_t buf,uint blk)203 static int put_free_dqblk(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk)
204 {
205 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
206 int err;
207
208 dh->dqdh_next_free = cpu_to_le32(info->u.v2_i.dqi_free_blk);
209 dh->dqdh_prev_free = cpu_to_le32(0);
210 dh->dqdh_entries = cpu_to_le16(0);
211 info->u.v2_i.dqi_free_blk = blk;
212 mark_info_dirty(info);
213 if ((err = write_blk(filp, blk, buf)) < 0) /* Some strange block. We had better leave it... */
214 return err;
215 return 0;
216 }
217
218 /* Remove given block from the list of blocks with free entries */
remove_free_dqentry(struct file * filp,struct mem_dqinfo * info,dqbuf_t buf,uint blk)219 static int remove_free_dqentry(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk)
220 {
221 dqbuf_t tmpbuf = getdqbuf();
222 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
223 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk = le32_to_cpu(dh->dqdh_prev_free);
224 int err;
225
226 if (!tmpbuf)
227 return -ENOMEM;
228 if (nextblk) {
229 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
230 goto out_buf;
231 ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_prev_free = dh->dqdh_prev_free;
232 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
233 goto out_buf;
234 }
235 if (prevblk) {
236 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
237 goto out_buf;
238 ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_next_free = dh->dqdh_next_free;
239 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
240 goto out_buf;
241 }
242 else {
243 info->u.v2_i.dqi_free_entry = nextblk;
244 mark_info_dirty(info);
245 }
246 freedqbuf(tmpbuf);
247 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
248 if (write_blk(filp, blk, buf) < 0) /* No matter whether write succeeds block is out of list */
249 printk(KERN_ERR "VFS: Can't write block (%u) with free entries.\n", blk);
250 return 0;
251 out_buf:
252 freedqbuf(tmpbuf);
253 return err;
254 }
255
256 /* Insert given block to the beginning of list with free entries */
insert_free_dqentry(struct file * filp,struct mem_dqinfo * info,dqbuf_t buf,uint blk)257 static int insert_free_dqentry(struct file *filp, struct mem_dqinfo *info, dqbuf_t buf, uint blk)
258 {
259 dqbuf_t tmpbuf = getdqbuf();
260 struct v2_disk_dqdbheader *dh = (struct v2_disk_dqdbheader *)buf;
261 int err;
262
263 if (!tmpbuf)
264 return -ENOMEM;
265 dh->dqdh_next_free = cpu_to_le32(info->u.v2_i.dqi_free_entry);
266 dh->dqdh_prev_free = cpu_to_le32(0);
267 if ((err = write_blk(filp, blk, buf)) < 0)
268 goto out_buf;
269 if (info->u.v2_i.dqi_free_entry) {
270 if ((err = read_blk(filp, info->u.v2_i.dqi_free_entry, tmpbuf)) < 0)
271 goto out_buf;
272 ((struct v2_disk_dqdbheader *)tmpbuf)->dqdh_prev_free = cpu_to_le32(blk);
273 if ((err = write_blk(filp, info->u.v2_i.dqi_free_entry, tmpbuf)) < 0)
274 goto out_buf;
275 }
276 freedqbuf(tmpbuf);
277 info->u.v2_i.dqi_free_entry = blk;
278 mark_info_dirty(info);
279 return 0;
280 out_buf:
281 freedqbuf(tmpbuf);
282 return err;
283 }
284
285 /* Find space for dquot */
find_free_dqentry(struct dquot * dquot,int * err)286 static uint find_free_dqentry(struct dquot *dquot, int *err)
287 {
288 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
289 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info+dquot->dq_type;
290 uint blk, i;
291 struct v2_disk_dqdbheader *dh;
292 struct v2_disk_dqblk *ddquot;
293 struct v2_disk_dqblk fakedquot;
294 dqbuf_t buf;
295
296 *err = 0;
297 if (!(buf = getdqbuf())) {
298 *err = -ENOMEM;
299 return 0;
300 }
301 dh = (struct v2_disk_dqdbheader *)buf;
302 ddquot = GETENTRIES(buf);
303 if (info->u.v2_i.dqi_free_entry) {
304 blk = info->u.v2_i.dqi_free_entry;
305 if ((*err = read_blk(filp, blk, buf)) < 0)
306 goto out_buf;
307 }
308 else {
309 blk = get_free_dqblk(filp, info);
310 if ((int)blk < 0) {
311 *err = blk;
312 return 0;
313 }
314 memset(buf, 0, V2_DQBLKSIZE);
315 info->u.v2_i.dqi_free_entry = blk; /* This is enough as block is already zeroed and entry list is empty... */
316 mark_info_dirty(info);
317 }
318 if (le16_to_cpu(dh->dqdh_entries)+1 >= V2_DQSTRINBLK) /* Block will be full? */
319 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
320 printk(KERN_ERR "VFS: find_free_dqentry(): Can't remove block (%u) from entry free list.\n", blk);
321 goto out_buf;
322 }
323 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries)+1);
324 memset(&fakedquot, 0, sizeof(struct v2_disk_dqblk));
325 /* Find free structure in block */
326 for (i = 0; i < V2_DQSTRINBLK && memcmp(&fakedquot, ddquot+i, sizeof(struct v2_disk_dqblk)); i++);
327 #ifdef __QUOTA_V2_PARANOIA
328 if (i == V2_DQSTRINBLK) {
329 printk(KERN_ERR "VFS: find_free_dqentry(): Data block full but it shouldn't.\n");
330 *err = -EIO;
331 goto out_buf;
332 }
333 #endif
334 if ((*err = write_blk(filp, blk, buf)) < 0) {
335 printk(KERN_ERR "VFS: find_free_dqentry(): Can't write quota data block %u.\n", blk);
336 goto out_buf;
337 }
338 dquot->dq_off = (blk<<V2_DQBLKSIZE_BITS)+sizeof(struct v2_disk_dqdbheader)+i*sizeof(struct v2_disk_dqblk);
339 freedqbuf(buf);
340 return blk;
341 out_buf:
342 freedqbuf(buf);
343 return 0;
344 }
345
346 /* Insert reference to structure into the trie */
do_insert_tree(struct dquot * dquot,uint * treeblk,int depth)347 static int do_insert_tree(struct dquot *dquot, uint *treeblk, int depth)
348 {
349 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
350 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type;
351 dqbuf_t buf;
352 int ret = 0, newson = 0, newact = 0;
353 u32 *ref;
354 uint newblk;
355
356 if (!(buf = getdqbuf()))
357 return -ENOMEM;
358 if (!*treeblk) {
359 ret = get_free_dqblk(filp, info);
360 if (ret < 0)
361 goto out_buf;
362 *treeblk = ret;
363 memset(buf, 0, V2_DQBLKSIZE);
364 newact = 1;
365 }
366 else {
367 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
368 printk(KERN_ERR "VFS: Can't read tree quota block %u.\n", *treeblk);
369 goto out_buf;
370 }
371 }
372 ref = (u32 *)buf;
373 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
374 if (!newblk)
375 newson = 1;
376 if (depth == V2_DQTREEDEPTH-1) {
377 #ifdef __QUOTA_V2_PARANOIA
378 if (newblk) {
379 printk(KERN_ERR "VFS: Inserting already present quota entry (block %u).\n", ref[GETIDINDEX(dquot->dq_id, depth)]);
380 ret = -EIO;
381 goto out_buf;
382 }
383 #endif
384 newblk = find_free_dqentry(dquot, &ret);
385 }
386 else
387 ret = do_insert_tree(dquot, &newblk, depth+1);
388 if (newson && ret >= 0) {
389 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
390 ret = write_blk(filp, *treeblk, buf);
391 }
392 else if (newact && ret < 0)
393 put_free_dqblk(filp, info, buf, *treeblk);
394 out_buf:
395 freedqbuf(buf);
396 return ret;
397 }
398
399 /* Wrapper for inserting quota structure into tree */
dq_insert_tree(struct dquot * dquot)400 static inline int dq_insert_tree(struct dquot *dquot)
401 {
402 int tmp = V2_DQTREEOFF;
403 return do_insert_tree(dquot, &tmp, 0);
404 }
405
406 /*
407 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
408 */
v2_write_dquot(struct dquot * dquot)409 static int v2_write_dquot(struct dquot *dquot)
410 {
411 int type = dquot->dq_type;
412 struct file *filp;
413 mm_segment_t fs;
414 loff_t offset;
415 ssize_t ret;
416 struct v2_disk_dqblk ddquot;
417
418 if (!dquot->dq_off)
419 if ((ret = dq_insert_tree(dquot)) < 0) {
420 printk(KERN_ERR "VFS: Error %d occured while creating quota.\n", ret);
421 return ret;
422 }
423 filp = sb_dqopt(dquot->dq_sb)->files[type];
424 offset = dquot->dq_off;
425 mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id);
426 fs = get_fs();
427 set_fs(KERNEL_DS);
428 ret = filp->f_op->write(filp, (char *)&ddquot, sizeof(struct v2_disk_dqblk), &offset);
429 set_fs(fs);
430 if (ret != sizeof(struct v2_disk_dqblk)) {
431 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n", kdevname(dquot->dq_dev));
432 if (ret >= 0)
433 ret = -ENOSPC;
434 }
435 else
436 ret = 0;
437 dqstats.writes++;
438 return ret;
439 }
440
441 /* Free dquot entry in data block */
free_dqentry(struct dquot * dquot,uint blk)442 static int free_dqentry(struct dquot *dquot, uint blk)
443 {
444 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
445 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type;
446 struct v2_disk_dqdbheader *dh;
447 dqbuf_t buf = getdqbuf();
448 int ret = 0;
449
450 if (!buf)
451 return -ENOMEM;
452 if (dquot->dq_off >> V2_DQBLKSIZE_BITS != blk) {
453 printk(KERN_ERR "VFS: Quota structure has offset to other block (%u) than it should (%u).\n", blk, (uint)(dquot->dq_off >> V2_DQBLKSIZE_BITS));
454 goto out_buf;
455 }
456 if ((ret = read_blk(filp, blk, buf)) < 0) {
457 printk(KERN_ERR "VFS: Can't read quota data block %u\n", blk);
458 goto out_buf;
459 }
460 dh = (struct v2_disk_dqdbheader *)buf;
461 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries)-1);
462 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
463 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
464 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
465 printk(KERN_ERR "VFS: Can't move quota data block (%u) to free list.\n", blk);
466 goto out_buf;
467 }
468 }
469 else {
470 memset(buf+(dquot->dq_off & ((1 << V2_DQBLKSIZE_BITS)-1)), 0, sizeof(struct v2_disk_dqblk));
471 if (le16_to_cpu(dh->dqdh_entries) == V2_DQSTRINBLK-1) {
472 /* Insert will write block itself */
473 if ((ret = insert_free_dqentry(filp, info, buf, blk)) < 0) {
474 printk(KERN_ERR "VFS: Can't insert quota data block (%u) to free entry list.\n", blk);
475 goto out_buf;
476 }
477 }
478 else
479 if ((ret = write_blk(filp, blk, buf)) < 0) {
480 printk(KERN_ERR "VFS: Can't write quota data block %u\n", blk);
481 goto out_buf;
482 }
483 }
484 dquot->dq_off = 0; /* Quota is now unattached */
485 out_buf:
486 freedqbuf(buf);
487 return ret;
488 }
489
490 /* Remove reference to dquot from tree */
remove_tree(struct dquot * dquot,uint * blk,int depth)491 static int remove_tree(struct dquot *dquot, uint *blk, int depth)
492 {
493 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
494 struct mem_dqinfo *info = sb_dqopt(dquot->dq_sb)->info + dquot->dq_type;
495 dqbuf_t buf = getdqbuf();
496 int ret = 0;
497 uint newblk;
498 u32 *ref = (u32 *)buf;
499
500 if (!buf)
501 return -ENOMEM;
502 if ((ret = read_blk(filp, *blk, buf)) < 0) {
503 printk(KERN_ERR "VFS: Can't read quota data block %u\n", *blk);
504 goto out_buf;
505 }
506 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
507 if (depth == V2_DQTREEDEPTH-1) {
508 ret = free_dqentry(dquot, newblk);
509 newblk = 0;
510 }
511 else
512 ret = remove_tree(dquot, &newblk, depth+1);
513 if (ret >= 0 && !newblk) {
514 int i;
515 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
516 for (i = 0; i < V2_DQBLKSIZE && !buf[i]; i++); /* Block got empty? */
517 if (i == V2_DQBLKSIZE) {
518 put_free_dqblk(filp, info, buf, *blk);
519 *blk = 0;
520 }
521 else
522 if ((ret = write_blk(filp, *blk, buf)) < 0)
523 printk(KERN_ERR "VFS: Can't write quota tree block %u.\n", *blk);
524 }
525 out_buf:
526 freedqbuf(buf);
527 return ret;
528 }
529
530 /* Delete dquot from tree */
v2_delete_dquot(struct dquot * dquot)531 static int v2_delete_dquot(struct dquot *dquot)
532 {
533 uint tmp = V2_DQTREEOFF;
534
535 if (!dquot->dq_off) /* Even not allocated? */
536 return 0;
537 return remove_tree(dquot, &tmp, 0);
538 }
539
540 /* Find entry in block */
find_block_dqentry(struct dquot * dquot,uint blk)541 static loff_t find_block_dqentry(struct dquot *dquot, uint blk)
542 {
543 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
544 dqbuf_t buf = getdqbuf();
545 loff_t ret = 0;
546 int i;
547 struct v2_disk_dqblk *ddquot = GETENTRIES(buf);
548
549 if (!buf)
550 return -ENOMEM;
551 if ((ret = read_blk(filp, blk, buf)) < 0) {
552 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
553 goto out_buf;
554 }
555 if (dquot->dq_id)
556 for (i = 0; i < V2_DQSTRINBLK && le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id; i++);
557 else { /* ID 0 as a bit more complicated searching... */
558 struct v2_disk_dqblk fakedquot;
559
560 memset(&fakedquot, 0, sizeof(struct v2_disk_dqblk));
561 for (i = 0; i < V2_DQSTRINBLK; i++)
562 if (!le32_to_cpu(ddquot[i].dqb_id) && memcmp(&fakedquot, ddquot+i, sizeof(struct v2_disk_dqblk)))
563 break;
564 }
565 if (i == V2_DQSTRINBLK) {
566 printk(KERN_ERR "VFS: Quota for id %u referenced but not present.\n", dquot->dq_id);
567 ret = -EIO;
568 goto out_buf;
569 }
570 else
571 ret = (blk << V2_DQBLKSIZE_BITS) + sizeof(struct v2_disk_dqdbheader) + i * sizeof(struct v2_disk_dqblk);
572 out_buf:
573 freedqbuf(buf);
574 return ret;
575 }
576
577 /* Find entry for given id in the tree */
find_tree_dqentry(struct dquot * dquot,uint blk,int depth)578 static loff_t find_tree_dqentry(struct dquot *dquot, uint blk, int depth)
579 {
580 struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
581 dqbuf_t buf = getdqbuf();
582 loff_t ret = 0;
583 u32 *ref = (u32 *)buf;
584
585 if (!buf)
586 return -ENOMEM;
587 if ((ret = read_blk(filp, blk, buf)) < 0) {
588 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
589 goto out_buf;
590 }
591 ret = 0;
592 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
593 if (!blk) /* No reference? */
594 goto out_buf;
595 if (depth < V2_DQTREEDEPTH-1)
596 ret = find_tree_dqentry(dquot, blk, depth+1);
597 else
598 ret = find_block_dqentry(dquot, blk);
599 out_buf:
600 freedqbuf(buf);
601 return ret;
602 }
603
604 /* Find entry for given id in the tree - wrapper function */
find_dqentry(struct dquot * dquot)605 static inline loff_t find_dqentry(struct dquot *dquot)
606 {
607 return find_tree_dqentry(dquot, V2_DQTREEOFF, 0);
608 }
609
v2_read_dquot(struct dquot * dquot)610 static int v2_read_dquot(struct dquot *dquot)
611 {
612 int type = dquot->dq_type;
613 struct file *filp;
614 mm_segment_t fs;
615 loff_t offset;
616 struct v2_disk_dqblk ddquot;
617 int ret = 0;
618
619 filp = sb_dqopt(dquot->dq_sb)->files[type];
620
621 #ifdef __QUOTA_V2_PARANOIA
622 if (!filp || !dquot->dq_sb) { /* Invalidated quota? */
623 printk(KERN_ERR "VFS: Quota invalidated while reading!\n");
624 return -EIO;
625 }
626 #endif
627 offset = find_dqentry(dquot);
628 if (offset <= 0) { /* Entry not present? */
629 if (offset < 0)
630 printk(KERN_ERR "VFS: Can't read quota structure for id %u.\n", dquot->dq_id);
631 dquot->dq_off = 0;
632 dquot->dq_flags |= DQ_FAKE;
633 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
634 ret = offset;
635 }
636 else {
637 dquot->dq_off = offset;
638 fs = get_fs();
639 set_fs(KERNEL_DS);
640 if ((ret = filp->f_op->read(filp, (char *)&ddquot, sizeof(struct v2_disk_dqblk), &offset)) != sizeof(struct v2_disk_dqblk)) {
641 if (ret >= 0)
642 ret = -EIO;
643 printk(KERN_ERR "VFS: Error while reading quota structure for id %u.\n", dquot->dq_id);
644 memset(&ddquot, 0, sizeof(struct v2_disk_dqblk));
645 }
646 else
647 ret = 0;
648 set_fs(fs);
649 disk2memdqb(&dquot->dq_dqb, &ddquot);
650 }
651 dqstats.reads++;
652 return ret;
653 }
654
655 /* Commit changes of dquot to disk - it might also mean deleting it when quota became fake one and user has no blocks... */
v2_commit_dquot(struct dquot * dquot)656 static int v2_commit_dquot(struct dquot *dquot)
657 {
658 /* We clear the flag everytime so we don't loop when there was an IO error... */
659 dquot->dq_flags &= ~DQ_MOD;
660 if (dquot->dq_flags & DQ_FAKE && !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
661 return v2_delete_dquot(dquot);
662 else
663 return v2_write_dquot(dquot);
664 }
665
666 static struct quota_format_ops v2_format_ops = {
667 check_quota_file: v2_check_quota_file,
668 read_file_info: v2_read_file_info,
669 write_file_info: v2_write_file_info,
670 free_file_info: NULL,
671 read_dqblk: v2_read_dquot,
672 commit_dqblk: v2_commit_dquot,
673 };
674
675 static struct quota_format_type v2_quota_format = {
676 qf_fmt_id: QFMT_VFS_V0,
677 qf_ops: &v2_format_ops,
678 qf_owner: THIS_MODULE
679 };
680
init_v2_quota_format(void)681 static int __init init_v2_quota_format(void)
682 {
683 return register_quota_format(&v2_quota_format);
684 }
685
exit_v2_quota_format(void)686 static void __exit exit_v2_quota_format(void)
687 {
688 unregister_quota_format(&v2_quota_format);
689 }
690
691 EXPORT_NO_SYMBOLS;
692
693 module_init(init_v2_quota_format);
694 module_exit(exit_v2_quota_format);
695