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 [sun.com URL with a
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lov/lov_pool.c
40 * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
41 * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
42 * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
45 #define DEBUG_SUBSYSTEM S_LOV
47 #include <libcfs/libcfs.h>
50 #include "lov_internal.h"
52 #define pool_tgt(_p, _i) \
53 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
55 static void lov_pool_getref(struct pool_desc *pool)
57 CDEBUG(D_INFO, "pool %p\n", pool);
58 atomic_inc(&pool->pool_refcount);
61 static void lov_pool_putref(struct pool_desc *pool)
63 CDEBUG(D_INFO, "pool %p\n", pool);
64 if (atomic_dec_and_test(&pool->pool_refcount)) {
65 LASSERT(hlist_unhashed(&pool->pool_hash));
66 LASSERT(list_empty(&pool->pool_list));
67 LASSERT(pool->pool_proc_entry == NULL);
68 lov_ost_pool_free(&(pool->pool_obds));
74 static void lov_pool_putref_locked(struct pool_desc *pool)
76 CDEBUG(D_INFO, "pool %p\n", pool);
77 LASSERT(atomic_read(&pool->pool_refcount) > 1);
79 atomic_dec(&pool->pool_refcount);
83 * hash function using a Rotating Hash algorithm
84 * Knuth, D. The Art of Computer Programming,
85 * Volume 3: Sorting and Searching,
87 * Addison Wesley, 1973
89 static __u32 pool_hashfn(struct cfs_hash *hash_body, const void *key,
97 poolname = (char *)key;
98 for (i = 0; i < LOV_MAXPOOLNAME; i++) {
99 if (poolname[i] == '\0')
101 result = (result << 4)^(result >> 28) ^ poolname[i];
103 return (result % mask);
106 static void *pool_key(struct hlist_node *hnode)
108 struct pool_desc *pool;
110 pool = hlist_entry(hnode, struct pool_desc, pool_hash);
111 return (pool->pool_name);
115 pool_hashkey_keycmp(const void *key, struct hlist_node *compared_hnode)
118 struct pool_desc *pool;
120 pool_name = (char *)key;
121 pool = hlist_entry(compared_hnode, struct pool_desc, pool_hash);
122 return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
125 static void *pool_hashobject(struct hlist_node *hnode)
127 return hlist_entry(hnode, struct pool_desc, pool_hash);
130 static void pool_hashrefcount_get(struct cfs_hash *hs, struct hlist_node *hnode)
132 struct pool_desc *pool;
134 pool = hlist_entry(hnode, struct pool_desc, pool_hash);
135 lov_pool_getref(pool);
138 static void pool_hashrefcount_put_locked(struct cfs_hash *hs,
139 struct hlist_node *hnode)
141 struct pool_desc *pool;
143 pool = hlist_entry(hnode, struct pool_desc, pool_hash);
144 lov_pool_putref_locked(pool);
147 struct cfs_hash_ops pool_hash_operations = {
148 .hs_hash = pool_hashfn,
150 .hs_keycmp = pool_hashkey_keycmp,
151 .hs_object = pool_hashobject,
152 .hs_get = pool_hashrefcount_get,
153 .hs_put_locked = pool_hashrefcount_put_locked,
157 #ifdef CONFIG_PROC_FS
158 /* ifdef needed for liblustre support */
160 * pool /proc seq_file methods
163 * iterator is used to go through the target pool entries
164 * index is the current entry index in the lp_array[] array
165 * index >= pos returned to the seq_file interface
166 * pos is from 0 to (pool->pool_obds.op_count - 1)
168 #define POOL_IT_MAGIC 0xB001CEA0
169 struct pool_iterator {
171 struct pool_desc *pool;
172 int idx; /* from 0 to pool_tgt_size - 1 */
175 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
177 struct pool_iterator *iter = (struct pool_iterator *)s->private;
180 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
182 /* test if end of file */
183 if (*pos >= pool_tgt_count(iter->pool))
186 /* iterate to find a non empty entry */
187 prev_idx = iter->idx;
188 down_read(&pool_tgt_rw_sem(iter->pool));
190 if (iter->idx == pool_tgt_count(iter->pool)) {
191 iter->idx = prev_idx; /* we stay on the last entry */
192 up_read(&pool_tgt_rw_sem(iter->pool));
195 up_read(&pool_tgt_rw_sem(iter->pool));
197 /* return != NULL to continue */
201 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
203 struct pool_desc *pool = (struct pool_desc *)s->private;
204 struct pool_iterator *iter;
206 lov_pool_getref(pool);
207 if ((pool_tgt_count(pool) == 0) ||
208 (*pos >= pool_tgt_count(pool))) {
209 /* iter is not created, so stop() has no way to
210 * find pool to dec ref */
211 lov_pool_putref(pool);
217 return ERR_PTR(-ENOMEM);
218 iter->magic = POOL_IT_MAGIC;
222 /* we use seq_file private field to memorized iterator so
223 * we can free it at stop() */
224 /* /!\ do not forget to restore it to pool before freeing it */
232 ptr = pool_proc_next(s, &iter, &i);
233 } while ((i < *pos) && (ptr != NULL));
239 static void pool_proc_stop(struct seq_file *s, void *v)
241 struct pool_iterator *iter = (struct pool_iterator *)s->private;
243 /* in some cases stop() method is called 2 times, without
244 * calling start() method (see seq_read() from fs/seq_file.c)
245 * we have to free only if s->private is an iterator */
246 if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
247 /* we restore s->private so next call to pool_proc_start()
249 s->private = iter->pool;
250 lov_pool_putref(iter->pool);
256 static int pool_proc_show(struct seq_file *s, void *v)
258 struct pool_iterator *iter = (struct pool_iterator *)v;
259 struct lov_tgt_desc *tgt;
261 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
262 LASSERT(iter->pool != NULL);
263 LASSERT(iter->idx <= pool_tgt_count(iter->pool));
265 down_read(&pool_tgt_rw_sem(iter->pool));
266 tgt = pool_tgt(iter->pool, iter->idx);
267 up_read(&pool_tgt_rw_sem(iter->pool));
269 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
274 static struct seq_operations pool_proc_ops = {
275 .start = pool_proc_start,
276 .next = pool_proc_next,
277 .stop = pool_proc_stop,
278 .show = pool_proc_show,
281 static int pool_proc_open(struct inode *inode, struct file *file)
285 rc = seq_open(file, &pool_proc_ops);
287 struct seq_file *s = file->private_data;
288 s->private = PDE_DATA(inode);
293 static struct file_operations pool_proc_operations = {
294 .open = pool_proc_open,
297 .release = seq_release,
299 #endif /* CONFIG_PROC_FS */
301 void lov_dump_pool(int level, struct pool_desc *pool)
305 lov_pool_getref(pool);
307 CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
308 pool->pool_name, pool->pool_obds.op_count);
309 down_read(&pool_tgt_rw_sem(pool));
311 for (i = 0; i < pool_tgt_count(pool) ; i++) {
312 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
314 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
316 obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
319 up_read(&pool_tgt_rw_sem(pool));
320 lov_pool_putref(pool);
323 #define LOV_POOL_INIT_COUNT 2
324 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
329 count = LOV_POOL_INIT_COUNT;
332 init_rwsem(&op->op_rw_sem);
334 OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
335 if (op->op_array == NULL) {
343 /* Caller must hold write op_rwlock */
344 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
349 LASSERT(min_count != 0);
351 if (op->op_count < op->op_size)
354 new_size = max(min_count, 2 * op->op_size);
355 OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
359 /* copy old array to new one */
360 memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
361 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
363 op->op_size = new_size;
367 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
372 down_write(&op->op_rw_sem);
374 rc = lov_ost_pool_extend(op, min_count);
378 /* search ost in pool array */
379 for (i = 0; i < op->op_count; i++) {
380 if (op->op_array[i] == idx)
381 GOTO(out, rc = -EEXIST);
383 /* ost not found we add it */
384 op->op_array[op->op_count] = idx;
388 up_write(&op->op_rw_sem);
392 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
397 down_write(&op->op_rw_sem);
399 for (i = 0; i < op->op_count; i++) {
400 if (op->op_array[i] == idx) {
401 memmove(&op->op_array[i], &op->op_array[i + 1],
402 (op->op_count - i - 1) * sizeof(op->op_array[0]));
404 up_write(&op->op_rw_sem);
410 up_write(&op->op_rw_sem);
414 int lov_ost_pool_free(struct ost_pool *op)
418 if (op->op_size == 0)
421 down_write(&op->op_rw_sem);
423 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
428 up_write(&op->op_rw_sem);
433 int lov_pool_new(struct obd_device *obd, char *poolname)
436 struct pool_desc *new_pool;
442 if (strlen(poolname) > LOV_MAXPOOLNAME)
443 RETURN(-ENAMETOOLONG);
445 OBD_ALLOC_PTR(new_pool);
446 if (new_pool == NULL)
449 strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
450 new_pool->pool_lobd = obd;
451 /* ref count init to 1 because when created a pool is always used
454 atomic_set(&new_pool->pool_refcount, 1);
455 rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
459 INIT_HLIST_NODE(&new_pool->pool_hash);
461 #ifdef CONFIG_PROC_FS
462 /* get ref for /proc file */
463 lov_pool_getref(new_pool);
464 new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
466 &pool_proc_operations);
467 if (IS_ERR(new_pool->pool_proc_entry)) {
468 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
469 new_pool->pool_proc_entry = NULL;
470 lov_pool_putref(new_pool);
472 CDEBUG(D_INFO, "pool %p - proc %p\n",
473 new_pool, new_pool->pool_proc_entry);
476 spin_lock(&obd->obd_dev_lock);
477 list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
478 lov->lov_pool_count++;
479 spin_unlock(&obd->obd_dev_lock);
481 /* add to find only when it fully ready */
482 rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
483 &new_pool->pool_hash);
485 GOTO(out_err, rc = -EEXIST);
487 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
488 poolname, lov->lov_pool_count);
493 spin_lock(&obd->obd_dev_lock);
494 list_del_init(&new_pool->pool_list);
495 lov->lov_pool_count--;
496 spin_unlock(&obd->obd_dev_lock);
497 lprocfs_remove(&new_pool->pool_proc_entry);
498 lov_ost_pool_free(&new_pool->pool_obds);
499 OBD_FREE_PTR(new_pool);
504 int lov_pool_del(struct obd_device *obd, char *poolname)
507 struct pool_desc *pool;
512 /* lookup and kill hash reference */
513 pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
517 if (pool->pool_proc_entry != NULL) {
518 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
519 lprocfs_remove(&pool->pool_proc_entry);
520 lov_pool_putref(pool);
523 spin_lock(&obd->obd_dev_lock);
524 list_del_init(&pool->pool_list);
525 lov->lov_pool_count--;
526 spin_unlock(&obd->obd_dev_lock);
528 /* release last reference */
529 lov_pool_putref(pool);
535 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
537 struct obd_uuid ost_uuid;
539 struct pool_desc *pool;
540 unsigned int lov_idx;
546 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
550 obd_str2uuid(&ost_uuid, ostname);
553 /* search ost in lov array */
555 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
556 if (!lov->lov_tgts[lov_idx])
558 if (obd_uuid_equals(&ost_uuid,
559 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
562 /* test if ost found in lov */
563 if (lov_idx == lov->desc.ld_tgt_count)
564 GOTO(out, rc = -EINVAL);
566 rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
570 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
571 ostname, poolname, pool_tgt_count(pool));
576 lov_pool_putref(pool);
580 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
582 struct obd_uuid ost_uuid;
584 struct pool_desc *pool;
585 unsigned int lov_idx;
591 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
595 obd_str2uuid(&ost_uuid, ostname);
598 /* search ost in lov array, to get index */
599 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
600 if (!lov->lov_tgts[lov_idx])
603 if (obd_uuid_equals(&ost_uuid,
604 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
608 /* test if ost found in lov */
609 if (lov_idx == lov->desc.ld_tgt_count)
610 GOTO(out, rc = -EINVAL);
612 lov_ost_pool_remove(&pool->pool_obds, lov_idx);
614 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
620 lov_pool_putref(pool);