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