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