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