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