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