Whamcloud - gitweb
LU-1346 libcfs: replace libcfs wrappers with kernel API
[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 #define pool_tgt(_p, _i) \
55                 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
56
57 static void lov_pool_getref(struct pool_desc *pool)
58 {
59         CDEBUG(D_INFO, "pool %p\n", pool);
60         cfs_atomic_inc(&pool->pool_refcount);
61 }
62
63 void lov_pool_putref(struct pool_desc *pool) 
64 {
65         CDEBUG(D_INFO, "pool %p\n", pool);
66         if (cfs_atomic_dec_and_test(&pool->pool_refcount)) {
67                 LASSERT(cfs_hlist_unhashed(&pool->pool_hash));
68                 LASSERT(cfs_list_empty(&pool->pool_list));
69                 LASSERT(pool->pool_proc_entry == NULL);
70                 lov_ost_pool_free(&(pool->pool_rr.lqr_pool));
71                 lov_ost_pool_free(&(pool->pool_obds));
72                 OBD_FREE_PTR(pool);
73                 EXIT;
74         }
75 }
76
77 void lov_pool_putref_locked(struct pool_desc *pool)
78 {
79         CDEBUG(D_INFO, "pool %p\n", pool);
80         LASSERT(cfs_atomic_read(&pool->pool_refcount) > 1);
81
82         cfs_atomic_dec(&pool->pool_refcount);
83 }
84
85 /*
86  * hash function using a Rotating Hash algorithm
87  * Knuth, D. The Art of Computer Programming,
88  * Volume 3: Sorting and Searching,
89  * Chapter 6.4.
90  * Addison Wesley, 1973
91  */
92 static __u32 pool_hashfn(cfs_hash_t *hash_body, const void *key, unsigned mask)
93 {
94         int i;
95         __u32 result;
96         char *poolname;
97
98         result = 0;
99         poolname = (char *)key;
100         for (i = 0; i < LOV_MAXPOOLNAME; i++) {
101                 if (poolname[i] == '\0')
102                         break;
103                 result = (result << 4)^(result >> 28) ^  poolname[i];
104         }
105         return (result % mask);
106 }
107
108 static void *pool_key(cfs_hlist_node_t *hnode)
109 {
110         struct pool_desc *pool;
111
112         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
113         return (pool->pool_name);
114 }
115
116 static int pool_hashkey_keycmp(const void *key, cfs_hlist_node_t *compared_hnode)
117 {
118         char *pool_name;
119         struct pool_desc *pool;
120
121         pool_name = (char *)key;
122         pool = cfs_hlist_entry(compared_hnode, struct pool_desc, pool_hash);
123         return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
124 }
125
126 static void *pool_hashobject(cfs_hlist_node_t *hnode)
127 {
128         return cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
129 }
130
131 static void pool_hashrefcount_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
132 {
133         struct pool_desc *pool;
134
135         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
136         lov_pool_getref(pool);
137 }
138
139 static void pool_hashrefcount_put_locked(cfs_hash_t *hs,
140                                          cfs_hlist_node_t *hnode)
141 {
142         struct pool_desc *pool;
143
144         pool = cfs_hlist_entry(hnode, struct pool_desc, pool_hash);
145         lov_pool_putref_locked(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         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                 up_read(&pool_tgt_rw_sem(iter->pool));
194                 return NULL;
195         }
196         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         down_read(&pool_tgt_rw_sem(iter->pool));
267         tgt = pool_tgt(iter->pool, iter->idx);
268         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         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         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         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         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         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         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                         up_write(&op->op_rw_sem);
406                         EXIT;
407                         return 0;
408                 }
409         }
410
411         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         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         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_lobd = obd;
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         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         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         spin_lock(&obd->obd_dev_lock);
502         cfs_list_del_init(&new_pool->pool_list);
503         lov->lov_pool_count--;
504         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         spin_lock(&obd->obd_dev_lock);
535         cfs_list_del_init(&pool->pool_list);
536         lov->lov_pool_count--;
537         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         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         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