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 2008 Sun Microsystems, Inc. All rights reserved
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lod/lod_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>
49 #include "lod_internal.h"
51 #define pool_tgt(_p, _i) \
52 OST_TGT(lu2lod_dev((_p)->pool_lobd->obd_lu_dev),(_p)->pool_obds.op_array[_i])
54 static void lod_pool_getref(struct pool_desc *pool)
56 CDEBUG(D_INFO, "pool %p\n", pool);
57 atomic_inc(&pool->pool_refcount);
60 void lod_pool_putref(struct pool_desc *pool)
62 CDEBUG(D_INFO, "pool %p\n", pool);
63 if (atomic_dec_and_test(&pool->pool_refcount)) {
64 LASSERT(cfs_hlist_unhashed(&pool->pool_hash));
65 LASSERT(cfs_list_empty(&pool->pool_list));
66 LASSERT(pool->pool_proc_entry == NULL);
67 lod_ost_pool_free(&(pool->pool_rr.lqr_pool));
68 lod_ost_pool_free(&(pool->pool_obds));
74 void lod_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);
84 * hash function using a Rotating Hash algorithm
85 * Knuth, D. The Art of Computer Programming,
86 * Volume 3: Sorting and Searching,
88 * Addison Wesley, 1973
90 static __u32 pool_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
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(cfs_hlist_node_t *hnode)
108 struct pool_desc *pool;
110 pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
111 return (pool->pool_name);
114 static int pool_hashkey_keycmp(const void *key, cfs_hlist_node_t *compared_hnode)
117 struct pool_desc *pool;
119 pool_name = (char *)key;
120 pool = cfs_hlist_entry(compared_hnode, struct pool_desc, pool_hash);
121 return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
124 static void *pool_hashobject(cfs_hlist_node_t *hnode)
126 return cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
129 static void pool_hashrefcount_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
131 struct pool_desc *pool;
133 pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
134 lod_pool_getref(pool);
137 static void pool_hashrefcount_put_locked(cfs_hash_t *hs,
138 cfs_hlist_node_t *hnode)
140 struct pool_desc *pool;
142 pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
143 lod_pool_putref_locked(pool);
146 cfs_hash_ops_t pool_hash_operations = {
147 .hs_hash = pool_hashfn,
149 .hs_keycmp = pool_hashkey_keycmp,
150 .hs_object = pool_hashobject,
151 .hs_get = pool_hashrefcount_get,
152 .hs_put_locked = pool_hashrefcount_put_locked,
156 /* ifdef needed for liblustre support */
158 * pool /proc seq_file methods
161 * iterator is used to go through the target pool entries
162 * index is the current entry index in the lp_array[] array
163 * index >= pos returned to the seq_file interface
164 * pos is from 0 to (pool->pool_obds.op_count - 1)
166 #define POOL_IT_MAGIC 0xB001CEA0
167 struct lod_pool_iterator {
169 int lpi_idx; /* from 0 to pool_tgt_size - 1 */
170 struct pool_desc *lpi_pool;
173 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
175 struct lod_pool_iterator *iter = s->private;
178 LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X", iter->lpi_magic);
180 /* test if end of file */
181 if (*pos >= pool_tgt_count(iter->lpi_pool))
184 /* iterate to find a non empty entry */
185 prev_idx = iter->lpi_idx;
186 down_read(&pool_tgt_rw_sem(iter->lpi_pool));
188 if (iter->lpi_idx == pool_tgt_count(iter->lpi_pool)) {
189 iter->lpi_idx = prev_idx; /* we stay on the last entry */
190 up_read(&pool_tgt_rw_sem(iter->lpi_pool));
193 up_read(&pool_tgt_rw_sem(iter->lpi_pool));
195 /* return != NULL to continue */
199 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
201 struct pool_desc *pool = s->private;
202 struct lod_pool_iterator *iter;
204 lod_pool_getref(pool);
205 if ((pool_tgt_count(pool) == 0) ||
206 (*pos >= pool_tgt_count(pool))) {
207 /* iter is not created, so stop() has no way to
208 * find pool to dec ref */
209 lod_pool_putref(pool);
215 return ERR_PTR(-ENOMEM);
216 iter->lpi_magic = POOL_IT_MAGIC;
217 iter->lpi_pool = pool;
220 /* we use seq_file private field to memorized iterator so
221 * we can free it at stop() */
222 /* /!\ do not forget to restore it to pool before freeing it */
230 ptr = pool_proc_next(s, &iter, &i);
231 } while ((i < *pos) && (ptr != NULL));
237 static void pool_proc_stop(struct seq_file *s, void *v)
239 struct lod_pool_iterator *iter = s->private;
241 /* in some cases stop() method is called 2 times, without
242 * calling start() method (see seq_read() from fs/seq_file.c)
243 * we have to free only if s->private is an iterator */
244 if (iter != NULL && (iter->lpi_magic == POOL_IT_MAGIC)) {
245 /* we restore s->private so next call to pool_proc_start()
247 s->private = iter->lpi_pool;
248 lod_pool_putref(iter->lpi_pool);
254 static int pool_proc_show(struct seq_file *s, void *v)
256 struct lod_pool_iterator *iter = v;
257 struct lod_tgt_desc *osc_desc;
259 LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X", iter->lpi_magic);
260 LASSERT(iter->lpi_pool != NULL);
261 LASSERT(iter->lpi_idx <= pool_tgt_count(iter->lpi_pool));
263 down_read(&pool_tgt_rw_sem(iter->lpi_pool));
264 osc_desc = pool_tgt(iter->lpi_pool, iter->lpi_idx);
265 up_read(&pool_tgt_rw_sem(iter->lpi_pool));
267 seq_printf(s, "%s\n", obd_uuid2str(&(osc_desc->ltd_uuid)));
272 static struct seq_operations pool_proc_ops = {
273 .start = pool_proc_start,
274 .next = pool_proc_next,
275 .stop = pool_proc_stop,
276 .show = pool_proc_show,
279 static int pool_proc_open(struct inode *inode, struct file *file)
283 rc = seq_open(file, &pool_proc_ops);
285 struct seq_file *s = file->private_data;
286 s->private = PDE_DATA(inode);
291 static struct file_operations pool_proc_operations = {
292 .open = pool_proc_open,
295 .release = seq_release,
299 void lod_dump_pool(int level, struct pool_desc *pool)
303 lod_pool_getref(pool);
305 CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
306 pool->pool_name, pool->pool_obds.op_count);
307 down_read(&pool_tgt_rw_sem(pool));
309 for (i = 0; i < pool_tgt_count(pool) ; i++) {
310 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
312 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
314 obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
317 up_read(&pool_tgt_rw_sem(pool));
318 lod_pool_putref(pool);
321 #define LOD_POOL_INIT_COUNT 2
322 int lod_ost_pool_init(struct ost_pool *op, unsigned int count)
327 count = LOD_POOL_INIT_COUNT;
330 init_rwsem(&op->op_rw_sem);
332 OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
333 if (op->op_array == NULL) {
341 /* Caller must hold write op_rwlock */
342 int lod_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
347 LASSERT(min_count != 0);
349 if (op->op_count < op->op_size)
352 new_size = max(min_count, 2 * op->op_size);
353 OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
357 /* copy old array to new one */
358 memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
359 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
361 op->op_size = new_size;
365 int lod_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
370 down_write(&op->op_rw_sem);
372 rc = lod_ost_pool_extend(op, min_count);
376 /* search ost in pool array */
377 for (i = 0; i < op->op_count; i++) {
378 if (op->op_array[i] == idx)
379 GOTO(out, rc = -EEXIST);
381 /* ost not found we add it */
382 op->op_array[op->op_count] = idx;
386 up_write(&op->op_rw_sem);
390 int lod_ost_pool_remove(struct ost_pool *op, __u32 idx)
395 down_write(&op->op_rw_sem);
397 for (i = 0; i < op->op_count; i++) {
398 if (op->op_array[i] == idx) {
399 memmove(&op->op_array[i], &op->op_array[i + 1],
400 (op->op_count - i - 1) * sizeof(op->op_array[0]));
402 up_write(&op->op_rw_sem);
408 up_write(&op->op_rw_sem);
412 int lod_ost_pool_free(struct ost_pool *op)
416 if (op->op_size == 0)
419 down_write(&op->op_rw_sem);
421 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
426 up_write(&op->op_rw_sem);
430 int lod_pool_new(struct obd_device *obd, char *poolname)
432 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
433 struct pool_desc *new_pool;
437 if (strlen(poolname) > LOV_MAXPOOLNAME)
438 RETURN(-ENAMETOOLONG);
440 OBD_ALLOC_PTR(new_pool);
441 if (new_pool == NULL)
444 strncpy(new_pool->pool_name, poolname, LOV_MAXPOOLNAME);
445 new_pool->pool_name[LOV_MAXPOOLNAME] = '\0';
446 new_pool->pool_lobd = obd;
447 /* ref count init to 1 because when created a pool is always used
450 atomic_set(&new_pool->pool_refcount, 1);
451 rc = lod_ost_pool_init(&new_pool->pool_obds, 0);
455 memset(&new_pool->pool_rr, 0, sizeof(new_pool->pool_rr));
456 rc = lod_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
458 GOTO(out_free_pool_obds, rc);
460 INIT_HLIST_NODE(&new_pool->pool_hash);
463 lod_pool_getref(new_pool);
464 new_pool->pool_proc_entry = lprocfs_add_simple(lod->lod_pool_proc_entry,
466 #ifndef HAVE_ONLY_PROCFS_SEQ
470 &pool_proc_operations);
471 if (IS_ERR(new_pool->pool_proc_entry)) {
472 CDEBUG(D_CONFIG, "%s: cannot add proc entry "LOV_POOLNAMEF"\n",
473 obd->obd_name, poolname);
474 new_pool->pool_proc_entry = NULL;
475 lod_pool_putref(new_pool);
477 CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool,
478 new_pool->pool_proc_entry);
481 spin_lock(&obd->obd_dev_lock);
482 cfs_list_add_tail(&new_pool->pool_list, &lod->lod_pool_list);
483 lod->lod_pool_count++;
484 spin_unlock(&obd->obd_dev_lock);
486 /* add to find only when it fully ready */
487 rc = cfs_hash_add_unique(lod->lod_pools_hash_body, poolname,
488 &new_pool->pool_hash);
490 GOTO(out_err, rc = -EEXIST);
492 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
493 poolname, lod->lod_pool_count);
498 spin_lock(&obd->obd_dev_lock);
499 cfs_list_del_init(&new_pool->pool_list);
500 lod->lod_pool_count--;
501 spin_unlock(&obd->obd_dev_lock);
503 lprocfs_remove(&new_pool->pool_proc_entry);
505 lod_ost_pool_free(&new_pool->pool_rr.lqr_pool);
507 lod_ost_pool_free(&new_pool->pool_obds);
508 OBD_FREE_PTR(new_pool);
512 int lod_pool_del(struct obd_device *obd, char *poolname)
514 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
515 struct pool_desc *pool;
518 /* lookup and kill hash reference */
519 pool = cfs_hash_del_key(lod->lod_pools_hash_body, poolname);
523 if (pool->pool_proc_entry != NULL) {
524 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
525 lprocfs_remove(&pool->pool_proc_entry);
526 lod_pool_putref(pool);
529 spin_lock(&obd->obd_dev_lock);
530 cfs_list_del_init(&pool->pool_list);
531 lod->lod_pool_count--;
532 spin_unlock(&obd->obd_dev_lock);
534 /* release last reference */
535 lod_pool_putref(pool);
541 int lod_pool_add(struct obd_device *obd, char *poolname, char *ostname)
543 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
544 struct obd_uuid ost_uuid;
545 struct pool_desc *pool;
550 pool = cfs_hash_lookup(lod->lod_pools_hash_body, poolname);
554 obd_str2uuid(&ost_uuid, ostname);
556 /* search ost in lod array */
557 lod_getref(&lod->lod_ost_descs);
558 lod_foreach_ost(lod, idx) {
559 if (obd_uuid_equals(&ost_uuid, &OST_TGT(lod, idx)->ltd_uuid)) {
568 rc = lod_ost_pool_add(&pool->pool_obds, idx, lod->lod_osts_size);
572 pool->pool_rr.lqr_dirty = 1;
574 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
575 ostname, poolname, pool_tgt_count(pool));
579 lod_putref(lod, &lod->lod_ost_descs);
580 lod_pool_putref(pool);
584 int lod_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
586 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
587 struct obd_uuid ost_uuid;
588 struct pool_desc *pool;
593 pool = cfs_hash_lookup(lod->lod_pools_hash_body, poolname);
597 obd_str2uuid(&ost_uuid, ostname);
599 lod_getref(&lod->lod_ost_descs);
600 /* search ost in lod array, to get index */
601 cfs_foreach_bit(lod->lod_ost_bitmap, idx) {
602 if (obd_uuid_equals(&ost_uuid, &OST_TGT(lod, idx)->ltd_uuid)) {
608 /* test if ost found in lod array */
612 lod_ost_pool_remove(&pool->pool_obds, idx);
614 pool->pool_rr.lqr_dirty = 1;
616 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
621 lod_putref(lod, &lod->lod_ost_descs);
622 lod_pool_putref(pool);
626 int lod_check_index_in_pool(__u32 idx, struct pool_desc *pool)
631 /* caller may no have a ref on pool if it got the pool
632 * without calling lod_find_pool() (e.g. go through the lod pool
635 lod_pool_getref(pool);
637 down_read(&pool_tgt_rw_sem(pool));
639 for (i = 0; i < pool_tgt_count(pool); i++) {
640 if (pool_tgt_array(pool)[i] == idx)
646 up_read(&pool_tgt_rw_sem(pool));
648 lod_pool_putref(pool);
652 struct pool_desc *lod_find_pool(struct lod_device *lod, char *poolname)
654 struct pool_desc *pool;
657 if (poolname[0] != '\0') {
658 pool = cfs_hash_lookup(lod->lod_pools_hash_body, poolname);
660 CDEBUG(D_CONFIG, "%s: request for an unknown pool ("
662 lod->lod_child_exp->exp_obd->obd_name, poolname);
663 if (pool != NULL && pool_tgt_count(pool) == 0) {
664 CDEBUG(D_CONFIG, "%s: request for an empty pool ("
666 lod->lod_child_exp->exp_obd->obd_name, poolname);
667 /* pool is ignored, so we remove ref on it */
668 lod_pool_putref(pool);