Whamcloud - gitweb
LU-4017 quota: cleanup codes of quota for new type
[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) 2012, 2015, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_LQUOTA
32
33 #include <linux/version.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36
37 #include "lquota_internal.h"
38
39 struct kmem_cache *lqe_kmem;
40
41 struct lu_kmem_descr lquota_caches[] = {
42         {
43                 .ckd_cache = &lqe_kmem,
44                 .ckd_name  = "lqe_kmem",
45                 .ckd_size  = sizeof(struct lquota_entry)
46         },
47         {
48                 .ckd_cache = NULL
49         }
50 };
51
52 /* register lquota key */
53 LU_KEY_INIT_FINI(lquota, struct lquota_thread_info);
54 LU_CONTEXT_KEY_DEFINE(lquota, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
55 LU_KEY_INIT_GENERIC(lquota);
56
57 static inline __u32 qtype2acct_oid(int qtype)
58 {
59         switch (qtype) {
60         case USRQUOTA:
61                 return ACCT_USER_OID;
62         case GRPQUOTA:
63                 return ACCT_GROUP_OID;
64         }
65
66         return ACCT_GROUP_OID;
67 }
68
69 /**
70  * Look-up accounting object to collect space usage information for user
71  * or group.
72  *
73  * \param env  - is the environment passed by the caller
74  * \param dev  - is the dt_device storing the accounting object
75  * \param type - is the quota type, either USRQUOTA or GRPQUOTA
76  */
77 struct dt_object *acct_obj_lookup(const struct lu_env *env,
78                                   struct dt_device *dev, int type)
79 {
80         struct lquota_thread_info       *qti = lquota_info(env);
81         struct dt_object                *obj = NULL;
82         ENTRY;
83
84         lu_local_obj_fid(&qti->qti_fid, qtype2acct_oid(type));
85
86         /* lookup the accounting object */
87         obj = dt_locate(env, dev, &qti->qti_fid);
88         if (IS_ERR(obj))
89                 RETURN(obj);
90
91         if (!dt_object_exists(obj)) {
92                 dt_object_put(env, obj);
93                 RETURN(ERR_PTR(-ENOENT));
94         }
95
96         if (obj->do_index_ops == NULL) {
97                 int rc;
98
99                 /* set up indexing operations */
100                 rc = obj->do_ops->do_index_try(env, obj, &dt_acct_features);
101                 if (rc) {
102                         CERROR("%s: failed to set up indexing operations for %s"
103                                " acct object rc:%d\n",
104                                dev->dd_lu_dev.ld_obd->obd_name,
105                                qtype_name(type), rc);
106                         dt_object_put(env, obj);
107                         RETURN(ERR_PTR(rc));
108                 }
109         }
110         RETURN(obj);
111 }
112
113 /**
114  * Initialize slave index object to collect local quota limit for user or group.
115  *
116  * \param env - is the environment passed by the caller
117  * \param dev - is the dt_device storing the slave index object
118  * \param type - is the quota type, either USRQUOTA or GRPQUOTA
119  */
120 static struct dt_object *quota_obj_lookup(const struct lu_env *env,
121                                           struct dt_device *dev, int type)
122 {
123         struct lquota_thread_info       *qti = lquota_info(env);
124         struct dt_object                *obj = NULL;
125         ENTRY;
126
127         qti->qti_fid.f_seq = FID_SEQ_QUOTA;
128         qti->qti_fid.f_oid = qtype2slv_oid(type);
129         qti->qti_fid.f_ver = 0;
130
131         /* lookup the quota object */
132         obj = dt_locate(env, dev, &qti->qti_fid);
133         if (IS_ERR(obj))
134                 RETURN(obj);
135
136         if (!dt_object_exists(obj)) {
137                 dt_object_put(env, obj);
138                 RETURN(ERR_PTR(-ENOENT));
139         }
140
141         if (obj->do_index_ops == NULL) {
142                 int rc;
143
144                 /* set up indexing operations */
145                 rc = obj->do_ops->do_index_try(env, obj,
146                                                &dt_quota_slv_features);
147                 if (rc) {
148                         CERROR("%s: failed to set up indexing operations for %s"
149                                " slave index object rc:%d\n",
150                                dev->dd_lu_dev.ld_obd->obd_name,
151                                qtype_name(type), rc);
152                         dt_object_put(env, obj);
153                         RETURN(ERR_PTR(rc));
154                 }
155         }
156         RETURN(obj);
157 }
158
159 /*
160  * Helper routine to retrieve slave information.
161  * This function converts a quotactl request into quota/accounting object
162  * operations. It is independant of the slave stack which is only accessible
163  * from the OSD layer.
164  *
165  * \param env   - is the environment passed by the caller
166  * \param dev   - is the dt_device this quotactl is executed on
167  * \param oqctl - is the quotactl request
168  */
169 int lquotactl_slv(const struct lu_env *env, struct dt_device *dev,
170                   struct obd_quotactl *oqctl)
171 {
172         struct lquota_thread_info       *qti = lquota_info(env);
173         __u64                            key;
174         struct dt_object                *obj;
175         struct obd_dqblk                *dqblk = &oqctl->qc_dqblk;
176         int                              rc;
177         ENTRY;
178
179         if (oqctl->qc_cmd != Q_GETOQUOTA) {
180                 /* as in many other places, dev->dd_lu_dev.ld_obd->obd_name
181                  * point to an invalid obd_name, to be fixed in LU-1574 */
182                 CERROR("%s: Unsupported quotactl command: %x\n",
183                        dev->dd_lu_dev.ld_obd->obd_name, oqctl->qc_cmd);
184                 RETURN(-EOPNOTSUPP);
185         }
186
187         if (oqctl->qc_type < 0 || oqctl->qc_type >= MAXQUOTAS)
188                 /* no support for directory quota yet */
189                 RETURN(-EOPNOTSUPP);
190
191         /* qc_id is a 32-bit field while a key has 64 bits */
192         key = oqctl->qc_id;
193
194         /* Step 1: collect accounting information */
195
196         obj = acct_obj_lookup(env, dev, oqctl->qc_type);
197         if (IS_ERR(obj))
198                 RETURN(-EOPNOTSUPP);
199         if (obj->do_index_ops == NULL)
200                 GOTO(out, rc = -EINVAL);
201
202         /* lookup record storing space accounting information for this ID */
203         rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_acct_rec,
204                        (struct dt_key *)&key);
205         if (rc < 0)
206                 GOTO(out, rc);
207
208         memset(&oqctl->qc_dqblk, 0, sizeof(struct obd_dqblk));
209         dqblk->dqb_curspace     = qti->qti_acct_rec.bspace;
210         dqblk->dqb_curinodes    = qti->qti_acct_rec.ispace;
211         dqblk->dqb_valid        = QIF_USAGE;
212
213         dt_object_put(env, obj);
214
215         /* Step 2: collect enforcement information */
216
217         obj = quota_obj_lookup(env, dev, oqctl->qc_type);
218         if (IS_ERR(obj))
219                 RETURN(0);
220         if (obj->do_index_ops == NULL)
221                 GOTO(out, rc = 0);
222
223         memset(&qti->qti_slv_rec, 0, sizeof(qti->qti_slv_rec));
224         /* lookup record storing enforcement information for this ID */
225         rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_slv_rec,
226                        (struct dt_key *)&key);
227         if (rc < 0 && rc != -ENOENT)
228                 GOTO(out, rc = 0);
229
230         if (lu_device_is_md(dev->dd_lu_dev.ld_site->ls_top_dev)) {
231                 dqblk->dqb_ihardlimit = qti->qti_slv_rec.qsr_granted;
232                 dqblk->dqb_bhardlimit = 0;
233         } else {
234                 dqblk->dqb_ihardlimit = 0;
235                 dqblk->dqb_bhardlimit = qti->qti_slv_rec.qsr_granted;
236         }
237         dqblk->dqb_valid |= QIF_LIMITS;
238
239         GOTO(out, rc = 0);
240 out:
241         dt_object_put(env, obj);
242         return rc;
243 }
244 EXPORT_SYMBOL(lquotactl_slv);
245
246 static inline __u8 qtype2lqtype(int qtype)
247 {
248         switch (qtype) {
249         case USRQUOTA:
250                 return LQUOTA_TYPE_USR;
251         case GRPQUOTA:
252                 return LQUOTA_TYPE_GRP;
253         }
254
255         return LQUOTA_TYPE_GRP;
256 }
257
258 static inline int lqtype2qtype(int lqtype)
259 {
260         switch (lqtype) {
261         case LQUOTA_TYPE_USR:
262                 return USRQUOTA;
263         case LQUOTA_TYPE_GRP:
264                 return GRPQUOTA;
265         }
266
267         return GRPQUOTA;
268 }
269
270 /**
271  * Helper routine returning the FID associated with the global index storing
272  * quota settings for the storage pool \pool_id, resource type \pool_type and
273  * the quota type \quota_type.
274  */
275 void lquota_generate_fid(struct lu_fid *fid, int pool_id, int pool_type,
276                         int quota_type)
277 {
278         __u8     lqtype = qtype2lqtype(quota_type);
279
280         fid->f_seq = FID_SEQ_QUOTA_GLB;
281         fid->f_oid = (lqtype << 24) | (pool_type << 16) | (__u16)pool_id;
282         fid->f_ver = 0;
283 }
284
285 /**
286  * Helper routine used to extract pool ID, pool type and quota type from a
287  * given FID.
288  */
289 int lquota_extract_fid(const struct lu_fid *fid, int *pool_id, int *pool_type,
290                        int *quota_type)
291 {
292         unsigned int lqtype;
293         ENTRY;
294
295         if (fid->f_seq != FID_SEQ_QUOTA_GLB)
296                 RETURN(-EINVAL);
297
298         if (pool_id != NULL) {
299                 lqtype = fid->f_oid & 0xffffU;
300                 if (lqtype != 0)
301                         /* we only support pool ID 0 for the time being */
302                         RETURN(-ENOTSUPP);
303                 *pool_id = lqtype;
304         }
305
306         if (pool_type != NULL) {
307                 lqtype = (fid->f_oid >> 16) & 0xffU;
308                 if (lqtype >= LQUOTA_LAST_RES)
309                         RETURN(-ENOTSUPP);
310
311                 *pool_type = lqtype;
312         }
313
314         if (quota_type != NULL) {
315                 lqtype = fid->f_oid >> 24;
316                 if (lqtype >= LQUOTA_TYPE_MAX)
317                         RETURN(-ENOTSUPP);
318
319                 *quota_type = lqtype2qtype(lqtype);
320         }
321
322         RETURN(0);
323 }
324
325 static int __init lquota_init(void)
326 {
327         int     rc;
328         ENTRY;
329
330         lquota_key_init_generic(&lquota_thread_key, NULL);
331         lu_context_key_register(&lquota_thread_key);
332
333         rc = lu_kmem_init(lquota_caches);
334         if (rc)
335                 GOTO(out_key, rc);
336
337         rc = qmt_glb_init();
338         if (rc)
339                 GOTO(out_caches, rc);
340
341         rc = qsd_glb_init();
342         if (rc)
343                 GOTO(out_qmt, rc);
344
345         RETURN(0);
346
347 out_qmt:
348         qmt_glb_fini();
349 out_caches:
350         lu_kmem_fini(lquota_caches);
351 out_key:
352         lu_context_key_degister(&lquota_thread_key);
353         return rc;
354 }
355
356 static void __exit lquota_exit(void)
357 {
358         qsd_glb_fini();
359         qmt_glb_fini();
360         lu_kmem_fini(lquota_caches);
361         lu_context_key_degister(&lquota_thread_key);
362 }
363
364 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
365 MODULE_DESCRIPTION("Lustre Quota");
366 MODULE_VERSION(LUSTRE_VERSION_STRING);
367 MODULE_LICENSE("GPL");
368
369 module_init(lquota_init);
370 module_exit(lquota_exit);