Whamcloud - gitweb
Update smfs: fix bugs of smfs_create
[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         
409         mdc_put_rpc_lock(rpc_lock, NULL);
410         wake_up(&req->rq_reply_waitq);
411         RETURN(rc);
412 }
413
414 /* We can't use ptlrpc_check_reply, because we don't want to wake up for 
415  * anything but a reply or an error. */
416 static int mdc_close_check_reply(struct ptlrpc_request *req)
417 {
418         int rc = 0;
419         unsigned long flags;
420
421         spin_lock_irqsave(&req->rq_lock, flags);
422         if (PTLRPC_REQUEST_COMPLETE(req)) {
423                 rc = 1;
424         }
425         spin_unlock_irqrestore (&req->rq_lock, flags);
426         return rc;
427 }
428
429 static int go_back_to_sleep(void *unused)
430 {
431         return 0;
432 }
433
434 int mdc_close(struct obd_export *exp, struct obdo *obdo,
435               struct obd_client_handle *och, struct ptlrpc_request **request)
436 {
437         struct mds_body *body;
438         struct obd_device *obd = class_exp2obd(exp);
439         int reqsize = sizeof(*body);
440         int rc, repsize[3] = {sizeof(*body),
441                               obd->u.cli.cl_max_mds_easize,
442                               obd->u.cli.cl_max_mds_cookiesize};
443         struct ptlrpc_request *req;
444         struct mdc_open_data *mod;
445         struct l_wait_info lwi;
446         struct mdc_rpc_lock *rpc_lock = obd->u.cli.cl_rpc_lock;
447         ENTRY;
448
449         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &reqsize,
450                               NULL);
451         if (req == NULL)
452                 GOTO(out, rc = -ENOMEM);
453
454         /* Ensure that this close's handle is fixed up during replay. */
455         LASSERT(och != NULL);
456         mod = och->och_mod;
457         if (likely(mod != NULL)) {
458                 mod->mod_close_req = req;
459                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
460                           mod->mod_open_req);
461         } else {
462                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
463         }
464
465         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
466         mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
467         memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
468         body->size = obdo->o_size;
469         body->blocks = obdo->o_blocks;
470         body->flags = obdo->o_flags;
471         body->valid = obdo->o_valid;
472
473         req->rq_replen = lustre_msg_size(3, repsize);
474         req->rq_commit_cb = mdc_commit_close;
475         LASSERT(req->rq_cb_data == NULL);
476         req->rq_cb_data = mod;
477
478         /* We hand a ref to the rpcd here, so we need another one of our own. */
479         ptlrpc_request_addref(req);
480
481         mdc_get_rpc_lock(rpc_lock, NULL);
482         req->rq_interpret_reply = mdc_close_interpret;
483         req->rq_async_args.pointer_arg[0] = rpc_lock;
484         ptlrpcd_add_req(req);
485         lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
486                                NULL, NULL);
487         rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
488                           &lwi);
489         
490          if (rc == 0) {
491                 rc = req->rq_repmsg->status;
492                 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
493                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err = %d", rc);
494                         if (rc > 0)
495                                 rc = -rc;
496                 } else if (mod == NULL) {
497                         CERROR("Unexpected: can't find mdc_open_data, but the close "
498                                "succeeded.  Please tell CFS.\n");
499                 }
500         }
501
502         EXIT;
503  out:
504         *request = req;
505         return rc;
506 }
507
508 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
509 {
510         struct ptlrpc_request *req;
511         struct mds_body *body;
512         int rc, size = sizeof(*body);
513         ENTRY;
514
515         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_DONE_WRITING, 1,
516                               &size, NULL);
517         if (req == NULL)
518                 RETURN(-ENOMEM);
519
520         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
521         mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
522         body->size = obdo->o_size;
523         body->blocks = obdo->o_blocks;
524         body->flags = obdo->o_flags;
525         body->valid = obdo->o_valid;
526 //        memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
527
528         req->rq_replen = lustre_msg_size(1, &size);
529
530         rc = ptlrpc_queue_wait(req);
531         ptlrpc_req_finished(req);
532         RETURN(rc);
533 }
534
535 int mdc_readpage(struct obd_export *exp, struct ll_fid *mdc_fid, __u64 offset,
536                  struct page *page, struct ptlrpc_request **request)
537 {
538         struct obd_import *imp = class_exp2cliimp(exp);
539         struct ptlrpc_request *req = NULL;
540         struct ptlrpc_bulk_desc *desc = NULL;
541         struct mds_body *body;
542         int rc, size = sizeof(*body);
543         ENTRY;
544
545         CDEBUG(D_INODE, "inode: %ld\n", (long)mdc_fid->id);
546
547         req = ptlrpc_prep_req(imp, MDS_READPAGE, 1, &size, NULL);
548         if (req == NULL)
549                 GOTO(out, rc = -ENOMEM);
550         /* XXX FIXME bug 249 */
551         req->rq_request_portal = MDS_READPAGE_PORTAL;
552
553         desc = ptlrpc_prep_bulk_imp(req, BULK_PUT_SINK, MDS_BULK_PORTAL);
554         if (desc == NULL)
555                 GOTO(out, rc = -ENOMEM);
556         /* NB req now owns desc and will free it when it gets freed */
557
558         rc = ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
559         if (rc != 0)
560                 GOTO(out, rc);
561
562         mdc_readdir_pack(req, offset, PAGE_CACHE_SIZE, mdc_fid);
563
564         req->rq_replen = lustre_msg_size(1, &size);
565         rc = ptlrpc_queue_wait(req);
566
567         if (rc == 0) {
568                 LASSERT(desc->bd_page_count == 1);
569                 body = lustre_swab_repbuf(req, 0, sizeof(*body),
570                                           lustre_swab_mds_body);
571                 if (body == NULL) {
572                         CERROR("Can't unpack mds_body\n");
573                         GOTO(out, rc = -EPROTO);
574                 }
575         }
576
577         EXIT;
578  out:
579         *request = req;
580         return rc;
581 }
582
583 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
584                          void *karg, void *uarg)
585 {
586         struct obd_device *obd = exp->exp_obd;
587         struct obd_ioctl_data *data = karg;
588         struct obd_import *imp = obd->u.cli.cl_import;
589         struct llog_ctxt *ctxt;
590         int rc;
591         ENTRY;
592         
593         MOD_INC_USE_COUNT;
594
595         switch (cmd) {
596         case OBD_IOC_CLIENT_RECOVER:
597                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
598                 if (rc < 0)
599                         GOTO(out, rc);
600                 GOTO(out, rc = 0);
601         case IOC_OSC_SET_ACTIVE:
602                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
603                 GOTO(out, rc);
604         case OBD_IOC_PARSE: {
605                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
606                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
607                 GOTO(out, rc);
608         }
609 #ifdef __KERNEL__
610         case OBD_IOC_LLOG_INFO:
611         case OBD_IOC_LLOG_PRINT: {
612                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
613                 rc = llog_ioctl(ctxt, cmd, data);
614                 
615                 GOTO(out, rc);
616         }
617 #endif
618         default:
619                 CERROR("osc_ioctl(): unrecognised ioctl %#x\n", cmd);
620                 GOTO(out, rc = -ENOTTY);
621         }
622 out:
623         MOD_DEC_USE_COUNT;
624         return rc;
625 }
626
627 int mdc_set_info(struct obd_export *exp, obd_count keylen,
628                  void *key, obd_count vallen, void *val)
629 {
630         int rc = -EINVAL;
631
632         if (keylen == strlen("initial_recov") &&
633             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
634                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
635                 if (vallen != sizeof(int))
636                         RETURN(-EINVAL);
637                 imp->imp_initial_recov = *(int *)val;
638                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
639                        exp->exp_obd->obd_name,
640                        imp->imp_initial_recov);
641                 RETURN(0);
642         }
643         
644         RETURN(rc);
645 }
646
647 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
648                       unsigned long max_age)
649 {
650         struct ptlrpc_request *req;
651         struct obd_statfs *msfs;
652         int rc, size = sizeof(*msfs);
653         ENTRY;
654
655         /* We could possibly pass max_age in the request (as an absolute
656          * timestamp or a "seconds.usec ago") so the target can avoid doing
657          * extra calls into the filesystem if that isn't necessary (e.g.
658          * during mount that would help a bit).  Having relative timestamps
659          * is not so great if request processing is slow, while absolute
660          * timestamps are not ideal because they need time synchronization. */
661         req = ptlrpc_prep_req(obd->u.cli.cl_import, MDS_STATFS, 0, NULL, NULL);
662         if (!req)
663                 RETURN(-ENOMEM);
664
665         req->rq_replen = lustre_msg_size(1, &size);
666
667         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
668         rc = ptlrpc_queue_wait(req);
669         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
670
671         if (rc)
672                 GOTO(out, rc);
673
674         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),lustre_swab_obd_statfs);
675         if (msfs == NULL) {
676                 CERROR("Can't unpack obd_statfs\n");
677                 GOTO(out, rc = -EPROTO);
678         }
679
680         memcpy(osfs, msfs, sizeof (*msfs));
681         EXIT;
682 out:
683         ptlrpc_req_finished(req);
684
685         return rc;
686 }
687
688 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
689                    struct obd_client_handle *handle, int flag)
690 {
691         struct ptlrpc_request *req;
692         struct mds_body *body;
693         int rc, size = sizeof(*body);
694         ENTRY;
695
696         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_PIN, 1, &size, NULL);
697         if (req == NULL)
698                 RETURN(-ENOMEM);
699
700         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
701         mdc_pack_fid(&body->fid1, ino, gen, type);
702         body->flags = flag;
703
704         req->rq_replen = lustre_msg_size(1, &size);
705
706         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
707         rc = ptlrpc_queue_wait(req);
708         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
709         if (rc) {
710                 CERROR("pin failed: %d\n", rc);
711                 ptlrpc_req_finished(req);
712                 RETURN(rc);
713         }
714
715         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
716         if (body == NULL) {
717                 ptlrpc_req_finished(req);
718                 RETURN(rc);
719         }
720
721         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
722         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
723
724         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
725         if (handle->och_mod == NULL) {
726                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
727                 RETURN(-ENOMEM);
728         }
729         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
730
731         RETURN(rc);
732 }
733
734 static int mdc_unpin(struct obd_export *exp,
735                      struct obd_client_handle *handle, int flag)
736 {
737         struct ptlrpc_request *req;
738         struct mds_body *body;
739         int rc, size = sizeof(*body);
740         ENTRY;
741
742         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
743                 RETURN(0);
744
745         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_CLOSE, 1, &size, NULL);
746         if (req == NULL)
747                 RETURN(-ENOMEM);
748
749         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
750         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
751         body->flags = flag;
752
753         req->rq_replen = lustre_msg_size(0, NULL);
754         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
755         rc = ptlrpc_queue_wait(req);
756         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
757
758         if (rc != 0)
759                 CERROR("unpin failed: %d\n", rc);
760
761         ptlrpc_req_finished(req);
762         ptlrpc_req_finished(handle->och_mod->mod_open_req);
763         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
764         RETURN(rc);
765 }
766
767 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
768              struct ptlrpc_request **request)
769 {
770         struct ptlrpc_request *req;
771         struct mds_body *body;
772         int size = sizeof(*body);
773         int rc;
774         ENTRY;
775
776         req = ptlrpc_prep_req(class_exp2cliimp(exp), MDS_SYNC, 1,&size,NULL);
777         if (!req)
778                 RETURN(rc = -ENOMEM);
779
780         if (fid) {
781                 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
782                 memcpy(&body->fid1, fid, sizeof(*fid));
783                 mdc_pack_req_body(req);
784         }
785
786         req->rq_replen = lustre_msg_size(1, &size);
787
788         rc = ptlrpc_queue_wait(req);
789         if (rc || request == NULL)
790                 ptlrpc_req_finished(req);
791         else
792                 *request = req;
793
794         RETURN(rc);
795 }
796
797 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
798 {
799         struct lprocfs_static_vars lvars;
800
801         lprocfs_init_vars(mdc, &lvars);
802         return lprocfs_obd_attach(dev, lvars.obd_vars);
803 }
804
805 static int mdc_detach(struct obd_device *dev)
806 {
807         return lprocfs_obd_detach(dev);
808 }
809
810 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
811 {
812         struct client_obd *cli = &obd->u.cli;
813         int rc;
814         ENTRY;
815
816         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
817         if (!cli->cl_rpc_lock)
818                 RETURN(-ENOMEM);
819         mdc_init_rpc_lock(cli->cl_rpc_lock);
820
821         ptlrpcd_addref();
822
823         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
824         if (!cli->cl_setattr_lock)
825                 GOTO(out_free_rpc, rc = -ENOMEM);
826         mdc_init_rpc_lock(cli->cl_setattr_lock);
827
828         rc = client_obd_setup(obd, len, buf);
829         if (rc) {
830                 OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
831  out_free_rpc:
832                 OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
833         }
834
835         rc = obd_llog_init(obd, obd, 0, NULL);
836         if (rc) {
837                 mdc_cleanup(obd, 0);
838                 CERROR("failed to setup llogging subsystems\n");
839         }
840
841         RETURN(rc);
842 }
843
844 int mdc_init_ea_size(struct obd_device *obd, char *lov_name)
845 {
846         struct client_obd *cli = &obd->u.cli;
847         struct obd_device *lov_obd;
848         struct obd_export *exp;
849         struct lustre_handle conn;
850         struct lov_desc desc;
851         int valsize;
852         int rc;
853
854         lov_obd = class_name2obd(lov_name);
855         if (!lov_obd) {
856                 CERROR("MDC cannot locate LOV %s!\n", lov_name);
857                 RETURN(-ENOTCONN);
858         }
859
860         rc = obd_connect(&conn, lov_obd, &obd->obd_uuid);
861         if (rc) {
862                 CERROR("MDC failed connect to LOV %s (%d)\n", lov_name, rc);
863                 RETURN(rc);
864         }
865         exp = class_conn2export(&conn);
866
867         valsize = sizeof(desc);
868         rc = obd_get_info(exp, strlen("lovdesc") + 1, "lovdesc",
869                           &valsize, &desc);
870         if (rc == 0) {
871                 cli->cl_max_mds_easize = obd_size_diskmd(exp, NULL);
872                 cli->cl_max_mds_cookiesize = desc.ld_tgt_count *
873                         sizeof(struct llog_cookie);
874         }
875         obd_disconnect(exp, 0);
876         RETURN(rc);
877 }
878
879 static int mdc_precleanup(struct obd_device *obd, int flags)
880 {
881         int rc = 0;
882         
883         rc = obd_llog_finish(obd, 0);
884         if (rc != 0)
885                 CERROR("failed to cleanup llogging subsystems\n");
886
887         RETURN(rc);
888 }
889
890 static int mdc_cleanup(struct obd_device *obd, int flags)
891 {
892         struct client_obd *cli = &obd->u.cli;
893  
894         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
895         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
896
897         ptlrpcd_decref();
898
899         return client_obd_cleanup(obd, flags);
900 }
901
902
903 static int mdc_llog_init(struct obd_device *obd, struct obd_device *tgt,
904                          int count, struct llog_logid *logid)
905 {
906         struct llog_ctxt *ctxt;
907         int rc;
908         ENTRY;
909
910         rc = llog_setup(obd, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
911                         &llog_client_ops);
912         if (rc == 0) {
913                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
914                 ctxt->loc_imp = obd->u.cli.cl_import; 
915         }
916
917         RETURN(rc);
918 }
919
920 static int mdc_llog_finish(struct obd_device *obd, int count)
921 {
922         int rc;
923         ENTRY;
924
925         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
926         RETURN(rc);
927 }
928
929 struct obd_ops mdc_obd_ops = {
930         o_owner:       THIS_MODULE,
931         o_attach:      mdc_attach,
932         o_detach:      mdc_detach,
933         o_setup:       mdc_setup,
934         o_precleanup:  mdc_precleanup,
935         o_cleanup:     mdc_cleanup,
936         o_connect:     client_connect_import,
937         o_disconnect:  client_disconnect_export,
938         o_iocontrol:   mdc_iocontrol,
939         o_set_info:    mdc_set_info,
940         o_statfs:      mdc_statfs,
941         o_pin:         mdc_pin,
942         o_unpin:       mdc_unpin,
943         o_llog_init:   mdc_llog_init,
944         o_llog_finish: mdc_llog_finish,
945 };
946
947 int __init mdc_init(void)
948 {
949         struct lprocfs_static_vars lvars;
950         lprocfs_init_vars(mdc, &lvars);
951         return class_register_type(&mdc_obd_ops, lvars.module_vars,
952                                    LUSTRE_MDC_NAME);
953 }
954
955 static void /*__exit*/ mdc_exit(void)
956 {
957         class_unregister_type(LUSTRE_MDC_NAME);
958 }
959
960 #ifdef __KERNEL__
961 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
962 MODULE_DESCRIPTION("Lustre Metadata Client");
963 MODULE_LICENSE("GPL");
964
965 EXPORT_SYMBOL(mdc_req2lustre_md);
966 EXPORT_SYMBOL(mdc_change_cbdata);
967 EXPORT_SYMBOL(mdc_getstatus);
968 EXPORT_SYMBOL(mdc_getattr);
969 EXPORT_SYMBOL(mdc_getattr_name);
970 EXPORT_SYMBOL(mdc_create);
971 EXPORT_SYMBOL(mdc_unlink);
972 EXPORT_SYMBOL(mdc_rename);
973 EXPORT_SYMBOL(mdc_link);
974 EXPORT_SYMBOL(mdc_readpage);
975 EXPORT_SYMBOL(mdc_setattr);
976 EXPORT_SYMBOL(mdc_close);
977 EXPORT_SYMBOL(mdc_done_writing);
978 EXPORT_SYMBOL(mdc_sync);
979 EXPORT_SYMBOL(mdc_set_open_replay_data);
980 EXPORT_SYMBOL(mdc_clear_open_replay_data);
981 EXPORT_SYMBOL(mdc_store_inode_generation);
982 EXPORT_SYMBOL(mdc_init_ea_size);
983
984 module_init(mdc_init);
985 module_exit(mdc_exit);
986 #endif