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 * Author: Johann Lombardi <johann@whamcloud.com>
28 * Author: Niu Yawei <niu@whamcloud.com>
32 #include "osd_internal.h"
35 * Helpers function to find out the quota type (USRQUOTA/GRPQUOTA) of a
38 static inline int fid2type(const struct lu_fid *fid)
40 LASSERT(fid_is_acct(fid));
41 if (fid_oid(fid) == ACCT_GROUP_OID)
46 static inline int obj2type(struct dt_object *obj)
48 return fid2type(lu_object_fid(&obj->do_lu));
52 * Space Accounting Management
56 * Look up an accounting object based on its fid.
58 * \param info - is the osd thread info passed by the caller
59 * \param osd - is the osd device
60 * \param fid - is the fid of the accounting object we want to look up
61 * \param id - is the osd_inode_id struct to fill with the inode number of
62 * the quota file if the lookup is successful
64 int osd_acct_obj_lookup(struct osd_thread_info *info, struct osd_device *osd,
65 const struct lu_fid *fid, struct osd_inode_id *id)
67 struct super_block *sb = osd_sb(osd);
70 LASSERT(fid_is_acct(fid));
72 if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
73 LDISKFS_FEATURE_RO_COMPAT_QUOTA))
76 id->oii_gen = OSD_OII_NOGEN;
77 id->oii_ino = LDISKFS_SB(sb)->s_qf_inums[fid2type(fid)];
78 if (!ldiskfs_valid_inum(sb, id->oii_ino))
84 * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
86 * \param env - is the environment passed by the caller
87 * \param dtobj - is the accounting object
88 * \param dtrec - is the record to fill with space usage information
89 * \param dtkey - is the id the of the user or group for which we would
90 * like to access disk usage.
91 * \param capa - is the capability, not used.
93 * \retval +ve - success : exact match
94 * \retval -ve - failure
96 static int osd_acct_index_lookup(const struct lu_env *env,
97 struct dt_object *dtobj,
99 const struct dt_key *dtkey,
100 struct lustre_capa *capa)
102 struct osd_thread_info *info = osd_oti_get(env);
103 struct if_dqblk *dqblk = &info->oti_dqblk;
104 struct super_block *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
105 struct acct_rec *rec = (struct acct_rec *)dtrec;
106 __u64 id = *((__u64 *)dtkey);
111 memset((void *)dqblk, 0, sizeof(struct obd_dqblk));
112 rc = sb->s_qcop->get_dqblk(sb, obj2type(dtobj), (qid_t) id, dqblk);
115 rec->bspace = dqblk->dqb_curspace;
116 rec->ispace = dqblk->dqb_curinodes;
120 #define QUOTA_IT_READ_ERROR(it, rc) \
121 CERROR("%s: Error while trying to read quota information, " \
122 "failed with %d\n", \
123 it->oiq_obj->oo_dt.do_lu.lo_dev->ld_obd->obd_name, rc); \
126 * Initialize osd Iterator for given osd index object.
128 * \param dt - osd index object
129 * \param attr - not used
130 * \param capa - BYPASS_CAPA
132 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
133 struct dt_object *dt,
134 __u32 attr, struct lustre_capa *capa)
136 struct osd_thread_info *info = osd_oti_get(env);
137 struct osd_it_quota *it;
138 struct lu_object *lo = &dt->do_lu;
139 struct osd_object *obj = osd_dt_obj(dt);
143 LASSERT(lu_object_exists(lo));
146 RETURN(ERR_PTR(-ENOMEM));
148 it = &info->oti_it_quota;
149 memset(it, 0, sizeof(*it));
153 /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
154 it->oiq_blk[0] = LUSTRE_DQTREEOFF;
156 /* NB: we don't need to store the tree depth since it is always
157 * equal to LUSTRE_DQTREEDEPTH - 1 (root has depth = 0) for a leaf
159 RETURN((struct dt_it *)it);
163 * Free given iterator.
165 * \param di - osd iterator
167 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
169 struct osd_it_quota *it = (struct osd_it_quota *)di;
172 lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
177 * Move Iterator to record specified by \a key, if the \a key isn't found,
178 * move to the first valid record.
180 * \param di - osd iterator
181 * \param key - uid or gid
183 * \retval +ve - di points to the first valid record
184 * \retval +1 - di points to exact matched key
185 * \retval -ve - failure
187 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
188 const struct dt_key *key)
190 struct osd_it_quota *it = (struct osd_it_quota *)di;
191 const struct lu_fid *fid =
192 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
193 int type = fid2type(fid);
194 qid_t dqid = *(qid_t *)key;
200 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
201 LUSTRE_DQTREEOFF, 0, it);
202 if (offset > 0) { /* Found */
204 } else if (offset < 0) { /* Error */
205 QUOTA_IT_READ_ERROR(it, (int)offset);
209 /* The @key is not found, move to the first valid entry */
210 rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
223 * \param di - osd iterator
225 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
231 * Move on to the next valid entry.
233 * \param di - osd iterator
235 * \retval +ve - iterator reached the end
236 * \retval 0 - iterator has not reached the end yet
237 * \retval -ve - unexpected failure
239 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
241 struct osd_it_quota *it = (struct osd_it_quota *)di;
242 const struct lu_fid *fid =
243 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
244 int type = fid2type(fid);
250 /* Let's first check if there are any remaining valid entry in the
251 * current leaf block. Start with the next entry after the current one.
253 depth = LUSTRE_DQTREEDEPTH - 1;
254 index = GETIDINDEX(it->oiq_id, depth);
255 if (++index < LUSTRE_DQSTRINBLK) {
256 /* Search for the next valid entry from current index */
257 rc = walk_block_dqentry(env, it->oiq_obj, type,
258 it->oiq_blk[depth], index, it);
260 QUOTA_IT_READ_ERROR(it, rc);
262 } else if (rc == 0) {
263 /* Found on entry, @it is already updated to the
264 * new position in walk_block_dqentry(). */
270 /* We have consumed all the entries of the current leaf block, move on
271 * to the next one. */
274 /* We keep searching as long as walk_tree_dqentry() returns +1
275 * (= no valid entry found). */
276 for (; depth >= 0 && rc > 0; depth--) {
277 index = GETIDINDEX(it->oiq_id, depth);
280 rc = walk_tree_dqentry(env, it->oiq_obj, type,
281 it->oiq_blk[depth], depth, index, it);
285 QUOTA_IT_READ_ERROR(it, rc);
290 * Return pointer to the key under iterator.
292 * \param di - osd iterator
294 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
295 const struct dt_it *di)
297 struct osd_it_quota *it = (struct osd_it_quota *)di;
300 RETURN((struct dt_key *)&it->oiq_id);
304 * Return size of key under iterator (in bytes)
306 * \param di - osd iterator
308 static int osd_it_acct_key_size(const struct lu_env *env,
309 const struct dt_it *di)
311 struct osd_it_quota *it = (struct osd_it_quota *)di;
314 RETURN((int)sizeof(it->oiq_id));
318 * Return pointer to the record under iterator.
320 * \param di - osd iterator
321 * \param attr - not used
323 static int osd_it_acct_rec(const struct lu_env *env,
324 const struct dt_it *di,
325 struct dt_rec *dtrec, __u32 attr)
327 struct osd_it_quota *it = (struct osd_it_quota *)di;
328 const struct dt_key *key = osd_it_acct_key(env, di);
333 rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key,
335 RETURN(rc > 0 ? 0 : rc);
339 * Returns cookie for current Iterator position.
341 * \param di - osd iterator
343 static __u64 osd_it_acct_store(const struct lu_env *env,
344 const struct dt_it *di)
346 struct osd_it_quota *it = (struct osd_it_quota *)di;
353 * Restore iterator from cookie. if the \a hash isn't found,
354 * restore the first valid record.
356 * \param di - osd iterator
357 * \param hash - iterator location cookie
359 * \retval +ve - di points to the first valid record
360 * \retval +1 - di points to exact matched hash
361 * \retval -ve - failure
363 static int osd_it_acct_load(const struct lu_env *env,
364 const struct dt_it *di, __u64 hash)
367 RETURN(osd_it_acct_get(env, (struct dt_it *)di,
368 (const struct dt_key *)&hash));
372 * Index and Iterator operations for accounting objects
374 const struct dt_index_operations osd_acct_index_ops = {
375 .dio_lookup = osd_acct_index_lookup,
377 .init = osd_it_acct_init,
378 .fini = osd_it_acct_fini,
379 .get = osd_it_acct_get,
380 .put = osd_it_acct_put,
381 .next = osd_it_acct_next,
382 .key = osd_it_acct_key,
383 .key_size = osd_it_acct_key_size,
384 .rec = osd_it_acct_rec,
385 .store = osd_it_acct_store,
386 .load = osd_it_acct_load