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