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