blob: e59d626a15481c14a4304badf0bf86830c2538c7 [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * 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 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050026 * Copyright (c) 2011, 2015, Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080027 */
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
Greg Kroah-Hartman9fdaf8c2014-07-11 20:51:16 -070041#include "../../include/linux/libcfs/libcfs.h"
Greg Kroah-Hartman0e9ad0e2014-07-11 21:51:13 -070042#include <linux/module.h>
43#include <asm/div64.h>
Peng Taod7e09d02013-05-02 16:46:55 +080044
Greg Kroah-Hartman0e9ad0e2014-07-11 21:51:13 -070045#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"
Peng Taod7e09d02013-05-02 16:46:55 +080050
Greg Kroah-Hartman0e9ad0e2014-07-11 21:51:13 -070051#include "../include/lustre_req_layout.h"
52#include "../include/lustre_fld.h"
53#include "../include/lustre_mdc.h"
Peng Taod7e09d02013-05-02 16:46:55 +080054#include "fld_internal.h"
55
56/* TODO: these 3 functions are copies of flow-control code from mdc_lib.c
Oleg Drokin52581b82016-02-24 22:00:26 -050057 * It should be common thing. The same about mdc RPC lock
58 */
Peng Taod7e09d02013-05-02 16:46:55 +080059static int fld_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
60{
61 int rc;
Greg Kroah-Hartman29aaf492013-08-02 18:14:51 +080062
John L. Hammond7d53d8f2016-03-30 19:48:36 -040063 spin_lock(&cli->cl_loi_list_lock);
Peng Taod7e09d02013-05-02 16:46:55 +080064 rc = list_empty(&mcw->mcw_entry);
John L. Hammond7d53d8f2016-03-30 19:48:36 -040065 spin_unlock(&cli->cl_loi_list_lock);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080066 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +080067};
68
69static void fld_enter_request(struct client_obd *cli)
70{
71 struct mdc_cache_waiter mcw;
72 struct l_wait_info lwi = { 0 };
73
John L. Hammond7d53d8f2016-03-30 19:48:36 -040074 spin_lock(&cli->cl_loi_list_lock);
Peng Taod7e09d02013-05-02 16:46:55 +080075 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
76 list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
77 init_waitqueue_head(&mcw.mcw_waitq);
John L. Hammond7d53d8f2016-03-30 19:48:36 -040078 spin_unlock(&cli->cl_loi_list_lock);
Peng Taod7e09d02013-05-02 16:46:55 +080079 l_wait_event(mcw.mcw_waitq, fld_req_avail(cli, &mcw), &lwi);
80 } else {
81 cli->cl_r_in_flight++;
John L. Hammond7d53d8f2016-03-30 19:48:36 -040082 spin_unlock(&cli->cl_loi_list_lock);
Peng Taod7e09d02013-05-02 16:46:55 +080083 }
84}
85
86static void fld_exit_request(struct client_obd *cli)
87{
88 struct list_head *l, *tmp;
89 struct mdc_cache_waiter *mcw;
90
John L. Hammond7d53d8f2016-03-30 19:48:36 -040091 spin_lock(&cli->cl_loi_list_lock);
Peng Taod7e09d02013-05-02 16:46:55 +080092 cli->cl_r_in_flight--;
93 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
Peng Taod7e09d02013-05-02 16:46:55 +080094 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
95 /* No free request slots anymore */
96 break;
97 }
98
99 mcw = list_entry(l, struct mdc_cache_waiter, mcw_entry);
100 list_del_init(&mcw->mcw_entry);
101 cli->cl_r_in_flight++;
102 wake_up(&mcw->mcw_waitq);
103 }
John L. Hammond7d53d8f2016-03-30 19:48:36 -0400104 spin_unlock(&cli->cl_loi_list_lock);
Peng Taod7e09d02013-05-02 16:46:55 +0800105}
106
Oleg Drokin114acca2014-08-15 12:55:55 -0400107static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
Peng Taod7e09d02013-05-02 16:46:55 +0800108{
109 LASSERT(fld->lcf_count > 0);
110 return do_div(seq, fld->lcf_count);
111}
112
113static struct lu_fld_target *
Oleg Drokin114acca2014-08-15 12:55:55 -0400114fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
Peng Taod7e09d02013-05-02 16:46:55 +0800115{
116 struct lu_fld_target *target;
117 int hash;
Peng Taod7e09d02013-05-02 16:46:55 +0800118
119 /* Because almost all of special sequence located in MDT0,
120 * it should go to index 0 directly, instead of calculating
121 * hash again, and also if other MDTs is not being connected,
122 * the fld lookup requests(for seq on MDT0) should not be
Oleg Drokin52581b82016-02-24 22:00:26 -0500123 * blocked because of other MDTs
124 */
Peng Taod7e09d02013-05-02 16:46:55 +0800125 if (fid_seq_is_norm(seq))
126 hash = fld_rrb_hash(fld, seq);
127 else
128 hash = 0;
129
wang dib7fb2222015-02-01 21:52:14 -0500130again:
Peng Taod7e09d02013-05-02 16:46:55 +0800131 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
132 if (target->ft_idx == hash)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800133 return target;
Peng Taod7e09d02013-05-02 16:46:55 +0800134 }
135
wang dib7fb2222015-02-01 21:52:14 -0500136 if (hash != 0) {
137 /* It is possible the remote target(MDT) are not connected to
138 * with client yet, so we will refer this to MDT0, which should
Oleg Drokin52581b82016-02-24 22:00:26 -0500139 * be connected during mount
140 */
wang dib7fb2222015-02-01 21:52:14 -0500141 hash = 0;
142 goto again;
143 }
144
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700145 CERROR("%s: Can't find target by hash %d (seq %#llx). Targets (%d):\n",
Oleg Drokincf677592016-02-26 01:49:51 -0500146 fld->lcf_name, hash, seq, fld->lcf_count);
Peng Taod7e09d02013-05-02 16:46:55 +0800147
148 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500149 const char *srv_name = target->ft_srv ?
Peng Taod7e09d02013-05-02 16:46:55 +0800150 target->ft_srv->lsf_name : "<null>";
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500151 const char *exp_name = target->ft_exp ?
Peng Taod7e09d02013-05-02 16:46:55 +0800152 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
153 "<null>";
154
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700155 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800156 target->ft_exp, exp_name, target->ft_srv,
157 srv_name, target->ft_idx);
158 }
159
160 /*
161 * If target is not found, there is logical error anyway, so here is
162 * LBUG() to catch this situation.
163 */
164 LBUG();
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800165 return NULL;
Peng Taod7e09d02013-05-02 16:46:55 +0800166}
167
168struct lu_fld_hash fld_hash[] = {
169 {
170 .fh_name = "RRB",
171 .fh_hash_func = fld_rrb_hash,
172 .fh_scan_func = fld_rrb_scan
173 },
174 {
Radek Dostalea7893b2014-07-27 23:22:57 +0200175 NULL,
Peng Taod7e09d02013-05-02 16:46:55 +0800176 }
177};
178
179static struct lu_fld_target *
Oleg Drokin114acca2014-08-15 12:55:55 -0400180fld_client_get_target(struct lu_client_fld *fld, u64 seq)
Peng Taod7e09d02013-05-02 16:46:55 +0800181{
182 struct lu_fld_target *target;
Peng Taod7e09d02013-05-02 16:46:55 +0800183
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500184 LASSERT(fld->lcf_hash);
Peng Taod7e09d02013-05-02 16:46:55 +0800185
186 spin_lock(&fld->lcf_lock);
187 target = fld->lcf_hash->fh_scan_func(fld, seq);
188 spin_unlock(&fld->lcf_lock);
189
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500190 if (target) {
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700191 CDEBUG(D_INFO, "%s: Found target (idx %llu) by seq %#llx\n",
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700192 fld->lcf_name, target->ft_idx, seq);
Peng Taod7e09d02013-05-02 16:46:55 +0800193 }
194
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800195 return target;
Peng Taod7e09d02013-05-02 16:46:55 +0800196}
197
198/*
199 * Add export to FLD. This is usually done by CMM and LMV as they are main users
200 * of FLD module.
201 */
202int fld_client_add_target(struct lu_client_fld *fld,
203 struct lu_fld_target *tar)
204{
205 const char *name;
206 struct lu_fld_target *target, *tmp;
Peng Taod7e09d02013-05-02 16:46:55 +0800207
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500208 LASSERT(tar);
Peng Taod7e09d02013-05-02 16:46:55 +0800209 name = fld_target_name(tar);
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500210 LASSERT(name);
211 LASSERT(tar->ft_srv || tar->ft_exp);
Peng Taod7e09d02013-05-02 16:46:55 +0800212
213 if (fld->lcf_flags != LUSTRE_FLD_INIT) {
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700214 CERROR("%s: Attempt to add target %s (idx %llu) on fly - skip it\n",
Oleg Drokincf677592016-02-26 01:49:51 -0500215 fld->lcf_name, name, tar->ft_idx);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800216 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800217 }
Alberto Pires de Oliveira Netocb6ec7f2015-03-01 22:44:04 -0300218 CDEBUG(D_INFO, "%s: Adding target %s (idx %llu)\n",
Oleg Drokincf677592016-02-26 01:49:51 -0500219 fld->lcf_name, name, tar->ft_idx);
Peng Taod7e09d02013-05-02 16:46:55 +0800220
Julia Lawall2e651012015-05-01 17:51:21 +0200221 target = kzalloc(sizeof(*target), GFP_NOFS);
Julia Lawall812f2052015-06-20 18:59:00 +0200222 if (!target)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800223 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800224
225 spin_lock(&fld->lcf_lock);
226 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
227 if (tmp->ft_idx == tar->ft_idx) {
228 spin_unlock(&fld->lcf_lock);
Julia Lawall2e651012015-05-01 17:51:21 +0200229 kfree(target);
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -0700230 CERROR("Target %s exists in FLD and known as %s:#%llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800231 name, fld_target_name(tmp), tmp->ft_idx);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800232 return -EEXIST;
Peng Taod7e09d02013-05-02 16:46:55 +0800233 }
234 }
235
236 target->ft_exp = tar->ft_exp;
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500237 if (target->ft_exp)
Peng Taod7e09d02013-05-02 16:46:55 +0800238 class_export_get(target->ft_exp);
239 target->ft_srv = tar->ft_srv;
240 target->ft_idx = tar->ft_idx;
241
Oleg Drokincf677592016-02-26 01:49:51 -0500242 list_add_tail(&target->ft_chain, &fld->lcf_targets);
Peng Taod7e09d02013-05-02 16:46:55 +0800243
244 fld->lcf_count++;
245 spin_unlock(&fld->lcf_lock);
246
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800247 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800248}
249EXPORT_SYMBOL(fld_client_add_target);
250
251/* Remove export from FLD */
252int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
253{
254 struct lu_fld_target *target, *tmp;
Peng Taod7e09d02013-05-02 16:46:55 +0800255
256 spin_lock(&fld->lcf_lock);
Oleg Drokincf677592016-02-26 01:49:51 -0500257 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
Peng Taod7e09d02013-05-02 16:46:55 +0800258 if (target->ft_idx == idx) {
259 fld->lcf_count--;
260 list_del(&target->ft_chain);
261 spin_unlock(&fld->lcf_lock);
262
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500263 if (target->ft_exp)
Peng Taod7e09d02013-05-02 16:46:55 +0800264 class_export_put(target->ft_exp);
265
Julia Lawall2e651012015-05-01 17:51:21 +0200266 kfree(target);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800267 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800268 }
269 }
270 spin_unlock(&fld->lcf_lock);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800271 return -ENOENT;
Peng Taod7e09d02013-05-02 16:46:55 +0800272}
273EXPORT_SYMBOL(fld_client_del_target);
274
Dmitry Eremin82765042015-05-21 15:32:26 -0400275static struct dentry *fld_debugfs_dir;
Liu Xuezhaoe62e5d92013-07-23 00:06:54 +0800276
Dmitry Eremin82765042015-05-21 15:32:26 -0400277static int fld_client_debugfs_init(struct lu_client_fld *fld)
Peng Taod7e09d02013-05-02 16:46:55 +0800278{
279 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800280
Dmitry Eremin82765042015-05-21 15:32:26 -0400281 fld->lcf_debugfs_entry = ldebugfs_register(fld->lcf_name,
282 fld_debugfs_dir,
283 NULL, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +0800284
Dmitry Eremin82765042015-05-21 15:32:26 -0400285 if (IS_ERR_OR_NULL(fld->lcf_debugfs_entry)) {
286 CERROR("%s: LdebugFS failed in fld-init\n", fld->lcf_name);
287 rc = fld->lcf_debugfs_entry ? PTR_ERR(fld->lcf_debugfs_entry)
288 : -ENOMEM;
289 fld->lcf_debugfs_entry = NULL;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800290 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800291 }
292
Dmitry Eremin82765042015-05-21 15:32:26 -0400293 rc = ldebugfs_add_vars(fld->lcf_debugfs_entry,
294 fld_client_debugfs_list, fld);
Peng Taod7e09d02013-05-02 16:46:55 +0800295 if (rc) {
Dmitry Eremin82765042015-05-21 15:32:26 -0400296 CERROR("%s: Can't init FLD debufs, rc %d\n", fld->lcf_name, rc);
Julia Lawall89180ca2014-08-30 16:41:23 +0200297 goto out_cleanup;
Peng Taod7e09d02013-05-02 16:46:55 +0800298 }
299
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800300 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800301
302out_cleanup:
Dmitry Eremin82765042015-05-21 15:32:26 -0400303 fld_client_debugfs_fini(fld);
Peng Taod7e09d02013-05-02 16:46:55 +0800304 return rc;
305}
306
Dmitry Eremin82765042015-05-21 15:32:26 -0400307void fld_client_debugfs_fini(struct lu_client_fld *fld)
Peng Taod7e09d02013-05-02 16:46:55 +0800308{
Dmitry Eremin82765042015-05-21 15:32:26 -0400309 if (!IS_ERR_OR_NULL(fld->lcf_debugfs_entry))
310 ldebugfs_remove(&fld->lcf_debugfs_entry);
Peng Taod7e09d02013-05-02 16:46:55 +0800311}
Dmitry Eremin82765042015-05-21 15:32:26 -0400312EXPORT_SYMBOL(fld_client_debugfs_fini);
Peng Taod7e09d02013-05-02 16:46:55 +0800313
314static inline int hash_is_sane(int hash)
315{
316 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
317}
318
319int fld_client_init(struct lu_client_fld *fld,
320 const char *prefix, int hash)
321{
322 int cache_size, cache_threshold;
323 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800324
Peng Taod7e09d02013-05-02 16:46:55 +0800325 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
326 "cli-%s", prefix);
327
328 if (!hash_is_sane(hash)) {
329 CERROR("%s: Wrong hash function %#x\n",
330 fld->lcf_name, hash);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800331 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +0800332 }
333
334 fld->lcf_count = 0;
335 spin_lock_init(&fld->lcf_lock);
336 fld->lcf_hash = &fld_hash[hash];
337 fld->lcf_flags = LUSTRE_FLD_INIT;
338 INIT_LIST_HEAD(&fld->lcf_targets);
339
340 cache_size = FLD_CLIENT_CACHE_SIZE /
341 sizeof(struct fld_cache_entry);
342
343 cache_threshold = cache_size *
344 FLD_CLIENT_CACHE_THRESHOLD / 100;
345
346 fld->lcf_cache = fld_cache_init(fld->lcf_name,
347 cache_size, cache_threshold);
348 if (IS_ERR(fld->lcf_cache)) {
349 rc = PTR_ERR(fld->lcf_cache);
350 fld->lcf_cache = NULL;
Julia Lawall89180ca2014-08-30 16:41:23 +0200351 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800352 }
353
Dmitry Eremin82765042015-05-21 15:32:26 -0400354 rc = fld_client_debugfs_init(fld);
Peng Taod7e09d02013-05-02 16:46:55 +0800355 if (rc)
Julia Lawall89180ca2014-08-30 16:41:23 +0200356 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800357out:
358 if (rc)
359 fld_client_fini(fld);
360 else
361 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
362 fld->lcf_name, fld->lcf_hash->fh_name);
363 return rc;
364}
365EXPORT_SYMBOL(fld_client_init);
366
367void fld_client_fini(struct lu_client_fld *fld)
368{
369 struct lu_fld_target *target, *tmp;
Peng Taod7e09d02013-05-02 16:46:55 +0800370
371 spin_lock(&fld->lcf_lock);
Oleg Drokincf677592016-02-26 01:49:51 -0500372 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
Peng Taod7e09d02013-05-02 16:46:55 +0800373 fld->lcf_count--;
374 list_del(&target->ft_chain);
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500375 if (target->ft_exp)
Peng Taod7e09d02013-05-02 16:46:55 +0800376 class_export_put(target->ft_exp);
Julia Lawall2e651012015-05-01 17:51:21 +0200377 kfree(target);
Peng Taod7e09d02013-05-02 16:46:55 +0800378 }
379 spin_unlock(&fld->lcf_lock);
380
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500381 if (fld->lcf_cache) {
Peng Taod7e09d02013-05-02 16:46:55 +0800382 if (!IS_ERR(fld->lcf_cache))
383 fld_cache_fini(fld->lcf_cache);
384 fld->lcf_cache = NULL;
385 }
Peng Taod7e09d02013-05-02 16:46:55 +0800386}
387EXPORT_SYMBOL(fld_client_fini);
388
389int fld_client_rpc(struct obd_export *exp,
wang dib78c2b9b2016-04-28 12:07:34 -0400390 struct lu_seq_range *range, __u32 fld_op,
391 struct ptlrpc_request **reqp)
Peng Taod7e09d02013-05-02 16:46:55 +0800392{
wang dib78c2b9b2016-04-28 12:07:34 -0400393 struct ptlrpc_request *req = NULL;
Peng Taod7e09d02013-05-02 16:46:55 +0800394 struct lu_seq_range *prange;
395 __u32 *op;
wang dib78c2b9b2016-04-28 12:07:34 -0400396 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800397 struct obd_import *imp;
Peng Taod7e09d02013-05-02 16:46:55 +0800398
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500399 LASSERT(exp);
Peng Taod7e09d02013-05-02 16:46:55 +0800400
401 imp = class_exp2cliimp(exp);
wang dib78c2b9b2016-04-28 12:07:34 -0400402 switch (fld_op) {
403 case FLD_QUERY:
404 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
405 LUSTRE_MDS_VERSION, FLD_QUERY);
406 if (!req)
407 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800408
wang dib78c2b9b2016-04-28 12:07:34 -0400409 /*
410 * XXX: only needed when talking to old server(< 2.6), it should
411 * be removed when < 2.6 server is not supported
412 */
413 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
414 *op = FLD_LOOKUP;
415
416 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS)
417 req->rq_allow_replay = 1;
418 break;
419 case FLD_READ:
420 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
421 LUSTRE_MDS_VERSION, FLD_READ);
422 if (!req)
423 return -ENOMEM;
424
425 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
426 RCL_SERVER, PAGE_SIZE);
427 break;
428 default:
429 rc = -EINVAL;
430 break;
431 }
432 if (rc)
433 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800434
435 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
436 *prange = *range;
Peng Taod7e09d02013-05-02 16:46:55 +0800437 ptlrpc_request_set_replen(req);
438 req->rq_request_portal = FLD_REQUEST_PORTAL;
Mikhail Pershina3310522014-09-08 21:41:27 -0400439 req->rq_reply_portal = MDC_REPLY_PORTAL;
Peng Taod7e09d02013-05-02 16:46:55 +0800440 ptlrpc_at_set_req_timeout(req);
441
Peng Taod7e09d02013-05-02 16:46:55 +0800442 fld_enter_request(&exp->exp_obd->u.cli);
443 rc = ptlrpc_queue_wait(req);
444 fld_exit_request(&exp->exp_obd->u.cli);
Peng Taod7e09d02013-05-02 16:46:55 +0800445 if (rc)
Julia Lawall89180ca2014-08-30 16:41:23 +0200446 goto out_req;
Peng Taod7e09d02013-05-02 16:46:55 +0800447
wang dib78c2b9b2016-04-28 12:07:34 -0400448 if (fld_op == FLD_QUERY) {
449 prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
450 if (!prange) {
451 rc = -EFAULT;
452 goto out_req;
453 }
454 *range = *prange;
Julia Lawall89180ca2014-08-30 16:41:23 +0200455 }
wang dib78c2b9b2016-04-28 12:07:34 -0400456
Peng Taod7e09d02013-05-02 16:46:55 +0800457out_req:
wang dib78c2b9b2016-04-28 12:07:34 -0400458 if (rc || !reqp) {
459 ptlrpc_req_finished(req);
460 req = NULL;
461 }
462
463 if (reqp)
464 *reqp = req;
465
Peng Taod7e09d02013-05-02 16:46:55 +0800466 return rc;
467}
468
Oleg Drokin114acca2014-08-15 12:55:55 -0400469int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
Peng Taod7e09d02013-05-02 16:46:55 +0800470 __u32 flags, const struct lu_env *env)
471{
472 struct lu_seq_range res = { 0 };
473 struct lu_fld_target *target;
474 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800475
476 fld->lcf_flags |= LUSTRE_FLD_RUN;
477
478 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
479 if (rc == 0) {
480 *mds = res.lsr_index;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800481 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800482 }
483
484 /* Can not find it in the cache */
485 target = fld_client_get_target(fld, seq);
Oleg Drokin6ac49ca2016-02-16 00:46:50 -0500486 LASSERT(target);
Peng Taod7e09d02013-05-02 16:46:55 +0800487
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700488 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on target %s (idx %llu)\n",
Oleg Drokincf677592016-02-26 01:49:51 -0500489 fld->lcf_name, seq, fld_target_name(target), target->ft_idx);
Peng Taod7e09d02013-05-02 16:46:55 +0800490
491 res.lsr_start = seq;
492 fld_range_set_type(&res, flags);
wang dib78c2b9b2016-04-28 12:07:34 -0400493 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
Peng Taod7e09d02013-05-02 16:46:55 +0800494
495 if (rc == 0) {
496 *mds = res.lsr_index;
497
498 fld_cache_insert(fld->lcf_cache, &res);
499 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800500 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800501}
502EXPORT_SYMBOL(fld_client_lookup);
503
504void fld_client_flush(struct lu_client_fld *fld)
505{
506 fld_cache_flush(fld->lcf_cache);
507}
508EXPORT_SYMBOL(fld_client_flush);
Liu Xuezhaoe62e5d92013-07-23 00:06:54 +0800509
Andreas Dilgere0f94112016-02-26 11:36:05 -0500510static int __init fld_init(void)
Liu Xuezhaoe62e5d92013-07-23 00:06:54 +0800511{
Dmitry Eremin82765042015-05-21 15:32:26 -0400512 fld_debugfs_dir = ldebugfs_register(LUSTRE_FLD_NAME,
513 debugfs_lustre_root,
514 NULL, NULL);
515 return PTR_ERR_OR_ZERO(fld_debugfs_dir);
Liu Xuezhaoe62e5d92013-07-23 00:06:54 +0800516}
517
Andreas Dilgere0f94112016-02-26 11:36:05 -0500518static void __exit fld_exit(void)
Liu Xuezhaoe62e5d92013-07-23 00:06:54 +0800519{
Dmitry Eremin82765042015-05-21 15:32:26 -0400520 if (!IS_ERR_OR_NULL(fld_debugfs_dir))
521 ldebugfs_remove(&fld_debugfs_dir);
Liu Xuezhaoe62e5d92013-07-23 00:06:54 +0800522}
523
James Simmonsa0455472015-11-04 13:40:02 -0500524MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
Andreas Dilger57878e12016-02-26 11:36:04 -0500525MODULE_DESCRIPTION("Lustre FID Location Database");
James Simmons5b0e50b2016-02-26 11:36:03 -0500526MODULE_VERSION(LUSTRE_VERSION_STRING);
Liu Xuezhaoe62e5d92013-07-23 00:06:54 +0800527MODULE_LICENSE("GPL");
528
Andreas Dilgere0f94112016-02-26 11:36:05 -0500529module_init(fld_init)
530module_exit(fld_exit)