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