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