Whamcloud - gitweb
b=5765
[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-2004 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 int mdc_get_secdesc_size(void)
47 {
48 #ifdef __KERNEL__
49         int ngroups = current_ngroups;
50
51         if (ngroups > LUSTRE_MAX_GROUPS)
52                 ngroups = LUSTRE_MAX_GROUPS;
53
54         return sizeof(struct mds_req_sec_desc) +
55                 sizeof(__u32) * ngroups;
56 #else
57         return 0;
58 #endif
59 }
60
61 /*
62  * because group info might have changed since last time we call
63  * get_secdesc_size(), so here we did more sanity check to prevent garbage gids
64  */
65 void mdc_pack_secdesc(struct ptlrpc_request *req, int size)
66 {
67 #ifdef __KERNEL__
68         struct mds_req_sec_desc *rsd;
69         
70 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
71         struct group_info *ginfo;
72 #endif
73
74         rsd = lustre_msg_buf(req->rq_reqmsg,
75                              MDS_REQ_SECDESC_OFF, size);
76         
77         rsd->rsd_uid = current->uid;
78         rsd->rsd_gid = current->gid;
79         rsd->rsd_fsuid = current->fsuid;
80         rsd->rsd_fsgid = current->fsgid;
81         rsd->rsd_cap = current->cap_effective;
82         rsd->rsd_ngroups = (size - sizeof(*rsd)) / sizeof(__u32);
83         LASSERT(rsd->rsd_ngroups <= LUSTRE_MAX_GROUPS);
84
85 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
86         task_lock(current);
87         get_group_info(current->group_info);
88         ginfo = current->group_info;
89         task_unlock(current);
90         if (rsd->rsd_ngroups > ginfo->ngroups)
91                 rsd->rsd_ngroups = ginfo->ngroups;
92         memcpy(rsd->rsd_groups, ginfo->blocks[0],
93                rsd->rsd_ngroups * sizeof(__u32));
94 #else
95         LASSERT(rsd->rsd_ngroups <= NGROUPS);
96         if (rsd->rsd_ngroups > current->ngroups)
97                 rsd->rsd_ngroups = current->ngroups;
98         memcpy(rsd->rsd_groups, current->groups,
99                rsd->rsd_ngroups * sizeof(__u32));
100 #endif
101 #endif
102 }
103
104 extern int mds_queue_req(struct ptlrpc_request *);
105 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
106 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
107 static int send_getstatus(struct obd_import *imp, struct lustre_id *rootid,
108                           int level, int msg_flags)
109 {
110         struct ptlrpc_request *req;
111         struct mds_body *body;
112         int rc, size[2] = {0, sizeof(*body)};
113         ENTRY;
114
115         //size[0] = mdc_get_secdesc_size();
116
117         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_GETSTATUS,
118                               2, size, NULL);
119         if (!req)
120                 GOTO(out, rc = -ENOMEM);
121
122         //mdc_pack_secdesc(req, size[0]);
123
124         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
125         req->rq_send_state = level;
126         req->rq_replen = lustre_msg_size(1, &size[1]);
127
128         req->rq_reqmsg->flags |= msg_flags;
129         rc = ptlrpc_queue_wait(req);
130
131         if (!rc) {
132                 body = lustre_swab_repbuf (req, 0, sizeof (*body),
133                                            lustre_swab_mds_body);
134                 if (body == NULL) {
135                         CERROR ("Can't extract mds_body\n");
136                         GOTO (out, rc = -EPROTO);
137                 }
138
139                 memcpy(rootid, &body->id1, sizeof(*rootid));
140
141                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
142                        ", last_xid="LPU64"\n", rootid->li_stc.u.e3s.l3s_ino,
143                        req->rq_repmsg->last_committed, req->rq_repmsg->last_xid);
144         }
145
146         EXIT;
147  out:
148         ptlrpc_req_finished(req);
149         return rc;
150 }
151
152 /* This should be mdc_get_info("rootid") */
153 int mdc_getstatus(struct obd_export *exp, struct lustre_id *rootid)
154 {
155         return send_getstatus(class_exp2cliimp(exp), rootid,
156                               LUSTRE_IMP_FULL, 0);
157 }
158
159 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size,
160                        struct ptlrpc_request *req)
161 {
162         struct mds_body *body;
163         void            *eadata;
164         int              rc;
165         int              repsize[2] = {sizeof(*body), 0};
166         int              bufcount = 1;
167         ENTRY;
168
169         /* request message already built */
170
171         if (ea_size != 0) {
172                 repsize[bufcount++] = ea_size;
173                 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
174                        ea_size);
175         }
176         req->rq_replen = lustre_msg_size(bufcount, repsize);
177
178         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
179         rc = ptlrpc_queue_wait(req);
180         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
181         if (rc != 0)
182                 RETURN (rc);
183
184         body = lustre_swab_repbuf (req, 0, sizeof (*body),
185                                    lustre_swab_mds_body);
186         if (body == NULL) {
187                 CERROR ("Can't unpack mds_body\n");
188                 RETURN (-EPROTO);
189         }
190
191         CDEBUG(D_NET, "mode: %o\n", body->mode);
192
193         LASSERT_REPSWAB (req, 1);
194         if (body->eadatasize != 0) {
195                 /* reply indicates presence of eadata; check it's there... */
196                 eadata = lustre_msg_buf (req->rq_repmsg, 1, body->eadatasize);
197                 if (eadata == NULL) {
198                         CERROR ("Missing/short eadata\n");
199                         RETURN (-EPROTO);
200                 }
201         }
202
203         RETURN (0);
204 }
205
206 int mdc_getattr(struct obd_export *exp, struct lustre_id *id,
207                 __u64 valid, unsigned int ea_size,
208                 struct ptlrpc_request **request)
209 {
210         struct ptlrpc_request *req;
211         struct mds_body *body;
212         int size[2] = {0, sizeof(*body)};
213         int rc;
214         ENTRY;
215
216         /* XXX do we need to make another request here?  We just did a getattr
217          *     to do the lookup in the first place.
218          */
219         size[0] = mdc_get_secdesc_size();
220
221         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
222                               MDS_GETATTR, 2, size, NULL);
223         if (!req)
224                 GOTO(out, rc = -ENOMEM);
225
226         mdc_pack_secdesc(req, size[0]);
227
228         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
229         memcpy(&body->id1, id, sizeof(*id));
230         body->valid = valid;
231         body->eadatasize = ea_size;
232
233         rc = mdc_getattr_common(exp, ea_size, req);
234         if (rc != 0) {
235                 ptlrpc_req_finished (req);
236                 req = NULL;
237         }
238  out:
239         *request = req;
240         RETURN (rc);
241 }
242
243 int mdc_getattr_lock(struct obd_export *exp, struct lustre_id *id,
244                      char *filename, int namelen, __u64 valid,
245                      unsigned int ea_size, struct ptlrpc_request **request)
246 {
247         struct ptlrpc_request *req;
248         struct mds_body *body;
249         int rc, size[3] = {0, sizeof(*body), namelen};
250         ENTRY;
251
252         size[0] = mdc_get_secdesc_size();
253
254         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
255                               MDS_GETATTR_LOCK, 3, size, NULL);
256         if (!req)
257                 GOTO(out, rc = -ENOMEM);
258
259         mdc_pack_secdesc(req, size[0]);
260
261         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof (*body));
262         memcpy(&body->id1, id, sizeof(*id));
263         body->valid = valid;
264         body->eadatasize = ea_size;
265
266         if (filename != NULL) {
267                 LASSERT (strnlen (filename, namelen) == namelen - 1);
268                 memcpy(lustre_msg_buf(req->rq_reqmsg, 2, namelen),
269                        filename, namelen);
270         } else {
271                 LASSERT(namelen == 1);
272         }
273
274         rc = mdc_getattr_common(exp, ea_size, req);
275         if (rc != 0) {
276                 ptlrpc_req_finished (req);
277                 req = NULL;
278         }
279  out:
280         *request = req;
281         RETURN(rc);
282 }
283
284 /* This should be called with both the request and the reply still packed. */
285 int mdc_store_inode_generation(struct obd_export *exp,
286                                struct ptlrpc_request *req,
287                                int reqoff, int repoff)
288 {
289         struct mds_rec_create *rec =
290                 lustre_msg_buf(req->rq_reqmsg, reqoff, sizeof(*rec));
291         struct mds_body *body =
292                 lustre_msg_buf(req->rq_repmsg, repoff, sizeof(*body));
293
294         LASSERT (rec != NULL);
295         LASSERT (body != NULL);
296
297         memcpy(&rec->cr_replayid, &body->id1, sizeof(rec->cr_replayid));
298         DEBUG_REQ(D_HA, req, "storing generation for ino "DLID4,
299                   OLID4(&rec->cr_replayid));
300         return 0;
301 }
302
303 int mdc_req2lustre_md(struct obd_export *exp_lmv, struct ptlrpc_request *req, 
304                       unsigned int offset, struct obd_export *exp_lov, 
305                       struct lustre_md *md)
306 {
307         int rc = 0;
308         ENTRY;
309
310         LASSERT(md);
311         memset(md, 0, sizeof(*md));
312
313         md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
314         LASSERT (md->body != NULL);
315         LASSERT_REPSWABBED (req, offset);
316
317         if (!(md->body->valid & OBD_MD_FLEASIZE) && 
318             !(md->body->valid & OBD_MD_FLDIREA))
319                 RETURN(0);
320
321         /* ea is presented in reply, parse it */
322         if (S_ISREG(md->body->mode)) {
323                 int lmmsize;
324                 struct lov_mds_md *lmm;
325
326                 if (md->body->eadatasize == 0) {
327                         CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
328                         RETURN(-EPROTO);
329                 }
330                 lmmsize = md->body->eadatasize;
331                 lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, lmmsize);
332                 LASSERT (lmm != NULL);
333                 LASSERT_REPSWABBED (req, offset + 1);
334
335                 rc = obd_unpackmd(exp_lov, &md->lsm, lmm, lmmsize);
336                 if (rc >= 0) {
337                         LASSERT (rc >= sizeof (*md->lsm));
338                         rc = 0;
339                 }
340         } else if (S_ISDIR(md->body->mode)) {
341                 struct mea *mea;
342                 int mdsize;
343                 LASSERT(exp_lmv != NULL);
344                 
345                 /* dir can be non-splitted */
346                 if (md->body->eadatasize == 0)
347                         RETURN(0);
348
349                 mdsize = md->body->eadatasize;
350                 mea = lustre_msg_buf(req->rq_repmsg, offset + 1, mdsize);
351                 LASSERT(mea != NULL);
352
353                 /* 
354                  * check mea for validness, as there is possible that old tests
355                  * will try to set lov EA to dir object and thus confuse this
356                  * stuff.
357                  */
358                 if (mea->mea_magic != MEA_MAGIC_LAST_CHAR &&
359                     mea->mea_magic != MEA_MAGIC_ALL_CHARS)
360                         GOTO(out_invalid_mea, rc = -EINVAL);
361
362                 if (mea->mea_count > 256 || mea->mea_master > 256 ||
363                     mea->mea_master > mea->mea_count)
364                         GOTO(out_invalid_mea, rc = -EINVAL);
365                         
366                 LASSERT(id_fid(&mea->mea_ids[0]));
367
368                 rc = obd_unpackmd(exp_lmv, (void *)&md->mea,
369                                   (void *)mea, mdsize);
370                 if (rc >= 0) {
371                         LASSERT (rc >= sizeof (*md->mea));
372                         rc = 0;
373                 }
374
375                 RETURN(rc);
376
377         out_invalid_mea:
378                 CERROR("Detected invalid mea, which does not "
379                        "support neither old either new format.\n");
380         } else {
381                 LASSERT(0);
382         }
383         RETURN(rc);
384 }
385
386 static void mdc_commit_open(struct ptlrpc_request *req)
387 {
388         struct mdc_open_data *mod = req->rq_cb_data;
389         if (mod == NULL)
390                 return;
391
392         if (mod->mod_close_req != NULL)
393                 mod->mod_close_req->rq_cb_data = NULL;
394
395         if (mod->mod_och != NULL)
396                 mod->mod_och->och_mod = NULL;
397
398         OBD_FREE(mod, sizeof(*mod));
399         req->rq_cb_data = NULL;
400 }
401
402 static void mdc_replay_open(struct ptlrpc_request *req)
403 {
404         struct mdc_open_data *mod = req->rq_cb_data;
405         struct obd_client_handle *och;
406         struct ptlrpc_request *close_req;
407         struct lustre_handle old;
408         struct mds_body *body;
409         ENTRY;
410
411         body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
412         LASSERT (body != NULL);
413
414         if (mod == NULL) {
415                 DEBUG_REQ(D_ERROR, req,
416                           "can't properly replay without open data");
417                 EXIT;
418                 return;
419         }
420
421         och = mod->mod_och;
422         if (och != NULL) {
423                 struct lustre_handle *file_fh;
424                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
425                 file_fh = &och->och_fh;
426                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
427                        file_fh->cookie, body->handle.cookie);
428                 memcpy(&old, file_fh, sizeof(old));
429                 memcpy(file_fh, &body->handle, sizeof(*file_fh));
430         }
431
432         close_req = mod->mod_close_req;
433         if (close_req != NULL) {
434                 struct mds_body *close_body;
435                 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
436                 close_body = lustre_msg_buf(close_req->rq_reqmsg,
437                                             MDS_REQ_REC_OFF,
438                                             sizeof(*close_body));
439                 if (och != NULL)
440                         LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
441                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
442                 memcpy(&close_body->handle, &body->handle,
443                        sizeof(close_body->handle));
444         }
445
446         EXIT;
447 }
448
449 int  mdc_set_open_replay_data(struct obd_export *exp,
450                               struct obd_client_handle *och,
451                               struct ptlrpc_request *open_req)
452 {
453         struct mdc_open_data *mod;
454         struct mds_rec_create *rec;
455         struct mds_body *body;
456
457         rec = lustre_msg_buf(open_req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
458                              sizeof(*rec));
459         body = lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
460
461         LASSERT(rec != NULL);
462         /* outgoing messages always in my byte order */
463         LASSERT(body != NULL);
464         /* incoming message in my byte order (it's been swabbed) */
465         LASSERT_REPSWABBED(open_req, 1);
466
467         OBD_ALLOC(mod, sizeof(*mod));
468         if (mod == NULL) {
469                 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
470                 return 0;
471         }
472
473         och->och_mod = mod;
474         mod->mod_och = och;
475         mod->mod_open_req = open_req;
476
477         memcpy(&rec->cr_replayid, &body->id1, sizeof rec->cr_replayid);
478         open_req->rq_replay_cb = mdc_replay_open;
479         open_req->rq_commit_cb = mdc_commit_open;
480         open_req->rq_cb_data = mod;
481         DEBUG_REQ(D_HA, open_req, "set up replay data");
482         return 0;
483 }
484
485 int mdc_clear_open_replay_data(struct obd_export *exp,
486                                struct obd_client_handle *och)
487 {
488         struct mdc_open_data *mod = och->och_mod;
489
490         /* Don't free the structure now (it happens in mdc_commit_open, after
491          * we're sure we won't need to fix up the close request in the future),
492          * but make sure that replay doesn't poke at the och, which is about to
493          * be freed. */
494         LASSERT(mod != LP_POISON);
495         if (mod != NULL)
496                 mod->mod_och = NULL;
497         och->och_mod = NULL;
498         return 0;
499 }
500
501 static void mdc_commit_close(struct ptlrpc_request *req)
502 {
503         struct mdc_open_data *mod = req->rq_cb_data;
504         struct ptlrpc_request *open_req;
505         struct obd_import *imp = req->rq_import;
506
507         DEBUG_REQ(D_HA, req, "close req committed");
508         if (mod == NULL)
509                 return;
510
511         mod->mod_close_req = NULL;
512         req->rq_cb_data = NULL;
513         req->rq_commit_cb = NULL;
514
515         open_req = mod->mod_open_req;
516         LASSERT(open_req != NULL);
517         LASSERT(open_req != LP_POISON);
518         LASSERT(open_req->rq_type != LI_POISON);
519
520         DEBUG_REQ(D_HA, open_req, "open req balanced");
521         if (open_req->rq_transno == 0) {
522                 DEBUG_REQ(D_ERROR, open_req, "BUG 3892  open");
523                 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
524                 LASSERTF(open_req->rq_transno != 0, "BUG 3892");
525         }
526         LASSERT(open_req->rq_import == imp);
527
528         /* We no longer want to preserve this for transno-unconditional
529          * replay. */
530         spin_lock(&open_req->rq_lock);
531         open_req->rq_replay = 0;
532         spin_unlock(&open_req->rq_lock);
533 }
534
535 static int mdc_close_interpret(struct ptlrpc_request *req, void *data, int rc)
536 {
537         union ptlrpc_async_args *aa = data;
538         struct mdc_rpc_lock *rpc_lock;
539         struct obd_device *obd = aa->pointer_arg[1];
540         unsigned long flags;
541
542         spin_lock_irqsave(&req->rq_lock, flags);
543         rpc_lock = aa->pointer_arg[0];
544         aa->pointer_arg[0] = NULL;
545         spin_unlock_irqrestore (&req->rq_lock, flags);
546
547         if (rpc_lock == NULL) {
548                 CERROR("called with NULL rpc_lock\n");
549         } else {
550                 mdc_put_rpc_lock(rpc_lock, NULL);
551                 LASSERTF(rpc_lock == obd->u.cli.cl_rpc_lock, "%p != %p\n",
552                          rpc_lock, obd->u.cli.cl_rpc_lock);
553         }
554         wake_up(&req->rq_reply_waitq);
555         RETURN(rc);
556 }
557
558 /* We can't use ptlrpc_check_reply, because we don't want to wake up for
559  * anything but a reply or an error. */
560 static int mdc_close_check_reply(struct ptlrpc_request *req)
561 {
562         int rc = 0;
563         unsigned long flags;
564
565         spin_lock_irqsave(&req->rq_lock, flags);
566         if (req->rq_async_args.pointer_arg[0] == NULL)
567                 rc = 1;
568         spin_unlock_irqrestore (&req->rq_lock, flags);
569         return rc;
570 }
571
572 static int go_back_to_sleep(void *unused)
573 {
574         return 0;
575 }
576
577 int mdc_close(struct obd_export *exp, struct obdo *oa,
578               struct obd_client_handle *och, struct ptlrpc_request **request)
579 {
580         struct obd_device *obd = class_exp2obd(exp);
581         int reqsize[3] = {0, sizeof(struct mds_body),
582                           obd->u.cli.cl_max_mds_cookiesize};
583         int rc, repsize[3] = {sizeof(struct mds_body),
584                               obd->u.cli.cl_max_mds_easize,
585                               obd->u.cli.cl_max_mds_cookiesize};
586         struct ptlrpc_request *req;
587         struct mdc_open_data *mod;
588         struct l_wait_info lwi;
589         ENTRY;
590
591         //reqsize[0] = mdc_get_secdesc_size();
592
593         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
594                               MDS_CLOSE, 3, reqsize, NULL);
595         if (req == NULL)
596                 GOTO(out, rc = -ENOMEM);
597
598         //mdc_pack_secdesc(req, reqsize[0]);
599
600         /* Ensure that this close's handle is fixed up during replay. */
601         LASSERT(och != NULL);
602         mod = och->och_mod;
603         if (likely(mod != NULL)) {
604                 mod->mod_close_req = req;
605                 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
606                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open req %p",
607                           mod->mod_open_req);
608         } else {
609                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
610         }
611
612         mdc_close_pack(req, 1, oa, oa->o_valid, och);
613
614         req->rq_replen = lustre_msg_size(3, repsize);
615         req->rq_commit_cb = mdc_commit_close;
616         LASSERT(req->rq_cb_data == NULL);
617         req->rq_cb_data = mod;
618
619         /* We hand a ref to the rpcd here, so we need another one of our own. */
620         ptlrpc_request_addref(req);
621
622         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
623         req->rq_interpret_reply = mdc_close_interpret;
624         req->rq_async_args.pointer_arg[0] = obd->u.cli.cl_rpc_lock;
625         req->rq_async_args.pointer_arg[1] = obd;
626         ptlrpcd_add_req(req);
627         lwi = LWI_TIMEOUT_INTR(MAX(req->rq_timeout * HZ, 1), go_back_to_sleep,
628                                NULL, NULL);
629         rc = l_wait_event(req->rq_reply_waitq, mdc_close_check_reply(req),
630                           &lwi);
631         if (req->rq_repmsg == NULL) {
632                 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
633                        req->rq_status);
634                 if (rc == 0)
635                         rc = req->rq_status ? req->rq_status : -EIO;
636         } else if (rc == 0) {
637                 rc = req->rq_repmsg->status;
638                 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
639                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
640                                   "= %d", rc);
641                         if (rc > 0)
642                                 rc = -rc;
643                 } else if (mod == NULL) {
644                         CERROR("Unexpected: can't find mdc_open_data, but the "
645                                "close succeeded.  Please tell CFS.\n");
646                 }
647                 if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
648                                         lustre_swab_mds_body)) {
649                         CERROR("Error unpacking mds_body\n");
650                         rc = -EPROTO;
651                 }
652         }
653         if (req->rq_async_args.pointer_arg[0] != NULL) {
654                 CERROR("returned without dropping rpc_lock: rc %d\n", rc);
655                 mdc_close_interpret(req, &req->rq_async_args, rc);
656         }
657
658         EXIT;
659  out:
660         *request = req;
661         return rc;
662 }
663
664 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
665 {
666         struct ptlrpc_request *req;
667         struct mds_body *body;
668         int rc, size[2] = {0, sizeof(*body)};
669         ENTRY;
670
671         size[0] = mdc_get_secdesc_size();
672
673         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
674                               MDS_DONE_WRITING, 2, size, NULL);
675         if (req == NULL)
676                 RETURN(-ENOMEM);
677
678         mdc_pack_secdesc(req, size[0]);
679
680         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, 
681                               sizeof(*body));
682         
683         mdc_pack_id(&body->id1, obdo->o_id, 0, obdo->o_mode, 
684                     obdo->o_mds, obdo->o_fid);
685         
686         body->size = obdo->o_size;
687         body->blocks = obdo->o_blocks;
688         body->flags = obdo->o_flags;
689         body->valid = obdo->o_valid;
690
691         req->rq_replen = lustre_msg_size(1, &size[1]);
692
693         rc = ptlrpc_queue_wait(req);
694         ptlrpc_req_finished(req);
695         RETURN(rc);
696 }
697
698 int mdc_readpage(struct obd_export *exp,
699                  struct lustre_id *id,
700                  __u64 offset, struct page *page,
701                  struct ptlrpc_request **request)
702 {
703         struct obd_import *imp = class_exp2cliimp(exp);
704         struct ptlrpc_request *req = NULL;
705         struct ptlrpc_bulk_desc *desc = NULL;
706         struct mds_body *body;
707         int rc, size[2] = {0, sizeof(*body)};
708         ENTRY;
709
710         CDEBUG(D_INODE, "inode: %ld\n", (long)id->li_stc.u.e3s.l3s_ino);
711
712         size[0] = mdc_get_secdesc_size();
713
714         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
715                               2, size, NULL);
716         if (req == NULL)
717                 GOTO(out, rc = -ENOMEM);
718         /* XXX FIXME bug 249 */
719         req->rq_request_portal = MDS_READPAGE_PORTAL;
720
721         mdc_pack_secdesc(req, size[0]);
722
723         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
724         if (desc == NULL)
725                 GOTO(out, rc = -ENOMEM);
726         /* NB req now owns desc and will free it when it gets freed */
727
728         ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
729         mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, id);
730
731         req->rq_replen = lustre_msg_size(1, &size[1]);
732         rc = ptlrpc_queue_wait(req);
733
734         if (rc == 0) {
735                 body = lustre_swab_repbuf(req, 0, sizeof (*body),
736                                           lustre_swab_mds_body);
737                 if (body == NULL) {
738                         CERROR("Can't unpack mds_body\n");
739                         GOTO(out, rc = -EPROTO);
740                 }
741
742                 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
743                         CERROR ("Unexpected # bytes transferred: %d"
744                                 " (%ld expected)\n",
745                                 req->rq_bulk->bd_nob_transferred,
746                                 PAGE_CACHE_SIZE);
747                         GOTO (out, rc = -EPROTO);
748                 }
749         }
750
751         EXIT;
752  out:
753         *request = req;
754         return rc;
755 }
756
757 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
758                          void *karg, void *uarg)
759 {
760         struct obd_device *obd = exp->exp_obd;
761         struct obd_ioctl_data *data = karg;
762         struct obd_import *imp = obd->u.cli.cl_import;
763         struct llog_ctxt *ctxt;
764         int rc;
765         ENTRY;
766
767 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
768         MOD_INC_USE_COUNT;
769 #else
770         if (!try_module_get(THIS_MODULE)) {
771                 CERROR("Can't get module. Is it alive?");
772                 return -EINVAL;
773         }
774 #endif
775         switch (cmd) {
776         case OBD_IOC_CLIENT_RECOVER:
777                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
778                 if (rc < 0)
779                         GOTO(out, rc);
780                 GOTO(out, rc = 0);
781         case IOC_OSC_SET_ACTIVE:
782                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
783                 GOTO(out, rc);
784         case OBD_IOC_PARSE: {
785                 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
786                                         LLOG_CONFIG_REPL_CTXT);
787                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
788                 GOTO(out, rc);
789         }
790 #ifdef __KERNEL__
791         case OBD_IOC_LLOG_INFO:
792         case OBD_IOC_LLOG_PRINT: {
793                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
794                 rc = llog_ioctl(ctxt, cmd, data);
795
796                 GOTO(out, rc);
797         }
798 #endif
799         default:
800                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
801                 GOTO(out, rc = -ENOTTY);
802         }
803 out:
804 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
805         MOD_DEC_USE_COUNT;
806 #else
807         module_put(THIS_MODULE);
808 #endif
809
810         return rc;
811 }
812
813 int mdc_set_info(struct obd_export *exp, obd_count keylen,
814                  void *key, obd_count vallen, void *val)
815 {
816         int rc = -EINVAL;
817
818         if (keylen == strlen("initial_recov") &&
819             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
820                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
821                 if (vallen != sizeof(int))
822                         RETURN(-EINVAL);
823                 imp->imp_initial_recov = *(int *)val;
824                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
825                        exp->exp_obd->obd_name,
826                        imp->imp_initial_recov);
827                 RETURN(0);
828         } else if (keylen >= strlen("mds_type") && strcmp(key, "mds_type") == 0) {
829                 struct ptlrpc_request *req;
830                 char *bufs[2] = {key, val};
831                 int rc, size[2] = {keylen, vallen};
832
833                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
834                                       OST_SET_INFO, 2, size, bufs);
835                 if (req == NULL)
836                         RETURN(-ENOMEM);
837
838                 req->rq_replen = lustre_msg_size(0, NULL);
839                 rc = ptlrpc_queue_wait(req);
840                 ptlrpc_req_finished(req);
841                 RETURN(rc);
842         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
843                 struct obd_import *imp = class_exp2cliimp(exp);
844                 imp->imp_server_timeout = 1;
845                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
846                 RETURN(0);
847         }
848         RETURN(rc);
849 }
850
851 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
852                       unsigned long max_age)
853 {
854         struct obd_statfs *msfs;
855         struct ptlrpc_request *req;
856         int rc, size = sizeof(*msfs);
857         ENTRY;
858
859         /* We could possibly pass max_age in the request (as an absolute
860          * timestamp or a "seconds.usec ago") so the target can avoid doing
861          * extra calls into the filesystem if that isn't necessary (e.g.
862          * during mount that would help a bit).  Having relative timestamps
863          * is not so great if request processing is slow, while absolute
864          * timestamps are not ideal because they need time synchronization. */
865         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
866                               MDS_STATFS, 0, NULL, NULL);
867         if (!req)
868                 RETURN(-ENOMEM);
869
870         req->rq_replen = lustre_msg_size(1, &size);
871
872         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
873         rc = ptlrpc_queue_wait(req);
874         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
875
876         if (rc) {
877                 /* this can be LMV fake import, whcih is not connected. */
878                 if (!req->rq_import->imp_connection)
879                         memset(osfs, 0, sizeof(*osfs));
880                 GOTO(out, rc);
881         }
882
883         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),
884                                   lustre_swab_obd_statfs);
885         if (msfs == NULL) {
886                 CERROR("Can't unpack obd_statfs\n");
887                 GOTO(out, rc = -EPROTO);
888         }
889
890         memcpy(osfs, msfs, sizeof (*msfs));
891         EXIT;
892 out:
893         ptlrpc_req_finished(req);
894         return rc;
895 }
896
897 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
898                    struct obd_client_handle *handle, int flag)
899 {
900         struct ptlrpc_request *req;
901         struct mds_body *body;
902         int rc, size[2] = {0, sizeof(*body)};
903         ENTRY;
904
905         //size[0] = mdc_get_secdesc_size();
906
907         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
908                               MDS_PIN, 2, size, NULL);
909         if (req == NULL)
910                 RETURN(-ENOMEM);
911
912         //mdc_pack_secdesc(req, size[0]);
913
914         body = lustre_msg_buf(req->rq_reqmsg, 
915                               MDS_REQ_REC_OFF, sizeof(*body));
916
917         /* FIXME-UMKA: here should be also mdsnum and fid. */
918         mdc_pack_id(&body->id1, ino, gen, type, 0, 0);
919         body->flags = flag;
920
921         req->rq_replen = lustre_msg_size(1, &size[1]);
922
923         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
924         rc = ptlrpc_queue_wait(req);
925         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
926         if (rc) {
927                 CERROR("pin failed: %d\n", rc);
928                 ptlrpc_req_finished(req);
929                 RETURN(rc);
930         }
931
932         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
933         if (body == NULL) {
934                 ptlrpc_req_finished(req);
935                 RETURN(rc);
936         }
937
938         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
939         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
940
941         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
942         if (handle->och_mod == NULL) {
943                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
944                 RETURN(-ENOMEM);
945         }
946         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
947
948         RETURN(rc);
949 }
950
951 static int mdc_unpin(struct obd_export *exp,
952                      struct obd_client_handle *handle, int flag)
953 {
954         struct ptlrpc_request *req;
955         struct mds_body *body;
956         int rc, size[2] = {0, sizeof(*body)};
957         ENTRY;
958
959         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
960                 RETURN(0);
961
962         //size[0] = mdc_get_secdesc_size();
963
964         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
965                               MDS_CLOSE, 2, size, NULL);
966         if (req == NULL)
967                 RETURN(-ENOMEM);
968
969         //mdc_pack_secdesc(req, size[0]);
970
971         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
972         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
973         body->flags = flag;
974
975         req->rq_replen = lustre_msg_size(0, NULL);
976         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
977         rc = ptlrpc_queue_wait(req);
978         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
979
980         if (rc != 0)
981                 CERROR("unpin failed: %d\n", rc);
982
983         ptlrpc_req_finished(req);
984         ptlrpc_req_finished(handle->och_mod->mod_open_req);
985         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
986         RETURN(rc);
987 }
988
989 int mdc_sync(struct obd_export *exp, struct lustre_id *id,
990              struct ptlrpc_request **request)
991 {
992         struct ptlrpc_request *req;
993         struct mds_body *body;
994         int size[2] = {0, sizeof(*body)};
995         int rc;
996         ENTRY;
997
998         //size[0] = mdc_get_secdesc_size();
999
1000         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1001                               MDS_SYNC, 2, size, NULL);
1002         if (!req)
1003                 RETURN(rc = -ENOMEM);
1004
1005         //mdc_pack_secdesc(req, size[0]);
1006
1007         if (id) {
1008                 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1009                                       sizeof (*body));
1010                 memcpy(&body->id1, id, sizeof(*id));
1011         }
1012
1013         req->rq_replen = lustre_msg_size(1, &size[1]);
1014
1015         rc = ptlrpc_queue_wait(req);
1016         if (rc || request == NULL)
1017                 ptlrpc_req_finished(req);
1018         else
1019                 *request = req;
1020
1021         RETURN(rc);
1022 }
1023
1024 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1025                             enum obd_import_event event)
1026 {
1027         int rc = 0;
1028
1029         LASSERT(imp->imp_obd == obd);
1030
1031         switch (event) {
1032         case IMP_EVENT_DISCON: {
1033                 break;
1034         }
1035         case IMP_EVENT_INACTIVE: {
1036                 if (obd->obd_observer)
1037                         rc = obd_notify(obd->obd_observer, obd, 0, 0);
1038                 break;
1039         }
1040         case IMP_EVENT_INVALIDATE: {
1041                 struct ldlm_namespace *ns = obd->obd_namespace;
1042
1043                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1044
1045                 break;
1046         }
1047         case IMP_EVENT_ACTIVE: {
1048                 if (obd->obd_observer)
1049                         rc = obd_notify(obd->obd_observer, obd, 1, 0);
1050                 break;
1051         }
1052         default:
1053                 CERROR("Unknown import event %d\n", event);
1054                 LBUG();
1055         }
1056         RETURN(rc);
1057 }
1058
1059 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1060 {
1061         struct lprocfs_static_vars lvars;
1062
1063         lprocfs_init_vars(mdc, &lvars);
1064         return lprocfs_obd_attach(dev, lvars.obd_vars);
1065 }
1066
1067 static int mdc_detach(struct obd_device *dev)
1068 {
1069         return lprocfs_obd_detach(dev);
1070 }
1071
1072 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1073 {
1074         struct client_obd *cli = &obd->u.cli;
1075         int rc;
1076         ENTRY;
1077
1078         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1079         if (!cli->cl_rpc_lock)
1080                 RETURN(-ENOMEM);
1081         mdc_init_rpc_lock(cli->cl_rpc_lock);
1082
1083         ptlrpcd_addref();
1084
1085         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1086         if (!cli->cl_setattr_lock)
1087                 GOTO(err_rpc_lock, rc = -ENOMEM);
1088         mdc_init_rpc_lock(cli->cl_setattr_lock);
1089
1090         rc = client_obd_setup(obd, len, buf);
1091         if (rc)
1092                 GOTO(err_setattr_lock, rc);
1093
1094         rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1095         if (rc) {
1096                 mdc_cleanup(obd, 0);
1097                 CERROR("failed to setup llogging subsystems\n");
1098         }
1099
1100         RETURN(rc);
1101
1102 err_setattr_lock:
1103         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1104 err_rpc_lock:
1105         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1106         ptlrpcd_decref();
1107         RETURN(rc);
1108 }
1109
1110 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1111 {
1112         struct obd_device *obd = exp->exp_obd;
1113         struct client_obd *cli = &obd->u.cli;
1114         ENTRY;
1115
1116         if (cli->cl_max_mds_easize < easize)
1117                 cli->cl_max_mds_easize = easize;
1118         if (cli->cl_max_mds_cookiesize < cookiesize)
1119                 cli->cl_max_mds_cookiesize = cookiesize;
1120         RETURN(0);
1121 }
1122
1123 static int mdc_precleanup(struct obd_device *obd, int flags)
1124 {
1125         int rc = 0;
1126         
1127         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1128         if (rc != 0)
1129                 CERROR("failed to cleanup llogging subsystems\n");
1130
1131         RETURN(rc);
1132 }
1133
1134 static int mdc_cleanup(struct obd_device *obd, int flags)
1135 {
1136         struct client_obd *cli = &obd->u.cli;
1137
1138         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1139         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1140
1141         ptlrpcd_decref();
1142
1143         return client_obd_cleanup(obd, flags);
1144 }
1145
1146
1147 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1148                          struct obd_device *tgt, int count,
1149                          struct llog_catid *logid)
1150 {
1151         struct llog_ctxt *ctxt;
1152         int rc;
1153         ENTRY;
1154
1155         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1156                             &llog_client_ops);
1157         if (rc == 0) {
1158                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1159                 ctxt->loc_imp = obd->u.cli.cl_import;
1160         }
1161
1162         RETURN(rc);
1163 }
1164
1165 static int mdc_llog_finish(struct obd_device *obd,
1166                            struct obd_llogs *llogs, int count)
1167 {
1168         int rc;
1169         ENTRY;
1170
1171         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1172         RETURN(rc);
1173 }
1174 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1175                                            struct lustre_id *id)
1176 {
1177        ENTRY;
1178        RETURN(exp->exp_obd);
1179 }
1180
1181 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1182                         void *key, __u32 *valsize, void *val)
1183 {
1184         struct ptlrpc_request *req;
1185         char *bufs[1] = {key};
1186         int rc = 0;
1187         ENTRY;
1188         
1189         if (!valsize || !val)
1190                 RETURN(-EFAULT);
1191
1192         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1193             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1194             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
1195                 RETURN(-EPROTO);
1196                 
1197         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1198                               OST_GET_INFO, 1, &keylen, bufs);
1199         if (req == NULL)
1200                 RETURN(-ENOMEM);
1201
1202         req->rq_replen = lustre_msg_size(1, valsize);
1203         rc = ptlrpc_queue_wait(req);
1204         if (rc)
1205                 GOTO(out_req, rc);
1206
1207         if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
1208                 struct lustre_id *reply;
1209                 
1210                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1211                                            lustre_swab_lustre_id);
1212                 if (reply == NULL) {
1213                         CERROR("Can't unpack %s\n", (char *)key);
1214                         GOTO(out_req, rc = -EPROTO);
1215                 }
1216
1217                 *(struct lustre_id *)val = *reply;
1218         } else {
1219                 __u32 *reply;
1220                 
1221                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1222                                            lustre_swab_generic_32s);
1223                 if (reply == NULL) {
1224                         CERROR("Can't unpack %s\n", (char *)key);
1225                         GOTO(out_req, rc = -EPROTO);
1226                 }
1227                 *((__u32 *)val) = *reply;
1228         }
1229 out_req:
1230         ptlrpc_req_finished(req);
1231         RETURN(rc);
1232 }
1233
1234 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1235                     struct lov_stripe_md **ea, struct obd_trans_info *oti)
1236 {
1237         struct ptlrpc_request *request;
1238         struct ost_body *body;
1239         int rc, size = sizeof(*body);
1240         ENTRY;
1241
1242         LASSERT(oa);
1243
1244         request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1245                                   OST_CREATE, 1, &size, NULL);
1246         if (!request)
1247                 GOTO(out_req, rc = -ENOMEM);
1248
1249         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1250         memcpy(&body->oa, oa, sizeof(body->oa));
1251
1252         request->rq_replen = lustre_msg_size(1, &size);
1253         rc = ptlrpc_queue_wait(request);
1254         if (rc)
1255                 GOTO(out_req, rc);
1256
1257         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1258                                   lustre_swab_ost_body);
1259         if (body == NULL) {
1260                 CERROR ("can't unpack ost_body\n");
1261                 GOTO (out_req, rc = -EPROTO);
1262         }
1263
1264         memcpy(oa, &body->oa, sizeof(*oa));
1265
1266         /* store ino/generation for recovery */
1267         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1268         body->oa.o_id = oa->o_id;
1269         body->oa.o_generation = oa->o_generation;
1270         body->oa.o_fid = oa->o_fid;
1271         body->oa.o_mds = oa->o_mds;
1272
1273         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1274         EXIT;
1275 out_req:
1276         ptlrpc_req_finished(request);
1277         return rc;
1278 }
1279
1280 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1281             struct lov_stripe_md *ea, obd_count oa_bufs,
1282             struct brw_page *pgarr, struct obd_trans_info *oti)
1283 {
1284         struct ptlrpc_bulk_desc *desc;
1285         struct niobuf_remote *niobuf;
1286         struct ptlrpc_request *req;
1287         struct obd_ioobj *ioobj;
1288         struct ost_body *body;
1289         int err, opc, i;
1290         int size[3];
1291
1292         opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1293         
1294         size[0] = sizeof(*body);
1295         size[1] = sizeof(*ioobj);
1296         size[2] = oa_bufs * sizeof(*niobuf);
1297
1298         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1299                               3, size, NULL);
1300         LASSERT(req != NULL);
1301
1302         if (opc == OST_WRITE)
1303                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1304                                             OST_BULK_PORTAL);
1305         else
1306                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1307                                             OST_BULK_PORTAL);
1308         LASSERT(desc != NULL);
1309
1310         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1311         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1312         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1313
1314         memcpy(&body->oa, oa, sizeof(*oa));
1315         obdo_to_ioobj(oa, ioobj);
1316         ioobj->ioo_bufcnt = oa_bufs;
1317
1318         for (i = 0; i < oa_bufs; i++, niobuf++) {
1319                 struct brw_page *pg = &pgarr[i];
1320
1321                 LASSERT(pg->count > 0);
1322                 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1323
1324                 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1325                                       pg->count);
1326
1327                 niobuf->offset = pg->disk_offset;
1328                 niobuf->len = pg->count;
1329                 niobuf->flags = pg->flag;
1330         }
1331
1332         /* size[0] still sizeof (*body) */
1333         if (opc == OST_WRITE) {
1334                 /* 1 RC per niobuf */
1335                 size[1] = sizeof(__u32) * oa_bufs;
1336                 req->rq_replen = lustre_msg_size(2, size);
1337         } else {
1338                 /* 1 RC for the whole I/O */
1339                 req->rq_replen = lustre_msg_size(1, size);
1340         }
1341         err = ptlrpc_queue_wait(req);
1342         LASSERT(err == 0);
1343
1344         ptlrpc_req_finished(req);
1345         return 0;
1346 }
1347
1348 static int mdc_valid_attrs(struct obd_export *exp,
1349                            struct lustre_id *id)
1350 {
1351         struct ldlm_res_id res_id = { .name = {0} };
1352         struct obd_device *obd = exp->exp_obd;
1353         struct lustre_handle lockh;
1354         ldlm_policy_data_t policy;
1355         int flags;
1356         ENTRY;
1357
1358         res_id.name[0] = id_fid(id);
1359         res_id.name[1] = id_group(id);
1360         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1361
1362         CDEBUG(D_INFO, "trying to match res "LPU64"\n",
1363                res_id.name[0]);
1364
1365         /* FIXME use LDLM_FL_TEST_LOCK instead */
1366         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1367         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1368                             LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1369                 ldlm_lock_decref(&lockh, LCK_PR);
1370                 RETURN(1);
1371         }
1372
1373         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1374                             LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1375                 ldlm_lock_decref(&lockh, LCK_PW);
1376                 RETURN(1);
1377         }
1378         RETURN(0);
1379 }
1380
1381 static int mdc_change_cbdata_name(struct obd_export *exp,
1382                                   struct lustre_id *pid,
1383                                   char *name, int len,
1384                                   struct lustre_id *cid,
1385                                   ldlm_iterator_t it, void *data)
1386 {
1387         int rc;
1388         rc = mdc_change_cbdata(exp, cid, it, data);
1389         RETURN(rc);
1390 }
1391
1392 struct obd_ops mdc_obd_ops = {
1393         .o_owner         = THIS_MODULE,
1394         .o_attach        = mdc_attach,
1395         .o_detach        = mdc_detach,
1396         .o_setup         = mdc_setup,
1397         .o_precleanup    = mdc_precleanup,
1398         .o_cleanup       = mdc_cleanup,
1399         .o_add_conn      = client_import_add_conn,
1400         .o_del_conn      = client_import_del_conn,
1401         .o_connect       = client_connect_import,
1402         .o_disconnect    = client_disconnect_export,
1403         .o_iocontrol     = mdc_iocontrol,
1404         .o_statfs        = mdc_statfs,
1405         .o_pin           = mdc_pin,
1406         .o_unpin         = mdc_unpin,
1407         .o_import_event  = mdc_import_event,
1408         .o_llog_init     = mdc_llog_init,
1409         .o_llog_finish   = mdc_llog_finish,
1410         .o_create        = mdc_obj_create,
1411         .o_set_info      = mdc_set_info,
1412         .o_get_info      = mdc_get_info,
1413         .o_brw           = mdc_brw,
1414         .o_init_ea_size  = mdc_init_ea_size,
1415 };
1416
1417 struct md_ops mdc_md_ops = {
1418         .m_getstatus     = mdc_getstatus,
1419         .m_getattr       = mdc_getattr,
1420         .m_close         = mdc_close,
1421         .m_create        = mdc_create,
1422         .m_done_writing  = mdc_done_writing,
1423         .m_enqueue       = mdc_enqueue,
1424         .m_getattr_lock  = mdc_getattr_lock,
1425         .m_intent_lock   = mdc_intent_lock,
1426         .m_link          = mdc_link,
1427         .m_rename        = mdc_rename,
1428         .m_setattr       = mdc_setattr,
1429         .m_sync          = mdc_sync,
1430         .m_readpage      = mdc_readpage,
1431         .m_unlink        = mdc_unlink,
1432         .m_valid_attrs   = mdc_valid_attrs,
1433         .m_req2lustre_md = mdc_req2lustre_md,
1434         .m_set_open_replay_data   = mdc_set_open_replay_data,
1435         .m_clear_open_replay_data = mdc_clear_open_replay_data,
1436         .m_store_inode_generation = mdc_store_inode_generation,
1437         .m_set_lock_data = mdc_set_lock_data,
1438         .m_get_real_obd  = mdc_get_real_obd,
1439         .m_change_cbdata_name = mdc_change_cbdata_name,
1440         .m_change_cbdata = mdc_change_cbdata,
1441 };
1442
1443 int __init mdc_init(void)
1444 {
1445         struct lprocfs_static_vars lvars;
1446         lprocfs_init_vars(mdc, &lvars);
1447         return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1448                                    LUSTRE_MDC_NAME);
1449 }
1450
1451 #ifdef __KERNEL__
1452 static void /*__exit*/ mdc_exit(void)
1453 {
1454         class_unregister_type(LUSTRE_MDC_NAME);
1455 }
1456
1457 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1458 MODULE_DESCRIPTION("Lustre Metadata Client");
1459 MODULE_LICENSE("GPL");
1460
1461 EXPORT_SYMBOL(mdc_req2lustre_md);
1462 EXPORT_SYMBOL(mdc_change_cbdata);
1463 EXPORT_SYMBOL(mdc_getstatus);
1464 EXPORT_SYMBOL(mdc_getattr);
1465 EXPORT_SYMBOL(mdc_getattr_lock);
1466 EXPORT_SYMBOL(mdc_create);
1467 EXPORT_SYMBOL(mdc_unlink);
1468 EXPORT_SYMBOL(mdc_rename);
1469 EXPORT_SYMBOL(mdc_link);
1470 EXPORT_SYMBOL(mdc_readpage);
1471 EXPORT_SYMBOL(mdc_setattr);
1472 EXPORT_SYMBOL(mdc_close);
1473 EXPORT_SYMBOL(mdc_done_writing);
1474 EXPORT_SYMBOL(mdc_sync);
1475 EXPORT_SYMBOL(mdc_set_open_replay_data);
1476 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1477 EXPORT_SYMBOL(mdc_store_inode_generation);
1478
1479 module_init(mdc_init);
1480 module_exit(mdc_exit);
1481 #endif