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