Whamcloud - gitweb
LU-3126 osd: remove fld lookup during configuration
[fs/lustre-release.git] / lustre / fld / fld_handler.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/fld/fld_handler.c
37  *
38  * FLD (Fids Location Database)
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  * Author: WangDi <wangdi@clusterfs.com>
42  * Author: Pravin Shelar <pravin.shelar@sun.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_FLD
46
47 #include <libcfs/libcfs.h>
48 #include <linux/module.h>
49
50 #include <obd.h>
51 #include <obd_support.h>
52 #include <lustre_fid.h>
53 #include <lustre_fld.h>
54 #include <md_object.h>
55 #include <lustre_req_layout.h>
56 #include <lprocfs_status.h>
57 #include "fld_internal.h"
58
59 /* context key constructor/destructor: fld_key_init, fld_key_fini */
60 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
61
62 /* context key: fld_thread_key */
63 /* MGS thread may create llog file causing FLD lookup */
64 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
65
66 int fld_server_mod_init(void)
67 {
68         LU_CONTEXT_KEY_INIT(&fld_thread_key);
69         return lu_context_key_register(&fld_thread_key);
70 }
71
72 void fld_server_mod_exit(void)
73 {
74         lu_context_key_degister(&fld_thread_key);
75 }
76
77 int fld_declare_server_create(const struct lu_env *env,
78                               struct lu_server_fld *fld,
79                               const struct lu_seq_range *range,
80                               struct thandle *th)
81 {
82         int rc;
83
84         rc = fld_declare_index_create(env, fld, range, th);
85         RETURN(rc);
86 }
87 EXPORT_SYMBOL(fld_declare_server_create);
88
89 /**
90  * Insert FLD index entry and update FLD cache.
91  *
92  * This function is called from the sequence allocator when a super-sequence
93  * is granted to a server.
94  */
95 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
96                       const struct lu_seq_range *range, struct thandle *th)
97 {
98         int rc;
99
100         mutex_lock(&fld->lsf_lock);
101         rc = fld_index_create(env, fld, range, th);
102         mutex_unlock(&fld->lsf_lock);
103
104         RETURN(rc);
105 }
106 EXPORT_SYMBOL(fld_server_create);
107
108 /**
109  *  Lookup mds by seq, returns a range for given seq.
110  *
111  *  If that entry is not cached in fld cache, request is sent to super
112  *  sequence controller node (MDT0). All other MDT[1...N] and client
113  *  cache fld entries, but this cache is not persistent.
114  */
115 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
116                       seqno_t seq, struct lu_seq_range *range)
117 {
118         struct lu_seq_range *erange;
119         struct fld_thread_info *info;
120         int rc;
121         ENTRY;
122
123         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
124         LASSERT(info != NULL);
125         erange = &info->fti_lrange;
126
127         /* Lookup it in the cache. */
128         rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
129         if (rc == 0) {
130                 if (unlikely(fld_range_type(erange) != fld_range_type(range) &&
131                              !fld_range_is_any(range))) {
132                         CERROR("%s: FLD cache range "DRANGE" does not match"
133                                "requested flag %x: rc = %d\n", fld->lsf_name,
134                                PRANGE(erange), range->lsr_flags, -EIO);
135                         RETURN(-EIO);
136                 }
137                 *range = *erange;
138                 RETURN(0);
139         }
140
141         if (fld->lsf_obj) {
142                 /* On server side, all entries should be in cache.
143                  * If we can not find it in cache, just return error */
144                 CERROR("%s: Cannot find sequence "LPX64": rc = %d\n",
145                        fld->lsf_name, seq, -EIO);
146                 RETURN(-EIO);
147         } else {
148                 if (fld->lsf_control_exp == NULL) {
149                         CERROR("%s: lookup "LPX64", but not connects to MDT0"
150                                "yet: rc = %d.\n", fld->lsf_name, seq, -EIO);
151                         RETURN(-EIO);
152                 }
153                 /* send request to mdt0 i.e. super seq. controller.
154                  * This is temporary solution, long term solution is fld
155                  * replication on all mdt servers.
156                  */
157                 range->lsr_start = seq;
158                 rc = fld_client_rpc(fld->lsf_control_exp,
159                                     range, FLD_LOOKUP);
160                 if (rc == 0)
161                         fld_cache_insert(fld->lsf_cache, range);
162         }
163         RETURN(rc);
164 }
165 EXPORT_SYMBOL(fld_server_lookup);
166
167 /**
168  * All MDT server handle fld lookup operation. But only MDT0 has fld index.
169  * if entry is not found in cache we need to forward lookup request to MDT0
170  */
171 static int fld_server_handle(struct lu_server_fld *fld,
172                              const struct lu_env *env,
173                              __u32 opc, struct lu_seq_range *range)
174 {
175         int rc;
176
177         ENTRY;
178
179         switch (opc) {
180         case FLD_LOOKUP:
181                 rc = fld_server_lookup(env, fld, range->lsr_start, range);
182                 break;
183         default:
184                 rc = -EINVAL;
185                 break;
186         }
187
188         CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
189                DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
190
191         RETURN(rc);
192 }
193
194 static int fld_handler(struct tgt_session_info *tsi)
195 {
196         struct obd_export       *exp = tsi->tsi_exp;
197         struct lu_site          *site = exp->exp_obd->obd_lu_dev->ld_site;
198         struct lu_seq_range     *in;
199         struct lu_seq_range     *out;
200         int                      rc;
201         __u32                   *opc;
202
203         ENTRY;
204
205         opc = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_OPC);
206         if (opc != NULL) {
207                 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
208                 if (in == NULL)
209                         RETURN(err_serious(-EPROTO));
210                 out = req_capsule_server_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
211                 if (out == NULL)
212                         RETURN(err_serious(-EPROTO));
213                 *out = *in;
214
215                 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
216                  * Set it as 'LU_SEQ_RANGE_MDT' by default. */
217                 if (!(exp_connect_flags(exp) & OBD_CONNECT_64BITHASH) &&
218                     !(exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
219                     !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT) &&
220                     !exp->exp_libclient)
221                         fld_range_set_mdt(out);
222
223                 rc = fld_server_handle(lu_site2seq(site)->ss_server_fld,
224                                        tsi->tsi_env, *opc, out);
225         } else {
226                 rc = err_serious(-EPROTO);
227         }
228
229         RETURN(rc);
230 }
231
232 /*
233  * Returns true, if fid is local to this server node.
234  *
235  * WARNING: this function is *not* guaranteed to return false if fid is
236  * remote: it makes an educated conservative guess only.
237  *
238  * fid_is_local() is supposed to be used in assertion checks only.
239  */
240 int fid_is_local(const struct lu_env *env,
241                  struct lu_site *site, const struct lu_fid *fid)
242 {
243         int result;
244         struct seq_server_site *ss_site;
245         struct lu_seq_range *range;
246         struct fld_thread_info *info;
247         ENTRY;
248
249         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
250         range = &info->fti_lrange;
251
252         result = 1; /* conservatively assume fid is local */
253         ss_site = lu_site2seq(site);
254         if (ss_site->ss_client_fld != NULL) {
255                 int rc;
256
257                 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
258                                       fid_seq(fid), range);
259                 if (rc == 0)
260                         result = (range->lsr_index == ss_site->ss_node_id);
261         }
262         return result;
263 }
264 EXPORT_SYMBOL(fid_is_local);
265
266 static void fld_server_proc_fini(struct lu_server_fld *fld);
267
268 #ifdef LPROCFS
269 static int fld_server_proc_init(struct lu_server_fld *fld)
270 {
271         int rc = 0;
272         ENTRY;
273
274         fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
275                                              fld_type_proc_dir,
276                                              fld_server_proc_list, fld);
277         if (IS_ERR(fld->lsf_proc_dir)) {
278                 rc = PTR_ERR(fld->lsf_proc_dir);
279                 RETURN(rc);
280         }
281
282         rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
283                                 &fld_proc_seq_fops, fld);
284         if (rc) {
285                 lprocfs_remove(&fld->lsf_proc_dir);
286                 fld->lsf_proc_dir = NULL;
287         }
288
289         RETURN(rc);
290 }
291
292 static void fld_server_proc_fini(struct lu_server_fld *fld)
293 {
294         ENTRY;
295         if (fld->lsf_proc_dir != NULL) {
296                 if (!IS_ERR(fld->lsf_proc_dir))
297                         lprocfs_remove(&fld->lsf_proc_dir);
298                 fld->lsf_proc_dir = NULL;
299         }
300         EXIT;
301 }
302 #else
303 static int fld_server_proc_init(struct lu_server_fld *fld)
304 {
305         return 0;
306 }
307
308 static void fld_server_proc_fini(struct lu_server_fld *fld)
309 {
310         return;
311 }
312 #endif
313
314 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
315                     struct dt_device *dt, const char *prefix, int mds_node_id,
316                     int type)
317 {
318         int cache_size, cache_threshold;
319         int rc;
320
321         ENTRY;
322
323         snprintf(fld->lsf_name, sizeof(fld->lsf_name),
324                  "srv-%s", prefix);
325
326         cache_size = FLD_SERVER_CACHE_SIZE / sizeof(struct fld_cache_entry);
327
328         cache_threshold = cache_size * FLD_SERVER_CACHE_THRESHOLD / 100;
329
330         mutex_init(&fld->lsf_lock);
331         fld->lsf_cache = fld_cache_init(fld->lsf_name, cache_size,
332                                         cache_threshold);
333         if (IS_ERR(fld->lsf_cache)) {
334                 rc = PTR_ERR(fld->lsf_cache);
335                 fld->lsf_cache = NULL;
336                 RETURN(rc);
337         }
338
339         if (!mds_node_id && type == LU_SEQ_RANGE_MDT) {
340                 rc = fld_index_init(env, fld, dt);
341                 if (rc)
342                         GOTO(out_cache, rc);
343         } else {
344                 fld->lsf_obj = NULL;
345         }
346
347         rc = fld_server_proc_init(fld);
348         if (rc)
349                 GOTO(out_index, rc);
350
351         fld->lsf_control_exp = NULL;
352
353         RETURN(0);
354 out_index:
355         fld_index_fini(env, fld);
356 out_cache:
357         fld_cache_fini(fld->lsf_cache);
358         return rc;
359 }
360 EXPORT_SYMBOL(fld_server_init);
361
362 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
363 {
364         ENTRY;
365
366         fld_server_proc_fini(fld);
367         fld_index_fini(env, fld);
368
369         if (fld->lsf_cache != NULL) {
370                 if (!IS_ERR(fld->lsf_cache))
371                         fld_cache_fini(fld->lsf_cache);
372                 fld->lsf_cache = NULL;
373         }
374
375         EXIT;
376 }
377 EXPORT_SYMBOL(fld_server_fini);
378
379 struct tgt_handler fld_handlers[] = {
380 TGT_FLD_HDL(HABEO_REFERO,       FLD_QUERY,      fld_handler),
381 };
382 EXPORT_SYMBOL(fld_handlers);