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