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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/lov/lov_pool.c
38 * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
39 * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
40 * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
43 #define DEBUG_SUBSYSTEM S_LOV
46 #include <libcfs/libcfs.h>
48 #include <liblustre.h>
52 #include "lov_internal.h"
54 #define pool_tgt(_p, _i) \
55 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
57 static void lov_pool_getref(struct pool_desc *pool)
59 CDEBUG(D_INFO, "pool %p\n", pool);
60 cfs_atomic_inc(&pool->pool_refcount);
63 void lov_pool_putref(struct pool_desc *pool)
65 CDEBUG(D_INFO, "pool %p\n", pool);
66 if (cfs_atomic_dec_and_test(&pool->pool_refcount)) {
67 LASSERT(cfs_hlist_unhashed(&pool->pool_hash));
68 LASSERT(cfs_list_empty(&pool->pool_list));
69 LASSERT(pool->pool_proc_entry == NULL);
70 lov_ost_pool_free(&(pool->pool_rr.lqr_pool));
71 lov_ost_pool_free(&(pool->pool_obds));
77 void lov_pool_putref_locked(struct pool_desc *pool)
79 CDEBUG(D_INFO, "pool %p\n", pool);
80 LASSERT(cfs_atomic_read(&pool->pool_refcount) > 1);
82 cfs_atomic_dec(&pool->pool_refcount);
86 * hash function using a Rotating Hash algorithm
87 * Knuth, D. The Art of Computer Programming,
88 * Volume 3: Sorting and Searching,
90 * Addison Wesley, 1973
92 static __u32 pool_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
99 poolname = (char *)key;
100 for (i = 0; i < LOV_MAXPOOLNAME; i++) {
101 if (poolname[i] == '\0')
103 result = (result << 4)^(result >> 28) ^ poolname[i];
105 return (result % mask);
108 static void *pool_key(cfs_hlist_node_t *hnode)
110 struct pool_desc *pool;
112 pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
113 return (pool->pool_name);
116 static int pool_hashkey_keycmp(const void *key, cfs_hlist_node_t *compared_hnode)
119 struct pool_desc *pool;
121 pool_name = (char *)key;
122 pool = cfs_hlist_entry(compared_hnode, struct pool_desc, pool_hash);
123 return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
126 static void *pool_hashobject(cfs_hlist_node_t *hnode)
128 return cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
131 static void pool_hashrefcount_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
133 struct pool_desc *pool;
135 pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
136 lov_pool_getref(pool);
139 static void pool_hashrefcount_put_locked(cfs_hash_t *hs,
140 cfs_hlist_node_t *hnode)
142 struct pool_desc *pool;
144 pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
145 lov_pool_putref_locked(pool);
148 cfs_hash_ops_t pool_hash_operations = {
149 .hs_hash = pool_hashfn,
151 .hs_keycmp = pool_hashkey_keycmp,
152 .hs_object = pool_hashobject,
153 .hs_get = pool_hashrefcount_get,
154 .hs_put_locked = pool_hashrefcount_put_locked,
159 /* ifdef needed for liblustre support */
161 * pool /proc seq_file methods
164 * iterator is used to go through the target pool entries
165 * index is the current entry index in the lp_array[] array
166 * index >= pos returned to the seq_file interface
167 * pos is from 0 to (pool->pool_obds.op_count - 1)
169 #define POOL_IT_MAGIC 0xB001CEA0
170 struct pool_iterator {
172 struct pool_desc *pool;
173 int idx; /* from 0 to pool_tgt_size - 1 */
176 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
178 struct pool_iterator *iter = (struct pool_iterator *)s->private;
181 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
183 /* test if end of file */
184 if (*pos >= pool_tgt_count(iter->pool))
187 /* iterate to find a non empty entry */
188 prev_idx = iter->idx;
189 down_read(&pool_tgt_rw_sem(iter->pool));
191 if (iter->idx == pool_tgt_count(iter->pool)) {
192 iter->idx = prev_idx; /* we stay on the last entry */
193 up_read(&pool_tgt_rw_sem(iter->pool));
196 up_read(&pool_tgt_rw_sem(iter->pool));
198 /* return != NULL to continue */
202 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
204 struct pool_desc *pool = (struct pool_desc *)s->private;
205 struct pool_iterator *iter;
207 lov_pool_getref(pool);
208 if ((pool_tgt_count(pool) == 0) ||
209 (*pos >= pool_tgt_count(pool))) {
210 /* iter is not created, so stop() has no way to
211 * find pool to dec ref */
212 lov_pool_putref(pool);
218 return ERR_PTR(-ENOMEM);
219 iter->magic = POOL_IT_MAGIC;
223 /* we use seq_file private field to memorized iterator so
224 * we can free it at stop() */
225 /* /!\ do not forget to restore it to pool before freeing it */
233 ptr = pool_proc_next(s, &iter, &i);
234 } while ((i < *pos) && (ptr != NULL));
240 static void pool_proc_stop(struct seq_file *s, void *v)
242 struct pool_iterator *iter = (struct pool_iterator *)s->private;
244 /* in some cases stop() method is called 2 times, without
245 * calling start() method (see seq_read() from fs/seq_file.c)
246 * we have to free only if s->private is an iterator */
247 if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
248 /* we restore s->private so next call to pool_proc_start()
250 s->private = iter->pool;
251 lov_pool_putref(iter->pool);
257 static int pool_proc_show(struct seq_file *s, void *v)
259 struct pool_iterator *iter = (struct pool_iterator *)v;
260 struct lov_tgt_desc *tgt;
262 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
263 LASSERT(iter->pool != NULL);
264 LASSERT(iter->idx <= pool_tgt_count(iter->pool));
266 down_read(&pool_tgt_rw_sem(iter->pool));
267 tgt = pool_tgt(iter->pool, iter->idx);
268 up_read(&pool_tgt_rw_sem(iter->pool));
270 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
275 static struct seq_operations pool_proc_ops = {
276 .start = pool_proc_start,
277 .next = pool_proc_next,
278 .stop = pool_proc_stop,
279 .show = pool_proc_show,
282 static int pool_proc_open(struct inode *inode, struct file *file)
286 rc = seq_open(file, &pool_proc_ops);
288 struct seq_file *s = file->private_data;
289 s->private = PROC_I(inode)->pde->data;
294 static struct file_operations pool_proc_operations = {
295 .open = pool_proc_open,
298 .release = seq_release,
302 void lov_dump_pool(int level, struct pool_desc *pool)
306 lov_pool_getref(pool);
308 CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
309 pool->pool_name, pool->pool_obds.op_count);
310 down_read(&pool_tgt_rw_sem(pool));
312 for (i = 0; i < pool_tgt_count(pool) ; i++) {
313 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
315 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
317 obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
320 up_read(&pool_tgt_rw_sem(pool));
321 lov_pool_putref(pool);
324 #define LOV_POOL_INIT_COUNT 2
325 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
330 count = LOV_POOL_INIT_COUNT;
333 init_rwsem(&op->op_rw_sem);
335 OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
336 if (op->op_array == NULL) {
344 /* Caller must hold write op_rwlock */
345 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
350 LASSERT(min_count != 0);
352 if (op->op_count < op->op_size)
355 new_size = max(min_count, 2 * op->op_size);
356 OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
360 /* copy old array to new one */
361 memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
362 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
364 op->op_size = new_size;
368 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
373 down_write(&op->op_rw_sem);
375 rc = lov_ost_pool_extend(op, min_count);
379 /* search ost in pool array */
380 for (i = 0; i < op->op_count; i++) {
381 if (op->op_array[i] == idx)
382 GOTO(out, rc = -EEXIST);
384 /* ost not found we add it */
385 op->op_array[op->op_count] = idx;
389 up_write(&op->op_rw_sem);
393 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
398 down_write(&op->op_rw_sem);
400 for (i = 0; i < op->op_count; i++) {
401 if (op->op_array[i] == idx) {
402 memmove(&op->op_array[i], &op->op_array[i + 1],
403 (op->op_count - i - 1) * sizeof(op->op_array[0]));
405 up_write(&op->op_rw_sem);
411 up_write(&op->op_rw_sem);
415 int lov_ost_pool_free(struct ost_pool *op)
419 if (op->op_size == 0)
422 down_write(&op->op_rw_sem);
424 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
429 up_write(&op->op_rw_sem);
434 int lov_pool_new(struct obd_device *obd, char *poolname)
437 struct pool_desc *new_pool;
443 if (strlen(poolname) > LOV_MAXPOOLNAME)
444 RETURN(-ENAMETOOLONG);
446 OBD_ALLOC_PTR(new_pool);
447 if (new_pool == NULL)
450 strncpy(new_pool->pool_name, poolname, LOV_MAXPOOLNAME);
451 new_pool->pool_name[LOV_MAXPOOLNAME] = '\0';
452 new_pool->pool_lobd = obd;
453 /* ref count init to 1 because when created a pool is always used
456 cfs_atomic_set(&new_pool->pool_refcount, 1);
457 rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
461 memset(&(new_pool->pool_rr), 0, sizeof(struct lov_qos_rr));
462 rc = lov_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
464 GOTO(out_free_pool_obds, rc);
466 CFS_INIT_HLIST_NODE(&new_pool->pool_hash);
469 /* we need this assert seq_file is not implementated for liblustre */
470 /* get ref for /proc file */
471 lov_pool_getref(new_pool);
472 new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
473 poolname, NULL, NULL,
475 &pool_proc_operations);
476 if (IS_ERR(new_pool->pool_proc_entry)) {
477 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
478 new_pool->pool_proc_entry = NULL;
479 lov_pool_putref(new_pool);
481 CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool, new_pool->pool_proc_entry);
484 spin_lock(&obd->obd_dev_lock);
485 cfs_list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
486 lov->lov_pool_count++;
487 spin_unlock(&obd->obd_dev_lock);
489 /* add to find only when it fully ready */
490 rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
491 &new_pool->pool_hash);
493 GOTO(out_err, rc = -EEXIST);
495 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
496 poolname, lov->lov_pool_count);
501 spin_lock(&obd->obd_dev_lock);
502 cfs_list_del_init(&new_pool->pool_list);
503 lov->lov_pool_count--;
504 spin_unlock(&obd->obd_dev_lock);
506 lprocfs_remove(&new_pool->pool_proc_entry);
508 lov_ost_pool_free(&new_pool->pool_rr.lqr_pool);
510 lov_ost_pool_free(&new_pool->pool_obds);
511 OBD_FREE_PTR(new_pool);
515 int lov_pool_del(struct obd_device *obd, char *poolname)
518 struct pool_desc *pool;
523 /* lookup and kill hash reference */
524 pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
528 if (pool->pool_proc_entry != NULL) {
529 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
530 lprocfs_remove(&pool->pool_proc_entry);
531 lov_pool_putref(pool);
534 spin_lock(&obd->obd_dev_lock);
535 cfs_list_del_init(&pool->pool_list);
536 lov->lov_pool_count--;
537 spin_unlock(&obd->obd_dev_lock);
539 /* release last reference */
540 lov_pool_putref(pool);
546 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
548 struct obd_uuid ost_uuid;
550 struct pool_desc *pool;
551 unsigned int lov_idx;
557 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
561 obd_str2uuid(&ost_uuid, ostname);
564 /* search ost in lov array */
566 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
567 if (!lov->lov_tgts[lov_idx])
569 if (obd_uuid_equals(&ost_uuid,
570 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
573 /* test if ost found in lov */
574 if (lov_idx == lov->desc.ld_tgt_count)
575 GOTO(out, rc = -EINVAL);
577 rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
581 pool->pool_rr.lqr_dirty = 1;
583 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
584 ostname, poolname, pool_tgt_count(pool));
589 lov_pool_putref(pool);
593 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
595 struct obd_uuid ost_uuid;
597 struct pool_desc *pool;
598 unsigned int lov_idx;
604 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
608 obd_str2uuid(&ost_uuid, ostname);
611 /* search ost in lov array, to get index */
612 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
613 if (!lov->lov_tgts[lov_idx])
616 if (obd_uuid_equals(&ost_uuid,
617 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
621 /* test if ost found in lov */
622 if (lov_idx == lov->desc.ld_tgt_count)
623 GOTO(out, rc = -EINVAL);
625 lov_ost_pool_remove(&pool->pool_obds, lov_idx);
627 pool->pool_rr.lqr_dirty = 1;
629 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
635 lov_pool_putref(pool);
639 int lov_check_index_in_pool(__u32 idx, struct pool_desc *pool)
644 /* caller may no have a ref on pool if it got the pool
645 * without calling lov_find_pool() (e.g. go through the lov pool
648 lov_pool_getref(pool);
650 down_read(&pool_tgt_rw_sem(pool));
652 for (i = 0; i < pool_tgt_count(pool); i++) {
653 if (pool_tgt_array(pool)[i] == idx)
659 up_read(&pool_tgt_rw_sem(pool));
661 lov_pool_putref(pool);
665 struct pool_desc *lov_find_pool(struct lov_obd *lov, char *poolname)
667 struct pool_desc *pool;
670 if (poolname[0] != '\0') {
671 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
673 CWARN("Request for an unknown pool ("LOV_POOLNAMEF")\n",
675 if ((pool != NULL) && (pool_tgt_count(pool) == 0)) {
676 CWARN("Request for an empty pool ("LOV_POOLNAMEF")\n",
678 /* pool is ignored, so we remove ref on it */
679 lov_pool_putref(pool);