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];
101 CERROR("check_quota_file failed!\n");
102 libcfs_debug_dumpstack(NULL);
109 size = f->f_op->read(f, (char *)&dqhead,
110 sizeof(struct lustre_disk_dqheader),
114 #ifndef KERNEL_SUPPORTS_QUOTA_READ
117 struct super_block *sb = inode->i_sb;
118 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
119 sizeof(struct lustre_disk_dqheader),
123 if (size != sizeof(struct lustre_disk_dqheader))
125 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
126 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
132 * Check whether given file is really lustre admin quotafile
134 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
136 struct file *f = lqi->qi_files[type];
137 return check_quota_file(f, NULL, type, lqi->qi_version);
140 int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
143 struct lustre_disk_dqinfo dinfo;
145 loff_t offset = LUSTRE_DQINFOOFF;
149 size = f->f_op->read(f, (char *)&dinfo,
150 sizeof(struct lustre_disk_dqinfo), &offset);
152 if (size != sizeof(struct lustre_disk_dqinfo)) {
153 CDEBUG(D_ERROR, "Can't read info structure on device %s.\n",
154 f->f_vfsmnt->mnt_sb->s_id);
157 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
158 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
159 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
160 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
161 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
162 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
167 * Read information header from quota file
169 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
171 return lustre_read_quota_file_info(lqi->qi_files[type],
172 &lqi->qi_info[type]);
176 * Write information header to quota file
178 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
181 struct lustre_disk_dqinfo dinfo;
182 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
183 struct file *f = lqi->qi_files[type];
185 loff_t offset = LUSTRE_DQINFOOFF;
187 info->dqi_flags &= ~DQF_INFO_DIRTY;
188 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
189 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
190 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
191 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
192 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
193 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
196 size = f->f_op->write(f, (char *)&dinfo,
197 sizeof(struct lustre_disk_dqinfo), &offset);
199 if (size != sizeof(struct lustre_disk_dqinfo)) {
201 "Can't write info structure on device %s.\n",
202 f->f_vfsmnt->mnt_sb->s_id);
208 void disk2memdqb(struct lustre_mem_dqblk *m, void *d,
209 lustre_quota_version_t version)
211 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
213 LASSERT(version == LUSTRE_QUOTA_V2);
215 m->dqb_ihardlimit = le64_to_cpu(dqblk->dqb_ihardlimit);
216 m->dqb_isoftlimit = le64_to_cpu(dqblk->dqb_isoftlimit);
217 m->dqb_curinodes = le64_to_cpu(dqblk->dqb_curinodes);
218 m->dqb_itime = le64_to_cpu(dqblk->dqb_itime);
219 m->dqb_bhardlimit = le64_to_cpu(dqblk->dqb_bhardlimit);
220 m->dqb_bsoftlimit = le64_to_cpu(dqblk->dqb_bsoftlimit);
221 m->dqb_curspace = le64_to_cpu(dqblk->dqb_curspace);
222 m->dqb_btime = le64_to_cpu(dqblk->dqb_btime);
225 static int mem2diskdqb(void *d, struct lustre_mem_dqblk *m,
226 qid_t id, lustre_quota_version_t version)
228 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
230 LASSERT(version == LUSTRE_QUOTA_V2);
232 dqblk->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
233 dqblk->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
234 dqblk->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
235 dqblk->dqb_itime = cpu_to_le64(m->dqb_itime);
236 dqblk->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
237 dqblk->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
238 dqblk->dqb_curspace = cpu_to_le64(m->dqb_curspace);
239 dqblk->dqb_btime = cpu_to_le64(m->dqb_btime);
240 dqblk->dqb_id = cpu_to_le32(id);
245 dqbuf_t getdqbuf(void)
247 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
250 "VFS: Not enough memory for quota buffers.\n");
254 void freedqbuf(dqbuf_t buf)
259 ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
263 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
265 memset(buf, 0, LUSTRE_DQBLKSIZE);
268 ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
273 ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
277 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
281 ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
286 void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
288 set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
292 * Remove empty block from list and return it
294 int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
296 dqbuf_t buf = getdqbuf();
297 struct lustre_disk_dqdbheader *dh =
298 (struct lustre_disk_dqdbheader *)buf;
303 if (info->dqi_free_blk) {
304 blk = info->dqi_free_blk;
305 if ((ret = read_blk(filp, blk, buf)) < 0)
307 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
309 memset(buf, 0, LUSTRE_DQBLKSIZE);
310 /* Assure block allocation... */
311 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0)
313 blk = info->dqi_blocks++;
315 lustre_mark_info_dirty(info);
323 * Insert empty block to the list
325 int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
326 dqbuf_t buf, uint blk)
328 struct lustre_disk_dqdbheader *dh =
329 (struct lustre_disk_dqdbheader *)buf;
332 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
333 dh->dqdh_prev_free = cpu_to_le32(0);
334 dh->dqdh_entries = cpu_to_le16(0);
335 info->dqi_free_blk = blk;
336 lustre_mark_info_dirty(info);
337 if ((err = write_blk(filp, blk, buf)) < 0)
338 /* Some strange block. We had better leave it... */
344 * Remove given block from the list of blocks with free entries
346 int remove_free_dqentry(struct file *filp,
347 struct lustre_mem_dqinfo *info, dqbuf_t buf,
350 dqbuf_t tmpbuf = getdqbuf();
351 struct lustre_disk_dqdbheader *dh =
352 (struct lustre_disk_dqdbheader *)buf;
353 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
354 le32_to_cpu(dh->dqdh_prev_free);
360 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
362 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
364 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
368 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
370 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
372 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
375 info->dqi_free_entry = nextblk;
376 lustre_mark_info_dirty(info);
379 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
380 if (write_blk(filp, blk, buf) < 0)
381 /* No matter whether write succeeds block is out of list */
383 "VFS: Can't write block (%u) with free entries.\n", blk);
391 * Insert given block to the beginning of list with free entries
393 int insert_free_dqentry(struct file *filp,
394 struct lustre_mem_dqinfo *info, dqbuf_t buf,
397 dqbuf_t tmpbuf = getdqbuf();
398 struct lustre_disk_dqdbheader *dh =
399 (struct lustre_disk_dqdbheader *)buf;
404 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
405 dh->dqdh_prev_free = cpu_to_le32(0);
406 if ((err = write_blk(filp, blk, buf)) < 0)
408 if (info->dqi_free_entry) {
409 if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
411 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
413 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
417 info->dqi_free_entry = blk;
418 lustre_mark_info_dirty(info);
428 * Find space for dquot
430 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
431 lustre_quota_version_t version)
433 struct lustre_quota_info *lqi = dquot->dq_info;
434 struct file *filp = lqi->qi_files[dquot->dq_type];
435 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
437 struct lustre_disk_dqdbheader *dh;
439 int dqblk_sz = lustre_disk_dqblk_sz[version];
440 int dqstrinblk = lustre_dqstrinblk[version];
444 if (!(buf = getdqbuf())) {
448 dh = (struct lustre_disk_dqdbheader *)buf;
449 ddquot = GETENTRIES(buf, version);
450 if (info->dqi_free_entry) {
451 blk = info->dqi_free_entry;
452 if ((*err = read_blk(filp, blk, buf)) < 0)
455 blk = get_free_dqblk(filp, info);
461 memset(buf, 0, LUSTRE_DQBLKSIZE);
462 info->dqi_free_entry = blk; /* This is enough as block is
463 already zeroed and entry list
465 lustre_mark_info_dirty(info);
468 /* Will block be full */
469 if (le16_to_cpu(dh->dqdh_entries) + 1 >= dqstrinblk)
470 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
472 "VFS: find_free_dqentry(): Can't remove block "
473 "(%u) from entry free list.\n", blk);
476 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
477 /* Find free structure in block */
478 for (i = 0; i < dqstrinblk &&
479 memcmp((char *)&emptydquot[version],
480 (char *)ddquot + i * dqblk_sz, dqblk_sz);
483 if (i == dqstrinblk) {
485 "VFS: find_free_dqentry(): Data block full but it "
491 if ((*err = write_blk(filp, blk, buf)) < 0) {
493 "VFS: find_free_dqentry(): Can't write quota data "
498 (blk << LUSTRE_DQBLKSIZE_BITS) +
499 sizeof(struct lustre_disk_dqdbheader) +
509 * Insert reference to structure into the trie
511 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
512 lustre_quota_version_t version)
514 struct lustre_quota_info *lqi = dquot->dq_info;
515 struct file *filp = lqi->qi_files[dquot->dq_type];
516 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
518 int ret = 0, newson = 0, newact = 0;
522 if (!(buf = getdqbuf()))
525 ret = get_free_dqblk(filp, info);
529 memset(buf, 0, LUSTRE_DQBLKSIZE);
532 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
534 "VFS: Can't read tree quota block %u.\n",
540 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
543 if (depth == LUSTRE_DQTREEDEPTH - 1) {
547 "VFS: Inserting already present quota entry "
549 ref[GETIDINDEX(dquot->dq_id, depth)]);
554 newblk = find_free_dqentry(dquot, &ret, version);
556 ret = do_insert_tree(dquot, &newblk, depth + 1, version);
557 if (newson && ret >= 0) {
558 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
559 ret = write_blk(filp, *treeblk, buf);
560 } else if (newact && ret < 0)
561 put_free_dqblk(filp, info, buf, *treeblk);
568 * Wrapper for inserting quota structure into tree
570 static inline int dq_insert_tree(struct lustre_dquot *dquot,
571 lustre_quota_version_t version)
573 int tmp = LUSTRE_DQTREEOFF;
574 return do_insert_tree(dquot, &tmp, 0, version);
578 * We don't have to be afraid of deadlocks as we never have quotas on
581 static int lustre_write_dquot(struct lustre_dquot *dquot,
582 lustre_quota_version_t version)
584 int type = dquot->dq_type;
589 int dqblk_sz = lustre_disk_dqblk_sz[version];
590 struct lustre_disk_dqblk_v2 ddquot;
592 ret = mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id, version);
597 if ((ret = dq_insert_tree(dquot, version)) < 0) {
599 "VFS: Error %Zd occurred while creating "
603 filp = dquot->dq_info->qi_files[type];
604 offset = dquot->dq_off;
605 /* Argh... We may need to write structure full of zeroes but that would
606 * be treated as an empty place by the rest of the code. Format change
607 * would be definitely cleaner but the problems probably are not worth
609 if (!memcmp((char *)&emptydquot[version], (char *)&ddquot, dqblk_sz))
610 ddquot.dqb_itime = cpu_to_le64(1);
613 ret = filp->f_op->write(filp, (char *)&ddquot,
616 if (ret != dqblk_sz) {
617 CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
618 filp->f_dentry->d_sb->s_id);
628 * Free dquot entry in data block
630 static int free_dqentry(struct lustre_dquot *dquot, uint blk,
631 lustre_quota_version_t version)
633 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
634 struct lustre_mem_dqinfo *info =
635 &dquot->dq_info->qi_info[dquot->dq_type];
636 struct lustre_disk_dqdbheader *dh;
637 dqbuf_t buf = getdqbuf();
638 int dqstrinblk = lustre_dqstrinblk[version];
643 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
645 "VFS: Quota structure has offset to other block (%u) "
646 "than it should (%u).\n",
647 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
650 if ((ret = read_blk(filp, blk, buf)) < 0) {
651 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
654 dh = (struct lustre_disk_dqdbheader *)buf;
655 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
656 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
657 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
658 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
660 "VFS: Can't move quota data block (%u) to free "
665 memset(buf + (dquot->dq_off & ((1<<LUSTRE_DQBLKSIZE_BITS) - 1)),
666 0, lustre_disk_dqblk_sz[version]);
667 if (le16_to_cpu(dh->dqdh_entries) == dqstrinblk - 1) {
668 /* Insert will write block itself */
670 insert_free_dqentry(filp, info, buf, blk)) < 0) {
672 "VFS: Can't insert quota data block "
673 "(%u) to free entry list.\n", blk);
676 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
678 "VFS: Can't write quota data block %u\n", blk);
682 dquot->dq_off = 0; /* Quota is now unattached */
689 * Remove reference to dquot from tree
691 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
692 lustre_quota_version_t version)
694 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
695 struct lustre_mem_dqinfo *info =
696 &dquot->dq_info->qi_info[dquot->dq_type];
697 dqbuf_t buf = getdqbuf();
700 u32 *ref = (u32 *) buf;
704 if ((ret = read_blk(filp, *blk, buf)) < 0) {
705 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", *blk);
708 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
709 if (depth == LUSTRE_DQTREEDEPTH - 1) {
710 ret = free_dqentry(dquot, newblk, version);
713 ret = remove_tree(dquot, &newblk, depth + 1, version);
714 if (ret >= 0 && !newblk) {
716 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
717 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++)
718 /* Block got empty? */ ;
719 /* don't put the root block into free blk list! */
720 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
721 put_free_dqblk(filp, info, buf, *blk);
723 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
725 "VFS: Can't write quota tree block %u.\n", *blk);
733 * Delete dquot from tree
735 static int lustre_delete_dquot(struct lustre_dquot *dquot,
736 lustre_quota_version_t version)
738 uint tmp = LUSTRE_DQTREEOFF;
740 if (!dquot->dq_off) /* Even not allocated? */
742 return remove_tree(dquot, &tmp, 0, version);
746 * Find entry in block
748 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
749 lustre_quota_version_t version)
751 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
752 dqbuf_t buf = getdqbuf();
755 struct lustre_disk_dqblk_v2 *ddquot =
756 (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
757 int dqblk_sz = lustre_disk_dqblk_sz[version];
758 int dqstrinblk = lustre_dqstrinblk[version];
760 LASSERT(version == LUSTRE_QUOTA_V2);
764 if ((ret = read_blk(filp, blk, buf)) < 0) {
765 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
769 for (i = 0; i < dqstrinblk &&
770 le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id;
772 else { /* ID 0 as a bit more complicated searching... */
773 for (i = 0; i < dqstrinblk; i++)
774 if (!le32_to_cpu(ddquot[i].dqb_id)
775 && memcmp((char *)&emptydquot[version],
776 (char *)&ddquot[i], dqblk_sz))
779 if (i == dqstrinblk) {
781 "VFS: Quota for id %u referenced but not present.\n",
787 (blk << LUSTRE_DQBLKSIZE_BITS) +
788 sizeof(struct lustre_disk_dqdbheader) +
796 * Find entry for given id in the tree
798 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
799 lustre_quota_version_t version)
801 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
802 dqbuf_t buf = getdqbuf();
804 u32 *ref = (u32 *) buf;
808 if ((ret = read_blk(filp, blk, buf)) < 0) {
809 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
813 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
814 if (!blk) /* No reference? */
816 if (depth < LUSTRE_DQTREEDEPTH - 1)
817 ret = find_tree_dqentry(dquot, blk, depth + 1, version);
819 ret = find_block_dqentry(dquot, blk, version);
826 * Find entry for given id in the tree - wrapper function
828 static inline loff_t find_dqentry(struct lustre_dquot *dquot,
829 lustre_quota_version_t version)
831 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
834 int lustre_read_dquot(struct lustre_dquot *dquot)
836 int type = dquot->dq_type;
840 int ret = 0, dqblk_sz;
841 lustre_quota_version_t version;
843 /* Invalidated quota? */
844 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
845 CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
849 version = dquot->dq_info->qi_version;
850 LASSERT(version == LUSTRE_QUOTA_V2);
851 dqblk_sz = lustre_disk_dqblk_sz[version];
853 offset = find_dqentry(dquot, version);
854 if (offset <= 0) { /* Entry not present? */
857 "VFS: Can't read quota structure for id %u.\n",
860 set_bit(DQ_FAKE_B, &dquot->dq_flags);
861 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
864 struct lustre_disk_dqblk_v2 ddquot;
866 dquot->dq_off = offset;
869 if ((ret = filp->f_op->read(filp, (char *)&ddquot,
870 dqblk_sz, &offset)) != dqblk_sz) {
874 "VFS: Error while reading quota structure for id "
875 "%u.\n", dquot->dq_id);
876 memset((char *)&ddquot, 0, dqblk_sz);
879 /* We need to escape back all-zero structure */
880 if (!memcmp((char *)&fakedquot[version],
881 (char *)&ddquot, dqblk_sz))
882 ddquot.dqb_itime = cpu_to_le64(0);
885 disk2memdqb(&dquot->dq_dqb, &ddquot, version);
892 * Commit changes of dquot to disk - it might also mean deleting
893 * it when quota became fake.
895 int lustre_commit_dquot(struct lustre_dquot *dquot)
898 lustre_quota_version_t version = dquot->dq_info->qi_version;
900 /* always clear the flag so we don't loop on an IO error... */
901 clear_bit(DQ_MOD_B, &dquot->dq_flags);
903 /* The block/inode usage in admin quotafile isn't the real usage
904 * over all cluster, so keep the fake dquot entry on disk is
905 * meaningless, just remove it */
906 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
907 rc = lustre_delete_dquot(dquot, version);
909 rc = lustre_write_dquot(dquot, version);
914 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
915 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
920 int lustre_init_quota_header(struct lustre_quota_info *lqi, int type,
923 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
924 static const uint fake_magics[] = LUSTRE_BADQMAGICS;
925 const uint* quota_versions = lustre_initqversions[lqi->qi_version];
926 struct lustre_disk_dqheader dqhead;
929 struct file *fp = lqi->qi_files[type];
932 /* write quotafile header */
933 dqhead.dqh_magic = cpu_to_le32(fakemagics ?
934 fake_magics[type] : quota_magics[type]);
935 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
936 size = fp->f_op->write(fp, (char *)&dqhead,
937 sizeof(struct lustre_disk_dqheader), &offset);
939 if (size != sizeof(struct lustre_disk_dqheader)) {
940 CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
948 * We need to export this function to initialize quotafile, because we haven't
949 * user level check utility
951 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
954 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
957 rc = lustre_init_quota_header(lqi, type, fakemagics);
961 /* write init quota info */
962 memset(dqinfo, 0, sizeof(*dqinfo));
963 dqinfo->dqi_bgrace = MAX_DQ_TIME;
964 dqinfo->dqi_igrace = MAX_IQ_TIME;
965 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
967 return lustre_write_quota_info(lqi, type);
970 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
972 return lustre_init_quota_info_generic(lqi, type, 0);
975 ssize_t quota_read(struct file *file, struct inode *inode, int type,
976 uint blk, dqbuf_t buf)
979 return read_blk(file, blk, buf);
981 #ifndef KERNEL_SUPPORTS_QUOTA_READ
984 struct super_block *sb = inode->i_sb;
985 memset(buf, 0, LUSTRE_DQBLKSIZE);
986 return sb->s_op->quota_read(sb, type, (char *)buf,
988 blk << LUSTRE_DQBLKSIZE_BITS);
993 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
994 uint blk, struct list_head *list)
996 dqbuf_t buf = getdqbuf();
998 struct lustre_disk_dqdbheader *dqhead =
999 (struct lustre_disk_dqdbheader *)buf;
1000 struct dqblk *blk_item;
1002 struct list_head *tmp;
1006 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1007 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1012 if (!le32_to_cpu(dqhead->dqdh_entries))
1015 if (list_empty(list)) {
1020 list_for_each_entry(pos, list, link) {
1021 if (blk == pos->blk) /* we got this blk already */
1029 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
1034 blk_item->blk = blk;
1035 INIT_LIST_HEAD(&blk_item->link);
1037 list_add_tail(&blk_item->link, tmp);
1044 int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
1045 uint blk, int depth, struct list_head *list)
1047 dqbuf_t buf = getdqbuf();
1050 u32 *ref = (u32 *) buf;
1054 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1055 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1060 for (index = 0; index <= 0xff && !ret; index++) {
1061 blk = le32_to_cpu(ref[index]);
1062 if (!blk) /* No reference */
1065 if (depth < LUSTRE_DQTREEDEPTH - 1)
1066 ret = walk_tree_dqentry(filp, inode, type, blk,
1069 ret = walk_block_dqentry(filp, inode, type, blk, list);
1077 * Walk through the quota file (v2 format) to get all ids with quota limit
1079 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
1080 struct list_head *list)
1082 struct list_head blk_list;
1083 struct dqblk *blk_item, *tmp;
1085 struct lustre_disk_dqblk_v2 *ddquot;
1087 lustre_quota_version_t version;
1091 LASSERT(ergo(fp == NULL, inode != NULL));
1093 if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
1094 version = LUSTRE_QUOTA_V2;
1096 CDEBUG(D_ERROR, "unknown quota file format!\n");
1100 if (!list_empty(list)) {
1101 CDEBUG(D_ERROR, "not empty list\n");
1105 INIT_LIST_HEAD(&blk_list);
1106 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
1108 CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
1111 if (list_empty(&blk_list))
1117 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
1119 list_for_each_entry(blk_item, &blk_list, link) {
1121 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
1123 memset(buf, 0, LUSTRE_DQBLKSIZE);
1124 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
1126 "VFS: Can't read quota tree block %u.\n",
1128 GOTO(out_free, rc = ret);
1131 for (i = 0; i < lustre_dqstrinblk[version]; i++) {
1132 struct dquot_id *dqid;
1133 /* skip empty entry */
1134 if (!memcmp((char *)&emptydquot[version],
1135 (char *)&ddquot[i], dqblk_sz))
1138 OBD_ALLOC_GFP(dqid, sizeof(*dqid), GFP_NOFS);
1140 GOTO(out_free, rc = -ENOMEM);
1142 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
1143 dqid->di_flag = le64_to_cpu(ddquot[i].dqb_ihardlimit) ?
1145 dqid->di_flag |= le64_to_cpu(ddquot[i].dqb_bhardlimit) ?
1148 INIT_LIST_HEAD(&dqid->di_link);
1149 list_add(&dqid->di_link, list);
1154 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1155 list_del_init(&blk_item->link);
1165 EXPORT_SYMBOL(lustre_read_quota_info);
1166 EXPORT_SYMBOL(lustre_write_quota_info);
1167 EXPORT_SYMBOL(lustre_check_quota_file);
1168 EXPORT_SYMBOL(lustre_read_dquot);
1169 EXPORT_SYMBOL(lustre_commit_dquot);
1170 EXPORT_SYMBOL(lustre_init_quota_info);
1171 EXPORT_SYMBOL(lustre_get_qids);