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