Whamcloud - gitweb
ec52b5c60eb15f48b3369059d581954af0f429f7
[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  *  lustre/fld/fld_handler.c
5  *  FLD (Fids Location Database)
6  *
7  *  Copyright (C) 2006 Cluster File Systems, Inc.
8  *   Author: Yury Umanets <umka@clusterfs.com>
9  *           WangDi <wangdi@clusterfs.com>
10  *
11  *   This file is part of the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_FLD
33
34 #ifdef __KERNEL__
35 # include <libcfs/libcfs.h>
36 # include <linux/module.h>
37 # include <linux/jbd.h>
38 # include <asm/div64.h>
39 #else /* __KERNEL__ */
40 # include <liblustre.h>
41 # include <libcfs/list.h>
42 #endif
43
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre_ver.h>
47 #include <obd_support.h>
48 #include <lprocfs_status.h>
49
50 #include <md_object.h>
51 #include <lustre_req_layout.h>
52 #include "fld_internal.h"
53
54 #ifdef __KERNEL__
55
56 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
57
58 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD|LCT_DT_THREAD);
59
60 cfs_proc_dir_entry_t *fld_type_proc_dir = NULL;
61
62 static int __init fld_mod_init(void)
63 {
64         fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
65                                              proc_lustre_root,
66                                              NULL, NULL);
67         if (IS_ERR(fld_type_proc_dir))
68                 return PTR_ERR(fld_type_proc_dir);
69
70         LU_CONTEXT_KEY_INIT(&fld_thread_key);
71         lu_context_key_register(&fld_thread_key);
72         return 0;
73 }
74
75 static void __exit fld_mod_exit(void)
76 {
77         lu_context_key_degister(&fld_thread_key);
78         if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
79                 lprocfs_remove(&fld_type_proc_dir);
80                 fld_type_proc_dir = NULL;
81         }
82 }
83
84 /* Insert index entry and update cache. */
85 int fld_server_create(struct lu_server_fld *fld,
86                       const struct lu_env *env,
87                       seqno_t seq, mdsno_t mds)
88 {
89         int rc;
90         ENTRY;
91         
92         rc = fld_index_create(fld, env, seq, mds);
93         
94         if (rc == 0) {
95                 /*
96                  * Do not return result of calling fld_cache_insert()
97                  * here. First of all because it may return -EEXISTS. Another
98                  * reason is that, we do not want to stop proceeding even after
99                  * cache errors.
100                  */
101                 fld_cache_insert(fld->lsf_cache, seq, mds);
102         }
103
104         RETURN(rc);
105 }
106 EXPORT_SYMBOL(fld_server_create);
107
108 /* Delete index entry. */
109 int fld_server_delete(struct lu_server_fld *fld,
110                       const struct lu_env *env,
111                       seqno_t seq)
112 {
113         int rc;
114         ENTRY;
115
116         fld_cache_delete(fld->lsf_cache, seq);
117         rc = fld_index_delete(fld, env, seq);
118         
119         RETURN(rc);
120 }
121 EXPORT_SYMBOL(fld_server_delete);
122
123 /* Lookup mds by seq. */
124 int fld_server_lookup(struct lu_server_fld *fld,
125                       const struct lu_env *env,
126                       seqno_t seq, mdsno_t *mds)
127 {
128         int rc;
129         ENTRY;
130         
131         /* Lookup it in the cache. */
132         rc = fld_cache_lookup(fld->lsf_cache, seq, mds);
133         if (rc == 0)
134                 RETURN(0);
135
136         rc = fld_index_lookup(fld, env, seq, mds);
137         if (rc == 0) {
138                 /*
139                  * Do not return error here as well. See previous comment in
140                  * same situation in function fld_server_create().
141                  */
142                 fld_cache_insert(fld->lsf_cache, seq, *mds);
143         }
144         RETURN(rc);
145 }
146 EXPORT_SYMBOL(fld_server_lookup);
147
148 static int fld_server_handle(struct lu_server_fld *fld,
149                              const struct lu_env *env,
150                              __u32 opc, struct md_fld *mf,
151                              struct fld_thread_info *info)
152 {
153         int rc;
154         ENTRY;
155
156         switch (opc) {
157         case FLD_CREATE:
158                 rc = fld_server_create(fld, env,
159                                        mf->mf_seq, mf->mf_mds);
160
161                 /* Do not return -EEXIST error for resent case */
162                 if ((info->fti_flags & MSG_RESENT) && rc == -EEXIST)
163                         rc = 0;
164                 break;
165         case FLD_DELETE:
166                 rc = fld_server_delete(fld, env, mf->mf_seq);
167
168                 /* Do not return -ENOENT error for resent case */
169                 if ((info->fti_flags & MSG_RESENT) && rc == -ENOENT)
170                         rc = 0;
171                 break;
172         case FLD_LOOKUP:
173                 rc = fld_server_lookup(fld, env,
174                                        mf->mf_seq, &mf->mf_mds);
175                 break;
176         default:
177                 rc = -EINVAL;
178                 break;
179         }
180
181         CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, seq: "
182                LPX64", mds: "LPU64")\n", fld->lsf_name, rc, opc,
183                mf->mf_seq, mf->mf_mds);
184         
185         RETURN(rc);
186
187 }
188
189 static int fld_req_handle(struct ptlrpc_request *req,
190                           struct fld_thread_info *info)
191 {
192         struct lu_site *site;
193         struct md_fld *in;
194         struct md_fld *out;
195         int rc;
196         __u32 *opc;
197         ENTRY;
198
199         site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
200
201         rc = req_capsule_pack(&info->fti_pill);
202         if (rc)
203                 RETURN(err_serious(rc));
204
205         opc = req_capsule_client_get(&info->fti_pill, &RMF_FLD_OPC);
206         if (opc != NULL) {
207                 in = req_capsule_client_get(&info->fti_pill, &RMF_FLD_MDFLD);
208                 if (in == NULL)
209                         RETURN(err_serious(-EPROTO));
210                 out = req_capsule_server_get(&info->fti_pill, &RMF_FLD_MDFLD);
211                 if (out == NULL)
212                         RETURN(err_serious(-EPROTO));
213                 *out = *in;
214
215                 rc = fld_server_handle(site->ls_server_fld,
216                                        req->rq_svc_thread->t_env,
217                                        *opc, out, info);
218         } else
219                 rc = err_serious(-EPROTO);
220
221         RETURN(rc);
222 }
223
224 static void fld_thread_info_init(struct ptlrpc_request *req,
225                                  struct fld_thread_info *info)
226 {
227         int i;
228
229         info->fti_flags = lustre_msg_get_flags(req->rq_reqmsg);
230
231         /* Mark rep buffer as req-layout stuff expects. */
232         for (i = 0; i < ARRAY_SIZE(info->fti_rep_buf_size); i++)
233                 info->fti_rep_buf_size[i] = -1;
234
235         /* Init request capsule. */
236         req_capsule_init(&info->fti_pill, req, RCL_SERVER,
237                          info->fti_rep_buf_size);
238
239         req_capsule_set(&info->fti_pill, &RQF_FLD_QUERY);
240 }
241
242 static void fld_thread_info_fini(struct fld_thread_info *info)
243 {
244         req_capsule_fini(&info->fti_pill);
245 }
246
247 static int fld_handle(struct ptlrpc_request *req)
248 {
249         struct fld_thread_info *info;
250         const struct lu_env *env;
251         int rc;
252
253         env = req->rq_svc_thread->t_env;
254         LASSERT(env != NULL);
255
256         info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
257         LASSERT(info != NULL);
258
259         fld_thread_info_init(req, info);
260         rc = fld_req_handle(req, info);
261         fld_thread_info_fini(info);
262
263         return rc;
264 }
265
266 /*
267  * Entry point for handling FLD RPCs called from MDT.
268  */
269 int fld_query(struct com_thread_info *info)
270 {
271         return fld_handle(info->cti_pill.rc_req);
272 }
273 EXPORT_SYMBOL(fld_query);
274
275 /*
276  * Returns true, if fid is local to this server node.
277  *
278  * WARNING: this function is *not* guaranteed to return false if fid is
279  * remote: it makes an educated conservative guess only.
280  *
281  * fid_is_local() is supposed to be used in assertion checks only.
282  */
283 int fid_is_local(struct lu_site *site, const struct lu_fid *fid)
284 {
285         int result;
286
287         result = 1; /* conservatively assume fid is local */
288         if (site->ls_client_fld != NULL) {
289                 mdsno_t mds;
290                 int rc;
291
292                 rc = fld_cache_lookup(site->ls_client_fld->lcf_cache,
293                                       fid_seq(fid), &mds);
294                 if (rc == 0)
295                         result = (mds == site->ls_node_id);
296         }
297         return result;
298 }
299 EXPORT_SYMBOL(fid_is_local);
300
301 static void fld_server_proc_fini(struct lu_server_fld *fld);
302
303 #ifdef LPROCFS
304 static int fld_server_proc_init(struct lu_server_fld *fld)
305 {
306         int rc = 0;
307         ENTRY;
308
309         fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
310                                              fld_type_proc_dir,
311                                              fld_server_proc_list, fld);
312         if (IS_ERR(fld->lsf_proc_dir)) {
313                 rc = PTR_ERR(fld->lsf_proc_dir);
314                 RETURN(rc);
315         }
316
317         RETURN(rc);
318 }
319
320 static void fld_server_proc_fini(struct lu_server_fld *fld)
321 {
322         ENTRY;
323         if (fld->lsf_proc_dir != NULL) {
324                 if (!IS_ERR(fld->lsf_proc_dir))
325                         lprocfs_remove(&fld->lsf_proc_dir);
326                 fld->lsf_proc_dir = NULL;
327         }
328         EXIT;
329 }
330 #else
331 static int fld_server_proc_init(struct lu_server_fld *fld)
332 {
333         return 0;
334 }
335
336 static void fld_server_proc_fini(struct lu_server_fld *fld)
337 {
338         return;
339 }
340 #endif
341
342 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
343                     const char *prefix, const struct lu_env *env)
344 {
345         int cache_size, cache_threshold;
346         int rc;
347         ENTRY;
348
349         snprintf(fld->lsf_name, sizeof(fld->lsf_name),
350                  "srv-%s", prefix);
351
352         cache_size = FLD_SERVER_CACHE_SIZE /
353                 sizeof(struct fld_cache_entry);
354
355         cache_threshold = cache_size *
356                 FLD_SERVER_CACHE_THRESHOLD / 100;
357
358         fld->lsf_cache = fld_cache_init(fld->lsf_name,
359                                         FLD_SERVER_HTABLE_SIZE,
360                                         cache_size, cache_threshold);
361         if (IS_ERR(fld->lsf_cache)) {
362                 rc = PTR_ERR(fld->lsf_cache);
363                 fld->lsf_cache = NULL;
364                 GOTO(out, rc);
365         }
366
367         rc = fld_index_init(fld, env, dt);
368         if (rc)
369                 GOTO(out, rc);
370
371         rc = fld_server_proc_init(fld);
372         if (rc)
373                 GOTO(out, rc);
374
375         EXIT;
376 out:
377         if (rc)
378                 fld_server_fini(fld, env);
379         return rc;
380 }
381 EXPORT_SYMBOL(fld_server_init);
382
383 void fld_server_fini(struct lu_server_fld *fld,
384                      const struct lu_env *env)
385 {
386         ENTRY;
387
388         fld_server_proc_fini(fld);
389         fld_index_fini(fld, env);
390
391         if (fld->lsf_cache != NULL) {
392                 if (!IS_ERR(fld->lsf_cache))
393                         fld_cache_fini(fld->lsf_cache);
394                 fld->lsf_cache = NULL;
395         }
396
397         EXIT;
398 }
399 EXPORT_SYMBOL(fld_server_fini);
400
401 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
402 MODULE_DESCRIPTION("Lustre FLD");
403 MODULE_LICENSE("GPL");
404
405 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);
406 #endif