1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
43 #define DEBUG_SUBSYSTEM S_LOV
46 #include <libcfs/libcfs.h>
48 #include <liblustre.h>
52 #include "lov_internal.h"
55 * hash function using a Rotating Hash algorithm
56 * Knuth, D. The Art of Computer Programming,
57 * Volume 3: Sorting and Searching,
59 * Addison Wesley, 1973
61 static __u32 pool_hashfn(lustre_hash_t *hash_body, void *key, unsigned mask)
68 poolname = (char *)key;
69 for (i = 0; i < MAXPOOLNAME; i++) {
70 if (poolname[i] == '\0')
72 result = (result << 4)^(result >> 28) ^ poolname[i];
74 return (result % mask);
77 static void *pool_key(struct hlist_node *hnode)
79 struct pool_desc *pool;
81 pool = hlist_entry(hnode, struct pool_desc, pool_hash);
82 return (pool->pool_name);
85 static int pool_hashkey_compare(void *key, struct hlist_node *compared_hnode)
88 struct pool_desc *pool;
91 pool_name = (char *)key;
92 pool = hlist_entry(compared_hnode, struct pool_desc, pool_hash);
93 rc = strncmp(pool_name, pool->pool_name, MAXPOOLNAME);
97 static void *pool_hashrefcount_get(struct hlist_node *hnode)
99 struct pool_desc *pool;
101 pool = hlist_entry(hnode, struct pool_desc, pool_hash);
105 static void *pool_hashrefcount_put(struct hlist_node *hnode)
107 struct pool_desc *pool;
109 pool = hlist_entry(hnode, struct pool_desc, pool_hash);
113 lustre_hash_ops_t pool_hash_operations = {
114 .lh_hash = pool_hashfn,
116 .lh_compare = pool_hashkey_compare,
117 .lh_get = pool_hashrefcount_get,
118 .lh_put = pool_hashrefcount_put,
122 /* ifdef needed for liblustre support */
124 * pool /proc seq_file methods
127 * iterator is used to go through the target pool entries
128 * index is the current entry index in the lp_array[] array
129 * index >= pos returned to the seq_file interface
130 * pos is from 0 to (pool->pool_obds.op_count - 1)
132 #define POOL_IT_MAGIC 0xB001CEA0
133 struct pool_iterator {
135 struct pool_desc *pool;
136 int idx; /* from 0 to pool_tgt_size - 1 */
139 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
141 struct pool_iterator *iter = (struct pool_iterator *)s->private;
144 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
146 /* test if end of file */
147 if (*pos >= pool_tgt_count(iter->pool))
150 /* iterate to find a non empty entry */
151 prev_idx = iter->idx;
152 read_lock(&pool_tgt_rwlock(iter->pool));
154 if (iter->idx == pool_tgt_count(iter->pool)) {
155 iter->idx = prev_idx; /* we stay on the last entry */
156 read_unlock(&pool_tgt_rwlock(iter->pool));
159 read_unlock(&pool_tgt_rwlock(iter->pool));
161 /* return != NULL to continue */
165 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
167 struct pool_desc *pool = (struct pool_desc *)s->private;
168 struct pool_iterator *iter;
170 if ((pool_tgt_count(pool) == 0) ||
171 (*pos >= pool_tgt_count(pool)))
174 OBD_ALLOC(iter, sizeof(struct pool_iterator));
176 return ERR_PTR(-ENOMEM);
177 iter->magic = POOL_IT_MAGIC;
181 /* we use seq_file private field to memorized iterator so
182 * we can free it at stop() */
183 /* /!\ do not forget to restore it to pool before freeing it */
191 ptr = pool_proc_next(s, &iter, &i);
192 } while ((i < *pos) && (ptr != NULL));
198 static void pool_proc_stop(struct seq_file *s, void *v)
200 struct pool_iterator *iter = (struct pool_iterator *)s->private;
202 /* in some cases stop() method is called 2 times, without
203 * calling start() method (see seq_read() from fs/seq_file.c)
204 * we have to free only if s->private is an iterator */
205 if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
206 /* we restore s->private so next call to pool_proc_start()
208 s->private = iter->pool;
209 OBD_FREE(iter, sizeof(struct pool_iterator));
214 static int pool_proc_show(struct seq_file *s, void *v)
216 struct pool_iterator *iter = (struct pool_iterator *)v;
217 struct lov_tgt_desc *tgt;
219 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
220 LASSERT(iter->pool != NULL);
221 LASSERT(iter->idx <= pool_tgt_count(iter->pool));
223 read_lock(&pool_tgt_rwlock(iter->pool));
224 tgt = pool_tgt(iter->pool, iter->idx);
225 read_unlock(&pool_tgt_rwlock(iter->pool));
227 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
232 static struct seq_operations pool_proc_ops = {
233 .start = pool_proc_start,
234 .next = pool_proc_next,
235 .stop = pool_proc_stop,
236 .show = pool_proc_show,
239 static int pool_proc_open(struct inode *inode, struct file *file)
243 rc = seq_open(file, &pool_proc_ops);
245 struct seq_file *s = file->private_data;
246 s->private = PROC_I(inode)->pde->data;
251 static struct file_operations pool_proc_operations = {
252 .open = pool_proc_open,
255 .release = seq_release,
259 void lov_dump_pool(int level, struct pool_desc *pool)
263 CDEBUG(level, "pool "POOLNAMEF" has %d members\n",
264 pool->pool_name, pool->pool_obds.op_count);
265 read_lock(&pool_tgt_rwlock(pool));
266 for (i = 0; i < pool_tgt_count(pool) ; i++) {
267 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
269 CDEBUG(level, "pool "POOLNAMEF"[%d] = %s\n", pool->pool_name,
270 i, obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
272 read_unlock(&pool_tgt_rwlock(pool));
275 #define LOV_POOL_INIT_COUNT 2
276 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
279 count = LOV_POOL_INIT_COUNT;
282 op->op_rwlock = RW_LOCK_UNLOCKED;
284 OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
285 if (op->op_array == NULL) {
292 int lov_ost_pool_extend(struct ost_pool *op, unsigned int max_count)
297 LASSERT(max_count != 0);
299 if (op->op_count < op->op_size)
302 new_size = min(max_count, 2 * op->op_size);
303 OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
307 /* copy old array to new one */
308 memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
309 write_lock(&op->op_rwlock);
310 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
312 op->op_size = new_size;
313 write_unlock(&op->op_rwlock);
317 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int max_count)
321 rc = lov_ost_pool_extend(op, max_count);
325 /* search ost in pool array */
326 read_lock(&op->op_rwlock);
327 for (i = 0; i < op->op_count; i++) {
328 if (op->op_array[i] == idx) {
329 read_unlock(&op->op_rwlock);
333 /* ost not found we add it */
334 op->op_array[op->op_count] = idx;
336 read_unlock(&op->op_rwlock);
340 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
344 read_lock(&op->op_rwlock);
345 for (i = 0; i < op->op_count; i++) {
346 if (op->op_array[i] == idx) {
347 memmove(&op->op_array[i], &op->op_array[i + 1],
348 (op->op_count - i - 1) * sizeof(op->op_array[0]));
350 read_unlock(&op->op_rwlock);
354 read_unlock(&op->op_rwlock);
358 int lov_ost_pool_free(struct ost_pool *op)
360 if (op->op_size == 0)
363 write_lock(&op->op_rwlock);
364 OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
368 write_unlock(&op->op_rwlock);
373 int lov_pool_new(struct obd_device *obd, char *poolname)
376 struct pool_desc *new_pool;
381 OBD_ALLOC(new_pool, sizeof(*new_pool));
383 if (new_pool == NULL)
386 if (strlen(poolname) > MAXPOOLNAME)
387 return -ENAMETOOLONG;
389 strncpy(new_pool->pool_name, poolname, MAXPOOLNAME);
390 new_pool->pool_name[MAXPOOLNAME] = '\0';
391 new_pool->pool_lov = lov;
392 rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
396 memset(&(new_pool->pool_rr), 0, sizeof(struct lov_qos_rr));
397 rc = lov_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
401 spin_lock(&obd->obd_dev_lock);
402 /* check if pool alreaddy exists */
403 if (lustre_hash_lookup(lov->lov_pools_hash_body,
405 spin_unlock(&obd->obd_dev_lock);
406 lov_ost_pool_free(&new_pool->pool_obds);
407 OBD_FREE(new_pool, sizeof(*new_pool));
411 INIT_HLIST_NODE(&new_pool->pool_hash);
412 lustre_hash_add_unique(lov->lov_pools_hash_body, poolname,
413 &new_pool->pool_hash);
414 list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
415 lov->lov_pool_count++;
416 spin_unlock(&obd->obd_dev_lock);
418 CDEBUG(D_CONFIG, POOLNAMEF" is pool #%d\n",
419 poolname, lov->lov_pool_count);
422 /* ifdef needed for liblustre */
423 new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
427 &pool_proc_operations);
430 if (IS_ERR(new_pool->pool_proc_entry)) {
431 CWARN("Cannot add proc pool entry "POOLNAMEF"\n", poolname);
432 new_pool->pool_proc_entry = NULL;
438 int lov_pool_del(struct obd_device *obd, char *poolname)
441 struct pool_desc *pool;
445 spin_lock(&obd->obd_dev_lock);
446 pool = lustre_hash_lookup(lov->lov_pools_hash_body,
449 spin_unlock(&obd->obd_dev_lock);
454 if (pool->pool_proc_entry != NULL)
455 remove_proc_entry(pool->pool_proc_entry->name,
456 pool->pool_proc_entry->parent);
459 /* pool is kept in the list to be freed by lov_cleanup()
460 * list_del(&pool->pool_list);
462 lustre_hash_del_key(lov->lov_pools_hash_body, poolname);
464 lov->lov_pool_count--;
466 spin_unlock(&obd->obd_dev_lock);
468 /* pool struct is not freed because it may be used by
470 * the struct is freed at lov_cleanup()
473 if (pool->pool_rr.lqr_size != 0)
474 OBD_FREE(pool->pool_rr.lqr_array, pool->pool_rr.lqr_size);
475 lov_ost_pool_free(&pool->pool_obds);
476 OBD_FREE(pool, sizeof(*pool));
482 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
484 struct obd_uuid ost_uuid;
486 struct pool_desc *pool;
487 unsigned int i, lov_idx;
492 pool = lustre_hash_lookup(lov->lov_pools_hash_body, poolname);
497 /* allocate pool tgt array if needed */
498 mutex_down(&lov->lov_lock);
499 rc = lov_ost_pool_extend(&pool->pool_obds, lov->lov_tgt_size);
501 mutex_up(&lov->lov_lock);
504 mutex_up(&lov->lov_lock);
506 obd_str2uuid(&ost_uuid, ostname);
508 spin_lock(&obd->obd_dev_lock);
510 /* search ost in lov array */
511 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
512 if (!lov->lov_tgts[i])
515 if (obd_uuid_equals(&ost_uuid, &(lov->lov_tgts[i]->ltd_uuid)))
519 /* test if ost found in lov */
520 if (i == lov->desc.ld_tgt_count) {
521 spin_unlock(&obd->obd_dev_lock);
525 spin_unlock(&obd->obd_dev_lock);
529 rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
533 pool->pool_rr.lqr_dirty = 1;
535 CDEBUG(D_CONFIG, "Added %s to "POOLNAMEF" as member %d\n",
536 ostname, poolname, pool_tgt_count(pool));
540 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
542 struct obd_uuid ost_uuid;
544 struct pool_desc *pool;
545 unsigned int i, lov_idx;
549 spin_lock(&obd->obd_dev_lock);
550 pool = lustre_hash_lookup(lov->lov_pools_hash_body, poolname);
552 spin_unlock(&obd->obd_dev_lock);
556 obd_str2uuid(&ost_uuid, ostname);
558 /* search ost in lov array, to get index */
559 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
560 if (!lov->lov_tgts[i])
563 if (obd_uuid_equals(&ost_uuid, &(lov->lov_tgts[i]->ltd_uuid)))
567 /* test if ost found in lov */
568 if (i == lov->desc.ld_tgt_count) {
569 spin_unlock(&obd->obd_dev_lock);
573 spin_unlock(&obd->obd_dev_lock);
577 lov_ost_pool_remove(&pool->pool_obds, lov_idx);
579 pool->pool_rr.lqr_dirty = 1;
581 CDEBUG(D_CONFIG, "%s removed from "POOLNAMEF"\n", ostname, poolname);
586 int lov_check_index_in_pool(__u32 idx, struct pool_desc *pool)
590 read_lock(&pool_tgt_rwlock(pool));
591 for (i = 0; i < pool_tgt_count(pool); i++) {
592 if (pool_tgt_array(pool)[i] == idx) {
593 read_unlock(&pool_tgt_rwlock(pool));
597 read_unlock(&pool_tgt_rwlock(pool));
601 struct pool_desc *lov_find_pool(struct lov_obd *lov, char *poolname)
603 struct pool_desc *pool;
606 if (poolname[0] != '\0') {
607 pool = lustre_hash_lookup(lov->lov_pools_hash_body, poolname);
609 CWARN("Request for an unknown pool ("POOLNAMEF")\n",
611 if ((pool != NULL) && (pool_tgt_count(pool) == 0)) {
612 CWARN("Request for an empty pool ("POOLNAMEF")\n",