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