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, 2013, Intel Corporation.
28 * Code originally extracted from quota directory
32 #include "osc_internal.h"
34 static inline struct osc_quota_info *osc_oqi_alloc(u32 id)
36 struct osc_quota_info *oqi;
38 OBD_SLAB_ALLOC_PTR(oqi, osc_quota_kmem);
45 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
50 for (type = 0; type < MAXQUOTAS; type++) {
51 struct osc_quota_info *oqi;
53 oqi = cfs_hash_lookup(cli->cl_quota_hash[type], &qid[type]);
55 /* do not try to access oqi here, it could have been
56 * freed by osc_quota_setdq() */
58 /* the slot is busy, the user is about to run out of
59 * quota space on this OST */
60 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
61 type == USRQUOTA ? "user" : "grout", qid[type]);
69 #define MD_QUOTA_FLAG(type) ((type == USRQUOTA) ? OBD_MD_FLUSRQUOTA \
71 #define FL_QUOTA_FLAG(type) ((type == USRQUOTA) ? OBD_FL_NO_USRQUOTA \
74 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
81 if ((valid & (OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA)) == 0)
84 for (type = 0; type < MAXQUOTAS; type++) {
85 struct osc_quota_info *oqi;
87 if ((valid & MD_QUOTA_FLAG(type)) == 0)
90 /* lookup the ID in the per-type hash table */
91 oqi = cfs_hash_lookup(cli->cl_quota_hash[type], &qid[type]);
92 if ((flags & FL_QUOTA_FLAG(type)) != 0) {
93 /* This ID is getting close to its quota limit, let's
94 * switch to sync I/O */
98 oqi = osc_oqi_alloc(qid[type]);
104 rc = cfs_hash_add_unique(cli->cl_quota_hash[type],
105 &qid[type], &oqi->oqi_hash);
106 /* race with others? */
107 if (rc == -EALREADY) {
109 OBD_SLAB_FREE_PTR(oqi, osc_quota_kmem);
112 CDEBUG(D_QUOTA, "%s: setdq to insert for %s %d (%d)\n",
114 type == USRQUOTA ? "user" : "group",
117 /* This ID is now off the hook, let's remove it from
122 oqi = cfs_hash_del_key(cli->cl_quota_hash[type],
125 OBD_SLAB_FREE_PTR(oqi, osc_quota_kmem);
127 CDEBUG(D_QUOTA, "%s: setdq to remove for %s %d (%p)\n",
129 type == USRQUOTA ? "user" : "group",
138 * Hash operations for uid/gid <-> osc_quota_info
141 oqi_hashfn(struct cfs_hash *hs, const void *key, unsigned mask)
143 return cfs_hash_u32_hash(*((__u32*)key), mask);
147 oqi_keycmp(const void *key, struct hlist_node *hnode)
149 struct osc_quota_info *oqi;
152 LASSERT(key != NULL);
154 oqi = hlist_entry(hnode, struct osc_quota_info, oqi_hash);
156 return uid == oqi->oqi_id;
160 oqi_key(struct hlist_node *hnode)
162 struct osc_quota_info *oqi;
163 oqi = hlist_entry(hnode, struct osc_quota_info, oqi_hash);
168 oqi_object(struct hlist_node *hnode)
170 return hlist_entry(hnode, struct osc_quota_info, oqi_hash);
174 oqi_get(struct cfs_hash *hs, struct hlist_node *hnode)
179 oqi_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
184 oqi_exit(struct cfs_hash *hs, struct hlist_node *hnode)
186 struct osc_quota_info *oqi;
188 oqi = hlist_entry(hnode, struct osc_quota_info, oqi_hash);
190 OBD_SLAB_FREE_PTR(oqi, osc_quota_kmem);
193 #define HASH_QUOTA_BKT_BITS 5
194 #define HASH_QUOTA_CUR_BITS 5
195 #define HASH_QUOTA_MAX_BITS 15
197 static struct cfs_hash_ops quota_hash_ops = {
198 .hs_hash = oqi_hashfn,
199 .hs_keycmp = oqi_keycmp,
201 .hs_object = oqi_object,
203 .hs_put_locked = oqi_put_locked,
207 int osc_quota_setup(struct obd_device *obd)
209 struct client_obd *cli = &obd->u.cli;
213 for (type = 0; type < MAXQUOTAS; type++) {
214 cli->cl_quota_hash[type] = cfs_hash_create("QUOTA_HASH",
223 if (cli->cl_quota_hash[type] == NULL)
227 if (type == MAXQUOTAS)
230 for (i = 0; i < type; i++)
231 cfs_hash_putref(cli->cl_quota_hash[i]);
236 int osc_quota_cleanup(struct obd_device *obd)
238 struct client_obd *cli = &obd->u.cli;
242 for (type = 0; type < MAXQUOTAS; type++)
243 cfs_hash_putref(cli->cl_quota_hash[type]);
248 int osc_quotactl(struct obd_device *unused, struct obd_export *exp,
249 struct obd_quotactl *oqctl)
251 struct ptlrpc_request *req;
252 struct obd_quotactl *oqc;
256 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
257 &RQF_OST_QUOTACTL, LUSTRE_OST_VERSION,
262 oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
265 ptlrpc_request_set_replen(req);
266 ptlrpc_at_set_req_timeout(req);
267 req->rq_no_resend = 1;
269 rc = ptlrpc_queue_wait(req);
271 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
273 if (req->rq_repmsg &&
274 (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
277 CERROR ("Can't unpack obd_quotactl\n");
280 ptlrpc_req_finished(req);