4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2011, 2012, Whamcloud, Inc.
25 * Use is subject to license terms.
27 * Lustre administrative quota format.
28 * from linux/fs/quota_v2.c
31 #include "osd_internal.h"
32 #include "osd_quota_fmt.h"
34 typedef char *dqbuf_t;
38 struct lustre_disk_dqblk_v2 r1;
39 } emptydquot = { .r1 = { 0 } };
41 static inline dqbuf_t getdqbuf(void)
43 dqbuf_t buf = cfs_alloc(LUSTRE_DQBLKSIZE, CFS_ALLOC_IO);
45 CWARN("Not enough memory for quota buffers.\n");
49 static inline void freedqbuf(dqbuf_t buf)
55 * Read the \a blk into \a buf.
57 * TODO Will support enforcement quota later.
59 static ssize_t quota_read_blk(const struct lu_env *env,
60 struct osd_object *obj,
61 int type, uint blk, dqbuf_t buf)
64 struct super_block *sb = obj->oo_inode->i_sb;
68 memset(buf, 0, LUSTRE_DQBLKSIZE);
69 ret = sb->s_op->quota_read(sb, type, buf, LUSTRE_DQBLKSIZE,
70 blk << LUSTRE_DQBLKSIZE_BITS);
72 /* Reading past EOF just returns a block of zeros */
80 * Find entry in block by given \a dqid in the leaf block \a blk
82 * \retval +ve, the offset of the entry in file
83 * \retval 0, entry not found
84 * \retval -ve, unexpected failure
86 static loff_t find_block_dqentry(const struct lu_env *env,
87 struct osd_object *obj, int type,
89 struct osd_it_quota *it)
91 dqbuf_t buf = getdqbuf();
94 struct lustre_disk_dqblk_v2 *ddquot;
99 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf);
100 dqblk_sz = sizeof(struct lustre_disk_dqblk_v2);
103 ret = quota_read_blk(env, obj, type, blk, buf);
105 CERROR("Can't read quota tree block %u.\n", blk);
110 for (i = 0; i < LUSTRE_DQSTRINBLK &&
111 le32_to_cpu(ddquot[i].dqb_id) != dqid; i++)
113 } else { /* ID 0 as a bit more complicated searching... */
114 for (i = 0; i < LUSTRE_DQSTRINBLK; i++)
115 if (!le32_to_cpu(ddquot[i].dqb_id) &&
116 memcmp((char *)&emptydquot, (char *)&ddquot[i],
120 if (i == LUSTRE_DQSTRINBLK) {
121 CDEBUG(D_QUOTA, "Quota for id %u not found.\n", dqid);
125 ret = (blk << LUSTRE_DQBLKSIZE_BITS) +
126 sizeof(struct lustre_disk_dqdbheader) + i * dqblk_sz;
129 it->oiq_blk[LUSTRE_DQTREEDEPTH - 1] = blk;
130 it->oiq_offset = ret;
142 * Find entry for given \a dqid in the tree block \a blk
144 * \retval +ve, the offset of the entry in file
145 * \retval 0, entry not found
146 * \retval -ve, unexpected failure
148 loff_t find_tree_dqentry(const struct lu_env *env,
149 struct osd_object *obj, int type,
150 qid_t dqid, uint blk, int depth,
151 struct osd_it_quota *it)
153 dqbuf_t buf = getdqbuf();
155 u32 *ref = (u32 *) buf;
161 ret = quota_read_blk(env, obj, 0, blk, buf);
163 CERROR("Can't read quota tree block %u.\n", blk);
167 blk = le32_to_cpu(ref[GETIDINDEX(dqid, depth)]);
168 if (!blk) /* No reference? */
171 if (depth < LUSTRE_DQTREEDEPTH - 1)
172 ret = find_tree_dqentry(env, obj, type, dqid, blk,
175 ret = find_block_dqentry(env, obj, type, dqid, blk, it);
177 if (it && ret > 0) /* Entry found */
178 it->oiq_blk[depth] = blk;
185 * Search from \a index within the leaf block \a blk, and fill the \a it with
186 * the first valid entry.
188 * \retval +ve, no valid entry found
189 * \retval 0, entry found
190 * \retval -ve, unexpected failure
192 int walk_block_dqentry(const struct lu_env *env, struct osd_object *obj,
193 int type, uint blk, uint index,
194 struct osd_it_quota *it)
196 dqbuf_t buf = getdqbuf();
198 struct lustre_disk_dqdbheader *dqhead;
200 struct lustre_disk_dqblk_v2 *ddquot;
204 dqhead = (struct lustre_disk_dqdbheader *)buf;
205 dqblk_sz = sizeof(struct lustre_disk_dqblk_v2);
208 ret = quota_read_blk(env, obj, type, blk, buf);
210 CERROR("Can't read quota tree block %u.\n", blk);
215 if (!le32_to_cpu(dqhead->dqdh_entries))
218 ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf);
219 LASSERT(index < LUSTRE_DQSTRINBLK);
220 for (i = index; i < LUSTRE_DQSTRINBLK; i++) {
221 /* skip empty entry */
222 if (!memcmp((char *)&emptydquot,
223 (char *)&ddquot[i], dqblk_sz))
226 it->oiq_blk[LUSTRE_DQTREEDEPTH - 1] = blk;
227 it->oiq_id = le32_to_cpu(ddquot[i].dqb_id);
228 it->oiq_offset = (blk << LUSTRE_DQBLKSIZE_BITS) +
229 sizeof(struct lustre_disk_dqdbheader) +
241 * Search from \a index within the tree block \a blk, and fill the \a it
242 * with the first valid entry.
244 * \retval +ve, no valid entry found
245 * \retval 0, entry found
246 * \retval -ve, unexpected failure
248 int walk_tree_dqentry(const struct lu_env *env, struct osd_object *obj,
249 int type, uint blk, int depth, uint index,
250 struct osd_it_quota *it)
252 dqbuf_t buf = getdqbuf();
254 u32 *ref = (u32 *) buf;
260 ret = quota_read_blk(env, obj, type, blk, buf);
262 CERROR("Can't read quota tree block %u.\n", blk);
267 for (; index <= 0xff && ret > 0; index++) {
268 blk = le32_to_cpu(ref[index]);
269 if (!blk) /* No reference */
272 if (depth < LUSTRE_DQTREEDEPTH - 1)
273 ret = walk_tree_dqentry(env, obj, type, blk,
276 ret = walk_block_dqentry(env, obj, type, blk, 0, it);
279 if (ret == 0) /* Entry found */
280 it->oiq_blk[depth] = blk;