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