Whamcloud - gitweb
9d6d1380aaee642c3697c2740dc81226e5b22072
[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 <lustre_mdt.h> /* err_serious() */
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 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
64
65 int fld_server_mod_init(void)
66 {
67         LU_CONTEXT_KEY_INIT(&fld_thread_key);
68         return lu_context_key_register(&fld_thread_key);
69 }
70
71 void fld_server_mod_exit(void)
72 {
73         lu_context_key_degister(&fld_thread_key);
74 }
75
76 int fld_declare_server_create(const struct lu_env *env,
77                               struct lu_server_fld *fld,
78                               const struct lu_seq_range *range,
79                               struct thandle *th)
80 {
81         int rc;
82
83         rc = fld_declare_index_create(env, fld, range, th);
84         RETURN(rc);
85 }
86 EXPORT_SYMBOL(fld_declare_server_create);
87
88 /**
89  * Insert FLD index entry and update FLD cache.
90  *
91  * This function is called from the sequence allocator when a super-sequence
92  * is granted to a server.
93  */
94 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
95                       const struct lu_seq_range *range, struct thandle *th)
96 {
97         int rc;
98
99         mutex_lock(&fld->lsf_lock);
100         rc = fld_index_create(env, fld, range, th);
101         mutex_unlock(&fld->lsf_lock);
102
103         RETURN(rc);
104 }
105 EXPORT_SYMBOL(fld_server_create);
106
107 /**
108  *  Lookup mds by seq, returns a range for given seq.
109  *
110  *  If that entry is not cached in fld cache, request is sent to super
111  *  sequence controller node (MDT0). All other MDT[1...N] and client
112  *  cache fld entries, but this cache is not persistent.
113  */
114 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
115                       seqno_t seq, struct lu_seq_range *range)
116 {
117         struct lu_seq_range *erange;
118         struct fld_thread_info *info;
119         int rc;
120         ENTRY;
121
122         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
123         LASSERT(info != NULL);
124         erange = &info->fti_lrange;
125
126         /* Lookup it in the cache. */
127         rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
128         if (rc == 0) {
129                 if (unlikely(fld_range_type(erange) != fld_range_type(range) &&
130                              !fld_range_is_any(range))) {
131                         CERROR("%s: FLD cache range "DRANGE" does not match"
132                                "requested flag %x: rc = %d\n", fld->lsf_name,
133                                PRANGE(erange), range->lsr_flags, -EIO);
134                         RETURN(-EIO);
135                 }
136                 *range = *erange;
137                 RETURN(0);
138         }
139
140         if (fld->lsf_obj) {
141                 /* On server side, all entries should be in cache.
142                  * If we can not find it in cache, just return error */
143                 CERROR("%s: Cannot find sequence "LPX64": rc = %d\n",
144                        fld->lsf_name, seq, -EIO);
145                 RETURN(-EIO);
146         } else {
147                 LASSERT(fld->lsf_control_exp);
148                 /* send request to mdt0 i.e. super seq. controller.
149                  * This is temporary solution, long term solution is fld
150                  * replication on all mdt servers.
151                  */
152                 range->lsr_start = seq;
153                 rc = fld_client_rpc(fld->lsf_control_exp,
154                                     range, FLD_LOOKUP);
155                 if (rc == 0)
156                         fld_cache_insert(fld->lsf_cache, range);
157         }
158         RETURN(rc);
159 }
160 EXPORT_SYMBOL(fld_server_lookup);
161
162 /**
163  * All MDT server handle fld lookup operation. But only MDT0 has fld index.
164  * if entry is not found in cache we need to forward lookup request to MDT0
165  */
166
167 static int fld_server_handle(struct lu_server_fld *fld,
168                              const struct lu_env *env,
169                              __u32 opc, struct lu_seq_range *range,
170                              struct fld_thread_info *info)
171 {
172         int rc;
173         ENTRY;
174
175         switch (opc) {
176         case FLD_LOOKUP:
177                 rc = fld_server_lookup(env, fld, range->lsr_start, range);
178                 break;
179         default:
180                 rc = -EINVAL;
181                 break;
182         }
183
184         CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
185                DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
186
187         RETURN(rc);
188
189 }
190
191 static int fld_req_handle(struct ptlrpc_request *req,
192                           struct fld_thread_info *info)
193 {
194         struct obd_export *exp = req->rq_export;
195         struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
196         struct lu_seq_range *in;
197         struct lu_seq_range *out;
198         int rc;
199         __u32 *opc;
200         ENTRY;
201
202         rc = req_capsule_server_pack(info->fti_pill);
203         if (rc)
204                 RETURN(err_serious(rc));
205
206         opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
207         if (opc != NULL) {
208                 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
209                 if (in == NULL)
210                         RETURN(err_serious(-EPROTO));
211                 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
212                 if (out == NULL)
213                         RETURN(err_serious(-EPROTO));
214                 *out = *in;
215
216                 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
217                  * Set it as 'LU_SEQ_RANGE_MDT' by default. */
218                 if (!(exp_connect_flags(exp) & OBD_CONNECT_64BITHASH) &&
219                     !(exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
220                     !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT) &&
221                     !exp->exp_libclient)
222                         fld_range_set_mdt(out);
223
224                 rc = fld_server_handle(lu_site2seq(site)->ss_server_fld,
225                                        req->rq_svc_thread->t_env,
226                                        *opc, out, info);
227         } else {
228                 rc = err_serious(-EPROTO);
229         }
230
231         RETURN(rc);
232 }
233
234 static void fld_thread_info_init(struct ptlrpc_request *req,
235                                  struct fld_thread_info *info)
236 {
237         info->fti_pill = &req->rq_pill;
238         /* Init request capsule. */
239         req_capsule_init(info->fti_pill, req, RCL_SERVER);
240         req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
241 }
242
243 static void fld_thread_info_fini(struct fld_thread_info *info)
244 {
245         req_capsule_fini(info->fti_pill);
246 }
247
248 static int fld_handle(struct ptlrpc_request *req)
249 {
250         struct fld_thread_info *info;
251         const struct lu_env *env;
252         int rc;
253
254         env = req->rq_svc_thread->t_env;
255         LASSERT(env != NULL);
256
257         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
258         LASSERT(info != NULL);
259
260         fld_thread_info_init(req, info);
261         rc = fld_req_handle(req, info);
262         fld_thread_info_fini(info);
263
264         return rc;
265 }
266
267 /*
268  * Entry point for handling FLD RPCs called from MDT.
269  */
270 int fld_query(struct com_thread_info *info)
271 {
272         return fld_handle(info->cti_pill->rc_req);
273 }
274 EXPORT_SYMBOL(fld_query);
275
276 /*
277  * Returns true, if fid is local to this server node.
278  *
279  * WARNING: this function is *not* guaranteed to return false if fid is
280  * remote: it makes an educated conservative guess only.
281  *
282  * fid_is_local() is supposed to be used in assertion checks only.
283  */
284 int fid_is_local(const struct lu_env *env,
285                  struct lu_site *site, const struct lu_fid *fid)
286 {
287         int result;
288         struct seq_server_site *ss_site;
289         struct lu_seq_range *range;
290         struct fld_thread_info *info;
291         ENTRY;
292
293         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
294         range = &info->fti_lrange;
295
296         result = 1; /* conservatively assume fid is local */
297         ss_site = lu_site2seq(site);
298         if (ss_site->ss_client_fld != NULL) {
299                 int rc;
300
301                 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
302                                       fid_seq(fid), range);
303                 if (rc == 0)
304                         result = (range->lsr_index == ss_site->ss_node_id);
305         }
306         return result;
307 }
308 EXPORT_SYMBOL(fid_is_local);
309
310 static void fld_server_proc_fini(struct lu_server_fld *fld);
311
312 #ifdef LPROCFS
313 static int fld_server_proc_init(struct lu_server_fld *fld)
314 {
315         int rc = 0;
316         ENTRY;
317
318         fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
319                                              fld_type_proc_dir,
320                                              fld_server_proc_list, fld);
321         if (IS_ERR(fld->lsf_proc_dir)) {
322                 rc = PTR_ERR(fld->lsf_proc_dir);
323                 RETURN(rc);
324         }
325
326         rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
327                                 &fld_proc_seq_fops, fld);
328         if (rc) {
329                 lprocfs_remove(&fld->lsf_proc_dir);
330                 fld->lsf_proc_dir = NULL;
331         }
332
333         RETURN(rc);
334 }
335
336 static void fld_server_proc_fini(struct lu_server_fld *fld)
337 {
338         ENTRY;
339         if (fld->lsf_proc_dir != NULL) {
340                 if (!IS_ERR(fld->lsf_proc_dir))
341                         lprocfs_remove(&fld->lsf_proc_dir);
342                 fld->lsf_proc_dir = NULL;
343         }
344         EXIT;
345 }
346 #else
347 static int fld_server_proc_init(struct lu_server_fld *fld)
348 {
349         return 0;
350 }
351
352 static void fld_server_proc_fini(struct lu_server_fld *fld)
353 {
354         return;
355 }
356 #endif
357
358 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
359                     struct dt_device *dt, const char *prefix, int mds_node_id,
360                     int type)
361 {
362         int cache_size, cache_threshold;
363         int rc;
364         ENTRY;
365
366         snprintf(fld->lsf_name, sizeof(fld->lsf_name),
367                  "srv-%s", prefix);
368
369         cache_size = FLD_SERVER_CACHE_SIZE /
370                 sizeof(struct fld_cache_entry);
371
372         cache_threshold = cache_size *
373                 FLD_SERVER_CACHE_THRESHOLD / 100;
374
375         mutex_init(&fld->lsf_lock);
376         fld->lsf_cache = fld_cache_init(fld->lsf_name,
377                                         cache_size, cache_threshold);
378         if (IS_ERR(fld->lsf_cache)) {
379                 rc = PTR_ERR(fld->lsf_cache);
380                 fld->lsf_cache = NULL;
381                 GOTO(out, rc);
382         }
383
384         if (!mds_node_id && type == LU_SEQ_RANGE_MDT) {
385                 rc = fld_index_init(env, fld, dt);
386                 if (rc)
387                         GOTO(out, rc);
388         } else {
389                 fld->lsf_obj = NULL;
390         }
391
392         rc = fld_server_proc_init(fld);
393         if (rc)
394                 GOTO(out, rc);
395
396         fld->lsf_control_exp = NULL;
397
398         GOTO(out, rc);
399
400 out:
401         if (rc)
402                 fld_server_fini(env, fld);
403         return rc;
404 }
405 EXPORT_SYMBOL(fld_server_init);
406
407 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
408 {
409         ENTRY;
410
411         fld_server_proc_fini(fld);
412         fld_index_fini(env, fld);
413
414         if (fld->lsf_cache != NULL) {
415                 if (!IS_ERR(fld->lsf_cache))
416                         fld_cache_fini(fld->lsf_cache);
417                 fld->lsf_cache = NULL;
418         }
419
420         EXIT;
421 }
422 EXPORT_SYMBOL(fld_server_fini);