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