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