ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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_KERNEL);
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;
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         spin_unlock(&dq_data_lock);
436         fs = get_fs();
437         set_fs(KERNEL_DS);
438         ret = filp->f_op->write(filp, (char *)&ddquot, sizeof(struct v2_disk_dqblk), &offset);
439         set_fs(fs);
440         if (ret != sizeof(struct v2_disk_dqblk)) {
441                 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n", dquot->dq_sb->s_id);
442                 if (ret >= 0)
443                         ret = -ENOSPC;
444         }
445         else
446                 ret = 0;
447         dqstats.writes++;
448
449         return ret;
450 }
451
452 /* Free dquot entry in data block */
453 static int free_dqentry(struct dquot *dquot, uint blk)
454 {
455         struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
456         struct v2_disk_dqdbheader *dh;
457         dqbuf_t buf = getdqbuf();
458         int ret = 0;
459
460         if (!buf)
461                 return -ENOMEM;
462         if (dquot->dq_off >> V2_DQBLKSIZE_BITS != blk) {
463                 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));
464                 goto out_buf;
465         }
466         if ((ret = read_blk(filp, blk, buf)) < 0) {
467                 printk(KERN_ERR "VFS: Can't read quota data block %u\n", blk);
468                 goto out_buf;
469         }
470         dh = (struct v2_disk_dqdbheader *)buf;
471         dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries)-1);
472         if (!le16_to_cpu(dh->dqdh_entries)) {   /* Block got free? */
473                 if ((ret = remove_free_dqentry(filp, dquot->dq_type, buf, blk)) < 0 ||
474                     (ret = put_free_dqblk(filp, dquot->dq_type, buf, blk)) < 0) {
475                         printk(KERN_ERR "VFS: Can't move quota data block (%u) to free list.\n", blk);
476                         goto out_buf;
477                 }
478         }
479         else {
480                 memset(buf+(dquot->dq_off & ((1 << V2_DQBLKSIZE_BITS)-1)), 0, sizeof(struct v2_disk_dqblk));
481                 if (le16_to_cpu(dh->dqdh_entries) == V2_DQSTRINBLK-1) {
482                         /* Insert will write block itself */
483                         if ((ret = insert_free_dqentry(filp, dquot->dq_type, buf, blk)) < 0) {
484                                 printk(KERN_ERR "VFS: Can't insert quota data block (%u) to free entry list.\n", blk);
485                                 goto out_buf;
486                         }
487                 }
488                 else
489                         if ((ret = write_blk(filp, blk, buf)) < 0) {
490                                 printk(KERN_ERR "VFS: Can't write quota data block %u\n", blk);
491                                 goto out_buf;
492                         }
493         }
494         dquot->dq_off = 0;      /* Quota is now unattached */
495 out_buf:
496         freedqbuf(buf);
497         return ret;
498 }
499
500 /* Remove reference to dquot from tree */
501 static int remove_tree(struct dquot *dquot, uint *blk, int depth)
502 {
503         struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
504         dqbuf_t buf = getdqbuf();
505         int ret = 0;
506         uint newblk;
507         u32 *ref = (u32 *)buf;
508         
509         if (!buf)
510                 return -ENOMEM;
511         if ((ret = read_blk(filp, *blk, buf)) < 0) {
512                 printk(KERN_ERR "VFS: Can't read quota data block %u\n", *blk);
513                 goto out_buf;
514         }
515         newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
516         if (depth == V2_DQTREEDEPTH-1) {
517                 ret = free_dqentry(dquot, newblk);
518                 newblk = 0;
519         }
520         else
521                 ret = remove_tree(dquot, &newblk, depth+1);
522         if (ret >= 0 && !newblk) {
523                 int i;
524                 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
525                 for (i = 0; i < V2_DQBLKSIZE && !buf[i]; i++);  /* Block got empty? */
526                 if (i == V2_DQBLKSIZE) {
527                         put_free_dqblk(filp, dquot->dq_type, buf, *blk);
528                         *blk = 0;
529                 }
530                 else
531                         if ((ret = write_blk(filp, *blk, buf)) < 0)
532                                 printk(KERN_ERR "VFS: Can't write quota tree block %u.\n", *blk);
533         }
534 out_buf:
535         freedqbuf(buf);
536         return ret;     
537 }
538
539 /* Delete dquot from tree */
540 static int v2_delete_dquot(struct dquot *dquot)
541 {
542         uint tmp = V2_DQTREEOFF;
543
544         if (!dquot->dq_off)     /* Even not allocated? */
545                 return 0;
546         return remove_tree(dquot, &tmp, 0);
547 }
548
549 /* Find entry in block */
550 static loff_t find_block_dqentry(struct dquot *dquot, uint blk)
551 {
552         struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
553         dqbuf_t buf = getdqbuf();
554         loff_t ret = 0;
555         int i;
556         struct v2_disk_dqblk *ddquot = GETENTRIES(buf);
557
558         if (!buf)
559                 return -ENOMEM;
560         if ((ret = read_blk(filp, blk, buf)) < 0) {
561                 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
562                 goto out_buf;
563         }
564         if (dquot->dq_id)
565                 for (i = 0; i < V2_DQSTRINBLK && le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id; i++);
566         else {  /* ID 0 as a bit more complicated searching... */
567                 struct v2_disk_dqblk fakedquot;
568
569                 memset(&fakedquot, 0, sizeof(struct v2_disk_dqblk));
570                 for (i = 0; i < V2_DQSTRINBLK; i++)
571                         if (!le32_to_cpu(ddquot[i].dqb_id) && memcmp(&fakedquot, ddquot+i, sizeof(struct v2_disk_dqblk)))
572                                 break;
573         }
574         if (i == V2_DQSTRINBLK) {
575                 printk(KERN_ERR "VFS: Quota for id %u referenced but not present.\n", dquot->dq_id);
576                 ret = -EIO;
577                 goto out_buf;
578         }
579         else
580                 ret = (blk << V2_DQBLKSIZE_BITS) + sizeof(struct v2_disk_dqdbheader) + i * sizeof(struct v2_disk_dqblk);
581 out_buf:
582         freedqbuf(buf);
583         return ret;
584 }
585
586 /* Find entry for given id in the tree */
587 static loff_t find_tree_dqentry(struct dquot *dquot, uint blk, int depth)
588 {
589         struct file *filp = sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
590         dqbuf_t buf = getdqbuf();
591         loff_t ret = 0;
592         u32 *ref = (u32 *)buf;
593
594         if (!buf)
595                 return -ENOMEM;
596         if ((ret = read_blk(filp, blk, buf)) < 0) {
597                 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
598                 goto out_buf;
599         }
600         ret = 0;
601         blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
602         if (!blk)       /* No reference? */
603                 goto out_buf;
604         if (depth < V2_DQTREEDEPTH-1)
605                 ret = find_tree_dqentry(dquot, blk, depth+1);
606         else
607                 ret = find_block_dqentry(dquot, blk);
608 out_buf:
609         freedqbuf(buf);
610         return ret;
611 }
612
613 /* Find entry for given id in the tree - wrapper function */
614 static inline loff_t find_dqentry(struct dquot *dquot)
615 {
616         return find_tree_dqentry(dquot, V2_DQTREEOFF, 0);
617 }
618
619 static int v2_read_dquot(struct dquot *dquot)
620 {
621         int type = dquot->dq_type;
622         struct file *filp;
623         mm_segment_t fs;
624         loff_t offset;
625         struct v2_disk_dqblk ddquot;
626         int ret = 0;
627
628         filp = sb_dqopt(dquot->dq_sb)->files[type];
629
630 #ifdef __QUOTA_V2_PARANOIA
631         if (!filp || !dquot->dq_sb) {   /* Invalidated quota? */
632                 printk(KERN_ERR "VFS: Quota invalidated while reading!\n");
633                 return -EIO;
634         }
635 #endif
636         offset = find_dqentry(dquot);
637         if (offset <= 0) {      /* Entry not present? */
638                 if (offset < 0)
639                         printk(KERN_ERR "VFS: Can't read quota structure for id %u.\n", dquot->dq_id);
640                 dquot->dq_off = 0;
641                 set_bit(DQ_FAKE_B, &dquot->dq_flags);
642                 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
643                 ret = offset;
644         }
645         else {
646                 dquot->dq_off = offset;
647                 fs = get_fs();
648                 set_fs(KERNEL_DS);
649                 if ((ret = filp->f_op->read(filp, (char *)&ddquot, sizeof(struct v2_disk_dqblk), &offset)) != sizeof(struct v2_disk_dqblk)) {
650                         if (ret >= 0)
651                                 ret = -EIO;
652                         printk(KERN_ERR "VFS: Error while reading quota structure for id %u.\n", dquot->dq_id);
653                         memset(&ddquot, 0, sizeof(struct v2_disk_dqblk));
654                 }
655                 else
656                         ret = 0;
657                 set_fs(fs);
658                 disk2memdqb(&dquot->dq_dqb, &ddquot);
659                 if (!dquot->dq_dqb.dqb_bhardlimit &&
660                         !dquot->dq_dqb.dqb_bsoftlimit &&
661                         !dquot->dq_dqb.dqb_ihardlimit &&
662                         !dquot->dq_dqb.dqb_isoftlimit)
663                         set_bit(DQ_FAKE_B, &dquot->dq_flags);
664         }
665         dqstats.reads++;
666
667         return ret;
668 }
669
670 /* Check whether dquot should not be deleted. We know we are
671  * the only one operating on dquot (thanks to dq_lock) */
672 static int v2_release_dquot(struct dquot *dquot)
673 {
674         if (test_bit(DQ_FAKE_B, &dquot->dq_flags) && !(dquot->dq_dqb.dqb_curinodes | dquot->dq_dqb.dqb_curspace))
675                 return v2_delete_dquot(dquot);
676         return 0;
677 }
678
679 static struct quota_format_ops v2_format_ops = {
680         .check_quota_file       = v2_check_quota_file,
681         .read_file_info         = v2_read_file_info,
682         .write_file_info        = v2_write_file_info,
683         .free_file_info         = NULL,
684         .read_dqblk             = v2_read_dquot,
685         .commit_dqblk           = v2_write_dquot,
686         .release_dqblk          = v2_release_dquot,
687 };
688
689 static struct quota_format_type v2_quota_format = {
690         .qf_fmt_id      = QFMT_VFS_V0,
691         .qf_ops         = &v2_format_ops,
692         .qf_owner       = THIS_MODULE
693 };
694
695 static int __init init_v2_quota_format(void)
696 {
697         return register_quota_format(&v2_quota_format);
698 }
699
700 static void __exit exit_v2_quota_format(void)
701 {
702         unregister_quota_format(&v2_quota_format);
703 }
704
705 module_init(init_v2_quota_format);
706 module_exit(exit_v2_quota_format);