GNU Linux-libre 4.9.332-gnu1
[releases.git] / drivers / staging / lustre / 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, 2015, 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 "../../include/linux/libcfs/libcfs.h"
42 #include <linux/module.h>
43 #include <asm/div64.h>
44
45 #include "../include/obd.h"
46 #include "../include/obd_class.h"
47 #include "../include/lustre_ver.h"
48 #include "../include/obd_support.h"
49 #include "../include/lprocfs_status.h"
50
51 #include "../include/lustre_req_layout.h"
52 #include "../include/lustre_fld.h"
53 #include "../include/lustre_mdc.h"
54 #include "fld_internal.h"
55
56 static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
57 {
58         LASSERT(fld->lcf_count > 0);
59         return do_div(seq, fld->lcf_count);
60 }
61
62 static struct lu_fld_target *
63 fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
64 {
65         struct lu_fld_target *target;
66         int hash;
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                 /* It is possible the remote target(MDT) are not connected to
87                  * with client yet, so we will refer this to MDT0, which should
88                  * be connected during mount
89                  */
90                 hash = 0;
91                 goto again;
92         }
93
94         CERROR("%s: Can't find target by hash %d (seq %#llx). Targets (%d):\n",
95                fld->lcf_name, hash, seq, fld->lcf_count);
96
97         list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
98                 const char *srv_name = target->ft_srv ?
99                         target->ft_srv->lsf_name : "<null>";
100                 const char *exp_name = target->ft_exp ?
101                         (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
102                         "<null>";
103
104                 CERROR("  exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
105                        target->ft_exp, exp_name, target->ft_srv,
106                        srv_name, target->ft_idx);
107         }
108
109         /*
110          * If target is not found, there is logical error anyway, so here is
111          * LBUG() to catch this situation.
112          */
113         LBUG();
114         return NULL;
115 }
116
117 struct lu_fld_hash fld_hash[] = {
118         {
119                 .fh_name = "RRB",
120                 .fh_hash_func = fld_rrb_hash,
121                 .fh_scan_func = fld_rrb_scan
122         },
123         {
124                 NULL,
125         }
126 };
127
128 static struct lu_fld_target *
129 fld_client_get_target(struct lu_client_fld *fld, u64 seq)
130 {
131         struct lu_fld_target *target;
132
133         LASSERT(fld->lcf_hash);
134
135         spin_lock(&fld->lcf_lock);
136         target = fld->lcf_hash->fh_scan_func(fld, seq);
137         spin_unlock(&fld->lcf_lock);
138
139         if (target) {
140                 CDEBUG(D_INFO, "%s: Found target (idx %llu) by seq %#llx\n",
141                        fld->lcf_name, 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
157         LASSERT(tar);
158         name = fld_target_name(tar);
159         LASSERT(name);
160         LASSERT(tar->ft_srv || tar->ft_exp);
161
162         if (fld->lcf_flags != LUSTRE_FLD_INIT) {
163                 CERROR("%s: Attempt to add target %s (idx %llu) on fly - skip it\n",
164                        fld->lcf_name, name, tar->ft_idx);
165                 return 0;
166         }
167         CDEBUG(D_INFO, "%s: Adding target %s (idx %llu)\n",
168                fld->lcf_name, name, tar->ft_idx);
169
170         target = kzalloc(sizeof(*target), GFP_NOFS);
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                         kfree(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         spin_lock(&fld->lcf_lock);
206         list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
207                 if (target->ft_idx == idx) {
208                         fld->lcf_count--;
209                         list_del(&target->ft_chain);
210                         spin_unlock(&fld->lcf_lock);
211
212                         if (target->ft_exp)
213                                 class_export_put(target->ft_exp);
214
215                         kfree(target);
216                         return 0;
217                 }
218         }
219         spin_unlock(&fld->lcf_lock);
220         return -ENOENT;
221 }
222
223 static struct dentry *fld_debugfs_dir;
224
225 static int fld_client_debugfs_init(struct lu_client_fld *fld)
226 {
227         int rc;
228
229         fld->lcf_debugfs_entry = ldebugfs_register(fld->lcf_name,
230                                                    fld_debugfs_dir,
231                                                    NULL, NULL);
232
233         if (IS_ERR_OR_NULL(fld->lcf_debugfs_entry)) {
234                 CERROR("%s: LdebugFS failed in fld-init\n", fld->lcf_name);
235                 rc = fld->lcf_debugfs_entry ? PTR_ERR(fld->lcf_debugfs_entry)
236                                             : -ENOMEM;
237                 fld->lcf_debugfs_entry = NULL;
238                 return rc;
239         }
240
241         rc = ldebugfs_add_vars(fld->lcf_debugfs_entry,
242                                fld_client_debugfs_list, fld);
243         if (rc) {
244                 CERROR("%s: Can't init FLD debufs, rc %d\n", fld->lcf_name, rc);
245                 goto out_cleanup;
246         }
247
248         return 0;
249
250 out_cleanup:
251         fld_client_debugfs_fini(fld);
252         return rc;
253 }
254
255 void fld_client_debugfs_fini(struct lu_client_fld *fld)
256 {
257         if (!IS_ERR_OR_NULL(fld->lcf_debugfs_entry))
258                 ldebugfs_remove(&fld->lcf_debugfs_entry);
259 }
260 EXPORT_SYMBOL(fld_client_debugfs_fini);
261
262 static inline int hash_is_sane(int hash)
263 {
264         return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
265 }
266
267 int fld_client_init(struct lu_client_fld *fld,
268                     const char *prefix, int hash)
269 {
270         int cache_size, cache_threshold;
271         int rc;
272
273         snprintf(fld->lcf_name, sizeof(fld->lcf_name),
274                  "cli-%s", prefix);
275
276         if (!hash_is_sane(hash)) {
277                 CERROR("%s: Wrong hash function %#x\n",
278                        fld->lcf_name, hash);
279                 return -EINVAL;
280         }
281
282         fld->lcf_count = 0;
283         spin_lock_init(&fld->lcf_lock);
284         fld->lcf_hash = &fld_hash[hash];
285         fld->lcf_flags = LUSTRE_FLD_INIT;
286         INIT_LIST_HEAD(&fld->lcf_targets);
287
288         cache_size = FLD_CLIENT_CACHE_SIZE /
289                 sizeof(struct fld_cache_entry);
290
291         cache_threshold = cache_size *
292                 FLD_CLIENT_CACHE_THRESHOLD / 100;
293
294         fld->lcf_cache = fld_cache_init(fld->lcf_name,
295                                         cache_size, cache_threshold);
296         if (IS_ERR(fld->lcf_cache)) {
297                 rc = PTR_ERR(fld->lcf_cache);
298                 fld->lcf_cache = NULL;
299                 goto out;
300         }
301
302         rc = fld_client_debugfs_init(fld);
303         if (rc)
304                 goto out;
305 out:
306         if (rc)
307                 fld_client_fini(fld);
308         else
309                 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
310                        fld->lcf_name, fld->lcf_hash->fh_name);
311         return rc;
312 }
313 EXPORT_SYMBOL(fld_client_init);
314
315 void fld_client_fini(struct lu_client_fld *fld)
316 {
317         struct lu_fld_target *target, *tmp;
318
319         spin_lock(&fld->lcf_lock);
320         list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
321                 fld->lcf_count--;
322                 list_del(&target->ft_chain);
323                 if (target->ft_exp)
324                         class_export_put(target->ft_exp);
325                 kfree(target);
326         }
327         spin_unlock(&fld->lcf_lock);
328
329         if (fld->lcf_cache) {
330                 if (!IS_ERR(fld->lcf_cache))
331                         fld_cache_fini(fld->lcf_cache);
332                 fld->lcf_cache = NULL;
333         }
334 }
335 EXPORT_SYMBOL(fld_client_fini);
336
337 int fld_client_rpc(struct obd_export *exp,
338                    struct lu_seq_range *range, __u32 fld_op,
339                    struct ptlrpc_request **reqp)
340 {
341         struct ptlrpc_request *req = NULL;
342         struct lu_seq_range   *prange;
343         __u32            *op;
344         int                 rc = 0;
345         struct obd_import     *imp;
346
347         LASSERT(exp);
348
349         imp = class_exp2cliimp(exp);
350         switch (fld_op) {
351         case FLD_QUERY:
352                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
353                                                 LUSTRE_MDS_VERSION, FLD_QUERY);
354                 if (!req)
355                         return -ENOMEM;
356
357                 /*
358                  * XXX: only needed when talking to old server(< 2.6), it should
359                  * be removed when < 2.6 server is not supported
360                  */
361                 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
362                 *op = FLD_LOOKUP;
363
364                 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS)
365                         req->rq_allow_replay = 1;
366                 break;
367         case FLD_READ:
368                 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
369                                                 LUSTRE_MDS_VERSION, FLD_READ);
370                 if (!req)
371                         return -ENOMEM;
372
373                 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
374                                      RCL_SERVER, PAGE_SIZE);
375                 break;
376         default:
377                 rc = -EINVAL;
378                 break;
379         }
380         if (rc)
381                 return rc;
382
383         prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
384         *prange = *range;
385         ptlrpc_request_set_replen(req);
386         req->rq_request_portal = FLD_REQUEST_PORTAL;
387         req->rq_reply_portal = MDC_REPLY_PORTAL;
388         ptlrpc_at_set_req_timeout(req);
389
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         if (rc)
394                 goto out_req;
395
396         if (fld_op == FLD_QUERY) {
397                 prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
398                 if (!prange) {
399                         rc = -EFAULT;
400                         goto out_req;
401                 }
402                 *range = *prange;
403         }
404
405 out_req:
406         if (rc || !reqp) {
407                 ptlrpc_req_finished(req);
408                 req = NULL;
409         }
410
411         if (reqp)
412                 *reqp = req;
413
414         return rc;
415 }
416
417 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
418                       __u32 flags, const struct lu_env *env)
419 {
420         struct lu_seq_range res = { 0 };
421         struct lu_fld_target *target;
422         int rc;
423
424         fld->lcf_flags |= LUSTRE_FLD_RUN;
425
426         rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
427         if (rc == 0) {
428                 *mds = res.lsr_index;
429                 return 0;
430         }
431
432         /* Can not find it in the cache */
433         target = fld_client_get_target(fld, seq);
434         LASSERT(target);
435
436         CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on target %s (idx %llu)\n",
437                fld->lcf_name, seq, fld_target_name(target), target->ft_idx);
438
439         res.lsr_start = seq;
440         fld_range_set_type(&res, flags);
441         rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
442
443         if (rc == 0) {
444                 *mds = res.lsr_index;
445
446                 fld_cache_insert(fld->lcf_cache, &res);
447         }
448         return rc;
449 }
450 EXPORT_SYMBOL(fld_client_lookup);
451
452 void fld_client_flush(struct lu_client_fld *fld)
453 {
454         fld_cache_flush(fld->lcf_cache);
455 }
456
457 static int __init fld_init(void)
458 {
459         fld_debugfs_dir = ldebugfs_register(LUSTRE_FLD_NAME,
460                                             debugfs_lustre_root,
461                                             NULL, NULL);
462         return PTR_ERR_OR_ZERO(fld_debugfs_dir);
463 }
464
465 static void __exit fld_exit(void)
466 {
467         if (!IS_ERR_OR_NULL(fld_debugfs_dir))
468                 ldebugfs_remove(&fld_debugfs_dir);
469 }
470
471 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
472 MODULE_DESCRIPTION("Lustre FID Location Database");
473 MODULE_VERSION(LUSTRE_VERSION_STRING);
474 MODULE_LICENSE("GPL");
475
476 module_init(fld_init)
477 module_exit(fld_exit)