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, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/lov/lov_pool.c
35 * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
36 * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
37 * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
40 #define DEBUG_SUBSYSTEM S_LOV
42 #include <libcfs/libcfs.h>
43 #include <libcfs/linux/linux-hash.h>
44 #include <libcfs/linux/linux-fs.h>
47 #include "lov_internal.h"
49 #define pool_tgt(_p, _i) \
50 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
52 static u32 pool_hashfh(const void *data, u32 len, u32 seed)
54 const char *pool_name = data;
56 return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
60 static int pool_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
62 const struct pool_desc *pool = obj;
63 const char *pool_name = arg->key;
65 return strcmp(pool_name, pool->pool_name);
68 static const struct rhashtable_params pools_hash_params = {
69 .key_len = 1, /* actually variable */
70 .key_offset = offsetof(struct pool_desc, pool_name),
71 .head_offset = offsetof(struct pool_desc, pool_hash),
72 .hashfn = pool_hashfh,
73 .obj_cmpfn = pool_cmpfn,
74 .automatic_shrinking = true,
77 static void lov_pool_getref(struct pool_desc *pool)
79 CDEBUG(D_INFO, "pool %p\n", pool);
80 atomic_inc(&pool->pool_refcount);
83 void lov_pool_putref(struct pool_desc *pool)
85 CDEBUG(D_INFO, "pool %p\n", pool);
86 if (atomic_dec_and_test(&pool->pool_refcount)) {
87 LASSERT(list_empty(&pool->pool_list));
88 LASSERT(pool->pool_proc_entry == NULL);
89 lu_tgt_pool_free(&(pool->pool_obds));
90 kfree_rcu(pool, pool_rcu);
97 * pool /proc seq_file methods
100 * iterator is used to go through the target pool entries
101 * index is the current entry index in the lp_array[] array
102 * index >= pos returned to the seq_file interface
103 * pos is from 0 to (pool->pool_obds.op_count - 1)
105 #define POOL_IT_MAGIC 0xB001CEA0
106 struct pool_iterator {
108 struct pool_desc *pool;
109 int idx; /* from 0 to pool_tgt_size - 1 */
112 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
114 struct pool_iterator *iter = (struct pool_iterator *)s->private;
117 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
120 /* test if end of file */
121 if (*pos > pool_tgt_count(iter->pool))
124 /* iterate to find a non empty entry */
125 prev_idx = iter->idx;
127 if (iter->idx >= pool_tgt_count(iter->pool)) {
128 iter->idx = prev_idx; /* we stay on the last entry */
131 /* return != NULL to continue */
135 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
137 struct pool_desc *pool = (struct pool_desc *)s->private;
138 struct pool_iterator *iter;
140 lov_pool_getref(pool);
141 if ((pool_tgt_count(pool) == 0) ||
142 (*pos >= pool_tgt_count(pool))) {
143 /* iter is not created, so stop() has no way to
144 * find pool to dec ref */
145 lov_pool_putref(pool);
151 return ERR_PTR(-ENOMEM);
152 iter->magic = POOL_IT_MAGIC;
156 /* we use seq_file private field to memorized iterator so
157 * we can free it at stop() */
158 /* /!\ do not forget to restore it to pool before freeing it */
160 down_read(&pool_tgt_rw_sem(pool));
167 ptr = pool_proc_next(s, &iter, &i);
168 } while ((i < *pos) && (ptr != NULL));
174 static void pool_proc_stop(struct seq_file *s, void *v)
176 struct pool_iterator *iter = (struct pool_iterator *)s->private;
178 /* in some cases stop() method is called 2 times, without
179 * calling start() method (see seq_read() from fs/seq_file.c)
180 * we have to free only if s->private is an iterator */
181 if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
182 up_read(&pool_tgt_rw_sem(iter->pool));
183 /* we restore s->private so next call to pool_proc_start()
185 s->private = iter->pool;
186 lov_pool_putref(iter->pool);
191 static int pool_proc_show(struct seq_file *s, void *v)
193 struct pool_iterator *iter = (struct pool_iterator *)v;
194 struct lov_tgt_desc *tgt;
196 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
197 LASSERT(iter->pool != NULL);
198 LASSERT(iter->idx <= pool_tgt_count(iter->pool));
200 tgt = pool_tgt(iter->pool, iter->idx);
202 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
207 static const struct seq_operations pool_proc_ops = {
208 .start = pool_proc_start,
209 .next = pool_proc_next,
210 .stop = pool_proc_stop,
211 .show = pool_proc_show,
214 static int pool_proc_open(struct inode *inode, struct file *file)
218 rc = seq_open(file, &pool_proc_ops);
220 struct seq_file *s = file->private_data;
221 s->private = pde_data(inode);
226 const static struct proc_ops pool_proc_operations = {
227 .proc_open = pool_proc_open,
228 .proc_read = seq_read,
229 .proc_lseek = seq_lseek,
230 .proc_release = seq_release,
232 #endif /* CONFIG_PROC_FS */
234 static void pools_hash_exit(void *vpool, void *data)
236 struct pool_desc *pool = vpool;
238 lov_pool_putref(pool);
241 int lov_pool_hash_init(struct rhashtable *tbl)
243 return rhashtable_init(tbl, &pools_hash_params);
246 void lov_pool_hash_destroy(struct rhashtable *tbl)
248 rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
251 int lov_pool_new(struct obd_device *obd, char *poolname)
254 struct pool_desc *new_pool;
260 if (strlen(poolname) > LOV_MAXPOOLNAME)
261 RETURN(-ENAMETOOLONG);
263 /* OBD_ALLOC doesn't work with direct use of kfree_rcu */
264 new_pool = kmalloc(sizeof(*new_pool), GFP_KERNEL);
265 if (new_pool == NULL)
268 strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
269 new_pool->pool_lobd = obd;
270 /* ref count init to 1 because when created a pool is always used
273 atomic_set(&new_pool->pool_refcount, 1);
274 rc = lu_tgt_pool_init(&new_pool->pool_obds, 0);
276 GOTO(out_free_pool, rc);
278 #ifdef CONFIG_PROC_FS
279 /* get ref for /proc file */
280 lov_pool_getref(new_pool);
281 new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
283 &pool_proc_operations);
284 if (IS_ERR(new_pool->pool_proc_entry)) {
285 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
286 new_pool->pool_proc_entry = NULL;
287 lov_pool_putref(new_pool);
289 CDEBUG(D_INFO, "pool %p - proc %p\n",
290 new_pool, new_pool->pool_proc_entry);
293 spin_lock(&obd->obd_dev_lock);
294 list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
295 lov->lov_pool_count++;
296 spin_unlock(&obd->obd_dev_lock);
298 /* Add to hash table only when it is fully ready. */
299 rc = rhashtable_lookup_insert_fast(&lov->lov_pools_hash_body,
300 &new_pool->pool_hash,
305 * Hide -E2BIG and -EBUSY which
312 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
313 poolname, lov->lov_pool_count);
318 spin_lock(&obd->obd_dev_lock);
319 list_del_init(&new_pool->pool_list);
320 lov->lov_pool_count--;
321 spin_unlock(&obd->obd_dev_lock);
322 lprocfs_remove(&new_pool->pool_proc_entry);
323 lu_tgt_pool_free(&new_pool->pool_obds);
325 OBD_FREE_PTR(new_pool);
330 struct pool_desc *lov_pool_find(struct obd_device *obd, char *poolname)
332 struct pool_desc *pool;
333 struct lov_obd *lov = &obd->u.lov;
336 pool = rhashtable_lookup(&lov->lov_pools_hash_body,
339 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
346 int lov_pool_del(struct obd_device *obd, char *poolname)
349 struct pool_desc *pool;
354 /* lookup and kill hash reference */
356 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
358 if (pool && rhashtable_remove_fast(&lov->lov_pools_hash_body,
360 pools_hash_params) != 0)
366 if (pool->pool_proc_entry != NULL) {
367 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
368 lprocfs_remove(&pool->pool_proc_entry);
369 lov_pool_putref(pool);
372 spin_lock(&obd->obd_dev_lock);
373 list_del_init(&pool->pool_list);
374 lov->lov_pool_count--;
375 spin_unlock(&obd->obd_dev_lock);
377 /* release last reference */
378 lov_pool_putref(pool);
384 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
386 struct obd_uuid ost_uuid;
388 struct pool_desc *pool;
389 unsigned int lov_idx;
396 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
398 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
404 obd_str2uuid(&ost_uuid, ostname);
407 /* search ost in lov array */
408 lov_tgts_getref(obd);
409 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
410 if (!lov->lov_tgts[lov_idx])
412 if (obd_uuid_equals(&ost_uuid,
413 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
416 /* test if ost found in lov */
417 if (lov_idx == lov->desc.ld_tgt_count)
418 GOTO(out, rc = -EINVAL);
420 rc = lu_tgt_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
424 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
425 ostname, poolname, pool_tgt_count(pool));
429 lov_tgts_putref(obd);
430 lov_pool_putref(pool);
435 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
437 struct obd_uuid ost_uuid;
439 struct pool_desc *pool;
440 unsigned int lov_idx;
446 /* lookup and kill hash reference */
448 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
450 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
456 obd_str2uuid(&ost_uuid, ostname);
458 lov_tgts_getref(obd);
459 /* search ost in lov array, to get index */
460 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
461 if (!lov->lov_tgts[lov_idx])
464 if (obd_uuid_equals(&ost_uuid,
465 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
469 /* test if ost found in lov */
470 if (lov_idx == lov->desc.ld_tgt_count)
471 GOTO(out, rc = -EINVAL);
473 lu_tgt_pool_remove(&pool->pool_obds, lov_idx);
475 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
480 lov_tgts_putref(obd);
481 lov_pool_putref(pool);