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