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 2008 Sun Microsystems, Inc. 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/
32 * lustre/lod/lod_pool.c
36 * This file provides code related to the Logical Object Device (LOD)
37 * handling of OST Pools on the MDT. Pools are named lists of targets
38 * that allow userspace to group targets that share a particlar property
39 * together so that users or kernel helpers can make decisions about file
40 * allocation based on these properties. For example, pools could be
41 * defined based on fault domains (e.g. separate racks of server nodes) so
42 * that RAID-1 mirroring could select targets from independent fault
43 * domains, or pools could define target performance characteristics so
44 * that applicatins could select IOP-optimized storage or stream-optimized
45 * storage for a particular output file.
47 * This file handles creation, lookup, and removal of pools themselves, as
48 * well as adding and removing targets to pools. It also handles lprocfs
49 * display of configured pool. The pools are accessed by name in the pool
50 * hash, and are refcounted to ensure proper pool structure lifetimes.
52 * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
53 * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
54 * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
57 #define DEBUG_SUBSYSTEM S_LOV
59 #include <libcfs/libcfs.h>
60 #include <libcfs/linux/linux-hash.h>
61 #include <libcfs/linux/linux-fs.h>
63 #include "lod_internal.h"
65 #define pool_tgt(_p, _i) OST_TGT(lu2lod_dev((_p)->pool_lobd->obd_lu_dev), \
66 (_p)->pool_obds.op_array[_i])
69 * Get a reference on the specified pool.
71 * To ensure the pool descriptor is not freed before the caller is finished
72 * with it. Any process that is accessing \a pool directly needs to hold
73 * reference on it, including /proc since a userspace thread may be holding
74 * the /proc file open and busy in the kernel.
76 * \param[in] pool pool descriptor on which to gain reference
78 static void pool_getref(struct pool_desc *pool)
80 CDEBUG(D_INFO, "pool %p\n", pool);
81 atomic_inc(&pool->pool_refcount);
85 * Drop a reference on the specified pool and free its memory if needed.
87 * One reference is held by the LOD OBD device while it is configured, from
88 * the time the configuration log defines the pool until the time when it is
89 * dropped when the LOD OBD is cleaned up or the pool is deleted. This means
90 * that the pool will not be freed while the LOD device is configured, unless
91 * it is explicitly destroyed by the sysadmin. The pool structure is freed
92 * after the last reference on the structure is released.
94 * \param[in] pool pool descriptor to drop reference on and possibly free
96 void lod_pool_putref(struct pool_desc *pool)
98 CDEBUG(D_INFO, "pool %p\n", pool);
99 if (atomic_dec_and_test(&pool->pool_refcount)) {
100 LASSERT(list_empty(&pool->pool_list));
101 LASSERT(pool->pool_proc_entry == NULL);
102 lu_tgt_pool_free(&(pool->pool_rr.lqr_pool));
103 lu_tgt_pool_free(&(pool->pool_obds));
104 kfree_rcu(pool, pool_rcu);
109 static u32 pool_hashfh(const void *data, u32 len, u32 seed)
111 const char *pool_name = data;
113 return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
117 static int pool_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
119 const struct pool_desc *pool = obj;
120 const char *pool_name = arg->key;
122 return strcmp(pool_name, pool->pool_name);
125 static const struct rhashtable_params pools_hash_params = {
126 .key_len = 1, /* actually variable */
127 .key_offset = offsetof(struct pool_desc, pool_name),
128 .head_offset = offsetof(struct pool_desc, pool_hash),
129 .hashfn = pool_hashfh,
130 .obj_cmpfn = pool_cmpfn,
131 .automatic_shrinking = true,
135 * Methods for /proc seq_file iteration of the defined pools.
138 #define POOL_IT_MAGIC 0xB001CEA0
139 struct lod_pool_iterator {
140 unsigned int lpi_magic; /* POOL_IT_MAGIC */
141 unsigned int lpi_idx; /* from 0 to pool_tgt_size - 1 */
142 struct pool_desc *lpi_pool;
146 * Return the next configured target within one pool for seq_file iteration.
148 * Iterator is used to go through the target entries of a single pool
149 * (i.e. the list of OSTs configured for a named pool).
150 * lpi_idx is the current target index in the pool's op_array[].
152 * The return type is a void * because this function is one of the
153 * struct seq_operations methods and must match the function template.
155 * \param[in] seq /proc sequence file iteration tracking structure
156 * \param[in] v unused
157 * \param[in] pos position within iteration; 0 to number of targets - 1
159 * \retval struct pool_iterator of the next pool descriptor
161 static void *pool_proc_next(struct seq_file *seq, void *v, loff_t *pos)
163 struct lod_pool_iterator *iter = seq->private;
166 LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X\n", iter->lpi_magic);
169 /* test if end of file */
170 if (*pos > pool_tgt_count(iter->lpi_pool))
173 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_LIST_ASSERT, cfs_fail_val);
175 /* iterate to find a non empty entry */
176 prev_idx = iter->lpi_idx;
178 if (iter->lpi_idx >= pool_tgt_count(iter->lpi_pool)) {
179 iter->lpi_idx = prev_idx; /* we stay on the last entry */
183 /* return != NULL to continue */
188 * Start seq_file iteration via /proc for a single pool.
190 * The \a pos parameter may be non-zero, indicating that the iteration
191 * is starting at some offset in the target list. Use the seq_file
192 * private field to memorize the iterator so we can free it at stop().
193 * Need to restore the private pointer to the pool before freeing it.
195 * \param[in] seq new sequence file structure to initialize
196 * \param[in] pos initial target number at which to start iteration
198 * \retval initialized pool iterator private structure
199 * \retval NULL if \a pos exceeds the number of targets in \a pool
200 * \retval negative error number on failure
202 static void *pool_proc_start(struct seq_file *seq, loff_t *pos)
204 struct pool_desc *pool = seq->private;
205 struct lod_pool_iterator *iter;
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 lod_pool_putref(pool);
218 return ERR_PTR(-ENOMEM);
219 iter->lpi_magic = POOL_IT_MAGIC;
220 iter->lpi_pool = pool;
224 down_read(&pool_tgt_rw_sem(pool));
231 ptr = pool_proc_next(seq, &iter, &i);
232 } while ((i < *pos) && (ptr != NULL));
241 * Finish seq_file iteration for a single pool.
243 * Once iteration has been completed, the pool_iterator struct must be
244 * freed, and the seq_file private pointer restored to the pool, as it
245 * was initially when pool_proc_start() was called.
247 * In some cases the stop() method may be called 2 times, without calling
248 * the start() method (see seq_read() from fs/seq_file.c). We have to free
249 * the private iterator struct only if seq->private points to the iterator.
251 * \param[in] seq sequence file structure to clean up
252 * \param[in] v (unused)
254 static void pool_proc_stop(struct seq_file *seq, void *v)
256 struct lod_pool_iterator *iter = seq->private;
258 if (iter != NULL && iter->lpi_magic == POOL_IT_MAGIC) {
259 up_read(&pool_tgt_rw_sem(iter->lpi_pool));
260 seq->private = iter->lpi_pool;
261 lod_pool_putref(iter->lpi_pool);
267 * Print out one target entry from the pool for seq_file iteration.
269 * The currently referenced pool target is given by op_array[lpi_idx].
271 * \param[in] seq new sequence file structure to initialize
272 * \param[in] v (unused)
274 static int pool_proc_show(struct seq_file *seq, void *v)
276 struct lod_pool_iterator *iter = v;
277 struct lod_tgt_desc *tgt;
279 LASSERTF(iter->lpi_magic == POOL_IT_MAGIC, "%08X\n", iter->lpi_magic);
280 LASSERT(iter->lpi_pool != NULL);
281 LASSERT(iter->lpi_idx <= pool_tgt_count(iter->lpi_pool));
283 tgt = pool_tgt(iter->lpi_pool, iter->lpi_idx);
285 seq_printf(seq, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
290 static const struct seq_operations pool_proc_ops = {
291 .start = pool_proc_start,
292 .next = pool_proc_next,
293 .stop = pool_proc_stop,
294 .show = pool_proc_show,
298 * Open a new /proc file for seq_file iteration of targets in one pool.
300 * Initialize the seq_file private pointer to reference the pool.
302 * \param inode inode to store iteration state for /proc
303 * \param file file descriptor to store iteration methods
305 * \retval 0 for success
306 * \retval negative error number on failure
308 static int pool_proc_open(struct inode *inode, struct file *file)
312 rc = seq_open(file, &pool_proc_ops);
314 struct seq_file *seq = file->private_data;
315 seq->private = PDE_DATA(inode);
320 const static struct proc_ops pool_proc_operations = {
321 .open = pool_proc_open,
324 .release = seq_release,
328 * Dump the pool target list into the Lustre debug log.
330 * This is a debugging function to allow dumping the list of targets
331 * in \a pool to the Lustre kernel debug log at the given \a level.
333 * This is not currently called by any existing code, but can be called
334 * from within gdb/crash to display the contents of the pool, or from
335 * code under development.
337 * \param[in] level Lustre debug level (D_INFO, D_WARN, D_ERROR, etc)
338 * \param[in] pool pool descriptor to be dumped
340 void lod_dump_pool(int level, struct pool_desc *pool)
346 CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
347 pool->pool_name, pool->pool_obds.op_count);
348 down_read(&pool_tgt_rw_sem(pool));
350 for (i = 0; i < pool_tgt_count(pool) ; i++) {
351 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
353 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
355 obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
358 up_read(&pool_tgt_rw_sem(pool));
359 lod_pool_putref(pool);
362 static void pools_hash_exit(void *vpool, void *data)
364 struct pool_desc *pool = vpool;
366 lod_pool_putref(pool);
369 int lod_pool_hash_init(struct rhashtable *tbl)
371 return rhashtable_init(tbl, &pools_hash_params);
374 void lod_pool_hash_destroy(struct rhashtable *tbl)
376 rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
380 * Allocate a new pool for the specified device.
382 * Allocate a new pool_desc structure for the specified \a new_pool
383 * device to create a pool with the given \a poolname. The new pool
384 * structure is created with a single reference, and is freed when the
385 * reference count drops to zero.
387 * \param[in] obd Lustre OBD device on which to add a pool iterator
388 * \param[in] poolname the name of the pool to be created
390 * \retval 0 in case of success
391 * \retval negative error code in case of error
393 int lod_pool_new(struct obd_device *obd, char *poolname)
395 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
396 struct pool_desc *new_pool;
400 if (strlen(poolname) > LOV_MAXPOOLNAME)
401 RETURN(-ENAMETOOLONG);
403 /* OBD_ALLOC_* doesn't work with direct kfree_rcu use */
404 new_pool = kmalloc(sizeof(*new_pool), GFP_KERNEL);
405 if (new_pool == NULL)
408 strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
409 new_pool->pool_lobd = obd;
410 atomic_set(&new_pool->pool_refcount, 1);
411 rc = lu_tgt_pool_init(&new_pool->pool_obds, 0);
415 lu_qos_rr_init(&new_pool->pool_rr);
417 rc = lu_tgt_pool_init(&new_pool->pool_rr.lqr_pool, 0);
419 GOTO(out_free_pool_obds, rc);
421 #ifdef CONFIG_PROC_FS
422 pool_getref(new_pool);
423 new_pool->pool_proc_entry = lprocfs_add_simple(lod->lod_pool_proc_entry,
425 &pool_proc_operations);
426 if (IS_ERR(new_pool->pool_proc_entry)) {
427 CDEBUG(D_CONFIG, "%s: cannot add proc entry "LOV_POOLNAMEF"\n",
428 obd->obd_name, poolname);
429 new_pool->pool_proc_entry = NULL;
430 lod_pool_putref(new_pool);
432 CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool,
433 new_pool->pool_proc_entry);
436 spin_lock(&obd->obd_dev_lock);
437 list_add_tail(&new_pool->pool_list, &lod->lod_pool_list);
438 lod->lod_pool_count++;
439 spin_unlock(&obd->obd_dev_lock);
441 /* Add to hash table only when it is fully ready. */
442 rc = rhashtable_lookup_insert_fast(&lod->lod_pools_hash_body,
443 &new_pool->pool_hash,
448 * Hide -E2BIG and -EBUSY which
455 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
456 poolname, lod->lod_pool_count);
461 spin_lock(&obd->obd_dev_lock);
462 list_del_init(&new_pool->pool_list);
463 lod->lod_pool_count--;
464 spin_unlock(&obd->obd_dev_lock);
466 lprocfs_remove(&new_pool->pool_proc_entry);
468 lu_tgt_pool_free(&new_pool->pool_rr.lqr_pool);
470 lu_tgt_pool_free(&new_pool->pool_obds);
471 OBD_FREE_PTR(new_pool);
476 * Remove the named pool from the OBD device.
478 * \param[in] obd OBD device on which pool was previously created
479 * \param[in] poolname name of pool to remove from \a obd
481 * \retval 0 on successfully removing the pool
482 * \retval negative error numbers for failures
484 int lod_pool_del(struct obd_device *obd, char *poolname)
486 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
487 struct pool_desc *pool;
490 /* lookup and kill hash reference */
492 pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
494 if (pool && rhashtable_remove_fast(&lod->lod_pools_hash_body,
496 pools_hash_params) != 0)
502 if (pool->pool_proc_entry != NULL) {
503 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
504 lprocfs_remove(&pool->pool_proc_entry);
505 lod_pool_putref(pool);
508 spin_lock(&obd->obd_dev_lock);
509 list_del_init(&pool->pool_list);
510 lod->lod_pool_count--;
511 spin_unlock(&obd->obd_dev_lock);
513 /* release last reference */
514 lod_pool_putref(pool);
520 * Add a single target device to the named pool.
522 * Add the target specified by \a ostname to the specified \a poolname.
524 * \param[in] obd OBD device on which to add the pool
525 * \param[in] poolname name of the pool to which to add the target \a ostname
526 * \param[in] ostname name of the target device to be added
528 * \retval 0 if \a ostname was (previously) added to the named pool
529 * \retval negative error number on failure
531 int lod_pool_add(struct obd_device *obd, char *poolname, char *ostname)
533 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
534 struct obd_uuid ost_uuid;
535 struct pool_desc *pool;
536 struct lu_tgt_desc *tgt;
541 pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
543 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
549 obd_str2uuid(&ost_uuid, ostname);
551 /* search ost in lod array */
552 lod_getref(&lod->lod_ost_descs);
553 lod_foreach_ost(lod, tgt) {
554 if (obd_uuid_equals(&ost_uuid, &tgt->ltd_uuid)) {
563 rc = lu_tgt_pool_add(&pool->pool_obds, tgt->ltd_index,
568 set_bit(LQ_DIRTY, &pool->pool_rr.lqr_flags);
570 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
571 ostname, poolname, pool_tgt_count(pool));
575 lod_putref(lod, &lod->lod_ost_descs);
576 lod_pool_putref(pool);
581 * Remove the named target from the specified pool.
583 * Remove one target named \a ostname from \a poolname. The \a ostname
584 * is searched for in the lod_device lod_ost_bitmap array, to ensure the
585 * specified name actually exists in the pool.
587 * \param[in] obd OBD device from which to remove \a poolname
588 * \param[in] poolname name of the pool to be changed
589 * \param[in] ostname name of the target to remove from \a poolname
591 * \retval 0 on successfully removing \a ostname from the pool
592 * \retval negative number on error (e.g. \a ostname not in pool)
594 int lod_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
596 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
597 struct lu_tgt_desc *ost;
598 struct obd_uuid ost_uuid;
599 struct pool_desc *pool;
603 /* lookup and kill hash reference */
605 pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
607 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
613 obd_str2uuid(&ost_uuid, ostname);
615 lod_getref(&lod->lod_ost_descs);
616 lod_foreach_ost(lod, ost) {
617 if (obd_uuid_equals(&ost_uuid, &ost->ltd_uuid)) {
623 /* test if ost found in lod array */
627 lu_tgt_pool_remove(&pool->pool_obds, ost->ltd_index);
628 set_bit(LQ_DIRTY, &pool->pool_rr.lqr_flags);
630 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
635 lod_putref(lod, &lod->lod_ost_descs);
636 lod_pool_putref(pool);
641 * Check if the specified target exists in the pool.
643 * The caller may not have a reference on \a pool if it got the pool without
644 * calling lod_find_pool() (e.g. directly from the lod pool list)
646 * \param[in] idx Target index to check
647 * \param[in] pool Pool in which to check if target is added.
649 * \retval 0 successfully found index in \a pool
650 * \retval negative error if device not found in \a pool
652 int lod_check_index_in_pool(__u32 idx, struct pool_desc *pool)
657 rc = lu_tgt_check_index(idx, &pool->pool_obds);
658 lod_pool_putref(pool);
663 * Find the pool descriptor for the specified pool and return it with a
664 * reference to the caller if found.
666 * \param[in] lod LOD on which the pools are configured
667 * \param[in] poolname NUL-terminated name of the pool
669 * \retval pointer to pool descriptor on success
670 * \retval NULL if \a poolname could not be found or poolname is empty
672 struct pool_desc *lod_find_pool(struct lod_device *lod, char *poolname)
674 struct pool_desc *pool;
677 if (poolname[0] != '\0') {
679 pool = rhashtable_lookup(&lod->lod_pools_hash_body, poolname,
681 if (pool && !atomic_inc_not_zero(&pool->pool_refcount))
686 "%s: request for an unknown pool (" LOV_POOLNAMEF ")\n",
687 lod->lod_child_exp->exp_obd->obd_name, poolname);
688 if (pool != NULL && pool_tgt_count(pool) == 0) {
689 CDEBUG(D_CONFIG, "%s: request for an empty pool ("
691 lod->lod_child_exp->exp_obd->obd_name, poolname);
692 /* pool is ignored, so we remove ref on it */
693 lod_pool_putref(pool);