Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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  *
31  * lustre/fld/fld_handler.c
32  *
33  * FLD (Fids Location Database)
34  *
35  * Author: Yury Umanets <umka@clusterfs.com>
36  * Author: WangDi <wangdi@clusterfs.com>
37  * Author: Pravin Shelar <pravin.shelar@sun.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_FLD
41
42 #include <libcfs/libcfs.h>
43 #include <linux/module.h>
44
45 #include <obd.h>
46 #include <obd_support.h>
47 #include <lu_target.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)
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                 int i;
268
269                 if (!fld->lsf_control_exp) {
270                         CERROR("%s: lookup %#llx, but not connects to MDT0 yet: rc = %d.\n",
271                                fld->lsf_name, seq, -EIO);
272                         RETURN(-EIO);
273                 }
274                 /*
275                  * send request to mdt0 i.e. super seq. controller.
276                  * This is temporary solution, long term solution is fld
277                  * replication on all mdt servers.
278                  */
279                 range->lsr_start = seq;
280                 for (i = 0; i < 5; i++) {
281                         rc = fld_client_rpc(fld->lsf_control_exp,
282                                             range, FLD_QUERY, NULL);
283                         if (rc != -EAGAIN)
284                                 break;
285                         schedule_timeout_interruptible(cfs_time_seconds(1));
286                 }
287                 if (rc == 0)
288                         fld_cache_insert(fld->lsf_cache, range);
289         }
290         RETURN(rc);
291 }
292 EXPORT_SYMBOL(fld_server_lookup);
293
294 /**
295  * All MDT server handle fld lookup operation. But only MDT0 has fld index.
296  * if entry is not found in cache we need to forward lookup request to MDT0
297  */
298 static int fld_handle_lookup(struct tgt_session_info *tsi)
299 {
300         struct obd_export *exp = tsi->tsi_exp;
301         struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
302         struct lu_server_fld *fld;
303         struct lu_seq_range *in;
304         struct lu_seq_range *out;
305         int rc;
306
307         ENTRY;
308
309         in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
310         if (!in)
311                 RETURN(err_serious(-EPROTO));
312
313         rc = req_capsule_server_pack(tsi->tsi_pill);
314         if (unlikely(rc != 0))
315                 RETURN(err_serious(rc));
316
317         out = req_capsule_server_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
318         if (!out)
319                 RETURN(err_serious(-EPROTO));
320         *out = *in;
321
322         fld = lu_site2seq(site)->ss_server_fld;
323
324         rc = fld_server_lookup(tsi->tsi_env, fld, in->lsr_start, out);
325
326         CDEBUG(D_INFO, "%s: FLD req handle: error %d (range: "DRANGE")\n",
327                fld->lsf_name, rc, PRANGE(out));
328
329         RETURN(rc);
330 }
331
332 static int fld_handle_read(struct tgt_session_info *tsi)
333 {
334         struct obd_export *exp = tsi->tsi_exp;
335         struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
336         struct lu_seq_range *in;
337         void *data;
338         int rc;
339
340         ENTRY;
341
342         req_capsule_set(tsi->tsi_pill, &RQF_FLD_READ);
343
344         in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
345         if (!in)
346                 RETURN(err_serious(-EPROTO));
347
348         req_capsule_set_size(tsi->tsi_pill, &RMF_GENERIC_DATA, RCL_SERVER,
349                              PAGE_SIZE);
350
351         rc = req_capsule_server_pack(tsi->tsi_pill);
352         if (unlikely(rc != 0))
353                 RETURN(err_serious(rc));
354
355         data = req_capsule_server_get(tsi->tsi_pill, &RMF_GENERIC_DATA);
356
357         rc = fld_server_read(tsi->tsi_env, lu_site2seq(site)->ss_server_fld,
358                              in, data, PAGE_SIZE);
359         RETURN(rc);
360 }
361
362 static int fld_handle_query(struct tgt_session_info *tsi)
363 {
364         int     rc;
365
366         ENTRY;
367
368         req_capsule_set(tsi->tsi_pill, &RQF_FLD_QUERY);
369
370         rc = fld_handle_lookup(tsi);
371
372         RETURN(rc);
373 }
374
375 /*
376  * Returns true, if fid is local to this server node.
377  *
378  * WARNING: this function is *not* guaranteed to return false if fid is
379  * remote: it makes an educated conservative guess only.
380  *
381  * fid_is_local() is supposed to be used in assertion checks only.
382  */
383 int fid_is_local(const struct lu_env *env,
384                  struct lu_site *site, const struct lu_fid *fid)
385 {
386         int result;
387         struct seq_server_site *ss_site;
388         struct lu_seq_range *range;
389         struct fld_thread_info *info;
390
391         ENTRY;
392
393         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
394         range = &info->fti_lrange;
395
396         result = 1; /* conservatively assume fid is local */
397         ss_site = lu_site2seq(site);
398         if (ss_site->ss_client_fld) {
399                 int rc;
400
401                 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
402                                       fid_seq(fid), range);
403                 if (rc == 0)
404                         result = (range->lsr_index == ss_site->ss_node_id);
405         }
406         return result;
407 }
408 EXPORT_SYMBOL(fid_is_local);
409
410 static void fld_server_debugfs_fini(struct lu_server_fld *fld)
411 {
412         debugfs_remove_recursive(fld->lsf_debugfs_entry);
413 }
414
415 static void fld_server_debugfs_init(struct lu_server_fld *fld)
416 {
417         ENTRY;
418         fld->lsf_debugfs_entry = debugfs_create_dir(fld->lsf_name,
419                                                     fld_debugfs_dir);
420
421         debugfs_create_file("fldb", 0444, fld->lsf_debugfs_entry, fld,
422                             &fld_debugfs_seq_fops);
423 }
424
425 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
426                     struct dt_device *dt, const char *prefix, int type)
427 {
428         int cache_size, cache_threshold;
429         int rc;
430
431         ENTRY;
432
433         snprintf(fld->lsf_name, sizeof(fld->lsf_name), "srv-%s", prefix);
434
435         cache_size = FLD_SERVER_CACHE_SIZE / sizeof(struct fld_cache_entry);
436
437         cache_threshold = cache_size * FLD_SERVER_CACHE_THRESHOLD / 100;
438
439         mutex_init(&fld->lsf_lock);
440         fld->lsf_cache = fld_cache_init(fld->lsf_name, cache_size,
441                                         cache_threshold);
442         if (IS_ERR(fld->lsf_cache)) {
443                 rc = PTR_ERR(fld->lsf_cache);
444                 fld->lsf_cache = NULL;
445                 RETURN(rc);
446         }
447
448         rc = fld_index_init(env, fld, dt, type);
449         if (rc)
450                 GOTO(out_cache, rc);
451
452         fld_server_debugfs_init(fld);
453
454         fld->lsf_control_exp = NULL;
455         fld->lsf_seq_lookup = fld_server_lookup;
456
457         fld->lsf_seq_lookup = fld_server_lookup;
458         RETURN(0);
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) {
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);