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, 2013, 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);
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)
74 LASSERT(fid_is_acct(fid));
76 if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
77 LDISKFS_FEATURE_RO_COMPAT_QUOTA))
80 id->oii_gen = OSD_OII_NOGEN;
81 id->oii_ino = qf_inums[fid2type(fid)];
82 if (!ldiskfs_valid_inum(sb, id->oii_ino))
88 * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
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.
97 * \retval +ve - success : exact match
98 * \retval -ve - failure
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)
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;
110 struct if_dqblk *dqblk = &info->oti_dqblk;
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);
116 #ifdef HAVE_DQUOT_KQID
122 memset((void *)dqblk, 0, sizeof(struct obd_dqblk));
123 #ifdef HAVE_DQUOT_KQID
124 qid = make_kqid(&init_user_ns, obj2type(dtobj), id);
125 rc = sb->s_qcop->get_dqblk(sb, qid, dqblk);
127 rc = sb->s_qcop->get_dqblk(sb, obj2type(dtobj), (qid_t) id, dqblk);
131 #ifdef HAVE_DQUOT_FS_DISK_QUOTA
132 rec->bspace = dqblk->d_bcount;
133 rec->ispace = dqblk->d_icount;
135 rec->bspace = dqblk->dqb_curspace;
136 rec->ispace = dqblk->dqb_curinodes;
141 #define QUOTA_IT_READ_ERROR(it, rc) \
142 CERROR("%s: Error while trying to read quota information, " \
143 "failed with %d\n", \
144 osd_dev(it->oiq_obj->oo_dt.do_lu.lo_dev)->od_svname, rc); \
147 * Initialize osd Iterator for given osd index object.
149 * \param dt - osd index object
150 * \param attr - not used
151 * \param capa - BYPASS_CAPA
153 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
154 struct dt_object *dt,
155 __u32 attr, struct lustre_capa *capa)
157 struct osd_thread_info *info = osd_oti_get(env);
158 struct osd_it_quota *it;
159 struct lu_object *lo = &dt->do_lu;
160 struct osd_object *obj = osd_dt_obj(dt);
164 LASSERT(lu_object_exists(lo));
167 RETURN(ERR_PTR(-ENOMEM));
169 it = &info->oti_it_quota;
170 memset(it, 0, sizeof(*it));
173 INIT_LIST_HEAD(&it->oiq_list);
175 /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
176 it->oiq_blk[0] = LUSTRE_DQTREEOFF;
178 /* NB: we don't need to store the tree depth since it is always
179 * equal to LUSTRE_DQTREEDEPTH - 1 (root has depth = 0) for a leaf
181 RETURN((struct dt_it *)it);
185 * Free given iterator.
187 * \param di - osd iterator
189 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
191 struct osd_it_quota *it = (struct osd_it_quota *)di;
192 struct osd_quota_leaf *leaf, *tmp;
195 lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
197 list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
198 list_del_init(&leaf->oql_link);
205 * Move Iterator to record specified by \a key, if the \a key isn't found,
206 * move to the first valid record.
208 * \param di - osd iterator
209 * \param key - uid or gid
211 * \retval +ve - di points to the first valid record
212 * \retval +1 - di points to exact matched key
213 * \retval -ve - failure
215 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
216 const struct dt_key *key)
218 struct osd_it_quota *it = (struct osd_it_quota *)di;
219 const struct lu_fid *fid =
220 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
221 int type = fid2type(fid);
222 qid_t dqid = *(qid_t *)key;
228 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
229 LUSTRE_DQTREEOFF, 0, it);
230 if (offset > 0) { /* Found */
232 } else if (offset < 0) { /* Error */
233 QUOTA_IT_READ_ERROR(it, (int)offset);
237 /* The @key is not found, move to the first valid entry */
238 rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
251 * \param di - osd iterator
253 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
258 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
260 struct osd_quota_leaf *leaf;
265 INIT_LIST_HEAD(&leaf->oql_link);
266 leaf->oql_blk = it->oiq_blk[depth];
267 list_add_tail(&leaf->oql_link, &it->oiq_list);
272 * Move on to the next valid entry.
274 * \param di - osd iterator
276 * \retval +ve - iterator reached the end
277 * \retval 0 - iterator has not reached the end yet
278 * \retval -ve - unexpected failure
280 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
282 struct osd_it_quota *it = (struct osd_it_quota *)di;
283 const struct lu_fid *fid =
284 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
285 int type = fid2type(fid);
291 /* Let's first check if there are any remaining valid entry in the
292 * current leaf block. Start with the next entry after the current one.
294 depth = LUSTRE_DQTREEDEPTH;
295 index = it->oiq_index[depth];
296 if (++index < LUSTRE_DQSTRINBLK) {
297 /* Search for the next valid entry from current index */
298 rc = walk_block_dqentry(env, it->oiq_obj, type,
299 it->oiq_blk[depth], index, it);
301 QUOTA_IT_READ_ERROR(it, rc);
303 } else if (rc == 0) {
304 /* Found on entry, @it is already updated to the
305 * new position in walk_block_dqentry(). */
308 rc = osd_it_add_processed(it, depth);
313 rc = osd_it_add_processed(it, depth);
319 /* We have consumed all the entries of the current leaf block, move on
320 * to the next one. */
323 /* We keep searching as long as walk_tree_dqentry() returns +1
324 * (= no valid entry found). */
325 for (; depth >= 0 && rc > 0; depth--) {
326 index = it->oiq_index[depth];
329 rc = walk_tree_dqentry(env, it->oiq_obj, type,
330 it->oiq_blk[depth], depth, index, it);
334 QUOTA_IT_READ_ERROR(it, rc);
339 * Return pointer to the key under iterator.
341 * \param di - osd iterator
343 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
344 const struct dt_it *di)
346 struct osd_it_quota *it = (struct osd_it_quota *)di;
349 RETURN((struct dt_key *)&it->oiq_id);
353 * Return size of key under iterator (in bytes)
355 * \param di - osd iterator
357 static int osd_it_acct_key_size(const struct lu_env *env,
358 const struct dt_it *di)
360 struct osd_it_quota *it = (struct osd_it_quota *)di;
363 RETURN((int)sizeof(it->oiq_id));
367 * Return pointer to the record under iterator.
369 * \param di - osd iterator
370 * \param attr - not used
372 static int osd_it_acct_rec(const struct lu_env *env,
373 const struct dt_it *di,
374 struct dt_rec *dtrec, __u32 attr)
376 struct osd_it_quota *it = (struct osd_it_quota *)di;
377 const struct dt_key *key = osd_it_acct_key(env, di);
382 rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key,
384 RETURN(rc > 0 ? 0 : rc);
388 * Returns cookie for current Iterator position.
390 * \param di - osd iterator
392 static __u64 osd_it_acct_store(const struct lu_env *env,
393 const struct dt_it *di)
395 struct osd_it_quota *it = (struct osd_it_quota *)di;
402 * Restore iterator from cookie. if the \a hash isn't found,
403 * restore the first valid record.
405 * \param di - osd iterator
406 * \param hash - iterator location cookie
408 * \retval +ve - di points to the first valid record
409 * \retval +1 - di points to exact matched hash
410 * \retval -ve - failure
412 static int osd_it_acct_load(const struct lu_env *env,
413 const struct dt_it *di, __u64 hash)
416 RETURN(osd_it_acct_get(env, (struct dt_it *)di,
417 (const struct dt_key *)&hash));
421 * Index and Iterator operations for accounting objects
423 const struct dt_index_operations osd_acct_index_ops = {
424 .dio_lookup = osd_acct_index_lookup,
426 .init = osd_it_acct_init,
427 .fini = osd_it_acct_fini,
428 .get = osd_it_acct_get,
429 .put = osd_it_acct_put,
430 .next = osd_it_acct_next,
431 .key = osd_it_acct_key,
432 .key_size = osd_it_acct_key_size,
433 .rec = osd_it_acct_rec,
434 .store = osd_it_acct_store,
435 .load = osd_it_acct_load
439 static inline void osd_quota_swab(char *ptr, size_t size)
443 LASSERT((size & (sizeof(__u64) - 1)) == 0);
445 for (offset = 0; offset < size; offset += sizeof(__u64))
446 __swab64s((__u64 *)(ptr + offset));
449 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
450 const struct dt_rec *rec,
451 union lquota_rec *quota_rec)
454 struct iam_descr *descr;
456 LASSERT(obj->oo_dir != NULL);
457 descr = obj->oo_dir->od_container.ic_descr;
459 memcpy(quota_rec, rec, descr->id_rec_size);
461 osd_quota_swab((char *)quota_rec, descr->id_rec_size);
462 return (const struct dt_rec *)quota_rec;
468 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
471 struct iam_descr *descr;
473 LASSERT(obj->oo_dir != NULL);
474 descr = obj->oo_dir->od_container.ic_descr;
476 osd_quota_swab((char *)rec, descr->id_rec_size);
482 static inline int osd_qid_type(struct osd_thandle *oh, int i)
484 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
487 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
489 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
493 * Reserve journal credits for quota files update first, then call
494 * ->op_begin() to perform quota enforcement.
496 * \param env - the environment passed by the caller
497 * \param oh - osd transaction handle
498 * \param qi - quota id & space required for this operation
499 * \param allocated - dquot entry in quota accounting file has been allocated
500 * \param flags - if the operation is write, return no user quota, no
501 * group quota, or sync commit flags to the caller
503 * \retval 0 - success
504 * \retval -ve - failure
506 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
507 struct lquota_id_info *qi, bool allocated, int *flags)
509 struct osd_thread_info *info = osd_oti_get(env);
510 struct osd_device *dev = info->oti_dev;
511 struct qsd_instance *qsd = dev->od_quota_slave;
517 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
520 for (i = 0; i < oh->ot_id_cnt; i++) {
521 if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
522 osd_qid_type(oh, i) == qi->lqi_type) {
529 /* we need to account for credits for this new ID */
530 if (i >= OSD_MAX_UGID_CNT) {
531 CERROR("Too many(%d) trans qids!\n", i + 1);
535 osd_trans_declare_op(env, oh, OSD_OT_QUOTA,
536 (allocated || qi->lqi_id.qid_uid == 0) ?
537 1: LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev)));
539 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
540 osd_qid_set_type(oh, i, qi->lqi_type);
544 if (unlikely(qsd == NULL))
545 /* quota slave instance hasn't been allocated yet */
549 rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
554 * Wrapper for osd_declare_qid()
556 * \param env - the environment passed by the caller
557 * \param uid - user id of the inode
558 * \param gid - group id of the inode
559 * \param space - how many blocks/inodes will be consumed/released
560 * \param oh - osd transaction handle
561 * \param is_blk - block quota or inode quota?
562 * \param allocated - dquot entry in quota accounting file has been allocated
563 * \param flags - if the operation is write, return no user quota, no
564 * group quota, or sync commit flags to the caller
565 * \param force - set to 1 when changes are performed by root user and thus
566 * can't failed with EDQUOT
568 * \retval 0 - success
569 * \retval -ve - failure
571 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
572 long long space, struct osd_thandle *oh,
573 bool is_blk, bool allocated, int *flags, bool force)
575 struct osd_thread_info *info = osd_oti_get(env);
576 struct lquota_id_info *qi = &info->oti_qi;
577 int rcu, rcg; /* user & group rc */
580 /* let's start with user quota */
581 qi->lqi_id.qid_uid = uid;
582 qi->lqi_type = USRQUOTA;
583 qi->lqi_space = space;
584 qi->lqi_is_blk = is_blk;
585 rcu = osd_declare_qid(env, oh, qi, allocated, flags);
587 if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
588 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
591 /* For non-fatal error, we want to continue to get the noquota flags
592 * for group id. This is only for commit write, which has @flags passed
593 * in. See osd_declare_write_commit().
594 * When force is set to true, we also want to proceed with the gid */
595 if (rcu && (rcu != -EDQUOT || flags == NULL))
598 /* and now group quota */
599 qi->lqi_id.qid_gid = gid;
600 qi->lqi_type = GRPQUOTA;
601 rcg = osd_declare_qid(env, oh, qi, allocated, flags);
603 if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
604 /* as before, ignore EDQUOT & EINPROGRESS for root */
607 RETURN(rcu ? rcu : rcg);
610 /* Following code is used to migrate old admin quota files (in Linux quota
611 * file v2 format) into the new quota global indexes (in IAM format). */
613 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
615 /* copied from osd_it_acct_get(), only changed the 'type' to -1 */
616 static int osd_it_admin_get(const struct lu_env *env, struct dt_it *di,
617 const struct dt_key *key)
619 struct osd_it_quota *it = (struct osd_it_quota *)di;
621 qid_t dqid = *(qid_t *)key;
626 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
627 LUSTRE_DQTREEOFF, 0, it);
628 if (offset > 0) { /* Found */
630 } else if (offset < 0) { /* Error */
631 QUOTA_IT_READ_ERROR(it, (int)offset);
635 /* The @key is not found, move to the first valid entry */
636 rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
639 /* no valid entry found */
644 static int osd_it_admin_load(const struct lu_env *env,
645 const struct dt_it *di, __u64 hash)
650 rc = osd_it_admin_get(env, (struct dt_it *)di,
651 (const struct dt_key *)&hash);
655 static int osd_it_admin_rec(const struct lu_env *env,
656 const struct dt_it *di,
657 struct dt_rec *dtrec, __u32 attr)
659 struct osd_it_quota *it = (struct osd_it_quota *)di;
663 struct lustre_disk_dqblk_v2 *dqblk =
664 (struct lustre_disk_dqblk_v2 *)dtrec;
668 buf.lb_len = sizeof(*dqblk);
670 pos = it->oiq_offset;
671 rc = dt_record_read(env, &it->oiq_obj->oo_dt, &buf, &pos);
675 /* copied from osd_it_acct_next(), only changed the 'type' to -1 */
676 static int osd_it_admin_next(const struct lu_env *env, struct dt_it *di)
678 struct osd_it_quota *it = (struct osd_it_quota *)di;
684 /* Let's first check if there are any remaining valid entry in the
685 * current leaf block. Start with the next entry after the current one.
687 depth = LUSTRE_DQTREEDEPTH;
688 index = it->oiq_index[depth];
689 if (++index < LUSTRE_DQSTRINBLK) {
690 /* Search for the next valid entry from current index */
691 rc = walk_block_dqentry(env, it->oiq_obj, type,
692 it->oiq_blk[depth], index, it);
694 QUOTA_IT_READ_ERROR(it, rc);
696 } else if (rc == 0) {
697 /* Found on entry, @it is already updated to the
698 * new position in walk_block_dqentry(). */
701 rc = osd_it_add_processed(it, depth);
706 rc = osd_it_add_processed(it, depth);
712 /* We have consumed all the entries of the current leaf block, move on
713 * to the next one. */
716 /* We keep searching as long as walk_tree_dqentry() returns +1
717 * (= no valid entry found). */
718 for (; depth >= 0 && rc > 0; depth--) {
719 index = it->oiq_index[depth];
722 rc = walk_tree_dqentry(env, it->oiq_obj, type,
723 it->oiq_blk[depth], depth, index, it);
727 QUOTA_IT_READ_ERROR(it, rc);
731 const struct dt_index_operations osd_admin_index_ops = {
732 .dio_lookup = osd_acct_index_lookup,
734 .init = osd_it_acct_init,
735 .fini = osd_it_acct_fini,
736 .get = osd_it_admin_get,
737 .put = osd_it_acct_put,
738 .next = osd_it_admin_next,
739 .key = osd_it_acct_key,
740 .key_size = osd_it_acct_key_size,
741 .rec = osd_it_admin_rec,
742 .store = osd_it_acct_store,
743 .load = osd_it_admin_load
747 static int convert_quota_file(const struct lu_env *env,
748 struct dt_object *old, struct dt_object *new,
751 const struct dt_it_ops *iops = &old->do_index_ops->dio_it;
752 struct osd_object *obj;
757 struct lquota_glb_rec *glb_rec = NULL;
760 struct lustre_disk_dqblk_v2 *dqblk = NULL;
761 struct lustre_disk_dqinfo *dqinfo = NULL;
764 obj = osd_dt_obj(old);
765 LASSERT(obj->oo_inode);
767 if (i_size_read(obj->oo_inode) == 0)
770 /* allocate buffers */
771 OBD_ALLOC_PTR(dqinfo);
775 OBD_ALLOC_PTR(glb_rec);
777 GOTO(out, rc = -ENOMEM);
779 OBD_ALLOC_PTR(dqblk);
781 GOTO(out, rc = -ENOMEM);
783 /* convert the old igrace/bgrace */
785 buf.lb_len = sizeof(*dqinfo);
786 pos = LUSTRE_DQINFOOFF;
788 rc = dt_record_read(env, old, &buf, &pos);
792 /* keep it in little endian */
793 grace = isblk ? dqinfo->dqi_bgrace : dqinfo->dqi_igrace;
795 glb_rec->qbr_time = grace;
796 rc = lquota_disk_write_glb(env, new, 0, glb_rec);
799 glb_rec->qbr_time = 0;
802 /* iterate the old admin file, insert each record into the
804 it = iops->init(env, old, 0, BYPASS_CAPA);
806 GOTO(out, rc = PTR_ERR(it));
808 rc = iops->load(env, it, 0);
810 GOTO(out_it, rc = 0);
815 key = iops->key(env, it);
817 GOTO(out_it, rc = PTR_ERR(key));
819 /* skip the root user/group */
820 if (*((__u64 *)key) == 0)
823 rc = iops->rec(env, it, (struct dt_rec *)dqblk, 0);
827 /* keep the value in little endian */
828 glb_rec->qbr_hardlimit = isblk ? dqblk->dqb_bhardlimit :
829 dqblk->dqb_ihardlimit;
830 glb_rec->qbr_softlimit = isblk ? dqblk->dqb_bsoftlimit :
831 dqblk->dqb_isoftlimit;
833 rc = lquota_disk_write_glb(env, new, *((__u64 *)key), glb_rec);
837 rc = iops->next(env, it);
851 OBD_FREE_PTR(glb_rec);
853 OBD_FREE_PTR(dqinfo);
857 /* Nobdy else can access the global index now, it's safe to truncate and
859 static int truncate_quota_index(const struct lu_env *env, struct dt_object *dt,
860 const struct dt_index_features *feat)
862 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
864 struct lu_attr *attr;
865 struct osd_thandle *oth;
868 struct iam_container *bag = &(osd_dt_obj(dt))->oo_dir->od_container;
869 struct lu_buf *lb = &osd_oti_get(env)->oti_buf;
872 LASSERT(bag->ic_root_bh != NULL);
873 iam_container_fini(bag);
875 LASSERT(fid_seq(lu_object_fid(&dt->do_lu)) == FID_SEQ_QUOTA_GLB);
882 attr->la_valid = LA_SIZE;
884 th = dt_trans_create(env, &osd->od_dt_dev);
890 rc = dt_declare_punch(env, dt, 0, OBD_OBJECT_EOF, th);
894 rc = dt_declare_attr_set(env, dt, attr, th);
898 inode = osd_dt_obj(dt)->oo_inode;
901 /* iam_lfix_create() writes two blocks at the beginning */
902 lb->lb_len = osd_sb(osd)->s_blocksize * 2;
903 rc = dt_declare_record_write(env, dt, lb, 0, th);
907 rc = dt_trans_start_local(env, &osd->od_dt_dev, th);
911 dt_write_lock(env, dt, 0);
912 rc = dt_punch(env, dt, 0, OBD_OBJECT_EOF, th, BYPASS_CAPA);
916 rc = dt_attr_set(env, dt, attr, th, BYPASS_CAPA);
920 oth = container_of(th, struct osd_thandle, ot_super);
922 if (feat->dif_flags & DT_IND_VARKEY)
923 rc = iam_lvar_create(osd_dt_obj(dt)->oo_inode,
924 feat->dif_keysize_max,
926 feat->dif_recsize_max, oth->ot_handle);
928 rc = iam_lfix_create(osd_dt_obj(dt)->oo_inode,
929 feat->dif_keysize_max,
931 feat->dif_recsize_max, oth->ot_handle);
933 dt_write_unlock(env, dt);
935 dt_trans_stop(env, &osd->od_dt_dev, th);
939 rc = iam_container_setup(bag);
941 iam_container_fini(bag);
946 static int set_quota_index_version(const struct lu_env *env,
947 struct dt_object *dt,
948 dt_obj_version_t version)
950 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
955 th = dt_trans_create(env, &osd->od_dt_dev);
959 rc = dt_declare_version_set(env, dt, th);
963 rc = dt_trans_start_local(env, &osd->od_dt_dev, th);
968 dt_version_set(env, dt, version, th);
970 dt_trans_stop(env, &osd->od_dt_dev, th);
974 int osd_quota_migration(const struct lu_env *env, struct dt_object *dt,
975 const struct dt_index_features *feat)
977 struct osd_thread_info *oti = osd_oti_get(env);
978 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
979 struct dt_object *root, *parent = NULL, *admin = NULL;
980 dt_obj_version_t version;
982 bool isblk = false, converted = false;
986 /* not newly created global index */
987 version = dt_version_get(env, dt);
992 rc = dt_root_get(env, &osd->od_dt_dev, &oti->oti_fid);
994 CERROR("%s: Can't get root FID, rc:%d\n", osd->od_svname, rc);
998 root = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
1000 CERROR("%s: Failed to locate root "DFID", rc:%ld\n",
1001 osd->od_svname, PFID(&oti->oti_fid), PTR_ERR(root));
1002 RETURN(PTR_ERR(root));
1005 /* locate /OBJECTS */
1006 rc = dt_lookup_dir(env, root, OBJECTS, &oti->oti_fid);
1007 if (rc == -ENOENT) {
1010 CERROR("%s: Failed to lookup %s, rc:%d\n",
1011 osd->od_svname, OBJECTS, rc);
1015 parent = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
1016 if (IS_ERR(parent)) {
1017 CERROR("%s: Failed to locate %s "DFID", rc:%ld\n",
1018 osd->od_svname, OBJECTS, PFID(&oti->oti_fid),
1020 GOTO(out, rc = PTR_ERR(parent));
1023 /* locate quota admin file */
1024 if (feat == &dt_quota_iusr_features) {
1027 } else if (feat == &dt_quota_busr_features) {
1030 } else if (feat == &dt_quota_igrp_features) {
1038 rc = dt_lookup_dir(env, parent, fname, &oti->oti_fid);
1039 if (rc == -ENOENT) {
1042 CERROR("%s: Failed to lookup %s, rc:%d\n",
1043 osd->od_svname, fname, rc);
1047 admin = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
1048 if (IS_ERR(admin)) {
1049 CERROR("%s: Failed to locate %s "DFID", rc:%d\n",
1050 osd->od_svname, fname, PFID(&oti->oti_fid), rc);
1051 GOTO(out, rc = PTR_ERR(admin));
1054 if (!dt_object_exists(admin)) {
1055 CERROR("%s: Old admin file %s doesn't exist, but is still "
1056 " referenced in parent directory.\n",
1057 osd->od_svname, fname);
1058 GOTO(out, rc = -ENOENT);
1061 /* truncate the new quota index file in case of any leftovers
1062 * from last failed migration */
1063 rc = truncate_quota_index(env, dt, feat);
1065 CERROR("%s: Failed to truncate the quota index "DFID", rc:%d\n",
1066 osd->od_svname, PFID(lu_object_fid(&dt->do_lu)), rc);
1070 /* set up indexing operations for the admin file */
1071 admin->do_index_ops = &osd_admin_index_ops;
1073 LCONSOLE_INFO("%s: Migrate %s quota from old admin quota file(%s) to "
1074 "new IAM quota index("DFID").\n", osd->od_svname,
1075 isblk ? "block" : "inode", fname,
1076 PFID(lu_object_fid(&dt->do_lu)));
1078 /* iterate the admin quota file, and insert each record into
1079 * the new index file */
1080 rc = convert_quota_file(env, admin, dt, isblk);
1082 CERROR("%s: Migrate old admin quota file(%s) failed, rc:%d\n",
1083 osd->od_svname, fname, rc);
1086 /* if no migration happen, we need to set the default grace time. */
1087 if (!converted && rc == 0) {
1088 struct lquota_glb_rec *rec = &oti->oti_quota_rec.lqr_glb_rec;
1090 rec->qbr_hardlimit = 0;
1091 rec->qbr_softlimit = 0;
1092 rec->qbr_granted = 0;
1093 rec->qbr_time = isblk ? MAX_DQ_TIME : MAX_IQ_TIME;
1095 rc = lquota_disk_write_glb(env, dt, 0, rec);
1097 CERROR("%s: Failed to set default grace time for "
1098 "index("DFID"), rc:%d\n", osd->od_svname,
1099 PFID(lu_object_fid(&dt->do_lu)), rc);
1102 /* bump index version to 1 (or 2 if migration happened), so the
1103 * migration will be skipped next time. */
1105 rc = set_quota_index_version(env , dt, converted ? 2 : 1);
1107 CERROR("%s: Failed to set quota index("DFID") "
1108 "version, rc:%d\n", osd->od_svname,
1109 PFID(lu_object_fid(&dt->do_lu)), rc);
1112 if (admin && !IS_ERR(admin))
1113 lu_object_put(env, &admin->do_lu);
1114 if (parent && !IS_ERR(parent))
1115 lu_object_put(env, &parent->do_lu);
1116 lu_object_put(env, &root->do_lu);
1121 #warning "remove old quota compatibility code"