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