Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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 |
Oleg Drokin | 06894ee | 2016-06-14 23:33:41 -0400 | [diff] [blame] | 17 | * version 2 along with this program; If not, see |
| 18 | * http://http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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, 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 | |
Greg Kroah-Hartman | 9fdaf8c | 2014-07-11 20:51:16 -0700 | [diff] [blame] | 43 | #include "../../include/linux/libcfs/libcfs.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 44 | |
Greg Kroah-Hartman | 0cf0f7a | 2014-07-11 22:01:58 -0700 | [diff] [blame] | 45 | #include "../include/obd.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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 | 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)); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 63 | LASSERT(!pool->pool_debugfs_entry); |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 64 | lov_ost_pool_free(&pool->pool_obds); |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 65 | kfree(pool); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Le Tan | 12e397c | 2015-02-11 12:13:14 +0800 | [diff] [blame] | 69 | static void lov_pool_putref_locked(struct pool_desc *pool) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 70 | { |
| 71 | CDEBUG(D_INFO, "pool %p\n", pool); |
| 72 | LASSERT(atomic_read(&pool->pool_refcount) > 1); |
| 73 | |
| 74 | atomic_dec(&pool->pool_refcount); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * hash function using a Rotating Hash algorithm |
| 79 | * Knuth, D. The Art of Computer Programming, |
| 80 | * Volume 3: Sorting and Searching, |
| 81 | * Chapter 6.4. |
| 82 | * Addison Wesley, 1973 |
| 83 | */ |
Lisa Nguyen | 6da6eab | 2013-10-21 18:16:26 -0700 | [diff] [blame] | 84 | static __u32 pool_hashfn(struct cfs_hash *hash_body, const void *key, unsigned mask) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 85 | { |
| 86 | int i; |
| 87 | __u32 result; |
| 88 | char *poolname; |
| 89 | |
| 90 | result = 0; |
| 91 | poolname = (char *)key; |
| 92 | for (i = 0; i < LOV_MAXPOOLNAME; i++) { |
| 93 | if (poolname[i] == '\0') |
| 94 | break; |
Oleg Drokin | cd94f23 | 2016-08-21 18:04:34 -0400 | [diff] [blame] | 95 | result = (result << 4) ^ (result >> 28) ^ poolname[i]; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 96 | } |
| 97 | return (result % mask); |
| 98 | } |
| 99 | |
| 100 | static void *pool_key(struct hlist_node *hnode) |
| 101 | { |
| 102 | struct pool_desc *pool; |
| 103 | |
| 104 | pool = hlist_entry(hnode, struct pool_desc, pool_hash); |
Greg Donald | e829197 | 2014-08-31 17:40:17 -0500 | [diff] [blame] | 105 | return pool->pool_name; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int pool_hashkey_keycmp(const void *key, struct hlist_node *compared_hnode) |
| 109 | { |
| 110 | char *pool_name; |
| 111 | struct pool_desc *pool; |
| 112 | |
| 113 | pool_name = (char *)key; |
| 114 | pool = hlist_entry(compared_hnode, struct pool_desc, pool_hash); |
| 115 | return !strncmp(pool_name, pool->pool_name, LOV_MAXPOOLNAME); |
| 116 | } |
| 117 | |
| 118 | static void *pool_hashobject(struct hlist_node *hnode) |
| 119 | { |
| 120 | return hlist_entry(hnode, struct pool_desc, pool_hash); |
| 121 | } |
| 122 | |
Lisa Nguyen | 6da6eab | 2013-10-21 18:16:26 -0700 | [diff] [blame] | 123 | static void pool_hashrefcount_get(struct cfs_hash *hs, struct hlist_node *hnode) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 124 | { |
| 125 | struct pool_desc *pool; |
| 126 | |
| 127 | pool = hlist_entry(hnode, struct pool_desc, pool_hash); |
| 128 | lov_pool_getref(pool); |
| 129 | } |
| 130 | |
Lisa Nguyen | 6da6eab | 2013-10-21 18:16:26 -0700 | [diff] [blame] | 131 | static void pool_hashrefcount_put_locked(struct cfs_hash *hs, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 132 | struct hlist_node *hnode) |
| 133 | { |
| 134 | struct pool_desc *pool; |
| 135 | |
| 136 | pool = hlist_entry(hnode, struct pool_desc, pool_hash); |
| 137 | lov_pool_putref_locked(pool); |
| 138 | } |
| 139 | |
James Simmons | db9fc06 | 2015-10-28 12:54:24 -0400 | [diff] [blame] | 140 | struct cfs_hash_ops pool_hash_operations = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 141 | .hs_hash = pool_hashfn, |
James Simmons | db9fc06 | 2015-10-28 12:54:24 -0400 | [diff] [blame] | 142 | .hs_key = pool_key, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 143 | .hs_keycmp = pool_hashkey_keycmp, |
| 144 | .hs_object = pool_hashobject, |
James Simmons | db9fc06 | 2015-10-28 12:54:24 -0400 | [diff] [blame] | 145 | .hs_get = pool_hashrefcount_get, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 146 | .hs_put_locked = pool_hashrefcount_put_locked, |
| 147 | |
| 148 | }; |
| 149 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 150 | /* |
Oleg Drokin | 64f17a1 | 2016-02-16 00:46:37 -0500 | [diff] [blame] | 151 | * pool debugfs seq_file methods |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 152 | */ |
| 153 | /* |
| 154 | * iterator is used to go through the target pool entries |
| 155 | * index is the current entry index in the lp_array[] array |
| 156 | * index >= pos returned to the seq_file interface |
| 157 | * pos is from 0 to (pool->pool_obds.op_count - 1) |
| 158 | */ |
| 159 | #define POOL_IT_MAGIC 0xB001CEA0 |
| 160 | struct pool_iterator { |
| 161 | int magic; |
| 162 | struct pool_desc *pool; |
| 163 | int idx; /* from 0 to pool_tgt_size - 1 */ |
| 164 | }; |
| 165 | |
| 166 | static void *pool_proc_next(struct seq_file *s, void *v, loff_t *pos) |
| 167 | { |
| 168 | struct pool_iterator *iter = (struct pool_iterator *)s->private; |
| 169 | int prev_idx; |
| 170 | |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 171 | LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 172 | |
| 173 | /* test if end of file */ |
| 174 | if (*pos >= pool_tgt_count(iter->pool)) |
| 175 | return NULL; |
| 176 | |
| 177 | /* iterate to find a non empty entry */ |
| 178 | prev_idx = iter->idx; |
| 179 | down_read(&pool_tgt_rw_sem(iter->pool)); |
| 180 | iter->idx++; |
| 181 | if (iter->idx == pool_tgt_count(iter->pool)) { |
| 182 | iter->idx = prev_idx; /* we stay on the last entry */ |
| 183 | up_read(&pool_tgt_rw_sem(iter->pool)); |
| 184 | return NULL; |
| 185 | } |
| 186 | up_read(&pool_tgt_rw_sem(iter->pool)); |
| 187 | (*pos)++; |
| 188 | /* return != NULL to continue */ |
| 189 | return iter; |
| 190 | } |
| 191 | |
| 192 | static void *pool_proc_start(struct seq_file *s, loff_t *pos) |
| 193 | { |
| 194 | struct pool_desc *pool = (struct pool_desc *)s->private; |
| 195 | struct pool_iterator *iter; |
| 196 | |
| 197 | lov_pool_getref(pool); |
| 198 | if ((pool_tgt_count(pool) == 0) || |
| 199 | (*pos >= pool_tgt_count(pool))) { |
| 200 | /* iter is not created, so stop() has no way to |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 201 | * find pool to dec ref |
| 202 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 203 | lov_pool_putref(pool); |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 207 | iter = kzalloc(sizeof(*iter), GFP_NOFS); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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 |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 215 | * we can free it at stop() |
| 216 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 217 | /* /!\ do not forget to restore it to pool before freeing it */ |
| 218 | s->private = iter; |
| 219 | if (*pos > 0) { |
| 220 | loff_t i; |
| 221 | void *ptr; |
| 222 | |
| 223 | i = 0; |
| 224 | do { |
Oleg Drokin | defa220 | 2016-02-24 22:00:39 -0500 | [diff] [blame] | 225 | ptr = pool_proc_next(s, &iter, &i); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 226 | } while ((i < *pos) && ptr); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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) |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 238 | * we have to free only if s->private is an iterator |
| 239 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 240 | if ((iter) && (iter->magic == POOL_IT_MAGIC)) { |
| 241 | /* we restore s->private so next call to pool_proc_start() |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 242 | * will work |
| 243 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 244 | s->private = iter->pool; |
| 245 | lov_pool_putref(iter->pool); |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 246 | kfree(iter); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 247 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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 | |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 255 | LASSERTF(iter->magic == POOL_IT_MAGIC, "%08X\n", iter->magic); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 256 | LASSERT(iter->pool); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 257 | LASSERT(iter->idx <= pool_tgt_count(iter->pool)); |
| 258 | |
| 259 | down_read(&pool_tgt_rw_sem(iter->pool)); |
| 260 | tgt = pool_tgt(iter->pool, iter->idx); |
| 261 | up_read(&pool_tgt_rw_sem(iter->pool)); |
| 262 | if (tgt) |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 263 | seq_printf(s, "%s\n", obd_uuid2str(&tgt->ltd_uuid)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Xavier Roche | 02b3107 | 2015-06-05 10:19:20 +0200 | [diff] [blame] | 268 | static const struct seq_operations pool_proc_ops = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 269 | .start = pool_proc_start, |
| 270 | .next = pool_proc_next, |
| 271 | .stop = pool_proc_stop, |
| 272 | .show = pool_proc_show, |
| 273 | }; |
| 274 | |
| 275 | static int pool_proc_open(struct inode *inode, struct file *file) |
| 276 | { |
| 277 | int rc; |
| 278 | |
| 279 | rc = seq_open(file, &pool_proc_ops); |
| 280 | if (!rc) { |
| 281 | struct seq_file *s = file->private_data; |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 282 | |
Dmitry Eremin | 61e87ab | 2015-05-21 15:32:27 -0400 | [diff] [blame] | 283 | s->private = inode->i_private; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 284 | } |
| 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | static struct file_operations pool_proc_operations = { |
| 289 | .open = pool_proc_open, |
| 290 | .read = seq_read, |
| 291 | .llseek = seq_lseek, |
| 292 | .release = seq_release, |
| 293 | }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 294 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 295 | #define LOV_POOL_INIT_COUNT 2 |
| 296 | int lov_ost_pool_init(struct ost_pool *op, unsigned int count) |
| 297 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 298 | if (count == 0) |
| 299 | count = LOV_POOL_INIT_COUNT; |
| 300 | op->op_array = NULL; |
| 301 | op->op_count = 0; |
| 302 | init_rwsem(&op->op_rw_sem); |
| 303 | op->op_size = count; |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 304 | op->op_array = kcalloc(op->op_size, sizeof(op->op_array[0]), GFP_NOFS); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 305 | if (!op->op_array) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 306 | op->op_size = 0; |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 307 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 308 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /* Caller must hold write op_rwlock */ |
| 313 | int lov_ost_pool_extend(struct ost_pool *op, unsigned int min_count) |
| 314 | { |
| 315 | __u32 *new; |
| 316 | int new_size; |
| 317 | |
| 318 | LASSERT(min_count != 0); |
| 319 | |
| 320 | if (op->op_count < op->op_size) |
| 321 | return 0; |
| 322 | |
| 323 | new_size = max(min_count, 2 * op->op_size); |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 324 | new = kcalloc(new_size, sizeof(op->op_array[0]), GFP_NOFS); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 325 | if (!new) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 326 | return -ENOMEM; |
| 327 | |
| 328 | /* copy old array to new one */ |
| 329 | memcpy(new, op->op_array, op->op_size * sizeof(op->op_array[0])); |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 330 | kfree(op->op_array); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 331 | op->op_array = new; |
| 332 | op->op_size = new_size; |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int lov_ost_pool_add(struct ost_pool *op, __u32 idx, unsigned int min_count) |
| 337 | { |
| 338 | int rc = 0, i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 339 | |
| 340 | down_write(&op->op_rw_sem); |
| 341 | |
| 342 | rc = lov_ost_pool_extend(op, min_count); |
| 343 | if (rc) |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 344 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 345 | |
| 346 | /* search ost in pool array */ |
| 347 | for (i = 0; i < op->op_count; i++) { |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 348 | if (op->op_array[i] == idx) { |
| 349 | rc = -EEXIST; |
| 350 | goto out; |
| 351 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 352 | } |
| 353 | /* ost not found we add it */ |
| 354 | op->op_array[op->op_count] = idx; |
| 355 | op->op_count++; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 356 | out: |
| 357 | up_write(&op->op_rw_sem); |
| 358 | return rc; |
| 359 | } |
| 360 | |
| 361 | int lov_ost_pool_remove(struct ost_pool *op, __u32 idx) |
| 362 | { |
| 363 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 364 | |
| 365 | down_write(&op->op_rw_sem); |
| 366 | |
| 367 | for (i = 0; i < op->op_count; i++) { |
| 368 | if (op->op_array[i] == idx) { |
| 369 | memmove(&op->op_array[i], &op->op_array[i + 1], |
| 370 | (op->op_count - i - 1) * sizeof(op->op_array[0])); |
| 371 | op->op_count--; |
| 372 | up_write(&op->op_rw_sem); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | up_write(&op->op_rw_sem); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 378 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | int lov_ost_pool_free(struct ost_pool *op) |
| 382 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 383 | if (op->op_size == 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 384 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 385 | |
| 386 | down_write(&op->op_rw_sem); |
| 387 | |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 388 | kfree(op->op_array); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 389 | op->op_array = NULL; |
| 390 | op->op_count = 0; |
| 391 | op->op_size = 0; |
| 392 | |
| 393 | up_write(&op->op_rw_sem); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 394 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 395 | } |
| 396 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 397 | int lov_pool_new(struct obd_device *obd, char *poolname) |
| 398 | { |
| 399 | struct lov_obd *lov; |
| 400 | struct pool_desc *new_pool; |
| 401 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 402 | |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 403 | lov = &obd->u.lov; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 404 | |
| 405 | if (strlen(poolname) > LOV_MAXPOOLNAME) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 406 | return -ENAMETOOLONG; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 407 | |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 408 | new_pool = kzalloc(sizeof(*new_pool), GFP_NOFS); |
Julia Lawall | 36a86fe | 2015-06-20 18:59:04 +0200 | [diff] [blame] | 409 | if (!new_pool) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 410 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 411 | |
Dmitry Eremin | 9563fe8 | 2015-11-04 13:40:00 -0500 | [diff] [blame] | 412 | strlcpy(new_pool->pool_name, poolname, sizeof(new_pool->pool_name)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 413 | new_pool->pool_lobd = obd; |
| 414 | /* ref count init to 1 because when created a pool is always used |
| 415 | * up to deletion |
| 416 | */ |
| 417 | atomic_set(&new_pool->pool_refcount, 1); |
| 418 | rc = lov_ost_pool_init(&new_pool->pool_obds, 0); |
| 419 | if (rc) |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 420 | goto out_err; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 421 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 422 | INIT_HLIST_NODE(&new_pool->pool_hash); |
| 423 | |
Oleg Drokin | 64f17a1 | 2016-02-16 00:46:37 -0500 | [diff] [blame] | 424 | /* get ref for debugfs file */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 425 | lov_pool_getref(new_pool); |
Dmitry Eremin | 61e87ab | 2015-05-21 15:32:27 -0400 | [diff] [blame] | 426 | new_pool->pool_debugfs_entry = ldebugfs_add_simple( |
| 427 | lov->lov_pool_debugfs_entry, |
| 428 | poolname, new_pool, |
| 429 | &pool_proc_operations); |
| 430 | if (IS_ERR_OR_NULL(new_pool->pool_debugfs_entry)) { |
| 431 | CWARN("Cannot add debugfs pool entry "LOV_POOLNAMEF"\n", |
| 432 | poolname); |
| 433 | new_pool->pool_debugfs_entry = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 434 | lov_pool_putref(new_pool); |
| 435 | } |
Dmitry Eremin | 61e87ab | 2015-05-21 15:32:27 -0400 | [diff] [blame] | 436 | CDEBUG(D_INFO, "pool %p - proc %p\n", |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 437 | new_pool, new_pool->pool_debugfs_entry); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 438 | |
| 439 | spin_lock(&obd->obd_dev_lock); |
| 440 | list_add_tail(&new_pool->pool_list, &lov->lov_pool_list); |
| 441 | lov->lov_pool_count++; |
| 442 | spin_unlock(&obd->obd_dev_lock); |
| 443 | |
| 444 | /* add to find only when it fully ready */ |
| 445 | rc = cfs_hash_add_unique(lov->lov_pools_hash_body, poolname, |
| 446 | &new_pool->pool_hash); |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 447 | if (rc) { |
| 448 | rc = -EEXIST; |
| 449 | goto out_err; |
| 450 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 451 | |
| 452 | CDEBUG(D_CONFIG, LOV_POOLNAMEF" is pool #%d\n", |
| 453 | poolname, lov->lov_pool_count); |
| 454 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 455 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 456 | |
| 457 | out_err: |
| 458 | spin_lock(&obd->obd_dev_lock); |
| 459 | list_del_init(&new_pool->pool_list); |
| 460 | lov->lov_pool_count--; |
| 461 | spin_unlock(&obd->obd_dev_lock); |
Dmitry Eremin | 61e87ab | 2015-05-21 15:32:27 -0400 | [diff] [blame] | 462 | ldebugfs_remove(&new_pool->pool_debugfs_entry); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 463 | lov_ost_pool_free(&new_pool->pool_obds); |
Julia Lawall | 840c94d | 2015-04-12 23:34:20 +0200 | [diff] [blame] | 464 | kfree(new_pool); |
John L. Hammond | ae322e0 | 2016-05-04 10:28:54 -0400 | [diff] [blame] | 465 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 466 | return rc; |
| 467 | } |
| 468 | |
| 469 | int lov_pool_del(struct obd_device *obd, char *poolname) |
| 470 | { |
| 471 | struct lov_obd *lov; |
| 472 | struct pool_desc *pool; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 473 | |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 474 | lov = &obd->u.lov; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 475 | |
| 476 | /* lookup and kill hash reference */ |
| 477 | pool = cfs_hash_del_key(lov->lov_pools_hash_body, poolname); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 478 | if (!pool) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 479 | return -ENOENT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 480 | |
Dmitry Eremin | 61e87ab | 2015-05-21 15:32:27 -0400 | [diff] [blame] | 481 | if (!IS_ERR_OR_NULL(pool->pool_debugfs_entry)) { |
| 482 | CDEBUG(D_INFO, "proc entry %p\n", pool->pool_debugfs_entry); |
| 483 | ldebugfs_remove(&pool->pool_debugfs_entry); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 484 | lov_pool_putref(pool); |
| 485 | } |
| 486 | |
| 487 | spin_lock(&obd->obd_dev_lock); |
| 488 | list_del_init(&pool->pool_list); |
| 489 | lov->lov_pool_count--; |
| 490 | spin_unlock(&obd->obd_dev_lock); |
| 491 | |
| 492 | /* release last reference */ |
| 493 | lov_pool_putref(pool); |
| 494 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 495 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 496 | } |
| 497 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 498 | int lov_pool_add(struct obd_device *obd, char *poolname, char *ostname) |
| 499 | { |
| 500 | struct obd_uuid ost_uuid; |
| 501 | struct lov_obd *lov; |
| 502 | struct pool_desc *pool; |
| 503 | unsigned int lov_idx; |
| 504 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 505 | |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 506 | lov = &obd->u.lov; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 507 | |
| 508 | pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 509 | if (!pool) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 510 | return -ENOENT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 511 | |
| 512 | obd_str2uuid(&ost_uuid, ostname); |
| 513 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 514 | /* search ost in lov array */ |
| 515 | obd_getref(obd); |
| 516 | for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) { |
| 517 | if (!lov->lov_tgts[lov_idx]) |
| 518 | continue; |
| 519 | if (obd_uuid_equals(&ost_uuid, |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 520 | &lov->lov_tgts[lov_idx]->ltd_uuid)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | /* test if ost found in lov */ |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 524 | if (lov_idx == lov->desc.ld_tgt_count) { |
| 525 | rc = -EINVAL; |
| 526 | goto out; |
| 527 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 528 | |
| 529 | rc = lov_ost_pool_add(&pool->pool_obds, lov_idx, lov->lov_tgt_size); |
| 530 | if (rc) |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 531 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 532 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 533 | CDEBUG(D_CONFIG, "Added %s to "LOV_POOLNAMEF" as member %d\n", |
| 534 | ostname, poolname, pool_tgt_count(pool)); |
| 535 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 536 | out: |
| 537 | obd_putref(obd); |
| 538 | lov_pool_putref(pool); |
| 539 | return rc; |
| 540 | } |
| 541 | |
| 542 | int lov_pool_remove(struct obd_device *obd, char *poolname, char *ostname) |
| 543 | { |
| 544 | struct obd_uuid ost_uuid; |
| 545 | struct lov_obd *lov; |
| 546 | struct pool_desc *pool; |
| 547 | unsigned int lov_idx; |
| 548 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 549 | |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 550 | lov = &obd->u.lov; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 551 | |
| 552 | pool = cfs_hash_lookup(lov->lov_pools_hash_body, poolname); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 553 | if (!pool) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 554 | return -ENOENT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 555 | |
| 556 | obd_str2uuid(&ost_uuid, ostname); |
| 557 | |
| 558 | obd_getref(obd); |
| 559 | /* search ost in lov array, to get index */ |
| 560 | for (lov_idx = 0; lov_idx < lov->desc.ld_tgt_count; lov_idx++) { |
| 561 | if (!lov->lov_tgts[lov_idx]) |
| 562 | continue; |
| 563 | |
| 564 | if (obd_uuid_equals(&ost_uuid, |
Emoly Liu | 4988026 | 2016-08-20 17:34:27 -0400 | [diff] [blame] | 565 | &lov->lov_tgts[lov_idx]->ltd_uuid)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
| 569 | /* test if ost found in lov */ |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 570 | if (lov_idx == lov->desc.ld_tgt_count) { |
| 571 | rc = -EINVAL; |
| 572 | goto out; |
| 573 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 574 | |
| 575 | lov_ost_pool_remove(&pool->pool_obds, lov_idx); |
| 576 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 577 | CDEBUG(D_CONFIG, "%s removed from "LOV_POOLNAMEF"\n", ostname, |
| 578 | poolname); |
| 579 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 580 | out: |
| 581 | obd_putref(obd); |
| 582 | lov_pool_putref(pool); |
| 583 | return rc; |
| 584 | } |