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 extern void *lustre_quota_journal_start(struct inode *inode, int delete);
93 extern void lustre_quota_journal_stop(void *handle);
94 extern ssize_t lustre_read_quota(struct file *f, struct inode *inode, int type,
95 char *buf, int count, loff_t pos);
96 extern ssize_t lustre_write_quota(struct file *f, char *buf, int count, loff_t pos);
98 int check_quota_file(struct file *f, struct inode *inode, int type,
99 lustre_quota_version_t version)
101 struct lustre_disk_dqheader dqhead;
103 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
104 const uint *quota_versions = lustre_initqversions[version];
107 CERROR("check_quota_file failed!\n");
108 libcfs_debug_dumpstack(NULL);
112 size = lustre_read_quota(f, inode, type, (char *)&dqhead,
113 sizeof(struct lustre_disk_dqheader), 0);
115 if (size != sizeof(struct lustre_disk_dqheader))
117 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
118 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
124 * Check whether given file is really lustre admin quotafile
126 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
128 struct file *f = lqi->qi_files[type];
129 return check_quota_file(f, NULL, type, lqi->qi_version);
132 int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
134 struct lustre_disk_dqinfo dinfo;
137 size = lustre_read_quota(f, NULL, 0, (char *)&dinfo,
138 sizeof(struct lustre_disk_dqinfo),
141 if (size != sizeof(struct lustre_disk_dqinfo)) {
142 CDEBUG(D_ERROR, "Can't read info structure on device %s.\n",
143 f->f_vfsmnt->mnt_sb->s_id);
146 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
147 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
148 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
149 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
150 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
151 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
156 * Read information header from quota file
158 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
160 return lustre_read_quota_file_info(lqi->qi_files[type],
161 &lqi->qi_info[type]);
165 * Write information header to quota file
167 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
169 struct lustre_disk_dqinfo dinfo;
170 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
171 struct file *f = lqi->qi_files[type];
174 info->dqi_flags &= ~DQF_INFO_DIRTY;
175 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
176 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
177 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
178 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
179 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
180 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
182 size = lustre_write_quota(f, (char *)&dinfo,
183 sizeof(struct lustre_disk_dqinfo),
186 if (size != sizeof(struct lustre_disk_dqinfo)) {
188 "Can't write info structure on device %s.\n",
189 f->f_vfsmnt->mnt_sb->s_id);
195 void disk2memdqb(struct lustre_mem_dqblk *m, void *d,
196 lustre_quota_version_t version)
198 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
200 LASSERT(version == LUSTRE_QUOTA_V2);
202 m->dqb_ihardlimit = le64_to_cpu(dqblk->dqb_ihardlimit);
203 m->dqb_isoftlimit = le64_to_cpu(dqblk->dqb_isoftlimit);
204 m->dqb_curinodes = le64_to_cpu(dqblk->dqb_curinodes);
205 m->dqb_itime = le64_to_cpu(dqblk->dqb_itime);
206 m->dqb_bhardlimit = le64_to_cpu(dqblk->dqb_bhardlimit);
207 m->dqb_bsoftlimit = le64_to_cpu(dqblk->dqb_bsoftlimit);
208 m->dqb_curspace = le64_to_cpu(dqblk->dqb_curspace);
209 m->dqb_btime = le64_to_cpu(dqblk->dqb_btime);
212 static int mem2diskdqb(void *d, struct lustre_mem_dqblk *m,
213 qid_t id, lustre_quota_version_t version)
215 struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
217 LASSERT(version == LUSTRE_QUOTA_V2);
219 dqblk->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
220 dqblk->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
221 dqblk->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
222 dqblk->dqb_itime = cpu_to_le64(m->dqb_itime);
223 dqblk->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
224 dqblk->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
225 dqblk->dqb_curspace = cpu_to_le64(m->dqb_curspace);
226 dqblk->dqb_btime = cpu_to_le64(m->dqb_btime);
227 dqblk->dqb_id = cpu_to_le32(id);
232 dqbuf_t getdqbuf(void)
234 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
237 "VFS: Not enough memory for quota buffers.\n");
241 void freedqbuf(dqbuf_t buf)
246 ssize_t read_blk(struct file *filp, struct inode *inode, int type,
247 uint blk, dqbuf_t buf)
251 memset(buf, 0, LUSTRE_DQBLKSIZE);
252 ret = lustre_read_quota(filp, inode, type, (char *)buf, LUSTRE_DQBLKSIZE,
253 blk << LUSTRE_DQBLKSIZE_BITS);
255 /* Reading past EOF just returns a block of zeros */
262 ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
266 ret = lustre_write_quota(filp, (char *)buf, LUSTRE_DQBLKSIZE,
267 blk << LUSTRE_DQBLKSIZE_BITS);
272 void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
274 cfs_set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
278 * Remove empty block from list and return it
280 int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
282 dqbuf_t buf = getdqbuf();
283 struct lustre_disk_dqdbheader *dh =
284 (struct lustre_disk_dqdbheader *)buf;
289 if (info->dqi_free_blk) {
290 blk = info->dqi_free_blk;
291 if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0)
293 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
295 memset(buf, 0, LUSTRE_DQBLKSIZE);
296 /* Assure block allocation... */
297 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0)
299 blk = info->dqi_blocks++;
301 lustre_mark_info_dirty(info);
309 * Insert empty block to the list
311 int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
312 dqbuf_t buf, uint blk)
314 struct lustre_disk_dqdbheader *dh =
315 (struct lustre_disk_dqdbheader *)buf;
318 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
319 dh->dqdh_prev_free = cpu_to_le32(0);
320 dh->dqdh_entries = cpu_to_le16(0);
321 info->dqi_free_blk = blk;
322 lustre_mark_info_dirty(info);
323 if ((err = write_blk(filp, blk, buf)) < 0)
324 /* Some strange block. We had better leave it... */
330 * Remove given block from the list of blocks with free entries
332 int remove_free_dqentry(struct file *filp,
333 struct lustre_mem_dqinfo *info, dqbuf_t buf,
336 dqbuf_t tmpbuf = getdqbuf();
337 struct lustre_disk_dqdbheader *dh =
338 (struct lustre_disk_dqdbheader *)buf;
339 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
340 le32_to_cpu(dh->dqdh_prev_free);
346 if ((err = read_blk(filp, NULL, 0, nextblk, tmpbuf)) < 0)
348 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
350 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
354 if ((err = read_blk(filp, NULL, 0, prevblk, tmpbuf)) < 0)
356 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
358 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
361 info->dqi_free_entry = nextblk;
362 lustre_mark_info_dirty(info);
365 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
366 if (write_blk(filp, blk, buf) < 0)
367 /* No matter whether write succeeds block is out of list */
369 "VFS: Can't write block (%u) with free entries.\n", blk);
377 * Insert given block to the beginning of list with free entries
379 int insert_free_dqentry(struct file *filp,
380 struct lustre_mem_dqinfo *info, dqbuf_t buf,
383 dqbuf_t tmpbuf = getdqbuf();
384 struct lustre_disk_dqdbheader *dh =
385 (struct lustre_disk_dqdbheader *)buf;
390 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
391 dh->dqdh_prev_free = cpu_to_le32(0);
392 if ((err = write_blk(filp, blk, buf)) < 0)
394 if (info->dqi_free_entry) {
395 if ((err = read_blk(filp, NULL, 0, info->dqi_free_entry, tmpbuf)) < 0)
397 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
399 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
403 info->dqi_free_entry = blk;
404 lustre_mark_info_dirty(info);
414 * Find space for dquot
416 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
417 lustre_quota_version_t version)
419 struct lustre_quota_info *lqi = dquot->dq_info;
420 struct file *filp = lqi->qi_files[dquot->dq_type];
421 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
423 struct lustre_disk_dqdbheader *dh;
425 int dqblk_sz = lustre_disk_dqblk_sz[version];
426 int dqstrinblk = lustre_dqstrinblk[version];
430 if (!(buf = getdqbuf())) {
434 dh = (struct lustre_disk_dqdbheader *)buf;
435 ddquot = GETENTRIES(buf, version);
436 if (info->dqi_free_entry) {
437 blk = info->dqi_free_entry;
438 if ((*err = read_blk(filp, NULL, 0, blk, buf)) < 0)
441 blk = get_free_dqblk(filp, info);
447 memset(buf, 0, LUSTRE_DQBLKSIZE);
448 info->dqi_free_entry = blk; /* This is enough as block is
449 already zeroed and entry list
451 lustre_mark_info_dirty(info);
454 /* Will block be full */
455 if (le16_to_cpu(dh->dqdh_entries) + 1 >= dqstrinblk)
456 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
458 "VFS: find_free_dqentry(): Can't remove block "
459 "(%u) from entry free list.\n", blk);
462 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
463 /* Find free structure in block */
464 for (i = 0; i < dqstrinblk &&
465 memcmp((char *)&emptydquot[version],
466 (char *)ddquot + i * dqblk_sz, dqblk_sz);
469 if (i == dqstrinblk) {
471 "VFS: find_free_dqentry(): Data block full but it "
477 if ((*err = write_blk(filp, blk, buf)) < 0) {
479 "VFS: find_free_dqentry(): Can't write quota data "
484 (blk << LUSTRE_DQBLKSIZE_BITS) +
485 sizeof(struct lustre_disk_dqdbheader) +
495 * Insert reference to structure into the trie
497 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
498 lustre_quota_version_t version)
500 struct lustre_quota_info *lqi = dquot->dq_info;
501 struct file *filp = lqi->qi_files[dquot->dq_type];
502 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
504 int ret = 0, newson = 0, newact = 0;
508 if (!(buf = getdqbuf()))
511 ret = get_free_dqblk(filp, info);
515 memset(buf, 0, LUSTRE_DQBLKSIZE);
518 if ((ret = read_blk(filp, NULL, 0, *treeblk, buf)) < 0) {
519 CERROR("VFS: Can't read tree quota block %u.\n",
525 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
528 if (depth == LUSTRE_DQTREEDEPTH - 1) {
532 "VFS: Inserting already present quota entry "
534 ref[GETIDINDEX(dquot->dq_id, depth)]);
539 newblk = find_free_dqentry(dquot, &ret, version);
541 ret = do_insert_tree(dquot, &newblk, depth + 1, version);
542 if (newson && ret >= 0) {
543 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
544 ret = write_blk(filp, *treeblk, buf);
545 } else if (newact && ret < 0)
546 put_free_dqblk(filp, info, buf, *treeblk);
553 * Wrapper for inserting quota structure into tree
555 static inline int dq_insert_tree(struct lustre_dquot *dquot,
556 lustre_quota_version_t version)
558 int tmp = LUSTRE_DQTREEOFF;
559 return do_insert_tree(dquot, &tmp, 0, version);
563 * We don't have to be afraid of deadlocks as we never have quotas on
566 static int lustre_write_dquot(struct lustre_dquot *dquot,
567 lustre_quota_version_t version)
569 int type = dquot->dq_type;
573 int dqblk_sz = lustre_disk_dqblk_sz[version];
574 struct lustre_disk_dqblk_v2 ddquot;
576 ret = mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id, version);
581 if ((ret = dq_insert_tree(dquot, version)) < 0) {
583 "VFS: Error %Zd occurred while creating "
587 filp = dquot->dq_info->qi_files[type];
588 offset = dquot->dq_off;
589 /* Argh... We may need to write structure full of zeroes but that would
590 * be treated as an empty place by the rest of the code. Format change
591 * would be definitely cleaner but the problems probably are not worth
593 if (!memcmp((char *)&emptydquot[version], (char *)&ddquot, dqblk_sz))
594 ddquot.dqb_itime = cpu_to_le64(1);
596 ret = lustre_write_quota(filp, (char *)&ddquot, dqblk_sz, offset);
597 if (ret != dqblk_sz) {
598 CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
599 filp->f_dentry->d_sb->s_id);
609 * Free dquot entry in data block
611 static int free_dqentry(struct lustre_dquot *dquot, uint blk,
612 lustre_quota_version_t version)
614 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
615 struct lustre_mem_dqinfo *info =
616 &dquot->dq_info->qi_info[dquot->dq_type];
617 struct lustre_disk_dqdbheader *dh;
618 dqbuf_t buf = getdqbuf();
619 int dqstrinblk = lustre_dqstrinblk[version];
624 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
626 "VFS: Quota structure has offset to other block (%u) "
627 "than it should (%u).\n",
628 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
631 if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0) {
632 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
635 dh = (struct lustre_disk_dqdbheader *)buf;
636 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
637 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
638 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
639 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
641 "VFS: Can't move quota data block (%u) to free "
646 memset(buf + (dquot->dq_off & ((1<<LUSTRE_DQBLKSIZE_BITS) - 1)),
647 0, lustre_disk_dqblk_sz[version]);
648 if (le16_to_cpu(dh->dqdh_entries) == dqstrinblk - 1) {
649 /* Insert will write block itself */
651 insert_free_dqentry(filp, info, buf, blk)) < 0) {
653 "VFS: Can't insert quota data block "
654 "(%u) to free entry list.\n", blk);
657 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
659 "VFS: Can't write quota data block %u\n", blk);
663 dquot->dq_off = 0; /* Quota is now unattached */
670 * Remove reference to dquot from tree
672 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
673 lustre_quota_version_t version)
675 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
676 struct lustre_mem_dqinfo *info =
677 &dquot->dq_info->qi_info[dquot->dq_type];
678 dqbuf_t buf = getdqbuf();
681 u32 *ref = (u32 *) buf;
685 if ((ret = read_blk(filp, NULL, 0, *blk, buf)) < 0) {
686 CERROR("VFS: Can't read quota data block %u\n", *blk);
689 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
690 if (depth == LUSTRE_DQTREEDEPTH - 1) {
691 ret = free_dqentry(dquot, newblk, version);
694 ret = remove_tree(dquot, &newblk, depth + 1, version);
695 if (ret >= 0 && !newblk) {
697 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
698 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++)
699 /* Block got empty? */ ;
700 /* don't put the root block into free blk list! */
701 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
702 put_free_dqblk(filp, info, buf, *blk);
704 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
706 "VFS: Can't write quota tree block %u.\n", *blk);
714 * Delete dquot from tree
716 static int lustre_delete_dquot(struct lustre_dquot *dquot,
717 lustre_quota_version_t version)
719 uint tmp = LUSTRE_DQTREEOFF;
721 if (!dquot->dq_off) /* Even not allocated? */
723 return remove_tree(dquot, &tmp, 0, version);
727 * Find entry in block
729 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
730 lustre_quota_version_t version)
732 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
733 dqbuf_t buf = getdqbuf();
736 struct lustre_disk_dqblk_v2 *ddquot =
737 (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
738 int dqblk_sz = lustre_disk_dqblk_sz[version];
739 int dqstrinblk = lustre_dqstrinblk[version];
741 LASSERT(version == LUSTRE_QUOTA_V2);
745 if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0) {
746 CERROR("VFS: Can't read quota tree block %u.\n", blk);
750 for (i = 0; i < dqstrinblk &&
751 le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id;
753 else { /* ID 0 as a bit more complicated searching... */
754 for (i = 0; i < dqstrinblk; i++)
755 if (!le32_to_cpu(ddquot[i].dqb_id)
756 && memcmp((char *)&emptydquot[version],
757 (char *)&ddquot[i], dqblk_sz))
760 if (i == dqstrinblk) {
762 "VFS: Quota for id %u referenced but not present.\n",
768 (blk << LUSTRE_DQBLKSIZE_BITS) +
769 sizeof(struct lustre_disk_dqdbheader) +
777 * Find entry for given id in the tree
779 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
780 lustre_quota_version_t version)
782 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
783 dqbuf_t buf = getdqbuf();
785 u32 *ref = (u32 *) buf;
789 if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0) {
790 CERROR("VFS: Can't read quota tree block %u.\n", blk);
794 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
795 if (!blk) /* No reference? */
797 if (depth < LUSTRE_DQTREEDEPTH - 1)
798 ret = find_tree_dqentry(dquot, blk, depth + 1, version);
800 ret = find_block_dqentry(dquot, blk, version);
807 * Find entry for given id in the tree - wrapper function
809 static inline loff_t find_dqentry(struct lustre_dquot *dquot,
810 lustre_quota_version_t version)
812 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
815 int lustre_read_dquot(struct lustre_dquot *dquot)
817 int type = dquot->dq_type;
820 int ret = 0, dqblk_sz;
821 lustre_quota_version_t version;
823 /* Invalidated quota? */
824 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
825 CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
829 version = dquot->dq_info->qi_version;
830 LASSERT(version == LUSTRE_QUOTA_V2);
831 dqblk_sz = lustre_disk_dqblk_sz[version];
833 offset = find_dqentry(dquot, version);
834 if (offset <= 0) { /* Entry not present? */
837 "VFS: Can't read quota structure for id %u.\n",
840 cfs_set_bit(DQ_FAKE_B, &dquot->dq_flags);
841 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
844 struct lustre_disk_dqblk_v2 ddquot;
846 dquot->dq_off = offset;
847 if ((ret = lustre_read_quota(filp, NULL, type, (char *)&ddquot,
848 dqblk_sz, offset)) != dqblk_sz) {
852 "VFS: Error while reading quota structure for id "
853 "%u.\n", dquot->dq_id);
854 memset((char *)&ddquot, 0, dqblk_sz);
857 /* We need to escape back all-zero structure */
858 if (!memcmp((char *)&fakedquot[version],
859 (char *)&ddquot, dqblk_sz))
860 ddquot.dqb_itime = cpu_to_le64(0);
862 disk2memdqb(&dquot->dq_dqb, &ddquot, version);
869 * Commit changes of dquot to disk - it might also mean deleting
870 * it when quota became fake.
872 int lustre_commit_dquot(struct lustre_dquot *dquot)
875 lustre_quota_version_t version = dquot->dq_info->qi_version;
877 struct inode *inode = dquot->dq_info->qi_files[dquot->dq_type]->f_dentry->d_inode;
880 /* always clear the flag so we don't loop on an IO error... */
881 cfs_clear_bit(DQ_MOD_B, &dquot->dq_flags);
883 /* The block/inode usage in admin quotafile isn't the real usage
884 * over all cluster, so keep the fake dquot entry on disk is
885 * meaningless, just remove it */
886 if (cfs_test_bit(DQ_FAKE_B, &dquot->dq_flags))
888 handle = lustre_quota_journal_start(inode, delete);
889 if (unlikely(IS_ERR(handle))) {
890 rc = PTR_ERR(handle);
891 CERROR("fail to lustre_quota_journal_start: rc = %d\n", rc);
896 rc = lustre_delete_dquot(dquot, version);
898 rc = lustre_write_dquot(dquot, version);
899 lustre_quota_journal_stop(handle);
904 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
905 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
910 int lustre_init_quota_header(struct lustre_quota_info *lqi, int type,
913 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
914 static const uint fake_magics[] = LUSTRE_BADQMAGICS;
915 const uint* quota_versions = lustre_initqversions[lqi->qi_version];
916 struct lustre_disk_dqheader dqhead;
918 struct file *fp = lqi->qi_files[type];
921 /* write quotafile header */
922 dqhead.dqh_magic = cpu_to_le32(fakemagics ?
923 fake_magics[type] : quota_magics[type]);
924 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
925 size = lustre_write_quota(fp, (char *)&dqhead,
926 sizeof(struct lustre_disk_dqheader), 0);
928 if (size != sizeof(struct lustre_disk_dqheader)) {
929 CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
937 * We need to export this function to initialize quotafile, because we haven't
938 * user level check utility
940 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
943 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
946 rc = lustre_init_quota_header(lqi, type, fakemagics);
950 /* write init quota info */
951 memset(dqinfo, 0, sizeof(*dqinfo));
952 dqinfo->dqi_bgrace = MAX_DQ_TIME;
953 dqinfo->dqi_igrace = MAX_IQ_TIME;
954 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
956 return lustre_write_quota_info(lqi, type);
959 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
961 return lustre_init_quota_info_generic(lqi, type, 0);
964 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
965 uint blk, cfs_list_t *list)
967 dqbuf_t buf = getdqbuf();
969 struct lustre_disk_dqdbheader *dqhead =
970 (struct lustre_disk_dqdbheader *)buf;
971 struct dqblk *blk_item;
977 if ((ret = read_blk(filp, inode, type, blk, buf)) < 0) {
978 CERROR("VFS: Can't read quota tree block %u.\n", blk);
983 if (!le32_to_cpu(dqhead->dqdh_entries))
986 if (cfs_list_empty(list)) {
991 cfs_list_for_each_entry(pos, list, link) {
992 if (blk == pos->blk) /* we got this blk already */
1000 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
1005 blk_item->blk = blk;
1006 CFS_INIT_LIST_HEAD(&blk_item->link);
1008 cfs_list_add_tail(&blk_item->link, tmp);
1015 int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
1016 uint blk, int depth, cfs_list_t *list)
1018 dqbuf_t buf = getdqbuf();
1021 u32 *ref = (u32 *) buf;
1025 if ((ret = read_blk(filp, inode, type, blk, buf)) < 0) {
1026 CERROR("VFS: Can't read quota tree block %u.\n", blk);
1031 for (index = 0; index <= 0xff && !ret; index++) {
1032 blk = le32_to_cpu(ref[index]);
1033 if (!blk) /* No reference */
1036 if (depth < LUSTRE_DQTREEDEPTH - 1)
1037 ret = walk_tree_dqentry(filp, inode, type, blk,
1040 ret = walk_block_dqentry(filp, inode, type, blk, list);
1048 * Walk through the quota file (v2 format) to get all ids with quota limit
1050 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
1053 cfs_list_t blk_list;
1054 struct dqblk *blk_item, *tmp;
1056 struct lustre_disk_dqblk_v2 *ddquot;
1058 lustre_quota_version_t version;
1062 LASSERT(ergo(fp == NULL, inode != NULL));
1064 if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
1065 version = LUSTRE_QUOTA_V2;
1067 CDEBUG(D_ERROR, "unknown quota file format!\n");
1071 if (!cfs_list_empty(list)) {
1072 CDEBUG(D_ERROR, "not empty list\n");
1076 CFS_INIT_LIST_HEAD(&blk_list);
1077 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
1079 CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
1082 if (cfs_list_empty(&blk_list))
1088 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
1090 cfs_list_for_each_entry(blk_item, &blk_list, link) {
1092 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
1094 memset(buf, 0, LUSTRE_DQBLKSIZE);
1095 if ((ret = read_blk(fp, inode, type, blk_item->blk, buf)) < 0) {
1096 CERROR("VFS: Can't read quota tree block %u.\n",
1098 GOTO(out_free, rc = ret);
1101 for (i = 0; i < lustre_dqstrinblk[version]; i++) {
1102 struct dquot_id *dqid;
1103 /* skip empty entry */
1104 if (!memcmp((char *)&emptydquot[version],
1105 (char *)&ddquot[i], dqblk_sz))
1108 OBD_ALLOC_GFP(dqid, sizeof(*dqid), GFP_NOFS);
1110 GOTO(out_free, rc = -ENOMEM);
1112 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
1113 dqid->di_flag = le64_to_cpu(ddquot[i].dqb_ihardlimit) ?
1115 dqid->di_flag |= le64_to_cpu(ddquot[i].dqb_bhardlimit) ?
1118 CFS_INIT_LIST_HEAD(&dqid->di_link);
1119 cfs_list_add(&dqid->di_link, list);
1124 cfs_list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1125 cfs_list_del_init(&blk_item->link);
1135 EXPORT_SYMBOL(lustre_read_quota_info);
1136 EXPORT_SYMBOL(lustre_write_quota_info);
1137 EXPORT_SYMBOL(lustre_check_quota_file);
1138 EXPORT_SYMBOL(lustre_read_dquot);
1139 EXPORT_SYMBOL(lustre_commit_dquot);
1140 EXPORT_SYMBOL(lustre_init_quota_info);
1141 EXPORT_SYMBOL(lustre_get_qids);