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