Whamcloud - gitweb
LU-6142 fld: Fix style issues for fld_handler.c
[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.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_handler.c
33  *
34  * FLD (Fids Location Database)
35  *
36  * Author: Yury Umanets <umka@clusterfs.com>
37  * Author: WangDi <wangdi@clusterfs.com>
38  * Author: Pravin Shelar <pravin.shelar@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FLD
42
43 #include <libcfs/libcfs.h>
44 #include <linux/module.h>
45
46 #include <obd.h>
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <lustre_fld.h>
50 #include <lustre_req_layout.h>
51 #include <lprocfs_status.h>
52 #include "fld_internal.h"
53
54 /* context key constructor/destructor: fld_key_init, fld_key_fini */
55 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
56
57 /* context key: fld_thread_key */
58 /* MGS thread may create llog file causing FLD lookup */
59 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
60
61 int fld_server_mod_init(void)
62 {
63         LU_CONTEXT_KEY_INIT(&fld_thread_key);
64         return lu_context_key_register(&fld_thread_key);
65 }
66
67 void fld_server_mod_exit(void)
68 {
69         lu_context_key_degister(&fld_thread_key);
70 }
71
72 int fld_declare_server_create(const struct lu_env *env,
73                               struct lu_server_fld *fld,
74                               const struct lu_seq_range *range,
75                               struct thandle *th)
76 {
77         int rc;
78
79         rc = fld_declare_index_create(env, fld, range, th);
80         RETURN(rc);
81 }
82 EXPORT_SYMBOL(fld_declare_server_create);
83
84 /**
85  * Insert FLD index entry and update FLD cache.
86  *
87  * This function is called from the sequence allocator when a super-sequence
88  * is granted to a server.
89  */
90 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
91                       const struct lu_seq_range *range, struct thandle *th)
92 {
93         int rc;
94
95         mutex_lock(&fld->lsf_lock);
96         rc = fld_index_create(env, fld, range, th);
97         mutex_unlock(&fld->lsf_lock);
98
99         RETURN(rc);
100 }
101 EXPORT_SYMBOL(fld_server_create);
102
103 /**
104  * Extract index information from fld name like srv-fsname-MDT0000
105  **/
106 int fld_name_to_index(const char *name, u32 *index)
107 {
108         char *dash;
109         int rc;
110
111         ENTRY;
112
113         CDEBUG(D_INFO, "get index from %s\n", name);
114         dash = strrchr(name, '-');
115         if (!dash)
116                 RETURN(-EINVAL);
117         dash++;
118         rc = target_name2index(dash, index, NULL);
119         RETURN(rc);
120 }
121
122 /**
123  * Retrieve fldb entry from MDT0 and add to local FLDB and cache.
124  **/
125 int fld_update_from_controller(const struct lu_env *env,
126                                struct lu_server_fld *fld)
127 {
128         struct fld_thread_info *info;
129         struct lu_seq_range *range;
130         struct lu_seq_range_array *lsra;
131         u32 index;
132         struct ptlrpc_request *req;
133         int rc;
134         int i;
135
136         ENTRY;
137
138         /*
139          * Update only happens during initalization, i.e. local FLDB
140          * does not exist yet
141          */
142         if (!fld->lsf_new)
143                 RETURN(0);
144
145         rc = fld_name_to_index(fld->lsf_name, &index);
146         if (rc < 0)
147                 RETURN(rc);
148
149         /* No need update fldb for MDT0 */
150         if (index == 0)
151                 RETURN(0);
152
153         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
154         LASSERT(info != NULL);
155         range = &info->fti_lrange;
156         memset(range, 0, sizeof(*range));
157         range->lsr_index = index;
158         fld_range_set_mdt(range);
159
160         do {
161                 rc = fld_client_rpc(fld->lsf_control_exp, range, FLD_READ,
162                                     &req);
163                 if (rc != 0 && rc != -EAGAIN)
164                         GOTO(out, rc);
165
166                 LASSERT(req != NULL);
167                 lsra = (struct lu_seq_range_array *)req_capsule_server_get(
168                                           &req->rq_pill, &RMF_GENERIC_DATA);
169                 if (!lsra)
170                         GOTO(out, rc = -EPROTO);
171
172                 range_array_le_to_cpu(lsra, lsra);
173                 for (i = 0; i < lsra->lsra_count; i++) {
174                         int rc1;
175
176                         if (lsra->lsra_lsr[i].lsr_flags != LU_SEQ_RANGE_MDT)
177                                 GOTO(out, rc = -EINVAL);
178
179                         if (lsra->lsra_lsr[i].lsr_index != index)
180                                 GOTO(out, rc = -EINVAL);
181
182                         mutex_lock(&fld->lsf_lock);
183                         rc1 = fld_insert_entry(env, fld, &lsra->lsra_lsr[i]);
184                         mutex_unlock(&fld->lsf_lock);
185
186                         if (rc1 != 0)
187                                 GOTO(out, rc = rc1);
188                 }
189                 if (rc == -EAGAIN)
190                         *range = lsra->lsra_lsr[lsra->lsra_count - 1];
191         } while (rc == -EAGAIN);
192
193         fld->lsf_new = 1;
194 out:
195         if (req)
196                 ptlrpc_req_finished(req);
197
198         RETURN(rc);
199 }
200 EXPORT_SYMBOL(fld_update_from_controller);
201
202 /**
203  * Lookup sequece in local cache/fldb.
204  **/
205 int fld_local_lookup(const struct lu_env *env, struct lu_server_fld *fld,
206                      u64 seq, struct lu_seq_range *range)
207 {
208         struct lu_seq_range *erange;
209         struct fld_thread_info *info;
210         int rc;
211
212         ENTRY;
213
214         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
215         LASSERT(info != NULL);
216         erange = &info->fti_lrange;
217
218         /* Lookup it in the cache. */
219         rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
220         if (rc == 0) {
221                 if (unlikely(fld_range_type(erange) != fld_range_type(range) &&
222                              !fld_range_is_any(range))) {
223                         CERROR("%s: FLD cache range "DRANGE" does not match requested flag %x: rc = %d\n",
224                                fld->lsf_name, PRANGE(erange), range->lsr_flags,
225                                -EIO);
226                         RETURN(-EIO);
227                 }
228                 *range = *erange;
229                 RETURN(0);
230         }
231         RETURN(rc);
232 }
233 EXPORT_SYMBOL(fld_local_lookup);
234
235 /**
236  *  Lookup MDT/OST by seq, returns a range for given seq.
237  *
238  *  If that entry is not cached in fld cache, request is sent to super
239  *  sequence controller node (MDT0). All other MDT[1...N] and client
240  *  cache fld entries, but this cache is not persistent.
241  */
242 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
243                       u64 seq, struct lu_seq_range *range)
244 {
245         u32 index;
246         int rc;
247
248         ENTRY;
249
250         rc = fld_local_lookup(env, fld, seq, range);
251         if (likely(rc == 0))
252                 RETURN(rc);
253
254         rc = fld_name_to_index(fld->lsf_name, &index);
255         if (rc < 0)
256                 RETURN(rc);
257
258         if (index == 0 && rc == LDD_F_SV_TYPE_MDT) {
259                 /*
260                  * On server side, all entries should be in cache.
261                  * If we can not find it in cache, just return error
262                  */
263                 CERROR("%s: Cannot find sequence %#llx: rc = %d\n",
264                        fld->lsf_name, seq, -ENOENT);
265                 RETURN(-ENOENT);
266         } else {
267                 if (!fld->lsf_control_exp) {
268                         CERROR("%s: lookup %#llx, but not connects to MDT0 yet: rc = %d.\n",
269                                fld->lsf_name, seq, -EIO);
270                         RETURN(-EIO);
271                 }
272                 /*
273                  * send request to mdt0 i.e. super seq. controller.
274                  * This is temporary solution, long term solution is fld
275                  * replication on all mdt servers.
276                  */
277                 range->lsr_start = seq;
278                 rc = fld_client_rpc(fld->lsf_control_exp,
279                                     range, FLD_QUERY, NULL);
280                 if (rc == 0)
281                         fld_cache_insert(fld->lsf_cache, range);
282         }
283         RETURN(rc);
284 }
285 EXPORT_SYMBOL(fld_server_lookup);
286
287 /**
288  * All MDT server handle fld lookup operation. But only MDT0 has fld index.
289  * if entry is not found in cache we need to forward lookup request to MDT0
290  */
291 static int fld_handle_lookup(struct tgt_session_info *tsi)
292 {
293         struct obd_export *exp = tsi->tsi_exp;
294         struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
295         struct lu_server_fld *fld;
296         struct lu_seq_range *in;
297         struct lu_seq_range *out;
298         int rc;
299
300         ENTRY;
301
302         in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
303         if (!in)
304                 RETURN(err_serious(-EPROTO));
305
306         rc = req_capsule_server_pack(tsi->tsi_pill);
307         if (unlikely(rc != 0))
308                 RETURN(err_serious(rc));
309
310         out = req_capsule_server_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
311         if (!out)
312                 RETURN(err_serious(-EPROTO));
313         *out = *in;
314
315         fld = lu_site2seq(site)->ss_server_fld;
316
317         rc = fld_server_lookup(tsi->tsi_env, fld, in->lsr_start, out);
318
319         CDEBUG(D_INFO, "%s: FLD req handle: error %d (range: "DRANGE")\n",
320                fld->lsf_name, rc, PRANGE(out));
321
322         RETURN(rc);
323 }
324
325 static int fld_handle_read(struct tgt_session_info *tsi)
326 {
327         struct obd_export *exp = tsi->tsi_exp;
328         struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
329         struct lu_seq_range *in;
330         void *data;
331         int rc;
332
333         ENTRY;
334
335         req_capsule_set(tsi->tsi_pill, &RQF_FLD_READ);
336
337         in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
338         if (!in)
339                 RETURN(err_serious(-EPROTO));
340
341         req_capsule_set_size(tsi->tsi_pill, &RMF_GENERIC_DATA, RCL_SERVER,
342                              PAGE_SIZE);
343
344         rc = req_capsule_server_pack(tsi->tsi_pill);
345         if (unlikely(rc != 0))
346                 RETURN(err_serious(rc));
347
348         data = req_capsule_server_get(tsi->tsi_pill, &RMF_GENERIC_DATA);
349
350         rc = fld_server_read(tsi->tsi_env, lu_site2seq(site)->ss_server_fld,
351                              in, data, PAGE_SIZE);
352         RETURN(rc);
353 }
354
355 static int fld_handle_query(struct tgt_session_info *tsi)
356 {
357         int     rc;
358
359         ENTRY;
360
361         req_capsule_set(tsi->tsi_pill, &RQF_FLD_QUERY);
362
363         rc = fld_handle_lookup(tsi);
364
365         RETURN(rc);
366 }
367
368 /*
369  * Returns true, if fid is local to this server node.
370  *
371  * WARNING: this function is *not* guaranteed to return false if fid is
372  * remote: it makes an educated conservative guess only.
373  *
374  * fid_is_local() is supposed to be used in assertion checks only.
375  */
376 int fid_is_local(const struct lu_env *env,
377                  struct lu_site *site, const struct lu_fid *fid)
378 {
379         int result;
380         struct seq_server_site *ss_site;
381         struct lu_seq_range *range;
382         struct fld_thread_info *info;
383
384         ENTRY;
385
386         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
387         range = &info->fti_lrange;
388
389         result = 1; /* conservatively assume fid is local */
390         ss_site = lu_site2seq(site);
391         if (ss_site->ss_client_fld) {
392                 int rc;
393
394                 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
395                                       fid_seq(fid), range);
396                 if (rc == 0)
397                         result = (range->lsr_index == ss_site->ss_node_id);
398         }
399         return result;
400 }
401
402 static void fld_server_debugfs_fini(struct lu_server_fld *fld)
403 {
404         if (!IS_ERR_OR_NULL(fld->lsf_debugfs_entry))
405                 ldebugfs_remove(&fld->lsf_debugfs_entry);
406 }
407
408 static int fld_server_debugfs_init(struct lu_server_fld *fld)
409 {
410         int rc = 0;
411
412         ENTRY;
413         fld->lsf_debugfs_entry = ldebugfs_register(fld->lsf_name,
414                                                    fld_debugfs_dir,
415                                                    NULL, NULL);
416         if (IS_ERR_OR_NULL(fld->lsf_debugfs_entry)) {
417                 rc = fld->lsf_debugfs_entry ? PTR_ERR(fld->lsf_debugfs_entry)
418                                             : -ENOMEM;
419                 fld->lsf_debugfs_entry = NULL;
420                 RETURN(rc);
421         }
422
423         rc = ldebugfs_seq_create(fld->lsf_debugfs_entry, "fldb", 0444,
424                                  &fld_debugfs_seq_fops, fld);
425         if (rc)
426                 ldebugfs_remove(&fld->lsf_debugfs_entry);
427
428         RETURN(rc);
429 }
430
431 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
432                     struct dt_device *dt, const char *prefix, int type)
433 {
434         int cache_size, cache_threshold;
435         int rc;
436
437         ENTRY;
438
439         snprintf(fld->lsf_name, sizeof(fld->lsf_name), "srv-%s", prefix);
440
441         cache_size = FLD_SERVER_CACHE_SIZE / sizeof(struct fld_cache_entry);
442
443         cache_threshold = cache_size * FLD_SERVER_CACHE_THRESHOLD / 100;
444
445         mutex_init(&fld->lsf_lock);
446         fld->lsf_cache = fld_cache_init(fld->lsf_name, cache_size,
447                                         cache_threshold);
448         if (IS_ERR(fld->lsf_cache)) {
449                 rc = PTR_ERR(fld->lsf_cache);
450                 fld->lsf_cache = NULL;
451                 RETURN(rc);
452         }
453
454         rc = fld_index_init(env, fld, dt, type);
455         if (rc)
456                 GOTO(out_cache, rc);
457
458         rc = fld_server_debugfs_init(fld);
459         if (rc)
460                 GOTO(out_index, rc);
461
462         fld->lsf_control_exp = NULL;
463         fld->lsf_seq_lookup = fld_server_lookup;
464
465         fld->lsf_seq_lookup = fld_server_lookup;
466         RETURN(0);
467 out_index:
468         fld_index_fini(env, fld);
469 out_cache:
470         fld_cache_fini(fld->lsf_cache);
471         return rc;
472 }
473 EXPORT_SYMBOL(fld_server_init);
474
475 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
476 {
477         ENTRY;
478
479         fld_server_debugfs_fini(fld);
480         fld_index_fini(env, fld);
481
482         if (fld->lsf_cache) {
483                 if (!IS_ERR(fld->lsf_cache))
484                         fld_cache_fini(fld->lsf_cache);
485                 fld->lsf_cache = NULL;
486         }
487
488         EXIT;
489 }
490 EXPORT_SYMBOL(fld_server_fini);
491
492 struct tgt_handler fld_handlers[] = {
493 TGT_FLD_HDL_VAR(0,      FLD_QUERY,      fld_handle_query),
494 TGT_FLD_HDL_VAR(0,      FLD_READ,       fld_handle_read),
495 };
496 EXPORT_SYMBOL(fld_handlers);