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, 2017, 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 switch (fid_oid(fid)) {
46 case ACCT_PROJECT_OID:
50 LASSERTF(0, "invalid fid for quota type: %u", fid_oid(fid));
55 * Space Accounting Management
59 * Look up an accounting object based on its fid.
61 * \param info - is the osd thread info passed by the caller
62 * \param osd - is the osd device
63 * \param fid - is the fid of the accounting object we want to look up
64 * \param id - is the osd_inode_id struct to fill with the inode number of
65 * the quota file if the lookup is successful
67 int osd_acct_obj_lookup(struct osd_thread_info *info, struct osd_device *osd,
68 const struct lu_fid *fid, struct osd_inode_id *id)
70 struct super_block *sb = osd_sb(osd);
73 LASSERT(fid_is_acct(fid));
75 if (!ldiskfs_has_feature_quota(sb))
78 id->oii_gen = OSD_OII_NOGEN;
79 switch (fid2type(fid)) {
82 le32_to_cpu(LDISKFS_SB(sb)->s_es->s_usr_quota_inum);
86 le32_to_cpu(LDISKFS_SB(sb)->s_es->s_grp_quota_inum);
89 #ifdef HAVE_PROJECT_QUOTA
90 if (ldiskfs_has_feature_project(sb)) {
93 prj_quota = LDISKFS_SB(sb)->s_es->s_prj_quota_inum;
94 id->oii_ino = le32_to_cpu(prj_quota);
100 if (!ldiskfs_valid_inum(sb, id->oii_ino))
106 * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
108 * \param env - is the environment passed by the caller
109 * \param dtobj - is the accounting object
110 * \param dtrec - is the record to fill with space usage information
111 * \param dtkey - is the id of the user or group for which we would
112 * like to access disk usage.
114 * \retval +ve - success : exact match
115 * \retval -ve - failure
117 static int osd_acct_index_lookup(const struct lu_env *env,
118 struct dt_object *dtobj,
119 struct dt_rec *dtrec,
120 const struct dt_key *dtkey)
122 struct osd_thread_info *info = osd_oti_get(env);
123 #if defined(HAVE_DQUOT_QC_DQBLK)
124 struct qc_dqblk *dqblk = &info->oti_qdq;
125 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
126 struct fs_disk_quota *dqblk = &info->oti_fdq;
128 struct if_dqblk *dqblk = &info->oti_dqblk;
130 struct super_block *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
131 struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
132 __u64 id = *((__u64 *)dtkey);
134 #ifdef HAVE_DQUOT_KQID
141 type = fid2type(lu_object_fid(&dtobj->do_lu));
142 memset(dqblk, 0, sizeof(*dqblk));
143 #ifdef HAVE_DQUOT_KQID
144 qid = make_kqid(&init_user_ns, type, id);
145 rc = sb->s_qcop->get_dqblk(sb, qid, dqblk);
147 rc = sb->s_qcop->get_dqblk(sb, type, (qid_t) id, dqblk);
151 #if defined(HAVE_DQUOT_QC_DQBLK)
152 rec->bspace = dqblk->d_space;
153 rec->ispace = dqblk->d_ino_count;
154 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
155 rec->bspace = dqblk->d_bcount;
156 rec->ispace = dqblk->d_icount;
158 rec->bspace = dqblk->dqb_curspace;
159 rec->ispace = dqblk->dqb_curinodes;
164 #define QUOTA_IT_READ_ERROR(it, rc) \
165 CERROR("%s: Error while trying to read quota information, " \
166 "failed with %d\n", \
167 osd_dev(it->oiq_obj->oo_dt.do_lu.lo_dev)->od_svname, rc) \
170 * Initialize osd Iterator for given osd index object.
172 * \param dt - osd index object
173 * \param attr - not used
175 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
176 struct dt_object *dt,
179 struct osd_it_quota *it;
180 struct lu_object *lo = &dt->do_lu;
181 struct osd_object *obj = osd_dt_obj(dt);
185 LASSERT(lu_object_exists(lo));
189 RETURN(ERR_PTR(-ENOMEM));
193 INIT_LIST_HEAD(&it->oiq_list);
195 /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
196 it->oiq_blk[0] = LUSTRE_DQTREEOFF;
199 * NB: we don't need to store the tree depth since it is always
200 * equal to LUSTRE_DQTREEDEPTH - 1 (root has depth = 0) for a leaf
203 RETURN((struct dt_it *)it);
207 * Free given iterator.
209 * \param di - osd iterator
211 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
213 struct osd_it_quota *it = (struct osd_it_quota *)di;
214 struct osd_quota_leaf *leaf, *tmp;
218 osd_object_put(env, it->oiq_obj);
220 list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
221 list_del_init(&leaf->oql_link);
231 * Move Iterator to record specified by \a key, if the \a key isn't found,
232 * move to the first valid record.
234 * \param di - osd iterator
235 * \param key - uid or gid
237 * \retval +ve - di points to the first valid record
238 * \retval +1 - di points to exact matched key
239 * \retval -ve - failure
241 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
242 const struct dt_key *key)
244 struct osd_it_quota *it = (struct osd_it_quota *)di;
245 const struct lu_fid *fid = lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
247 qid_t dqid = *(qid_t *)key;
252 type = fid2type(fid);
254 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
255 LUSTRE_DQTREEOFF, 0, it);
256 if (offset > 0) { /* Found */
258 } else if (offset < 0) { /* Error */
259 QUOTA_IT_READ_ERROR(it, (int)offset);
263 /* The @key is not found, move to the first valid entry */
264 rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
277 * \param di - osd iterator
279 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
284 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
286 struct osd_quota_leaf *leaf;
291 INIT_LIST_HEAD(&leaf->oql_link);
292 leaf->oql_blk = it->oiq_blk[depth];
293 list_add_tail(&leaf->oql_link, &it->oiq_list);
298 * Move on to the next valid entry.
300 * \param di - osd iterator
302 * \retval +ve - iterator reached the end
303 * \retval 0 - iterator has not reached the end yet
304 * \retval -ve - unexpected failure
306 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
308 struct osd_it_quota *it = (struct osd_it_quota *)di;
309 const struct lu_fid *fid = lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
316 type = fid2type(fid);
319 * Let's first check if there are any remaining valid entry in the
320 * current leaf block. Start with the next entry after the current one.
322 depth = LUSTRE_DQTREEDEPTH;
323 index = it->oiq_index[depth];
324 if (++index < LUSTRE_DQSTRINBLK) {
325 /* Search for the next valid entry from current index */
326 rc = walk_block_dqentry(env, it->oiq_obj, type,
327 it->oiq_blk[depth], index, it);
329 QUOTA_IT_READ_ERROR(it, rc);
331 } else if (rc == 0) {
333 * Found on entry, @it is already updated to the
334 * new position in walk_block_dqentry().
338 rc = osd_it_add_processed(it, depth);
343 rc = osd_it_add_processed(it, depth);
350 * We have consumed all the entries of the current leaf block, move on
356 * We keep searching as long as walk_tree_dqentry() returns +1
357 * (= no valid entry found).
359 for (; depth >= 0 && rc > 0; depth--) {
360 index = it->oiq_index[depth];
363 rc = walk_tree_dqentry(env, it->oiq_obj, type,
364 it->oiq_blk[depth], depth, index, it);
368 QUOTA_IT_READ_ERROR(it, rc);
373 * Return pointer to the key under iterator.
375 * \param di - osd iterator
377 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
378 const struct dt_it *di)
380 struct osd_it_quota *it = (struct osd_it_quota *)di;
383 RETURN((struct dt_key *)&it->oiq_id);
387 * Return size of key under iterator (in bytes)
389 * \param di - osd iterator
391 static int osd_it_acct_key_size(const struct lu_env *env,
392 const struct dt_it *di)
394 struct osd_it_quota *it = (struct osd_it_quota *)di;
397 RETURN((int)sizeof(it->oiq_id));
401 * Return pointer to the record under iterator.
403 * \param di - osd iterator
404 * \param attr - not used
406 static int osd_it_acct_rec(const struct lu_env *env,
407 const struct dt_it *di,
408 struct dt_rec *dtrec, __u32 attr)
410 struct osd_it_quota *it = (struct osd_it_quota *)di;
411 const struct dt_key *key = osd_it_acct_key(env, di);
416 rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key);
417 RETURN(rc > 0 ? 0 : rc);
421 * Returns cookie for current Iterator position.
423 * \param di - osd iterator
425 static __u64 osd_it_acct_store(const struct lu_env *env,
426 const struct dt_it *di)
428 struct osd_it_quota *it = (struct osd_it_quota *)di;
435 * Restore iterator from cookie. if the \a hash isn't found,
436 * restore the first valid record.
438 * \param di - osd iterator
439 * \param hash - iterator location cookie
441 * \retval +ve - di points to the first valid record
442 * \retval +1 - di points to exact matched hash
443 * \retval -ve - failure
445 static int osd_it_acct_load(const struct lu_env *env,
446 const struct dt_it *di, __u64 hash)
448 struct osd_it_quota *it = (struct osd_it_quota *)di;
453 * LU-8999 - If it is called to resume the iteration, calling
454 * osd_it_acct_get could change the block orders in the lower level
455 * of the quota tree, which are saved in osd_it_quota->oiq_blk.
457 if (it->oiq_id != 0 && it->oiq_id == hash)
460 RETURN(osd_it_acct_get(env, (struct dt_it *)di,
461 (const struct dt_key *)&hash));
465 * Index and Iterator operations for accounting objects
467 const struct dt_index_operations osd_acct_index_ops = {
468 .dio_lookup = osd_acct_index_lookup,
470 .init = osd_it_acct_init,
471 .fini = osd_it_acct_fini,
472 .get = osd_it_acct_get,
473 .put = osd_it_acct_put,
474 .next = osd_it_acct_next,
475 .key = osd_it_acct_key,
476 .key_size = osd_it_acct_key_size,
477 .rec = osd_it_acct_rec,
478 .store = osd_it_acct_store,
479 .load = osd_it_acct_load
483 static inline void osd_quota_swab(char *ptr, size_t size)
487 LASSERT((size & (sizeof(__u64) - 1)) == 0);
489 for (offset = 0; offset < size; offset += sizeof(__u64))
490 __swab64s((__u64 *)(ptr + offset));
493 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
494 const struct dt_rec *rec,
495 union lquota_rec *quota_rec)
498 struct iam_descr *descr;
500 LASSERT(obj->oo_dir != NULL);
501 descr = obj->oo_dir->od_container.ic_descr;
503 memcpy(quota_rec, rec, descr->id_rec_size);
505 osd_quota_swab((char *)quota_rec, descr->id_rec_size);
506 return (const struct dt_rec *)quota_rec;
512 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
515 struct iam_descr *descr;
517 LASSERT(obj->oo_dir != NULL);
518 descr = obj->oo_dir->od_container.ic_descr;
520 osd_quota_swab((char *)rec, descr->id_rec_size);
526 static inline int osd_qid_type(struct osd_thandle *oh, int i)
528 return oh->ot_id_types[i];
532 * Reserve journal credits for quota files update first, then call
533 * ->op_begin() to perform quota enforcement.
535 * \param env - the environment passed by the caller
536 * \param oh - osd transaction handle
537 * \param qi - quota id & space required for this operation
538 * \param obj - osd object, could be NULL when it's under create
539 * \param enforce - whether to perform quota enforcement
540 * \param flags - if the operation is write, return no user quota, no
541 * group quota, or sync commit flags to the caller
543 * \retval 0 - success
544 * \retval -ve - failure
546 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
547 struct lquota_id_info *qi, struct osd_object *obj,
548 bool enforce, int *flags)
550 struct osd_device *dev;
551 struct qsd_instance *qsd;
552 struct inode *inode = NULL;
559 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
562 dev = osd_dt_dev(oh->ot_super.th_dev);
563 LASSERT(dev != NULL);
565 qsd = dev->od_quota_slave;
567 for (i = 0; i < oh->ot_id_cnt; i++) {
568 if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
569 oh->ot_id_types[i] == qi->lqi_type) {
576 /* we need to account for credits for this new ID */
577 if (i >= OSD_MAX_UGID_CNT) {
578 CERROR("Too many(%d) trans qids!\n", i + 1);
583 inode = obj->oo_inode;
585 /* root ID entry should be always present in the quota file */
586 if (qi->lqi_id.qid_uid == 0) {
590 * used space for this ID could be dropped to zero,
591 * reserve extra credits for removing ID entry from
594 if (qi->lqi_space < 0)
595 crd = LDISKFS_QUOTA_DEL_BLOCKS(osd_sb(dev));
597 * reserve credits for adding ID entry to the quota
598 * file if the i_dquot isn't initialized yet.
600 else if (inode == NULL ||
601 #ifdef HAVE_EXT4_INFO_DQUOT
602 LDISKFS_I(inode)->i_dquot[qi->lqi_type] ==
605 inode->i_dquot[qi->lqi_type] == NULL)
607 crd = LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
612 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, crd);
614 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
615 oh->ot_id_types[i] = qi->lqi_type;
619 if (unlikely(qsd == NULL))
620 /* quota slave instance hasn't been allocated yet */
625 rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
630 * Wrapper for osd_declare_qid()
632 * \param env - the environment passed by the caller
633 * \param uid - user id of the inode
634 * \param gid - group id of the inode
635 * \param space - how many blocks/inodes will be consumed/released
636 * \param oh - osd transaction handle
637 * \param obj - osd object, could be NULL when it's under create
638 * \param flags - if the operation is write, return no user quota, no
639 * group quota, or sync commit flags to the caller
640 * \param osd_qid_flags - indicate this is a inode/block accounting
641 * and whether changes are performed by root user
643 * \retval 0 - success
644 * \retval -ve - failure
646 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
647 __u32 projid, long long space, struct osd_thandle *oh,
648 struct osd_object *obj, int *flags,
649 enum osd_qid_declare_flags osd_qid_declare_flags)
651 struct osd_thread_info *info = osd_oti_get(env);
652 struct lquota_id_info *qi = &info->oti_qi;
653 int rcu, rcg, rcp = 0; /* user & group & project rc */
654 struct thandle *th = &oh->ot_super;
655 bool force = !!(osd_qid_declare_flags & OSD_QID_FORCE) ||
659 /* let's start with user quota */
660 qi->lqi_id.qid_uid = uid;
661 qi->lqi_type = USRQUOTA;
662 qi->lqi_space = space;
663 qi->lqi_is_blk = !!(osd_qid_declare_flags & OSD_QID_BLK);
664 rcu = osd_declare_qid(env, oh, qi, obj, true, flags);
666 if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
667 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
671 * For non-fatal error, we want to continue to get the noquota flags
672 * for group id. This is only for commit write, which has @flags passed
673 * in. See osd_declare_write_commit().
674 * When force is set to true, we also want to proceed with the gid
676 if (rcu && (rcu != -EDQUOT || flags == NULL))
679 /* and now group quota */
680 qi->lqi_id.qid_gid = gid;
681 qi->lqi_type = GRPQUOTA;
682 rcg = osd_declare_qid(env, oh, qi, obj, true, flags);
684 if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
685 /* as before, ignore EDQUOT & EINPROGRESS for root */
688 #ifdef HAVE_PROJECT_QUOTA
689 if (rcg && (rcg != -EDQUOT || flags == NULL))
692 /* and now project quota */
693 qi->lqi_id.qid_projid = projid;
694 qi->lqi_type = PRJQUOTA;
695 rcp = osd_declare_qid(env, oh, qi, obj, true, flags);
697 if (force && (rcp == -EDQUOT || rcp == -EINPROGRESS))
698 /* as before, ignore EDQUOT & EINPROGRESS for root */
702 RETURN(rcu ? rcu : (rcg ? rcg : rcp));