Whamcloud - gitweb
- cleanups in cmobd and others:
[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         LASSERT(atomic_read(&req->rq_refcount) > 1);
400         ptlrpc_req_finished(req);
401 }
402
403 static void mdc_replay_open(struct ptlrpc_request *req)
404 {
405         struct mdc_open_data *mod = req->rq_cb_data;
406         struct obd_client_handle *och;
407         struct ptlrpc_request *close_req;
408         struct lustre_handle old;
409         struct mds_body *body;
410         ENTRY;
411
412         body = lustre_swab_repbuf(req, 1, sizeof(*body), lustre_swab_mds_body);
413         LASSERT (body != NULL);
414
415         if (mod == NULL) {
416                 DEBUG_REQ(D_ERROR, req,
417                           "can't properly replay without open data");
418                 EXIT;
419                 return;
420         }
421
422         och = mod->mod_och;
423         if (och != NULL) {
424                 struct lustre_handle *file_fh;
425                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
426                 file_fh = &och->och_fh;
427                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
428                        file_fh->cookie, body->handle.cookie);
429                 memcpy(&old, file_fh, sizeof(old));
430                 memcpy(file_fh, &body->handle, sizeof(*file_fh));
431         }
432
433         close_req = mod->mod_close_req;
434         if (close_req != NULL) {
435                 struct mds_body *close_body;
436                 LASSERT(close_req->rq_reqmsg->opc == MDS_CLOSE);
437                 close_body = lustre_msg_buf(close_req->rq_reqmsg,
438                                             MDS_REQ_REC_OFF,
439                                             sizeof(*close_body));
440                 if (och != NULL)
441                         LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
442                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
443                 memcpy(&close_body->handle, &body->handle,
444                        sizeof(close_body->handle));
445         }
446
447         EXIT;
448 }
449
450 int mdc_set_open_replay_data(struct obd_export *exp,
451                              struct obd_client_handle *och,
452                              struct ptlrpc_request *open_req)
453 {
454         struct mdc_open_data *mod;
455         struct mds_rec_create *rec;
456         struct mds_body *body;
457
458         rec = lustre_msg_buf(open_req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
459                              sizeof(*rec));
460         body = lustre_msg_buf(open_req->rq_repmsg, 1, sizeof(*body));
461
462         LASSERT(rec != NULL);
463         /* outgoing messages always in my byte order */
464         LASSERT(body != NULL);
465         /* incoming message in my byte order (it's been swabbed) */
466         LASSERT_REPSWABBED(open_req, 1);
467
468         OBD_ALLOC(mod, sizeof(*mod));
469         if (mod == NULL) {
470                 DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
471                 return 0;
472         }
473
474         och->och_mod = mod;
475         mod->mod_och = och;
476         mod->mod_open_req = ptlrpc_request_addref(open_req);
477
478         memcpy(&rec->cr_replayid, &body->id1, sizeof rec->cr_replayid);
479         open_req->rq_replay_cb = mdc_replay_open;
480         open_req->rq_commit_cb = mdc_commit_open;
481         open_req->rq_cb_data = mod;
482         DEBUG_REQ(D_HA, open_req, "set up replay data");
483         return 0;
484 }
485
486 int mdc_clear_open_replay_data(struct obd_export *exp,
487                                struct obd_client_handle *och)
488 {
489         struct mdc_open_data *mod = och->och_mod;
490
491         /* Don't free the structure now (it happens in mdc_commit_open, after
492          * we're sure we won't need to fix up the close request in the future),
493          * but make sure that replay doesn't poke at the och, which is about to
494          * be freed. */
495         LASSERT(mod != LP_POISON);
496         if (mod != NULL)
497                 mod->mod_och = NULL;
498         och->och_mod = NULL;
499         return 0;
500 }
501
502 static void mdc_commit_close(struct ptlrpc_request *req)
503 {
504         struct mdc_open_data *mod = req->rq_cb_data;
505         struct ptlrpc_request *open_req;
506         struct obd_import *imp = req->rq_import;
507
508         DEBUG_REQ(D_HA, req, "close req committed");
509         if (mod == NULL)
510                 return;
511
512         mod->mod_close_req = NULL;
513         req->rq_cb_data = NULL;
514         req->rq_commit_cb = NULL;
515
516         open_req = mod->mod_open_req;
517         LASSERT(open_req != NULL);
518         LASSERT(open_req != LP_POISON);
519         LASSERT(open_req->rq_type != LI_POISON);
520
521         DEBUG_REQ(D_HA, open_req, "open req balanced");
522         if (open_req->rq_transno == 0) {
523                 DEBUG_REQ(D_ERROR, open_req, "BUG 3892  open");
524                 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
525                 LASSERTF(open_req->rq_transno != 0, "BUG 3892");
526         }
527         LASSERT(open_req->rq_import == imp);
528
529         /* We no longer want to preserve this for transno-unconditional
530          * replay. */
531         spin_lock(&open_req->rq_lock);
532         open_req->rq_replay = 0;
533         spin_unlock(&open_req->rq_lock);
534 }
535
536 int mdc_close(struct obd_export *exp, struct obdo *oa,
537               struct obd_client_handle *och,
538               struct ptlrpc_request **request)
539 {
540         struct obd_device *obd = class_exp2obd(exp);
541         struct obd_import *imp = class_exp2cliimp(exp);
542         int reqsize[3] = {0, sizeof(struct mds_body),
543                           obd->u.cli.cl_max_mds_cookiesize};
544         int rc, repsize[3] = {sizeof(struct mds_body),
545                               obd->u.cli.cl_max_mds_easize,
546                               obd->u.cli.cl_max_mds_cookiesize};
547         struct ptlrpc_request *req;
548         struct mdc_open_data *mod;
549         ENTRY;
550
551         if (imp->imp_connection == NULL) {
552                 CERROR("request on not connected import %s\n",
553                         imp->imp_obd->obd_name);
554                 RETURN(-EIO);
555         }
556
557         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
558                               MDS_CLOSE, 3, reqsize, NULL);
559         if (req == NULL)
560                 GOTO(out, rc = -ENOMEM);
561
562         //reqsize[0] = lustre_secdesc_size();
563         //lustre_pack_secdesc(req, reqsize[0]);
564
565         /* Ensure that this close's handle is fixed up during replay. */
566         LASSERT(och != NULL);
567         mod = och->och_mod;
568         if (likely(mod != NULL)) {
569                 mod->mod_close_req = req;
570                 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
571                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
572         } else {
573                 CDEBUG(D_HA, "couldn't find open req; "
574                        "expecting close error\n");
575         }
576
577         mdc_close_pack(req, 1, oa, oa->o_valid, och);
578
579         req->rq_replen = lustre_msg_size(3, repsize);
580         req->rq_commit_cb = mdc_commit_close;
581         LASSERT(req->rq_cb_data == NULL);
582         req->rq_cb_data = mod;
583
584         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
585         rc = ptlrpc_queue_wait(req);
586         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
587
588         if (req->rq_repmsg == NULL) {
589                 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
590                        req->rq_status);
591                 if (rc == 0)
592                         rc = req->rq_status ? req->rq_status : -EIO;
593         } else if (rc == 0) {
594                 rc = req->rq_repmsg->status;
595                 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
596                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, "
597                                   "err = %d", rc);
598                         if (rc > 0)
599                                 rc = -rc;
600                 } else {
601                         if (mod == NULL)
602                                 CERROR("Unexpected: can't find mdc_open_data, but "
603                                        "close succeeded. Please tell CFS.\n");
604                         if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
605                                                 lustre_swab_mds_body))
606                         {
607                                 CERROR("Error unpacking mds_body\n");
608                                 rc = -EPROTO;
609                         }
610                 }
611         }
612
613         EXIT;
614  out:
615         *request = req;
616         return rc;
617 }
618
619 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
620 {
621         struct ptlrpc_request *req;
622         struct mds_body *body;
623         int rc, size[2] = {0, sizeof(*body)};
624         ENTRY;
625
626         size[0] = lustre_secdesc_size();
627
628         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
629                               MDS_DONE_WRITING, 2, size, NULL);
630         if (req == NULL)
631                 RETURN(-ENOMEM);
632
633         lustre_pack_secdesc(req, size[0]);
634
635         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, 
636                               sizeof(*body));
637         
638         mdc_pack_id(&body->id1, obdo->o_id, 0, obdo->o_mode, 
639                     obdo->o_mds, obdo->o_fid);
640         
641         body->size = obdo->o_size;
642         body->blocks = obdo->o_blocks;
643         body->flags = obdo->o_flags;
644         body->valid = obdo->o_valid;
645
646         req->rq_replen = lustre_msg_size(1, &size[1]);
647
648         rc = ptlrpc_queue_wait(req);
649         ptlrpc_req_finished(req);
650         RETURN(rc);
651 }
652
653 int mdc_readpage(struct obd_export *exp,
654                  struct lustre_id *id,
655                  __u64 offset, struct page *page,
656                  struct ptlrpc_request **request)
657 {
658         struct obd_import *imp = class_exp2cliimp(exp);
659         struct ptlrpc_request *req = NULL;
660         struct ptlrpc_bulk_desc *desc = NULL;
661         struct mds_body *body;
662         int rc, size[2] = {0, sizeof(*body)};
663         ENTRY;
664
665         CDEBUG(D_INODE, "inode: %ld\n", (long)id->li_stc.u.e3s.l3s_ino);
666
667         size[0] = lustre_secdesc_size();
668
669         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
670                               2, size, NULL);
671         if (req == NULL)
672                 GOTO(out, rc = -ENOMEM);
673         /* XXX FIXME bug 249 */
674         req->rq_request_portal = MDS_READPAGE_PORTAL;
675
676         lustre_pack_secdesc(req, size[0]);
677
678         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
679         if (desc == NULL)
680                 GOTO(out, rc = -ENOMEM);
681         /* NB req now owns desc and will free it when it gets freed */
682
683         ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
684         mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, id);
685
686         req->rq_replen = lustre_msg_size(1, &size[1]);
687         rc = ptlrpc_queue_wait(req);
688
689         if (rc == 0) {
690                 body = lustre_swab_repbuf(req, 0, sizeof (*body),
691                                           lustre_swab_mds_body);
692                 if (body == NULL) {
693                         CERROR("Can't unpack mds_body\n");
694                         GOTO(out, rc = -EPROTO);
695                 }
696
697                 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
698                         CERROR ("Unexpected # bytes transferred: %d"
699                                 " (%ld expected)\n",
700                                 req->rq_bulk->bd_nob_transferred,
701                                 PAGE_CACHE_SIZE);
702                         GOTO (out, rc = -EPROTO);
703                 }
704         }
705
706         EXIT;
707  out:
708         *request = req;
709         return rc;
710 }
711
712 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
713                          void *karg, void *uarg)
714 {
715         struct obd_device *obd = exp->exp_obd;
716         struct obd_ioctl_data *data = karg;
717         struct obd_import *imp = obd->u.cli.cl_import;
718         struct llog_ctxt *ctxt;
719         int rc;
720         ENTRY;
721
722 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
723         MOD_INC_USE_COUNT;
724 #else
725         if (!try_module_get(THIS_MODULE)) {
726                 CERROR("Can't get module. Is it alive?");
727                 return -EINVAL;
728         }
729 #endif
730         switch (cmd) {
731         case OBD_IOC_CLIENT_RECOVER:
732                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
733                 if (rc < 0)
734                         GOTO(out, rc);
735                 GOTO(out, rc = 0);
736         case IOC_OSC_SET_ACTIVE:
737                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
738                 GOTO(out, rc);
739         case IOC_OSC_CTL_RECOVERY:
740                 rc = ptlrpc_import_control_recovery(imp, data->ioc_offset);
741                 GOTO(out, rc);
742         case OBD_IOC_PARSE: {
743                 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
744                                         LLOG_CONFIG_REPL_CTXT);
745                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
746                 GOTO(out, rc);
747         }
748 #ifdef __KERNEL__
749         case OBD_IOC_LLOG_INFO:
750         case OBD_IOC_LLOG_PRINT: {
751                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
752                 rc = llog_ioctl(ctxt, cmd, data);
753
754                 GOTO(out, rc);
755         }
756 #endif
757         default:
758                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
759                 GOTO(out, rc = -ENOTTY);
760         }
761 out:
762 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
763         MOD_DEC_USE_COUNT;
764 #else
765         module_put(THIS_MODULE);
766 #endif
767
768         return rc;
769 }
770
771 int mdc_set_info(struct obd_export *exp, obd_count keylen,
772                  void *key, obd_count vallen, void *val)
773 {
774         int rc = -EINVAL;
775
776         if (keylen == strlen("initial_recov") &&
777             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
778                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
779                 if (vallen != sizeof(int))
780                         RETURN(-EINVAL);
781                 imp->imp_initial_recov = *(int *)val;
782                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
783                        exp->exp_obd->obd_name,
784                        imp->imp_initial_recov);
785                 RETURN(0);
786         } else if (keylen >= strlen("mds_type") &&
787                    strcmp(key, "mds_type") == 0) {
788                 struct ptlrpc_request *req;
789                 char *bufs[2] = {key, val};
790                 int rc, size[2] = {keylen, vallen};
791
792                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
793                                       OST_SET_INFO, 2, size, bufs);
794                 if (req == NULL)
795                         RETURN(-ENOMEM);
796
797                 req->rq_replen = lustre_msg_size(0, NULL);
798                 rc = ptlrpc_queue_wait(req);
799                 ptlrpc_req_finished(req);
800                 RETURN(rc);
801         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
802                 struct obd_import *imp = class_exp2cliimp(exp);
803                 imp->imp_server_timeout = 1;
804                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
805                 RETURN(0);
806         } else if (keylen == strlen("sec") &&
807                    memcmp(key, "sec", keylen) == 0) {
808                 struct client_obd *cli = &exp->exp_obd->u.cli;
809
810                 cli->cl_sec_flavor = ptlrpcs_name2flavor(val);
811                 if (cli->cl_sec_flavor == PTLRPCS_FLVR_INVALID) {
812                         CERROR("unrecognized security type %s\n", (char*) val);
813                         RETURN(-EINVAL);
814                 }
815
816                 RETURN(0);
817         } else if (keylen == strlen("sec_flags") &&
818                    memcmp(key, "sec_flags", keylen) == 0) {
819                 struct client_obd *cli = &exp->exp_obd->u.cli;
820
821                 cli->cl_sec_flags = *((unsigned long *) val);
822                 RETURN(0);
823         } else if (keylen == strlen("flush_cred") &&
824                    memcmp(key, "flush_cred", keylen) == 0) {
825                 struct client_obd *cli = &exp->exp_obd->u.cli;
826
827                 if (cli->cl_import)
828                         ptlrpcs_import_flush_creds(cli->cl_import,
829                                                    *((uid_t *) val));
830                 RETURN(0);
831         } else if (keylen == strlen("async") && memcmp(key, "async", keylen) == 0) {
832                 struct client_obd *cl = &exp->exp_obd->u.cli;
833                 if (vallen != sizeof(int))
834                         RETURN(-EINVAL);
835                 cl->cl_async = *(int *)val;
836                 CDEBUG(D_HA, "%s: set async = %d\n",
837                        exp->exp_obd->obd_name, cl->cl_async);
838                 RETURN(0);
839         }
840         RETURN(rc);
841 }
842
843 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
844                       unsigned long max_age)
845 {
846         struct obd_statfs *msfs;
847         struct ptlrpc_request *req;
848         int rc, size = sizeof(*msfs);
849         ENTRY;
850
851         /* We could possibly pass max_age in the request (as an absolute
852          * timestamp or a "seconds.usec ago") so the target can avoid doing
853          * extra calls into the filesystem if that isn't necessary (e.g.
854          * during mount that would help a bit).  Having relative timestamps
855          * is not so great if request processing is slow, while absolute
856          * timestamps are not ideal because they need time synchronization. */
857         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
858                               MDS_STATFS, 0, NULL, NULL);
859         if (!req)
860                 RETURN(-ENOMEM);
861
862         req->rq_replen = lustre_msg_size(1, &size);
863
864         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
865         rc = ptlrpc_queue_wait(req);
866         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
867
868         if (rc) {
869                 /* this can be LMV fake import, whcih is not connected. */
870                 if (!req->rq_import->imp_connection)
871                         memset(osfs, 0, sizeof(*osfs));
872                 GOTO(out, rc);
873         }
874
875         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),
876                                   lustre_swab_obd_statfs);
877         if (msfs == NULL) {
878                 CERROR("Can't unpack obd_statfs\n");
879                 GOTO(out, rc = -EPROTO);
880         }
881
882         memcpy(osfs, msfs, sizeof (*msfs));
883         EXIT;
884 out:
885         ptlrpc_req_finished(req);
886         return rc;
887 }
888
889 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
890                    struct obd_client_handle *handle, int flag)
891 {
892         struct ptlrpc_request *req;
893         struct mds_body *body;
894         int rc, size[2] = {0, sizeof(*body)};
895         ENTRY;
896
897         //size[0] = lustre_secdesc_size();
898
899         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
900                               MDS_PIN, 2, size, NULL);
901         if (req == NULL)
902                 RETURN(-ENOMEM);
903
904         //lustre_pack_secdesc(req, size[0]);
905
906         body = lustre_msg_buf(req->rq_reqmsg, 
907                               MDS_REQ_REC_OFF, sizeof(*body));
908
909         /* FIXME-UMKA: here should be also mdsnum and fid. */
910         mdc_pack_id(&body->id1, ino, gen, type, 0, 0);
911         body->flags = flag;
912
913         req->rq_replen = lustre_msg_size(1, &size[1]);
914
915         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
916         rc = ptlrpc_queue_wait(req);
917         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
918         if (rc) {
919                 CERROR("pin failed: %d\n", rc);
920                 ptlrpc_req_finished(req);
921                 RETURN(rc);
922         }
923
924         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
925         if (body == NULL) {
926                 ptlrpc_req_finished(req);
927                 RETURN(rc);
928         }
929
930         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
931         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
932
933         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
934         if (handle->och_mod == NULL) {
935                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
936                 RETURN(-ENOMEM);
937         }
938         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
939
940         RETURN(rc);
941 }
942
943 static int mdc_unpin(struct obd_export *exp,
944                      struct obd_client_handle *handle, int flag)
945 {
946         struct ptlrpc_request *req;
947         struct mds_body *body;
948         int rc, size[2] = {0, sizeof(*body)};
949         ENTRY;
950
951         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
952                 RETURN(0);
953
954         //size[0] = lustre_secdesc_size();
955
956         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
957                               MDS_CLOSE, 2, size, NULL);
958         if (req == NULL)
959                 RETURN(-ENOMEM);
960
961         //lustre_pack_secdesc(req, size[0]);
962
963         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
964         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
965         body->flags = flag;
966
967         req->rq_replen = lustre_msg_size(0, NULL);
968         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
969         rc = ptlrpc_queue_wait(req);
970         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
971
972         if (rc != 0)
973                 CERROR("unpin failed: %d\n", rc);
974
975         ptlrpc_req_finished(req);
976         ptlrpc_req_finished(handle->och_mod->mod_open_req);
977         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
978         RETURN(rc);
979 }
980
981 int mdc_sync(struct obd_export *exp, struct lustre_id *id,
982              struct ptlrpc_request **request)
983 {
984         struct ptlrpc_request *req;
985         struct mds_body *body;
986         int size[2] = {0, sizeof(*body)};
987         int rc;
988         ENTRY;
989
990         //size[0] = lustre_secdesc_size();
991
992         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
993                               MDS_SYNC, 2, size, NULL);
994         if (!req)
995                 RETURN(rc = -ENOMEM);
996
997         //lustre_pack_secdesc(req, size[0]);
998
999         if (id) {
1000                 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1001                                       sizeof (*body));
1002                 memcpy(&body->id1, id, sizeof(*id));
1003         }
1004
1005         req->rq_replen = lustre_msg_size(1, &size[1]);
1006
1007         rc = ptlrpc_queue_wait(req);
1008         if (rc || request == NULL)
1009                 ptlrpc_req_finished(req);
1010         else
1011                 *request = req;
1012
1013         RETURN(rc);
1014 }
1015
1016 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1017                             enum obd_import_event event)
1018 {
1019         int rc = 0;
1020
1021         LASSERT(imp->imp_obd == obd);
1022
1023         switch (event) {
1024         case IMP_EVENT_DISCON: {
1025                 break;
1026         }
1027         case IMP_EVENT_INACTIVE: {
1028                 if (obd->obd_observer)
1029                         rc = obd_notify(obd->obd_observer, obd, 0, 0);
1030                 break;
1031         }
1032         case IMP_EVENT_INVALIDATE: {
1033                 struct ldlm_namespace *ns = obd->obd_namespace;
1034
1035                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1036
1037                 break;
1038         }
1039         case IMP_EVENT_ACTIVE: {
1040                 if (obd->obd_observer)
1041                         rc = obd_notify(obd->obd_observer, obd, 1, 0);
1042                 break;
1043         }
1044         default:
1045                 CERROR("Unknown import event %d\n", event);
1046                 LBUG();
1047         }
1048         RETURN(rc);
1049 }
1050
1051 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1052 {
1053         struct lprocfs_static_vars lvars;
1054
1055         lprocfs_init_vars(mdc, &lvars);
1056         return lprocfs_obd_attach(dev, lvars.obd_vars);
1057 }
1058
1059 static int mdc_detach(struct obd_device *dev)
1060 {
1061         return lprocfs_obd_detach(dev);
1062 }
1063
1064 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1065 {
1066         struct client_obd *cli = &obd->u.cli;
1067         int rc;
1068         ENTRY;
1069
1070         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1071         if (!cli->cl_rpc_lock)
1072                 RETURN(-ENOMEM);
1073         mdc_init_rpc_lock(cli->cl_rpc_lock);
1074
1075         ptlrpcd_addref();
1076
1077         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1078         if (!cli->cl_setattr_lock)
1079                 GOTO(err_rpc_lock, rc = -ENOMEM);
1080         mdc_init_rpc_lock(cli->cl_setattr_lock);
1081
1082         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1083         if (!cli->cl_close_lock)
1084                 GOTO(err_setattr_lock, rc = -ENOMEM);
1085         mdc_init_rpc_lock(cli->cl_close_lock);
1086
1087         rc = client_obd_setup(obd, len, buf);
1088         if (rc)
1089                 GOTO(err_close_lock, rc);
1090
1091         rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1092         if (rc) {
1093                 mdc_cleanup(obd, 0);
1094                 CERROR("failed to setup llogging subsystems\n");
1095         }
1096
1097         RETURN(rc);
1098
1099 err_close_lock:
1100         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1101 err_setattr_lock:
1102         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1103 err_rpc_lock:
1104         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1105         ptlrpcd_decref();
1106         RETURN(rc);
1107 }
1108
1109 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1110 {
1111         struct obd_device *obd = exp->exp_obd;
1112         struct client_obd *cli = &obd->u.cli;
1113         ENTRY;
1114
1115         if (cli->cl_max_mds_easize < easize)
1116                 cli->cl_max_mds_easize = easize;
1117         if (cli->cl_max_mds_cookiesize < cookiesize)
1118                 cli->cl_max_mds_cookiesize = cookiesize;
1119         RETURN(0);
1120 }
1121
1122 static int mdc_precleanup(struct obd_device *obd, int flags)
1123 {
1124         int rc = 0;
1125         
1126         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1127         if (rc != 0)
1128                 CERROR("failed to cleanup llogging subsystems\n");
1129
1130         RETURN(rc);
1131 }
1132
1133 static int mdc_cleanup(struct obd_device *obd, int flags)
1134 {
1135         struct client_obd *cli = &obd->u.cli;
1136
1137         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1138         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1139         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_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, __u32 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("remote_flag") && !strcmp(key, "remote_flag")) {
1193                 struct obd_import *imp;
1194                 struct obd_connect_data *data;
1195
1196                 imp = class_exp2cliimp(exp);
1197                 if (!imp) {
1198                         LBUG();
1199                         RETURN(-EINVAL);
1200                 }
1201
1202                 if (imp->imp_state != LUSTRE_IMP_FULL) {
1203                         CERROR("import state not full\n");
1204                         RETURN(-EINVAL);
1205                 }
1206
1207                 data = &imp->imp_connect_data;
1208                 if (data->ocd_connect_flags & OBD_CONNECT_REMOTE) {
1209                         *((int *)val) = 1;
1210                         RETURN(0);
1211                 } else if (data->ocd_connect_flags & OBD_CONNECT_LOCAL) {
1212                         *((int *)val) = 0;
1213                         RETURN(0);
1214                 }
1215                 CERROR("no remote flag set?\n");
1216                 RETURN(-EINVAL);
1217         }
1218
1219         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1220             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1221             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
1222                 RETURN(-EPROTO);
1223                 
1224         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1225                               OST_GET_INFO, 1, (int *)&keylen, bufs);
1226         if (req == NULL)
1227                 RETURN(-ENOMEM);
1228
1229         req->rq_replen = lustre_msg_size(1, (int *)valsize);
1230         rc = ptlrpc_queue_wait(req);
1231         if (rc)
1232                 GOTO(out_req, rc);
1233
1234         if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
1235                 struct lustre_id *reply;
1236                 
1237                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1238                                            lustre_swab_lustre_id);
1239                 if (reply == NULL) {
1240                         CERROR("Can't unpack %s\n", (char *)key);
1241                         GOTO(out_req, rc = -EPROTO);
1242                 }
1243
1244                 *(struct lustre_id *)val = *reply;
1245         } else {
1246                 __u32 *reply;
1247                 
1248                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1249                                            lustre_swab_generic_32s);
1250                 if (reply == NULL) {
1251                         CERROR("Can't unpack %s\n", (char *)key);
1252                         GOTO(out_req, rc = -EPROTO);
1253                 }
1254                 *((__u32 *)val) = *reply;
1255         }
1256 out_req:
1257         ptlrpc_req_finished(req);
1258         RETURN(rc);
1259 }
1260
1261 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1262                    void *acl, int acl_size,
1263                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
1264 {
1265         struct ptlrpc_request *request;
1266         struct ost_body *body;
1267         char *acl_buf;
1268         int rc, size[2] = { sizeof(*body), acl_size };
1269         ENTRY;
1270
1271         LASSERT(oa);
1272
1273         request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1274                                   OST_CREATE, 2, size, NULL);
1275         if (!request)
1276                 GOTO(out_req, rc = -ENOMEM);
1277
1278         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1279         memcpy(&body->oa, oa, sizeof(body->oa));
1280
1281         if (acl_size) {
1282                 acl_buf = lustre_msg_buf(request->rq_reqmsg, 1, acl_size);
1283                 memcpy(acl_buf, acl, acl_size);
1284         }
1285
1286         request->rq_replen = lustre_msg_size(1, size);
1287         rc = ptlrpc_queue_wait(request);
1288         if (rc)
1289                 GOTO(out_req, rc);
1290
1291         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1292                                   lustre_swab_ost_body);
1293         if (body == NULL) {
1294                 CERROR ("can't unpack ost_body\n");
1295                 GOTO (out_req, rc = -EPROTO);
1296         }
1297
1298         memcpy(oa, &body->oa, sizeof(*oa));
1299
1300         /* store ino/generation for recovery */
1301         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1302         body->oa.o_id = oa->o_id;
1303         body->oa.o_generation = oa->o_generation;
1304         body->oa.o_fid = oa->o_fid;
1305         body->oa.o_mds = oa->o_mds;
1306
1307         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1308         EXIT;
1309 out_req:
1310         ptlrpc_req_finished(request);
1311         return rc;
1312 }
1313
1314 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1315             struct lov_stripe_md *ea, obd_count oa_bufs,
1316             struct brw_page *pgarr, struct obd_trans_info *oti)
1317 {
1318         struct ptlrpc_bulk_desc *desc;
1319         struct niobuf_remote *niobuf;
1320         struct ptlrpc_request *req;
1321         struct obd_ioobj *ioobj;
1322         struct ost_body *body;
1323         int err, opc, i;
1324         int size[3];
1325
1326         opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1327         
1328         size[0] = sizeof(*body);
1329         size[1] = sizeof(*ioobj);
1330         size[2] = oa_bufs * sizeof(*niobuf);
1331
1332         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1333                               3, size, NULL);
1334         LASSERT(req != NULL);
1335
1336         if (opc == OST_WRITE)
1337                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1338                                             OST_BULK_PORTAL);
1339         else
1340                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1341                                             OST_BULK_PORTAL);
1342         LASSERT(desc != NULL);
1343
1344         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1345         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1346         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1347
1348         memcpy(&body->oa, oa, sizeof(*oa));
1349         obdo_to_ioobj(oa, ioobj);
1350         ioobj->ioo_bufcnt = oa_bufs;
1351
1352         for (i = 0; i < oa_bufs; i++, niobuf++) {
1353                 struct brw_page *pg = &pgarr[i];
1354
1355                 LASSERT(pg->count > 0);
1356                 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1357
1358                 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1359                                       pg->count);
1360
1361                 niobuf->offset = pg->disk_offset;
1362                 niobuf->len = pg->count;
1363                 niobuf->flags = pg->flag;
1364         }
1365
1366         /* size[0] still sizeof (*body) */
1367         if (opc == OST_WRITE) {
1368                 /* 1 RC per niobuf */
1369                 size[1] = sizeof(__u32) * oa_bufs;
1370                 req->rq_replen = lustre_msg_size(2, size);
1371         } else {
1372                 /* 1 RC for the whole I/O */
1373                 req->rq_replen = lustre_msg_size(1, size);
1374         }
1375         err = ptlrpc_queue_wait(req);
1376         LASSERT(err == 0);
1377
1378         ptlrpc_req_finished(req);
1379         return 0;
1380 }
1381
1382 static int mdc_valid_attrs(struct obd_export *exp,
1383                            struct lustre_id *id)
1384 {
1385         struct ldlm_res_id res_id = { .name = {0} };
1386         struct obd_device *obd = exp->exp_obd;
1387         struct lustre_handle lockh;
1388         ldlm_policy_data_t policy;
1389         int flags;
1390         ENTRY;
1391
1392         res_id.name[0] = id_fid(id);
1393         res_id.name[1] = id_group(id);
1394         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1395
1396         CDEBUG(D_INFO, "trying to match res "LPU64"\n",
1397                res_id.name[0]);
1398
1399         /* FIXME use LDLM_FL_TEST_LOCK instead */
1400         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1401         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1402                             LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1403                 ldlm_lock_decref(&lockh, LCK_PR);
1404                 RETURN(1);
1405         }
1406
1407         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1408                             LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1409                 ldlm_lock_decref(&lockh, LCK_PW);
1410                 RETURN(1);
1411         }
1412         RETURN(0);
1413 }
1414
1415 static int mdc_change_cbdata_name(struct obd_export *exp,
1416                                   struct lustre_id *pid,
1417                                   char *name, int len,
1418                                   struct lustre_id *cid,
1419                                   ldlm_iterator_t it, void *data)
1420 {
1421         int rc;
1422         rc = mdc_change_cbdata(exp, cid, it, data);
1423         RETURN(rc);
1424 }
1425
1426 struct obd_ops mdc_obd_ops = {
1427         .o_owner         = THIS_MODULE,
1428         .o_attach        = mdc_attach,
1429         .o_detach        = mdc_detach,
1430         .o_setup         = mdc_setup,
1431         .o_precleanup    = mdc_precleanup,
1432         .o_cleanup       = mdc_cleanup,
1433         .o_add_conn      = client_import_add_conn,
1434         .o_del_conn      = client_import_del_conn,
1435         .o_connect       = client_connect_import,
1436         .o_disconnect    = client_disconnect_export,
1437         .o_iocontrol     = mdc_iocontrol,
1438         .o_packmd        = mdc_packmd,
1439         .o_unpackmd      = mdc_unpackmd,
1440         .o_statfs        = mdc_statfs,
1441         .o_pin           = mdc_pin,
1442         .o_unpin         = mdc_unpin,
1443         .o_import_event  = mdc_import_event,
1444         .o_llog_init     = mdc_llog_init,
1445         .o_llog_finish   = mdc_llog_finish,
1446         .o_create        = mdc_obj_create,
1447         .o_set_info      = mdc_set_info,
1448         .o_get_info      = mdc_get_info,
1449         .o_brw           = mdc_brw,
1450         .o_cancel_unused = mdc_cancel_unused,
1451         .o_init_ea_size  = mdc_init_ea_size,
1452 };
1453
1454 struct md_ops mdc_md_ops = {
1455         .m_getstatus     = mdc_getstatus,
1456         .m_getattr       = mdc_getattr,
1457         .m_close         = mdc_close,
1458         .m_create        = mdc_create,
1459         .m_done_writing  = mdc_done_writing,
1460         .m_enqueue       = mdc_enqueue,
1461         .m_getattr_lock  = mdc_getattr_lock,
1462         .m_intent_lock   = mdc_intent_lock,
1463         .m_link          = mdc_link,
1464         .m_rename        = mdc_rename,
1465         .m_setattr       = mdc_setattr,
1466         .m_sync          = mdc_sync,
1467         .m_readpage      = mdc_readpage,
1468         .m_unlink        = mdc_unlink,
1469         .m_valid_attrs   = mdc_valid_attrs,
1470         .m_req2lustre_md = mdc_req2lustre_md,
1471         .m_set_open_replay_data   = mdc_set_open_replay_data,
1472         .m_clear_open_replay_data = mdc_clear_open_replay_data,
1473         .m_store_inode_generation = mdc_store_inode_generation,
1474         .m_set_lock_data = mdc_set_lock_data,
1475         .m_get_real_obd  = mdc_get_real_obd,
1476         .m_change_cbdata_name = mdc_change_cbdata_name,
1477         .m_change_cbdata = mdc_change_cbdata,
1478 };
1479
1480 int __init mdc_init(void)
1481 {
1482         struct lprocfs_static_vars lvars;
1483         
1484         lprocfs_init_vars(mdc, &lvars);
1485         return class_register_type(&mdc_obd_ops, &mdc_md_ops,
1486                                    lvars.module_vars, OBD_MDC_DEVICENAME);
1487 }
1488
1489 #ifdef __KERNEL__
1490 static void /*__exit*/ mdc_exit(void)
1491 {
1492         class_unregister_type(OBD_MDC_DEVICENAME);
1493 }
1494
1495 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1496 MODULE_DESCRIPTION("Lustre Metadata Client");
1497 MODULE_LICENSE("GPL");
1498
1499 EXPORT_SYMBOL(mdc_req2lustre_md);
1500 EXPORT_SYMBOL(mdc_change_cbdata);
1501 EXPORT_SYMBOL(mdc_getstatus);
1502 EXPORT_SYMBOL(mdc_getattr);
1503 EXPORT_SYMBOL(mdc_getattr_lock);
1504 EXPORT_SYMBOL(mdc_create);
1505 EXPORT_SYMBOL(mdc_unlink);
1506 EXPORT_SYMBOL(mdc_rename);
1507 EXPORT_SYMBOL(mdc_link);
1508 EXPORT_SYMBOL(mdc_readpage);
1509 EXPORT_SYMBOL(mdc_setattr);
1510 EXPORT_SYMBOL(mdc_close);
1511 EXPORT_SYMBOL(mdc_done_writing);
1512 EXPORT_SYMBOL(mdc_sync);
1513 EXPORT_SYMBOL(mdc_set_open_replay_data);
1514 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1515 EXPORT_SYMBOL(mdc_store_inode_generation);
1516
1517 module_init(mdc_init);
1518 module_exit(mdc_exit);
1519 #endif