Whamcloud - gitweb
LU-11678 quota: protect quota flags at OSC
[fs/lustre-release.git] / lustre / osc / osc_quota.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  *
28  * Code originally extracted from quota directory
29  */
30
31 #include <obd.h>
32 #include <lustre_osc.h>
33
34 #include "osc_internal.h"
35
36 static inline struct osc_quota_info *osc_oqi_alloc(u32 id)
37 {
38         struct osc_quota_info *oqi;
39
40         OBD_SLAB_ALLOC_PTR(oqi, osc_quota_kmem);
41         if (oqi != NULL)
42                 oqi->oqi_id = id;
43
44         return oqi;
45 }
46
47 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
48 {
49         int type;
50         ENTRY;
51
52         for (type = 0; type < LL_MAXQUOTAS; type++) {
53                 struct osc_quota_info *oqi;
54
55                 oqi = cfs_hash_lookup(cli->cl_quota_hash[type], &qid[type]);
56                 if (oqi) {
57                         /* do not try to access oqi here, it could have been
58                          * freed by osc_quota_setdq() */
59
60                         /* the slot is busy, the user is about to run out of
61                          * quota space on this OST */
62                         CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
63                                type == USRQUOTA ? "user" : "grout", qid[type]);
64                         RETURN(NO_QUOTA);
65                 }
66         }
67
68         RETURN(QUOTA_OK);
69 }
70
71 static inline u32 md_quota_flag(int qtype)
72 {
73         switch (qtype) {
74         case USRQUOTA:
75                 return OBD_MD_FLUSRQUOTA;
76         case GRPQUOTA:
77                 return OBD_MD_FLGRPQUOTA;
78         case PRJQUOTA:
79                 return OBD_MD_FLPRJQUOTA;
80         default:
81                 return 0;
82         }
83 }
84
85 static inline u32 fl_quota_flag(int qtype)
86 {
87         switch (qtype) {
88         case USRQUOTA:
89                 return OBD_FL_NO_USRQUOTA;
90         case GRPQUOTA:
91                 return OBD_FL_NO_GRPQUOTA;
92         case PRJQUOTA:
93                 return OBD_FL_NO_PRJQUOTA;
94         default:
95                 return 0;
96         }
97 }
98
99 int osc_quota_setdq(struct client_obd *cli, __u64 xid, const unsigned int qid[],
100                     u64 valid, u32 flags)
101 {
102         int type;
103         int rc = 0;
104
105         ENTRY;
106
107         if ((valid & (OBD_MD_FLALLQUOTA)) == 0)
108                 RETURN(0);
109
110         mutex_lock(&cli->cl_quota_mutex);
111         if (cli->cl_quota_last_xid > xid)
112                 GOTO(out_unlock, rc);
113
114         cli->cl_quota_last_xid = xid;
115         for (type = 0; type < LL_MAXQUOTAS; type++) {
116                 struct osc_quota_info *oqi;
117
118                 if ((valid & md_quota_flag(type)) == 0)
119                         continue;
120
121                 /* lookup the ID in the per-type hash table */
122                 oqi = cfs_hash_lookup(cli->cl_quota_hash[type], &qid[type]);
123                 if ((flags & fl_quota_flag(type)) != 0) {
124                         /* This ID is getting close to its quota limit, let's
125                          * switch to sync I/O */
126                         if (oqi != NULL)
127                                 continue;
128
129                         oqi = osc_oqi_alloc(qid[type]);
130                         if (oqi == NULL) {
131                                 rc = -ENOMEM;
132                                 break;
133                         }
134
135                         rc = cfs_hash_add_unique(cli->cl_quota_hash[type],
136                                                  &qid[type], &oqi->oqi_hash);
137                         /* race with others? */
138                         if (rc == -EALREADY) {
139                                 rc = 0;
140                                 OBD_SLAB_FREE_PTR(oqi, osc_quota_kmem);
141                         }
142
143                         CDEBUG(D_QUOTA, "%s: setdq to insert for %s %d (%d)\n",
144                                cli_name(cli), qtype_name(type), qid[type], rc);
145                 } else {
146                         /* This ID is now off the hook, let's remove it from
147                          * the hash table */
148                         if (oqi == NULL)
149                                 continue;
150
151                         oqi = cfs_hash_del_key(cli->cl_quota_hash[type],
152                                                &qid[type]);
153                         if (oqi)
154                                 OBD_SLAB_FREE_PTR(oqi, osc_quota_kmem);
155
156                         CDEBUG(D_QUOTA, "%s: setdq to remove for %s %d (%p)\n",
157                                cli_name(cli), qtype_name(type), qid[type], oqi);
158                 }
159         }
160
161 out_unlock:
162         mutex_unlock(&cli->cl_quota_mutex);
163         RETURN(rc);
164 }
165
166 /*
167  * Hash operations for uid/gid <-> osc_quota_info
168  */
169 static unsigned
170 oqi_hashfn(struct cfs_hash *hs, const void *key, unsigned mask)
171 {
172         return cfs_hash_u32_hash(*((__u32*)key), mask);
173 }
174
175 static int
176 oqi_keycmp(const void *key, struct hlist_node *hnode)
177 {
178         struct osc_quota_info *oqi;
179         u32 uid;
180
181         LASSERT(key != NULL);
182         uid = *((u32 *)key);
183         oqi = hlist_entry(hnode, struct osc_quota_info, oqi_hash);
184
185         return uid == oqi->oqi_id;
186 }
187
188 static void *
189 oqi_key(struct hlist_node *hnode)
190 {
191         struct osc_quota_info *oqi;
192         oqi = hlist_entry(hnode, struct osc_quota_info, oqi_hash);
193         return &oqi->oqi_id;
194 }
195
196 static void *
197 oqi_object(struct hlist_node *hnode)
198 {
199         return hlist_entry(hnode, struct osc_quota_info, oqi_hash);
200 }
201
202 static void
203 oqi_get(struct cfs_hash *hs, struct hlist_node *hnode)
204 {
205 }
206
207 static void
208 oqi_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
209 {
210 }
211
212 static void
213 oqi_exit(struct cfs_hash *hs, struct hlist_node *hnode)
214 {
215         struct osc_quota_info *oqi;
216
217         oqi = hlist_entry(hnode, struct osc_quota_info, oqi_hash);
218
219         OBD_SLAB_FREE_PTR(oqi, osc_quota_kmem);
220 }
221
222 #define HASH_QUOTA_BKT_BITS 5
223 #define HASH_QUOTA_CUR_BITS 5
224 #define HASH_QUOTA_MAX_BITS 15
225
226 static struct cfs_hash_ops quota_hash_ops = {
227         .hs_hash        = oqi_hashfn,
228         .hs_keycmp      = oqi_keycmp,
229         .hs_key         = oqi_key,
230         .hs_object      = oqi_object,
231         .hs_get         = oqi_get,
232         .hs_put_locked  = oqi_put_locked,
233         .hs_exit        = oqi_exit,
234 };
235
236 int osc_quota_setup(struct obd_device *obd)
237 {
238         struct client_obd *cli = &obd->u.cli;
239         int i, type;
240         ENTRY;
241
242         mutex_init(&cli->cl_quota_mutex);
243
244         for (type = 0; type < LL_MAXQUOTAS; type++) {
245                 cli->cl_quota_hash[type] = cfs_hash_create("QUOTA_HASH",
246                                                            HASH_QUOTA_CUR_BITS,
247                                                            HASH_QUOTA_MAX_BITS,
248                                                            HASH_QUOTA_BKT_BITS,
249                                                            0,
250                                                            CFS_HASH_MIN_THETA,
251                                                            CFS_HASH_MAX_THETA,
252                                                            &quota_hash_ops,
253                                                            CFS_HASH_DEFAULT);
254                 if (cli->cl_quota_hash[type] == NULL)
255                         break;
256         }
257
258         if (type == LL_MAXQUOTAS)
259                 RETURN(0);
260
261         for (i = 0; i < type; i++)
262                 cfs_hash_putref(cli->cl_quota_hash[i]);
263
264         RETURN(-ENOMEM);
265 }
266
267 int osc_quota_cleanup(struct obd_device *obd)
268 {
269         struct client_obd     *cli = &obd->u.cli;
270         int type;
271         ENTRY;
272
273         for (type = 0; type < LL_MAXQUOTAS; type++)
274                 cfs_hash_putref(cli->cl_quota_hash[type]);
275
276         RETURN(0);
277 }
278
279 int osc_quotactl(struct obd_device *unused, struct obd_export *exp,
280                  struct obd_quotactl *oqctl)
281 {
282         struct ptlrpc_request *req;
283         struct obd_quotactl   *oqc;
284         int                    rc;
285         ENTRY;
286
287         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
288                                         &RQF_OST_QUOTACTL, LUSTRE_OST_VERSION,
289                                         OST_QUOTACTL);
290         if (req == NULL)
291                 RETURN(-ENOMEM);
292
293         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
294         *oqc = *oqctl;
295
296         ptlrpc_request_set_replen(req);
297         ptlrpc_at_set_req_timeout(req);
298         req->rq_no_resend = 1;
299
300         rc = ptlrpc_queue_wait(req);
301         if (rc)
302                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
303
304         if (req->rq_repmsg &&
305             (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
306                 *oqctl = *oqc;
307         } else if (!rc) {
308                 CERROR ("Can't unpack obd_quotactl\n");
309                 rc = -EPROTO;
310         }
311         ptlrpc_req_finished(req);
312
313         RETURN(rc);
314 }