Whamcloud - gitweb
LU-5396 lov: (and ldlm) make some functions static
[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 #include <libcfs/libcfs.h>
48
49 #include <obd.h>
50 #include "lov_internal.h"
51
52 #define pool_tgt(_p, _i) \
53                 _p->pool_lobd->u.lov.lov_tgts[_p->pool_obds.op_array[_i]]
54
55 static void lov_pool_getref(struct pool_desc *pool)
56 {
57         CDEBUG(D_INFO, "pool %p\n", pool);
58         atomic_inc(&pool->pool_refcount);
59 }
60
61 static void lov_pool_putref(struct pool_desc *pool)
62 {
63         CDEBUG(D_INFO, "pool %p\n", pool);
64         if (atomic_dec_and_test(&pool->pool_refcount)) {
65                 LASSERT(hlist_unhashed(&pool->pool_hash));
66                 LASSERT(list_empty(&pool->pool_list));
67                 LASSERT(pool->pool_proc_entry == NULL);
68                 lov_ost_pool_free(&(pool->pool_obds));
69                 OBD_FREE_PTR(pool);
70                 EXIT;
71         }
72 }
73
74 static void lov_pool_putref_locked(struct pool_desc *pool)
75 {
76         CDEBUG(D_INFO, "pool %p\n", pool);
77         LASSERT(atomic_read(&pool->pool_refcount) > 1);
78
79         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(struct hlist_node *hnode)
106 {
107         struct pool_desc *pool;
108
109         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
110         return (pool->pool_name);
111 }
112
113 static int
114 pool_hashkey_keycmp(const void *key, struct hlist_node *compared_hnode)
115 {
116         char *pool_name;
117         struct pool_desc *pool;
118
119         pool_name = (char *)key;
120         pool = hlist_entry(compared_hnode, struct pool_desc, pool_hash);
121         return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME);
122 }
123
124 static void *pool_hashobject(struct hlist_node *hnode)
125 {
126         return hlist_entry(hnode, struct pool_desc, pool_hash);
127 }
128
129 static void pool_hashrefcount_get(cfs_hash_t *hs, struct hlist_node *hnode)
130 {
131         struct pool_desc *pool;
132
133         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
134         lov_pool_getref(pool);
135 }
136
137 static void pool_hashrefcount_put_locked(cfs_hash_t *hs,
138                                          struct hlist_node *hnode)
139 {
140         struct pool_desc *pool;
141
142         pool = hlist_entry(hnode, struct pool_desc, pool_hash);
143         lov_pool_putref_locked(pool);
144 }
145
146 cfs_hash_ops_t pool_hash_operations = {
147         .hs_hash        = pool_hashfn,
148         .hs_key         = pool_key,
149         .hs_keycmp      = pool_hashkey_keycmp,
150         .hs_object      = pool_hashobject,
151         .hs_get         = pool_hashrefcount_get,
152         .hs_put_locked  = pool_hashrefcount_put_locked,
153
154 };
155
156 #ifdef LPROCFS
157 /* ifdef needed for liblustre support */
158 /*
159  * pool /proc seq_file methods
160  */
161 /*
162  * iterator is used to go through the target pool entries
163  * index is the current entry index in the lp_array[] array
164  * index >= pos returned to the seq_file interface
165  * pos is from 0 to (pool->pool_obds.op_count - 1)
166  */
167 #define POOL_IT_MAGIC 0xB001CEA0
168 struct pool_iterator {
169         int magic;
170         struct pool_desc *pool;
171         int idx;        /* from 0 to pool_tgt_size - 1 */
172 };
173
174 static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos)
175 {
176         struct pool_iterator *iter = (struct pool_iterator *)s->private;
177         int prev_idx;
178
179         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
180
181         /* test if end of file */
182         if (*pos >= pool_tgt_count(iter->pool))
183                 return NULL;
184
185         /* iterate to find a non empty entry */
186         prev_idx = iter->idx;
187         down_read(&pool_tgt_rw_sem(iter->pool));
188         iter->idx++;
189         if (iter->idx == pool_tgt_count(iter->pool)) {
190                 iter->idx = prev_idx; /* we stay on the last entry */
191                 up_read(&pool_tgt_rw_sem(iter->pool));
192                 return NULL;
193         }
194         up_read(&pool_tgt_rw_sem(iter->pool));
195         (*pos)++;
196         /* return != NULL to continue */
197         return iter;
198 }
199
200 static void *pool_proc_start(struct seq_file *s, loff_t *pos)
201 {
202         struct pool_desc *pool = (struct pool_desc *)s->private;
203         struct pool_iterator *iter;
204
205         lov_pool_getref(pool);
206         if ((pool_tgt_count(pool) == 0) ||
207             (*pos >= pool_tgt_count(pool))) {
208                 /* iter is not created, so stop() has no way to
209                  * find pool to dec ref */
210                 lov_pool_putref(pool);
211                 return NULL;
212         }
213
214         OBD_ALLOC_PTR(iter);
215         if (!iter)
216                 return ERR_PTR(-ENOMEM);
217         iter->magic = POOL_IT_MAGIC;
218         iter->pool = pool;
219         iter->idx = 0;
220
221         /* we use seq_file private field to memorized iterator so
222          * we can free it at stop() */
223         /* /!\ do not forget to restore it to pool before freeing it */
224         s->private = iter;
225         if (*pos > 0) {
226                 loff_t i;
227                 void *ptr;
228
229                 i = 0;
230                 do {
231                      ptr = pool_proc_next(s, &iter, &i);
232                 } while ((i < *pos) && (ptr != NULL));
233                 return ptr;
234         }
235         return iter;
236 }
237
238 static void pool_proc_stop(struct seq_file *s, void *v)
239 {
240         struct pool_iterator *iter = (struct pool_iterator *)s->private;
241
242         /* in some cases stop() method is called 2 times, without
243          * calling start() method (see seq_read() from fs/seq_file.c)
244          * we have to free only if s->private is an iterator */
245         if ((iter) && (iter->magic == POOL_IT_MAGIC)) {
246                 /* we restore s->private so next call to pool_proc_start()
247                  * will work */
248                 s->private = iter->pool;
249                 lov_pool_putref(iter->pool);
250                 OBD_FREE_PTR(iter);
251         }
252         return;
253 }
254
255 static int pool_proc_show(struct seq_file *s, void *v)
256 {
257         struct pool_iterator *iter = (struct pool_iterator *)v;
258         struct lov_tgt_desc *tgt;
259
260         LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic);
261         LASSERT(iter->pool != NULL);
262         LASSERT(iter->idx <= pool_tgt_count(iter->pool));
263
264         down_read(&pool_tgt_rw_sem(iter->pool));
265         tgt = pool_tgt(iter->pool, iter->idx);
266         up_read(&pool_tgt_rw_sem(iter->pool));
267         if (tgt)
268                 seq_printf(s, "%s\n", obd_uuid2str(&(tgt->ltd_uuid)));
269
270         return 0;
271 }
272
273 static struct seq_operations pool_proc_ops = {
274         .start          = pool_proc_start,
275         .next           = pool_proc_next,
276         .stop           = pool_proc_stop,
277         .show           = pool_proc_show,
278 };
279
280 static int pool_proc_open(struct inode *inode, struct file *file)
281 {
282         int rc;
283
284         rc = seq_open(file, &pool_proc_ops);
285         if (!rc) {
286                 struct seq_file *s = file->private_data;
287                 s->private = PDE_DATA(inode);
288         }
289         return rc;
290 }
291
292 static struct file_operations pool_proc_operations = {
293         .open           = pool_proc_open,
294         .read           = seq_read,
295         .llseek         = seq_lseek,
296         .release        = seq_release,
297 };
298 #endif /* LPROCFS */
299
300 void lov_dump_pool(int level, struct pool_desc *pool)
301 {
302         int i;
303
304         lov_pool_getref(pool);
305
306         CDEBUG(level, "pool "LOV_POOLNAMEF" has %d members\n",
307                pool->pool_name, pool->pool_obds.op_count);
308         down_read(&pool_tgt_rw_sem(pool));
309
310         for (i = 0; i < pool_tgt_count(pool) ; i++) {
311                 if (!pool_tgt(pool, i) || !(pool_tgt(pool, i))->ltd_exp)
312                         continue;
313                 CDEBUG(level, "pool "LOV_POOLNAMEF"[%d] = %s\n",
314                        pool->pool_name, i,
315                        obd_uuid2str(&((pool_tgt(pool, i))->ltd_uuid)));
316         }
317
318         up_read(&pool_tgt_rw_sem(pool));
319         lov_pool_putref(pool);
320 }
321
322 #define LOV_POOL_INIT_COUNT 2
323 int lov_ost_pool_init(struct ost_pool *op, unsigned int count)
324 {
325         ENTRY;
326
327         if (count == 0)
328                 count = LOV_POOL_INIT_COUNT;
329         op->op_array = NULL;
330         op->op_count = 0;
331         init_rwsem(&op->op_rw_sem);
332         op->op_size = count;
333         OBD_ALLOC(op->op_array, op->op_size * sizeof(op->op_array[0]));
334         if (op->op_array == NULL) {
335                 op->op_size = 0;
336                 RETURN(-ENOMEM);
337         }
338         EXIT;
339         return 0;
340 }
341
342 /* Caller must hold write op_rwlock */
343 int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count)
344 {
345         __u32 *new;
346         int new_size;
347
348         LASSERT(min_count != 0);
349
350         if (op->op_count < op->op_size)
351                 return 0;
352
353         new_size = max(min_count, 2 * op->op_size);
354         OBD_ALLOC(new, new_size * sizeof(op->op_array[0]));
355         if (new == NULL)
356                 return -ENOMEM;
357
358         /* copy old array to new one */
359         memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0]));
360         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
361         op->op_array = new;
362         op->op_size = new_size;
363         return 0;
364 }
365
366 int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count)
367 {
368         int rc = 0, i;
369         ENTRY;
370
371         down_write(&op->op_rw_sem);
372
373         rc = lov_ost_pool_extend(op, min_count);
374         if (rc)
375                 GOTO(out, rc);
376
377         /* search ost in pool array */
378         for (i = 0; i < op->op_count; i++) {
379                 if (op->op_array[i] == idx)
380                         GOTO(out, rc = -EEXIST);
381         }
382         /* ost not found we add it */
383         op->op_array[op->op_count] = idx;
384         op->op_count++;
385         EXIT;
386 out:
387         up_write(&op->op_rw_sem);
388         return rc;
389 }
390
391 int lov_ost_pool_remove(struct ost_pool *op, __u32 idx)
392 {
393         int i;
394         ENTRY;
395
396         down_write(&op->op_rw_sem);
397
398         for (i = 0; i < op->op_count; i++) {
399                 if (op->op_array[i] == idx) {
400                         memmove(&op->op_array[i], &op->op_array[i + 1],
401                                 (op->op_count - i - 1) * sizeof(op->op_array[0]));
402                         op->op_count--;
403                         up_write(&op->op_rw_sem);
404                         EXIT;
405                         return 0;
406                 }
407         }
408
409         up_write(&op->op_rw_sem);
410         RETURN(-EINVAL);
411 }
412
413 int lov_ost_pool_free(struct ost_pool *op)
414 {
415         ENTRY;
416
417         if (op->op_size == 0)
418                 RETURN(0);
419
420         down_write(&op->op_rw_sem);
421
422         OBD_FREE(op->op_array, op->op_size * sizeof(op->op_array[0]));
423         op->op_array = NULL;
424         op->op_count = 0;
425         op->op_size = 0;
426
427         up_write(&op->op_rw_sem);
428         RETURN(0);
429 }
430
431
432 int lov_pool_new(struct obd_device *obd, char *poolname)
433 {
434         struct lov_obd *lov;
435         struct pool_desc *new_pool;
436         int rc;
437         ENTRY;
438
439         lov = &(obd->u.lov);
440
441         if (strlen(poolname) > LOV_MAXPOOLNAME)
442                 RETURN(-ENAMETOOLONG);
443
444         OBD_ALLOC_PTR(new_pool);
445         if (new_pool == NULL)
446                 RETURN(-ENOMEM);
447
448         strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name));
449         new_pool->pool_lobd = obd;
450         /* ref count init to 1 because when created a pool is always used
451          * up to deletion
452          */
453         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         INIT_HLIST_NODE(&new_pool->pool_hash);
459
460 #ifdef LPROCFS
461         /* we need this assert seq_file is not implementated for liblustre */
462         /* get ref for /proc file */
463         lov_pool_getref(new_pool);
464         new_pool->pool_proc_entry = lprocfs_add_simple(lov->lov_pool_proc_entry,
465                                                         poolname, new_pool,
466                                                         &pool_proc_operations);
467         if (IS_ERR(new_pool->pool_proc_entry)) {
468                 CWARN("Cannot add proc pool entry "LOV_POOLNAMEF"\n", poolname);
469                 new_pool->pool_proc_entry = NULL;
470                 lov_pool_putref(new_pool);
471         }
472         CDEBUG(D_INFO, "pool %p - proc %p\n", new_pool, new_pool->pool_proc_entry);
473 #endif
474
475         spin_lock(&obd->obd_dev_lock);
476         list_add_tail(&new_pool->pool_list, &lov->lov_pool_list);
477         lov->lov_pool_count++;
478         spin_unlock(&obd->obd_dev_lock);
479
480         /* add to find only when it fully ready  */
481         rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname,
482                                  &new_pool->pool_hash);
483         if (rc)
484                 GOTO(out_err, rc = -EEXIST);
485
486         CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n",
487                poolname, lov->lov_pool_count);
488
489         RETURN(0);
490
491 out_err:
492         spin_lock(&obd->obd_dev_lock);
493         list_del_init(&new_pool->pool_list);
494         lov->lov_pool_count--;
495         spin_unlock(&obd->obd_dev_lock);
496         lprocfs_remove(&new_pool->pool_proc_entry);
497         lov_ost_pool_free(&new_pool->pool_obds);
498         OBD_FREE_PTR(new_pool);
499
500         return rc;
501 }
502
503 int lov_pool_del(struct obd_device *obd, char *poolname)
504 {
505         struct lov_obd *lov;
506         struct pool_desc *pool;
507         ENTRY;
508
509         lov = &(obd->u.lov);
510
511         /* lookup and kill hash reference */
512         pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname);
513         if (pool == NULL)
514                 RETURN(-ENOENT);
515
516         if (pool->pool_proc_entry != NULL) {
517                 CDEBUG(D_INFO, "proc entry %p\n", pool->pool_proc_entry);
518                 lprocfs_remove(&pool->pool_proc_entry);
519                 lov_pool_putref(pool);
520         }
521
522         spin_lock(&obd->obd_dev_lock);
523         list_del_init(&pool->pool_list);
524         lov->lov_pool_count--;
525         spin_unlock(&obd->obd_dev_lock);
526
527         /* release last reference */
528         lov_pool_putref(pool);
529
530         RETURN(0);
531 }
532
533
534 int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname)
535 {
536         struct obd_uuid ost_uuid;
537         struct lov_obd *lov;
538         struct pool_desc *pool;
539         unsigned int lov_idx;
540         int rc;
541         ENTRY;
542
543         lov = &(obd->u.lov);
544
545         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
546         if (pool == NULL)
547                 RETURN(-ENOENT);
548
549         obd_str2uuid(&ost_uuid, ostname);
550
551
552         /* search ost in lov array */
553         obd_getref(obd);
554         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
555                 if (!lov->lov_tgts[lov_idx])
556                         continue;
557                 if (obd_uuid_equals(&ost_uuid,
558                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
559                         break;
560         }
561         /* test if ost found in lov */
562         if (lov_idx == lov->desc.ld_tgt_count)
563                 GOTO(out, rc = -EINVAL);
564
565         rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size);
566         if (rc)
567                 GOTO(out, rc);
568
569         CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n",
570                ostname, poolname,  pool_tgt_count(pool));
571
572         EXIT;
573 out:
574         obd_putref(obd);
575         lov_pool_putref(pool);
576         return rc;
577 }
578
579 int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname)
580 {
581         struct obd_uuid ost_uuid;
582         struct lov_obd *lov;
583         struct pool_desc *pool;
584         unsigned int lov_idx;
585         int rc = 0;
586         ENTRY;
587
588         lov = &(obd->u.lov);
589
590         pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname);
591         if (pool == NULL)
592                 RETURN(-ENOENT);
593
594         obd_str2uuid(&ost_uuid, ostname);
595
596         obd_getref(obd);
597         /* search ost in lov array, to get index */
598         for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) {
599                 if (!lov->lov_tgts[lov_idx])
600                         continue;
601
602                 if (obd_uuid_equals(&ost_uuid,
603                                     &(lov->lov_tgts[lov_idx]->ltd_uuid)))
604                         break;
605         }
606
607         /* test if ost found in lov */
608         if (lov_idx == lov->desc.ld_tgt_count)
609                 GOTO(out, rc = -EINVAL);
610
611         lov_ost_pool_remove(&pool->pool_obds, lov_idx);
612
613         CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname,
614                poolname);
615
616         EXIT;
617 out:
618         obd_putref(obd);
619         lov_pool_putref(pool);
620         return rc;
621 }