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"
34 #include <sys/dnode.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/dsl_prop.h>
42 * the structure tracks per-ID change/state
44 struct zfs_id_change {
45 struct hlist_node zic_hash;
51 * callback data for cfs_hash_for_each_safe()
52 * used in txg commit and OSD cleanup path
55 struct osd_device *hcb_osd;
61 * Helper function to retrieve DMU object id from fid for accounting object
63 static inline uint64_t osd_quota_fid2dmu(const struct lu_fid *fid)
65 LASSERT(fid_is_acct(fid));
66 if (fid_oid(fid) == ACCT_GROUP_OID)
67 return DMU_GROUPUSED_OBJECT;
68 return DMU_USERUSED_OBJECT;
72 * a note about locking:
73 * entries in per-OSD cache never go before umount,
74 * so there is no need in locking for lookups.
76 * entries in per-txg deltas never go before txg is closed,
77 * there is no concurrency between removal/insertions.
79 * also, given all above, there is no need in reference counting.
81 static struct zfs_id_change *osd_zfs_lookup_by_id(cfs_hash_t *hash, __u64 id)
83 struct zfs_id_change *za = NULL;
84 struct hlist_node *hnode;
87 cfs_hash_bd_get(hash, &id, &bd);
88 hnode = cfs_hash_bd_peek_locked(hash, &bd, &id);
90 za = container_of0(hnode, struct zfs_id_change, zic_hash);
95 static struct zfs_id_change *lookup_or_create_by_id(struct osd_device *osd,
96 cfs_hash_t *hash, __u64 id)
98 struct zfs_id_change *za, *tmp;
99 struct hlist_node *hnode;
102 za = osd_zfs_lookup_by_id(hash, id);
103 if (likely(za != NULL))
107 if (unlikely(za == NULL))
112 cfs_hash_bd_get(hash, &id, &bd);
113 spin_lock(&osd->od_known_txg_lock);
114 hnode = cfs_hash_bd_findadd_locked(hash, &bd, &id, &za->zic_hash, 1);
115 LASSERT(hnode != NULL);
116 tmp = container_of0(hnode, struct zfs_id_change, zic_hash);
117 spin_unlock(&osd->od_known_txg_lock);
121 * our structure got into the hash
124 /* somebody won the race, we wasted the cycles */
132 * used to maintain per-txg deltas
134 static int osd_zfs_acct_id(const struct lu_env *env, cfs_hash_t *hash,
135 __u64 id, int delta, struct osd_thandle *oh)
137 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
138 struct zfs_id_change *za;
142 LASSERT(oh->ot_tx->tx_txg == osd->od_known_txg);
143 LASSERT(osd->od_acct_delta != NULL);
145 za = lookup_or_create_by_id(osd, hash, id);
146 if (unlikely(za == NULL))
149 atomic_add(delta, &za->zic_num);
155 * this function is used to maintain current state for given ID:
156 * at the beginning it initializes the cache from correspoding ZAP
158 static void osd_zfs_acct_cache_init(const struct lu_env *env,
159 struct osd_device *osd,
160 cfs_hash_t *hash, __u64 oid,
162 struct osd_thandle *oh)
164 char *buf = osd_oti_get(env)->oti_buf;
165 struct hlist_node *hnode;
167 struct zfs_id_change *za, *tmp;
171 za = osd_zfs_lookup_by_id(hash, id);
172 if (likely(za != NULL))
176 * any concurrent thread is running in the same txg, so no on-disk
177 * accounting ZAP can be modified until this txg is closed
178 * thus all the concurrent threads must be getting the same value
179 * from that ZAP and we don't need to serialize lookups
181 snprintf(buf, sizeof(osd_oti_get(env)->oti_buf), "%llx", id);
182 /* XXX: we should be using zap_lookup_int_key(), but it consumes
183 * 20 bytes on the stack for buf .. */
184 rc = -zap_lookup(osd->od_objset.os, oid, buf, sizeof(uint64_t), 1, &v);
187 } else if (unlikely(rc != 0)) {
188 CERROR("%s: can't access accounting zap %llu\n",
189 osd->od_svname, oid);
194 if (unlikely(za == NULL)) {
195 CERROR("%s: can't allocate za\n", osd->od_svname);
200 atomic_set(&za->zic_num, v);
202 cfs_hash_bd_get(hash, &id, &bd);
203 spin_lock(&osd->od_known_txg_lock);
204 hnode = cfs_hash_bd_findadd_locked(hash, &bd, &id, &za->zic_hash, 1);
205 LASSERT(hnode != NULL);
206 tmp = container_of0(hnode, struct zfs_id_change, zic_hash);
207 spin_unlock(&osd->od_known_txg_lock);
210 /* our structure got into the hash */
212 /* there was no entry in ZAP yet, we have
213 * to initialize with 0, so that accounting
214 * reports can find that and then find our
217 rc = -zap_update(osd->od_objset.os, oid, buf,
218 sizeof(uint64_t), 1, &v, oh->ot_tx);
219 if (unlikely(rc != 0))
220 CERROR("%s: can't initialize: rc = %d\n",
224 /* somebody won the race, we wasted the cycles */
231 atomic_add(delta, &za->zic_num);
234 static __u32 acct_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
236 const __u64 *id = key;
239 result = (__u32) *id;
240 return result % mask;
243 static void *acct_key(struct hlist_node *hnode)
245 struct zfs_id_change *ac;
247 ac = hlist_entry(hnode, struct zfs_id_change, zic_hash);
251 static int acct_hashkey_keycmp(const void *key,
252 struct hlist_node *compared_hnode)
254 struct zfs_id_change *ac;
255 const __u64 *id = key;
257 ac = hlist_entry(compared_hnode, struct zfs_id_change, zic_hash);
258 return *id == ac->zic_id;
261 static void *acct_hashobject(struct hlist_node *hnode)
263 return hlist_entry(hnode, struct zfs_id_change, zic_hash);
266 static cfs_hash_ops_t acct_hash_operations = {
267 .hs_hash = acct_hashfn,
269 .hs_keycmp = acct_hashkey_keycmp,
270 .hs_object = acct_hashobject,
273 #define ACCT_HASH_OPS (CFS_HASH_NO_LOCK|CFS_HASH_NO_ITEMREF|CFS_HASH_ADD_TAIL)
275 int osd_zfs_acct_init(const struct lu_env *env, struct osd_device *o)
280 spin_lock_init(&o->od_known_txg_lock);
282 /* global structure representing current state for given ID */
283 o->od_acct_usr = cfs_hash_create("usr", 4, 4, 4, 0, 0, 0,
284 &acct_hash_operations,
286 if (o->od_acct_usr == NULL)
287 GOTO(out, rc = -ENOMEM);
289 o->od_acct_grp = cfs_hash_create("grp", 4, 4, 4, 0, 0, 0,
290 &acct_hash_operations,
292 if (o->od_acct_grp == NULL)
293 GOTO(out, rc = -ENOMEM);
299 static int osd_zfs_delete_item(cfs_hash_t *hs, cfs_hash_bd_t *bd,
300 struct hlist_node *node, void *data)
302 struct hash_cbdata *d = data;
303 struct zfs_id_change *za;
308 za = hlist_entry(node, struct zfs_id_change, zic_hash);
311 * XXX: should we try to fix accounting we failed to update before?
313 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 5, 70, 0)
315 * extra checks to ensure our cache matches on-disk state
317 snprintf(buf, sizeof(buf), "%llx", za->zic_id);
318 rc = -zap_lookup(d->hcb_osd->od_objset.os, d->hcb_zapid,
319 buf, sizeof(uint64_t), 1, &v);
320 /* pairs with zero value are removed by ZAP automatically */
323 if (atomic_read(&za->zic_num) != v) {
324 CERROR("%s: INVALID ACCOUNTING FOR %llu %d != %lld: rc = %d\n",
325 d->hcb_osd->od_svname, za->zic_id,
326 atomic_read(&za->zic_num), v, rc);
327 /* XXX: to catch with automated testing */
331 #warning "remove this additional check before release"
334 cfs_hash_bd_del_locked(hs, bd, node);
340 void osd_zfs_acct_fini(const struct lu_env *env, struct osd_device *o)
342 struct hash_cbdata cbdata;
346 /* release object accounting cache (owners) */
347 cbdata.hcb_zapid = o->od_iusr_oid;
349 if (o->od_acct_usr) {
350 cfs_hash_for_each_safe(o->od_acct_usr, osd_zfs_delete_item,
352 cfs_hash_putref(o->od_acct_usr);
353 o->od_acct_usr = NULL;
356 /* release object accounting cache (groups) */
357 cbdata.hcb_zapid = o->od_igrp_oid;
359 if (o->od_acct_grp) {
360 cfs_hash_for_each_safe(o->od_acct_grp, osd_zfs_delete_item,
362 cfs_hash_putref(o->od_acct_grp);
363 o->od_acct_grp = NULL;
367 static int osd_zfs_commit_item(cfs_hash_t *hs, cfs_hash_bd_t *bd,
368 struct hlist_node *node, void *data)
370 struct hash_cbdata *d = data;
371 struct osd_device *osd = d->hcb_osd;
372 struct zfs_id_change *za;
375 za = hlist_entry(node, struct zfs_id_change, zic_hash);
377 rc = -zap_increment_int(osd->od_objset.os, d->hcb_zapid, za->zic_id,
378 atomic_read(&za->zic_num), d->hcb_tx);
379 if (unlikely(rc != 0))
380 CERROR("%s: quota update for UID "LPU64" failed: rc = %d\n",
381 osd->od_svname, za->zic_id, rc);
383 cfs_hash_bd_del_locked(hs, bd, node);
390 * this function is called as part of txg commit procedure,
391 * no more normal changes are allowed to this txg.
392 * we go over all the changes cached in per-txg structure
393 * and apply them to actual ZAPs
395 static void osd_zfs_acct_update(void *arg, void *arg2, dmu_tx_t *tx)
397 struct osd_zfs_acct_txg *zat = arg;
398 struct osd_device *osd = zat->zat_osd;
399 struct hash_cbdata cbdata;
401 cbdata.hcb_osd = osd;
404 CDEBUG(D_OTHER, "COMMIT %llu on %s\n", tx->tx_txg, osd->od_svname);
406 /* apply changes related to the owners */
407 cbdata.hcb_zapid = osd->od_iusr_oid;
408 cfs_hash_for_each_safe(zat->zat_usr, osd_zfs_commit_item, &cbdata);
410 /* apply changes related to the groups */
411 cbdata.hcb_zapid = osd->od_igrp_oid;
412 cfs_hash_for_each_safe(zat->zat_grp, osd_zfs_commit_item, &cbdata);
414 cfs_hash_putref(zat->zat_usr);
415 cfs_hash_putref(zat->zat_grp);
420 static int osd_zfs_acct_check(void *arg1, void *arg2, dmu_tx_t *tx)
422 /* check function isn't used currently */
427 * if any change to the object accounting is going to happen,
428 * we create one structure per txg to track all the changes
429 * and register special routine to be called as part of txg
432 int osd_zfs_acct_trans_start(const struct lu_env *env, struct osd_thandle *oh)
434 struct osd_device *osd = osd_dt_dev(oh->ot_super.th_dev);
435 struct osd_zfs_acct_txg *ac = NULL;
436 int rc = 0, add_work = 0;
438 if (likely(oh->ot_tx->tx_txg == osd->od_known_txg)) {
439 /* already created */
444 if (unlikely(ac == NULL))
447 ac->zat_usr = cfs_hash_create("usr", 4, 4, 4, 0, 0, 0,
448 &acct_hash_operations,
450 if (unlikely(ac->zat_usr == NULL)) {
451 CERROR("%s: can't allocate hash for accounting\n",
453 GOTO(out, rc = -ENOMEM);
456 ac->zat_grp = cfs_hash_create("grp", 4, 4, 4, 0, 0, 0,
457 &acct_hash_operations,
459 if (unlikely(ac->zat_grp == NULL)) {
460 CERROR("%s: can't allocate hash for accounting\n",
462 GOTO(out, rc = -ENOMEM);
465 spin_lock(&osd->od_known_txg_lock);
466 if (oh->ot_tx->tx_txg != osd->od_known_txg) {
467 osd->od_acct_delta = ac;
468 osd->od_known_txg = oh->ot_tx->tx_txg;
471 spin_unlock(&osd->od_known_txg_lock);
473 /* schedule a callback to be run in the context of txg
474 * once the latter is closed and syncing */
476 spa_t *spa = dmu_objset_spa(osd->od_objset.os);
477 LASSERT(ac->zat_osd == NULL);
479 dsl_sync_task_do_nowait(spa_get_dsl(spa),
482 ac, NULL, 128, oh->ot_tx);
484 /* no to be freed now */
490 /* another thread has installed new structure already */
492 cfs_hash_putref(ac->zat_usr);
494 cfs_hash_putref(ac->zat_grp);
501 void osd_zfs_acct_uid(const struct lu_env *env, struct osd_device *osd,
502 __u64 uid, int delta, struct osd_thandle *oh)
506 /* add per-txg job to update accounting */
507 rc = osd_zfs_acct_trans_start(env, oh);
508 if (unlikely(rc != 0))
511 /* maintain per-OSD cached value */
512 osd_zfs_acct_cache_init(env, osd, osd->od_acct_usr,
513 osd->od_iusr_oid, uid, delta, oh);
515 /* maintain per-TXG delta */
516 osd_zfs_acct_id(env, osd->od_acct_delta->zat_usr, uid, delta, oh);
520 void osd_zfs_acct_gid(const struct lu_env *env, struct osd_device *osd,
521 __u64 gid, int delta, struct osd_thandle *oh)
525 /* add per-txg job to update accounting */
526 rc = osd_zfs_acct_trans_start(env, oh);
527 if (unlikely(rc != 0))
530 /* maintain per-OSD cached value */
531 osd_zfs_acct_cache_init(env, osd, osd->od_acct_grp,
532 osd->od_igrp_oid, gid, delta, oh);
534 /* maintain per-TXG delta */
535 osd_zfs_acct_id(env, osd->od_acct_delta->zat_grp, gid, delta, oh);
539 * Space Accounting Management
543 * Return space usage consumed by a given uid or gid.
544 * Block usage is accurrate since it is maintained by DMU itself.
545 * However, DMU does not provide inode accounting, so the #inodes in use
546 * is estimated from the block usage and statfs information.
548 * \param env - is the environment passed by the caller
549 * \param dtobj - is the accounting object
550 * \param dtrec - is the record to fill with space usage information
551 * \param dtkey - is the id the of the user or group for which we would
552 * like to access disk usage.
553 * \param capa - is the capability, not used.
555 * \retval +ve - success : exact match
556 * \retval -ve - failure
558 static int osd_acct_index_lookup(const struct lu_env *env,
559 struct dt_object *dtobj,
560 struct dt_rec *dtrec,
561 const struct dt_key *dtkey,
562 struct lustre_capa *capa)
564 struct osd_thread_info *info = osd_oti_get(env);
565 char *buf = info->oti_buf;
566 struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
567 struct osd_object *obj = osd_dt_obj(dtobj);
568 struct osd_device *osd = osd_obj2dev(obj);
570 struct zfs_id_change *za = NULL;
574 rec->bspace = rec->ispace = 0;
576 /* convert the 64-bit uid/gid into a string */
577 sprintf(buf, "%llx", *((__u64 *)dtkey));
578 /* fetch DMU object ID (DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT) to be
580 oid = osd_quota_fid2dmu(lu_object_fid(&dtobj->do_lu));
582 /* disk usage (in bytes) is maintained by DMU.
583 * DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT are special objects which
584 * not associated with any dmu_but_t (see dnode_special_open()).
585 * As a consequence, we cannot use udmu_zap_lookup() here since it
586 * requires a valid oo_db. */
587 rc = -zap_lookup(osd->od_objset.os, oid, buf, sizeof(uint64_t), 1,
590 /* user/group has not created anything yet */
591 CDEBUG(D_QUOTA, "%s: id %s not found in DMU accounting ZAP\n",
592 osd->od_svname, buf);
596 if (osd->od_quota_iused_est) {
597 if (rec->bspace != 0)
598 /* estimate #inodes in use */
599 rec->ispace = udmu_objset_user_iused(&osd->od_objset,
604 /* as for inode accounting, it is not maintained by DMU, so we just
605 * use our own ZAP to track inode usage */
606 if (oid == DMU_USERUSED_OBJECT) {
607 za = osd_zfs_lookup_by_id(osd->od_acct_usr,
609 } else if (oid == DMU_GROUPUSED_OBJECT) {
610 za = osd_zfs_lookup_by_id(osd->od_acct_grp,
614 rec->ispace = atomic_read(&za->zic_num);
616 rc = -zap_lookup(osd->od_objset.os, obj->oo_db->db_object,
617 buf, sizeof(uint64_t), 1, &rec->ispace);
621 /* user/group has not created any file yet */
622 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
623 osd->od_svname, buf);
631 * Initialize osd Iterator for given osd index object.
633 * \param dt - osd index object
634 * \param attr - not used
635 * \param capa - BYPASS_CAPA
637 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
638 struct dt_object *dt,
640 struct lustre_capa *capa)
642 struct osd_thread_info *info = osd_oti_get(env);
643 struct osd_it_quota *it;
644 struct lu_object *lo = &dt->do_lu;
645 struct osd_device *osd = osd_dev(lo->lo_dev);
649 LASSERT(lu_object_exists(lo));
652 RETURN(ERR_PTR(-ENOMEM));
654 it = &info->oti_it_quota;
655 memset(it, 0, sizeof(*it));
656 it->oiq_oid = osd_quota_fid2dmu(lu_object_fid(lo));
658 if (it->oiq_oid == DMU_GROUPUSED_OBJECT)
659 it->oiq_hash = osd->od_acct_grp;
660 else if (it->oiq_oid == DMU_USERUSED_OBJECT)
661 it->oiq_hash = osd->od_acct_usr;
665 /* initialize zap cursor */
666 rc = -udmu_zap_cursor_init(&it->oiq_zc, &osd->od_objset, it->oiq_oid,0);
670 /* take object reference */
672 it->oiq_obj = osd_dt_obj(dt);
675 RETURN((struct dt_it *)it);
679 * Free given iterator.
681 * \param di - osd iterator
683 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
685 struct osd_it_quota *it = (struct osd_it_quota *)di;
687 udmu_zap_cursor_fini(it->oiq_zc);
688 lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
693 * Move on to the next valid entry.
695 * \param di - osd iterator
697 * \retval +ve - iterator reached the end
698 * \retval 0 - iterator has not reached the end yet
699 * \retval -ve - unexpected failure
701 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
703 struct osd_it_quota *it = (struct osd_it_quota *)di;
707 if (it->oiq_reset == 0)
708 zap_cursor_advance(it->oiq_zc);
710 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, NULL, 32);
711 if (rc == -ENOENT) /* reached the end */
717 * Return pointer to the key under iterator.
719 * \param di - osd iterator
721 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
722 const struct dt_it *di)
724 struct osd_it_quota *it = (struct osd_it_quota *)di;
725 struct osd_thread_info *info = osd_oti_get(env);
726 char *buf = info->oti_buf;
732 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, buf, 32);
735 it->oiq_id = simple_strtoull(buf, &p, 16);
736 RETURN((struct dt_key *) &it->oiq_id);
740 * Return size of key under iterator (in bytes)
742 * \param di - osd iterator
744 static int osd_it_acct_key_size(const struct lu_env *env,
745 const struct dt_it *di)
748 RETURN((int)sizeof(uint64_t));
752 * Return pointer to the record under iterator.
754 * \param di - osd iterator
755 * \param attr - not used
757 static int osd_it_acct_rec(const struct lu_env *env,
758 const struct dt_it *di,
759 struct dt_rec *dtrec, __u32 attr)
761 struct osd_thread_info *info = osd_oti_get(env);
762 char *buf = info->oti_buf;
763 struct osd_it_quota *it = (struct osd_it_quota *)di;
764 struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
765 struct osd_object *obj = it->oiq_obj;
766 struct osd_device *osd = osd_obj2dev(obj);
768 struct zfs_id_change *za;
773 rec->ispace = rec->bspace = 0;
775 /* retrieve block usage from the DMU accounting object */
776 rc = -udmu_zap_cursor_retrieve_value(env, it->oiq_zc,
777 (char *)&rec->bspace,
778 sizeof(uint64_t), &bytes_read);
782 if (osd->od_quota_iused_est) {
783 if (rec->bspace != 0)
784 /* estimate #inodes in use */
785 rec->ispace = udmu_objset_user_iused(&osd->od_objset,
790 /* retrieve key associated with the current cursor */
791 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, buf, 32);
795 /* inode accounting is not maintained by DMU, so we use our own ZAP to
796 * track inode usage */
797 za = osd_zfs_lookup_by_id(it->oiq_hash, it->oiq_id);
799 /* found in the cache */
800 rec->ispace = atomic_read(&za->zic_num);
802 rc = -zap_lookup(osd->od_objset.os,
803 it->oiq_obj->oo_db->db_object,
804 buf, sizeof(uint64_t), 1, &rec->ispace);
806 /* user/group has not created any file yet */
807 CDEBUG(D_QUOTA, "%s: id %s not found in ZAP\n",
808 osd->od_svname, buf);
817 * Returns cookie for current Iterator position.
819 * \param di - osd iterator
821 static __u64 osd_it_acct_store(const struct lu_env *env,
822 const struct dt_it *di)
824 struct osd_it_quota *it = (struct osd_it_quota *)di;
827 RETURN(udmu_zap_cursor_serialize(it->oiq_zc));
831 * Restore iterator from cookie. if the \a hash isn't found,
832 * restore the first valid record.
834 * \param di - osd iterator
835 * \param hash - iterator location cookie
837 * \retval +ve - di points to exact matched key
838 * \retval 0 - di points to the first valid record
839 * \retval -ve - failure
841 static int osd_it_acct_load(const struct lu_env *env,
842 const struct dt_it *di, __u64 hash)
844 struct osd_it_quota *it = (struct osd_it_quota *)di;
845 struct osd_device *osd = osd_obj2dev(it->oiq_obj);
850 /* create new cursor pointing to the new hash */
851 rc = -udmu_zap_cursor_init(&zc, &osd->od_objset, it->oiq_oid, hash);
854 udmu_zap_cursor_fini(it->oiq_zc);
858 rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, NULL, 32);
861 else if (rc == -ENOENT)
867 * Move Iterator to record specified by \a key, if the \a key isn't found,
868 * move to the first valid record.
870 * \param di - osd iterator
871 * \param key - uid or gid
873 * \retval +ve - di points to exact matched key
874 * \retval 0 - di points to the first valid record
875 * \retval -ve - failure
877 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
878 const struct dt_key *key)
882 /* XXX: like osd_zap_it_get(), API is currently broken */
883 LASSERT(*((__u64 *)key) == 0);
885 RETURN(osd_it_acct_load(env, di, 0));
891 * \param di - osd iterator
893 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
898 * Index and Iterator operations for accounting objects
900 const struct dt_index_operations osd_acct_index_ops = {
901 .dio_lookup = osd_acct_index_lookup,
903 .init = osd_it_acct_init,
904 .fini = osd_it_acct_fini,
905 .get = osd_it_acct_get,
906 .put = osd_it_acct_put,
907 .next = osd_it_acct_next,
908 .key = osd_it_acct_key,
909 .key_size = osd_it_acct_key_size,
910 .rec = osd_it_acct_rec,
911 .store = osd_it_acct_store,
912 .load = osd_it_acct_load
917 * Quota Enforcement Management
921 * Wrapper for qsd_op_begin().
923 * \param env - the environment passed by the caller
924 * \param osd - is the osd_device
925 * \param uid - user id of the inode
926 * \param gid - group id of the inode
927 * \param space - how many blocks/inodes will be consumed/released
928 * \param oh - osd transaction handle
929 * \param is_blk - block quota or inode quota?
930 * \param flags - if the operation is write, return no user quota, no
931 * group quota, or sync commit flags to the caller
932 * \param force - set to 1 when changes are performed by root user and thus
933 * can't failed with EDQUOT
935 * \retval 0 - success
936 * \retval -ve - failure
938 int osd_declare_quota(const struct lu_env *env, struct osd_device *osd,
939 qid_t uid, qid_t gid, long long space,
940 struct osd_thandle *oh, bool is_blk, int *flags,
943 struct osd_thread_info *info = osd_oti_get(env);
944 struct lquota_id_info *qi = &info->oti_qi;
945 struct qsd_instance *qsd = osd->od_quota_slave;
946 int rcu, rcg; /* user & group rc */
949 if (unlikely(qsd == NULL))
950 /* quota slave instance hasn't been allocated yet */
953 /* let's start with user quota */
954 qi->lqi_id.qid_uid = uid;
955 qi->lqi_type = USRQUOTA;
956 qi->lqi_space = space;
957 qi->lqi_is_blk = is_blk;
958 rcu = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
960 if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
961 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
964 /* For non-fatal error, we want to continue to get the noquota flags
965 * for group id. This is only for commit write, which has @flags passed
966 * in. See osd_declare_write_commit().
967 * When force is set to true, we also want to proceed with the gid */
968 if (rcu && (rcu != -EDQUOT || flags == NULL))
971 /* and now group quota */
972 qi->lqi_id.qid_gid = gid;
973 qi->lqi_type = GRPQUOTA;
974 rcg = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
976 if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
977 /* as before, ignore EDQUOT & EINPROGRESS for root */
980 RETURN(rcu ? rcu : rcg);