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