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