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