Whamcloud - gitweb
Land b_head_interop_disk on HEAD (20081119_1314)
[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  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_FLD
48
49 #ifdef __KERNEL__
50 # include <libcfs/libcfs.h>
51 # include <linux/module.h>
52 # include <linux/jbd.h>
53 # include <asm/div64.h>
54 #else /* __KERNEL__ */
55 # include <liblustre.h>
56 # include <libcfs/list.h>
57 #endif
58
59 #include <obd.h>
60 #include <obd_class.h>
61 #include <lustre_ver.h>
62 #include <obd_support.h>
63 #include <lprocfs_status.h>
64
65 #include <md_object.h>
66 #include <lustre_fid.h>
67 #include <lustre_req_layout.h>
68 #include "fld_internal.h"
69
70 #ifdef __KERNEL__
71
72 /* context key constructor/destructor: fld_key_init, fld_key_fini */
73 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
74
75 /* context key: fld_thread_key */
76 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD|LCT_DT_THREAD);
77
78 cfs_proc_dir_entry_t *fld_type_proc_dir = NULL;
79
80 static struct lu_local_obj_desc llod_fld_index = {
81         .llod_name      = fld_index_name,
82         .llod_oid       = FLD_INDEX_OID,
83         .llod_is_index  = 1,
84         .llod_feat      = &fld_index_features,
85 };
86
87 static int __init fld_mod_init(void)
88 {
89         fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
90                                              proc_lustre_root,
91                                              NULL, NULL);
92         if (IS_ERR(fld_type_proc_dir))
93                 return PTR_ERR(fld_type_proc_dir);
94
95         llo_local_obj_register(&llod_fld_index);
96
97         LU_CONTEXT_KEY_INIT(&fld_thread_key);
98         lu_context_key_register(&fld_thread_key);
99         return 0;
100 }
101
102 static void __exit fld_mod_exit(void)
103 {
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 /* Insert index entry and update cache. */
112 int fld_server_create(struct lu_server_fld *fld,
113                       const struct lu_env *env,
114                       seqno_t seq, mdsno_t mds)
115 {
116         int rc;
117         ENTRY;
118         
119         rc = fld_index_create(fld, env, seq, mds);
120         
121         if (rc == 0) {
122                 /*
123                  * Do not return result of calling fld_cache_insert()
124                  * here. First of all because it may return -EEXISTS. Another
125                  * reason is that, we do not want to stop proceeding even after
126                  * cache errors.
127                  */
128                 fld_cache_insert(fld->lsf_cache, seq, mds);
129         }
130
131         RETURN(rc);
132 }
133 EXPORT_SYMBOL(fld_server_create);
134
135 /* Delete index entry. */
136 int fld_server_delete(struct lu_server_fld *fld,
137                       const struct lu_env *env,
138                       seqno_t seq)
139 {
140         int rc;
141         ENTRY;
142
143         fld_cache_delete(fld->lsf_cache, seq);
144         rc = fld_index_delete(fld, env, seq);
145         
146         RETURN(rc);
147 }
148 EXPORT_SYMBOL(fld_server_delete);
149
150 /* Lookup mds by seq. */
151 int fld_server_lookup(struct lu_server_fld *fld,
152                       const struct lu_env *env,
153                       seqno_t seq, mdsno_t *mds)
154 {
155         int rc;
156         ENTRY;
157         
158         /* Lookup it in the cache. */
159         rc = fld_cache_lookup(fld->lsf_cache, seq, mds);
160         if (rc == 0)
161                 RETURN(0);
162
163         rc = fld_index_lookup(fld, env, seq, mds);
164         if (rc == 0) {
165                 /*
166                  * Do not return error here as well. See previous comment in
167                  * same situation in function fld_server_create().
168                  */
169                 fld_cache_insert(fld->lsf_cache, seq, *mds);
170         }
171         RETURN(rc);
172 }
173 EXPORT_SYMBOL(fld_server_lookup);
174
175 static int fld_server_handle(struct lu_server_fld *fld,
176                              const struct lu_env *env,
177                              __u32 opc, struct md_fld *mf,
178                              struct fld_thread_info *info)
179 {
180         int rc;
181         ENTRY;
182
183         switch (opc) {
184         case FLD_CREATE:
185                 rc = fld_server_create(fld, env,
186                                        mf->mf_seq, mf->mf_mds);
187
188                 /* Do not return -EEXIST error for resent case */
189                 if ((info->fti_flags & MSG_RESENT) && rc == -EEXIST)
190                         rc = 0;
191                 break;
192         case FLD_DELETE:
193                 rc = fld_server_delete(fld, env, mf->mf_seq);
194
195                 /* Do not return -ENOENT error for resent case */
196                 if ((info->fti_flags & MSG_RESENT) && rc == -ENOENT)
197                         rc = 0;
198                 break;
199         case FLD_LOOKUP:
200                 rc = fld_server_lookup(fld, env,
201                                        mf->mf_seq, &mf->mf_mds);
202                 break;
203         default:
204                 rc = -EINVAL;
205                 break;
206         }
207
208         CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, seq: "
209                LPX64", mds: "LPU64")\n", fld->lsf_name, rc, opc,
210                mf->mf_seq, mf->mf_mds);
211         
212         RETURN(rc);
213
214 }
215
216 static int fld_req_handle(struct ptlrpc_request *req,
217                           struct fld_thread_info *info)
218 {
219         struct lu_site *site;
220         struct md_fld *in;
221         struct md_fld *out;
222         int rc;
223         __u32 *opc;
224         ENTRY;
225
226         site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
227
228         rc = req_capsule_server_pack(info->fti_pill);
229         if (rc)
230                 RETURN(err_serious(rc));
231
232         opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
233         if (opc != NULL) {
234                 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
235                 if (in == NULL)
236                         RETURN(err_serious(-EPROTO));
237                 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
238                 if (out == NULL)
239                         RETURN(err_serious(-EPROTO));
240                 *out = *in;
241
242                 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
243                                        req->rq_svc_thread->t_env,
244                                        *opc, out, info);
245         } else
246                 rc = err_serious(-EPROTO);
247
248         RETURN(rc);
249 }
250
251 static void fld_thread_info_init(struct ptlrpc_request *req,
252                                  struct fld_thread_info *info)
253 {
254         info->fti_flags = lustre_msg_get_flags(req->rq_reqmsg);
255
256         info->fti_pill = &req->rq_pill;
257         /* Init request capsule. */
258         req_capsule_init(info->fti_pill, req, RCL_SERVER);
259         req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
260 }
261
262 static void fld_thread_info_fini(struct fld_thread_info *info)
263 {
264         req_capsule_fini(info->fti_pill);
265 }
266
267 static int fld_handle(struct ptlrpc_request *req)
268 {
269         struct fld_thread_info *info;
270         const struct lu_env *env;
271         int rc;
272
273         env = req->rq_svc_thread->t_env;
274         LASSERT(env != NULL);
275
276         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
277         LASSERT(info != NULL);
278
279         fld_thread_info_init(req, info);
280         rc = fld_req_handle(req, info);
281         fld_thread_info_fini(info);
282
283         return rc;
284 }
285
286 /*
287  * Entry point for handling FLD RPCs called from MDT.
288  */
289 int fld_query(struct com_thread_info *info)
290 {
291         return fld_handle(info->cti_pill->rc_req);
292 }
293 EXPORT_SYMBOL(fld_query);
294
295 /*
296  * Returns true, if fid is local to this server node.
297  *
298  * WARNING: this function is *not* guaranteed to return false if fid is
299  * remote: it makes an educated conservative guess only.
300  *
301  * fid_is_local() is supposed to be used in assertion checks only.
302  */
303 int fid_is_local(struct lu_site *site, const struct lu_fid *fid)
304 {
305         int result;
306         struct md_site *msite;
307
308         result = 1; /* conservatively assume fid is local */
309         msite = lu_site2md(site);
310         if (msite->ms_client_fld != NULL) {
311                 mdsno_t mds;
312                 int rc;
313
314                 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
315                                       fid_seq(fid), &mds);
316                 if (rc == 0)
317                         result = (mds == msite->ms_node_id);
318         }
319         return result;
320 }
321 EXPORT_SYMBOL(fid_is_local);
322
323 static void fld_server_proc_fini(struct lu_server_fld *fld);
324
325 #ifdef LPROCFS
326 static int fld_server_proc_init(struct lu_server_fld *fld)
327 {
328         int rc = 0;
329         ENTRY;
330
331         fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
332                                              fld_type_proc_dir,
333                                              fld_server_proc_list, fld);
334         if (IS_ERR(fld->lsf_proc_dir)) {
335                 rc = PTR_ERR(fld->lsf_proc_dir);
336                 RETURN(rc);
337         }
338
339         RETURN(rc);
340 }
341
342 static void fld_server_proc_fini(struct lu_server_fld *fld)
343 {
344         ENTRY;
345         if (fld->lsf_proc_dir != NULL) {
346                 if (!IS_ERR(fld->lsf_proc_dir))
347                         lprocfs_remove(&fld->lsf_proc_dir);
348                 fld->lsf_proc_dir = NULL;
349         }
350         EXIT;
351 }
352 #else
353 static int fld_server_proc_init(struct lu_server_fld *fld)
354 {
355         return 0;
356 }
357
358 static void fld_server_proc_fini(struct lu_server_fld *fld)
359 {
360         return;
361 }
362 #endif
363
364 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
365                     const char *prefix, const struct lu_env *env)
366 {
367         int cache_size, cache_threshold;
368         int rc;
369         ENTRY;
370
371         snprintf(fld->lsf_name, sizeof(fld->lsf_name),
372                  "srv-%s", prefix);
373
374         cache_size = FLD_SERVER_CACHE_SIZE /
375                 sizeof(struct fld_cache_entry);
376
377         cache_threshold = cache_size *
378                 FLD_SERVER_CACHE_THRESHOLD / 100;
379
380         fld->lsf_cache = fld_cache_init(fld->lsf_name,
381                                         FLD_SERVER_HTABLE_SIZE,
382                                         cache_size, cache_threshold);
383         if (IS_ERR(fld->lsf_cache)) {
384                 rc = PTR_ERR(fld->lsf_cache);
385                 fld->lsf_cache = NULL;
386                 GOTO(out, rc);
387         }
388
389         rc = fld_index_init(fld, env, dt);
390         if (rc)
391                 GOTO(out, rc);
392
393         rc = fld_server_proc_init(fld);
394         if (rc)
395                 GOTO(out, rc);
396
397         EXIT;
398 out:
399         if (rc)
400                 fld_server_fini(fld, env);
401         return rc;
402 }
403 EXPORT_SYMBOL(fld_server_init);
404
405 void fld_server_fini(struct lu_server_fld *fld,
406                      const struct lu_env *env)
407 {
408         ENTRY;
409
410         fld_server_proc_fini(fld);
411         fld_index_fini(fld, env);
412
413         if (fld->lsf_cache != NULL) {
414                 if (!IS_ERR(fld->lsf_cache))
415                         fld_cache_fini(fld->lsf_cache);
416                 fld->lsf_cache = NULL;
417         }
418
419         EXIT;
420 }
421 EXPORT_SYMBOL(fld_server_fini);
422
423 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
424 MODULE_DESCRIPTION("Lustre FLD");
425 MODULE_LICENSE("GPL");
426
427 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);
428 #endif