1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
26 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Copyright (c) 2012, Whamcloud, Inc.
30 * Code originally extracted from quota directory
33 # include <liblustre.h>
37 #include "osc_internal.h"
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 */
46 cfs_spinlock_t qinfo_list_lock = CFS_SPIN_LOCK_UNLOCKED;
48 static cfs_list_t qinfo_hash[NR_DQHASH];
49 /* SLAB cache for client quota context */
50 cfs_mem_cache_t *qinfo_cachep = NULL;
52 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
53 __attribute__((__const__));
55 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
57 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
58 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
62 /* caller must hold qinfo_list_lock */
63 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
65 cfs_list_t *head = qinfo_hash +
66 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
68 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
69 cfs_list_add(&oqi->oqi_hash, head);
72 /* caller must hold qinfo_list_lock */
73 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
75 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
76 cfs_list_del_init(&oqi->oqi_hash);
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)
83 struct osc_quota_info *oqi;
84 unsigned int hashent = hashfn(cli, id, type);
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)
96 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
97 unsigned int id, int type)
99 struct osc_quota_info *oqi;
102 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_IO, sizeof(*oqi));
106 CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
109 oqi->oqi_type = type;
114 static void free_qinfo(struct osc_quota_info *oqi)
116 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
119 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
122 int cnt, rc = QUOTA_OK;
125 cfs_spin_lock(&qinfo_list_lock);
126 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
127 struct osc_quota_info *oqi = NULL;
129 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
130 oqi = find_qinfo(cli, id, cnt);
136 cfs_spin_unlock(&qinfo_list_lock);
139 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
140 cnt == USRQUOTA ? "user" : "group", id);
144 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
145 obd_flag valid, obd_flag flags)
152 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
153 struct osc_quota_info *oqi = NULL, *old;
155 if (!(valid & ((cnt == USRQUOTA) ?
156 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
159 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
160 noquota = (cnt == USRQUOTA) ?
161 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
164 oqi = alloc_qinfo(cli, id, cnt);
167 CDEBUG(D_QUOTA, "setdq for %s %d failed, "
169 cnt == USRQUOTA ? "user" : "group",
175 cfs_spin_lock(&qinfo_list_lock);
176 old = find_qinfo(cli, id, cnt);
178 remove_qinfo_hash(old);
179 else if (!old && noquota)
180 insert_qinfo_hash(oqi);
181 cfs_spin_unlock(&qinfo_list_lock);
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);
200 int osc_quota_cleanup(struct obd_device *obd)
202 struct client_obd *cli = &obd->u.cli;
203 struct osc_quota_info *oqi, *n;
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)
212 remove_qinfo_hash(oqi);
216 cfs_spin_unlock(&qinfo_list_lock);
226 LASSERT(qinfo_cachep == NULL);
227 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
228 sizeof(struct osc_quota_info),
233 for (i = 0; i < NR_DQHASH; i++)
234 CFS_INIT_LIST_HEAD(qinfo_hash + i);
241 struct osc_quota_info *oqi, *n;
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);
252 cfs_spin_unlock(&qinfo_list_lock);
254 rc = cfs_mem_cache_destroy(qinfo_cachep);
255 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
261 int osc_quotactl(struct obd_device *unused, struct obd_export *exp,
262 struct obd_quotactl *oqctl)
264 struct ptlrpc_request *req;
265 struct obd_quotactl *oqc;
269 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
270 &RQF_OST_QUOTACTL, LUSTRE_OST_VERSION,
275 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
278 ptlrpc_request_set_replen(req);
279 ptlrpc_at_set_req_timeout(req);
280 req->rq_no_resend = 1;
282 rc = ptlrpc_queue_wait(req);
284 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
286 if (req->rq_repmsg &&
287 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
290 CERROR ("Can't unpack obd_quotactl\n");
293 ptlrpc_req_finished(req);
298 int osc_quotacheck(struct obd_device *unused, struct obd_export *exp,
299 struct obd_quotactl *oqctl)
301 struct client_obd *cli = &exp->exp_obd->u.cli;
302 struct ptlrpc_request *req;
303 struct obd_quotactl *body;
307 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
308 &RQF_OST_QUOTACHECK, LUSTRE_OST_VERSION,
313 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
316 ptlrpc_request_set_replen(req);
318 /* the next poll will find -ENODATA, that means quotacheck is
320 cli->cl_qchk_stat = -ENODATA;
321 rc = ptlrpc_queue_wait(req);
323 cli->cl_qchk_stat = rc;
324 ptlrpc_req_finished(req);
328 int osc_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
330 struct client_obd *cli = &exp->exp_obd->u.cli;
334 qchk->obd_uuid = cli->cl_target_uuid;
335 memcpy(qchk->obd_type, LUSTRE_OST_NAME, strlen(LUSTRE_OST_NAME));
337 rc = cli->cl_qchk_stat;
338 /* the client is not the previous one */
339 if (rc == CL_NOT_QUOTACHECKED)
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)
349 struct ptlrpc_request *req;
350 struct quota_adjust_qunit *oqa;
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);
360 if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
365 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
366 &RQF_OST_QUOTA_ADJUST_QUNIT,
368 OST_QUOTA_ADJUST_QUNIT);
372 oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
375 ptlrpc_request_set_replen(req);
377 ptlrpc_set_add_req(rqset, req);