Whamcloud - gitweb
4b1c2e251c38a15b9e9447ef33813c9cf78bf261
[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, 2012, Whamcloud, Inc.
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 #ifdef __KERNEL__
48 # include <libcfs/libcfs.h>
49 # include <linux/module.h>
50 # include <linux/jbd.h>
51 # include <asm/div64.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
54 # include <libcfs/list.h>
55 #endif
56
57 #include <obd.h>
58 #include <obd_class.h>
59 #include <lustre_ver.h>
60 #include <obd_support.h>
61 #include <lprocfs_status.h>
62
63 #include <md_object.h>
64 #include <lustre_fid.h>
65 #include <lustre_req_layout.h>
66 #include "fld_internal.h"
67 #include <lustre_fid.h>
68
69 #ifdef __KERNEL__
70
71 /* context key constructor/destructor: fld_key_init, fld_key_fini */
72 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
73
74 /* context key: fld_thread_key */
75 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD|LCT_DT_THREAD);
76
77 cfs_proc_dir_entry_t *fld_type_proc_dir = NULL;
78
79 static int __init fld_mod_init(void)
80 {
81         fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
82                                              proc_lustre_root,
83                                              NULL, NULL);
84         if (IS_ERR(fld_type_proc_dir))
85                 return PTR_ERR(fld_type_proc_dir);
86
87         LU_CONTEXT_KEY_INIT(&fld_thread_key);
88         lu_context_key_register(&fld_thread_key);
89         return 0;
90 }
91
92 static void __exit fld_mod_exit(void)
93 {
94         lu_context_key_degister(&fld_thread_key);
95         if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
96                 lprocfs_remove(&fld_type_proc_dir);
97                 fld_type_proc_dir = NULL;
98         }
99 }
100
101 int fld_declare_server_create(struct lu_server_fld *fld,
102                               const struct lu_env *env,
103                               struct thandle *th)
104 {
105         struct dt_object *dt_obj = fld->lsf_obj;
106         int rc;
107
108         ENTRY;
109
110         if (fld->lsf_no_range_lookup) {
111                 /* Stub for underlying FS which can't lookup ranges */
112                 return 0;
113         }
114
115         /* for ldiskfs OSD it's enough to declare operation with any ops
116          * with DMU we'll probably need to specify exact key/value */
117         rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
118         if (rc)
119                 GOTO(out, rc);
120         rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
121         if (rc)
122                 GOTO(out, rc);
123         rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
124                                                       NULL, NULL, th);
125 out:
126         RETURN(rc);
127 }
128 EXPORT_SYMBOL(fld_declare_server_create);
129
130 /**
131  * Insert FLD index entry and update FLD cache.
132  *
133  * First it try to merge given range with existing range then update
134  * FLD index and FLD cache accordingly. FLD index consistency is maintained
135  * by this function.
136  * This function is called from the sequence allocator when a super-sequence
137  * is granted to a server.
138  */
139
140 int fld_server_create(struct lu_server_fld *fld,
141                       const struct lu_env *env,
142                       struct lu_seq_range *add_range,
143                       struct thandle *th)
144 {
145         struct lu_seq_range *erange;
146         struct lu_seq_range *new;
147         struct fld_thread_info *info;
148         int rc = 0;
149         int do_merge=0;
150
151         ENTRY;
152
153         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
154         cfs_mutex_lock(&fld->lsf_lock);
155
156         erange = &info->fti_lrange;
157         new = &info->fti_irange;
158         *new = *add_range;
159
160         /* STEP 1: try to merge with previous range */
161         rc = fld_index_lookup(fld, env, new->lsr_start, erange);
162         if (rc == 0) {
163                 /* in case of range overlap, the location must be same */
164                 if (range_compare_loc(new, erange) != 0) {
165                         CERROR("the start of given range "DRANGE" conflict to"
166                                "an existing range "DRANGE"\n",
167                                PRANGE(new), PRANGE(erange));
168                         GOTO(out, rc = -EIO);
169                 }
170
171                 if (new->lsr_end < erange->lsr_end)
172                         GOTO(out, rc);
173                 do_merge = 1;
174         } else if (rc == -ENOENT) {
175                 /* check for merge case: optimizes for single mds lustre.
176                  * As entry does not exist, returned entry must be left side
177                  * entry compared to start of new range (ref dio_lookup()).
178                  * So try to merge from left.
179                  */
180                 if (new->lsr_start == erange->lsr_end &&
181                     range_compare_loc(new, erange) == 0)
182                         do_merge = 1;
183         } else {
184                 /* no overlap allowed in fld, so failure in lookup is error */
185                 GOTO(out, rc);
186         }
187
188         if (do_merge) {
189                 /* new range will be merged with the existing one.
190                  * delete this range at first. */
191                 rc = fld_index_delete(fld, env, erange, th);
192                 if (rc != 0)
193                         GOTO(out, rc);
194
195                 new->lsr_start = min(erange->lsr_start, new->lsr_start);
196                 new->lsr_end = max(erange->lsr_end, new->lsr_end);
197                 do_merge = 0;
198         }
199
200         /* STEP 2: try to merge with next range */
201         rc = fld_index_lookup(fld, env, new->lsr_end, erange);
202         if (rc == 0) {
203                 /* found a matched range, meaning we're either
204                  * overlapping or ajacent, must merge with it. */
205                 do_merge = 1;
206         } else if (rc == -ENOENT) {
207                 /* this range is left of new range end point */
208                 LASSERT(erange->lsr_end <= new->lsr_end);
209                 /*
210                  * the found left range must be either:
211                  *  1. withing new range.
212                  *  2. left of new range (no overlapping).
213                  * because if they're partly overlapping, the STEP 1 must have
214                  * been removed this range.
215                  */
216                 LASSERTF(erange->lsr_start > new->lsr_start ||
217                          erange->lsr_end < new->lsr_start ||
218                          (erange->lsr_end == new->lsr_start &&
219                           range_compare_loc(new, erange) != 0),
220                          "left "DRANGE", new "DRANGE"\n",
221                          PRANGE(erange), PRANGE(new));
222
223                 /* if it's within the new range, merge it */
224                 if (erange->lsr_start > new->lsr_start)
225                         do_merge = 1;
226         } else {
227                GOTO(out, rc);
228         }
229
230         if (do_merge) {
231                 if (range_compare_loc(new, erange) != 0) {
232                         CERROR("the end of given range "DRANGE" overlaps "
233                                "with an existing range "DRANGE"\n",
234                                PRANGE(new), PRANGE(erange));
235                         GOTO(out, rc = -EIO);
236                 }
237
238                 /* merge with next range */
239                 rc = fld_index_delete(fld, env, erange, th);
240                 if (rc != 0)
241                         GOTO(out, rc);
242
243                 new->lsr_start = min(erange->lsr_start, new->lsr_start);
244                 new->lsr_end = max(erange->lsr_end, new->lsr_end);
245         }
246
247         /* now update fld entry. */
248         rc = fld_index_create(fld, env, new, th);
249
250         LASSERT(rc != -EEXIST);
251 out:
252         if (rc == 0)
253                 fld_cache_insert(fld->lsf_cache, new);
254
255         cfs_mutex_unlock(&fld->lsf_lock);
256
257         CDEBUG((rc != 0 ? D_ERROR : D_INFO),
258                "%s: FLD create: given range : "DRANGE
259                "after merge "DRANGE" rc = %d \n", fld->lsf_name,
260                 PRANGE(add_range), PRANGE(new), rc);
261
262         RETURN(rc);
263 }
264 EXPORT_SYMBOL(fld_server_create);
265
266 /**
267  *  Lookup mds by seq, returns a range for given seq.
268  *
269  *  If that entry is not cached in fld cache, request is sent to super
270  *  sequence controller node (MDT0). All other MDT[1...N] and client
271  *  cache fld entries, but this cache is not persistent.
272  */
273
274 int fld_server_lookup(struct lu_server_fld *fld,
275                       const struct lu_env *env,
276                       seqno_t seq, struct lu_seq_range *range)
277 {
278         struct lu_seq_range *erange;
279         struct fld_thread_info *info;
280         int rc;
281         ENTRY;
282
283         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
284         erange = &info->fti_lrange;
285
286         /* Lookup it in the cache. */
287         rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
288         if (rc == 0) {
289                 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
290                         CERROR("FLD cache found a range "DRANGE" doesn't "
291                                "match the requested flag %x\n",
292                                PRANGE(erange), range->lsr_flags);
293                         RETURN(-EIO);
294                 }
295                 *range = *erange;
296                 RETURN(0);
297         }
298
299         if (fld->lsf_obj) {
300                 rc = fld_index_lookup(fld, env, seq, erange);
301                 if (rc == 0) {
302                         if (unlikely(erange->lsr_flags != range->lsr_flags)) {
303                                 CERROR("FLD found a range "DRANGE" doesn't "
304                                        "match the requested flag %x\n",
305                                        PRANGE(erange), range->lsr_flags);
306                                 RETURN(-EIO);
307                         }
308                         *range = *erange;
309                 }
310         } else {
311                 LASSERT(fld->lsf_control_exp);
312                 /* send request to mdt0 i.e. super seq. controller.
313                  * This is temporary solution, long term solution is fld
314                  * replication on all mdt servers.
315                  */
316                 rc = fld_client_rpc(fld->lsf_control_exp,
317                                     range, FLD_LOOKUP);
318         }
319
320         if (rc == 0)
321                 fld_cache_insert(fld->lsf_cache, range);
322
323         RETURN(rc);
324 }
325 EXPORT_SYMBOL(fld_server_lookup);
326
327 /**
328  * All MDT server handle fld lookup operation. But only MDT0 has fld index.
329  * if entry is not found in cache we need to forward lookup request to MDT0
330  */
331
332 static int fld_server_handle(struct lu_server_fld *fld,
333                              const struct lu_env *env,
334                              __u32 opc, struct lu_seq_range *range,
335                              struct fld_thread_info *info)
336 {
337         int rc;
338         ENTRY;
339
340         switch (opc) {
341         case FLD_LOOKUP:
342                 rc = fld_server_lookup(fld, env,
343                                        range->lsr_start, range);
344                 break;
345         default:
346                 rc = -EINVAL;
347                 break;
348         }
349
350         CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
351                DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
352
353         RETURN(rc);
354
355 }
356
357 static int fld_req_handle(struct ptlrpc_request *req,
358                           struct fld_thread_info *info)
359 {
360         struct obd_export *exp = req->rq_export;
361         struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
362         struct lu_seq_range *in;
363         struct lu_seq_range *out;
364         int rc;
365         __u32 *opc;
366         ENTRY;
367
368         rc = req_capsule_server_pack(info->fti_pill);
369         if (rc)
370                 RETURN(err_serious(rc));
371
372         opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
373         if (opc != NULL) {
374                 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
375                 if (in == NULL)
376                         RETURN(err_serious(-EPROTO));
377                 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
378                 if (out == NULL)
379                         RETURN(err_serious(-EPROTO));
380                 *out = *in;
381
382                 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
383                  * Set it as 'LU_SEQ_RANGE_MDT' by default.
384                  * Old 2.0 liblustre client cannot talk with new 2.1 server. */
385                 if (!(exp->exp_connect_flags & OBD_CONNECT_64BITHASH) &&
386                     !exp->exp_libclient)
387                         out->lsr_flags = LU_SEQ_RANGE_MDT;
388
389                 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
390                                        req->rq_svc_thread->t_env,
391                                        *opc, out, info);
392         } else
393                 rc = err_serious(-EPROTO);
394
395         RETURN(rc);
396 }
397
398 static void fld_thread_info_init(struct ptlrpc_request *req,
399                                  struct fld_thread_info *info)
400 {
401         info->fti_pill = &req->rq_pill;
402         /* Init request capsule. */
403         req_capsule_init(info->fti_pill, req, RCL_SERVER);
404         req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
405 }
406
407 static void fld_thread_info_fini(struct fld_thread_info *info)
408 {
409         req_capsule_fini(info->fti_pill);
410 }
411
412 static int fld_handle(struct ptlrpc_request *req)
413 {
414         struct fld_thread_info *info;
415         const struct lu_env *env;
416         int rc;
417
418         env = req->rq_svc_thread->t_env;
419         LASSERT(env != NULL);
420
421         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
422         LASSERT(info != NULL);
423
424         fld_thread_info_init(req, info);
425         rc = fld_req_handle(req, info);
426         fld_thread_info_fini(info);
427
428         return rc;
429 }
430
431 /*
432  * Entry point for handling FLD RPCs called from MDT.
433  */
434 int fld_query(struct com_thread_info *info)
435 {
436         return fld_handle(info->cti_pill->rc_req);
437 }
438 EXPORT_SYMBOL(fld_query);
439
440 /*
441  * Returns true, if fid is local to this server node.
442  *
443  * WARNING: this function is *not* guaranteed to return false if fid is
444  * remote: it makes an educated conservative guess only.
445  *
446  * fid_is_local() is supposed to be used in assertion checks only.
447  */
448 int fid_is_local(const struct lu_env *env,
449                  struct lu_site *site, const struct lu_fid *fid)
450 {
451         int result;
452         struct md_site *msite;
453         struct lu_seq_range *range;
454         struct fld_thread_info *info;
455         ENTRY;
456
457         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
458         range = &info->fti_lrange;
459
460         result = 1; /* conservatively assume fid is local */
461         msite = lu_site2md(site);
462         if (msite->ms_client_fld != NULL) {
463                 int rc;
464
465                 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
466                                       fid_seq(fid), range);
467                 if (rc == 0)
468                         result = (range->lsr_index == msite->ms_node_id);
469         }
470         return result;
471 }
472 EXPORT_SYMBOL(fid_is_local);
473
474 static void fld_server_proc_fini(struct lu_server_fld *fld);
475
476 #ifdef LPROCFS
477 static int fld_server_proc_init(struct lu_server_fld *fld)
478 {
479         int rc = 0;
480         ENTRY;
481
482         fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
483                                              fld_type_proc_dir,
484                                              fld_server_proc_list, fld);
485         if (IS_ERR(fld->lsf_proc_dir)) {
486                 rc = PTR_ERR(fld->lsf_proc_dir);
487                 RETURN(rc);
488         }
489
490         rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
491                                 &fld_proc_seq_fops, fld);
492         if (rc) {
493                 lprocfs_remove(&fld->lsf_proc_dir);
494                 fld->lsf_proc_dir = NULL;
495         }
496
497         RETURN(rc);
498 }
499
500 static void fld_server_proc_fini(struct lu_server_fld *fld)
501 {
502         ENTRY;
503         if (fld->lsf_proc_dir != NULL) {
504                 if (!IS_ERR(fld->lsf_proc_dir))
505                         lprocfs_remove(&fld->lsf_proc_dir);
506                 fld->lsf_proc_dir = NULL;
507         }
508         EXIT;
509 }
510 #else
511 static int fld_server_proc_init(struct lu_server_fld *fld)
512 {
513         return 0;
514 }
515
516 static void fld_server_proc_fini(struct lu_server_fld *fld)
517 {
518         return;
519 }
520 #endif
521
522 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
523                     const char *prefix, const struct lu_env *env,
524                     int mds_node_id)
525 {
526         int cache_size, cache_threshold;
527         struct lu_seq_range range;
528         int rc;
529         ENTRY;
530
531         snprintf(fld->lsf_name, sizeof(fld->lsf_name),
532                  "srv-%s", prefix);
533
534         cache_size = FLD_SERVER_CACHE_SIZE /
535                 sizeof(struct fld_cache_entry);
536
537         cache_threshold = cache_size *
538                 FLD_SERVER_CACHE_THRESHOLD / 100;
539
540         cfs_mutex_init(&fld->lsf_lock);
541         fld->lsf_cache = fld_cache_init(fld->lsf_name,
542                                         cache_size, cache_threshold);
543         if (IS_ERR(fld->lsf_cache)) {
544                 rc = PTR_ERR(fld->lsf_cache);
545                 fld->lsf_cache = NULL;
546                 GOTO(out, rc);
547         }
548
549         if (!mds_node_id) {
550                 rc = fld_index_init(fld, env, dt);
551                 if (rc)
552                         GOTO(out, rc);
553         } else
554                 fld->lsf_obj = NULL;
555
556         rc = fld_server_proc_init(fld);
557         if (rc)
558                 GOTO(out, rc);
559
560         fld->lsf_control_exp = NULL;
561
562         /* Insert reserved sequence number of ".lustre" into fld cache. */
563         range.lsr_start = FID_SEQ_DOT_LUSTRE;
564         range.lsr_end = FID_SEQ_DOT_LUSTRE + 1;
565         range.lsr_index = 0;
566         range.lsr_flags = LU_SEQ_RANGE_MDT;
567         fld_cache_insert(fld->lsf_cache, &range);
568
569         EXIT;
570 out:
571         if (rc)
572                 fld_server_fini(fld, env);
573         return rc;
574 }
575 EXPORT_SYMBOL(fld_server_init);
576
577 void fld_server_fini(struct lu_server_fld *fld,
578                      const struct lu_env *env)
579 {
580         ENTRY;
581
582         fld_server_proc_fini(fld);
583         fld_index_fini(fld, env);
584
585         if (fld->lsf_cache != NULL) {
586                 if (!IS_ERR(fld->lsf_cache))
587                         fld_cache_fini(fld->lsf_cache);
588                 fld->lsf_cache = NULL;
589         }
590
591         EXIT;
592 }
593 EXPORT_SYMBOL(fld_server_fini);
594
595 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
596 MODULE_DESCRIPTION("Lustre FLD");
597 MODULE_LICENSE("GPL");
598
599 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);
600 #endif