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