Whamcloud - gitweb
LU-2600 osd-zfs: batched object accounting
[fs/lustre-release.git] / lustre / osd-zfs / osd_quota.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann@whamcloud.com>
28  */
29
30 #include <lustre_quota.h>
31 #include <obd.h>
32 #include "osd_internal.h"
33
34 #include <sys/dnode.h>
35 #include <sys/spa.h>
36 #include <sys/zap.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/dsl_prop.h>
39 #include <sys/txg.h>
40
41 /*
42  * the structure tracks per-ID change/state
43  */
44 struct zfs_id_change {
45         struct hlist_node       zic_hash;
46         __u64                   zic_id;
47         atomic_t                zic_num;
48 };
49
50 /*
51  * callback data for cfs_hash_for_each_safe()
52  * used in txg commit and OSD cleanup path
53  */
54 struct hash_cbdata {
55         struct osd_device       *hcb_osd;
56         uint64_t                 hcb_zapid;
57         dmu_tx_t                *hcb_tx;
58 };
59
60 /**
61  * Helper function to retrieve DMU object id from fid for accounting object
62  */
63 static inline uint64_t osd_quota_fid2dmu(const struct lu_fid *fid)
64 {
65         LASSERT(fid_is_acct(fid));
66         if (fid_oid(fid) == ACCT_GROUP_OID)
67                 return DMU_GROUPUSED_OBJECT;
68         return DMU_USERUSED_OBJECT;
69 }
70
71 /*
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.
75  *
76  *  entries in per-txg deltas never go before txg is closed,
77  *  there is no concurrency between removal/insertions.
78  *
79  * also, given all above, there is no need in reference counting.
80  */
81 static struct zfs_id_change *osd_zfs_lookup_by_id(cfs_hash_t *hash, __u64 id)
82 {
83         struct zfs_id_change    *za = NULL;
84         struct hlist_node       *hnode;
85         cfs_hash_bd_t            bd;
86
87         cfs_hash_bd_get(hash, &id, &bd);
88         hnode = cfs_hash_bd_peek_locked(hash, &bd, &id);
89         if (hnode != NULL)
90                 za = container_of0(hnode, struct zfs_id_change, zic_hash);
91
92         return za;
93 }
94
95 static struct zfs_id_change *lookup_or_create_by_id(struct osd_device *osd,
96                                                 cfs_hash_t *hash, __u64 id)
97 {
98         struct zfs_id_change    *za, *tmp;
99         struct hlist_node       *hnode;
100         cfs_hash_bd_t            bd;
101
102         za = osd_zfs_lookup_by_id(hash, id);
103         if (likely(za != NULL))
104                 return za;
105
106         OBD_ALLOC_PTR(za);
107         if (unlikely(za == NULL))
108                 return NULL;
109
110         za->zic_id = id;
111
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);
118
119         if (tmp == za) {
120                 /*
121                  * our structure got into the hash
122                  */
123         } else {
124                 /* somebody won the race, we wasted the cycles */
125                 OBD_FREE_PTR(za);
126         }
127
128         return tmp;
129 }
130
131 /*
132  * used to maintain per-txg deltas
133  */
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)
136 {
137         struct osd_device       *osd = osd_dt_dev(oh->ot_super.th_dev);
138         struct zfs_id_change    *za;
139
140         LASSERT(hash);
141         LASSERT(oh->ot_tx);
142         LASSERT(oh->ot_tx->tx_txg == osd->od_known_txg);
143         LASSERT(osd->od_acct_delta != NULL);
144
145         za = lookup_or_create_by_id(osd, hash, id);
146         if (unlikely(za == NULL))
147                 return -ENOMEM;
148
149         atomic_add(delta, &za->zic_num);
150
151         return 0;
152 }
153
154 /*
155  * this function is used to maintain current state for given ID:
156  * at the beginning it initializes the cache from correspoding ZAP
157  */
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,
161                                     __u64 id, int delta,
162                                     struct osd_thandle *oh)
163 {
164         char                    *buf  = osd_oti_get(env)->oti_buf;
165         struct hlist_node       *hnode;
166         cfs_hash_bd_t            bd;
167         struct zfs_id_change    *za, *tmp;
168         __u64                    v;
169         int                      rc;
170
171         za = osd_zfs_lookup_by_id(hash, id);
172         if (likely(za != NULL))
173                 goto apply;
174
175         /*
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
180          */
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);
185         if (rc == -ENOENT) {
186                 v = 0;
187         } else if (unlikely(rc != 0)) {
188                 CERROR("%s: can't access accounting zap %llu\n",
189                        osd->od_svname, oid);
190                 return;
191         }
192
193         OBD_ALLOC_PTR(za);
194         if (unlikely(za == NULL)) {
195                 CERROR("%s: can't allocate za\n", osd->od_svname);
196                 return;
197         }
198
199         za->zic_id = id;
200         atomic_set(&za->zic_num, v);
201
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);
208
209         if (tmp == za) {
210                 /* our structure got into the hash */
211                 if (rc == -ENOENT) {
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
215                          * cached value. */
216                         v = 0;
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",
221                                        osd->od_svname, rc);
222                 }
223         } else {
224                 /* somebody won the race, we wasted the cycles */
225                 OBD_FREE_PTR(za);
226                 za = tmp;
227         }
228
229 apply:
230         LASSERT(za != NULL);
231         atomic_add(delta, &za->zic_num);
232 }
233
234 static __u32 acct_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
235 {
236         const __u64     *id = key;
237         __u32            result;
238
239         result = (__u32) *id;
240         return result % mask;
241 }
242
243 static void *acct_key(struct hlist_node *hnode)
244 {
245         struct zfs_id_change    *ac;
246
247         ac = hlist_entry(hnode, struct zfs_id_change, zic_hash);
248         return &ac->zic_id;
249 }
250
251 static int acct_hashkey_keycmp(const void *key,
252                                struct hlist_node *compared_hnode)
253 {
254         struct zfs_id_change    *ac;
255         const __u64             *id = key;
256
257         ac = hlist_entry(compared_hnode, struct zfs_id_change, zic_hash);
258         return *id == ac->zic_id;
259 }
260
261 static void *acct_hashobject(struct hlist_node *hnode)
262 {
263         return hlist_entry(hnode, struct zfs_id_change, zic_hash);
264 }
265
266 static cfs_hash_ops_t acct_hash_operations = {
267         .hs_hash        = acct_hashfn,
268         .hs_key         = acct_key,
269         .hs_keycmp      = acct_hashkey_keycmp,
270         .hs_object      = acct_hashobject,
271 };
272
273 #define ACCT_HASH_OPS (CFS_HASH_NO_LOCK|CFS_HASH_NO_ITEMREF|CFS_HASH_ADD_TAIL)
274
275 int osd_zfs_acct_init(const struct lu_env *env, struct osd_device *o)
276 {
277         int rc = 0;
278         ENTRY;
279
280         spin_lock_init(&o->od_known_txg_lock);
281
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,
285                                          ACCT_HASH_OPS);
286         if (o->od_acct_usr == NULL)
287                 GOTO(out, rc = -ENOMEM);
288
289         o->od_acct_grp = cfs_hash_create("grp", 4, 4, 4, 0, 0, 0,
290                                          &acct_hash_operations,
291                                          ACCT_HASH_OPS);
292         if (o->od_acct_grp == NULL)
293                 GOTO(out, rc = -ENOMEM);
294
295 out:
296         RETURN(rc);
297 }
298
299 static int osd_zfs_delete_item(cfs_hash_t *hs, cfs_hash_bd_t *bd,
300                                struct hlist_node *node, void *data)
301 {
302         struct hash_cbdata      *d = data;
303         struct zfs_id_change    *za;
304         __u64                    v;
305         char                     buf[12];
306         int                      rc;
307
308         za = hlist_entry(node, struct zfs_id_change, zic_hash);
309
310         /*
311          * XXX: should we try to fix accounting we failed to update before?
312          */
313 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 5, 70, 0)
314         /*
315          * extra checks to ensure our cache matches on-disk state
316          */
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 */
321         if (rc == -ENOENT)
322                 v = 0;
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 */
328                 LBUG();
329         }
330 #else
331 #warning "remove this additional check before release"
332 #endif
333
334         cfs_hash_bd_del_locked(hs, bd, node);
335         OBD_FREE_PTR(za);
336
337         return 0;
338 }
339
340 void osd_zfs_acct_fini(const struct lu_env *env, struct osd_device *o)
341 {
342         struct hash_cbdata      cbdata;
343
344         cbdata.hcb_osd = o;
345
346         /* release object accounting cache (owners) */
347         cbdata.hcb_zapid = o->od_iusr_oid;
348
349         if (o->od_acct_usr) {
350                 cfs_hash_for_each_safe(o->od_acct_usr, osd_zfs_delete_item,
351                                        &cbdata);
352                 cfs_hash_putref(o->od_acct_usr);
353                 o->od_acct_usr = NULL;
354         }
355
356         /* release object accounting cache (groups) */
357         cbdata.hcb_zapid = o->od_igrp_oid;
358
359         if (o->od_acct_grp) {
360                 cfs_hash_for_each_safe(o->od_acct_grp, osd_zfs_delete_item,
361                                        &cbdata);
362                 cfs_hash_putref(o->od_acct_grp);
363                 o->od_acct_grp = NULL;
364         }
365 }
366
367 static int osd_zfs_commit_item(cfs_hash_t *hs, cfs_hash_bd_t *bd,
368                                struct hlist_node *node, void *data)
369 {
370         struct hash_cbdata      *d = data;
371         struct osd_device       *osd = d->hcb_osd;
372         struct zfs_id_change    *za;
373         int                      rc;
374
375         za = hlist_entry(node, struct zfs_id_change, zic_hash);
376
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);
382
383         cfs_hash_bd_del_locked(hs, bd, node);
384         OBD_FREE_PTR(za);
385
386         return 0;
387 }
388
389 /*
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
394  */
395 static void osd_zfs_acct_update(void *arg, void *arg2, dmu_tx_t *tx)
396 {
397         struct osd_zfs_acct_txg *zat = arg;
398         struct osd_device       *osd = zat->zat_osd;
399         struct hash_cbdata       cbdata;
400
401         cbdata.hcb_osd = osd;
402         cbdata.hcb_tx = tx;
403
404         CDEBUG(D_OTHER, "COMMIT %llu on %s\n", tx->tx_txg, osd->od_svname);
405
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);
409
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);
413
414         cfs_hash_putref(zat->zat_usr);
415         cfs_hash_putref(zat->zat_grp);
416
417         OBD_FREE_PTR(zat);
418 }
419
420 static int osd_zfs_acct_check(void *arg1, void *arg2, dmu_tx_t *tx)
421 {
422         /* check function isn't used currently */
423         return 0;
424 }
425
426 /*
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
430  * commit procedure.
431  */
432 int osd_zfs_acct_trans_start(const struct lu_env *env, struct osd_thandle *oh)
433 {
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;
437
438         if (likely(oh->ot_tx->tx_txg == osd->od_known_txg)) {
439                 /* already created */
440                 return 0;
441         }
442
443         OBD_ALLOC_PTR(ac);
444         if (unlikely(ac == NULL))
445                 return -ENOMEM;
446
447         ac->zat_usr = cfs_hash_create("usr", 4, 4, 4, 0, 0, 0,
448                                       &acct_hash_operations,
449                                       ACCT_HASH_OPS);
450         if (unlikely(ac->zat_usr == NULL)) {
451                 CERROR("%s: can't allocate hash for accounting\n",
452                         osd->od_svname);
453                 GOTO(out, rc = -ENOMEM);
454         }
455
456         ac->zat_grp = cfs_hash_create("grp", 4, 4, 4, 0, 0, 0,
457                                       &acct_hash_operations,
458                                       ACCT_HASH_OPS);
459         if (unlikely(ac->zat_grp == NULL)) {
460                 CERROR("%s: can't allocate hash for accounting\n",
461                         osd->od_svname);
462                 GOTO(out, rc = -ENOMEM);
463         }
464
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;
469                 add_work = 1;
470         }
471         spin_unlock(&osd->od_known_txg_lock);
472
473         /* schedule a callback to be run in the context of txg
474          * once the latter is closed and syncing */
475         if (add_work) {
476                 spa_t *spa = dmu_objset_spa(osd->od_objset.os);
477                 LASSERT(ac->zat_osd == NULL);
478                 ac->zat_osd = osd;
479                 dsl_sync_task_do_nowait(spa_get_dsl(spa),
480                                         osd_zfs_acct_check,
481                                         osd_zfs_acct_update,
482                                         ac, NULL, 128, oh->ot_tx);
483
484                 /* no to be freed now */
485                 ac = NULL;
486         }
487
488 out:
489         if (ac != NULL) {
490                 /* another thread has installed new structure already */
491                 if (ac->zat_usr)
492                         cfs_hash_putref(ac->zat_usr);
493                 if (ac->zat_grp)
494                         cfs_hash_putref(ac->zat_grp);
495                 OBD_FREE_PTR(ac);
496         }
497
498         return rc;
499 }
500
501 void osd_zfs_acct_uid(const struct lu_env *env, struct osd_device *osd,
502                       __u64 uid, int delta, struct osd_thandle *oh)
503 {
504         int rc;
505
506         /* add per-txg job to update accounting */
507         rc = osd_zfs_acct_trans_start(env, oh);
508         if (unlikely(rc != 0))
509                 return;
510
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);
514
515         /* maintain per-TXG delta */
516         osd_zfs_acct_id(env, osd->od_acct_delta->zat_usr, uid, delta, oh);
517
518 }
519
520 void osd_zfs_acct_gid(const struct lu_env *env, struct osd_device *osd,
521                       __u64 gid, int delta, struct osd_thandle *oh)
522 {
523         int rc;
524
525         /* add per-txg job to update accounting */
526         rc = osd_zfs_acct_trans_start(env, oh);
527         if (unlikely(rc != 0))
528                 return;
529
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);
533
534         /* maintain per-TXG delta */
535         osd_zfs_acct_id(env, osd->od_acct_delta->zat_grp, gid, delta, oh);
536 }
537
538 /**
539  * Space Accounting Management
540  */
541
542 /**
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.
547  *
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.
554  *
555  * \retval +ve - success : exact match
556  * \retval -ve - failure
557  */
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)
563 {
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);
569         uint64_t                 oid;
570         struct zfs_id_change    *za = NULL;
571         int                      rc;
572         ENTRY;
573
574         rec->bspace = rec->ispace = 0;
575
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
579          * used */
580         oid = osd_quota_fid2dmu(lu_object_fid(&dtobj->do_lu));
581
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,
588                         &rec->bspace);
589         if (rc == -ENOENT)
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);
593         else if (rc)
594                 RETURN(rc);
595
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,
600                                                              rec->bspace);
601                 RETURN(+1);
602         }
603
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,
608                                          *((__u64 *)dtkey));
609         } else if (oid == DMU_GROUPUSED_OBJECT) {
610                 za = osd_zfs_lookup_by_id(osd->od_acct_grp,
611                                          *((__u64 *)dtkey));
612         }
613         if (za) {
614                 rec->ispace = atomic_read(&za->zic_num);
615         } else {
616                 rc = -zap_lookup(osd->od_objset.os, obj->oo_db->db_object,
617                                 buf, sizeof(uint64_t), 1, &rec->ispace);
618         }
619
620         if (rc == -ENOENT)
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);
624         else if (rc)
625                 RETURN(rc);
626
627         RETURN(+1);
628 }
629
630 /**
631  * Initialize osd Iterator for given osd index object.
632  *
633  * \param  dt    - osd index object
634  * \param  attr  - not used
635  * \param  capa  - BYPASS_CAPA
636  */
637 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
638                                       struct dt_object *dt,
639                                       __u32 attr,
640                                       struct lustre_capa *capa)
641 {
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);
646         int                      rc;
647         ENTRY;
648
649         LASSERT(lu_object_exists(lo));
650
651         if (info == NULL)
652                 RETURN(ERR_PTR(-ENOMEM));
653
654         it = &info->oti_it_quota;
655         memset(it, 0, sizeof(*it));
656         it->oiq_oid = osd_quota_fid2dmu(lu_object_fid(lo));
657
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;
662         else
663                 LBUG();
664
665         /* initialize zap cursor */
666         rc = -udmu_zap_cursor_init(&it->oiq_zc, &osd->od_objset, it->oiq_oid,0);
667         if (rc)
668                 RETURN(ERR_PTR(rc));
669
670         /* take object reference */
671         lu_object_get(lo);
672         it->oiq_obj   = osd_dt_obj(dt);
673         it->oiq_reset = 1;
674
675         RETURN((struct dt_it *)it);
676 }
677
678 /**
679  * Free given iterator.
680  *
681  * \param  di   - osd iterator
682  */
683 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
684 {
685         struct osd_it_quota *it = (struct osd_it_quota *)di;
686         ENTRY;
687         udmu_zap_cursor_fini(it->oiq_zc);
688         lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
689         EXIT;
690 }
691
692 /**
693  * Move on to the next valid entry.
694  *
695  * \param  di   - osd iterator
696  *
697  * \retval +ve  - iterator reached the end
698  * \retval   0  - iterator has not reached the end yet
699  * \retval -ve  - unexpected failure
700  */
701 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
702 {
703         struct osd_it_quota     *it = (struct osd_it_quota *)di;
704         int                      rc;
705         ENTRY;
706
707         if (it->oiq_reset == 0)
708                 zap_cursor_advance(it->oiq_zc);
709         it->oiq_reset = 0;
710         rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, NULL, 32);
711         if (rc == -ENOENT) /* reached the end */
712                 RETURN(+1);
713         RETURN(rc);
714 }
715
716 /**
717  * Return pointer to the key under iterator.
718  *
719  * \param  di   - osd iterator
720  */
721 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
722                                       const struct dt_it *di)
723 {
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;
727         char                    *p;
728         int                      rc;
729         ENTRY;
730
731         it->oiq_reset = 0;
732         rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, buf, 32);
733         if (rc)
734                 RETURN(ERR_PTR(rc));
735         it->oiq_id = simple_strtoull(buf, &p, 16);
736         RETURN((struct dt_key *) &it->oiq_id);
737 }
738
739 /**
740  * Return size of key under iterator (in bytes)
741  *
742  * \param  di   - osd iterator
743  */
744 static int osd_it_acct_key_size(const struct lu_env *env,
745                                 const struct dt_it *di)
746 {
747         ENTRY;
748         RETURN((int)sizeof(uint64_t));
749 }
750
751 /**
752  * Return pointer to the record under iterator.
753  *
754  * \param  di    - osd iterator
755  * \param  attr  - not used
756  */
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)
760 {
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);
767         int                      bytes_read;
768         struct zfs_id_change    *za;
769         int                      rc;
770         ENTRY;
771
772         it->oiq_reset = 0;
773         rec->ispace = rec->bspace = 0;
774
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);
779         if (rc)
780                 RETURN(rc);
781
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,
786                                                              rec->bspace);
787                 RETURN(0);
788         }
789
790         /* retrieve key associated with the current cursor */
791         rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, buf, 32);
792         if (rc)
793                 RETURN(rc);
794
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);
798         if (za != NULL) {
799                 /* found in the cache */
800                 rec->ispace = atomic_read(&za->zic_num);
801         } else {
802                  rc = -zap_lookup(osd->od_objset.os,
803                                   it->oiq_obj->oo_db->db_object,
804                                   buf, sizeof(uint64_t), 1, &rec->ispace);
805                  if (rc == -ENOENT) {
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);
809                         rc = 0;
810                 }
811         }
812
813         RETURN(rc);
814 }
815
816 /**
817  * Returns cookie for current Iterator position.
818  *
819  * \param  di    - osd iterator
820  */
821 static __u64 osd_it_acct_store(const struct lu_env *env,
822                                const struct dt_it *di)
823 {
824         struct osd_it_quota *it = (struct osd_it_quota *)di;
825         ENTRY;
826         it->oiq_reset = 0;
827         RETURN(udmu_zap_cursor_serialize(it->oiq_zc));
828 }
829
830 /**
831  * Restore iterator from cookie. if the \a hash isn't found,
832  * restore the first valid record.
833  *
834  * \param  di    - osd iterator
835  * \param  hash  - iterator location cookie
836  *
837  * \retval +ve  - di points to exact matched key
838  * \retval  0   - di points to the first valid record
839  * \retval -ve  - failure
840  */
841 static int osd_it_acct_load(const struct lu_env *env,
842                             const struct dt_it *di, __u64 hash)
843 {
844         struct osd_it_quota     *it  = (struct osd_it_quota *)di;
845         struct osd_device       *osd = osd_obj2dev(it->oiq_obj);
846         zap_cursor_t            *zc;
847         int                      rc;
848         ENTRY;
849
850         /* create new cursor pointing to the new hash */
851         rc = -udmu_zap_cursor_init(&zc, &osd->od_objset, it->oiq_oid, hash);
852         if (rc)
853                 RETURN(rc);
854         udmu_zap_cursor_fini(it->oiq_zc);
855         it->oiq_zc = zc;
856         it->oiq_reset = 0;
857
858         rc = -udmu_zap_cursor_retrieve_key(env, it->oiq_zc, NULL, 32);
859         if (rc == 0)
860                 RETURN(+1);
861         else if (rc == -ENOENT)
862                 RETURN(0);
863         RETURN(rc);
864 }
865
866 /**
867  * Move Iterator to record specified by \a key, if the \a key isn't found,
868  * move to the first valid record.
869  *
870  * \param  di   - osd iterator
871  * \param  key  - uid or gid
872  *
873  * \retval +ve  - di points to exact matched key
874  * \retval 0    - di points to the first valid record
875  * \retval -ve  - failure
876  */
877 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
878                 const struct dt_key *key)
879 {
880         ENTRY;
881
882         /* XXX: like osd_zap_it_get(), API is currently broken */
883         LASSERT(*((__u64 *)key) == 0);
884
885         RETURN(osd_it_acct_load(env, di, 0));
886 }
887
888 /**
889  * Release Iterator
890  *
891  * \param  di   - osd iterator
892  */
893 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
894 {
895 }
896
897 /**
898  * Index and Iterator operations for accounting objects
899  */
900 const struct dt_index_operations osd_acct_index_ops = {
901         .dio_lookup = osd_acct_index_lookup,
902         .dio_it     = {
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
913         }
914 };
915
916 /**
917  * Quota Enforcement Management
918  */
919
920 /*
921  * Wrapper for qsd_op_begin().
922  *
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
934  *
935  * \retval 0      - success
936  * \retval -ve    - failure
937  */
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,
941                       bool force)
942 {
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 */
947         ENTRY;
948
949         if (unlikely(qsd == NULL))
950                 /* quota slave instance hasn't been allocated yet */
951                 RETURN(0);
952
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);
959
960         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
961                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
962                 rcu = 0;
963
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))
969                 RETURN(rcu);
970
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);
975
976         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
977                 /* as before, ignore EDQUOT & EINPROGRESS for root */
978                 rcg = 0;
979
980         RETURN(rcu ? rcu : rcg);
981 }