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.
27 * Copyright (c) 2011 Whamcloud, Inc.
29 * Code originally extracted from quota directory
32 # include <liblustre.h>
36 #include "osc_internal.h"
38 struct osc_quota_info {
39 cfs_list_t oqi_hash; /* hash list */
40 struct client_obd *oqi_cli; /* osc obd */
41 unsigned int oqi_id; /* uid/gid of a file */
42 short oqi_type; /* quota type */
45 cfs_spinlock_t qinfo_list_lock = CFS_SPIN_LOCK_UNLOCKED;
47 static cfs_list_t qinfo_hash[NR_DQHASH];
48 /* SLAB cache for client quota context */
49 cfs_mem_cache_t *qinfo_cachep = NULL;
51 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
52 __attribute__((__const__));
54 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
56 unsigned long tmp = ((unsigned long)cli>>6) ^ id;
57 tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
61 /* caller must hold qinfo_list_lock */
62 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
64 cfs_list_t *head = qinfo_hash +
65 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
67 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
68 cfs_list_add(&oqi->oqi_hash, head);
71 /* caller must hold qinfo_list_lock */
72 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
74 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
75 cfs_list_del_init(&oqi->oqi_hash);
78 /* caller must hold qinfo_list_lock */
79 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
80 unsigned int id, int type)
82 struct osc_quota_info *oqi;
83 unsigned int hashent = hashfn(cli, id, type);
86 LASSERT_SPIN_LOCKED(&qinfo_list_lock);
87 cfs_list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
88 if (oqi->oqi_cli == cli &&
89 oqi->oqi_id == id && oqi->oqi_type == type)
95 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
96 unsigned int id, int type)
98 struct osc_quota_info *oqi;
101 OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_IO, sizeof(*oqi));
105 CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
108 oqi->oqi_type = type;
113 static void free_qinfo(struct osc_quota_info *oqi)
115 OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
118 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
121 int cnt, rc = QUOTA_OK;
124 cfs_spin_lock(&qinfo_list_lock);
125 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
126 struct osc_quota_info *oqi = NULL;
128 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
129 oqi = find_qinfo(cli, id, cnt);
135 cfs_spin_unlock(&qinfo_list_lock);
138 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
139 cnt == USRQUOTA ? "user" : "group", id);
143 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
144 obd_flag valid, obd_flag flags)
151 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
152 struct osc_quota_info *oqi = NULL, *old;
154 if (!(valid & ((cnt == USRQUOTA) ?
155 OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
158 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
159 noquota = (cnt == USRQUOTA) ?
160 (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
163 oqi = alloc_qinfo(cli, id, cnt);
166 CDEBUG(D_QUOTA, "setdq for %s %d failed, "
168 cnt == USRQUOTA ? "user" : "group",
174 cfs_spin_lock(&qinfo_list_lock);
175 old = find_qinfo(cli, id, cnt);
177 remove_qinfo_hash(old);
178 else if (!old && noquota)
179 insert_qinfo_hash(oqi);
180 cfs_spin_unlock(&qinfo_list_lock);
183 CDEBUG(D_QUOTA, "setdq to remove for %s %d\n",
184 cnt == USRQUOTA ? "user" : "group", id);
185 else if (!old && noquota)
186 CDEBUG(D_QUOTA, "setdq to insert for %s %d\n",
187 cnt == USRQUOTA ? "user" : "group", id);
199 int osc_quota_cleanup(struct obd_device *obd)
201 struct client_obd *cli = &obd->u.cli;
202 struct osc_quota_info *oqi, *n;
206 cfs_spin_lock(&qinfo_list_lock);
207 for (i = 0; i < NR_DQHASH; i++) {
208 cfs_list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
209 if (oqi->oqi_cli != cli)
211 remove_qinfo_hash(oqi);
215 cfs_spin_unlock(&qinfo_list_lock);
225 LASSERT(qinfo_cachep == NULL);
226 qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
227 sizeof(struct osc_quota_info),
232 for (i = 0; i < NR_DQHASH; i++)
233 CFS_INIT_LIST_HEAD(qinfo_hash + i);
240 struct osc_quota_info *oqi, *n;
244 cfs_spin_lock(&qinfo_list_lock);
245 for (i = 0; i < NR_DQHASH; i++) {
246 cfs_list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
247 remove_qinfo_hash(oqi);
251 cfs_spin_unlock(&qinfo_list_lock);
253 rc = cfs_mem_cache_destroy(qinfo_cachep);
254 LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
260 int osc_quotactl(struct obd_device *unused, struct obd_export *exp,
261 struct obd_quotactl *oqctl)
263 struct ptlrpc_request *req;
264 struct obd_quotactl *oqc;
268 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
269 &RQF_OST_QUOTACTL, LUSTRE_OST_VERSION,
274 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
277 ptlrpc_request_set_replen(req);
278 ptlrpc_at_set_req_timeout(req);
279 req->rq_no_resend = 1;
281 rc = ptlrpc_queue_wait(req);
283 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
285 if (req->rq_repmsg &&
286 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
289 CERROR ("Can't unpack obd_quotactl\n");
292 ptlrpc_req_finished(req);
297 int osc_quotacheck(struct obd_device *unused, struct obd_export *exp,
298 struct obd_quotactl *oqctl)
300 struct client_obd *cli = &exp->exp_obd->u.cli;
301 struct ptlrpc_request *req;
302 struct obd_quotactl *body;
306 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
307 &RQF_OST_QUOTACHECK, LUSTRE_OST_VERSION,
312 body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
315 ptlrpc_request_set_replen(req);
317 /* the next poll will find -ENODATA, that means quotacheck is
319 cli->cl_qchk_stat = -ENODATA;
320 rc = ptlrpc_queue_wait(req);
322 cli->cl_qchk_stat = rc;
323 ptlrpc_req_finished(req);
327 int osc_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
329 struct client_obd *cli = &exp->exp_obd->u.cli;
333 qchk->obd_uuid = cli->cl_target_uuid;
334 memcpy(qchk->obd_type, LUSTRE_OST_NAME, strlen(LUSTRE_OST_NAME));
336 rc = cli->cl_qchk_stat;
337 /* the client is not the previous one */
338 if (rc == CL_NOT_QUOTACHECKED)
343 int osc_quota_adjust_qunit(struct obd_export *exp,
344 struct quota_adjust_qunit *oqaq,
345 struct lustre_quota_ctxt *qctxt,
346 struct ptlrpc_request_set *rqset)
348 struct ptlrpc_request *req;
349 struct quota_adjust_qunit *oqa;
353 /* client don't support this kind of operation, abort it */
354 if (!(exp->exp_connect_flags & OBD_CONNECT_CHANGE_QS)) {
355 CDEBUG(D_QUOTA, "osc: %s don't support change qunit size\n",
356 exp->exp_obd->obd_name);
359 if (strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME))
364 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
365 &RQF_OST_QUOTA_ADJUST_QUNIT,
367 OST_QUOTA_ADJUST_QUNIT);
371 oqa = req_capsule_client_get(&req->rq_pill, &RMF_QUOTA_ADJUST_QUNIT);
374 ptlrpc_request_set_replen(req);
376 ptlrpc_set_add_req(rqset, req);