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.sun.com/software/products/lustre/docs/GPLv2.pdf
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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2011, 2013, 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/fld/fld_request.c
37 *
38 * FLD (Fids Location Database)
39 *
40 * Author: Yury Umanets <umka@clusterfs.com>
41 */
42
43 #define DEBUG_SUBSYSTEM S_FLD
44
45 #include "../../include/linux/libcfs/libcfs.h"
46 #include <linux/module.h>
47 #include <asm/div64.h>
48
49 #include "../include/obd.h"
50 #include "../include/obd_class.h"
51 #include "../include/lustre_ver.h"
52 #include "../include/obd_support.h"
53 #include "../include/lprocfs_status.h"
54
55 #include "../include/lustre_req_layout.h"
56 #include "../include/lustre_fld.h"
57 #include "../include/lustre_mdc.h"
58 #include "fld_internal.h"
59
60 /* TODO: these 3 functions are copies of flow-control code from mdc_lib.c
61 * It should be common thing. The same about mdc RPC lock */
fld_req_avail(struct client_obd * cli,struct mdc_cache_waiter * mcw)62 static int fld_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
63 {
64 int rc;
65
66 client_obd_list_lock(&cli->cl_loi_list_lock);
67 rc = list_empty(&mcw->mcw_entry);
68 client_obd_list_unlock(&cli->cl_loi_list_lock);
69 return rc;
70 };
71
fld_enter_request(struct client_obd * cli)72 static void fld_enter_request(struct client_obd *cli)
73 {
74 struct mdc_cache_waiter mcw;
75 struct l_wait_info lwi = { 0 };
76
77 client_obd_list_lock(&cli->cl_loi_list_lock);
78 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
79 list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
80 init_waitqueue_head(&mcw.mcw_waitq);
81 client_obd_list_unlock(&cli->cl_loi_list_lock);
82 l_wait_event(mcw.mcw_waitq, fld_req_avail(cli, &mcw), &lwi);
83 } else {
84 cli->cl_r_in_flight++;
85 client_obd_list_unlock(&cli->cl_loi_list_lock);
86 }
87 }
88
fld_exit_request(struct client_obd * cli)89 static void fld_exit_request(struct client_obd *cli)
90 {
91 struct list_head *l, *tmp;
92 struct mdc_cache_waiter *mcw;
93
94 client_obd_list_lock(&cli->cl_loi_list_lock);
95 cli->cl_r_in_flight--;
96 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
97
98 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
99 /* No free request slots anymore */
100 break;
101 }
102
103 mcw = list_entry(l, struct mdc_cache_waiter, mcw_entry);
104 list_del_init(&mcw->mcw_entry);
105 cli->cl_r_in_flight++;
106 wake_up(&mcw->mcw_waitq);
107 }
108 client_obd_list_unlock(&cli->cl_loi_list_lock);
109 }
110
fld_rrb_hash(struct lu_client_fld * fld,u64 seq)111 static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
112 {
113 LASSERT(fld->lcf_count > 0);
114 return do_div(seq, fld->lcf_count);
115 }
116
117 static struct lu_fld_target *
fld_rrb_scan(struct lu_client_fld * fld,u64 seq)118 fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
119 {
120 struct lu_fld_target *target;
121 int hash;
122
123 /* Because almost all of special sequence located in MDT0,
124 * it should go to index 0 directly, instead of calculating
125 * hash again, and also if other MDTs is not being connected,
126 * the fld lookup requests(for seq on MDT0) should not be
127 * blocked because of other MDTs */
128 if (fid_seq_is_norm(seq))
129 hash = fld_rrb_hash(fld, seq);
130 else
131 hash = 0;
132
133 again:
134 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
135 if (target->ft_idx == hash)
136 return target;
137 }
138
139 if (hash != 0) {
140 /* It is possible the remote target(MDT) are not connected to
141 * with client yet, so we will refer this to MDT0, which should
142 * be connected during mount */
143 hash = 0;
144 goto again;
145 }
146
147 CERROR("%s: Can't find target by hash %d (seq %#llx). Targets (%d):\n",
148 fld->lcf_name, hash, seq, fld->lcf_count);
149
150 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
151 const char *srv_name = target->ft_srv != NULL ?
152 target->ft_srv->lsf_name : "<null>";
153 const char *exp_name = target->ft_exp != NULL ?
154 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
155 "<null>";
156
157 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
158 target->ft_exp, exp_name, target->ft_srv,
159 srv_name, target->ft_idx);
160 }
161
162 /*
163 * If target is not found, there is logical error anyway, so here is
164 * LBUG() to catch this situation.
165 */
166 LBUG();
167 return NULL;
168 }
169
170 struct lu_fld_hash fld_hash[] = {
171 {
172 .fh_name = "RRB",
173 .fh_hash_func = fld_rrb_hash,
174 .fh_scan_func = fld_rrb_scan
175 },
176 {
177 NULL,
178 }
179 };
180
181 static struct lu_fld_target *
fld_client_get_target(struct lu_client_fld * fld,u64 seq)182 fld_client_get_target(struct lu_client_fld *fld, u64 seq)
183 {
184 struct lu_fld_target *target;
185
186 LASSERT(fld->lcf_hash != NULL);
187
188 spin_lock(&fld->lcf_lock);
189 target = fld->lcf_hash->fh_scan_func(fld, seq);
190 spin_unlock(&fld->lcf_lock);
191
192 if (target != NULL) {
193 CDEBUG(D_INFO, "%s: Found target (idx %llu) by seq %#llx\n",
194 fld->lcf_name, target->ft_idx, seq);
195 }
196
197 return target;
198 }
199
200 /*
201 * Add export to FLD. This is usually done by CMM and LMV as they are main users
202 * of FLD module.
203 */
fld_client_add_target(struct lu_client_fld * fld,struct lu_fld_target * tar)204 int fld_client_add_target(struct lu_client_fld *fld,
205 struct lu_fld_target *tar)
206 {
207 const char *name;
208 struct lu_fld_target *target, *tmp;
209
210 LASSERT(tar != NULL);
211 name = fld_target_name(tar);
212 LASSERT(name != NULL);
213 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
214
215 if (fld->lcf_flags != LUSTRE_FLD_INIT) {
216 CERROR("%s: Attempt to add target %s (idx %llu) on fly - skip it\n",
217 fld->lcf_name, name, tar->ft_idx);
218 return 0;
219 }
220 CDEBUG(D_INFO, "%s: Adding target %s (idx %llu)\n",
221 fld->lcf_name, name, tar->ft_idx);
222
223 target = kzalloc(sizeof(*target), GFP_NOFS);
224 if (!target)
225 return -ENOMEM;
226
227 spin_lock(&fld->lcf_lock);
228 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
229 if (tmp->ft_idx == tar->ft_idx) {
230 spin_unlock(&fld->lcf_lock);
231 kfree(target);
232 CERROR("Target %s exists in FLD and known as %s:#%llu\n",
233 name, fld_target_name(tmp), tmp->ft_idx);
234 return -EEXIST;
235 }
236 }
237
238 target->ft_exp = tar->ft_exp;
239 if (target->ft_exp != NULL)
240 class_export_get(target->ft_exp);
241 target->ft_srv = tar->ft_srv;
242 target->ft_idx = tar->ft_idx;
243
244 list_add_tail(&target->ft_chain,
245 &fld->lcf_targets);
246
247 fld->lcf_count++;
248 spin_unlock(&fld->lcf_lock);
249
250 return 0;
251 }
252 EXPORT_SYMBOL(fld_client_add_target);
253
254 /* Remove export from FLD */
fld_client_del_target(struct lu_client_fld * fld,__u64 idx)255 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
256 {
257 struct lu_fld_target *target, *tmp;
258
259 spin_lock(&fld->lcf_lock);
260 list_for_each_entry_safe(target, tmp,
261 &fld->lcf_targets, ft_chain) {
262 if (target->ft_idx == idx) {
263 fld->lcf_count--;
264 list_del(&target->ft_chain);
265 spin_unlock(&fld->lcf_lock);
266
267 if (target->ft_exp != NULL)
268 class_export_put(target->ft_exp);
269
270 kfree(target);
271 return 0;
272 }
273 }
274 spin_unlock(&fld->lcf_lock);
275 return -ENOENT;
276 }
277 EXPORT_SYMBOL(fld_client_del_target);
278
279 static struct dentry *fld_debugfs_dir;
280
fld_client_debugfs_init(struct lu_client_fld * fld)281 static int fld_client_debugfs_init(struct lu_client_fld *fld)
282 {
283 int rc;
284
285 fld->lcf_debugfs_entry = ldebugfs_register(fld->lcf_name,
286 fld_debugfs_dir,
287 NULL, NULL);
288
289 if (IS_ERR_OR_NULL(fld->lcf_debugfs_entry)) {
290 CERROR("%s: LdebugFS failed in fld-init\n", fld->lcf_name);
291 rc = fld->lcf_debugfs_entry ? PTR_ERR(fld->lcf_debugfs_entry)
292 : -ENOMEM;
293 fld->lcf_debugfs_entry = NULL;
294 return rc;
295 }
296
297 rc = ldebugfs_add_vars(fld->lcf_debugfs_entry,
298 fld_client_debugfs_list, fld);
299 if (rc) {
300 CERROR("%s: Can't init FLD debufs, rc %d\n", fld->lcf_name, rc);
301 goto out_cleanup;
302 }
303
304 return 0;
305
306 out_cleanup:
307 fld_client_debugfs_fini(fld);
308 return rc;
309 }
310
fld_client_debugfs_fini(struct lu_client_fld * fld)311 void fld_client_debugfs_fini(struct lu_client_fld *fld)
312 {
313 if (!IS_ERR_OR_NULL(fld->lcf_debugfs_entry))
314 ldebugfs_remove(&fld->lcf_debugfs_entry);
315 }
316 EXPORT_SYMBOL(fld_client_debugfs_fini);
317
hash_is_sane(int hash)318 static inline int hash_is_sane(int hash)
319 {
320 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
321 }
322
fld_client_init(struct lu_client_fld * fld,const char * prefix,int hash)323 int fld_client_init(struct lu_client_fld *fld,
324 const char *prefix, int hash)
325 {
326 int cache_size, cache_threshold;
327 int rc;
328
329 LASSERT(fld != NULL);
330
331 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
332 "cli-%s", prefix);
333
334 if (!hash_is_sane(hash)) {
335 CERROR("%s: Wrong hash function %#x\n",
336 fld->lcf_name, hash);
337 return -EINVAL;
338 }
339
340 fld->lcf_count = 0;
341 spin_lock_init(&fld->lcf_lock);
342 fld->lcf_hash = &fld_hash[hash];
343 fld->lcf_flags = LUSTRE_FLD_INIT;
344 INIT_LIST_HEAD(&fld->lcf_targets);
345
346 cache_size = FLD_CLIENT_CACHE_SIZE /
347 sizeof(struct fld_cache_entry);
348
349 cache_threshold = cache_size *
350 FLD_CLIENT_CACHE_THRESHOLD / 100;
351
352 fld->lcf_cache = fld_cache_init(fld->lcf_name,
353 cache_size, cache_threshold);
354 if (IS_ERR(fld->lcf_cache)) {
355 rc = PTR_ERR(fld->lcf_cache);
356 fld->lcf_cache = NULL;
357 goto out;
358 }
359
360 rc = fld_client_debugfs_init(fld);
361 if (rc)
362 goto out;
363 out:
364 if (rc)
365 fld_client_fini(fld);
366 else
367 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
368 fld->lcf_name, fld->lcf_hash->fh_name);
369 return rc;
370 }
371 EXPORT_SYMBOL(fld_client_init);
372
fld_client_fini(struct lu_client_fld * fld)373 void fld_client_fini(struct lu_client_fld *fld)
374 {
375 struct lu_fld_target *target, *tmp;
376
377 spin_lock(&fld->lcf_lock);
378 list_for_each_entry_safe(target, tmp,
379 &fld->lcf_targets, ft_chain) {
380 fld->lcf_count--;
381 list_del(&target->ft_chain);
382 if (target->ft_exp != NULL)
383 class_export_put(target->ft_exp);
384 kfree(target);
385 }
386 spin_unlock(&fld->lcf_lock);
387
388 if (fld->lcf_cache != NULL) {
389 if (!IS_ERR(fld->lcf_cache))
390 fld_cache_fini(fld->lcf_cache);
391 fld->lcf_cache = NULL;
392 }
393 }
394 EXPORT_SYMBOL(fld_client_fini);
395
fld_client_rpc(struct obd_export * exp,struct lu_seq_range * range,__u32 fld_op)396 int fld_client_rpc(struct obd_export *exp,
397 struct lu_seq_range *range, __u32 fld_op)
398 {
399 struct ptlrpc_request *req;
400 struct lu_seq_range *prange;
401 __u32 *op;
402 int rc;
403 struct obd_import *imp;
404
405 LASSERT(exp != NULL);
406
407 imp = class_exp2cliimp(exp);
408 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY, LUSTRE_MDS_VERSION,
409 FLD_QUERY);
410 if (req == NULL)
411 return -ENOMEM;
412
413 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
414 *op = fld_op;
415
416 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
417 *prange = *range;
418
419 ptlrpc_request_set_replen(req);
420 req->rq_request_portal = FLD_REQUEST_PORTAL;
421 req->rq_reply_portal = MDC_REPLY_PORTAL;
422 ptlrpc_at_set_req_timeout(req);
423
424 if (fld_op == FLD_LOOKUP &&
425 imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS)
426 req->rq_allow_replay = 1;
427
428 if (fld_op != FLD_LOOKUP)
429 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
430 fld_enter_request(&exp->exp_obd->u.cli);
431 rc = ptlrpc_queue_wait(req);
432 fld_exit_request(&exp->exp_obd->u.cli);
433 if (fld_op != FLD_LOOKUP)
434 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
435 if (rc)
436 goto out_req;
437
438 prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
439 if (prange == NULL) {
440 rc = -EFAULT;
441 goto out_req;
442 }
443 *range = *prange;
444 out_req:
445 ptlrpc_req_finished(req);
446 return rc;
447 }
448
fld_client_lookup(struct lu_client_fld * fld,u64 seq,u32 * mds,__u32 flags,const struct lu_env * env)449 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
450 __u32 flags, const struct lu_env *env)
451 {
452 struct lu_seq_range res = { 0 };
453 struct lu_fld_target *target;
454 int rc;
455
456 fld->lcf_flags |= LUSTRE_FLD_RUN;
457
458 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
459 if (rc == 0) {
460 *mds = res.lsr_index;
461 return 0;
462 }
463
464 /* Can not find it in the cache */
465 target = fld_client_get_target(fld, seq);
466 LASSERT(target != NULL);
467
468 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on target %s (idx %llu)\n",
469 fld->lcf_name, seq, fld_target_name(target), target->ft_idx);
470
471 res.lsr_start = seq;
472 fld_range_set_type(&res, flags);
473 rc = fld_client_rpc(target->ft_exp, &res, FLD_LOOKUP);
474
475 if (rc == 0) {
476 *mds = res.lsr_index;
477
478 fld_cache_insert(fld->lcf_cache, &res);
479 }
480 return rc;
481 }
482 EXPORT_SYMBOL(fld_client_lookup);
483
fld_client_flush(struct lu_client_fld * fld)484 void fld_client_flush(struct lu_client_fld *fld)
485 {
486 fld_cache_flush(fld->lcf_cache);
487 }
488 EXPORT_SYMBOL(fld_client_flush);
489
fld_mod_init(void)490 static int __init fld_mod_init(void)
491 {
492 fld_debugfs_dir = ldebugfs_register(LUSTRE_FLD_NAME,
493 debugfs_lustre_root,
494 NULL, NULL);
495 return PTR_ERR_OR_ZERO(fld_debugfs_dir);
496 }
497
fld_mod_exit(void)498 static void __exit fld_mod_exit(void)
499 {
500 if (!IS_ERR_OR_NULL(fld_debugfs_dir))
501 ldebugfs_remove(&fld_debugfs_dir);
502 }
503
504 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
505 MODULE_DESCRIPTION("Lustre FLD");
506 MODULE_LICENSE("GPL");
507
508 module_init(fld_mod_init)
509 module_exit(fld_mod_exit)
510