Whamcloud - gitweb
req-layout: handle 0-length fields: use (-1) as variable field marker
[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: WangDi <wangdi@clusterfs.com>
9  *           Yury Umanets <umka@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 <dt_object.h>
51 #include <md_object.h>
52 #include <lustre_req_layout.h>
53 #include <lustre_fld.h>
54 #include "fld_internal.h"
55
56 #ifdef __KERNEL__
57 static int __init fld_mod_init(void)
58 {
59         /* nothing to init seems */
60         return 0;
61 }
62
63 static void __exit fld_mod_exit(void)
64 {
65         /* nothing to fini seems */
66         return;
67 }
68
69 /* insert index entry and update cache */
70 int fld_server_create(struct lu_server_fld *fld,
71                       const struct lu_context *ctx,
72                       seqno_t seq, mdsno_t mds)
73 {
74         ENTRY;
75         RETURN(fld_index_create(fld, ctx, seq, mds));
76 }
77 EXPORT_SYMBOL(fld_server_create);
78
79 /* delete index entry */
80 int fld_server_delete(struct lu_server_fld *fld,
81                       const struct lu_context *ctx,
82                       seqno_t seq)
83 {
84         ENTRY;
85         RETURN(fld_index_delete(fld, ctx, seq));
86 }
87 EXPORT_SYMBOL(fld_server_delete);
88
89 /* issue on-disk index lookup */
90 int fld_server_lookup(struct lu_server_fld *fld,
91                       const struct lu_context *ctx,
92                       seqno_t seq, mdsno_t *mds)
93 {
94         ENTRY;
95         RETURN(fld_index_lookup(fld, ctx, seq, mds));
96 }
97 EXPORT_SYMBOL(fld_server_lookup);
98
99 static int fld_server_handle(struct lu_server_fld *fld,
100                              const struct lu_context *ctx,
101                              __u32 opc, struct md_fld *mf)
102 {
103         int rc;
104         ENTRY;
105
106         switch (opc) {
107         case FLD_CREATE:
108                 rc = fld_server_create(fld, ctx,
109                                        mf->mf_seq, mf->mf_mds);
110                 break;
111         case FLD_DELETE:
112                 rc = fld_server_delete(fld, ctx, mf->mf_seq);
113                 break;
114         case FLD_LOOKUP:
115                 rc = fld_server_lookup(fld, ctx,
116                                        mf->mf_seq, &mf->mf_mds);
117                 break;
118         default:
119                 rc = -EINVAL;
120                 break;
121         }
122         RETURN(rc);
123
124 }
125
126 static int fld_req_handle0(const struct lu_context *ctx,
127                            struct lu_server_fld *fld,
128                            struct ptlrpc_request *req)
129 {
130         int rep_buf_size[3] = { -1, -1 };
131         struct req_capsule pill;
132         struct md_fld *in;
133         struct md_fld *out;
134         int rc = -EPROTO;
135         __u32 *opc;
136         ENTRY;
137
138         req_capsule_init(&pill, req, RCL_SERVER, rep_buf_size);
139
140         req_capsule_set(&pill, &RQF_FLD_QUERY);
141         req_capsule_pack(&pill);
142
143         opc = req_capsule_client_get(&pill, &RMF_FLD_OPC);
144         if (opc != NULL) {
145                 in = req_capsule_client_get(&pill, &RMF_FLD_MDFLD);
146                 if (in == NULL) {
147                         CERROR("cannot unpack fld request\n");
148                         GOTO(out_pill, rc = -EPROTO);
149                 }
150                 out = req_capsule_server_get(&pill, &RMF_FLD_MDFLD);
151                 if (out == NULL) {
152                         CERROR("cannot allocate fld response\n");
153                         GOTO(out_pill, rc = -EPROTO);
154                 }
155                 *out = *in;
156                 rc = fld_server_handle(fld, ctx, *opc, out);
157         } else {
158                 CERROR("cannot unpack FLD operation\n");
159         }
160
161 out_pill:
162         EXIT;
163         req_capsule_fini(&pill);
164         return rc;
165 }
166
167 static int fld_req_handle(struct ptlrpc_request *req)
168 {
169         int fail = OBD_FAIL_FLD_ALL_REPLY_NET;
170         const struct lu_context *ctx;
171         struct lu_site *site;
172         int rc = -EPROTO;
173         ENTRY;
174
175         OBD_FAIL_RETURN(OBD_FAIL_FLD_ALL_REPLY_NET | OBD_FAIL_ONCE, 0);
176
177         ctx = req->rq_svc_thread->t_ctx;
178         LASSERT(ctx != NULL);
179         LASSERT(ctx->lc_thread == req->rq_svc_thread);
180         if (req->rq_reqmsg->opc == FLD_QUERY) {
181                 if (req->rq_export != NULL) {
182                         site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
183                         LASSERT(site != NULL);
184                         rc = fld_req_handle0(ctx, site->ls_server_fld, req);
185                 } else {
186                         CERROR("Unconnected request\n");
187                         req->rq_status = -ENOTCONN;
188                         GOTO(out, rc = -ENOTCONN);
189                 }
190         } else {
191                 CERROR("Wrong opcode: %d\n", req->rq_reqmsg->opc);
192                 req->rq_status = -ENOTSUPP;
193                 rc = ptlrpc_error(req);
194                 RETURN(rc);
195         }
196
197         EXIT;
198 out:
199         target_send_reply(req, rc, fail);
200         return 0;
201 }
202
203 /*
204  * Returns true, if fid is local to this server node.
205  *
206  * WARNING: this function is *not* guaranteed to return false if fid is
207  * remote: it makes an educated conservative guess only.
208  *
209  * fid_is_local() is supposed to be used in assertion checks only.
210  */
211 int fid_is_local(struct lu_site *site, const struct lu_fid *fid)
212 {
213         int result;
214
215         result = 1; /* conservatively assume fid is local */
216         if (site->ls_client_fld != NULL) {
217                 mdsno_t mds;
218                 int rc;
219
220                 rc = fld_cache_lookup(site->ls_client_fld->fld_cache,
221                                       fid_seq(fid), &mds);
222                 if (rc == 0)
223                         result = (mds == site->ls_node_id);
224         }
225         return result;
226 }
227 EXPORT_SYMBOL(fid_is_local);
228
229 #ifdef LPROCFS
230 static int fld_server_proc_init(struct lu_server_fld *fld)
231 {
232         int rc;
233         ENTRY;
234
235         fld->fld_proc_dir = lprocfs_register(fld->fld_name,
236                                              proc_lustre_root,
237                                              NULL, NULL);
238         if (IS_ERR(fld->fld_proc_dir)) {
239                 CERROR("LProcFS failed in fld-init\n");
240                 rc = PTR_ERR(fld->fld_proc_dir);
241                 GOTO(err, rc);
242         }
243
244         fld->fld_proc_entry = lprocfs_register("services",
245                                                fld->fld_proc_dir,
246                                                NULL, NULL);
247         if (IS_ERR(fld->fld_proc_entry)) {
248                 CERROR("LProcFS failed in fld-init\n");
249                 rc = PTR_ERR(fld->fld_proc_entry);
250                 GOTO(err_type, rc);
251         }
252
253         rc = lprocfs_add_vars(fld->fld_proc_dir,
254                               fld_server_proc_list, fld);
255         if (rc) {
256                 CERROR("can't init FLD proc, rc %d\n", rc);
257                 GOTO(err_entry, rc);
258         }
259
260         RETURN(0);
261
262 err_entry:
263         lprocfs_remove(fld->fld_proc_entry);
264 err_type:
265         lprocfs_remove(fld->fld_proc_dir);
266 err:
267         fld->fld_proc_dir = NULL;
268         fld->fld_proc_entry = NULL;
269         return rc;
270 }
271
272 static void fld_server_proc_fini(struct lu_server_fld *fld)
273 {
274         ENTRY;
275         if (fld->fld_proc_entry) {
276                 lprocfs_remove(fld->fld_proc_entry);
277                 fld->fld_proc_entry = NULL;
278         }
279
280         if (fld->fld_proc_dir) {
281                 lprocfs_remove(fld->fld_proc_dir);
282                 fld->fld_proc_dir = NULL;
283         }
284         EXIT;
285 }
286 #endif
287
288 int fld_server_init(struct lu_server_fld *fld,
289                     const struct lu_context *ctx,
290                     struct dt_device *dt,
291                     const char *uuid)
292 {
293         int rc;
294         struct ptlrpc_service_conf fld_conf = {
295                 .psc_nbufs            = MDS_NBUFS,
296                 .psc_bufsize          = MDS_BUFSIZE,
297                 .psc_max_req_size     = FLD_MAXREQSIZE,
298                 .psc_max_reply_size   = FLD_MAXREPSIZE,
299                 .psc_req_portal       = FLD_REQUEST_PORTAL,
300                 .psc_rep_portal       = MDC_REPLY_PORTAL,
301                 .psc_watchdog_timeout = FLD_SERVICE_WATCHDOG_TIMEOUT,
302                 .psc_num_threads      = FLD_NUM_THREADS,
303                 .psc_ctx_tags         = LCT_DT_THREAD|LCT_MD_THREAD
304         };
305         ENTRY;
306
307         fld->fld_dt = dt;
308         lu_device_get(&dt->dd_lu_dev);
309
310         snprintf(fld->fld_name, sizeof(fld->fld_name),
311                  "%s-srv-%s", LUSTRE_FLD_NAME, uuid);
312
313         rc = fld_index_init(fld, ctx);
314         if (rc)
315                 GOTO(out, rc);
316
317 #ifdef LPROCFS
318         rc = fld_server_proc_init(fld);
319         if (rc)
320                 GOTO(out, rc);
321 #endif
322
323         fld->fld_service =
324                 ptlrpc_init_svc_conf(&fld_conf, fld_req_handle,
325                                      LUSTRE_FLD_NAME,
326                                      fld->fld_proc_entry, NULL);
327         if (fld->fld_service != NULL)
328                 rc = ptlrpc_start_threads(NULL, fld->fld_service,
329                                           LUSTRE_FLD_NAME);
330         else
331                 rc = -ENOMEM;
332
333         EXIT;
334 out:
335         if (rc)
336                 fld_server_fini(fld, ctx);
337         else
338                 CDEBUG(D_INFO|D_WARNING, "Server FLD\n");
339         return rc;
340 }
341 EXPORT_SYMBOL(fld_server_init);
342
343 void fld_server_fini(struct lu_server_fld *fld,
344                      const struct lu_context *ctx)
345 {
346         ENTRY;
347
348 #ifdef LPROCFS
349         fld_server_proc_fini(fld);
350 #endif
351
352         if (fld->fld_service != NULL) {
353                 ptlrpc_unregister_service(fld->fld_service);
354                 fld->fld_service = NULL;
355         }
356
357         if (fld->fld_dt != NULL) {
358                 lu_device_put(&fld->fld_dt->dd_lu_dev);
359                 fld_index_fini(fld, ctx);
360                 fld->fld_dt = NULL;
361         }
362         EXIT;
363 }
364 EXPORT_SYMBOL(fld_server_fini);
365
366 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
367 MODULE_DESCRIPTION("Lustre FLD");
368 MODULE_LICENSE("GPL");
369
370 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);
371 #endif