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