Whamcloud - gitweb
Land b1_2 onto HEAD (20040304_171022)
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
32 #else
33 # include <liblustre.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/lprocfs_status.h>
40 #include "mdc_internal.h"
41
42 #define REQUEST_MINOR 244
43
44 static int mdc_cleanup(struct obd_device *obd, int flags);
45
46 extern int mds_queue_req(struct ptlrpc_request *);
47 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
48 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
49 static int send_getstatus(struct obd_import *imp, struct ll_fid *rootfid,
50                           int level, int msg_flags)
51 {
52         struct ptlrpc_request *req;
53         struct mds_body *body;
54         int rc, size = sizeof(*body);
55         ENTRY;
56
57         req = ptlrpc_prep_req(imp, MDS_GETSTATUS, 1, &size, NULL);
58         if (!req)
59                 GOTO(out, rc = -ENOMEM);
60
61         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
62         req->rq_send_state = level;
63         req->rq_replen = lustre_msg_size(1, &size);
64
65         mdc_pack_req_body(req);
66         req->rq_reqmsg->flags |= msg_flags;
67         rc = ptlrpc_queue_wait(req);
68
69         if (!rc) {
70                 body = lustre_swab_repbuf (req, 0, sizeof (*body),
71                                            lustre_swab_mds_body);
72                 if (body == NULL) {
73                         CERROR ("Can't extract mds_body\n");
74                         GOTO (out, rc = -EPROTO);
75                 }
76
77                 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
78
79                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
80                        ", last_xid="LPU64"\n",
81                        rootfid->id, req->rq_repmsg->last_committed,
82                        req->rq_repmsg->last_xid);
83         }
84
85         EXIT;
86  out:
87         ptlrpc_req_finished(req);
88         return rc;
89 }
90
91 /* This should be mdc_get_info("rootfid") */
92 int mdc_getstatus(struct obd_export *exp, struct ll_fid *rootfid)
93 {
94         return send_getstatus(class_exp2cliimp(exp), rootfid, LUSTRE_IMP_FULL,
95                               0);
96 }
97
98 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size, 
99                        struct ptlrpc_request *req)
100 {
101         struct mds_body *body;
102         void            *eadata;
103         int              rc;
104         int              size[2] = {sizeof(*body), 0};
105         int              bufcount = 1;
106         ENTRY;
107
108         /* request message already built */
109
110         if (ea_size != 0) {
111                 size[bufcount++] = ea_size;
112                 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
113                        ea_size);
114         }
115         req->rq_replen = lustre_msg_size(bufcount, size);
116
117         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
118         rc = ptlrpc_queue_wait(req);
119         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
120         if (rc != 0)
121                 RETURN (rc);
122
123         body = lustre_swab_repbuf (req, 0, sizeof (*body),
124                                    lustre_swab_mds_body);
125         if (body == NULL) {
126                 CERROR ("Can't unpack mds_body\n");
127                 RETURN (-EPROTO);
128         }
129
130         CDEBUG(D_NET, "mode: %o\n", body->mode);
131
132         LASSERT_REPSWAB (req, 1);
133         if (body->eadatasize != 0) {
134                 /* reply indicates presence of eadata; check it's there... */
135                 eadata = lustre_msg_buf (req->rq_repmsg, 1, body->eadatasize);
136                 if (eadata == NULL) {
137                         CERROR ("Missing/short eadata\n");
138                         RETURN (-EPROTO);
139                 }
140         }
141
142         RETURN (0);
143 }
144
145 int mdc_getattr(struct obd_export *exp, struct ll_fid *fid,
146                 unsigned long valid, unsigned int ea_size,
147                 struct ptlrpc_request **request)
148 {
149         struct ptlrpc_request *req;
150         struct mds_body *body;
151         int size = sizeof(*body);
152         int rc;
153         ENTRY;
154
155         /* XXX do we need to make another request here?  We just did a getattr
156          *     to do the lookup in the first place.
157          */
158         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_GETATTR, 1, &size,
159                               NULL);
160         if (!req)
161                 GOTO(out, rc = -ENOMEM);
162
163         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
164         memcpy(&body->fid1, fid, sizeof(*fid));
165         body->valid = valid;
166         body->eadatasize = ea_size;
167         mdc_pack_req_body(req);
168
169         rc = mdc_getattr_common(exp, ea_size, req);
170         if (rc != 0) {
171                 ptlrpc_req_finished (req);
172                 req = NULL;
173         }
174  out:
175         *request = req;
176         RETURN (rc);
177 }
178
179 int mdc_getattr_name(struct obd_export *exp, struct ll_fid *fid,
180                      char *filename, int namelen, unsigned long valid,
181                      unsigned int ea_size, struct ptlrpc_request **request)
182 {
183         struct ptlrpc_request *req;
184         struct mds_body *body;
185         int rc, size[2] = {sizeof(*body), namelen};
186         ENTRY;
187
188         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_GETATTR_NAME, 2,
189                               size, NULL);
190         if (!req)
191                 GOTO(out, rc = -ENOMEM);
192
193         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
194         memcpy(&body->fid1, fid, sizeof(*fid));
195         body->valid = valid;
196         body->eadatasize = ea_size;
197         mdc_pack_req_body(req);
198
199         LASSERT (strnlen (filename, namelen) == namelen - 1);
200         memcpy(lustre_msg_buf(req->rq_reqmsg, 1, namelen), filename, namelen);
201
202         rc = mdc_getattr_common(exp, ea_size, req);
203         if (rc != 0) {
204                 ptlrpc_req_finished (req);
205                 req = NULL;
206         }
207  out:
208         *request = req;
209         RETURN(rc);
210 }
211
212 /* This should be called with both the request and the reply still packed. */
213 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
214                                 int repoff)
215 {
216         struct mds_rec_create *rec =
217                 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
218         struct mds_body *body =
219                 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
220
221         LASSERT (rec != NULL);
222         LASSERT (body != NULL);
223
224         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
225         DEBUG_REQ(D_HA, req, "storing generation %u for ino "LPU64,
226                   rec->cr_replayfid.generation, rec->cr_replayfid.id);
227 }
228
229 int mdc_req2lustre_md(struct ptlrpc_request *req, int offset,
230                       struct obd_export *exp,
231                       struct lustre_md *md)
232 {
233         int rc = 0;
234         ENTRY;
235
236         LASSERT(md);
237         memset(md, 0, sizeof(*md));
238
239         md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
240         LASSERT (md->body != NULL);
241         LASSERT_REPSWABBED (req, offset);
242
243         if (md->body->valid & OBD_MD_FLEASIZE) {
244                 int lmmsize;
245                 struct lov_mds_md *lmm;
246
247                 LASSERT(S_ISREG(md->body->mode));
248
249                 if (md->body->eadatasize == 0) {
250                         CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
251                         RETURN(-EPROTO);
252                 }
253                 lmmsize = md->body->eadatasize;
254                 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, lmmsize);
255                 LASSERT (lmm != NULL);
256                 LASSERT_REPSWABBED (req, offset + 1);
257
258                 rc = obd_unpackmd(exp, &md->lsm, lmm, lmmsize);
259                 if (rc >= 0) {
260                         LASSERT (rc >= sizeof (*md->lsm));
261                         rc = 0;
262                 }
263         }
264         RETURN(rc);
265 }
266
267 static void mdc_commit_open(struct ptlrpc_request *req)
268 {
269         struct mdc_open_data *mod = req->rq_cb_data;
270         if (mod == NULL)
271                 return;
272
273         if (mod->mod_close_req != NULL)
274                 mod->mod_close_req->rq_cb_data = NULL;
275
276         if (mod->mod_och != NULL)
277                 mod->mod_och->och_mod = NULL;
278
279         OBD_FREE(mod, sizeof(*mod));
280         req->rq_cb_data = NULL;
281 }
282
283 static void mdc_replay_open(struct ptlrpc_request *req)
284 {
285         struct mdc_open_data *mod = req->rq_cb_data;
286         struct obd_client_handle *och;
287         struct ptlrpc_request *close_req;
288         struct lustre_handle old; 
289         struct mds_body *body;
290         ENTRY;
291
292         body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
293         LASSERT (body != NULL);
294
295         if (mod == NULL) {
296                 DEBUG_REQ(D_ERROR, req,
297                           "can't properly replay without open data");
298                 EXIT;
299                 return;
300         }
301
302         och = mod->mod_och;
303         if (och != NULL) {
304                 struct lustre_handle *file_fh; 
305                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
306                 file_fh = &och->och_fh;
307                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
308                        file_fh->cookie, body->handle.cookie);
309                 memcpy(&old, file_fh, sizeof(old));
310                 memcpy(file_fh, &body->handle, sizeof(*file_fh));
311         } 
312
313         close_req = mod->mod_close_req;
314         if (close_req != NULL) {
315                 struct mds_body *close_body;
316                 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
317                 close_body = lustre_msg_buf(close_req->rq_reqmsg, 0,
318                                             sizeof(*close_body));
319                 if (och != NULL)
320                         LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
321                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
322                 memcpy(&close_body->handle, &body->handle,
323                        sizeof(close_body->handle));
324         }
325
326         EXIT;
327 }
328
329 void mdc_set_open_replay_data(struct obd_client_handle *och,
330                               struct ptlrpc_request *open_req)
331 {
332         struct mdc_open_data *mod;
333         struct mds_rec_create *rec =
334                 lustre_msg_buf(open_req->rq_reqmsg, 2, sizeof(*rec));
335         struct mds_body *body =
336                 lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
337
338         LASSERT(rec != NULL);
339         /* outgoing messages always in my byte order */
340         LASSERT(body != NULL);
341         /* incoming message in my byte order (it's been swabbed) */
342         LASSERT_REPSWABBED(open_req, 1);
343
344         OBD_ALLOC(mod, sizeof(*mod));
345         if (mod == NULL) {
346                 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
347                 return;
348         }
349
350         och->och_mod = mod;
351         mod->mod_och = och;
352         mod->mod_open_req = open_req;
353
354         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
355         open_req->rq_replay_cb = mdc_replay_open;
356         open_req->rq_commit_cb = mdc_commit_open;
357         open_req->rq_cb_data = mod;
358         DEBUG_REQ(D_HA, open_req, "set up replay data");
359 }
360
361 void mdc_clear_open_replay_data(struct obd_client_handle *och)
362 {
363         struct mdc_open_data *mod = och->och_mod;
364
365         /* Don't free the structure now (it happens in mdc_commit_open, after
366          * we're sure we won't need to fix up the close request in the future),
367          * but make sure that replay doesn't poke at the och, which is about to
368          * be freed. */
369         LASSERT(mod != (void *)0x5a5a5a5a);
370         if (mod != NULL)
371                 mod->mod_och = NULL;
372         och->och_mod = NULL;
373 }
374
375 static void mdc_commit_close(struct ptlrpc_request *req)
376 {
377         struct mdc_open_data *mod = req->rq_cb_data;
378         struct ptlrpc_request *open_req;
379         struct obd_import *imp = req->rq_import;
380
381         DEBUG_REQ(D_HA, req, "close req committed");
382         if (mod == NULL)
383                 return;
384
385         mod->mod_close_req = NULL;
386         req->rq_cb_data = NULL;
387         req->rq_commit_cb = NULL;
388
389         open_req = mod->mod_open_req;
390         LASSERT(open_req != NULL);
391         LASSERT(open_req != (void *)0x5a5a5a5a);
392
393         DEBUG_REQ(D_HA, open_req, "open req balanced");
394         LASSERT(open_req->rq_transno != 0);
395         LASSERT(open_req->rq_import == imp);
396
397         /* We no longer want to preserve this for transno-unconditional
398          * replay. */
399         spin_lock(&open_req->rq_lock);
400         open_req->rq_replay = 0;
401         spin_unlock(&open_req->rq_lock);
402 }
403
404 static int mdc_close_interpret(struct ptlrpc_request *req, void *data, int rc)
405 {
406         union ptlrpc_async_args *aa = data;
407         struct mdc_rpc_lock *rpc_lock = aa->pointer_arg[0];
408         struct obd_device *obd = aa->pointer_arg[1];
409
410         if (rpc_lock == NULL) {
411                 CERROR("called with NULL rpc_lock\n");
412         } else {
413                 mdc_put_rpc_lock(rpc_lock, NULL);
414                 LASSERTF(req->rq_async_args.pointer_arg[0] ==
415                          obd->u.cli.cl_rpc_lock, "%p != %p\n",
416                          req->rq_async_args.pointer_arg[0],
417                          obd->u.cli.cl_rpc_lock);
418                 aa->pointer_arg[0] = NULL;
419         }
420         wake_up(&req->rq_reply_waitq);
421         RETURN(rc);
422 }
423
424 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
425  * anything but a reply or an error. */
426 static int mdc_close_check_reply(struct ptlrpc_request *req)
427 {
428         int rc = 0;
429         unsigned long flags;
430
431         spin_lock_irqsave(&req->rq_lock, flags);
432         if (PTLRPC_REQUEST_COMPLETE(req)) {
433                 rc = 1;
434         }
435         spin_unlock_irqrestore (&req->rq_lock, flags);
436         return rc;
437 }
438
439 static int go_back_to_sleep(void *unused)
440 {
441         return 0;
442 }
443
444 int mdc_close(struct obd_export *exp, struct obdo *obdo,
445               struct obd_client_handle *och, struct ptlrpc_request **request)
446 {
447         struct mds_body *body;
448         struct obd_device *obd = class_exp2obd(exp);
449         int reqsize = sizeof(*body);
450         int rc, repsize[3] = {sizeof(*body),
451                               obd->u.cli.cl_max_mds_easize,
452                               obd->u.cli.cl_max_mds_cookiesize};
453         struct ptlrpc_request *req;
454         struct mdc_open_data *mod;
455         struct l_wait_info lwi;
456         ENTRY;
457
458         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &reqsize,
459                               NULL);
460         if (req == NULL)
461                 GOTO(out, rc = -ENOMEM);
462
463         /* Ensure that this close's handle is fixed up during replay. */
464         LASSERT(och != NULL);
465         mod = och->och_mod;
466         if (likely(mod != NULL)) {
467                 mod->mod_close_req = req;
468                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
469                           mod->mod_open_req);
470         } else {
471                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
472         }
473
474         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
475         mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
476         memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
477         body->size = obdo->o_size;
478         body->blocks = obdo->o_blocks;
479         body->flags = obdo->o_flags;
480         body->valid = obdo->o_valid;
481
482         req->rq_replen = lustre_msg_size(3, repsize);
483         req->rq_commit_cb = mdc_commit_close;
484         LASSERT(req->rq_cb_data == NULL);
485         req->rq_cb_data = mod;
486
487         /* We hand a ref to the rpcd here, so we need another one of our own. */
488         ptlrpc_request_addref(req);
489
490         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
491         req->rq_interpret_reply = mdc_close_interpret;
492         req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
493         req->rq_async_args.pointer_arg[1] = obd;
494         ptlrpcd_add_req(req);
495         lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
496                                NULL, NULL);
497         rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
498                           &lwi);
499         if (rc == 0) {
500                 rc = req->rq_repmsg->status;
501                 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
502                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
503                                   "= %d", rc);
504                         if (rc > 0)
505                                 rc = -rc;
506                 } else if (mod == NULL) {
507                         CERROR("Unexpected: can't find mdc_open_data, but the "
508                                "close succeeded.  Please tell CFS.\n");
509                 }
510         }
511         if (req->rq_async_args.pointer_arg[0] != NULL) {
512                 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
513                 mdc_close_interpret(req, &req->rq_async_args, rc);
514                 portals_debug_dumplog();
515         }
516
517         EXIT;
518  out:
519         *request = req;
520         return rc;
521 }
522
523 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
524 {
525         struct ptlrpc_request *req;
526         struct mds_body *body;
527         int rc, size = sizeof(*body);
528         ENTRY;
529
530         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_DONE_WRITING, 1,
531                               &size, NULL);
532         if (req == NULL)
533                 RETURN(-ENOMEM);
534
535         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
536         mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
537         body->size = obdo->o_size;
538         body->blocks = obdo->o_blocks;
539         body->flags = obdo->o_flags;
540         body->valid = obdo->o_valid;
541 //        memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
542
543         req->rq_replen = lustre_msg_size(1, &size);
544
545         rc = ptlrpc_queue_wait(req);
546         ptlrpc_req_finished(req);
547         RETURN(rc);
548 }
549
550 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid, __u64 offset,
551                  struct page *page, struct ptlrpc_request **request)
552 {
553         struct obd_import *imp = class_exp2cliimp(exp);
554         struct ptlrpc_request *req = NULL;
555         struct ptlrpc_bulk_desc *desc = NULL;
556         struct mds_body *body;
557         int rc, size = sizeof(*body);
558         ENTRY;
559
560         CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
561
562         req = ptlrpc_prep_req(imp, MDS_READPAGE, 1, &size, NULL);
563         if (req == NULL)
564                 GOTO(out, rc = -ENOMEM);
565         /* XXX FIXME bug 249 */
566         req->rq_request_portal = MDS_READPAGE_PORTAL;
567
568         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
569         if (desc == NULL)
570                 GOTO(out, rc = -ENOMEM);
571         /* NB req now owns desc and will free it when it gets freed */
572
573         ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
574
575         mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE, mdc_fid);
576
577         req->rq_replen = lustre_msg_size(1, &size);
578         rc = ptlrpc_queue_wait(req);
579
580         if (rc == 0) {
581                 body = lustre_swab_repbuf(req, 0, sizeof (*body),
582                                           lustre_swab_mds_body);
583                 if (body == NULL) {
584                         CERROR("Can't unpack mds_body\n");
585                         GOTO(out, rc = -EPROTO);
586                 }
587
588                 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
589                         CERROR ("Unexpected # bytes transferred: %d"
590                                 " (%ld expected)\n",
591                                 req->rq_bulk->bd_nob_transferred,
592                                 PAGE_CACHE_SIZE);
593                         GOTO (out, rc = -EPROTO);
594                 }
595         }
596
597         EXIT;
598  out:
599         *request = req;
600         return rc;
601 }
602
603 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
604                          void *karg, void *uarg)
605 {
606         struct obd_device *obd = exp->exp_obd;
607         struct obd_ioctl_data *data = karg;
608         struct obd_import *imp = obd->u.cli.cl_import;
609         struct llog_ctxt *ctxt;
610         int rc;
611         ENTRY;
612         
613         MOD_INC_USE_COUNT;
614
615         switch (cmd) {
616         case OBD_IOC_CLIENT_RECOVER:
617                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
618                 if (rc < 0)
619                         GOTO(out, rc);
620                 GOTO(out, rc = 0);
621         case IOC_OSC_SET_ACTIVE:
622                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
623                 GOTO(out, rc);
624         case OBD_IOC_PARSE: {
625                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
626                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
627                 GOTO(out, rc);
628         }
629 #ifdef __KERNEL__
630         case OBD_IOC_LLOG_INFO:
631         case OBD_IOC_LLOG_PRINT: {
632                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
633                 rc = llog_ioctl(ctxt, cmd, data);
634                 
635                 GOTO(out, rc);
636         }
637 #endif
638         default:
639                 CERROR("osc_ioctl(): unrecognised ioctl %#x\n", cmd);
640                 GOTO(out, rc = -ENOTTY);
641         }
642 out:
643         MOD_DEC_USE_COUNT;
644         return rc;
645 }
646
647 int mdc_set_info(struct obd_export *exp, obd_count keylen,
648                  void *key, obd_count vallen, void *val)
649 {
650         int rc = -EINVAL;
651
652         if (keylen == strlen("initial_recov") &&
653             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
654                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
655                 if (vallen != sizeof(int))
656                         RETURN(-EINVAL);
657                 imp->imp_initial_recov = *(int *)val;
658                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
659                        exp->exp_obd->obd_name,
660                        imp->imp_initial_recov);
661                 RETURN(0);
662         }
663         
664         RETURN(rc);
665 }
666
667 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
668                       unsigned long max_age)
669 {
670         struct ptlrpc_request *req;
671         struct obd_statfs *msfs;
672         int rc, size = sizeof(*msfs);
673         ENTRY;
674
675         /* We could possibly pass max_age in the request (as an absolute
676          * timestamp or a "seconds.usec ago") so the target can avoid doing
677          * extra calls into the filesystem if that isn't necessary (e.g.
678          * during mount that would help a bit).  Having relative timestamps
679          * is not so great if request processing is slow, while absolute
680          * timestamps are not ideal because they need time synchronization. */
681         req = ptlrpc_prep_req(obd->u.cli.cl_import, MDS_STATFS, 0, NULL, NULL);
682         if (!req)
683                 RETURN(-ENOMEM);
684
685         req->rq_replen = lustre_msg_size(1, &size);
686
687         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
688         rc = ptlrpc_queue_wait(req);
689         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
690
691         if (rc)
692                 GOTO(out, rc);
693
694         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
695         if (msfs == NULL) {
696                 CERROR("Can't unpack obd_statfs\n");
697                 GOTO(out, rc = -EPROTO);
698         }
699
700         memcpy(osfs, msfs, sizeof (*msfs));
701         EXIT;
702 out:
703         ptlrpc_req_finished(req);
704
705         return rc;
706 }
707
708 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
709                    struct obd_client_handle *handle, int flag)
710 {
711         struct ptlrpc_request *req;
712         struct mds_body *body;
713         int rc, size = sizeof(*body);
714         ENTRY;
715
716         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_PIN, 1, &size, NULL);
717         if (req == NULL)
718                 RETURN(-ENOMEM);
719
720         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
721         mdc_pack_fid(&body->fid1, ino, gen, type);
722         body->flags = flag;
723
724         req->rq_replen = lustre_msg_size(1, &size);
725
726         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
727         rc = ptlrpc_queue_wait(req);
728         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
729         if (rc) {
730                 CERROR("pin failed: %d\n", rc);
731                 ptlrpc_req_finished(req);
732                 RETURN(rc);
733         }
734
735         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
736         if (body == NULL) {
737                 ptlrpc_req_finished(req);
738                 RETURN(rc);
739         }
740
741         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
742         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
743
744         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
745         if (handle->och_mod == NULL) {
746                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
747                 RETURN(-ENOMEM);
748         }
749         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
750
751         RETURN(rc);
752 }
753
754 static int mdc_unpin(struct obd_export *exp,
755                      struct obd_client_handle *handle, int flag)
756 {
757         struct ptlrpc_request *req;
758         struct mds_body *body;
759         int rc, size = sizeof(*body);
760         ENTRY;
761
762         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
763                 RETURN(0);
764
765         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &size, NULL);
766         if (req == NULL)
767                 RETURN(-ENOMEM);
768
769         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
770         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
771         body->flags = flag;
772
773         req->rq_replen = lustre_msg_size(0, NULL);
774         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
775         rc = ptlrpc_queue_wait(req);
776         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
777
778         if (rc != 0)
779                 CERROR("unpin failed: %d\n", rc);
780
781         ptlrpc_req_finished(req);
782         ptlrpc_req_finished(handle->och_mod->mod_open_req);
783         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
784         RETURN(rc);
785 }
786
787 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
788              struct ptlrpc_request **request)
789 {
790         struct ptlrpc_request *req;
791         struct mds_body *body;
792         int size = sizeof(*body);
793         int rc;
794         ENTRY;
795
796         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_SYNC, 1,&size,NULL);
797         if (!req)
798                 RETURN(rc = -ENOMEM);
799
800         if (fid) {
801                 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
802                 memcpy(&body->fid1, fid, sizeof(*fid));
803                 mdc_pack_req_body(req);
804         }
805
806         req->rq_replen = lustre_msg_size(1, &size);
807
808         rc = ptlrpc_queue_wait(req);
809         if (rc || request == NULL)
810                 ptlrpc_req_finished(req);
811         else
812                 *request = req;
813
814         RETURN(rc);
815 }
816
817 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
818 {
819         struct lprocfs_static_vars lvars;
820
821         lprocfs_init_vars(mdc, &lvars);
822         return lprocfs_obd_attach(dev, lvars.obd_vars);
823 }
824
825 static int mdc_detach(struct obd_device *dev)
826 {
827         return lprocfs_obd_detach(dev);
828 }
829
830 static int mdc_import_event(struct obd_device *obd,
831                             struct obd_import *imp, 
832                             enum obd_import_event event)
833 {
834         int rc = 0;
835
836         LASSERT(imp->imp_obd == obd);
837
838         switch (event) {
839         case IMP_EVENT_DISCON: {
840                 break;
841         }
842         case IMP_EVENT_INVALIDATE: {
843                 struct ldlm_namespace *ns = obd->obd_namespace;
844                 
845                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
846
847                 if (obd->obd_observer)
848                         rc = obd_notify(obd->obd_observer, obd, 0);
849                 break;
850         }
851         case IMP_EVENT_ACTIVE: {
852                 if (obd->obd_observer)
853                         rc = obd_notify(obd->obd_observer, obd, 1);
854                 break;
855         }
856         default:
857                 CERROR("Unknown import event %d\n", event);
858                 LBUG();
859         }
860         RETURN(rc);
861 }
862
863 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
864 {
865         struct client_obd *cli = &obd->u.cli;
866         int rc;
867         ENTRY;
868
869         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
870         if (!cli->cl_rpc_lock)
871                 RETURN(-ENOMEM);
872         mdc_init_rpc_lock(cli->cl_rpc_lock);
873
874         ptlrpcd_addref();
875
876         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
877         if (!cli->cl_setattr_lock)
878                 GOTO(out_free_rpc, rc = -ENOMEM);
879         mdc_init_rpc_lock(cli->cl_setattr_lock);
880
881         rc = client_obd_setup(obd, len, buf);
882         if (rc) {
883                 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
884  out_free_rpc:
885                 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
886         }
887
888         rc = obd_llog_init(obd, obd, 0, NULL);
889         if (rc) {
890                 mdc_cleanup(obd, 0);
891                 CERROR("failed to setup llogging subsystems\n");
892         }
893
894         RETURN(rc);
895 }
896
897 int mdc_init_ea_size(struct obd_device *obd, char *lov_name)
898 {
899         struct client_obd *cli = &obd->u.cli;
900         struct obd_device *lov_obd;
901         struct obd_export *exp;
902         struct lustre_handle conn;
903         struct lov_desc desc;
904         int valsize;
905         int rc;
906
907         lov_obd = class_name2obd(lov_name);
908         if (!lov_obd) {
909                 CERROR("MDC cannot locate LOV %s!\n", lov_name);
910                 RETURN(-ENOTCONN);
911         }
912
913         rc = obd_connect(&conn, lov_obd, &obd->obd_uuid);
914         if (rc) {
915                 CERROR("MDC failed connect to LOV %s (%d)\n", lov_name, rc);
916                 RETURN(rc);
917         }
918         exp = class_conn2export(&conn);
919
920         valsize = sizeof(desc);
921         rc = obd_get_info(exp, strlen("lovdesc") + 1, "lovdesc",
922                           &valsize, &desc);
923         if (rc == 0) {
924                 cli->cl_max_mds_easize = obd_size_diskmd(exp, NULL);
925                 cli->cl_max_mds_cookiesize = desc.ld_tgt_count *
926                         sizeof(struct llog_cookie);
927         }
928         obd_disconnect(exp, 0);
929         RETURN(rc);
930 }
931
932 static int mdc_precleanup(struct obd_device *obd, int flags)
933 {
934         int rc = 0;
935         
936         rc = obd_llog_finish(obd, 0);
937         if (rc != 0)
938                 CERROR("failed to cleanup llogging subsystems\n");
939
940         RETURN(rc);
941 }
942
943 static int mdc_cleanup(struct obd_device *obd, int flags)
944 {
945         struct client_obd *cli = &obd->u.cli;
946  
947         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
948         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
949
950         ptlrpcd_decref();
951
952         return client_obd_cleanup(obd, flags);
953 }
954
955
956 static int mdc_llog_init(struct obd_device *obd, struct obd_device *tgt,
957                          int count, struct llog_catid *logid)
958 {
959         struct llog_ctxt *ctxt;
960         int rc;
961         ENTRY;
962
963         rc = llog_setup(obd, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
964                         &llog_client_ops);
965         if (rc == 0) {
966                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
967                 ctxt->loc_imp = obd->u.cli.cl_import;
968         }
969
970         RETURN(rc);
971 }
972
973 static int mdc_llog_finish(struct obd_device *obd, int count)
974 {
975         int rc;
976         ENTRY;
977
978         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
979         RETURN(rc);
980 }
981
982 struct obd_ops mdc_obd_ops = {
983         o_owner:       THIS_MODULE,
984         o_attach:      mdc_attach,
985         o_detach:      mdc_detach,
986         o_setup:       mdc_setup,
987         o_precleanup:  mdc_precleanup,
988         o_cleanup:     mdc_cleanup,
989         o_connect:     client_connect_import,
990         o_disconnect:  client_disconnect_export,
991         o_iocontrol:   mdc_iocontrol,
992         o_set_info:    mdc_set_info,
993         o_statfs:      mdc_statfs,
994         o_pin:         mdc_pin,
995         o_unpin:       mdc_unpin,
996         o_import_event: mdc_import_event,
997         o_llog_init:   mdc_llog_init,
998         o_llog_finish: mdc_llog_finish,
999 };
1000
1001 int __init mdc_init(void)
1002 {
1003         struct lprocfs_static_vars lvars;
1004         lprocfs_init_vars(mdc, &lvars);
1005         return class_register_type(&mdc_obd_ops, lvars.module_vars,
1006                                    LUSTRE_MDC_NAME);
1007 }
1008
1009 static void /*__exit*/ mdc_exit(void)
1010 {
1011         class_unregister_type(LUSTRE_MDC_NAME);
1012 }
1013
1014 #ifdef __KERNEL__
1015 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1016 MODULE_DESCRIPTION("Lustre Metadata Client");
1017 MODULE_LICENSE("GPL");
1018
1019 EXPORT_SYMBOL(mdc_req2lustre_md);
1020 EXPORT_SYMBOL(mdc_change_cbdata);
1021 EXPORT_SYMBOL(mdc_getstatus);
1022 EXPORT_SYMBOL(mdc_getattr);
1023 EXPORT_SYMBOL(mdc_getattr_name);
1024 EXPORT_SYMBOL(mdc_create);
1025 EXPORT_SYMBOL(mdc_unlink);
1026 EXPORT_SYMBOL(mdc_rename);
1027 EXPORT_SYMBOL(mdc_link);
1028 EXPORT_SYMBOL(mdc_readpage);
1029 EXPORT_SYMBOL(mdc_setattr);
1030 EXPORT_SYMBOL(mdc_close);
1031 EXPORT_SYMBOL(mdc_done_writing);
1032 EXPORT_SYMBOL(mdc_sync);
1033 EXPORT_SYMBOL(mdc_set_open_replay_data);
1034 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1035 EXPORT_SYMBOL(mdc_store_inode_generation);
1036 EXPORT_SYMBOL(mdc_init_ea_size);
1037
1038 module_init(mdc_init);
1039 module_exit(mdc_exit);
1040 #endif