Whamcloud - gitweb
LU-2446 build: Update Whamcloud copyright messages for Intel
[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 [sun.com URL with a
18  * copy of GPLv2].
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lov/lov_pool.c
37  *
38  * OST pool methods
39  *
40  * Author: Jacques-Charles LAFOUCRIERE <jc.lafoucriere@cea.fr>
41  * Author: Alex Lyashkov <Alexey.Lyashkov@Sun.COM>
42  * Author: Nathaniel Rutman <Nathan.Rutman@Sun.COM>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOV
46
47 #ifdef __KERNEL__
48 #include <libcfs/libcfs.h>
49 #else
50 #include <liblustre.h>
51 #endif
52
53 #include <obd.h>
54 #include "lov_internal.h"
55
56 #define pool_tgt(_p, _i) \
57                 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
58
59 static void lov_pool_getref(struct pool_desc *pool)
60 {
61         CDEBUG(D_INFO, "pool %p\n", pool);
62         cfs_atomic_inc(&pool->pool_refcount);
63 }
64
65 void lov_pool_putref(struct pool_desc *pool) 
66 {
67         CDEBUG(D_INFO, "pool %p\n", pool);
68         if (cfs_atomic_dec_and_test(&pool->pool_refcount)) {
69                 LASSERT(cfs_hlist_unhashed(&pool->pool_hash));
70                 LASSERT(cfs_list_empty(&pool->pool_list));
71                 LASSERT(pool->pool_proc_entry == NULL);
72                 lov_ost_pool_free(&(pool->pool_rr.lqr_pool));
73                 lov_ost_pool_free(&(pool->pool_obds));
74                 OBD_FREE_PTR(pool);
75                 EXIT;
76         }
77 }
78
79 void lov_pool_putref_locked(struct pool_desc *pool)
80 {
81         CDEBUG(D_INFO, "pool %p\n", pool);
82         LASSERT(cfs_atomic_read(&pool->pool_refcount) > 1);
83
84         cfs_atomic_dec(&pool->pool_refcount);
85 }
86
87 /*
88  * hash function using a Rotating Hash algorithm
89  * Knuth, D. The Art of Computer Programming,
90  * Volume 3: Sorting and Searching,
91  * Chapter 6.4.
92  * Addison Wesley, 1973
93  */
94 static __u32 pool_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
95 {
96         int i;
97         __u32 result;
98         char *poolname;
99
100         result = 0;
101         poolname = (char *)key;
102         for (i = 0; i < LOV_MAXPOOLNAME; i++) {
103                 if (poolname[i] == '\0')
104                         break;
105                 result = (result << 4)^(result >> 28) ^  poolname[i];
106         }
107         return (result % mask);
108 }
109
110 static void *pool_key(cfs_hlist_node_t *hnode)
111 {
112         struct pool_desc *pool;
113
114         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
115         return (pool->pool_name);
116 }
117
118 static int pool_hashkey_keycmp(const void *key, cfs_hlist_node_t *compared_hnode)
119 {
120         char *pool_name;
121         struct pool_desc *pool;
122
123         pool_name = (char *)key;
124         pool = cfs_hlist_entry(compared_hnode, struct pool_desc, pool_hash);
125         return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
126 }
127
128 static void *pool_hashobject(cfs_hlist_node_t *hnode)
129 {
130         return cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
131 }
132
133 static void pool_hashrefcount_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
134 {
135         struct pool_desc *pool;
136
137         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
138         lov_pool_getref(pool);
139 }
140
141 static void pool_hashrefcount_put_locked(cfs_hash_t *hs,
142                                          cfs_hlist_node_t *hnode)
143 {
144         struct pool_desc *pool;
145
146         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
147         lov_pool_putref_locked(pool);
148 }
149
150 cfs_hash_ops_t pool_hash_operations = {
151         .hs_hash        = pool_hashfn,
152         .hs_key         = pool_key,
153         .hs_keycmp      = pool_hashkey_keycmp,
154         .hs_object      = pool_hashobject,
155         .hs_get         = pool_hashrefcount_get,
156         .hs_put_locked  = pool_hashrefcount_put_locked,
157
158 };
159
160 #ifdef LPROCFS
161 /* ifdef needed for liblustre support */
162 /*
163  * pool /proc seq_file methods
164  */
165 /*
166  * iterator is used to go through the target pool entries
167  * index is the current entry index in the lp_array[] array
168  * index >= pos returned to the seq_file interface
169  * pos is from 0 to (pool->pool_obds.op_count - 1)
170  */
171 #define POOL_IT_MAGIC 0xB001CEA0
172 struct pool_iterator {
173         int magic;
174         struct pool_desc *pool;
175         int idx;        /* from 0 to pool_tgt_size - 1 */
176 };
177
178 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
179 {
180         struct pool_iterator *iter = (struct pool_iterator *)s->private;
181         int prev_idx;
182
183         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
184
185         /* test if end of file */
186         if (*pos >= pool_tgt_count(iter->pool))
187                 return NULL;
188
189         /* iterate to find a non empty entry */
190         prev_idx = iter->idx;
191         down_read(&pool_tgt_rw_sem(iter->pool));
192         iter->idx++;
193         if (iter->idx == pool_tgt_count(iter->pool)) {
194                 iter->idx = prev_idx; /* we stay on the last entry */
195                 up_read(&pool_tgt_rw_sem(iter->pool));
196                 return NULL;
197         }
198         up_read(&pool_tgt_rw_sem(iter->pool));
199         (*pos)++;
200         /* return != NULL to continue */
201         return iter;
202 }
203
204 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
205 {
206         struct pool_desc *pool = (struct pool_desc *)s->private;
207         struct pool_iterator *iter;
208
209         lov_pool_getref(pool);
210         if ((pool_tgt_count(pool) == 0) ||
211             (*pos >= pool_tgt_count(pool))) {
212                 /* iter is not created, so stop() has no way to
213                  * find pool to dec ref */
214                 lov_pool_putref(pool);
215                 return NULL;
216         }
217
218         OBD_ALLOC_PTR(iter);
219         if (!iter)
220                 return ERR_PTR(-ENOMEM);
221         iter->magic = POOL_IT_MAGIC;
222         iter->pool = pool;
223         iter->idx = 0;
224
225         /* we use seq_file private field to memorized iterator so
226          * we can free it at stop() */
227         /* /!\ do not forget to restore it to pool before freeing it */
228         s->private = iter;
229         if (*pos > 0) {
230                 loff_t i;
231                 void *ptr;
232
233                 i = 0;
234                 do {
235                      ptr = pool_proc_next(s, &iter, &i);
236                 } while ((i < *pos) && (ptr != NULL));
237                 return ptr;
238         }
239         return iter;
240 }
241
242 static void pool_proc_stop(struct seq_file *s, void *v)
243 {
244         struct pool_iterator *iter = (struct pool_iterator *)s->private;
245
246         /* in some cases stop() method is called 2 times, without
247          * calling start() method (see seq_read() from fs/seq_file.c)
248          * we have to free only if s->private is an iterator */
249         if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
250                 /* we restore s->private so next call to pool_proc_start()
251                  * will work */
252                 s->private = iter->pool;
253                 lov_pool_putref(iter->pool);
254                 OBD_FREE_PTR(iter);
255         }
256         return;
257 }
258
259 static int pool_proc_show(struct seq_file *s, void *v)
260 {
261         struct pool_iterator *iter = (struct pool_iterator *)v;
262         struct lov_tgt_desc *tgt;
263
264         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X", iter->magic);
265         LASSERT(iter->pool != NULL);
266         LASSERT(iter->idx <= pool_tgt_count(iter->pool));
267
268         down_read(&pool_tgt_rw_sem(iter->pool));
269         tgt = pool_tgt(iter->pool, iter->idx);
270         up_read(&pool_tgt_rw_sem(iter->pool));
271         if (tgt)
272                 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
273
274         return 0;
275 }
276
277 static struct seq_operations pool_proc_ops = {
278         .start          = pool_proc_start,
279         .next           = pool_proc_next,
280         .stop           = pool_proc_stop,
281         .show           = pool_proc_show,
282 };
283
284 static int pool_proc_open(struct inode *inode, struct file *file)
285 {
286         int rc;
287
288         rc = seq_open(file, &pool_proc_ops);
289         if (!rc) {
290                 struct seq_file *s = file->private_data;
291                 s->private = PROC_I(inode)->pde->data;
292         }
293         return rc;
294 }
295
296 static struct file_operations pool_proc_operations = {
297         .open           = pool_proc_open,
298         .read           = seq_read,
299         .llseek         = seq_lseek,
300         .release        = seq_release,
301 };
302 #endif /* LPROCFS */
303
304 void lov_dump_pool(int level, struct pool_desc *pool)
305 {
306         int i;
307
308         lov_pool_getref(pool);
309
310         CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
311                pool->pool_name, pool->pool_obds.op_count);
312         down_read(&pool_tgt_rw_sem(pool));
313
314         for (i = 0; i < pool_tgt_count(pool) ; i++) {
315                 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
316                         continue;
317                 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
318                        pool->pool_name, i,
319                        obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
320         }
321
322         up_read(&pool_tgt_rw_sem(pool));
323         lov_pool_putref(pool);
324 }
325
326 #define LOV_POOL_INIT_COUNT 2
327 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
328 {
329         ENTRY;
330
331         if (count == 0)
332                 count = LOV_POOL_INIT_COUNT;
333         op->op_array = NULL;
334         op->op_count = 0;
335         init_rwsem(&op->op_rw_sem);
336         op->op_size = count;
337         OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
338         if (op->op_array == NULL) {
339                 op->op_size = 0;
340                 RETURN(-ENOMEM);
341         }
342         EXIT;
343         return 0;
344 }
345
346 /* Caller must hold write op_rwlock */
347 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
348 {
349         __u32 *new;
350         int new_size;
351
352         LASSERT(min_count != 0);
353
354         if (op->op_count < op->op_size)
355                 return 0;
356
357         new_size = max(min_count, 2 * op->op_size);
358         OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
359         if (new == NULL)
360                 return -ENOMEM;
361
362         /* copy old array to new one */
363         memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
364         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
365         op->op_array = new;
366         op->op_size = new_size;
367         return 0;
368 }
369
370 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
371 {
372         int rc = 0, i;
373         ENTRY;
374
375         down_write(&op->op_rw_sem);
376
377         rc = lov_ost_pool_extend(op, min_count);
378         if (rc)
379                 GOTO(out, rc);
380
381         /* search ost in pool array */
382         for (i = 0; i < op->op_count; i++) {
383                 if (op->op_array[i] == idx)
384                         GOTO(out, rc = -EEXIST);
385         }
386         /* ost not found we add it */
387         op->op_array[op->op_count] = idx;
388         op->op_count++;
389         EXIT;
390 out:
391         up_write(&op->op_rw_sem);
392         return rc;
393 }
394
395 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
396 {
397         int i;
398         ENTRY;
399
400         down_write(&op->op_rw_sem);
401
402         for (i = 0; i < op->op_count; i++) {
403                 if (op->op_array[i] == idx) {
404                         memmove(&op->op_array[i], &op->op_array[i + 1],
405                                 (op->op_count - i - 1) * sizeof(op->op_array[0]));
406                         op->op_count--;
407                         up_write(&op->op_rw_sem);
408                         EXIT;
409                         return 0;
410                 }
411         }
412
413         up_write(&op->op_rw_sem);
414         RETURN(-EINVAL);
415 }
416
417 int lov_ost_pool_free(struct ost_pool *op)
418 {
419         ENTRY;
420
421         if (op->op_size == 0)
422                 RETURN(0);
423
424         down_write(&op->op_rw_sem);
425
426         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
427         op->op_array = NULL;
428         op->op_count = 0;
429         op->op_size = 0;
430
431         up_write(&op->op_rw_sem);
432         RETURN(0);
433 }
434
435
436 int lov_pool_new(struct obd_device *obd, char *poolname)
437 {
438         struct lov_obd *lov;
439         struct pool_desc *new_pool;
440         int rc;
441         ENTRY;
442
443         lov = &(obd->u.lov);
444
445         if (strlen(poolname) > LOV_MAXPOOLNAME)
446                 RETURN(-ENAMETOOLONG);
447
448         OBD_ALLOC_PTR(new_pool);
449         if (new_pool == NULL)
450                 RETURN(-ENOMEM);
451
452         strncpy(new_pool->pool_name, poolname, LOV_MAXPOOLNAME);
453         new_pool->pool_name[LOV_MAXPOOLNAME] = '\0';
454         new_pool->pool_lobd = obd;
455         /* ref count init to 1 because when created a pool is always used
456          * up to deletion
457          */
458         cfs_atomic_set(&new_pool->pool_refcount, 1);
459         rc = lov_ost_pool_init(&new_pool->pool_obds, 0);
460         if (rc)
461                GOTO(out_err, rc);
462
463         memset(&(new_pool->pool_rr), 0, sizeof(struct lov_qos_rr));
464         rc = lov_ost_pool_init(&new_pool->pool_rr.lqr_pool, 0);
465         if (rc)
466                 GOTO(out_free_pool_obds, rc);
467
468         CFS_INIT_HLIST_NODE(&new_pool->pool_hash);
469
470 #ifdef LPROCFS
471         /* we need this assert seq_file is not implementated for liblustre */
472         /* get ref for /proc file */
473         lov_pool_getref(new_pool);
474         new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
475                                                        poolname, NULL, NULL,
476                                                        new_pool,
477                                                        &pool_proc_operations);
478         if (IS_ERR(new_pool->pool_proc_entry)) {
479                 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
480                 new_pool->pool_proc_entry = NULL;
481                 lov_pool_putref(new_pool);
482         }
483         CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool, new_pool->pool_proc_entry);
484 #endif
485
486         spin_lock(&obd->obd_dev_lock);
487         cfs_list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
488         lov->lov_pool_count++;
489         spin_unlock(&obd->obd_dev_lock);
490
491         /* add to find only when it fully ready  */
492         rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
493                                  &new_pool->pool_hash);
494         if (rc)
495                 GOTO(out_err, rc = -EEXIST);
496
497         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
498                poolname, lov->lov_pool_count);
499
500         RETURN(0);
501
502 out_err:
503         spin_lock(&obd->obd_dev_lock);
504         cfs_list_del_init(&new_pool->pool_list);
505         lov->lov_pool_count--;
506         spin_unlock(&obd->obd_dev_lock);
507
508         lprocfs_remove(&new_pool->pool_proc_entry);
509
510         lov_ost_pool_free(&new_pool->pool_rr.lqr_pool);
511 out_free_pool_obds:
512         lov_ost_pool_free(&new_pool->pool_obds);
513         OBD_FREE_PTR(new_pool);
514         return rc;
515 }
516
517 int lov_pool_del(struct obd_device *obd, char *poolname)
518 {
519         struct lov_obd *lov;
520         struct pool_desc *pool;
521         ENTRY;
522
523         lov = &(obd->u.lov);
524
525         /* lookup and kill hash reference */
526         pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
527         if (pool == NULL)
528                 RETURN(-ENOENT);
529
530         if (pool->pool_proc_entry != NULL) {
531                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
532                 lprocfs_remove(&pool->pool_proc_entry);
533                 lov_pool_putref(pool);
534         }
535
536         spin_lock(&obd->obd_dev_lock);
537         cfs_list_del_init(&pool->pool_list);
538         lov->lov_pool_count--;
539         spin_unlock(&obd->obd_dev_lock);
540
541         /* release last reference */
542         lov_pool_putref(pool);
543
544         RETURN(0);
545 }
546
547
548 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
549 {
550         struct obd_uuid ost_uuid;
551         struct lov_obd *lov;
552         struct pool_desc *pool;
553         unsigned int lov_idx;
554         int rc;
555         ENTRY;
556
557         lov = &(obd->u.lov);
558
559         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
560         if (pool == NULL)
561                 RETURN(-ENOENT);
562
563         obd_str2uuid(&ost_uuid, ostname);
564
565
566         /* search ost in lov array */
567         obd_getref(obd);
568         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
569                 if (!lov->lov_tgts[lov_idx])
570                         continue;
571                 if (obd_uuid_equals(&ost_uuid,
572                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
573                         break;
574         }
575         /* test if ost found in lov */
576         if (lov_idx == lov->desc.ld_tgt_count)
577                 GOTO(out, rc = -EINVAL);
578
579         rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
580         if (rc)
581                 GOTO(out, rc);
582
583         pool->pool_rr.lqr_dirty = 1;
584
585         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
586                ostname, poolname,  pool_tgt_count(pool));
587
588         EXIT;
589 out:
590         obd_putref(obd);
591         lov_pool_putref(pool);
592         return rc;
593 }
594
595 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
596 {
597         struct obd_uuid ost_uuid;
598         struct lov_obd *lov;
599         struct pool_desc *pool;
600         unsigned int lov_idx;
601         int rc = 0;
602         ENTRY;
603
604         lov = &(obd->u.lov);
605
606         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
607         if (pool == NULL)
608                 RETURN(-ENOENT);
609
610         obd_str2uuid(&ost_uuid, ostname);
611
612         obd_getref(obd);
613         /* search ost in lov array, to get index */
614         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
615                 if (!lov->lov_tgts[lov_idx])
616                         continue;
617
618                 if (obd_uuid_equals(&ost_uuid,
619                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
620                         break;
621         }
622
623         /* test if ost found in lov */
624         if (lov_idx == lov->desc.ld_tgt_count)
625                 GOTO(out, rc = -EINVAL);
626
627         lov_ost_pool_remove(&pool->pool_obds, lov_idx);
628
629         pool->pool_rr.lqr_dirty = 1;
630
631         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
632                poolname);
633
634         EXIT;
635 out:
636         obd_putref(obd);
637         lov_pool_putref(pool);
638         return rc;
639 }
640
641 int lov_check_index_in_pool(__u32 idx, struct pool_desc *pool)
642 {
643         int i, rc;
644         ENTRY;
645
646         /* caller may no have a ref on pool if it got the pool
647          * without calling lov_find_pool() (e.g. go through the lov pool
648          * list)
649          */
650         lov_pool_getref(pool);
651
652         down_read(&pool_tgt_rw_sem(pool));
653
654         for (i = 0; i < pool_tgt_count(pool); i++) {
655                 if (pool_tgt_array(pool)[i] == idx)
656                         GOTO(out, rc = 0);
657         }
658         rc = -ENOENT;
659         EXIT;
660 out:
661         up_read(&pool_tgt_rw_sem(pool));
662
663         lov_pool_putref(pool);
664         return rc;
665 }
666
667 struct pool_desc *lov_find_pool(struct lov_obd *lov, char *poolname)
668 {
669         struct pool_desc *pool;
670
671         pool = NULL;
672         if (poolname[0] != '\0') {
673                 pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
674                 if (pool == NULL)
675                         CWARN("Request for an unknown pool ("LOV_POOLNAMEF")\n",
676                               poolname);
677                 if ((pool != NULL) && (pool_tgt_count(pool) == 0)) {
678                         CWARN("Request for an empty pool ("LOV_POOLNAMEF")\n",
679                                poolname);
680                         /* pool is ignored, so we remove ref on it */
681                         lov_pool_putref(pool);
682                         pool = NULL;
683                 }
684         }
685         return pool;
686 }
687