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 kref_get(&pool->pool_refcount);
111 static void lov_pool_putref_free(struct kref *kref)
113 struct lov_pool_desc *pool = container_of(kref, struct lov_pool_desc,
116 LASSERT(list_empty(&pool->pool_list));
117 LASSERT(pool->pool_proc_entry == NULL);
118 lu_tgt_pool_free(&(pool->pool_obds));
119 kfree_rcu(pool, pool_rcu);
124 * Drop a reference on the specified lov pool and free its memory if needed
126 * One reference is held by the LOD OBD device while it is configured, from
127 * the time the configuration log defines the pool until the time when it is
128 * dropped when the LOD OBD is cleaned up or the pool is deleted. This means
129 * that the pool will not be freed while the LOD device is configured, unless
130 * it is explicitly destroyed by the sysadmin. The pool structure is freed
131 * after the last reference on the structure is released.
133 * \param[in] pool lov pool descriptor to drop reference on and possibly
136 void lov_pool_putref(struct lov_pool_desc *pool)
138 CDEBUG(D_INFO, "pool %p\n", pool);
139 kref_put(&pool->pool_refcount, lov_pool_putref_free);
142 #ifdef CONFIG_PROC_FS
144 * pool /proc seq_file methods
147 * iterator is used to go through the target pool entries
148 * index is the current entry index in the lp_array[] array
149 * index >= pos returned to the seq_file interface
150 * pos is from 0 to (pool->pool_obds.op_count - 1)
152 #define POOL_IT_MAGIC 0xB001CEA0
153 struct pool_iterator {
154 int magic; /* POOL_IT_MAGIC */
155 int idx; /* from 0 to pool_tgt_size - 1 */
156 struct lov_pool_desc *pool;
160 * Return the next configured target within one pool for seq_file iteration
162 * Iterator is used to go through the target entries of a single pool
163 * (i.e. the list of OSTs configured for a named pool).
164 * lpi_idx is the current target index in the pool's op_array[].
166 * The return type is a void * because this function is one of the
167 * struct seq_operations methods and must match the function template.
169 * \param[in] seq /proc sequence file iteration tracking structure
170 * \param[in] v unused
171 * \param[in] pos position within iteration; 0 to number of targets - 1
173 * \retval struct pool_iterator of the next pool descriptor
175 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
177 struct pool_iterator *iter = (struct pool_iterator *)s->private;
180 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", 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;
190 if (iter->idx >= pool_tgt_count(iter->pool)) {
191 iter->idx = prev_idx; /* we stay on the last entry */
194 /* return != NULL to continue */
199 * Start seq_file iteration via /proc for a single pool
201 * The \a pos parameter may be non-zero, indicating that the iteration
202 * is starting at some offset in the target list. Use the seq_file
203 * private field to memorize the iterator so we can free it at stop().
204 * Need to restore the private pointer to the pool before freeing it.
206 * \param[in] seq new sequence file structure to initialize
207 * \param[in] pos initial target number at which to start iteration
209 * \retval initialized pool iterator private structure
210 * \retval NULL if \a pos exceeds the number of targets in \a pool
211 * \retval negative error number on failure
213 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
215 struct lov_pool_desc *pool = (struct lov_pool_desc *)s->private;
216 struct pool_iterator *iter;
218 lov_pool_getref(pool);
219 if ((pool_tgt_count(pool) == 0) || (*pos >= pool_tgt_count(pool))) {
220 /* iter is not created, so stop() has no way to
221 * find pool to dec ref */
222 lov_pool_putref(pool);
228 return ERR_PTR(-ENOMEM);
229 iter->magic = POOL_IT_MAGIC;
233 /* we use seq_file private field to memorized iterator so
234 * we can free it at stop() */
235 /* /!\ do not forget to restore it to pool before freeing it */
237 down_read(&pool_tgt_rw_sem(pool));
244 ptr = pool_proc_next(s, &iter, &i);
245 } while ((i < *pos) && (ptr != NULL));
252 * Finish seq_file iteration for a single pool
254 * Once iteration has been completed, the pool_iterator struct must be
255 * freed, and the seq_file private pointer restored to the pool, as it
256 * was initially when pool_proc_start() was called.
258 * In some cases the stop() method may be called 2 times, without calling
259 * the start() method (see seq_read() from fs/seq_file.c). We have to free
260 * the private iterator struct only if seq->private points to the iterator.
262 * \param[in] seq sequence file structure to clean up
263 * \param[in] v (unused)
265 static void pool_proc_stop(struct seq_file *s, void *v)
267 struct pool_iterator *iter = (struct pool_iterator *)s->private;
269 /* in some cases stop() method is called 2 times, without
270 * calling start() method (see seq_read() from fs/seq_file.c)
271 * we have to free only if s->private is an iterator */
272 if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
273 up_read(&pool_tgt_rw_sem(iter->pool));
274 /* we restore s->private so next call to pool_proc_start()
276 s->private = iter->pool;
277 lov_pool_putref(iter->pool);
283 * Print out one target entry from the pool for seq_file iteration
285 * The currently referenced pool target is given by op_array[lpi_idx].
287 * \param[in] seq new sequence file structure to initialize
288 * \param[in] v (unused)
290 static int pool_proc_show(struct seq_file *s, void *v)
292 struct pool_iterator *iter = (struct pool_iterator *)v;
293 struct lov_tgt_desc *tgt;
295 LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
296 LASSERT(iter->pool != NULL);
297 LASSERT(iter->idx <= pool_tgt_count(iter->pool));
299 tgt = pool_tgt(iter->pool, iter->idx);
301 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
306 static const struct seq_operations pool_proc_ops = {
307 .start = pool_proc_start,
308 .next = pool_proc_next,
309 .stop = pool_proc_stop,
310 .show = pool_proc_show,
314 * Open a new /proc file for seq_file iteration of targets in one pool
316 * Initialize the seq_file private pointer to reference the pool.
318 * \param[in] inode inode to store iteration state for /proc
319 * \param[in] file file descriptor to store iteration methods
321 * \retval 0 for success
322 * \retval negative error number on failure
324 static int pool_proc_open(struct inode *inode, struct file *file)
328 rc = seq_open(file, &pool_proc_ops);
330 struct seq_file *s = file->private_data;
332 s->private = pde_data(inode);
337 const static struct proc_ops pool_proc_operations = {
338 .proc_open = pool_proc_open,
339 .proc_read = seq_read,
340 .proc_lseek = seq_lseek,
341 .proc_release = seq_release,
343 #endif /* CONFIG_PROC_FS */
345 static void pools_hash_exit(void *vpool, void *data)
347 struct lov_pool_desc *pool = vpool;
349 lov_pool_putref(pool);
352 int lov_pool_hash_init(struct rhashtable *tbl)
354 return rhashtable_init(tbl, &pools_hash_params);
357 void lov_pool_hash_destroy(struct rhashtable *tbl)
359 rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
363 * Allocate a new pool for the specified device
365 * Allocate a new pool_desc structure for the specified \a new_pool
366 * device to create a pool with the given \a poolname. The new pool
367 * structure is created with a single refrence, and is freed when the
368 * reference count drops to zero.
370 * \param[in] obd Lustre OBD device on which to add a pool iterator
371 * \param[in] poolname the name of the pool to be created
373 * \retval 0 in case of success
374 * \retval negative error code in case of error
376 int lov_pool_new(struct obd_device *obd, char *poolname)
379 struct lov_pool_desc *new_pool;
385 if (strlen(poolname) > LOV_MAXPOOLNAME)
386 RETURN(-ENAMETOOLONG);
388 /* OBD_ALLOC doesn't work with direct use of kfree_rcu */
389 new_pool = kmalloc(sizeof(*new_pool), GFP_KERNEL);
390 if (new_pool == NULL)
393 strscpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
394 new_pool->pool_lobd = obd;
395 /* ref count init to 1 because when created a pool is always used
398 kref_init(&new_pool->pool_refcount);
399 rc = lu_tgt_pool_init(&new_pool->pool_obds, 0);
401 GOTO(out_free_pool, rc);
403 #ifdef CONFIG_PROC_FS
404 /* get ref for /proc file */
405 lov_pool_getref(new_pool);
406 new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
408 &pool_proc_operations);
409 if (IS_ERR(new_pool->pool_proc_entry)) {
410 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
411 new_pool->pool_proc_entry = NULL;
412 lov_pool_putref(new_pool);
414 CDEBUG(D_INFO, "pool %p - proc %p\n",
415 new_pool, new_pool->pool_proc_entry);
418 spin_lock(&obd->obd_dev_lock);
419 list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
420 lov->lov_pool_count++;
421 spin_unlock(&obd->obd_dev_lock);
423 /* Add to hash table only when it is fully ready. */
424 rc = rhashtable_lookup_insert_fast(&lov->lov_pools_hash_body,
425 &new_pool->pool_hash,
430 * Hide -E2BIG and -EBUSY which
437 CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n", poolname,
438 lov->lov_pool_count);
443 spin_lock(&obd->obd_dev_lock);
444 list_del_init(&new_pool->pool_list);
445 lov->lov_pool_count--;
446 spin_unlock(&obd->obd_dev_lock);
447 lprocfs_remove(&new_pool->pool_proc_entry);
448 lu_tgt_pool_free(&new_pool->pool_obds);
450 OBD_FREE_PTR(new_pool);
455 struct lov_pool_desc *lov_pool_find(struct obd_device *obd, char *poolname)
457 struct lov_pool_desc *pool;
458 struct lov_obd *lov = &obd->u.lov;
461 pool = rhashtable_lookup(&lov->lov_pools_hash_body,
464 if (pool && !kref_get_unless_zero(&pool->pool_refcount))
472 * Remove the named pool from the OBD device
474 * \param[in] obd OBD device on which pool was previously created
475 * \param[in] poolname name of pool to remove from \a obd
477 * \retval 0 on successfully removing the pool
478 * \retval negative error numbers for failures
480 int lov_pool_del(struct obd_device *obd, char *poolname)
483 struct lov_pool_desc *pool;
488 /* lookup and kill hash reference */
490 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
492 if (pool && rhashtable_remove_fast(&lov->lov_pools_hash_body,
494 pools_hash_params) != 0)
500 if (pool->pool_proc_entry != NULL) {
501 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
502 lprocfs_remove(&pool->pool_proc_entry);
503 lov_pool_putref(pool);
506 spin_lock(&obd->obd_dev_lock);
507 list_del_init(&pool->pool_list);
508 lov->lov_pool_count--;
509 spin_unlock(&obd->obd_dev_lock);
511 /* release last reference */
512 lov_pool_putref(pool);
518 * Add a single target device to the named pool
520 * Add the target specified by \a ostname to the specified \a poolname.
522 * \param[in] obd OBD device on which to add the pool
523 * \param[in] poolname name of the pool to which to add the target \a ostname
524 * \param[in] ostname name of the target device to be added
526 * \retval 0 if \a ostname was (previously) added to the named pool
527 * \retval negative error number on failure
529 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
531 struct obd_uuid ost_uuid;
533 struct lov_pool_desc *pool;
534 unsigned int lov_idx;
541 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
543 if (pool && !kref_get_unless_zero(&pool->pool_refcount))
549 obd_str2uuid(&ost_uuid, ostname);
551 /* search ost in lov array */
552 lov_tgts_getref(obd);
553 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
554 if (!lov->lov_tgts[lov_idx])
556 if (obd_uuid_equals(&ost_uuid,
557 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
560 /* test if ost found in lov */
561 if (lov_idx == lov->desc.ld_tgt_count)
562 GOTO(out, rc = -EINVAL);
564 rc = lu_tgt_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
568 CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
569 ostname, poolname, pool_tgt_count(pool));
573 lov_tgts_putref(obd);
574 lov_pool_putref(pool);
580 * Remove the named target from the specified pool
582 * Remove one target named \a ostname from \a poolname. The \a ostname
583 * is searched for in the lod_device lod_ost_bitmap array, to ensure the
584 * specified name actually exists in the pool.
586 * \param[in] obd OBD device from which to remove \a poolname
587 * \param[in] poolname name of the pool to be changed
588 * \param[in] ostname name of the target to remove from \a poolname
590 * \retval 0 on successfully removing \a ostname from the pool
591 * \retval negative number on error (e.g. \a ostname not in pool)
593 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
595 struct obd_uuid ost_uuid;
597 struct lov_pool_desc *pool;
598 unsigned int lov_idx;
604 /* lookup and kill hash reference */
606 pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
608 if (pool && !kref_get_unless_zero(&pool->pool_refcount))
614 obd_str2uuid(&ost_uuid, ostname);
616 lov_tgts_getref(obd);
617 /* search ost in lov array, to get index */
618 for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
619 if (!lov->lov_tgts[lov_idx])
622 if (obd_uuid_equals(&ost_uuid,
623 &(lov->lov_tgts[lov_idx]->ltd_uuid)))
627 /* test if ost found in lov */
628 if (lov_idx == lov->desc.ld_tgt_count)
629 GOTO(out, rc = -EINVAL);
631 lu_tgt_pool_remove(&pool->pool_obds, lov_idx);
633 CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
638 lov_tgts_putref(obd);
639 lov_pool_putref(pool);