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) 2012, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Johann Lombardi <johann@whamcloud.com>
30 #include <lustre_quota.h>
32 #include "osd_internal.h"
35 * Helper function to retrieve DMU object id from fid for accounting object
37 uint64_t osd_quota_fid2dmu(const struct lu_fid *fid)
39 LASSERT(fid_is_acct(fid));
40 if (fid_oid(fid) == ACCT_GROUP_OID)
41 return DMU_GROUPUSED_OBJECT;
42 return DMU_USERUSED_OBJECT;
46 * Space Accounting Management
50 * Return space usage consumed by a given uid or gid.
51 * Block usage is accurrate since it is maintained by DMU itself.
52 * However, DMU does not provide inode accounting, so the #inodes in use
53 * is estimated from the block usage and statfs information.
55 * \param env - is the environment passed by the caller
56 * \param dtobj - is the accounting object
57 * \param dtrec - is the record to fill with space usage information
58 * \param dtkey - is the id the of the user or group for which we would
59 * like to access disk usage.
60 * \param capa - is the capability, not used.
62 * \retval +ve - success : exact match
63 * \retval -ve - failure
65 static int osd_acct_index_lookup(const struct lu_env *env,
66 struct dt_object *dtobj,
68 const struct dt_key *dtkey,
69 struct lustre_capa *capa)
71 struct osd_thread_info *info = osd_oti_get(env);
72 char *buf = info->oti_buf;
73 struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
74 struct osd_object *obj = osd_dt_obj(dtobj);
75 struct osd_device *osd = osd_obj2dev(obj);
80 rec->bspace = rec->ispace = 0;
82 /* convert the 64-bit uid/gid into a string */
83 sprintf(buf, "%llx", *((__u64 *)dtkey));
84 /* fetch DMU object ID (DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT) to be
86 oid = osd_quota_fid2dmu(lu_object_fid(&dtobj->do_lu));
88 /* disk usage (in bytes) is maintained by DMU.
89 * DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT are special objects which
90 * not associated with any dmu_but_t (see dnode_special_open()).
91 * As a consequence, we cannot use udmu_zap_lookup() here since it
92 * requires a valid oo_db. */
93 rc = -zap_lookup(osd->od_objset.os, oid, buf, sizeof(uint64_t), 1,
96 /* user/group has not created anything yet */
97 CDEBUG(D_QUOTA, "%s: id %s not found in DMU accounting ZAP\n",
102 if (osd->od_quota_iused_est) {
103 if (rec->bspace != 0)
104 /* estimate #inodes in use */
105 rec->ispace = udmu_objset_user_iused(&osd->od_objset,
110 /* as for inode accounting, it is not maintained by DMU, so we just
111 * use our own ZAP to track inode usage */
112 rc = -zap_lookup(osd->od_objset.os, obj->oo_db->db_object,
113 buf, sizeof(uint64_t), 1, &rec->ispace);
115 /* user/group has not created any file yet */
116 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
117 osd->od_svname, buf);
125 * Initialize osd Iterator for given osd index object.
127 * \param dt - osd index object
128 * \param attr - not used
129 * \param capa - BYPASS_CAPA
131 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
132 struct dt_object *dt,
134 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_device *osd = osd_dev(lo->lo_dev);
143 LASSERT(lu_object_exists(lo));
146 RETURN(ERR_PTR(-ENOMEM));
148 it = &info->oti_it_quota;
149 memset(it, 0, sizeof(*it));
150 it->oiq_oid = osd_quota_fid2dmu(lu_object_fid(lo));
152 /* initialize zap cursor */
153 rc = -udmu_zap_cursor_init(&it->oiq_zc, &osd->od_objset, it->oiq_oid,0);
157 /* take object reference */
159 it->oiq_obj = osd_dt_obj(dt);
162 RETURN((struct dt_it *)it);
166 * Free given iterator.
168 * \param di - osd iterator
170 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
172 struct osd_it_quota *it = (struct osd_it_quota *)di;
174 udmu_zap_cursor_fini(it->oiq_zc);
175 lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
180 * Move on to the next valid entry.
182 * \param di - osd iterator
184 * \retval +ve - iterator reached the end
185 * \retval 0 - iterator has not reached the end yet
186 * \retval -ve - unexpected failure
188 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
190 struct osd_it_quota *it = (struct osd_it_quota *)di;
194 if (it->oiq_reset == 0)
195 zap_cursor_advance(it->oiq_zc);
197 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, NULL, 32);
198 if (rc == -ENOENT) /* reached the end */
204 * Return pointer to the key under iterator.
206 * \param di - osd iterator
208 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
209 const struct dt_it *di)
211 struct osd_it_quota *it = (struct osd_it_quota *)di;
212 struct osd_thread_info *info = osd_oti_get(env);
213 char *buf = info->oti_buf;
219 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, buf, 32);
222 it->oiq_id = simple_strtoull(buf, &p, 16);
223 RETURN((struct dt_key *) &it->oiq_id);
227 * Return size of key under iterator (in bytes)
229 * \param di - osd iterator
231 static int osd_it_acct_key_size(const struct lu_env *env,
232 const struct dt_it *di)
235 RETURN((int)sizeof(uint64_t));
239 * Return pointer to the record under iterator.
241 * \param di - osd iterator
242 * \param attr - not used
244 static int osd_it_acct_rec(const struct lu_env *env,
245 const struct dt_it *di,
246 struct dt_rec *dtrec, __u32 attr)
248 struct osd_thread_info *info = osd_oti_get(env);
249 char *buf = info->oti_buf;
250 struct osd_it_quota *it = (struct osd_it_quota *)di;
251 struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
252 struct osd_object *obj = it->oiq_obj;
253 struct osd_device *osd = osd_obj2dev(obj);
259 rec->ispace = rec->bspace = 0;
261 /* retrieve block usage from the DMU accounting object */
262 rc = -udmu_zap_cursor_retrieve_value(env, it->oiq_zc,
263 (char *)&rec->bspace,
264 sizeof(uint64_t), &bytes_read);
268 if (osd->od_quota_iused_est) {
269 if (rec->bspace != 0)
270 /* estimate #inodes in use */
271 rec->ispace = udmu_objset_user_iused(&osd->od_objset,
276 /* retrieve key associated with the current cursor */
277 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, buf, 32);
281 /* inode accounting is not maintained by DMU, so we use our own ZAP to
282 * track inode usage */
283 rc = -zap_lookup(osd->od_objset.os, it->oiq_obj->oo_db->db_object,
284 buf, sizeof(uint64_t), 1, &rec->ispace);
286 /* user/group has not created any file yet */
287 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
288 osd->od_svname, buf);
296 * Returns cookie for current Iterator position.
298 * \param di - osd iterator
300 static __u64 osd_it_acct_store(const struct lu_env *env,
301 const struct dt_it *di)
303 struct osd_it_quota *it = (struct osd_it_quota *)di;
306 RETURN(udmu_zap_cursor_serialize(it->oiq_zc));
310 * Restore iterator from cookie. if the \a hash isn't found,
311 * restore the first valid record.
313 * \param di - osd iterator
314 * \param hash - iterator location cookie
316 * \retval +ve - di points to exact matched key
317 * \retval 0 - di points to the first valid record
318 * \retval -ve - failure
320 static int osd_it_acct_load(const struct lu_env *env,
321 const struct dt_it *di, __u64 hash)
323 struct osd_it_quota *it = (struct osd_it_quota *)di;
324 struct osd_device *osd = osd_obj2dev(it->oiq_obj);
329 /* create new cursor pointing to the new hash */
330 rc = -udmu_zap_cursor_init(&zc, &osd->od_objset, it->oiq_oid, hash);
333 udmu_zap_cursor_fini(it->oiq_zc);
337 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, NULL, 32);
340 else if (rc == -ENOENT)
346 * Move Iterator to record specified by \a key, if the \a key isn't found,
347 * move to the first valid record.
349 * \param di - osd iterator
350 * \param key - uid or gid
352 * \retval +ve - di points to exact matched key
353 * \retval 0 - di points to the first valid record
354 * \retval -ve - failure
356 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
357 const struct dt_key *key)
361 /* XXX: like osd_zap_it_get(), API is currently broken */
362 LASSERT(*((__u64 *)key) == 0);
364 RETURN(osd_it_acct_load(env, di, 0));
370 * \param di - osd iterator
372 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
377 * Index and Iterator operations for accounting objects
379 const struct dt_index_operations osd_acct_index_ops = {
380 .dio_lookup = osd_acct_index_lookup,
382 .init = osd_it_acct_init,
383 .fini = osd_it_acct_fini,
384 .get = osd_it_acct_get,
385 .put = osd_it_acct_put,
386 .next = osd_it_acct_next,
387 .key = osd_it_acct_key,
388 .key_size = osd_it_acct_key_size,
389 .rec = osd_it_acct_rec,
390 .store = osd_it_acct_store,
391 .load = osd_it_acct_load
396 * Quota Enforcement Management
400 * Wrapper for qsd_op_begin().
402 * \param env - the environment passed by the caller
403 * \param osd - is the osd_device
404 * \param uid - user id of the inode
405 * \param gid - group id of the inode
406 * \param space - how many blocks/inodes will be consumed/released
407 * \param oh - osd transaction handle
408 * \param is_blk - block quota or inode quota?
409 * \param flags - if the operation is write, return no user quota, no
410 * group quota, or sync commit flags to the caller
411 * \param force - set to 1 when changes are performed by root user and thus
412 * can't failed with EDQUOT
414 * \retval 0 - success
415 * \retval -ve - failure
417 int osd_declare_quota(const struct lu_env *env, struct osd_device *osd,
418 qid_t uid, qid_t gid, long long space,
419 struct osd_thandle *oh, bool is_blk, int *flags,
422 struct osd_thread_info *info = osd_oti_get(env);
423 struct lquota_id_info *qi = &info->oti_qi;
424 struct qsd_instance *qsd = osd->od_quota_slave;
425 int rcu, rcg; /* user & group rc */
428 if (unlikely(qsd == NULL))
429 /* quota slave instance hasn't been allocated yet */
432 /* let's start with user quota */
433 qi->lqi_id.qid_uid = uid;
434 qi->lqi_type = USRQUOTA;
435 qi->lqi_space = space;
436 qi->lqi_is_blk = is_blk;
437 rcu = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
439 if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
440 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
443 /* For non-fatal error, we want to continue to get the noquota flags
444 * for group id. This is only for commit write, which has @flags passed
445 * in. See osd_declare_write_commit().
446 * When force is set to true, we also want to proceed with the gid */
447 if (rcu && (rcu != -EDQUOT || flags == NULL))
450 /* and now group quota */
451 qi->lqi_id.qid_gid = gid;
452 qi->lqi_type = GRPQUOTA;
453 rcg = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
455 if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
456 /* as before, ignore EDQUOT & EINPROGRESS for root */
459 RETURN(rcu ? rcu : rcg);