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