Whamcloud - gitweb
LU-1842 quota: helper routines to manage FID_SEQ_QUOTA_GLB
[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 /* register lquota key */
44 LU_KEY_INIT_FINI(lquota, struct lquota_thread_info);
45 LU_CONTEXT_KEY_DEFINE(lquota, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
46 LU_KEY_INIT_GENERIC(lquota);
47
48 /**
49  * Look-up accounting object to collect space usage information for user
50  * or group.
51  *
52  * \param env  - is the environment passed by the caller
53  * \param dev  - is the dt_device storing the accounting object
54  * \param type - is the quota type, either USRQUOTA or GRPQUOTA
55  */
56 struct dt_object *acct_obj_lookup(const struct lu_env *env,
57                                   struct dt_device *dev, int type)
58 {
59         struct lquota_thread_info       *qti = lquota_info(env);
60         struct dt_object                *obj = NULL;
61         ENTRY;
62
63         lu_local_obj_fid(&qti->qti_fid,
64                          type == USRQUOTA ? ACCT_USER_OID : ACCT_GROUP_OID);
65
66         /* lookup the accounting object */
67         obj = dt_locate(env, dev, &qti->qti_fid);
68         if (IS_ERR(obj))
69                 RETURN(obj);
70
71         if (!dt_object_exists(obj)) {
72                 lu_object_put(env, &obj->do_lu);
73                 RETURN(ERR_PTR(-ENOENT));
74         }
75
76         if (obj->do_index_ops == NULL) {
77                 int rc;
78
79                 /* set up indexing operations */
80                 rc = obj->do_ops->do_index_try(env, obj, &dt_acct_features);
81                 if (rc) {
82                         CERROR("%s: failed to set up indexing operations for %s"
83                                " acct object rc:%d\n",
84                                dev->dd_lu_dev.ld_obd->obd_name,
85                                QTYPE_NAME(type), rc);
86                         lu_object_put(env, &obj->do_lu);
87                         RETURN(ERR_PTR(rc));
88                 }
89         }
90         RETURN(obj);
91 }
92
93 /**
94  * Initialize slave index object to collect local quota limit for user or group.
95  *
96  * \param env - is the environment passed by the caller
97  * \param dev - is the dt_device storing the slave index object
98  * \param type - is the quota type, either USRQUOTA or GRPQUOTA
99  */
100 static struct dt_object *quota_obj_lookup(const struct lu_env *env,
101                                           struct dt_device *dev, int type)
102 {
103         struct lquota_thread_info       *qti = lquota_info(env);
104         struct dt_object                *obj = NULL;
105         ENTRY;
106
107         qti->qti_fid.f_seq = FID_SEQ_QUOTA;
108         qti->qti_fid.f_oid = type == USRQUOTA ? LQUOTA_USR_OID : LQUOTA_GRP_OID;
109         qti->qti_fid.f_ver = 0;
110
111         /* lookup the quota object */
112         obj = dt_locate(env, dev, &qti->qti_fid);
113         if (IS_ERR(obj))
114                 RETURN(obj);
115
116         if (!dt_object_exists(obj)) {
117                 lu_object_put(env, &obj->do_lu);
118                 RETURN(ERR_PTR(-ENOENT));
119         }
120
121         if (obj->do_index_ops == NULL) {
122                 int rc;
123
124                 /* set up indexing operations */
125                 rc = obj->do_ops->do_index_try(env, obj,
126                                                &dt_quota_slv_features);
127                 if (rc) {
128                         CERROR("%s: failed to set up indexing operations for %s"
129                                " slave index object rc:%d\n",
130                                dev->dd_lu_dev.ld_obd->obd_name,
131                                QTYPE_NAME(type), rc);
132                         lu_object_put(env, &obj->do_lu);
133                         RETURN(ERR_PTR(rc));
134                 }
135         }
136         RETURN(obj);
137 }
138
139 /*
140  * Helper routine to retrieve slave information.
141  * This function converts a quotactl request into quota/accounting object
142  * operations. It is independant of the slave stack which is only accessible
143  * from the OSD layer.
144  *
145  * \param env   - is the environment passed by the caller
146  * \param dev   - is the dt_device this quotactl is executed on
147  * \param oqctl - is the quotactl request
148  */
149 int lquotactl_slv(const struct lu_env *env, struct dt_device *dev,
150                   struct obd_quotactl *oqctl)
151 {
152         struct lquota_thread_info       *qti = lquota_info(env);
153         __u64                            key;
154         struct dt_object                *obj;
155         struct obd_dqblk                *dqblk = &oqctl->qc_dqblk;
156         int                              rc;
157         ENTRY;
158
159         if (oqctl->qc_cmd != Q_GETOQUOTA) {
160                 /* as in many other places, dev->dd_lu_dev.ld_obd->obd_name
161                  * point to an invalid obd_name, to be fixed in LU-1574 */
162                 CERROR("%s: Unsupported quotactl command: %x\n",
163                        dev->dd_lu_dev.ld_obd->obd_name, oqctl->qc_cmd);
164                 RETURN(-EOPNOTSUPP);
165         }
166
167         if (oqctl->qc_type != USRQUOTA && oqctl->qc_type != GRPQUOTA)
168                 /* no support for directory quota yet */
169                 RETURN(-EOPNOTSUPP);
170
171         /* qc_id is a 32-bit field while a key has 64 bits */
172         key = oqctl->qc_id;
173
174         /* Step 1: collect accounting information */
175
176         obj = acct_obj_lookup(env, dev, oqctl->qc_type);
177         if (IS_ERR(obj))
178                 RETURN(-EOPNOTSUPP);
179         if (obj->do_index_ops == NULL)
180                 GOTO(out, rc = -EINVAL);
181
182         /* lookup record storing space accounting information for this ID */
183         rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_acct_rec,
184                        (struct dt_key *)&key, BYPASS_CAPA);
185         if (rc < 0)
186                 GOTO(out, rc);
187
188         memset(&oqctl->qc_dqblk, 0, sizeof(struct obd_dqblk));
189         dqblk->dqb_curspace     = qti->qti_acct_rec.bspace;
190         dqblk->dqb_curinodes    = qti->qti_acct_rec.ispace;
191         dqblk->dqb_valid        = QIF_USAGE;
192
193         lu_object_put(env, &obj->do_lu);
194
195         /* Step 2: collect enforcement information */
196
197         obj = quota_obj_lookup(env, dev, oqctl->qc_type);
198         if (IS_ERR(obj))
199                 RETURN(0);
200         if (obj->do_index_ops == NULL)
201                 GOTO(out, rc = 0);
202
203         memset(&qti->qti_slv_rec, 0, sizeof(qti->qti_slv_rec));
204         /* lookup record storing enforcement information for this ID */
205         rc = dt_lookup(env, obj, (struct dt_rec *)&qti->qti_slv_rec,
206                        (struct dt_key *)&key, BYPASS_CAPA);
207         if (rc < 0 && rc != -ENOENT)
208                 GOTO(out, rc = 0);
209
210         if (lu_device_is_md(dev->dd_lu_dev.ld_site->ls_top_dev)) {
211                 dqblk->dqb_ihardlimit = qti->qti_slv_rec.qsr_granted;
212                 dqblk->dqb_bhardlimit = 0;
213         } else {
214                 dqblk->dqb_ihardlimit = 0;
215                 dqblk->dqb_bhardlimit = qti->qti_slv_rec.qsr_granted;
216         }
217         dqblk->dqb_valid |= QIF_LIMITS;
218
219         GOTO(out, rc = 0);
220 out:
221         lu_object_put(env, &obj->do_lu);
222         return rc;
223 }
224 EXPORT_SYMBOL(lquotactl_slv);
225
226 /**
227  * Helper routine returning the FID associated with the global index storing
228  * quota settings for the storage pool \pool_id, resource type \pool_type and
229  * the quota type \quota_type.
230  */
231 void lquota_generate_fid(struct lu_fid *fid, int pool_id, int pool_type,
232                          int quota_type)
233 {
234         __u8     qtype;
235
236         qtype = (quota_type == USRQUOTA) ? LQUOTA_TYPE_USR : LQUOTA_TYPE_GRP;
237
238         fid->f_seq = FID_SEQ_QUOTA_GLB;
239         fid->f_oid = (qtype << 24) | (pool_type << 16) | (__u16)pool_id;
240         fid->f_ver = 0;
241 }
242
243 /**
244  * Helper routine used to extract pool ID, pool type and quota type from a
245  * given FID.
246  */
247 int lquota_extract_fid(struct lu_fid *fid, int *pool_id, int *pool_type,
248                        int *quota_type)
249 {
250         unsigned int     tmp;
251         ENTRY;
252
253         if (fid->f_seq != FID_SEQ_QUOTA_GLB)
254                 RETURN(-EINVAL);
255
256         if (pool_id != NULL) {
257                 tmp = fid->f_oid & 0xffffU;
258                 if (tmp != 0)
259                         /* we only support pool ID 0 for the time being */
260                         RETURN(-ENOTSUPP);
261                 *pool_id = tmp;
262         }
263
264         if (pool_type != NULL) {
265                 tmp = (fid->f_oid >> 16) & 0xffU;
266                 if (tmp >= LQUOTA_LAST_RES)
267                         RETURN(-ENOTSUPP);
268
269                 *pool_type = tmp;
270         }
271
272         if (quota_type != NULL) {
273                 tmp = fid->f_oid >> 24;
274                 if (tmp >= LQUOTA_TYPE_MAX)
275                         RETURN(-ENOTSUPP);
276
277                 *quota_type = (tmp == LQUOTA_TYPE_USR) ? USRQUOTA : GRPQUOTA;
278         }
279
280         RETURN(0);
281 }
282
283 static int __init init_lquota(void)
284 {
285         int     rc;
286
287         /* call old quota module init function */
288         rc = init_lustre_quota();
289         if (rc)
290                 return rc;
291
292         /* new quota initialization */
293         lquota_key_init_generic(&lquota_thread_key, NULL);
294         lu_context_key_register(&lquota_thread_key);
295
296         return 0;
297 }
298
299 static void exit_lquota(void)
300 {
301         /* call old quota module exit function */
302         exit_lustre_quota();
303
304         lu_context_key_degister(&lquota_thread_key);
305 }
306
307 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
308 MODULE_DESCRIPTION("Lustre Quota");
309 MODULE_LICENSE("GPL");
310
311 cfs_module(lquota, "2.4.0", init_lquota, exit_lquota);