1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre administrative quota format.
11 # define EXPORT_SYMTAB
14 #include <linux/errno.h>
16 #include <linux/mount.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/quotaio_v1.h>
23 #include <asm/byteorder.h>
24 #include <asm/uaccess.h>
26 #include <lustre_quota.h>
27 #include "lustre_quota_fmt.h"
29 typedef char *dqbuf_t;
31 #define GETIDINDEX(id, depth) (((id) >> ((LUSTRE_DQTREEDEPTH-(depth)-1)*8)) & 0xff)
32 #define GETENTRIES(buf) ((struct lustre_disk_dqblk *)(((char *)buf)+sizeof(struct lustre_disk_dqdbheader)))
34 static int check_quota_file(struct file *f, struct inode *inode, int type)
36 struct lustre_disk_dqheader dqhead;
40 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
41 static const uint quota_versions[] = LUSTRE_INITQVERSIONS;
46 size = f->f_op->read(f, (char *)&dqhead,
47 sizeof(struct lustre_disk_dqheader),
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
54 struct super_block *sb = inode->i_sb;
55 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
56 sizeof(struct lustre_disk_dqheader), 0);
59 if (size != sizeof(struct lustre_disk_dqheader))
61 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
62 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
67 /* Check whether given file is really lustre admin quotafile */
68 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
70 struct file *f = lqi->qi_files[type];
71 return check_quota_file(f, NULL, type);
74 /* Read information header from quota file */
75 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
78 struct lustre_disk_dqinfo dinfo;
79 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
80 struct file *f = lqi->qi_files[type];
82 loff_t offset = LUSTRE_DQINFOOFF;
86 size = f->f_op->read(f, (char *)&dinfo,
87 sizeof(struct lustre_disk_dqinfo), &offset);
89 if (size != sizeof(struct lustre_disk_dqinfo)) {
90 printk(KERN_WARNING "Can't read info structure on device %s.\n",
91 f->f_vfsmnt->mnt_sb->s_id);
94 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
95 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
96 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
97 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
98 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
99 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
103 /* Write information header to quota file */
104 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
107 struct lustre_disk_dqinfo dinfo;
108 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
109 struct file *f = lqi->qi_files[type];
111 loff_t offset = LUSTRE_DQINFOOFF;
113 info->dqi_flags &= ~DQF_INFO_DIRTY;
114 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
115 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
116 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
117 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
118 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
119 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
122 size = f->f_op->write(f, (char *)&dinfo,
123 sizeof(struct lustre_disk_dqinfo), &offset);
125 if (size != sizeof(struct lustre_disk_dqinfo)) {
127 "Can't write info structure on device %s.\n",
128 f->f_vfsmnt->mnt_sb->s_id);
134 static void disk2memdqb(struct mem_dqblk *m, struct lustre_disk_dqblk *d)
136 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
137 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
138 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
139 m->dqb_itime = le64_to_cpu(d->dqb_itime);
140 m->dqb_bhardlimit = le32_to_cpu(d->dqb_bhardlimit);
141 m->dqb_bsoftlimit = le32_to_cpu(d->dqb_bsoftlimit);
142 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
143 m->dqb_btime = le64_to_cpu(d->dqb_btime);
146 static void mem2diskdqb(struct lustre_disk_dqblk *d, struct mem_dqblk *m,
149 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
150 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
151 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
152 d->dqb_itime = cpu_to_le64(m->dqb_itime);
153 d->dqb_bhardlimit = cpu_to_le32(m->dqb_bhardlimit);
154 d->dqb_bsoftlimit = cpu_to_le32(m->dqb_bsoftlimit);
155 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
156 d->dqb_btime = cpu_to_le64(m->dqb_btime);
157 d->dqb_id = cpu_to_le32(id);
160 static dqbuf_t getdqbuf(void)
162 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
165 "VFS: Not enough memory for quota buffers.\n");
169 static inline void freedqbuf(dqbuf_t buf)
174 static ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
178 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
180 memset(buf, 0, LUSTRE_DQBLKSIZE);
183 ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
188 static ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
192 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
196 ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
202 static void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
204 set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
207 #define lustre_info_dirty(info) test_bit(DQF_INFO_DIRTY_B, &(info)->dqi_flags)
209 /* Remove empty block from list and return it */
210 static int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
212 dqbuf_t buf = getdqbuf();
213 struct lustre_disk_dqdbheader *dh =
214 (struct lustre_disk_dqdbheader *)buf;
219 if (info->dqi_free_blk) {
220 blk = info->dqi_free_blk;
221 if ((ret = read_blk(filp, blk, buf)) < 0)
223 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
225 memset(buf, 0, LUSTRE_DQBLKSIZE);
226 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0) /* Assure block allocation... */
228 blk = info->dqi_blocks++;
230 lustre_mark_info_dirty(info);
237 /* Insert empty block to the list */
238 static int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
239 dqbuf_t buf, uint blk)
241 struct lustre_disk_dqdbheader *dh =
242 (struct lustre_disk_dqdbheader *)buf;
245 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
246 dh->dqdh_prev_free = cpu_to_le32(0);
247 dh->dqdh_entries = cpu_to_le16(0);
248 info->dqi_free_blk = blk;
249 lustre_mark_info_dirty(info);
250 if ((err = write_blk(filp, blk, buf)) < 0)
251 /* Some strange block. We had better leave it... */
256 /* Remove given block from the list of blocks with free entries */
257 static int remove_free_dqentry(struct file *filp,
258 struct lustre_mem_dqinfo *info, dqbuf_t buf,
261 dqbuf_t tmpbuf = getdqbuf();
262 struct lustre_disk_dqdbheader *dh =
263 (struct lustre_disk_dqdbheader *)buf;
264 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
265 le32_to_cpu(dh->dqdh_prev_free);
271 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
273 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
275 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
279 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
281 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
283 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
286 info->dqi_free_entry = nextblk;
287 lustre_mark_info_dirty(info);
290 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
291 if (write_blk(filp, blk, buf) < 0) /* No matter whether write succeeds block is out of list */
293 "VFS: Can't write block (%u) with free entries.\n", blk);
300 /* Insert given block to the beginning of list with free entries */
301 static int insert_free_dqentry(struct file *filp,
302 struct lustre_mem_dqinfo *info, dqbuf_t buf,
305 dqbuf_t tmpbuf = getdqbuf();
306 struct lustre_disk_dqdbheader *dh =
307 (struct lustre_disk_dqdbheader *)buf;
312 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
313 dh->dqdh_prev_free = cpu_to_le32(0);
314 if ((err = write_blk(filp, blk, buf)) < 0)
316 if (info->dqi_free_entry) {
317 if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
319 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
321 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
325 info->dqi_free_entry = blk;
326 lustre_mark_info_dirty(info);
333 /* Find space for dquot */
334 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err)
336 struct lustre_quota_info *lqi = dquot->dq_info;
337 struct file *filp = lqi->qi_files[dquot->dq_type];
338 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
340 struct lustre_disk_dqdbheader *dh;
341 struct lustre_disk_dqblk *ddquot;
342 struct lustre_disk_dqblk fakedquot;
346 if (!(buf = getdqbuf())) {
350 dh = (struct lustre_disk_dqdbheader *)buf;
351 ddquot = GETENTRIES(buf);
352 if (info->dqi_free_entry) {
353 blk = info->dqi_free_entry;
354 if ((*err = read_blk(filp, blk, buf)) < 0)
357 blk = get_free_dqblk(filp, info);
363 memset(buf, 0, LUSTRE_DQBLKSIZE);
364 info->dqi_free_entry = blk; /* This is enough as block is already zeroed and entry list is empty... */
365 lustre_mark_info_dirty(info);
367 if (le16_to_cpu(dh->dqdh_entries) + 1 >= LUSTRE_DQSTRINBLK) /* Block will be full? */
368 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
370 "VFS: find_free_dqentry(): Can't remove block (%u) from entry free list.\n",
374 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
375 memset(&fakedquot, 0, sizeof(struct lustre_disk_dqblk));
376 /* Find free structure in block */
377 for (i = 0; i < LUSTRE_DQSTRINBLK &&
378 memcmp(&fakedquot, ddquot + i, sizeof(fakedquot)); i++) ;
380 if (i == LUSTRE_DQSTRINBLK) {
382 "VFS: find_free_dqentry(): Data block full but it shouldn't.\n");
387 if ((*err = write_blk(filp, blk, buf)) < 0) {
389 "VFS: find_free_dqentry(): Can't write quota data block %u.\n",
394 (blk << LUSTRE_DQBLKSIZE_BITS) +
395 sizeof(struct lustre_disk_dqdbheader) +
396 i * sizeof(struct lustre_disk_dqblk);
404 /* Insert reference to structure into the trie */
405 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth)
407 struct lustre_quota_info *lqi = dquot->dq_info;
408 struct file *filp = lqi->qi_files[dquot->dq_type];
409 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
411 int ret = 0, newson = 0, newact = 0;
415 if (!(buf = getdqbuf()))
418 ret = get_free_dqblk(filp, info);
422 memset(buf, 0, LUSTRE_DQBLKSIZE);
425 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
427 "VFS: Can't read tree quota block %u.\n",
433 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
436 if (depth == LUSTRE_DQTREEDEPTH - 1) {
440 "VFS: Inserting already present quota entry (block %u).\n",
441 ref[GETIDINDEX(dquot->dq_id, depth)]);
446 newblk = find_free_dqentry(dquot, &ret);
448 ret = do_insert_tree(dquot, &newblk, depth + 1);
449 if (newson && ret >= 0) {
450 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
451 ret = write_blk(filp, *treeblk, buf);
452 } else if (newact && ret < 0)
453 put_free_dqblk(filp, info, buf, *treeblk);
459 /* Wrapper for inserting quota structure into tree */
460 static inline int dq_insert_tree(struct lustre_dquot *dquot)
462 int tmp = LUSTRE_DQTREEOFF;
463 return do_insert_tree(dquot, &tmp, 0);
467 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
469 static int lustre_write_dquot(struct lustre_dquot *dquot)
471 int type = dquot->dq_type;
476 struct lustre_disk_dqblk ddquot, empty;
479 if ((ret = dq_insert_tree(dquot)) < 0) {
481 "VFS: Error %Zd occurred while creating quota.\n",
485 filp = dquot->dq_info->qi_files[type];
486 offset = dquot->dq_off;
487 mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id);
488 /* Argh... We may need to write structure full of zeroes but that would be
489 * treated as an empty place by the rest of the code. Format change would
490 * be definitely cleaner but the problems probably are not worth it */
491 memset(&empty, 0, sizeof(struct lustre_disk_dqblk));
492 if (!memcmp(&empty, &ddquot, sizeof(struct lustre_disk_dqblk)))
493 ddquot.dqb_itime = cpu_to_le64(1);
496 ret = filp->f_op->write(filp, (char *)&ddquot,
497 sizeof(struct lustre_disk_dqblk), &offset);
499 if (ret != sizeof(struct lustre_disk_dqblk)) {
500 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
501 filp->f_dentry->d_sb->s_id);
510 /* Free dquot entry in data block */
511 static int free_dqentry(struct lustre_dquot *dquot, uint blk)
513 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
514 struct lustre_mem_dqinfo *info =
515 &dquot->dq_info->qi_info[dquot->dq_type];
516 struct lustre_disk_dqdbheader *dh;
517 dqbuf_t buf = getdqbuf();
522 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
524 "VFS: Quota structure has offset to other block (%u) than it should (%u).\n",
525 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
528 if ((ret = read_blk(filp, blk, buf)) < 0) {
529 printk(KERN_ERR "VFS: Can't read quota data block %u\n", blk);
532 dh = (struct lustre_disk_dqdbheader *)buf;
533 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
534 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
535 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
536 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
538 "VFS: Can't move quota data block (%u) to free list.\n",
544 (dquot->dq_off & ((1 << LUSTRE_DQBLKSIZE_BITS) - 1)), 0,
545 sizeof(struct lustre_disk_dqblk));
546 if (le16_to_cpu(dh->dqdh_entries) == LUSTRE_DQSTRINBLK - 1) {
547 /* Insert will write block itself */
549 insert_free_dqentry(filp, info, buf, blk)) < 0) {
551 "VFS: Can't insert quota data block (%u) to free entry list.\n",
555 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
557 "VFS: Can't write quota data block %u\n", blk);
561 dquot->dq_off = 0; /* Quota is now unattached */
567 /* Remove reference to dquot from tree */
568 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth)
570 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
571 struct lustre_mem_dqinfo *info =
572 &dquot->dq_info->qi_info[dquot->dq_type];
573 dqbuf_t buf = getdqbuf();
576 u32 *ref = (u32 *) buf;
580 if ((ret = read_blk(filp, *blk, buf)) < 0) {
581 printk(KERN_ERR "VFS: Can't read quota data block %u\n", *blk);
584 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
585 if (depth == LUSTRE_DQTREEDEPTH - 1) {
586 ret = free_dqentry(dquot, newblk);
589 ret = remove_tree(dquot, &newblk, depth + 1);
590 if (ret >= 0 && !newblk) {
592 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
593 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++) ; /* Block got empty? */
594 /* don't put the root block into free blk list! */
595 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
596 put_free_dqblk(filp, info, buf, *blk);
598 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
600 "VFS: Can't write quota tree block %u.\n", *blk);
607 /* Delete dquot from tree */
608 static int lustre_delete_dquot(struct lustre_dquot *dquot)
610 uint tmp = LUSTRE_DQTREEOFF;
612 if (!dquot->dq_off) /* Even not allocated? */
614 return remove_tree(dquot, &tmp, 0);
617 /* Find entry in block */
618 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk)
620 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
621 dqbuf_t buf = getdqbuf();
624 struct lustre_disk_dqblk *ddquot = GETENTRIES(buf);
628 if ((ret = read_blk(filp, blk, buf)) < 0) {
629 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
634 i < LUSTRE_DQSTRINBLK
635 && le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id; i++) ;
636 else { /* ID 0 as a bit more complicated searching... */
637 struct lustre_disk_dqblk fakedquot;
639 memset(&fakedquot, 0, sizeof(struct lustre_disk_dqblk));
640 for (i = 0; i < LUSTRE_DQSTRINBLK; i++)
641 if (!le32_to_cpu(ddquot[i].dqb_id)
642 && memcmp(&fakedquot, ddquot + i,
643 sizeof(struct lustre_disk_dqblk)))
646 if (i == LUSTRE_DQSTRINBLK) {
648 "VFS: Quota for id %u referenced but not present.\n",
654 (blk << LUSTRE_DQBLKSIZE_BITS) +
655 sizeof(struct lustre_disk_dqdbheader) +
656 i * sizeof(struct lustre_disk_dqblk);
662 /* Find entry for given id in the tree */
663 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth)
665 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
666 dqbuf_t buf = getdqbuf();
668 u32 *ref = (u32 *) buf;
672 if ((ret = read_blk(filp, blk, buf)) < 0) {
673 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
677 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
678 if (!blk) /* No reference? */
680 if (depth < LUSTRE_DQTREEDEPTH - 1)
681 ret = find_tree_dqentry(dquot, blk, depth + 1);
683 ret = find_block_dqentry(dquot, blk);
689 /* Find entry for given id in the tree - wrapper function */
690 static inline loff_t find_dqentry(struct lustre_dquot *dquot)
692 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0);
695 int lustre_read_dquot(struct lustre_dquot *dquot)
697 int type = dquot->dq_type;
701 struct lustre_disk_dqblk ddquot, empty;
704 filp = dquot->dq_info->qi_files[type];
706 if (!filp || !dquot->dq_info) { /* Invalidated quota? */
707 printk(KERN_ERR "VFS: Quota invalidated while reading!\n");
711 offset = find_dqentry(dquot);
712 if (offset <= 0) { /* Entry not present? */
715 "VFS: Can't read quota structure for id %u.\n",
718 set_bit(DQ_FAKE_B, &dquot->dq_flags);
719 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
722 dquot->dq_off = offset;
725 if ((ret = filp->f_op->read(filp, (char *)&ddquot,
726 sizeof(struct lustre_disk_dqblk),
728 sizeof(struct lustre_disk_dqblk)) {
732 "VFS: Error while reading quota structure for id %u.\n",
734 memset(&ddquot, 0, sizeof(struct lustre_disk_dqblk));
737 /* We need to escape back all-zero structure */
738 memset(&empty, 0, sizeof(struct lustre_disk_dqblk));
739 empty.dqb_itime = cpu_to_le64(1);
740 if (!memcmp(&empty, &ddquot,
741 sizeof(struct lustre_disk_dqblk)))
742 ddquot.dqb_itime = 0;
745 disk2memdqb(&dquot->dq_dqb, &ddquot);
751 /* Commit changes of dquot to disk - it might also mean deleting it when quota became fake */
752 int lustre_commit_dquot(struct lustre_dquot *dquot)
755 /* always clear the flag so we don't loop on an IO error... */
756 clear_bit(DQ_MOD_B, &dquot->dq_flags);
758 /* The block/inode usage in admin quotafile isn't the real usage
759 * over all cluster, so keep the fake dquot entry on disk is
760 * meaningless, just remove it */
761 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
762 rc = lustre_delete_dquot(dquot);
764 rc = lustre_write_dquot(dquot);
769 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
770 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
775 /* We need to export this function to initialize quotafile, because we haven't
776 * user level check utility */
777 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
779 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
780 struct lustre_disk_dqheader dqhead;
781 struct file *fp = lqi->qi_files[type];
785 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
786 static const uint quota_versions[] = LUSTRE_INITQVERSIONS;
788 /* write quotafile header */
789 dqhead.dqh_magic = cpu_to_le32(quota_magics[type]);
790 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
791 size = fp->f_op->write(fp, (char *)&dqhead,
792 sizeof(struct lustre_disk_dqheader), &offset);
794 if (size != sizeof(struct lustre_disk_dqheader)) {
795 printk(KERN_ERR "error writing quoafile header (rc:%d)\n", rc);
801 /* write init quota info */
802 memset(dqinfo, 0, sizeof(*dqinfo));
803 dqinfo->dqi_bgrace = MAX_DQ_TIME;
804 dqinfo->dqi_igrace = MAX_IQ_TIME;
805 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
807 return lustre_write_quota_info(lqi, type);
811 struct list_head link;
815 static ssize_t quota_read(struct file *file, struct inode *inode, int type,
816 uint blk, dqbuf_t buf)
819 return read_blk(file, blk, buf);
821 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
824 struct super_block *sb = inode->i_sb;
825 memset(buf, 0, LUSTRE_DQBLKSIZE);
826 return sb->s_op->quota_read(sb, type, (char *)buf,
828 blk << LUSTRE_DQBLKSIZE_BITS);
833 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
834 uint blk, struct list_head *list)
836 dqbuf_t buf = getdqbuf();
838 struct lustre_disk_dqdbheader *dqhead =
839 (struct lustre_disk_dqdbheader *)buf;
840 struct dqblk *blk_item;
842 struct list_head *tmp;
846 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
847 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
852 if (!le32_to_cpu(dqhead->dqdh_entries))
855 if (list_empty(list)) {
860 list_for_each_entry(pos, list, link) {
861 if (blk == pos->blk) /* we got this blk already */
869 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
875 INIT_LIST_HEAD(&blk_item->link);
877 list_add_tail(&blk_item->link, tmp);
884 static int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
885 uint blk, int depth, struct list_head *list)
887 dqbuf_t buf = getdqbuf();
890 u32 *ref = (u32 *) buf;
894 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
895 printk(KERN_ERR "VFS: Can't read quota tree block %u.\n", blk);
900 for (index = 0; index <= 0xff && !ret; index++) {
901 blk = le32_to_cpu(ref[index]);
902 if (!blk) /* No reference */
905 if (depth < LUSTRE_DQTREEDEPTH - 1)
906 ret = walk_tree_dqentry(filp, inode, type, blk,
909 ret = walk_block_dqentry(filp, inode, type, blk, list);
916 /* Walk through the quota file (v2 format) to get all ids with quota limit */
917 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
918 struct list_head *list)
920 struct list_head blk_list;
921 struct dqblk *blk_item, *tmp;
923 struct lustre_disk_dqblk *ddquot;
926 if (!check_quota_file(fp, inode, type)) {
927 printk(KERN_ERR "unknown quota file format!\n");
930 if (!list_empty(list)) {
931 printk(KERN_ERR "not empty list\n");
935 INIT_LIST_HEAD(&blk_list);
936 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
938 printk(KERN_ERR "walk through quota file failed!(%d)\n", rc);
941 if (list_empty(&blk_list))
947 ddquot = GETENTRIES(buf);
949 list_for_each_entry(blk_item, &blk_list, link) {
952 struct lustre_disk_dqblk fakedquot;
954 memset(buf, 0, LUSTRE_DQBLKSIZE);
955 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
957 "VFS: Can't read quota tree block %u.\n",
963 memset(&fakedquot, 0, sizeof(struct lustre_disk_dqblk));
964 for (i = 0; i < LUSTRE_DQSTRINBLK; i++) {
965 struct dquot_id *dqid;
966 /* skip empty entry */
968 (&fakedquot, ddquot + i,
969 sizeof(struct lustre_disk_dqblk)))
972 dqid = kmalloc(sizeof(*dqid), GFP_NOFS);
977 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
978 INIT_LIST_HEAD(&dqid->di_link);
979 list_add(&dqid->di_link, list);
984 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
985 list_del_init(&blk_item->link);
993 EXPORT_SYMBOL(lustre_check_quota_file);
994 EXPORT_SYMBOL(lustre_read_quota_info);
995 EXPORT_SYMBOL(lustre_write_quota_info);
996 EXPORT_SYMBOL(lustre_read_dquot);
997 EXPORT_SYMBOL(lustre_commit_dquot);
998 EXPORT_SYMBOL(lustre_init_quota_info);
999 EXPORT_SYMBOL(lustre_get_qids);