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