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, 2015, 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_RO_COMPAT_FEATURE(sb,
76 LDISKFS_FEATURE_RO_COMPAT_QUOTA))
79 id->oii_gen = OSD_OII_NOGEN;
80 switch (fid2type(fid)) {
83 le32_to_cpu(LDISKFS_SB(sb)->s_es->s_usr_quota_inum);
87 le32_to_cpu(LDISKFS_SB(sb)->s_es->s_grp_quota_inum);
90 #ifdef HAVE_PROJECT_QUOTA
91 if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
92 LDISKFS_FEATURE_RO_COMPAT_PROJECT))
95 le32_to_cpu(LDISKFS_SB(sb)->s_es->s_prj_quota_inum);
101 if (!ldiskfs_valid_inum(sb, id->oii_ino))
107 * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
109 * \param env - is the environment passed by the caller
110 * \param dtobj - is the accounting object
111 * \param dtrec - is the record to fill with space usage information
112 * \param dtkey - is the id of the user or group for which we would
113 * like to access disk usage.
115 * \retval +ve - success : exact match
116 * \retval -ve - failure
118 static int osd_acct_index_lookup(const struct lu_env *env,
119 struct dt_object *dtobj,
120 struct dt_rec *dtrec,
121 const struct dt_key *dtkey)
123 struct osd_thread_info *info = osd_oti_get(env);
124 #if defined(HAVE_DQUOT_QC_DQBLK)
125 struct qc_dqblk *dqblk = &info->oti_qdq;
126 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
127 struct fs_disk_quota *dqblk = &info->oti_fdq;
129 struct if_dqblk *dqblk = &info->oti_dqblk;
131 struct super_block *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
132 struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
133 __u64 id = *((__u64 *)dtkey);
135 #ifdef HAVE_DQUOT_KQID
142 type = fid2type(lu_object_fid(&dtobj->do_lu));
143 memset(dqblk, 0, sizeof(*dqblk));
144 #ifdef HAVE_DQUOT_KQID
145 qid = make_kqid(&init_user_ns, type, id);
146 rc = sb->s_qcop->get_dqblk(sb, qid, dqblk);
148 rc = sb->s_qcop->get_dqblk(sb, type, (qid_t) id, dqblk);
152 #if defined(HAVE_DQUOT_QC_DQBLK)
153 rec->bspace = dqblk->d_space;
154 rec->ispace = dqblk->d_ino_count;
155 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
156 rec->bspace = dqblk->d_bcount;
157 rec->ispace = dqblk->d_icount;
159 rec->bspace = dqblk->dqb_curspace;
160 rec->ispace = dqblk->dqb_curinodes;
165 #define QUOTA_IT_READ_ERROR(it, rc) \
166 CERROR("%s: Error while trying to read quota information, " \
167 "failed with %d\n", \
168 osd_dev(it->oiq_obj->oo_dt.do_lu.lo_dev)->od_svname, rc); \
171 * Initialize osd Iterator for given osd index object.
173 * \param dt - osd index object
174 * \param attr - not used
176 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
177 struct dt_object *dt,
180 struct osd_it_quota *it;
181 struct lu_object *lo = &dt->do_lu;
182 struct osd_object *obj = osd_dt_obj(dt);
186 LASSERT(lu_object_exists(lo));
190 RETURN(ERR_PTR(-ENOMEM));
194 INIT_LIST_HEAD(&it->oiq_list);
196 /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
197 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
202 RETURN((struct dt_it *)it);
206 * Free given iterator.
208 * \param di - osd iterator
210 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
212 struct osd_it_quota *it = (struct osd_it_quota *)di;
213 struct osd_quota_leaf *leaf, *tmp;
216 osd_object_put(env, it->oiq_obj);
218 list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
219 list_del_init(&leaf->oql_link);
229 * Move Iterator to record specified by \a key, if the \a key isn't found,
230 * move to the first valid record.
232 * \param di - osd iterator
233 * \param key - uid or gid
235 * \retval +ve - di points to the first valid record
236 * \retval +1 - di points to exact matched key
237 * \retval -ve - failure
239 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
240 const struct dt_key *key)
242 struct osd_it_quota *it = (struct osd_it_quota *)di;
243 const struct lu_fid *fid =
244 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
246 qid_t dqid = *(qid_t *)key;
251 type = fid2type(fid);
253 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
254 LUSTRE_DQTREEOFF, 0, it);
255 if (offset > 0) { /* Found */
257 } else if (offset < 0) { /* Error */
258 QUOTA_IT_READ_ERROR(it, (int)offset);
262 /* The @key is not found, move to the first valid entry */
263 rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
276 * \param di - osd iterator
278 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
283 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
285 struct osd_quota_leaf *leaf;
290 INIT_LIST_HEAD(&leaf->oql_link);
291 leaf->oql_blk = it->oiq_blk[depth];
292 list_add_tail(&leaf->oql_link, &it->oiq_list);
297 * Move on to the next valid entry.
299 * \param di - osd iterator
301 * \retval +ve - iterator reached the end
302 * \retval 0 - iterator has not reached the end yet
303 * \retval -ve - unexpected failure
305 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
307 struct osd_it_quota *it = (struct osd_it_quota *)di;
308 const struct lu_fid *fid =
309 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
316 type = fid2type(fid);
318 /* Let's first check if there are any remaining valid entry in the
319 * current leaf block. Start with the next entry after the current one.
321 depth = LUSTRE_DQTREEDEPTH;
322 index = it->oiq_index[depth];
323 if (++index < LUSTRE_DQSTRINBLK) {
324 /* Search for the next valid entry from current index */
325 rc = walk_block_dqentry(env, it->oiq_obj, type,
326 it->oiq_blk[depth], index, it);
328 QUOTA_IT_READ_ERROR(it, rc);
330 } else if (rc == 0) {
331 /* Found on entry, @it is already updated to the
332 * new position in walk_block_dqentry(). */
335 rc = osd_it_add_processed(it, depth);
340 rc = osd_it_add_processed(it, depth);
346 /* We have consumed all the entries of the current leaf block, move on
347 * to the next one. */
350 /* We keep searching as long as walk_tree_dqentry() returns +1
351 * (= no valid entry found). */
352 for (; depth >= 0 && rc > 0; depth--) {
353 index = it->oiq_index[depth];
356 rc = walk_tree_dqentry(env, it->oiq_obj, type,
357 it->oiq_blk[depth], depth, index, it);
361 QUOTA_IT_READ_ERROR(it, rc);
366 * Return pointer to the key under iterator.
368 * \param di - osd iterator
370 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
371 const struct dt_it *di)
373 struct osd_it_quota *it = (struct osd_it_quota *)di;
376 RETURN((struct dt_key *)&it->oiq_id);
380 * Return size of key under iterator (in bytes)
382 * \param di - osd iterator
384 static int osd_it_acct_key_size(const struct lu_env *env,
385 const struct dt_it *di)
387 struct osd_it_quota *it = (struct osd_it_quota *)di;
390 RETURN((int)sizeof(it->oiq_id));
394 * Return pointer to the record under iterator.
396 * \param di - osd iterator
397 * \param attr - not used
399 static int osd_it_acct_rec(const struct lu_env *env,
400 const struct dt_it *di,
401 struct dt_rec *dtrec, __u32 attr)
403 struct osd_it_quota *it = (struct osd_it_quota *)di;
404 const struct dt_key *key = osd_it_acct_key(env, di);
409 rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key);
410 RETURN(rc > 0 ? 0 : rc);
414 * Returns cookie for current Iterator position.
416 * \param di - osd iterator
418 static __u64 osd_it_acct_store(const struct lu_env *env,
419 const struct dt_it *di)
421 struct osd_it_quota *it = (struct osd_it_quota *)di;
428 * Restore iterator from cookie. if the \a hash isn't found,
429 * restore the first valid record.
431 * \param di - osd iterator
432 * \param hash - iterator location cookie
434 * \retval +ve - di points to the first valid record
435 * \retval +1 - di points to exact matched hash
436 * \retval -ve - failure
438 static int osd_it_acct_load(const struct lu_env *env,
439 const struct dt_it *di, __u64 hash)
442 RETURN(osd_it_acct_get(env, (struct dt_it *)di,
443 (const struct dt_key *)&hash));
447 * Index and Iterator operations for accounting objects
449 const struct dt_index_operations osd_acct_index_ops = {
450 .dio_lookup = osd_acct_index_lookup,
452 .init = osd_it_acct_init,
453 .fini = osd_it_acct_fini,
454 .get = osd_it_acct_get,
455 .put = osd_it_acct_put,
456 .next = osd_it_acct_next,
457 .key = osd_it_acct_key,
458 .key_size = osd_it_acct_key_size,
459 .rec = osd_it_acct_rec,
460 .store = osd_it_acct_store,
461 .load = osd_it_acct_load
465 static inline void osd_quota_swab(char *ptr, size_t size)
469 LASSERT((size & (sizeof(__u64) - 1)) == 0);
471 for (offset = 0; offset < size; offset += sizeof(__u64))
472 __swab64s((__u64 *)(ptr + offset));
475 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
476 const struct dt_rec *rec,
477 union lquota_rec *quota_rec)
480 struct iam_descr *descr;
482 LASSERT(obj->oo_dir != NULL);
483 descr = obj->oo_dir->od_container.ic_descr;
485 memcpy(quota_rec, rec, descr->id_rec_size);
487 osd_quota_swab((char *)quota_rec, descr->id_rec_size);
488 return (const struct dt_rec *)quota_rec;
494 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
497 struct iam_descr *descr;
499 LASSERT(obj->oo_dir != NULL);
500 descr = obj->oo_dir->od_container.ic_descr;
502 osd_quota_swab((char *)rec, descr->id_rec_size);
508 static inline int osd_qid_type(struct osd_thandle *oh, int i)
510 return oh->ot_id_types[i];
514 * Reserve journal credits for quota files update first, then call
515 * ->op_begin() to perform quota enforcement.
517 * \param env - the environment passed by the caller
518 * \param oh - osd transaction handle
519 * \param qi - quota id & space required for this operation
520 * \param obj - osd object, could be NULL when it's under create
521 * \param enforce - whether to perform quota enforcement
522 * \param flags - if the operation is write, return no user quota, no
523 * group quota, or sync commit flags to the caller
525 * \retval 0 - success
526 * \retval -ve - failure
528 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
529 struct lquota_id_info *qi, struct osd_object *obj,
530 bool enforce, int *flags)
532 struct osd_device *dev;
533 struct qsd_instance *qsd;
534 struct inode *inode = NULL;
540 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
543 dev = osd_dt_dev(oh->ot_super.th_dev);
544 LASSERT(dev != NULL);
546 qsd = dev->od_quota_slave;
548 for (i = 0; i < oh->ot_id_cnt; i++) {
549 if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
550 oh->ot_id_types[i] == qi->lqi_type) {
557 /* we need to account for credits for this new ID */
558 if (i >= OSD_MAX_UGID_CNT) {
559 CERROR("Too many(%d) trans qids!\n", i + 1);
564 inode = obj->oo_inode;
566 /* root ID entry should be always present in the quota file */
567 if (qi->lqi_id.qid_uid == 0) {
570 /* used space for this ID could be dropped to zero,
571 * reserve extra credits for removing ID entry from
573 if (qi->lqi_space < 0)
574 crd = LDISKFS_QUOTA_DEL_BLOCKS(osd_sb(dev));
575 /* reserve credits for adding ID entry to the quota
576 * file if the i_dquot isn't initialized yet. */
577 else if (inode == NULL ||
578 #ifdef HAVE_EXT4_INFO_DQUOT
579 LDISKFS_I(inode)->i_dquot[qi->lqi_type] == NULL)
581 inode->i_dquot[qi->lqi_type] == NULL)
583 crd = LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
588 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, crd);
590 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
591 oh->ot_id_types[i] = qi->lqi_type;
595 if (unlikely(qsd == NULL))
596 /* quota slave instance hasn't been allocated yet */
601 rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
606 * Wrapper for osd_declare_qid()
608 * \param env - the environment passed by the caller
609 * \param uid - user id of the inode
610 * \param gid - group id of the inode
611 * \param space - how many blocks/inodes will be consumed/released
612 * \param oh - osd transaction handle
613 * \param obj - osd object, could be NULL when it's under create
614 * \param is_blk - block quota or inode quota?
615 * \param flags - if the operation is write, return no user quota, no
616 * group quota, or sync commit flags to the caller
617 * \param force - set to 1 when changes are performed by root user and thus
618 * can't failed with EDQUOT
620 * \retval 0 - success
621 * \retval -ve - failure
623 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
624 __u32 projid, long long space, struct osd_thandle *oh,
625 struct osd_object *obj, bool is_blk, int *flags,
628 struct osd_thread_info *info = osd_oti_get(env);
629 struct lquota_id_info *qi = &info->oti_qi;
630 int rcu, rcg, rcp; /* user & group & project rc */
633 /* let's start with user quota */
634 qi->lqi_id.qid_uid = uid;
635 qi->lqi_type = USRQUOTA;
636 qi->lqi_space = space;
637 qi->lqi_is_blk = is_blk;
638 rcu = osd_declare_qid(env, oh, qi, obj, true, flags);
640 if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
641 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
644 /* For non-fatal error, we want to continue to get the noquota flags
645 * for group id. This is only for commit write, which has @flags passed
646 * in. See osd_declare_write_commit().
647 * When force is set to true, we also want to proceed with the gid */
648 if (rcu && (rcu != -EDQUOT || flags == NULL))
651 /* and now group quota */
652 qi->lqi_id.qid_gid = gid;
653 qi->lqi_type = GRPQUOTA;
654 rcg = osd_declare_qid(env, oh, qi, obj, true, flags);
656 if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
657 /* as before, ignore EDQUOT & EINPROGRESS for root */
659 if (rcg && (rcg != -EDQUOT || flags == NULL))
662 /* and now project quota */
663 qi->lqi_id.qid_gid = projid;
664 qi->lqi_type = PRJQUOTA;
665 rcp = osd_declare_qid(env, oh, qi, obj, true, flags);
667 if (force && (rcp == -EDQUOT || rcp == -EINPROGRESS))
668 /* as before, ignore EDQUOT & EINPROGRESS for root */