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
44 # define EXPORT_SYMTAB
47 #include <linux/errno.h>
49 #include <linux/mount.h>
50 #include <linux/kernel.h>
51 #include <linux/init.h>
52 #include <linux/module.h>
53 #include <linux/slab.h>
54 #include <linux/quotaio_v1.h>
56 #include <asm/byteorder.h>
57 #include <asm/uaccess.h>
59 #include <lustre_quota.h>
60 #include "lustre_quota_fmt.h"
62 typedef char *dqbuf_t;
64 #define GETIDINDEX(id, depth) (((id) >> ((LUSTRE_DQTREEDEPTH-(depth)-1)*8)) & 0xff)
65 #define GETENTRIES(buf) ((struct lustre_disk_dqblk *)(((char *)buf)+sizeof(struct lustre_disk_dqdbheader)))
67 static int check_quota_file(struct file *f, struct inode *inode, int type)
69 struct lustre_disk_dqheader dqhead;
73 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
74 static const uint quota_versions[] = LUSTRE_INITQVERSIONS;
79 size = f->f_op->read(f, (char *)&dqhead,
80 sizeof(struct lustre_disk_dqheader),
84 #ifndef KERNEL_SUPPORTS_QUOTA_READ
87 struct super_block *sb = inode->i_sb;
88 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
89 sizeof(struct lustre_disk_dqheader), 0);
92 if (size != sizeof(struct lustre_disk_dqheader))
94 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
95 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
100 /* Check whether given file is really lustre admin quotafile */
101 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
103 struct file *f = lqi->qi_files[type];
104 return check_quota_file(f, NULL, type);
107 /* Read information header from quota file */
108 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
111 struct lustre_disk_dqinfo dinfo;
112 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
113 struct file *f = lqi->qi_files[type];
115 loff_t offset = LUSTRE_DQINFOOFF;
119 size = f->f_op->read(f, (char *)&dinfo,
120 sizeof(struct lustre_disk_dqinfo), &offset);
122 if (size != sizeof(struct lustre_disk_dqinfo)) {
123 CDEBUG(D_WARNING, "Can't read info structure on device %s.\n",
124 f->f_vfsmnt->mnt_sb->s_id);
127 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
128 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
129 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
130 info->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
131 info->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
132 info->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
136 /* Write information header to quota file */
137 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
140 struct lustre_disk_dqinfo dinfo;
141 struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
142 struct file *f = lqi->qi_files[type];
144 loff_t offset = LUSTRE_DQINFOOFF;
146 info->dqi_flags &= ~DQF_INFO_DIRTY;
147 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
148 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
149 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
150 dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
151 dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
152 dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
155 size = f->f_op->write(f, (char *)&dinfo,
156 sizeof(struct lustre_disk_dqinfo), &offset);
158 if (size != sizeof(struct lustre_disk_dqinfo)) {
160 "Can't write info structure on device %s.\n",
161 f->f_vfsmnt->mnt_sb->s_id);
167 static void disk2memdqb(struct mem_dqblk *m, struct lustre_disk_dqblk *d)
169 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
170 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
171 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
172 m->dqb_itime = le64_to_cpu(d->dqb_itime);
173 m->dqb_bhardlimit = le32_to_cpu(d->dqb_bhardlimit);
174 m->dqb_bsoftlimit = le32_to_cpu(d->dqb_bsoftlimit);
175 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
176 m->dqb_btime = le64_to_cpu(d->dqb_btime);
179 static void mem2diskdqb(struct lustre_disk_dqblk *d, struct mem_dqblk *m,
182 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
183 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
184 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
185 d->dqb_itime = cpu_to_le64(m->dqb_itime);
186 d->dqb_bhardlimit = cpu_to_le32(m->dqb_bhardlimit);
187 d->dqb_bsoftlimit = cpu_to_le32(m->dqb_bsoftlimit);
188 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
189 d->dqb_btime = cpu_to_le64(m->dqb_btime);
190 d->dqb_id = cpu_to_le32(id);
193 static dqbuf_t getdqbuf(void)
195 dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
198 "VFS: Not enough memory for quota buffers.\n");
202 static inline void freedqbuf(dqbuf_t buf)
207 static ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
211 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
213 memset(buf, 0, LUSTRE_DQBLKSIZE);
216 ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
221 static ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
225 loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
229 ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
235 static void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
237 set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
240 #define lustre_info_dirty(info) test_bit(DQF_INFO_DIRTY_B, &(info)->dqi_flags)
242 /* Remove empty block from list and return it */
243 static int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
245 dqbuf_t buf = getdqbuf();
246 struct lustre_disk_dqdbheader *dh =
247 (struct lustre_disk_dqdbheader *)buf;
252 if (info->dqi_free_blk) {
253 blk = info->dqi_free_blk;
254 if ((ret = read_blk(filp, blk, buf)) < 0)
256 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
258 memset(buf, 0, LUSTRE_DQBLKSIZE);
259 if ((ret = write_blk(filp, info->dqi_blocks, buf)) < 0) /* Assure block allocation... */
261 blk = info->dqi_blocks++;
263 lustre_mark_info_dirty(info);
270 /* Insert empty block to the list */
271 static int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
272 dqbuf_t buf, uint blk)
274 struct lustre_disk_dqdbheader *dh =
275 (struct lustre_disk_dqdbheader *)buf;
278 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_blk);
279 dh->dqdh_prev_free = cpu_to_le32(0);
280 dh->dqdh_entries = cpu_to_le16(0);
281 info->dqi_free_blk = blk;
282 lustre_mark_info_dirty(info);
283 if ((err = write_blk(filp, blk, buf)) < 0)
284 /* Some strange block. We had better leave it... */
289 /* Remove given block from the list of blocks with free entries */
290 static int remove_free_dqentry(struct file *filp,
291 struct lustre_mem_dqinfo *info, dqbuf_t buf,
294 dqbuf_t tmpbuf = getdqbuf();
295 struct lustre_disk_dqdbheader *dh =
296 (struct lustre_disk_dqdbheader *)buf;
297 uint nextblk = le32_to_cpu(dh->dqdh_next_free), prevblk =
298 le32_to_cpu(dh->dqdh_prev_free);
304 if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
306 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
308 if ((err = write_blk(filp, nextblk, tmpbuf)) < 0)
312 if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
314 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
316 if ((err = write_blk(filp, prevblk, tmpbuf)) < 0)
319 info->dqi_free_entry = nextblk;
320 lustre_mark_info_dirty(info);
323 dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
324 if (write_blk(filp, blk, buf) < 0) /* No matter whether write succeeds block is out of list */
326 "VFS: Can't write block (%u) with free entries.\n", blk);
333 /* Insert given block to the beginning of list with free entries */
334 static int insert_free_dqentry(struct file *filp,
335 struct lustre_mem_dqinfo *info, dqbuf_t buf,
338 dqbuf_t tmpbuf = getdqbuf();
339 struct lustre_disk_dqdbheader *dh =
340 (struct lustre_disk_dqdbheader *)buf;
345 dh->dqdh_next_free = cpu_to_le32(info->dqi_free_entry);
346 dh->dqdh_prev_free = cpu_to_le32(0);
347 if ((err = write_blk(filp, blk, buf)) < 0)
349 if (info->dqi_free_entry) {
350 if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
352 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
354 if ((err = write_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
358 info->dqi_free_entry = blk;
359 lustre_mark_info_dirty(info);
366 /* Find space for dquot */
367 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err)
369 struct lustre_quota_info *lqi = dquot->dq_info;
370 struct file *filp = lqi->qi_files[dquot->dq_type];
371 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
373 struct lustre_disk_dqdbheader *dh;
374 struct lustre_disk_dqblk *ddquot;
375 struct lustre_disk_dqblk fakedquot;
379 if (!(buf = getdqbuf())) {
383 dh = (struct lustre_disk_dqdbheader *)buf;
384 ddquot = GETENTRIES(buf);
385 if (info->dqi_free_entry) {
386 blk = info->dqi_free_entry;
387 if ((*err = read_blk(filp, blk, buf)) < 0)
390 blk = get_free_dqblk(filp, info);
396 memset(buf, 0, LUSTRE_DQBLKSIZE);
397 info->dqi_free_entry = blk; /* This is enough as block is already zeroed and entry list is empty... */
398 lustre_mark_info_dirty(info);
400 if (le16_to_cpu(dh->dqdh_entries) + 1 >= LUSTRE_DQSTRINBLK) /* Block will be full? */
401 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
403 "VFS: find_free_dqentry(): Can't remove block (%u) from entry free list.\n",
407 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
408 memset(&fakedquot, 0, sizeof(struct lustre_disk_dqblk));
409 /* Find free structure in block */
410 for (i = 0; i < LUSTRE_DQSTRINBLK &&
411 memcmp(&fakedquot, ddquot + i, sizeof(fakedquot)); i++) ;
413 if (i == LUSTRE_DQSTRINBLK) {
415 "VFS: find_free_dqentry(): Data block full but it shouldn't.\n");
420 if ((*err = write_blk(filp, blk, buf)) < 0) {
422 "VFS: find_free_dqentry(): Can't write quota data block %u.\n",
427 (blk << LUSTRE_DQBLKSIZE_BITS) +
428 sizeof(struct lustre_disk_dqdbheader) +
429 i * sizeof(struct lustre_disk_dqblk);
437 /* Insert reference to structure into the trie */
438 static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth)
440 struct lustre_quota_info *lqi = dquot->dq_info;
441 struct file *filp = lqi->qi_files[dquot->dq_type];
442 struct lustre_mem_dqinfo *info = &lqi->qi_info[dquot->dq_type];
444 int ret = 0, newson = 0, newact = 0;
448 if (!(buf = getdqbuf()))
451 ret = get_free_dqblk(filp, info);
455 memset(buf, 0, LUSTRE_DQBLKSIZE);
458 if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
460 "VFS: Can't read tree quota block %u.\n",
466 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
469 if (depth == LUSTRE_DQTREEDEPTH - 1) {
473 "VFS: Inserting already present quota entry (block %u).\n",
474 ref[GETIDINDEX(dquot->dq_id, depth)]);
479 newblk = find_free_dqentry(dquot, &ret);
481 ret = do_insert_tree(dquot, &newblk, depth + 1);
482 if (newson && ret >= 0) {
483 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(newblk);
484 ret = write_blk(filp, *treeblk, buf);
485 } else if (newact && ret < 0)
486 put_free_dqblk(filp, info, buf, *treeblk);
492 /* Wrapper for inserting quota structure into tree */
493 static inline int dq_insert_tree(struct lustre_dquot *dquot)
495 int tmp = LUSTRE_DQTREEOFF;
496 return do_insert_tree(dquot, &tmp, 0);
500 * We don't have to be afraid of deadlocks as we never have quotas on quota files...
502 static int lustre_write_dquot(struct lustre_dquot *dquot)
504 int type = dquot->dq_type;
509 struct lustre_disk_dqblk ddquot, empty;
512 if ((ret = dq_insert_tree(dquot)) < 0) {
514 "VFS: Error %Zd occurred while creating quota.\n",
518 filp = dquot->dq_info->qi_files[type];
519 offset = dquot->dq_off;
520 mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id);
521 /* Argh... We may need to write structure full of zeroes but that would be
522 * treated as an empty place by the rest of the code. Format change would
523 * be definitely cleaner but the problems probably are not worth it */
524 memset(&empty, 0, sizeof(struct lustre_disk_dqblk));
525 if (!memcmp(&empty, &ddquot, sizeof(struct lustre_disk_dqblk)))
526 ddquot.dqb_itime = cpu_to_le64(1);
529 ret = filp->f_op->write(filp, (char *)&ddquot,
530 sizeof(struct lustre_disk_dqblk), &offset);
532 if (ret != sizeof(struct lustre_disk_dqblk)) {
533 CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
534 filp->f_dentry->d_sb->s_id);
543 /* Free dquot entry in data block */
544 static int free_dqentry(struct lustre_dquot *dquot, uint blk)
546 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
547 struct lustre_mem_dqinfo *info =
548 &dquot->dq_info->qi_info[dquot->dq_type];
549 struct lustre_disk_dqdbheader *dh;
550 dqbuf_t buf = getdqbuf();
555 if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
557 "VFS: Quota structure has offset to other block (%u) than it should (%u).\n",
558 blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
561 if ((ret = read_blk(filp, blk, buf)) < 0) {
562 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
565 dh = (struct lustre_disk_dqdbheader *)buf;
566 dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) - 1);
567 if (!le16_to_cpu(dh->dqdh_entries)) { /* Block got free? */
568 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
569 (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
571 "VFS: Can't move quota data block (%u) to free list.\n",
577 (dquot->dq_off & ((1 << LUSTRE_DQBLKSIZE_BITS) - 1)), 0,
578 sizeof(struct lustre_disk_dqblk));
579 if (le16_to_cpu(dh->dqdh_entries) == LUSTRE_DQSTRINBLK - 1) {
580 /* Insert will write block itself */
582 insert_free_dqentry(filp, info, buf, blk)) < 0) {
584 "VFS: Can't insert quota data block (%u) to free entry list.\n",
588 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
590 "VFS: Can't write quota data block %u\n", blk);
594 dquot->dq_off = 0; /* Quota is now unattached */
600 /* Remove reference to dquot from tree */
601 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth)
603 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
604 struct lustre_mem_dqinfo *info =
605 &dquot->dq_info->qi_info[dquot->dq_type];
606 dqbuf_t buf = getdqbuf();
609 u32 *ref = (u32 *) buf;
613 if ((ret = read_blk(filp, *blk, buf)) < 0) {
614 CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", *blk);
617 newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
618 if (depth == LUSTRE_DQTREEDEPTH - 1) {
619 ret = free_dqentry(dquot, newblk);
622 ret = remove_tree(dquot, &newblk, depth + 1);
623 if (ret >= 0 && !newblk) {
625 ref[GETIDINDEX(dquot->dq_id, depth)] = cpu_to_le32(0);
626 for (i = 0; i < LUSTRE_DQBLKSIZE && !buf[i]; i++) ; /* Block got empty? */
627 /* don't put the root block into free blk list! */
628 if (i == LUSTRE_DQBLKSIZE && *blk != LUSTRE_DQTREEOFF) {
629 put_free_dqblk(filp, info, buf, *blk);
631 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
633 "VFS: Can't write quota tree block %u.\n", *blk);
640 /* Delete dquot from tree */
641 static int lustre_delete_dquot(struct lustre_dquot *dquot)
643 uint tmp = LUSTRE_DQTREEOFF;
645 if (!dquot->dq_off) /* Even not allocated? */
647 return remove_tree(dquot, &tmp, 0);
650 /* Find entry in block */
651 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk)
653 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
654 dqbuf_t buf = getdqbuf();
657 struct lustre_disk_dqblk *ddquot = GETENTRIES(buf);
661 if ((ret = read_blk(filp, blk, buf)) < 0) {
662 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
667 i < LUSTRE_DQSTRINBLK
668 && le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id; i++) ;
669 else { /* ID 0 as a bit more complicated searching... */
670 struct lustre_disk_dqblk fakedquot;
672 memset(&fakedquot, 0, sizeof(struct lustre_disk_dqblk));
673 for (i = 0; i < LUSTRE_DQSTRINBLK; i++)
674 if (!le32_to_cpu(ddquot[i].dqb_id)
675 && memcmp(&fakedquot, ddquot + i,
676 sizeof(struct lustre_disk_dqblk)))
679 if (i == LUSTRE_DQSTRINBLK) {
681 "VFS: Quota for id %u referenced but not present.\n",
687 (blk << LUSTRE_DQBLKSIZE_BITS) +
688 sizeof(struct lustre_disk_dqdbheader) +
689 i * sizeof(struct lustre_disk_dqblk);
695 /* Find entry for given id in the tree */
696 static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth)
698 struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
699 dqbuf_t buf = getdqbuf();
701 u32 *ref = (u32 *) buf;
705 if ((ret = read_blk(filp, blk, buf)) < 0) {
706 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
710 blk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
711 if (!blk) /* No reference? */
713 if (depth < LUSTRE_DQTREEDEPTH - 1)
714 ret = find_tree_dqentry(dquot, blk, depth + 1);
716 ret = find_block_dqentry(dquot, blk);
722 /* Find entry for given id in the tree - wrapper function */
723 static inline loff_t find_dqentry(struct lustre_dquot *dquot)
725 return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0);
728 int lustre_read_dquot(struct lustre_dquot *dquot)
730 int type = dquot->dq_type;
734 struct lustre_disk_dqblk ddquot, empty;
737 /* Invalidated quota? */
738 if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
739 CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
743 offset = find_dqentry(dquot);
744 if (offset <= 0) { /* Entry not present? */
747 "VFS: Can't read quota structure for id %u.\n",
750 set_bit(DQ_FAKE_B, &dquot->dq_flags);
751 memset(&dquot->dq_dqb, 0, sizeof(struct mem_dqblk));
754 dquot->dq_off = offset;
757 if ((ret = filp->f_op->read(filp, (char *)&ddquot,
758 sizeof(struct lustre_disk_dqblk),
760 sizeof(struct lustre_disk_dqblk)) {
764 "VFS: Error while reading quota structure for id %u.\n",
766 memset(&ddquot, 0, sizeof(struct lustre_disk_dqblk));
769 /* We need to escape back all-zero structure */
770 memset(&empty, 0, sizeof(struct lustre_disk_dqblk));
771 empty.dqb_itime = cpu_to_le64(1);
772 if (!memcmp(&empty, &ddquot,
773 sizeof(struct lustre_disk_dqblk)))
774 ddquot.dqb_itime = 0;
777 disk2memdqb(&dquot->dq_dqb, &ddquot);
783 /* Commit changes of dquot to disk - it might also mean deleting it when quota became fake */
784 int lustre_commit_dquot(struct lustre_dquot *dquot)
787 /* always clear the flag so we don't loop on an IO error... */
788 clear_bit(DQ_MOD_B, &dquot->dq_flags);
790 /* The block/inode usage in admin quotafile isn't the real usage
791 * over all cluster, so keep the fake dquot entry on disk is
792 * meaningless, just remove it */
793 if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
794 rc = lustre_delete_dquot(dquot);
796 rc = lustre_write_dquot(dquot);
801 if (lustre_info_dirty(&dquot->dq_info->qi_info[dquot->dq_type]))
802 rc = lustre_write_quota_info(dquot->dq_info, dquot->dq_type);
807 /* We need to export this function to initialize quotafile, because we haven't
808 * user level check utility */
809 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
811 struct lustre_mem_dqinfo *dqinfo = &lqi->qi_info[type];
812 struct lustre_disk_dqheader dqhead;
813 struct file *fp = lqi->qi_files[type];
817 static const uint quota_magics[] = LUSTRE_INITQMAGICS;
818 static const uint quota_versions[] = LUSTRE_INITQVERSIONS;
820 /* write quotafile header */
821 dqhead.dqh_magic = cpu_to_le32(quota_magics[type]);
822 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
823 size = fp->f_op->write(fp, (char *)&dqhead,
824 sizeof(struct lustre_disk_dqheader), &offset);
826 if (size != sizeof(struct lustre_disk_dqheader)) {
827 CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
833 /* write init quota info */
834 memset(dqinfo, 0, sizeof(*dqinfo));
835 dqinfo->dqi_bgrace = MAX_DQ_TIME;
836 dqinfo->dqi_igrace = MAX_IQ_TIME;
837 dqinfo->dqi_blocks = LUSTRE_DQTREEOFF + 1;
839 return lustre_write_quota_info(lqi, type);
843 struct list_head link;
847 static ssize_t quota_read(struct file *file, struct inode *inode, int type,
848 uint blk, dqbuf_t buf)
851 return read_blk(file, blk, buf);
853 #ifndef KERNEL_SUPPORTS_QUOTA_READ
856 struct super_block *sb = inode->i_sb;
857 memset(buf, 0, LUSTRE_DQBLKSIZE);
858 return sb->s_op->quota_read(sb, type, (char *)buf,
860 blk << LUSTRE_DQBLKSIZE_BITS);
865 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
866 uint blk, struct list_head *list)
868 dqbuf_t buf = getdqbuf();
870 struct lustre_disk_dqdbheader *dqhead =
871 (struct lustre_disk_dqdbheader *)buf;
872 struct dqblk *blk_item;
874 struct list_head *tmp;
878 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
879 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
884 if (!le32_to_cpu(dqhead->dqdh_entries))
887 if (list_empty(list)) {
892 list_for_each_entry(pos, list, link) {
893 if (blk == pos->blk) /* we got this blk already */
901 blk_item = kmalloc(sizeof(*blk_item), GFP_NOFS);
907 INIT_LIST_HEAD(&blk_item->link);
909 list_add_tail(&blk_item->link, tmp);
916 static int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
917 uint blk, int depth, struct list_head *list)
919 dqbuf_t buf = getdqbuf();
922 u32 *ref = (u32 *) buf;
926 if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
927 CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
932 for (index = 0; index <= 0xff && !ret; index++) {
933 blk = le32_to_cpu(ref[index]);
934 if (!blk) /* No reference */
937 if (depth < LUSTRE_DQTREEDEPTH - 1)
938 ret = walk_tree_dqentry(filp, inode, type, blk,
941 ret = walk_block_dqentry(filp, inode, type, blk, list);
948 /* Walk through the quota file (v2 format) to get all ids with quota limit */
949 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
950 struct list_head *list)
952 struct list_head blk_list;
953 struct dqblk *blk_item, *tmp;
955 struct lustre_disk_dqblk *ddquot;
958 if (!check_quota_file(fp, inode, type)) {
959 CDEBUG(D_ERROR, "unknown quota file format!\n");
962 if (!list_empty(list)) {
963 CDEBUG(D_ERROR, "not empty list\n");
967 INIT_LIST_HEAD(&blk_list);
968 rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
970 CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
973 if (list_empty(&blk_list))
979 ddquot = GETENTRIES(buf);
981 list_for_each_entry(blk_item, &blk_list, link) {
984 struct lustre_disk_dqblk fakedquot;
986 memset(buf, 0, LUSTRE_DQBLKSIZE);
987 if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
989 "VFS: Can't read quota tree block %u.\n",
995 memset(&fakedquot, 0, sizeof(struct lustre_disk_dqblk));
996 for (i = 0; i < LUSTRE_DQSTRINBLK; i++) {
997 struct dquot_id *dqid;
998 /* skip empty entry */
1000 (&fakedquot, ddquot + i,
1001 sizeof(struct lustre_disk_dqblk)))
1004 dqid = kmalloc(sizeof(*dqid), GFP_NOFS);
1009 dqid->di_id = le32_to_cpu(ddquot[i].dqb_id);
1010 INIT_LIST_HEAD(&dqid->di_link);
1011 list_add(&dqid->di_link, list);
1016 list_for_each_entry_safe(blk_item, tmp, &blk_list, link) {
1017 list_del_init(&blk_item->link);
1025 EXPORT_SYMBOL(lustre_check_quota_file);
1026 EXPORT_SYMBOL(lustre_read_quota_info);
1027 EXPORT_SYMBOL(lustre_write_quota_info);
1028 EXPORT_SYMBOL(lustre_read_dquot);
1029 EXPORT_SYMBOL(lustre_commit_dquot);
1030 EXPORT_SYMBOL(lustre_init_quota_info);
1031 EXPORT_SYMBOL(lustre_get_qids);