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 /* register lquota key */
44 LU_KEY_INIT_FINI(lquota, struct lquota_thread_info);
45 LU_CONTEXT_KEY_DEFINE(lquota, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
46 LU_KEY_INIT_GENERIC(lquota);
49 * Look-up accounting object to collect space usage information for user
52 * \param env - is the environment passed by the caller
53 * \param dev - is the dt_device storing the accounting object
54 * \param type - is the quota type, either USRQUOTA or GRPQUOTA
56 struct dt_object *acct_obj_lookup(const struct lu_env *env,
57 struct dt_device *dev, int type)
59 struct lquota_thread_info *qti = lquota_info(env);
60 struct dt_object *obj = NULL;
63 lu_local_obj_fid(&qti->qti_fid,
64 type == USRQUOTA ? ACCT_USER_OID : ACCT_GROUP_OID);
66 /* lookup the accounting object */
67 obj = dt_locate(env, dev, &qti->qti_fid);
71 if (!dt_object_exists(obj)) {
72 lu_object_put(env, &obj->do_lu);
73 RETURN(ERR_PTR(-ENOENT));
76 if (obj->do_index_ops == NULL) {
79 /* set up indexing operations */
80 rc = obj->do_ops->do_index_try(env, obj, &dt_acct_features);
82 CERROR("%s: failed to set up indexing operations for %s"
83 " acct object rc:%d\n",
84 dev->dd_lu_dev.ld_obd->obd_name,
85 QTYPE_NAME(type), rc);
86 lu_object_put(env, &obj->do_lu);
94 * Initialize slave index object to collect local quota limit for user or group.
96 * \param env - is the environment passed by the caller
97 * \param dev - is the dt_device storing the slave index object
98 * \param type - is the quota type, either USRQUOTA or GRPQUOTA
100 static struct dt_object *quota_obj_lookup(const struct lu_env *env,
101 struct dt_device *dev, int type)
103 struct lquota_thread_info *qti = lquota_info(env);
104 struct dt_object *obj = NULL;
107 qti->qti_fid.f_seq = FID_SEQ_QUOTA;
108 qti->qti_fid.f_oid = type == USRQUOTA ? LQUOTA_USR_OID : LQUOTA_GRP_OID;
109 qti->qti_fid.f_ver = 0;
111 /* lookup the quota object */
112 obj = dt_locate(env, dev, &qti->qti_fid);
116 if (!dt_object_exists(obj)) {
117 lu_object_put(env, &obj->do_lu);
118 RETURN(ERR_PTR(-ENOENT));
121 if (obj->do_index_ops == NULL) {
124 /* set up indexing operations */
125 rc = obj->do_ops->do_index_try(env, obj,
126 &dt_quota_slv_features);
128 CERROR("%s: failed to set up indexing operations for %s"
129 " slave index object rc:%d\n",
130 dev->dd_lu_dev.ld_obd->obd_name,
131 QTYPE_NAME(type), rc);
132 lu_object_put(env, &obj->do_lu);
140 * Helper routine to retrieve slave information.
141 * This function converts a quotactl request into quota/accounting object
142 * operations. It is independant of the slave stack which is only accessible
143 * from the OSD layer.
145 * \param env - is the environment passed by the caller
146 * \param dev - is the dt_device this quotactl is executed on
147 * \param oqctl - is the quotactl request
149 int lquotactl_slv(const struct lu_env *env, struct dt_device *dev,
150 struct obd_quotactl *oqctl)
152 struct lquota_thread_info *qti = lquota_info(env);
154 struct dt_object *obj;
155 struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
159 if (oqctl->qc_cmd != Q_GETOQUOTA) {
160 /* as in many other places, dev->dd_lu_dev.ld_obd->obd_name
161 * point to an invalid obd_name, to be fixed in LU-1574 */
162 CERROR("%s: Unsupported quotactl command: %x\n",
163 dev->dd_lu_dev.ld_obd->obd_name, oqctl->qc_cmd);
167 if (oqctl->qc_type != USRQUOTA && oqctl->qc_type != GRPQUOTA)
168 /* no support for directory quota yet */
171 /* qc_id is a 32-bit field while a key has 64 bits */
174 /* Step 1: collect accounting information */
176 obj = acct_obj_lookup(env, dev, oqctl->qc_type);
179 if (obj->do_index_ops == NULL)
180 GOTO(out, rc = -EINVAL);
182 /* lookup record storing space accounting information for this ID */
183 rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_acct_rec,
184 (struct dt_key *)&key, BYPASS_CAPA);
188 memset(&oqctl->qc_dqblk, 0, sizeof(struct obd_dqblk));
189 dqblk->dqb_curspace = qti->qti_acct_rec.bspace;
190 dqblk->dqb_curinodes = qti->qti_acct_rec.ispace;
191 dqblk->dqb_valid = QIF_USAGE;
193 lu_object_put(env, &obj->do_lu);
195 /* Step 2: collect enforcement information */
197 obj = quota_obj_lookup(env, dev, oqctl->qc_type);
200 if (obj->do_index_ops == NULL)
203 memset(&qti->qti_slv_rec, 0, sizeof(qti->qti_slv_rec));
204 /* lookup record storing enforcement information for this ID */
205 rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_slv_rec,
206 (struct dt_key *)&key, BYPASS_CAPA);
207 if (rc < 0 && rc != -ENOENT)
210 if (lu_device_is_md(dev->dd_lu_dev.ld_site->ls_top_dev)) {
211 dqblk->dqb_ihardlimit = qti->qti_slv_rec.qsr_granted;
212 dqblk->dqb_bhardlimit = 0;
214 dqblk->dqb_ihardlimit = 0;
215 dqblk->dqb_bhardlimit = qti->qti_slv_rec.qsr_granted;
217 dqblk->dqb_valid |= QIF_LIMITS;
221 lu_object_put(env, &obj->do_lu);
224 EXPORT_SYMBOL(lquotactl_slv);
227 * Helper routine returning the FID associated with the global index storing
228 * quota settings for the storage pool \pool_id, resource type \pool_type and
229 * the quota type \quota_type.
231 void lquota_generate_fid(struct lu_fid *fid, int pool_id, int pool_type,
236 qtype = (quota_type == USRQUOTA) ? LQUOTA_TYPE_USR : LQUOTA_TYPE_GRP;
238 fid->f_seq = FID_SEQ_QUOTA_GLB;
239 fid->f_oid = (qtype << 24) | (pool_type << 16) | (__u16)pool_id;
244 * Helper routine used to extract pool ID, pool type and quota type from a
247 int lquota_extract_fid(struct lu_fid *fid, int *pool_id, int *pool_type,
253 if (fid->f_seq != FID_SEQ_QUOTA_GLB)
256 if (pool_id != NULL) {
257 tmp = fid->f_oid & 0xffffU;
259 /* we only support pool ID 0 for the time being */
264 if (pool_type != NULL) {
265 tmp = (fid->f_oid >> 16) & 0xffU;
266 if (tmp >= LQUOTA_LAST_RES)
272 if (quota_type != NULL) {
273 tmp = fid->f_oid >> 24;
274 if (tmp >= LQUOTA_TYPE_MAX)
277 *quota_type = (tmp == LQUOTA_TYPE_USR) ? USRQUOTA : GRPQUOTA;
283 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
284 /* Index features supported by the global index objects.
285 * We actually use one dt_index_features structure for each quota combination
286 * of quota type x [inode, block] to allow the ldiskfs OSD to recognize those
287 * objects and to handle the conversion from the old administrative quota file
289 struct dt_index_features dt_quota_iusr_features;
290 EXPORT_SYMBOL(dt_quota_iusr_features);
291 struct dt_index_features dt_quota_busr_features;
292 EXPORT_SYMBOL(dt_quota_busr_features);
293 struct dt_index_features dt_quota_igrp_features;
294 EXPORT_SYMBOL(dt_quota_igrp_features);
295 struct dt_index_features dt_quota_bgrp_features;
296 EXPORT_SYMBOL(dt_quota_bgrp_features);
299 * Helper routine returning the right index feature structure to be used
300 * depending on the FID of the global index.
302 const struct dt_index_features *glb_idx_feature(struct lu_fid *fid)
304 int res_type, quota_type, rc;
306 rc = lquota_extract_fid(fid, NULL, &res_type, "a_type);
310 if (quota_type == USRQUOTA) {
311 if (res_type == LQUOTA_RES_MD)
312 return &dt_quota_iusr_features;
314 return &dt_quota_busr_features;
316 if (res_type == LQUOTA_RES_MD)
317 return &dt_quota_igrp_features;
319 return &dt_quota_bgrp_features;
323 #warning "remove old quota compatibility code"
326 static int __init init_lquota(void)
328 /* new quota initialization */
329 lquota_key_init_generic(&lquota_thread_key, NULL);
330 lu_context_key_register(&lquota_thread_key);
332 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2,7,50,0)
333 dt_quota_iusr_features = dt_quota_busr_features = dt_quota_glb_features;
334 dt_quota_igrp_features = dt_quota_bgrp_features = dt_quota_glb_features;
336 #warning "remove old quota compatibility code"
342 static void exit_lquota(void)
344 lu_context_key_degister(&lquota_thread_key);
347 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
348 MODULE_DESCRIPTION("Lustre Quota");
349 MODULE_LICENSE("GPL");
351 cfs_module(lquota, "2.4.0", init_lquota, exit_lquota);