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_V1] = LUSTRE_INITQVERSIONS_V1,
68 [LUSTRE_QUOTA_V2] = LUSTRE_INITQVERSIONS_V2
71 static const int lustre_dqstrinblk[] = {
72 [LUSTRE_QUOTA_V1] = LUSTRE_DQSTRINBLK,
73 [LUSTRE_QUOTA_V2] = LUSTRE_DQSTRINBLK_V2
76 static const int lustre_disk_dqblk_sz[] = {
77 [LUSTRE_QUOTA_V1] = sizeof(struct lustre_disk_dqblk),
78 [LUSTRE_QUOTA_V2] = sizeof(struct lustre_disk_dqblk_v2)
83 struct lustre_disk_dqblk r0;
84 struct lustre_disk_dqblk_v2 r1;
86 [LUSTRE_QUOTA_V1] = {.r0 = {.dqb_itime = __constant_cpu_to_le64(1LLU)} },
87 [LUSTRE_QUOTA_V2] = {.r1 = {.dqb_itime = __constant_cpu_to_le64(1LLU)} }
92 struct lustre_disk_dqblk r0;
93 struct lustre_disk_dqblk_v2 r1;
95 [LUSTRE_QUOTA_V1] = {.r0 = { 0 } },
96 [LUSTRE_QUOTA_V2] = {.r1 = { 0 } }
99 int check_quota_file(struct file *f, struct inode *inode, int type,
100 lustre_quota_version_t version)
102 struct lustre_disk_dqheader dqhead;
106 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
107 const uint *quota_versions = lustre_initqversions[version];
110 CERROR("check_quota_file failed!\n");
111 libcfs_debug_dumpstack(NULL);
118 size = f->f_op->read(f, (char *)&dqhead,
119 sizeof(struct lustre_disk_dqheader),
123 #ifndef KERNEL_SUPPORTS_QUOTA_READ
126 struct super_block *sb = inode->i_sb;
127 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
128 sizeof(struct lustre_disk_dqheader), 0);
131 if (size != sizeof(struct lustre_disk_dqheader))
133 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
134 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
139 /* Check whether given file is really lustre admin quotafile */
140 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
142 struct file *f = lqi->qi_files[type];
143 return check_quota_file(f, NULL, type, lqi->qi_version);
146 int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
149 struct lustre_disk_dqinfo dinfo;
151 loff_t offset = LUSTRE_DQINFOOFF;
155 size = f->f_op->read(f, (char *)&dinfo,
156 sizeof(struct lustre_disk_dqinfo), &offset);
158 if (size != sizeof(struct lustre_disk_dqinfo)) {
159 CERROR("Can't read info structure on device %s.\n",
160 f->f_vfsmnt->mnt_sb->s_id);
163 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
164 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
165 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
166 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
167 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
168 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
172 /* Read information header from quota file */
173 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
175 return lustre_read_quota_file_info(lqi->qi_files[type], &lqi->qi_info[type]);
178 /* Write information header to quota file */
179 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
182 struct lustre_disk_dqinfo dinfo;
183 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
184 struct file *f = lqi->qi_files[type];
186 loff_t offset = LUSTRE_DQINFOOFF;
188 info->dqi_flags &= ~DQF_INFO_DIRTY;
189 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
190 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
191 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
192 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
193 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
194 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
197 size = f->f_op->write(f, (char *)&dinfo,
198 sizeof(struct lustre_disk_dqinfo), &offset);
200 if (size != sizeof(struct lustre_disk_dqinfo)) {
201 CWARN("Can't write info structure on device %s.\n",
202 f->f_vfsmnt->mnt_sb->s_id);
208 #define DQ2MQ(v) ((sizeof(v) == sizeof(__u64)) ? \
209 le64_to_cpu(v) : le32_to_cpu(v))
211 #define MQ2DQ(v,newv) ((sizeof(v) == sizeof(__u64)) ? \
212 (v = cpu_to_le64((__u64)newv)) : (v = cpu_to_le32((__u32)newv)))
214 #define DQF_GET(var,ver,field) ((ver == LUSTRE_QUOTA_V1)?\
215 DQ2MQ(((struct lustre_disk_dqblk*)(var))->field):\
216 DQ2MQ(((struct lustre_disk_dqblk_v2*)(var))->field))
218 #define DQF_PUT(var,ver,field,val) ((ver == LUSTRE_QUOTA_V1)?\
219 MQ2DQ(((struct lustre_disk_dqblk*)(var))->field, val):\
220 MQ2DQ(((struct lustre_disk_dqblk_v2*)(var))->field, val))
222 void disk2memdqb(struct lustre_mem_dqblk *m, void *d,
223 lustre_quota_version_t version)
225 m->dqb_ihardlimit = DQF_GET(d, version, dqb_ihardlimit);
226 m->dqb_isoftlimit = DQF_GET(d, version, dqb_isoftlimit);
227 m->dqb_curinodes = DQF_GET(d, version, dqb_curinodes);
228 m->dqb_itime = DQF_GET(d, version, dqb_itime);
229 m->dqb_bhardlimit = DQF_GET(d, version, dqb_bhardlimit);
230 m->dqb_bsoftlimit = DQF_GET(d, version, dqb_bsoftlimit);
231 m->dqb_curspace = DQF_GET(d, version, dqb_curspace);
232 m->dqb_btime = DQF_GET(d, version, dqb_btime);
235 static int check_quota_bounds(struct lustre_mem_dqblk *m,
236 lustre_quota_version_t version)
238 return (version == LUSTRE_QUOTA_V1 &&
239 m->dqb_ihardlimit <= MAX_UL &&
240 m->dqb_isoftlimit <= MAX_UL &&
241 m->dqb_curinodes <= MAX_UL &&
242 m->dqb_bhardlimit <= MAX_UL &&
243 m->dqb_bsoftlimit <= MAX_UL) ||
244 version != LUSTRE_QUOTA_V1;
247 static int mem2diskdqb(void *d, struct lustre_mem_dqblk *m,
248 qid_t id, lustre_quota_version_t version)
250 if (!check_quota_bounds(m, version))
253 DQF_PUT(d, version, dqb_ihardlimit, m->dqb_ihardlimit);
254 DQF_PUT(d, version, dqb_isoftlimit, m->dqb_isoftlimit);
255 DQF_PUT(d, version, dqb_curinodes, m->dqb_curinodes);
256 DQF_PUT(d, version, dqb_itime, m->dqb_itime);
257 DQF_PUT(d, version, dqb_bhardlimit, m->dqb_bhardlimit);
258 DQF_PUT(d, version, dqb_bsoftlimit, m->dqb_bsoftlimit);
259 DQF_PUT(d, version, dqb_curspace, m->dqb_curspace);
260 DQF_PUT(d, version, dqb_btime, m->dqb_btime);
261 DQF_PUT(d, version, dqb_id, id);
266 dqbuf_t getdqbuf(void)
268 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
270 CWARN("VFS: Not enough memory for quota buffers.\n");
274 void freedqbuf(dqbuf_t buf)
279 ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
283 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
285 memset(buf, 0, LUSTRE_DQBLKSIZE);
288 ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
293 ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
297 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
301 ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
306 void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
308 set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
311 /* Remove empty block from list and return it */
312 int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
314 dqbuf_t buf = getdqbuf();
315 struct lustre_disk_dqdbheader *dh =
316 (struct lustre_disk_dqdbheader *)buf;
321 if (info->dqi_free_blk) {
322 blk = info->dqi_free_blk;
323 if ((ret = read_blk(filp, blk, buf)) < 0)
325 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
327 memset(buf, 0, LUSTRE_DQBLKSIZE);
328 /* Assure block allocation... */
329 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0)
331 blk = info->dqi_blocks++;
333 lustre_mark_info_dirty(info);
340 /* Insert empty block to the list */
341 int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
342 dqbuf_t buf, uint blk)
344 struct lustre_disk_dqdbheader *dh =
345 (struct lustre_disk_dqdbheader *)buf;
348 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
349 dh->dqdh_prev_free = cpu_to_le32(0);
350 dh->dqdh_entries = cpu_to_le16(0);
351 info->dqi_free_blk = blk;
352 lustre_mark_info_dirty(info);
353 if ((err = write_blk(filp, blk, buf)) < 0)
354 /* Some strange block. We had better leave it... */
359 /* Remove given block from the list of blocks with free entries */
360 int remove_free_dqentry(struct file *filp,
361 struct lustre_mem_dqinfo *info, dqbuf_t buf,
364 dqbuf_t tmpbuf = getdqbuf();
365 struct lustre_disk_dqdbheader *dh =
366 (struct lustre_disk_dqdbheader *)buf;
367 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
368 le32_to_cpu(dh->dqdh_prev_free);
374 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
376 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
378 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
382 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
384 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
386 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
389 info->dqi_free_entry = nextblk;
390 lustre_mark_info_dirty(info);
393 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
394 err = write_blk(filp, blk, buf);
395 if (err < 0) /* No matter whether write succeeds block is out of list */
396 CERROR("VFS: Can't write block (%u) with "
397 "free entries (rc=%d).\n", blk, err);
404 /* Insert given block to the beginning of list with free entries */
405 int insert_free_dqentry(struct file *filp,
406 struct lustre_mem_dqinfo *info, dqbuf_t buf,
409 dqbuf_t tmpbuf = getdqbuf();
410 struct lustre_disk_dqdbheader *dh =
411 (struct lustre_disk_dqdbheader *)buf;
416 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
417 dh->dqdh_prev_free = cpu_to_le32(0);
418 if ((err = write_blk(filp, blk, buf)) < 0)
420 if (info->dqi_free_entry) {
421 if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
423 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
425 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
429 info->dqi_free_entry = blk;
430 lustre_mark_info_dirty(info);
439 /* Find space for dquot */
440 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
441 lustre_quota_version_t version)
443 struct lustre_quota_info *lqi = dquot->dq_info;
444 struct file *filp = lqi->qi_files[dquot->dq_type];
445 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
447 struct lustre_disk_dqdbheader *dh;
449 int dqblk_sz = lustre_disk_dqblk_sz[version];
450 int dqstrinblk = lustre_dqstrinblk[version];
454 if (!(buf = getdqbuf())) {
458 dh = (struct lustre_disk_dqdbheader *)buf;
459 ddquot = GETENTRIES(buf, version);
460 if (info->dqi_free_entry) {
461 blk = info->dqi_free_entry;
462 if ((*err = read_blk(filp, blk, buf)) < 0)
465 blk = get_free_dqblk(filp, info);
471 memset(buf, 0, LUSTRE_DQBLKSIZE);
472 info->dqi_free_entry = blk; /* This is enough as block is
473 already zeroed and entry list
475 lustre_mark_info_dirty(info);
478 /* Will block be full */
479 if (le16_to_cpu(dh->dqdh_entries) + 1 >= dqstrinblk)
480 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
481 CERROR("VFS: Can't remove block %u"
482 " from entry free list.\n", blk);
485 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
486 /* Find free structure in block */
487 for (i = 0; i < dqstrinblk &&
488 memcmp((char *)&emptydquot[version],
489 (char*)ddquot + i * dqblk_sz,
492 if (i == dqstrinblk) {
493 CERROR("VFS: Data block full but it shouldn't.\n");
498 if ((*err = write_blk(filp, blk, buf)) < 0) {
499 CERROR("VFS: Can't write quota data block %u.\n", blk);
503 (blk << LUSTRE_DQBLKSIZE_BITS) +
504 sizeof(struct lustre_disk_dqdbheader) +
513 /* Insert reference to structure into the trie */
514 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
515 lustre_quota_version_t version)
517 struct lustre_quota_info *lqi = dquot->dq_info;
518 struct file *filp = lqi->qi_files[dquot->dq_type];
519 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
521 int ret = 0, newson = 0, newact = 0;
525 if (!(buf = getdqbuf()))
528 ret = get_free_dqblk(filp, info);
532 memset(buf, 0, LUSTRE_DQBLKSIZE);
535 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
536 CERROR("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) {
548 CERROR("VFS: Inserting already present quota entry "
550 ref[GETIDINDEX(dquot->dq_id, depth)]);
555 newblk = find_free_dqentry(dquot, &ret, version);
557 ret = do_insert_tree(dquot, &newblk, depth + 1, version);
558 if (newson && ret >= 0) {
559 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
560 ret = write_blk(filp, *treeblk, buf);
561 } else if (newact && ret < 0)
562 put_free_dqblk(filp, info, buf, *treeblk);
568 /* Wrapper for inserting quota structure into tree */
569 static inline int dq_insert_tree(struct lustre_dquot *dquot,
570 lustre_quota_version_t version)
572 int tmp = LUSTRE_DQTREEOFF;
573 return do_insert_tree(dquot, &tmp, 0, version);
577 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
579 static int lustre_write_dquot(struct lustre_dquot *dquot,
580 lustre_quota_version_t version)
582 int type = dquot->dq_type;
587 int dqblk_sz = lustre_disk_dqblk_sz[version];
588 char ddquot[sizeof(union lustre_disk_dqblk_un)];
590 ret = mem2diskdqb(ddquot, &dquot->dq_dqb, dquot->dq_id, version);
595 if ((ret = dq_insert_tree(dquot, version)) < 0) {
596 CERROR("VFS: Error %Zd occurred while creating quota.\n",
600 filp = dquot->dq_info->qi_files[type];
601 offset = dquot->dq_off;
602 /* Argh... We may need to write structure full of zeroes but that would be
603 * treated as an empty place by the rest of the code. Format change would
604 * be definitely cleaner but the problems probably are not worth it */
605 if (!memcmp((char *)&emptydquot[version], ddquot, dqblk_sz))
606 DQF_PUT(ddquot, version, dqb_itime, 1);
609 ret = filp->f_op->write(filp, ddquot,
612 if (ret != dqblk_sz) {
613 CWARN("VFS: dquota write failed on dev %s\n",
614 filp->f_dentry->d_sb->s_id);
623 /* Free dquot entry in data block */
624 static int free_dqentry(struct lustre_dquot *dquot, uint blk,
625 lustre_quota_version_t version)
627 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
628 struct lustre_mem_dqinfo *info =
629 &dquot->dq_info->qi_info[dquot->dq_type];
630 struct lustre_disk_dqdbheader *dh;
631 dqbuf_t buf = getdqbuf();
632 int dqstrinblk = lustre_dqstrinblk[version];
637 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
638 CERROR("VFS: Quota structure has offset to other block (%u) "
639 "than it should (%u).\n", blk,
640 (uint)(dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
643 if ((ret = read_blk(filp, blk, buf)) < 0) {
644 CERROR("VFS: Can't read quota data block %u\n", blk);
647 dh = (struct lustre_disk_dqdbheader *)buf;
648 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
649 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
650 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
651 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
652 CERROR("VFS: Can't move quota data block (%u) "
653 "to free list.\n", blk);
657 memset(buf + (dquot->dq_off & ((1<<LUSTRE_DQBLKSIZE_BITS) - 1)),
658 0, lustre_disk_dqblk_sz[version]);
659 if (le16_to_cpu(dh->dqdh_entries) == dqstrinblk - 1) {
660 /* Insert will write block itself */
662 insert_free_dqentry(filp, info, buf, blk)) < 0) {
663 CERROR("VFS: Can't insert quota data block (%u) "
664 "to free entry list.\n", blk);
667 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
668 CERROR("VFS: Can't write quota data block %u\n", blk);
672 dquot->dq_off = 0; /* Quota is now unattached */
678 /* 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 CERROR("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)
712 CERROR("VFS: Can't write quota tree block %u.\n", *blk);
719 /* Delete dquot from tree */
720 static int lustre_delete_dquot(struct lustre_dquot *dquot,
721 lustre_quota_version_t version)
723 uint tmp = LUSTRE_DQTREEOFF;
725 if (!dquot->dq_off) /* Even not allocated? */
727 return remove_tree(dquot, &tmp, 0, version);
730 /* Find entry in block */
731 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
732 lustre_quota_version_t version)
734 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
735 dqbuf_t buf = getdqbuf();
738 char *ddquot = GETENTRIES(buf, version);
739 int dqblk_sz = lustre_disk_dqblk_sz[version];
740 int dqstrinblk = lustre_dqstrinblk[version];
744 if ((ret = read_blk(filp, blk, buf)) < 0) {
745 CERROR("VFS: Can't read quota tree block %u.\n", blk);
749 for (i = 0; i < dqstrinblk &&
750 DQF_GET(ddquot+i*dqblk_sz, version, dqb_id) != dquot->dq_id;
752 else { /* ID 0 as a bit more complicated searching... */
753 for (i = 0; i < dqstrinblk; i++)
754 if (!DQF_GET(ddquot + i*dqblk_sz, version, dqb_id)
755 && memcmp((char *)&emptydquot[version],
760 if (i == dqstrinblk) {
761 CERROR("VFS: Quota for id %u referenced but not present.\n",
767 (blk << LUSTRE_DQBLKSIZE_BITS) +
768 sizeof(struct lustre_disk_dqdbheader) +
775 /* Find entry for given id in the tree */
776 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
777 lustre_quota_version_t version)
779 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
780 dqbuf_t buf = getdqbuf();
782 u32 *ref = (u32 *) buf;
786 if ((ret = read_blk(filp, blk, buf)) < 0) {
787 CERROR("VFS: Can't read quota tree block %u.\n", blk);
791 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
792 if (!blk) /* No reference? */
794 if (depth < LUSTRE_DQTREEDEPTH - 1)
795 ret = find_tree_dqentry(dquot, blk, depth + 1, version);
797 ret = find_block_dqentry(dquot, blk, version);
803 /* Find entry for given id in the tree - wrapper function */
804 static inline loff_t find_dqentry(struct lustre_dquot *dquot,
805 lustre_quota_version_t version)
807 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
811 int lustre_read_dquot(struct lustre_dquot *dquot)
813 int type = dquot->dq_type;
817 int ret = 0, dqblk_sz;
818 lustre_quota_version_t version;
820 /* Invalidated quota? */
821 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
822 CERROR("VFS: Quota invalidated while reading!\n");
826 version = dquot->dq_info->qi_version;
827 dqblk_sz = lustre_disk_dqblk_sz[version];
829 offset = find_dqentry(dquot, version);
830 if (offset <= 0) { /* Entry not present? */
832 CERROR("VFS: Can't read quota structure for id %u.\n",
835 set_bit(DQ_FAKE_B, &dquot->dq_flags);
836 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
839 char ddquot[sizeof(union lustre_disk_dqblk_un)];
841 dquot->dq_off = offset;
844 if ((ret = filp->f_op->read(filp, ddquot, dqblk_sz, &offset)) !=
848 CERROR("VFS: Error while reading quota structure "
849 "for id %u.\n", dquot->dq_id);
850 memset(ddquot, 0, dqblk_sz);
853 /* We need to escape back all-zero structure */
854 if (!memcmp((char *)&fakedquot[version],
856 DQF_PUT(ddquot, version, dqb_itime, 0);
859 disk2memdqb(&dquot->dq_dqb, ddquot, version);
865 /* Commit changes of dquot to disk - it might also mean deleting it when quota became fake */
866 int lustre_commit_dquot(struct lustre_dquot *dquot)
869 lustre_quota_version_t version = dquot->dq_info->qi_version;
871 /* always clear the flag so we don't loop on an IO error... */
872 clear_bit(DQ_MOD_B, &dquot->dq_flags);
874 /* The block/inode usage in admin quotafile isn't the real usage
875 * over all cluster, so keep the fake dquot entry on disk is
876 * meaningless, just remove it */
877 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
878 rc = lustre_delete_dquot(dquot, version);
880 rc = lustre_write_dquot(dquot, version);
885 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
886 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
891 int lustre_init_quota_header(struct lustre_quota_info *lqi, int type, int fakemagics)
893 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
894 static const uint fake_magics[] = LUSTRE_BADQMAGICS;
895 const uint* quota_versions = lustre_initqversions[lqi->qi_version];
896 struct lustre_disk_dqheader dqhead;
899 struct file *fp = lqi->qi_files[type];
902 /* write quotafile header */
903 dqhead.dqh_magic = cpu_to_le32(fakemagics ?
904 fake_magics[type] : quota_magics[type]);
905 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
906 size = fp->f_op->write(fp, (char *)&dqhead,
907 sizeof(struct lustre_disk_dqheader), &offset);
909 if (size != sizeof(struct lustre_disk_dqheader)) {
910 CERROR("error writing quotafile header (rc:%d)\n", rc);
917 /* We need to export this function to initialize quotafile, because we haven't
918 * user level check utility */
919 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
922 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
925 rc = lustre_init_quota_header(lqi, type, fakemagics);
929 /* write init quota info */
930 memset(dqinfo, 0, sizeof(*dqinfo));
931 dqinfo->dqi_bgrace = MAX_DQ_TIME;
932 dqinfo->dqi_igrace = MAX_IQ_TIME;
933 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
935 return lustre_write_quota_info(lqi, type);
938 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
940 return lustre_init_quota_info_generic(lqi, type, 0);
943 ssize_t quota_read(struct file *file, struct inode *inode, int type,
944 uint blk, dqbuf_t buf)
947 return read_blk(file, blk, buf);
949 #ifndef KERNEL_SUPPORTS_QUOTA_READ
952 struct super_block *sb = inode->i_sb;
953 memset(buf, 0, LUSTRE_DQBLKSIZE);
954 return sb->s_op->quota_read(sb, type, (char *)buf,
956 blk << LUSTRE_DQBLKSIZE_BITS);
961 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
962 uint blk, struct list_head *list)
964 dqbuf_t buf = getdqbuf();
966 struct lustre_disk_dqdbheader *dqhead =
967 (struct lustre_disk_dqdbheader *)buf;
968 struct dqblk *blk_item;
970 struct list_head *tmp;
974 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
975 CERROR("VFS: Can't read quota tree block %u.\n", blk);
980 if (!le32_to_cpu(dqhead->dqdh_entries))
983 if (list_empty(list)) {
988 list_for_each_entry(pos, list, link) {
989 if (blk == pos->blk) /* we got this blk already */
997 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
1002 blk_item->blk = blk;
1003 INIT_LIST_HEAD(&blk_item->link);
1005 list_add_tail(&blk_item->link, tmp);
1012 int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
1013 uint blk, int depth, struct list_head *list)
1015 dqbuf_t buf = getdqbuf();
1018 u32 *ref = (u32 *) buf;
1022 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
1023 CERROR("VFS: Can't read quota tree block %u.\n", blk);
1028 for (index = 0; index <= 0xff && !ret; index++) {
1029 blk = le32_to_cpu(ref[index]);
1030 if (!blk) /* No reference */
1033 if (depth < LUSTRE_DQTREEDEPTH - 1)
1034 ret = walk_tree_dqentry(filp, inode, type, blk,
1037 ret = walk_block_dqentry(filp, inode, type, blk, list);
1044 /* Walk through the quota file (v2 format) to get all ids with quota limit */
1045 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
1046 struct list_head *list)
1048 struct list_head blk_list;
1049 struct dqblk *blk_item, *tmp;
1053 lustre_quota_version_t version;
1057 LASSERT(ergo(fp == NULL, inode != NULL));
1059 if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V1) == 0)
1060 version = LUSTRE_QUOTA_V1;
1061 else if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
1062 version = LUSTRE_QUOTA_V2;
1064 CERROR("unknown quota file format!\n");
1068 if (!list_empty(list)) {
1069 CERROR("not empty list\n");
1073 INIT_LIST_HEAD(&blk_list);
1074 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
1076 CERROR("walk through quota file failed!(%d)\n", rc);
1079 if (list_empty(&blk_list))
1085 ddquot = GETENTRIES(buf, version);
1087 list_for_each_entry(blk_item, &blk_list, link) {
1089 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
1091 memset(buf, 0, LUSTRE_DQBLKSIZE);
1092 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
1093 CERROR("VFS: Can't read quota tree block %u.\n",
1095 GOTO(out_free, rc = ret);
1098 for (i = 0; i < lustre_dqstrinblk[version]; i++) {
1099 struct dquot_id *dqid;
1100 /* skip empty entry */
1101 if (!memcmp((char *)&emptydquot[version],
1102 ddquot + i*dqblk_sz, dqblk_sz))
1105 OBD_ALLOC_GFP(dqid, sizeof(*dqid), GFP_NOFS);
1107 GOTO(out_free, rc = -ENOMEM);
1109 dqid->di_id = DQF_GET(ddquot + i * dqblk_sz,
1111 dqid->di_flag = DQF_GET(ddquot + i * dqblk_sz, version,
1112 dqb_ihardlimit) ? QI_SET : 0;
1113 dqid->di_flag |= DQF_GET(ddquot + i * dqblk_sz, version,
1114 dqb_bhardlimit) ? QB_SET : 0;
1115 INIT_LIST_HEAD(&dqid->di_link);
1116 list_add(&dqid->di_link, list);
1121 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1122 list_del_init(&blk_item->link);
1132 EXPORT_SYMBOL(lustre_read_quota_info);
1133 EXPORT_SYMBOL(lustre_write_quota_info);
1134 EXPORT_SYMBOL(lustre_check_quota_file);
1135 EXPORT_SYMBOL(lustre_read_dquot);
1136 EXPORT_SYMBOL(lustre_commit_dquot);
1137 EXPORT_SYMBOL(lustre_init_quota_info);
1138 EXPORT_SYMBOL(lustre_get_qids);