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