Whamcloud - gitweb
LU-1346 libcfs: replace cfs_ memory wrappers
[fs/lustre-release.git] / lustre / quota / lquota_entry.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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.
8  *
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).
14  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, Intel Corporation.
24  * Use is subject to license terms.
25  *
26  * Author: Johann Lombardi <johann.lombardi@intel.com>
27  * Author: Niu    Yawei    <yawei.niu@intel.com>
28  */
29
30 #ifndef EXPORT_SYMTAB
31 # define EXPORT_SYMTAB
32 #endif
33
34 #define DEBUG_SUBSYSTEM S_LQUOTA
35
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <obd_class.h>
39 #include "lquota_internal.h"
40
41 static int hash_lqs_cur_bits = HASH_LQE_CUR_BITS;
42 CFS_MODULE_PARM(hash_lqs_cur_bits, "i", int, 0444,
43                 "the current bits of lqe hash");
44
45 static unsigned lqe64_hash_hash(cfs_hash_t *hs, const void *key, unsigned mask)
46 {
47         return cfs_hash_u64_hash(*((__u64 *)key), mask);
48 }
49
50 static void *lqe64_hash_key(cfs_hlist_node_t *hnode)
51 {
52         struct lquota_entry *lqe;
53         lqe = cfs_hlist_entry(hnode, struct lquota_entry, lqe_hash);
54         return &lqe->lqe_id.qid_uid;
55 }
56
57 static int lqe64_hash_keycmp(const void *key, cfs_hlist_node_t *hnode)
58 {
59         struct lquota_entry *lqe;
60         lqe = cfs_hlist_entry(hnode, struct lquota_entry, lqe_hash);
61         return (lqe->lqe_id.qid_uid == *((__u64*)key));
62 }
63
64 static void *lqe_hash_object(cfs_hlist_node_t *hnode)
65 {
66         return cfs_hlist_entry(hnode, struct lquota_entry, lqe_hash);
67 }
68
69 static void lqe_hash_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
70 {
71         struct lquota_entry *lqe;
72         lqe = cfs_hlist_entry(hnode, struct lquota_entry, lqe_hash);
73         lqe_getref(lqe);
74 }
75
76 static void lqe_hash_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
77 {
78         struct lquota_entry *lqe;
79         lqe = cfs_hlist_entry(hnode, struct lquota_entry, lqe_hash);
80         lqe_putref(lqe);
81 }
82
83 static void lqe_hash_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
84 {
85         CERROR("Should not have any item left!\n");
86 }
87
88 /* lqe hash methods for 64-bit uid/gid, new hash functions would have to be
89  * defined for per-directory quota relying on a 128-bit FID */
90 static cfs_hash_ops_t lqe64_hash_ops = {
91         .hs_hash       = lqe64_hash_hash,
92         .hs_key        = lqe64_hash_key,
93         .hs_keycmp     = lqe64_hash_keycmp,
94         .hs_object     = lqe_hash_object,
95         .hs_get        = lqe_hash_get,
96         .hs_put_locked = lqe_hash_put_locked,
97         .hs_exit       = lqe_hash_exit
98 };
99
100 /* Logging helper function */
101 void lquota_lqe_debug0(struct lquota_entry *lqe,
102                        struct libcfs_debug_msg_data *msgdata,
103                        const char *fmt, ...)
104 {
105         struct lquota_site *site = lqe->lqe_site;
106         va_list args;
107
108         LASSERT(site->lqs_ops->lqe_debug != NULL);
109
110         va_start(args, fmt);
111         site->lqs_ops->lqe_debug(lqe, site->lqs_parent, msgdata, fmt, args);
112         va_end(args);
113 }
114
115 struct lqe_iter_data {
116         unsigned long   lid_inuse;
117         unsigned long   lid_freed;
118         bool            lid_free_all;
119 };
120
121 static int lqe_iter_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
122                        cfs_hlist_node_t *hnode, void *data)
123 {
124         struct lqe_iter_data *d = (struct lqe_iter_data *)data;
125         struct lquota_entry  *lqe;
126
127         lqe = cfs_hlist_entry(hnode, struct lquota_entry, lqe_hash);
128         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
129
130         /* Only one reference held by hash table, and nobody else can
131          * grab the entry at this moment, it's safe to remove it from
132          * the hash and free it. */
133         if (atomic_read(&lqe->lqe_ref) == 1) {
134                 if (!lqe_is_master(lqe)) {
135                         LASSERT(lqe->lqe_pending_write == 0);
136                         LASSERT(lqe->lqe_pending_req == 0);
137                 }
138                 if (d->lid_free_all || lqe->lqe_enforced) {
139                         d->lid_freed++;
140                         cfs_hash_bd_del_locked(hs, bd, hnode);
141                         return 0;
142                 }
143         }
144         d->lid_inuse++;
145
146         if (d->lid_free_all)
147                 LQUOTA_ERROR(lqe, "Inuse quota entry");
148         return 0;
149 }
150
151 /**
152  * Cleanup the entries in the hashtable
153  *
154  * \param hash     - hash table which stores quota entries
155  * \param free_all - free all entries or only free the entries
156  *                   without quota enforce ?
157  */
158 static void lqe_cleanup(cfs_hash_t *hash, bool free_all)
159 {
160         struct lqe_iter_data    d;
161         int                     repeat = 0;
162         ENTRY;
163 retry:
164         memset(&d, 0, sizeof(d));
165         d.lid_free_all = free_all;
166
167         cfs_hash_for_each_safe(hash, lqe_iter_cb, &d);
168
169         /* In most case, when this function is called on master or
170          * slave finalization, there should be no inuse quota entry.
171          *
172          * If the per-fs quota updating thread is still holding
173          * some entries, we just wait for it's finished. */
174         if (free_all && d.lid_inuse) {
175                 CDEBUG(D_QUOTA, "Hash:%p has entries inuse: inuse:%lu, "
176                         "freed:%lu, repeat:%u\n", hash,
177                         d.lid_inuse, d.lid_freed, repeat);
178                 repeat++;
179                 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
180                                                 cfs_time_seconds(1));
181                 goto retry;
182         }
183         EXIT;
184 }
185
186 /*
187  * Allocate a new lquota site.
188  *
189  * \param env    - the environment passed by the caller
190  * \param parent - is a pointer to the parent structure, either a qmt_pool_info
191  *                 structure on the master or a qsd_qtype_info structure on the
192  *                 slave.
193  * \param is_master - is set when the site belongs to a QMT.
194  * \param qtype     - is the quota type managed by this site
195  * \param ops       - is the quota entry operation vector to be used for quota
196  *                    entry belonging to this site.
197  *
198  * \retval 0     - success
199  * \retval -ve   - failure
200  */
201 struct lquota_site *lquota_site_alloc(const struct lu_env *env, void *parent,
202                                       bool is_master, short qtype,
203                                       struct lquota_entry_operations *ops)
204 {
205         struct lquota_site      *site;
206         char                     hashname[15];
207         ENTRY;
208
209         LASSERT(qtype < MAXQUOTAS);
210
211         OBD_ALLOC_PTR(site);
212         if (site == NULL)
213                 RETURN(ERR_PTR(-ENOMEM));
214
215         /* assign parameters */
216         site->lqs_qtype  = qtype;
217         site->lqs_parent = parent;
218         site->lqs_is_mst = is_master;
219         site->lqs_ops    = ops;
220
221         /* allocate hash table */
222         memset(hashname, 0, sizeof(hashname));
223         sprintf(hashname, "LQUOTA_HASH%u", qtype);
224         site->lqs_hash= cfs_hash_create(hashname, hash_lqs_cur_bits,
225                                         HASH_LQE_MAX_BITS,
226                                         min(hash_lqs_cur_bits,
227                                             HASH_LQE_BKT_BITS),
228                                         0, CFS_HASH_MIN_THETA,
229                                         CFS_HASH_MAX_THETA, &lqe64_hash_ops,
230                                         CFS_HASH_DEFAULT|CFS_HASH_BIGNAME);
231         if (site->lqs_hash == NULL) {
232                 OBD_FREE_PTR(site);
233                 RETURN(ERR_PTR(-ENOMEM));
234         }
235
236         RETURN(site);
237 }
238
239 /*
240  * Destroy a lquota site.
241  *
242  * \param env  - the environment passed by the caller
243  * \param site - lquota site to be destroyed
244  *
245  * \retval 0     - success
246  * \retval -ve   - failure
247  */
248 void lquota_site_free(const struct lu_env *env, struct lquota_site *site)
249 {
250         /* cleanup hash table */
251         lqe_cleanup(site->lqs_hash, true);
252         cfs_hash_putref(site->lqs_hash);
253
254         site->lqs_parent = NULL;
255         OBD_FREE_PTR(site);
256 }
257
258 /*
259  * Initialize qsd/qmt-specific fields of quota entry.
260  *
261  * \param lqe - is the quota entry to initialize
262  */
263 static void lqe_init(struct lquota_entry *lqe)
264 {
265         struct lquota_site *site;
266         ENTRY;
267
268         LASSERT(lqe != NULL);
269         site = lqe->lqe_site;
270         LASSERT(site != NULL);
271         LASSERT(site->lqs_ops->lqe_init != NULL);
272
273         LQUOTA_DEBUG(lqe, "init");
274
275         site->lqs_ops->lqe_init(lqe, site->lqs_parent);
276 }
277
278 /*
279  * Update a lquota entry. This is done by reading quota settings from the
280  * on-disk index. The lquota entry must be write locked.
281  *
282  * \param env - the environment passed by the caller
283  * \param lqe - is the quota entry to refresh
284  */
285 static int lqe_read(const struct lu_env *env, struct lquota_entry *lqe)
286 {
287         struct lquota_site      *site;
288         int                      rc;
289         ENTRY;
290
291         LASSERT(lqe != NULL);
292         site = lqe->lqe_site;
293         LASSERT(site != NULL);
294         LASSERT(site->lqs_ops->lqe_read != NULL);
295
296         LQUOTA_DEBUG(lqe, "read");
297
298         rc = site->lqs_ops->lqe_read(env, lqe, site->lqs_parent);
299         if (rc == 0)
300                 /* mark the entry as up-to-date */
301                 lqe->lqe_uptodate = true;
302
303         RETURN(rc);
304 }
305
306 /*
307  * Find or create a quota entry.
308  *
309  * \param env  - the environment passed by the caller
310  * \param site - lquota site which stores quota entries in a hash table
311  * \param qid  - is the quota ID to be found/created
312  *
313  * \retval 0     - success
314  * \retval -ve   - failure
315  */
316 struct lquota_entry *lqe_locate(const struct lu_env *env,
317                                 struct lquota_site *site, union lquota_id *qid)
318 {
319         struct lquota_entry     *lqe, *new = NULL;
320         int                      rc = 0;
321         ENTRY;
322
323         lqe = cfs_hash_lookup(site->lqs_hash, (void *)&qid->qid_uid);
324         if (lqe != NULL) {
325                 LASSERT(lqe->lqe_uptodate);
326                 RETURN(lqe);
327         }
328
329         OBD_SLAB_ALLOC_PTR_GFP(new, lqe_kmem, __GFP_IO);
330         if (new == NULL) {
331                 CERROR("Fail to allocate lqe for id:"LPU64", "
332                         "hash:%s\n", qid->qid_uid, site->lqs_hash->hs_name);
333                 RETURN(ERR_PTR(-ENOMEM));
334         }
335
336         atomic_set(&new->lqe_ref, 1); /* hold 1 for caller */
337         new->lqe_id     = *qid;
338         new->lqe_site   = site;
339         CFS_INIT_LIST_HEAD(&new->lqe_link);
340
341         /* quota settings need to be updated from disk, that's why
342          * lqe->lqe_uptodate isn't set yet */
343         new->lqe_uptodate = false;
344
345         /* perform qmt/qsd specific initialization */
346         lqe_init(new);
347
348         /* read quota settings from disk and mark lqe as up-to-date */
349         rc = lqe_read(env, new);
350         if (rc)
351                 GOTO(out, lqe = ERR_PTR(rc));
352
353         /* add new entry to hash */
354         lqe = cfs_hash_findadd_unique(site->lqs_hash, &new->lqe_id.qid_uid,
355                                       &new->lqe_hash);
356         if (lqe == new)
357                 new = NULL;
358 out:
359         if (new)
360                 lqe_putref(new);
361         RETURN(lqe);
362 }