Whamcloud - gitweb
d7e2703c428f104d8ad4b8ddf9460875044289cc
[fs/lustre-release.git] / lustre / quota / lquota_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2011, 2012, Intel, Inc.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34
35 #define DEBUG_SUBSYSTEM S_LQUOTA
36
37 #include <linux/version.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40
41 #include "lquota_internal.h"
42
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");
46
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);
51
52 /**
53  * Look-up accounting object to collect space usage information for user
54  * or group.
55  *
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
59  */
60 struct dt_object *acct_obj_lookup(const struct lu_env *env,
61                                   struct dt_device *dev, int type)
62 {
63         struct lquota_thread_info       *qti = lquota_info(env);
64         struct dt_object                *obj = NULL;
65         ENTRY;
66
67         lu_local_obj_fid(&qti->qti_fid,
68                          type == USRQUOTA ? ACCT_USER_OID : ACCT_GROUP_OID);
69
70         /* lookup the accounting object */
71         obj = dt_locate(env, dev, &qti->qti_fid);
72         if (IS_ERR(obj))
73                 RETURN(obj);
74
75         if (!dt_object_exists(obj)) {
76                 lu_object_put(env, &obj->do_lu);
77                 RETURN(ERR_PTR(-ENOENT));
78         }
79
80         if (obj->do_index_ops == NULL) {
81                 int rc;
82
83                 /* set up indexing operations */
84                 rc = obj->do_ops->do_index_try(env, obj, &dt_acct_features);
85                 if (rc) {
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);
91                         RETURN(ERR_PTR(rc));
92                 }
93         }
94         RETURN(obj);
95 }
96
97 /**
98  * Initialize slave index object to collect local quota limit for user or group.
99  *
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
103  */
104 static struct dt_object *quota_obj_lookup(const struct lu_env *env,
105                                           struct dt_device *dev, int type)
106 {
107         struct lquota_thread_info       *qti = lquota_info(env);
108         struct dt_object                *obj = NULL;
109         ENTRY;
110
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;
114
115         /* lookup the quota object */
116         obj = dt_locate(env, dev, &qti->qti_fid);
117         if (IS_ERR(obj))
118                 RETURN(obj);
119
120         if (!dt_object_exists(obj)) {
121                 lu_object_put(env, &obj->do_lu);
122                 RETURN(ERR_PTR(-ENOENT));
123         }
124
125         if (obj->do_index_ops == NULL) {
126                 int rc;
127
128                 /* set up indexing operations */
129                 rc = obj->do_ops->do_index_try(env, obj,
130                                                &dt_quota_slv_features);
131                 if (rc) {
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);
137                         RETURN(ERR_PTR(rc));
138                 }
139         }
140         RETURN(obj);
141 }
142
143 /*
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.
148  *
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
152  */
153 int lquotactl_slv(const struct lu_env *env, struct dt_device *dev,
154                   struct obd_quotactl *oqctl)
155 {
156         struct lquota_thread_info       *qti = lquota_info(env);
157         __u64                            key;
158         struct dt_object                *obj;
159         struct obd_dqblk                *dqblk = &oqctl->qc_dqblk;
160         int                              rc;
161         ENTRY;
162
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);
168                 RETURN(-EOPNOTSUPP);
169         }
170
171         if (oqctl->qc_type != USRQUOTA && oqctl->qc_type != GRPQUOTA)
172                 /* no support for directory quota yet */
173                 RETURN(-EOPNOTSUPP);
174
175         /* qc_id is a 32-bit field while a key has 64 bits */
176         key = oqctl->qc_id;
177
178         /* Step 1: collect accounting information */
179
180         obj = acct_obj_lookup(env, dev, oqctl->qc_type);
181         if (IS_ERR(obj))
182                 RETURN(-EOPNOTSUPP);
183         if (obj->do_index_ops == NULL)
184                 GOTO(out, rc = -EINVAL);
185
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);
189         if (rc < 0)
190                 GOTO(out, rc);
191
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;
196
197         lu_object_put(env, &obj->do_lu);
198
199         /* Step 2: collect enforcement information */
200
201         obj = quota_obj_lookup(env, dev, oqctl->qc_type);
202         if (IS_ERR(obj))
203                 RETURN(0);
204         if (obj->do_index_ops == NULL)
205                 GOTO(out, rc = 0);
206
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)
212                 GOTO(out, rc = 0);
213
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;
217         } else {
218                 dqblk->dqb_ihardlimit = 0;
219                 dqblk->dqb_bhardlimit = qti->qti_slv_rec.qsr_granted;
220         }
221         dqblk->dqb_valid |= QIF_LIMITS;
222
223         GOTO(out, rc = 0);
224 out:
225         lu_object_put(env, &obj->do_lu);
226         return rc;
227 }
228 EXPORT_SYMBOL(lquotactl_slv);
229
230 /**
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.
234  */
235 void lquota_generate_fid(struct lu_fid *fid, int pool_id, int pool_type,
236                          int quota_type)
237 {
238         __u8     qtype;
239
240         qtype = (quota_type == USRQUOTA) ? LQUOTA_TYPE_USR : LQUOTA_TYPE_GRP;
241
242         fid->f_seq = FID_SEQ_QUOTA_GLB;
243         fid->f_oid = (qtype << 24) | (pool_type << 16) | (__u16)pool_id;
244         fid->f_ver = 0;
245 }
246
247 /**
248  * Helper routine used to extract pool ID, pool type and quota type from a
249  * given FID.
250  */
251 int lquota_extract_fid(struct lu_fid *fid, int *pool_id, int *pool_type,
252                        int *quota_type)
253 {
254         unsigned int     tmp;
255         ENTRY;
256
257         if (fid->f_seq != FID_SEQ_QUOTA_GLB)
258                 RETURN(-EINVAL);
259
260         if (pool_id != NULL) {
261                 tmp = fid->f_oid & 0xffffU;
262                 if (tmp != 0)
263                         /* we only support pool ID 0 for the time being */
264                         RETURN(-ENOTSUPP);
265                 *pool_id = tmp;
266         }
267
268         if (pool_type != NULL) {
269                 tmp = (fid->f_oid >> 16) & 0xffU;
270                 if (tmp >= LQUOTA_LAST_RES)
271                         RETURN(-ENOTSUPP);
272
273                 *pool_type = tmp;
274         }
275
276         if (quota_type != NULL) {
277                 tmp = fid->f_oid >> 24;
278                 if (tmp >= LQUOTA_TYPE_MAX)
279                         RETURN(-ENOTSUPP);
280
281                 *quota_type = (tmp == LQUOTA_TYPE_USR) ? USRQUOTA : GRPQUOTA;
282         }
283
284         RETURN(0);
285 }
286
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
292  * format */
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);
301
302 /**
303  * Helper routine returning the right index feature structure to be used
304  * depending on the FID of the global index.
305  */
306 const struct dt_index_features *glb_idx_feature(struct lu_fid *fid)
307 {
308         int     res_type, quota_type, rc;
309
310         rc = lquota_extract_fid(fid, NULL, &res_type, &quota_type);
311         if (rc)
312                 return ERR_PTR(rc);
313
314         if (quota_type == USRQUOTA) {
315                 if (res_type == LQUOTA_RES_MD)
316                         return &dt_quota_iusr_features;
317                 else
318                         return &dt_quota_busr_features;
319         } else {
320                 if (res_type == LQUOTA_RES_MD)
321                         return &dt_quota_igrp_features;
322                 else
323                         return &dt_quota_bgrp_features;
324         }
325 }
326 #else
327 #warning "remove old quota compatibility code"
328 #endif
329
330 static int __init init_lquota(void)
331 {
332         /* new quota initialization */
333         lquota_key_init_generic(&lquota_thread_key, NULL);
334         lu_context_key_register(&lquota_thread_key);
335
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;
339 #else
340 #warning "remove old quota compatibility code"
341 #endif
342
343         return 0;
344 }
345
346 static void exit_lquota(void)
347 {
348         lu_context_key_degister(&lquota_thread_key);
349 }
350
351 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
352 MODULE_DESCRIPTION("Lustre Quota");
353 MODULE_LICENSE("GPL");
354
355 cfs_module(lquota, "2.4.0", init_lquota, exit_lquota);