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), 0);
116 if (size != sizeof(struct lustre_disk_dqheader))
118 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
119 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
125 * Check whether given file is really lustre admin quotafile
127 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
129 struct file *f = lqi->qi_files[type];
130 return check_quota_file(f, NULL, type, lqi->qi_version);
133 int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
136 struct lustre_disk_dqinfo dinfo;
138 loff_t offset = LUSTRE_DQINFOOFF;
142 size = f->f_op->read(f, (char *)&dinfo,
143 sizeof(struct lustre_disk_dqinfo), &offset);
145 if (size != sizeof(struct lustre_disk_dqinfo)) {
146 CDEBUG(D_ERROR, "Can't read info structure on device %s.\n",
147 f->f_vfsmnt->mnt_sb->s_id);
150 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
151 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
152 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
153 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
154 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
155 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
160 * Read information header from quota file
162 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
164 return lustre_read_quota_file_info(lqi->qi_files[type], &lqi->qi_info[type]);
168 * Write information header to quota file
170 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
173 struct lustre_disk_dqinfo dinfo;
174 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
175 struct file *f = lqi->qi_files[type];
177 loff_t offset = LUSTRE_DQINFOOFF;
179 info->dqi_flags &= ~DQF_INFO_DIRTY;
180 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
181 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
182 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
183 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
184 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
185 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
188 size = f->f_op->write(f, (char *)&dinfo,
189 sizeof(struct lustre_disk_dqinfo), &offset);
191 if (size != sizeof(struct lustre_disk_dqinfo)) {
193 "Can't write info structure on device %s.\n",
194 f->f_vfsmnt->mnt_sb->s_id);
200 void disk2memdqb(struct lustre_mem_dqblk *m, void *d,
201 lustre_quota_version_t version)
203 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
205 LASSERT(version == LUSTRE_QUOTA_V2);
207 m->dqb_ihardlimit = le64_to_cpu(dqblk->dqb_ihardlimit);
208 m->dqb_isoftlimit = le64_to_cpu(dqblk->dqb_isoftlimit);
209 m->dqb_curinodes = le64_to_cpu(dqblk->dqb_curinodes);
210 m->dqb_itime = le64_to_cpu(dqblk->dqb_itime);
211 m->dqb_bhardlimit = le64_to_cpu(dqblk->dqb_bhardlimit);
212 m->dqb_bsoftlimit = le64_to_cpu(dqblk->dqb_bsoftlimit);
213 m->dqb_curspace = le64_to_cpu(dqblk->dqb_curspace);
214 m->dqb_btime = le64_to_cpu(dqblk->dqb_btime);
217 static int mem2diskdqb(void *d, struct lustre_mem_dqblk *m,
218 qid_t id, lustre_quota_version_t version)
220 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
222 LASSERT(version == LUSTRE_QUOTA_V2);
224 dqblk->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
225 dqblk->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
226 dqblk->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
227 dqblk->dqb_itime = cpu_to_le64(m->dqb_itime);
228 dqblk->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
229 dqblk->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
230 dqblk->dqb_curspace = cpu_to_le64(m->dqb_curspace);
231 dqblk->dqb_btime = cpu_to_le64(m->dqb_btime);
232 dqblk->dqb_id = cpu_to_le32(id);
237 dqbuf_t getdqbuf(void)
239 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
242 "VFS: Not enough memory for quota buffers.\n");
246 void freedqbuf(dqbuf_t buf)
251 ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
255 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
257 memset(buf, 0, LUSTRE_DQBLKSIZE);
260 ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
265 ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
269 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
273 ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
278 void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
280 set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
284 * Remove empty block from list and return it
286 int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
288 dqbuf_t buf = getdqbuf();
289 struct lustre_disk_dqdbheader *dh =
290 (struct lustre_disk_dqdbheader *)buf;
295 if (info->dqi_free_blk) {
296 blk = info->dqi_free_blk;
297 if ((ret = read_blk(filp, blk, buf)) < 0)
299 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
301 memset(buf, 0, LUSTRE_DQBLKSIZE);
302 /* Assure block allocation... */
303 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0)
305 blk = info->dqi_blocks++;
307 lustre_mark_info_dirty(info);
315 * Insert empty block to the list
317 int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
318 dqbuf_t buf, uint blk)
320 struct lustre_disk_dqdbheader *dh =
321 (struct lustre_disk_dqdbheader *)buf;
324 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
325 dh->dqdh_prev_free = cpu_to_le32(0);
326 dh->dqdh_entries = cpu_to_le16(0);
327 info->dqi_free_blk = blk;
328 lustre_mark_info_dirty(info);
329 if ((err = write_blk(filp, blk, buf)) < 0)
330 /* Some strange block. We had better leave it... */
336 * Remove given block from the list of blocks with free entries
338 int remove_free_dqentry(struct file *filp,
339 struct lustre_mem_dqinfo *info, dqbuf_t buf,
342 dqbuf_t tmpbuf = getdqbuf();
343 struct lustre_disk_dqdbheader *dh =
344 (struct lustre_disk_dqdbheader *)buf;
345 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
346 le32_to_cpu(dh->dqdh_prev_free);
352 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
354 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
356 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
360 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
362 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
364 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
367 info->dqi_free_entry = nextblk;
368 lustre_mark_info_dirty(info);
371 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
372 if (write_blk(filp, blk, buf) < 0)
373 /* No matter whether write succeeds block is out of list */
375 "VFS: Can't write block (%u) with free entries.\n", blk);
383 * Insert given block to the beginning of list with free entries
385 int insert_free_dqentry(struct file *filp,
386 struct lustre_mem_dqinfo *info, dqbuf_t buf,
389 dqbuf_t tmpbuf = getdqbuf();
390 struct lustre_disk_dqdbheader *dh =
391 (struct lustre_disk_dqdbheader *)buf;
396 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
397 dh->dqdh_prev_free = cpu_to_le32(0);
398 if ((err = write_blk(filp, blk, buf)) < 0)
400 if (info->dqi_free_entry) {
401 if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
403 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
405 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
409 info->dqi_free_entry = blk;
410 lustre_mark_info_dirty(info);
420 * Find space for dquot
422 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
423 lustre_quota_version_t version)
425 struct lustre_quota_info *lqi = dquot->dq_info;
426 struct file *filp = lqi->qi_files[dquot->dq_type];
427 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
429 struct lustre_disk_dqdbheader *dh;
431 int dqblk_sz = lustre_disk_dqblk_sz[version];
432 int dqstrinblk = lustre_dqstrinblk[version];
436 if (!(buf = getdqbuf())) {
440 dh = (struct lustre_disk_dqdbheader *)buf;
441 ddquot = GETENTRIES(buf, version);
442 if (info->dqi_free_entry) {
443 blk = info->dqi_free_entry;
444 if ((*err = read_blk(filp, blk, buf)) < 0)
447 blk = get_free_dqblk(filp, info);
453 memset(buf, 0, LUSTRE_DQBLKSIZE);
454 info->dqi_free_entry = blk; /* This is enough as block is
455 already zeroed and entry list
457 lustre_mark_info_dirty(info);
460 /* Will block be full */
461 if (le16_to_cpu(dh->dqdh_entries) + 1 >= dqstrinblk)
462 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
464 "VFS: find_free_dqentry(): Can't remove block (%u) from entry free list.\n",
468 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
469 /* Find free structure in block */
470 for (i = 0; i < dqstrinblk &&
471 memcmp((char *)&emptydquot[version],
472 (char*)ddquot + i * dqblk_sz,
475 if (i == dqstrinblk) {
477 "VFS: find_free_dqentry(): Data block full but it shouldn't.\n");
482 if ((*err = write_blk(filp, blk, buf)) < 0) {
484 "VFS: find_free_dqentry(): Can't write quota data block %u.\n",
489 (blk << LUSTRE_DQBLKSIZE_BITS) +
490 sizeof(struct lustre_disk_dqdbheader) +
500 * Insert reference to structure into the trie
502 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
503 lustre_quota_version_t version)
505 struct lustre_quota_info *lqi = dquot->dq_info;
506 struct file *filp = lqi->qi_files[dquot->dq_type];
507 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
509 int ret = 0, newson = 0, newact = 0;
513 if (!(buf = getdqbuf()))
516 ret = get_free_dqblk(filp, info);
520 memset(buf, 0, LUSTRE_DQBLKSIZE);
523 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
525 "VFS: Can't read tree quota block %u.\n",
531 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
534 if (depth == LUSTRE_DQTREEDEPTH - 1) {
538 "VFS: Inserting already present quota entry (block %u).\n",
539 ref[GETIDINDEX(dquot->dq_id, depth)]);
544 newblk = find_free_dqentry(dquot, &ret, version);
546 ret = do_insert_tree(dquot, &newblk, depth + 1, version);
547 if (newson && ret >= 0) {
548 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
549 ret = write_blk(filp, *treeblk, buf);
550 } else if (newact && ret < 0)
551 put_free_dqblk(filp, info, buf, *treeblk);
558 * Wrapper for inserting quota structure into tree
560 static inline int dq_insert_tree(struct lustre_dquot *dquot,
561 lustre_quota_version_t version)
563 int tmp = LUSTRE_DQTREEOFF;
564 return do_insert_tree(dquot, &tmp, 0, version);
568 * We don't have to be afraid of deadlocks as we never have quotas on
571 static int lustre_write_dquot(struct lustre_dquot *dquot,
572 lustre_quota_version_t version)
574 int type = dquot->dq_type;
579 int dqblk_sz = lustre_disk_dqblk_sz[version];
580 char ddquot[dqblk_sz];
582 ret = mem2diskdqb(ddquot, &dquot->dq_dqb, dquot->dq_id, version);
587 if ((ret = dq_insert_tree(dquot, version)) < 0) {
589 "VFS: Error %Zd occurred while creating quota.\n",
593 filp = dquot->dq_info->qi_files[type];
594 offset = dquot->dq_off;
595 /* Argh... We may need to write structure full of zeroes but that would be
596 * treated as an empty place by the rest of the code. Format change would
597 * be definitely cleaner but the problems probably are not worth it */
598 if (!memcmp((char *)&emptydquot[version], ddquot, dqblk_sz))
599 ((struct lustre_disk_dqblk_v2 *)ddquot)->dqb_itime = cpu_to_le64(1);
602 ret = filp->f_op->write(filp, ddquot,
605 if (ret != dqblk_sz) {
606 CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
607 filp->f_dentry->d_sb->s_id);
617 * Free dquot entry in data block
619 static int free_dqentry(struct lustre_dquot *dquot, uint blk,
620 lustre_quota_version_t version)
622 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
623 struct lustre_mem_dqinfo *info =
624 &dquot->dq_info->qi_info[dquot->dq_type];
625 struct lustre_disk_dqdbheader *dh;
626 dqbuf_t buf = getdqbuf();
627 int dqstrinblk = lustre_dqstrinblk[version];
632 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
634 "VFS: Quota structure has offset to other block (%u) than it should (%u).\n",
635 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
638 if ((ret = read_blk(filp, blk, buf)) < 0) {
639 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
642 dh = (struct lustre_disk_dqdbheader *)buf;
643 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
644 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
645 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
646 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
648 "VFS: Can't move quota data block (%u) to free list.\n",
653 memset(buf + (dquot->dq_off & ((1<<LUSTRE_DQBLKSIZE_BITS) - 1)),
654 0, lustre_disk_dqblk_sz[version]);
655 if (le16_to_cpu(dh->dqdh_entries) == dqstrinblk - 1) {
656 /* Insert will write block itself */
658 insert_free_dqentry(filp, info, buf, blk)) < 0) {
660 "VFS: Can't insert quota data block (%u) to free entry list.\n",
664 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
666 "VFS: Can't write quota data block %u\n", blk);
670 dquot->dq_off = 0; /* Quota is now unattached */
677 * Remove reference to dquot from tree
679 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
680 lustre_quota_version_t version)
682 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
683 struct lustre_mem_dqinfo *info =
684 &dquot->dq_info->qi_info[dquot->dq_type];
685 dqbuf_t buf = getdqbuf();
688 u32 *ref = (u32 *) buf;
692 if ((ret = read_blk(filp, *blk, buf)) < 0) {
693 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", *blk);
696 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
697 if (depth == LUSTRE_DQTREEDEPTH - 1) {
698 ret = free_dqentry(dquot, newblk, version);
701 ret = remove_tree(dquot, &newblk, depth + 1, version);
702 if (ret >= 0 && !newblk) {
704 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
705 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++)
706 /* Block got empty? */ ;
707 /* don't put the root block into free blk list! */
708 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
709 put_free_dqblk(filp, info, buf, *blk);
711 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
713 "VFS: Can't write quota tree block %u.\n", *blk);
721 * Delete dquot from tree
723 static int lustre_delete_dquot(struct lustre_dquot *dquot,
724 lustre_quota_version_t version)
726 uint tmp = LUSTRE_DQTREEOFF;
728 if (!dquot->dq_off) /* Even not allocated? */
730 return remove_tree(dquot, &tmp, 0, version);
734 * Find entry in block
736 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
737 lustre_quota_version_t version)
739 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
740 dqbuf_t buf = getdqbuf();
743 struct lustre_disk_dqblk_v2 *ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
744 int dqblk_sz = lustre_disk_dqblk_sz[version];
745 int dqstrinblk = lustre_dqstrinblk[version];
747 LASSERT(version == LUSTRE_QUOTA_V2);
751 if ((ret = read_blk(filp, blk, buf)) < 0) {
752 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
756 for (i = 0; i < dqstrinblk &&
757 le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id;
759 else { /* ID 0 as a bit more complicated searching... */
760 for (i = 0; i < dqstrinblk; i++)
761 if (!le32_to_cpu(ddquot[i].dqb_id)
762 && memcmp((char *)&emptydquot[version],
767 if (i == dqstrinblk) {
769 "VFS: Quota for id %u referenced but not present.\n",
775 (blk << LUSTRE_DQBLKSIZE_BITS) +
776 sizeof(struct lustre_disk_dqdbheader) +
784 * Find entry for given id in the tree
786 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
787 lustre_quota_version_t version)
789 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
790 dqbuf_t buf = getdqbuf();
792 u32 *ref = (u32 *) buf;
796 if ((ret = read_blk(filp, blk, buf)) < 0) {
797 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
801 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
802 if (!blk) /* No reference? */
804 if (depth < LUSTRE_DQTREEDEPTH - 1)
805 ret = find_tree_dqentry(dquot, blk, depth + 1, version);
807 ret = find_block_dqentry(dquot, blk, version);
814 * Find entry for given id in the tree - wrapper function
816 static inline loff_t find_dqentry(struct lustre_dquot *dquot,
817 lustre_quota_version_t version)
819 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
822 int lustre_read_dquot(struct lustre_dquot *dquot)
824 int type = dquot->dq_type;
828 int ret = 0, dqblk_sz;
829 lustre_quota_version_t version;
831 /* Invalidated quota? */
832 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
833 CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
837 version = dquot->dq_info->qi_version;
838 LASSERT(version == LUSTRE_QUOTA_V2);
839 dqblk_sz = lustre_disk_dqblk_sz[version];
841 offset = find_dqentry(dquot, version);
842 if (offset <= 0) { /* Entry not present? */
845 "VFS: Can't read quota structure for id %u.\n",
848 set_bit(DQ_FAKE_B, &dquot->dq_flags);
849 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
852 char ddquot[dqblk_sz];
854 dquot->dq_off = offset;
857 if ((ret = filp->f_op->read(filp, ddquot, dqblk_sz, &offset)) !=
862 "VFS: Error while reading quota structure for id %u.\n",
864 memset(ddquot, 0, dqblk_sz);
867 /* We need to escape back all-zero structure */
868 if (!memcmp((char *)&fakedquot[version],
870 ((struct lustre_disk_dqblk_v2 *)ddquot)->dqb_itime = cpu_to_le64(0);
873 disk2memdqb(&dquot->dq_dqb, ddquot, version);
880 * Commit changes of dquot to disk - it might also mean deleting
881 * it when quota became fake.
883 int lustre_commit_dquot(struct lustre_dquot *dquot)
886 lustre_quota_version_t version = dquot->dq_info->qi_version;
888 /* always clear the flag so we don't loop on an IO error... */
889 clear_bit(DQ_MOD_B, &dquot->dq_flags);
891 /* The block/inode usage in admin quotafile isn't the real usage
892 * over all cluster, so keep the fake dquot entry on disk is
893 * meaningless, just remove it */
894 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
895 rc = lustre_delete_dquot(dquot, version);
897 rc = lustre_write_dquot(dquot, version);
902 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
903 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
908 int lustre_init_quota_header(struct lustre_quota_info *lqi, int type, int fakemagics)
910 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
911 static const uint fake_magics[] = LUSTRE_BADQMAGICS;
912 const uint* quota_versions = lustre_initqversions[lqi->qi_version];
913 struct lustre_disk_dqheader dqhead;
916 struct file *fp = lqi->qi_files[type];
919 /* write quotafile header */
920 dqhead.dqh_magic = cpu_to_le32(fakemagics ?
921 fake_magics[type] : quota_magics[type]);
922 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
923 size = fp->f_op->write(fp, (char *)&dqhead,
924 sizeof(struct lustre_disk_dqheader), &offset);
926 if (size != sizeof(struct lustre_disk_dqheader)) {
927 CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
935 * We need to export this function to initialize quotafile, because we haven't
936 * user level check utility
938 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
941 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
944 rc = lustre_init_quota_header(lqi, type, fakemagics);
948 /* write init quota info */
949 memset(dqinfo, 0, sizeof(*dqinfo));
950 dqinfo->dqi_bgrace = MAX_DQ_TIME;
951 dqinfo->dqi_igrace = MAX_IQ_TIME;
952 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
954 return lustre_write_quota_info(lqi, type);
957 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
959 return lustre_init_quota_info_generic(lqi, type, 0);
962 ssize_t quota_read(struct file *file, struct inode *inode, int type,
963 uint blk, dqbuf_t buf)
966 return read_blk(file, blk, buf);
968 #ifndef KERNEL_SUPPORTS_QUOTA_READ
971 struct super_block *sb = inode->i_sb;
972 memset(buf, 0, LUSTRE_DQBLKSIZE);
973 return sb->s_op->quota_read(sb, type, (char *)buf,
975 blk << LUSTRE_DQBLKSIZE_BITS);
980 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
981 uint blk, struct list_head *list)
983 dqbuf_t buf = getdqbuf();
985 struct lustre_disk_dqdbheader *dqhead =
986 (struct lustre_disk_dqdbheader *)buf;
987 struct dqblk *blk_item;
989 struct list_head *tmp;
993 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
994 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
999 if (!le32_to_cpu(dqhead->dqdh_entries))
1002 if (list_empty(list)) {
1007 list_for_each_entry(pos, list, link) {
1008 if (blk == pos->blk) /* we got this blk already */
1016 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
1021 blk_item->blk = blk;
1022 INIT_LIST_HEAD(&blk_item->link);
1024 list_add_tail(&blk_item->link, tmp);
1031 int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
1032 uint blk, int depth, struct list_head *list)
1034 dqbuf_t buf = getdqbuf();
1037 u32 *ref = (u32 *) buf;
1041 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1042 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1047 for (index = 0; index <= 0xff && !ret; index++) {
1048 blk = le32_to_cpu(ref[index]);
1049 if (!blk) /* No reference */
1052 if (depth < LUSTRE_DQTREEDEPTH - 1)
1053 ret = walk_tree_dqentry(filp, inode, type, blk,
1056 ret = walk_block_dqentry(filp, inode, type, blk, list);
1064 * Walk through the quota file (v2 format) to get all ids with quota limit
1066 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
1067 struct list_head *list)
1069 struct list_head blk_list;
1070 struct dqblk *blk_item, *tmp;
1072 struct lustre_disk_dqblk_v2 *ddquot;
1074 lustre_quota_version_t version;
1078 if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
1079 version = LUSTRE_QUOTA_V2;
1081 CDEBUG(D_ERROR, "unknown quota file format!\n");
1085 if (!list_empty(list)) {
1086 CDEBUG(D_ERROR, "not empty list\n");
1090 INIT_LIST_HEAD(&blk_list);
1091 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
1093 CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
1096 if (list_empty(&blk_list))
1102 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
1104 list_for_each_entry(blk_item, &blk_list, link) {
1106 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
1108 memset(buf, 0, LUSTRE_DQBLKSIZE);
1109 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
1111 "VFS: Can't read quota tree block %u.\n",
1113 GOTO(out_free, rc = ret);
1116 for (i = 0; i < lustre_dqstrinblk[version]; i++) {
1117 struct dquot_id *dqid;
1118 /* skip empty entry */
1119 if (!memcmp((char *)&emptydquot[version],
1120 ddquot + i*dqblk_sz, dqblk_sz))
1123 dqid = kmalloc(sizeof(*dqid), GFP_NOFS);
1125 GOTO(out_free, rc = -ENOMEM);
1127 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
1128 INIT_LIST_HEAD(&dqid->di_link);
1129 list_add(&dqid->di_link, list);
1134 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1135 list_del_init(&blk_item->link);
1145 EXPORT_SYMBOL(lustre_read_quota_info);
1146 EXPORT_SYMBOL(lustre_write_quota_info);
1147 EXPORT_SYMBOL(lustre_check_quota_file);
1148 EXPORT_SYMBOL(lustre_read_dquot);
1149 EXPORT_SYMBOL(lustre_commit_dquot);
1150 EXPORT_SYMBOL(lustre_init_quota_info);
1151 EXPORT_SYMBOL(lustre_get_qids);