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) 2011, 2012, Intel, Inc.
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 static int hash_lqs_cur_bits = HASH_LQS_CUR_BITS;
44 CFS_MODULE_PARM(hash_lqs_cur_bits, "i", int, 0444,
45 "the current bits of lqs hash");
47 /* register lquota key */
48 LU_KEY_INIT_FINI(lquota, struct lquota_thread_info);
49 LU_CONTEXT_KEY_DEFINE(lquota, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
50 LU_KEY_INIT_GENERIC(lquota);
53 * Look-up accounting object to collect space usage information for user
56 * \param env - is the environment passed by the caller
57 * \param dev - is the dt_device storing the accounting object
58 * \param type - is the quota type, either USRQUOTA or GRPQUOTA
60 struct dt_object *acct_obj_lookup(const struct lu_env *env,
61 struct dt_device *dev, int type)
63 struct lquota_thread_info *qti = lquota_info(env);
64 struct dt_object *obj = NULL;
67 lu_local_obj_fid(&qti->qti_fid,
68 type == USRQUOTA ? ACCT_USER_OID : ACCT_GROUP_OID);
70 /* lookup the accounting object */
71 obj = dt_locate(env, dev, &qti->qti_fid);
75 if (!dt_object_exists(obj)) {
76 lu_object_put(env, &obj->do_lu);
77 RETURN(ERR_PTR(-ENOENT));
80 if (obj->do_index_ops == NULL) {
83 /* set up indexing operations */
84 rc = obj->do_ops->do_index_try(env, obj, &dt_acct_features);
86 CERROR("%s: failed to set up indexing operations for %s"
87 " acct object rc:%d\n",
88 dev->dd_lu_dev.ld_obd->obd_name,
89 QTYPE_NAME(type), rc);
90 lu_object_put(env, &obj->do_lu);
98 * Initialize slave index object to collect local quota limit for user or group.
100 * \param env - is the environment passed by the caller
101 * \param dev - is the dt_device storing the slave index object
102 * \param type - is the quota type, either USRQUOTA or GRPQUOTA
104 static struct dt_object *quota_obj_lookup(const struct lu_env *env,
105 struct dt_device *dev, int type)
107 struct lquota_thread_info *qti = lquota_info(env);
108 struct dt_object *obj = NULL;
111 qti->qti_fid.f_seq = FID_SEQ_QUOTA;
112 qti->qti_fid.f_oid = type == USRQUOTA ? LQUOTA_USR_OID : LQUOTA_GRP_OID;
113 qti->qti_fid.f_ver = 0;
115 /* lookup the quota object */
116 obj = dt_locate(env, dev, &qti->qti_fid);
120 if (!dt_object_exists(obj)) {
121 lu_object_put(env, &obj->do_lu);
122 RETURN(ERR_PTR(-ENOENT));
125 if (obj->do_index_ops == NULL) {
128 /* set up indexing operations */
129 rc = obj->do_ops->do_index_try(env, obj,
130 &dt_quota_slv_features);
132 CERROR("%s: failed to set up indexing operations for %s"
133 " slave index object rc:%d\n",
134 dev->dd_lu_dev.ld_obd->obd_name,
135 QTYPE_NAME(type), rc);
136 lu_object_put(env, &obj->do_lu);
144 * Helper routine to retrieve slave information.
145 * This function converts a quotactl request into quota/accounting object
146 * operations. It is independant of the slave stack which is only accessible
147 * from the OSD layer.
149 * \param env - is the environment passed by the caller
150 * \param dev - is the dt_device this quotactl is executed on
151 * \param oqctl - is the quotactl request
153 int lquotactl_slv(const struct lu_env *env, struct dt_device *dev,
154 struct obd_quotactl *oqctl)
156 struct lquota_thread_info *qti = lquota_info(env);
158 struct dt_object *obj;
159 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
163 if (oqctl->qc_cmd != Q_GETOQUOTA) {
164 /* as in many other places, dev->dd_lu_dev.ld_obd->obd_name
165 * point to an invalid obd_name, to be fixed in LU-1574 */
166 CERROR("%s: Unsupported quotactl command: %x\n",
167 dev->dd_lu_dev.ld_obd->obd_name, oqctl->qc_cmd);
171 if (oqctl->qc_type != USRQUOTA && oqctl->qc_type != GRPQUOTA)
172 /* no support for directory quota yet */
175 /* qc_id is a 32-bit field while a key has 64 bits */
178 /* Step 1: collect accounting information */
180 obj = acct_obj_lookup(env, dev, oqctl->qc_type);
183 if (obj->do_index_ops == NULL)
184 GOTO(out, rc = -EINVAL);
186 /* lookup record storing space accounting information for this ID */
187 rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_acct_rec,
188 (struct dt_key *)&key, BYPASS_CAPA);
192 memset(&oqctl->qc_dqblk, 0, sizeof(struct obd_dqblk));
193 dqblk->dqb_curspace = qti->qti_acct_rec.bspace;
194 dqblk->dqb_curinodes = qti->qti_acct_rec.ispace;
195 dqblk->dqb_valid = QIF_USAGE;
197 lu_object_put(env, &obj->do_lu);
199 /* Step 2: collect enforcement information */
201 obj = quota_obj_lookup(env, dev, oqctl->qc_type);
204 if (obj->do_index_ops == NULL)
207 memset(&qti->qti_slv_rec, 0, sizeof(qti->qti_slv_rec));
208 /* lookup record storing enforcement information for this ID */
209 rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_slv_rec,
210 (struct dt_key *)&key, BYPASS_CAPA);
211 if (rc < 0 && rc != -ENOENT)
214 if (lu_device_is_md(dev->dd_lu_dev.ld_site->ls_top_dev)) {
215 dqblk->dqb_ihardlimit = qti->qti_slv_rec.qsr_granted;
216 dqblk->dqb_bhardlimit = 0;
218 dqblk->dqb_ihardlimit = 0;
219 dqblk->dqb_bhardlimit = qti->qti_slv_rec.qsr_granted;
221 dqblk->dqb_valid |= QIF_LIMITS;
225 lu_object_put(env, &obj->do_lu);
228 EXPORT_SYMBOL(lquotactl_slv);
231 * Helper routine returning the FID associated with the global index storing
232 * quota settings for the storage pool \pool_id, resource type \pool_type and
233 * the quota type \quota_type.
235 void lquota_generate_fid(struct lu_fid *fid, int pool_id, int pool_type,
240 qtype = (quota_type == USRQUOTA) ? LQUOTA_TYPE_USR : LQUOTA_TYPE_GRP;
242 fid->f_seq = FID_SEQ_QUOTA_GLB;
243 fid->f_oid = (qtype << 24) | (pool_type << 16) | (__u16)pool_id;
248 * Helper routine used to extract pool ID, pool type and quota type from a
251 int lquota_extract_fid(struct lu_fid *fid, int *pool_id, int *pool_type,
257 if (fid->f_seq != FID_SEQ_QUOTA_GLB)
260 if (pool_id != NULL) {
261 tmp = fid->f_oid & 0xffffU;
263 /* we only support pool ID 0 for the time being */
268 if (pool_type != NULL) {
269 tmp = (fid->f_oid >> 16) & 0xffU;
270 if (tmp >= LQUOTA_LAST_RES)
276 if (quota_type != NULL) {
277 tmp = fid->f_oid >> 24;
278 if (tmp >= LQUOTA_TYPE_MAX)
281 *quota_type = (tmp == LQUOTA_TYPE_USR) ? USRQUOTA : GRPQUOTA;
287 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
288 /* Index features supported by the global index objects.
289 * We actually use one dt_index_features structure for each quota combination
290 * of quota type x [inode, block] to allow the ldiskfs OSD to recognize those
291 * objects and to handle the conversion from the old administrative quota file
293 struct dt_index_features dt_quota_iusr_features;
294 EXPORT_SYMBOL(dt_quota_iusr_features);
295 struct dt_index_features dt_quota_busr_features;
296 EXPORT_SYMBOL(dt_quota_busr_features);
297 struct dt_index_features dt_quota_igrp_features;
298 EXPORT_SYMBOL(dt_quota_igrp_features);
299 struct dt_index_features dt_quota_bgrp_features;
300 EXPORT_SYMBOL(dt_quota_bgrp_features);
303 * Helper routine returning the right index feature structure to be used
304 * depending on the FID of the global index.
306 const struct dt_index_features *glb_idx_feature(struct lu_fid *fid)
308 int res_type, quota_type, rc;
310 rc = lquota_extract_fid(fid, NULL, &res_type, "a_type);
314 if (quota_type == USRQUOTA) {
315 if (res_type == LQUOTA_RES_MD)
316 return &dt_quota_iusr_features;
318 return &dt_quota_busr_features;
320 if (res_type == LQUOTA_RES_MD)
321 return &dt_quota_igrp_features;
323 return &dt_quota_bgrp_features;
327 #warning "remove old quota compatibility code"
330 static int __init init_lquota(void)
332 /* new quota initialization */
333 lquota_key_init_generic(&lquota_thread_key, NULL);
334 lu_context_key_register(&lquota_thread_key);
336 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
337 dt_quota_iusr_features = dt_quota_busr_features = dt_quota_glb_features;
338 dt_quota_igrp_features = dt_quota_bgrp_features = dt_quota_glb_features;
340 #warning "remove old quota compatibility code"
346 static void exit_lquota(void)
348 lu_context_key_degister(&lquota_thread_key);
351 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
352 MODULE_DESCRIPTION("Lustre Quota");
353 MODULE_LICENSE("GPL");
355 cfs_module(lquota, "2.4.0", init_lquota, exit_lquota);