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.lombardi@intel.com>
28 * Author: Niu Yawei <yawei.niu@intel.com>
32 # define EXPORT_SYMTAB
35 #define DEBUG_SUBSYSTEM S_LQUOTA
37 #include <linux/version.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
41 #include "lquota_internal.h"
43 cfs_mem_cache_t *lqe_kmem;
45 struct lu_kmem_descr lquota_caches[] = {
47 .ckd_cache = &lqe_kmem,
48 .ckd_name = "lqe_kmem",
49 .ckd_size = sizeof(struct lquota_entry)
56 /* register lquota key */
57 LU_KEY_INIT_FINI(lquota, struct lquota_thread_info);
58 LU_CONTEXT_KEY_DEFINE(lquota, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
59 LU_KEY_INIT_GENERIC(lquota);
62 * Look-up accounting object to collect space usage information for user
65 * \param env - is the environment passed by the caller
66 * \param dev - is the dt_device storing the accounting object
67 * \param type - is the quota type, either USRQUOTA or GRPQUOTA
69 struct dt_object *acct_obj_lookup(const struct lu_env *env,
70 struct dt_device *dev, int type)
72 struct lquota_thread_info *qti = lquota_info(env);
73 struct dt_object *obj = NULL;
76 lu_local_obj_fid(&qti->qti_fid,
77 type == USRQUOTA ? ACCT_USER_OID : ACCT_GROUP_OID);
79 /* lookup the accounting object */
80 obj = dt_locate(env, dev, &qti->qti_fid);
84 if (!dt_object_exists(obj)) {
85 lu_object_put(env, &obj->do_lu);
86 RETURN(ERR_PTR(-ENOENT));
89 if (obj->do_index_ops == NULL) {
92 /* set up indexing operations */
93 rc = obj->do_ops->do_index_try(env, obj, &dt_acct_features);
95 CERROR("%s: failed to set up indexing operations for %s"
96 " acct object rc:%d\n",
97 dev->dd_lu_dev.ld_obd->obd_name,
98 QTYPE_NAME(type), rc);
99 lu_object_put(env, &obj->do_lu);
107 * Initialize slave index object to collect local quota limit for user or group.
109 * \param env - is the environment passed by the caller
110 * \param dev - is the dt_device storing the slave index object
111 * \param type - is the quota type, either USRQUOTA or GRPQUOTA
113 static struct dt_object *quota_obj_lookup(const struct lu_env *env,
114 struct dt_device *dev, int type)
116 struct lquota_thread_info *qti = lquota_info(env);
117 struct dt_object *obj = NULL;
120 qti->qti_fid.f_seq = FID_SEQ_QUOTA;
121 qti->qti_fid.f_oid = type == USRQUOTA ? LQUOTA_USR_OID : LQUOTA_GRP_OID;
122 qti->qti_fid.f_ver = 0;
124 /* lookup the quota object */
125 obj = dt_locate(env, dev, &qti->qti_fid);
129 if (!dt_object_exists(obj)) {
130 lu_object_put(env, &obj->do_lu);
131 RETURN(ERR_PTR(-ENOENT));
134 if (obj->do_index_ops == NULL) {
137 /* set up indexing operations */
138 rc = obj->do_ops->do_index_try(env, obj,
139 &dt_quota_slv_features);
141 CERROR("%s: failed to set up indexing operations for %s"
142 " slave index object rc:%d\n",
143 dev->dd_lu_dev.ld_obd->obd_name,
144 QTYPE_NAME(type), rc);
145 lu_object_put(env, &obj->do_lu);
153 * Helper routine to retrieve slave information.
154 * This function converts a quotactl request into quota/accounting object
155 * operations. It is independant of the slave stack which is only accessible
156 * from the OSD layer.
158 * \param env - is the environment passed by the caller
159 * \param dev - is the dt_device this quotactl is executed on
160 * \param oqctl - is the quotactl request
162 int lquotactl_slv(const struct lu_env *env, struct dt_device *dev,
163 struct obd_quotactl *oqctl)
165 struct lquota_thread_info *qti = lquota_info(env);
167 struct dt_object *obj;
168 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
172 if (oqctl->qc_cmd != Q_GETOQUOTA) {
173 /* as in many other places, dev->dd_lu_dev.ld_obd->obd_name
174 * point to an invalid obd_name, to be fixed in LU-1574 */
175 CERROR("%s: Unsupported quotactl command: %x\n",
176 dev->dd_lu_dev.ld_obd->obd_name, oqctl->qc_cmd);
180 if (oqctl->qc_type != USRQUOTA && oqctl->qc_type != GRPQUOTA)
181 /* no support for directory quota yet */
184 /* qc_id is a 32-bit field while a key has 64 bits */
187 /* Step 1: collect accounting information */
189 obj = acct_obj_lookup(env, dev, oqctl->qc_type);
192 if (obj->do_index_ops == NULL)
193 GOTO(out, rc = -EINVAL);
195 /* lookup record storing space accounting information for this ID */
196 rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_acct_rec,
197 (struct dt_key *)&key, BYPASS_CAPA);
201 memset(&oqctl->qc_dqblk, 0, sizeof(struct obd_dqblk));
202 dqblk->dqb_curspace = qti->qti_acct_rec.bspace;
203 dqblk->dqb_curinodes = qti->qti_acct_rec.ispace;
204 dqblk->dqb_valid = QIF_USAGE;
206 lu_object_put(env, &obj->do_lu);
208 /* Step 2: collect enforcement information */
210 obj = quota_obj_lookup(env, dev, oqctl->qc_type);
213 if (obj->do_index_ops == NULL)
216 memset(&qti->qti_slv_rec, 0, sizeof(qti->qti_slv_rec));
217 /* lookup record storing enforcement information for this ID */
218 rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_slv_rec,
219 (struct dt_key *)&key, BYPASS_CAPA);
220 if (rc < 0 && rc != -ENOENT)
223 if (lu_device_is_md(dev->dd_lu_dev.ld_site->ls_top_dev)) {
224 dqblk->dqb_ihardlimit = qti->qti_slv_rec.qsr_granted;
225 dqblk->dqb_bhardlimit = 0;
227 dqblk->dqb_ihardlimit = 0;
228 dqblk->dqb_bhardlimit = qti->qti_slv_rec.qsr_granted;
230 dqblk->dqb_valid |= QIF_LIMITS;
234 lu_object_put(env, &obj->do_lu);
237 EXPORT_SYMBOL(lquotactl_slv);
240 * Helper routine returning the FID associated with the global index storing
241 * quota settings for the storage pool \pool_id, resource type \pool_type and
242 * the quota type \quota_type.
244 void lquota_generate_fid(struct lu_fid *fid, int pool_id, int pool_type,
249 qtype = (quota_type == USRQUOTA) ? LQUOTA_TYPE_USR : LQUOTA_TYPE_GRP;
251 fid->f_seq = FID_SEQ_QUOTA_GLB;
252 fid->f_oid = (qtype << 24) | (pool_type << 16) | (__u16)pool_id;
257 * Helper routine used to extract pool ID, pool type and quota type from a
260 int lquota_extract_fid(const struct lu_fid *fid, int *pool_id, int *pool_type,
266 if (fid->f_seq != FID_SEQ_QUOTA_GLB)
269 if (pool_id != NULL) {
270 tmp = fid->f_oid & 0xffffU;
272 /* we only support pool ID 0 for the time being */
277 if (pool_type != NULL) {
278 tmp = (fid->f_oid >> 16) & 0xffU;
279 if (tmp >= LQUOTA_LAST_RES)
285 if (quota_type != NULL) {
286 tmp = fid->f_oid >> 24;
287 if (tmp >= LQUOTA_TYPE_MAX)
290 *quota_type = (tmp == LQUOTA_TYPE_USR) ? USRQUOTA : GRPQUOTA;
296 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
297 /* Index features supported by the global index objects.
298 * We actually use one dt_index_features structure for each quota combination
299 * of quota type x [inode, block] to allow the ldiskfs OSD to recognize those
300 * objects and to handle the conversion from the old administrative quota file
302 struct dt_index_features dt_quota_iusr_features;
303 EXPORT_SYMBOL(dt_quota_iusr_features);
304 struct dt_index_features dt_quota_busr_features;
305 EXPORT_SYMBOL(dt_quota_busr_features);
306 struct dt_index_features dt_quota_igrp_features;
307 EXPORT_SYMBOL(dt_quota_igrp_features);
308 struct dt_index_features dt_quota_bgrp_features;
309 EXPORT_SYMBOL(dt_quota_bgrp_features);
312 * Helper routine returning the right index feature structure to be used
313 * depending on the FID of the global index.
315 const struct dt_index_features *glb_idx_feature(struct lu_fid *fid)
317 int res_type, quota_type, rc;
319 rc = lquota_extract_fid(fid, NULL, &res_type, "a_type);
323 if (quota_type == USRQUOTA) {
324 if (res_type == LQUOTA_RES_MD)
325 return &dt_quota_iusr_features;
327 return &dt_quota_busr_features;
329 if (res_type == LQUOTA_RES_MD)
330 return &dt_quota_igrp_features;
332 return &dt_quota_bgrp_features;
336 #warning "remove old quota compatibility code"
339 static int __init init_lquota(void)
344 lquota_key_init_generic(&lquota_thread_key, NULL);
345 lu_context_key_register(&lquota_thread_key);
347 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
348 dt_quota_iusr_features = dt_quota_busr_features = dt_quota_glb_features;
349 dt_quota_igrp_features = dt_quota_bgrp_features = dt_quota_glb_features;
351 #warning "remove old quota compatibility code"
354 rc = lu_kmem_init(lquota_caches);
360 GOTO(out_caches, rc);
371 lu_kmem_fini(lquota_caches);
373 lu_context_key_degister(&lquota_thread_key);
377 static void exit_lquota(void)
381 lu_kmem_fini(lquota_caches);
382 lu_context_key_degister(&lquota_thread_key);
385 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
386 MODULE_DESCRIPTION("Lustre Quota");
387 MODULE_LICENSE("GPL");
389 cfs_module(lquota, "2.4.0", init_lquota, exit_lquota);