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