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