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 char ddquot[dqblk_sz];
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], ddquot, dqblk_sz))
604 ((struct lustre_disk_dqblk_v2 *)ddquot)->dqb_itime =
608 ret = filp->f_op->write(filp, ddquot,
611 if (ret != dqblk_sz) {
612 CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
613 filp->f_dentry->d_sb->s_id);
623 * Free dquot entry in data block
625 static int free_dqentry(struct lustre_dquot *dquot, uint blk,
626 lustre_quota_version_t version)
628 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
629 struct lustre_mem_dqinfo *info =
630 &dquot->dq_info->qi_info[dquot->dq_type];
631 struct lustre_disk_dqdbheader *dh;
632 dqbuf_t buf = getdqbuf();
633 int dqstrinblk = lustre_dqstrinblk[version];
638 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
640 "VFS: Quota structure has offset to other block (%u) "
641 "than it should (%u).\n",
642 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
645 if ((ret = read_blk(filp, blk, buf)) < 0) {
646 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
649 dh = (struct lustre_disk_dqdbheader *)buf;
650 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
651 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
652 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
653 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
655 "VFS: Can't move quota data block (%u) to free "
660 memset(buf + (dquot->dq_off & ((1<<LUSTRE_DQBLKSIZE_BITS) - 1)),
661 0, lustre_disk_dqblk_sz[version]);
662 if (le16_to_cpu(dh->dqdh_entries) == dqstrinblk - 1) {
663 /* Insert will write block itself */
665 insert_free_dqentry(filp, info, buf, blk)) < 0) {
667 "VFS: Can't insert quota data block "
668 "(%u) to free entry list.\n", blk);
671 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
673 "VFS: Can't write quota data block %u\n", blk);
677 dquot->dq_off = 0; /* Quota is now unattached */
684 * Remove reference to dquot from tree
686 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
687 lustre_quota_version_t version)
689 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
690 struct lustre_mem_dqinfo *info =
691 &dquot->dq_info->qi_info[dquot->dq_type];
692 dqbuf_t buf = getdqbuf();
695 u32 *ref = (u32 *) buf;
699 if ((ret = read_blk(filp, *blk, buf)) < 0) {
700 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", *blk);
703 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
704 if (depth == LUSTRE_DQTREEDEPTH - 1) {
705 ret = free_dqentry(dquot, newblk, version);
708 ret = remove_tree(dquot, &newblk, depth + 1, version);
709 if (ret >= 0 && !newblk) {
711 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
712 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++)
713 /* Block got empty? */ ;
714 /* don't put the root block into free blk list! */
715 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
716 put_free_dqblk(filp, info, buf, *blk);
718 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
720 "VFS: Can't write quota tree block %u.\n", *blk);
728 * Delete dquot from tree
730 static int lustre_delete_dquot(struct lustre_dquot *dquot,
731 lustre_quota_version_t version)
733 uint tmp = LUSTRE_DQTREEOFF;
735 if (!dquot->dq_off) /* Even not allocated? */
737 return remove_tree(dquot, &tmp, 0, version);
741 * Find entry in block
743 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
744 lustre_quota_version_t version)
746 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
747 dqbuf_t buf = getdqbuf();
750 struct lustre_disk_dqblk_v2 *ddquot =
751 (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
752 int dqblk_sz = lustre_disk_dqblk_sz[version];
753 int dqstrinblk = lustre_dqstrinblk[version];
755 LASSERT(version == LUSTRE_QUOTA_V2);
759 if ((ret = read_blk(filp, blk, buf)) < 0) {
760 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
764 for (i = 0; i < dqstrinblk &&
765 le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id;
767 else { /* ID 0 as a bit more complicated searching... */
768 for (i = 0; i < dqstrinblk; i++)
769 if (!le32_to_cpu(ddquot[i].dqb_id)
770 && memcmp((char *)&emptydquot[version],
771 (char *)&ddquot[i], dqblk_sz))
774 if (i == dqstrinblk) {
776 "VFS: Quota for id %u referenced but not present.\n",
782 (blk << LUSTRE_DQBLKSIZE_BITS) +
783 sizeof(struct lustre_disk_dqdbheader) +
791 * Find entry for given id in the tree
793 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
794 lustre_quota_version_t version)
796 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
797 dqbuf_t buf = getdqbuf();
799 u32 *ref = (u32 *) buf;
803 if ((ret = read_blk(filp, blk, buf)) < 0) {
804 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
808 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
809 if (!blk) /* No reference? */
811 if (depth < LUSTRE_DQTREEDEPTH - 1)
812 ret = find_tree_dqentry(dquot, blk, depth + 1, version);
814 ret = find_block_dqentry(dquot, blk, version);
821 * Find entry for given id in the tree - wrapper function
823 static inline loff_t find_dqentry(struct lustre_dquot *dquot,
824 lustre_quota_version_t version)
826 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
829 int lustre_read_dquot(struct lustre_dquot *dquot)
831 int type = dquot->dq_type;
835 int ret = 0, dqblk_sz;
836 lustre_quota_version_t version;
838 /* Invalidated quota? */
839 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
840 CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
844 version = dquot->dq_info->qi_version;
845 LASSERT(version == LUSTRE_QUOTA_V2);
846 dqblk_sz = lustre_disk_dqblk_sz[version];
848 offset = find_dqentry(dquot, version);
849 if (offset <= 0) { /* Entry not present? */
852 "VFS: Can't read quota structure for id %u.\n",
855 set_bit(DQ_FAKE_B, &dquot->dq_flags);
856 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
859 char ddquot[dqblk_sz];
861 dquot->dq_off = offset;
864 if ((ret = filp->f_op->read(filp, ddquot, dqblk_sz, &offset)) !=
869 "VFS: Error while reading quota structure for id "
870 "%u.\n", dquot->dq_id);
871 memset(ddquot, 0, dqblk_sz);
874 /* We need to escape back all-zero structure */
875 if (!memcmp((char *)&fakedquot[version],
877 ((struct lustre_disk_dqblk_v2 *)ddquot)->dqb_itime =
881 disk2memdqb(&dquot->dq_dqb, ddquot, version);
888 * Commit changes of dquot to disk - it might also mean deleting
889 * it when quota became fake.
891 int lustre_commit_dquot(struct lustre_dquot *dquot)
894 lustre_quota_version_t version = dquot->dq_info->qi_version;
896 /* always clear the flag so we don't loop on an IO error... */
897 clear_bit(DQ_MOD_B, &dquot->dq_flags);
899 /* The block/inode usage in admin quotafile isn't the real usage
900 * over all cluster, so keep the fake dquot entry on disk is
901 * meaningless, just remove it */
902 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
903 rc = lustre_delete_dquot(dquot, version);
905 rc = lustre_write_dquot(dquot, version);
910 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
911 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
916 int lustre_init_quota_header(struct lustre_quota_info *lqi, int type,
919 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
920 static const uint fake_magics[] = LUSTRE_BADQMAGICS;
921 const uint* quota_versions = lustre_initqversions[lqi->qi_version];
922 struct lustre_disk_dqheader dqhead;
925 struct file *fp = lqi->qi_files[type];
928 /* write quotafile header */
929 dqhead.dqh_magic = cpu_to_le32(fakemagics ?
930 fake_magics[type] : quota_magics[type]);
931 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
932 size = fp->f_op->write(fp, (char *)&dqhead,
933 sizeof(struct lustre_disk_dqheader), &offset);
935 if (size != sizeof(struct lustre_disk_dqheader)) {
936 CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
944 * We need to export this function to initialize quotafile, because we haven't
945 * user level check utility
947 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
950 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
953 rc = lustre_init_quota_header(lqi, type, fakemagics);
957 /* write init quota info */
958 memset(dqinfo, 0, sizeof(*dqinfo));
959 dqinfo->dqi_bgrace = MAX_DQ_TIME;
960 dqinfo->dqi_igrace = MAX_IQ_TIME;
961 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
963 return lustre_write_quota_info(lqi, type);
966 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
968 return lustre_init_quota_info_generic(lqi, type, 0);
971 ssize_t quota_read(struct file *file, struct inode *inode, int type,
972 uint blk, dqbuf_t buf)
975 return read_blk(file, blk, buf);
977 #ifndef KERNEL_SUPPORTS_QUOTA_READ
980 struct super_block *sb = inode->i_sb;
981 memset(buf, 0, LUSTRE_DQBLKSIZE);
982 return sb->s_op->quota_read(sb, type, (char *)buf,
984 blk << LUSTRE_DQBLKSIZE_BITS);
989 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
990 uint blk, struct list_head *list)
992 dqbuf_t buf = getdqbuf();
994 struct lustre_disk_dqdbheader *dqhead =
995 (struct lustre_disk_dqdbheader *)buf;
996 struct dqblk *blk_item;
998 struct list_head *tmp;
1002 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1003 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1008 if (!le32_to_cpu(dqhead->dqdh_entries))
1011 if (list_empty(list)) {
1016 list_for_each_entry(pos, list, link) {
1017 if (blk == pos->blk) /* we got this blk already */
1025 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
1030 blk_item->blk = blk;
1031 INIT_LIST_HEAD(&blk_item->link);
1033 list_add_tail(&blk_item->link, tmp);
1040 int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
1041 uint blk, int depth, struct list_head *list)
1043 dqbuf_t buf = getdqbuf();
1046 u32 *ref = (u32 *) buf;
1050 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1051 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1056 for (index = 0; index <= 0xff && !ret; index++) {
1057 blk = le32_to_cpu(ref[index]);
1058 if (!blk) /* No reference */
1061 if (depth < LUSTRE_DQTREEDEPTH - 1)
1062 ret = walk_tree_dqentry(filp, inode, type, blk,
1065 ret = walk_block_dqentry(filp, inode, type, blk, list);
1073 * Walk through the quota file (v2 format) to get all ids with quota limit
1075 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
1076 struct list_head *list)
1078 struct list_head blk_list;
1079 struct dqblk *blk_item, *tmp;
1081 struct lustre_disk_dqblk_v2 *ddquot;
1083 lustre_quota_version_t version;
1087 if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
1088 version = LUSTRE_QUOTA_V2;
1090 CDEBUG(D_ERROR, "unknown quota file format!\n");
1094 if (!list_empty(list)) {
1095 CDEBUG(D_ERROR, "not empty list\n");
1099 INIT_LIST_HEAD(&blk_list);
1100 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
1102 CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
1105 if (list_empty(&blk_list))
1111 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
1113 list_for_each_entry(blk_item, &blk_list, link) {
1115 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
1117 memset(buf, 0, LUSTRE_DQBLKSIZE);
1118 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
1120 "VFS: Can't read quota tree block %u.\n",
1122 GOTO(out_free, rc = ret);
1125 for (i = 0; i < lustre_dqstrinblk[version]; i++) {
1126 struct dquot_id *dqid;
1127 /* skip empty entry */
1128 if (!memcmp((char *)&emptydquot[version],
1129 (char *)&ddquot[i], dqblk_sz))
1132 dqid = kmalloc(sizeof(*dqid), GFP_NOFS);
1134 GOTO(out_free, rc = -ENOMEM);
1136 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
1137 INIT_LIST_HEAD(&dqid->di_link);
1138 list_add(&dqid->di_link, list);
1143 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1144 list_del_init(&blk_item->link);
1154 EXPORT_SYMBOL(lustre_read_quota_info);
1155 EXPORT_SYMBOL(lustre_write_quota_info);
1156 EXPORT_SYMBOL(lustre_check_quota_file);
1157 EXPORT_SYMBOL(lustre_read_dquot);
1158 EXPORT_SYMBOL(lustre_commit_dquot);
1159 EXPORT_SYMBOL(lustre_init_quota_info);
1160 EXPORT_SYMBOL(lustre_get_qids);