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