Whamcloud - gitweb
3c2d48f25078a808fcfee1dd9ed48f7a44d23386
[fs/lustre-release.git] / lustre / fld / fld_request.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.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 #ifdef __KERNEL__
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 # include <asm/div64.h>
49 #else /* __KERNEL__ */
50 # include <liblustre.h>
51 # include <libcfs/list.h>
52 #endif
53
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <obd_support.h>
57 #include <lprocfs_status.h>
58 #include <lustre_req_layout.h>
59 #include <lustre_fld.h>
60 #include <lustre_mdc.h>
61 #include "fld_internal.h"
62
63 /* TODO: these 3 functions are copies of flow-control code from mdc_lib.c
64  * It should be common thing. The same about mdc RPC lock */
65 static int fld_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
66 {
67         int rc;
68         ENTRY;
69         client_obd_list_lock(&cli->cl_loi_list_lock);
70         rc = cfs_list_empty(&mcw->mcw_entry);
71         client_obd_list_unlock(&cli->cl_loi_list_lock);
72         RETURN(rc);
73 };
74
75 static void fld_enter_request(struct client_obd *cli)
76 {
77         struct mdc_cache_waiter mcw;
78         struct l_wait_info lwi = { 0 };
79
80         client_obd_list_lock(&cli->cl_loi_list_lock);
81         if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
82                 cfs_list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
83                 init_waitqueue_head(&mcw.mcw_waitq);
84                 client_obd_list_unlock(&cli->cl_loi_list_lock);
85                 l_wait_event(mcw.mcw_waitq, fld_req_avail(cli, &mcw), &lwi);
86         } else {
87                 cli->cl_r_in_flight++;
88                 client_obd_list_unlock(&cli->cl_loi_list_lock);
89         }
90 }
91
92 static void fld_exit_request(struct client_obd *cli)
93 {
94         cfs_list_t *l, *tmp;
95         struct mdc_cache_waiter *mcw;
96
97         client_obd_list_lock(&cli->cl_loi_list_lock);
98         cli->cl_r_in_flight--;
99         cfs_list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
100
101                 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
102                         /* No free request slots anymore */
103                         break;
104                 }
105
106                 mcw = cfs_list_entry(l, struct mdc_cache_waiter, mcw_entry);
107                 cfs_list_del_init(&mcw->mcw_entry);
108                 cli->cl_r_in_flight++;
109                 wake_up(&mcw->mcw_waitq);
110         }
111         client_obd_list_unlock(&cli->cl_loi_list_lock);
112 }
113
114 static int fld_rrb_hash(struct lu_client_fld *fld,
115                         seqno_t seq)
116 {
117         LASSERT(fld->lcf_count > 0);
118         return do_div(seq, fld->lcf_count);
119 }
120
121 static struct lu_fld_target *
122 fld_rrb_scan(struct lu_client_fld *fld, seqno_t seq)
123 {
124         struct lu_fld_target *target;
125         int hash;
126         ENTRY;
127
128         /* Because almost all of special sequence located in MDT0,
129          * it should go to index 0 directly, instead of calculating
130          * hash again, and also if other MDTs is not being connected,
131          * the fld lookup requests(for seq on MDT0) should not be
132          * blocked because of other MDTs */
133         if (fid_seq_is_norm(seq))
134                 hash = fld_rrb_hash(fld, seq);
135         else
136                 hash = 0;
137
138         cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
139                 if (target->ft_idx == hash)
140                         RETURN(target);
141         }
142
143         CERROR("%s: Can't find target by hash %d (seq "LPX64"). "
144                "Targets (%d):\n", fld->lcf_name, hash, seq,
145                fld->lcf_count);
146
147         cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
148                 const char *srv_name = target->ft_srv != NULL  ?
149                         target->ft_srv->lsf_name : "<null>";
150                 const char *exp_name = target->ft_exp != NULL ?
151                         (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
152                         "<null>";
153
154                 CERROR("  exp: 0x%p (%s), srv: 0x%p (%s), idx: "LPU64"\n",
155                        target->ft_exp, exp_name, target->ft_srv,
156                        srv_name, target->ft_idx);
157         }
158
159         /*
160          * If target is not found, there is logical error anyway, so here is
161          * LBUG() to catch this situation.
162          */
163         LBUG();
164         RETURN(NULL);
165 }
166
167 struct lu_fld_hash fld_hash[] = {
168         {
169                 .fh_name = "RRB",
170                 .fh_hash_func = fld_rrb_hash,
171                 .fh_scan_func = fld_rrb_scan
172         },
173         {
174                 0,
175         }
176 };
177
178 static struct lu_fld_target *
179 fld_client_get_target(struct lu_client_fld *fld, seqno_t seq)
180 {
181         struct lu_fld_target *target;
182         ENTRY;
183
184         LASSERT(fld->lcf_hash != NULL);
185
186         spin_lock(&fld->lcf_lock);
187         target = fld->lcf_hash->fh_scan_func(fld, seq);
188         spin_unlock(&fld->lcf_lock);
189
190         if (target != NULL) {
191                 CDEBUG(D_INFO, "%s: Found target (idx "LPU64
192                        ") by seq "LPX64"\n", fld->lcf_name,
193                        target->ft_idx, seq);
194         }
195
196         RETURN(target);
197 }
198
199 /*
200  * Add export to FLD. This is usually done by CMM and LMV as they are main users
201  * of FLD module.
202  */
203 int fld_client_add_target(struct lu_client_fld *fld,
204                           struct lu_fld_target *tar)
205 {
206         const char *name;
207         struct lu_fld_target *target, *tmp;
208         ENTRY;
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 "LPU64") "
217                        "on fly - skip it\n", fld->lcf_name, name,
218                        tar->ft_idx);
219                 RETURN(0);
220         } else {
221                 CDEBUG(D_INFO, "%s: Adding target %s (idx "
222                        LPU64")\n", fld->lcf_name, name, tar->ft_idx);
223         }
224
225         OBD_ALLOC_PTR(target);
226         if (target == NULL)
227                 RETURN(-ENOMEM);
228
229         spin_lock(&fld->lcf_lock);
230         cfs_list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
231                 if (tmp->ft_idx == tar->ft_idx) {
232                         spin_unlock(&fld->lcf_lock);
233                         OBD_FREE_PTR(target);
234                         CERROR("Target %s exists in FLD and known as %s:#"LPU64"\n",
235                                name, fld_target_name(tmp), tmp->ft_idx);
236                         RETURN(-EEXIST);
237                 }
238         }
239
240         target->ft_exp = tar->ft_exp;
241         if (target->ft_exp != NULL)
242                 class_export_get(target->ft_exp);
243         target->ft_srv = tar->ft_srv;
244         target->ft_idx = tar->ft_idx;
245
246         cfs_list_add_tail(&target->ft_chain,
247                           &fld->lcf_targets);
248
249         fld->lcf_count++;
250         spin_unlock(&fld->lcf_lock);
251
252         RETURN(0);
253 }
254 EXPORT_SYMBOL(fld_client_add_target);
255
256 /* Remove export from FLD */
257 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
258 {
259         struct lu_fld_target *target, *tmp;
260         ENTRY;
261
262         spin_lock(&fld->lcf_lock);
263         cfs_list_for_each_entry_safe(target, tmp,
264                                      &fld->lcf_targets, ft_chain) {
265                 if (target->ft_idx == idx) {
266                         fld->lcf_count--;
267                         cfs_list_del(&target->ft_chain);
268                         spin_unlock(&fld->lcf_lock);
269
270                         if (target->ft_exp != NULL)
271                                 class_export_put(target->ft_exp);
272
273                         OBD_FREE_PTR(target);
274                         RETURN(0);
275                 }
276         }
277         spin_unlock(&fld->lcf_lock);
278         RETURN(-ENOENT);
279 }
280 EXPORT_SYMBOL(fld_client_del_target);
281
282 #ifdef LPROCFS
283 static int fld_client_proc_init(struct lu_client_fld *fld)
284 {
285         int rc;
286         ENTRY;
287
288         fld->lcf_proc_dir = lprocfs_seq_register(fld->lcf_name,
289                                                  fld_type_proc_dir,
290                                                  NULL, NULL);
291         if (IS_ERR(fld->lcf_proc_dir)) {
292                 CERROR("%s: LProcFS failed in fld-init\n",
293                        fld->lcf_name);
294                 rc = PTR_ERR(fld->lcf_proc_dir);
295                 RETURN(rc);
296         }
297
298         rc = lprocfs_seq_add_vars(fld->lcf_proc_dir,
299                                   fld_client_proc_list, fld);
300         if (rc) {
301                 CERROR("%s: Can't init FLD proc, rc %d\n",
302                        fld->lcf_name, rc);
303                 GOTO(out_cleanup, rc);
304         }
305
306         RETURN(0);
307
308 out_cleanup:
309         fld_client_proc_fini(fld);
310         return rc;
311 }
312
313 void fld_client_proc_fini(struct lu_client_fld *fld)
314 {
315         ENTRY;
316         if (fld->lcf_proc_dir) {
317                 if (!IS_ERR(fld->lcf_proc_dir))
318                         lprocfs_remove(&fld->lcf_proc_dir);
319                 fld->lcf_proc_dir = NULL;
320         }
321         EXIT;
322 }
323 #else
324 static int fld_client_proc_init(struct lu_client_fld *fld)
325 {
326         return 0;
327 }
328
329 void fld_client_proc_fini(struct lu_client_fld *fld)
330 {
331         return;
332 }
333 #endif
334
335 EXPORT_SYMBOL(fld_client_proc_fini);
336
337 static inline int hash_is_sane(int hash)
338 {
339         return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
340 }
341
342 int fld_client_init(struct lu_client_fld *fld,
343                     const char *prefix, int hash)
344 {
345         int cache_size, cache_threshold;
346         int rc;
347         ENTRY;
348
349         LASSERT(fld != NULL);
350
351         snprintf(fld->lcf_name, sizeof(fld->lcf_name),
352                  "cli-%s", prefix);
353
354         if (!hash_is_sane(hash)) {
355                 CERROR("%s: Wrong hash function %#x\n",
356                        fld->lcf_name, hash);
357                 RETURN(-EINVAL);
358         }
359
360         fld->lcf_count = 0;
361         spin_lock_init(&fld->lcf_lock);
362         fld->lcf_hash = &fld_hash[hash];
363         fld->lcf_flags = LUSTRE_FLD_INIT;
364         CFS_INIT_LIST_HEAD(&fld->lcf_targets);
365
366         cache_size = FLD_CLIENT_CACHE_SIZE /
367                 sizeof(struct fld_cache_entry);
368
369         cache_threshold = cache_size *
370                 FLD_CLIENT_CACHE_THRESHOLD / 100;
371
372         fld->lcf_cache = fld_cache_init(fld->lcf_name,
373                                         cache_size, cache_threshold);
374         if (IS_ERR(fld->lcf_cache)) {
375                 rc = PTR_ERR(fld->lcf_cache);
376                 fld->lcf_cache = NULL;
377                 GOTO(out, rc);
378         }
379
380         rc = fld_client_proc_init(fld);
381         if (rc)
382                 GOTO(out, rc);
383         EXIT;
384 out:
385         if (rc)
386                 fld_client_fini(fld);
387         else
388                 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
389                        fld->lcf_name, fld->lcf_hash->fh_name);
390         return rc;
391 }
392 EXPORT_SYMBOL(fld_client_init);
393
394 void fld_client_fini(struct lu_client_fld *fld)
395 {
396         struct lu_fld_target *target, *tmp;
397         ENTRY;
398
399         spin_lock(&fld->lcf_lock);
400         cfs_list_for_each_entry_safe(target, tmp,
401                                      &fld->lcf_targets, ft_chain) {
402                 fld->lcf_count--;
403                 cfs_list_del(&target->ft_chain);
404                 if (target->ft_exp != NULL)
405                         class_export_put(target->ft_exp);
406                 OBD_FREE_PTR(target);
407         }
408         spin_unlock(&fld->lcf_lock);
409
410         if (fld->lcf_cache != NULL) {
411                 if (!IS_ERR(fld->lcf_cache))
412                         fld_cache_fini(fld->lcf_cache);
413                 fld->lcf_cache = NULL;
414         }
415
416         EXIT;
417 }
418 EXPORT_SYMBOL(fld_client_fini);
419
420 int fld_client_rpc(struct obd_export *exp,
421                    struct lu_seq_range *range, __u32 fld_op,
422                    struct ptlrpc_request **reqp)
423 {
424         struct ptlrpc_request *req = NULL;
425         struct lu_seq_range   *prange;
426         __u32                 *op;
427         int                    rc = 0;
428         struct obd_import     *imp;
429         ENTRY;
430
431         LASSERT(exp != NULL);
432
433 again:
434         imp = class_exp2cliimp(exp);
435         switch (fld_op) {
436         case FLD_QUERY:
437                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
438                                                 LUSTRE_MDS_VERSION, FLD_QUERY);
439                 if (req == NULL)
440                         RETURN(-ENOMEM);
441
442                 /* XXX: only needed when talking to old server(< 2.6), it should
443                  * be removed when < 2.6 server is not supported */
444                 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
445                 *op = FLD_LOOKUP;
446
447                 /* For MDS_MDS seq lookup, it will always use LWP connection,
448                  * but LWP will be evicted after restart, so cause the error.
449                  * so we will set no_delay for seq lookup request, once the
450                  * request fails because of the eviction. always retry here */
451                 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
452                         req->rq_allow_replay = 1;
453                         req->rq_no_delay = 1;
454                 }
455                 break;
456         case FLD_READ:
457                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
458                                                 LUSTRE_MDS_VERSION, FLD_READ);
459                 if (req == NULL)
460                         RETURN(-ENOMEM);
461
462                 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
463                                      RCL_SERVER, PAGE_CACHE_SIZE);
464                 break;
465         default:
466                 rc = -EINVAL;
467                 break;
468         }
469
470         if (rc != 0)
471                 RETURN(rc);
472
473         prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
474         *prange = *range;
475         ptlrpc_request_set_replen(req);
476         req->rq_request_portal = FLD_REQUEST_PORTAL;
477         req->rq_reply_portal = MDC_REPLY_PORTAL;
478         ptlrpc_at_set_req_timeout(req);
479
480         fld_enter_request(&exp->exp_obd->u.cli);
481         rc = ptlrpc_queue_wait(req);
482         fld_exit_request(&exp->exp_obd->u.cli);
483         if (rc != 0) {
484                 if (rc == -EWOULDBLOCK) {
485                         /* For no_delay req(see above), EWOULDBLOCK means the
486                          * connection is being evicted, but this seq lookup
487                          * should not return error, since it would cause
488                          * unecessary failure of the application, instead
489                          * it should retry here */
490                         ptlrpc_req_finished(req);
491                         goto again;
492                 }
493                 GOTO(out_req, rc);
494         }
495
496         if (fld_op == FLD_QUERY) {
497                 prange = req_capsule_server_get(&req->rq_pill,
498                                                 &RMF_FLD_MDFLD);
499                 if (prange == NULL)
500                         GOTO(out_req, rc = -EFAULT);
501                 *range = *prange;
502         }
503
504         EXIT;
505 out_req:
506         if (rc != 0 || reqp == NULL) {
507                 ptlrpc_req_finished(req);
508                 req = NULL;
509         }
510
511         if (reqp != NULL)
512                 *reqp = req;
513
514         return rc;
515 }
516
517 int fld_client_lookup(struct lu_client_fld *fld, seqno_t seq, mdsno_t *mds,
518                       __u32 flags, const struct lu_env *env)
519 {
520         struct lu_seq_range res = { 0 };
521         struct lu_fld_target *target;
522         int rc;
523         ENTRY;
524
525         fld->lcf_flags |= LUSTRE_FLD_RUN;
526
527         rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
528         if (rc == 0) {
529                 *mds = res.lsr_index;
530                 RETURN(0);
531         }
532
533         /* Can not find it in the cache */
534         target = fld_client_get_target(fld, seq);
535         LASSERT(target != NULL);
536
537         CDEBUG(D_INFO, "%s: Lookup fld entry (seq: "LPX64") on "
538                "target %s (idx "LPU64")\n", fld->lcf_name, seq,
539                fld_target_name(target), target->ft_idx);
540
541         res.lsr_start = seq;
542         fld_range_set_type(&res, flags);
543
544 #if defined(__KERNEL__) && defined(HAVE_SERVER_SUPPORT)
545         if (target->ft_srv != NULL) {
546                 LASSERT(env != NULL);
547                 rc = fld_server_lookup(env, target->ft_srv, seq, &res);
548         } else
549 #endif
550         {
551                 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
552         }
553
554         if (rc == 0) {
555                 *mds = res.lsr_index;
556                 fld_cache_insert(fld->lcf_cache, &res);
557         }
558
559         RETURN(rc);
560 }
561 EXPORT_SYMBOL(fld_client_lookup);
562
563 void fld_client_flush(struct lu_client_fld *fld)
564 {
565         fld_cache_flush(fld->lcf_cache);
566 }
567 EXPORT_SYMBOL(fld_client_flush);
568
569 #ifdef __KERNEL__
570
571 struct proc_dir_entry *fld_type_proc_dir;
572
573 static int __init fld_mod_init(void)
574 {
575         fld_type_proc_dir = lprocfs_seq_register(LUSTRE_FLD_NAME,
576                                                  proc_lustre_root,
577                                                  NULL, NULL);
578         if (IS_ERR(fld_type_proc_dir))
579                 return PTR_ERR(fld_type_proc_dir);
580
581 #ifdef HAVE_SERVER_SUPPORT
582         fld_server_mod_init();
583 #endif
584
585         return 0;
586 }
587
588 static void __exit fld_mod_exit(void)
589 {
590 #ifdef HAVE_SERVER_SUPPORT
591         fld_server_mod_exit();
592 #endif
593
594         if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
595                 lprocfs_remove(&fld_type_proc_dir);
596                 fld_type_proc_dir = NULL;
597         }
598 }
599
600 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
601 MODULE_DESCRIPTION("Lustre FLD");
602 MODULE_LICENSE("GPL");
603
604 cfs_module(mdd, LUSTRE_VERSION_STRING, fld_mod_init, fld_mod_exit);
605 #endif /* __KERNEL__ */