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