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