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