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