3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License version 2 for more details (a copy is included
13 * in the LICENSE file that accompanied this code).
15 * You should have received a copy of the GNU General Public License
16 * version 2 along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 021110-1307, USA
23 * Copyright (c) 2012, 2015, Intel Corporation.
24 * Use is subject to license terms.
26 * Author: Johann Lombardi <johann.lombardi@intel.com>
27 * Author: Niu Yawei <yawei.niu@intel.com>
30 #define DEBUG_SUBSYSTEM S_LQUOTA
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <obd_class.h>
35 #include "lquota_internal.h"
37 static int hash_lqs_cur_bits = HASH_LQE_CUR_BITS;
38 CFS_MODULE_PARM(hash_lqs_cur_bits, "i", int, 0444,
39 "the current bits of lqe hash");
42 lqe64_hash_hash(struct cfs_hash *hs, const void *key, unsigned mask)
44 return cfs_hash_u64_hash(*((__u64 *)key), mask);
47 static void *lqe64_hash_key(struct hlist_node *hnode)
49 struct lquota_entry *lqe;
50 lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
51 return &lqe->lqe_id.qid_uid;
54 static int lqe64_hash_keycmp(const void *key, struct hlist_node *hnode)
56 struct lquota_entry *lqe;
57 lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
58 return (lqe->lqe_id.qid_uid == *((__u64*)key));
61 static void *lqe_hash_object(struct hlist_node *hnode)
63 return hlist_entry(hnode, struct lquota_entry, lqe_hash);
66 static void lqe_hash_get(struct cfs_hash *hs, struct hlist_node *hnode)
68 struct lquota_entry *lqe;
69 lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
73 static void lqe_hash_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
75 struct lquota_entry *lqe;
76 lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
80 static void lqe_hash_exit(struct cfs_hash *hs, struct hlist_node *hnode)
82 CERROR("Should not have any item left!\n");
85 /* lqe hash methods for 64-bit uid/gid, new hash functions would have to be
86 * defined for per-directory quota relying on a 128-bit FID */
87 static struct cfs_hash_ops lqe64_hash_ops = {
88 .hs_hash = lqe64_hash_hash,
89 .hs_key = lqe64_hash_key,
90 .hs_keycmp = lqe64_hash_keycmp,
91 .hs_object = lqe_hash_object,
92 .hs_get = lqe_hash_get,
93 .hs_put_locked = lqe_hash_put_locked,
94 .hs_exit = lqe_hash_exit
97 /* Logging helper function */
98 void lquota_lqe_debug0(struct lquota_entry *lqe,
99 struct libcfs_debug_msg_data *msgdata,
100 const char *fmt, ...)
102 struct lquota_site *site = lqe->lqe_site;
105 LASSERT(site->lqs_ops->lqe_debug != NULL);
108 site->lqs_ops->lqe_debug(lqe, site->lqs_parent, msgdata, fmt, args);
112 struct lqe_iter_data {
113 unsigned long lid_inuse;
114 unsigned long lid_freed;
118 static int lqe_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
119 struct hlist_node *hnode, void *data)
121 struct lqe_iter_data *d = (struct lqe_iter_data *)data;
122 struct lquota_entry *lqe;
124 lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
125 LASSERT(atomic_read(&lqe->lqe_ref) > 0);
127 /* Only one reference held by hash table, and nobody else can
128 * grab the entry at this moment, it's safe to remove it from
129 * the hash and free it. */
130 if (atomic_read(&lqe->lqe_ref) == 1) {
131 if (!lqe_is_master(lqe)) {
132 LASSERT(lqe->lqe_pending_write == 0);
133 LASSERT(lqe->lqe_pending_req == 0);
135 if (d->lid_free_all || lqe->lqe_enforced) {
137 cfs_hash_bd_del_locked(hs, bd, hnode);
144 LQUOTA_ERROR(lqe, "Inuse quota entry");
149 * Cleanup the entries in the hashtable
151 * \param hash - hash table which stores quota entries
152 * \param free_all - free all entries or only free the entries
153 * without quota enforce ?
155 static void lqe_cleanup(struct cfs_hash *hash, bool free_all)
157 struct lqe_iter_data d;
161 memset(&d, 0, sizeof(d));
162 d.lid_free_all = free_all;
164 cfs_hash_for_each_safe(hash, lqe_iter_cb, &d);
166 /* In most case, when this function is called on master or
167 * slave finalization, there should be no inuse quota entry.
169 * If the per-fs quota updating thread is still holding
170 * some entries, we just wait for it's finished. */
171 if (free_all && d.lid_inuse) {
172 CDEBUG(D_QUOTA, "Hash:%p has entries inuse: inuse:%lu, "
173 "freed:%lu, repeat:%u\n", hash,
174 d.lid_inuse, d.lid_freed, repeat);
176 set_current_state(TASK_INTERRUPTIBLE);
177 schedule_timeout(cfs_time_seconds(1));
184 * Allocate a new lquota site.
186 * \param env - the environment passed by the caller
187 * \param parent - is a pointer to the parent structure, either a qmt_pool_info
188 * structure on the master or a qsd_qtype_info structure on the
190 * \param is_master - is set when the site belongs to a QMT.
191 * \param qtype - is the quota type managed by this site
192 * \param ops - is the quota entry operation vector to be used for quota
193 * entry belonging to this site.
195 * \retval 0 - success
196 * \retval -ve - failure
198 struct lquota_site *lquota_site_alloc(const struct lu_env *env, void *parent,
199 bool is_master, short qtype,
200 struct lquota_entry_operations *ops)
202 struct lquota_site *site;
206 LASSERT(qtype < MAXQUOTAS);
210 RETURN(ERR_PTR(-ENOMEM));
212 /* assign parameters */
213 site->lqs_qtype = qtype;
214 site->lqs_parent = parent;
215 site->lqs_is_mst = is_master;
218 /* allocate hash table */
219 memset(hashname, 0, sizeof(hashname));
220 snprintf(hashname, sizeof(hashname), "LQUOTA_HASH%hu", qtype);
221 site->lqs_hash= cfs_hash_create(hashname, hash_lqs_cur_bits,
223 min(hash_lqs_cur_bits,
225 0, CFS_HASH_MIN_THETA,
226 CFS_HASH_MAX_THETA, &lqe64_hash_ops,
227 CFS_HASH_DEFAULT|CFS_HASH_BIGNAME);
228 if (site->lqs_hash == NULL) {
230 RETURN(ERR_PTR(-ENOMEM));
237 * Destroy a lquota site.
239 * \param env - the environment passed by the caller
240 * \param site - lquota site to be destroyed
242 * \retval 0 - success
243 * \retval -ve - failure
245 void lquota_site_free(const struct lu_env *env, struct lquota_site *site)
247 /* cleanup hash table */
248 lqe_cleanup(site->lqs_hash, true);
249 cfs_hash_putref(site->lqs_hash);
251 site->lqs_parent = NULL;
256 * Initialize qsd/qmt-specific fields of quota entry.
258 * \param lqe - is the quota entry to initialize
260 static void lqe_init(struct lquota_entry *lqe)
262 struct lquota_site *site;
265 LASSERT(lqe != NULL);
266 site = lqe->lqe_site;
267 LASSERT(site != NULL);
268 LASSERT(site->lqs_ops->lqe_init != NULL);
270 LQUOTA_DEBUG(lqe, "init");
272 site->lqs_ops->lqe_init(lqe, site->lqs_parent);
276 * Update a lquota entry. This is done by reading quota settings from the
277 * on-disk index. The lquota entry must be write locked.
279 * \param env - the environment passed by the caller
280 * \param lqe - is the quota entry to refresh
282 static int lqe_read(const struct lu_env *env, struct lquota_entry *lqe)
284 struct lquota_site *site;
288 LASSERT(lqe != NULL);
289 site = lqe->lqe_site;
290 LASSERT(site != NULL);
291 LASSERT(site->lqs_ops->lqe_read != NULL);
293 LQUOTA_DEBUG(lqe, "read");
295 rc = site->lqs_ops->lqe_read(env, lqe, site->lqs_parent);
297 /* mark the entry as up-to-date */
298 lqe->lqe_uptodate = true;
304 * Find or create a quota entry.
306 * \param env - the environment passed by the caller
307 * \param site - lquota site which stores quota entries in a hash table
308 * \param qid - is the quota ID to be found/created
310 * \retval 0 - success
311 * \retval -ve - failure
313 struct lquota_entry *lqe_locate(const struct lu_env *env,
314 struct lquota_site *site, union lquota_id *qid)
316 struct lquota_entry *lqe, *new = NULL;
320 lqe = cfs_hash_lookup(site->lqs_hash, (void *)&qid->qid_uid);
322 LASSERT(lqe->lqe_uptodate);
326 OBD_SLAB_ALLOC_PTR_GFP(new, lqe_kmem, GFP_NOFS);
328 CERROR("Fail to allocate lqe for id:"LPU64", "
329 "hash:%s\n", qid->qid_uid, site->lqs_hash->hs_name);
330 RETURN(ERR_PTR(-ENOMEM));
333 atomic_set(&new->lqe_ref, 1); /* hold 1 for caller */
335 new->lqe_site = site;
336 INIT_LIST_HEAD(&new->lqe_link);
338 /* quota settings need to be updated from disk, that's why
339 * lqe->lqe_uptodate isn't set yet */
340 new->lqe_uptodate = false;
342 /* perform qmt/qsd specific initialization */
345 /* read quota settings from disk and mark lqe as up-to-date */
346 rc = lqe_read(env, new);
348 GOTO(out, lqe = ERR_PTR(rc));
350 /* add new entry to hash */
351 lqe = cfs_hash_findadd_unique(site->lqs_hash, &new->lqe_id.qid_uid,