Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lustre / lov / lov_pool.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/lov/lov_pool.c
32  *
33  * OST pool methods
34  *
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>
38  */
39
40 #define DEBUG_SUBSYSTEM S_LOV
41
42 #include <libcfs/libcfs.h>
43 #include <libcfs/linux/linux-hash.h>
44 #include <libcfs/linux/linux-fs.h>
45
46 #include <obd.h>
47 #include "lov_internal.h"
48
49 #define pool_tgt(_p, _i) \
50                 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
51
52 /**
53  * Hash the pool name for use by the hashtable handlers.
54  *
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
58  *
59  * \retval              computed hash value of the key(poolname)
60  */
61 static u32 pool_hashfh(const void *data, u32 len, u32 seed)
62 {
63         const char *pool_name = data;
64
65         return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
66                                                pool_name));
67 }
68
69 /**
70  * Compare the pool name with key
71  *
72  * \param[in] arg       key (poolname) to compare against
73  * \param[in] obj       Entry that is being compared
74  *
75  * \retval              0 if matched
76  * \retval              1 if not matched
77  */
78 static int pool_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
79 {
80         const struct lov_pool_desc *pool = obj;
81         const char *pool_name = arg->key;
82
83         return strcmp(pool_name, pool->pool_name);
84 }
85
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,
93 };
94
95 /**
96  * Get a reference on the specified lov pool.
97  *
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.
102  *
103  * \param[in] pool      pool descriptor on which to gain reference
104  */
105 static void lov_pool_getref(struct lov_pool_desc *pool)
106 {
107         CDEBUG(D_INFO, "pool %p\n", pool);
108         kref_get(&pool->pool_refcount);
109 }
110
111 static void lov_pool_putref_free(struct kref *kref)
112 {
113         struct lov_pool_desc *pool = container_of(kref, struct lov_pool_desc,
114                                                   pool_refcount);
115
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);
120         EXIT;
121 }
122
123 /**
124  * Drop a reference on the specified lov pool and free its memory if needed
125  *
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.
132  *
133  * \param[in] pool      lov pool descriptor to drop reference on and possibly
134  *                      free
135  */
136 void lov_pool_putref(struct lov_pool_desc *pool)
137 {
138         CDEBUG(D_INFO, "pool %p\n", pool);
139         kref_put(&pool->pool_refcount, lov_pool_putref_free);
140 }
141
142 #ifdef CONFIG_PROC_FS
143 /*
144  * pool /proc seq_file methods
145  */
146 /*
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)
151  */
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;
157 };
158
159 /**
160  * Return the next configured target within one pool for seq_file iteration
161  *
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[].
165  *
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.
168  *
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
172  *
173  * \retval      struct pool_iterator of the next pool descriptor
174  */
175 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
176 {
177         struct pool_iterator *iter = (struct pool_iterator *)s->private;
178         int prev_idx;
179
180         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
181
182         (*pos)++;
183         /* test if end of file */
184         if (*pos > pool_tgt_count(iter->pool))
185                 return NULL;
186
187         /* iterate to find a non empty entry */
188         prev_idx = iter->idx;
189         iter->idx++;
190         if (iter->idx >= pool_tgt_count(iter->pool)) {
191                 iter->idx = prev_idx; /* we stay on the last entry */
192                 return NULL;
193         }
194         /* return != NULL to continue */
195         return iter;
196 }
197
198 /**
199  * Start seq_file iteration via /proc for a single pool
200  *
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.
205  *
206  * \param[in] seq       new sequence file structure to initialize
207  * \param[in] pos       initial target number at which to start iteration
208  *
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
212  */
213 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
214 {
215         struct lov_pool_desc *pool = (struct lov_pool_desc *)s->private;
216         struct pool_iterator *iter;
217
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);
223                 return NULL;
224         }
225
226         OBD_ALLOC_PTR(iter);
227         if (!iter)
228                 return ERR_PTR(-ENOMEM);
229         iter->magic = POOL_IT_MAGIC;
230         iter->pool = pool;
231         iter->idx = 0;
232
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 */
236         s->private = iter;
237         down_read(&pool_tgt_rw_sem(pool));
238         if (*pos > 0) {
239                 loff_t i;
240                 void *ptr;
241
242                 i = 0;
243                 do {
244                         ptr = pool_proc_next(s, &iter, &i);
245                 } while ((i < *pos) && (ptr != NULL));
246                 return ptr;
247         }
248         return iter;
249 }
250
251 /**
252  * Finish seq_file iteration for a single pool
253  *
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.
257  *
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.
261  *
262  * \param[in] seq       sequence file structure to clean up
263  * \param[in] v         (unused)
264  */
265 static void pool_proc_stop(struct seq_file *s, void *v)
266 {
267         struct pool_iterator *iter = (struct pool_iterator *)s->private;
268
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()
275                  * will work */
276                 s->private = iter->pool;
277                 lov_pool_putref(iter->pool);
278                 OBD_FREE_PTR(iter);
279         }
280 }
281
282 /**
283  * Print out one target entry from the pool for seq_file iteration
284  *
285  * The currently referenced pool target is given by op_array[lpi_idx].
286  *
287  * \param[in] seq       new sequence file structure to initialize
288  * \param[in] v         (unused)
289  */
290 static int pool_proc_show(struct seq_file *s, void *v)
291 {
292         struct pool_iterator *iter = (struct pool_iterator *)v;
293         struct lov_tgt_desc *tgt;
294
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));
298
299         tgt = pool_tgt(iter->pool, iter->idx);
300         if (tgt)
301                 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
302
303         return 0;
304 }
305
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,
311 };
312
313 /**
314  * Open a new /proc file for seq_file iteration of targets in one pool
315  *
316  * Initialize the seq_file private pointer to reference the pool.
317  *
318  * \param[in] inode     inode to store iteration state for /proc
319  * \param[in] file      file descriptor to store iteration methods
320  *
321  * \retval      0 for success
322  * \retval      negative error number on failure
323  */
324 static int pool_proc_open(struct inode *inode, struct file *file)
325 {
326         int rc;
327
328         rc = seq_open(file, &pool_proc_ops);
329         if (!rc) {
330                 struct seq_file *s = file->private_data;
331
332                 s->private = pde_data(inode);
333         }
334         return rc;
335 }
336
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,
342 };
343 #endif /* CONFIG_PROC_FS */
344
345 static void pools_hash_exit(void *vpool, void *data)
346 {
347         struct lov_pool_desc *pool = vpool;
348
349         lov_pool_putref(pool);
350 }
351
352 int lov_pool_hash_init(struct rhashtable *tbl)
353 {
354         return rhashtable_init(tbl, &pools_hash_params);
355 }
356
357 void lov_pool_hash_destroy(struct rhashtable *tbl)
358 {
359         rhashtable_free_and_destroy(tbl, pools_hash_exit, NULL);
360 }
361
362 /**
363  * Allocate a new pool for the specified device
364  *
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.
369  *
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
372  *
373  * \retval              0 in case of success
374  * \retval              negative error code in case of error
375  */
376 int lov_pool_new(struct obd_device *obd, char *poolname)
377 {
378         struct lov_obd *lov;
379         struct lov_pool_desc *new_pool;
380         int rc;
381         ENTRY;
382
383         lov = &(obd->u.lov);
384
385         if (strlen(poolname) > LOV_MAXPOOLNAME)
386                 RETURN(-ENAMETOOLONG);
387
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)
391                 RETURN(-ENOMEM);
392
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
396          * up to deletion
397          */
398         kref_init(&new_pool->pool_refcount);
399         rc = lu_tgt_pool_init(&new_pool->pool_obds, 0);
400         if (rc)
401                 GOTO(out_free_pool, rc);
402
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,
407                                                        poolname, new_pool,
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);
413         }
414         CDEBUG(D_INFO, "pool %p - proc %p\n",
415                new_pool, new_pool->pool_proc_entry);
416 #endif
417
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);
422
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,
426                                            pools_hash_params);
427         if (rc) {
428                 if (rc != -EEXIST)
429                         /*
430                          * Hide -E2BIG and -EBUSY which
431                          * are not helpful.
432                          */
433                         rc = -ENOMEM;
434                 GOTO(out_err, rc);
435         }
436
437         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n", poolname,
438                lov->lov_pool_count);
439
440         RETURN(0);
441
442 out_err:
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);
449 out_free_pool:
450         OBD_FREE_PTR(new_pool);
451
452         return rc;
453 }
454
455 struct lov_pool_desc *lov_pool_find(struct obd_device *obd, char *poolname)
456 {
457         struct lov_pool_desc *pool;
458         struct lov_obd *lov = &obd->u.lov;
459
460         rcu_read_lock();
461         pool = rhashtable_lookup(&lov->lov_pools_hash_body,
462                                  poolname,
463                                  pools_hash_params);
464         if (pool && !kref_get_unless_zero(&pool->pool_refcount))
465                 pool = NULL;
466         rcu_read_unlock();
467
468         return pool;
469 }
470
471 /**
472  * Remove the named pool from the OBD device
473  *
474  * \param[in] obd       OBD device on which pool was previously created
475  * \param[in] poolname  name of pool to remove from \a obd
476  *
477  * \retval              0 on successfully removing the pool
478  * \retval              negative error numbers for failures
479  */
480 int lov_pool_del(struct obd_device *obd, char *poolname)
481 {
482         struct lov_obd *lov;
483         struct lov_pool_desc *pool;
484         ENTRY;
485
486         lov = &(obd->u.lov);
487
488         /* lookup and kill hash reference */
489         rcu_read_lock();
490         pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
491                                  pools_hash_params);
492         if (pool && rhashtable_remove_fast(&lov->lov_pools_hash_body,
493                                            &pool->pool_hash,
494                                            pools_hash_params) != 0)
495                 pool = NULL;
496         rcu_read_unlock();
497         if (!pool)
498                 RETURN(-ENOENT);
499
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);
504         }
505
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);
510
511         /* release last reference */
512         lov_pool_putref(pool);
513
514         RETURN(0);
515 }
516
517 /**
518  * Add a single target device to the named pool
519  *
520  * Add the target specified by \a ostname to the specified \a poolname.
521  *
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
525  *
526  * \retval              0 if \a ostname was (previously) added to the named pool
527  * \retval              negative error number on failure
528  */
529 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
530 {
531         struct obd_uuid ost_uuid;
532         struct lov_obd *lov;
533         struct lov_pool_desc *pool;
534         unsigned int lov_idx;
535         int rc;
536         ENTRY;
537
538         lov = &(obd->u.lov);
539
540         rcu_read_lock();
541         pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
542                                  pools_hash_params);
543         if (pool && !kref_get_unless_zero(&pool->pool_refcount))
544                 pool = NULL;
545         rcu_read_unlock();
546         if (!pool)
547                 RETURN(-ENOENT);
548
549         obd_str2uuid(&ost_uuid, ostname);
550
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])
555                         continue;
556                 if (obd_uuid_equals(&ost_uuid,
557                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
558                         break;
559         }
560         /* test if ost found in lov */
561         if (lov_idx == lov->desc.ld_tgt_count)
562                 GOTO(out, rc = -EINVAL);
563
564         rc = lu_tgt_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
565         if (rc)
566                 GOTO(out, rc);
567
568         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
569                ostname, poolname,  pool_tgt_count(pool));
570
571         EXIT;
572 out:
573         lov_tgts_putref(obd);
574         lov_pool_putref(pool);
575
576         return rc;
577 }
578
579 /**
580  * Remove the named target from the specified pool
581  *
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.
585  *
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
589  *
590  * \retval              0 on successfully removing \a ostname from the pool
591  * \retval              negative number on error (e.g. \a ostname not in pool)
592  */
593 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
594 {
595         struct obd_uuid ost_uuid;
596         struct lov_obd *lov;
597         struct lov_pool_desc *pool;
598         unsigned int lov_idx;
599         int rc = 0;
600         ENTRY;
601
602         lov = &(obd->u.lov);
603
604         /* lookup and kill hash reference */
605         rcu_read_lock();
606         pool = rhashtable_lookup(&lov->lov_pools_hash_body, poolname,
607                                  pools_hash_params);
608         if (pool && !kref_get_unless_zero(&pool->pool_refcount))
609                 pool = NULL;
610         rcu_read_unlock();
611         if (!pool)
612                 RETURN(-ENOENT);
613
614         obd_str2uuid(&ost_uuid, ostname);
615
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])
620                         continue;
621
622                 if (obd_uuid_equals(&ost_uuid,
623                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
624                         break;
625         }
626
627         /* test if ost found in lov */
628         if (lov_idx == lov->desc.ld_tgt_count)
629                 GOTO(out, rc = -EINVAL);
630
631         lu_tgt_pool_remove(&pool->pool_obds, lov_idx);
632
633         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
634                poolname);
635
636         EXIT;
637 out:
638         lov_tgts_putref(obd);
639         lov_pool_putref(pool);
640
641         return rc;
642 }