Whamcloud - gitweb
LU-13799 llite: Implement lower/upper aio
[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  *
31  * lustre/fld/fld_request.c
32  *
33  * FLD (Fids Location Database)
34  *
35  * Author: Yury Umanets <umka@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_FLD
39
40 #include <libcfs/libcfs.h>
41 #include <linux/module.h>
42 #include <linux/math64.h>
43
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
48 #include <lustre_req_layout.h>
49 #include <lustre_fld.h>
50 #include <lustre_mdc.h>
51 #include "fld_internal.h"
52
53 static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
54 {
55         LASSERT(fld->lcf_count > 0);
56         return do_div(seq, fld->lcf_count);
57 }
58
59 static struct lu_fld_target *
60 fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
61 {
62         struct lu_fld_target *target;
63         int hash;
64
65         ENTRY;
66
67         /*
68          * Because almost all of special sequence located in MDT0,
69          * it should go to index 0 directly, instead of calculating
70          * hash again, and also if other MDTs is not being connected,
71          * the fld lookup requests(for seq on MDT0) should not be
72          * blocked because of other MDTs
73          */
74         if (fid_seq_is_norm(seq))
75                 hash = fld_rrb_hash(fld, seq);
76         else
77                 hash = 0;
78
79 again:
80         list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
81                 if (target->ft_idx == hash)
82                         RETURN(target);
83         }
84
85         if (hash != 0) {
86                 /*
87                  * It is possible the remote target(MDT) are not connected to
88                  * with client yet, so we will refer this to MDT0, which should
89                  * be connected during mount
90                  */
91                 hash = 0;
92                 goto again;
93         }
94
95         CERROR("%s: Can't find target by hash %d (seq %#llx). Targets (%d):\n",
96                fld->lcf_name, hash, seq, fld->lcf_count);
97
98         list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
99                 const char *srv_name = target->ft_srv != NULL  ?
100                         target->ft_srv->lsf_name : "<null>";
101                 const char *exp_name = target->ft_exp != NULL ?
102                         (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
103                         "<null>";
104
105                 CERROR("  exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
106                        target->ft_exp, exp_name, target->ft_srv,
107                        srv_name, target->ft_idx);
108         }
109
110         /*
111          * If target is not found, there is logical error anyway, so here is
112          * LBUG() to catch this situation.
113          */
114         LBUG();
115         RETURN(NULL);
116 }
117
118 struct lu_fld_hash fld_hash[] = {
119         {
120                 .fh_name = "RRB",
121                 .fh_hash_func = fld_rrb_hash,
122                 .fh_scan_func = fld_rrb_scan
123         },
124         {
125                 NULL,
126         }
127 };
128
129 static struct lu_fld_target *
130 fld_client_get_target(struct lu_client_fld *fld, u64 seq)
131 {
132         struct lu_fld_target *target;
133
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) {
143                 CDEBUG(D_INFO, "%s: Found target (idx %llu) by seq %#llx\n",
144                        fld->lcf_name, target->ft_idx, seq);
145         }
146
147         RETURN(target);
148 }
149
150 /*
151  * Add export to FLD. This is usually done by CMM and LMV as they are main users
152  * of FLD module.
153  */
154 int fld_client_add_target(struct lu_client_fld *fld,
155                           struct lu_fld_target *tar)
156 {
157         const char *name;
158         struct lu_fld_target *target, *tmp;
159
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 %llu)\n", fld->lcf_name,
168                name, tar->ft_idx);
169
170         OBD_ALLOC_PTR(target);
171         if (!target)
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:#%llu\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)
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
205         ENTRY;
206
207         spin_lock(&fld->lcf_lock);
208         list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
209                 if (target->ft_idx == idx) {
210                         fld->lcf_count--;
211                         list_del(&target->ft_chain);
212                         spin_unlock(&fld->lcf_lock);
213
214                         if (target->ft_exp)
215                                 class_export_put(target->ft_exp);
216
217                         OBD_FREE_PTR(target);
218                         RETURN(0);
219                 }
220         }
221         spin_unlock(&fld->lcf_lock);
222         RETURN(-ENOENT);
223 }
224
225 struct dentry *fld_debugfs_dir;
226
227 static void fld_client_debugfs_init(struct lu_client_fld *fld)
228 {
229         ENTRY;
230         fld->lcf_debugfs_entry = debugfs_create_dir(fld->lcf_name,
231                                                    fld_debugfs_dir);
232         ldebugfs_add_vars(fld->lcf_debugfs_entry,
233                           fld_client_debugfs_list,
234                           fld);
235 }
236
237 void fld_client_debugfs_fini(struct lu_client_fld *fld)
238 {
239         debugfs_remove_recursive(fld->lcf_debugfs_entry);
240 }
241 EXPORT_SYMBOL(fld_client_debugfs_fini);
242
243 static inline int hash_is_sane(int hash)
244 {
245         return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
246 }
247
248 int fld_client_init(struct lu_client_fld *fld,
249                     const char *prefix, int hash)
250 {
251         int cache_size, cache_threshold;
252         int rc = 0;
253
254         ENTRY;
255         snprintf(fld->lcf_name, sizeof(fld->lcf_name),
256                  "cli-%s", prefix);
257
258         if (!hash_is_sane(hash)) {
259                 CERROR("%s: Wrong hash function %#x\n",
260                        fld->lcf_name, hash);
261                 RETURN(-EINVAL);
262         }
263
264         fld->lcf_count = 0;
265         spin_lock_init(&fld->lcf_lock);
266         fld->lcf_hash = &fld_hash[hash];
267         INIT_LIST_HEAD(&fld->lcf_targets);
268
269         cache_size = FLD_CLIENT_CACHE_SIZE /
270                 sizeof(struct fld_cache_entry);
271
272         cache_threshold = cache_size *
273                 FLD_CLIENT_CACHE_THRESHOLD / 100;
274
275         fld->lcf_cache = fld_cache_init(fld->lcf_name,
276                                         cache_size, cache_threshold);
277         if (IS_ERR(fld->lcf_cache)) {
278                 rc = PTR_ERR(fld->lcf_cache);
279                 fld->lcf_cache = NULL;
280                 GOTO(out, rc);
281         }
282
283         fld_client_debugfs_init(fld);
284         EXIT;
285 out:
286         if (rc)
287                 fld_client_fini(fld);
288         else
289                 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
290                        fld->lcf_name, fld->lcf_hash->fh_name);
291         return rc;
292 }
293 EXPORT_SYMBOL(fld_client_init);
294
295 void fld_client_fini(struct lu_client_fld *fld)
296 {
297         struct lu_fld_target *target, *tmp;
298
299         ENTRY;
300
301         spin_lock(&fld->lcf_lock);
302         list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
303                 fld->lcf_count--;
304                 list_del(&target->ft_chain);
305                 if (target->ft_exp)
306                         class_export_put(target->ft_exp);
307                 OBD_FREE_PTR(target);
308         }
309         spin_unlock(&fld->lcf_lock);
310
311         if (fld->lcf_cache) {
312                 if (!IS_ERR(fld->lcf_cache))
313                         fld_cache_fini(fld->lcf_cache);
314                 fld->lcf_cache = NULL;
315         }
316
317         EXIT;
318 }
319 EXPORT_SYMBOL(fld_client_fini);
320
321 int fld_client_rpc(struct obd_export *exp,
322                    struct lu_seq_range *range, u32 fld_op,
323                    struct ptlrpc_request **reqp)
324 {
325         struct ptlrpc_request *req = NULL;
326         struct lu_seq_range *prange;
327         u32 *op;
328         int rc = 0;
329         struct obd_import *imp;
330
331         ENTRY;
332
333         LASSERT(exp != NULL);
334
335         imp = class_exp2cliimp(exp);
336         switch (fld_op) {
337         case FLD_QUERY:
338                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
339                                                 LUSTRE_MDS_VERSION, FLD_QUERY);
340                 if (!req)
341                         RETURN(-ENOMEM);
342
343                 /*
344                  * XXX: only needed when talking to old server(< 2.6), it should
345                  * be removed when < 2.6 server is not supported
346                  */
347                 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
348                 *op = FLD_LOOKUP;
349
350                 /*
351                  * For MDS_MDS seq lookup, it will always use LWP connection,
352                  * but LWP will be evicted after restart, so cause the error.
353                  * so we will set no_delay for seq lookup request, once the
354                  * request fails because of the eviction. always retry here
355                  */
356                 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
357                         req->rq_allow_replay = 1;
358                         req->rq_no_delay = 1;
359                 }
360                 break;
361         case FLD_READ:
362                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
363                                                 LUSTRE_MDS_VERSION, FLD_READ);
364                 if (!req)
365                         RETURN(-ENOMEM);
366
367                 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
368                                      RCL_SERVER, PAGE_SIZE);
369                 break;
370         default:
371                 rc = -EINVAL;
372                 break;
373         }
374
375         if (rc != 0)
376                 RETURN(rc);
377
378         prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
379         *prange = *range;
380         ptlrpc_request_set_replen(req);
381         req->rq_request_portal = FLD_REQUEST_PORTAL;
382         req->rq_reply_portal = MDC_REPLY_PORTAL;
383         ptlrpc_at_set_req_timeout(req);
384
385         if (OBD_FAIL_CHECK(OBD_FAIL_FLD_QUERY_REQ && req->rq_no_delay)) {
386                 /* the same error returned by ptlrpc_import_delay_req */
387                 rc = -EAGAIN;
388                 req->rq_status = rc;
389         } else {
390                 obd_get_request_slot(&exp->exp_obd->u.cli);
391                 rc = ptlrpc_queue_wait(req);
392                 obd_put_request_slot(&exp->exp_obd->u.cli);
393         }
394
395         if (rc == -ENOENT) {
396                 /* Don't loop forever on non-existing FID sequences. */
397                 GOTO(out_req, rc);
398         }
399
400         if (rc != 0) {
401                 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
402                     !imp->imp_deactive &&
403                     imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS &&
404                     OCD_HAS_FLAG(&imp->imp_connect_data, LIGHTWEIGHT) &&
405                     rc != -ENOTSUPP) {
406                         /*
407                          * Since LWP is not replayable, so notify the caller
408                          * to retry if needed after a while.
409                          */
410                         rc = -EAGAIN;
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)
419                         GOTO(out_req, rc = -EFAULT);
420                 *range = *prange;
421         }
422
423         EXIT;
424 out_req:
425         if (rc != 0 || !reqp) {
426                 ptlrpc_req_finished(req);
427                 req = NULL;
428         }
429
430         if (reqp)
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
444         ENTRY;
445
446         rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
447         if (rc == 0) {
448                 *mds = res.lsr_index;
449                 RETURN(0);
450         }
451
452         /* Can not find it in the cache */
453         target = fld_client_get_target(fld, seq);
454         LASSERT(target != NULL);
455         origin = target;
456 again:
457         CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on target %s (idx %llu)\n",
458                fld->lcf_name, seq, 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) {
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                 /*
475                  * If fld lookup failed because the target has been shutdown,
476                  * then try next target in the list, until trying all targets
477                  * or fld lookup succeeds
478                  */
479                 spin_lock(&fld->lcf_lock);
480                 /*
481                  * If the next entry in the list is the head of the list,
482                  * move to the next entry after the head and retrieve
483                  * the target. Else retreive the next target entry.
484                  */
485                 if (target->ft_chain.next == &fld->lcf_targets)
486                         target = list_entry(target->ft_chain.next->next,
487                                             struct lu_fld_target, ft_chain);
488                 else
489                         target = list_entry(target->ft_chain.next,
490                                                  struct lu_fld_target,
491                                                  ft_chain);
492                 spin_unlock(&fld->lcf_lock);
493                 if (target != origin)
494                         goto again;
495         }
496         if (rc == 0) {
497                 *mds = res.lsr_index;
498                 fld_cache_insert(fld->lcf_cache, &res);
499         }
500
501         RETURN(rc);
502 }
503 EXPORT_SYMBOL(fld_client_lookup);
504
505 void fld_client_flush(struct lu_client_fld *fld)
506 {
507         fld_cache_flush(fld->lcf_cache);
508 }
509
510 static int __init fld_init(void)
511 {
512 #ifdef HAVE_SERVER_SUPPORT
513         int rc;
514
515         rc = fld_server_mod_init();
516         if (rc)
517                 return rc;
518 #endif /* HAVE_SERVER_SUPPORT */
519
520         fld_debugfs_dir = debugfs_create_dir(LUSTRE_FLD_NAME,
521                                              debugfs_lustre_root);
522         return PTR_ERR_OR_ZERO(fld_debugfs_dir);
523 }
524
525 static void __exit fld_exit(void)
526 {
527 #ifdef HAVE_SERVER_SUPPORT
528         fld_server_mod_exit();
529 #endif /* HAVE_SERVER_SUPPORT */
530
531         debugfs_remove_recursive(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);