4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2012, Whamcloud, Inc.
28 * Code originally extracted from quota directory
31 # include <liblustre.h>
35 #include "osc_internal.h"
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 */
44 cfs_spinlock_t qinfo_list_lock = CFS_SPIN_LOCK_UNLOCKED;
46 static cfs_list_t qinfo_hash[NR_DQHASH];
47 /* SLAB cache for client quota context */
48 cfs_mem_cache_t *qinfo_cachep = NULL;
50 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
51 __attribute__((__const__));
53 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
55 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
56 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
60 /* caller must hold qinfo_list_lock */
61 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
63 cfs_list_t *head = qinfo_hash +
64 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
66 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
67 cfs_list_add(&oqi->oqi_hash, head);
70 /* caller must hold qinfo_list_lock */
71 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
73 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
74 cfs_list_del_init(&oqi->oqi_hash);
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)
81 struct osc_quota_info *oqi;
82 unsigned int hashent = hashfn(cli, id, type);
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)
94 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
95 unsigned int id, int type)
97 struct osc_quota_info *oqi;
100 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_IO, sizeof(*oqi));
104 CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
107 oqi->oqi_type = type;
112 static void free_qinfo(struct osc_quota_info *oqi)
114 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
117 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
120 int cnt, rc = QUOTA_OK;
123 cfs_spin_lock(&qinfo_list_lock);
124 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
125 struct osc_quota_info *oqi = NULL;
127 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
128 oqi = find_qinfo(cli, id, cnt);
134 cfs_spin_unlock(&qinfo_list_lock);
137 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
138 cnt == USRQUOTA ? "user" : "group", id);
142 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
143 obd_flag valid, obd_flag flags)
150 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
151 struct osc_quota_info *oqi = NULL, *old;
153 if (!(valid & ((cnt == USRQUOTA) ?
154 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
157 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
158 noquota = (cnt == USRQUOTA) ?
159 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
162 oqi = alloc_qinfo(cli, id, cnt);
165 CDEBUG(D_QUOTA, "setdq for %s %d failed, "
167 cnt == USRQUOTA ? "user" : "group",
173 cfs_spin_lock(&qinfo_list_lock);
174 old = find_qinfo(cli, id, cnt);
176 remove_qinfo_hash(old);
177 else if (!old && noquota)
178 insert_qinfo_hash(oqi);
179 cfs_spin_unlock(&qinfo_list_lock);
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);
198 int osc_quota_cleanup(struct obd_device *obd)
200 struct client_obd *cli = &obd->u.cli;
201 struct osc_quota_info *oqi, *n;
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)
210 remove_qinfo_hash(oqi);
214 cfs_spin_unlock(&qinfo_list_lock);
224 LASSERT(qinfo_cachep == NULL);
225 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
226 sizeof(struct osc_quota_info),
231 for (i = 0; i < NR_DQHASH; i++)
232 CFS_INIT_LIST_HEAD(qinfo_hash + i);
239 struct osc_quota_info *oqi, *n;
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);
250 cfs_spin_unlock(&qinfo_list_lock);
252 rc = cfs_mem_cache_destroy(qinfo_cachep);
253 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
259 int osc_quotactl(struct obd_device *unused, struct obd_export *exp,
260 struct obd_quotactl *oqctl)
262 struct ptlrpc_request *req;
263 struct obd_quotactl *oqc;
267 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
268 &RQF_OST_QUOTACTL, LUSTRE_OST_VERSION,
273 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
276 ptlrpc_request_set_replen(req);
277 ptlrpc_at_set_req_timeout(req);
278 req->rq_no_resend = 1;
280 rc = ptlrpc_queue_wait(req);
282 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
284 if (req->rq_repmsg &&
285 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
288 CERROR ("Can't unpack obd_quotactl\n");
291 ptlrpc_req_finished(req);
296 int osc_quotacheck(struct obd_device *unused, struct obd_export *exp,
297 struct obd_quotactl *oqctl)
299 struct client_obd *cli = &exp->exp_obd->u.cli;
300 struct ptlrpc_request *req;
301 struct obd_quotactl *body;
305 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
306 &RQF_OST_QUOTACHECK, LUSTRE_OST_VERSION,
311 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
314 ptlrpc_request_set_replen(req);
316 /* the next poll will find -ENODATA, that means quotacheck is
318 cli->cl_qchk_stat = -ENODATA;
319 rc = ptlrpc_queue_wait(req);
321 cli->cl_qchk_stat = rc;
322 ptlrpc_req_finished(req);
326 int osc_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
328 struct client_obd *cli = &exp->exp_obd->u.cli;
332 qchk->obd_uuid = cli->cl_target_uuid;
333 memcpy(qchk->obd_type, LUSTRE_OST_NAME, strlen(LUSTRE_OST_NAME));
335 rc = cli->cl_qchk_stat;
336 /* the client is not the previous one */
337 if (rc == CL_NOT_QUOTACHECKED)
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)
347 struct ptlrpc_request *req;
348 struct quota_adjust_qunit *oqa;
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);
358 if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
363 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
364 &RQF_OST_QUOTA_ADJUST_QUNIT,
366 OST_QUOTA_ADJUST_QUNIT);
370 oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
373 ptlrpc_request_set_replen(req);
375 ptlrpc_set_add_req(rqset, req);