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 #ifdef HAVE_QUOTAIO_V1_H
54 # include <linux/quotaio_v1.h>
57 #include <asm/byteorder.h>
58 #include <asm/uaccess.h>
60 #include <lustre_quota.h>
61 #include <obd_support.h>
62 #include "lustre_quota_fmt.h"
64 #ifdef HAVE_QUOTA_SUPPORT
66 static const uint lustre_initqversions[][MAXQUOTAS] = {
67 [LUSTRE_QUOTA_V2] = LUSTRE_INITQVERSIONS_V2
70 static const int lustre_dqstrinblk[] = {
71 [LUSTRE_QUOTA_V2] = LUSTRE_DQSTRINBLK_V2
74 static const int lustre_disk_dqblk_sz[] = {
75 [LUSTRE_QUOTA_V2] = sizeof(struct lustre_disk_dqblk_v2)
80 struct lustre_disk_dqblk_v2 r1;
82 [LUSTRE_QUOTA_V2] = {.r1 = {.dqb_itime = __constant_cpu_to_le64(1LLU)} }
87 struct lustre_disk_dqblk_v2 r1;
89 [LUSTRE_QUOTA_V2] = {.r1 = { 0 } }
92 int check_quota_file(struct file *f, struct inode *inode, int type,
93 lustre_quota_version_t version)
95 struct lustre_disk_dqheader dqhead;
99 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
100 const uint *quota_versions = lustre_initqversions[version];
103 CERROR("check_quota_file failed!\n");
104 libcfs_debug_dumpstack(NULL);
111 size = f->f_op->read(f, (char *)&dqhead,
112 sizeof(struct lustre_disk_dqheader),
116 #ifndef KERNEL_SUPPORTS_QUOTA_READ
119 struct super_block *sb = inode->i_sb;
120 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
121 sizeof(struct lustre_disk_dqheader),
125 if (size != sizeof(struct lustre_disk_dqheader))
127 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
128 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
134 * Check whether given file is really lustre admin quotafile
136 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
138 struct file *f = lqi->qi_files[type];
139 return check_quota_file(f, NULL, type, lqi->qi_version);
142 int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
145 struct lustre_disk_dqinfo dinfo;
147 loff_t offset = LUSTRE_DQINFOOFF;
151 size = f->f_op->read(f, (char *)&dinfo,
152 sizeof(struct lustre_disk_dqinfo), &offset);
154 if (size != sizeof(struct lustre_disk_dqinfo)) {
155 CDEBUG(D_ERROR, "Can't read info structure on device %s.\n",
156 f->f_vfsmnt->mnt_sb->s_id);
159 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
160 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
161 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
162 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
163 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
164 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
169 * Read information header from quota file
171 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
173 return lustre_read_quota_file_info(lqi->qi_files[type],
174 &lqi->qi_info[type]);
178 * Write information header to quota file
180 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
183 struct lustre_disk_dqinfo dinfo;
184 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
185 struct file *f = lqi->qi_files[type];
187 loff_t offset = LUSTRE_DQINFOOFF;
189 info->dqi_flags &= ~DQF_INFO_DIRTY;
190 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
191 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
192 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
193 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
194 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
195 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
198 size = f->f_op->write(f, (char *)&dinfo,
199 sizeof(struct lustre_disk_dqinfo), &offset);
201 if (size != sizeof(struct lustre_disk_dqinfo)) {
203 "Can't write info structure on device %s.\n",
204 f->f_vfsmnt->mnt_sb->s_id);
210 void disk2memdqb(struct lustre_mem_dqblk *m, void *d,
211 lustre_quota_version_t version)
213 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
215 LASSERT(version == LUSTRE_QUOTA_V2);
217 m->dqb_ihardlimit = le64_to_cpu(dqblk->dqb_ihardlimit);
218 m->dqb_isoftlimit = le64_to_cpu(dqblk->dqb_isoftlimit);
219 m->dqb_curinodes = le64_to_cpu(dqblk->dqb_curinodes);
220 m->dqb_itime = le64_to_cpu(dqblk->dqb_itime);
221 m->dqb_bhardlimit = le64_to_cpu(dqblk->dqb_bhardlimit);
222 m->dqb_bsoftlimit = le64_to_cpu(dqblk->dqb_bsoftlimit);
223 m->dqb_curspace = le64_to_cpu(dqblk->dqb_curspace);
224 m->dqb_btime = le64_to_cpu(dqblk->dqb_btime);
227 static int mem2diskdqb(void *d, struct lustre_mem_dqblk *m,
228 qid_t id, lustre_quota_version_t version)
230 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
232 LASSERT(version == LUSTRE_QUOTA_V2);
234 dqblk->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
235 dqblk->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
236 dqblk->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
237 dqblk->dqb_itime = cpu_to_le64(m->dqb_itime);
238 dqblk->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
239 dqblk->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
240 dqblk->dqb_curspace = cpu_to_le64(m->dqb_curspace);
241 dqblk->dqb_btime = cpu_to_le64(m->dqb_btime);
242 dqblk->dqb_id = cpu_to_le32(id);
247 dqbuf_t getdqbuf(void)
249 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
252 "VFS: Not enough memory for quota buffers.\n");
256 void freedqbuf(dqbuf_t buf)
261 ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
265 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
267 memset(buf, 0, LUSTRE_DQBLKSIZE);
270 ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
275 ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
279 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
283 ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
288 void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
290 set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
294 * Remove empty block from list and return it
296 int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
298 dqbuf_t buf = getdqbuf();
299 struct lustre_disk_dqdbheader *dh =
300 (struct lustre_disk_dqdbheader *)buf;
305 if (info->dqi_free_blk) {
306 blk = info->dqi_free_blk;
307 if ((ret = read_blk(filp, blk, buf)) < 0)
309 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
311 memset(buf, 0, LUSTRE_DQBLKSIZE);
312 /* Assure block allocation... */
313 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0)
315 blk = info->dqi_blocks++;
317 lustre_mark_info_dirty(info);
325 * Insert empty block to the list
327 int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
328 dqbuf_t buf, uint blk)
330 struct lustre_disk_dqdbheader *dh =
331 (struct lustre_disk_dqdbheader *)buf;
334 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
335 dh->dqdh_prev_free = cpu_to_le32(0);
336 dh->dqdh_entries = cpu_to_le16(0);
337 info->dqi_free_blk = blk;
338 lustre_mark_info_dirty(info);
339 if ((err = write_blk(filp, blk, buf)) < 0)
340 /* Some strange block. We had better leave it... */
346 * Remove given block from the list of blocks with free entries
348 int remove_free_dqentry(struct file *filp,
349 struct lustre_mem_dqinfo *info, dqbuf_t buf,
352 dqbuf_t tmpbuf = getdqbuf();
353 struct lustre_disk_dqdbheader *dh =
354 (struct lustre_disk_dqdbheader *)buf;
355 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
356 le32_to_cpu(dh->dqdh_prev_free);
362 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
364 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
366 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
370 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
372 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
374 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
377 info->dqi_free_entry = nextblk;
378 lustre_mark_info_dirty(info);
381 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
382 if (write_blk(filp, blk, buf) < 0)
383 /* No matter whether write succeeds block is out of list */
385 "VFS: Can't write block (%u) with free entries.\n", blk);
393 * Insert given block to the beginning of list with free entries
395 int insert_free_dqentry(struct file *filp,
396 struct lustre_mem_dqinfo *info, dqbuf_t buf,
399 dqbuf_t tmpbuf = getdqbuf();
400 struct lustre_disk_dqdbheader *dh =
401 (struct lustre_disk_dqdbheader *)buf;
406 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
407 dh->dqdh_prev_free = cpu_to_le32(0);
408 if ((err = write_blk(filp, blk, buf)) < 0)
410 if (info->dqi_free_entry) {
411 if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
413 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
415 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
419 info->dqi_free_entry = blk;
420 lustre_mark_info_dirty(info);
430 * Find space for dquot
432 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
433 lustre_quota_version_t version)
435 struct lustre_quota_info *lqi = dquot->dq_info;
436 struct file *filp = lqi->qi_files[dquot->dq_type];
437 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
439 struct lustre_disk_dqdbheader *dh;
441 int dqblk_sz = lustre_disk_dqblk_sz[version];
442 int dqstrinblk = lustre_dqstrinblk[version];
446 if (!(buf = getdqbuf())) {
450 dh = (struct lustre_disk_dqdbheader *)buf;
451 ddquot = GETENTRIES(buf, version);
452 if (info->dqi_free_entry) {
453 blk = info->dqi_free_entry;
454 if ((*err = read_blk(filp, blk, buf)) < 0)
457 blk = get_free_dqblk(filp, info);
463 memset(buf, 0, LUSTRE_DQBLKSIZE);
464 info->dqi_free_entry = blk; /* This is enough as block is
465 already zeroed and entry list
467 lustre_mark_info_dirty(info);
470 /* Will block be full */
471 if (le16_to_cpu(dh->dqdh_entries) + 1 >= dqstrinblk)
472 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
474 "VFS: find_free_dqentry(): Can't remove block "
475 "(%u) from entry free list.\n", blk);
478 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
479 /* Find free structure in block */
480 for (i = 0; i < dqstrinblk &&
481 memcmp((char *)&emptydquot[version],
482 (char *)ddquot + i * dqblk_sz, dqblk_sz);
485 if (i == dqstrinblk) {
487 "VFS: find_free_dqentry(): Data block full but it "
493 if ((*err = write_blk(filp, blk, buf)) < 0) {
495 "VFS: find_free_dqentry(): Can't write quota data "
500 (blk << LUSTRE_DQBLKSIZE_BITS) +
501 sizeof(struct lustre_disk_dqdbheader) +
511 * Insert reference to structure into the trie
513 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
514 lustre_quota_version_t version)
516 struct lustre_quota_info *lqi = dquot->dq_info;
517 struct file *filp = lqi->qi_files[dquot->dq_type];
518 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
520 int ret = 0, newson = 0, newact = 0;
524 if (!(buf = getdqbuf()))
527 ret = get_free_dqblk(filp, info);
531 memset(buf, 0, LUSTRE_DQBLKSIZE);
534 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
536 "VFS: Can't read tree quota block %u.\n",
542 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
545 if (depth == LUSTRE_DQTREEDEPTH - 1) {
549 "VFS: Inserting already present quota entry "
551 ref[GETIDINDEX(dquot->dq_id, depth)]);
556 newblk = find_free_dqentry(dquot, &ret, version);
558 ret = do_insert_tree(dquot, &newblk, depth + 1, version);
559 if (newson && ret >= 0) {
560 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
561 ret = write_blk(filp, *treeblk, buf);
562 } else if (newact && ret < 0)
563 put_free_dqblk(filp, info, buf, *treeblk);
570 * Wrapper for inserting quota structure into tree
572 static inline int dq_insert_tree(struct lustre_dquot *dquot,
573 lustre_quota_version_t version)
575 int tmp = LUSTRE_DQTREEOFF;
576 return do_insert_tree(dquot, &tmp, 0, version);
580 * We don't have to be afraid of deadlocks as we never have quotas on
583 static int lustre_write_dquot(struct lustre_dquot *dquot,
584 lustre_quota_version_t version)
586 int type = dquot->dq_type;
591 int dqblk_sz = lustre_disk_dqblk_sz[version];
592 struct lustre_disk_dqblk_v2 ddquot;
594 ret = mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id, version);
599 if ((ret = dq_insert_tree(dquot, version)) < 0) {
601 "VFS: Error %Zd occurred while creating "
605 filp = dquot->dq_info->qi_files[type];
606 offset = dquot->dq_off;
607 /* Argh... We may need to write structure full of zeroes but that would
608 * be treated as an empty place by the rest of the code. Format change
609 * would be definitely cleaner but the problems probably are not worth
611 if (!memcmp((char *)&emptydquot[version], (char *)&ddquot, dqblk_sz))
612 ddquot.dqb_itime = cpu_to_le64(1);
615 ret = filp->f_op->write(filp, (char *)&ddquot,
618 if (ret != dqblk_sz) {
619 CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
620 filp->f_dentry->d_sb->s_id);
630 * Free dquot entry in data block
632 static int free_dqentry(struct lustre_dquot *dquot, uint blk,
633 lustre_quota_version_t version)
635 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
636 struct lustre_mem_dqinfo *info =
637 &dquot->dq_info->qi_info[dquot->dq_type];
638 struct lustre_disk_dqdbheader *dh;
639 dqbuf_t buf = getdqbuf();
640 int dqstrinblk = lustre_dqstrinblk[version];
645 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
647 "VFS: Quota structure has offset to other block (%u) "
648 "than it should (%u).\n",
649 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
652 if ((ret = read_blk(filp, blk, buf)) < 0) {
653 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
656 dh = (struct lustre_disk_dqdbheader *)buf;
657 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
658 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
659 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
660 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
662 "VFS: Can't move quota data block (%u) to free "
667 memset(buf + (dquot->dq_off & ((1<<LUSTRE_DQBLKSIZE_BITS) - 1)),
668 0, lustre_disk_dqblk_sz[version]);
669 if (le16_to_cpu(dh->dqdh_entries) == dqstrinblk - 1) {
670 /* Insert will write block itself */
672 insert_free_dqentry(filp, info, buf, blk)) < 0) {
674 "VFS: Can't insert quota data block "
675 "(%u) to free entry list.\n", blk);
678 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
680 "VFS: Can't write quota data block %u\n", blk);
684 dquot->dq_off = 0; /* Quota is now unattached */
691 * Remove reference to dquot from tree
693 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
694 lustre_quota_version_t version)
696 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
697 struct lustre_mem_dqinfo *info =
698 &dquot->dq_info->qi_info[dquot->dq_type];
699 dqbuf_t buf = getdqbuf();
702 u32 *ref = (u32 *) buf;
706 if ((ret = read_blk(filp, *blk, buf)) < 0) {
707 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", *blk);
710 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
711 if (depth == LUSTRE_DQTREEDEPTH - 1) {
712 ret = free_dqentry(dquot, newblk, version);
715 ret = remove_tree(dquot, &newblk, depth + 1, version);
716 if (ret >= 0 && !newblk) {
718 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
719 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++)
720 /* Block got empty? */ ;
721 /* don't put the root block into free blk list! */
722 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
723 put_free_dqblk(filp, info, buf, *blk);
725 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
727 "VFS: Can't write quota tree block %u.\n", *blk);
735 * Delete dquot from tree
737 static int lustre_delete_dquot(struct lustre_dquot *dquot,
738 lustre_quota_version_t version)
740 uint tmp = LUSTRE_DQTREEOFF;
742 if (!dquot->dq_off) /* Even not allocated? */
744 return remove_tree(dquot, &tmp, 0, version);
748 * Find entry in block
750 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
751 lustre_quota_version_t version)
753 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
754 dqbuf_t buf = getdqbuf();
757 struct lustre_disk_dqblk_v2 *ddquot =
758 (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
759 int dqblk_sz = lustre_disk_dqblk_sz[version];
760 int dqstrinblk = lustre_dqstrinblk[version];
762 LASSERT(version == LUSTRE_QUOTA_V2);
766 if ((ret = read_blk(filp, blk, buf)) < 0) {
767 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
771 for (i = 0; i < dqstrinblk &&
772 le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id;
774 else { /* ID 0 as a bit more complicated searching... */
775 for (i = 0; i < dqstrinblk; i++)
776 if (!le32_to_cpu(ddquot[i].dqb_id)
777 && memcmp((char *)&emptydquot[version],
778 (char *)&ddquot[i], dqblk_sz))
781 if (i == dqstrinblk) {
783 "VFS: Quota for id %u referenced but not present.\n",
789 (blk << LUSTRE_DQBLKSIZE_BITS) +
790 sizeof(struct lustre_disk_dqdbheader) +
798 * Find entry for given id in the tree
800 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
801 lustre_quota_version_t version)
803 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
804 dqbuf_t buf = getdqbuf();
806 u32 *ref = (u32 *) buf;
810 if ((ret = read_blk(filp, blk, buf)) < 0) {
811 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
815 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
816 if (!blk) /* No reference? */
818 if (depth < LUSTRE_DQTREEDEPTH - 1)
819 ret = find_tree_dqentry(dquot, blk, depth + 1, version);
821 ret = find_block_dqentry(dquot, blk, version);
828 * Find entry for given id in the tree - wrapper function
830 static inline loff_t find_dqentry(struct lustre_dquot *dquot,
831 lustre_quota_version_t version)
833 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
836 int lustre_read_dquot(struct lustre_dquot *dquot)
838 int type = dquot->dq_type;
842 int ret = 0, dqblk_sz;
843 lustre_quota_version_t version;
845 /* Invalidated quota? */
846 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
847 CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
851 version = dquot->dq_info->qi_version;
852 LASSERT(version == LUSTRE_QUOTA_V2);
853 dqblk_sz = lustre_disk_dqblk_sz[version];
855 offset = find_dqentry(dquot, version);
856 if (offset <= 0) { /* Entry not present? */
859 "VFS: Can't read quota structure for id %u.\n",
862 set_bit(DQ_FAKE_B, &dquot->dq_flags);
863 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
866 struct lustre_disk_dqblk_v2 ddquot;
868 dquot->dq_off = offset;
871 if ((ret = filp->f_op->read(filp, (char *)&ddquot,
872 dqblk_sz, &offset)) != dqblk_sz) {
876 "VFS: Error while reading quota structure for id "
877 "%u.\n", dquot->dq_id);
878 memset((char *)&ddquot, 0, dqblk_sz);
881 /* We need to escape back all-zero structure */
882 if (!memcmp((char *)&fakedquot[version],
883 (char *)&ddquot, dqblk_sz))
884 ddquot.dqb_itime = cpu_to_le64(0);
887 disk2memdqb(&dquot->dq_dqb, &ddquot, version);
894 * Commit changes of dquot to disk - it might also mean deleting
895 * it when quota became fake.
897 int lustre_commit_dquot(struct lustre_dquot *dquot)
900 lustre_quota_version_t version = dquot->dq_info->qi_version;
902 /* always clear the flag so we don't loop on an IO error... */
903 clear_bit(DQ_MOD_B, &dquot->dq_flags);
905 /* The block/inode usage in admin quotafile isn't the real usage
906 * over all cluster, so keep the fake dquot entry on disk is
907 * meaningless, just remove it */
908 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
909 rc = lustre_delete_dquot(dquot, version);
911 rc = lustre_write_dquot(dquot, version);
916 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
917 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
922 int lustre_init_quota_header(struct lustre_quota_info *lqi, int type,
925 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
926 static const uint fake_magics[] = LUSTRE_BADQMAGICS;
927 const uint* quota_versions = lustre_initqversions[lqi->qi_version];
928 struct lustre_disk_dqheader dqhead;
931 struct file *fp = lqi->qi_files[type];
934 /* write quotafile header */
935 dqhead.dqh_magic = cpu_to_le32(fakemagics ?
936 fake_magics[type] : quota_magics[type]);
937 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
938 size = fp->f_op->write(fp, (char *)&dqhead,
939 sizeof(struct lustre_disk_dqheader), &offset);
941 if (size != sizeof(struct lustre_disk_dqheader)) {
942 CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
950 * We need to export this function to initialize quotafile, because we haven't
951 * user level check utility
953 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
956 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
959 rc = lustre_init_quota_header(lqi, type, fakemagics);
963 /* write init quota info */
964 memset(dqinfo, 0, sizeof(*dqinfo));
965 dqinfo->dqi_bgrace = MAX_DQ_TIME;
966 dqinfo->dqi_igrace = MAX_IQ_TIME;
967 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
969 return lustre_write_quota_info(lqi, type);
972 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
974 return lustre_init_quota_info_generic(lqi, type, 0);
977 ssize_t quota_read(struct file *file, struct inode *inode, int type,
978 uint blk, dqbuf_t buf)
981 return read_blk(file, blk, buf);
983 #ifndef KERNEL_SUPPORTS_QUOTA_READ
986 struct super_block *sb = inode->i_sb;
987 memset(buf, 0, LUSTRE_DQBLKSIZE);
988 return sb->s_op->quota_read(sb, type, (char *)buf,
990 blk << LUSTRE_DQBLKSIZE_BITS);
995 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
996 uint blk, struct list_head *list)
998 dqbuf_t buf = getdqbuf();
1000 struct lustre_disk_dqdbheader *dqhead =
1001 (struct lustre_disk_dqdbheader *)buf;
1002 struct dqblk *blk_item;
1004 struct list_head *tmp;
1008 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1009 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1014 if (!le32_to_cpu(dqhead->dqdh_entries))
1017 if (list_empty(list)) {
1022 list_for_each_entry(pos, list, link) {
1023 if (blk == pos->blk) /* we got this blk already */
1031 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
1036 blk_item->blk = blk;
1037 INIT_LIST_HEAD(&blk_item->link);
1039 list_add_tail(&blk_item->link, tmp);
1046 int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
1047 uint blk, int depth, struct list_head *list)
1049 dqbuf_t buf = getdqbuf();
1052 u32 *ref = (u32 *) buf;
1056 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1057 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
1062 for (index = 0; index <= 0xff && !ret; index++) {
1063 blk = le32_to_cpu(ref[index]);
1064 if (!blk) /* No reference */
1067 if (depth < LUSTRE_DQTREEDEPTH - 1)
1068 ret = walk_tree_dqentry(filp, inode, type, blk,
1071 ret = walk_block_dqentry(filp, inode, type, blk, list);
1079 * Walk through the quota file (v2 format) to get all ids with quota limit
1081 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
1082 struct list_head *list)
1084 struct list_head blk_list;
1085 struct dqblk *blk_item, *tmp;
1087 struct lustre_disk_dqblk_v2 *ddquot;
1089 lustre_quota_version_t version;
1093 LASSERT(ergo(fp == NULL, inode != NULL));
1095 if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
1096 version = LUSTRE_QUOTA_V2;
1098 CDEBUG(D_ERROR, "unknown quota file format!\n");
1102 if (!list_empty(list)) {
1103 CDEBUG(D_ERROR, "not empty list\n");
1107 INIT_LIST_HEAD(&blk_list);
1108 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
1110 CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
1113 if (list_empty(&blk_list))
1119 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
1121 list_for_each_entry(blk_item, &blk_list, link) {
1123 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
1125 memset(buf, 0, LUSTRE_DQBLKSIZE);
1126 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
1128 "VFS: Can't read quota tree block %u.\n",
1130 GOTO(out_free, rc = ret);
1133 for (i = 0; i < lustre_dqstrinblk[version]; i++) {
1134 struct dquot_id *dqid;
1135 /* skip empty entry */
1136 if (!memcmp((char *)&emptydquot[version],
1137 (char *)&ddquot[i], dqblk_sz))
1140 OBD_ALLOC_GFP(dqid, sizeof(*dqid), GFP_NOFS);
1142 GOTO(out_free, rc = -ENOMEM);
1144 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
1145 dqid->di_flag = le64_to_cpu(ddquot[i].dqb_ihardlimit) ?
1147 dqid->di_flag |= le64_to_cpu(ddquot[i].dqb_bhardlimit) ?
1150 INIT_LIST_HEAD(&dqid->di_link);
1151 list_add(&dqid->di_link, list);
1156 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1157 list_del_init(&blk_item->link);
1167 EXPORT_SYMBOL(lustre_read_quota_info);
1168 EXPORT_SYMBOL(lustre_write_quota_info);
1169 EXPORT_SYMBOL(lustre_check_quota_file);
1170 EXPORT_SYMBOL(lustre_read_dquot);
1171 EXPORT_SYMBOL(lustre_commit_dquot);
1172 EXPORT_SYMBOL(lustre_init_quota_info);
1173 EXPORT_SYMBOL(lustre_get_qids);