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