Whamcloud - gitweb
LU-1222 ldlm: Fix the race in AST sender vs multiple arriving RPCs
[fs/lustre-release.git] / lustre / osc / osc_quota.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 021110-1307, USA
22  *
23  * GPL HEADER END
24  */
25 /*
26  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
27  *
28  * Copyright (c) 2012, Whamcloud, Inc.
29  *
30  * Code originally extracted from quota directory
31  */
32 #ifndef __KERNEL__
33 # include <liblustre.h>
34 #endif
35
36 #include <obd_ost.h>
37 #include "osc_internal.h"
38
39 struct osc_quota_info {
40         cfs_list_t         oqi_hash; /* hash list */
41         struct client_obd *oqi_cli;  /* osc obd */
42         unsigned int       oqi_id;   /* uid/gid of a file */
43         short              oqi_type; /* quota type */
44 };
45
46 cfs_spinlock_t qinfo_list_lock = CFS_SPIN_LOCK_UNLOCKED;
47
48 static cfs_list_t qinfo_hash[NR_DQHASH];
49 /* SLAB cache for client quota context */
50 cfs_mem_cache_t *qinfo_cachep = NULL;
51
52 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
53                          __attribute__((__const__));
54
55 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
56 {
57         unsigned long tmp = ((unsigned long)cli>>6) ^ id;
58         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
59         return tmp;
60 }
61
62 /* caller must hold qinfo_list_lock */
63 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
64 {
65         cfs_list_t *head = qinfo_hash +
66                 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
67
68         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
69         cfs_list_add(&oqi->oqi_hash, head);
70 }
71
72 /* caller must hold qinfo_list_lock */
73 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
74 {
75         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
76         cfs_list_del_init(&oqi->oqi_hash);
77 }
78
79 /* caller must hold qinfo_list_lock */
80 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
81                                                 unsigned int id, int type)
82 {
83         struct osc_quota_info *oqi;
84         unsigned int           hashent = hashfn(cli, id, type);
85         ENTRY;
86
87         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
88         cfs_list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
89                 if (oqi->oqi_cli == cli &&
90                     oqi->oqi_id == id && oqi->oqi_type == type)
91                         RETURN(oqi);
92         }
93         RETURN(NULL);
94 }
95
96 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
97                                           unsigned int id, int type)
98 {
99         struct osc_quota_info *oqi;
100         ENTRY;
101
102         OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_IO, sizeof(*oqi));
103         if(!oqi)
104                 RETURN(NULL);
105
106         CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
107         oqi->oqi_cli = cli;
108         oqi->oqi_id = id;
109         oqi->oqi_type = type;
110
111         RETURN(oqi);
112 }
113
114 static void free_qinfo(struct osc_quota_info *oqi)
115 {
116         OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
117 }
118
119 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
120 {
121         unsigned int id;
122         int          cnt, rc = QUOTA_OK;
123         ENTRY;
124
125         cfs_spin_lock(&qinfo_list_lock);
126         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
127                 struct osc_quota_info *oqi = NULL;
128
129                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
130                 oqi = find_qinfo(cli, id, cnt);
131                 if (oqi) {
132                         rc = NO_QUOTA;
133                         break;
134                 }
135         }
136         cfs_spin_unlock(&qinfo_list_lock);
137
138         if (rc == NO_QUOTA)
139                 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
140                        cnt == USRQUOTA ? "user" : "group", id);
141         RETURN(rc);
142 }
143
144 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
145                     obd_flag valid, obd_flag flags)
146 {
147         unsigned int id;
148         obd_flag     noquota;
149         int          cnt, rc = 0;
150         ENTRY;
151
152         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
153                 struct osc_quota_info *oqi = NULL, *old;
154
155                 if (!(valid & ((cnt == USRQUOTA) ?
156                     OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
157                         continue;
158
159                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
160                 noquota = (cnt == USRQUOTA) ?
161                     (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
162
163                 if (noquota) {
164                         oqi = alloc_qinfo(cli, id, cnt);
165                         if (!oqi) {
166                                 rc = -ENOMEM;
167                                 CDEBUG(D_QUOTA, "setdq for %s %d failed, "
168                                        "(rc = %d)\n",
169                                        cnt == USRQUOTA ? "user" : "group",
170                                        id, rc);
171                                 break;
172                         }
173                 }
174
175                 cfs_spin_lock(&qinfo_list_lock);
176                 old = find_qinfo(cli, id, cnt);
177                 if (old && !noquota)
178                         remove_qinfo_hash(old);
179                 else if (!old && noquota)
180                         insert_qinfo_hash(oqi);
181                 cfs_spin_unlock(&qinfo_list_lock);
182
183                 if (old && !noquota)
184                         CDEBUG(D_QUOTA, "setdq to remove for %s %d\n",
185                                cnt == USRQUOTA ? "user" : "group", id);
186                 else if (!old && noquota)
187                         CDEBUG(D_QUOTA, "setdq to insert for %s %d\n",
188                                cnt == USRQUOTA ? "user" : "group", id);
189
190                 if (old) {
191                         if (noquota)
192                                 free_qinfo(oqi);
193                         else
194                                 free_qinfo(old);
195                 }
196         }
197         RETURN(rc);
198 }
199
200 int osc_quota_cleanup(struct obd_device *obd)
201 {
202         struct client_obd     *cli = &obd->u.cli;
203         struct osc_quota_info *oqi, *n;
204         int i;
205         ENTRY;
206
207         cfs_spin_lock(&qinfo_list_lock);
208         for (i = 0; i < NR_DQHASH; i++) {
209                 cfs_list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
210                         if (oqi->oqi_cli != cli)
211                                 continue;
212                         remove_qinfo_hash(oqi);
213                         free_qinfo(oqi);
214                 }
215         }
216         cfs_spin_unlock(&qinfo_list_lock);
217
218         RETURN(0);
219 }
220
221 int osc_quota_init()
222 {
223         int i;
224         ENTRY;
225
226         LASSERT(qinfo_cachep == NULL);
227         qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
228                                             sizeof(struct osc_quota_info),
229                                             0, 0);
230         if (!qinfo_cachep)
231                 RETURN(-ENOMEM);
232
233         for (i = 0; i < NR_DQHASH; i++)
234                 CFS_INIT_LIST_HEAD(qinfo_hash + i);
235
236         RETURN(0);
237 }
238
239 int osc_quota_exit()
240 {
241         struct osc_quota_info *oqi, *n;
242         int                    i, rc;
243         ENTRY;
244
245         cfs_spin_lock(&qinfo_list_lock);
246         for (i = 0; i < NR_DQHASH; i++) {
247                 cfs_list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
248                         remove_qinfo_hash(oqi);
249                         free_qinfo(oqi);
250                 }
251         }
252         cfs_spin_unlock(&qinfo_list_lock);
253
254         rc = cfs_mem_cache_destroy(qinfo_cachep);
255         LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
256         qinfo_cachep = NULL;
257
258         RETURN(0);
259 }
260
261 int osc_quotactl(struct obd_device *unused, struct obd_export *exp,
262                  struct obd_quotactl *oqctl)
263 {
264         struct ptlrpc_request *req;
265         struct obd_quotactl   *oqc;
266         int                    rc;
267         ENTRY;
268
269         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
270                                         &RQF_OST_QUOTACTL, LUSTRE_OST_VERSION,
271                                         OST_QUOTACTL);
272         if (req == NULL)
273                 RETURN(-ENOMEM);
274
275         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
276         *oqc = *oqctl;
277
278         ptlrpc_request_set_replen(req);
279         ptlrpc_at_set_req_timeout(req);
280         req->rq_no_resend = 1;
281
282         rc = ptlrpc_queue_wait(req);
283         if (rc)
284                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
285
286         if (req->rq_repmsg &&
287             (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
288                 *oqctl = *oqc;
289         } else if (!rc) {
290                 CERROR ("Can't unpack obd_quotactl\n");
291                 rc = -EPROTO;
292         }
293         ptlrpc_req_finished(req);
294
295         RETURN(rc);
296 }
297
298 int osc_quotacheck(struct obd_device *unused, struct obd_export *exp,
299                    struct obd_quotactl *oqctl)
300 {
301         struct client_obd       *cli = &exp->exp_obd->u.cli;
302         struct ptlrpc_request   *req;
303         struct obd_quotactl     *body;
304         int                      rc;
305         ENTRY;
306
307         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
308                                         &RQF_OST_QUOTACHECK, LUSTRE_OST_VERSION,
309                                         OST_QUOTACHECK);
310         if (req == NULL)
311                 RETURN(-ENOMEM);
312
313         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
314         *body = *oqctl;
315
316         ptlrpc_request_set_replen(req);
317
318         /* the next poll will find -ENODATA, that means quotacheck is
319          * going on */
320         cli->cl_qchk_stat = -ENODATA;
321         rc = ptlrpc_queue_wait(req);
322         if (rc)
323                 cli->cl_qchk_stat = rc;
324         ptlrpc_req_finished(req);
325         RETURN(rc);
326 }
327
328 int osc_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
329 {
330         struct client_obd *cli = &exp->exp_obd->u.cli;
331         int rc;
332         ENTRY;
333
334         qchk->obd_uuid = cli->cl_target_uuid;
335         memcpy(qchk->obd_type, LUSTRE_OST_NAME, strlen(LUSTRE_OST_NAME));
336
337         rc = cli->cl_qchk_stat;
338         /* the client is not the previous one */
339         if (rc == CL_NOT_QUOTACHECKED)
340                 rc = -EINTR;
341         RETURN(rc);
342 }
343
344 int osc_quota_adjust_qunit(struct obd_export *exp,
345                            struct quota_adjust_qunit *oqaq,
346                            struct lustre_quota_ctxt *qctxt,
347                            struct ptlrpc_request_set *rqset)
348 {
349         struct ptlrpc_request     *req;
350         struct quota_adjust_qunit *oqa;
351         int                        rc = 0;
352         ENTRY;
353
354         /* client don't support this kind of operation, abort it */
355         if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)) {
356                 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
357                        exp->exp_obd->obd_name);
358                 RETURN(rc);
359         }
360         if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
361                 RETURN(-EINVAL);
362
363         LASSERT(rqset);
364
365         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
366                                         &RQF_OST_QUOTA_ADJUST_QUNIT,
367                                         LUSTRE_OST_VERSION,
368                                         OST_QUOTA_ADJUST_QUNIT);
369         if (req == NULL)
370                 RETURN(-ENOMEM);
371
372         oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
373         *oqa = *oqaq;
374
375         ptlrpc_request_set_replen(req);
376
377         ptlrpc_set_add_req(rqset, req);
378         RETURN(rc);
379 }