Whamcloud - gitweb
93549226e0dace033b057774f64e7e65942cd356
[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, 2015, 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                 NULL,
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         CDEBUG(D_INFO, "%s: Adding target %s (idx "LPU64")\n", fld->lcf_name,
168                name, tar->ft_idx);
169
170         OBD_ALLOC_PTR(target);
171         if (target == NULL)
172                 RETURN(-ENOMEM);
173
174         spin_lock(&fld->lcf_lock);
175         list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
176                 if (tmp->ft_idx == tar->ft_idx) {
177                         spin_unlock(&fld->lcf_lock);
178                         OBD_FREE_PTR(target);
179                         CERROR("Target %s exists in FLD and known as %s:#"LPU64"\n",
180                                name, fld_target_name(tmp), tmp->ft_idx);
181                         RETURN(-EEXIST);
182                 }
183         }
184
185         target->ft_exp = tar->ft_exp;
186         if (target->ft_exp != NULL)
187                 class_export_get(target->ft_exp);
188         target->ft_srv = tar->ft_srv;
189         target->ft_idx = tar->ft_idx;
190
191         list_add_tail(&target->ft_chain, &fld->lcf_targets);
192
193         fld->lcf_count++;
194         spin_unlock(&fld->lcf_lock);
195
196         RETURN(0);
197 }
198 EXPORT_SYMBOL(fld_client_add_target);
199
200 /* Remove export from FLD */
201 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
202 {
203         struct lu_fld_target *target, *tmp;
204         ENTRY;
205
206         spin_lock(&fld->lcf_lock);
207         list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
208                 if (target->ft_idx == idx) {
209                         fld->lcf_count--;
210                         list_del(&target->ft_chain);
211                         spin_unlock(&fld->lcf_lock);
212
213                         if (target->ft_exp != NULL)
214                                 class_export_put(target->ft_exp);
215
216                         OBD_FREE_PTR(target);
217                         RETURN(0);
218                 }
219         }
220         spin_unlock(&fld->lcf_lock);
221         RETURN(-ENOENT);
222 }
223
224 #ifdef CONFIG_PROC_FS
225 static int fld_client_proc_init(struct lu_client_fld *fld)
226 {
227         int rc;
228         ENTRY;
229
230         fld->lcf_proc_dir = lprocfs_register(fld->lcf_name, fld_type_proc_dir,
231                                              NULL, NULL);
232         if (IS_ERR(fld->lcf_proc_dir)) {
233                 CERROR("%s: LProcFS failed in fld-init\n",
234                        fld->lcf_name);
235                 rc = PTR_ERR(fld->lcf_proc_dir);
236                 RETURN(rc);
237         }
238
239         rc = lprocfs_add_vars(fld->lcf_proc_dir, fld_client_proc_list, fld);
240         if (rc) {
241                 CERROR("%s: Can't init FLD proc, rc %d\n",
242                        fld->lcf_name, rc);
243                 GOTO(out_cleanup, rc);
244         }
245
246         RETURN(0);
247
248 out_cleanup:
249         fld_client_proc_fini(fld);
250         return rc;
251 }
252
253 void fld_client_proc_fini(struct lu_client_fld *fld)
254 {
255         ENTRY;
256         if (fld->lcf_proc_dir) {
257                 if (!IS_ERR(fld->lcf_proc_dir))
258                         lprocfs_remove(&fld->lcf_proc_dir);
259                 fld->lcf_proc_dir = NULL;
260         }
261         EXIT;
262 }
263 #else /* !CONFIG_PROC_FS */
264 static int fld_client_proc_init(struct lu_client_fld *fld)
265 {
266         return 0;
267 }
268
269 void fld_client_proc_fini(struct lu_client_fld *fld)
270 {
271         return;
272 }
273 #endif /* CONFIG_PROC_FS */
274
275 EXPORT_SYMBOL(fld_client_proc_fini);
276
277 static inline int hash_is_sane(int hash)
278 {
279         return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
280 }
281
282 int fld_client_init(struct lu_client_fld *fld,
283                     const char *prefix, int hash)
284 {
285         int cache_size, cache_threshold;
286         int rc;
287         ENTRY;
288
289         LASSERT(fld != NULL);
290
291         snprintf(fld->lcf_name, sizeof(fld->lcf_name),
292                  "cli-%s", prefix);
293
294         if (!hash_is_sane(hash)) {
295                 CERROR("%s: Wrong hash function %#x\n",
296                        fld->lcf_name, hash);
297                 RETURN(-EINVAL);
298         }
299
300         fld->lcf_count = 0;
301         spin_lock_init(&fld->lcf_lock);
302         fld->lcf_hash = &fld_hash[hash];
303         INIT_LIST_HEAD(&fld->lcf_targets);
304
305         cache_size = FLD_CLIENT_CACHE_SIZE /
306                 sizeof(struct fld_cache_entry);
307
308         cache_threshold = cache_size *
309                 FLD_CLIENT_CACHE_THRESHOLD / 100;
310
311         fld->lcf_cache = fld_cache_init(fld->lcf_name,
312                                         cache_size, cache_threshold);
313         if (IS_ERR(fld->lcf_cache)) {
314                 rc = PTR_ERR(fld->lcf_cache);
315                 fld->lcf_cache = NULL;
316                 GOTO(out, rc);
317         }
318
319         rc = fld_client_proc_init(fld);
320         if (rc)
321                 GOTO(out, rc);
322         EXIT;
323 out:
324         if (rc)
325                 fld_client_fini(fld);
326         else
327                 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
328                        fld->lcf_name, fld->lcf_hash->fh_name);
329         return rc;
330 }
331 EXPORT_SYMBOL(fld_client_init);
332
333 void fld_client_fini(struct lu_client_fld *fld)
334 {
335         struct lu_fld_target *target, *tmp;
336         ENTRY;
337
338         spin_lock(&fld->lcf_lock);
339         list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
340                 fld->lcf_count--;
341                 list_del(&target->ft_chain);
342                 if (target->ft_exp != NULL)
343                         class_export_put(target->ft_exp);
344                 OBD_FREE_PTR(target);
345         }
346         spin_unlock(&fld->lcf_lock);
347
348         if (fld->lcf_cache != NULL) {
349                 if (!IS_ERR(fld->lcf_cache))
350                         fld_cache_fini(fld->lcf_cache);
351                 fld->lcf_cache = NULL;
352         }
353
354         EXIT;
355 }
356 EXPORT_SYMBOL(fld_client_fini);
357
358 int fld_client_rpc(struct obd_export *exp,
359                    struct lu_seq_range *range, __u32 fld_op,
360                    struct ptlrpc_request **reqp)
361 {
362         struct ptlrpc_request *req = NULL;
363         struct lu_seq_range   *prange;
364         __u32                 *op;
365         int                    rc = 0;
366         struct obd_import     *imp;
367         ENTRY;
368
369         LASSERT(exp != NULL);
370
371 again:
372         imp = class_exp2cliimp(exp);
373         switch (fld_op) {
374         case FLD_QUERY:
375                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
376                                                 LUSTRE_MDS_VERSION, FLD_QUERY);
377                 if (req == NULL)
378                         RETURN(-ENOMEM);
379
380                 /* XXX: only needed when talking to old server(< 2.6), it should
381                  * be removed when < 2.6 server is not supported */
382                 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
383                 *op = FLD_LOOKUP;
384
385                 /* For MDS_MDS seq lookup, it will always use LWP connection,
386                  * but LWP will be evicted after restart, so cause the error.
387                  * so we will set no_delay for seq lookup request, once the
388                  * request fails because of the eviction. always retry here */
389                 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
390                         req->rq_allow_replay = 1;
391                         req->rq_no_delay = 1;
392                 }
393                 break;
394         case FLD_READ:
395                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
396                                                 LUSTRE_MDS_VERSION, FLD_READ);
397                 if (req == NULL)
398                         RETURN(-ENOMEM);
399
400                 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
401                                      RCL_SERVER, PAGE_CACHE_SIZE);
402                 break;
403         default:
404                 rc = -EINVAL;
405                 break;
406         }
407
408         if (rc != 0)
409                 RETURN(rc);
410
411         prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
412         *prange = *range;
413         ptlrpc_request_set_replen(req);
414         req->rq_request_portal = FLD_REQUEST_PORTAL;
415         req->rq_reply_portal = MDC_REPLY_PORTAL;
416         ptlrpc_at_set_req_timeout(req);
417
418         obd_get_request_slot(&exp->exp_obd->u.cli);
419         rc = ptlrpc_queue_wait(req);
420         obd_put_request_slot(&exp->exp_obd->u.cli);
421
422         if (rc == -ENOENT) {
423                 /* Don't loop forever on non-existing FID sequences. */
424                 GOTO(out_req, rc);
425         }
426
427         if (rc != 0) {
428                 if (imp->imp_state != LUSTRE_IMP_CLOSED && !imp->imp_deactive) {
429                         /* Since LWP is not replayable, so it will keep
430                          * trying unless umount happens, otherwise it would
431                          * cause unecessary failure of the application. */
432                         ptlrpc_req_finished(req);
433                         rc = 0;
434                         goto again;
435                 }
436                 GOTO(out_req, rc);
437         }
438
439         if (fld_op == FLD_QUERY) {
440                 prange = req_capsule_server_get(&req->rq_pill,
441                                                 &RMF_FLD_MDFLD);
442                 if (prange == NULL)
443                         GOTO(out_req, rc = -EFAULT);
444                 *range = *prange;
445         }
446
447         EXIT;
448 out_req:
449         if (rc != 0 || reqp == NULL) {
450                 ptlrpc_req_finished(req);
451                 req = NULL;
452         }
453
454         if (reqp != NULL)
455                 *reqp = req;
456
457         return rc;
458 }
459
460 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
461                       __u32 flags, const struct lu_env *env)
462 {
463         struct lu_seq_range res = { 0 };
464         struct lu_fld_target *target;
465         struct lu_fld_target *origin;
466         int rc;
467         ENTRY;
468
469         rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
470         if (rc == 0) {
471                 *mds = res.lsr_index;
472                 RETURN(0);
473         }
474
475         /* Can not find it in the cache */
476         target = fld_client_get_target(fld, seq);
477         LASSERT(target != NULL);
478         origin = target;
479 again:
480         CDEBUG(D_INFO, "%s: Lookup fld entry (seq: "LPX64") on "
481                "target %s (idx "LPU64")\n", fld->lcf_name, seq,
482                fld_target_name(target), target->ft_idx);
483
484         res.lsr_start = seq;
485         fld_range_set_type(&res, flags);
486
487 #ifdef HAVE_SERVER_SUPPORT
488         if (target->ft_srv != NULL) {
489                 LASSERT(env != NULL);
490                 rc = fld_server_lookup(env, target->ft_srv, seq, &res);
491         } else
492 #endif /* HAVE_SERVER_SUPPORT */
493         {
494                 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
495         }
496
497         if (rc == -ESHUTDOWN) {
498                 /* If fld lookup failed because the target has been shutdown,
499                  * then try next target in the list, until trying all targets
500                  * or fld lookup succeeds */
501                 spin_lock(&fld->lcf_lock);
502
503                 /* If the next entry in the list is the head of the list,
504                  * move to the next entry after the head and retrieve
505                  * the target. Else retreive the next target entry. */
506
507                 if (target->ft_chain.next == &fld->lcf_targets)
508                         target = list_entry(target->ft_chain.next->next,
509                                             struct lu_fld_target, ft_chain);
510                 else
511                         target = list_entry(target->ft_chain.next,
512                                                  struct lu_fld_target,
513                                                  ft_chain);
514                 spin_unlock(&fld->lcf_lock);
515                 if (target != origin)
516                         goto again;
517         }
518         if (rc == 0) {
519                 *mds = res.lsr_index;
520                 fld_cache_insert(fld->lcf_cache, &res);
521         }
522
523         RETURN(rc);
524 }
525 EXPORT_SYMBOL(fld_client_lookup);
526
527 void fld_client_flush(struct lu_client_fld *fld)
528 {
529         fld_cache_flush(fld->lcf_cache);
530 }
531
532
533 struct proc_dir_entry *fld_type_proc_dir;
534
535 static int __init fld_init(void)
536 {
537         fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
538                                              proc_lustre_root,
539                                              NULL, NULL);
540         if (IS_ERR(fld_type_proc_dir))
541                 return PTR_ERR(fld_type_proc_dir);
542
543 #ifdef HAVE_SERVER_SUPPORT
544         fld_server_mod_init();
545 #endif /* HAVE_SERVER_SUPPORT */
546
547         return 0;
548 }
549
550 static void __exit fld_exit(void)
551 {
552 #ifdef HAVE_SERVER_SUPPORT
553         fld_server_mod_exit();
554 #endif /* HAVE_SERVER_SUPPORT */
555
556         if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
557                 lprocfs_remove(&fld_type_proc_dir);
558                 fld_type_proc_dir = NULL;
559         }
560 }
561
562 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
563 MODULE_DESCRIPTION("Lustre FID Location Database");
564 MODULE_VERSION(LUSTRE_VERSION_STRING);
565 MODULE_LICENSE("GPL");
566
567 module_init(fld_init);
568 module_exit(fld_exit);