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