Whamcloud - gitweb
land b_hd_sec onto HEAD:
[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 OBD_IOC_PARSE: {
779                 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
780                                         LLOG_CONFIG_REPL_CTXT);
781                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
782                 GOTO(out, rc);
783         }
784 #ifdef __KERNEL__
785         case OBD_IOC_LLOG_INFO:
786         case OBD_IOC_LLOG_PRINT: {
787                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
788                 rc = llog_ioctl(ctxt, cmd, data);
789
790                 GOTO(out, rc);
791         }
792 #endif
793         default:
794                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
795                 GOTO(out, rc = -ENOTTY);
796         }
797 out:
798 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
799         MOD_DEC_USE_COUNT;
800 #else
801         module_put(THIS_MODULE);
802 #endif
803
804         return rc;
805 }
806
807 int mdc_set_info(struct obd_export *exp, obd_count keylen,
808                  void *key, obd_count vallen, void *val)
809 {
810         int rc = -EINVAL;
811
812         if (keylen == strlen("initial_recov") &&
813             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
814                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
815                 if (vallen != sizeof(int))
816                         RETURN(-EINVAL);
817                 imp->imp_initial_recov = *(int *)val;
818                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
819                        exp->exp_obd->obd_name,
820                        imp->imp_initial_recov);
821                 RETURN(0);
822         } else if (keylen >= strlen("mds_type") && strcmp(key, "mds_type") == 0) {
823                 struct ptlrpc_request *req;
824                 char *bufs[2] = {key, val};
825                 int rc, size[2] = {keylen, vallen};
826
827                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
828                                       OST_SET_INFO, 2, size, bufs);
829                 if (req == NULL)
830                         RETURN(-ENOMEM);
831
832                 req->rq_replen = lustre_msg_size(0, NULL);
833                 rc = ptlrpc_queue_wait(req);
834                 ptlrpc_req_finished(req);
835                 RETURN(rc);
836         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
837                 struct obd_import *imp = class_exp2cliimp(exp);
838                 imp->imp_server_timeout = 1;
839                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
840                 RETURN(0);
841         } else if (keylen == strlen("sec") && memcmp(key, "sec", keylen) == 0) {
842                 struct client_obd *cli = &exp->exp_obd->u.cli;
843
844                 if (vallen == strlen("null") &&
845                     memcmp(val, "null", vallen) == 0) {
846                         cli->cl_sec_flavor = PTLRPC_SEC_NULL;
847                         cli->cl_sec_subflavor = 0;
848                         RETURN(0);
849                 }
850                 if (vallen == strlen("krb5i") &&
851                     memcmp(val, "krb5i", vallen) == 0) {
852                         cli->cl_sec_flavor = PTLRPC_SEC_GSS;
853                         cli->cl_sec_subflavor = PTLRPC_SEC_GSS_KRB5I;
854                         RETURN(0);
855                 }
856                 if (vallen == strlen("krb5p") &&
857                     memcmp(val, "krb5p", vallen) == 0) {
858                         cli->cl_sec_flavor = PTLRPC_SEC_GSS;
859                         cli->cl_sec_subflavor = PTLRPC_SEC_GSS_KRB5P;
860                         RETURN(0);
861                 }
862                 CERROR("unrecognized security type %s\n", (char*) val);
863                 rc = -EINVAL;
864         } else if (keylen == strlen("async") && memcmp(key, "async", keylen) == 0) {
865                 struct client_obd *cl = &exp->exp_obd->u.cli;
866                 if (vallen != sizeof(int))
867                         RETURN(-EINVAL);
868                 cl->cl_async = *(int *)val;
869                 CDEBUG(D_HA, "%s: set async = %d\n",
870                        exp->exp_obd->obd_name, cl->cl_async);
871                 RETURN(0);
872         }
873         RETURN(rc);
874 }
875
876 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
877                       unsigned long max_age)
878 {
879         struct obd_statfs *msfs;
880         struct ptlrpc_request *req;
881         int rc, size = sizeof(*msfs);
882         ENTRY;
883
884         /* We could possibly pass max_age in the request (as an absolute
885          * timestamp or a "seconds.usec ago") so the target can avoid doing
886          * extra calls into the filesystem if that isn't necessary (e.g.
887          * during mount that would help a bit).  Having relative timestamps
888          * is not so great if request processing is slow, while absolute
889          * timestamps are not ideal because they need time synchronization. */
890         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
891                               MDS_STATFS, 0, NULL, NULL);
892         if (!req)
893                 RETURN(-ENOMEM);
894
895         req->rq_replen = lustre_msg_size(1, &size);
896
897         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
898         rc = ptlrpc_queue_wait(req);
899         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
900
901         if (rc) {
902                 /* this can be LMV fake import, whcih is not connected. */
903                 if (!req->rq_import->imp_connection)
904                         memset(osfs, 0, sizeof(*osfs));
905                 GOTO(out, rc);
906         }
907
908         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),
909                                   lustre_swab_obd_statfs);
910         if (msfs == NULL) {
911                 CERROR("Can't unpack obd_statfs\n");
912                 GOTO(out, rc = -EPROTO);
913         }
914
915         memcpy(osfs, msfs, sizeof (*msfs));
916         EXIT;
917 out:
918         ptlrpc_req_finished(req);
919         return rc;
920 }
921
922 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
923                    struct obd_client_handle *handle, int flag)
924 {
925         struct ptlrpc_request *req;
926         struct mds_body *body;
927         int rc, size[2] = {0, sizeof(*body)};
928         ENTRY;
929
930         //size[0] = lustre_secdesc_size();
931
932         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
933                               MDS_PIN, 2, size, NULL);
934         if (req == NULL)
935                 RETURN(-ENOMEM);
936
937         //lustre_pack_secdesc(req, size[0]);
938
939         body = lustre_msg_buf(req->rq_reqmsg, 
940                               MDS_REQ_REC_OFF, sizeof(*body));
941
942         /* FIXME-UMKA: here should be also mdsnum and fid. */
943         mdc_pack_id(&body->id1, ino, gen, type, 0, 0);
944         body->flags = flag;
945
946         req->rq_replen = lustre_msg_size(1, &size[1]);
947
948         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
949         rc = ptlrpc_queue_wait(req);
950         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
951         if (rc) {
952                 CERROR("pin failed: %d\n", rc);
953                 ptlrpc_req_finished(req);
954                 RETURN(rc);
955         }
956
957         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
958         if (body == NULL) {
959                 ptlrpc_req_finished(req);
960                 RETURN(rc);
961         }
962
963         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
964         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
965
966         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
967         if (handle->och_mod == NULL) {
968                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
969                 RETURN(-ENOMEM);
970         }
971         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
972
973         RETURN(rc);
974 }
975
976 static int mdc_unpin(struct obd_export *exp,
977                      struct obd_client_handle *handle, int flag)
978 {
979         struct ptlrpc_request *req;
980         struct mds_body *body;
981         int rc, size[2] = {0, sizeof(*body)};
982         ENTRY;
983
984         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
985                 RETURN(0);
986
987         //size[0] = lustre_secdesc_size();
988
989         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
990                               MDS_CLOSE, 2, size, NULL);
991         if (req == NULL)
992                 RETURN(-ENOMEM);
993
994         //lustre_pack_secdesc(req, size[0]);
995
996         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
997         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
998         body->flags = flag;
999
1000         req->rq_replen = lustre_msg_size(0, NULL);
1001         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1002         rc = ptlrpc_queue_wait(req);
1003         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1004
1005         if (rc != 0)
1006                 CERROR("unpin failed: %d\n", rc);
1007
1008         ptlrpc_req_finished(req);
1009         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1010         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1011         RETURN(rc);
1012 }
1013
1014 int mdc_sync(struct obd_export *exp, struct lustre_id *id,
1015              struct ptlrpc_request **request)
1016 {
1017         struct ptlrpc_request *req;
1018         struct mds_body *body;
1019         int size[2] = {0, sizeof(*body)};
1020         int rc;
1021         ENTRY;
1022
1023         //size[0] = lustre_secdesc_size();
1024
1025         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1026                               MDS_SYNC, 2, size, NULL);
1027         if (!req)
1028                 RETURN(rc = -ENOMEM);
1029
1030         //lustre_pack_secdesc(req, size[0]);
1031
1032         if (id) {
1033                 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1034                                       sizeof (*body));
1035                 memcpy(&body->id1, id, sizeof(*id));
1036         }
1037
1038         req->rq_replen = lustre_msg_size(1, &size[1]);
1039
1040         rc = ptlrpc_queue_wait(req);
1041         if (rc || request == NULL)
1042                 ptlrpc_req_finished(req);
1043         else
1044                 *request = req;
1045
1046         RETURN(rc);
1047 }
1048
1049 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1050                             enum obd_import_event event)
1051 {
1052         int rc = 0;
1053
1054         LASSERT(imp->imp_obd == obd);
1055
1056         switch (event) {
1057         case IMP_EVENT_DISCON: {
1058                 break;
1059         }
1060         case IMP_EVENT_INACTIVE: {
1061                 if (obd->obd_observer)
1062                         rc = obd_notify(obd->obd_observer, obd, 0, 0);
1063                 break;
1064         }
1065         case IMP_EVENT_INVALIDATE: {
1066                 struct ldlm_namespace *ns = obd->obd_namespace;
1067
1068                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1069
1070                 break;
1071         }
1072         case IMP_EVENT_ACTIVE: {
1073                 if (obd->obd_observer)
1074                         rc = obd_notify(obd->obd_observer, obd, 1, 0);
1075                 break;
1076         }
1077         default:
1078                 CERROR("Unknown import event %d\n", event);
1079                 LBUG();
1080         }
1081         RETURN(rc);
1082 }
1083
1084 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1085 {
1086         struct lprocfs_static_vars lvars;
1087
1088         lprocfs_init_vars(mdc, &lvars);
1089         return lprocfs_obd_attach(dev, lvars.obd_vars);
1090 }
1091
1092 static int mdc_detach(struct obd_device *dev)
1093 {
1094         return lprocfs_obd_detach(dev);
1095 }
1096
1097 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1098 {
1099         struct client_obd *cli = &obd->u.cli;
1100         int rc;
1101         ENTRY;
1102
1103         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1104         if (!cli->cl_rpc_lock)
1105                 RETURN(-ENOMEM);
1106         mdc_init_rpc_lock(cli->cl_rpc_lock);
1107
1108         ptlrpcd_addref();
1109
1110         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1111         if (!cli->cl_setattr_lock)
1112                 GOTO(err_rpc_lock, rc = -ENOMEM);
1113         mdc_init_rpc_lock(cli->cl_setattr_lock);
1114
1115         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1116         if (!cli->cl_close_lock)
1117                 GOTO(err_setattr_lock, rc = -ENOMEM);
1118         mdc_init_rpc_lock(cli->cl_close_lock);
1119
1120         rc = client_obd_setup(obd, len, buf);
1121         if (rc)
1122                 GOTO(err_close_lock, rc);
1123
1124         rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1125         if (rc) {
1126                 mdc_cleanup(obd, 0);
1127                 CERROR("failed to setup llogging subsystems\n");
1128         }
1129
1130         RETURN(rc);
1131
1132 err_close_lock:
1133         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1134 err_setattr_lock:
1135         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1136 err_rpc_lock:
1137         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1138         ptlrpcd_decref();
1139         RETURN(rc);
1140 }
1141
1142 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1143 {
1144         struct obd_device *obd = exp->exp_obd;
1145         struct client_obd *cli = &obd->u.cli;
1146         ENTRY;
1147
1148         if (cli->cl_max_mds_easize < easize)
1149                 cli->cl_max_mds_easize = easize;
1150         if (cli->cl_max_mds_cookiesize < cookiesize)
1151                 cli->cl_max_mds_cookiesize = cookiesize;
1152         RETURN(0);
1153 }
1154
1155 static int mdc_precleanup(struct obd_device *obd, int flags)
1156 {
1157         int rc = 0;
1158         
1159         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1160         if (rc != 0)
1161                 CERROR("failed to cleanup llogging subsystems\n");
1162
1163         RETURN(rc);
1164 }
1165
1166 static int mdc_cleanup(struct obd_device *obd, int flags)
1167 {
1168         struct client_obd *cli = &obd->u.cli;
1169
1170         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1171         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1172         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1173
1174         ptlrpcd_decref();
1175
1176         return client_obd_cleanup(obd, flags);
1177 }
1178
1179
1180 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1181                          struct obd_device *tgt, int count,
1182                          struct llog_catid *logid)
1183 {
1184         struct llog_ctxt *ctxt;
1185         int rc;
1186         ENTRY;
1187
1188         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1189                             &llog_client_ops);
1190         if (rc == 0) {
1191                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1192                 ctxt->loc_imp = obd->u.cli.cl_import;
1193         }
1194
1195         RETURN(rc);
1196 }
1197
1198 static int mdc_llog_finish(struct obd_device *obd,
1199                            struct obd_llogs *llogs, int count)
1200 {
1201         int rc;
1202         ENTRY;
1203
1204         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1205         RETURN(rc);
1206 }
1207 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1208                                            struct lustre_id *id)
1209 {
1210        ENTRY;
1211        RETURN(exp->exp_obd);
1212 }
1213
1214 static int mdc_get_info(struct obd_export *exp, obd_count keylen,
1215                         void *key, __u32 *valsize, void *val)
1216 {
1217         struct ptlrpc_request *req;
1218         char *bufs[1] = {key};
1219         int rc = 0;
1220         ENTRY;
1221         
1222         if (!valsize || !val)
1223                 RETURN(-EFAULT);
1224
1225         if (keylen == strlen("remote_flag") && !strcmp(key, "remote_flag")) {
1226                 struct obd_import *imp;
1227                 struct obd_connect_data *data;
1228
1229                 imp = class_exp2cliimp(exp);
1230                 if (!imp) {
1231                         LBUG();
1232                         RETURN(-EINVAL);
1233                 }
1234
1235                 if (imp->imp_state != LUSTRE_IMP_FULL) {
1236                         CERROR("import state not full\n");
1237                         RETURN(-EINVAL);
1238                 }
1239
1240                 data = &imp->imp_connect_data;
1241                 if (data->ocd_connect_flags & OBD_CONNECT_REMOTE) {
1242                         *((int *)val) = 1;
1243                         RETURN(0);
1244                 } else if (data->ocd_connect_flags & OBD_CONNECT_LOCAL) {
1245                         *((int *)val) = 0;
1246                         RETURN(0);
1247                 }
1248                 CERROR("no remote flag set?\n");
1249                 RETURN(-EINVAL);
1250         }
1251
1252         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1253             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1254             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
1255                 RETURN(-EPROTO);
1256                 
1257         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1258                               OST_GET_INFO, 1, &keylen, bufs);
1259         if (req == NULL)
1260                 RETURN(-ENOMEM);
1261
1262         req->rq_replen = lustre_msg_size(1, valsize);
1263         rc = ptlrpc_queue_wait(req);
1264         if (rc)
1265                 GOTO(out_req, rc);
1266
1267         if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
1268                 struct lustre_id *reply;
1269                 
1270                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1271                                            lustre_swab_lustre_id);
1272                 if (reply == NULL) {
1273                         CERROR("Can't unpack %s\n", (char *)key);
1274                         GOTO(out_req, rc = -EPROTO);
1275                 }
1276
1277                 *(struct lustre_id *)val = *reply;
1278         } else {
1279                 __u32 *reply;
1280                 
1281                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1282                                            lustre_swab_generic_32s);
1283                 if (reply == NULL) {
1284                         CERROR("Can't unpack %s\n", (char *)key);
1285                         GOTO(out_req, rc = -EPROTO);
1286                 }
1287                 *((__u32 *)val) = *reply;
1288         }
1289 out_req:
1290         ptlrpc_req_finished(req);
1291         RETURN(rc);
1292 }
1293
1294 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1295                     struct lov_stripe_md **ea, struct obd_trans_info *oti)
1296 {
1297         struct ptlrpc_request *request;
1298         struct ost_body *body;
1299         int rc, size = sizeof(*body);
1300         ENTRY;
1301
1302         LASSERT(oa);
1303
1304         request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1305                                   OST_CREATE, 1, &size, NULL);
1306         if (!request)
1307                 GOTO(out_req, rc = -ENOMEM);
1308
1309         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1310         memcpy(&body->oa, oa, sizeof(body->oa));
1311
1312         request->rq_replen = lustre_msg_size(1, &size);
1313         rc = ptlrpc_queue_wait(request);
1314         if (rc)
1315                 GOTO(out_req, rc);
1316
1317         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1318                                   lustre_swab_ost_body);
1319         if (body == NULL) {
1320                 CERROR ("can't unpack ost_body\n");
1321                 GOTO (out_req, rc = -EPROTO);
1322         }
1323
1324         memcpy(oa, &body->oa, sizeof(*oa));
1325
1326         /* store ino/generation for recovery */
1327         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1328         body->oa.o_id = oa->o_id;
1329         body->oa.o_generation = oa->o_generation;
1330         body->oa.o_fid = oa->o_fid;
1331         body->oa.o_mds = oa->o_mds;
1332
1333         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1334         EXIT;
1335 out_req:
1336         ptlrpc_req_finished(request);
1337         return rc;
1338 }
1339
1340 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1341             struct lov_stripe_md *ea, obd_count oa_bufs,
1342             struct brw_page *pgarr, struct obd_trans_info *oti)
1343 {
1344         struct ptlrpc_bulk_desc *desc;
1345         struct niobuf_remote *niobuf;
1346         struct ptlrpc_request *req;
1347         struct obd_ioobj *ioobj;
1348         struct ost_body *body;
1349         int err, opc, i;
1350         int size[3];
1351
1352         opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1353         
1354         size[0] = sizeof(*body);
1355         size[1] = sizeof(*ioobj);
1356         size[2] = oa_bufs * sizeof(*niobuf);
1357
1358         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1359                               3, size, NULL);
1360         LASSERT(req != NULL);
1361
1362         if (opc == OST_WRITE)
1363                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1364                                             OST_BULK_PORTAL);
1365         else
1366                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1367                                             OST_BULK_PORTAL);
1368         LASSERT(desc != NULL);
1369
1370         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1371         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1372         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1373
1374         memcpy(&body->oa, oa, sizeof(*oa));
1375         obdo_to_ioobj(oa, ioobj);
1376         ioobj->ioo_bufcnt = oa_bufs;
1377
1378         for (i = 0; i < oa_bufs; i++, niobuf++) {
1379                 struct brw_page *pg = &pgarr[i];
1380
1381                 LASSERT(pg->count > 0);
1382                 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1383
1384                 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1385                                       pg->count);
1386
1387                 niobuf->offset = pg->disk_offset;
1388                 niobuf->len = pg->count;
1389                 niobuf->flags = pg->flag;
1390         }
1391
1392         /* size[0] still sizeof (*body) */
1393         if (opc == OST_WRITE) {
1394                 /* 1 RC per niobuf */
1395                 size[1] = sizeof(__u32) * oa_bufs;
1396                 req->rq_replen = lustre_msg_size(2, size);
1397         } else {
1398                 /* 1 RC for the whole I/O */
1399                 req->rq_replen = lustre_msg_size(1, size);
1400         }
1401         err = ptlrpc_queue_wait(req);
1402         LASSERT(err == 0);
1403
1404         ptlrpc_req_finished(req);
1405         return 0;
1406 }
1407
1408 static int mdc_valid_attrs(struct obd_export *exp,
1409                            struct lustre_id *id)
1410 {
1411         struct ldlm_res_id res_id = { .name = {0} };
1412         struct obd_device *obd = exp->exp_obd;
1413         struct lustre_handle lockh;
1414         ldlm_policy_data_t policy;
1415         int flags;
1416         ENTRY;
1417
1418         res_id.name[0] = id_fid(id);
1419         res_id.name[1] = id_group(id);
1420         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1421
1422         CDEBUG(D_INFO, "trying to match res "LPU64"\n",
1423                res_id.name[0]);
1424
1425         /* FIXME use LDLM_FL_TEST_LOCK instead */
1426         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1427         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1428                             LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1429                 ldlm_lock_decref(&lockh, LCK_PR);
1430                 RETURN(1);
1431         }
1432
1433         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1434                             LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1435                 ldlm_lock_decref(&lockh, LCK_PW);
1436                 RETURN(1);
1437         }
1438         RETURN(0);
1439 }
1440
1441 static int mdc_change_cbdata_name(struct obd_export *exp,
1442                                   struct lustre_id *pid,
1443                                   char *name, int len,
1444                                   struct lustre_id *cid,
1445                                   ldlm_iterator_t it, void *data)
1446 {
1447         int rc;
1448         rc = mdc_change_cbdata(exp, cid, it, data);
1449         RETURN(rc);
1450 }
1451
1452 struct obd_ops mdc_obd_ops = {
1453         .o_owner         = THIS_MODULE,
1454         .o_attach        = mdc_attach,
1455         .o_detach        = mdc_detach,
1456         .o_setup         = mdc_setup,
1457         .o_precleanup    = mdc_precleanup,
1458         .o_cleanup       = mdc_cleanup,
1459         .o_add_conn      = client_import_add_conn,
1460         .o_del_conn      = client_import_del_conn,
1461         .o_connect       = client_connect_import,
1462         .o_disconnect    = client_disconnect_export,
1463         .o_iocontrol     = mdc_iocontrol,
1464         .o_packmd        = mdc_packmd,
1465         .o_unpackmd      = mdc_unpackmd,
1466         .o_statfs        = mdc_statfs,
1467         .o_pin           = mdc_pin,
1468         .o_unpin         = mdc_unpin,
1469         .o_import_event  = mdc_import_event,
1470         .o_llog_init     = mdc_llog_init,
1471         .o_llog_finish   = mdc_llog_finish,
1472         .o_create        = mdc_obj_create,
1473         .o_set_info      = mdc_set_info,
1474         .o_get_info      = mdc_get_info,
1475         .o_brw           = mdc_brw,
1476         .o_init_ea_size  = mdc_init_ea_size,
1477 };
1478
1479 struct md_ops mdc_md_ops = {
1480         .m_getstatus     = mdc_getstatus,
1481         .m_getattr       = mdc_getattr,
1482         .m_close         = mdc_close,
1483         .m_create        = mdc_create,
1484         .m_done_writing  = mdc_done_writing,
1485         .m_enqueue       = mdc_enqueue,
1486         .m_getattr_lock  = mdc_getattr_lock,
1487         .m_intent_lock   = mdc_intent_lock,
1488         .m_link          = mdc_link,
1489         .m_rename        = mdc_rename,
1490         .m_setattr       = mdc_setattr,
1491         .m_sync          = mdc_sync,
1492         .m_readpage      = mdc_readpage,
1493         .m_unlink        = mdc_unlink,
1494         .m_valid_attrs   = mdc_valid_attrs,
1495         .m_req2lustre_md = mdc_req2lustre_md,
1496         .m_set_open_replay_data   = mdc_set_open_replay_data,
1497         .m_clear_open_replay_data = mdc_clear_open_replay_data,
1498         .m_store_inode_generation = mdc_store_inode_generation,
1499         .m_set_lock_data = mdc_set_lock_data,
1500         .m_get_real_obd  = mdc_get_real_obd,
1501         .m_change_cbdata_name = mdc_change_cbdata_name,
1502         .m_change_cbdata = mdc_change_cbdata,
1503 };
1504
1505 int __init mdc_init(void)
1506 {
1507         struct lprocfs_static_vars lvars;
1508         lprocfs_init_vars(mdc, &lvars);
1509         return class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
1510                                    LUSTRE_MDC_NAME);
1511 }
1512
1513 #ifdef __KERNEL__
1514 static void /*__exit*/ mdc_exit(void)
1515 {
1516         class_unregister_type(LUSTRE_MDC_NAME);
1517 }
1518
1519 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1520 MODULE_DESCRIPTION("Lustre Metadata Client");
1521 MODULE_LICENSE("GPL");
1522
1523 EXPORT_SYMBOL(mdc_req2lustre_md);
1524 EXPORT_SYMBOL(mdc_change_cbdata);
1525 EXPORT_SYMBOL(mdc_getstatus);
1526 EXPORT_SYMBOL(mdc_getattr);
1527 EXPORT_SYMBOL(mdc_getattr_lock);
1528 EXPORT_SYMBOL(mdc_create);
1529 EXPORT_SYMBOL(mdc_unlink);
1530 EXPORT_SYMBOL(mdc_rename);
1531 EXPORT_SYMBOL(mdc_link);
1532 EXPORT_SYMBOL(mdc_readpage);
1533 EXPORT_SYMBOL(mdc_setattr);
1534 EXPORT_SYMBOL(mdc_close);
1535 EXPORT_SYMBOL(mdc_done_writing);
1536 EXPORT_SYMBOL(mdc_sync);
1537 EXPORT_SYMBOL(mdc_set_open_replay_data);
1538 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1539 EXPORT_SYMBOL(mdc_store_inode_generation);
1540
1541 module_init(mdc_init);
1542 module_exit(mdc_exit);
1543 #endif