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