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 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 of the user or group for which we would
94 * like to access disk usage.
96 * \retval +ve - success : exact match
97 * \retval -ve - failure
99 static int osd_acct_index_lookup(const struct lu_env *env,
100 struct dt_object *dtobj,
101 struct dt_rec *dtrec,
102 const struct dt_key *dtkey)
104 struct osd_thread_info *info = osd_oti_get(env);
105 #ifdef HAVE_DQUOT_FS_DISK_QUOTA
106 struct fs_disk_quota *dqblk = &info->oti_fdq;
108 struct if_dqblk *dqblk = &info->oti_dqblk;
110 struct super_block *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
111 struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
112 __u64 id = *((__u64 *)dtkey);
114 #ifdef HAVE_DQUOT_KQID
120 memset(dqblk, 0, sizeof(*dqblk));
121 #ifdef HAVE_DQUOT_KQID
122 qid = make_kqid(&init_user_ns, obj2type(dtobj), id);
123 rc = sb->s_qcop->get_dqblk(sb, qid, dqblk);
125 rc = sb->s_qcop->get_dqblk(sb, obj2type(dtobj), (qid_t)id, dqblk);
129 #ifdef HAVE_DQUOT_FS_DISK_QUOTA
130 rec->bspace = dqblk->d_bcount;
131 rec->ispace = dqblk->d_icount;
133 rec->bspace = dqblk->dqb_curspace;
134 rec->ispace = dqblk->dqb_curinodes;
139 #define QUOTA_IT_READ_ERROR(it, rc) \
140 CERROR("%s: Error while trying to read quota information, " \
141 "failed with %d\n", \
142 osd_dev(it->oiq_obj->oo_dt.do_lu.lo_dev)->od_svname, rc); \
145 * Initialize osd Iterator for given osd index object.
147 * \param dt - osd index object
148 * \param attr - not used
150 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
151 struct dt_object *dt,
154 struct osd_it_quota *it;
155 struct lu_object *lo = &dt->do_lu;
156 struct osd_object *obj = osd_dt_obj(dt);
160 LASSERT(lu_object_exists(lo));
164 RETURN(ERR_PTR(-ENOMEM));
168 INIT_LIST_HEAD(&it->oiq_list);
170 /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
171 it->oiq_blk[0] = LUSTRE_DQTREEOFF;
173 /* NB: we don't need to store the tree depth since it is always
174 * equal to LUSTRE_DQTREEDEPTH - 1 (root has depth = 0) for a leaf
176 RETURN((struct dt_it *)it);
180 * Free given iterator.
182 * \param di - osd iterator
184 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
186 struct osd_it_quota *it = (struct osd_it_quota *)di;
187 struct osd_quota_leaf *leaf, *tmp;
190 lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
192 list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
193 list_del_init(&leaf->oql_link);
203 * Move Iterator to record specified by \a key, if the \a key isn't found,
204 * move to the first valid record.
206 * \param di - osd iterator
207 * \param key - uid or gid
209 * \retval +ve - di points to the first valid record
210 * \retval +1 - di points to exact matched key
211 * \retval -ve - failure
213 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
214 const struct dt_key *key)
216 struct osd_it_quota *it = (struct osd_it_quota *)di;
217 const struct lu_fid *fid =
218 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
219 int type = fid2type(fid);
220 qid_t dqid = *(qid_t *)key;
226 offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
227 LUSTRE_DQTREEOFF, 0, it);
228 if (offset > 0) { /* Found */
230 } else if (offset < 0) { /* Error */
231 QUOTA_IT_READ_ERROR(it, (int)offset);
235 /* The @key is not found, move to the first valid entry */
236 rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
249 * \param di - osd iterator
251 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
256 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
258 struct osd_quota_leaf *leaf;
263 INIT_LIST_HEAD(&leaf->oql_link);
264 leaf->oql_blk = it->oiq_blk[depth];
265 list_add_tail(&leaf->oql_link, &it->oiq_list);
270 * Move on to the next valid entry.
272 * \param di - osd iterator
274 * \retval +ve - iterator reached the end
275 * \retval 0 - iterator has not reached the end yet
276 * \retval -ve - unexpected failure
278 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
280 struct osd_it_quota *it = (struct osd_it_quota *)di;
281 const struct lu_fid *fid =
282 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
283 int type = fid2type(fid);
289 /* Let's first check if there are any remaining valid entry in the
290 * current leaf block. Start with the next entry after the current one.
292 depth = LUSTRE_DQTREEDEPTH;
293 index = it->oiq_index[depth];
294 if (++index < LUSTRE_DQSTRINBLK) {
295 /* Search for the next valid entry from current index */
296 rc = walk_block_dqentry(env, it->oiq_obj, type,
297 it->oiq_blk[depth], index, it);
299 QUOTA_IT_READ_ERROR(it, rc);
301 } else if (rc == 0) {
302 /* Found on entry, @it is already updated to the
303 * new position in walk_block_dqentry(). */
306 rc = osd_it_add_processed(it, depth);
311 rc = osd_it_add_processed(it, depth);
317 /* We have consumed all the entries of the current leaf block, move on
318 * to the next one. */
321 /* We keep searching as long as walk_tree_dqentry() returns +1
322 * (= no valid entry found). */
323 for (; depth >= 0 && rc > 0; depth--) {
324 index = it->oiq_index[depth];
327 rc = walk_tree_dqentry(env, it->oiq_obj, type,
328 it->oiq_blk[depth], depth, index, it);
332 QUOTA_IT_READ_ERROR(it, rc);
337 * Return pointer to the key under iterator.
339 * \param di - osd iterator
341 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
342 const struct dt_it *di)
344 struct osd_it_quota *it = (struct osd_it_quota *)di;
347 RETURN((struct dt_key *)&it->oiq_id);
351 * Return size of key under iterator (in bytes)
353 * \param di - osd iterator
355 static int osd_it_acct_key_size(const struct lu_env *env,
356 const struct dt_it *di)
358 struct osd_it_quota *it = (struct osd_it_quota *)di;
361 RETURN((int)sizeof(it->oiq_id));
365 * Return pointer to the record under iterator.
367 * \param di - osd iterator
368 * \param attr - not used
370 static int osd_it_acct_rec(const struct lu_env *env,
371 const struct dt_it *di,
372 struct dt_rec *dtrec, __u32 attr)
374 struct osd_it_quota *it = (struct osd_it_quota *)di;
375 const struct dt_key *key = osd_it_acct_key(env, di);
380 rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key);
381 RETURN(rc > 0 ? 0 : rc);
385 * Returns cookie for current Iterator position.
387 * \param di - osd iterator
389 static __u64 osd_it_acct_store(const struct lu_env *env,
390 const struct dt_it *di)
392 struct osd_it_quota *it = (struct osd_it_quota *)di;
399 * Restore iterator from cookie. if the \a hash isn't found,
400 * restore the first valid record.
402 * \param di - osd iterator
403 * \param hash - iterator location cookie
405 * \retval +ve - di points to the first valid record
406 * \retval +1 - di points to exact matched hash
407 * \retval -ve - failure
409 static int osd_it_acct_load(const struct lu_env *env,
410 const struct dt_it *di, __u64 hash)
413 RETURN(osd_it_acct_get(env, (struct dt_it *)di,
414 (const struct dt_key *)&hash));
418 * Index and Iterator operations for accounting objects
420 const struct dt_index_operations osd_acct_index_ops = {
421 .dio_lookup = osd_acct_index_lookup,
423 .init = osd_it_acct_init,
424 .fini = osd_it_acct_fini,
425 .get = osd_it_acct_get,
426 .put = osd_it_acct_put,
427 .next = osd_it_acct_next,
428 .key = osd_it_acct_key,
429 .key_size = osd_it_acct_key_size,
430 .rec = osd_it_acct_rec,
431 .store = osd_it_acct_store,
432 .load = osd_it_acct_load
436 static inline void osd_quota_swab(char *ptr, size_t size)
440 LASSERT((size & (sizeof(__u64) - 1)) == 0);
442 for (offset = 0; offset < size; offset += sizeof(__u64))
443 __swab64s((__u64 *)(ptr + offset));
446 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
447 const struct dt_rec *rec,
448 union lquota_rec *quota_rec)
451 struct iam_descr *descr;
453 LASSERT(obj->oo_dir != NULL);
454 descr = obj->oo_dir->od_container.ic_descr;
456 memcpy(quota_rec, rec, descr->id_rec_size);
458 osd_quota_swab((char *)quota_rec, descr->id_rec_size);
459 return (const struct dt_rec *)quota_rec;
465 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
468 struct iam_descr *descr;
470 LASSERT(obj->oo_dir != NULL);
471 descr = obj->oo_dir->od_container.ic_descr;
473 osd_quota_swab((char *)rec, descr->id_rec_size);
479 static inline int osd_qid_type(struct osd_thandle *oh, int i)
481 return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
484 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
486 oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
490 * Reserve journal credits for quota files update first, then call
491 * ->op_begin() to perform quota enforcement.
493 * \param env - the environment passed by the caller
494 * \param oh - osd transaction handle
495 * \param qi - quota id & space required for this operation
496 * \param obj - osd object, could be NULL when it's under create
497 * \param enforce - whether to perform quota enforcement
498 * \param flags - if the operation is write, return no user quota, no
499 * group quota, or sync commit flags to the caller
501 * \retval 0 - success
502 * \retval -ve - failure
504 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
505 struct lquota_id_info *qi, struct osd_object *obj,
506 bool enforce, int *flags)
508 struct osd_device *dev = osd_dt_dev(oh->ot_super.th_dev);
509 struct qsd_instance *qsd = dev->od_quota_slave;
510 struct inode *inode = NULL;
516 LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
519 for (i = 0; i < oh->ot_id_cnt; i++) {
520 if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
521 osd_qid_type(oh, i) == qi->lqi_type) {
528 /* we need to account for credits for this new ID */
529 if (i >= OSD_MAX_UGID_CNT) {
530 CERROR("Too many(%d) trans qids!\n", i + 1);
535 inode = obj->oo_inode;
537 /* root ID entry should be always present in the quota file */
538 if (qi->lqi_id.qid_uid == 0) {
541 /* used space for this ID could be dropped to zero,
542 * reserve extra credits for removing ID entry from
544 if (qi->lqi_space < 0)
545 crd = LDISKFS_QUOTA_DEL_BLOCKS(osd_sb(dev));
546 /* reserve credits for adding ID entry to the quota
547 * file if the i_dquot isn't initialized yet. */
548 else if (inode == NULL ||
549 inode->i_dquot[qi->lqi_type] == NULL)
550 crd = LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
555 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, crd);
557 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
558 osd_qid_set_type(oh, i, qi->lqi_type);
562 if (unlikely(qsd == NULL))
563 /* quota slave instance hasn't been allocated yet */
568 rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
573 * Wrapper for osd_declare_qid()
575 * \param env - the environment passed by the caller
576 * \param uid - user id of the inode
577 * \param gid - group id of the inode
578 * \param space - how many blocks/inodes will be consumed/released
579 * \param oh - osd transaction handle
580 * \param obj - osd object, could be NULL when it's under create
581 * \param is_blk - block quota or inode quota?
582 * \param flags - if the operation is write, return no user quota, no
583 * group quota, or sync commit flags to the caller
584 * \param force - set to 1 when changes are performed by root user and thus
585 * can't failed with EDQUOT
587 * \retval 0 - success
588 * \retval -ve - failure
590 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
591 long long space, struct osd_thandle *oh,
592 struct osd_object *obj, bool is_blk, int *flags,
595 struct osd_thread_info *info = osd_oti_get(env);
596 struct lquota_id_info *qi = &info->oti_qi;
597 int rcu, rcg; /* user & group rc */
600 /* let's start with user quota */
601 qi->lqi_id.qid_uid = uid;
602 qi->lqi_type = USRQUOTA;
603 qi->lqi_space = space;
604 qi->lqi_is_blk = is_blk;
605 rcu = osd_declare_qid(env, oh, qi, obj, true, flags);
607 if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
608 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
611 /* For non-fatal error, we want to continue to get the noquota flags
612 * for group id. This is only for commit write, which has @flags passed
613 * in. See osd_declare_write_commit().
614 * When force is set to true, we also want to proceed with the gid */
615 if (rcu && (rcu != -EDQUOT || flags == NULL))
618 /* and now group quota */
619 qi->lqi_id.qid_gid = gid;
620 qi->lqi_type = GRPQUOTA;
621 rcg = osd_declare_qid(env, oh, qi, obj, true, flags);
623 if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
624 /* as before, ignore EDQUOT & EINPROGRESS for root */
627 RETURN(rcu ? rcu : rcg);
630 int osd_quota_migration(const struct lu_env *env, struct dt_object *dt)
632 struct osd_thread_info *oti = osd_oti_get(env);
633 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
634 struct dt_object *root, *parent = NULL, *admin = NULL;
635 dt_obj_version_t version;
636 char *fname, *fnames[] = {ADMIN_USR, ADMIN_GRP};
640 /* not newly created global index */
641 version = dt_version_get(env, dt);
646 rc = dt_root_get(env, &osd->od_dt_dev, &oti->oti_fid);
648 CERROR("%s: Can't get root FID, rc:%d\n", osd->od_svname, rc);
652 root = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
654 CERROR("%s: Failed to locate root "DFID", rc:%ld\n",
655 osd->od_svname, PFID(&oti->oti_fid), PTR_ERR(root));
656 RETURN(PTR_ERR(root));
659 /* locate /OBJECTS */
660 rc = dt_lookup_dir(env, root, OBJECTS, &oti->oti_fid);
664 CERROR("%s: Failed to lookup %s, rc:%d\n",
665 osd->od_svname, OBJECTS, rc);
669 parent = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
670 if (IS_ERR(parent)) {
671 CERROR("%s: Failed to locate %s "DFID", rc:%ld\n",
672 osd->od_svname, OBJECTS, PFID(&oti->oti_fid),
674 GOTO(out, rc = PTR_ERR(parent));
677 /* locate quota admin files */
678 for (i = 0; i < 2; i++) {
680 rc = dt_lookup_dir(env, parent, fname, &oti->oti_fid);
685 CERROR("%s: Failed to lookup %s, rc:%d\n",
686 osd->od_svname, fname, rc);
690 admin = dt_locate(env, &osd->od_dt_dev, &oti->oti_fid);
692 CERROR("%s: Failed to locate %s "DFID", rc:%d\n",
693 osd->od_svname, fname, PFID(&oti->oti_fid), rc);
694 GOTO(out, rc = PTR_ERR(admin));
697 if (!dt_object_exists(admin)) {
698 CERROR("%s: Old admin file %s doesn't exist, but is "
699 "still referenced in parent directory.\n",
700 osd->od_svname, fname);
701 lu_object_put(env, &admin->do_lu);
702 GOTO(out, rc = -ENOENT);
705 LCONSOLE_WARN("%s: Detected old quota admin file(%s)! If you "
706 "want to keep the old quota limits settings, "
707 "please upgrade to lower version(2.5) first to "
708 "convert them into new format.\n",
709 osd->od_svname, fname);
711 lu_object_put(env, &admin->do_lu);
712 GOTO(out, rc = -EINVAL);
715 if (parent && !IS_ERR(parent))
716 lu_object_put(env, &parent->do_lu);
717 lu_object_put(env, &root->do_lu);