1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lvfs/lustre_quota_fmt.c
38 * Lustre administrative quota format.
39 * from linux/fs/quota_v2.c
43 # define EXPORT_SYMTAB
46 #include <linux/errno.h>
48 #include <linux/mount.h>
49 #include <linux/kernel.h>
50 #include <linux/init.h>
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/quotaio_v1.h>
55 #include <asm/byteorder.h>
56 #include <asm/uaccess.h>
58 #include <lustre_quota.h>
59 #include <obd_support.h>
60 #include "lustre_quota_fmt.h"
62 #ifdef HAVE_QUOTA_SUPPORT
64 static const uint lustre_initqversions[][MAXQUOTAS] = {
65 [LUSTRE_QUOTA_V2] = LUSTRE_INITQVERSIONS_V2
68 static const int lustre_dqstrinblk[] = {
69 [LUSTRE_QUOTA_V2] = LUSTRE_DQSTRINBLK_V2
72 static const int lustre_disk_dqblk_sz[] = {
73 [LUSTRE_QUOTA_V2] = sizeof(struct lustre_disk_dqblk_v2)
78 struct lustre_disk_dqblk_v2 r1;
80 [LUSTRE_QUOTA_V2] = {.r1 = {.dqb_itime = __constant_cpu_to_le64(1LLU)} }
85 struct lustre_disk_dqblk_v2 r1;
87 [LUSTRE_QUOTA_V2] = {.r1 = { 0 } }
90 int check_quota_file(struct file *f, struct inode *inode, int type,
91 lustre_quota_version_t version)
93 struct lustre_disk_dqheader dqhead;
97 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
98 const uint *quota_versions = lustre_initqversions[version];
103 size = f->f_op->read(f, (char *)&dqhead,
104 sizeof(struct lustre_disk_dqheader),
108 #ifndef KERNEL_SUPPORTS_QUOTA_READ
111 struct super_block *sb = inode->i_sb;
112 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
113 sizeof(struct lustre_disk_dqheader),
117 if (size != sizeof(struct lustre_disk_dqheader))
119 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
120 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
126 * Check whether given file is really lustre admin quotafile
128 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
130 struct file *f = lqi->qi_files[type];
131 return check_quota_file(f, NULL, type, lqi->qi_version);
134 int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
137 struct lustre_disk_dqinfo dinfo;
139 loff_t offset = LUSTRE_DQINFOOFF;
143 size = f->f_op->read(f, (char *)&dinfo,
144 sizeof(struct lustre_disk_dqinfo), &offset);
146 if (size != sizeof(struct lustre_disk_dqinfo)) {
147 CDEBUG(D_ERROR, "Can't read info structure on device %s.\n",
148 f->f_vfsmnt->mnt_sb->s_id);
151 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
152 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
153 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
154 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
155 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
156 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
161 * Read information header from quota file
163 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
165 return lustre_read_quota_file_info(lqi->qi_files[type],
166 &lqi->qi_info[type]);
170 * Write information header to quota file
172 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
175 struct lustre_disk_dqinfo dinfo;
176 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
177 struct file *f = lqi->qi_files[type];
179 loff_t offset = LUSTRE_DQINFOOFF;
181 info->dqi_flags &= ~DQF_INFO_DIRTY;
182 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
183 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
184 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
185 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
186 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
187 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
190 size = f->f_op->write(f, (char *)&dinfo,
191 sizeof(struct lustre_disk_dqinfo), &offset);
193 if (size != sizeof(struct lustre_disk_dqinfo)) {
195 "Can't write info structure on device %s.\n",
196 f->f_vfsmnt->mnt_sb->s_id);
202 void disk2memdqb(struct lustre_mem_dqblk *m, void *d,
203 lustre_quota_version_t version)
205 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
207 LASSERT(version == LUSTRE_QUOTA_V2);
209 m->dqb_ihardlimit = le64_to_cpu(dqblk->dqb_ihardlimit);
210 m->dqb_isoftlimit = le64_to_cpu(dqblk->dqb_isoftlimit);
211 m->dqb_curinodes = le64_to_cpu(dqblk->dqb_curinodes);
212 m->dqb_itime = le64_to_cpu(dqblk->dqb_itime);
213 m->dqb_bhardlimit = le64_to_cpu(dqblk->dqb_bhardlimit);
214 m->dqb_bsoftlimit = le64_to_cpu(dqblk->dqb_bsoftlimit);
215 m->dqb_curspace = le64_to_cpu(dqblk->dqb_curspace);
216 m->dqb_btime = le64_to_cpu(dqblk->dqb_btime);
219 static int mem2diskdqb(void *d, struct lustre_mem_dqblk *m,
220 qid_t id, lustre_quota_version_t version)
222 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
224 LASSERT(version == LUSTRE_QUOTA_V2);
226 dqblk->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
227 dqblk->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
228 dqblk->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
229 dqblk->dqb_itime = cpu_to_le64(m->dqb_itime);
230 dqblk->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
231 dqblk->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
232 dqblk->dqb_curspace = cpu_to_le64(m->dqb_curspace);
233 dqblk->dqb_btime = cpu_to_le64(m->dqb_btime);
234 dqblk->dqb_id = cpu_to_le32(id);
239 dqbuf_t getdqbuf(void)
241 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
244 "VFS: Not enough memory for quota buffers.\n");
248 void freedqbuf(dqbuf_t buf)
253 ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
257 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
259 memset(buf, 0, LUSTRE_DQBLKSIZE);
262 ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
267 ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
271 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
275 ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
280 void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
282 set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
286 * Remove empty block from list and return it
288 int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
290 dqbuf_t buf = getdqbuf();
291 struct lustre_disk_dqdbheader *dh =
292 (struct lustre_disk_dqdbheader *)buf;
297 if (info->dqi_free_blk) {
298 blk = info->dqi_free_blk;
299 if ((ret = read_blk(filp, blk, buf)) < 0)
301 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
303 memset(buf, 0, LUSTRE_DQBLKSIZE);
304 /* Assure block allocation... */
305 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0)
307 blk = info->dqi_blocks++;
309 lustre_mark_info_dirty(info);
317 * Insert empty block to the list
319 int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
320 dqbuf_t buf, uint blk)
322 struct lustre_disk_dqdbheader *dh =
323 (struct lustre_disk_dqdbheader *)buf;
326 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
327 dh->dqdh_prev_free = cpu_to_le32(0);
328 dh->dqdh_entries = cpu_to_le16(0);
329 info->dqi_free_blk = blk;
330 lustre_mark_info_dirty(info);
331 if ((err = write_blk(filp, blk, buf)) < 0)
332 /* Some strange block. We had better leave it... */
338 * Remove given block from the list of blocks with free entries
340 int remove_free_dqentry(struct file *filp,
341 struct lustre_mem_dqinfo *info, dqbuf_t buf,
344 dqbuf_t tmpbuf = getdqbuf();
345 struct lustre_disk_dqdbheader *dh =
346 (struct lustre_disk_dqdbheader *)buf;
347 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
348 le32_to_cpu(dh->dqdh_prev_free);
354 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
356 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
358 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
362 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
364 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
366 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
369 info->dqi_free_entry = nextblk;
370 lustre_mark_info_dirty(info);
373 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
374 if (write_blk(filp, blk, buf) < 0)
375 /* No matter whether write succeeds block is out of list */
377 "VFS: Can't write block (%u) with free entries.\n", blk);
385 * Insert given block to the beginning of list with free entries
387 int insert_free_dqentry(struct file *filp,
388 struct lustre_mem_dqinfo *info, dqbuf_t buf,
391 dqbuf_t tmpbuf = getdqbuf();
392 struct lustre_disk_dqdbheader *dh =
393 (struct lustre_disk_dqdbheader *)buf;
398 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
399 dh->dqdh_prev_free = cpu_to_le32(0);
400 if ((err = write_blk(filp, blk, buf)) < 0)
402 if (info->dqi_free_entry) {
403 if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
405 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
407 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
411 info->dqi_free_entry = blk;
412 lustre_mark_info_dirty(info);
422 * Find space for dquot
424 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
425 lustre_quota_version_t version)
427 struct lustre_quota_info *lqi = dquot->dq_info;
428 struct file *filp = lqi->qi_files[dquot->dq_type];
429 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
431 struct lustre_disk_dqdbheader *dh;
433 int dqblk_sz = lustre_disk_dqblk_sz[version];
434 int dqstrinblk = lustre_dqstrinblk[version];
438 if (!(buf = getdqbuf())) {
442 dh = (struct lustre_disk_dqdbheader *)buf;
443 ddquot = GETENTRIES(buf, version);
444 if (info->dqi_free_entry) {
445 blk = info->dqi_free_entry;
446 if ((*err = read_blk(filp, blk, buf)) < 0)
449 blk = get_free_dqblk(filp, info);
455 memset(buf, 0, LUSTRE_DQBLKSIZE);
456 info->dqi_free_entry = blk; /* This is enough as block is
457 already zeroed and entry list
459 lustre_mark_info_dirty(info);
462 /* Will block be full */
463 if (le16_to_cpu(dh->dqdh_entries) + 1 >= dqstrinblk)
464 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
466 "VFS: find_free_dqentry(): Can't remove block "
467 "(%u) from entry free list.\n", blk);
470 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
471 /* Find free structure in block */
472 for (i = 0; i < dqstrinblk &&
473 memcmp((char *)&emptydquot[version],
474 (char *)ddquot + i * dqblk_sz, dqblk_sz);
477 if (i == dqstrinblk) {
479 "VFS: find_free_dqentry(): Data block full but it "
485 if ((*err = write_blk(filp, blk, buf)) < 0) {
487 "VFS: find_free_dqentry(): Can't write quota data "
492 (blk << LUSTRE_DQBLKSIZE_BITS) +
493 sizeof(struct lustre_disk_dqdbheader) +
503 * Insert reference to structure into the trie
505 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
506 lustre_quota_version_t version)
508 struct lustre_quota_info *lqi = dquot->dq_info;
509 struct file *filp = lqi->qi_files[dquot->dq_type];
510 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
512 int ret = 0, newson = 0, newact = 0;
516 if (!(buf = getdqbuf()))
519 ret = get_free_dqblk(filp, info);
523 memset(buf, 0, LUSTRE_DQBLKSIZE);
526 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
528 "VFS: Can't read tree quota block %u.\n",
534 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
537 if (depth == LUSTRE_DQTREEDEPTH - 1) {
541 "VFS: Inserting already present quota entry "
543 ref[GETIDINDEX(dquot->dq_id, depth)]);
548 newblk = find_free_dqentry(dquot, &ret, version);
550 ret = do_insert_tree(dquot, &newblk, depth + 1, version);
551 if (newson && ret >= 0) {
552 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
553 ret = write_blk(filp, *treeblk, buf);
554 } else if (newact && ret < 0)
555 put_free_dqblk(filp, info, buf, *treeblk);
562 * Wrapper for inserting quota structure into tree
564 static inline int dq_insert_tree(struct lustre_dquot *dquot,
565 lustre_quota_version_t version)
567 int tmp = LUSTRE_DQTREEOFF;
568 return do_insert_tree(dquot, &tmp, 0, version);
572 * We don't have to be afraid of deadlocks as we never have quotas on
575 static int lustre_write_dquot(struct lustre_dquot *dquot,
576 lustre_quota_version_t version)
578 int type = dquot->dq_type;
583 int dqblk_sz = lustre_disk_dqblk_sz[version];
584 struct lustre_disk_dqblk_v2 ddquot;
586 ret = mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id, version);
591 if ((ret = dq_insert_tree(dquot, version)) < 0) {
593 "VFS: Error %Zd occurred while creating "
597 filp = dquot->dq_info->qi_files[type];
598 offset = dquot->dq_off;
599 /* Argh... We may need to write structure full of zeroes but that would
600 * be treated as an empty place by the rest of the code. Format change
601 * would be definitely cleaner but the problems probably are not worth
603 if (!memcmp((char *)&emptydquot[version], (char *)&ddquot, dqblk_sz))
604 ddquot.dqb_itime = cpu_to_le64(1);
607 ret = filp->f_op->write(filp, (char *)&ddquot,
610 if (ret != dqblk_sz) {
611 CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
612 filp->f_dentry->d_sb->s_id);
622 * Free dquot entry in data block
624 static int free_dqentry(struct lustre_dquot *dquot, uint blk,
625 lustre_quota_version_t version)
627 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
628 struct lustre_mem_dqinfo *info =
629 &dquot->dq_info->qi_info[dquot->dq_type];
630 struct lustre_disk_dqdbheader *dh;
631 dqbuf_t buf = getdqbuf();
632 int dqstrinblk = lustre_dqstrinblk[version];
637 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
639 "VFS: Quota structure has offset to other block (%u) "
640 "than it should (%u).\n",
641 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
644 if ((ret = read_blk(filp, blk, buf)) < 0) {
645 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
648 dh = (struct lustre_disk_dqdbheader *)buf;
649 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
650 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
651 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
652 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
654 "VFS: Can't move quota data block (%u) to free "
659 memset(buf + (dquot->dq_off & ((1<<LUSTRE_DQBLKSIZE_BITS) - 1)),
660 0, lustre_disk_dqblk_sz[version]);
661 if (le16_to_cpu(dh->dqdh_entries) == dqstrinblk - 1) {
662 /* Insert will write block itself */
664 insert_free_dqentry(filp, info, buf, blk)) < 0) {
666 "VFS: Can't insert quota data block "
667 "(%u) to free entry list.\n", blk);
670 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
672 "VFS: Can't write quota data block %u\n", blk);
676 dquot->dq_off = 0; /* Quota is now unattached */
683 * Remove reference to dquot from tree
685 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
686 lustre_quota_version_t version)
688 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
689 struct lustre_mem_dqinfo *info =
690 &dquot->dq_info->qi_info[dquot->dq_type];
691 dqbuf_t buf = getdqbuf();
694 u32 *ref = (u32 *) buf;
698 if ((ret = read_blk(filp, *blk, buf)) < 0) {
699 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", *blk);
702 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
703 if (depth == LUSTRE_DQTREEDEPTH - 1) {
704 ret = free_dqentry(dquot, newblk, version);
707 ret = remove_tree(dquot, &newblk, depth + 1, version);
708 if (ret >= 0 && !newblk) {
710 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
711 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++)
712 /* Block got empty? */ ;
713 /* don't put the root block into free blk list! */
714 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
715 put_free_dqblk(filp, info, buf, *blk);
717 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
719 "VFS: Can't write quota tree block %u.\n", *blk);
727 * Delete dquot from tree
729 static int lustre_delete_dquot(struct lustre_dquot *dquot,
730 lustre_quota_version_t version)
732 uint tmp = LUSTRE_DQTREEOFF;
734 if (!dquot->dq_off) /* Even not allocated? */
736 return remove_tree(dquot, &tmp, 0, version);
740 * Find entry in block
742 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
743 lustre_quota_version_t version)
745 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
746 dqbuf_t buf = getdqbuf();
749 struct lustre_disk_dqblk_v2 *ddquot =
750 (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
751 int dqblk_sz = lustre_disk_dqblk_sz[version];
752 int dqstrinblk = lustre_dqstrinblk[version];
754 LASSERT(version == LUSTRE_QUOTA_V2);
758 if ((ret = read_blk(filp, blk, buf)) < 0) {
759 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
763 for (i = 0; i < dqstrinblk &&
764 le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id;
766 else { /* ID 0 as a bit more complicated searching... */
767 for (i = 0; i < dqstrinblk; i++)
768 if (!le32_to_cpu(ddquot[i].dqb_id)
769 && memcmp((char *)&emptydquot[version],
770 (char *)&ddquot[i], dqblk_sz))
773 if (i == dqstrinblk) {
775 "VFS: Quota for id %u referenced but not present.\n",
781 (blk << LUSTRE_DQBLKSIZE_BITS) +
782 sizeof(struct lustre_disk_dqdbheader) +
790 * Find entry for given id in the tree
792 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
793 lustre_quota_version_t version)
795 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
796 dqbuf_t buf = getdqbuf();
798 u32 *ref = (u32 *) buf;
802 if ((ret = read_blk(filp, blk, buf)) < 0) {
803 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
807 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
808 if (!blk) /* No reference? */
810 if (depth < LUSTRE_DQTREEDEPTH - 1)
811 ret = find_tree_dqentry(dquot, blk, depth + 1, version);
813 ret = find_block_dqentry(dquot, blk, version);
820 * Find entry for given id in the tree - wrapper function
822 static inline loff_t find_dqentry(struct lustre_dquot *dquot,
823 lustre_quota_version_t version)
825 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
828 int lustre_read_dquot(struct lustre_dquot *dquot)
830 int type = dquot->dq_type;
834 int ret = 0, dqblk_sz;
835 lustre_quota_version_t version;
837 /* Invalidated quota? */
838 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
839 CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
843 version = dquot->dq_info->qi_version;
844 LASSERT(version == LUSTRE_QUOTA_V2);
845 dqblk_sz = lustre_disk_dqblk_sz[version];
847 offset = find_dqentry(dquot, version);
848 if (offset <= 0) { /* Entry not present? */
851 "VFS: Can't read quota structure for id %u.\n",
854 set_bit(DQ_FAKE_B, &dquot->dq_flags);
855 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
858 struct lustre_disk_dqblk_v2 ddquot;
860 dquot->dq_off = offset;
863 if ((ret = filp->f_op->read(filp, (char *)&ddquot,
864 dqblk_sz, &offset)) != dqblk_sz) {
868 "VFS: Error while reading quota structure for id "
869 "%u.\n", dquot->dq_id);
870 memset((char *)&ddquot, 0, dqblk_sz);
873 /* We need to escape back all-zero structure */
874 if (!memcmp((char *)&fakedquot[version],
875 (char *)&ddquot, dqblk_sz))
876 ddquot.dqb_itime = cpu_to_le64(0);
879 disk2memdqb(&dquot->dq_dqb, &ddquot, version);
886 * Commit changes of dquot to disk - it might also mean deleting
887 * it when quota became fake.
889 int lustre_commit_dquot(struct lustre_dquot *dquot)
892 lustre_quota_version_t version = dquot->dq_info->qi_version;
894 /* always clear the flag so we don't loop on an IO error... */
895 clear_bit(DQ_MOD_B, &dquot->dq_flags);
897 /* The block/inode usage in admin quotafile isn't the real usage
898 * over all cluster, so keep the fake dquot entry on disk is
899 * meaningless, just remove it */
900 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
901 rc = lustre_delete_dquot(dquot, version);
903 rc = lustre_write_dquot(dquot, version);
908 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
909 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
914 int lustre_init_quota_header(struct lustre_quota_info *lqi, int type,
917 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
918 static const uint fake_magics[] = LUSTRE_BADQMAGICS;
919 const uint* quota_versions = lustre_initqversions[lqi->qi_version];
920 struct lustre_disk_dqheader dqhead;
923 struct file *fp = lqi->qi_files[type];
926 /* write quotafile header */
927 dqhead.dqh_magic = cpu_to_le32(fakemagics ?
928 fake_magics[type] : quota_magics[type]);
929 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
930 size = fp->f_op->write(fp, (char *)&dqhead,
931 sizeof(struct lustre_disk_dqheader), &offset);
933 if (size != sizeof(struct lustre_disk_dqheader)) {
934 CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
942 * We need to export this function to initialize quotafile, because we haven't
943 * user level check utility
945 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
948 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
951 rc = lustre_init_quota_header(lqi, type, fakemagics);
955 /* write init quota info */
956 memset(dqinfo, 0, sizeof(*dqinfo));
957 dqinfo->dqi_bgrace = MAX_DQ_TIME;
958 dqinfo->dqi_igrace = MAX_IQ_TIME;
959 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
961 return lustre_write_quota_info(lqi, type);
964 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
966 return lustre_init_quota_info_generic(lqi, type, 0);
969 ssize_t quota_read(struct file *file, struct inode *inode, int type,
970 uint blk, dqbuf_t buf)
973 return read_blk(file, blk, buf);
975 #ifndef KERNEL_SUPPORTS_QUOTA_READ
978 struct super_block *sb = inode->i_sb;
979 memset(buf, 0, LUSTRE_DQBLKSIZE);
980 return sb->s_op->quota_read(sb, type, (char *)buf,
982 blk << LUSTRE_DQBLKSIZE_BITS);
987 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
988 uint blk, struct list_head *list)
990 dqbuf_t buf = getdqbuf();
992 struct lustre_disk_dqdbheader *dqhead =
993 (struct lustre_disk_dqdbheader *)buf;
994 struct dqblk *blk_item;
996 struct list_head *tmp;
1000 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1001 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1006 if (!le32_to_cpu(dqhead->dqdh_entries))
1009 if (list_empty(list)) {
1014 list_for_each_entry(pos, list, link) {
1015 if (blk == pos->blk) /* we got this blk already */
1023 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
1028 blk_item->blk = blk;
1029 INIT_LIST_HEAD(&blk_item->link);
1031 list_add_tail(&blk_item->link, tmp);
1038 int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
1039 uint blk, int depth, struct list_head *list)
1041 dqbuf_t buf = getdqbuf();
1044 u32 *ref = (u32 *) buf;
1048 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1049 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1054 for (index = 0; index <= 0xff && !ret; index++) {
1055 blk = le32_to_cpu(ref[index]);
1056 if (!blk) /* No reference */
1059 if (depth < LUSTRE_DQTREEDEPTH - 1)
1060 ret = walk_tree_dqentry(filp, inode, type, blk,
1063 ret = walk_block_dqentry(filp, inode, type, blk, list);
1071 * Walk through the quota file (v2 format) to get all ids with quota limit
1073 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
1074 struct list_head *list)
1076 struct list_head blk_list;
1077 struct dqblk *blk_item, *tmp;
1079 struct lustre_disk_dqblk_v2 *ddquot;
1081 lustre_quota_version_t version;
1085 if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
1086 version = LUSTRE_QUOTA_V2;
1088 CDEBUG(D_ERROR, "unknown quota file format!\n");
1092 if (!list_empty(list)) {
1093 CDEBUG(D_ERROR, "not empty list\n");
1097 INIT_LIST_HEAD(&blk_list);
1098 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
1100 CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
1103 if (list_empty(&blk_list))
1109 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
1111 list_for_each_entry(blk_item, &blk_list, link) {
1113 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
1115 memset(buf, 0, LUSTRE_DQBLKSIZE);
1116 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
1118 "VFS: Can't read quota tree block %u.\n",
1120 GOTO(out_free, rc = ret);
1123 for (i = 0; i < lustre_dqstrinblk[version]; i++) {
1124 struct dquot_id *dqid;
1125 /* skip empty entry */
1126 if (!memcmp((char *)&emptydquot[version],
1127 (char *)&ddquot[i], dqblk_sz))
1130 dqid = kmalloc(sizeof(*dqid), GFP_NOFS);
1132 GOTO(out_free, rc = -ENOMEM);
1134 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
1135 INIT_LIST_HEAD(&dqid->di_link);
1136 list_add(&dqid->di_link, list);
1141 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1142 list_del_init(&blk_item->link);
1152 EXPORT_SYMBOL(lustre_read_quota_info);
1153 EXPORT_SYMBOL(lustre_write_quota_info);
1154 EXPORT_SYMBOL(lustre_check_quota_file);
1155 EXPORT_SYMBOL(lustre_read_dquot);
1156 EXPORT_SYMBOL(lustre_commit_dquot);
1157 EXPORT_SYMBOL(lustre_init_quota_info);
1158 EXPORT_SYMBOL(lustre_get_qids);