4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2012, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Johann Lombardi <johann@whamcloud.com>
28 * Author: Niu Yawei <niu@whamcloud.com>
31 #include <lustre_quota.h>
32 #include "osd_internal.h"
35 * Helpers function to find out the quota type (USRQUOTA/GRPQUOTA) of a
38 static inline int fid2type(const struct lu_fid *fid)
40 LASSERT(fid_is_acct(fid));
41 if (fid_oid(fid) == ACCT_GROUP_OID)
46 static inline int obj2type(struct dt_object *obj)
48 return fid2type(lu_object_fid(&obj->do_lu));
52 * Space Accounting Management
56 * Look up an accounting object based on its fid.
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
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)
67 struct super_block *sb = osd_sb(osd);
70 LASSERT(fid_is_acct(fid));
72 if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
73 LDISKFS_FEATURE_RO_COMPAT_QUOTA))
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))
84 * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
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.
93 * \retval +ve - success : exact match
94 * \retval -ve - failure
96 static int osd_acct_index_lookup(const struct lu_env *env,
97 struct dt_object *dtobj,
99 const struct dt_key *dtkey,
100 struct lustre_capa *capa)
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;
106 struct if_dqblk *dqblk = &info->oti_dqblk;
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);
115 memset((void *)dqblk, 0, sizeof(struct obd_dqblk));
116 rc = sb->s_qcop->get_dqblk(sb, obj2type(dtobj), (qid_t) id, dqblk);
119 #ifdef HAVE_DQUOT_FS_DISK_QUOTA
120 rec->bspace = dqblk->d_bcount;
121 rec->ispace = dqblk->d_icount;
123 rec->bspace = dqblk->dqb_curspace;
124 rec->ispace = dqblk->dqb_curinodes;
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); \
135 * Initialize osd Iterator for given osd index object.
137 * \param dt - osd index object
138 * \param attr - not used
139 * \param capa - BYPASS_CAPA
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)
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);
152 LASSERT(lu_object_exists(lo));
155 RETURN(ERR_PTR(-ENOMEM));
157 it = &info->oti_it_quota;
158 memset(it, 0, sizeof(*it));
161 CFS_INIT_LIST_HEAD(&it->oiq_list);
163 /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
164 it->oiq_blk[0] = LUSTRE_DQTREEOFF;
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
169 RETURN((struct dt_it *)it);
173 * Free given iterator.
175 * \param di - osd iterator
177 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
179 struct osd_it_quota *it = (struct osd_it_quota *)di;
180 struct osd_quota_leaf *leaf, *tmp;
183 lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
185 cfs_list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
186 cfs_list_del_init(&leaf->oql_link);
193 * Move Iterator to record specified by \a key, if the \a key isn't found,
194 * move to the first valid record.
196 * \param di - osd iterator
197 * \param key - uid or gid
199 * \retval +ve - di points to the first valid record
200 * \retval +1 - di points to exact matched key
201 * \retval -ve - failure
203 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
204 const struct dt_key *key)
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;
216 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
217 LUSTRE_DQTREEOFF, 0, it);
218 if (offset > 0) { /* Found */
220 } else if (offset < 0) { /* Error */
221 QUOTA_IT_READ_ERROR(it, (int)offset);
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,
239 * \param di - osd iterator
241 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
246 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
248 struct osd_quota_leaf *leaf;
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);
260 * Move on to the next valid entry.
262 * \param di - osd iterator
264 * \retval +ve - iterator reached the end
265 * \retval 0 - iterator has not reached the end yet
266 * \retval -ve - unexpected failure
268 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
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);
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.
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);
289 QUOTA_IT_READ_ERROR(it, rc);
291 } else if (rc == 0) {
292 /* Found on entry, @it is already updated to the
293 * new position in walk_block_dqentry(). */
296 rc = osd_it_add_processed(it, depth);
301 rc = osd_it_add_processed(it, depth);
307 /* We have consumed all the entries of the current leaf block, move on
308 * to the next one. */
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];
317 rc = walk_tree_dqentry(env, it->oiq_obj, type,
318 it->oiq_blk[depth], depth, index, it);
322 QUOTA_IT_READ_ERROR(it, rc);
327 * Return pointer to the key under iterator.
329 * \param di - osd iterator
331 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
332 const struct dt_it *di)
334 struct osd_it_quota *it = (struct osd_it_quota *)di;
337 RETURN((struct dt_key *)&it->oiq_id);
341 * Return size of key under iterator (in bytes)
343 * \param di - osd iterator
345 static int osd_it_acct_key_size(const struct lu_env *env,
346 const struct dt_it *di)
348 struct osd_it_quota *it = (struct osd_it_quota *)di;
351 RETURN((int)sizeof(it->oiq_id));
355 * Return pointer to the record under iterator.
357 * \param di - osd iterator
358 * \param attr - not used
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)
364 struct osd_it_quota *it = (struct osd_it_quota *)di;
365 const struct dt_key *key = osd_it_acct_key(env, di);
370 rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key,
372 RETURN(rc > 0 ? 0 : rc);
376 * Returns cookie for current Iterator position.
378 * \param di - osd iterator
380 static __u64 osd_it_acct_store(const struct lu_env *env,
381 const struct dt_it *di)
383 struct osd_it_quota *it = (struct osd_it_quota *)di;
390 * Restore iterator from cookie. if the \a hash isn't found,
391 * restore the first valid record.
393 * \param di - osd iterator
394 * \param hash - iterator location cookie
396 * \retval +ve - di points to the first valid record
397 * \retval +1 - di points to exact matched hash
398 * \retval -ve - failure
400 static int osd_it_acct_load(const struct lu_env *env,
401 const struct dt_it *di, __u64 hash)
404 RETURN(osd_it_acct_get(env, (struct dt_it *)di,
405 (const struct dt_key *)&hash));
409 * Index and Iterator operations for accounting objects
411 const struct dt_index_operations osd_acct_index_ops = {
412 .dio_lookup = osd_acct_index_lookup,
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
427 static inline void osd_quota_swab(char *ptr, size_t size)
431 LASSERT((size & (sizeof(__u64) - 1)) == 0);
433 for (offset = 0; offset < size; offset += sizeof(__u64))
434 __swab64s((__u64 *)(ptr + offset));
437 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
438 const struct dt_rec *rec,
439 union lquota_rec *quota_rec)
442 struct iam_descr *descr;
444 LASSERT(obj->oo_dir != NULL);
445 descr = obj->oo_dir->od_container.ic_descr;
447 memcpy(quota_rec, rec, descr->id_rec_size);
449 osd_quota_swab((char *)quota_rec, descr->id_rec_size);
450 return (const struct dt_rec *)quota_rec;
456 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
459 struct iam_descr *descr;
461 LASSERT(obj->oo_dir != NULL);
462 descr = obj->oo_dir->od_container.ic_descr;
464 osd_quota_swab((char *)rec, descr->id_rec_size);
470 static inline int osd_qid_type(struct osd_thandle *oh, int i)
472 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
475 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
477 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
481 * Reserve journal credits for quota files update first, then call
482 * ->op_begin() to perform quota enforcement.
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
491 * \retval 0 - success
492 * \retval -ve - failure
494 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
495 struct lquota_id_info *qi, bool allocated, int *flags)
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;
505 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
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) {
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);
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)));
527 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
528 osd_qid_set_type(oh, i, qi->lqi_type);
532 if (unlikely(qsd == NULL))
533 /* quota slave instance hasn't been allocated yet */
537 rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
542 * Wrapper for osd_declare_qid()
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
556 * \retval 0 - success
557 * \retval -ve - failure
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)
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 */
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);
575 if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
576 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
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))
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);
591 if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
592 /* as before, ignore EDQUOT & EINPROGRESS for root */
595 RETURN(rcu ? rcu : rcg);
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). */
601 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
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)
607 struct osd_it_quota *it = (struct osd_it_quota *)di;
609 qid_t dqid = *(qid_t *)key;
614 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
615 LUSTRE_DQTREEOFF, 0, it);
616 if (offset > 0) { /* Found */
618 } else if (offset < 0) { /* Error */
619 QUOTA_IT_READ_ERROR(it, (int)offset);
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,
627 /* no valid entry found */
632 static int osd_it_admin_load(const struct lu_env *env,
633 const struct dt_it *di, __u64 hash)
638 rc = osd_it_admin_get(env, (struct dt_it *)di,
639 (const struct dt_key *)&hash);
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)
647 struct osd_it_quota *it = (struct osd_it_quota *)di;
651 struct lustre_disk_dqblk_v2 *dqblk =
652 (struct lustre_disk_dqblk_v2 *)dtrec;
656 buf.lb_len = sizeof(*dqblk);
658 pos = it->oiq_offset;
659 rc = dt_record_read(env, &it->oiq_obj->oo_dt, &buf, &pos);
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)
666 struct osd_it_quota *it = (struct osd_it_quota *)di;
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.
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);
682 QUOTA_IT_READ_ERROR(it, rc);
684 } else if (rc == 0) {
685 /* Found on entry, @it is already updated to the
686 * new position in walk_block_dqentry(). */
689 rc = osd_it_add_processed(it, depth);
694 rc = osd_it_add_processed(it, depth);
700 /* We have consumed all the entries of the current leaf block, move on
701 * to the next one. */
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];
710 rc = walk_tree_dqentry(env, it->oiq_obj, type,
711 it->oiq_blk[depth], depth, index, it);
715 QUOTA_IT_READ_ERROR(it, rc);
719 const struct dt_index_operations osd_admin_index_ops = {
720 .dio_lookup = osd_acct_index_lookup,
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
735 static int convert_quota_file(const struct lu_env *env,
736 struct dt_object *old, struct dt_object *new,
739 const struct dt_it_ops *iops = &old->do_index_ops->dio_it;
740 struct osd_object *obj;
745 struct lquota_glb_rec *glb_rec = NULL;
748 struct lustre_disk_dqblk_v2 *dqblk = NULL;
749 struct lustre_disk_dqinfo *dqinfo = NULL;
752 obj = osd_dt_obj(old);
753 LASSERT(obj->oo_inode);
755 if (i_size_read(obj->oo_inode) == 0)
758 /* allocate buffers */
759 OBD_ALLOC_PTR(dqinfo);
763 OBD_ALLOC_PTR(glb_rec);
765 GOTO(out, rc = -ENOMEM);
767 OBD_ALLOC_PTR(dqblk);
769 GOTO(out, rc = -ENOMEM);
771 /* convert the old igrace/bgrace */
773 buf.lb_len = sizeof(*dqinfo);
774 pos = LUSTRE_DQINFOOFF;
776 rc = dt_record_read(env, old, &buf, &pos);
780 /* keep it in little endian */
781 grace = isblk ? dqinfo->dqi_bgrace : dqinfo->dqi_igrace;
783 glb_rec->qbr_time = grace;
784 rc = lquota_disk_write_glb(env, new, 0, glb_rec);
787 glb_rec->qbr_time = 0;
790 /* iterate the old admin file, insert each record into the
792 it = iops->init(env, old, 0, BYPASS_CAPA);
794 GOTO(out, rc = PTR_ERR(it));
796 rc = iops->load(env, it, 0);
798 GOTO(out_it, rc = 0);
803 key = iops->key(env, it);
805 GOTO(out_it, rc = PTR_ERR(key));
807 /* skip the root user/group */
808 if (*((__u64 *)key) == 0)
811 rc = iops->rec(env, it, (struct dt_rec *)dqblk, 0);
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;
821 rc = lquota_disk_write_glb(env, new, *((__u64 *)key), glb_rec);
825 rc = iops->next(env, it);
839 OBD_FREE_PTR(glb_rec);
841 OBD_FREE_PTR(dqinfo);
845 static int truncate_quota_index(const struct lu_env *env, struct dt_object *dt,
846 const struct dt_index_features *feat)
848 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
850 struct lu_attr *attr;
851 struct osd_thandle *oth;
861 attr->la_valid = LA_SIZE;
863 th = dt_trans_create(env, &osd->od_dt_dev);
869 rc = dt_declare_punch(env, dt, 0, OBD_OBJECT_EOF, th);
873 rc = dt_declare_attr_set(env, dt, attr, th);
877 inode = osd_dt_obj(dt)->oo_inode;
880 rc = dt_declare_record_write(env, dt, inode->i_sb->s_blocksize * 2, 0, th);
884 rc = dt_trans_start_local(env, &osd->od_dt_dev, th);
888 dt_write_lock(env, dt, 0);
889 rc = dt_punch(env, dt, 0, OBD_OBJECT_EOF, th, BYPASS_CAPA);
893 rc = dt_attr_set(env, dt, attr, th, BYPASS_CAPA);
897 oth = container_of(th, struct osd_thandle, ot_super);
899 if (feat->dif_flags & DT_IND_VARKEY)
900 rc = iam_lvar_create(osd_dt_obj(dt)->oo_inode,
901 feat->dif_keysize_max,
903 feat->dif_recsize_max, oth->ot_handle);
905 rc = iam_lfix_create(osd_dt_obj(dt)->oo_inode,
906 feat->dif_keysize_max,
908 feat->dif_recsize_max, oth->ot_handle);
910 dt_write_unlock(env, dt);
912 dt_trans_stop(env, &osd->od_dt_dev, th);
917 static int set_quota_index_version(const struct lu_env *env,
918 struct dt_object *dt,
919 dt_obj_version_t version)
921 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
926 th = dt_trans_create(env, &osd->od_dt_dev);
930 rc = dt_declare_version_set(env, dt, th);
934 rc = dt_trans_start_local(env, &osd->od_dt_dev, th);
939 dt_version_set(env, dt, version, th);
941 dt_trans_stop(env, &osd->od_dt_dev, th);
945 int osd_quota_migration(const struct lu_env *env, struct dt_object *dt,
946 const struct dt_index_features *feat)
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;
953 bool isblk, converted = false;
957 /* not newly created global index */
958 version = dt_version_get(env, dt);
963 rc = dt_root_get(env, &osd->od_dt_dev, &oti->oti_fid);
965 CERROR("%s: Can't get root FID, rc:%d\n", osd->od_svname, rc);
969 root = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
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));
976 /* locate /OBJECTS */
977 rc = dt_lookup_dir(env, root, OBJECTS, &oti->oti_fid);
981 CERROR("%s: Failed to lookup %s, rc:%d\n",
982 osd->od_svname, OBJECTS, rc);
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),
991 GOTO(out, rc = PTR_ERR(parent));
994 /* locate quota admin file */
995 if (feat == &dt_quota_iusr_features) {
998 } else if (feat == &dt_quota_busr_features) {
1001 } else if (feat == &dt_quota_igrp_features) {
1009 rc = dt_lookup_dir(env, parent, fname, &oti->oti_fid);
1010 if (rc == -ENOENT) {
1013 CERROR("%s: Failed to lookup %s, rc:%d\n",
1014 osd->od_svname, fname, rc);
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));
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);
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);
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);
1041 /* set up indexing operations for the admin file */
1042 admin->do_index_ops = &osd_admin_index_ops;
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)));
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);
1053 CERROR("%s: Migrate old admin quota file(%s) failed, rc:%d\n",
1054 osd->od_svname, fname, rc);
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;
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;
1066 rc = lquota_disk_write_glb(env, dt, 0, rec);
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);
1073 /* bump index version to 1 (or 2 if migration happened), so the
1074 * migration will be skipped next time. */
1076 rc = set_quota_index_version(env , dt, converted ? 2 : 1);
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);
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);
1092 #warning "remove old quota compatibility code"