Whamcloud - gitweb
LU-3536 osd: allocate it for each iteration.
[fs/lustre-release.git] / lustre / osd-ldiskfs / 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, 2014, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann@whamcloud.com>
28  * Author: Niu    Yawei    <niu@whamcloud.com>
29  */
30
31 #include <lustre_quota.h>
32 #include "osd_internal.h"
33
34 /**
35  * Helpers function to find out the quota type (USRQUOTA/GRPQUOTA) of a
36  * given object
37  */
38 static inline int fid2type(const struct lu_fid *fid)
39 {
40         LASSERT(fid_is_acct(fid));
41         if (fid_oid(fid) == ACCT_GROUP_OID)
42                 return GRPQUOTA;
43         return USRQUOTA;
44 }
45
46 static inline int obj2type(struct dt_object *obj)
47 {
48         return fid2type(lu_object_fid(&obj->do_lu));
49 }
50
51 /**
52  * Space Accounting Management
53  */
54
55 /**
56  * Look up an accounting object based on its fid.
57  *
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
63  */
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)
66 {
67         struct super_block *sb = osd_sb(osd);
68         unsigned long qf_inums[2] = {
69                 le32_to_cpu(LDISKFS_SB(sb)->s_es->s_usr_quota_inum),
70                 le32_to_cpu(LDISKFS_SB(sb)->s_es->s_grp_quota_inum)
71         };
72
73         ENTRY;
74         LASSERT(fid_is_acct(fid));
75
76         if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
77                                            LDISKFS_FEATURE_RO_COMPAT_QUOTA))
78                 RETURN(-ENOENT);
79
80         id->oii_gen = OSD_OII_NOGEN;
81         id->oii_ino = qf_inums[fid2type(fid)];
82         if (!ldiskfs_valid_inum(sb, id->oii_ino))
83                 RETURN(-ENOENT);
84         RETURN(0);
85 }
86
87 /**
88  * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
89  *
90  * \param env   - is the environment passed by the caller
91  * \param dtobj - is the accounting object
92  * \param dtrec - is the record to fill with space usage information
93  * \param dtkey - is the id the of the user or group for which we would
94  *                like to access disk usage.
95  * \param capa - is the capability, not used.
96  *
97  * \retval +ve - success : exact match
98  * \retval -ve - failure
99  */
100 static int osd_acct_index_lookup(const struct lu_env *env,
101                                  struct dt_object *dtobj,
102                                  struct dt_rec *dtrec,
103                                  const struct dt_key *dtkey,
104                                  struct lustre_capa *capa)
105 {
106         struct osd_thread_info  *info = osd_oti_get(env);
107 #ifdef HAVE_DQUOT_FS_DISK_QUOTA
108         struct fs_disk_quota    *dqblk = &info->oti_fdq;
109 #else
110         struct if_dqblk         *dqblk = &info->oti_dqblk;
111 #endif
112         struct super_block      *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
113         struct lquota_acct_rec  *rec = (struct lquota_acct_rec *)dtrec;
114         __u64                    id = *((__u64 *)dtkey);
115         int                      rc;
116 #ifdef HAVE_DQUOT_KQID
117         struct kqid             qid;
118 #endif
119
120         ENTRY;
121
122         memset((void *)dqblk, 0, sizeof(struct obd_dqblk));
123 #ifdef HAVE_DQUOT_KQID
124         qid = make_kqid(&init_user_ns, obj2type(dtobj), id);
125         rc = sb->s_qcop->get_dqblk(sb, qid, dqblk);
126 #else
127         rc = sb->s_qcop->get_dqblk(sb, obj2type(dtobj), (qid_t) id, dqblk);
128 #endif
129         if (rc)
130                 RETURN(rc);
131 #ifdef HAVE_DQUOT_FS_DISK_QUOTA
132         rec->bspace = dqblk->d_bcount;
133         rec->ispace = dqblk->d_icount;
134 #else
135         rec->bspace = dqblk->dqb_curspace;
136         rec->ispace = dqblk->dqb_curinodes;
137 #endif
138         RETURN(+1);
139 }
140
141 #define QUOTA_IT_READ_ERROR(it, rc)                                    \
142         CERROR("%s: Error while trying to read quota information, "    \
143                "failed with %d\n",                                     \
144                osd_dev(it->oiq_obj->oo_dt.do_lu.lo_dev)->od_svname, rc); \
145
146 /**
147  * Initialize osd Iterator for given osd index object.
148  *
149  * \param  dt    - osd index object
150  * \param  attr  - not used
151  * \param  capa  - BYPASS_CAPA
152  */
153 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
154                                       struct dt_object *dt,
155                                       __u32 attr, struct lustre_capa *capa)
156 {
157         struct osd_it_quota     *it;
158         struct lu_object        *lo = &dt->do_lu;
159         struct osd_object       *obj = osd_dt_obj(dt);
160
161         ENTRY;
162
163         LASSERT(lu_object_exists(lo));
164
165         OBD_ALLOC_PTR(it);
166         if (it == NULL)
167                 RETURN(ERR_PTR(-ENOMEM));
168
169         lu_object_get(lo);
170         it->oiq_obj = obj;
171         INIT_LIST_HEAD(&it->oiq_list);
172
173         /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
174         it->oiq_blk[0] = LUSTRE_DQTREEOFF;
175
176         /* NB: we don't need to store the tree depth since it is always
177          * equal to LUSTRE_DQTREEDEPTH - 1 (root has depth = 0) for a leaf
178          * block. */
179         RETURN((struct dt_it *)it);
180 }
181
182 /**
183  * Free given iterator.
184  *
185  * \param  di   - osd iterator
186  */
187 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
188 {
189         struct osd_it_quota *it = (struct osd_it_quota *)di;
190         struct osd_quota_leaf *leaf, *tmp;
191         ENTRY;
192
193         lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
194
195         list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
196                 list_del_init(&leaf->oql_link);
197                 OBD_FREE_PTR(leaf);
198         }
199
200         OBD_FREE_PTR(it);
201
202         EXIT;
203 }
204
205 /**
206  * Move Iterator to record specified by \a key, if the \a key isn't found,
207  * move to the first valid record.
208  *
209  * \param  di   - osd iterator
210  * \param  key  - uid or gid
211  *
212  * \retval +ve  - di points to the first valid record
213  * \retval  +1  - di points to exact matched key
214  * \retval -ve  - failure
215  */
216 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
217                            const struct dt_key *key)
218 {
219         struct osd_it_quota     *it = (struct osd_it_quota *)di;
220         const struct lu_fid     *fid =
221                                 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
222         int                      type = fid2type(fid);
223         qid_t                    dqid = *(qid_t *)key;
224         loff_t                   offset;
225         int                      rc;
226
227         ENTRY;
228
229         offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
230                                    LUSTRE_DQTREEOFF, 0, it);
231         if (offset > 0) { /* Found */
232                 RETURN(+1);
233         } else if (offset < 0) { /* Error */
234                 QUOTA_IT_READ_ERROR(it, (int)offset);
235                 RETURN((int)offset);
236         }
237
238         /* The @key is not found, move to the first valid entry */
239         rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
240                                0, it);
241         if (rc == 0)
242                 rc = 1;
243         else if (rc > 0)
244                 rc = -ENOENT;
245
246         RETURN(rc);
247 }
248
249 /**
250  * Release Iterator
251  *
252  * \param  di   - osd iterator
253  */
254 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
255 {
256         return;
257 }
258
259 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
260 {
261         struct osd_quota_leaf *leaf;
262
263         OBD_ALLOC_PTR(leaf);
264         if (leaf == NULL)
265                 RETURN(-ENOMEM);
266         INIT_LIST_HEAD(&leaf->oql_link);
267         leaf->oql_blk = it->oiq_blk[depth];
268         list_add_tail(&leaf->oql_link, &it->oiq_list);
269         RETURN(0);
270 }
271
272 /**
273  * Move on to the next valid entry.
274  *
275  * \param  di   - osd iterator
276  *
277  * \retval +ve  - iterator reached the end
278  * \retval   0  - iterator has not reached the end yet
279  * \retval -ve  - unexpected failure
280  */
281 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
282 {
283         struct osd_it_quota     *it = (struct osd_it_quota *)di;
284         const struct lu_fid     *fid =
285                                 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
286         int                      type = fid2type(fid);
287         int                      depth, rc;
288         uint                     index;
289
290         ENTRY;
291
292         /* Let's first check if there are any remaining valid entry in the
293          * current leaf block. Start with the next entry after the current one.
294          */
295         depth = LUSTRE_DQTREEDEPTH;
296         index = it->oiq_index[depth];
297         if (++index < LUSTRE_DQSTRINBLK) {
298                 /* Search for the next valid entry from current index */
299                 rc = walk_block_dqentry(env, it->oiq_obj, type,
300                                         it->oiq_blk[depth], index, it);
301                 if (rc < 0) {
302                         QUOTA_IT_READ_ERROR(it, rc);
303                         RETURN(rc);
304                 } else if (rc == 0) {
305                         /* Found on entry, @it is already updated to the
306                          * new position in walk_block_dqentry(). */
307                         RETURN(0);
308                 } else {
309                         rc = osd_it_add_processed(it, depth);
310                         if (rc)
311                                 RETURN(rc);
312                 }
313         } else {
314                 rc = osd_it_add_processed(it, depth);
315                 if (rc)
316                         RETURN(rc);
317         }
318         rc = 1;
319
320         /* We have consumed all the entries of the current leaf block, move on
321          * to the next one. */
322         depth--;
323
324         /* We keep searching as long as walk_tree_dqentry() returns +1
325          * (= no valid entry found). */
326         for (; depth >= 0 && rc > 0; depth--) {
327                 index = it->oiq_index[depth];
328                 if (++index > 0xff)
329                         continue;
330                 rc = walk_tree_dqentry(env, it->oiq_obj, type,
331                                        it->oiq_blk[depth], depth, index, it);
332         }
333
334         if (rc < 0)
335                 QUOTA_IT_READ_ERROR(it, rc);
336         RETURN(rc);
337 }
338
339 /**
340  * Return pointer to the key under iterator.
341  *
342  * \param  di   - osd iterator
343  */
344 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
345                                       const struct dt_it *di)
346 {
347         struct osd_it_quota *it = (struct osd_it_quota *)di;
348
349         ENTRY;
350         RETURN((struct dt_key *)&it->oiq_id);
351 }
352
353 /**
354  * Return size of key under iterator (in bytes)
355  *
356  * \param  di   - osd iterator
357  */
358 static int osd_it_acct_key_size(const struct lu_env *env,
359                                 const struct dt_it *di)
360 {
361         struct osd_it_quota *it = (struct osd_it_quota *)di;
362
363         ENTRY;
364         RETURN((int)sizeof(it->oiq_id));
365 }
366
367 /**
368  * Return pointer to the record under iterator.
369  *
370  * \param  di    - osd iterator
371  * \param  attr  - not used
372  */
373 static int osd_it_acct_rec(const struct lu_env *env,
374                            const struct dt_it *di,
375                            struct dt_rec *dtrec, __u32 attr)
376 {
377         struct osd_it_quota     *it = (struct osd_it_quota *)di;
378         const struct dt_key     *key = osd_it_acct_key(env, di);
379         int                      rc;
380
381         ENTRY;
382
383         rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key,
384                                    BYPASS_CAPA);
385         RETURN(rc > 0 ? 0 : rc);
386 }
387
388 /**
389  * Returns cookie for current Iterator position.
390  *
391  * \param  di    - osd iterator
392  */
393 static __u64 osd_it_acct_store(const struct lu_env *env,
394                                const struct dt_it *di)
395 {
396         struct osd_it_quota *it = (struct osd_it_quota *)di;
397
398         ENTRY;
399         RETURN(it->oiq_id);
400 }
401
402 /**
403  * Restore iterator from cookie. if the \a hash isn't found,
404  * restore the first valid record.
405  *
406  * \param  di    - osd iterator
407  * \param  hash  - iterator location cookie
408  *
409  * \retval +ve   - di points to the first valid record
410  * \retval  +1   - di points to exact matched hash
411  * \retval -ve   - failure
412  */
413 static int osd_it_acct_load(const struct lu_env *env,
414                             const struct dt_it *di, __u64 hash)
415 {
416         ENTRY;
417         RETURN(osd_it_acct_get(env, (struct dt_it *)di,
418                                (const struct dt_key *)&hash));
419 }
420
421 /**
422  * Index and Iterator operations for accounting objects
423  */
424 const struct dt_index_operations osd_acct_index_ops = {
425         .dio_lookup     = osd_acct_index_lookup,
426         .dio_it         = {
427                 .init           = osd_it_acct_init,
428                 .fini           = osd_it_acct_fini,
429                 .get            = osd_it_acct_get,
430                 .put            = osd_it_acct_put,
431                 .next           = osd_it_acct_next,
432                 .key            = osd_it_acct_key,
433                 .key_size       = osd_it_acct_key_size,
434                 .rec            = osd_it_acct_rec,
435                 .store          = osd_it_acct_store,
436                 .load           = osd_it_acct_load
437         }
438 };
439
440 static inline void osd_quota_swab(char *ptr, size_t size)
441 {
442         int offset;
443
444         LASSERT((size & (sizeof(__u64) - 1)) == 0);
445
446         for (offset = 0; offset < size; offset += sizeof(__u64))
447              __swab64s((__u64 *)(ptr + offset));
448 }
449
450 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
451                                     const struct dt_rec *rec,
452                                     union lquota_rec *quota_rec)
453 {
454 #ifdef __BIG_ENDIAN
455         struct iam_descr        *descr;
456
457         LASSERT(obj->oo_dir != NULL);
458         descr = obj->oo_dir->od_container.ic_descr;
459
460         memcpy(quota_rec, rec, descr->id_rec_size);
461
462         osd_quota_swab((char *)quota_rec, descr->id_rec_size);
463         return (const struct dt_rec *)quota_rec;
464 #else
465         return rec;
466 #endif
467 }
468
469 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
470 {
471 #ifdef __BIG_ENDIAN
472         struct iam_descr *descr;
473
474         LASSERT(obj->oo_dir != NULL);
475         descr = obj->oo_dir->od_container.ic_descr;
476
477         osd_quota_swab((char *)rec, descr->id_rec_size);
478 #else
479         return;
480 #endif
481 }
482
483 static inline int osd_qid_type(struct osd_thandle *oh, int i)
484 {
485         return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
486 }
487
488 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
489 {
490         oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
491 }
492
493 /**
494  * Reserve journal credits for quota files update first, then call
495  * ->op_begin() to perform quota enforcement.
496  *
497  * \param  env     - the environment passed by the caller
498  * \param  oh      - osd transaction handle
499  * \param  qi      - quota id & space required for this operation
500  * \param  obj     - osd object, could be NULL when it's under create
501  * \param  enforce - whether to perform quota enforcement
502  * \param  flags   - if the operation is write, return no user quota, no
503  *                   group quota, or sync commit flags to the caller
504  *
505  * \retval 0       - success
506  * \retval -ve     - failure
507  */
508 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
509                     struct lquota_id_info *qi, struct osd_object *obj,
510                     bool enforce, int *flags)
511 {
512         struct osd_thread_info  *info = osd_oti_get(env);
513         struct osd_device       *dev = info->oti_dev;
514         struct qsd_instance     *qsd = dev->od_quota_slave;
515         struct inode            *inode = NULL;
516         int                      i, rc = 0, crd;
517         bool                     found = false;
518         ENTRY;
519
520         LASSERT(oh != NULL);
521         LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
522                  oh->ot_id_cnt);
523
524         for (i = 0; i < oh->ot_id_cnt; i++) {
525                 if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
526                     osd_qid_type(oh, i) == qi->lqi_type) {
527                         found = true;
528                         break;
529                 }
530         }
531
532         if (!found) {
533                 /* we need to account for credits for this new ID */
534                 if (i >= OSD_MAX_UGID_CNT) {
535                         CERROR("Too many(%d) trans qids!\n", i + 1);
536                         RETURN(-EOVERFLOW);
537                 }
538
539                 if (obj != NULL)
540                         inode = obj->oo_inode;
541
542                 /* root ID entry should be always present in the quota file */
543                 if (qi->lqi_id.qid_uid == 0) {
544                         crd = 1;
545                 } else {
546                         /* used space for this ID could be dropped to zero,
547                          * reserve extra credits for removing ID entry from
548                          * the quota file */
549                         if (qi->lqi_space < 0)
550                                 crd = LDISKFS_QUOTA_DEL_BLOCKS(osd_sb(dev));
551                         /* reserve credits for adding ID entry to the quota
552                          * file if the i_dquot isn't initialized yet. */
553                         else if (inode == NULL ||
554                                  inode->i_dquot[qi->lqi_type] == NULL)
555                                 crd = LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
556                         else
557                                 crd = 1;
558                 }
559
560                 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, crd);
561
562                 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
563                 osd_qid_set_type(oh, i, qi->lqi_type);
564                 oh->ot_id_cnt++;
565         }
566
567         if (unlikely(qsd == NULL))
568                 /* quota slave instance hasn't been allocated yet */
569                 RETURN(0);
570
571         /* check quota */
572         if (enforce)
573                 rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
574         RETURN(rc);
575 }
576
577 /**
578  * Wrapper for osd_declare_qid()
579  *
580  * \param  env    - the environment passed by the caller
581  * \param  uid    - user id of the inode
582  * \param  gid    - group id of the inode
583  * \param  space  - how many blocks/inodes will be consumed/released
584  * \param  oh     - osd transaction handle
585  * \param  obj    - osd object, could be NULL when it's under create
586  * \param  is_blk - block quota or inode quota?
587  * \param  flags  - if the operation is write, return no user quota, no
588  *                  group quota, or sync commit flags to the caller
589  * \param force   - set to 1 when changes are performed by root user and thus
590  *                  can't failed with EDQUOT
591  *
592  * \retval 0      - success
593  * \retval -ve    - failure
594  */
595 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
596                           long long space, struct osd_thandle *oh,
597                           struct osd_object *obj, bool is_blk, int *flags,
598                           bool force)
599 {
600         struct osd_thread_info  *info = osd_oti_get(env);
601         struct lquota_id_info   *qi = &info->oti_qi;
602         int                      rcu, rcg; /* user & group rc */
603         ENTRY;
604
605         /* let's start with user quota */
606         qi->lqi_id.qid_uid = uid;
607         qi->lqi_type       = USRQUOTA;
608         qi->lqi_space      = space;
609         qi->lqi_is_blk     = is_blk;
610         rcu = osd_declare_qid(env, oh, qi, obj, true, flags);
611
612         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
613                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
614                 rcu = 0;
615
616         /* For non-fatal error, we want to continue to get the noquota flags
617          * for group id. This is only for commit write, which has @flags passed
618          * in. See osd_declare_write_commit().
619          * When force is set to true, we also want to proceed with the gid */
620         if (rcu && (rcu != -EDQUOT || flags == NULL))
621                 RETURN(rcu);
622
623         /* and now group quota */
624         qi->lqi_id.qid_gid = gid;
625         qi->lqi_type       = GRPQUOTA;
626         rcg = osd_declare_qid(env, oh, qi, obj, true, flags);
627
628         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
629                 /* as before, ignore EDQUOT & EINPROGRESS for root */
630                 rcg = 0;
631
632         RETURN(rcu ? rcu : rcg);
633 }
634
635 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 53, 0)
636 /* Following code is used to migrate old admin quota files (in Linux quota
637  * file v2 format) into the new quota global indexes (in IAM format). */
638
639 /* copied from osd_it_acct_get(), only changed the 'type' to -1 */
640 static int osd_it_admin_get(const struct lu_env *env, struct dt_it *di,
641                             const struct dt_key *key)
642 {
643         struct osd_it_quota     *it = (struct osd_it_quota *)di;
644         int                      type = -1;
645         qid_t                    dqid = *(qid_t *)key;
646         loff_t                   offset;
647         int                      rc;
648         ENTRY;
649
650         offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
651                                    LUSTRE_DQTREEOFF, 0, it);
652         if (offset > 0) { /* Found */
653                 RETURN(+1);
654         } else if (offset < 0) { /* Error */
655                 QUOTA_IT_READ_ERROR(it, (int)offset);
656                 RETURN((int)offset);
657         }
658
659         /* The @key is not found, move to the first valid entry */
660         rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
661                                0, it);
662         if (rc > 0)
663                 /* no valid entry found */
664                 rc = -ENOENT;
665         RETURN(rc);
666 }
667
668 static int osd_it_admin_load(const struct lu_env *env,
669                              const struct dt_it *di, __u64 hash)
670 {
671         int rc;
672         ENTRY;
673
674         rc = osd_it_admin_get(env, (struct dt_it *)di,
675                               (const struct dt_key *)&hash);
676         RETURN(rc);
677 }
678
679 static int osd_it_admin_rec(const struct lu_env *env,
680                             const struct dt_it *di,
681                             struct dt_rec *dtrec, __u32 attr)
682 {
683         struct osd_it_quota     *it = (struct osd_it_quota *)di;
684         struct lu_buf            buf;
685         loff_t                   pos;
686         int                      rc;
687         struct lustre_disk_dqblk_v2 *dqblk =
688                 (struct lustre_disk_dqblk_v2 *)dtrec;
689         ENTRY;
690
691         buf.lb_buf = dqblk;
692         buf.lb_len = sizeof(*dqblk);
693
694         pos = it->oiq_offset;
695         rc = dt_record_read(env, &it->oiq_obj->oo_dt, &buf, &pos);
696         RETURN(rc);
697 }
698
699 /* copied from osd_it_acct_next(), only changed the 'type' to -1 */
700 static int osd_it_admin_next(const struct lu_env *env, struct dt_it *di)
701 {
702         struct osd_it_quota     *it = (struct osd_it_quota *)di;
703         int                      type = -1;
704         int                      depth, rc;
705         uint                     index;
706         ENTRY;
707
708         /* Let's first check if there are any remaining valid entry in the
709          * current leaf block. Start with the next entry after the current one.
710          */
711         depth = LUSTRE_DQTREEDEPTH;
712         index = it->oiq_index[depth];
713         if (++index < LUSTRE_DQSTRINBLK) {
714                 /* Search for the next valid entry from current index */
715                 rc = walk_block_dqentry(env, it->oiq_obj, type,
716                                         it->oiq_blk[depth], index, it);
717                 if (rc < 0) {
718                         QUOTA_IT_READ_ERROR(it, rc);
719                         RETURN(rc);
720                 } else if (rc == 0) {
721                         /* Found on entry, @it is already updated to the
722                          * new position in walk_block_dqentry(). */
723                         RETURN(0);
724                 } else {
725                         rc = osd_it_add_processed(it, depth);
726                         if (rc)
727                                 RETURN(rc);
728                 }
729         } else {
730                 rc = osd_it_add_processed(it, depth);
731                 if (rc)
732                         RETURN(rc);
733         }
734         rc = 1;
735
736         /* We have consumed all the entries of the current leaf block, move on
737          * to the next one. */
738         depth--;
739
740         /* We keep searching as long as walk_tree_dqentry() returns +1
741          * (= no valid entry found). */
742         for (; depth >= 0 && rc > 0; depth--) {
743                 index = it->oiq_index[depth];
744                 if (++index > 0xff)
745                         continue;
746                 rc = walk_tree_dqentry(env, it->oiq_obj, type,
747                                        it->oiq_blk[depth], depth, index, it);
748         }
749
750         if (rc < 0)
751                 QUOTA_IT_READ_ERROR(it, rc);
752         RETURN(rc);
753 }
754
755 static const struct dt_index_operations osd_admin_index_ops = {
756         .dio_lookup     = osd_acct_index_lookup,
757         .dio_it         = {
758                 .init     = osd_it_acct_init,
759                 .fini     = osd_it_acct_fini,
760                 .get      = osd_it_admin_get,
761                 .put      = osd_it_acct_put,
762                 .next     = osd_it_admin_next,
763                 .key      = osd_it_acct_key,
764                 .key_size = osd_it_acct_key_size,
765                 .rec      = osd_it_admin_rec,
766                 .store    = osd_it_acct_store,
767                 .load     = osd_it_admin_load
768         }
769 };
770
771 static int convert_quota_file(const struct lu_env *env,
772                               struct dt_object *old, struct dt_object *new,
773                               bool isblk)
774 {
775         const struct dt_it_ops  *iops = &old->do_index_ops->dio_it;
776         struct osd_object       *obj;
777         struct lu_buf            buf;
778         struct dt_it            *it;
779         struct dt_key           *key;
780         __u32                    grace;
781         struct lquota_glb_rec   *glb_rec = NULL;
782         loff_t                   pos;
783         int                      rc;
784         struct lustre_disk_dqblk_v2     *dqblk = NULL;
785         struct lustre_disk_dqinfo       *dqinfo = NULL;
786         ENTRY;
787
788         obj = osd_dt_obj(old);
789         LASSERT(obj->oo_inode);
790
791         if (i_size_read(obj->oo_inode) == 0)
792                 RETURN(0);
793
794         /* allocate buffers */
795         OBD_ALLOC_PTR(dqinfo);
796         if (dqinfo == NULL)
797                 RETURN(-ENOMEM);
798
799         OBD_ALLOC_PTR(glb_rec);
800         if (glb_rec == NULL)
801                 GOTO(out, rc = -ENOMEM);
802
803         OBD_ALLOC_PTR(dqblk);
804         if (dqblk == NULL)
805                 GOTO(out, rc = -ENOMEM);
806
807         /* convert the old igrace/bgrace */
808         buf.lb_buf = dqinfo;
809         buf.lb_len = sizeof(*dqinfo);
810         pos = LUSTRE_DQINFOOFF;
811
812         rc = dt_record_read(env, old, &buf, &pos);
813         if (rc)
814                 GOTO(out, rc);
815
816         /* keep it in little endian */
817         grace = isblk ? dqinfo->dqi_bgrace : dqinfo->dqi_igrace;
818         if (grace != 0) {
819                 glb_rec->qbr_time = grace;
820                 rc = lquota_disk_write_glb(env, new, 0, glb_rec);
821                 if (rc)
822                         GOTO(out, rc);
823                 glb_rec->qbr_time = 0;
824         }
825
826         /* iterate the old admin file, insert each record into the
827          * new index file. */
828         it = iops->init(env, old, 0, BYPASS_CAPA);
829         if (IS_ERR(it))
830                 GOTO(out, rc = PTR_ERR(it));
831
832         rc = iops->load(env, it, 0);
833         if (rc == -ENOENT)
834                 GOTO(out_it, rc = 0);
835         else if (rc < 0)
836                 GOTO(out_it, rc);
837
838         do {
839                 key = iops->key(env, it);
840                 if (IS_ERR(key))
841                         GOTO(out_it, rc = PTR_ERR(key));
842
843                 /* skip the root user/group */
844                 if (*((__u64 *)key) == 0)
845                         goto next;
846
847                 rc = iops->rec(env, it, (struct dt_rec *)dqblk, 0);
848                 if (rc)
849                         GOTO(out_it, rc);
850
851                 /* keep the value in little endian */
852                 glb_rec->qbr_hardlimit = isblk ? dqblk->dqb_bhardlimit :
853                                                  dqblk->dqb_ihardlimit;
854                 glb_rec->qbr_softlimit = isblk ? dqblk->dqb_bsoftlimit :
855                                                  dqblk->dqb_isoftlimit;
856
857                 rc = lquota_disk_write_glb(env, new, *((__u64 *)key), glb_rec);
858                 if (rc)
859                         GOTO(out_it, rc);
860 next:
861                 rc = iops->next(env, it);
862         } while (rc == 0);
863
864         /* reach the end */
865         if (rc > 0)
866                 rc = 0;
867
868 out_it:
869         iops->put(env, it);
870         iops->fini(env, it);
871 out:
872         if (dqblk != NULL)
873                 OBD_FREE_PTR(dqblk);
874         if (glb_rec != NULL)
875                 OBD_FREE_PTR(glb_rec);
876         if (dqinfo != NULL)
877                 OBD_FREE_PTR(dqinfo);
878         return rc;
879 }
880
881 /* Nobdy else can access the global index now, it's safe to truncate and
882  * reinitialize it */
883 static int truncate_quota_index(const struct lu_env *env, struct dt_object *dt,
884                                 const struct dt_index_features *feat)
885 {
886         struct osd_device       *osd = osd_obj2dev(osd_dt_obj(dt));
887         struct thandle          *th;
888         struct lu_attr          *attr;
889         struct osd_thandle      *oth;
890         struct inode            *inode;
891         int                      rc;
892         struct iam_container    *bag = &(osd_dt_obj(dt))->oo_dir->od_container;
893         struct lu_buf           *lb = &osd_oti_get(env)->oti_buf;
894         ENTRY;
895
896         LASSERT(bag->ic_root_bh != NULL);
897         iam_container_fini(bag);
898
899         LASSERT(fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB);
900
901         OBD_ALLOC_PTR(attr);
902         if (attr == NULL)
903                 RETURN(-ENOMEM);
904
905         attr->la_size = 0;
906         attr->la_valid = LA_SIZE;
907
908         th = dt_trans_create(env, &osd->od_dt_dev);
909         if (IS_ERR(th)) {
910                 OBD_FREE_PTR(attr);
911                 RETURN(PTR_ERR(th));
912         }
913
914         rc = dt_declare_punch(env, dt, 0, OBD_OBJECT_EOF, th);
915         if (rc)
916                 GOTO(out, rc);
917
918         rc = dt_declare_attr_set(env, dt, attr, th);
919         if (rc)
920                 GOTO(out, rc);
921
922         inode = osd_dt_obj(dt)->oo_inode;
923         LASSERT(inode);
924
925         /* iam_lfix_create() writes two blocks at the beginning */
926         lb->lb_len = osd_sb(osd)->s_blocksize * 2;
927         rc = dt_declare_record_write(env, dt, lb, 0, th);
928         if (rc)
929                 GOTO(out, rc);
930
931         rc = dt_trans_start_local(env, &osd->od_dt_dev, th);
932         if (rc)
933                 GOTO(out, rc);
934
935         dt_write_lock(env, dt, 0);
936         rc = dt_punch(env, dt, 0, OBD_OBJECT_EOF, th, BYPASS_CAPA);
937         if (rc)
938                 GOTO(out_lock, rc);
939
940         rc = dt_attr_set(env, dt, attr, th, BYPASS_CAPA);
941         if (rc)
942                 GOTO(out_lock, rc);
943
944         oth = container_of(th, struct osd_thandle, ot_super);
945
946         if (feat->dif_flags & DT_IND_VARKEY)
947                 rc = iam_lvar_create(osd_dt_obj(dt)->oo_inode,
948                                      feat->dif_keysize_max,
949                                      feat->dif_ptrsize,
950                                      feat->dif_recsize_max, oth->ot_handle);
951         else
952                 rc = iam_lfix_create(osd_dt_obj(dt)->oo_inode,
953                                      feat->dif_keysize_max,
954                                      feat->dif_ptrsize,
955                                      feat->dif_recsize_max, oth->ot_handle);
956 out_lock:
957         dt_write_unlock(env, dt);
958 out:
959         dt_trans_stop(env, &osd->od_dt_dev, th);
960         OBD_FREE_PTR(attr);
961
962         if (rc == 0) {
963                 rc  = iam_container_setup(bag);
964                 if (rc != 0)
965                         iam_container_fini(bag);
966         }
967         RETURN(rc);
968 }
969
970 static int set_quota_index_version(const struct lu_env *env,
971                                    struct dt_object *dt,
972                                    dt_obj_version_t version)
973 {
974         struct osd_device       *osd = osd_obj2dev(osd_dt_obj(dt));
975         struct thandle          *th;
976         int                      rc;
977         ENTRY;
978
979         th = dt_trans_create(env, &osd->od_dt_dev);
980         if (IS_ERR(th))
981                 RETURN(PTR_ERR(th));
982
983         rc = dt_declare_version_set(env, dt, th);
984         if (rc)
985                 GOTO(out, rc);
986
987         rc = dt_trans_start_local(env, &osd->od_dt_dev, th);
988         if (rc)
989                 GOTO(out, rc);
990
991         th->th_sync = 1;
992         dt_version_set(env, dt, version, th);
993 out:
994         dt_trans_stop(env, &osd->od_dt_dev, th);
995         RETURN(rc);
996 }
997
998 int osd_quota_migration(const struct lu_env *env, struct dt_object *dt,
999                         const struct dt_index_features *feat)
1000 {
1001         struct osd_thread_info  *oti = osd_oti_get(env);
1002         struct osd_device       *osd = osd_obj2dev(osd_dt_obj(dt));
1003         struct dt_object        *root, *parent = NULL, *admin = NULL;
1004         dt_obj_version_t         version;
1005         char                    *fname;
1006         bool                     isblk = false, converted = false;
1007         int                      rc;
1008         ENTRY;
1009
1010         /* not newly created global index */
1011         version = dt_version_get(env, dt);
1012         if (version != 0)
1013                 RETURN(0);
1014
1015         /* locate root */
1016         rc = dt_root_get(env, &osd->od_dt_dev, &oti->oti_fid);
1017         if (rc) {
1018                 CERROR("%s: Can't get root FID, rc:%d\n", osd->od_svname, rc);
1019                 RETURN(rc);
1020         }
1021
1022         root = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
1023         if (IS_ERR(root)) {
1024                 CERROR("%s: Failed to locate root "DFID", rc:%ld\n",
1025                        osd->od_svname, PFID(&oti->oti_fid), PTR_ERR(root));
1026                 RETURN(PTR_ERR(root));
1027         }
1028
1029         /* locate /OBJECTS */
1030         rc = dt_lookup_dir(env, root, OBJECTS, &oti->oti_fid);
1031         if (rc == -ENOENT) {
1032                 GOTO(out, rc = 0);
1033         } else if (rc) {
1034                 CERROR("%s: Failed to lookup %s, rc:%d\n",
1035                        osd->od_svname, OBJECTS, rc);
1036                 GOTO(out, rc);
1037         }
1038
1039         parent = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
1040         if (IS_ERR(parent)) {
1041                 CERROR("%s: Failed to locate %s "DFID", rc:%ld\n",
1042                        osd->od_svname, OBJECTS, PFID(&oti->oti_fid),
1043                        PTR_ERR(parent));
1044                 GOTO(out, rc = PTR_ERR(parent));
1045         }
1046
1047         /* locate quota admin file */
1048         if (feat == &dt_quota_iusr_features) {
1049                 fname = ADMIN_USR;
1050                 isblk = false;
1051         } else if (feat == &dt_quota_busr_features) {
1052                 fname = ADMIN_USR;
1053                 isblk = true;
1054         } else if (feat == &dt_quota_igrp_features) {
1055                 fname = ADMIN_GRP;
1056                 isblk = false;
1057         } else {
1058                 fname = ADMIN_GRP;
1059                 isblk = true;
1060         }
1061
1062         rc = dt_lookup_dir(env, parent, fname, &oti->oti_fid);
1063         if (rc == -ENOENT) {
1064                 GOTO(out, rc = 0);
1065         } else if (rc) {
1066                 CERROR("%s: Failed to lookup %s, rc:%d\n",
1067                        osd->od_svname, fname, rc);
1068                 GOTO(out, rc);
1069         }
1070
1071         admin = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
1072         if (IS_ERR(admin)) {
1073                 CERROR("%s: Failed to locate %s "DFID", rc:%d\n",
1074                        osd->od_svname, fname, PFID(&oti->oti_fid), rc);
1075                 GOTO(out, rc = PTR_ERR(admin));
1076         }
1077
1078         if (!dt_object_exists(admin)) {
1079                 CERROR("%s: Old admin file %s doesn't exist, but is still "
1080                        " referenced in parent directory.\n",
1081                        osd->od_svname, fname);
1082                 GOTO(out, rc = -ENOENT);
1083         }
1084
1085         /* truncate the new quota index file in case of any leftovers
1086          * from last failed migration */
1087         rc = truncate_quota_index(env, dt, feat);
1088         if (rc) {
1089                 CERROR("%s: Failed to truncate the quota index "DFID", rc:%d\n",
1090                        osd->od_svname, PFID(lu_object_fid(&dt->do_lu)), rc);
1091                 GOTO(out, rc);
1092         }
1093
1094         /* set up indexing operations for the admin file */
1095         admin->do_index_ops = &osd_admin_index_ops;
1096
1097         LCONSOLE_INFO("%s: Migrate %s quota from old admin quota file(%s) to "
1098                       "new IAM quota index("DFID").\n", osd->od_svname,
1099                       isblk ? "block" : "inode", fname,
1100                       PFID(lu_object_fid(&dt->do_lu)));
1101
1102         /* iterate the admin quota file, and insert each record into
1103          * the new index file */
1104         rc = convert_quota_file(env, admin, dt, isblk);
1105         if (rc)
1106                 CERROR("%s: Migrate old admin quota file(%s) failed, rc:%d\n",
1107                        osd->od_svname, fname, rc);
1108         converted = true;
1109 out:
1110         /* if no migration happen, we need to set the default grace time. */
1111         if (!converted && rc == 0) {
1112                 struct lquota_glb_rec *rec = &oti->oti_quota_rec.lqr_glb_rec;
1113
1114                 rec->qbr_hardlimit = 0;
1115                 rec->qbr_softlimit = 0;
1116                 rec->qbr_granted = 0;
1117                 rec->qbr_time = isblk ? MAX_DQ_TIME : MAX_IQ_TIME;
1118
1119                 rc = lquota_disk_write_glb(env, dt, 0, rec);
1120                 if (rc)
1121                         CERROR("%s: Failed to set default grace time for "
1122                                "index("DFID"), rc:%d\n", osd->od_svname,
1123                                PFID(lu_object_fid(&dt->do_lu)), rc);
1124         }
1125
1126         /* bump index version to 1 (or 2 if migration happened), so the
1127          * migration will be skipped next time. */
1128         if (rc == 0) {
1129                 rc = set_quota_index_version(env , dt, converted ? 2 : 1);
1130                 if (rc)
1131                         CERROR("%s: Failed to set quota index("DFID") "
1132                                "version, rc:%d\n", osd->od_svname,
1133                                PFID(lu_object_fid(&dt->do_lu)), rc);
1134         }
1135
1136         if (admin && !IS_ERR(admin))
1137                 lu_object_put(env, &admin->do_lu);
1138         if (parent && !IS_ERR(parent))
1139                 lu_object_put(env, &parent->do_lu);
1140         lu_object_put(env, &root->do_lu);
1141
1142         RETURN(rc);
1143 }
1144 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 53, 0) */