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]]
53 * Hash the pool name for use by the hashtable handlers.
55 * \param[in] data poolname (null-terminated string to be hashed or key)
56 * \param[in] len length of key
57 * \param[in] seed Random seed or previous hash
59 * \retval computed hash value of the key(poolname)
61 static u32 pool_hashfh(const void *data, u32 len, u32 seed)
63 const char *pool_name = data;
65 return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
70 * Compare the pool name with key
72 * \param[in] arg key (poolname) to compare against
73 * \param[in] obj Entry that is being compared
75 * \retval 0 if matched
76 * \retval 1 if not matched
78 static int pool_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
80 const struct lov_pool_desc *pool = obj;
81 const char *pool_name = arg->key;
83 return strcmp(pool_name, pool->pool_name);
86 static const struct rhashtable_params pools_hash_params = {
87 .key_len = 1, /* actually variable */
88 .key_offset = offsetof(struct lov_pool_desc, pool_name),
89 .head_offset = offsetof(struct lov_pool_desc, pool_hash),
90 .hashfn = pool_hashfh,
91 .obj_cmpfn = pool_cmpfn,
92 .automatic_shrinking = true,
96 * Get a reference on the specified lov pool.
98 * To ensure the pool descriptor is not freed before the caller is finished
99 * with it. Any process that is accessing \a pool directly needs to hold
100 * reference on it, including /proc since a userspace thread may be holding
101 * the /proc file open and busy in the kernel.
103 * \param[in] pool pool descriptor on which to gain reference
105 static void lov_pool_getref(struct lov_pool_desc *pool)
107 CDEBUG(D_INFO, "pool %p\n", pool);
108 atomic_inc(&pool->pool_refcount);
112 * Drop a reference on the specified lov pool and free its memory if needed
114 * One reference is held by the LOD OBD device while it is configured, from
115 * the time the configuration log defines the pool until the time when it is
116 * dropped when the LOD OBD is cleaned up or the pool is deleted. This means
117 * that the pool will not be freed while the LOD device is configured, unless
118 * it is explicitly destroyed by the sysadmin. The pool structure is freed
119 * after the last reference on the structure is released.
121 * \param[in] pool pool descriptor to drop reference on and possibly free
123 void lov_pool_putref(struct lov_pool_desc *pool)
125 CDEBUG(D_INFO, "pool %p\n", pool);
126 if (atomic_dec_and_test(&pool->pool_refcount)) {
127 LASSERT(list_empty(&pool->pool_list));
128 LASSERT(pool->pool_proc_entry == NULL);
129 lu_tgt_pool_free(&(pool->pool_obds));
130 kfree_rcu(pool, pool_rcu);
135 #ifdef CONFIG_PROC_FS
137 * pool /proc seq_file methods
140 * iterator is used to go through the target pool entries
141 * index is the current entry index in the lp_array[] array
142 * index >= pos returned to the seq_file interface
143 * pos is from 0 to (pool->pool_obds.op_count - 1)
145 #define POOL_IT_MAGIC 0xB001CEA0
146 struct pool_iterator {
147 int magic; /* POOL_IT_MAGIC */
148 int idx; /* from 0 to pool_tgt_size - 1 */
149 struct lov_pool_desc *pool;
153 * Return the next configured target within one pool for seq_file iteration
155 * Iterator is used to go through the target entries of a single pool
156 * (i.e. the list of OSTs configured for a named pool).
157 * lpi_idx is the current target index in the pool's op_array[].
159 * The return type is a void * because this function is one of the
160 * struct seq_operations methods and must match the function template.
162 * \param[in] seq /proc sequence file iteration tracking structure
163 * \param[in] v unused
164 * \param[in] pos position within iteration; 0 to number of targets - 1
166 * \retval struct pool_iterator of the next pool descriptor
168 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
170 struct pool_iterator *iter = (struct pool_iterator *)s->private;
173 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
176 /* test if end of file */
177 if (*pos > pool_tgt_count(iter->pool))
180 /* iterate to find a non empty entry */
181 prev_idx = iter->idx;
183 if (iter->idx >= pool_tgt_count(iter->pool)) {
184 iter->idx = prev_idx; /* we stay on the last entry */
187 /* return != NULL to continue */
192 * Start seq_file iteration via /proc for a single pool
194 * The \a pos parameter may be non-zero, indicating that the iteration
195 * is starting at some offset in the target list. Use the seq_file
196 * private field to memorize the iterator so we can free it at stop().
197 * Need to restore the private pointer to the pool before freeing it.
199 * \param[in] seq new sequence file structure to initialize
200 * \param[in] pos initial target number at which to start iteration
202 * \retval initialized pool iterator private structure
203 * \retval NULL if \a pos exceeds the number of targets in \a pool
204 * \retval negative error number on failure
206 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
208 struct lov_pool_desc *pool = (struct lov_pool_desc *)s->private;
209 struct pool_iterator *iter;
211 lov_pool_getref(pool);
212 if ((pool_tgt_count(pool) == 0) || (*pos >= pool_tgt_count(pool))) {
213 /* iter is not created, so stop() has no way to
214 * find pool to dec ref */
215 lov_pool_putref(pool);
221 return ERR_PTR(-ENOMEM);
222 iter->magic = POOL_IT_MAGIC;
226 /* we use seq_file private field to memorized iterator so
227 * we can free it at stop() */
228 /* /!\ do not forget to restore it to pool before freeing it */
230 down_read(&pool_tgt_rw_sem(pool));
237 ptr = pool_proc_next(s, &iter, &i);
238 } while ((i < *pos) && (ptr != NULL));
245 * Finish seq_file iteration for a single pool
247 * Once iteration has been completed, the pool_iterator struct must be
248 * freed, and the seq_file private pointer restored to the pool, as it
249 * was initially when pool_proc_start() was called.
251 * In some cases the stop() method may be called 2 times, without calling
252 * the start() method (see seq_read() from fs/seq_file.c). We have to free
253 * the private iterator struct only if seq->private points to the iterator.
255 * \param[in] seq sequence file structure to clean up
256 * \param[in] v (unused)
258 static void pool_proc_stop(struct seq_file *s, void *v)
260 struct pool_iterator *iter = (struct pool_iterator *)s->private;
262 /* in some cases stop() method is called 2 times, without
263 * calling start() method (see seq_read() from fs/seq_file.c)
264 * we have to free only if s->private is an iterator */
265 if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
266 up_read(&pool_tgt_rw_sem(iter->pool));
267 /* we restore s->private so next call to pool_proc_start()
269 s->private = iter->pool;
270 lov_pool_putref(iter->pool);
276 * Print out one target entry from the pool for seq_file iteration
278 * The currently referenced pool target is given by op_array[lpi_idx].
280 * \param[in] seq new sequence file structure to initialize
281 * \param[in] v (unused)
283 static int pool_proc_show(struct seq_file *s, void *v)
285 struct pool_iterator *iter = (struct pool_iterator *)v;
286 struct lov_tgt_desc *tgt;
288 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
289 LASSERT(iter->pool != NULL);
290 LASSERT(iter->idx <= pool_tgt_count(iter->pool));
292 tgt = pool_tgt(iter->pool, iter->idx);
294 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
299 static const struct seq_operations pool_proc_ops = {
300 .start = pool_proc_start,
301 .next = pool_proc_next,
302 .stop = pool_proc_stop,
303 .show = pool_proc_show,
307 * Open a new /proc file for seq_file iteration of targets in one pool
309 * Initialize the seq_file private pointer to reference the pool.
311 * \param[in] inode inode to store iteration state for /proc
312 * \param[in] file file descriptor to store iteration methods
314 * \retval 0 for success
315 * \retval negative error number on failure
317 static int pool_proc_open(struct inode *inode, struct file *file)
321 rc = seq_open(file, &pool_proc_ops);
323 struct seq_file *s = file->private_data;
325 s->private = pde_data(inode);
330 const static struct proc_ops pool_proc_operations = {
331 .proc_open = pool_proc_open,
332 .proc_read = seq_read,
333 .proc_lseek = seq_lseek,
334 .proc_release = seq_release,
336 #endif /* CONFIG_PROC_FS */
338 static void pools_hash_exit(void *vpool, void *data)
340 struct lov_pool_desc *pool = vpool;
342 lov_pool_putref(pool);
345 int lov_pool_hash_init(struct rhashtable *tbl)
347 return rhashtable_init(tbl, &pools_hash_params);
350 void lov_pool_hash_destroy(struct rhashtable *tbl)
352 rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
356 * Allocate a new pool for the specified device
358 * Allocate a new pool_desc structure for the specified \a new_pool
359 * device to create a pool with the given \a poolname. The new pool
360 * structure is created with a single refrence, and is freed when the
361 * reference count drops to zero.
363 * \param[in] obd Lustre OBD device on which to add a pool iterator
364 * \param[in] poolname the name of the pool to be created
366 * \retval 0 in case of success
367 * \retval negative error code in case of error
369 int lov_pool_new(struct obd_device *obd, char *poolname)
372 struct lov_pool_desc *new_pool;
378 if (strlen(poolname) > LOV_MAXPOOLNAME)
379 RETURN(-ENAMETOOLONG);
381 /* OBD_ALLOC doesn't work with direct use of kfree_rcu */
382 new_pool = kmalloc(sizeof(*new_pool), GFP_KERNEL);
383 if (new_pool == NULL)
386 strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
387 new_pool->pool_lobd = obd;
388 /* ref count init to 1 because when created a pool is always used
391 atomic_set(&new_pool->pool_refcount, 1);
392 rc = lu_tgt_pool_init(&new_pool->pool_obds, 0);
394 GOTO(out_free_pool, rc);
396 #ifdef CONFIG_PROC_FS
397 /* get ref for /proc file */
398 lov_pool_getref(new_pool);
399 new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
401 &pool_proc_operations);
402 if (IS_ERR(new_pool->pool_proc_entry)) {
403 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
404 new_pool->pool_proc_entry = NULL;
405 lov_pool_putref(new_pool);
407 CDEBUG(D_INFO, "pool %p - proc %p\n",
408 new_pool, new_pool->pool_proc_entry);
411 spin_lock(&obd->obd_dev_lock);
412 list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
413 lov->lov_pool_count++;
414 spin_unlock(&obd->obd_dev_lock);
416 /* Add to hash table only when it is fully ready. */
417 rc = rhashtable_lookup_insert_fast(&lov->lov_pools_hash_body,
418 &new_pool->pool_hash,
423 * Hide -E2BIG and -EBUSY which
430 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n", poolname,
431 lov->lov_pool_count);
436 spin_lock(&obd->obd_dev_lock);
437 list_del_init(&new_pool->pool_list);
438 lov->lov_pool_count--;
439 spin_unlock(&obd->obd_dev_lock);
440 lprocfs_remove(&new_pool->pool_proc_entry);
441 lu_tgt_pool_free(&new_pool->pool_obds);
443 OBD_FREE_PTR(new_pool);
448 struct lov_pool_desc *lov_pool_find(struct obd_device *obd, char *poolname)
450 struct lov_pool_desc *pool;
451 struct lov_obd *lov = &obd->u.lov;
454 pool = rhashtable_lookup(&lov->lov_pools_hash_body,
457 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
465 * Remove the named pool from the OBD device
467 * \param[in] obd OBD device on which pool was previously created
468 * \param[in] poolname name of pool to remove from \a obd
470 * \retval 0 on successfully removing the pool
471 * \retval negative error numbers for failures
473 int lov_pool_del(struct obd_device *obd, char *poolname)
476 struct lov_pool_desc *pool;
481 /* lookup and kill hash reference */
483 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
485 if (pool && rhashtable_remove_fast(&lov->lov_pools_hash_body,
487 pools_hash_params) != 0)
493 if (pool->pool_proc_entry != NULL) {
494 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
495 lprocfs_remove(&pool->pool_proc_entry);
496 lov_pool_putref(pool);
499 spin_lock(&obd->obd_dev_lock);
500 list_del_init(&pool->pool_list);
501 lov->lov_pool_count--;
502 spin_unlock(&obd->obd_dev_lock);
504 /* release last reference */
505 lov_pool_putref(pool);
511 * Add a single target device to the named pool
513 * Add the target specified by \a ostname to the specified \a poolname.
515 * \param[in] obd OBD device on which to add the pool
516 * \param[in] poolname name of the pool to which to add the target \a ostname
517 * \param[in] ostname name of the target device to be added
519 * \retval 0 if \a ostname was (previously) added to the named pool
520 * \retval negative error number on failure
522 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
524 struct obd_uuid ost_uuid;
526 struct lov_pool_desc *pool;
527 unsigned int lov_idx;
534 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
536 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
542 obd_str2uuid(&ost_uuid, ostname);
544 /* search ost in lov array */
545 lov_tgts_getref(obd);
546 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
547 if (!lov->lov_tgts[lov_idx])
549 if (obd_uuid_equals(&ost_uuid,
550 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
553 /* test if ost found in lov */
554 if (lov_idx == lov->desc.ld_tgt_count)
555 GOTO(out, rc = -EINVAL);
557 rc = lu_tgt_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
561 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
562 ostname, poolname, pool_tgt_count(pool));
566 lov_tgts_putref(obd);
567 lov_pool_putref(pool);
573 * Remove the named target from the specified pool
575 * Remove one target named \a ostname from \a poolname. The \a ostname
576 * is searched for in the lod_device lod_ost_bitmap array, to ensure the
577 * specified name actually exists in the pool.
579 * \param[in] obd OBD device from which to remove \a poolname
580 * \param[in] poolname name of the pool to be changed
581 * \param[in] ostname name of the target to remove from \a poolname
583 * \retval 0 on successfully removing \a ostname from the pool
584 * \retval negative number on error (e.g. \a ostname not in pool)
586 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
588 struct obd_uuid ost_uuid;
590 struct lov_pool_desc *pool;
591 unsigned int lov_idx;
597 /* lookup and kill hash reference */
599 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
601 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
607 obd_str2uuid(&ost_uuid, ostname);
609 lov_tgts_getref(obd);
610 /* search ost in lov array, to get index */
611 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
612 if (!lov->lov_tgts[lov_idx])
615 if (obd_uuid_equals(&ost_uuid,
616 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
620 /* test if ost found in lov */
621 if (lov_idx == lov->desc.ld_tgt_count)
622 GOTO(out, rc = -EINVAL);
624 lu_tgt_pool_remove(&pool->pool_obds, lov_idx);
626 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
631 lov_tgts_putref(obd);
632 lov_pool_putref(pool);